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 34 2e 20 20 42 ersion 3.6.4. B
00b0: 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c 20 y combining all
00c0: 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 43 the individual C
00d0: 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74 6f code files into
00e0: 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c 65 this .** single
00f0: 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68 65 large file, the
0100: 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61 6e entire code can
0110: 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73 20 be compiled as
0120: 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69 6f a one translatio
0130: 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69 73 n.** unit. This
0140: 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f 6d allows many com
0150: 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70 74 pilers to do opt
0160: 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74 20 imizations that
0170: 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 would not be.**
0180: 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65 20 possible if the
0190: 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70 69 files were compi
01a0: 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 20 led separately.
01b0: 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70 Performance imp
01c0: 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 rovements.** of
01d0: 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65 20 5% are more are
01e0: 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77 68 commonly seen wh
01f0: 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d en SQLite is com
0200: 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67 6c piled as a singl
0210: 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e e.** translation
0220: 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 unit..**.** Thi
0230: 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79 6f s file is all yo
0240: 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69 6c u need to compil
0250: 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75 73 e SQLite. To us
0260: 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68 65 e SQLite in othe
0270: 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20 79 r.** programs, y
0280: 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69 6c ou need this fil
0290: 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69 74 e and the "sqlit
02a0: 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69 6c e3.h" header fil
02b0: 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a 2a e that defines.*
02c0: 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69 6e * the programmin
02d0: 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 g interface to t
02e0: 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
02f0: 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20 6e y. (If you do n
0300: 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65 20 ot have .** the
0310: 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61 64 "sqlite3.h" head
0320: 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64 2c er file at hand,
0330: 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20 61 you will find a
0340: 20 63 6f 70 79 20 69 6e 20 74 68 65 20 66 69 72 copy in the fir
0350: 73 74 0a 2a 2a 20 36 35 36 39 20 6c 69 6e 65 73 st.** 6569 lines
0360: 20 70 61 73 74 20 74 68 69 73 20 68 65 61 64 65 past this heade
0370: 72 20 63 6f 6d 6d 65 6e 74 2e 29 20 20 41 64 64 r comment.) Add
0380: 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20 66 69 6c itional code fil
0390: 65 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6e 65 65 es may be.** nee
03a0: 64 65 64 20 69 66 20 79 6f 75 20 77 61 6e 74 20 ded if you want
03b0: 61 20 77 72 61 70 70 65 72 20 74 6f 20 69 6e 74 a wrapper to int
03c0: 65 72 66 61 63 65 20 53 51 4c 69 74 65 20 77 69 erface SQLite wi
03d0: 74 68 20 79 6f 75 72 20 63 68 6f 69 63 65 20 6f th your choice o
03e0: 66 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 69 6e 67 f.** programming
03f0: 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 20 language. The
0400: 63 6f 64 65 20 66 6f 72 20 74 68 65 20 22 73 71 code for the "sq
0410: 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e 64 2d 6c lite3" command-l
0420: 69 6e 65 20 73 68 65 6c 6c 0a 2a 2a 20 69 73 20 ine shell.** is
0430: 61 6c 73 6f 20 69 6e 20 61 20 73 65 70 61 72 61 also in a separa
0440: 74 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 66 te file. This f
0450: 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c ile contains onl
0460: 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 63 y code for the c
0470: 6f 72 65 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 ore.** SQLite li
0480: 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 brary..**.** Thi
0490: 73 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77 s amalgamation w
04a0: 61 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e 20 as generated on
04b0: 32 30 30 38 2d 31 31 2d 31 30 20 30 30 3a 31 34 2008-11-10 00:14
04c0: 3a 33 36 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 :36 UTC..*/.#def
04d0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 ine SQLITE_CORE
04e0: 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
04f0: 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a _AMALGAMATION 1.
0500: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 #ifndef SQLITE_P
0510: 52 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 RIVATE.# define
0520: 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
0530: 74 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 tatic.#endif.#if
0540: 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a ndef SQLITE_API.
0550: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
0560: 41 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a API.#endif./****
0570: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
0580: 20 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e file sqliteInt.
0590: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
05c0: 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
05d0: 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
05e0: 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
05f0: 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
0600: 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
0610: 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
0620: 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
0630: 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
0640: 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
0650: 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
0660: 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
0670: 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
0680: 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
0690: 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
06a0: 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
06b0: 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
06c0: 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
06d0: 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
06e0: 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
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 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e *.** Internal in
0740: 74 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 terface definiti
0750: 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a ons for SQLite..
0760: 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
0770: 73 71 6c 69 74 65 49 6e 74 2e 68 2c 76 20 31 2e sqliteInt.h,v 1.
0780: 37 38 38 20 32 30 30 38 2f 31 31 2f 30 35 20 31 788 2008/11/05 1
0790: 36 3a 33 37 3a 33 35 20 64 72 68 20 45 78 70 20 6:37:35 drh Exp
07a0: 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 $.*/.#ifndef _SQ
07b0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 LITEINT_H_.#defi
07c0: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f ne _SQLITEINT_H_
07d0: 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 ../*.** Include
07e0: 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f the configuratio
07f0: 6e 20 68 65 61 64 65 72 20 6f 75 74 70 75 74 20 n header output
0800: 62 79 20 27 63 6f 6e 66 69 67 75 72 65 27 20 69 by 'configure' i
0810: 66 20 77 65 27 72 65 20 75 73 69 6e 67 20 74 68 f we're using th
0820: 65 0a 2a 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 e.** autoconf-ba
0830: 73 65 64 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 sed build.*/.#if
0840: 64 65 66 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 def _HAVE_SQLITE
0850: 5f 43 4f 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 _CONFIG_H.#inclu
0860: 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 de "config.h".#e
0870: 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
0880: 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 ***** Include sq
0890: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 liteLimit.h in t
08a0: 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
08b0: 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
08c0: 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
08d0: 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
08e0: 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a sqliteLimit.h *
08f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0910: 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
0920: 20 4d 61 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 May 7.**.** The
0930: 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
0940: 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
0950: 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
0960: 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
0970: 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
0980: 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
0990: 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
09a0: 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
09b0: 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
09c0: 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
09d0: 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
09e0: 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
09f0: 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
0a00: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
0a10: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
0a20: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
0a30: 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
0a40: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a80: 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 *****.** .** Thi
0a90: 73 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 76 s file defines v
0aa0: 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 arious limits of
0ab0: 20 77 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e what SQLite can
0ac0: 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 process..**.**
0ad0: 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65 @(#) $Id: sqlite
0ae0: 4c 69 6d 69 74 2e 68 2c 76 20 31 2e 38 20 32 30 Limit.h,v 1.8 20
0af0: 30 38 2f 30 33 2f 32 36 20 31 35 3a 35 36 3a 32 08/03/26 15:56:2
0b00: 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2 drh Exp $.*/..
0b10: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 /*.** The maximu
0b20: 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 54 45 m length of a TE
0b30: 58 54 20 6f 72 20 42 4c 4f 42 20 69 6e 20 62 79 XT or BLOB in by
0b40: 74 65 73 2e 20 20 20 54 68 69 73 20 61 6c 73 6f tes. This also
0b50: 0a 2a 2a 20 6c 69 6d 69 74 73 20 74 68 65 20 73 .** limits the s
0b60: 69 7a 65 20 6f 66 20 61 20 72 6f 77 20 69 6e 20 ize of a row in
0b70: 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 a table or index
0b80: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 72 64 ..**.** The hard
0b90: 20 6c 69 6d 69 74 20 69 73 20 74 68 65 20 61 62 limit is the ab
0ba0: 69 6c 69 74 79 20 6f 66 20 61 20 33 32 2d 62 69 ility of a 32-bi
0bb0: 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
0bc0: 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74 20 74 68 65 .** to count the
0bd0: 20 73 69 7a 65 3a 20 32 5e 33 31 2d 31 20 6f 72 size: 2^31-1 or
0be0: 20 32 31 34 37 34 38 33 36 34 37 2e 0a 2a 2f 0a 2147483647..*/.
0bf0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
0c00: 41 58 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 AX_LENGTH.# defi
0c10: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 ne SQLITE_MAX_LE
0c20: 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 30 0a NGTH 1000000000.
0c30: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
0c40: 69 73 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 is is the maximu
0c50: 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 0a 2a m number of.**.*
0c60: 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 * * Columns i
0c70: 6e 20 61 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 n a table.**
0c80: 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 * Columns in an
0c90: 69 6e 64 65 78 0a 2a 2a 20 20 20 20 2a 20 43 6f index.** * Co
0ca0: 6c 75 6d 6e 73 20 69 6e 20 61 20 76 69 65 77 0a lumns in a view.
0cb0: 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e ** * Terms in
0cc0: 20 74 68 65 20 53 45 54 20 63 6c 61 75 73 65 20 the SET clause
0cd0: 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 of an UPDATE sta
0ce0: 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 tement.** * T
0cf0: 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73 75 erms in the resu
0d00: 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 lt set of a SELE
0d10: 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 CT statement.**
0d20: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 * Terms in th
0d30: 65 20 47 52 4f 55 50 20 42 59 20 6f 72 20 4f 52 e GROUP BY or OR
0d40: 44 45 52 20 42 59 20 63 6c 61 75 73 65 73 20 6f DER BY clauses o
0d50: 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 f a SELECT state
0d60: 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20 2a 20 54 65 ment..** * Te
0d70: 72 6d 73 20 69 6e 20 74 68 65 20 56 41 4c 55 45 rms in the VALUE
0d80: 53 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 49 S clause of an I
0d90: 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 0a NSERT statement.
0da0: 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 72 64 20 75 **.** The hard u
0db0: 70 70 65 72 20 6c 69 6d 69 74 20 68 65 72 65 20 pper limit here
0dc0: 69 73 20 33 32 36 37 36 2e 20 20 4d 6f 73 74 20 is 32676. Most
0dd0: 64 61 74 61 62 61 73 65 20 70 65 6f 70 6c 65 20 database people
0de0: 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c 20 79 6f 75 will.** tell you
0df0: 20 74 68 61 74 20 69 6e 20 61 20 77 65 6c 6c 2d that in a well-
0e00: 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 61 74 61 62 normalized datab
0e10: 61 73 65 2c 20 79 6f 75 20 75 73 75 61 6c 6c 79 ase, you usually
0e20: 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 68 should.** not h
0e30: 61 76 65 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 ave more than a
0e40: 64 6f 7a 65 6e 20 6f 72 20 73 6f 20 63 6f 6c 75 dozen or so colu
0e50: 6d 6e 73 20 69 6e 20 61 6e 79 20 74 61 62 6c 65 mns in any table
0e60: 2e 20 20 41 6e 64 20 69 66 0a 2a 2a 20 74 68 61 . And if.** tha
0e70: 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 t is the case, t
0e80: 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 here is no point
0e90: 20 69 6e 20 68 61 76 69 6e 67 20 6d 6f 72 65 20 in having more
0ea0: 74 68 61 6e 20 61 20 66 65 77 0a 2a 2a 20 64 6f than a few.** do
0eb0: 7a 65 6e 20 76 61 6c 75 65 73 20 69 6e 20 61 6e zen values in an
0ec0: 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 73 y of the other s
0ed0: 69 74 75 61 74 69 6f 6e 73 20 64 65 73 63 72 69 ituations descri
0ee0: 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 bed above..*/.#i
0ef0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
0f00: 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65 66 69 6e 65 _COLUMN.# define
0f10: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 SQLITE_MAX_COLU
0f20: 4d 4e 20 32 30 30 30 0a 23 65 6e 64 69 66 0a 0a MN 2000.#endif..
0f30: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 /*.** The maximu
0f40: 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73 69 m length of a si
0f50: 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 ngle SQL stateme
0f60: 6e 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a nt in bytes..**.
0f70: 2a 2a 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 ** It used to be
0f80: 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 73 the case that s
0f90: 65 74 74 69 6e 67 20 74 68 69 73 20 76 61 6c 75 etting this valu
0fa0: 65 20 74 6f 20 7a 65 72 6f 20 77 6f 75 6c 64 0a e to zero would.
0fb0: 2a 2a 20 74 75 72 6e 20 74 68 65 20 6c 69 6d 69 ** turn the limi
0fc0: 74 20 6f 66 66 2e 20 20 54 68 61 74 20 69 73 20 t off. That is
0fd0: 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 65 2e 20 no longer true.
0fe0: 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 It is not possi
0ff0: 62 6c 65 0a 2a 2a 20 74 6f 20 74 75 72 6e 20 74 ble.** to turn t
1000: 68 69 73 20 6c 69 6d 69 74 20 6f 66 66 2e 0a 2a his limit off..*
1010: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1020: 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 0a _MAX_SQL_LENGTH.
1030: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1040: 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 31 MAX_SQL_LENGTH 1
1050: 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 66 000000000.#endif
1060: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 ../*.** The maxi
1070: 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 mum depth of an
1080: 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e expression tree.
1090: 20 54 68 69 73 20 69 73 20 6c 69 6d 69 74 65 64 This is limited
10a0: 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65 20 65 78 74 to .** some ext
10b0: 65 6e 74 20 62 79 20 53 51 4c 49 54 45 5f 4d 41 ent by SQLITE_MA
10c0: 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2e 20 42 75 X_SQL_LENGTH. Bu
10d0: 74 20 73 6f 6d 65 74 69 6d 65 20 79 6f 75 20 6d t sometime you m
10e0: 69 67 68 74 20 0a 2a 2a 20 77 61 6e 74 20 74 6f ight .** want to
10f0: 20 70 6c 61 63 65 20 6d 6f 72 65 20 73 65 76 65 place more seve
1100: 72 65 20 6c 69 6d 69 74 73 20 6f 6e 20 74 68 65 re limits on the
1110: 20 63 6f 6d 70 6c 65 78 69 74 79 20 6f 66 20 61 complexity of a
1120: 6e 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e n .** expression
1130: 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20 ..**.** A value
1140: 6f 66 20 30 20 75 73 65 64 20 74 6f 20 6d 65 61 of 0 used to mea
1150: 6e 20 74 68 61 74 20 74 68 65 20 6c 69 6d 69 74 n that the limit
1160: 20 77 61 73 20 6e 6f 74 20 65 6e 66 6f 72 63 65 was not enforce
1170: 64 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74 20 69 d..** But that i
1180: 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 65 s no longer true
1190: 2e 20 20 54 68 65 20 6c 69 6d 69 74 20 69 73 20 . The limit is
11a0: 6e 6f 77 20 73 74 72 69 63 74 6c 79 20 65 6e 66 now strictly enf
11b0: 6f 72 63 65 64 0a 2a 2a 20 61 74 20 61 6c 6c 20 orced.** at all
11c0: 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 times..*/.#ifnde
11d0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 f SQLITE_MAX_EXP
11e0: 52 5f 44 45 50 54 48 0a 23 20 64 65 66 69 6e 65 R_DEPTH.# define
11f0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 SQLITE_MAX_EXPR
1200: 5f 44 45 50 54 48 20 31 30 30 30 0a 23 65 6e 64 _DEPTH 1000.#end
1210: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
1220: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
1230: 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f terms in a compo
1240: 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 und SELECT state
1250: 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 63 6f 64 ment..** The cod
1260: 65 20 67 65 6e 65 72 61 74 6f 72 20 66 6f 72 20 e generator for
1270: 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 compound SELECT
1280: 73 74 61 74 65 6d 65 6e 74 73 20 64 6f 65 73 20 statements does
1290: 6f 6e 65 0a 2a 2a 20 6c 65 76 65 6c 20 6f 66 20 one.** level of
12a0: 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 65 61 recursion for ea
12b0: 63 68 20 74 65 72 6d 2e 20 20 41 20 73 74 61 63 ch term. A stac
12c0: 6b 20 6f 76 65 72 66 6c 6f 77 20 63 61 6e 20 72 k overflow can r
12d0: 65 73 75 6c 74 0a 2a 2a 20 69 66 20 74 68 65 20 esult.** if the
12e0: 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 number of terms
12f0: 69 73 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 49 is too large. I
1300: 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 6f 73 74 n practice, most
1310: 20 53 51 4c 0a 2a 2a 20 6e 65 76 65 72 20 68 61 SQL.** never ha
1320: 73 20 6d 6f 72 65 20 74 68 61 6e 20 33 20 6f 72 s more than 3 or
1330: 20 34 20 74 65 72 6d 73 2e 20 20 55 73 65 20 61 4 terms. Use a
1340: 20 76 61 6c 75 65 20 6f 66 20 30 20 74 6f 20 64 value of 0 to d
1350: 69 73 61 62 6c 65 0a 2a 2a 20 61 6e 79 20 6c 69 isable.** any li
1360: 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 mit on the numbe
1370: 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 r of terms in a
1380: 63 6f 6d 70 6f 75 6e 74 20 53 45 4c 45 43 54 2e compount SELECT.
1390: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
13a0: 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f TE_MAX_COMPOUND_
13b0: 53 45 4c 45 43 54 0a 23 20 64 65 66 69 6e 65 20 SELECT.# define
13c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f SQLITE_MAX_COMPO
13d0: 55 4e 44 5f 53 45 4c 45 43 54 20 35 30 30 0a 23 UND_SELECT 500.#
13e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
13f0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1400: 6f 66 20 6f 70 63 6f 64 65 73 20 69 6e 20 61 20 of opcodes in a
1410: 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a VDBE program..**
1420: 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 Not currently e
1430: 6e 66 6f 72 63 65 64 2e 0a 2a 2f 0a 23 69 66 6e nforced..*/.#ifn
1440: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 def SQLITE_MAX_V
1450: 44 42 45 5f 4f 50 0a 23 20 64 65 66 69 6e 65 20 DBE_OP.# define
1460: 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f SQLITE_MAX_VDBE_
1470: 4f 50 20 32 35 30 30 30 0a 23 65 6e 64 69 66 0a OP 25000.#endif.
1480: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
1490: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 um number of arg
14a0: 75 6d 65 6e 74 73 20 74 6f 20 61 6e 20 53 51 4c uments to an SQL
14b0: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 function..*/.#i
14c0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
14d0: 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 0a 23 20 _FUNCTION_ARG.#
14e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
14f0: 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 31 X_FUNCTION_ARG 1
1500: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1510: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
1520: 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 ber of in-memory
1530: 20 70 61 67 65 73 20 74 6f 20 75 73 65 20 66 6f pages to use fo
1540: 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 r the main datab
1550: 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 ase.** table and
1560: 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 for temporary t
1570: 61 62 6c 65 73 2e 20 20 54 68 65 20 53 51 4c 49 ables. The SQLI
1580: 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 TE_DEFAULT_CACHE
1590: 5f 53 49 5a 45 0a 2a 2f 0a 23 69 66 6e 64 65 66 _SIZE.*/.#ifndef
15a0: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
15b0: 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 CACHE_SIZE.# def
15c0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ine SQLITE_DEFAU
15d0: 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 32 LT_CACHE_SIZE 2
15e0: 30 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 000.#endif.#ifnd
15f0: 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ef SQLITE_DEFAUL
1600: 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a T_TEMP_CACHE_SIZ
1610: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
1620: 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 E_DEFAULT_TEMP_C
1630: 41 43 48 45 5f 53 49 5a 45 20 20 35 30 30 0a 23 ACHE_SIZE 500.#
1640: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
1650: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1660: 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 74 61 of attached data
1670: 62 61 73 65 73 2e 20 20 54 68 69 73 20 6d 75 73 bases. This mus
1680: 74 20 62 65 20 62 65 74 77 65 65 6e 20 30 0a 2a t be between 0.*
1690: 2a 20 61 6e 64 20 33 30 2e 20 20 54 68 65 20 75 * and 30. The u
16a0: 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 33 30 pper bound on 30
16b0: 20 69 73 20 62 65 63 61 75 73 65 20 61 20 33 32 is because a 32
16c0: 2d 62 69 74 20 69 6e 74 65 67 65 72 20 62 69 74 -bit integer bit
16d0: 6d 61 70 0a 2a 2a 20 69 73 20 75 73 65 64 20 69 map.** is used i
16e0: 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 74 72 61 nternally to tra
16f0: 63 6b 20 61 74 74 61 63 68 65 64 20 64 61 74 61 ck attached data
1700: 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 bases..*/.#ifnde
1710: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 f SQLITE_MAX_ATT
1720: 41 43 48 45 44 0a 23 20 64 65 66 69 6e 65 20 53 ACHED.# define S
1730: 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 QLITE_MAX_ATTACH
1740: 45 44 20 31 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f ED 10.#endif.../
1750: 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d *.** The maximum
1760: 20 76 61 6c 75 65 20 6f 66 20 61 20 3f 6e 6e 6e value of a ?nnn
1770: 20 77 69 6c 64 63 61 72 64 20 74 68 61 74 20 74 wildcard that t
1780: 68 65 20 70 61 72 73 65 72 20 77 69 6c 6c 20 61 he parser will a
1790: 63 63 65 70 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ccept..*/.#ifnde
17a0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 f SQLITE_MAX_VAR
17b0: 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a 23 20 64 IABLE_NUMBER.# d
17c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 efine SQLITE_MAX
17d0: 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
17e0: 20 39 39 39 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 999.#endif../*
17f0: 4d 61 78 69 6d 75 6d 20 70 61 67 65 20 73 69 7a Maximum page siz
1800: 65 2e 20 20 54 68 65 20 75 70 70 65 72 20 62 6f e. The upper bo
1810: 75 6e 64 20 6f 6e 20 74 68 69 73 20 76 61 6c 75 und on this valu
1820: 65 20 69 73 20 33 32 37 36 38 2e 20 20 54 68 69 e is 32768. Thi
1830: 73 20 61 20 6c 69 6d 69 74 0a 2a 2a 20 69 6d 70 s a limit.** imp
1840: 6f 73 65 64 20 62 79 20 74 68 65 20 6e 65 63 65 osed by the nece
1850: 73 73 69 74 79 20 6f 66 20 73 74 6f 72 69 6e 67 ssity of storing
1860: 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 61 20 the value in a
1870: 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 2-byte unsigned
1880: 69 6e 74 65 67 65 72 0a 2a 2a 20 61 6e 64 20 74 integer.** and t
1890: 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 he fact that the
18a0: 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 page size must
18b0: 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e be a power of 2.
18c0: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
18d0: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
18e0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
18f0: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 33 _MAX_PAGE_SIZE 3
1900: 32 37 36 38 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 2768.#endif.../*
1910: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 .** The default
1920: 73 69 7a 65 20 6f 66 20 61 20 64 61 74 61 62 61 size of a databa
1930: 73 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e se page..*/.#ifn
1940: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
1950: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 LT_PAGE_SIZE.# d
1960: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
1970: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 31 AULT_PAGE_SIZE 1
1980: 30 32 34 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 024.#endif.#if S
1990: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
19a0: 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d GE_SIZE>SQLITE_M
19b0: 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 75 AX_PAGE_SIZE.# u
19c0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
19d0: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 ULT_PAGE_SIZE.#
19e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
19f0: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 FAULT_PAGE_SIZE
1a00: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1a10: 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a SIZE.#endif../*.
1a20: 2a 2a 20 4f 72 64 69 6e 61 72 69 6c 79 2c 20 69 ** Ordinarily, i
1a30: 66 20 6e 6f 20 76 61 6c 75 65 20 69 73 20 65 78 f no value is ex
1a40: 70 6c 69 63 69 74 6c 79 20 70 72 6f 76 69 64 65 plicitly provide
1a50: 64 2c 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 d, SQLite create
1a60: 73 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 77 s databases.** w
1a70: 69 74 68 20 70 61 67 65 20 73 69 7a 65 20 53 51 ith page size SQ
1a80: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
1a90: 45 5f 53 49 5a 45 2e 20 48 6f 77 65 76 65 72 2c E_SIZE. However,
1aa0: 20 62 61 73 65 64 20 6f 6e 20 63 65 72 74 61 69 based on certai
1ab0: 6e 0a 2a 2a 20 64 65 76 69 63 65 20 63 68 61 72 n.** device char
1ac0: 61 63 74 65 72 69 73 74 69 63 73 20 28 73 65 63 acteristics (sec
1ad0: 74 6f 72 2d 73 69 7a 65 20 61 6e 64 20 61 74 6f tor-size and ato
1ae0: 6d 69 63 20 77 72 69 74 65 28 29 20 73 75 70 70 mic write() supp
1af0: 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 ort),.** SQLite
1b00: 6d 61 79 20 63 68 6f 6f 73 65 20 61 20 6c 61 72 may choose a lar
1b10: 67 65 72 20 76 61 6c 75 65 2e 20 54 68 69 73 20 ger value. This
1b20: 63 6f 6e 73 74 61 6e 74 20 69 73 20 74 68 65 20 constant is the
1b30: 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 0a 2a 2a maximum value.**
1b40: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 63 68 6f SQLite will cho
1b50: 6f 73 65 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 0a ose on its own..
1b60: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1b70: 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
1b80: 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 GE_SIZE.# define
1b90: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 SQLITE_MAX_DEFA
1ba0: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 38 31 ULT_PAGE_SIZE 81
1bb0: 39 32 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 92.#endif.#if SQ
1bc0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
1bd0: 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 _PAGE_SIZE>SQLIT
1be0: 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a E_MAX_PAGE_SIZE.
1bf0: 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d # undef SQLITE_M
1c00: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
1c10: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
1c20: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
1c30: 5f 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 _PAGE_SIZE SQLIT
1c40: 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a E_MAX_PAGE_SIZE.
1c50: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d #endif.../*.** M
1c60: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
1c70: 20 70 61 67 65 73 20 69 6e 20 6f 6e 65 20 64 61 pages in one da
1c80: 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1c90: 2a 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c ** This is reall
1ca0: 79 20 6a 75 73 74 20 74 68 65 20 64 65 66 61 75 y just the defau
1cb0: 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
1cc0: 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 max_page_count
1cd0: 70 72 61 67 6d 61 2e 0a 2a 2a 20 54 68 69 73 20 pragma..** This
1ce0: 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6c 6f 77 value can be low
1cf0: 65 72 65 64 20 28 6f 72 20 72 61 69 73 65 64 29 ered (or raised)
1d00: 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69 at run-time usi
1d10: 6e 67 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 6d ng that the.** m
1d20: 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 6d 61 ax_page_count ma
1d30: 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 cro..*/.#ifndef
1d40: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1d50: 43 4f 55 4e 54 0a 23 20 64 65 66 69 6e 65 20 53 COUNT.# define S
1d60: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 QLITE_MAX_PAGE_C
1d70: 4f 55 4e 54 20 31 30 37 33 37 34 31 38 32 33 0a OUNT 1073741823.
1d80: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
1d90: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69 6e ximum length (in
1da0: 20 62 79 74 65 73 29 20 6f 66 20 74 68 65 20 70 bytes) of the p
1db0: 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b 45 attern in a LIKE
1dc0: 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65 72 or GLOB.** oper
1dd0: 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 ator..*/.#ifndef
1de0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 SQLITE_MAX_LIKE
1df0: 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0a _PATTERN_LENGTH.
1e00: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1e10: 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e MAX_LIKE_PATTERN
1e20: 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a 23 65 _LENGTH 50000.#e
1e30: 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
1e40: 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c ***** End of sql
1e50: 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a iteLimit.h *****
1e60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e80: 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1e90: 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
1ea0: 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
1eb0: 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
1ec0: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
1ed0: 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73 61 62 6c ****/../* Disabl
1ee0: 65 20 6e 75 69 73 61 6e 63 65 20 77 61 72 6e 69 e nuisance warni
1ef0: 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e 64 20 63 ngs on Borland c
1f00: 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23 69 66 20 ompilers */.#if
1f10: 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e defined(__BORLAN
1f20: 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61 20 77 61 DC__).#pragma wa
1f30: 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e 72 65 61 rn -rch /* unrea
1f40: 63 68 61 62 6c 65 20 63 6f 64 65 20 2a 2f 0a 23 chable code */.#
1f50: 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 63 63 pragma warn -ccc
1f60: 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 69 73 /* Condition is
1f70: 20 61 6c 77 61 79 73 20 74 72 75 65 20 6f 72 20 always true or
1f80: 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61 67 6d 61 false */.#pragma
1f90: 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a 20 41 73 warn -aus /* As
1fa0: 73 69 67 6e 65 64 20 76 61 6c 75 65 20 69 73 20 signed value is
1fb0: 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 70 never used */.#p
1fc0: 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 73 75 20 ragma warn -csu
1fd0: 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20 73 69 67 /* Comparing sig
1fe0: 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 6e 65 64 ned and unsigned
1ff0: 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e */.#pragma warn
2000: 20 2d 73 70 61 20 2f 2a 20 53 75 73 70 69 63 6f -spa /* Suspico
2010: 75 73 20 70 6f 69 6e 74 65 72 20 61 72 69 74 68 us pointer arith
2020: 6d 65 74 69 63 20 2a 2f 0a 23 65 6e 64 69 66 0a metic */.#endif.
2030: 0a 2f 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 76 ./* Needed for v
2040: 61 72 69 6f 75 73 20 64 65 66 69 6e 69 74 69 6f arious definitio
2050: 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 ns... */.#ifndef
2060: 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 20 64 _GNU_SOURCE.# d
2070: 65 66 69 6e 65 20 5f 47 4e 55 5f 53 4f 55 52 43 efine _GNU_SOURC
2080: 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 E.#endif../*.**
2090: 49 6e 63 6c 75 64 65 20 73 74 61 6e 64 61 72 64 Include standard
20a0: 20 68 65 61 64 65 72 20 66 69 6c 65 73 20 61 73 header files as
20b0: 20 6e 65 63 65 73 73 61 72 79 0a 2a 2f 0a 23 69 necessary.*/.#i
20c0: 66 64 65 66 20 48 41 56 45 5f 53 54 44 49 4e 54 fdef HAVE_STDINT
20d0: 5f 48 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 _H.#include <std
20e0: 69 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 0a 23 69 int.h>.#endif.#i
20f0: 66 64 65 66 20 48 41 56 45 5f 49 4e 54 54 59 50 fdef HAVE_INTTYP
2100: 45 53 5f 48 0a 23 69 6e 63 6c 75 64 65 20 3c 69 ES_H.#include <i
2110: 6e 74 74 79 70 65 73 2e 68 3e 0a 23 65 6e 64 69 nttypes.h>.#endi
2120: 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f f../*.** A macro
2130: 20 75 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20 used to aid in
2140: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 coverage testing
2150: 2e 20 20 57 68 65 6e 20 64 6f 69 6e 67 20 63 6f . When doing co
2160: 76 65 72 61 67 65 0a 2a 2a 20 74 65 73 74 69 6e verage.** testin
2170: 67 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e g, the condition
2180: 20 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 inside the argu
2190: 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 65 76 61 ment must be eva
21a0: 6c 75 61 74 65 64 20 0a 2a 2a 20 62 6f 74 68 20 luated .** both
21b0: 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 69 true and false i
21c0: 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 66 n order to get f
21d0: 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 ull branch cover
21e0: 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 6d 61 63 age..** This mac
21f0: 72 6f 20 63 61 6e 20 62 65 20 69 6e 73 65 72 74 ro can be insert
2200: 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61 64 65 ed to ensure ade
2210: 71 75 61 74 65 20 74 65 73 74 20 63 6f 76 65 72 quate test cover
2220: 61 67 65 0a 2a 2a 20 69 6e 20 70 6c 61 63 65 73 age.** in places
2230: 20 77 68 65 72 65 20 73 69 6d 70 6c 65 20 63 6f where simple co
2240: 6e 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e ndition/decision
2250: 20 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e 61 coverage is ina
2260: 64 65 71 75 61 74 65 2e 0a 2a 2f 0a 23 69 66 64 dequate..*/.#ifd
2270: 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 ef SQLITE_COVERA
2280: 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 GE_TEST.SQLITE_P
2290: 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
22a0: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e lite3Coverage(in
22b0: 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73 t);.# define tes
22c0: 74 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20 tcase(X) if( X
22d0: 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 ){ sqlite3Covera
22e0: 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a ge(__LINE__); }.
22f0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 #else.# define t
2300: 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69 estcase(X).#endi
2310: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 f../*.** The ALW
2320: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 AYS and NEVER ma
2330: 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f cros surround bo
2340: 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e olean expression
2350: 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 s which .** are
2360: 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 intended to alwa
2370: 79 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61 ys be true or fa
2380: 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c lse, respectivel
2390: 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 y. Such.** expr
23a0: 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 essions could be
23b0: 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 omitted from th
23c0: 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c e code completel
23d0: 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 y. But they.**
23e0: 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 are included in
23f0: 61 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f a few cases in o
2400: 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 rder to enhance
2410: 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a the resilience.*
2420: 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75 * of SQLite to u
2430: 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69 nexpected behavi
2440: 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 or - to make the
2450: 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c code "self-heal
2460: 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 ing".** or "duct
2470: 69 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e ile" rather than
2480: 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22 being "brittle"
2490: 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74 and crashing at
24a0: 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 the first.** hi
24b0: 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 nt of unplanned
24c0: 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
24d0: 57 68 65 6e 20 64 6f 69 6e 67 20 63 6f 76 65 72 When doing cover
24e0: 61 67 65 20 74 65 73 74 69 6e 67 20 41 4c 57 41 age testing ALWA
24f0: 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61 72 65 YS and NEVER are
2500: 20 68 61 72 64 2d 63 6f 64 65 64 20 74 6f 0a 2a hard-coded to.*
2510: 2a 20 62 65 20 74 72 75 65 20 61 6e 64 20 66 61 * be true and fa
2520: 6c 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 lse so that the
2530: 75 6e 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 unreachable code
2540: 20 74 68 65 6e 20 73 70 65 63 69 66 79 20 77 69 then specify wi
2550: 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65 20 63 6f 75 ll.** not be cou
2560: 6e 74 65 64 20 61 73 20 75 6e 74 65 73 74 65 64 nted as untested
2570: 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 code..*/.#ifdef
2580: 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 SQLITE_COVERAGE
2590: 5f 54 45 53 54 0a 23 20 64 65 66 69 6e 65 20 41 _TEST.# define A
25a0: 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 31 LWAYS(X) (1
25b0: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 ).# define NEVER
25c0: 28 58 29 20 20 20 20 20 20 20 28 30 29 0a 23 65 (X) (0).#e
25d0: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 lse.# define ALW
25e0: 41 59 53 28 58 29 20 20 20 20 20 20 28 58 29 0a AYS(X) (X).
25f0: 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 # define NEVER(X
2600: 29 20 20 20 20 20 20 20 28 58 29 0a 23 65 6e 64 ) (X).#end
2610: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
2620: 63 72 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 cro unlikely() i
2630: 73 20 61 20 68 69 6e 74 20 74 68 61 74 20 73 75 s a hint that su
2640: 72 72 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 rrounds a boolea
2650: 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 n.** expression
2660: 74 68 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20 that is usually
2670: 66 61 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 false. Macro li
2680: 6b 65 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73 kely() surrounds
2690: 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 .** a boolean ex
26a0: 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 pression that is
26b0: 20 75 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 usually true.
26c0: 47 43 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a GCC is able to.*
26d0: 2a 20 75 73 65 20 74 68 65 73 65 20 68 69 6e 74 * use these hint
26e0: 73 20 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65 s to generate be
26f0: 74 74 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 tter code, somet
2700: 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 imes..*/.#if def
2710: 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
2720: 26 20 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b & 0.# define lik
2730: 65 6c 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c ely(X) __buil
2740: 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31 tin_expect((X),1
2750: 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b ).# define unlik
2760: 65 6c 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 ely(X) __builti
2770: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a n_expect((X),0).
2780: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c #else.# define l
2790: 69 6b 65 6c 79 28 58 29 20 20 20 20 21 21 28 58 ikely(X) !!(X
27a0: 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b ).# define unlik
27b0: 65 6c 79 28 58 29 20 20 21 21 28 58 29 0a 23 65 ely(X) !!(X).#e
27c0: 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68 69 73 ndif../*. * This
27d0: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 macro is used t
27e0: 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 75 67 o "hide" some ug
27f0: 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 69 6e liness in castin
2800: 67 20 61 6e 20 69 6e 74 0a 20 2a 20 76 61 6c 75 g an int. * valu
2810: 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c 75 65 e to a ptr value
2820: 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 43 20 under the MSVC
2830: 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 2e 64-bit compiler.
2840: 20 20 20 43 61 73 74 69 6e 67 0a 20 2a 20 6e 6f Casting. * no
2850: 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 73 20 n 64-bit values
2860: 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 65 73 to ptr types res
2870: 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 64 22 ults in a "hard"
2880: 20 65 72 72 6f 72 20 77 69 74 68 20 0a 20 2a 20 error with . *
2890: 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20 the MSVC 64-bit
28a0: 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 20 74 compiler which t
28b0: 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 6f 20 his attempts to
28c0: 61 76 6f 69 64 2e 20 20 0a 20 2a 0a 20 2a 20 41 avoid. . *. * A
28d0: 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c 65 72 simple compiler
28e0: 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 74 69 pragma or casti
28f0: 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 75 6c ng sequence coul
2900: 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 20 d not be found.
2910: 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 69 * to correct thi
2920: 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 74 69 s in all situati
2930: 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d 61 63 ons, so this mac
2940: 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 63 65 ro was introduce
2950: 64 2e 0a 20 2a 0a 20 2a 20 49 74 20 63 6f 75 6c d.. *. * It coul
2960: 64 20 62 65 20 61 72 67 75 65 64 20 74 68 61 74 d be argued that
2970: 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 74 79 the intptr_t ty
2980: 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 65 64 pe could be used
2990: 20 69 6e 20 74 68 69 73 0a 20 2a 20 63 61 73 65 in this. * case
29a0: 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 65 20 , but that type
29b0: 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
29c0: 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 on all compiler
29d0: 73 2c 20 6f 72 20 0a 20 2a 20 72 65 71 75 69 72 s, or . * requir
29e0: 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20 es the #include
29f0: 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 61 64 of specific head
2a00: 65 72 73 20 77 68 69 63 68 20 64 69 66 66 65 72 ers which differ
2a10: 73 20 62 65 74 77 65 65 6e 0a 20 2a 20 70 6c 61 s between. * pla
2a20: 74 66 6f 72 6d 73 2e 0a 20 2a 2f 0a 23 64 65 66 tforms.. */.#def
2a30: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 ine SQLITE_INT_T
2a40: 4f 5f 50 54 52 28 58 29 20 20 20 28 28 76 6f 69 O_PTR(X) ((voi
2a50: 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58 d*)&((char*)0)[X
2a60: 5d 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 ]).#define SQLIT
2a70: 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 20 E_PTR_TO_INT(X)
2a80: 20 20 28 28 69 6e 74 29 28 28 28 63 68 61 72 2a ((int)(((char*
2a90: 29 58 29 2d 28 63 68 61 72 2a 29 30 29 29 0a 0a )X)-(char*)0))..
2aa0: 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66 /*.** These #def
2ab0: 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62 ines should enab
2ac0: 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70 le >2GB file sup
2ad0: 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 69 66 port on Posix if
2ae0: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 the.** underlyi
2af0: 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 ng operating sys
2b00: 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e tem supports it.
2b10: 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b If the OS lack
2b20: 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20 s.** large file
2b30: 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 20 74 support, or if t
2b40: 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f 77 73 he OS is windows
2b50: 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 , these should b
2b60: 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 e no-ops..**.**
2b70: 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 20 54 Ticket #2739: T
2b80: 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f he _LARGEFILE_SO
2b90: 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 74 20 URCE macro must
2ba0: 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 61 6e appear before an
2bb0: 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 6e 63 y.** system #inc
2bc0: 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c 20 74 ludes. Hence, t
2bd0: 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 his block of cod
2be0: 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 76 65 e must be the ve
2bf0: 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f 64 65 ry first.** code
2c00: 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 in all source f
2c10: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 iles..**.** Larg
2c20: 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63 e file support c
2c30: 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75 an be disabled u
2c40: 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54 sing the -DSQLIT
2c50: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 E_DISABLE_LFS sw
2c60: 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 itch.** on the c
2c70: 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 ompiler command
2c80: 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e line. This is n
2c90: 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20 ecessary if you
2ca0: 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a are compiling.**
2cb0: 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63 on a recent mac
2cc0: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 hine (ex: RedHat
2cd0: 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 61 7.2) but you wa
2ce0: 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 nt your code to
2cf0: 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c work.** on an ol
2d00: 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 3a der machine (ex:
2d10: 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20 49 RedHat 6.0). I
2d20: 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e f you compile on
2d30: 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 RedHat 7.2.** w
2d40: 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69 ithout this opti
2d50: 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c on, LFS is enabl
2d60: 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73 e. But LFS does
2d70: 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68 not exist in th
2d80: 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 e kernel.** in R
2d90: 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 edHat 6.0, so th
2da0: 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 e code won't wor
2db0: 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d k. Hence, for m
2dc0: 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a aximum binary.**
2dd0: 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f 75 portability you
2de0: 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 should omit LFS
2df0: 2e 0a 2a 2a 0a 2a 2a 20 53 69 6d 69 6c 61 72 20 ..**.** Similar
2e00: 69 73 20 74 72 75 65 20 66 6f 72 20 4d 61 63 4f is true for MacO
2e10: 53 2e 20 20 4c 46 53 20 69 73 20 6f 6e 6c 79 20 S. LFS is only
2e20: 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 supported on Mac
2e30: 4f 53 20 39 20 61 6e 64 20 6c 61 74 65 72 2e 0a OS 9 and later..
2e40: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
2e50: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 E_DISABLE_LFS.#
2e60: 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 define _LARGE_FI
2e70: 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 66 6e LE 1.# ifn
2e80: 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 def _FILE_OFFSET
2e90: 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e 65 _BITS.# define
2ea0: 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 _FILE_OFFSET_BI
2eb0: 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 20 TS 64.# endif.#
2ec0: 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c define _LARGEFIL
2ed0: 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 69 E_SOURCE 1.#endi
2ee0: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 f.../*.** The SQ
2ef0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
2f00: 6d 61 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65 macro must be de
2f10: 66 69 6e 65 64 20 61 73 20 65 69 74 68 65 72 20 fined as either
2f20: 30 20 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72 0 or 1..** Older
2f30: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
2f40: 69 74 65 20 75 73 65 64 20 61 6e 20 6f 70 74 69 ite used an opti
2f50: 6f 6e 61 6c 20 54 48 52 45 41 44 53 41 46 45 20 onal THREADSAFE
2f60: 6d 61 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75 70 macro..** We sup
2f70: 70 6f 72 74 20 74 68 61 74 20 66 6f 72 20 6c 65 port that for le
2f80: 67 61 63 79 0a 2a 2f 0a 23 69 66 20 21 64 65 66 gacy.*/.#if !def
2f90: 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 ined(SQLITE_THRE
2fa0: 41 44 53 41 46 45 29 0a 23 69 66 20 64 65 66 69 ADSAFE).#if defi
2fb0: 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29 0a ned(THREADSAFE).
2fc0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
2fd0: 54 48 52 45 41 44 53 41 46 45 20 54 48 52 45 41 THREADSAFE THREA
2fe0: 44 53 41 46 45 0a 23 65 6c 73 65 0a 23 20 64 65 DSAFE.#else.# de
2ff0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 fine SQLITE_THRE
3000: 41 44 53 41 46 45 20 31 0a 23 65 6e 64 69 66 0a ADSAFE 1.#endif.
3010: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
3020: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
3030: 5f 4d 45 4d 53 54 41 54 55 53 20 6d 61 63 72 6f _MEMSTATUS macro
3040: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 must be defined
3050: 20 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 as either 0 or
3060: 31 2e 0a 2a 2a 20 49 74 20 64 65 74 65 72 6d 69 1..** It determi
3070: 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e nes whether or n
3080: 6f 74 20 74 68 65 20 66 65 61 74 75 72 65 73 20 ot the features
3090: 72 65 6c 61 74 65 64 20 74 6f 20 0a 2a 2a 20 53 related to .** S
30a0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d QLITE_CONFIG_MEM
30b0: 53 54 41 54 55 53 20 61 72 65 20 61 76 61 69 6c STATUS are avail
30c0: 61 62 65 20 62 79 20 64 65 66 61 75 6c 74 20 6f abe by default o
30d0: 72 20 6e 6f 74 2e 20 54 68 69 73 20 76 61 6c 75 r not. This valu
30e0: 65 20 63 61 6e 0a 2a 2a 20 62 65 20 6f 76 65 72 e can.** be over
30f0: 72 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 6d ridden at runtim
3100: 65 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 e using the sqli
3110: 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 41 50 49 te3_config() API
3120: 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
3130: 64 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 d(SQLITE_DEFAULT
3140: 5f 4d 45 4d 53 54 41 54 55 53 29 0a 23 20 64 65 _MEMSTATUS).# de
3150: 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 fine SQLITE_DEFA
3160: 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 31 0a ULT_MEMSTATUS 1.
3170: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 #endif../*.** Ex
3180: 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 actly one of the
3190: 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
31a0: 73 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 s must be define
31b0: 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a d in order to.**
31c0: 20 73 70 65 63 69 66 79 20 77 68 69 63 68 20 6d specify which m
31d0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
31e0: 20 73 75 62 73 79 73 74 65 6d 20 74 6f 20 75 73 subsystem to us
31f0: 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c e..**.** SQL
3200: 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f ITE_SYSTEM_MALLO
3210: 43 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55 73 C // Us
3220: 65 20 6e 6f 72 6d 61 6c 20 73 79 73 74 65 6d 20 e normal system
3230: 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 malloc().**
3240: 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 SQLITE_MEMDEBUG
3250: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f //
3260: 20 44 65 62 75 67 67 69 6e 67 20 76 65 72 73 69 Debugging versi
3270: 6f 6e 20 6f 66 20 73 79 73 74 65 6d 20 6d 61 6c on of system mal
3280: 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c loc().** SQL
3290: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 ITE_MEMORY_SIZE
32a0: 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 69 6e // in
32b0: 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 6f 72 ternal allocator
32c0: 20 23 31 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 #1.** SQLIT
32d0: 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 E_MMAP_HEAP_SIZE
32e0: 20 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 // inte
32f0: 72 6e 61 6c 20 6d 6d 61 70 28 29 20 61 6c 6c 6f rnal mmap() allo
3300: 63 61 74 6f 72 0a 2a 2a 20 20 20 20 20 53 51 4c cator.** SQL
3310: 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f ITE_POW2_MEMORY_
3320: 53 49 5a 45 20 20 20 20 20 20 20 2f 2f 20 69 6e SIZE // in
3330: 74 65 72 6e 61 6c 20 70 6f 77 65 72 2d 6f 66 2d ternal power-of-
3340: 74 77 6f 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a two allocator.**
3350: 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 .** If none of t
3360: 68 65 20 61 62 6f 76 65 20 61 72 65 20 64 65 66 he above are def
3370: 69 6e 65 64 2c 20 74 68 65 6e 20 73 65 74 20 53 ined, then set S
3380: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
3390: 4c 4f 43 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 LOC as.** the de
33a0: 66 61 75 6c 74 2e 0a 2a 2f 0a 23 69 66 20 64 65 fault..*/.#if de
33b0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 fined(SQLITE_SYS
33c0: 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 TEM_MALLOC)+defi
33d0: 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 ned(SQLITE_MEMDE
33e0: 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e BUG)+\. defin
33f0: 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 ed(SQLITE_MEMORY
3400: 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 _SIZE)+defined(S
3410: 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f QLITE_MMAP_HEAP_
3420: 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 SIZE)+\. defi
3430: 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f ned(SQLITE_POW2_
3440: 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3e 31 0a 23 MEMORY_SIZE)>1.#
3450: 20 65 72 72 6f 72 20 22 41 74 20 6d 6f 73 74 20 error "At most
3460: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
3470: 77 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d wing compile-tim
3480: 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 e configuration
3490: 6f 70 74 69 6f 6e 73 5c 0a 20 69 73 20 61 6c 6c options\. is all
34a0: 6f 77 73 3a 20 53 51 4c 49 54 45 5f 53 59 53 54 ows: SQLITE_SYST
34b0: 45 4d 5f 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 54 EM_MALLOC, SQLIT
34c0: 45 5f 4d 45 4d 44 45 42 55 47 2c 20 53 51 4c 49 E_MEMDEBUG, SQLI
34d0: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2c 5c TE_MEMORY_SIZE,\
34e0: 0a 20 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 . SQLITE_MMAP_HE
34f0: 41 50 5f 53 49 5a 45 2c 20 53 51 4c 49 54 45 5f AP_SIZE, SQLITE_
3500: 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 POW2_MEMORY_SIZE
3510: 22 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 ".#endif.#if def
3520: 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 ined(SQLITE_SYST
3530: 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e EM_MALLOC)+defin
3540: 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 ed(SQLITE_MEMDEB
3550: 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 UG)+\. define
3560: 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f d(SQLITE_MEMORY_
3570: 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 51 SIZE)+defined(SQ
3580: 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 LITE_MMAP_HEAP_S
3590: 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e IZE)+\. defin
35a0: 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d ed(SQLITE_POW2_M
35b0: 45 4d 4f 52 59 5f 53 49 5a 45 29 3d 3d 30 0a 23 EMORY_SIZE)==0.#
35c0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 define SQLITE_S
35d0: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 31 0a 23 YSTEM_MALLOC 1.#
35e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
35f0: 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f SQLITE_MALLOC_SO
3600: 46 54 5f 4c 49 4d 49 54 20 69 73 20 64 65 66 69 FT_LIMIT is defi
3610: 6e 65 64 2c 20 74 68 65 6e 20 74 72 79 20 74 6f ned, then try to
3620: 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 69 7a keep the.** siz
3630: 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c es of memory all
3640: 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f 77 20 74 ocations below t
3650: 68 69 73 20 76 61 6c 75 65 20 77 68 65 72 65 20 his value where
3660: 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 possible..*/.#if
3670: 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
3680: 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 POW2_MEMORY_SIZE
3690: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
36a0: 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 LITE_MALLOC_SOFT
36b0: 5f 4c 49 4d 49 54 29 0a 23 20 64 65 66 69 6e 65 _LIMIT).# define
36c0: 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 SQLITE_MALLOC_S
36d0: 4f 46 54 5f 4c 49 4d 49 54 20 31 30 32 34 0a 23 OFT_LIMIT 1024.#
36e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 endif../*.** We
36f0: 6e 65 65 64 20 74 6f 20 64 65 66 69 6e 65 20 5f need to define _
3700: 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 61 73 20 XOPEN_SOURCE as
3710: 66 6f 6c 6c 6f 77 73 20 69 6e 20 6f 72 64 65 72 follows in order
3720: 20 74 6f 20 65 6e 61 62 6c 65 0a 2a 2a 20 72 65 to enable.** re
3730: 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 cursive mutexes
3740: 6f 6e 20 6d 6f 73 74 20 75 6e 69 78 20 73 79 73 on most unix sys
3750: 74 65 6d 73 2e 20 20 42 75 74 20 4d 61 63 20 4f tems. But Mac O
3760: 53 20 58 20 69 73 20 64 69 66 66 65 72 65 6e 74 S X is different
3770: 2e 0a 2a 2a 20 54 68 65 20 5f 58 4f 50 45 4e 5f ..** The _XOPEN_
3780: 53 4f 55 52 43 45 20 64 65 66 69 6e 65 20 63 61 SOURCE define ca
3790: 75 73 65 73 20 70 72 6f 62 6c 65 6d 73 20 66 6f uses problems fo
37a0: 72 20 4d 61 63 20 4f 53 20 58 20 77 65 20 61 72 r Mac OS X we ar
37b0: 65 20 74 6f 6c 64 2c 0a 2a 2a 20 73 6f 20 69 74 e told,.** so it
37c0: 20 69 73 20 6f 6d 69 74 74 65 64 20 74 68 65 72 is omitted ther
37d0: 65 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 e. See ticket #
37e0: 32 36 37 33 2e 0a 2a 2a 0a 2a 2a 20 4c 61 74 65 2673..**.** Late
37f0: 72 20 77 65 20 6c 65 61 72 6e 20 74 68 61 74 20 r we learn that
3800: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 69 73 _XOPEN_SOURCE is
3810: 20 70 6f 6f 72 6c 79 20 6f 72 20 69 6e 63 6f 72 poorly or incor
3820: 72 65 63 74 6c 79 0a 2a 2a 20 69 6d 70 6c 65 6d rectly.** implem
3830: 65 6e 74 65 64 20 6f 6e 20 73 6f 6d 65 20 73 79 ented on some sy
3840: 73 74 65 6d 73 2e 20 20 53 6f 20 77 65 20 61 76 stems. So we av
3850: 6f 69 64 20 64 65 66 69 6e 69 6e 67 20 69 74 20 oid defining it
3860: 61 74 20 61 6c 6c 0a 2a 2a 20 69 66 20 69 74 20 at all.** if it
3870: 69 73 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e is already defin
3880: 65 64 20 6f 72 20 69 66 20 69 74 20 69 73 20 75 ed or if it is u
3890: 6e 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20 nneeded because
38a0: 77 65 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 64 6f we are.** not do
38b0: 69 6e 67 20 61 20 74 68 72 65 61 64 73 61 66 65 ing a threadsafe
38c0: 20 62 75 69 6c 64 2e 20 20 54 69 63 6b 65 74 20 build. Ticket
38d0: 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 #2681..**.** See
38e0: 20 61 6c 73 6f 20 74 69 63 6b 65 74 20 23 32 37 also ticket #27
38f0: 34 31 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 41..*/.#if !defi
3900: 6e 65 64 28 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 ned(_XOPEN_SOURC
3910: 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f E) && !defined(_
3920: 5f 44 41 52 57 49 4e 5f 5f 29 20 26 26 20 21 64 _DARWIN__) && !d
3930: 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
3940: 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 ) && SQLITE_THRE
3950: 41 44 53 41 46 45 0a 23 20 20 64 65 66 69 6e 65 ADSAFE.# define
3960: 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 35 _XOPEN_SOURCE 5
3970: 30 30 20 20 2f 2a 20 4e 65 65 64 65 64 20 74 6f 00 /* Needed to
3980: 20 65 6e 61 62 6c 65 20 70 74 68 72 65 61 64 20 enable pthread
3990: 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 recursive mutexe
39a0: 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a s */.#endif../*.
39b0: 2a 2a 20 54 68 65 20 54 43 4c 20 68 65 61 64 65 ** The TCL heade
39c0: 72 73 20 61 72 65 20 6f 6e 6c 79 20 6e 65 65 64 rs are only need
39d0: 65 64 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e ed when compilin
39e0: 67 20 74 68 65 20 54 43 4c 20 62 69 6e 64 69 6e g the TCL bindin
39f0: 67 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e gs..*/.#if defin
3a00: 65 64 28 53 51 4c 49 54 45 5f 54 43 4c 29 20 7c ed(SQLITE_TCL) |
3a10: 7c 20 64 65 66 69 6e 65 64 28 54 43 4c 53 48 29 | defined(TCLSH)
3a20: 0a 23 20 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e .# include <tcl.
3a30: 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a h>.#endif../*.**
3a40: 20 4d 61 6e 79 20 70 65 6f 70 6c 65 20 61 72 65 Many people are
3a50: 20 66 61 69 6c 69 6e 67 20 74 6f 20 73 65 74 20 failing to set
3a60: 2d 44 4e 44 45 42 55 47 3d 31 20 77 68 65 6e 20 -DNDEBUG=1 when
3a70: 63 6f 6d 70 69 6c 69 6e 67 20 53 51 4c 69 74 65 compiling SQLite
3a80: 2e 0a 2a 2a 20 53 65 74 74 69 6e 67 20 4e 44 45 ..** Setting NDE
3a90: 42 55 47 20 6d 61 6b 65 73 20 74 68 65 20 63 6f BUG makes the co
3aa0: 64 65 20 73 6d 61 6c 6c 65 72 20 61 6e 64 20 72 de smaller and r
3ab0: 75 6e 20 66 61 73 74 65 72 2e 20 20 53 6f 20 74 un faster. So t
3ac0: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
3ad0: 6c 69 6e 65 73 20 61 72 65 20 61 64 64 65 64 20 lines are added
3ae0: 74 6f 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 to automatically
3af0: 20 73 65 74 20 4e 44 45 42 55 47 20 75 6e 6c 65 set NDEBUG unle
3b00: 73 73 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f ss the -DSQLITE_
3b10: 44 45 42 55 47 3d 31 0a 2a 2a 20 6f 70 74 69 6f DEBUG=1.** optio
3b20: 6e 20 69 73 20 73 65 74 2e 20 20 54 68 75 73 20 n is set. Thus
3b30: 4e 44 45 42 55 47 20 62 65 63 6f 6d 65 73 20 61 NDEBUG becomes a
3b40: 6e 20 6f 70 74 2d 69 6e 20 72 61 74 68 65 72 20 n opt-in rather
3b50: 74 68 61 6e 20 61 6e 20 6f 70 74 2d 6f 75 74 0a than an opt-out.
3b60: 2a 2a 20 66 65 61 74 75 72 65 2e 0a 2a 2f 0a 23 ** feature..*/.#
3b70: 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
3b80: 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 UG) && !defined(
3b90: 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 0a 23 SQLITE_DEBUG) .#
3ba0: 20 64 65 66 69 6e 65 20 4e 44 45 42 55 47 20 31 define NDEBUG 1
3bb0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
3bc0: 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
3bd0: 20 73 71 6c 69 74 65 33 2e 68 20 69 6e 20 74 68 sqlite3.h in th
3be0: 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
3bf0: 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
3c00: 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
3c10: 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
3c20: 69 6c 65 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a ile sqlite3.h **
3c30: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
3c60: 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
3c70: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
3c80: 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
3c90: 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
3ca0: 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
3cb0: 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
3cc0: 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
3cd0: 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
3ce0: 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
3cf0: 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
3d00: 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
3d10: 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
3d20: 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
3d30: 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
3d40: 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
3d50: 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
3d60: 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
3d70: 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
3d80: 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
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 0a ***************.
3dd0: 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 ** This header f
3de0: 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 ile defines the
3df0: 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 interface that t
3e00: 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
3e10: 79 0a 2a 2a 20 70 72 65 73 65 6e 74 73 20 74 6f y.** presents to
3e20: 20 63 6c 69 65 6e 74 20 70 72 6f 67 72 61 6d 73 client programs
3e30: 2e 20 20 49 66 20 61 20 43 2d 66 75 6e 63 74 69 . If a C-functi
3e40: 6f 6e 2c 20 73 74 72 75 63 74 75 72 65 2c 20 64 on, structure, d
3e50: 61 74 61 74 79 70 65 2c 0a 2a 2a 20 6f 72 20 63 atatype,.** or c
3e60: 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 69 74 69 onstant definiti
3e70: 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 65 on does not appe
3e80: 61 72 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2c ar in this file,
3e90: 20 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e then it is.** n
3ea0: 6f 74 20 61 20 70 75 62 6c 69 73 68 65 64 20 41 ot a published A
3eb0: 50 49 20 6f 66 20 53 51 4c 69 74 65 2c 20 69 73 PI of SQLite, is
3ec0: 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e subject to chan
3ed0: 67 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6e 6f ge without.** no
3ee0: 74 69 63 65 2c 20 61 6e 64 20 73 68 6f 75 6c 64 tice, and should
3ef0: 20 6e 6f 74 20 62 65 20 72 65 66 65 72 65 6e 63 not be referenc
3f00: 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 ed by programs t
3f10: 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 2e 0a hat use SQLite..
3f20: 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 **.** Some of th
3f30: 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 74 68 e definitions th
3f40: 61 74 20 61 72 65 20 69 6e 20 74 68 69 73 20 66 at are in this f
3f50: 69 6c 65 20 61 72 65 20 6d 61 72 6b 65 64 20 61 ile are marked a
3f60: 73 0a 2a 2a 20 22 65 78 70 65 72 69 6d 65 6e 74 s.** "experiment
3f70: 61 6c 22 2e 20 20 45 78 70 65 72 69 6d 65 6e 74 al". Experiment
3f80: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 al interfaces ar
3f90: 65 20 6e 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a 2a e normally new.*
3fa0: 2a 20 66 65 61 74 75 72 65 73 20 72 65 63 65 6e * features recen
3fb0: 74 6c 79 20 61 64 64 65 64 20 74 6f 20 53 51 4c tly added to SQL
3fc0: 69 74 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 ite. We do not
3fd0: 61 6e 74 69 63 69 70 61 74 65 20 63 68 61 6e 67 anticipate chang
3fe0: 65 73 0a 2a 2a 20 74 6f 20 65 78 70 65 72 69 6d es.** to experim
3ff0: 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 ental interfaces
4000: 20 62 75 74 20 72 65 73 65 72 76 65 20 74 6f 20 but reserve to
4010: 6d 61 6b 65 20 6d 69 6e 6f 72 20 63 68 61 6e 67 make minor chang
4020: 65 73 20 69 66 0a 2a 2a 20 65 78 70 65 72 69 65 es if.** experie
4030: 6e 63 65 20 66 72 6f 6d 20 75 73 65 20 22 69 6e nce from use "in
4040: 20 74 68 65 20 77 69 6c 64 22 20 73 75 67 67 65 the wild" sugge
4050: 73 74 20 73 75 63 68 20 63 68 61 6e 67 65 73 20 st such changes
4060: 61 72 65 20 70 72 75 64 65 6e 74 2e 0a 2a 2a 0a are prudent..**.
4070: 2a 2a 20 54 68 65 20 6f 66 66 69 63 69 61 6c 20 ** The official
4080: 43 2d 6c 61 6e 67 75 61 67 65 20 41 50 49 20 64 C-language API d
4090: 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 ocumentation for
40a0: 20 53 51 4c 69 74 65 20 69 73 20 64 65 72 69 76 SQLite is deriv
40b0: 65 64 0a 2a 2a 20 66 72 6f 6d 20 63 6f 6d 6d 65 ed.** from comme
40c0: 6e 74 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 nts in this file
40d0: 2e 20 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 . This file is
40e0: 74 68 65 20 61 75 74 68 6f 72 69 74 61 74 69 76 the authoritativ
40f0: 65 20 73 6f 75 72 63 65 0a 2a 2a 20 6f 6e 20 68 e source.** on h
4100: 6f 77 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 ow SQLite interf
4110: 61 63 65 73 20 61 72 65 20 73 75 70 70 6f 73 65 aces are suppose
4120: 20 74 6f 20 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a to operate..**.
4130: 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 ** The name of t
4140: 68 69 73 20 66 69 6c 65 20 75 6e 64 65 72 20 63 his file under c
4150: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e onfiguration man
4160: 61 67 65 6d 65 6e 74 20 69 73 20 22 73 71 6c 69 agement is "sqli
4170: 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a 20 54 68 65 te.h.in"..** The
4180: 20 6d 61 6b 65 66 69 6c 65 20 6d 61 6b 65 73 20 makefile makes
4190: 73 6f 6d 65 20 6d 69 6e 6f 72 20 63 68 61 6e 67 some minor chang
41a0: 65 73 20 74 6f 20 74 68 69 73 20 66 69 6c 65 20 es to this file
41b0: 28 73 75 63 68 20 61 73 20 69 6e 73 65 72 74 69 (such as inserti
41c0: 6e 67 0a 2a 2a 20 74 68 65 20 76 65 72 73 69 6f ng.** the versio
41d0: 6e 20 6e 75 6d 62 65 72 29 20 61 6e 64 20 63 68 n number) and ch
41e0: 61 6e 67 65 73 20 69 74 73 20 6e 61 6d 65 20 74 anges its name t
41f0: 6f 20 22 73 71 6c 69 74 65 33 2e 68 22 20 61 73 o "sqlite3.h" as
4200: 0a 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 .** part of the
4210: 62 75 69 6c 64 20 70 72 6f 63 65 73 73 2e 0a 2a build process..*
4220: 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73 *.** @(#) $Id: s
4230: 71 6c 69 74 65 2e 68 2e 69 6e 2c 76 20 31 2e 34 qlite.h.in,v 1.4
4240: 30 39 20 32 30 30 38 2f 31 31 2f 30 37 20 30 30 09 2008/11/07 00
4250: 3a 30 36 3a 31 38 20 64 72 68 20 45 78 70 20 24 :06:18 drh Exp $
4260: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c .*/.#ifndef _SQL
4270: 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e 65 20 ITE3_H_.#define
4280: 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69 6e 63 _SQLITE3_H_.#inc
4290: 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68 3e 20 lude <stdarg.h>
42a0: 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20 66 6f /* Needed fo
42b0: 72 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e r the definition
42c0: 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f 0a 0a of va_list */..
42d0: 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
42e0: 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68 69 73 we can call this
42f0: 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b 2b 2e stuff from C++.
4300: 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65 72 6e .*/.#if 0.extern
4310: 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a 0a 0a "C" {.#endif...
4320: 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 61 62 /*.** Add the ab
4330: 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 69 64 ility to overrid
4340: 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a 23 69 e 'extern'.*/.#i
4350: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 58 54 fndef SQLITE_EXT
4360: 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ERN.# define SQL
4370: 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74 65 72 ITE_EXTERN exter
4380: 6e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 n.#endif../*.**
4390: 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d 61 63 72 These no-op macr
43a0: 6f 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 66 os are used in f
43b0: 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 66 61 63 ront of interfac
43c0: 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 6f 73 65 es to mark those
43d0: 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 61 .** interfaces a
43e0: 73 20 65 69 74 68 65 72 20 64 65 70 72 65 63 61 s either depreca
43f0: 74 65 64 20 6f 72 20 65 78 70 65 72 69 6d 65 6e ted or experimen
4400: 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 6c 69 63 tal. New applic
4410: 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 ations.** should
4420: 20 6e 6f 74 20 75 73 65 20 64 65 70 72 65 63 61 not use depreca
4430: 74 65 64 20 69 6e 74 72 66 61 63 65 73 20 2d 20 ted intrfaces -
4440: 74 68 65 79 20 61 72 65 20 73 75 70 70 6f 72 74 they are support
4450: 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 0a 2a for backwards.*
4460: 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 * compatibility
4470: 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 only. Applicati
4480: 6f 6e 20 77 72 69 74 65 72 73 20 73 68 6f 75 6c on writers shoul
4490: 64 20 62 65 20 61 77 61 72 65 20 74 68 61 74 0a d be aware that.
44a0: 2a 2a 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 ** experimental
44b0: 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 73 interfaces are s
44c0: 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 ubject to change
44d0: 20 69 6e 20 70 6f 69 6e 74 20 72 65 6c 65 61 73 in point releas
44e0: 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 es..**.** These
44f0: 6d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 72 macros used to r
4500: 65 73 6f 6c 76 65 20 74 6f 20 76 61 72 69 6f 75 esolve to variou
4510: 73 20 6b 69 6e 64 73 20 6f 66 20 63 6f 6d 70 69 s kinds of compi
4520: 6c 65 72 20 6d 61 67 69 63 20 74 68 61 74 0a 2a ler magic that.*
4530: 2a 20 77 6f 75 6c 64 20 67 65 6e 65 72 61 74 65 * would generate
4540: 20 77 61 72 6e 69 6e 67 20 6d 65 73 73 61 67 65 warning message
4550: 73 20 77 68 65 6e 20 74 68 65 79 20 77 65 72 65 s when they were
4560: 20 75 73 65 64 2e 20 20 42 75 74 20 74 68 61 74 used. But that
4570: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67 .** compiler mag
4580: 69 63 20 65 6e 64 65 64 20 75 70 20 67 65 6e 65 ic ended up gene
4590: 72 61 74 69 6e 67 20 73 75 63 68 20 61 20 66 6c rating such a fl
45a0: 75 72 72 79 20 6f 66 20 62 75 67 20 72 65 70 6f urry of bug repo
45b0: 72 74 73 0a 2a 2a 20 74 68 61 74 20 77 65 20 68 rts.** that we h
45c0: 61 76 65 20 74 61 6b 65 6e 20 69 74 20 61 6c 6c ave taken it all
45d0: 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65 20 62 61 out and gone ba
45e0: 63 6b 20 74 6f 20 75 73 69 6e 67 20 73 69 6d 70 ck to using simp
45f0: 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d 61 63 72 6f le.** noop macro
4600: 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 s..*/.#define SQ
4610: 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 0a LITE_DEPRECATED.
4620: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 #define SQLITE_E
4630: 58 50 45 52 49 4d 45 4e 54 41 4c 0a 0a 2f 2a 0a XPERIMENTAL../*.
4640: 2a 2a 20 45 6e 73 75 72 65 20 74 68 65 73 65 20 ** Ensure these
4650: 73 79 6d 62 6f 6c 73 20 77 65 72 65 20 6e 6f 74 symbols were not
4660: 20 64 65 66 69 6e 65 64 20 62 79 20 73 6f 6d 65 defined by some
4670: 20 70 72 65 76 69 6f 75 73 20 68 65 61 64 65 72 previous header
4680: 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 file..*/.#ifdef
4690: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a SQLITE_VERSION.
46a0: 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 # undef SQLITE_V
46b0: 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66 0a 23 69 ERSION.#endif.#i
46c0: 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 fdef SQLITE_VERS
46d0: 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20 75 6e 64 ION_NUMBER.# und
46e0: 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f ef SQLITE_VERSIO
46f0: 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64 69 66 0a N_NUMBER.#endif.
4700: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
4710: 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 4c 69 Compile-Time Li
4720: 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 brary Version Nu
4730: 6d 62 65 72 73 20 7b 48 31 30 30 31 30 7d 20 3c mbers {H10010} <
4740: 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S60100>.**.** Th
4750: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e e SQLITE_VERSION
4760: 20 61 6e 64 20 53 51 4c 49 54 45 5f 56 45 52 53 and SQLITE_VERS
4770: 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64 65 66 69 ION_NUMBER #defi
4780: 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71 nes in.** the sq
4790: 6c 69 74 65 33 2e 68 20 66 69 6c 65 20 73 70 65 lite3.h file spe
47a0: 63 69 66 79 20 74 68 65 20 76 65 72 73 69 6f 6e cify the version
47b0: 20 6f 66 20 53 51 4c 69 74 65 20 77 69 74 68 20 of SQLite with
47c0: 77 68 69 63 68 0a 2a 2a 20 74 68 61 74 20 68 65 which.** that he
47d0: 61 64 65 72 20 66 69 6c 65 20 69 73 20 61 73 73 ader file is ass
47e0: 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 ociated..**.** T
47f0: 68 65 20 22 76 65 72 73 69 6f 6e 22 20 6f 66 20 he "version" of
4800: 53 51 4c 69 74 65 20 69 73 20 61 20 73 74 72 69 SQLite is a stri
4810: 6e 67 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 ng of the form "
4820: 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 70 X.Y.Z"..** The p
4830: 68 72 61 73 65 20 22 61 6c 70 68 61 22 20 6f 72 hrase "alpha" or
4840: 20 22 62 65 74 61 22 20 6d 69 67 68 74 20 62 65 "beta" might be
4850: 20 61 70 70 65 6e 64 65 64 20 61 66 74 65 72 20 appended after
4860: 74 68 65 20 5a 2e 0a 2a 2a 20 54 68 65 20 58 20 the Z..** The X
4870: 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 20 76 value is major v
4880: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6c ersion number al
4890: 77 61 79 73 20 33 20 69 6e 20 53 51 4c 69 74 65 ways 3 in SQLite
48a0: 33 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 3..** The X valu
48b0: 65 20 6f 6e 6c 79 20 63 68 61 6e 67 65 73 20 77 e only changes w
48c0: 68 65 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f hen backwards co
48d0: 6d 70 61 74 69 62 69 6c 69 74 79 20 69 73 0a 2a mpatibility is.*
48e0: 2a 20 62 72 6f 6b 65 6e 20 61 6e 64 20 77 65 20 * broken and we
48f0: 69 6e 74 65 6e 64 20 74 6f 20 6e 65 76 65 72 20 intend to never
4900: 62 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20 break backwards
4910: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a compatibility..*
4920: 2a 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73 * The Y value is
4930: 20 74 68 65 20 6d 69 6e 6f 72 20 76 65 72 73 69 the minor versi
4940: 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 6f 6e on number and on
4950: 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 0a ly changes when.
4960: 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6d 61 6a ** there are maj
4970: 6f 72 20 66 65 61 74 75 72 65 20 65 6e 68 61 6e or feature enhan
4980: 63 65 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 cements that are
4990: 20 66 6f 72 77 61 72 64 73 20 63 6f 6d 70 61 74 forwards compat
49a0: 69 62 6c 65 0a 2a 2a 20 62 75 74 20 6e 6f 74 20 ible.** but not
49b0: 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 backwards compat
49c0: 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 5a 20 76 ible..** The Z v
49d0: 61 6c 75 65 20 69 73 20 74 68 65 20 72 65 6c 65 alue is the rele
49e0: 61 73 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 69 ase number and i
49f0: 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 s incremented wi
4a00: 74 68 0a 2a 2a 20 65 61 63 68 20 72 65 6c 65 61 th.** each relea
4a10: 73 65 20 62 75 74 20 72 65 73 65 74 73 20 62 61 se but resets ba
4a20: 63 6b 20 74 6f 20 30 20 77 68 65 6e 65 76 65 72 ck to 0 whenever
4a30: 20 59 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 Y is incremente
4a40: 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 d..**.** See als
4a50: 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 o: [sqlite3_libv
4a60: 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 ersion()] and [s
4a70: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f qlite3_libversio
4a80: 6e 5f 6e 75 6d 62 65 72 28 29 5d 2e 0a 2a 2a 0a n_number()]..**.
4a90: 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
4aa0: 2a 0a 2a 2a 20 7b 48 31 30 30 31 31 7d 20 54 68 *.** {H10011} Th
4ab0: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e e SQLITE_VERSION
4ac0: 20 23 64 65 66 69 6e 65 20 69 6e 20 74 68 65 20 #define in the
4ad0: 73 71 6c 69 74 65 33 2e 68 20 68 65 61 64 65 72 sqlite3.h header
4ae0: 20 66 69 6c 65 20 73 68 61 6c 6c 0a 2a 2a 20 20 file shall.**
4af0: 20 20 20 20 20 20 20 20 65 76 61 6c 75 61 74 65 evaluate
4b00: 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74 to a string lit
4b10: 65 72 61 6c 20 74 68 61 74 20 69 73 20 74 68 65 eral that is the
4b20: 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 0a SQLite version.
4b30: 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68 ** with
4b40: 20 77 68 69 63 68 20 74 68 65 20 68 65 61 64 65 which the heade
4b50: 72 20 66 69 6c 65 20 69 73 20 61 73 73 6f 63 69 r file is associ
4b60: 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 ated..**.** {H10
4b70: 30 31 34 7d 20 54 68 65 20 53 51 4c 49 54 45 5f 014} The SQLITE_
4b80: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 VERSION_NUMBER #
4b90: 64 65 66 69 6e 65 20 73 68 61 6c 6c 20 72 65 73 define shall res
4ba0: 6f 6c 76 65 20 74 6f 20 61 6e 20 69 6e 74 65 67 olve to an integ
4bb0: 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 er.** w
4bc0: 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 28 58 ith the value (X
4bd0: 2a 31 30 30 30 30 30 30 20 2b 20 59 2a 31 30 30 *1000000 + Y*100
4be0: 30 20 2b 20 5a 29 20 77 68 65 72 65 20 58 2c 20 0 + Z) where X,
4bf0: 59 2c 20 61 6e 64 20 5a 0a 2a 2a 20 20 20 20 20 Y, and Z.**
4c00: 20 20 20 20 20 61 72 65 20 74 68 65 20 6d 61 6a are the maj
4c10: 6f 72 20 76 65 72 73 69 6f 6e 2c 20 6d 69 6e 6f or version, mino
4c20: 72 20 76 65 72 73 69 6f 6e 2c 20 61 6e 64 20 72 r version, and r
4c30: 65 6c 65 61 73 65 20 6e 75 6d 62 65 72 2e 0a 2a elease number..*
4c40: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
4c50: 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20 _VERSION
4c60: 20 22 33 2e 36 2e 34 22 0a 23 64 65 66 69 6e 65 "3.6.4".#define
4c70: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f SQLITE_VERSION_
4c80: 4e 55 4d 42 45 52 20 20 33 30 30 36 30 30 34 0a NUMBER 3006004.
4c90: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
4ca0: 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 72 61 72 Run-Time Librar
4cb0: 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 y Version Number
4cc0: 73 20 7b 48 31 30 30 32 30 7d 20 3c 53 36 30 31 s {H10020} <S601
4cd0: 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 00>.** KEYWORDS:
4ce0: 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e sqlite3_version
4cf0: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 65 61 .**.** These fea
4d00: 74 75 72 65 73 20 70 72 6f 76 69 64 65 20 74 68 tures provide th
4d10: 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 e same informati
4d20: 6f 6e 20 61 73 20 74 68 65 20 5b 53 51 4c 49 54 on as the [SQLIT
4d30: 45 5f 56 45 52 53 49 4f 4e 5d 0a 2a 2a 20 61 6e E_VERSION].** an
4d40: 64 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f d [SQLITE_VERSIO
4d50: 4e 5f 4e 55 4d 42 45 52 5d 20 23 64 65 66 69 6e N_NUMBER] #defin
4d60: 65 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 es in the header
4d70: 2c 20 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 , but are associ
4d80: 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 ated.** with the
4d90: 20 6c 69 62 72 61 72 79 20 69 6e 73 74 65 61 64 library instead
4da0: 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 66 of the header f
4db0: 69 6c 65 2e 20 20 43 61 75 74 69 6f 75 73 20 70 ile. Cautious p
4dc0: 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 rogrammers might
4dd0: 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 61 20 63 68 .** include a ch
4de0: 65 63 6b 20 69 6e 20 74 68 65 69 72 20 61 70 70 eck in their app
4df0: 6c 69 63 61 74 69 6f 6e 20 74 6f 20 76 65 72 69 lication to veri
4e00: 66 79 20 74 68 61 74 0a 2a 2a 20 73 71 6c 69 74 fy that.** sqlit
4e10: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 e3_libversion_nu
4e20: 6d 62 65 72 28 29 20 61 6c 77 61 79 73 20 72 65 mber() always re
4e30: 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75 65 0a turns the value.
4e40: 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 ** [SQLITE_VERSI
4e50: 4f 4e 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a ON_NUMBER]..**.*
4e60: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 * The sqlite3_li
4e70: 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 bversion() funct
4e80: 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 ion returns the
4e90: 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e same information
4ea0: 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68 65 as is.** in the
4eb0: 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e sqlite3_version
4ec0: 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 [] string consta
4ed0: 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f nt. The functio
4ee0: 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a 2a 2a n is provided.**
4ef0: 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c 73 for use in DLLs
4f00: 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 72 73 since DLL users
4f10: 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 usually do not
4f20: 68 61 76 65 20 64 69 72 65 63 74 20 61 63 63 65 have direct acce
4f30: 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 ss to string.**
4f40: 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 69 6e constants within
4f50: 20 74 68 65 20 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 the DLL..**.**
4f60: 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
4f70: 2a 20 7b 48 31 30 30 32 31 7d 20 54 68 65 20 5b * {H10021} The [
4f80: 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 sqlite3_libversi
4f90: 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d 20 69 6e 74 on_number()] int
4fa0: 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 erface shall ret
4fb0: 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 urn.**
4fc0: 61 6e 20 69 6e 74 65 67 65 72 20 65 71 75 61 6c an integer equal
4fd0: 20 74 6f 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 to [SQLITE_VERS
4fe0: 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a ION_NUMBER]..**.
4ff0: 2a 2a 20 7b 48 31 30 30 32 32 7d 20 54 68 65 20 ** {H10022} The
5000: 5b 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e [sqlite3_version
5010: 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e ] string constan
5020: 74 20 73 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 0a t shall contain.
5030: 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
5040: 74 65 78 74 20 6f 66 20 74 68 65 20 5b 53 51 4c text of the [SQL
5050: 49 54 45 5f 56 45 52 53 49 4f 4e 5d 20 73 74 72 ITE_VERSION] str
5060: 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 30 ing..**.** {H100
5070: 32 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 23} The [sqlite3
5080: 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 5d 20 66 _libversion()] f
5090: 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 unction shall re
50a0: 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 turn.**
50b0: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
50c0: 65 20 5b 73 71 6c 69 74 65 33 5f 76 65 72 73 69 e [sqlite3_versi
50d0: 6f 6e 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 on] string const
50e0: 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ant..*/.SQLITE_A
50f0: 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 PI const char sq
5100: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 3b lite3_version[];
5110: 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
5120: 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
5130: 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 libversion(void)
5140: 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
5150: 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 sqlite3_libvers
5160: 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 ion_number(void)
5170: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
5180: 46 3a 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 F: Test To See I
5190: 66 20 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 f The Library Is
51a0: 20 54 68 72 65 61 64 73 61 66 65 20 7b 48 31 30 Threadsafe {H10
51b0: 31 30 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 100} <S60100>.**
51c0: 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62 .** SQLite can b
51d0: 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 e compiled with
51e0: 6f 72 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78 or without mutex
51f0: 65 73 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 es. When.** the
5200: 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 [SQLITE_THREADS
5210: 41 46 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73 AFE] C preproces
5220: 73 6f 72 20 6d 61 63 72 6f 20 31 20 6f 72 20 32 sor macro 1 or 2
5230: 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 , mutexes.** are
5240: 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c enabled and SQL
5250: 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61 66 ite is threadsaf
5260: 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 e. When the.**
5270: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
5280: 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 FE] macro is 0,
5290: 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 .** the mutexes
52a0: 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 are omitted. Wi
52b0: 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 thout the mutexe
52c0: 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 s, it is not saf
52d0: 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 e.** to use SQLi
52e0: 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 te concurrently
52f0: 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f from more than o
5300: 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a ne thread..**.**
5310: 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 Enabling mutexe
5320: 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 s incurs a measu
5330: 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 rable performanc
5340: 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f e penalty..** So
5350: 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 if speed is of
5360: 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 utmost importanc
5370: 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 e, it makes sens
5380: 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 e to disable.**
5390: 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 the mutexes. Bu
53a0: 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 t for maximum sa
53b0: 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 fety, mutexes sh
53c0: 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e ould be enabled.
53d0: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 .** The default
53e0: 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 behavior is for
53f0: 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e mutexes to be en
5400: 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 abled..**.** Thi
5410: 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 s interface can
5420: 62 65 20 75 73 65 64 20 62 79 20 61 20 70 72 6f be used by a pro
5430: 67 72 61 6d 20 74 6f 20 6d 61 6b 65 20 73 75 72 gram to make sur
5440: 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 76 65 e that the.** ve
5450: 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 rsion of SQLite
5460: 74 68 61 74 20 69 74 20 69 73 20 6c 69 6e 6b 69 that it is linki
5470: 6e 67 20 61 67 61 69 6e 73 74 20 77 61 73 20 63 ng against was c
5480: 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 ompiled with.**
5490: 74 68 65 20 64 65 73 69 72 65 64 20 73 65 74 74 the desired sett
54a0: 69 6e 67 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 ing of the [SQLI
54b0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d TE_THREADSAFE] m
54c0: 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 acro..**.** This
54d0: 20 69 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79 20 interface only
54e0: 72 65 70 6f 72 74 73 20 6f 6e 20 74 68 65 20 63 reports on the c
54f0: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74 65 ompile-time mute
5500: 78 20 73 65 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 x setting.** of
5510: 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 the [SQLITE_THRE
5520: 41 44 53 41 46 45 5d 20 66 6c 61 67 2e 20 20 49 ADSAFE] flag. I
5530: 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 f SQLite is comp
5540: 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 53 51 4c iled with.** SQL
5550: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 ITE_THREADSAFE=1
5560: 20 74 68 65 6e 20 6d 75 74 65 78 65 73 20 61 72 then mutexes ar
5570: 65 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 e enabled by def
5580: 61 75 6c 74 20 62 75 74 0a 2a 2a 20 63 61 6e 20 ault but.** can
5590: 62 65 20 66 75 6c 6c 79 20 6f 72 20 70 61 72 74 be fully or part
55a0: 69 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 75 ially disabled u
55b0: 73 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 5b sing a call to [
55c0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
55d0: 5d 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 76 65 ].** with the ve
55e0: 72 62 73 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 rbs [SQLITE_CONF
55f0: 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d IG_SINGLETHREAD]
5600: 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 , [SQLITE_CONFIG
5610: 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d 2c 0a 2a _MULTITHREAD],.*
5620: 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e * or [SQLITE_CON
5630: 46 49 47 5f 4d 55 54 45 58 5d 2e 20 20 54 68 65 FIG_MUTEX]. The
5640: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
5650: 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 this function s
5660: 68 6f 77 73 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 hows.** only the
5670: 20 64 65 66 61 75 6c 74 20 63 6f 6d 70 69 6c 65 default compile
5680: 2d 74 69 6d 65 20 73 65 74 74 69 6e 67 2c 20 6e -time setting, n
5690: 6f 74 20 61 6e 79 20 72 75 6e 2d 74 69 6d 65 20 ot any run-time
56a0: 63 68 61 6e 67 65 73 0a 2a 2a 20 74 6f 20 74 68 changes.** to th
56b0: 61 74 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a at setting..**.*
56c0: 2a 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 * See the [threa
56d0: 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d ding mode] docum
56e0: 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 entation for add
56f0: 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
5700: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 ion..**.** INVAR
5710: 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 IANTS:.**.** {H1
5720: 30 31 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 0101} The [sqlit
5730: 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 29 5d e3_threadsafe()]
5740: 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 function shall
5750: 72 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 0a 2a return zero if.*
5760: 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 6f * and o
5770: 6e 6c 79 20 69 66 20 53 51 4c 69 74 65 20 77 61 nly if SQLite wa
5780: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 s compiled with
5790: 6d 75 74 65 78 69 6e 67 20 63 6f 64 65 20 6f 6d mutexing code om
57a0: 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 itted..**.** {H1
57b0: 30 31 30 32 7d 20 54 68 65 20 76 61 6c 75 65 20 0102} The value
57c0: 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 returned by the
57d0: 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 [sqlite3_threads
57e0: 61 66 65 28 29 5d 20 66 75 6e 63 74 69 6f 6e 0a afe()] function.
57f0: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c ** shal
5800: 6c 20 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d l remain the sam
5810: 65 20 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 74 e across calls t
5820: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 o [sqlite3_confi
5830: 67 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f g()]..*/.SQLITE_
5840: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
5850: 74 68 72 65 61 64 73 61 66 65 28 76 6f 69 64 29 threadsafe(void)
5860: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
5870: 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e F: Database Conn
5880: 65 63 74 69 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 ection Handle {H
5890: 31 32 30 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 12000} <S40200>.
58a0: 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 64 61 ** KEYWORDS: {da
58b0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
58c0: 6e 7d 20 7b 64 61 74 61 62 61 73 65 20 63 6f 6e n} {database con
58d0: 6e 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 nections}.**.**
58e0: 45 61 63 68 20 6f 70 65 6e 20 53 51 4c 69 74 65 Each open SQLite
58f0: 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 70 database is rep
5900: 72 65 73 65 6e 74 65 64 20 62 79 20 61 20 70 6f resented by a po
5910: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 inter to an inst
5920: 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6f ance of.** the o
5930: 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 paque structure
5940: 6e 61 6d 65 64 20 22 73 71 6c 69 74 65 33 22 2e named "sqlite3".
5950: 20 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 74 It is useful t
5960: 6f 20 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73 71 o think of an sq
5970: 6c 69 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 lite3.** pointer
5980: 20 61 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 20 as an object.
5990: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 The [sqlite3_ope
59a0: 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f n()], [sqlite3_o
59b0: 70 65 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a pen16()], and.**
59c0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
59d0: 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 2()] interfaces
59e0: 61 72 65 20 69 74 73 20 63 6f 6e 73 74 72 75 63 are its construc
59f0: 74 6f 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69 74 tors, and [sqlit
5a00: 65 33 5f 63 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 e3_close()].** i
5a10: 73 20 69 74 73 20 64 65 73 74 72 75 63 74 6f 72 s its destructor
5a20: 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e . There are man
5a30: 79 20 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63 y other interfac
5a40: 65 73 20 28 73 75 63 68 20 61 73 0a 2a 2a 20 5b es (such as.** [
5a50: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
5a60: 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f v2()], [sqlite3_
5a70: 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
5a80: 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 )], and.** [sqli
5a90: 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 te3_busy_timeout
5aa0: 28 29 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74 20 ()] to name but
5ab0: 74 68 72 65 65 29 20 74 68 61 74 20 61 72 65 20 three) that are
5ac0: 6d 65 74 68 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a methods on an.**
5ad0: 20 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 2e sqlite3 object.
5ae0: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
5af0: 63 74 20 73 71 6c 69 74 65 33 20 73 71 6c 69 74 ct sqlite3 sqlit
5b00: 65 33 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 e3;../*.** CAPI3
5b10: 52 45 46 3a 20 36 34 2d 42 69 74 20 49 6e 74 65 REF: 64-Bit Inte
5b20: 67 65 72 20 54 79 70 65 73 20 7b 48 31 30 32 30 ger Types {H1020
5b30: 30 7d 20 3c 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 0} <S10110>.** K
5b40: 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 5f EYWORDS: sqlite_
5b50: 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 69 6e int64 sqlite_uin
5b60: 74 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 t64.**.** Becaus
5b70: 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 72 e there is no cr
5b80: 6f 73 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61 79 oss-platform way
5b90: 20 74 6f 20 73 70 65 63 69 66 79 20 36 34 2d 62 to specify 64-b
5ba0: 69 74 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 it integer types
5bb0: 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c 75 .** SQLite inclu
5bc0: 64 65 73 20 74 79 70 65 64 65 66 73 20 66 6f 72 des typedefs for
5bd0: 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 61 64-bit signed a
5be0: 6e 64 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 nd unsigned inte
5bf0: 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 gers..**.** The
5c00: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6e sqlite3_int64 an
5c10: 64 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 d sqlite3_uint64
5c20: 20 61 72 65 20 74 68 65 20 70 72 65 66 65 72 72 are the preferr
5c30: 65 64 20 74 79 70 65 20 64 65 66 69 6e 69 74 69 ed type definiti
5c40: 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ons..** The sqli
5c50: 74 65 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c te_int64 and sql
5c60: 69 74 65 5f 75 69 6e 74 36 34 20 74 79 70 65 73 ite_uint64 types
5c70: 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 20 66 are supported f
5c80: 6f 72 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 or backwards.**
5c90: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e compatibility on
5ca0: 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 ly..**.** INVARI
5cb0: 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 ANTS:.**.** {H10
5cc0: 32 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 201} The [sqlite
5cd0: 5f 69 6e 74 36 34 5d 20 61 6e 64 20 5b 73 71 6c _int64] and [sql
5ce0: 69 74 65 33 5f 69 6e 74 36 34 5d 20 74 79 70 65 ite3_int64] type
5cf0: 20 73 68 61 6c 6c 20 73 70 65 63 69 66 79 0a 2a shall specify.*
5d00: 2a 20 20 20 20 20 20 20 20 20 20 61 20 36 34 2d * a 64-
5d10: 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
5d20: 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30 er..**.** {H1020
5d30: 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 5f 75 2} The [sqlite_u
5d40: 69 6e 74 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69 int64] and [sqli
5d50: 74 65 33 5f 75 69 6e 74 36 34 5d 20 74 79 70 65 te3_uint64] type
5d60: 20 73 68 61 6c 6c 20 73 70 65 63 69 66 79 0a 2a shall specify.*
5d70: 2a 20 20 20 20 20 20 20 20 20 20 61 20 36 34 2d * a 64-
5d80: 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 bit unsigned int
5d90: 65 67 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 eger..*/.#ifdef
5da0: 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 SQLITE_INT64_TYP
5db0: 45 0a 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 E. typedef SQLI
5dc0: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 TE_INT64_TYPE sq
5dd0: 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 lite_int64;. ty
5de0: 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 pedef unsigned S
5df0: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 QLITE_INT64_TYPE
5e00: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a sqlite_uint64;.
5e10: 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d #elif defined(_M
5e20: 53 43 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e SC_VER) || defin
5e30: 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 ed(__BORLANDC__)
5e40: 0a 20 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 . typedef __int
5e50: 36 34 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 64 sqlite_int64;
5e60: 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 . typedef unsig
5e70: 6e 65 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 ned __int64 sqli
5e80: 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 te_uint64;.#else
5e90: 0a 20 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 . typedef long
5ea0: 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f long int sqlite_
5eb0: 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 int64;. typedef
5ec0: 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
5ed0: 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 ong int sqlite_u
5ee0: 69 6e 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 int64;.#endif.ty
5ef0: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 pedef sqlite_int
5f00: 36 34 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 64 sqlite3_int64
5f10: 3b 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 ;.typedef sqlite
5f20: 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f _uint64 sqlite3_
5f30: 75 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 uint64;../*.** I
5f40: 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 f compiling for
5f50: 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 a processor that
5f60: 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 lacks floating
5f70: 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a point support,.*
5f80: 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 * substitute int
5f90: 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e eger for floatin
5fa0: 67 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 g-point..*/.#ifd
5fb0: 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
5fc0: 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 LOATING_POINT.#
5fd0: 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 define double sq
5fe0: 6c 69 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 lite3_int64.#end
5ff0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 if../*.** CAPI3R
6000: 45 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 EF: Closing A Da
6010: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f tabase Connectio
6020: 6e 20 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 n {H12010} <S301
6030: 30 30 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 00><S40200>.**.*
6040: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
6050: 73 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 s the destructor
6060: 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 for the [sqlite
6070: 33 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 3] object..**.**
6080: 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 Applications sh
6090: 6f 75 6c 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 ould [sqlite3_fi
60a0: 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a nalize | finaliz
60b0: 65 5d 20 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 e] all [prepared
60c0: 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 statements].**
60d0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f and [sqlite3_blo
60e0: 62 5f 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d b_close | close]
60f0: 20 61 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c all [BLOB handl
6100: 65 73 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 es] associated w
6110: 69 74 68 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 ith.** the [sqli
6120: 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f te3] object prio
6130: 72 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 r to attempting
6140: 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a to close the obj
6150: 65 63 74 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c ect..** The [sql
6160: 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29 ite3_next_stmt()
6170: 5d 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 ] interface can
6180: 62 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 be used to locat
6190: 65 20 61 6c 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 e all.** [prepar
61a0: 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 61 ed statements] a
61b0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
61c0: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
61d0: 63 74 69 6f 6e 5d 20 69 66 20 64 65 73 69 72 65 ction] if desire
61e0: 64 2e 0a 2a 2a 20 54 79 70 69 63 61 6c 20 63 6f d..** Typical co
61f0: 64 65 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 de might look li
6200: 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c ke this:.**.** <
6210: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
6220: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 .** sqlite3_stmt
6230: 20 2a 70 53 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c *pStmt;.** whil
6240: 65 28 20 28 70 53 74 6d 74 20 3d 20 73 71 6c 69 e( (pStmt = sqli
6250: 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 64 62 te3_next_stmt(db
6260: 2c 20 30 29 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 , 0))!=0 ){.** &
6270: 6e 62 73 70 3b 20 20 20 73 71 6c 69 74 65 33 5f nbsp; sqlite3_
6280: 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b finalize(pStmt);
6290: 0a 2a 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c .** }.** </pre><
62a0: 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
62b0: 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 63 ** If [sqlite3_c
62c0: 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b lose()] is invok
62d0: 65 64 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 ed while a trans
62e0: 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a action is open,.
62f0: 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ** the transacti
6300: 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 on is automatica
6310: 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e lly rolled back.
6320: 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
6330: 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 31 31 S:.**.** {H12011
6340: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
6350: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
6360: 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 6c 6c 20 close(C)] shall
6370: 64 65 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 destroy the.**
6380: 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 [databas
6390: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 e connection] ob
63a0: 6a 65 63 74 20 43 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ject C..**.** {H
63b0: 31 32 30 31 32 7d 20 41 20 73 75 63 63 65 73 73 12012} A success
63c0: 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c ful call to [sql
63d0: 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 73 ite3_close(C)] s
63e0: 68 61 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 hall return SQLI
63f0: 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 TE_OK..**.** {H1
6400: 32 30 31 33 7d 20 41 20 73 75 63 63 65 73 73 66 2013} A successf
6410: 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ul call to [sqli
6420: 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 73 68 te3_close(C)] sh
6430: 61 6c 6c 20 72 65 6c 65 61 73 65 20 61 6c 6c 0a all release all.
6440: 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f ** memo
6450: 72 79 20 61 6e 64 20 73 79 73 74 65 6d 20 72 65 ry and system re
6460: 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 sources associat
6470: 65 64 20 77 69 74 68 20 5b 64 61 74 61 62 61 73 ed with [databas
6480: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a e connection].**
6490: 20 20 20 20 20 20 20 20 20 20 43 2e 0a 2a 2a 0a C..**.
64a0: 2a 2a 20 7b 48 31 32 30 31 34 7d 20 41 20 63 61 ** {H12014} A ca
64b0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
64c0: 6c 6f 73 65 28 43 29 5d 20 6f 6e 20 61 20 5b 64 lose(C)] on a [d
64d0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
64e0: 6f 6e 5d 20 43 20 74 68 61 74 0a 2a 2a 20 20 20 on] C that.**
64f0: 20 20 20 20 20 20 20 68 61 73 20 6f 6e 65 20 6f has one o
6500: 72 20 6d 6f 72 65 20 6f 70 65 6e 20 5b 70 72 65 r more open [pre
6510: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
6520: 5d 20 73 68 61 6c 6c 20 66 61 69 6c 20 77 69 74 ] shall fail wit
6530: 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e h.** an
6540: 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 [SQLITE_BUSY] e
6550: 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a rror code..**.**
6560: 20 7b 48 31 32 30 31 35 7d 20 41 20 63 61 6c 6c {H12015} A call
6570: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f to [sqlite3_clo
6580: 73 65 28 43 29 5d 20 77 68 65 72 65 20 43 20 69 se(C)] where C i
6590: 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
65a0: 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 shall.**
65b0: 20 20 20 62 65 20 61 20 68 61 72 6d 6c 65 73 73 be a harmless
65c0: 20 6e 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 no-op returning
65d0: 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a SQLITE_OK..**.*
65e0: 2a 20 7b 48 31 32 30 31 39 7d 20 57 68 65 6e 20 * {H12019} When
65f0: 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 [sqlite3_close(C
6600: 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e )] is invoked on
6610: 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e a [database con
6620: 6e 65 63 74 69 6f 6e 5d 20 43 0a 2a 2a 20 20 20 nection] C.**
6630: 20 20 20 20 20 20 20 74 68 61 74 20 68 61 73 20 that has
6640: 61 20 70 65 6e 64 69 6e 67 20 74 72 61 6e 73 61 a pending transa
6650: 63 74 69 6f 6e 2c 20 74 68 65 20 74 72 61 6e 73 ction, the trans
6660: 61 63 74 69 6f 6e 20 73 68 61 6c 6c 20 62 65 0a action shall be.
6670: 2a 2a 20 20 20 20 20 20 20 20 20 20 72 6f 6c 6c ** roll
6680: 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 41 ed back..**.** A
6690: 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a SSUMPTIONS:.**.*
66a0: 2a 20 7b 41 31 32 30 31 36 7d 20 54 68 65 20 43 * {A12016} The C
66b0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
66c0: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d qlite3_close(C)]
66d0: 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
66e0: 61 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 a NULL.**
66f0: 20 20 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 6e pointer or an
6700: 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 [sqlite3] objec
6710: 74 20 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 6e t pointer obtain
6720: 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 ed.** f
6730: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 rom [sqlite3_ope
6740: 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f n()], [sqlite3_o
6750: 70 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 pen16()], or.**
6760: 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
6770: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 3_open_v2()], an
6780: 64 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 d not previously
6790: 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 closed..*/.SQLI
67a0: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
67b0: 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 e3_close(sqlite3
67c0: 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 *);../*.** The
67d0: 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62 type for a callb
67e0: 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a ack function..**
67f0: 20 54 68 69 73 20 69 73 20 6c 65 67 61 63 79 20 This is legacy
6800: 61 6e 64 20 64 65 70 72 65 63 61 74 65 64 2e 20 and deprecated.
6810: 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 It is included
6820: 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a for historical.*
6830: 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 * compatibility
6840: 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d and is not docum
6850: 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 ented..*/.typede
6860: 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f f int (*sqlite3_
6870: 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c callback)(void*,
6880: 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72 int,char**, char
6890: 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 **);../*.** CAPI
68a0: 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51 3REF: One-Step Q
68b0: 75 65 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49 uery Execution I
68c0: 6e 74 65 72 66 61 63 65 20 7b 48 31 32 31 30 30 nterface {H12100
68d0: 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a } <S10000>.**.**
68e0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 The sqlite3_exe
68f0: 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 c() interface is
6900: 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 61 a convenient wa
6910: 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 y of running one
6920: 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 or more.** SQL
6930: 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68 6f statements witho
6940: 75 74 20 68 61 76 69 6e 67 20 74 6f 20 77 72 69 ut having to wri
6950: 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 6f te a lot of C co
6960: 64 65 2e 20 20 54 68 65 20 55 54 46 2d 38 20 65 de. The UTF-8 e
6970: 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 74 ncoded.** SQL st
6980: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73 atements are pas
6990: 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 73 65 sed in as the se
69a0: 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 cond parameter t
69b0: 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 o sqlite3_exec()
69c0: 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 ..** The stateme
69d0: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 nts are evaluate
69e0: 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e 74 d one by one unt
69f0: 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65 72 72 il either an err
6a00: 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 or or.** an inte
6a10: 72 72 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74 rrupt is encount
6a20: 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 74 ered, or until t
6a30: 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e 65 hey are all done
6a40: 2e 20 20 54 68 65 20 33 72 64 20 70 61 72 61 6d . The 3rd param
6a50: 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 eter.** is an op
6a60: 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 tional callback
6a70: 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 that is invoked
6a80: 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f once for each ro
6a90: 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79 0a 2a w of any query.*
6aa0: 2a 20 72 65 73 75 6c 74 73 20 70 72 6f 64 75 63 * results produc
6ab0: 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 ed by the SQL st
6ac0: 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 35 atements. The 5
6ad0: 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 65 6c th parameter tel
6ae0: 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 77 ls where.** to w
6af0: 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d rite any error m
6b00: 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 essages..**.** T
6b10: 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
6b20: 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 68 72 passed back thr
6b30: 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72 ough the 5th par
6b40: 61 6d 65 74 65 72 20 69 73 20 68 65 6c 64 0a 2a ameter is held.*
6b50: 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 * in memory obta
6b60: 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 ined from [sqlit
6b70: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 e3_malloc()]. T
6b80: 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79 o avoid a memory
6b90: 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 leak,.** the ca
6ba0: 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f lling applicatio
6bb0: 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 n should call [s
6bc0: 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f qlite3_free()] o
6bd0: 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 6d n any error.** m
6be0: 65 73 73 61 67 65 20 72 65 74 75 72 6e 65 64 20 essage returned
6bf0: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 through the 5th
6c00: 70 61 72 61 6d 65 74 65 72 20 77 68 65 6e 20 69 parameter when i
6c10: 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 t has finished u
6c20: 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 6f sing.** the erro
6c30: 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a r message..**.**
6c40: 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 If the SQL stat
6c50: 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 32 6e 64 ement in the 2nd
6c60: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 parameter is NU
6c70: 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 LL or an empty s
6c80: 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 74 tring.** or a st
6c90: 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ring containing
6ca0: 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 63 65 20 only whitespace
6cb0: 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 and comments, th
6cc0: 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 en no SQL.** sta
6cd0: 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c tements are eval
6ce0: 75 61 74 65 64 20 61 6e 64 20 74 68 65 20 64 61 uated and the da
6cf0: 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 63 68 tabase is not ch
6d00: 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 anged..**.** The
6d10: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
6d20: 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 interface is imp
6d30: 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 6d lemented in term
6d40: 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 s of.** [sqlite3
6d50: 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 _prepare_v2()],
6d60: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
6d70: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 , and [sqlite3_f
6d80: 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 inalize()]..** T
6d90: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 he sqlite3_exec(
6da0: 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e ) routine does n
6db0: 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 othing to the da
6dc0: 74 61 62 61 73 65 20 74 68 61 74 20 63 61 6e 6e tabase that cann
6dd0: 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 ot be done.** by
6de0: 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
6df0: 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 e_v2()], [sqlite
6e00: 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 5b 3_step()], and [
6e10: 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
6e20: 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 ()]..**.** INVAR
6e30: 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 IANTS:.**.** {H1
6e40: 32 31 30 31 7d 20 41 20 73 75 63 63 65 73 73 66 2101} A successf
6e50: 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 ul invocation of
6e60: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 [sqlite3_exec(D
6e70: 2c 53 2c 43 2c 41 2c 45 29 5d 0a 2a 2a 20 20 20 ,S,C,A,E)].**
6e80: 20 20 20 20 20 20 20 73 68 61 6c 6c 20 73 65 71 shall seq
6e90: 75 65 6e 74 69 61 6c 6c 79 20 65 76 61 6c 75 61 uentially evalua
6ea0: 74 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 55 54 te all of the UT
6eb0: 46 2d 38 20 65 6e 63 6f 64 65 64 2c 0a 2a 2a 20 F-8 encoded,.**
6ec0: 20 20 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c semicol
6ed0: 6f 6e 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c on-separated SQL
6ee0: 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 statements in t
6ef0: 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 he zero-terminat
6f00: 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 ed.** s
6f10: 74 72 69 6e 67 20 53 20 77 69 74 68 69 6e 20 74 tring S within t
6f20: 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 he context of th
6f30: 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
6f40: 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a ection] D..**.**
6f50: 20 7b 48 31 32 31 30 32 7d 20 49 66 20 74 68 65 {H12102} If the
6f60: 20 53 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 S parameter to
6f70: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c [sqlite3_exec(D,
6f80: 53 2c 43 2c 41 2c 45 29 5d 20 69 73 20 4e 55 4c S,C,A,E)] is NUL
6f90: 4c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 L then.**
6fa0: 20 20 20 74 68 65 20 61 63 74 69 6f 6e 73 20 6f the actions o
6fb0: 66 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 f the interface
6fc0: 73 68 61 6c 6c 20 62 65 20 74 68 65 20 73 61 6d shall be the sam
6fd0: 65 20 61 73 20 69 66 20 74 68 65 0a 2a 2a 20 20 e as if the.**
6fe0: 20 20 20 20 20 20 20 20 53 20 70 61 72 61 6d 65 S parame
6ff0: 74 65 72 20 77 65 72 65 20 61 6e 20 65 6d 70 74 ter were an empt
7000: 79 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 y string..**.**
7010: 7b 48 31 32 31 30 34 7d 20 54 68 65 20 72 65 74 {H12104} The ret
7020: 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 urn value of [sq
7030: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 lite3_exec()] sh
7040: 61 6c 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 4f all be [SQLITE_O
7050: 4b 5d 20 69 66 20 61 6c 6c 0a 2a 2a 20 20 20 20 K] if all.**
7060: 20 20 20 20 20 20 53 51 4c 20 73 74 61 74 65 6d SQL statem
7070: 65 6e 74 73 20 72 75 6e 20 73 75 63 63 65 73 73 ents run success
7080: 66 75 6c 6c 79 20 61 6e 64 20 74 6f 20 63 6f 6d fully and to com
7090: 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b pletion..**.** {
70a0: 48 31 32 31 30 35 7d 20 54 68 65 20 72 65 74 75 H12105} The retu
70b0: 72 6e 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c rn value of [sql
70c0: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 ite3_exec()] sha
70d0: 6c 6c 20 62 65 20 61 6e 20 61 70 70 72 6f 70 72 ll be an appropr
70e0: 69 61 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 iate.**
70f0: 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 non-zero [error
7100: 20 63 6f 64 65 5d 20 69 66 20 61 6e 79 20 53 51 code] if any SQ
7110: 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69 6c L statement fail
7120: 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 30 37 s..**.** {H12107
7130: 7d 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 } If one or more
7140: 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 of the SQL stat
7150: 65 6d 65 6e 74 73 20 68 61 6e 64 65 64 20 74 6f ements handed to
7160: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
7170: 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 ].** re
7180: 74 75 72 6e 20 72 65 73 75 6c 74 73 20 61 6e 64 turn results and
7190: 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 the 3rd paramet
71a0: 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 er is not NULL,
71b0: 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 then.**
71c0: 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 the callback fu
71d0: 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 nction specified
71e0: 20 62 79 20 74 68 65 20 33 72 64 20 70 61 72 61 by the 3rd para
71f0: 6d 65 74 65 72 20 73 68 61 6c 6c 20 62 65 0a 2a meter shall be.*
7200: 2a 20 20 20 20 20 20 20 20 20 20 69 6e 76 6f 6b * invok
7210: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ed once for each
7220: 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a row of result..
7230: 2a 2a 0a 2a 2a 20 7b 48 31 32 31 31 30 7d 20 49 **.** {H12110} I
7240: 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 f the callback r
7250: 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 eturns a non-zer
7260: 6f 20 76 61 6c 75 65 20 74 68 65 6e 20 5b 73 71 o value then [sq
7270: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a lite3_exec()].**
7280: 20 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 shall
7290: 61 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 abort the SQL st
72a0: 61 74 65 6d 65 6e 74 20 69 74 20 69 73 20 63 75 atement it is cu
72b0: 72 72 65 6e 74 6c 79 20 65 76 61 6c 75 61 74 69 rrently evaluati
72c0: 6e 67 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ng,.**
72d0: 73 6b 69 70 20 61 6c 6c 20 73 75 62 73 65 71 75 skip all subsequ
72e0: 65 6e 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ent SQL statemen
72f0: 74 73 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 5b ts, and return [
7300: 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a SQLITE_ABORT]..*
7310: 2a 0a 2a 2a 20 7b 48 31 32 31 31 33 7d 20 54 68 *.** {H12113} Th
7320: 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 e [sqlite3_exec(
7330: 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c )] routine shall
7340: 20 70 61 73 73 20 69 74 73 20 34 74 68 20 70 61 pass its 4th pa
7350: 72 61 6d 65 74 65 72 20 74 68 72 6f 75 67 68 0a rameter through.
7360: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 73 20 74 ** as t
7370: 68 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 he 1st parameter
7380: 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b of the callback
7390: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 31 36 7d ..**.** {H12116}
73a0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 The [sqlite3_ex
73b0: 65 63 28 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 ec()] routine sh
73c0: 61 6c 6c 20 73 65 74 20 74 68 65 20 32 6e 64 20 all set the 2nd
73d0: 70 61 72 61 6d 65 74 65 72 20 6f 66 20 69 74 73 parameter of its
73e0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c .** cal
73f0: 6c 62 61 63 6b 20 74 6f 20 62 65 20 74 68 65 20 lback to be the
7400: 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
7410: 73 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 s in the current
7420: 20 72 6f 77 20 6f 66 0a 2a 2a 20 20 20 20 20 20 row of.**
7430: 20 20 20 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a result..**.*
7440: 2a 20 7b 48 31 32 31 31 39 7d 20 54 68 65 20 5b * {H12119} The [
7450: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 sqlite3_exec()]
7460: 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 73 65 routine shall se
7470: 74 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 t the 3rd parame
7480: 74 65 72 20 6f 66 20 69 74 73 0a 2a 2a 20 20 20 ter of its.**
7490: 20 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 callback
74a0: 74 6f 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f to be an array o
74b0: 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 f pointers to st
74c0: 72 69 6e 67 73 20 68 6f 6c 64 69 6e 67 20 74 68 rings holding th
74d0: 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 e.** va
74e0: 6c 75 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f lues for each co
74f0: 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 lumn in the curr
7500: 65 6e 74 20 72 65 73 75 6c 74 20 73 65 74 20 72 ent result set r
7510: 6f 77 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 20 ow as.**
7520: 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 obtained from
7530: 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
7540: 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b text()]..**.** {
7550: 48 31 32 31 32 32 7d 20 54 68 65 20 5b 73 71 6c H12122} The [sql
7560: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 72 6f 75 ite3_exec()] rou
7570: 74 69 6e 65 20 73 68 61 6c 6c 20 73 65 74 20 74 tine shall set t
7580: 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 he 4th parameter
7590: 20 6f 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 20 of its.**
75a0: 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 callback to
75b0: 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 be an array of p
75c0: 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e ointers to strin
75d0: 67 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a gs holding the.*
75e0: 2a 20 20 20 20 20 20 20 20 20 20 6e 61 6d 65 73 * names
75f0: 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d of result colum
7600: 6e 73 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66 ns as obtained f
7610: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c rom [sqlite3_col
7620: 75 6d 6e 5f 6e 61 6d 65 28 29 5d 2e 0a 2a 2a 0a umn_name()]..**.
7630: 2a 2a 20 7b 48 31 32 31 32 35 7d 20 49 66 20 74 ** {H12125} If t
7640: 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 he 3rd parameter
7650: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 to [sqlite3_exe
7660: 63 28 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 c()] is NULL the
7670: 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 n.** [s
7680: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 qlite3_exec()] s
7690: 68 61 6c 6c 20 73 69 6c 65 6e 74 6c 79 20 64 69 hall silently di
76a0: 73 63 61 72 64 20 71 75 65 72 79 20 72 65 73 75 scard query resu
76b0: 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 lts..**.** {H121
76c0: 33 31 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 31} If an error
76d0: 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 61 72 occurs while par
76e0: 73 69 6e 67 20 6f 72 20 65 76 61 6c 75 61 74 69 sing or evaluati
76f0: 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20 53 51 ng any of the SQ
7700: 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 L.** st
7710: 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 atements in the
7720: 53 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 5b S parameter of [
7730: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 sqlite3_exec(D,S
7740: 2c 43 2c 41 2c 45 29 5d 20 61 6e 64 20 69 66 0a ,C,A,E)] and if.
7750: 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
7760: 45 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e E parameter is n
7770: 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b 73 ot NULL, then [s
7780: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 qlite3_exec()] s
7790: 68 61 6c 6c 20 73 74 6f 72 65 0a 2a 2a 20 20 20 hall store.**
77a0: 20 20 20 20 20 20 20 69 6e 20 2a 45 20 61 6e 20 in *E an
77b0: 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f appropriate erro
77c0: 72 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 r message writte
77d0: 6e 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 n into memory ob
77e0: 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 tained.**
77f0: 20 20 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 from [sqlite3
7800: 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a _malloc()]..**.*
7810: 2a 20 7b 48 31 32 31 33 34 7d 20 54 68 65 20 5b * {H12134} The [
7820: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 sqlite3_exec(D,S
7830: 2c 43 2c 41 2c 45 29 5d 20 72 6f 75 74 69 6e 65 ,C,A,E)] routine
7840: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 76 shall set the v
7850: 61 6c 75 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 alue of.**
7860: 20 20 20 20 2a 45 20 74 6f 20 4e 55 4c 4c 20 69 *E to NULL i
7870: 66 20 45 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 f E is not NULL
7880: 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 6e 6f and there are no
7890: 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b errors..**.** {
78a0: 48 31 32 31 33 37 7d 20 54 68 65 20 5b 73 71 6c H12137} The [sql
78b0: 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c ite3_exec(D,S,C,
78c0: 41 2c 45 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 A,E)] function s
78d0: 68 61 6c 6c 20 73 65 74 20 74 68 65 20 5b 65 72 hall set the [er
78e0: 72 6f 72 20 63 6f 64 65 5d 0a 2a 2a 20 20 20 20 ror code].**
78f0: 20 20 20 20 20 20 61 6e 64 20 6d 65 73 73 61 67 and messag
7900: 65 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 e accessible via
7910: 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 [sqlite3_errcod
7920: 65 28 29 5d 2c 20 0a 2a 2a 20 20 20 20 20 20 20 e()], .**
7930: 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 [sqlite3_exte
7940: 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c nded_errcode()],
7950: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
7960: 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c lite3_errmsg()],
7970: 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 and [sqlite3_er
7980: 72 6d 73 67 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a rmsg16()]..**.**
7990: 20 7b 48 31 32 31 33 38 7d 20 49 66 20 74 68 65 {H12138} If the
79a0: 20 53 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 S parameter to
79b0: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c [sqlite3_exec(D,
79c0: 53 2c 43 2c 41 2c 45 29 5d 20 69 73 20 4e 55 4c S,C,A,E)] is NUL
79d0: 4c 20 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 L or an.**
79e0: 20 20 20 20 65 6d 70 74 79 20 73 74 72 69 6e 67 empty string
79f0: 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 or contains not
7a00: 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 hing other than
7a10: 77 68 69 74 65 73 70 61 63 65 2c 20 63 6f 6d 6d whitespace, comm
7a20: 65 6e 74 73 2c 0a 2a 2a 20 20 20 20 20 20 20 20 ents,.**
7a30: 20 20 61 6e 64 2f 6f 72 20 73 65 6d 69 63 6f 6c and/or semicol
7a40: 6f 6e 73 2c 20 74 68 65 6e 20 72 65 73 75 6c 74 ons, then result
7a50: 73 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 65 72 s of [sqlite3_er
7a60: 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 rcode()],.**
7a70: 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 [sqlite3_e
7a80: 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 xtended_errcode(
7a90: 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 )],.**
7aa0: 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 [sqlite3_errmsg(
7ab0: 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 )], and [sqlite3
7ac0: 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a 20 _errmsg16()].**
7ad0: 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 72 shall r
7ae0: 65 73 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65 eset to indicate
7af0: 20 6e 6f 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a no errors..**.*
7b00: 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a * ASSUMPTIONS:.*
7b10: 2a 0a 2a 2a 20 7b 41 31 32 31 34 31 7d 20 54 68 *.** {A12141} Th
7b20: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
7b30: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 r to [sqlite3_ex
7b40: 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e ec()] must be an
7b50: 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a valid and open.
7b60: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61 74 ** [dat
7b70: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
7b80: 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 32 ]..**.** {A12142
7b90: 7d 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 } The database c
7ba0: 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e onnection must n
7bb0: 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 ot be closed whi
7bc0: 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b le.** [
7bd0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 sqlite3_exec()]
7be0: 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a is running..**.*
7bf0: 2a 20 7b 41 31 32 31 34 33 7d 20 54 68 65 20 63 * {A12143} The c
7c00: 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
7c10: 73 68 6f 75 6c 64 20 75 73 65 20 5b 73 71 6c 69 should use [sqli
7c20: 74 65 33 5f 66 72 65 65 28 29 5d 20 74 6f 20 66 te3_free()] to f
7c30: 72 65 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ree.**
7c40: 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 the memory that
7c50: 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 66 74 20 *errmsg is left
7c60: 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 65 pointing at once
7c70: 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 20 20 the error.**
7c80: 20 20 20 20 20 20 20 6d 65 73 73 61 67 65 20 69 message i
7c90: 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64 s no longer need
7ca0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 ed..**.** {A1214
7cb0: 35 7d 20 54 68 65 20 53 51 4c 20 73 74 61 74 65 5} The SQL state
7cc0: 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65 ment text in the
7cd0: 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 2nd parameter t
7ce0: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 o [sqlite3_exec(
7cf0: 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d )].** m
7d00: 75 73 74 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 ust remain uncha
7d10: 6e 67 65 64 20 77 68 69 6c 65 20 5b 73 71 6c 69 nged while [sqli
7d20: 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72 te3_exec()] is r
7d30: 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 unning..*/.SQLIT
7d40: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
7d50: 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65 3_exec(. sqlite
7d60: 33 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 3*,
7d70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d80: 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 /* An open
7d90: 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f database */. co
7da0: 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c 20 20 nst char *sql,
7db0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7dc0: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 /* SQL
7dd0: 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 to be evaluated
7de0: 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 61 6c 6c 62 */. int (*callb
7df0: 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 ack)(void*,int,c
7e00: 68 61 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c 20 20 har**,char**),
7e10: 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 /* Callback func
7e20: 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a tion */. void *
7e30: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
7e40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e50: 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 /* 1st argu
7e60: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b ment to callback
7e70: 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 65 72 72 */. char **err
7e80: 6d 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20 msg
7e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7ea0: 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 /* Error msg wr
7eb0: 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b itten here */.);
7ec0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
7ed0: 3a 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b : Result Codes {
7ee0: 48 31 30 32 31 30 7d 20 3c 53 31 30 37 30 30 3e H10210} <S10700>
7ef0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 .** KEYWORDS: SQ
7f00: 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f 72 20 63 LITE_OK {error c
7f10: 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63 6f 64 65 ode} {error code
7f20: 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 s}.** KEYWORDS:
7f30: 7b 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 72 {result code} {r
7f40: 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a esult codes}.**.
7f50: 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 65 20 66 ** Many SQLite f
7f60: 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 unctions return
7f70: 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c an integer resul
7f80: 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 t code from the
7f90: 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 68 65 72 set shown.** her
7fa0: 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e e in order to in
7fb0: 64 69 63 61 74 65 73 20 73 75 63 63 65 73 73 20 dicates success
7fc0: 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a or failure..**.*
7fd0: 2a 20 4e 65 77 20 65 72 72 6f 72 20 63 6f 64 65 * New error code
7fe0: 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 s may be added i
7ff0: 6e 20 66 75 74 75 72 65 20 76 65 72 73 69 6f 6e n future version
8000: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a s of SQLite..**.
8010: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 ** See also: [SQ
8020: 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 LITE_IOERR_READ
8030: 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c | extended resul
8040: 74 20 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64 65 66 t codes].*/.#def
8050: 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 ine SQLITE_OK
8060: 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20 53 0 /* S
8070: 75 63 63 65 73 73 66 75 6c 20 72 65 73 75 6c 74 uccessful result
8080: 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e 67 */./* beginning
8090: 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 -of-error-codes
80a0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
80b0: 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20 20 31 E_ERROR 1
80c0: 20 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f 72 20 /* SQL error
80d0: 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 or missing datab
80e0: 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ase */.#define S
80f0: 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20 QLITE_INTERNAL
8100: 20 20 20 32 20 20 20 2f 2a 20 49 6e 74 65 72 6e 2 /* Intern
8110: 61 6c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 al logic error i
8120: 6e 20 53 51 4c 69 74 65 20 2a 2f 0a 23 64 65 66 n SQLite */.#def
8130: 69 6e 65 20 53 51 4c 49 54 45 5f 50 45 52 4d 20 ine SQLITE_PERM
8140: 20 20 20 20 20 20 20 20 33 20 20 20 2f 2a 20 41 3 /* A
8150: 63 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e ccess permission
8160: 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 denied */.#defi
8170: 6e 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 ne SQLITE_ABORT
8180: 20 20 20 20 20 20 20 34 20 20 20 2f 2a 20 43 61 4 /* Ca
8190: 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 72 llback routine r
81a0: 65 71 75 65 73 74 65 64 20 61 6e 20 61 62 6f 72 equested an abor
81b0: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c t */.#define SQL
81c0: 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20 20 ITE_BUSY
81d0: 20 35 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 5 /* The data
81e0: 62 61 73 65 20 66 69 6c 65 20 69 73 20 6c 6f 63 base file is loc
81f0: 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ked */.#define S
8200: 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20 QLITE_LOCKED
8210: 20 20 20 36 20 20 20 2f 2a 20 41 20 74 61 62 6c 6 /* A tabl
8220: 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
8230: 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 e is locked */.#
8240: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f define SQLITE_NO
8250: 4d 45 4d 20 20 20 20 20 20 20 20 37 20 20 20 2f MEM 7 /
8260: 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 * A malloc() fai
8270: 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 led */.#define S
8280: 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20 QLITE_READONLY
8290: 20 20 20 38 20 20 20 2f 2a 20 41 74 74 65 6d 70 8 /* Attemp
82a0: 74 20 74 6f 20 77 72 69 74 65 20 61 20 72 65 61 t to write a rea
82b0: 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a donly database *
82c0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
82d0: 5f 49 4e 54 45 52 52 55 50 54 20 20 20 20 39 20 _INTERRUPT 9
82e0: 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 74 /* Operation t
82f0: 65 72 6d 69 6e 61 74 65 64 20 62 79 20 73 71 6c erminated by sql
8300: 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
8310: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8320: 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20 31 30 E_IOERR 10
8330: 20 20 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 64 20 /* Some kind
8340: 6f 66 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f of disk I/O erro
8350: 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 23 64 r occurred */.#d
8360: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 efine SQLITE_COR
8370: 52 55 50 54 20 20 20 20 20 31 31 20 20 20 2f 2a RUPT 11 /*
8380: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 64 69 The database di
8390: 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 sk image is malf
83a0: 6f 72 6d 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 ormed */.#define
83b0: 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 SQLITE_NOTFOUND
83c0: 20 20 20 20 31 32 20 20 20 2f 2a 20 4e 4f 54 20 12 /* NOT
83d0: 55 53 45 44 2e 20 54 61 62 6c 65 20 6f 72 20 72 USED. Table or r
83e0: 65 63 6f 72 64 20 6e 6f 74 20 66 6f 75 6e 64 20 ecord not found
83f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8400: 45 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 31 33 E_FULL 13
8410: 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6f 6e 20 /* Insertion
8420: 66 61 69 6c 65 64 20 62 65 63 61 75 73 65 20 64 failed because d
8430: 61 74 61 62 61 73 65 20 69 73 20 66 75 6c 6c 20 atabase is full
8440: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8450: 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20 31 34 E_CANTOPEN 14
8460: 20 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 /* Unable to
8470: 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 open the databas
8480: 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e e file */.#defin
8490: 65 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f e SQLITE_PROTOCO
84a0: 4c 20 20 20 20 31 35 20 20 20 2f 2a 20 4e 4f 54 L 15 /* NOT
84b0: 20 55 53 45 44 2e 20 44 61 74 61 62 61 73 65 20 USED. Database
84c0: 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72 lock protocol er
84d0: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ror */.#define S
84e0: 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20 QLITE_EMPTY
84f0: 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61 16 /* Databa
8500: 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 se is empty */.#
8510: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43 define SQLITE_SC
8520: 48 45 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f HEMA 17 /
8530: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73 * The database s
8540: 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f chema changed */
8550: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8560: 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38 20 20 TOOBIG 18
8570: 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c /* String or BL
8580: 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a 65 20 OB exceeds size
8590: 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65 limit */.#define
85a0: 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
85b0: 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72 NT 19 /* Abor
85c0: 74 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61 t due to constra
85d0: 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f int violation */
85e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
85f0: 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20 MISMATCH 20
8600: 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69 /* Data type mi
8610: 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e smatch */.#defin
8620: 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 e SQLITE_MISUSE
8630: 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62 21 /* Lib
8640: 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72 rary used incorr
8650: 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 ectly */.#define
8660: 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20 SQLITE_NOLFS
8670: 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73 22 /* Uses
8680: 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74 OS features not
8690: 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f supported on ho
86a0: 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 st */.#define SQ
86b0: 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20 20 LITE_AUTH
86c0: 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 23 /* Authori
86d0: 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f zation denied */
86e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
86f0: 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34 20 20 FORMAT 24
8700: 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61 /* Auxiliary da
8710: 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 tabase format er
8720: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ror */.#define S
8730: 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20 QLITE_RANGE
8740: 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61 25 /* 2nd pa
8750: 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
8760: 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72 e3_bind out of r
8770: 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ange */.#define
8780: 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20 SQLITE_NOTADB
8790: 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20 26 /* File
87a0: 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e opened that is n
87b0: 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69 ot a database fi
87c0: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 le */.#define SQ
87d0: 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20 LITE_ROW
87e0: 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 100 /* sqlite3
87f0: 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74 _step() has anot
8800: 68 65 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f her row ready */
8810: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8820: 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30 31 20 DONE 101
8830: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 /* sqlite3_step
8840: 28 29 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 () has finished
8850: 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20 executing */./*
8860: 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 end-of-error-cod
8870: 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 es */../*.** CAP
8880: 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20 I3REF: Extended
8890: 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 Result Codes {H1
88a0: 30 32 32 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 0220} <S10700>.*
88b0: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 * KEYWORDS: {ext
88c0: 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 ended error code
88d0: 7d 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f } {extended erro
88e0: 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 r codes}.** KEYW
88f0: 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 ORDS: {extended
8900: 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 65 78 result code} {ex
8910: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
8920: 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 des}.**.** In it
8930: 73 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 s default config
8940: 75 72 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 uration, SQLite
8950: 41 50 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 API routines ret
8960: 75 72 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e urn one of 26 in
8970: 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 teger.** [SQLITE
8980: 5f 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 _OK | result cod
8990: 65 73 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 es]. However, e
89a0: 78 70 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 xperience has sh
89b0: 6f 77 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66 own that many of
89c0: 0a 2a 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74 .** these result
89d0: 20 63 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63 codes are too c
89e0: 6f 61 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20 oarse-grained.
89f0: 54 68 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76 They do not prov
8a00: 69 64 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69 ide as.** much i
8a10: 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
8a20: 20 70 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f problems as pro
8a30: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c grammers might l
8a40: 69 6b 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f ike. In an effo
8a50: 72 74 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73 rt to.** address
8a60: 20 74 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72 this, newer ver
8a70: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
8a80: 28 76 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61 (version 3.3.8 a
8a90: 6e 64 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64 nd later) includ
8aa0: 65 0a 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72 e.** support for
8ab0: 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75 additional resu
8ac0: 6c 74 20 63 6f 64 65 73 20 74 68 61 74 20 70 72 lt codes that pr
8ad0: 6f 76 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 ovide more detai
8ae0: 6c 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a led information.
8af0: 2a 2a 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e ** about errors.
8b00: 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 The extended re
8b10: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65 sult codes are e
8b20: 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c nabled or disabl
8b30: 65 64 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 20 64 ed.** on a per d
8b40: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
8b50: 6f 6e 20 62 61 73 69 73 20 75 73 69 6e 67 20 74 on basis using t
8b60: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 he.** [sqlite3_e
8b70: 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 xtended_result_c
8b80: 6f 64 65 73 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a odes()] API..**.
8b90: 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 ** Some of the a
8ba0: 76 61 69 6c 61 62 6c 65 20 65 78 74 65 6e 64 65 vailable extende
8bb0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 d result codes a
8bc0: 72 65 20 6c 69 73 74 65 64 20 68 65 72 65 2e 0a re listed here..
8bd0: 2a 2a 20 4f 6e 65 20 6d 61 79 20 65 78 70 65 63 ** One may expec
8be0: 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
8bf0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
8c00: 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 65 78 codes will be ex
8c10: 70 61 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 69 6d pand.** over tim
8c20: 65 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 e. Software tha
8c30: 74 20 75 73 65 73 20 65 78 74 65 6e 64 65 64 20 t uses extended
8c40: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 73 68 6f result codes sho
8c50: 75 6c 64 20 65 78 70 65 63 74 0a 2a 2a 20 74 6f uld expect.** to
8c60: 20 73 65 65 20 6e 65 77 20 72 65 73 75 6c 74 20 see new result
8c70: 63 6f 64 65 73 20 69 6e 20 66 75 74 75 72 65 20 codes in future
8c80: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 releases of SQLi
8c90: 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 te..**.** The SQ
8ca0: 4c 49 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63 LITE_OK result c
8cb0: 6f 64 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 ode will never b
8cc0: 65 20 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20 e extended. It
8cd0: 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 will always.** b
8ce0: 65 20 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a e exactly zero..
8cf0: 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
8d00: 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 33 7d :.**.** {H10223}
8d10: 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 The symbolic na
8d20: 6d 65 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64 me for an extend
8d30: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 20 73 ed result code s
8d40: 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a hall contains.**
8d50: 20 20 20 20 20 20 20 20 20 20 61 20 72 65 6c 61 a rela
8d60: 74 65 64 20 70 72 69 6d 61 72 79 20 72 65 73 75 ted primary resu
8d70: 6c 74 20 63 6f 64 65 20 61 73 20 61 20 70 72 65 lt code as a pre
8d80: 66 69 78 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 fix..**.** {H102
8d90: 32 34 7d 20 50 72 69 6d 61 72 79 20 72 65 73 75 24} Primary resu
8da0: 6c 74 20 63 6f 64 65 20 6e 61 6d 65 73 20 73 68 lt code names sh
8db0: 61 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 20 73 69 all contain a si
8dc0: 6e 67 6c 65 20 22 5f 22 20 63 68 61 72 61 63 74 ngle "_" charact
8dd0: 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 er..**.** {H1022
8de0: 35 7d 20 45 78 74 65 6e 64 65 64 20 72 65 73 75 5} Extended resu
8df0: 6c 74 20 63 6f 64 65 20 6e 61 6d 65 73 20 73 68 lt code names sh
8e00: 61 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 77 6f 20 all contain two
8e10: 6f 72 20 6d 6f 72 65 20 22 5f 22 20 63 68 61 72 or more "_" char
8e20: 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 acters..**.** {H
8e30: 31 30 32 32 36 7d 20 54 68 65 20 6e 75 6d 65 72 10226} The numer
8e40: 69 63 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 65 ic value of an e
8e50: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
8e60: 6f 64 65 20 73 68 61 6c 6c 20 63 6f 6e 74 61 69 ode shall contai
8e70: 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 n the.**
8e80: 20 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 numeric value
8e90: 6f 66 20 69 74 73 20 63 6f 72 72 65 73 70 6f 6e of its correspon
8ea0: 64 69 6e 67 20 70 72 69 6d 61 72 79 20 72 65 73 ding primary res
8eb0: 75 6c 74 20 63 6f 64 65 20 69 6e 0a 2a 2a 20 20 ult code in.**
8ec0: 20 20 20 20 20 20 20 20 69 74 73 20 6c 65 61 73 its leas
8ed0: 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 38 20 t significant 8
8ee0: 62 69 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 bits..*/.#define
8ef0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 SQLITE_IOERR_RE
8f00: 41 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AD
8f10: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 (SQLITE_IOERR |
8f20: 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 (1<<8)).#define
8f30: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
8f40: 52 54 5f 52 45 41 44 20 20 20 20 20 20 20 20 28 RT_READ (
8f50: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
8f60: 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 2<<8)).#define S
8f70: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 QLITE_IOERR_WRIT
8f80: 45 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 E (S
8f90: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33 QLITE_IOERR | (3
8fa0: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
8fb0: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 LITE_IOERR_FSYNC
8fc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 (SQ
8fd0: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c LITE_IOERR | (4<
8fe0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8ff0: 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 ITE_IOERR_DIR_FS
9000: 59 4e 43 20 20 20 20 20 20 20 20 20 28 53 51 4c YNC (SQL
9010: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c ITE_IOERR | (5<<
9020: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
9030: 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 TE_IOERR_TRUNCAT
9040: 45 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 E (SQLI
9050: 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38 TE_IOERR | (6<<8
9060: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
9070: 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20 E_IOERR_FSTAT
9080: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
9090: 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29 E_IOERR | (7<<8)
90a0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
90b0: 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 _IOERR_UNLOCK
90c0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
90d0: 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 29 _IOERR | (8<<8))
90e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
90f0: 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 20 IOERR_RDLOCK
9100: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
9110: 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a IOERR | (9<<8)).
9120: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
9130: 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20 OERR_DELETE
9140: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
9150: 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 0a OERR | (10<<8)).
9160: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
9170: 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 20 OERR_BLOCKED
9180: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
9190: 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a OERR | (11<<8)).
91a0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
91b0: 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 OERR_NOMEM
91c0: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
91d0: 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 0a OERR | (12<<8)).
91e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
91f0: 4f 45 52 52 5f 41 43 43 45 53 53 20 20 20 20 20 OERR_ACCESS
9200: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
9210: 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 0a OERR | (13<<8)).
9220: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
9230: 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
9240: 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 49 EDLOCK (SQLITE_I
9250: 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 0a OERR | (14<<8)).
9260: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
9270: 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20 OERR_LOCK
9280: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
9290: 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 0a OERR | (15<<8)).
92a0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
92b0: 20 46 6c 61 67 73 20 46 6f 72 20 46 69 6c 65 20 Flags For File
92c0: 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73 20 Open Operations
92d0: 7b 48 31 30 32 33 30 7d 20 3c 48 31 31 31 32 30 {H10230} <H11120
92e0: 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a > <H12700>.**.**
92f0: 20 54 68 65 73 65 20 62 69 74 20 76 61 6c 75 65 These bit value
9300: 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 s are intended f
9310: 6f 72 20 75 73 65 20 69 6e 20 74 68 65 0a 2a 2a or use in the.**
9320: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 3rd parameter t
9330: 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f o the [sqlite3_o
9340: 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 pen_v2()] interf
9350: 61 63 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 ace and.** in th
9360: 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 4th parameter
9370: 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 to the xOpen met
9380: 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 hod of the.** [s
9390: 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
93a0: 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ct..*/.#define S
93b0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
93c0: 4e 4c 59 20 20 20 20 20 20 20 20 20 30 78 30 30 NLY 0x00
93d0: 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 000001.#define S
93e0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
93f0: 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30 30 RITE 0x00
9400: 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 000002.#define S
9410: 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
9420: 45 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 E 0x00
9430: 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 000004.#define S
9440: 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
9450: 45 4f 4e 43 4c 4f 53 45 20 20 20 20 30 78 30 30 EONCLOSE 0x00
9460: 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 000008.#define S
9470: 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
9480: 53 49 56 45 20 20 20 20 20 20 20 20 30 78 30 30 SIVE 0x00
9490: 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 000010.#define S
94a0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
94b0: 44 42 20 20 20 20 20 20 20 20 20 20 30 78 30 30 DB 0x00
94c0: 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 000100.#define S
94d0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f QLITE_OPEN_TEMP_
94e0: 44 42 20 20 20 20 20 20 20 20 20 20 30 78 30 30 DB 0x00
94f0: 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 000200.#define S
9500: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 QLITE_OPEN_TRANS
9510: 49 45 4e 54 5f 44 42 20 20 20 20 20 30 78 30 30 IENT_DB 0x00
9520: 30 30 30 34 30 30 0a 23 64 65 66 69 6e 65 20 53 000400.#define S
9530: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
9540: 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 JOURNAL 0x00
9550: 30 30 30 38 30 30 0a 23 64 65 66 69 6e 65 20 53 000800.#define S
9560: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f QLITE_OPEN_TEMP_
9570: 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 JOURNAL 0x00
9580: 30 30 31 30 30 30 0a 23 64 65 66 69 6e 65 20 53 001000.#define S
9590: 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f QLITE_OPEN_SUBJO
95a0: 55 52 4e 41 4c 20 20 20 20 20 20 20 30 78 30 30 URNAL 0x00
95b0: 30 30 32 30 30 30 0a 23 64 65 66 69 6e 65 20 53 002000.#define S
95c0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 QLITE_OPEN_MASTE
95d0: 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 78 30 30 R_JOURNAL 0x00
95e0: 30 30 34 30 30 30 0a 23 64 65 66 69 6e 65 20 53 004000.#define S
95f0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 QLITE_OPEN_NOMUT
9600: 45 58 20 20 20 20 20 20 20 20 20 20 30 78 30 30 EX 0x00
9610: 30 30 38 30 30 30 0a 23 64 65 66 69 6e 65 20 53 008000.#define S
9620: 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d QLITE_OPEN_FULLM
9630: 55 54 45 58 20 20 20 20 20 20 20 20 30 78 30 30 UTEX 0x00
9640: 30 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 010000../*.** CA
9650: 50 49 33 52 45 46 3a 20 44 65 76 69 63 65 20 43 PI3REF: Device C
9660: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 7b haracteristics {
9670: 48 31 30 32 34 30 7d 20 3c 48 31 31 31 32 30 3e H10240} <H11120>
9680: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76 69 .**.** The xDevi
9690: 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 20 6d ceCapabilities m
96a0: 65 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 ethod of the [sq
96b0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
96c0: 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 ].** object retu
96d0: 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 rns an integer w
96e0: 68 69 63 68 20 69 73 20 61 20 76 65 63 74 6f 72 hich is a vector
96f0: 20 6f 66 20 74 68 65 20 74 68 65 73 65 0a 2a 2a of the these.**
9700: 20 62 69 74 20 76 61 6c 75 65 73 20 65 78 70 72 bit values expr
9710: 65 73 73 69 6e 67 20 49 2f 4f 20 63 68 61 72 61 essing I/O chara
9720: 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 cteristics of th
9730: 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 0a 2a e mass storage.*
9740: 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 68 6f * device that ho
9750: 6c 64 73 20 74 68 65 20 66 69 6c 65 20 74 68 61 lds the file tha
9760: 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 t the [sqlite3_i
9770: 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 o_methods].** re
9780: 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 fers to..**.** T
9790: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f he SQLITE_IOCAP_
97a0: 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 ATOMIC property
97b0: 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 means that all w
97c0: 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 rites of.** any
97d0: 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e size are atomic.
97e0: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 The SQLITE_IOC
97f0: 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c AP_ATOMICnnn val
9800: 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 ues.** mean that
9810: 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b writes of block
9820: 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 s that are nnn b
9830: 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 ytes in size and
9840: 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20 .** are aligned
9850: 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 77 68 to an address wh
9860: 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ich is an intege
9870: 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a r multiple of.**
9880: 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e nnn are atomic.
9890: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 The SQLITE_IOC
98a0: 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76 AP_SAFE_APPEND v
98b0: 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 alue means.** th
98c0: 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 at when data is
98d0: 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69 appended to a fi
98e0: 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 73 20 le, the data is
98f0: 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 appended.** firs
9900: 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 t then the size
9910: 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 65 of the file is e
9920: 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 xtended, never t
9930: 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 he other.** way
9940: 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c around. The SQL
9950: 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e ITE_IOCAP_SEQUEN
9960: 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 TIAL property me
9970: 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f ans that.** info
9980: 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74 rmation is writt
9990: 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 en to disk in th
99a0: 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 e same order as
99b0: 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 calls.** to xWri
99c0: 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 te()..*/.#define
99d0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
99e0: 4f 4d 49 43 20 20 20 20 20 20 20 20 20 20 30 78 OMIC 0x
99f0: 30 30 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65 00000001.#define
9a00: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9a10: 4f 4d 49 43 35 31 32 20 20 20 20 20 20 20 30 78 OMIC512 0x
9a20: 30 30 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65 00000002.#define
9a30: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9a40: 4f 4d 49 43 31 4b 20 20 20 20 20 20 20 20 30 78 OMIC1K 0x
9a50: 30 30 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65 00000004.#define
9a60: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9a70: 4f 4d 49 43 32 4b 20 20 20 20 20 20 20 20 30 78 OMIC2K 0x
9a80: 30 30 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65 00000008.#define
9a90: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9aa0: 4f 4d 49 43 34 4b 20 20 20 20 20 20 20 20 30 78 OMIC4K 0x
9ab0: 30 30 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65 00000010.#define
9ac0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9ad0: 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20 30 78 OMIC8K 0x
9ae0: 30 30 30 30 30 30 32 30 0a 23 64 65 66 69 6e 65 00000020.#define
9af0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9b00: 4f 4d 49 43 31 36 4b 20 20 20 20 20 20 20 30 78 OMIC16K 0x
9b10: 30 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65 00000040.#define
9b20: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9b30: 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20 30 78 OMIC32K 0x
9b40: 30 30 30 30 30 30 38 30 0a 23 64 65 66 69 6e 65 00000080.#define
9b50: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9b60: 4f 4d 49 43 36 34 4b 20 20 20 20 20 20 20 30 78 OMIC64K 0x
9b70: 30 30 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 00000100.#define
9b80: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 SQLITE_IOCAP_SA
9b90: 46 45 5f 41 50 50 45 4e 44 20 20 20 20 20 30 78 FE_APPEND 0x
9ba0: 30 30 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65 00000200.#define
9bb0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 SQLITE_IOCAP_SE
9bc0: 51 55 45 4e 54 49 41 4c 20 20 20 20 20 20 30 78 QUENTIAL 0x
9bd0: 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 00000400../*.**
9be0: 43 41 50 49 33 52 45 46 3a 20 46 69 6c 65 20 4c CAPI3REF: File L
9bf0: 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b 48 ocking Levels {H
9c00: 31 30 32 35 30 7d 20 3c 48 31 31 31 32 30 3e 20 10250} <H11120>
9c10: 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 <H11310>.**.** S
9c20: 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f QLite uses one o
9c30: 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 f these integer
9c40: 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 values as the se
9c50: 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 cond.** argument
9c60: 20 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b to calls it mak
9c70: 65 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 es to the xLock(
9c80: 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 ) and xUnlock()
9c90: 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e methods.** of an
9ca0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 [sqlite3_io_met
9cb0: 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f hods] object..*/
9cc0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9cd0: 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 LOCK_NONE
9ce0: 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
9cf0: 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 ITE_LOCK_SHARED
9d00: 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
9d10: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 SQLITE_LOCK_RES
9d20: 45 52 56 45 44 20 20 20 20 20 20 32 0a 23 64 65 ERVED 2.#de
9d30: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b fine SQLITE_LOCK
9d40: 5f 50 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33 _PENDING 3
9d50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9d60: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 LOCK_EXCLUSIVE
9d70: 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 4../*.** CAPI
9d80: 33 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 3REF: Synchroniz
9d90: 61 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 ation Type Flags
9da0: 20 7b 48 31 30 32 36 30 7d 20 3c 48 31 31 31 32 {H10260} <H1112
9db0: 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 0>.**.** When SQ
9dc0: 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 Lite invokes the
9dd0: 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 xSync() method
9de0: 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 of an.** [sqlite
9df0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 3_io_methods] ob
9e00: 6a 65 63 74 20 69 74 20 75 73 65 73 20 61 20 63 ject it uses a c
9e10: 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a ombination of.**
9e20: 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 these integer v
9e30: 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63 alues as the sec
9e40: 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a ond argument..**
9e50: 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c .** When the SQL
9e60: 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c ITE_SYNC_DATAONL
9e70: 59 20 66 6c 61 67 20 69 73 20 75 73 65 64 2c 20 Y flag is used,
9e80: 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 it means that th
9e90: 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 e.** sync operat
9ea0: 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 ion only needs t
9eb0: 6f 20 66 6c 75 73 68 20 64 61 74 61 20 74 6f 20 o flush data to
9ec0: 6d 61 73 73 20 73 74 6f 72 61 67 65 2e 20 20 49 mass storage. I
9ed0: 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 node.** informat
9ee0: 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 ion need not be
9ef0: 66 6c 75 73 68 65 64 2e 20 54 68 65 20 53 51 4c flushed. The SQL
9f00: 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 ITE_SYNC_NORMAL
9f10: 66 6c 61 67 20 6d 65 61 6e 73 0a 2a 2a 20 74 6f flag means.** to
9f20: 20 75 73 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e use normal fsyn
9f30: 63 28 29 20 73 65 6d 61 6e 74 69 63 73 2e 20 54 c() semantics. T
9f40: 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 he SQLITE_SYNC_F
9f50: 55 4c 4c 20 66 6c 61 67 20 6d 65 61 6e 73 0a 2a ULL flag means.*
9f60: 2a 20 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 2d * to use Mac OS-
9f70: 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 X style fullsync
9f80: 20 69 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e instead of fsyn
9f90: 63 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 c()..*/.#define
9fa0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
9fb0: 41 4c 20 20 20 20 20 20 20 20 30 78 30 30 30 30 AL 0x0000
9fc0: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
9fd0: 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20 _SYNC_FULL
9fe0: 20 20 20 20 30 78 30 30 30 30 33 0a 23 64 65 66 0x00003.#def
9ff0: 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f ine SQLITE_SYNC_
a000: 44 41 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78 DATAONLY 0x
a010: 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 00010../*.** CAP
a020: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 I3REF: OS Interf
a030: 61 63 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61 ace Open File Ha
a040: 6e 64 6c 65 20 7b 48 31 31 31 31 30 7d 20 3c 53 ndle {H11110} <S
a050: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 20110>.**.** An
a060: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f [sqlite3_file] o
a070: 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 bject represents
a080: 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e an open file in
a090: 20 74 68 65 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 the OS.** inter
a0a0: 66 61 63 65 20 6c 61 79 65 72 2e 20 20 49 6e 64 face layer. Ind
a0b0: 69 76 69 64 75 61 6c 20 4f 53 20 69 6e 74 65 72 ividual OS inter
a0c0: 66 61 63 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 face implementat
a0d0: 69 6f 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e ions will.** wan
a0e0: 74 20 74 6f 20 73 75 62 63 6c 61 73 73 20 74 68 t to subclass th
a0f0: 69 73 20 6f 62 6a 65 63 74 20 62 79 20 61 70 70 is object by app
a100: 65 6e 64 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 ending additiona
a110: 6c 20 66 69 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 l fields.** for
a120: 74 68 65 69 72 20 6f 77 6e 20 75 73 65 2e 20 20 their own use.
a130: 54 68 65 20 70 4d 65 74 68 6f 64 73 20 65 6e 74 The pMethods ent
a140: 72 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 ry is a pointer
a150: 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 to an.** [sqlite
a160: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 3_io_methods] ob
a170: 6a 65 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 ject that define
a180: 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 70 65 s methods for pe
a190: 72 66 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 rforming.** I/O
a1a0: 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 operations on th
a1b0: 65 20 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a e open file..*/.
a1c0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
a1d0: 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 71 6c 69 qlite3_file sqli
a1e0: 74 65 33 5f 66 69 6c 65 3b 0a 73 74 72 75 63 74 te3_file;.struct
a1f0: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 7b 0a sqlite3_file {.
a200: 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 const struct s
a210: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
a220: 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 2f 2a s *pMethods; /*
a230: 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 61 6e 20 Methods for an
a240: 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a open file */.};.
a250: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
a260: 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 46 69 OS Interface Fi
a270: 6c 65 20 56 69 72 74 75 61 6c 20 4d 65 74 68 6f le Virtual Metho
a280: 64 73 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 32 ds Object {H1112
a290: 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 0} <S20110>.**.*
a2a0: 2a 20 45 76 65 72 79 20 66 69 6c 65 20 6f 70 65 * Every file ope
a2b0: 6e 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 ned by the [sqli
a2c0: 74 65 33 5f 76 66 73 5d 20 78 4f 70 65 6e 20 6d te3_vfs] xOpen m
a2d0: 65 74 68 6f 64 20 70 6f 70 75 6c 61 74 65 73 20 ethod populates
a2e0: 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 an.** [sqlite3_f
a2f0: 69 6c 65 5d 20 6f 62 6a 65 63 74 20 28 6f 72 2c ile] object (or,
a300: 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 more commonly,
a310: 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 a subclass of th
a320: 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 e.** [sqlite3_fi
a330: 6c 65 5d 20 6f 62 6a 65 63 74 29 20 77 69 74 68 le] object) with
a340: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
a350: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
a360: 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 69 s object..** Thi
a370: 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 s object defines
a380: 20 74 68 65 20 6d 65 74 68 6f 64 73 20 75 73 65 the methods use
a390: 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 d to perform var
a3a0: 69 6f 75 73 20 6f 70 65 72 61 74 69 6f 6e 73 0a ious operations.
a3b0: 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 6f ** against the o
a3c0: 70 65 6e 20 66 69 6c 65 20 72 65 70 72 65 73 65 pen file represe
a3d0: 6e 74 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c nted by the [sql
a3e0: 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 ite3_file] objec
a3f0: 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 t..**.** The fla
a400: 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 gs argument to x
a410: 53 79 6e 63 20 6d 61 79 20 62 65 20 6f 6e 65 20 Sync may be one
a420: 6f 66 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f of [SQLITE_SYNC_
a430: 4e 4f 52 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 NORMAL] or.** [S
a440: 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d QLITE_SYNC_FULL]
a450: 2e 20 20 54 68 65 20 66 69 72 73 74 20 63 68 6f . The first cho
a460: 69 63 65 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 ice is the norma
a470: 6c 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 l fsync()..** Th
a480: 65 20 73 65 63 6f 6e 64 20 63 68 6f 69 63 65 20 e second choice
a490: 69 73 20 61 20 4d 61 63 20 4f 53 2d 58 20 73 74 is a Mac OS-X st
a4a0: 79 6c 65 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54 yle fullsync. T
a4b0: 68 65 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f he [SQLITE_SYNC_
a4c0: 44 41 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 DATAONLY].** fla
a4d0: 67 20 6d 61 79 20 62 65 20 4f 52 65 64 20 69 6e g may be ORed in
a4e0: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 to indicate tha
a4f0: 74 20 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 t only the data
a500: 6f 66 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 61 of the file.** a
a510: 6e 64 20 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65 nd not its inode
a520: 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e needs to be syn
a530: 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 ced..**.** The i
a540: 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f nteger values to
a550: 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e xLock() and xUn
a560: 6c 6f 63 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f lock() are one o
a570: 66 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 f.** <ul>.** <li
a580: 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e > [SQLITE_LOCK_N
a590: 4f 4e 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 ONE],.** <li> [S
a5a0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 QLITE_LOCK_SHARE
a5b0: 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c D],.** <li> [SQL
a5c0: 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 ITE_LOCK_RESERVE
a5d0: 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c D],.** <li> [SQL
a5e0: 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 ITE_LOCK_PENDING
a5f0: 5d 2c 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 ], or.** <li> [S
a600: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 QLITE_LOCK_EXCLU
a610: 53 49 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a SIVE]..** </ul>.
a620: 2a 2a 20 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65 ** xLock() incre
a630: 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78 ases the lock. x
a640: 55 6e 6c 6f 63 6b 28 29 20 64 65 63 72 65 61 73 Unlock() decreas
a650: 65 73 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 es the lock..**
a660: 54 68 65 20 78 43 68 65 63 6b 52 65 73 65 72 76 The xCheckReserv
a670: 65 64 4c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 20 edLock() method
a680: 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 61 checks whether a
a690: 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ny database conn
a6a0: 65 63 74 69 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 ection,.** eithe
a6b0: 72 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 r in this proces
a6c0: 73 20 6f 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68 s or in some oth
a6d0: 65 72 20 70 72 6f 63 65 73 73 2c 20 69 73 20 68 er process, is h
a6e0: 6f 6c 64 69 6e 67 20 61 20 52 45 53 45 52 56 45 olding a RESERVE
a6f0: 44 2c 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f D,.** PENDING, o
a700: 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b r EXCLUSIVE lock
a710: 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 49 on the file. I
a720: 74 20 72 65 74 75 72 6e 73 20 74 72 75 65 0a 2a t returns true.*
a730: 2a 20 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b * if such a lock
a740: 20 65 78 69 73 74 73 20 61 6e 64 20 66 61 6c 73 exists and fals
a750: 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a e otherwise..**.
a760: 2a 2a 20 54 68 65 20 78 46 69 6c 65 43 6f 6e 74 ** The xFileCont
a770: 72 6f 6c 28 29 20 6d 65 74 68 6f 64 20 69 73 20 rol() method is
a780: 61 20 67 65 6e 65 72 69 63 20 69 6e 74 65 72 66 a generic interf
a790: 61 63 65 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 ace that allows
a7a0: 63 75 73 74 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d custom.** VFS im
a7b0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f plementations to
a7c0: 20 64 69 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f directly contro
a7d0: 6c 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75 l an open file u
a7e0: 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c sing the.** [sql
a7f0: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f ite3_file_contro
a800: 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 l()] interface.
a810: 20 54 68 65 20 73 65 63 6f 6e 64 20 22 6f 70 22 The second "op"
a820: 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 0a argument is an.
a830: 2a 2a 20 69 6e 74 65 67 65 72 20 6f 70 63 6f 64 ** integer opcod
a840: 65 2e 20 20 54 68 65 20 74 68 69 72 64 20 61 72 e. The third ar
a850: 67 75 6d 65 6e 74 20 69 73 20 61 20 67 65 6e 65 gument is a gene
a860: 72 69 63 20 70 6f 69 6e 74 65 72 20 69 6e 74 65 ric pointer inte
a870: 6e 64 65 64 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 nded to.** point
a880: 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 to a structure
a890: 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e that may contain
a8a0: 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 73 70 arguments or sp
a8b0: 61 63 65 20 69 6e 20 77 68 69 63 68 20 74 6f 0a ace in which to.
a8c0: 2a 2a 20 77 72 69 74 65 20 72 65 74 75 72 6e 20 ** write return
a8d0: 76 61 6c 75 65 73 2e 20 20 50 6f 74 65 6e 74 69 values. Potenti
a8e0: 61 6c 20 75 73 65 73 20 66 6f 72 20 78 46 69 6c al uses for xFil
a8f0: 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74 eControl() might
a900: 20 62 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 be.** functions
a910: 20 74 6f 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b to enable block
a920: 69 6e 67 20 6c 6f 63 6b 73 20 77 69 74 68 20 74 ing locks with t
a930: 69 6d 65 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e imeouts, to chan
a940: 67 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e ge the.** lockin
a950: 67 20 73 74 72 61 74 65 67 79 20 28 66 6f 72 20 g strategy (for
a960: 65 78 61 6d 70 6c 65 20 74 6f 20 75 73 65 20 64 example to use d
a970: 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 ot-file locks),
a980: 74 6f 20 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62 to inquire.** ab
a990: 6f 75 74 20 74 68 65 20 73 74 61 74 75 73 20 6f out the status o
a9a0: 66 20 61 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 f a lock, or to
a9b0: 62 72 65 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b break stale lock
a9c0: 73 2e 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a s. The SQLite.*
a9d0: 2a 20 63 6f 72 65 20 72 65 73 65 72 76 65 73 20 * core reserves
a9e0: 61 6c 6c 20 6f 70 63 6f 64 65 73 20 6c 65 73 73 all opcodes less
a9f0: 20 74 68 61 6e 20 31 30 30 20 66 6f 72 20 69 74 than 100 for it
aa00: 73 20 6f 77 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 s own use..** A
aa10: 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f [SQLITE_FCNTL_LO
aa20: 43 4b 53 54 41 54 45 20 7c 20 6c 69 73 74 20 6f CKSTATE | list o
aa30: 66 20 6f 70 63 6f 64 65 73 5d 20 6c 65 73 73 20 f opcodes] less
aa40: 74 68 61 6e 20 31 30 30 20 69 73 20 61 76 61 69 than 100 is avai
aa50: 6c 61 62 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 lable..** Applic
aa60: 61 74 69 6f 6e 73 20 74 68 61 74 20 64 65 66 69 ations that defi
aa70: 6e 65 20 61 20 63 75 73 74 6f 6d 20 78 46 69 6c ne a custom xFil
aa80: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 eControl method
aa90: 73 68 6f 75 6c 64 20 75 73 65 20 6f 70 63 6f 64 should use opcod
aaa0: 65 73 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 es.** greater th
aab0: 61 6e 20 31 30 30 20 74 6f 20 61 76 6f 69 64 20 an 100 to avoid
aac0: 63 6f 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a conflicts..**.**
aad0: 20 54 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 The xSectorSize
aae0: 28 29 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e () method return
aaf0: 73 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a s the sector siz
ab00: 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 69 e of the.** devi
ab10: 63 65 20 74 68 61 74 20 75 6e 64 65 72 6c 69 65 ce that underlie
ab20: 73 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 s the file. The
ab30: 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 sector size is
ab40: 74 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 the.** minimum w
ab50: 72 69 74 65 20 74 68 61 74 20 63 61 6e 20 62 65 rite that can be
ab60: 20 70 65 72 66 6f 72 6d 65 64 20 77 69 74 68 6f performed witho
ab70: 75 74 20 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a ut disturbing.**
ab80: 20 6f 74 68 65 72 20 62 79 74 65 73 20 69 6e 20 other bytes in
ab90: 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 78 the file. The x
aba0: 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
abb0: 73 74 69 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f stics().** metho
abc0: 64 20 72 65 74 75 72 6e 73 20 61 20 62 69 74 20 d returns a bit
abd0: 76 65 63 74 6f 72 20 64 65 73 63 72 69 62 69 6e vector describin
abe0: 67 20 62 65 68 61 76 69 6f 72 73 20 6f 66 20 74 g behaviors of t
abf0: 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 he.** underlying
ac00: 20 64 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c device:.**.** <
ac10: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c ul>.** <li> [SQL
ac20: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
ac30: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ].** <li> [SQLIT
ac40: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 E_IOCAP_ATOMIC51
ac50: 32 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 2].** <li> [SQLI
ac60: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 TE_IOCAP_ATOMIC1
ac70: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 K].** <li> [SQLI
ac80: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 TE_IOCAP_ATOMIC2
ac90: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 K].** <li> [SQLI
aca0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 TE_IOCAP_ATOMIC4
acb0: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 K].** <li> [SQLI
acc0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 TE_IOCAP_ATOMIC8
acd0: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 K].** <li> [SQLI
ace0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 TE_IOCAP_ATOMIC1
acf0: 36 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 6K].** <li> [SQL
ad00: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
ad10: 33 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 32K].** <li> [SQ
ad20: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
ad30: 43 36 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 C64K].** <li> [S
ad40: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
ad50: 5f 41 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e _APPEND].** <li>
ad60: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 [SQLITE_IOCAP_S
ad70: 45 51 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f EQUENTIAL].** </
ad80: 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 ul>.**.** The SQ
ad90: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
ada0: 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 C property means
adb0: 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 that all writes
adc0: 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 of.** any size
add0: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 are atomic. The
ade0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
adf0: 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a OMICnnn values.*
ae00: 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 * mean that writ
ae10: 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 es of blocks tha
ae20: 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 t are nnn bytes
ae30: 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 in size and.** a
ae40: 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e re aligned to an
ae50: 20 61 64 64 72 65 73 73 20 77 68 69 63 68 20 69 address which i
ae60: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c s an integer mul
ae70: 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 tiple of.** nnn
ae80: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 are atomic. The
ae90: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 SQLITE_IOCAP_SA
aea0: 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 FE_APPEND value
aeb0: 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 means.** that wh
aec0: 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 65 6e en data is appen
aed0: 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 ded to a file, t
aee0: 68 65 20 64 61 74 61 20 69 73 20 61 70 70 65 6e he data is appen
aef0: 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 ded.** first the
af00: 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 n the size of th
af10: 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 e file is extend
af20: 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 ed, never the ot
af30: 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e her.** way aroun
af40: 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 d. The SQLITE_I
af50: 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 OCAP_SEQUENTIAL
af60: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 property means t
af70: 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 hat.** informati
af80: 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f on is written to
af90: 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d disk in the sam
afa0: 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 e order as calls
afb0: 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e .** to xWrite().
afc0: 0a 2a 2a 0a 2a 2a 20 49 66 20 78 52 65 61 64 28 .**.** If xRead(
afd0: 29 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 ) returns SQLITE
afe0: 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
aff0: 44 20 69 74 20 6d 75 73 74 20 61 6c 73 6f 20 66 D it must also f
b000: 69 6c 6c 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e ill.** in the un
b010: 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 read portions of
b020: 20 74 68 65 20 62 75 66 66 65 72 20 77 69 74 68 the buffer with
b030: 20 7a 65 72 6f 73 2e 20 20 41 20 56 46 53 20 74 zeros. A VFS t
b040: 68 61 74 0a 2a 2a 20 66 61 69 6c 73 20 74 6f 20 hat.** fails to
b050: 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 zero-fill short
b060: 72 65 61 64 73 20 6d 69 67 68 74 20 73 65 65 6d reads might seem
b070: 20 74 6f 20 77 6f 72 6b 2e 20 20 48 6f 77 65 76 to work. Howev
b080: 65 72 2c 0a 2a 2a 20 66 61 69 6c 75 72 65 20 74 er,.** failure t
b090: 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 o zero-fill shor
b0a0: 74 20 72 65 61 64 73 20 77 69 6c 6c 20 65 76 65 t reads will eve
b0b0: 6e 74 75 61 6c 6c 79 20 6c 65 61 64 20 74 6f 0a ntually lead to.
b0c0: 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 ** database corr
b0d0: 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 uption..*/.typed
b0e0: 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
b0f0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 3_io_methods sql
b100: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b ite3_io_methods;
b110: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
b120: 69 6f 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 io_methods {. i
b130: 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 nt iVersion;. i
b140: 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c nt (*xClose)(sql
b150: 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 ite3_file*);. i
b160: 6e 74 20 28 2a 78 52 65 61 64 29 28 73 71 6c 69 nt (*xRead)(sqli
b170: 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a te3_file*, void*
b180: 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 , int iAmt, sqli
b190: 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 te3_int64 iOfst)
b1a0: 3b 0a 20 20 69 6e 74 20 28 2a 78 57 72 69 74 65 ;. int (*xWrite
b1b0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c )(sqlite3_file*,
b1c0: 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
b1d0: 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f t iAmt, sqlite3_
b1e0: 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 int64 iOfst);.
b1f0: 69 6e 74 20 28 2a 78 54 72 75 6e 63 61 74 65 29 int (*xTruncate)
b200: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
b210: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 69 sqlite3_int64 si
b220: 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 ze);. int (*xSy
b230: 6e 63 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 nc)(sqlite3_file
b240: 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20 *, int flags);.
b250: 20 69 6e 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 int (*xFileSize
b260: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c )(sqlite3_file*,
b270: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
b280: 70 53 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a pSize);. int (*
b290: 78 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 xLock)(sqlite3_f
b2a0: 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e ile*, int);. in
b2b0: 74 20 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c t (*xUnlock)(sql
b2c0: 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 ite3_file*, int)
b2d0: 3b 0a 20 20 69 6e 74 20 28 2a 78 43 68 65 63 6b ;. int (*xCheck
b2e0: 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 28 73 71 ReservedLock)(sq
b2f0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
b300: 20 2a 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e *pResOut);. in
b310: 74 20 28 2a 78 46 69 6c 65 43 6f 6e 74 72 6f 6c t (*xFileControl
b320: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c )(sqlite3_file*,
b330: 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 int op, void *p
b340: 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 Arg);. int (*xS
b350: 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 ectorSize)(sqlit
b360: 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 e3_file*);. int
b370: 20 28 2a 78 44 65 76 69 63 65 43 68 61 72 61 63 (*xDeviceCharac
b380: 74 65 72 69 73 74 69 63 73 29 28 73 71 6c 69 74 teristics)(sqlit
b390: 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 e3_file*);. /*
b3a0: 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 74 68 6f Additional metho
b3b0: 64 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 ds may be added
b3c0: 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
b3d0: 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 es */.};../*.**
b3e0: 43 41 50 49 33 52 45 46 3a 20 53 74 61 6e 64 61 CAPI3REF: Standa
b3f0: 72 64 20 46 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 rd File Control
b400: 4f 70 63 6f 64 65 73 20 7b 48 31 31 33 31 30 7d Opcodes {H11310}
b410: 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S30800>.**.**
b420: 54 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f These integer co
b430: 6e 73 74 61 6e 74 73 20 61 72 65 20 6f 70 63 6f nstants are opco
b440: 64 65 73 20 66 6f 72 20 74 68 65 20 78 46 69 6c des for the xFil
b450: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0a eControl method.
b460: 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ** of the [sqlit
b470: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f e3_io_methods] o
b480: 62 6a 65 63 74 20 61 6e 64 20 66 6f 72 20 74 68 bject and for th
b490: 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f e [sqlite3_file_
b4a0: 63 6f 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e control()].** in
b4b0: 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 terface..**.** T
b4c0: 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c he [SQLITE_FCNTL
b4d0: 5f 4c 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63 6f _LOCKSTATE] opco
b4e0: 64 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 de is used for d
b4f0: 65 62 75 67 67 69 6e 67 2e 20 20 54 68 69 73 0a ebugging. This.
b500: 2a 2a 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 ** opcode causes
b510: 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f the xFileContro
b520: 6c 20 6d 65 74 68 6f 64 20 74 6f 20 77 72 69 74 l method to writ
b530: 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 e the current st
b540: 61 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f ate of.** the lo
b550: 63 6b 20 28 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 ck (one of [SQLI
b560: 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b TE_LOCK_NONE], [
b570: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 SQLITE_LOCK_SHAR
b580: 45 44 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f ED],.** [SQLITE_
b590: 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 20 LOCK_RESERVED],
b5a0: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e [SQLITE_LOCK_PEN
b5b0: 44 49 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 DING], or [SQLIT
b5c0: 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 E_LOCK_EXCLUSIVE
b5d0: 5d 29 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 69 6e ]).** into an in
b5e0: 74 65 67 65 72 20 74 68 61 74 20 74 68 65 20 70 teger that the p
b5f0: 41 72 67 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 Arg argument poi
b600: 6e 74 73 20 74 6f 2e 20 54 68 69 73 20 63 61 70 nts to. This cap
b610: 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 75 73 ability.** is us
b620: 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e ed during testin
b630: 67 20 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 64 73 g and only needs
b640: 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 to be supported
b650: 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 54 45 53 when SQLITE_TES
b660: 54 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e T.** is defined.
b670: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
b680: 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 TE_FCNTL_LOCKSTA
b690: 54 45 20 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a TE 1../*.
b6a0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 ** CAPI3REF: Mut
b6b0: 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 31 ex Handle {H1711
b6c0: 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a 0} <S20130>.**.*
b6d0: 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75 * The mutex modu
b6e0: 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 le within SQLite
b6f0: 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65 defines [sqlite
b700: 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61 3_mutex] to be a
b710: 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 79 n.** abstract ty
b720: 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 6f pe for a mutex o
b730: 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c 69 bject. The SQLi
b740: 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c 6f te core never lo
b750: 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 6e oks.** at the in
b760: 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 ternal represent
b770: 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 6c ation of an [sql
b780: 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 74 ite3_mutex]. It
b790: 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 77 only.** deals w
b7a0: 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ith pointers to
b7b0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 the [sqlite3_mut
b7c0: 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a ex] object..**.*
b7d0: 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 72 * Mutexes are cr
b7e0: 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c eated using [sql
b7f0: 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
b800: 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ()]..*/.typedef
b810: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d struct sqlite3_m
b820: 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 74 utex sqlite3_mut
b830: 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 ex;../*.** CAPI3
b840: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 REF: OS Interfac
b850: 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 30 e Object {H11140
b860: 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S20100>.**.**
b870: 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
b880: 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 the sqlite3_vfs
b890: 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 object defines t
b8a0: 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 he interface bet
b8b0: 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 69 ween.** the SQLi
b8c0: 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20 te core and the
b8d0: 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 underlying opera
b8e0: 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68 ting system. Th
b8f0: 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68 e "vfs".** in th
b900: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62 e name of the ob
b910: 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20 ject stands for
b920: 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 "virtual file sy
b930: 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 stem"..**.** The
b940: 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56 value of the iV
b950: 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 ersion field is
b960: 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20 initially 1 but
b970: 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e may be larger in
b980: 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69 .** future versi
b990: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ons of SQLite.
b9a0: 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 Additional field
b9b0: 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 s may be appende
b9c0: 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 6a d to this.** obj
b9d0: 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65 ect when the iVe
b9e0: 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69 rsion value is i
b9f0: 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20 ncreased. Note
ba00: 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 that the structu
ba10: 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c re.** of the sql
ba20: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 ite3_vfs object
ba30: 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 74 changes in the t
ba40: 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 65 ransaction betwe
ba50: 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 en.** SQLite ver
ba60: 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 33 sion 3.5.9 and 3
ba70: 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 65 .6.0 and yet the
ba80: 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 iVersion field
ba90: 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66 was not.** modif
baa0: 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ied..**.** The s
bab0: 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 73 zOsFile field is
bac0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
bad0: 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 6c subclassed [sql
bae0: 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74 ite3_file].** st
baf0: 72 75 63 74 75 72 65 20 75 73 65 64 20 62 79 20 ructure used by
bb00: 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 74 this VFS. mxPat
bb10: 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 78 hname is the max
bb20: 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a 2a imum length of.*
bb30: 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 * a pathname in
bb40: 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 this VFS..**.**
bb50: 52 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 74 Registered sqlit
bb60: 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 61 e3_vfs objects a
bb70: 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e re kept on a lin
bb80: 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20 ked list formed
bb90: 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20 by.** the pNext
bba0: 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73 pointer. The [s
bbb0: 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
bbc0: 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 ter()].** and [s
bbd0: 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 qlite3_vfs_unreg
bbe0: 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61 ister()] interfa
bbf0: 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20 ces manage this
bc00: 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72 list.** in a thr
bc10: 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54 ead-safe way. T
bc20: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f he [sqlite3_vfs_
bc30: 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63 find()] interfac
bc40: 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68 e.** searches th
bc50: 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 72 e list. Neither
bc60: 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
bc70: 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 46 code nor the VF
bc80: 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 S.** implementat
bc90: 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74 ion should use t
bca0: 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 he pNext pointer
bcb0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 78 ..**.** The pNex
bcc0: 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f t field is the o
bcd0: 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65 nly field in the
bce0: 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 sqlite3_vfs.**
bcf0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 53 structure that S
bd00: 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 20 QLite will ever
bd10: 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 20 modify. SQLite
bd20: 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 73 will only access
bd30: 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 .** or modify th
bd40: 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 68 is field while h
bd50: 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 olding a particu
bd60: 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 78 lar static mutex
bd70: 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 ..** The applica
bd80: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 tion should neve
bd90: 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 6e r modify anythin
bda0: 67 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 6c g within the sql
bdb0: 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65 ite3_vfs.** obje
bdc0: 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a 65 ct once the obje
bdd0: 63 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 ct has been regi
bde0: 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 stered..**.** Th
bdf0: 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 6f e zName field ho
be00: 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 lds the name of
be10: 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e 20 the VFS module.
be20: 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a 2a The name must.*
be30: 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 6f * be unique acro
be40: 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 6c ss all VFS modul
be50: 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 es..**.** {H1114
be60: 31 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 67 1} SQLite will g
be70: 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 uarantee that th
be80: 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 e zFilename para
be90: 6d 65 74 65 72 20 74 6f 20 78 4f 70 65 6e 0a 2a meter to xOpen.*
bea0: 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 4e 55 * is either a NU
beb0: 4c 4c 20 70 6f 69 6e 74 65 72 20 6f 72 20 73 74 LL pointer or st
bec0: 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 0a 2a 2a ring obtained.**
bed0: 20 66 72 6f 6d 20 78 46 75 6c 6c 50 61 74 68 6e from xFullPathn
bee0: 61 6d 65 28 29 2e 20 20 53 51 4c 69 74 65 20 66 ame(). SQLite f
bef0: 75 72 74 68 65 72 20 67 75 61 72 61 6e 74 65 65 urther guarantee
bf00: 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 73 74 s that.** the st
bf10: 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 76 61 6c ring will be val
bf20: 69 64 20 61 6e 64 20 75 6e 63 68 61 6e 67 65 64 id and unchanged
bf30: 20 75 6e 74 69 6c 20 78 43 6c 6f 73 65 28 29 20 until xClose()
bf40: 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20 7b 45 is.** called. {E
bf50: 4e 44 7d 20 20 42 65 63 61 75 73 65 20 6f 66 20 ND} Because of
bf60: 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e the previous sen
bf70: 74 65 6e 73 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 tense,.** the [s
bf80: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e qlite3_file] can
bf90: 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20 safely store a
bfa0: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a pointer to the.*
bfb0: 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74 * filename if it
bfc0: 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62 needs to rememb
bfd0: 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 er the filename
bfe0: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e for some reason.
bff0: 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65 .** If the zFile
c000: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 name parameter i
c010: 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c s xOpen is a NUL
c020: 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78 L pointer then x
c030: 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 Open.** must inv
c040: 69 74 65 20 69 74 73 20 6f 77 6e 20 74 65 6d 70 ite its own temp
c050: 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74 orary name for t
c060: 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 76 he file. Whenev
c070: 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c 65 er the .** xFile
c080: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 name parameter i
c090: 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 61 s NULL it will a
c0a0: 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 lso be the case
c0b0: 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 67 that the.** flag
c0c0: 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c s parameter will
c0d0: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 include [SQLITE
c0e0: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
c0f0: 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 OSE]..**.** {H11
c100: 31 34 32 7d 20 54 68 65 20 66 6c 61 67 73 20 61 142} The flags a
c110: 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e rgument to xOpen
c120: 28 29 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20 () includes all
c130: 62 69 74 73 20 73 65 74 20 69 6e 0a 2a 2a 20 74 bits set in.** t
c140: 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e he flags argumen
c150: 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 t to [sqlite3_op
c160: 65 6e 5f 76 32 28 29 5d 2e 20 20 4f 72 20 69 66 en_v2()]. Or if
c170: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
c180: 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 ].** or [sqlite3
c190: 5f 6f 70 65 6e 31 36 28 29 5d 20 69 73 20 75 73 _open16()] is us
c1a0: 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73 20 69 ed, then flags i
c1b0: 6e 63 6c 75 64 65 73 20 61 74 20 6c 65 61 73 74 ncludes at least
c1c0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e .** [SQLITE_OPEN
c1d0: 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 _READWRITE] | [S
c1e0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
c1f0: 45 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 49 66 20 E]. {END}.** If
c200: 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20 61 20 xOpen() opens a
c210: 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74 file read-only t
c220: 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70 4f 75 hen it sets *pOu
c230: 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69 6e 63 tFlags to.** inc
c240: 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 lude [SQLITE_OPE
c250: 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74 N_READONLY]. Ot
c260: 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70 4f 75 her bits in *pOu
c270: 74 46 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 tFlags may be se
c280: 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 33 t..**.** {H11143
c290: 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c } SQLite will al
c2a0: 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 so add one of th
c2b0: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 e following flag
c2c0: 73 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 28 29 s to the xOpen()
c2d0: 0a 2a 2a 20 63 61 6c 6c 2c 20 64 65 70 65 6e 64 .** call, depend
c2e0: 69 6e 67 20 6f 6e 20 74 68 65 20 6f 62 6a 65 63 ing on the objec
c2f0: 74 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 3a 0a t being opened:.
c300: 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
c310: 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e i> [SQLITE_OPEN
c320: 5f 4d 41 49 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 _MAIN_DB].** <li
c330: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f > [SQLITE_OPEN_
c340: 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a MAIN_JOURNAL].**
c350: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f <li> [SQLITE_O
c360: 50 45 4e 5f 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 PEN_TEMP_DB].**
c370: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
c380: 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d EN_TEMP_JOURNAL]
c390: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 .** <li> [SQLIT
c3a0: 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 E_OPEN_TRANSIENT
c3b0: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 _DB].** <li> [S
c3c0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f QLITE_OPEN_SUBJO
c3d0: 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 URNAL].** <li>
c3e0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 [SQLITE_OPEN_MAS
c3f0: 54 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 TER_JOURNAL].**
c400: 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a </ul> {END}.**.*
c410: 2a 20 54 68 65 20 66 69 6c 65 20 49 2f 4f 20 69 * The file I/O i
c420: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 mplementation ca
c430: 6e 20 75 73 65 20 74 68 65 20 6f 62 6a 65 63 74 n use the object
c440: 20 74 79 70 65 20 66 6c 61 67 73 20 74 6f 0a 2a type flags to.*
c450: 2a 20 63 68 61 6e 67 65 20 74 68 65 20 77 61 79 * change the way
c460: 20 69 74 20 64 65 61 6c 73 20 77 69 74 68 20 66 it deals with f
c470: 69 6c 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 iles. For examp
c480: 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 le, an applicati
c490: 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 on.** that does
c4a0: 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 63 not care about c
c4b0: 72 61 73 68 20 72 65 63 6f 76 65 72 79 20 6f 72 rash recovery or
c4c0: 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 68 74 20 rollback might
c4d0: 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f 70 65 6e make.** the open
c4e0: 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 of a journal fi
c4f0: 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 57 72 69 le a no-op. Wri
c500: 74 65 73 20 74 6f 20 74 68 69 73 20 6a 6f 75 72 tes to this jour
c510: 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 nal would.** als
c520: 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 61 6e 64 o be no-ops, and
c530: 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 any attempt to
c540: 72 65 61 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c read the journal
c550: 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 0a 2a 2a would return.**
c560: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2e 20 20 SQLITE_IOERR.
c570: 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 Or the implement
c580: 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 65 63 6f ation might reco
c590: 67 6e 69 7a 65 20 74 68 61 74 20 61 20 64 61 74 gnize that a dat
c5a0: 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77 69 abase.** file wi
c5b0: 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70 61 67 65 ll be doing page
c5c0: 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 6f 72 20 -aligned sector
c5d0: 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 reads and writes
c5e0: 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a 2a 2a 20 in a random.**
c5f0: 6f 72 64 65 72 20 61 6e 64 20 73 65 74 20 75 70 order and set up
c600: 20 69 74 73 20 49 2f 4f 20 73 75 62 73 79 73 74 its I/O subsyst
c610: 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a em accordingly..
c620: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 69 67 **.** SQLite mig
c630: 68 74 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 ht also add one
c640: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
c650: 20 66 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f flags to the xO
c660: 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a pen method:.**.*
c670: 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b * <ul>.** <li> [
c680: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
c690: 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c TEONCLOSE].** <l
c6a0: 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f i> [SQLITE_OPEN_
c6b0: 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f EXCLUSIVE].** </
c6c0: 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 ul>.**.** {H1114
c6d0: 35 7d 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 5} The [SQLITE_O
c6e0: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
c6f0: 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 E] flag means th
c700: 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 e file should be
c710: 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e .** deleted when
c720: 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 it is closed.
c730: 7b 48 31 31 31 34 36 7d 20 54 68 65 20 5b 53 51 {H11146} The [SQ
c740: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
c750: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c ONCLOSE].** will
c760: 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50 be set for TEMP
c770: 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f 75 databases, jou
c780: 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73 75 rnals and for su
c790: 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a bjournals..**.**
c7a0: 20 7b 48 31 31 31 34 37 7d 20 54 68 65 20 5b 53 {H11147} The [S
c7b0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
c7c0: 53 49 56 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 SIVE] flag means
c7d0: 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 the file should
c7e0: 20 62 65 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f be opened.** fo
c7f0: 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 r exclusive acce
c800: 73 73 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69 ss. This flag i
c810: 73 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 66 69 s set for all fi
c820: 6c 65 73 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f les except.** fo
c830: 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 r the main datab
c840: 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
c850: 7b 48 31 31 31 34 38 7d 20 41 74 20 6c 65 61 73 {H11148} At leas
c860: 74 20 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 t szOsFile bytes
c870: 20 6f 66 20 6d 65 6d 6f 72 79 20 61 72 65 20 61 of memory are a
c880: 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69 llocated by SQLi
c890: 74 65 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 74 68 te.** to hold th
c8a0: 65 20 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 e [sqlite3_file
c8b0: 5d 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 ] structure pass
c8c0: 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64 0a ed as the third.
c8d0: 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 ** argument to x
c8e0: 4f 70 65 6e 2e 20 7b 45 4e 44 7d 20 20 54 68 65 Open. {END} The
c8f0: 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f xOpen method do
c900: 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a es not have to.*
c910: 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 * allocate the s
c920: 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f tructure; it sho
c930: 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 uld just fill it
c940: 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 in..**.** {H111
c950: 34 39 7d 20 54 68 65 20 66 6c 61 67 73 20 61 72 49} The flags ar
c960: 67 75 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73 gument to xAcces
c970: 73 28 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 s() may be [SQLI
c980: 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
c990: 5d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 ].** to test for
c9a0: 20 74 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f the existence o
c9b0: 66 20 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 f a file, or [SQ
c9c0: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
c9d0: 57 52 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 WRITE] to.** tes
c9e0: 74 20 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 t whether a file
c9f0: 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 is readable and
ca00: 20 77 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 writable, or [S
ca10: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
ca20: 44 5d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 D].** to test wh
ca30: 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 ether a file is
ca40: 61 74 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c at least readabl
ca50: 65 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 66 69 e. {END} The fi
ca60: 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a 2a 20 64 le can be a.** d
ca70: 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 irectory..**.**
ca80: 7b 48 31 31 31 35 30 7d 20 53 51 4c 69 74 65 20 {H11150} SQLite
ca90: 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f will always allo
caa0: 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78 cate at least mx
cab0: 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 Pathname+1 bytes
cac0: 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 for the.** outp
cad0: 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50 ut buffer xFullP
cae0: 61 74 68 6e 61 6d 65 2e 20 7b 48 31 31 31 35 31 athname. {H11151
caf0: 7d 20 54 68 65 20 65 78 61 63 74 20 73 69 7a 65 } The exact size
cb00: 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 of the output b
cb10: 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f uffer.** is also
cb20: 20 70 61 73 73 65 64 20 61 73 20 61 20 70 61 72 passed as a par
cb30: 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20 20 ameter to both
cb40: 6d 65 74 68 6f 64 73 2e 20 7b 45 4e 44 7d 20 20 methods. {END}
cb50: 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 If the output bu
cb60: 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c ffer.** is not l
cb70: 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51 arge enough, [SQ
cb80: 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 LITE_CANTOPEN] s
cb90: 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 hould be returne
cba0: 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69 73 d. Since this is
cbb0: 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 61 .** handled as a
cbc0: 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79 20 fatal error by
cbd0: 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 6c SQLite, vfs impl
cbe0: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 ementations shou
cbf0: 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 ld endeavor.** t
cc00: 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 62 o prevent this b
cc10: 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74 68 y setting mxPath
cc20: 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69 63 name to a suffic
cc30: 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 6c iently large val
cc40: 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 ue..**.** The xR
cc50: 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c andomness(), xSl
cc60: 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72 72 eep(), and xCurr
cc70: 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72 66 entTime() interf
cc80: 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 aces.** are not
cc90: 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 20 strictly a part
cca0: 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 of the filesyste
ccb0: 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 0a m, but they are.
ccc0: 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 ** included in t
ccd0: 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65 he VFS structure
cce0: 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73 for completenes
ccf0: 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f s..** The xRando
cd00: 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f 6e mness() function
cd10: 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 74 attempts to ret
cd20: 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 73 urn nBytes bytes
cd30: 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c .** of good-qual
cd40: 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 ity randomness i
cd50: 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 72 nto zOut. The r
cd60: 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a eturn value is.*
cd70: 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 6d * the actual num
cd80: 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
cd90: 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 randomness obtai
cda0: 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65 ned..** The xSle
cdb0: 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 73 ep() method caus
cdc0: 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 es the calling t
cdd0: 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 66 hread to sleep f
cde0: 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74 or at.** least t
cdf0: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 he number of mic
ce00: 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e roseconds given.
ce10: 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54 69 The xCurrentTi
ce20: 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 me().** method r
ce30: 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20 eturns a Julian
ce40: 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 74 Day Number for t
ce50: 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 20 he current date
ce60: 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 and time..*/.typ
ce70: 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
ce80: 74 65 33 5f 76 66 73 20 73 71 6c 69 74 65 33 5f te3_vfs sqlite3_
ce90: 76 66 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 vfs;.struct sqli
cea0: 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e 74 20 te3_vfs {. int
ceb0: 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 20 20 iVersion;
cec0: 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72 /* Structur
ced0: 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 e version number
cee0: 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 46 69 */. int szOsFi
cef0: 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f le; /
cf00: 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63 6c 61 * Size of subcla
cf10: 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66 69 6c ssed sqlite3_fil
cf20: 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 74 e */. int mxPat
cf30: 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 hname;
cf40: 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c 65 20 /* Maximum file
cf50: 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74 68 20 pathname length
cf60: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 */. sqlite3_vfs
cf70: 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a *pNext; /*
cf80: 20 4e 65 78 74 20 72 65 67 69 73 74 65 72 65 64 Next registered
cf90: 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 VFS */. const
cfa0: 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
cfb0: 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
cfc0: 69 73 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 is virtual file
cfd0: 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 system */. void
cfe0: 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 *pAppData;
cff0: 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
d000: 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 to application-s
d010: 70 65 63 69 66 69 63 20 64 61 74 61 20 2a 2f 0a pecific data */.
d020: 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 int (*xOpen)(s
d030: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e qlite3_vfs*, con
d040: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
d050: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 sqlite3_file*,.
d060: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
d070: 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 4f t flags, int *pO
d080: 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 utFlags);. int
d090: 28 2a 78 44 65 6c 65 74 65 29 28 73 71 6c 69 74 (*xDelete)(sqlit
d0a0: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
d0b0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 har *zName, int
d0c0: 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 6e 74 20 syncDir);. int
d0d0: 28 2a 78 41 63 63 65 73 73 29 28 73 71 6c 69 74 (*xAccess)(sqlit
d0e0: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
d0f0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 har *zName, int
d100: 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 52 65 73 flags, int *pRes
d110: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 Out);. int (*xF
d120: 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73 71 6c ullPathname)(sql
d130: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 ite3_vfs*, const
d140: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e char *zName, in
d150: 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 2a 7a 4f t nOut, char *zO
d160: 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 ut);. void *(*x
d170: 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 65 33 5f DlOpen)(sqlite3_
d180: 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 vfs*, const char
d190: 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 *zFilename);.
d1a0: 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 6f 72 29 void (*xDlError)
d1b0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 (sqlite3_vfs*, i
d1c0: 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a nt nByte, char *
d1d0: 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 6f 69 64 zErrMsg);. void
d1e0: 20 2a 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 *(*xDlSym)(sqli
d1f0: 74 65 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 te3_vfs*,void*,
d200: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d const char *zSym
d210: 62 6f 6c 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 bol);. void (*x
d220: 44 6c 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 DlClose)(sqlite3
d230: 5f 76 66 73 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 _vfs*, void*);.
d240: 20 69 6e 74 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 int (*xRandomne
d250: 73 73 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a ss)(sqlite3_vfs*
d260: 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 , int nByte, cha
d270: 72 20 2a 7a 4f 75 74 29 3b 0a 20 20 69 6e 74 20 r *zOut);. int
d280: 28 2a 78 53 6c 65 65 70 29 28 73 71 6c 69 74 65 (*xSleep)(sqlite
d290: 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6d 69 63 72 3_vfs*, int micr
d2a0: 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 oseconds);. int
d2b0: 20 28 2a 78 43 75 72 72 65 6e 74 54 69 6d 65 29 (*xCurrentTime)
d2c0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 64 (sqlite3_vfs*, d
d2d0: 6f 75 62 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 ouble*);. int (
d2e0: 2a 78 47 65 74 4c 61 73 74 45 72 72 6f 72 29 28 *xGetLastError)(
d2f0: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e sqlite3_vfs*, in
d300: 74 2c 20 63 68 61 72 20 2a 29 3b 0a 20 20 2f 2a t, char *);. /*
d310: 20 4e 65 77 20 66 69 65 6c 64 73 20 6d 61 79 20 New fields may
d320: 62 65 20 61 70 70 65 6e 64 65 64 20 69 6e 20 66 be appended in f
d330: 69 67 75 72 65 20 76 65 72 73 69 6f 6e 73 2e 20 igure versions.
d340: 20 54 68 65 20 69 56 65 72 73 69 6f 6e 0a 20 20 The iVersion.
d350: 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 69 6e ** value will in
d360: 63 72 65 6d 65 6e 74 20 77 68 65 6e 65 76 65 72 crement whenever
d370: 20 74 68 69 73 20 68 61 70 70 65 6e 73 2e 20 2a this happens. *
d380: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.};../*.** CAPI
d390: 33 52 45 46 3a 20 46 6c 61 67 73 20 66 6f 72 20 3REF: Flags for
d3a0: 74 68 65 20 78 41 63 63 65 73 73 20 56 46 53 20 the xAccess VFS
d3b0: 6d 65 74 68 6f 64 20 7b 48 31 31 31 39 30 7d 20 method {H11190}
d3c0: 3c 48 31 31 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 7b <H11140>.**.** {
d3d0: 48 31 31 31 39 31 7d 20 54 68 65 73 65 20 69 6e H11191} These in
d3e0: 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 teger constants
d3f0: 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 can be used as t
d400: 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 he third paramet
d410: 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 41 63 er to.** the xAc
d420: 63 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 61 cess method of a
d430: 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 n [sqlite3_vfs]
d440: 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 20 54 object. {END} T
d450: 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a hey determine.**
d460: 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70 65 what kind of pe
d470: 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78 41 rmissions the xA
d480: 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73 20 ccess method is
d490: 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20 looking for..**
d4a0: 7b 48 31 31 31 39 32 7d 20 57 69 74 68 20 53 51 {H11192} With SQ
d4b0: 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
d4c0: 54 53 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 TS, the xAccess
d4d0: 6d 65 74 68 6f 64 0a 2a 2a 20 73 69 6d 70 6c 79 method.** simply
d4e0: 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 checks whether
d4f0: 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 2e the file exists.
d500: 0a 2a 2a 20 7b 48 31 31 31 39 33 7d 20 57 69 74 .** {H11193} Wit
d510: 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f h SQLITE_ACCESS_
d520: 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78 READWRITE, the x
d530: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a Access method.**
d540: 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 checks whether
d550: 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68 the file is both
d560: 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 readable and wr
d570: 69 74 61 62 6c 65 2e 0a 2a 2a 20 7b 48 31 31 31 itable..** {H111
d580: 39 34 7d 20 57 69 74 68 20 53 51 4c 49 54 45 5f 94} With SQLITE_
d590: 41 43 43 45 53 53 5f 52 45 41 44 2c 20 74 68 65 ACCESS_READ, the
d5a0: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a xAccess method.
d5b0: 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 ** checks whethe
d5c0: 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 r the file is re
d5d0: 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 adable..*/.#defi
d5e0: 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 ne SQLITE_ACCESS
d5f0: 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64 65 _EXISTS 0.#de
d600: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 fine SQLITE_ACCE
d610: 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 0a 23 SS_READWRITE 1.#
d620: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 define SQLITE_AC
d630: 43 45 53 53 5f 52 45 41 44 20 20 20 20 20 20 32 CESS_READ 2
d640: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
d650: 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 65 : Initialize The
d660: 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 20 SQLite Library
d670: 7b 48 31 30 31 33 30 7d 20 3c 53 32 30 30 30 30 {H10130} <S20000
d680: 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 ><S30100>.**.**
d690: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 The sqlite3_init
d6a0: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 ialize() routine
d6b0: 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 initializes the
d6c0: 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72 61 .** SQLite libra
d6d0: 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 ry. The sqlite3
d6e0: 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f 75 74 _shutdown() rout
d6f0: 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 ine.** deallocat
d700: 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 es any resources
d710: 20 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f 63 that were alloc
d720: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f ated by sqlite3_
d730: 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a initialize()..**
d740: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 .** A call to sq
d750: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
d760: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 () is an "effect
d770: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 ive" call if it
d780: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 is.** the first
d790: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 time sqlite3_ini
d7a0: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 tialize() is inv
d7b0: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 oked during the
d7c0: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 lifetime of.** t
d7d0: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 he process, or i
d7e0: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73 f it is the firs
d7f0: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 t time sqlite3_i
d800: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 nitialize() is i
d810: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 nvoked.** follow
d820: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ing a call to sq
d830: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
d840: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63 . Only an effec
d850: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 tive call.** of
d860: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
d870: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e ze() does any in
d880: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 itialization. A
d890: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a ll other calls.*
d8a0: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e * are harmless n
d8b0: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f o-ops..**.** Amo
d8c0: 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c ng other things,
d8d0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
d8e0: 69 7a 65 28 29 20 73 68 61 6c 6c 20 69 6e 76 6f ize() shall invo
d8f0: 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 ke.** sqlite3_os
d900: 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61 _init(). Simila
d910: 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f 73 68 75 rly, sqlite3_shu
d920: 74 64 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c tdown().** shall
d930: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
d940: 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 os_end()..**.**
d950: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 The sqlite3_init
d960: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 ialize() routine
d970: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
d980: 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 2e _OK] on success.
d990: 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d 65 20 .** If for some
d9a0: 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 33 5f reason, sqlite3_
d9b0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 initialize() is
d9c0: 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61 unable to initia
d9d0: 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 lize.** the libr
d9e0: 61 72 79 20 28 70 65 72 68 61 70 73 20 69 74 20 ary (perhaps it
d9f0: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c is unable to all
da00: 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 20 72 ocate a needed r
da10: 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a 2a 20 esource such.**
da20: 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 20 72 as a mutex) it r
da30: 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 6f 72 eturns an [error
da40: 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 68 61 code] other tha
da50: 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a n [SQLITE_OK]..*
da60: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
da70: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f _initialize() ro
da80: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
da90: 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61 internally by ma
daa0: 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 ny other.** SQLi
dab0: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f te interfaces so
dac0: 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61 that an applica
dad0: 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65 tion usually doe
dae0: 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 2a 2a s not need to.**
daf0: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
db00: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72 initialize() dir
db10: 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d ectly. For exam
db20: 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ple, [sqlite3_op
db30: 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73 en()].** calls s
db40: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
db50: 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c 69 74 e() so the SQLit
db60: 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 62 e library will b
db70: 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a e automatically.
db80: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 ** initialized w
db90: 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 hen [sqlite3_ope
dba0: 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 69 n()] is called i
dbb0: 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 20 f it has not be
dbc0: 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 initialized.** a
dbd0: 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76 65 72 lready. However
dbe0: 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 , if SQLite is c
dbf0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
dc00: 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 [SQLITE_OMIT_AU
dc10: 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69 TOINIT].** compi
dc20: 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 le-time option,
dc30: 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d 61 74 then the automat
dc40: 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 ic calls to sqli
dc50: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
dc60: 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65 64 20 .** are omitted
dc70: 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 and the applicat
dc80: 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 ion must call sq
dc90: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
dca0: 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 70 () directly.** p
dcb0: 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e rior to using an
dcc0: 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 y other SQLite i
dcd0: 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72 20 6d nterface. For m
dce0: 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69 6c 69 aximum portabili
dcf0: 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72 65 63 ty,.** it is rec
dd00: 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70 ommended that ap
dd10: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77 61 79 plications alway
dd20: 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 s invoke sqlite3
dd30: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a _initialize().**
dd40: 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f 72 20 directly prior
dd50: 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 to using any oth
dd60: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 er SQLite interf
dd70: 61 63 65 2e 20 20 46 75 74 75 72 65 20 72 65 6c ace. Future rel
dd80: 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 eases.** of SQLi
dd90: 74 65 20 6d 61 79 20 72 65 71 75 69 72 65 20 74 te may require t
dda0: 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 his. In other w
ddb0: 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61 76 69 ords, the behavi
ddc0: 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a 2a 20 or exhibited.**
ddd0: 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 when SQLite is c
dde0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51 ompiled with [SQ
ddf0: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
de00: 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 IT] might become
de10: 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c 74 20 the.** default
de20: 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f 6d 65 behavior in some
de30: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 20 future release
de40: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a of SQLite..**.**
de50: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f The sqlite3_os_
de60: 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64 init() routine d
de70: 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 oes operating-sy
de80: 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a 2a 2a stem specific.**
de90: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
dea0: 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 of the SQLite li
deb0: 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69 brary. The sqli
dec0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 te3_os_end().**
ded0: 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74 routine undoes t
dee0: 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c he effect of sql
def0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 ite3_os_init().
df00: 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 0a 2a Typical tasks.*
df10: 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 * performed by t
df20: 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e hese routines in
df30: 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e clude allocation
df40: 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f 6e or deallocation
df50: 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20 72 65 .** of static re
df60: 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69 61 6c sources, initial
df70: 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f 62 61 ization of globa
df80: 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a 2a 20 l variables,.**
df90: 73 65 74 74 69 6e 67 20 75 70 20 61 20 64 65 66 setting up a def
dfa0: 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 ault [sqlite3_vf
dfb0: 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 73 65 s] module, or se
dfc0: 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20 64 65 tting up.** a de
dfd0: 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 fault configurat
dfe0: 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ion using [sqlit
dff0: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a e3_config()]..**
e000: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 .** The applicat
e010: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 ion should never
e020: 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72 20 73 invoke either s
e030: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
e040: 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f .** or sqlite3_o
e050: 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74 6c 79 s_end() directly
e060: 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 . The applicati
e070: 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 69 on should only i
e080: 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 nvoke.** sqlite3
e090: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e _initialize() an
e0a0: 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f d sqlite3_shutdo
e0b0: 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c 69 74 wn(). The sqlit
e0c0: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 e3_os_init().**
e0d0: 69 6e 74 65 72 66 61 63 65 20 69 73 20 63 61 6c interface is cal
e0e0: 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c led automaticall
e0f0: 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 y by sqlite3_ini
e100: 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a 2a 2a tialize() and.**
e110: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
e120: 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73 ) is called by s
e130: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
e140: 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 0a ). Appropriate.
e150: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
e160: 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f ns for sqlite3_o
e170: 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c s_init() and sql
e180: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a ite3_os_end().**
e190: 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 are built into
e1a0: 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74 20 69 SQLite when it i
e1b0: 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 20 75 s compiled for u
e1c0: 6e 69 78 2c 20 77 69 6e 64 6f 77 73 2c 20 6f 72 nix, windows, or
e1d0: 20 6f 73 2f 32 2e 0a 2a 2a 20 57 68 65 6e 20 62 os/2..** When b
e1e0: 75 69 6c 74 20 66 6f 72 20 6f 74 68 65 72 20 70 uilt for other p
e1f0: 6c 61 74 66 6f 72 6d 73 20 28 75 73 69 6e 67 20 latforms (using
e200: 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f the [SQLITE_OS_O
e210: 54 48 45 52 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d THER=1] compile-
e220: 74 69 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20 time.** option)
e230: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
e240: 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 73 75 must supply a su
e250: 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 itable implement
e260: 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c ation for.** sql
e270: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 ite3_os_init() a
e280: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nd sqlite3_os_en
e290: 64 28 29 2e 20 20 41 6e 20 61 70 70 6c 69 63 61 d(). An applica
e2a0: 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 0a 2a 2a tion-supplied.**
e2b0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
e2c0: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e of sqlite3_os_in
e2d0: 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f it() or sqlite3_
e2e0: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 6d 75 73 74 os_end().** must
e2f0: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
e300: 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61 OK] on success a
e310: 6e 64 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b 65 nd some other [e
e320: 72 72 6f 72 20 63 6f 64 65 5d 20 75 70 6f 6e 0a rror code] upon.
e330: 2a 2a 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53 ** failure..*/.S
e340: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e350: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
e360: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 (void);.SQLITE_A
e370: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
e380: 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 53 hutdown(void);.S
e390: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e3a0: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f lite3_os_init(vo
e3b0: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
e3c0: 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 int sqlite3_os_e
e3d0: 6e 64 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a nd(void);../*.**
e3e0: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 CAPI3REF: Confi
e3f0: 67 75 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74 guring The SQLit
e400: 65 20 4c 69 62 72 61 72 79 20 7b 48 31 34 31 30 e Library {H1410
e410: 30 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 32 0} <S20000><S302
e420: 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
e430: 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 TAL.**.** The sq
e440: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 lite3_config() i
e450: 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
e460: 20 74 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 to make global
e470: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a configuration.**
e480: 20 63 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69 changes to SQLi
e490: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 te in order to t
e4a0: 75 6e 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68 une SQLite to th
e4b0: 65 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73 e specific needs
e4c0: 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 of.** the appli
e4d0: 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 cation. The def
e4e0: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 ault configurati
e4f0: 6f 6e 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 on is recommende
e500: 64 20 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 d for most.** ap
e510: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73 plications and s
e520: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 o this routine i
e530: 73 20 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 s usually not ne
e540: 63 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a cessary. It is.
e550: 2a 2a 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73 ** provided to s
e560: 75 70 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c upport rare appl
e570: 69 63 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e ications with un
e580: 75 73 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a usual needs..**.
e590: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
e5a0: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
e5b0: 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 e is not threads
e5c0: 61 66 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63 afe. The applic
e5d0: 61 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e ation.** must in
e5e0: 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 sure that no oth
e5f0: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 er SQLite interf
e600: 61 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 aces are invoked
e610: 20 62 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 by other.** thr
e620: 65 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74 eads while sqlit
e630: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72 e3_config() is r
e640: 75 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72 unning. Further
e650: 6d 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f more, sqlite3_co
e660: 6e 66 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e nfig().** may on
e670: 6c 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 ly be invoked pr
e680: 69 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69 ior to library i
e690: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73 nitialization us
e6a0: 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ing.** [sqlite3_
e6b0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72 initialize()] or
e6c0: 20 61 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20 after shutdown
e6d0: 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 by [sqlite3_shut
e6e0: 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 down()]..** Note
e6f0: 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 20 , however, that
e700: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
e710: 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 can be called a
e720: 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a s part of the.**
e730: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
e740: 6f 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f of an applicatio
e750: 6e 2d 64 65 66 69 6e 65 64 20 5b 73 71 6c 69 74 n-defined [sqlit
e760: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a e3_os_init()]..*
e770: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
e780: 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
e790: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61 e3_config() is a
e7a0: 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51 n integer.** [SQ
e7b0: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 LITE_CONFIG_SING
e7c0: 4c 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 LETHREAD | confi
e7d0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d guration option]
e7e0: 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 that determines
e7f0: 0a 2a 2a 20 77 68 61 74 20 70 72 6f 70 65 72 74 .** what propert
e800: 79 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 74 y of SQLite is t
e810: 6f 20 62 65 20 63 6f 6e 66 69 67 75 72 65 64 2e o be configured.
e820: 20 20 53 75 62 73 65 71 75 65 6e 74 20 61 72 67 Subsequent arg
e830: 75 6d 65 6e 74 73 0a 2a 2a 20 76 61 72 79 20 64 uments.** vary d
e840: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 epending on the
e850: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
e860: 49 4e 47 4c 45 54 48 52 45 41 44 20 7c 20 63 6f INGLETHREAD | co
e870: 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
e880: 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 on].** in the fi
e890: 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a rst argument..**
e8a0: 0a 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 66 69 .** When a confi
e8b0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 guration option
e8c0: 69 73 20 73 65 74 2c 20 73 71 6c 69 74 65 33 5f is set, sqlite3_
e8d0: 63 6f 6e 66 69 67 28 29 20 72 65 74 75 72 6e 73 config() returns
e8e0: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a [SQLITE_OK]..**
e8f0: 20 49 66 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 If the option i
e900: 73 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c s unknown or SQL
e910: 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f ite is unable to
e920: 20 73 65 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a set the option.
e930: 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 ** then this rou
e940: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e tine returns a n
e950: 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 on-zero [error c
e960: 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 ode]..**.** INVA
e970: 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
e980: 31 34 31 30 33 7d 20 41 20 73 75 63 63 65 73 73 14103} A success
e990: 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f ful invocation o
e9a0: 66 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 f [sqlite3_confi
e9b0: 67 28 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 g()] shall retur
e9c0: 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 n.** [S
e9d0: 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a QLITE_OK]..**.**
e9e0: 20 7b 48 31 34 31 30 36 7d 20 54 68 65 20 5b 73 {H14106} The [s
e9f0: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
ea00: 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c interface shall
ea10: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
ea20: 4d 49 53 55 53 45 5d 0a 2a 2a 20 20 20 20 20 20 MISUSE].**
ea30: 20 20 20 20 69 66 20 69 74 20 69 73 20 69 6e 76 if it is inv
ea40: 6f 6b 65 64 20 69 6e 20 62 65 74 77 65 65 6e 20 oked in between
ea50: 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
ea60: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 3_initialize()]
ea70: 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
ea80: 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 [sqlite3_shutdow
ea90: 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 n()]..**.** {H14
eaa0: 31 32 30 7d 20 41 20 73 75 63 63 65 73 73 66 75 120} A successfu
eab0: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
eac0: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 e3_config]([SQLI
ead0: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 TE_CONFIG_SINGLE
eae0: 54 48 52 45 41 44 5d 29 0a 2a 2a 20 20 20 20 20 THREAD]).**
eaf0: 20 20 20 20 20 73 68 61 6c 6c 20 73 65 74 20 74 shall set t
eb00: 68 65 20 64 65 66 61 75 6c 74 20 5b 74 68 72 65 he default [thre
eb10: 61 64 69 6e 67 20 6d 6f 64 65 5d 20 74 6f 20 53 ading mode] to S
eb20: 69 6e 67 6c 65 2d 74 68 72 65 61 64 2e 0a 2a 2a ingle-thread..**
eb30: 0a 2a 2a 20 7b 48 31 34 31 32 33 7d 20 41 20 73 .** {H14123} A s
eb40: 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
eb50: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 o [sqlite3_confi
eb60: 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 g]([SQLITE_CONFI
eb70: 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d 29 0a G_MULTITHREAD]).
eb80: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c ** shal
eb90: 6c 20 73 65 74 20 74 68 65 20 64 65 66 61 75 6c l set the defaul
eba0: 74 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 t [threading mod
ebb0: 65 5d 20 74 6f 20 4d 75 6c 74 69 2d 74 68 72 65 e] to Multi-thre
ebc0: 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32 ad..**.** {H1412
ebd0: 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 6} A successful
ebe0: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
ebf0: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
ec00: 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a _CONFIG_SERIALIZ
ec10: 45 44 5d 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 ED]).**
ec20: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 64 shall set the d
ec30: 65 66 61 75 6c 74 20 5b 74 68 72 65 61 64 69 6e efault [threadin
ec40: 67 20 6d 6f 64 65 5d 20 74 6f 20 53 65 72 69 61 g mode] to Seria
ec50: 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 lized..**.** {H1
ec60: 34 31 32 39 7d 20 41 20 73 75 63 63 65 73 73 66 4129} A successf
ec70: 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ul call to [sqli
ec80: 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c te3_config]([SQL
ec90: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
eca0: 5d 2c 58 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 ],X).**
ecb0: 20 77 68 65 72 65 20 58 20 69 73 20 61 20 70 6f where X is a po
ecc0: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 69 74 inter to an init
ecd0: 69 61 6c 69 7a 65 64 20 5b 73 71 6c 69 74 65 33 ialized [sqlite3
ece0: 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a _mutex_methods].
ecf0: 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65 ** obje
ed00: 63 74 20 73 68 61 6c 6c 20 63 61 75 73 65 20 61 ct shall cause a
ed10: 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 6d 75 ll subsequent mu
ed20: 74 65 78 20 6f 70 65 72 61 74 69 6f 6e 73 20 70 tex operations p
ed30: 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 20 20 20 20 erformed.**
ed40: 20 20 20 20 20 62 79 20 53 51 4c 69 74 65 20 74 by SQLite t
ed50: 6f 20 75 73 65 20 74 68 65 20 6d 75 74 65 78 20 o use the mutex
ed60: 6d 65 74 68 6f 64 73 20 74 68 61 74 20 77 65 72 methods that wer
ed70: 65 20 70 72 65 73 65 6e 74 20 69 6e 20 58 0a 2a e present in X.*
ed80: 2a 20 20 20 20 20 20 20 20 20 20 64 75 72 69 6e * durin
ed90: 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 g the call to [s
eda0: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
edb0: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 32 7d ..**.** {H14132}
edc0: 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 A successful ca
edd0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
ede0: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
edf0: 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 5d 2c ONFIG_GETMUTEX],
ee00: 58 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 X).** w
ee10: 68 65 72 65 20 58 20 69 73 20 61 20 70 6f 69 6e here X is a poin
ee20: 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74 ter to an [sqlit
ee30: 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
ee40: 5d 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 20 20 20 ] object .**
ee50: 20 20 20 20 20 20 73 68 61 6c 6c 20 6f 76 65 72 shall over
ee60: 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
ee70: 74 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 75 t of [sqlite3_mu
ee80: 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a tex_methods] obj
ee90: 65 63 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ect.**
eea0: 77 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 6d with the mutex m
eeb0: 65 74 68 6f 64 73 20 63 75 72 72 65 6e 74 6c 79 ethods currently
eec0: 20 69 6e 20 75 73 65 20 62 79 20 53 51 4c 69 74 in use by SQLit
eed0: 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 35 e..**.** {H14135
eee0: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
eef0: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
ef00: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f config]([SQLITE_
ef10: 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2c 4d CONFIG_MALLOC],M
ef20: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 ).** wh
ef30: 65 72 65 20 4d 20 69 73 20 61 20 70 6f 69 6e 74 ere M is a point
ef40: 65 72 20 74 6f 20 61 6e 20 69 6e 69 74 69 61 6c er to an initial
ef50: 69 7a 65 64 20 5b 73 71 6c 69 74 65 33 5f 6d 65 ized [sqlite3_me
ef60: 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 20 20 m_methods].**
ef70: 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 73 68 object sh
ef80: 61 6c 6c 20 63 61 75 73 65 20 61 6c 6c 20 73 75 all cause all su
ef90: 62 73 65 71 75 65 6e 74 20 6d 65 6d 6f 72 79 20 bsequent memory
efa0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 70 65 72 61 allocation opera
efb0: 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 tions.**
efc0: 20 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 53 performed by S
efd0: 51 4c 69 74 65 20 74 6f 20 75 73 65 20 74 68 65 QLite to use the
efe0: 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 77 65 methods that we
eff0: 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 0a 2a re present in .*
f000: 2a 20 20 20 20 20 20 20 20 20 20 4d 20 64 75 72 * M dur
f010: 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 ing the call to
f020: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
f030: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 )]..**.** {H1413
f040: 38 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 8} A successful
f050: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
f060: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
f070: 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f _CONFIG_GETMALLO
f080: 43 5d 2c 4d 29 0a 2a 2a 20 20 20 20 20 20 20 20 C],M).**
f090: 20 20 77 68 65 72 65 20 4d 20 69 73 20 61 20 70 where M is a p
f0a0: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 ointer to an [sq
f0b0: 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
f0c0: 73 5d 20 6f 62 6a 65 63 74 20 73 68 61 6c 6c 0a s] object shall.
f0d0: 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 ** over
f0e0: 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
f0f0: 74 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 65 t of [sqlite3_me
f100: 6d 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 m_methods] objec
f110: 74 20 77 69 74 68 20 0a 2a 2a 20 20 20 20 20 20 t with .**
f120: 20 20 20 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 the memory a
f130: 6c 6c 6f 63 61 74 69 6f 6e 20 6d 65 74 68 6f 64 llocation method
f140: 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 s currently in u
f150: 73 65 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 se by.**
f160: 20 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 SQLite..**.**
f170: 7b 48 31 34 31 34 31 7d 20 41 20 73 75 63 63 65 {H14141} A succe
f180: 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
f190: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b qlite3_config]([
f1a0: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 SQLITE_CONFIG_ME
f1b0: 4d 53 54 41 54 55 53 5d 2c 31 29 0a 2a 2a 20 20 MSTATUS],1).**
f1c0: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 65 6e shall en
f1d0: 61 62 6c 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 able the memory
f1e0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 74 61 74 75 allocation statu
f1f0: 73 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6c 6f 67 s collection log
f200: 69 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 34 ic..**.** {H1414
f210: 34 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 4} A successful
f220: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
f230: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
f240: 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 _CONFIG_MEMSTATU
f250: 53 5d 2c 30 29 0a 2a 2a 20 20 20 20 20 20 20 20 S],0).**
f260: 20 20 73 68 61 6c 6c 20 64 69 73 61 62 6c 65 20 shall disable
f270: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
f280: 61 74 69 6f 6e 20 73 74 61 74 75 73 20 63 6f 6c ation status col
f290: 6c 65 63 74 69 6f 6e 20 6c 6f 67 69 63 2e 0a 2a lection logic..*
f2a0: 2a 0a 2a 2a 20 7b 48 31 34 31 34 37 7d 20 54 68 *.** {H14147} Th
f2b0: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
f2c0: 69 6f 6e 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 ion status colle
f2d0: 63 74 69 6f 6e 20 6c 6f 67 69 63 20 73 68 61 6c ction logic shal
f2e0: 6c 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 l be.**
f2f0: 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 enabled by defa
f300: 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 ult..**.** {H141
f310: 35 30 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 50} A successful
f320: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
f330: 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 3_config]([SQLIT
f340: 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
f350: 5d 2c 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 ],S,Z,N).**
f360: 20 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e 64 where Z and
f370: 20 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 N are non-negat
f380: 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 ive integers and
f390: 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 20 .** S
f3a0: 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
f3b0: 61 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 an aligned memor
f3c0: 79 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 73 y buffer not les
f3d0: 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 s than.**
f3e0: 20 20 20 5a 2a 4e 20 62 79 74 65 73 20 69 6e 20 Z*N bytes in
f3f0: 73 69 7a 65 20 73 68 61 6c 6c 20 63 61 75 73 65 size shall cause
f400: 20 53 20 74 6f 20 62 65 20 75 73 65 64 20 62 79 S to be used by
f410: 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
f420: 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 [scratch memory
f430: 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f 72 20 allocator] for
f440: 61 73 20 6d 61 6e 79 20 61 73 20 4e 20 73 69 6d as many as N sim
f450: 75 6c 61 74 61 6e 65 6f 75 73 0a 2a 2a 20 20 20 ulataneous.**
f460: 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f allocatio
f470: 6e 73 20 65 61 63 68 20 6f 66 20 73 69 7a 65 20 ns each of size
f480: 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 33 Z..**.** {H14153
f490: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
f4a0: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
f4b0: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f config]([SQLITE_
f4c0: 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2c CONFIG_SCRATCH],
f4d0: 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 S,Z,N).**
f4e0: 20 20 20 77 68 65 72 65 20 53 20 69 73 20 61 20 where S is a
f4f0: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 NULL pointer sha
f500: 6c 6c 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a ll disable the.*
f510: 2a 20 20 20 20 20 20 20 20 20 20 5b 73 63 72 61 * [scra
f520: 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 tch memory alloc
f530: 61 74 6f 72 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ator]..**.** {H1
f540: 34 31 35 36 7d 20 41 20 73 75 63 63 65 73 73 66 4156} A successf
f550: 75 6c 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 ul call to.**
f560: 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
f570: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f config]([SQLITE_
f580: 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
f590: 5d 2c 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 ],S,Z,N).**
f5a0: 20 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e 64 where Z and
f5b0: 20 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 N are non-negat
f5c0: 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 ive integers and
f5d0: 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 20 .** S
f5e0: 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
f5f0: 61 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 an aligned memor
f600: 79 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 73 y buffer not les
f610: 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 s than.**
f620: 20 20 20 5a 2a 4e 20 62 79 74 65 73 20 69 6e 20 Z*N bytes in
f630: 73 69 7a 65 20 73 68 61 6c 6c 20 63 61 75 73 65 size shall cause
f640: 20 53 20 74 6f 20 62 65 20 75 73 65 64 20 62 79 S to be used by
f650: 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
f660: 20 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f [pagecache memo
f670: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f ry allocator] fo
f680: 72 20 61 73 20 6d 61 6e 79 20 61 73 20 4e 20 73 r as many as N s
f690: 69 6d 75 6c 61 74 61 6e 65 6f 75 73 0a 2a 2a 20 imulataneous.**
f6a0: 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 allocat
f6b0: 69 6f 6e 73 20 65 61 63 68 20 6f 66 20 73 69 7a ions each of siz
f6c0: 65 20 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 e Z..**.** {H141
f6d0: 35 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 59} A successful
f6e0: 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 call to.**
f6f0: 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f [sqlite3_co
f700: 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f nfig]([SQLITE_CO
f710: 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2c NFIG_PAGECACHE],
f720: 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 S,Z,N).**
f730: 20 20 20 77 68 65 72 65 20 53 20 69 73 20 61 20 where S is a
f740: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 NULL pointer sha
f750: 6c 6c 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a ll disable the.*
f760: 2a 20 20 20 20 20 20 20 20 20 20 5b 70 61 67 65 * [page
f770: 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c cache memory all
f780: 6f 63 61 74 6f 72 5d 2e 0a 2a 2a 0a 2a 2a 20 7b ocator]..**.** {
f790: 48 31 34 31 36 32 7d 20 41 20 73 75 63 63 65 73 H14162} A succes
f7a0: 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 sful call to [sq
f7b0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 lite3_config]([S
f7c0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 QLITE_CONFIG_HEA
f7d0: 50 5d 2c 48 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 P],H,Z,N).**
f7e0: 20 20 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e where Z an
f7f0: 64 20 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 d N are non-nega
f800: 74 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e tive integers an
f810: 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 48 d .** H
f820: 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
f830: 20 61 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f an aligned memo
f840: 72 79 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 ry buffer not le
f850: 73 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 ss than.**
f860: 20 20 20 20 5a 20 62 79 74 65 73 20 69 6e 20 73 Z bytes in s
f870: 69 7a 65 20 73 68 61 6c 6c 20 65 6e 61 62 6c 65 ize shall enable
f880: 20 74 68 65 20 5b 6d 65 6d 73 79 73 35 5d 20 6d the [memsys5] m
f890: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 0a emory allocator.
f8a0: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 ** and
f8b0: 63 61 75 73 65 20 69 74 20 74 6f 20 75 73 65 20 cause it to use
f8c0: 62 75 66 66 65 72 20 53 20 61 73 20 69 74 73 20 buffer S as its
f8d0: 6d 65 6d 6f 72 79 20 73 6f 75 72 63 65 20 61 6e memory source an
f8e0: 64 20 74 6f 20 75 73 65 0a 2a 2a 20 20 20 20 20 d to use.**
f8f0: 20 20 20 20 20 61 20 6d 69 6e 69 6d 75 6d 20 61 a minimum a
f900: 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 6f llocation size o
f910: 66 20 4e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 f N..**.** {H141
f920: 36 35 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 65} A successful
f930: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
f940: 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 3_config]([SQLIT
f950: 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 5d 2c 48 E_CONFIG_HEAP],H
f960: 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 ,Z,N).**
f970: 20 20 77 68 65 72 65 20 48 20 69 73 20 61 20 4e where H is a N
f980: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 6c ULL pointer shal
f990: 6c 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a l disable the.**
f9a0: 20 20 20 20 20 20 20 20 20 20 5b 6d 65 6d 73 79 [memsy
f9b0: 73 35 5d 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 s5] memory alloc
f9c0: 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 ator..**.** {H14
f9d0: 31 36 38 7d 20 41 20 73 75 63 63 65 73 73 66 75 168} A successfu
f9e0: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
f9f0: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 e3_config]([SQLI
fa00: 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 TE_CONFIG_LOOKAS
fa10: 49 44 45 5d 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 IDE],Z,N).**
fa20: 20 20 20 20 20 20 73 68 61 6c 6c 20 63 61 75 73 shall caus
fa30: 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 6c e the default [l
fa40: 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 ookaside memory
fa50: 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 allocator] confi
fa60: 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 guration.**
fa70: 20 20 20 20 20 66 6f 72 20 6e 65 77 20 5b 64 61 for new [da
fa80: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
fa90: 6e 73 5d 20 74 6f 20 62 65 20 4e 20 73 6c 6f 74 ns] to be N slot
faa0: 73 20 6f 66 20 5a 20 62 79 74 65 73 20 65 61 63 s of Z bytes eac
fab0: 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 h..*/.SQLITE_API
fac0: 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 SQLITE_EXPERIME
fad0: 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 NTAL int sqlite3
fae0: 5f 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e 2e 2e _config(int, ...
faf0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
fb00: 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20 64 61 EF: Configure da
fb10: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
fb20: 6e 73 20 20 7b 48 31 34 32 30 30 7d 20 3c 53 32 ns {H14200} <S2
fb30: 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0000>.** EXPERIM
fb40: 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ENTAL.**.** The
fb50: 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 sqlite3_db_confi
fb60: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 g() interface is
fb70: 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 63 6f used to make co
fb80: 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 nfiguration.** c
fb90: 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64 61 74 hanges to a [dat
fba0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
fbb0: 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 ]. The interfac
fbc0: 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 0a e is similar to.
fbd0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ** [sqlite3_conf
fbe0: 69 67 28 29 5d 20 65 78 63 65 70 74 20 74 68 61 ig()] except tha
fbf0: 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 61 70 t the changes ap
fc00: 70 6c 79 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a ply to a single.
fc10: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
fc20: 6e 65 63 74 69 6f 6e 5d 20 28 73 70 65 63 69 66 nection] (specif
fc30: 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 ied in the first
fc40: 20 61 72 67 75 6d 65 6e 74 29 2e 20 20 54 68 65 argument). The
fc50: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 .** sqlite3_db_c
fc60: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
fc70: 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 e can only be us
fc80: 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 ed immediately a
fc90: 66 74 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 fter.** the data
fca0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
fcb0: 69 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 is created using
fcc0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
fcd0: 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f ],.** [sqlite3_o
fce0: 70 65 6e 31 36 28 29 5d 2c 20 6f 72 20 5b 73 71 pen16()], or [sq
fcf0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
fd00: 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 . .**.** The se
fd10: 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
fd20: 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 sqlite3_db_conf
fd30: 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73 20 ig(D,V,...) is
fd40: 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61 the.** configura
fd50: 74 69 6f 6e 20 76 65 72 62 20 2d 20 61 6e 20 69 tion verb - an i
fd60: 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 nteger code that
fd70: 20 69 6e 64 69 63 61 74 65 73 20 77 68 61 74 0a indicates what.
fd80: 2a 2a 20 61 73 70 65 63 74 20 6f 66 20 74 68 65 ** aspect of the
fd90: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
fda0: 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 ction] is being
fdb0: 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a 20 54 configured..** T
fdc0: 68 65 20 6f 6e 6c 79 20 63 68 6f 69 63 65 20 66 he only choice f
fdd0: 6f 72 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 or this value is
fde0: 20 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 [SQLITE_DBCONFI
fdf0: 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a 2a 2a G_LOOKASIDE]..**
fe00: 20 4e 65 77 20 76 65 72 62 73 20 61 72 65 20 6c New verbs are l
fe10: 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 64 64 65 ikely to be adde
fe20: 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 d in future rele
fe30: 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
fe40: 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72 ** Additional ar
fe50: 67 75 6d 65 6e 74 73 20 64 65 70 65 6e 64 20 6f guments depend o
fe60: 6e 20 74 68 65 20 76 65 72 62 2e 0a 2a 2a 0a 2a n the verb..**.*
fe70: 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
fe80: 0a 2a 2a 20 7b 48 31 34 32 30 33 7d 20 41 20 63 .** {H14203} A c
fe90: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
fea0: 64 62 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e db_config(D,V,..
feb0: 2e 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e .)] shall return
fec0: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 [SQLITE_OK].**
fed0: 20 20 20 20 20 20 20 20 20 69 66 20 61 6e 64 20 if and
fee0: 6f 6e 6c 79 20 69 66 20 74 68 65 20 63 61 6c 6c only if the call
fef0: 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 0a is successful..
ff00: 2a 2a 0a 2a 2a 20 7b 48 31 34 32 30 36 7d 20 49 **.** {H14206} I
ff10: 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 6c f one or more sl
ff20: 6f 74 73 20 6f 66 20 74 68 65 20 5b 6c 6f 6f 6b ots of the [look
ff30: 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c aside memory all
ff40: 6f 63 61 74 6f 72 5d 20 66 6f 72 0a 2a 2a 20 20 ocator] for.**
ff50: 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 [databas
ff60: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 e connection] D
ff70: 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68 65 6e are in use, then
ff80: 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 a call to.**
ff90: 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
ffa0: 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 db_config](D,[SQ
ffb0: 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f LITE_DBCONFIG_LO
ffc0: 4f 4b 41 53 49 44 45 5d 2c 2e 2e 2e 29 20 73 68 OKASIDE],...) sh
ffd0: 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 all.**
ffe0: 66 61 69 6c 20 77 69 74 68 20 61 6e 20 5b 53 51 fail with an [SQ
fff0: 4c 49 54 45 5f 42 55 53 59 5d 20 72 65 74 75 72 LITE_BUSY] retur
10000 6e 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 n code..**.** {H
10010 31 34 32 30 39 7d 20 41 20 73 75 63 63 65 73 73 14209} A success
10020 66 75 6c 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 ful call to .**
10030 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
10040 33 5f 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 3_db_config](D,[
10050 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f SQLITE_DBCONFIG_
10060 4c 4f 4f 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e LOOKASIDE],B,Z,N
10070 29 20 77 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 ) where.**
10080 20 20 20 20 44 20 69 73 20 61 6e 20 6f 70 65 6e D is an open
10090 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
100a0 63 74 69 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 ction] and Z and
100b0 20 4e 20 61 72 65 20 70 6f 73 69 74 69 76 65 0a N are positive.
100c0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 ** inte
100d0 67 65 72 73 20 61 6e 64 20 42 20 69 73 20 61 6e gers and B is an
100e0 20 61 6c 69 67 6e 65 64 20 62 75 66 66 65 72 20 aligned buffer
100f0 61 74 20 6c 65 61 73 74 20 5a 2a 4e 20 62 79 74 at least Z*N byt
10100 65 73 20 69 6e 20 73 69 7a 65 0a 2a 2a 20 20 20 es in size.**
10110 20 20 20 20 20 20 20 73 68 61 6c 6c 20 63 61 75 shall cau
10120 73 65 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 69 64 se the [lookasid
10130 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
10140 6f 72 5d 20 66 6f 72 20 44 20 74 6f 20 75 73 65 or] for D to use
10150 20 62 75 66 66 65 72 20 42 20 0a 2a 2a 20 20 20 buffer B .**
10160 20 20 20 20 20 20 20 77 69 74 68 20 4e 20 73 6c with N sl
10170 6f 74 73 20 6f 66 20 5a 20 62 79 74 65 73 20 65 ots of Z bytes e
10180 61 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32 ach..**.** {H142
10190 31 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 12} A successful
101a0 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 20 call to .**
101b0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 [sqlite3_d
101c0 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c b_config](D,[SQL
101d0 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f ITE_DBCONFIG_LOO
101e0 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77 KASIDE],B,Z,N) w
101f0 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 here.**
10200 20 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b 64 D is an open [d
10210 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
10220 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e 20 on] and Z and N
10230 61 72 65 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 are positive.**
10240 20 20 20 20 20 20 20 20 20 69 6e 74 65 67 65 72 integer
10250 73 20 61 6e 64 20 42 20 69 73 20 4e 55 4c 4c 20 s and B is NULL
10260 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 63 61 pointer shall ca
10270 75 73 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 use the.**
10280 20 20 20 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d [lookaside m
10290 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d emory allocator]
102a0 20 66 6f 72 20 44 20 74 6f 20 61 20 6f 62 74 61 for D to a obta
102b0 69 6e 20 5a 2a 4e 20 62 79 74 65 20 62 75 66 66 in Z*N byte buff
102c0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 er.** f
102d0 72 6f 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 rom the primary
102e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
102f0 20 61 6e 64 20 75 73 65 20 74 68 61 74 20 62 75 and use that bu
10300 66 66 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ffer.**
10310 20 77 69 74 68 20 4e 20 6c 6f 6f 6b 61 73 69 64 with N lookasid
10320 65 20 73 6c 6f 74 73 20 6f 66 20 5a 20 62 79 74 e slots of Z byt
10330 65 73 20 65 61 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b es each..**.** {
10340 48 31 34 32 31 35 7d 20 41 20 73 75 63 63 65 73 H14215} A succes
10350 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a sful call to .**
10360 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
10370 65 33 5f 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c e3_db_config](D,
10380 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 [SQLITE_DBCONFIG
10390 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c _LOOKASIDE],B,Z,
103a0 4e 29 20 77 68 65 72 65 0a 2a 2a 20 20 20 20 20 N) where.**
103b0 20 20 20 20 20 44 20 69 73 20 61 6e 20 6f 70 65 D is an ope
103c0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e n [database conn
103d0 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e ection] and Z an
103e0 64 20 4e 20 61 72 65 20 7a 65 72 6f 20 73 68 61 d N are zero sha
103f0 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 ll.** d
10400 69 73 61 62 6c 65 20 74 68 65 20 5b 6c 6f 6f 6b isable the [look
10410 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c aside memory all
10420 6f 63 61 74 6f 72 5d 20 66 6f 72 20 44 2e 0a 2a ocator] for D..*
10430 2a 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 *.**.*/.SQLITE_A
10440 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 PI SQLITE_EXPERI
10450 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 MENTAL int sqlit
10460 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c e3_db_config(sql
10470 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e ite3*, int op, .
10480 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ..);../*.** CAPI
10490 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 3REF: Memory All
104a0 6f 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 ocation Routines
104b0 20 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32 {H10155} <S2012
104c0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
104d0 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 AL.**.** An inst
104e0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a ance of this obj
104f0 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 ect defines the
10500 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65 interface betwee
10510 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 n SQLite.** and
10520 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
10530 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 allocation rout
10540 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ines..**.** This
10550 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 20 object is used
10560 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 in only one plac
10570 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 e in the SQLite
10580 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 interface..** A
10590 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e pointer to an in
105a0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
105b0 62 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 67 bject is the arg
105c0 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c ument to.** [sql
105d0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 ite3_config()] w
105e0 68 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72 hen the configur
105f0 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a ation option is.
10600 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
10610 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 42 79 20 63 G_MALLOC]. By c
10620 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 reating an insta
10630 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
10640 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e ct.** and passin
10650 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 g it to [sqlite3
10660 5f 63 6f 6e 66 69 67 28 29 5d 20 64 75 72 69 6e _config()] durin
10670 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c g configuration,
10680 20 61 6e 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 an.** applicati
10690 6f 6e 20 63 61 6e 20 73 70 65 63 69 66 79 20 61 on can specify a
106a0 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 n alternative me
106b0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
106c0 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 66 6f 72 subsystem.** for
106d0 20 53 51 4c 69 74 65 20 74 6f 20 75 73 65 20 66 SQLite to use f
106e0 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 or all of its dy
106f0 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65 namic memory nee
10700 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 ds..**.** Note t
10710 68 61 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73 hat SQLite comes
10720 20 77 69 74 68 20 61 20 62 75 69 6c 74 2d 69 6e with a built-in
10730 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
10740 72 20 74 68 61 74 20 69 73 0a 2a 2a 20 70 65 72 r that is.** per
10750 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 20 fectly adequate
10760 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c for the overwhel
10770 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66 ming majority of
10780 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a applications.**
10790 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f and that this o
107a0 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73 bject is only us
107b0 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d eful to a tiny m
107c0 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 inority of appli
107d0 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 cations.** with
107e0 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f specialized memo
107f0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 ry allocation re
10800 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69 quirements. Thi
10810 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61 s object is.** a
10820 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20 lso used during
10830 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74 testing of SQLit
10840 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 e in order to sp
10850 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 ecify an alterna
10860 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 tive.** memory a
10870 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69 llocator that si
10880 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f mulates memory o
10890 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e ut-of-memory con
108a0 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 ditions in.** or
108b0 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 der to verify th
108c0 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 at SQLite recove
108d0 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 rs gracefully fr
108e0 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 om such.** condi
108f0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tions..**.** The
10900 20 78 4d 61 6c 6c 6f 63 2c 20 78 46 72 65 65 2c xMalloc, xFree,
10910 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 20 6d 65 and xRealloc me
10920 74 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20 thods must work
10930 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c like the.** mall
10940 6f 63 28 29 2c 20 66 72 65 65 28 29 2c 20 61 6e oc(), free(), an
10950 64 20 72 65 61 6c 6c 6f 63 28 29 20 66 75 6e 63 d realloc() func
10960 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 tions from the s
10970 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 2e tandard library.
10980 0a 2a 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f .**.** xSize sho
10990 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 61 uld return the a
109a0 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 llocated size of
109b0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
109c0 74 69 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 tion.** previous
109d0 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ly obtained from
109e0 20 78 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 xMalloc or xRea
109f0 6c 6c 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 lloc. The alloc
10a00 61 74 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20 ated size.** is
10a10 61 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 always at least
10a20 61 73 20 62 69 67 20 61 73 20 74 68 65 20 72 65 as big as the re
10a30 71 75 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 quested size but
10a40 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a may be larger..
10a50 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64 **.** The xRound
10a60 75 70 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e up method return
10a70 73 20 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20 s what would be
10a80 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 the allocated si
10a90 7a 65 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 ze of.** a memor
10aa0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76 y allocation giv
10ab0 65 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 en a particular
10ac0 72 65 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20 requested size.
10ad0 20 4d 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 Most memory.**
10ae0 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 allocators round
10af0 20 75 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 up memory alloc
10b00 61 74 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20 ations at least
10b10 74 6f 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74 to the next mult
10b20 69 70 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 iple.** of 8. S
10b30 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 ome allocators r
10b40 6f 75 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72 ound up to a lar
10b50 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20 ger multiple or
10b60 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e to a power of 2.
10b70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 .**.** The xInit
10b80 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69 method initiali
10b90 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 zes the memory a
10ba0 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20 llocator. (For
10bb0 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d example,.** it m
10bc0 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e ight allocate an
10bd0 79 20 72 65 71 75 69 72 65 20 6d 75 74 65 78 65 y require mutexe
10be0 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20 s or initialize
10bf0 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a internal data.**
10c00 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 54 68 structures. Th
10c10 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 e xShutdown meth
10c20 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 od is invoked (i
10c30 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a ndirectly) by.**
10c40 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f [sqlite3_shutdo
10c50 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64 wn()] and should
10c60 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 deallocate any
10c70 72 65 73 6f 75 72 63 65 73 20 61 63 71 75 69 72 resources acquir
10c80 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20 ed.** by xInit.
10c90 20 54 68 65 20 70 41 70 70 44 61 74 61 20 70 6f The pAppData po
10ca0 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 inter is used as
10cb0 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65 the only parame
10cc0 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 ter to.** xInit
10cd0 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a and xShutdown..*
10ce0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
10cf0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
10d00 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 6d hods sqlite3_mem
10d10 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 _methods;.struct
10d20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
10d30 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 28 hods {. void *(
10d40 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 *xMalloc)(int);
10d50 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
10d60 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e y allocation fun
10d70 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ction */. void
10d80 28 2a 78 46 72 65 65 29 28 76 6f 69 64 2a 29 3b (*xFree)(void*);
10d90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 /* Fre
10da0 65 20 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 e a prior alloca
10db0 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a tion */. void *
10dc0 28 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64 (*xRealloc)(void
10dd0 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 69 *,int); /* Resi
10de0 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e ze an allocation
10df0 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 7a */. int (*xSiz
10e00 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 e)(void*);
10e10 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 /* Return t
10e20 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 6c he size of an al
10e30 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e location */. in
10e40 74 20 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 6e t (*xRoundup)(in
10e50 74 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t); /*
10e60 52 6f 75 6e 64 20 75 70 20 72 65 71 75 65 73 74 Round up request
10e70 20 73 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 74 size to allocat
10e80 69 6f 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e ion size */. in
10e90 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a t (*xInit)(void*
10ea0 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ); /*
10eb0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d Initialize the m
10ec0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
10ed0 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75 */. void (*xShu
10ee0 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 20 tdown)(void*);
10ef0 20 20 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 6c /* Deinitial
10f00 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ize the memory a
10f10 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f llocator */. vo
10f20 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 id *pAppData;
10f30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10f40 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e 69 Argument to xIni
10f50 74 28 29 20 61 6e 64 20 78 53 68 75 74 64 6f 77 t() and xShutdow
10f60 6e 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a n() */.};../*.**
10f70 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 CAPI3REF: Confi
10f80 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 guration Options
10f90 20 7b 48 31 30 31 36 30 7d 20 3c 53 32 30 30 30 {H10160} <S2000
10fa0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
10fb0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 AL.**.** These c
10fc0 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 onstants are the
10fd0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 available integ
10fe0 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e er configuration
10ff0 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a options that.**
11000 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 can be passed a
11010 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
11020 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c ment to the [sql
11030 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 ite3_config()] i
11040 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
11050 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f New configuratio
11060 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 n options may be
11070 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 added in future
11080 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c releases of SQL
11090 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 ite..** Existing
110a0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
110b0 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 ptions might be
110c0 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 discontinued. A
110d0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 pplications.** s
110e0 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 hould check the
110f0 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d return code from
11100 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 [sqlite3_config
11110 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ()] to make sure
11120 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c that.** the cal
11130 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b l worked. The [
11140 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
11150 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c ] interface will
11160 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e return a.** non
11170 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 -zero [error cod
11180 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 e] if a disconti
11190 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 nued or unsuppor
111a0 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ted configuratio
111b0 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 n option.** is i
111c0 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 nvoked..**.** <d
111d0 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 l>.** <dt>SQLITE
111e0 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
111f0 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 READ</dt>.** <dd
11200 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 >There are no ar
11210 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 guments to this
11220 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 option. This op
11230 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a tion disables.**
11240 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e all mutexing an
11250 64 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e d puts SQLite in
11260 74 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20 to a mode where
11270 69 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 it can only be u
11280 73 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67 sed.** by a sing
11290 6c 65 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e 0a le thread.</dd>.
112a0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
112b0 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 _CONFIG_MULTITHR
112c0 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e EAD</dt>.** <dd>
112d0 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 There are no arg
112e0 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f uments to this o
112f0 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 ption. This opt
11300 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 ion disables.**
11310 6d 75 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 74 mutexing on [dat
11320 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
11330 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 ] and [prepared
11340 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 statement] objec
11350 74 73 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 ts..** The appli
11360 63 61 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e cation is respon
11370 73 69 62 6c 65 20 66 6f 72 20 73 65 72 69 61 6c sible for serial
11380 69 7a 69 6e 67 20 61 63 63 65 73 73 20 74 6f 0a izing access to.
11390 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
113a0 6e 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 nections] and [p
113b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
113c0 74 73 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 20 ts]. But other
113d0 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 mutexes.** are e
113e0 6e 61 62 6c 65 64 20 73 6f 20 74 68 61 74 20 53 nabled so that S
113f0 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 73 61 QLite will be sa
11400 66 65 20 74 6f 20 75 73 65 20 69 6e 20 61 20 6d fe to use in a m
11410 75 6c 74 69 2d 74 68 72 65 61 64 65 64 0a 2a 2a ulti-threaded.**
11420 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 20 environment as
11430 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 74 long as no two t
11440 68 72 65 61 64 73 20 61 74 74 65 6d 70 74 20 74 hreads attempt t
11450 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a o use the same.*
11460 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
11470 65 63 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 73 ection] at the s
11480 61 6d 65 20 74 69 6d 65 2e 20 20 53 65 65 20 74 ame time. See t
11490 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f he [threading mo
114a0 64 65 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 de].** documenta
114b0 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f tion for additio
114c0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
114d0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
114e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 SQLITE_CONFIG_SE
114f0 52 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a RIALIZED</dt>.**
11500 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e <dd>There are n
11510 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 o arguments to t
11520 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 his option. Thi
11530 73 20 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 73 s option enables
11540 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 20 .** all mutexes
11550 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 72 65 including the re
11560 63 75 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 78 cursive.** mutex
11570 65 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 es on [database
11580 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 connection] and
11590 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
115a0 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a ent] objects..**
115b0 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 77 In this mode (w
115c0 68 69 63 68 20 69 73 20 74 68 65 20 64 65 66 61 hich is the defa
115d0 75 6c 74 20 77 68 65 6e 20 53 51 4c 69 74 65 20 ult when SQLite
115e0 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
115f0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 .** [SQLITE_THRE
11600 41 44 53 41 46 45 3d 31 5d 29 20 74 68 65 20 53 ADSAFE=1]) the S
11610 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 QLite library wi
11620 6c 6c 20 69 74 73 65 6c 66 20 73 65 72 69 61 6c ll itself serial
11630 69 7a 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f ize access.** to
11640 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
11650 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 ctions] and [pre
11660 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
11670 5d 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a ] so that the.**
11680 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 application is
11690 66 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 20 free to use the
116a0 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 same [database c
116b0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 68 onnection] or th
116c0 65 0a 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 61 e.** same [prepa
116d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 red statement] i
116e0 6e 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 n different thre
116f0 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 ads at the same
11700 74 69 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 65 time..** See the
11710 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
11720 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 ] documentation
11730 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
11740 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e nformation.</dd>
11750 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
11760 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c E_CONFIG_MALLOC<
11770 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
11780 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
11790 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
117a0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
117b0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 er to an.** inst
117c0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c ance of the [sql
117d0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
117e0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 ] structure. Th
117f0 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 e argument speci
11800 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 fies.** alternat
11810 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 ive low-level me
11820 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
11830 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 routines to be u
11840 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a sed in place of.
11850 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ** the memory al
11860 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 location routine
11870 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c s built into SQL
11880 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ite.</dd>.**.**
11890 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
118a0 47 5f 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e G_GETMALLOC</dt>
118b0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
118c0 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
118d0 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
118e0 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
118f0 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 o an.** instance
11900 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
11910 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 _mem_methods] st
11920 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73 ructure. The [s
11930 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
11940 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ds].** structure
11950 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 is filled with
11960 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 the currently de
11970 66 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c fined memory all
11980 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 ocation routines
11990 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e ..** This option
119a0 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
119b0 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 overload the def
119c0 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ault memory allo
119d0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e cation.** routin
119e0 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65 es with a wrappe
119f0 72 20 74 68 61 74 20 73 69 6d 75 6c 61 74 69 6f r that simulatio
11a00 6e 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ns memory alloca
11a10 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a tion failure or.
11a20 2a 2a 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79 ** tracks memory
11a30 20 75 73 61 67 65 2c 20 66 6f 72 20 65 78 61 6d usage, for exam
11a40 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ple.</dd>.**.**
11a50 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
11a60 47 5f 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 3e G_MEMSTATUS</dt>
11a70 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
11a80 69 6f 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c 65 ion takes single
11a90 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 argument of typ
11aa0 65 20 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 74 e int, interpret
11ab0 65 64 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c ed as a .** bool
11ac0 65 61 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 6c ean, which enabl
11ad0 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 es or disables t
11ae0 68 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 he collection of
11af0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
11b00 6f 6e 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 63 on .** statistic
11b10 73 2e 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64 s. When disabled
11b20 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 , the following
11b30 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
11b40 73 20 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e s become .** non
11b50 2d 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a -operational:.**
11b60 20 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 <ul>.** <li
11b70 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 > [sqlite3_memor
11b80 79 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c y_used()].** <
11b90 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d li> [sqlite3_mem
11ba0 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d ory_highwater()]
11bb0 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 .** <li> [sqli
11bc0 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 te3_soft_heap_li
11bd0 6d 69 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e mit()].** <li>
11be0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 [sqlite3_status
11bf0 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a ()].** </ul>.*
11c00 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 * </dd>.**.** <d
11c10 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
11c20 53 43 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 SCRATCH</dt>.**
11c30 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
11c40 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 specifies a stat
11c50 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 ic memory buffer
11c60 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e that SQLite can
11c70 20 75 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61 use for.** scra
11c80 74 63 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 tch memory. The
11c90 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67 re are three arg
11ca0 75 6d 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74 uments: A point
11cb0 65 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 er to the memory
11cc0 2c 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 , the.** size of
11cd0 20 65 61 63 68 20 73 63 72 61 74 63 68 20 62 75 each scratch bu
11ce0 66 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 74 ffer (sz), and t
11cf0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 66 he number of buf
11d00 66 65 72 73 20 28 4e 29 2e 20 20 54 68 65 20 73 fers (N). The s
11d10 7a 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 z.** argument mu
11d20 73 74 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 st be a multiple
11d30 20 6f 66 20 31 36 2e 20 54 68 65 20 73 7a 20 70 of 16. The sz p
11d40 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 arameter should
11d50 62 65 20 61 20 66 65 77 20 62 79 74 65 73 0a 2a be a few bytes.*
11d60 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 * larger than th
11d70 65 20 61 63 74 75 61 6c 20 73 63 72 61 74 63 68 e actual scratch
11d80 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 space required
11d90 64 75 65 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 due internal ove
11da0 72 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 rhead..** The fi
11db0 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 rst.** argument
11dc0 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 should point to
11dd0 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 an allocation of
11de0 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 at least sz*N b
11df0 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a ytes of memory..
11e00 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 ** SQLite will u
11e10 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 se no more than
11e20 6f 6e 65 20 73 63 72 61 74 63 68 20 62 75 66 66 one scratch buff
11e30 65 72 20 61 74 20 6f 6e 63 65 20 70 65 72 20 74 er at once per t
11e40 68 72 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 hread, so.** N s
11e50 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 hould be set to
11e60 74 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 78 the expected max
11e70 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 imum number of t
11e80 68 72 65 61 64 73 2e 20 20 54 68 65 20 73 7a 0a hreads. The sz.
11e90 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f ** parameter sho
11ea0 75 6c 64 20 62 65 20 36 20 74 69 6d 65 73 20 74 uld be 6 times t
11eb0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c he size of the l
11ec0 61 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20 argest database
11ed0 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 page size..** Sc
11ee0 72 61 74 63 68 20 62 75 66 66 65 72 73 20 61 72 ratch buffers ar
11ef0 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f e used as part o
11f00 66 20 74 68 65 20 62 74 72 65 65 20 62 61 6c 61 f the btree bala
11f10 6e 63 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 nce operation.
11f20 49 66 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 20 If.** The btree
11f30 62 61 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 61 balancer needs a
11f40 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 dditional memory
11f50 20 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 beyond what is
11f60 70 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73 provided by.** s
11f70 63 72 61 74 63 68 20 62 75 66 66 65 72 73 20 6f cratch buffers o
11f80 72 20 69 66 20 6e 6f 20 73 63 72 61 74 63 68 20 r if no scratch
11f90 62 75 66 66 65 72 20 73 70 61 63 65 20 69 73 20 buffer space is
11fa0 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 specified, then
11fb0 53 51 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 74 SQLite.** goes t
11fc0 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f o [sqlite3_mallo
11fd0 63 28 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74 c()] to obtain t
11fe0 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 he memory it nee
11ff0 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ds.</dd>.**.** <
12000 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
12010 5f 50 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0a _PAGECACHE</dt>.
12020 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 ** <dd>This opti
12030 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 on specifies a s
12040 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 tatic memory buf
12050 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 fer that SQLite
12060 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 can use for.** t
12070 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
12080 20 63 61 63 68 65 2e 20 20 54 68 65 72 65 20 61 cache. There a
12090 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e re three argumen
120a0 74 73 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f ts: A pointer to
120b0 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 the.** memory,
120c0 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68 the size of each
120d0 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a page buffer (sz
120e0 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 ), and the numbe
120f0 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a r of pages (N)..
12100 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65 ** The sz argume
12110 6e 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 nt must be a pow
12120 65 72 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 er of two betwee
12130 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 2e n 512 and 32768.
12140 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 The first.** a
12150 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 rgument should p
12160 6f 69 6e 74 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 oint to an alloc
12170 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 ation of at leas
12180 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 t sz*N bytes of
12190 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 memory..** SQLit
121a0 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 6d e will use the m
121b0 65 6d 6f 72 79 20 70 72 6f 76 69 64 65 64 20 62 emory provided b
121c0 79 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 y the first argu
121d0 6d 65 6e 74 20 74 6f 20 73 61 74 69 73 66 79 20 ment to satisfy
121e0 69 74 73 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 its.** memory ne
121f0 65 64 73 20 66 6f 72 20 74 68 65 20 66 69 72 73 eds for the firs
12200 74 20 4e 20 70 61 67 65 73 20 74 68 61 74 20 69 t N pages that i
12210 74 20 61 64 64 73 20 74 6f 20 63 61 63 68 65 2e t adds to cache.
12220 20 20 49 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a If additional.
12230 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 6d 65 ** page cache me
12240 6d 6f 72 79 20 69 73 20 6e 65 65 64 65 64 20 62 mory is needed b
12250 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72 eyond what is pr
12260 6f 76 69 64 65 64 20 62 79 20 74 68 69 73 20 6f ovided by this o
12270 70 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 ption, then.** S
12280 51 4c 69 74 65 20 67 6f 65 73 20 74 6f 20 5b 73 QLite goes to [s
12290 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
122a0 20 66 6f 72 20 74 68 65 20 61 64 64 69 74 69 6f for the additio
122b0 6e 61 6c 20 73 74 6f 72 61 67 65 20 73 70 61 63 nal storage spac
122c0 65 2e 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d e..** The implem
122d0 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 75 entation might u
122e0 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f se one or more o
122f0 66 20 74 68 65 20 4e 20 62 75 66 66 65 72 73 20 f the N buffers
12300 74 6f 20 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f to hold .** memo
12310 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20 69 6e ry accounting in
12320 66 6f 72 6d 61 74 69 6f 6e 2e 20 3c 2f 64 64 3e formation. </dd>
12330 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
12340 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64 E_CONFIG_HEAP</d
12350 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
12360 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 ption specifies
12370 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 a static memory
12380 62 75 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 buffer that SQLi
12390 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 te will use.** f
123a0 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 or all of its dy
123b0 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c namic memory all
123c0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 20 62 65 ocation needs be
123d0 79 6f 6e 64 20 74 68 6f 73 65 20 70 72 6f 76 69 yond those provi
123e0 64 65 64 0a 2a 2a 20 66 6f 72 20 62 79 20 5b 53 ded.** for by [S
123f0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 QLITE_CONFIG_SCR
12400 41 54 43 48 5d 20 61 6e 64 20 5b 53 51 4c 49 54 ATCH] and [SQLIT
12410 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
12420 48 45 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 HE]..** There ar
12430 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 e three argument
12440 73 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 s: A pointer to
12450 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 the memory, the
12460 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 number of.** byt
12470 65 73 20 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 es in the memory
12480 20 62 75 66 66 65 72 2c 20 61 6e 64 20 74 68 65 buffer, and the
12490 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 minimum allocat
124a0 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0a 2a 2a ion size. If.**
124b0 20 74 68 65 20 66 69 72 73 74 20 70 6f 69 6e 74 the first point
124c0 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 er (the memory p
124d0 6f 69 6e 74 65 72 29 20 69 73 20 4e 55 4c 4c 2c ointer) is NULL,
124e0 20 74 68 65 6e 20 53 51 4c 69 74 65 20 72 65 76 then SQLite rev
124f0 65 72 74 73 0a 2a 2a 20 74 6f 20 75 73 69 6e 67 erts.** to using
12500 20 69 74 73 20 64 65 66 61 75 6c 74 20 6d 65 6d its default mem
12510 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 28 74 ory allocator (t
12520 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 he system malloc
12530 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f () implementatio
12540 6e 29 2c 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20 61 n),.** undoing a
12550 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 ny prior invocat
12560 69 6f 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 43 ion of [SQLITE_C
12570 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 ONFIG_MALLOC].
12580 49 66 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 If the.** memory
12590 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 pointer is not
125a0 4e 55 4c 4c 20 61 6e 64 20 65 69 74 68 65 72 20 NULL and either
125b0 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d [SQLITE_ENABLE_M
125c0 45 4d 53 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53 EMSYS3] or.** [S
125d0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
125e0 53 59 53 35 5d 20 61 72 65 20 64 65 66 69 6e 65 SYS5] are define
125f0 64 2c 20 74 68 65 6e 20 74 68 65 20 61 6c 74 65 d, then the alte
12600 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 79 0a 2a rnative memory.*
12610 2a 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 65 * allocator is e
12620 6e 67 61 67 65 64 20 74 6f 20 68 61 6e 64 6c 65 ngaged to handle
12630 20 61 6c 6c 20 6f 66 20 53 51 4c 69 74 65 73 20 all of SQLites
12640 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
12650 6e 20 6e 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a n needs.</dd>.**
12660 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
12670 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e ONFIG_MUTEX</dt>
12680 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
12690 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
126a0 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
126b0 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
126c0 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 o an.** instance
126d0 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
126e0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 _mutex_methods]
126f0 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
12700 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 argument specifi
12710 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 es.** alternativ
12720 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 e low-level mute
12730 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 x routines to be
12740 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a used in place.*
12750 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f 75 74 * the mutex rout
12760 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 ines built into
12770 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a SQLite.</dd>.**.
12780 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
12790 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 NFIG_GETMUTEX</d
127a0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
127b0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
127c0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 ngle argument wh
127d0 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ich is a pointer
127e0 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e to an.** instan
127f0 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ce of the [sqlit
12800 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
12810 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 ] structure. Th
12820 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 e.** [sqlite3_mu
12830 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 tex_methods].**
12840 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c structure is fil
12850 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72 led with the cur
12860 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d rently defined m
12870 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a utex routines..*
12880 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 * This option ca
12890 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 n be used to ove
128a0 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c rload the defaul
128b0 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 69 t mutex allocati
128c0 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 on.** routines w
128d0 69 74 68 20 61 20 77 72 61 70 70 65 72 20 75 73 ith a wrapper us
128e0 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 65 ed to track mute
128f0 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 72 66 x usage for perf
12900 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 ormance.** profi
12910 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 2c ling or testing,
12920 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 for example.</d
12930 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
12940 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 ITE_CONFIG_LOOKA
12950 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SIDE</dt>.** <dd
12960 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b >This option tak
12970 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 es two arguments
12980 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20 that determine
12990 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d the default.** m
129a0 65 6d 6f 72 79 20 61 6c 6c 63 61 74 69 6f 6e 20 emory allcation
129b0 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d 69 lookaside optimi
129c0 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 72 zation. The fir
129d0 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 st argument is t
129e0 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 he.** size of ea
129f0 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 ch lookaside buf
12a00 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 fer slot and the
12a10 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e second is the n
12a20 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 umber of.** slot
12a30 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 s allocated to e
12a40 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e ach database con
12a50 6e 65 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a nection.</dd>.**
12a60 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 .** </dl>.*/.#de
12a70 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 fine SQLITE_CONF
12a80 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 IG_SINGLETHREAD
12a90 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 1 /* nil */.#d
12aa0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
12ab0 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 FIG_MULTITHREAD
12ac0 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 2 /* nil */.#
12ad0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
12ae0 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 20 NFIG_SERIALIZED
12af0 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 3 /* nil */.
12b00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
12b10 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 20 ONFIG_MALLOC
12b20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 4 /* sqlite
12b30 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 3_mem_methods* *
12b40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
12b50 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f _CONFIG_GETMALLO
12b60 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 C 5 /* sqli
12b70 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a te3_mem_methods*
12b80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
12b90 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 TE_CONFIG_SCRATC
12ba0 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 6f H 6 /* vo
12bb0 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 id*, int sz, int
12bc0 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 N */.#define SQ
12bd0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 LITE_CONFIG_PAGE
12be0 43 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a 20 CACHE 7 /*
12bf0 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 void*, int sz, i
12c00 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nt N */.#define
12c10 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 SQLITE_CONFIG_HE
12c20 41 50 20 20 20 20 20 20 20 20 20 20 38 20 20 2f AP 8 /
12c30 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79 * void*, int nBy
12c40 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a 23 te, int min */.#
12c50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
12c60 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20 NFIG_MEMSTATUS
12c70 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e 9 /* boolean
12c80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
12c90 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20 TE_CONFIG_MUTEX
12ca0 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71 10 /* sq
12cb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
12cc0 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ods* */.#define
12cd0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 SQLITE_CONFIG_GE
12ce0 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 2f TMUTEX 11 /
12cf0 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f * sqlite3_mutex_
12d00 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 methods* */.#def
12d10 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ine SQLITE_CONFI
12d20 47 5f 43 48 55 4e 4b 41 4c 4c 4f 43 20 20 20 31 G_CHUNKALLOC 1
12d30 32 20 20 2f 2a 20 69 6e 74 20 74 68 72 65 73 68 2 /* int thresh
12d40 6f 6c 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 old */.#define S
12d50 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f QLITE_CONFIG_LOO
12d60 4b 41 53 49 44 45 20 20 20 20 31 33 20 20 2f 2a KASIDE 13 /*
12d70 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 2f 2a 0a int int */../*.
12d80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e ** CAPI3REF: Con
12d90 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f figuration Optio
12da0 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c 53 32 30 ns {H10170} <S20
12db0 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 000>.** EXPERIME
12dc0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 NTAL.**.** These
12dd0 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 constants are t
12de0 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 he available int
12df0 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 eger configurati
12e00 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a on options that.
12e10 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 ** can be passed
12e20 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 as the second a
12e30 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b rgument to the [
12e40 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 sqlite3_db_confi
12e50 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a g()] interface..
12e60 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 67 **.** New config
12e70 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 uration options
12e80 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 may be added in
12e90 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 future releases
12ea0 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45 78 of SQLite..** Ex
12eb0 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72 61 isting configura
12ec0 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69 67 tion options mig
12ed0 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 ht be discontinu
12ee0 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e ed. Application
12ef0 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65 63 s.** should chec
12f00 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 k the return cod
12f10 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f e from [sqlite3_
12f20 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20 db_config()] to
12f30 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a make sure that.*
12f40 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 * the call worke
12f50 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 d. The [sqlite3
12f60 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e _db_config()] in
12f70 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 terface will ret
12f80 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 urn a.** non-zer
12f90 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 o [error code] i
12fa0 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 f a discontinued
12fb0 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 or unsupported
12fc0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
12fd0 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b tion.** is invok
12fe0 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a ed..**.** <dl>.*
12ff0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 43 * <dt>SQLITE_DBC
13000 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c ONFIG_LOOKASIDE<
13010 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
13020 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 68 option takes th
13030 72 65 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 ree additional a
13040 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64 65 rguments that de
13050 74 65 72 6d 69 6e 65 20 74 68 65 20 0a 2a 2a 20 termine the .**
13060 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 [lookaside memor
13070 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e y allocator] con
13080 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 20 74 figuration for t
13090 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
130a0 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 68 65 nection]..** The
130b0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
130c0 28 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d (the third param
130d0 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 eter to [sqlite3
130e0 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 73 _db_config()] is
130f0 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f a.** pointer to
13100 20 61 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 a memory buffer
13110 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f 6f 6b to use for look
13120 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 20 20 54 aside memory. T
13130 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 he first.** argu
13140 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e 55 4c 4c ment may be NULL
13150 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 in which case S
13160 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f 63 QLite will alloc
13170 61 74 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 ate the lookasid
13180 65 0a 2a 2a 20 62 75 66 66 65 72 20 69 74 73 65 e.** buffer itse
13190 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 lf using [sqlite
131a0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 3_malloc()]. Th
131b0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
131c0 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 t is the.** size
131d0 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 of each lookasi
131e0 64 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20 61 de buffer slot a
131f0 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 67 nd the third arg
13200 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d ument is the num
13210 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 2e ber of.** slots.
13220 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 The size of th
13230 65 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 20 e buffer in the
13240 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 6d first argument m
13250 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20 74 ust be greater t
13260 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 61 6c 20 han.** or equal
13270 74 6f 20 74 68 65 20 70 72 6f 64 75 63 74 20 6f to the product o
13280 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 f the second and
13290 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 73 third arguments
132a0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 .</dd>.**.** </d
132b0 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 l>.*/.#define SQ
132c0 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f LITE_DBCONFIG_LO
132d0 4f 4b 41 53 49 44 45 20 20 20 20 31 30 30 31 20 OKASIDE 1001
132e0 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 74 20 69 6e /* void* int in
132f0 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 t */.../*.** CAP
13300 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 I3REF: Enable Or
13310 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e 64 65 Disable Extende
13320 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b d Result Codes {
13330 48 31 32 32 30 30 7d 20 3c 53 31 30 37 30 30 3e H12200} <S10700>
13340 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
13350 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 e3_extended_resu
13360 6c 74 5f 63 6f 64 65 73 28 29 20 72 6f 75 74 69 lt_codes() routi
13370 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 ne enables or di
13380 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a 20 5b 65 sables the.** [e
13390 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
133a0 6f 64 65 73 5d 20 66 65 61 74 75 72 65 20 6f 66 odes] feature of
133b0 20 53 51 4c 69 74 65 2e 20 54 68 65 20 65 78 74 SQLite. The ext
133c0 65 6e 64 65 64 20 72 65 73 75 6c 74 0a 2a 2a 20 ended result.**
133d0 63 6f 64 65 73 20 61 72 65 20 64 69 73 61 62 6c codes are disabl
133e0 65 64 20 62 79 20 64 65 66 61 75 6c 74 20 66 6f ed by default fo
133f0 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d r historical com
13400 70 61 74 69 62 69 6c 69 74 79 20 63 6f 6e 73 69 patibility consi
13410 64 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a derations..**.**
13420 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
13430 2a 2a 20 7b 48 31 32 32 30 31 7d 20 45 61 63 68 ** {H12201} Each
13440 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 new [database c
13450 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 68 61 6c 6c onnection] shall
13460 20 68 61 76 65 20 74 68 65 0a 2a 2a 20 20 20 20 have the.**
13470 20 20 20 20 20 20 5b 65 78 74 65 6e 64 65 64 20 [extended
13480 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 result codes] fe
13490 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 20 62 ature disabled b
134a0 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a y default..**.**
134b0 20 7b 48 31 32 32 30 32 7d 20 54 68 65 20 5b 73 {H12202} The [s
134c0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f qlite3_extended_
134d0 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 44 2c 46 result_codes(D,F
134e0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 )] interface sha
134f0 6c 6c 20 65 6e 61 62 6c 65 0a 2a 2a 20 20 20 20 ll enable.**
13500 20 20 20 20 20 20 5b 65 78 74 65 6e 64 65 64 20 [extended
13510 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 6f result codes] fo
13520 72 20 74 68 65 20 20 5b 64 61 74 61 62 61 73 65 r the [database
13530 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a 2a connection] D.*
13540 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 74 68 * if th
13550 65 20 46 20 70 61 72 61 6d 65 74 65 72 20 69 73 e F parameter is
13560 20 74 72 75 65 2c 20 6f 72 20 64 69 73 61 62 6c true, or disabl
13570 65 20 74 68 65 6d 20 69 66 20 46 20 69 73 20 66 e them if F is f
13580 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alse..*/.SQLITE_
13590 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
135a0 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f extended_result_
135b0 63 6f 64 65 73 28 73 71 6c 69 74 65 33 2a 2c 20 codes(sqlite3*,
135c0 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a int onoff);../*.
135d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 61 73 ** CAPI3REF: Las
135e0 74 20 49 6e 73 65 72 74 20 52 6f 77 69 64 20 7b t Insert Rowid {
135f0 48 31 32 32 32 30 7d 20 3c 53 31 30 37 30 30 3e H12220} <S10700>
13600 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 .**.** Each entr
13610 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 74 y in an SQLite t
13620 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69 71 75 able has a uniqu
13630 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 0a e 64-bit signed.
13640 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 63 ** integer key c
13650 61 6c 6c 65 64 20 74 68 65 20 22 72 6f 77 69 64 alled the "rowid
13660 22 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73 20 ". The rowid is
13670 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c 65 always available
13680 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63 6c .** as an undecl
13690 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 ared column name
136a0 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 d ROWID, OID, or
136b0 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 _ROWID_ as long
136c0 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d as those.** nam
136d0 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 es are not also
136e0 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69 74 used by explicit
136f0 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 ly declared colu
13700 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 mns. If.** the t
13710 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75 6d able has a colum
13720 6e 20 6f 66 20 74 79 70 65 20 49 4e 54 45 47 45 n of type INTEGE
13730 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 74 68 R PRIMARY KEY th
13740 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a en that column.*
13750 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 * is another ali
13760 61 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 as for the rowid
13770 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
13780 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
13790 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6d 6f rowid of the mo
137a0 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 63 st recent.** suc
137b0 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d cessful [INSERT]
137c0 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
137d0 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 74 se from the [dat
137e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
137f0 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73 ].** in the firs
13800 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 t argument. If
13810 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 no successful [I
13820 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 20 NSERT]s.** have
13830 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f 6e ever occurred on
13840 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63 that database c
13850 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f 20 onnection, zero
13860 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
13870 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 54 ** If an [INSERT
13880 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 ] occurs within
13890 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e 20 a trigger, then
138a0 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 the rowid of the
138b0 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 72 6f 77 inserted.** row
138c0 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 is returned by
138d0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 20 this routine as
138e0 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 69 67 long as the trig
138f0 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a ger is running..
13900 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 68 65 20 ** But once the
13910 74 72 69 67 67 65 72 20 74 65 72 6d 69 6e 61 74 trigger terminat
13920 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65 es, the value re
13930 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 turned by this r
13940 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 65 72 74 outine.** revert
13950 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 76 61 s to the last va
13960 6c 75 65 20 69 6e 73 65 72 74 65 64 20 62 65 66 lue inserted bef
13970 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72 20 ore the trigger
13980 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 fired..**.** An
13990 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 20 66 61 [INSERT] that fa
139a0 69 6c 73 20 64 75 65 20 74 6f 20 61 20 63 6f 6e ils due to a con
139b0 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f straint violatio
139c0 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 73 75 n is not a.** su
139d0 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 ccessful [INSERT
139e0 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 63 ] and does not c
139f0 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 hange the value
13a00 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 returned by this
13a10 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 .** routine. Th
13a20 75 73 20 49 4e 53 45 52 54 20 4f 52 20 46 41 49 us INSERT OR FAI
13a30 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 47 4e L, INSERT OR IGN
13a40 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 20 52 ORE, INSERT OR R
13a50 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e 64 20 OLLBACK,.** and
13a60 49 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 54 20 INSERT OR ABORT
13a70 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 make no changes
13a80 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 to the return va
13a90 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 72 lue of this.** r
13aa0 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 69 outine when thei
13ab0 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c r insertion fail
13ac0 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 52 54 20 s. When INSERT
13ad0 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 65 6e OR REPLACE.** en
13ae0 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e 73 74 counters a const
13af0 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2c raint violation,
13b00 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 61 69 it does not fai
13b10 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 45 52 l. The.** INSER
13b20 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63 T continues to c
13b30 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72 20 ompletion after
13b40 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74 68 deleting rows th
13b50 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 68 65 at caused.** the
13b60 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f 62 constraint prob
13b70 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f 52 lem so INSERT OR
13b80 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61 6c REPLACE will al
13b90 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a 20 74 ways change.** t
13ba0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
13bb0 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 of this interfac
13bc0 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 e..**.** For the
13bd0 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 purposes of thi
13be0 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 20 5b 49 s routine, an [I
13bf0 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e 73 69 64 NSERT] is consid
13c00 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 20 73 75 ered to.** be su
13c10 63 63 65 73 73 66 75 6c 20 65 76 65 6e 20 69 66 ccessful even if
13c20 20 69 74 20 69 73 20 73 75 62 73 65 71 75 65 6e it is subsequen
13c30 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e tly rolled back.
13c40 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
13c50 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 32 31 S:.**.** {H12221
13c60 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c } The [sqlite3_l
13c70 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 ast_insert_rowid
13c80 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 ()] function sha
13c90 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 72 6f ll return the ro
13ca0 77 69 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 wid.**
13cb0 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
13cc0 6e 74 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 nt successful [I
13cd0 4e 53 45 52 54 5d 20 70 65 72 66 6f 72 6d 65 64 NSERT] performed
13ce0 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 on the same.**
13cf0 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 [databa
13d00 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 se connection] a
13d10 6e 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 nd within the sa
13d20 6d 65 20 6f 72 20 68 69 67 68 65 72 20 6c 65 76 me or higher lev
13d30 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 el.** t
13d40 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c 20 rigger context,
13d50 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65 or zero if there
13d60 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20 71 75 have been no qu
13d70 61 6c 69 66 79 69 6e 67 0a 2a 2a 20 20 20 20 20 alifying.**
13d80 20 20 20 20 20 5b 49 4e 53 45 52 54 5d 20 73 74 [INSERT] st
13d90 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 atements..**.**
13da0 7b 48 31 32 32 32 33 7d 20 54 68 65 20 5b 73 71 {H12223} The [sq
13db0 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
13dc0 74 5f 72 6f 77 69 64 28 29 5d 20 66 75 6e 63 74 t_rowid()] funct
13dd0 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72 6e ion shall return
13de0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
13df0 20 73 61 6d 65 20 76 61 6c 75 65 20 77 68 65 6e same value when
13e00 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
13e10 20 73 61 6d 65 20 74 72 69 67 67 65 72 20 63 6f same trigger co
13e20 6e 74 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 ntext.**
13e30 20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 65 immediately be
13e40 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 20 61 fore and after a
13e50 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a [ROLLBACK]..**.
13e60 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a ** ASSUMPTIONS:.
13e70 2a 2a 0a 2a 2a 20 7b 41 31 32 32 33 32 7d 20 49 **.** {A12232} I
13e80 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 f a separate thr
13e90 65 61 64 20 70 65 72 66 6f 72 6d 73 20 61 20 6e ead performs a n
13ea0 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e 20 74 ew [INSERT] on t
13eb0 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 he same.**
13ec0 20 20 20 20 64 61 74 61 62 61 73 65 20 63 6f 6e database con
13ed0 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 nection while th
13ee0 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f e [sqlite3_last_
13ef0 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 0a insert_rowid()].
13f00 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 ** func
13f10 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20 tion is running
13f20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65 73 and thus changes
13f30 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 the last insert
13f40 20 72 6f 77 69 64 2c 0a 2a 2a 20 20 20 20 20 20 rowid,.**
13f50 20 20 20 20 74 68 65 6e 20 74 68 65 20 76 61 6c then the val
13f60 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b ue returned by [
13f70 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 sqlite3_last_ins
13f80 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73 0a ert_rowid()] is.
13f90 2a 2a 20 20 20 20 20 20 20 20 20 20 75 6e 70 72 ** unpr
13fa0 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6d 69 edictable and mi
13fb0 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 65 69 ght not equal ei
13fc0 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f 72 20 ther the old or
13fd0 74 68 65 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 the new.**
13fe0 20 20 20 20 6c 61 73 74 20 69 6e 73 65 72 74 20 last insert
13ff0 72 6f 77 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rowid..*/.SQLITE
14000 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 _API sqlite3_int
14010 36 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 64 sqlite3_last_
14020 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c insert_rowid(sql
14030 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ite3*);../*.** C
14040 41 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 20 54 API3REF: Count T
14050 68 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 he Number Of Row
14060 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32 s Modified {H122
14070 34 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 40} <S10600>.**.
14080 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
14090 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
140a0 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 20 ber of database
140b0 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63 rows that were c
140c0 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 6e 73 hanged.** or ins
140d0 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 erted or deleted
140e0 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 by the most rec
140f0 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 ently completed
14100 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a SQL statement.**
14110 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 on the [databas
14120 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 e connection] sp
14130 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 66 ecified by the f
14140 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a irst parameter..
14150 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20 ** Only changes
14160 74 68 61 74 20 61 72 65 20 64 69 72 65 63 74 6c that are directl
14170 79 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 y specified by t
14180 68 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50 he [INSERT], [UP
14190 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 45 DATE],.** or [DE
141a0 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 20 LETE] statement
141b0 61 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41 75 are counted. Au
141c0 78 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73 20 xiliary changes
141d0 63 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 72 69 caused by.** tri
141e0 67 67 65 72 73 20 61 72 65 20 6e 6f 74 20 63 6f ggers are not co
141f0 75 6e 74 65 64 2e 20 55 73 65 20 74 68 65 20 5b unted. Use the [
14200 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 sqlite3_total_ch
14210 61 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f anges()] functio
14220 6e 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65 n.** to find the
14230 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
14240 20 63 68 61 6e 67 65 73 20 69 6e 63 6c 75 64 69 changes includi
14250 6e 67 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 ng changes cause
14260 64 20 62 79 20 74 72 69 67 67 65 72 73 2e 0a 2a d by triggers..*
14270 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61 6e *.** A "row chan
14280 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20 ge" is a change
14290 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 to a single row
142a0 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c of a single tabl
142b0 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 61 e.** caused by a
142c0 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 n INSERT, DELETE
142d0 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 , or UPDATE stat
142e0 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 61 ement. Rows tha
142f0 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 64 t.** are changed
14300 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74 73 as side effects
14310 20 6f 66 20 52 45 50 4c 41 43 45 20 63 6f 6e 73 of REPLACE cons
14320 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f traint resolutio
14330 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 n,.** rollback,
14340 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 ABORT processing
14350 2c 20 44 52 4f 50 20 54 41 42 4c 45 2c 20 6f 72 , DROP TABLE, or
14360 20 62 79 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a by any other.**
14370 20 6d 65 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e mechanisms do n
14380 6f 74 20 63 6f 75 6e 74 20 61 73 20 64 69 72 65 ot count as dire
14390 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a ct row changes..
143a0 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67 67 65 72 **.** A "trigger
143b0 20 63 6f 6e 74 65 78 74 22 20 69 73 20 61 20 73 context" is a s
143c0 63 6f 70 65 20 6f 66 20 65 78 65 63 75 74 69 6f cope of executio
143d0 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 6e n that begins an
143e0 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74 68 20 74 d.** ends with t
143f0 68 65 20 73 63 72 69 70 74 20 6f 66 20 61 20 74 he script of a t
14400 72 69 67 67 65 72 2e 20 20 4d 6f 73 74 20 53 51 rigger. Most SQ
14410 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 L statements are
14420 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6f 75 .** evaluated ou
14430 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74 72 69 tside of any tri
14440 67 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 74 gger. This is t
14450 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22 0a 2a he "top level".*
14460 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 * trigger contex
14470 74 2e 20 20 49 66 20 61 20 74 72 69 67 67 65 72 t. If a trigger
14480 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68 65 20 fires from the
14490 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20 top level, a.**
144a0 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74 new trigger cont
144b0 65 78 74 20 69 73 20 65 6e 74 65 72 65 64 20 66 ext is entered f
144c0 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 or the duration
144d0 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a 20 74 of that one.** t
144e0 72 69 67 67 65 72 2e 20 20 53 75 62 74 72 69 67 rigger. Subtrig
144f0 67 65 72 73 20 63 72 65 61 74 65 20 73 75 62 63 gers create subc
14500 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68 65 69 ontexts for thei
14510 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a r duration..**.*
14520 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 * Calling [sqlit
14530 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20 5b 73 e3_exec()] or [s
14540 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 72 qlite3_step()] r
14550 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65 73 0a ecursively does.
14560 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 61 20 ** not create a
14570 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74 new trigger cont
14580 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ext..**.** This
14590 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
145a0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 the number of d
145b0 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 irect row change
145c0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 s in the.** most
145d0 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 recent INSERT,
145e0 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 UPDATE, or DELET
145f0 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 E statement with
14600 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 in the same.** t
14610 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a rigger context..
14620 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68 65 6e **.** Thus, when
14630 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
14640 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 73 top level, this
14650 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
14660 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 s the.** number
14670 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 of changes in th
14680 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e e most recent IN
14690 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 SERT, UPDATE, or
146a0 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 DELETE.** that
146b0 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 74 also occurred at
146c0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20 the top level.
146d0 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 Within the body
146e0 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a 2a of a trigger,.*
146f0 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68 * the sqlite3_ch
14700 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61 63 anges() interfac
14710 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 e can be called
14720 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 to find the numb
14730 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 73 er of.** changes
14740 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 in the most rec
14750 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 ently completed
14760 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 INSERT, UPDATE,
14770 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61 or DELETE.** sta
14780 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 tement within th
14790 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73 61 e body of the sa
147a0 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20 48 me trigger..** H
147b0 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d 62 owever, the numb
147c0 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65 73 er returned does
147d0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61 not include cha
147e0 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20 62 nges.** caused b
147f0 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73 69 y subtriggers si
14800 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20 74 nce those have t
14810 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78 74 heir own context
14820 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 ..**.** SQLite i
14830 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 63 6f mplements the co
14840 6d 6d 61 6e 64 20 22 44 45 4c 45 54 45 20 46 52 mmand "DELETE FR
14850 4f 4d 20 74 61 62 6c 65 22 20 77 69 74 68 6f 75 OM table" withou
14860 74 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 t a WHERE clause
14870 0a 2a 2a 20 62 79 20 64 72 6f 70 70 69 6e 67 20 .** by dropping
14880 61 6e 64 20 72 65 63 72 65 61 74 69 6e 67 20 74 and recreating t
14890 68 65 20 74 61 62 6c 65 2e 20 20 44 6f 69 6e 67 he table. Doing
148a0 20 73 6f 20 69 73 20 6d 75 63 68 20 66 61 73 74 so is much fast
148b0 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 0a 2a 2a er than going.**
148c0 20 74 68 72 6f 75 67 68 20 61 6e 64 20 64 65 6c through and del
148d0 65 74 69 6e 67 20 69 6e 64 69 76 69 64 75 61 6c eting individual
148e0 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 74 elements from t
148f0 68 65 20 74 61 62 6c 65 2e 20 20 42 65 63 61 75 he table. Becau
14900 73 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f 70 se of this.** op
14910 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74 68 65 20 timization, the
14920 64 65 6c 65 74 69 6f 6e 73 20 69 6e 20 22 44 45 deletions in "DE
14930 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 22 LETE FROM table"
14940 20 61 72 65 20 6e 6f 74 20 72 6f 77 20 63 68 61 are not row cha
14950 6e 67 65 73 20 61 6e 64 0a 2a 2a 20 77 69 6c 6c nges and.** will
14960 20 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20 not be counted
14970 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 by the sqlite3_c
14980 68 61 6e 67 65 73 28 29 20 6f 72 20 5b 73 71 6c hanges() or [sql
14990 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 ite3_total_chang
149a0 65 73 28 29 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f es()].** functio
149b0 6e 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f ns, regardless o
149c0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
149d0 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 65 elements that we
149e0 72 65 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a re originally.**
149f0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 in the table.
14a00 54 6f 20 67 65 74 20 61 6e 20 61 63 63 75 72 61 To get an accura
14a10 74 65 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 te count of the
14a20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 64 number of rows d
14a30 65 6c 65 74 65 64 2c 20 75 73 65 0a 2a 2a 20 22 eleted, use.** "
14a40 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c DELETE FROM tabl
14a50 65 20 57 48 45 52 45 20 31 22 20 69 6e 73 74 65 e WHERE 1" inste
14a60 61 64 2e 20 20 4f 72 20 72 65 63 6f 6d 70 69 6c ad. Or recompil
14a70 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b e using the.** [
14a80 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e SQLITE_OMIT_TRUN
14a90 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f CATE_OPTIMIZATIO
14aa0 4e 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 N] compile-time
14ab0 6f 70 74 69 6f 6e 20 74 6f 20 64 69 73 61 62 6c option to disabl
14ac0 65 20 74 68 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a e the.** optimiz
14ad0 61 74 69 6f 6e 20 6f 6e 20 61 6c 6c 20 71 75 65 ation on all que
14ae0 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 ries..**.** INVA
14af0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
14b00 31 32 32 34 31 7d 20 54 68 65 20 5b 73 71 6c 69 12241} The [sqli
14b10 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 66 te3_changes()] f
14b20 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 unction shall re
14b30 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
14b40 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 of.** r
14b50 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 ow changes cause
14b60 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 d by the most re
14b70 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 cent INSERT, UPD
14b80 41 54 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ATE,.**
14b90 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 or DELETE state
14ba0 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 61 6d 65 ment on the same
14bb0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14bc0 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20 20 20 tion and.**
14bd0 20 20 20 20 20 77 69 74 68 69 6e 20 74 68 65 20 within the
14be0 73 61 6d 65 20 6f 72 20 68 69 67 68 65 72 20 74 same or higher t
14bf0 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c 20 rigger context,
14c00 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65 or zero if there
14c10 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 have.**
14c20 20 20 6e 6f 74 20 62 65 65 6e 20 61 6e 79 20 71 not been any q
14c30 75 61 6c 69 66 79 69 6e 67 20 72 6f 77 20 63 68 ualifying row ch
14c40 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 anges..**.** {H1
14c50 32 32 34 33 7d 20 53 74 61 74 65 6d 65 6e 74 73 2243} Statements
14c60 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 44 45 of the form "DE
14c70 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 6e LETE FROM tablen
14c80 61 6d 65 22 20 77 69 74 68 20 6e 6f 0a 2a 2a 20 ame" with no.**
14c90 20 20 20 20 20 20 20 20 20 57 48 45 52 45 20 63 WHERE c
14ca0 6c 61 75 73 65 20 73 68 61 6c 6c 20 63 61 75 73 lause shall caus
14cb0 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c e subsequent cal
14cc0 6c 73 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 ls to.**
14cd0 20 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 [sqlite3_chang
14ce0 65 73 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 20 es()] to return
14cf0 7a 65 72 6f 2c 20 72 65 67 61 72 64 6c 65 73 73 zero, regardless
14d00 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 of the.**
14d10 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f number of ro
14d20 77 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69 6e ws originally in
14d30 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a the table..**.*
14d40 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a * ASSUMPTIONS:.*
14d50 2a 0a 2a 2a 20 7b 41 31 32 32 35 32 7d 20 49 66 *.** {A12252} If
14d60 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65 a separate thre
14d70 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 ad makes changes
14d80 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 on the same dat
14d90 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
14da0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 69 .** whi
14db0 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e le [sqlite3_chan
14dc0 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e ges()] is runnin
14dd0 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 g then the value
14de0 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 20 20 20 returned.**
14df0 20 20 20 20 20 20 69 73 20 75 6e 70 72 65 64 69 is unpredi
14e00 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d ctable and not m
14e10 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 eaningful..*/.SQ
14e20 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
14e30 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c ite3_changes(sql
14e40 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ite3*);../*.** C
14e50 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e API3REF: Total N
14e60 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f umber Of Rows Mo
14e70 64 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20 dified {H12260}
14e80 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10600>.**.** T
14e90 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
14ea0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
14eb0 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63 of row changes c
14ec0 61 75 73 65 64 20 62 79 20 49 4e 53 45 52 54 2c aused by INSERT,
14ed0 0a 2a 2a 20 55 50 44 41 54 45 20 6f 72 20 44 45 .** UPDATE or DE
14ee0 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 LETE statements
14ef0 73 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62 since the [datab
14f00 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
14f10 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54 was opened..** T
14f20 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65 he count include
14f30 73 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72 s all changes fr
14f40 6f 6d 20 61 6c 6c 20 74 72 69 67 67 65 72 20 63 om all trigger c
14f50 6f 6e 74 65 78 74 73 2e 20 20 48 6f 77 65 76 65 ontexts. Howeve
14f60 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 r,.** the count
14f70 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 does not include
14f80 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f changes used to
14f90 20 69 6d 70 6c 65 6d 65 6e 74 20 52 45 50 4c 41 implement REPLA
14fa0 43 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a CE constraints,.
14fb0 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 ** do rollbacks
14fc0 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 or ABORT process
14fd0 69 6e 67 2c 20 6f 72 20 44 52 4f 50 20 74 61 62 ing, or DROP tab
14fe0 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a le processing..*
14ff0 2a 20 54 68 65 20 63 68 61 6e 67 65 73 20 61 72 * The changes ar
15000 65 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f e counted as soo
15010 6e 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 n as the stateme
15020 6e 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 nt that makes th
15030 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 em is.** complet
15040 65 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 61 ed (when the sta
15050 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 tement handle is
15060 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 passed to [sqli
15070 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a te3_reset()] or.
15080 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 ** [sqlite3_fina
15090 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 lize()])..**.**
150a0 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 SQLite implement
150b0 73 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22 44 s the command "D
150c0 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 ELETE FROM table
150d0 22 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52 " without a WHER
150e0 45 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 20 64 E clause.** by d
150f0 72 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 63 72 ropping and recr
15100 65 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65 eating the table
15110 2e 20 20 28 54 68 69 73 20 69 73 20 6d 75 63 68 . (This is much
15120 20 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f 69 faster than goi
15130 6e 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e ng.** through an
15140 64 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69 76 d deleting indiv
15150 69 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20 66 idual elements f
15160 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 29 20 rom the table.)
15170 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 Because of this
15180 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e .** optimization
15190 2c 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 73 20 , the deletions
151a0 69 6e 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 in "DELETE FROM
151b0 74 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20 72 table" are not r
151c0 6f 77 20 63 68 61 6e 67 65 73 20 61 6e 64 0a 2a ow changes and.*
151d0 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f * will not be co
151e0 75 6e 74 65 64 20 62 79 20 74 68 65 20 73 71 6c unted by the sql
151f0 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f ite3_changes() o
15200 72 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c r [sqlite3_total
15210 5f 63 68 61 6e 67 65 73 28 29 5d 0a 2a 2a 20 66 _changes()].** f
15220 75 6e 63 74 69 6f 6e 73 2c 20 72 65 67 61 72 64 unctions, regard
15230 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 less of the numb
15240 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 74 er of elements t
15250 68 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e 61 hat were origina
15260 6c 6c 79 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 lly.** in the ta
15270 62 6c 65 2e 20 20 54 6f 20 67 65 74 20 61 6e 20 ble. To get an
15280 61 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20 6f accurate count o
15290 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
152a0 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75 73 rows deleted, us
152b0 65 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52 4f e.** "DELETE FRO
152c0 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 31 22 M table WHERE 1"
152d0 20 69 6e 73 74 65 61 64 2e 20 20 20 4f 72 20 72 instead. Or r
152e0 65 63 6f 6d 70 69 6c 65 20 75 73 69 6e 67 20 74 ecompile using t
152f0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4d he.** [SQLITE_OM
15300 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 IT_TRUNCATE_OPTI
15310 4d 49 5a 41 54 49 4f 4e 5d 20 63 6f 6d 70 69 6c MIZATION] compil
15320 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 6f e-time option to
15330 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 disable the.**
15340 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 optimization on
15350 61 6c 6c 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a all queries..**.
15360 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 ** See also the
15370 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 [sqlite3_changes
15380 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a ()] interface..*
15390 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
153a0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 36 31 7d 20 .**.** {H12261}
153b0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 The [sqlite3_tot
153c0 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 72 65 al_changes()] re
153d0 74 75 72 6e 73 20 74 68 65 20 74 6f 74 61 6c 20 turns the total
153e0 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 20 number.**
153f0 20 20 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 of row change
15400 73 20 63 61 75 73 65 64 20 62 79 20 49 4e 53 45 s caused by INSE
15410 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 2f RT, UPDATE, and/
15420 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 or DELETE.**
15430 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 73 statements
15440 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 on the same [da
15450 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
15460 6e 5d 2c 20 69 6e 20 61 6e 79 0a 2a 2a 20 20 20 n], in any.**
15470 20 20 20 20 20 20 20 74 72 69 67 67 65 72 20 63 trigger c
15480 6f 6e 74 65 78 74 2c 20 73 69 6e 63 65 20 74 68 ontext, since th
15490 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
154a0 63 74 69 6f 6e 20 77 61 73 20 63 72 65 61 74 65 ction was create
154b0 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 36 33 d..**.** {H12263
154c0 7d 20 53 74 61 74 65 6d 65 6e 74 73 20 6f 66 20 } Statements of
154d0 74 68 65 20 66 6f 72 6d 20 22 44 45 4c 45 54 45 the form "DELETE
154e0 20 46 52 4f 4d 20 74 61 62 6c 65 6e 61 6d 65 22 FROM tablename"
154f0 20 77 69 74 68 20 6e 6f 0a 2a 2a 20 20 20 20 20 with no.**
15500 20 20 20 20 20 57 48 45 52 45 20 63 6c 61 75 73 WHERE claus
15510 65 20 73 68 61 6c 6c 20 6e 6f 74 20 63 68 61 6e e shall not chan
15520 67 65 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 ge the value ret
15530 75 72 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 urned.**
15540 20 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 6f by [sqlite3_to
15550 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 2e 0a tal_changes()]..
15560 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e **.** ASSUMPTION
15570 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 36 34 S:.**.** {A12264
15580 7d 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 } If a separate
15590 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 thread makes cha
155a0 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 nges on the same
155b0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
155c0 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 tion.**
155d0 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f while [sqlite3_
155e0 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d total_changes()]
155f0 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e is running then
15600 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 20 20 the value.**
15610 20 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 20 returned
15620 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 is unpredictable
15630 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 and not meaning
15640 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ful..*/.SQLITE_A
15650 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 PI int sqlite3_t
15660 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c otal_changes(sql
15670 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ite3*);../*.** C
15680 41 50 49 33 52 45 46 3a 20 49 6e 74 65 72 72 75 API3REF: Interru
15690 70 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e pt A Long-Runnin
156a0 67 20 51 75 65 72 79 20 7b 48 31 32 32 37 30 7d g Query {H12270}
156b0 20 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S30500>.**.**
156c0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 This function ca
156d0 75 73 65 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 uses any pending
156e0 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 database operat
156f0 69 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 ion to abort and
15700 0a 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 69 74 .** return at it
15710 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 s earliest oppor
15720 74 75 6e 69 74 79 2e 20 54 68 69 73 20 72 6f 75 tunity. This rou
15730 74 69 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c tine is typicall
15740 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 y.** called in r
15750 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 esponse to a use
15760 72 20 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73 r action such as
15770 20 70 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65 pressing "Cance
15780 6c 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 l".** or Ctrl-C
15790 77 68 65 72 65 20 74 68 65 20 75 73 65 72 20 77 where the user w
157a0 61 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 ants a long quer
157b0 79 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 y operation to h
157c0 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 alt.** immediate
157d0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 ly..**.** It is
157e0 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 safe to call thi
157f0 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 s routine from a
15800 20 74 68 72 65 61 64 20 64 69 66 66 65 72 65 6e thread differen
15810 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 t from the.** th
15820 72 65 61 64 20 74 68 61 74 20 69 73 20 63 75 72 read that is cur
15830 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 rently running t
15840 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 he database oper
15850 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a ation. But it.*
15860 2a 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f * is not safe to
15870 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 call this routi
15880 6e 65 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 ne with a [datab
15890 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
158a0 74 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65 that.** is close
158b0 64 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 65 d or might close
158c0 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f before sqlite3_
158d0 69 6e 74 65 72 72 75 70 74 28 29 20 72 65 74 75 interrupt() retu
158e0 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e rns..**.** If an
158f0 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 SQL operation i
15900 73 20 76 65 72 79 20 6e 65 61 72 6c 79 20 66 69 s very nearly fi
15910 6e 69 73 68 65 64 20 61 74 20 74 68 65 20 74 69 nished at the ti
15920 6d 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 me when.** sqlit
15930 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 69 e3_interrupt() i
15940 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69 s called, then i
15950 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 t might not have
15960 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a an opportunity.
15970 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 75 ** to be interru
15980 70 74 65 64 20 61 6e 64 20 6d 69 67 68 74 20 63 pted and might c
15990 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c ontinue to compl
159a0 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 etion..**.** An
159b0 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 SQL operation th
159c0 61 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 at is interrupte
159d0 64 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 d will return [S
159e0 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d QLITE_INTERRUPT]
159f0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 ..** If the inte
15a00 72 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65 72 rrupted SQL oper
15a10 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 ation is an INSE
15a20 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 RT, UPDATE, or D
15a30 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 ELETE.** that is
15a40 20 69 6e 73 69 64 65 20 61 6e 20 65 78 70 6c 69 inside an expli
15a50 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c cit transaction,
15a60 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65 then the entire
15a70 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
15a80 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 will be rolled b
15a90 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ack automaticall
15aa0 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 y..**.** A call
15ab0 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 to sqlite3_inter
15ac0 72 75 70 74 28 29 20 68 61 73 20 6e 6f 20 65 66 rupt() has no ef
15ad0 66 65 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 fect on SQL stat
15ae0 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 ements.** that a
15af0 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72 re started after
15b00 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
15b10 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a pt() returns..**
15b20 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
15b30 2a 2a 0a 2a 2a 20 7b 48 31 32 32 37 31 7d 20 54 **.** {H12271} T
15b40 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 he [sqlite3_inte
15b50 72 72 75 70 74 28 29 5d 20 69 6e 74 65 72 66 61 rrupt()] interfa
15b60 63 65 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c ce will force al
15b70 6c 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 20 20 20 l running.**
15b80 20 20 20 20 20 20 53 51 4c 20 73 74 61 74 65 6d SQL statem
15b90 65 6e 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 ents associated
15ba0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 64 61 with the same da
15bb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
15bc0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f n.** to
15bd0 20 68 61 6c 74 20 61 66 74 65 72 20 70 72 6f 63 halt after proc
15be0 65 73 73 69 6e 67 20 61 74 20 6d 6f 73 74 20 6f essing at most o
15bf0 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 6f ne additional ro
15c00 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a w of data..**.**
15c10 20 7b 48 31 32 32 37 32 7d 20 41 6e 79 20 53 51 {H12272} Any SQ
15c20 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 L statement that
15c30 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 20 is interrupted
15c40 62 79 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 by [sqlite3_inte
15c50 72 72 75 70 74 28 29 5d 0a 2a 2a 20 20 20 20 20 rrupt()].**
15c60 20 20 20 20 20 77 69 6c 6c 20 72 65 74 75 72 6e will return
15c70 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 [SQLITE_INTERRU
15c80 50 54 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d PT]..**.** ASSUM
15c90 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 PTIONS:.**.** {A
15ca0 31 32 32 37 39 7d 20 49 66 20 74 68 65 20 64 61 12279} If the da
15cb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
15cc0 6e 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b n closes while [
15cd0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
15ce0 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 t()].**
15cf0 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e is running then
15d00 20 62 61 64 20 74 68 69 6e 67 73 20 77 69 6c 6c bad things will
15d10 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 6e 2e 0a likely happen..
15d20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
15d30 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 id sqlite3_inter
15d40 72 75 70 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a rupt(sqlite3*);.
15d50 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
15d60 20 44 65 74 65 72 6d 69 6e 65 20 49 66 20 41 6e Determine If An
15d70 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 49 SQL Statement I
15d80 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 31 30 35 s Complete {H105
15d90 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a 10} <S70200>.**.
15da0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
15db0 73 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 s are useful for
15dc0 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e command-line in
15dd0 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 put to determine
15de0 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 if the.** curre
15df0 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78 ntly entered tex
15e00 74 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20 t seems to form
15e10 63 6f 6d 70 6c 65 74 65 20 61 20 53 51 4c 20 73 complete a SQL s
15e20 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69 tatement or.** i
15e30 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70 f additional inp
15e40 75 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 ut is needed bef
15e50 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20 ore sending the
15e60 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c text into.** SQL
15e70 69 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e ite for parsing.
15e80 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
15e90 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 return true if
15ea0 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 the input string
15eb0 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f 20 62 .** appears to b
15ec0 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c e a complete SQL
15ed0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 73 statement. A s
15ee0 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75 64 67 tatement is judg
15ef0 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 ed to be.** comp
15f00 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64 73 20 lete if it ends
15f10 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e with a semicolon
15f20 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20 6e 6f token and is no
15f30 74 20 61 20 66 72 61 67 6d 65 6e 74 20 6f 66 20 t a fragment of
15f40 61 0a 2a 2a 20 43 52 45 41 54 45 20 54 52 49 47 a.** CREATE TRIG
15f50 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 GER statement.
15f60 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20 Semicolons that
15f70 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 74 are embedded wit
15f80 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 hin.** string li
15f90 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64 terals or quoted
15fa0 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 identifier name
15fb0 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72 s or comments ar
15fc0 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e e not.** indepen
15fd0 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65 dent tokens (the
15fe0 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 y are part of th
15ff0 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68 e token in which
16000 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62 they are.** emb
16010 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 20 edded) and thus
16020 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 do not count as
16030 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d a statement term
16040 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 inator..**.** Th
16050 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 ese routines do
16060 6e 6f 74 20 70 61 72 73 65 20 74 68 65 20 53 51 not parse the SQ
16070 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 75 L statements thu
16080 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 s.** will not de
16090 74 65 63 74 20 73 79 6e 74 61 63 74 69 63 61 6c tect syntactical
160a0 6c 79 20 69 6e 63 6f 72 72 65 63 74 20 53 51 4c ly incorrect SQL
160b0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
160c0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 35 31 TS:.**.** {H1051
160d0 31 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 1} A successful
160e0 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b 73 evaluation of [s
160f0 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 qlite3_complete(
16100 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 )] or.**
16110 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c [sqlite3_compl
16120 65 74 65 31 36 28 29 5d 20 66 75 6e 63 74 69 6f ete16()] functio
16130 6e 73 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 ns shall.**
16140 20 20 20 20 20 72 65 74 75 72 6e 20 61 20 6e 75 return a nu
16150 6d 65 72 69 63 20 31 20 69 66 20 61 6e 64 20 6f meric 1 if and o
16160 6e 6c 79 20 69 66 20 74 68 65 20 6c 61 73 74 20 nly if the last
16170 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63 65 0a 2a non-whitespace.*
16180 2a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e * token
16190 20 69 6e 20 74 68 65 69 72 20 69 6e 70 75 74 20 in their input
161a0 69 73 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 is a semicolon t
161b0 68 61 74 20 69 73 20 6e 6f 74 20 69 6e 20 62 65 hat is not in be
161c0 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 tween.**
161d0 20 20 74 68 65 20 42 45 47 49 4e 20 61 6e 64 20 the BEGIN and
161e0 45 4e 44 20 6f 66 20 61 20 43 52 45 41 54 45 20 END of a CREATE
161f0 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e TRIGGER statemen
16200 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 35 31 32 t..**.** {H10512
16210 7d 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c } If a memory al
16220 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f location error o
16230 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 6e 20 ccurs during an
16240 69 6e 76 6f 63 61 74 69 6f 6e 0a 2a 2a 20 20 20 invocation.**
16250 20 20 20 20 20 20 20 6f 66 20 5b 73 71 6c 69 74 of [sqlit
16260 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6f e3_complete()] o
16270 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c r [sqlite3_compl
16280 65 74 65 31 36 28 29 5d 20 74 68 65 6e 20 74 68 ete16()] then th
16290 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 6f e.** ro
162a0 75 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 75 utine shall retu
162b0 72 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rn [SQLITE_NOMEM
162c0 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 ]..**.** ASSUMPT
162d0 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 30 IONS:.**.** {A10
162e0 35 31 32 7d 20 54 68 65 20 69 6e 70 75 74 20 74 512} The input t
162f0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c o [sqlite3_compl
16300 65 74 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61 ete()] must be a
16310 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
16320 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 55 54 46 .** UTF
16330 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a -8 string..**.**
16340 20 7b 41 31 30 35 31 33 7d 20 54 68 65 20 69 6e {A10513} The in
16350 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f put to [sqlite3_
16360 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 6d 75 complete16()] mu
16370 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 st be a zero-ter
16380 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 minated.**
16390 20 20 20 20 55 54 46 2d 31 36 20 73 74 72 69 6e UTF-16 strin
163a0 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 g in native byte
163b0 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 order..*/.SQLIT
163c0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
163d0 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74 3_complete(const
163e0 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51 4c char *sql);.SQL
163f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
16400 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63 te3_complete16(c
16410 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29 3b onst void *sql);
16420 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
16430 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 61 6c : Register A Cal
16440 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65 20 lback To Handle
16450 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 72 6f SQLITE_BUSY Erro
16460 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 34 30 rs {H12310} <S40
16470 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 400>.**.** This
16480 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 63 routine sets a c
16490 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
164a0 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 69 that might be i
164b0 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 0a nvoked whenever.
164c0 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 ** an attempt is
164d0 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 made to open a
164e0 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 74 database table t
164f0 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 hat another thre
16500 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73 73 ad.** or process
16510 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a has locked..**.
16520 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20 63 ** If the busy c
16530 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2c allback is NULL,
16540 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42 55 then [SQLITE_BU
16550 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 SY] or [SQLITE_I
16560 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a OERR_BLOCKED].**
16570 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d is returned imm
16580 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 65 6e ediately upon en
16590 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 20 6c countering the l
165a0 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 73 79 ock. If the busy
165b0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 callback.** is
165c0 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 not NULL, then t
165d0 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c he callback will
165e0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 be invoked with
165f0 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a two arguments..
16600 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
16610 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
16620 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f 70 handler is a cop
16630 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 y of the void* p
16640 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a 20 ointer which.**
16650 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67 is the third arg
16660 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
16670 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 2e _busy_handler().
16680 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 The second arg
16690 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 ument to.** the
166a0 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b handler callback
166b0 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
166c0 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 f times that the
166d0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68 61 busy handler ha
166e0 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b 65 s.** been invoke
166f0 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b 69 d for this locki
16700 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 74 68 ng event. If th
16710 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61 e.** busy callba
16720 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 ck returns 0, th
16730 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61 6c en no additional
16740 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 6d 61 attempts are ma
16750 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73 20 de to.** access
16760 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 the database and
16770 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f [SQLITE_BUSY] o
16780 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f r [SQLITE_IOERR_
16790 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75 BLOCKED] is retu
167a0 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 rned..** If the
167b0 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
167c0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
167d0 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 74 0a another attempt.
167e0 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 ** is made to op
167f0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
16800 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 for reading and
16810 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 61 74 the cycle repeat
16820 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 s..**.** The pre
16830 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 79 20 sence of a busy
16840 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f 74 handler does not
16850 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
16860 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b it will be invok
16870 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72 65 ed.** when there
16880 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 is lock content
16890 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 20 64 ion. If SQLite d
168a0 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 20 69 etermines that i
168b0 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 nvoking the busy
168c0 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75 6c .** handler coul
168d0 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 64 65 d result in a de
168e0 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c 20 adlock, it will
168f0 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65 74 go ahead and ret
16900 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 urn [SQLITE_BUSY
16910 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f ].** or [SQLITE_
16920 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 IOERR_BLOCKED] i
16930 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 nstead of invoki
16940 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 ng the busy hand
16950 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 ler..** Consider
16960 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 65 72 a scenario wher
16970 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 69 73 e one process is
16980 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 20 holding a read
16990 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74 20 lock that.** it
169a0 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 72 6f is trying to pro
169b0 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 72 76 mote to a reserv
169c0 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20 61 ed lock and.** a
169d0 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 second process
169e0 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 73 is holding a res
169f0 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 20 erved lock that
16a00 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a 20 it is trying.**
16a10 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 6e to promote to an
16a20 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e exclusive lock.
16a30 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f 63 The first proc
16a40 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 65 ess cannot proce
16a50 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 69 74 ed.** because it
16a60 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 is blocked by t
16a70 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 he second and th
16a80 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 e second process
16a90 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63 65 cannot.** proce
16aa0 65 64 20 62 65 63 61 75 73 65 20 69 74 20 69 73 ed because it is
16ab0 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 blocked by the
16ac0 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 68 20 first. If both
16ad0 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e 76 processes.** inv
16ae0 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e oke the busy han
16af0 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 20 77 dlers, neither w
16b00 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72 6f ill make any pro
16b10 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 6f 72 gress. Therefor
16b20 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65 74 e,.** SQLite ret
16b30 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55 53 urns [SQLITE_BUS
16b40 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 Y] for the first
16b50 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e 67 process, hoping
16b60 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 77 69 that this.** wi
16b70 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 66 69 ll induce the fi
16b80 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f 20 72 rst process to r
16b90 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 64 20 elease its read
16ba0 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a 2a lock and allow.*
16bb0 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f * the second pro
16bc0 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 64 2e cess to proceed.
16bd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 .**.** The defau
16be0 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b lt busy callback
16bf0 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 is NULL..**.**
16c00 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 The [SQLITE_BUSY
16c10 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e 76 65 ] error is conve
16c20 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 45 5f rted to [SQLITE_
16c30 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a IOERR_BLOCKED].*
16c40 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 * when SQLite is
16c50 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
16c60 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e 73 61 f a large transa
16c70 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c 20 ction where all
16c80 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20 77 the.** changes w
16c90 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 6f ill not fit into
16ca0 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 the in-memory c
16cb0 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 77 69 ache. SQLite wi
16cc0 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 6f ll.** already ho
16cd0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f ld a RESERVED lo
16ce0 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
16cf0 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74 20 se file, but it
16d00 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f 6d needs.** to prom
16d10 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 6f ote this lock to
16d20 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74 68 EXCLUSIVE so th
16d30 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c 20 at it can spill
16d40 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 20 69 cache.** pages i
16d50 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
16d60 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68 61 file without ha
16d70 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e 74 rm to concurrent
16d80 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 49 66 .** readers. If
16d90 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f it is unable to
16da0 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f 63 promote the loc
16db0 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 2d 6d k, then the in-m
16dc0 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20 77 emory.** cache w
16dd0 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e 20 61 ill be left in a
16de0 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 n inconsistent s
16df0 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 65 20 tate and so the
16e00 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 error.** code is
16e10 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20 74 promoted from t
16e20 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 62 65 he relatively be
16e30 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 nign [SQLITE_BUS
16e40 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f 72 Y] to.** the mor
16e50 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 54 45 e severe [SQLITE
16e60 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e _IOERR_BLOCKED].
16e70 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 6f 64 This error cod
16e80 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 e promotion.** f
16e90 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d 61 74 orces an automat
16ea0 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 ic rollback of t
16eb0 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 65 65 he changes. See
16ec0 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d the.** <a href=
16ed0 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f 70 "/cvstrac/wiki?p
16ee0 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f =CorruptionFollo
16ef0 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e 0a wingBusyError">.
16f00 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c ** CorruptionFol
16f10 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 3c lowingBusyError<
16f20 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66 6f /a> wiki page fo
16f30 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20 6f r a discussion o
16f40 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 73 f why.** this is
16f50 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a 2a important..**.*
16f60 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 * There can only
16f70 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 be a single bus
16f80 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 y handler define
16f90 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b 64 d for each.** [d
16fa0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
16fb0 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20 on]. Setting a
16fc0 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 new busy handler
16fd0 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 70 clears any.** p
16fe0 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68 61 reviously set ha
16ff0 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68 61 ndler. Note tha
17000 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 t calling [sqlit
17010 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 e3_busy_timeout(
17020 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f 20 )].** will also
17030 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65 set or clear the
17040 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a busy handler..*
17050 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 63 61 *.** The busy ca
17060 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e 6f llback should no
17070 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 69 6f t take any actio
17080 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 79 20 ns which modify
17090 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
170a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 connection that
170b0 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 invoked the busy
170c0 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20 73 handler. Any s
170d0 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 72 uch actions.** r
170e0 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e esult in undefin
170f0 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 20 ed behavior..**
17100 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
17110 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 31 7d 20 54 **.** {H12311} T
17120 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 he [sqlite3_busy
17130 5f 68 61 6e 64 6c 65 72 28 44 2c 43 2c 41 29 5d _handler(D,C,A)]
17140 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 function shall
17150 72 65 70 6c 61 63 65 0a 2a 2a 20 20 20 20 20 20 replace.**
17160 20 20 20 20 62 75 73 79 20 63 61 6c 6c 62 61 63 busy callbac
17170 6b 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61 k in the [databa
17180 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 se connection] D
17190 20 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20 20 with a new.**
171a0 20 20 20 20 20 20 20 20 61 20 6e 65 77 20 62 75 a new bu
171b0 73 79 20 68 61 6e 64 6c 65 72 20 43 20 61 6e 64 sy handler C and
171c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 61 74 application dat
171d0 61 20 70 6f 69 6e 74 65 72 20 41 2e 0a 2a 2a 0a a pointer A..**.
171e0 2a 2a 20 7b 48 31 32 33 31 32 7d 20 4e 65 77 6c ** {H12312} Newl
171f0 79 20 63 72 65 61 74 65 64 20 5b 64 61 74 61 62 y created [datab
17200 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d ase connections]
17210 20 73 68 61 6c 6c 20 68 61 76 65 20 61 20 62 75 shall have a bu
17220 73 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68 sy.** h
17230 61 6e 64 6c 65 72 20 6f 66 20 4e 55 4c 4c 2e 0a andler of NULL..
17240 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 34 7d 20 57 **.** {H12314} W
17250 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 hen two or more
17260 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
17270 74 69 6f 6e 73 5d 20 73 68 61 72 65 20 61 0a 2a tions] share a.*
17280 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
17290 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 te3_enable_share
172a0 64 5f 63 61 63 68 65 20 7c 20 63 6f 6d 6d 6f 6e d_cache | common
172b0 20 63 61 63 68 65 5d 2c 0a 2a 2a 20 20 20 20 20 cache],.**
172c0 20 20 20 20 20 74 68 65 20 62 75 73 79 20 68 61 the busy ha
172d0 6e 64 6c 65 72 20 66 6f 72 20 74 68 65 20 64 61 ndler for the da
172e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
172f0 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e n currently usin
17300 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 g.** th
17310 65 20 63 61 63 68 65 20 73 68 61 6c 6c 20 62 65 e cache shall be
17320 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 74 68 invoked when th
17330 65 20 63 61 63 68 65 20 65 6e 63 6f 75 6e 74 65 e cache encounte
17340 72 73 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a rs a lock..**.**
17350 20 7b 48 31 32 33 31 36 7d 20 49 66 20 61 20 62 {H12316} If a b
17360 75 73 79 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c usy handler call
17370 62 61 63 6b 20 72 65 74 75 72 6e 73 20 7a 65 72 back returns zer
17380 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 69 o, then the SQLi
17390 74 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 te interface.**
173a0 20 20 20 20 20 20 20 20 20 74 68 61 74 20 70 72 that pr
173b0 6f 76 6f 6b 65 64 20 74 68 65 20 6c 6f 63 6b 69 ovoked the locki
173c0 6e 67 20 65 76 65 6e 74 20 73 68 61 6c 6c 20 72 ng event shall r
173d0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 eturn [SQLITE_BU
173e0 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 SY]..**.** {H123
173f0 31 38 7d 20 53 51 4c 69 74 65 20 73 68 61 6c 6c 18} SQLite shall
17400 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73 invokes the bus
17410 79 20 68 61 6e 64 6c 65 72 20 77 69 74 68 20 74 y handler with t
17420 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 77 68 69 wo arguments whi
17430 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 ch.** a
17440 72 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 re a copy of the
17450 20 70 6f 69 6e 74 65 72 20 73 75 70 70 6c 69 65 pointer supplie
17460 64 20 62 79 20 74 68 65 20 33 72 64 20 70 61 72 d by the 3rd par
17470 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 ameter to.**
17480 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 [sqlite3_b
17490 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 20 61 usy_handler()] a
174a0 6e 64 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 nd a count of th
174b0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f e number of prio
174c0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e r.** in
174d0 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 vocations of the
174e0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f busy handler fo
174f0 72 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 r the same locki
17500 6e 67 20 65 76 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 ng event..**.**
17510 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a ASSUMPTIONS:.**.
17520 2a 2a 20 7b 41 31 32 33 31 39 7d 20 41 20 62 75 ** {A12319} A bu
17530 73 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 sy handler must
17540 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61 not close the da
17550 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
17560 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 n.** or
17570 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
17580 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f 6b ment] that invok
17590 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 ed the busy hand
175a0 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ler..*/.SQLITE_A
175b0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
175c0 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 usy_handler(sqli
175d0 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 te3*, int(*)(voi
175e0 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b d*,int), void*);
175f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
17600 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69 6d : Set A Busy Tim
17610 65 6f 75 74 20 7b 48 31 32 33 34 30 7d 20 3c 53 eout {H12340} <S
17620 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 40410>.**.** Thi
17630 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 s routine sets a
17640 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 [sqlite3_busy_h
17650 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68 61 andler | busy ha
17660 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 65 ndler] that slee
17670 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 63 ps.** for a spec
17680 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 ified amount of
17690 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61 62 6c time when a tabl
176a0 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54 68 e is locked. Th
176b0 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c e handler.** wil
176c0 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c 65 l sleep multiple
176d0 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 61 74 20 times until at
176e0 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c 69 least "ms" milli
176f0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 seconds of sleep
17700 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 63 63 75 ing.** have accu
17710 6d 75 6c 61 74 65 64 2e 20 7b 48 31 32 33 34 33 mulated. {H12343
17720 7d 20 41 66 74 65 72 20 22 6d 73 22 20 6d 69 6c } After "ms" mil
17730 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 liseconds of sle
17740 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 61 eping,.** the ha
17750 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 30 20 ndler returns 0
17760 77 68 69 63 68 20 63 61 75 73 65 73 20 5b 73 71 which causes [sq
17770 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 74 6f lite3_step()] to
17780 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 return.** [SQLI
17790 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c TE_BUSY] or [SQL
177a0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
177b0 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e D]..**.** Callin
177c0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 g this routine w
177d0 69 74 68 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 ith an argument
177e0 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 less than or equ
177f0 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75 al to zero.** tu
17800 72 6e 73 20 6f 66 66 20 61 6c 6c 20 62 75 73 79 rns off all busy
17810 20 68 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a handlers..**.**
17820 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 There can only
17830 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 be a single busy
17840 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 70 handler for a p
17850 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 articular.** [da
17860 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
17870 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69 76 65 6e n] any any given
17880 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e 6f moment. If ano
17890 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 ther busy handle
178a0 72 0a 2a 2a 20 77 61 73 20 64 65 66 69 6e 65 64 r.** was defined
178b0 20 20 28 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 (using [sqlite
178c0 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 3_busy_handler()
178d0 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c ]) prior to call
178e0 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 ing.** this rout
178f0 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65 72 20 ine, that other
17900 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20 busy handler is
17910 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 cleared..**.** I
17920 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
17930 20 7b 48 31 32 33 34 31 7d 20 54 68 65 20 5b 73 {H12341} The [s
17940 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 qlite3_busy_time
17950 6f 75 74 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 out()] function
17960 73 68 61 6c 6c 20 6f 76 65 72 72 69 64 65 20 61 shall override a
17970 6e 79 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20 20 ny prior.**
17980 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 75 [sqlite3_bu
17990 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 6f 72 sy_timeout()] or
179a0 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 [sqlite3_busy_h
179b0 61 6e 64 6c 65 72 28 29 5d 20 73 65 74 74 69 6e andler()] settin
179c0 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 6e g.** on
179d0 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 the same [datab
179e0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
179f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 34 33 7d 20 .**.** {H12343}
17a00 49 66 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d If the 2nd param
17a10 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 eter to [sqlite3
17a20 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d _busy_timeout()]
17a30 20 69 73 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a is less than.**
17a40 20 20 20 20 20 20 20 20 20 20 6f 72 20 65 71 75 or equ
17a50 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e al to zero, then
17a60 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
17a70 72 20 73 68 61 6c 6c 20 62 65 20 63 6c 65 61 72 r shall be clear
17a80 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20 ed so that.**
17a90 20 20 20 20 20 20 20 61 6c 6c 20 73 75 62 73 65 all subse
17aa0 71 75 65 6e 74 20 6c 6f 63 6b 69 6e 67 20 65 76 quent locking ev
17ab0 65 6e 74 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 ents immediately
17ac0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
17ad0 42 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 BUSY]..**.** {H1
17ae0 32 33 34 34 7d 20 49 66 20 74 68 65 20 32 6e 64 2344} If the 2nd
17af0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
17b00 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 qlite3_busy_time
17b10 6f 75 74 28 29 5d 20 69 73 20 61 20 70 6f 73 69 out()] is a posi
17b20 74 69 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 tive.**
17b30 20 6e 75 6d 62 65 72 20 4e 2c 20 74 68 65 6e 20 number N, then
17b40 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 73 a busy handler s
17b50 68 61 6c 6c 20 62 65 20 73 65 74 20 74 68 61 74 hall be set that
17b60 20 72 65 70 65 61 74 65 64 6c 79 20 63 61 6c 6c repeatedly call
17b70 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 s.** th
17b80 65 20 78 53 6c 65 65 70 28 29 20 6d 65 74 68 6f e xSleep() metho
17b90 64 20 69 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 d in the [sqlite
17ba0 33 5f 76 66 73 20 7c 20 56 46 53 20 69 6e 74 65 3_vfs | VFS inte
17bb0 72 66 61 63 65 5d 20 75 6e 74 69 6c 0a 2a 2a 20 rface] until.**
17bc0 20 20 20 20 20 20 20 20 20 65 69 74 68 65 72 20 either
17bd0 74 68 65 20 6c 6f 63 6b 20 63 6c 65 61 72 73 20 the lock clears
17be0 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 63 75 6d or until the cum
17bf0 75 6c 61 74 69 76 65 20 73 6c 65 65 70 20 74 69 ulative sleep ti
17c00 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 me.** r
17c10 65 70 6f 72 74 65 64 20 62 61 63 6b 20 62 79 20 eported back by
17c20 78 53 6c 65 65 70 28 29 20 65 78 63 65 65 64 73 xSleep() exceeds
17c30 20 4e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2e N milliseconds.
17c40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
17c50 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f nt sqlite3_busy_
17c60 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a timeout(sqlite3*
17c70 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a , int ms);../*.*
17c80 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 * CAPI3REF: Conv
17c90 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 enience Routines
17ca0 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 For Running Que
17cb0 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c 53 ries {H12370} <S
17cc0 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 10000>.**.** Def
17cd0 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65 inition: A <b>re
17ce0 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69 sult table</b> i
17cf0 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 s memory data st
17d00 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64 20 ructure created
17d10 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 by the.** [sqlit
17d20 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 e3_get_table()]
17d30 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 65 interface. A re
17d40 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f 72 sult table recor
17d50 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 ds the.** comple
17d60 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 te query results
17d70 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72 from one or mor
17d80 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a e queries..**.**
17d90 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 65 The table conce
17da0 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e 75 ptually has a nu
17db0 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 mber of rows and
17dc0 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a columns. But.*
17dd0 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 20 * these numbers
17de0 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 are not part of
17df0 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 the result table
17e00 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 0a itself. These.
17e10 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f ** numbers are o
17e20 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 65 btained separate
17e30 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 68 ly. Let N be th
17e40 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 e number of rows
17e50 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 65 .** and M be the
17e60 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
17e70 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 ns..**.** A resu
17e80 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 61 lt table is an a
17e90 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 rray of pointers
17ea0 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 to zero-termina
17eb0 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 ted UTF-8 string
17ec0 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 s..** There are
17ed0 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 (N+1)*M elements
17ee0 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20 in the array.
17ef0 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 6e The first M poin
17f00 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f ters point.** to
17f10 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
17f20 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 63 strings that c
17f30 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 ontain the names
17f40 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e of the columns.
17f50 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e .** The remainin
17f60 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f g entries all po
17f70 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 73 int to query res
17f80 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 ults. NULL valu
17f90 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 es result.** in
17fa0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20 NULL pointers.
17fb0 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73 All other values
17fc0 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 54 are in their UT
17fd0 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 F-8 zero-termina
17fe0 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65 ted.** string re
17ff0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 20 presentation as
18000 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c returned by [sql
18010 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
18020 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 ()]..**.** A res
18030 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20 ult table might
18040 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f consist of one o
18050 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c r more memory al
18060 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 locations..** It
18070 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 is not safe to
18080 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 61 pass a result ta
18090 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 ble directly to
180a0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
180b0 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 ..** A result ta
180c0 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65 ble should be de
180d0 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 allocated using
180e0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 [sqlite3_free_ta
180f0 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 ble()]..**.** As
18100 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 an example of t
18110 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 he result table
18120 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 format, suppose
18130 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a a query result.*
18140 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a * is as follows:
18150 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f .**.** <blockquo
18160 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 te><pre>.**
18170 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c Name |
18180 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d Age.** -
18190 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
181a0 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 ------.**
181b0 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34 Alice | 4
181c0 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20 3.** Bob
181d0 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 | 28.**
181e0 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 20 Cindy
181f0 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 | 21.** </pre
18200 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a ></blockquote>.*
18210 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 *.** There are t
18220 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 wo column (M==2)
18230 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20 and three rows
18240 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68 (N==3). Thus th
18250 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c e.** result tabl
18260 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e e has 8 entries.
18270 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 65 Suppose the re
18280 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74 sult table is st
18290 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 ored.** in an ar
182a0 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 ray names azResu
182b0 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75 lt. Then azResu
182c0 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f lt holds this co
182d0 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c ntent:.**.** <bl
182e0 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
182f0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c * azResul
18300 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 t[0] = "Name
18310 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
18320 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 esult[1] = "
18330 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 Age";.**
18340 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 azResult[2]
18350 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 = "Alice";.**
18360 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 azResult	
18370 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 1;3] = "43";.**
18380 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 azResult&
18390 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a #91;4] = "Bob";.
183a0 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
183b0 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 lt[5] = "28"
183c0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 ;.** azRe
183d0 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 sult[6] = "C
183e0 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 indy";.**
183f0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d azResult[7]
18400 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 = "21";.** </pr
18410 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
18420 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
18430 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75 3_get_table() fu
18440 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 nction evaluates
18450 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 one or more.**
18460 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 semicolon-separa
18470 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ted SQL statemen
18480 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 ts in the zero-t
18490 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a erminated UTF-8.
184a0 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73 ** string of its
184b0 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 2nd parameter.
184c0 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 65 It returns a re
184d0 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 sult table to th
184e0 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 e.** pointer giv
184f0 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 en in its 3rd pa
18500 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 rameter..**.** A
18510 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 fter the calling
18520 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 function has fi
18530 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 nished using the
18540 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 result, it shou
18550 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70 ld.** pass the p
18560 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 ointer to the re
18570 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71 sult table to sq
18580 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 lite3_free_table
18590 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a () in order to.*
185a0 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65 * release the me
185b0 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d 61 mory that was ma
185c0 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 65 lloced. Because
185d0 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 0a of the way the.
185e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ** [sqlite3_mall
185f0 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 69 oc()] happens wi
18600 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 thin sqlite3_get
18610 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 61 _table(), the ca
18620 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f lling.** functio
18630 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 74 n must not try t
18640 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f o call [sqlite3_
18650 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79 free()] directly
18660 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69 . Only.** [sqli
18670 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 te3_free_table()
18680 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 6c ] is able to rel
18690 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 ease the memory
186a0 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66 properly and saf
186b0 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ely..**.** The s
186c0 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 qlite3_get_table
186d0 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 () interface is
186e0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 implemented as a
186f0 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a wrapper around.
18700 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 ** [sqlite3_exec
18710 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 65 ()]. The sqlite
18720 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 6f 3_get_table() ro
18730 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68 utine does not h
18740 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f ave access.** to
18750 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 any internal da
18760 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 ta structures of
18770 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 65 SQLite. It use
18780 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 s only the publi
18790 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64 c.** interface d
187a0 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73 efined here. As
187b0 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 a consequence,
187c0 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 errors that occu
187d0 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70 r in the.** wrap
187e0 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64 per layer outsid
187f0 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61 e of the interna
18800 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 l [sqlite3_exec(
18810 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a )] call are not.
18820 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20 ** reflected in
18830 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
18840 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 to [sqlite3_err
18850 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 code()] or [sqli
18860 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a te3_errmsg()]..*
18870 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
18880 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 31 7d 20 .**.** {H12371}
18890 49 66 20 61 20 5b 73 71 6c 69 74 65 33 5f 67 65 If a [sqlite3_ge
188a0 74 5f 74 61 62 6c 65 28 29 5d 20 66 61 69 6c 73 t_table()] fails
188b0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
188c0 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 tion, then.**
188d0 20 20 20 20 20 20 20 69 74 20 73 68 61 6c 6c 20 it shall
188e0 66 72 65 65 20 74 68 65 20 72 65 73 75 6c 74 20 free the result
188f0 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 table under cons
18900 74 72 75 63 74 69 6f 6e 2c 20 61 62 6f 72 74 20 truction, abort
18910 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
18920 71 75 65 72 79 20 69 6e 20 70 72 6f 63 65 73 73 query in process
18930 2c 20 73 6b 69 70 20 61 6e 79 20 73 75 62 73 65 , skip any subse
18940 71 75 65 6e 74 20 71 75 65 72 69 65 73 2c 20 73 quent queries, s
18950 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 et the.**
18960 20 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 6f 75 *pazResult ou
18970 74 70 75 74 20 70 6f 69 6e 74 65 72 20 74 6f 20 tput pointer to
18980 4e 55 4c 4c 20 61 6e 64 20 72 65 74 75 72 6e 20 NULL and return
18990 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a [SQLITE_NOMEM]..
189a0 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 33 7d 20 49 **.** {H12373} I
189b0 66 20 74 68 65 20 70 6e 43 6f 6c 75 6d 6e 20 70 f the pnColumn p
189c0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
189d0 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 ite3_get_table()
189e0 5d 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 0a 2a 2a ] is not NULL.**
189f0 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 61 then a
18a00 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f successful invo
18a10 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 cation of [sqlit
18a20 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 e3_get_table()]
18a30 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 shall.**
18a40 20 20 77 72 69 74 65 20 74 68 65 20 6e 75 6d 62 write the numb
18a50 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
18a60 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
18a70 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 result set of t
18a80 68 65 20 71 75 65 72 79 20 69 6e 74 6f 20 2a 70 he query into *p
18a90 6e 43 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 7b nColumn..**.** {
18aa0 48 31 32 33 37 34 7d 20 49 66 20 74 68 65 20 70 H12374} If the p
18ab0 6e 52 6f 77 20 70 61 72 61 6d 65 74 65 72 20 74 nRow parameter t
18ac0 6f 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 o [sqlite3_get_t
18ad0 61 62 6c 65 28 29 5d 20 69 73 20 6e 6f 74 20 4e able()] is not N
18ae0 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ULL.**
18af0 74 68 65 6e 20 61 20 73 75 63 63 65 73 73 66 75 then a successfu
18b00 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 l invocation of
18b10 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 [sqlite3_get_tab
18b20 6c 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20 20 le()] shall.**
18b30 20 20 20 20 20 20 20 20 77 72 69 74 65 73 20 74 writes t
18b40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
18b50 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 s in the.**
18b60 20 20 20 20 20 72 65 73 75 6c 74 20 73 65 74 20 result set
18b70 6f 66 20 74 68 65 20 71 75 65 72 79 20 69 6e 74 of the query int
18b80 6f 20 2a 70 6e 52 6f 77 2e 0a 2a 2a 0a 2a 2a 20 o *pnRow..**.**
18b90 7b 48 31 32 33 37 36 7d 20 41 20 73 75 63 63 65 {H12376} A succe
18ba0 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e ssful invocation
18bb0 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 of [sqlite3_get
18bc0 5f 74 61 62 6c 65 28 29 5d 20 74 68 61 74 20 63 _table()] that c
18bd0 6f 6d 70 75 74 65 73 0a 2a 2a 20 20 20 20 20 20 omputes.**
18be0 20 20 20 20 4e 20 72 6f 77 73 20 6f 66 20 72 65 N rows of re
18bf0 73 75 6c 74 20 77 69 74 68 20 43 20 63 6f 6c 75 sult with C colu
18c00 6d 6e 73 20 70 65 72 20 72 6f 77 20 73 68 61 6c mns per row shal
18c10 6c 20 6d 61 6b 65 20 2a 70 61 7a 52 65 73 75 6c l make *pazResul
18c20 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 6f t.** po
18c30 69 6e 74 20 74 6f 20 61 6e 20 61 72 72 61 79 20 int to an array
18c40 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 28 of pointers to (
18c50 4e 2b 31 29 2a 43 20 73 74 72 69 6e 67 73 20 77 N+1)*C strings w
18c60 68 65 72 65 20 74 68 65 20 66 69 72 73 74 0a 2a here the first.*
18c70 2a 20 20 20 20 20 20 20 20 20 20 43 20 73 74 72 * C str
18c80 69 6e 67 73 20 61 72 65 20 63 6f 6c 75 6d 6e 20 ings are column
18c90 6e 61 6d 65 73 20 61 73 20 6f 62 74 61 69 6e 65 names as obtaine
18ca0 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 d from.**
18cb0 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 [sqlite3_colu
18cc0 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61 6e 64 20 74 mn_name()] and t
18cd0 68 65 20 72 65 73 74 20 61 72 65 20 63 6f 6c 75 he rest are colu
18ce0 6d 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 73 mn result values
18cf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 74 .** obt
18d00 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 ained from [sqli
18d10 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 te3_column_text(
18d20 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 )]..**.** {H1237
18d30 39 7d 20 54 68 65 20 76 61 6c 75 65 73 20 69 6e 9} The values in
18d40 20 74 68 65 20 70 61 7a 52 65 73 75 6c 74 20 61 the pazResult a
18d50 72 72 61 79 20 72 65 74 75 72 6e 65 64 20 62 79 rray returned by
18d60 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 [sqlite3_get_ta
18d70 62 6c 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ble()].**
18d80 20 20 20 73 68 61 6c 6c 20 72 65 6d 61 69 6e 20 shall remain
18d90 76 61 6c 69 64 20 75 6e 74 69 6c 20 63 6c 65 61 valid until clea
18da0 72 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f red by [sqlite3_
18db0 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a 2a free_table()]..*
18dc0 2a 0a 2a 2a 20 7b 48 31 32 33 38 32 7d 20 57 68 *.** {H12382} Wh
18dd0 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 en an error occu
18de0 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75 61 rs during evalua
18df0 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 tion of [sqlite3
18e00 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 0a 2a 2a _get_table()].**
18e10 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75 the fu
18e20 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 73 65 74 nction shall set
18e30 20 2a 70 61 7a 52 65 73 75 6c 74 20 74 6f 20 4e *pazResult to N
18e40 55 4c 4c 2c 20 77 72 69 74 65 20 61 6e 20 65 72 ULL, write an er
18e50 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 20 ror message.**
18e60 20 20 20 20 20 20 20 20 69 6e 74 6f 20 6d 65 6d into mem
18e70 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
18e80 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f m [sqlite3_mallo
18e90 63 28 29 5d 2c 20 6d 61 6b 65 0a 2a 2a 20 20 20 c()], make.**
18ea0 20 20 20 20 20 20 20 2a 2a 70 7a 45 72 72 6d 73 **pzErrms
18eb0 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 g point to that
18ec0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61 error message, a
18ed0 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 20 nd return a.**
18ee0 20 20 20 20 20 20 20 20 61 70 70 72 6f 70 72 69 appropri
18ef0 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d ate [error code]
18f00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
18f10 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f int sqlite3_get_
18f20 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 table(. sqlite3
18f30 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f *db, /
18f40 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 * An open databa
18f50 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 se */. const ch
18f60 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 2f 2a ar *zSql, /*
18f70 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75 SQL to be evalu
18f80 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a ated */. char *
18f90 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 20 **pazResult,
18fa0 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 74 68 /* Results of th
18fb0 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 e query */. int
18fc0 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 20 *pnRow,
18fd0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
18fe0 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69 74 result rows writ
18ff0 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ten here */. in
19000 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 t *pnColumn,
19010 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
19020 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 result columns
19030 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a written here */.
19040 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d 73 char **pzErrms
19050 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 g /* Error
19060 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 72 msg written her
19070 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 e */.);.SQLITE_A
19080 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
19090 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72 20 free_table(char
190a0 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a **result);../*.*
190b0 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72 6d * CAPI3REF: Form
190c0 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72 69 atted String Pri
190d0 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20 nting Functions
190e0 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 30 30 {H17400} <S70000
190f0 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 ><S20000>.**.**
19100 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
19110 72 65 20 77 6f 72 6b 61 6c 69 6b 65 73 20 6f 66 re workalikes of
19120 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22 20 the "printf()"
19130 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 69 family of functi
19140 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ons.** from the
19150 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 standard C libra
19160 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ry..**.** The sq
19170 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 lite3_mprintf()
19180 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 and sqlite3_vmpr
19190 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73 20 intf() routines
191a0 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20 72 write their.** r
191b0 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d 6f esults into memo
191c0 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
191d0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
191e0 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72 69 ()]..** The stri
191f0 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 ngs returned by
19200 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69 6e these two routin
19210 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 es should be.**
19220 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71 6c released by [sql
19230 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20 42 ite3_free()]. B
19240 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65 74 oth routines ret
19250 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f urn a.** NULL po
19260 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74 65 inter if [sqlite
19270 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20 75 3_malloc()] is u
19280 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 nable to allocat
19290 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d 6f e enough.** memo
192a0 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ry to hold the r
192b0 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e esulting string.
192c0 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74 65 .**.** In sqlite
192d0 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f 75 3_snprintf() rou
192e0 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 tine is similar
192f0 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22 20 to "snprintf()"
19300 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61 6e from.** the stan
19310 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 20 dard C library.
19320 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77 The result is w
19330 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a ritten into the.
19340 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c 69 ** buffer suppli
19350 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
19360 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73 65 parameter whose
19370 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20 62 size is given b
19380 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 y.** the first p
19390 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 65 20 74 arameter. Note t
193a0 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f 66 hat the order of
193b0 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74 77 the.** first tw
193c0 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69 73 20 o parameters is
193d0 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20 73 6e reversed from sn
193e0 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69 73 20 printf(). This
193f0 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f 72 69 is an.** histori
19400 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74 68 61 cal accident tha
19410 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69 78 65 t cannot be fixe
19420 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69 d without breaki
19430 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 ng.** backwards
19440 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20 20 compatibility.
19450 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 73 Note also that s
19460 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
19470 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 70 ).** returns a p
19480 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62 75 ointer to its bu
19490 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 ffer instead of
194a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
194b0 20 63 68 61 72 61 63 74 65 72 73 20 61 63 74 75 characters actu
194c0 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 ally written int
194d0 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20 20 57 o the buffer. W
194e0 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a 2a 20 e admit that.**
194f0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
19500 61 72 61 63 74 65 72 73 20 77 72 69 74 74 65 6e aracters written
19510 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f 72 65 would be a more
19520 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e 0a 2a useful return.*
19530 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65 20 63 * value but we c
19540 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 annot change the
19550 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
19560 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 of sqlite3_snpri
19570 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77 69 74 ntf().** now wit
19580 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63 6f hout breaking co
19590 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a mpatibility..**.
195a0 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20 74 68 ** As long as th
195b0 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 69 73 e buffer size is
195c0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 greater than ze
195d0 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 ro, sqlite3_snpr
195e0 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72 61 6e intf().** guaran
195f0 74 65 65 73 20 74 68 61 74 20 74 68 65 20 62 75 tees that the bu
19600 66 66 65 72 20 69 73 20 61 6c 77 61 79 73 20 7a ffer is always z
19610 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 ero-terminated.
19620 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 The first.** pa
19630 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73 20 74 rameter "n" is t
19640 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20 6f 66 he total size of
19650 20 74 68 65 20 62 75 66 66 65 72 2c 20 69 6e 63 the buffer, inc
19660 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66 6f 72 luding space for
19670 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74 65 72 .** the zero ter
19680 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68 65 minator. So the
19690 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e 67 20 longest string
196a0 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d 70 that can be comp
196b0 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74 74 65 letely.** writte
196c0 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63 68 n will be n-1 ch
196d0 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 aracters..**.**
196e0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
196f0 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f 6d ll implement som
19700 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f 72 e additional for
19710 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69 6f matting.** optio
19720 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65 66 ns that are usef
19730 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63 74 ul for construct
19740 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ing SQL statemen
19750 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ts..** All of th
19760 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28 29 e usual printf()
19770 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 formatting opti
19780 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20 61 ons apply. In a
19790 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a 2a ddition, there.*
197a0 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20 22 * is are "%q", "
197b0 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f 70 %Q", and "%z" op
197c0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tions..**.** The
197d0 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 %q option works
197e0 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 61 74 like %s in that
197f0 20 69 74 20 73 75 62 73 74 69 74 75 74 65 73 20 it substitutes
19800 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 a null-terminate
19810 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f 6d d.** string from
19820 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c 69 the argument li
19830 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73 6f st. But %q also
19840 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20 27 doubles every '
19850 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a 2a \'' character..*
19860 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65 64 * %q is designed
19870 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 for use inside
19880 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c a string literal
19890 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20 65 . By doubling e
198a0 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 68 61 72 ach '\''.** char
198b0 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65 73 acter it escapes
198c0 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72 20 that character
198d0 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74 6f and allows it to
198e0 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 be inserted int
198f0 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 2e o.** the string.
19900 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 .**.** For examp
19910 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20 73 le, assume the s
19920 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65 20 7a tring variable z
19930 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 74 65 Text contains te
19940 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a xt as follows:.*
19950 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
19960 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 ><pre>.** char
19970 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20 61 *zText = "It's a
19980 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a 2a happy day!";.**
19990 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 </pre></blockqu
199a0 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 ote>.**.** One c
199b0 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78 74 an use this text
199c0 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 in an SQL state
199d0 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a ment as follows:
199e0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f .**.** <blockquo
199f0 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 te><pre>.** cha
19a00 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 r *zSQL = sqlite
19a10 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 52 3_mprintf("INSER
19a20 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c T INTO table VAL
19a30 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65 78 UES('%q')", zTex
19a40 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f t);.** sqlite3_
19a50 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 exec(db, zSQL, 0
19a60 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c , 0, 0);.** sql
19a70 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b ite3_free(zSQL);
19a80 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 .** </pre></bloc
19a90 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 kquote>.**.** Be
19aa0 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f 72 cause the %q for
19ab0 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75 73 mat string is us
19ac0 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68 61 ed, the '\'' cha
19ad0 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74 0a racter in zText.
19ae0 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61 6e ** is escaped an
19af0 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72 61 d the SQL genera
19b00 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 ted is as follow
19b10 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 s:.**.** <blockq
19b20 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 uote><pre>.** I
19b30 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 NSERT INTO table
19b40 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73 20 1 VALUES('It''s
19b50 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a 2a a happy day!').*
19b60 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 * </pre></blockq
19b70 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 uote>.**.** This
19b80 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48 61 is correct. Ha
19b90 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e 73 d we used %s ins
19ba0 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65 20 tead of %q, the
19bb0 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a 2a generated SQL.**
19bc0 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f 6b would have look
19bd0 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a ed like this:.**
19be0 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
19bf0 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 <pre>.** INSERT
19c00 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c INTO table1 VAL
19c10 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70 70 UES('It's a happ
19c20 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f 70 y day!');.** </p
19c30 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
19c40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 6f .**.** This seco
19c50 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61 6e nd example is an
19c60 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72 6f SQL syntax erro
19c70 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61 6c r. As a general
19c80 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c 64 rule you should
19c90 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20 25 .** always use %
19ca0 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73 20 q instead of %s
19cb0 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 74 when inserting t
19cc0 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69 6e ext into a strin
19cd0 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a g literal..**.**
19ce0 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20 77 The %Q option w
19cf0 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78 63 orks like %q exc
19d00 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64 73 ept it also adds
19d10 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 61 single quotes a
19d20 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75 74 round.** the out
19d30 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74 61 side of the tota
19d40 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 64 69 74 l string. Addit
19d50 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 20 ionally, if the
19d60 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 65 parameter in the
19d70 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6c 69 73 .** argument lis
19d80 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e t is a NULL poin
19d90 74 65 72 2c 20 25 51 20 73 75 62 73 74 69 74 75 ter, %Q substitu
19da0 74 65 73 20 74 68 65 20 74 65 78 74 20 22 4e 55 tes the text "NU
19db0 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a 2a 2a 20 LL" (without.**
19dc0 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 29 20 69 single quotes) i
19dd0 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 25 n place of the %
19de0 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f 2c 20 66 Q option. So, f
19df0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65 20 or example, one
19e00 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a 2a could say:.**.**
19e10 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 <blockquote><pr
19e20 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51 e>.** char *zSQ
19e30 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 L = sqlite3_mpri
19e40 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 4f ntf("INSERT INTO
19e50 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 25 51 table VALUES(%Q
19e60 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 )", zText);.**
19e70 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c sqlite3_exec(db,
19e80 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b zSQL, 0, 0, 0);
19e90 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 .** sqlite3_fre
19ea0 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 e(zSQL);.** </pr
19eb0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
19ec0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 61 **.** The code a
19ed0 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e 64 65 72 bove will render
19ee0 20 61 20 63 6f 72 72 65 63 74 20 53 51 4c 20 73 a correct SQL s
19ef0 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 tatement in the
19f00 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 zSQL.** variable
19f10 20 65 76 65 6e 20 69 66 20 74 68 65 20 7a 54 65 even if the zTe
19f20 78 74 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 xt variable is a
19f30 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a NULL pointer..*
19f40 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 20 66 6f *.** The "%z" fo
19f50 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 20 rmatting option
19f60 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c 69 works exactly li
19f70 6b 65 20 22 25 73 22 20 77 69 74 68 20 74 68 65 ke "%s" with the
19f80 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 74 68 61 .** addition tha
19f90 74 20 61 66 74 65 72 20 74 68 65 20 73 74 72 69 t after the stri
19fa0 6e 67 20 68 61 73 20 62 65 65 6e 20 72 65 61 64 ng has been read
19fb0 20 61 6e 64 20 63 6f 70 69 65 64 20 69 6e 74 6f and copied into
19fc0 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20 .** the result,
19fd0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
19fe0 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 is called on th
19ff0 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e 20 e input string.
1a000 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 {END}.**.** INVA
1a010 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
1a020 31 37 34 30 33 7d 20 20 54 68 65 20 5b 73 71 6c 17403} The [sql
1a030 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d 20 ite3_mprintf()]
1a040 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 76 6d 70 and [sqlite3_vmp
1a050 72 69 6e 74 66 28 29 5d 20 69 6e 74 65 72 66 61 rintf()] interfa
1a060 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ces.**
1a070 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 70 return either p
1a080 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d ointers to zero-
1a090 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
1a0a0 20 73 74 72 69 6e 67 73 20 68 65 6c 64 20 69 6e strings held in
1a0b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6d 65 .** me
1a0c0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
1a0d0 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c om [sqlite3_mall
1a0e0 6f 63 28 29 5d 20 6f 72 20 4e 55 4c 4c 20 70 6f oc()] or NULL po
1a0f0 69 6e 74 65 72 73 20 69 66 0a 2a 2a 20 20 20 20 inters if.**
1a100 20 20 20 20 20 20 20 61 20 63 61 6c 6c 20 74 6f a call to
1a110 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
1a120 28 29 5d 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a ()] fails..**.**
1a130 20 7b 48 31 37 34 30 36 7d 20 20 54 68 65 20 5b {H17406} The [
1a140 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1a150 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 72 ()] interface wr
1a160 69 74 65 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d ites a zero-term
1a170 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 inated.**
1a180 20 20 20 20 55 54 46 2d 38 20 73 74 72 69 6e 67 UTF-8 string
1a190 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 into the buffer
1a1a0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 pointed to by t
1a1b0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
1a1c0 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ter.**
1a1d0 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74 provided that t
1a1e0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
1a1f0 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 er is greater th
1a200 61 6e 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b an zero..**.** {
1a210 48 31 37 34 30 37 7d 20 20 54 68 65 20 5b 73 71 H17407} The [sq
1a220 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
1a230 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 ] interface does
1a240 20 6e 6f 74 20 77 72 69 74 65 20 73 6c 6f 74 73 not write slots
1a250 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
1a260 20 69 74 73 20 6f 75 74 70 75 74 20 62 75 66 66 its output buff
1a270 65 72 20 28 74 68 65 20 73 65 63 6f 6e 64 20 70 er (the second p
1a280 61 72 61 6d 65 74 65 72 29 20 6f 75 74 73 69 64 arameter) outsid
1a290 65 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20 20 e the range.**
1a2a0 20 20 20 20 20 20 20 20 20 6f 66 20 30 20 74 68 of 0 th
1a2b0 72 6f 75 67 68 20 4e 2d 31 20 28 77 68 65 72 65 rough N-1 (where
1a2c0 20 4e 20 69 73 20 74 68 65 20 66 69 72 73 74 20 N is the first
1a2d0 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20 20 20 parameter).**
1a2e0 20 20 20 20 20 20 20 20 72 65 67 61 72 64 6c 65 regardle
1a2f0 73 73 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68 ss of the length
1a300 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a of the string.*
1a310 2a 20 20 20 20 20 20 20 20 20 20 20 72 65 71 75 * requ
1a320 65 73 74 65 64 20 62 79 20 74 68 65 20 66 6f 72 ested by the for
1a330 6d 61 74 20 73 70 65 63 69 66 69 63 61 74 69 6f mat specificatio
1a340 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 n..*/.SQLITE_API
1a350 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d char *sqlite3_m
1a360 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 printf(const cha
1a370 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f r*,...);.SQLITE_
1a380 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
1a390 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 3_vmprintf(const
1a3a0 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 char*, va_list)
1a3b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 ;.SQLITE_API cha
1a3c0 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 r *sqlite3_snpri
1a3d0 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f ntf(int,char*,co
1a3e0 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
1a3f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
1a400 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 : Memory Allocat
1a410 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48 ion Subsystem {H
1a420 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 17300} <S20000>.
1a430 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 **.** The SQLite
1a440 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 73 core uses thes
1a450 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 e three routines
1a460 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 for all of its
1a470 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 own.** internal
1a480 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1a490 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20 n needs. "Core"
1a4a0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 in the previous
1a4b0 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 sentence.** does
1a4c0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 not include ope
1a4d0 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 rating-system sp
1a4e0 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65 ecific VFS imple
1a4f0 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a mentation. The.
1a500 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75 ** Windows VFS u
1a510 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f ses native mallo
1a520 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 c() and free() f
1a530 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f or some operatio
1a540 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ns..**.** The sq
1a550 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 lite3_malloc() r
1a560 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 outine returns a
1a570 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c pointer to a bl
1a580 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 ock.** of memory
1a590 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 at least N byte
1a5a0 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 s in length, whe
1a5b0 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 61 re N is the para
1a5c0 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c meter..** If sql
1a5d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 ite3_malloc() is
1a5e0 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 unable to obtai
1a5f0 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 65 n sufficient fre
1a600 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 e.** memory, it
1a610 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 returns a NULL p
1a620 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 ointer. If the
1a630 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a parameter N to.*
1a640 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 * sqlite3_malloc
1a650 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 () is zero or ne
1a660 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 gative then sqli
1a670 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 te3_malloc() ret
1a680 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 urns.** a NULL p
1a690 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 ointer..**.** Ca
1a6a0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 lling sqlite3_fr
1a6b0 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e ee() with a poin
1a6c0 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 ter previously r
1a6d0 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 eturned.** by sq
1a6e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f lite3_malloc() o
1a6f0 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f r sqlite3_reallo
1a700 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 c() releases tha
1a710 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 t memory so.** t
1a720 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 hat it might be
1a730 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c reused. The sql
1a740 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74 ite3_free() rout
1a750 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f ine is.** a no-o
1a760 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77 p if is called w
1a770 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ith a NULL point
1a780 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e er. Passing a N
1a790 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 ULL pointer.** t
1a7a0 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 o sqlite3_free()
1a7b0 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 is harmless. A
1a7c0 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64 fter being freed
1a7d0 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 , memory.** shou
1a7e0 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65 ld neither be re
1a7f0 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 ad nor written.
1a800 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72 Even reading pr
1a810 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a eviously freed.*
1a820 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 * memory might r
1a830 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 esult in a segme
1a840 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 ntation fault or
1a850 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72 other severe er
1a860 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 ror..** Memory c
1a870 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 orruption, a seg
1a880 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c mentation fault,
1a890 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 or other severe
1a8a0 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 error.** might
1a8b0 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 result if sqlite
1a8c0 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 3_free() is call
1a8d0 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 ed with a non-NU
1a8e0 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a LL pointer that.
1a8f0 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 ** was not obtai
1a900 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
1a910 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c _malloc() or sql
1a920 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a ite3_realloc()..
1a930 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
1a940 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 3_realloc() inte
1a950 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 rface attempts t
1a960 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 o resize a.** pr
1a970 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ior memory alloc
1a980 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c ation to be at l
1a990 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68 east N bytes, wh
1a9a0 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 ere N is the.**
1a9b0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
1a9c0 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c . The memory al
1a9d0 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 location to be r
1a9e0 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 69 esized is the fi
1a9f0 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 rst.** parameter
1aa00 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74 20 . If the first
1aa10 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
1aa20 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a ite3_realloc().*
1aa30 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e * is a NULL poin
1aa40 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 68 ter then its beh
1aa50 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63 avior is identic
1aa60 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a al to calling.**
1aa70 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
1aa80 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 N) where N is th
1aa90 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
1aaa0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 er to sqlite3_re
1aab0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 alloc()..** If t
1aac0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
1aad0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
1aae0 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f ealloc() is zero
1aaf0 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 or.** negative
1ab00 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f then the behavio
1ab10 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 r is exactly the
1ab20 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 same as calling
1ab30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 .** sqlite3_free
1ab40 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74 (P) where P is t
1ab50 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
1ab60 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 er to sqlite3_re
1ab70 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 alloc()..** sqli
1ab80 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 te3_realloc() re
1ab90 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
1aba0 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f to a memory allo
1abb0 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 cation.** of at
1abc0 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e least N bytes in
1abd0 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 size or NULL if
1abe0 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f sufficient memo
1abf0 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c ry is unavailabl
1ac00 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 e..** If M is th
1ac10 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72 e size of the pr
1ac20 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 ior allocation,
1ac30 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 then min(N,M) by
1ac40 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 tes.** of the pr
1ac50 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 ior allocation a
1ac60 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 re copied into t
1ac70 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
1ac80 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a buffer returned.
1ac90 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65 ** by sqlite3_re
1aca0 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20 alloc() and the
1acb0 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e prior allocation
1acc0 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 is freed..** If
1acd0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
1ace0 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c () returns NULL,
1acf0 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 then the prior
1ad00 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 allocation.** is
1ad10 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a not freed..**.*
1ad20 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 * The memory ret
1ad30 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
1ad40 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 _malloc() and sq
1ad50 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a lite3_realloc().
1ad60 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69 ** is always ali
1ad70 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74 gned to at least
1ad80 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 an 8 byte bound
1ad90 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a ary. {END}.**.**
1ada0 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 The default imp
1adb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
1adc0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
1add0 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75 tion subsystem u
1ade0 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f ses.** the mallo
1adf0 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 c(), realloc() a
1ae00 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 64 nd free() provid
1ae10 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 ed by the standa
1ae20 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a rd C library..**
1ae30 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65 {H17382} Howeve
1ae40 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 r, if SQLite is
1ae50 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
1ae60 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f e.** SQLITE_MEMO
1ae70 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f RY_SIZE=<i>NNN</
1ae80 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f i> C preprocesso
1ae90 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c r macro (where <
1aea0 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 i>NNN</i>.** is
1aeb0 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65 an integer), the
1aec0 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20 n SQLite create
1aed0 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20 6f a static array o
1aee0 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 f at least.** <i
1aef0 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 >NNN</i> bytes i
1af00 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20 n size and uses
1af10 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 61 that array for a
1af20 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 ll of its dynami
1af30 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f c.** memory allo
1af40 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 cation needs. {E
1af50 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 ND} Additional
1af60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
1af70 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 options.** may
1af80 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 be added in futu
1af90 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a re releases..**.
1afa0 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72 ** In SQLite ver
1afb0 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 sion 3.5.0 and 3
1afc0 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73 .5.1, it was pos
1afd0 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a sible to define.
1afe0 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d ** the SQLITE_OM
1aff0 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 IT_MEMORY_ALLOCA
1b000 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 TION which would
1b010 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 74 cause the built
1b020 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 -in.** implement
1b030 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 ation of these r
1b040 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d outines to be om
1b050 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 70 itted. That cap
1b060 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f ability.** is no
1b070 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64 longer provided
1b080 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e . Only built-in
1b090 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
1b0a0 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a rs can be used..
1b0b0 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 **.** The Window
1b0c0 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c s OS interface l
1b0d0 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 ayer calls.** th
1b0e0 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 e system malloc(
1b0f0 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72 ) and free() dir
1b100 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 ectly when conve
1b110 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d rting.** filenam
1b120 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 55 es between the U
1b130 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 TF-8 encoding us
1b140 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 ed by SQLite.**
1b150 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 6c and whatever fil
1b160 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 ename encoding i
1b170 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 s used by the pa
1b180 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 rticular Windows
1b190 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e .** installation
1b1a0 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 . Memory alloca
1b1b0 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20 tion errors are
1b1c0 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a detected, but.**
1b1d0 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 74 they are report
1b1e0 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 ed back as [SQLI
1b1f0 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a TE_CANTOPEN] or.
1b200 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 ** [SQLITE_IOERR
1b210 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53 ] rather than [S
1b220 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a QLITE_NOMEM]..**
1b230 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
1b240 2a 2a 0a 2a 2a 20 7b 48 31 37 33 30 33 7d 20 20 **.** {H17303}
1b250 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c The [sqlite3_mal
1b260 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 loc(N)] interfac
1b270 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 e returns either
1b280 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a a pointer to.**
1b290 20 20 20 20 20 20 20 20 20 20 20 61 20 6e 65 77 a new
1b2a0 6c 79 20 63 68 65 63 6b 65 64 2d 6f 75 74 20 62 ly checked-out b
1b2b0 6c 6f 63 6b 20 6f 66 20 61 74 20 6c 65 61 73 74 lock of at least
1b2c0 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f N bytes of memo
1b2d0 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ry.**
1b2e0 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20 61 that is 8-byte a
1b2f0 6c 69 67 6e 65 64 2c 20 6f 72 20 69 74 20 72 65 ligned, or it re
1b300 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 74 turns NULL if it
1b310 20 69 73 20 75 6e 61 62 6c 65 0a 2a 2a 20 20 20 is unable.**
1b320 20 20 20 20 20 20 20 20 74 6f 20 66 75 6c 66 69 to fulfi
1b330 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 2e 0a ll the request..
1b340 2a 2a 0a 2a 2a 20 7b 48 31 37 33 30 34 7d 20 20 **.** {H17304}
1b350 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c The [sqlite3_mal
1b360 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 loc(N)] interfac
1b370 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c e returns a NULL
1b380 20 70 6f 69 6e 74 65 72 20 69 66 0a 2a 2a 20 20 pointer if.**
1b390 20 20 20 20 20 20 20 20 20 4e 20 69 73 20 6c 65 N is le
1b3a0 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
1b3b0 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 to zero..**.**
1b3c0 7b 48 31 37 33 30 35 7d 20 20 54 68 65 20 5b 73 {H17305} The [s
1b3d0 71 6c 69 74 65 33 5f 66 72 65 65 28 50 29 5d 20 qlite3_free(P)]
1b3e0 69 6e 74 65 72 66 61 63 65 20 72 65 6c 65 61 73 interface releas
1b3f0 65 73 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f es memory previo
1b400 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 usly.**
1b410 20 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 returned from
1b420 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
1b430 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 )] or [sqlite3_r
1b440 65 61 6c 6c 6f 63 28 29 5d 2c 0a 2a 2a 20 20 20 ealloc()],.**
1b450 20 20 20 20 20 20 20 20 6d 61 6b 69 6e 67 20 69 making i
1b460 74 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 t available for
1b470 72 65 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 reuse..**.** {H1
1b480 37 33 30 36 7d 20 20 41 20 63 61 6c 6c 20 74 6f 7306} A call to
1b490 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 4e [sqlite3_free(N
1b4a0 55 4c 4c 29 5d 20 69 73 20 61 20 68 61 72 6d 6c ULL)] is a harml
1b4b0 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a ess no-op..**.**
1b4c0 20 7b 48 31 37 33 31 30 7d 20 20 41 20 63 61 6c {H17310} A cal
1b4d0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 l to [sqlite3_re
1b4e0 61 6c 6c 6f 63 28 30 2c 4e 29 5d 20 69 73 20 65 alloc(0,N)] is e
1b4f0 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 20 63 quivalent to a c
1b500 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 all.**
1b510 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c to [sqlite3_mal
1b520 6c 6f 63 28 4e 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b loc(N)]..**.** {
1b530 48 31 37 33 31 32 7d 20 20 41 20 63 61 6c 6c 20 H17312} A call
1b540 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c to [sqlite3_real
1b550 6c 6f 63 28 50 2c 30 29 5d 20 69 73 20 65 71 75 loc(P,0)] is equ
1b560 69 76 61 6c 65 6e 74 20 74 6f 20 61 20 63 61 6c ivalent to a cal
1b570 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 l.** t
1b580 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 o [sqlite3_free(
1b590 50 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 P)]..**.** {H173
1b5a0 31 35 7d 20 20 54 68 65 20 53 51 4c 69 74 65 20 15} The SQLite
1b5b0 63 6f 72 65 20 75 73 65 73 20 5b 73 71 6c 69 74 core uses [sqlit
1b5c0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 e3_malloc()], [s
1b5d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
1b5e0 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ],.**
1b5f0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 and [sqlite3_fre
1b600 65 28 29 5d 20 66 6f 72 20 61 6c 6c 20 6f 66 20 e()] for all of
1b610 69 74 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 its memory alloc
1b620 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20 20 ation and.**
1b630 20 20 20 20 20 20 20 64 65 61 6c 6c 6f 63 61 74 deallocat
1b640 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a ion needs..**.**
1b650 20 7b 48 31 37 33 31 38 7d 20 20 54 68 65 20 5b {H17318} The [
1b660 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
1b670 50 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 P,N)] interface
1b680 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20 61 returns either a
1b690 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 pointer.**
1b6a0 20 20 20 20 20 20 74 6f 20 61 20 62 6c 6f 63 6b to a block
1b6b0 20 6f 66 20 63 68 65 63 6b 65 64 2d 6f 75 74 20 of checked-out
1b6c0 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 memory of at lea
1b6d0 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69 st N bytes in si
1b6e0 7a 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ze.**
1b6f0 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20 61 that is 8-byte a
1b700 6c 69 67 6e 65 64 2c 20 6f 72 20 61 20 4e 55 4c ligned, or a NUL
1b710 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a L pointer..**.**
1b720 20 7b 48 31 37 33 32 31 7d 20 20 57 68 65 6e 20 {H17321} When
1b730 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
1b740 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20 61 (P,N)] returns a
1b750 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 non-NULL pointe
1b760 72 2c 20 69 74 20 66 69 72 73 74 0a 2a 2a 20 20 r, it first.**
1b770 20 20 20 20 20 20 20 20 20 63 6f 70 69 65 73 20 copies
1b780 74 68 65 20 66 69 72 73 74 20 4b 20 62 79 74 65 the first K byte
1b790 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 66 72 6f s of content fro
1b7a0 6d 20 50 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 m P into the new
1b7b0 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ly.**
1b7c0 61 6c 6c 6f 63 61 74 65 64 20 62 6c 6f 63 6b 2c allocated block,
1b7d0 20 77 68 65 72 65 20 4b 20 69 73 20 74 68 65 20 where K is the
1b7e0 6c 65 73 73 65 72 20 6f 66 20 4e 20 61 6e 64 20 lesser of N and
1b7f0 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 20 the size of.**
1b800 20 20 20 20 20 20 20 20 20 74 68 65 20 62 75 66 the buf
1b810 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 fer P..**.** {H1
1b820 37 33 32 32 7d 20 20 57 68 65 6e 20 5b 73 71 6c 7322} When [sql
1b830 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 50 2c 4e ite3_realloc(P,N
1b840 29 5d 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e )] returns a non
1b850 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 69 -NULL pointer, i
1b860 74 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 t first.**
1b870 20 20 20 20 20 72 65 6c 65 61 73 65 73 20 74 68 releases th
1b880 65 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a e buffer P..**.*
1b890 2a 20 7b 48 31 37 33 32 33 7d 20 20 57 68 65 6e * {H17323} When
1b8a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
1b8b0 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20 c(P,N)] returns
1b8c0 4e 55 4c 4c 2c 20 74 68 65 20 62 75 66 66 65 72 NULL, the buffer
1b8d0 20 50 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 P is.**
1b8e0 20 20 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 not modified
1b8f0 6f 72 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a or released..**.
1b900 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a ** ASSUMPTIONS:.
1b910 2a 2a 0a 2a 2a 20 7b 41 31 37 33 35 30 7d 20 20 **.** {A17350}
1b920 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 75 The pointer argu
1b930 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 65 ments to [sqlite
1b940 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b 73 3_free()] and [s
1b950 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
1b960 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6d ].** m
1b970 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 55 ust be either NU
1b980 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 LL or else point
1b990 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ers obtained fro
1b9a0 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20 m a prior.**
1b9b0 20 20 20 20 20 20 20 69 6e 76 6f 63 61 74 69 6f invocatio
1b9c0 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 n of [sqlite3_ma
1b9d0 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 lloc()] or [sqli
1b9e0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74 te3_realloc()] t
1b9f0 68 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 hat have.**
1ba00 20 20 20 20 20 20 6e 6f 74 20 79 65 74 20 62 65 not yet be
1ba10 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a en released..**.
1ba20 2a 2a 20 7b 41 31 37 33 35 31 7d 20 20 54 68 65 ** {A17351} The
1ba30 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 application mus
1ba40 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77 72 t not read or wr
1ba50 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66 0a ite any part of.
1ba60 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 20 62 ** a b
1ba70 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 lock of memory a
1ba80 66 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e fter it has been
1ba90 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a released using.
1baa0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 ** [sq
1bab0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 72 lite3_free()] or
1bac0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
1bad0 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f c()]..*/.SQLITE_
1bae0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 API void *sqlite
1baf0 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 3_malloc(int);.S
1bb00 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
1bb10 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
1bb20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c void*, int);.SQL
1bb30 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1bb40 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 ite3_free(void*)
1bb50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
1bb60 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 F: Memory Alloca
1bb70 74 6f 72 20 53 74 61 74 69 73 74 69 63 73 20 7b tor Statistics {
1bb80 48 31 37 33 37 30 7d 20 3c 53 33 30 32 31 30 3e H17370} <S30210>
1bb90 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 .**.** SQLite pr
1bba0 6f 76 69 64 65 73 20 74 68 65 73 65 20 74 77 6f ovides these two
1bbb0 20 69 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 interfaces for
1bbc0 72 65 70 6f 72 74 69 6e 67 20 6f 6e 20 74 68 65 reporting on the
1bbd0 20 73 74 61 74 75 73 0a 2a 2a 20 6f 66 20 74 68 status.** of th
1bbe0 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f e [sqlite3_mallo
1bbf0 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 66 c()], [sqlite3_f
1bc00 72 65 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c ree()], and [sql
1bc10 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a ite3_realloc()].
1bc20 2a 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77 68 69 ** routines, whi
1bc30 63 68 20 66 6f 72 6d 20 74 68 65 20 62 75 69 6c ch form the buil
1bc40 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f t-in memory allo
1bc50 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
1bc60 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
1bc70 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 37 TS:.**.** {H1737
1bc80 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 1} The [sqlite3_
1bc90 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 72 memory_used()] r
1bca0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
1bcb0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
1bcc0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f es.** o
1bcd0 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 f memory current
1bce0 6c 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 28 ly outstanding (
1bcf0 6d 61 6c 6c 6f 63 65 64 20 62 75 74 20 6e 6f 74 malloced but not
1bd00 20 66 72 65 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 7b freed)..**.** {
1bd10 48 31 37 33 37 33 7d 20 54 68 65 20 5b 73 71 6c H17373} The [sql
1bd20 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
1bd30 77 61 74 65 72 28 29 5d 20 72 6f 75 74 69 6e 65 water()] routine
1bd40 20 72 65 74 75 72 6e 73 20 74 68 65 20 6d 61 78 returns the max
1bd50 69 6d 75 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 imum.**
1bd60 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 value of [sqlit
1bd70 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 e3_memory_used()
1bd80 5d 20 73 69 6e 63 65 20 74 68 65 20 68 69 67 68 ] since the high
1bd90 2d 77 61 74 65 72 20 6d 61 72 6b 0a 2a 2a 20 20 -water mark.**
1bda0 20 20 20 20 20 20 20 20 77 61 73 20 6c 61 73 74 was last
1bdb0 20 72 65 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 reset..**.** {H
1bdc0 31 37 33 37 34 7d 20 54 68 65 20 76 61 6c 75 65 17374} The value
1bdd0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 s returned by [s
1bde0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 qlite3_memory_us
1bdf0 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20 20 ed()] and.**
1be00 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6d [sqlite3_m
1be10 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 emory_highwater(
1be20 29 5d 20 69 6e 63 6c 75 64 65 20 61 6e 79 20 6f )] include any o
1be30 76 65 72 68 65 61 64 0a 2a 2a 20 20 20 20 20 20 verhead.**
1be40 20 20 20 20 61 64 64 65 64 20 62 79 20 53 51 4c added by SQL
1be50 69 74 65 20 69 6e 20 69 74 73 20 69 6d 70 6c 65 ite in its imple
1be60 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 73 71 mentation of [sq
1be70 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c lite3_malloc()],
1be80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 .** but
1be90 20 6e 6f 74 20 6f 76 65 72 68 65 61 64 20 61 64 not overhead ad
1bea0 64 65 64 20 62 79 20 74 68 65 20 61 6e 79 20 75 ded by the any u
1beb0 6e 64 65 72 6c 79 69 6e 67 20 73 79 73 74 65 6d nderlying system
1bec0 20 6c 69 62 72 61 72 79 0a 2a 2a 20 20 20 20 20 library.**
1bed0 20 20 20 20 20 72 6f 75 74 69 6e 65 73 20 74 68 routines th
1bee0 61 74 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c at [sqlite3_mall
1bef0 6f 63 28 29 5d 20 6d 61 79 20 63 61 6c 6c 2e 0a oc()] may call..
1bf00 2a 2a 0a 2a 2a 20 7b 48 31 37 33 37 35 7d 20 54 **.** {H17375} T
1bf10 68 65 20 6d 65 6d 6f 72 79 20 68 69 67 68 2d 77 he memory high-w
1bf20 61 74 65 72 20 6d 61 72 6b 20 69 73 20 72 65 73 ater mark is res
1bf30 65 74 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e et to the curren
1bf40 74 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 20 20 t value of.**
1bf50 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
1bf60 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 69 memory_used()] i
1bf70 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 f and only if th
1bf80 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a e parameter to.*
1bf90 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
1bfa0 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 te3_memory_highw
1bfb0 61 74 65 72 28 29 5d 20 69 73 20 74 72 75 65 2e ater()] is true.
1bfc0 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 The value retu
1bfd0 72 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 rned.**
1bfe0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d by [sqlite3_mem
1bff0 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 31 29 ory_highwater(1)
1c000 5d 20 69 73 20 74 68 65 20 68 69 67 68 2d 77 61 ] is the high-wa
1c010 74 65 72 20 6d 61 72 6b 0a 2a 2a 20 20 20 20 20 ter mark.**
1c020 20 20 20 20 20 70 72 69 6f 72 20 74 6f 20 74 68 prior to th
1c030 65 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 e reset..*/.SQLI
1c040 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 TE_API sqlite3_i
1c050 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d nt64 sqlite3_mem
1c060 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a ory_used(void);.
1c070 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
1c080 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
1c090 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 _memory_highwate
1c0a0 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 r(int resetFlag)
1c0b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
1c0c0 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d F: Pseudo-Random
1c0d0 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 6f Number Generato
1c0e0 72 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30 30 r {H17390} <S200
1c0f0 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 00>.**.** SQLite
1c100 20 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 68 contains a high
1c110 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f 2d -quality pseudo-
1c120 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 random number ge
1c130 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 75 nerator (PRNG) u
1c140 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 74 sed to.** select
1c150 20 72 61 6e 64 6f 6d 20 52 4f 57 49 44 73 20 77 random ROWIDs w
1c160 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 hen inserting ne
1c170 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 w records into a
1c180 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 61 table that.** a
1c190 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 65 20 lready uses the
1c1a0 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 largest possible
1c1b0 20 52 4f 57 49 44 2e 20 20 54 68 65 20 50 52 4e ROWID. The PRN
1c1c0 47 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 66 G is also used f
1c1d0 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d or.** the build-
1c1e0 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e 64 20 in random() and
1c1f0 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 51 4c randomblob() SQL
1c200 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 functions. Thi
1c210 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f s interface allo
1c220 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f ws.** applicatio
1c230 6e 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ns to access the
1c240 20 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 20 6f same PRNG for o
1c250 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a ther purposes..*
1c260 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 *.** A call to t
1c270 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 his routine stor
1c280 65 73 20 4e 20 62 79 74 65 73 20 6f 66 20 72 61 es N bytes of ra
1c290 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 62 75 ndomness into bu
1c2a0 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 ffer P..**.** Th
1c2b0 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 e first time thi
1c2c0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 s routine is inv
1c2d0 6f 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e 74 oked (either int
1c2e0 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a ernally or by.**
1c2f0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
1c300 29 20 74 68 65 20 50 52 4e 47 20 69 73 20 73 65 ) the PRNG is se
1c310 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64 6f eded using rando
1c320 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0a 2a mness obtained.*
1c330 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e 64 * from the xRand
1c340 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 omness method of
1c350 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 the default [sq
1c360 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 lite3_vfs] objec
1c370 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 62 t..** On all sub
1c380 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 sequent invocati
1c390 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 64 6f 2d ons, the pseudo-
1c3a0 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 67 65 randomness is ge
1c3b0 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72 nerated.** inter
1c3c0 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 6f 75 nally and withou
1c3d0 74 20 72 65 63 6f 75 72 73 65 20 74 6f 20 74 68 t recourse to th
1c3e0 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 e [sqlite3_vfs]
1c3f0 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d xRandomness.** m
1c400 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 ethod..**.** INV
1c410 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
1c420 48 31 37 33 39 32 7d 20 54 68 65 20 5b 73 71 6c H17392} The [sql
1c430 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
1c440 4e 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 N,P)] interface
1c450 77 72 69 74 65 73 20 4e 20 62 79 74 65 73 20 6f writes N bytes o
1c460 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68 69 f.** hi
1c470 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64 gh-quality pseud
1c480 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 o-randomness int
1c490 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2f 0a 53 o buffer P..*/.S
1c4a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1c4b0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
1c4c0 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 s(int N, void *P
1c4d0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
1c4e0 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 EF: Compile-Time
1c4f0 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 Authorization C
1c500 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30 allbacks {H12500
1c510 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S70100>.**.**
1c520 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
1c530 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f 72 gisters a author
1c540 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 izer callback wi
1c550 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a th a particular.
1c560 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
1c570 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 nection], suppli
1c580 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 ed in the first
1c590 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 argument..** The
1c5a0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1c5b0 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
1c5c0 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 as SQL statement
1c5d0 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 s are being comp
1c5e0 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 iled.** by [sqli
1c5f0 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f te3_prepare()] o
1c600 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b r its variants [
1c610 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
1c620 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 v2()],.** [sqlit
1c630 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 e3_prepare16()]
1c640 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 and [sqlite3_pre
1c650 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 pare16_v2()]. A
1c660 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 t various.** poi
1c670 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 63 nts during the c
1c680 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 ompilation proce
1c690 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20 ss, as logic is
1c6a0 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a being created.**
1c6b0 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 to perform vari
1c6c0 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 ous actions, the
1c6d0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1c6e0 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
1c6f0 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f to.** see if tho
1c700 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61 se actions are a
1c710 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74 llowed. The aut
1c720 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1c730 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 should.** retur
1c740 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f n [SQLITE_OK] to
1c750 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f allow the actio
1c760 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 n, [SQLITE_IGNOR
1c770 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 E] to disallow t
1c780 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61 he.** specific a
1c790 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 ction but allow
1c7a0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
1c7b0 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f t to continue to
1c7c0 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c be.** compiled,
1c7d0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 or [SQLITE_DENY
1c7e0 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65 ] to cause the e
1c7f0 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d ntire SQL statem
1c800 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a ent to be.** rej
1c810 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 ected with an er
1c820 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 74 ror. If the aut
1c830 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1c840 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 returns.** any
1c850 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e value other than
1c860 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
1c870 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f , [SQLITE_OK], o
1c880 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a r [SQLITE_DENY].
1c890 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c ** then the [sql
1c8a0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
1c8b0 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 )] or equivalent
1c8c0 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 call that trigg
1c8d0 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 ered.** the auth
1c8e0 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c orizer will fail
1c8f0 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d with an error m
1c900 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 essage..**.** Wh
1c910 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 en the callback
1c920 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
1c930 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 OK], that means
1c940 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a the operation.**
1c950 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b requested is ok
1c960 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c . When the call
1c970 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 back returns [SQ
1c980 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a LITE_DENY], the.
1c990 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
1c9a0 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 are_v2()] or equ
1c9b0 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 ivalent call tha
1c9c0 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 0a t triggered the.
1c9d0 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 ** authorizer wi
1c9e0 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 ll fail with an
1c9f0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78 error message ex
1ca00 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a plaining that.**
1ca10 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65 access is denie
1ca20 64 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 6f d. If the autho
1ca30 72 69 7a 65 72 20 63 6f 64 65 20 69 73 20 5b 53 rizer code is [S
1ca40 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 61 QLITE_READ].** a
1ca50 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 nd the callback
1ca60 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
1ca70 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65 IGNORE] then the
1ca80 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
1ca90 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d 65 atement] stateme
1caa0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 nt is constructe
1cab0 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 0a d to substitute.
1cac0 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 ** a NULL value
1cad0 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 in place of the
1cae0 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 table column tha
1caf0 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 t would have.**
1cb00 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 been read if [SQ
1cb10 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 LITE_OK] had bee
1cb20 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 n returned. The
1cb30 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
1cb40 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 62 .** return can b
1cb50 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 61 e used to deny a
1cb60 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 72 n untrusted user
1cb70 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 76 access to indiv
1cb80 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 idual.** columns
1cb90 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 0a of a table..**.
1cba0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 ** The first par
1cbb0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 ameter to the au
1cbc0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1cbd0 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 k is a copy of t
1cbe0 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 he third.** para
1cbf0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c meter to the sql
1cc00 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
1cc10 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e zer() interface.
1cc20 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 The second para
1cc30 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 meter.** to the
1cc40 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 callback is an i
1cc50 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 nteger [SQLITE_C
1cc60 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 OPY | action cod
1cc70 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 65 e] that specifie
1cc80 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 75 s.** the particu
1cc90 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 lar action to be
1cca0 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 authorized. The
1ccb0 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 73 third through s
1ccc0 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 0a ixth parameters.
1ccd0 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 ** to the callba
1cce0 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d ck are zero-term
1ccf0 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 inated strings t
1cd00 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 hat contain addi
1cd10 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c tional.** detail
1cd20 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 69 s about the acti
1cd30 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 on to be authori
1cd40 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75 zed..**.** An au
1cd50 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 64 thorizer is used
1cd60 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 when [sqlite3_p
1cd70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 69 repare | prepari
1cd80 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 ng].** SQL state
1cd90 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e ments from an un
1cda0 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c 20 trusted source,
1cdb0 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 to ensure that t
1cdc0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1cdd0 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20 s.** do not try
1cde0 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 74 to access data t
1cdf0 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f hey are not allo
1ce00 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 74 wed to see, or t
1ce10 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 0a hat they do not.
1ce20 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 74 ** try to execut
1ce30 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74 e malicious stat
1ce40 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61 ements that dama
1ce50 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ge the database.
1ce60 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 For.** example
1ce70 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e , an application
1ce80 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65 may allow a use
1ce90 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74 r to enter arbit
1cea0 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 72 rary.** SQL quer
1ceb0 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69 ies for evaluati
1cec0 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65 on by a database
1ced0 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69 . But the appli
1cee0 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e cation does.** n
1cef0 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 72 ot want the user
1cf00 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6d to be able to m
1cf10 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 68 ake arbitrary ch
1cf20 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 anges to the.**
1cf30 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 75 database. An au
1cf40 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 74 thorizer could t
1cf50 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 6c hen be put in pl
1cf60 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a 2a ace while the.**
1cf70 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51 user-entered SQ
1cf80 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c 69 L is being [sqli
1cf90 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 te3_prepare | pr
1cfa0 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a 20 epared] that.**
1cfb0 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74 disallows everyt
1cfc0 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 4c hing except [SEL
1cfd0 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 2e ECT] statements.
1cfe0 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 .**.** Applicati
1cff0 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f ons that need to
1d000 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 6f process SQL fro
1d010 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 m untrusted sour
1d020 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c 73 ces.** might als
1d030 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 72 o consider lower
1d040 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 6d ing resource lim
1d050 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 its using [sqlit
1d060 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 61 e3_limit()].** a
1d070 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 61 nd limiting data
1d080 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 20 base size using
1d090 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f the [max_page_co
1d0a0 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a unt] [PRAGMA].**
1d0b0 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 in addition to
1d0c0 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 using an authori
1d0d0 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 zer..**.** Only
1d0e0 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69 a single authori
1d0f0 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c zer can be in pl
1d100 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73 ace on a databas
1d110 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
1d120 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68 at a time. Each
1d130 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
1d140 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 _set_authorizer
1d150 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a overrides the.**
1d160 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20 previous call.
1d170 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 74 Disable the aut
1d180 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 61 horizer by insta
1d190 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 6c lling a NULL cal
1d1a0 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 75 lback..** The au
1d1b0 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 61 thorizer is disa
1d1c0 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e bled by default.
1d1d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1d1e0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d rizer callback m
1d1f0 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 ust not do anyth
1d200 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f ing that will mo
1d210 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 dify.** the data
1d220 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1d230 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 that invoked the
1d240 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1d250 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 back..** Note th
1d260 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 at [sqlite3_prep
1d270 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 are_v2()] and [s
1d280 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 qlite3_step()] b
1d290 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 oth modify their
1d2a0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
1d2b0 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 nections for the
1d2c0 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 meaning of "mod
1d2d0 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 ify" in this par
1d2e0 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68 agraph..**.** Wh
1d2f0 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 en [sqlite3_prep
1d300 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 65 are_v2()] is use
1d310 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 73 d to prepare a s
1d320 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a tatement, the.**
1d330 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 74 statement might
1d340 20 62 65 20 72 65 70 72 65 70 61 72 65 64 20 64 be reprepared d
1d350 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 uring [sqlite3_s
1d360 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 20 tep()] due to a
1d370 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 .** schema chang
1d380 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 61 e. Hence, the a
1d390 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c pplication shoul
1d3a0 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 d ensure that th
1d3b0 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 74 e.** correct aut
1d3c0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1d3d0 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 63 remains in plac
1d3e0 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 71 e during the [sq
1d3f0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a lite3_step()]..*
1d400 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 *.** Note that t
1d410 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
1d420 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 llback is invoke
1d430 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a 2a d only during.**
1d440 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
1d450 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 e()] or its vari
1d460 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a 61 ants. Authoriza
1d470 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 tion is not.** p
1d480 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 20 erformed during
1d490 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 61 statement evalua
1d4a0 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 33 tion in [sqlite3
1d4b0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 _step()]..**.**
1d4c0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
1d4d0 2a 20 7b 48 31 32 35 30 31 7d 20 54 68 65 20 5b * {H12501} The [
1d4e0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
1d4f0 6f 72 69 7a 65 72 28 44 2c 2e 2e 2e 29 5d 20 69 orizer(D,...)] i
1d500 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 nterface registe
1d510 72 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 rs a.**
1d520 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1d530 62 61 63 6b 20 77 69 74 68 20 64 61 74 61 62 61 back with databa
1d540 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e se connection D.
1d550 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 32 7d 20 .**.** {H12502}
1d560 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 The authorizer c
1d570 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
1d580 65 64 20 61 73 20 53 51 4c 20 73 74 61 74 65 6d ed as SQL statem
1d590 65 6e 74 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 ents are.**
1d5a0 20 20 20 20 20 62 65 69 6e 67 20 70 61 72 73 65 being parse
1d5b0 65 64 20 61 6e 64 20 63 6f 6d 70 69 6c 65 64 2e ed and compiled.
1d5c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 33 7d 20 .**.** {H12503}
1d5d0 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 If the authorize
1d5e0 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 r callback retur
1d5f0 6e 73 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 ns any value oth
1d600 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 er than.**
1d610 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f [SQLITE_IGNO
1d620 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d RE], [SQLITE_OK]
1d630 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e , or [SQLITE_DEN
1d640 59 5d 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 Y], then.**
1d650 20 20 20 20 20 74 68 65 20 61 70 70 6c 69 63 61 the applica
1d660 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 63 tion interface c
1d670 61 6c 6c 20 74 68 61 74 20 63 61 75 73 65 64 0a all that caused.
1d680 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
1d690 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
1d6a0 61 63 6b 20 74 6f 20 72 75 6e 20 73 68 61 6c 6c ack to run shall
1d6b0 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a fail with an.**
1d6c0 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 [SQLIT
1d6d0 45 5f 45 52 52 4f 52 5d 20 65 72 72 6f 72 20 63 E_ERROR] error c
1d6e0 6f 64 65 20 61 6e 64 20 61 6e 20 61 70 70 72 6f ode and an appro
1d6f0 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 priate error mes
1d700 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 sage..**.** {H12
1d710 35 30 34 7d 20 57 68 65 6e 20 74 68 65 20 61 75 504} When the au
1d720 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1d730 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 k returns [SQLIT
1d740 45 5f 4f 4b 5d 2c 20 74 68 65 20 6f 70 65 72 61 E_OK], the opera
1d750 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 tion.**
1d760 20 64 65 73 63 72 69 62 65 64 20 69 73 20 70 72 described is pr
1d770 6f 63 65 73 73 65 64 20 6e 6f 72 6d 61 6c 6c 79 ocessed normally
1d780 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 35 7d ..**.** {H12505}
1d790 20 57 68 65 6e 20 74 68 65 20 61 75 74 68 6f 72 When the author
1d7a0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 izer callback re
1d7b0 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 turns [SQLITE_DE
1d7c0 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 20 20 20 20 NY], the.**
1d7d0 20 20 20 20 20 61 70 70 6c 69 63 61 74 69 6f 6e application
1d7e0 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20 interface call
1d7f0 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 0a that caused the.
1d800 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 68 ** auth
1d810 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
1d820 74 6f 20 72 75 6e 20 73 68 61 6c 6c 20 66 61 69 to run shall fai
1d830 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 l.** wi
1d840 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 45 52 th an [SQLITE_ER
1d850 52 4f 52 5d 20 65 72 72 6f 72 20 63 6f 64 65 20 ROR] error code
1d860 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 and an error mes
1d870 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 sage.**
1d880 20 65 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 explaining that
1d890 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65 access is denie
1d8a0 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 36 d..**.** {H12506
1d8b0 7d 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 } If the authori
1d8c0 7a 65 72 20 63 6f 64 65 20 28 74 68 65 20 32 6e zer code (the 2n
1d8d0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 d parameter to t
1d8e0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 0a 2a 2a he authorizer.**
1d8f0 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 61 callba
1d900 63 6b 29 20 69 73 20 5b 53 51 4c 49 54 45 5f 52 ck) is [SQLITE_R
1d910 45 41 44 5d 20 61 6e 64 20 74 68 65 20 61 75 74 EAD] and the aut
1d920 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1d930 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 returns.**
1d940 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 4e [SQLITE_IGN
1d950 4f 52 45 5d 2c 20 74 68 65 6e 20 74 68 65 20 70 ORE], then the p
1d960 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1d970 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64 t is constructed
1d980 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 to.**
1d990 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 76 61 insert a NULL va
1d9a0 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 lue in place of
1d9b0 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e the table column
1d9c0 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 that would have
1d9d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65 65 .** bee
1d9e0 6e 20 72 65 61 64 20 69 66 20 5b 53 51 4c 49 54 n read if [SQLIT
1d9f0 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 72 E_OK] had been r
1da00 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b eturned..**.** {
1da10 48 31 32 35 30 37 7d 20 49 66 20 74 68 65 20 61 H12507} If the a
1da20 75 74 68 6f 72 69 7a 65 72 20 63 6f 64 65 20 28 uthorizer code (
1da30 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
1da40 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 r to the authori
1da50 7a 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 zer.**
1da60 63 61 6c 6c 62 61 63 6b 29 20 69 73 20 61 6e 79 callback) is any
1da70 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e thing other than
1da80 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 2c 20 [SQLITE_READ],
1da90 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 then.**
1daa0 20 61 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 51 a return of [SQ
1dab0 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 68 61 73 LITE_IGNORE] has
1dac0 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 74 the same effect
1dad0 20 61 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 as [SQLITE_DENY
1dae0 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 31 30 ]..**.** {H12510
1daf0 7d 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 } The first para
1db00 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 74 meter to the aut
1db10 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1db20 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a is a copy of.**
1db30 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74 68 the th
1db40 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f ird parameter to
1db50 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 the [sqlite3_se
1db60 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20 t_authorizer()]
1db70 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a interface..**.**
1db80 20 7b 48 31 32 35 31 31 7d 20 54 68 65 20 73 65 {H12511} The se
1db90 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 cond parameter t
1dba0 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 o the callback i
1dbb0 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 s an integer.**
1dbc0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 [SQLITE
1dbd0 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 _COPY | action c
1dbe0 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 ode] that specif
1dbf0 69 65 73 20 74 68 65 20 70 61 72 74 69 63 75 6c ies the particul
1dc00 61 72 20 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 ar action.**
1dc10 20 20 20 20 20 20 74 6f 20 62 65 20 61 75 74 68 to be auth
1dc20 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 orized..**.** {H
1dc30 31 32 35 31 32 7d 20 54 68 65 20 74 68 69 72 64 12512} The third
1dc40 20 74 68 72 6f 75 67 68 20 73 69 78 74 68 20 70 through sixth p
1dc50 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65 arameters to the
1dc60 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 0a 2a 2a callback are.**
1dc70 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 zero-t
1dc80 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 erminated string
1dc90 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 0a 2a s that contain.*
1dca0 2a 20 20 20 20 20 20 20 20 20 20 61 64 64 69 74 * addit
1dcb0 69 6f 6e 61 6c 20 64 65 74 61 69 6c 73 20 61 62 ional details ab
1dcc0 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20 74 out the action t
1dcd0 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 2e o be authorized.
1dce0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 30 7d 20 .**.** {H12520}
1dcf0 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 Each call to [sq
1dd00 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
1dd10 69 7a 65 72 28 29 5d 20 6f 76 65 72 72 69 64 65 izer()] override
1dd20 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e s.** an
1dd30 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 y previously ins
1dd40 74 61 6c 6c 65 64 20 61 75 74 68 6f 72 69 7a 65 talled authorize
1dd50 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 31 r..**.** {H12521
1dd60 7d 20 41 20 4e 55 4c 4c 20 61 75 74 68 6f 72 69 } A NULL authori
1dd70 7a 65 72 20 6d 65 61 6e 73 20 74 68 61 74 20 6e zer means that n
1dd80 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a o authorization.
1dd90 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c ** call
1dda0 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 2e back is invoked.
1ddb0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 32 7d 20 .**.** {H12522}
1ddc0 54 68 65 20 64 65 66 61 75 6c 74 20 61 75 74 68 The default auth
1ddd0 6f 72 69 7a 65 72 20 69 73 20 4e 55 4c 4c 2e 0a orizer is NULL..
1dde0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1ddf0 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 t sqlite3_set_au
1de00 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 thorizer(. sqli
1de10 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78 41 te3*,. int (*xA
1de20 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 uth)(void*,int,c
1de30 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
1de40 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 char*,const cha
1de50 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c r*,const char*),
1de60 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 . void *pUserDa
1de70 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ta.);../*.** CAP
1de80 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a 65 I3REF: Authorize
1de90 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20 7b r Return Codes {
1dea0 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30 3e H12590} <H12500>
1deb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 .**.** The [sqli
1dec0 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a te3_set_authoriz
1ded0 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 er | authorizer
1dee0 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
1def0 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 72 n] must.** retur
1df00 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 n either [SQLITE
1df10 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 _OK] or one of t
1df20 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 6e hese two constan
1df30 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 ts in order.** t
1df40 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 20 o signal SQLite
1df50 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
1df60 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72 he action is per
1df70 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 65 mitted. See the
1df80 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 .** [sqlite3_set
1df90 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 _authorizer | au
1dfa0 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 6e thorizer documen
1dfb0 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 69 tation] for addi
1dfc0 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d tional.** inform
1dfd0 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e ation..*/.#defin
1dfe0 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 20 e SQLITE_DENY
1dff0 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 65 1 /* Abort the
1e000 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 SQL statement w
1e010 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a ith an error */.
1e020 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
1e030 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f 6e GNORE 2 /* Don
1e040 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 2c 't allow access,
1e050 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 72 but don't gener
1e060 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a ate an error */.
1e070 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
1e080 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 69 Authorizer Acti
1e090 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35 30 on Codes {H12550
1e0a0 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a } <H12500>.**.**
1e0b0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 The [sqlite3_se
1e0c0 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20 t_authorizer()]
1e0d0 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
1e0e0 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 ers a callback f
1e0f0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 unction.** that
1e100 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 75 is invoked to au
1e110 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e 20 thorize certain
1e120 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 63 SQL statement ac
1e130 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 73 tions. The.** s
1e140 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
1e150 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 to the callback
1e160 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f is an integer co
1e170 64 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65 de that specifie
1e180 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f 6e s.** what action
1e190 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 is being author
1e1a0 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 65 ized. These are
1e1b0 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 74 the integer act
1e1c0 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a 2a ion codes that.*
1e1d0 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 * the authorizer
1e1e0 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 65 callback may be
1e1f0 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 passed..**.** T
1e200 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 hese action code
1e210 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 20 values signify
1e220 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 65 what kind of ope
1e230 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 0a ration is to be.
1e240 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 ** authorized.
1e250 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20 The 3rd and 4th
1e260 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 parameters to th
1e270 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a e authorization.
1e280 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 ** callback func
1e290 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 72 tion will be par
1e2a0 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20 ameters or NULL
1e2b0 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 depending on whi
1e2c0 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 63 ch of these.** c
1e2d0 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73 20 odes is used as
1e2e0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
1e2f0 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 70 eter. The 5th p
1e300 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 0a arameter to the.
1e310 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 ** authorizer ca
1e320 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 61 llback is the na
1e330 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1e340 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d se ("main", "tem
1e350 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66 20 p",.** etc.) if
1e360 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68 65 applicable. The
1e370 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6th parameter t
1e380 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 o the authorizer
1e390 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 callback.** is
1e3a0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1e3b0 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67 67 inner-most trigg
1e3c0 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74 20 er or view that
1e3d0 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
1e3e0 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73 73 or.** the access
1e3f0 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c 4c attempt or NULL
1e400 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73 20 if this access
1e410 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65 63 attempt is direc
1e420 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70 2d tly from.** top-
1e430 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e 0a level SQL code..
1e440 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
1e450 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 31 7d :.**.** {H12551}
1e460 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 The second para
1e470 6d 65 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 20 meter to an.**
1e480 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
1e490 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 _set_authorizer
1e4a0 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c | authorizer cal
1e4b0 6c 62 61 63 6b 5d 20 73 68 61 6c 6c 20 62 65 20 lback] shall be
1e4c0 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 an integer.**
1e4d0 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 43 [SQLITE_C
1e4e0 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 OPY | authorizer
1e4f0 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63 code] that spec
1e500 69 66 69 65 73 20 77 68 61 74 20 61 63 74 69 6f ifies what actio
1e510 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 n.** is
1e520 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69 7a 65 being authorize
1e530 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 32 d..**.** {H12552
1e540 7d 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 } The 3rd and 4t
1e550 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 h parameters to
1e560 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
1e570 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 [sqlite3_set_aut
1e580 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 horizer | author
1e590 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b ization callback
1e5a0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 ].** sh
1e5b0 61 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 all be parameter
1e5c0 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 s or NULL depend
1e5d0 69 6e 67 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 ing on which.**
1e5e0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 [SQLITE
1e5f0 5f 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 7a _COPY | authoriz
1e600 65 72 20 63 6f 64 65 5d 20 69 73 20 75 73 65 64 er code] is used
1e610 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 as the second p
1e620 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 arameter..**.**
1e630 7b 48 31 32 35 35 33 7d 20 54 68 65 20 35 74 68 {H12553} The 5th
1e640 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
1e650 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 e.** [s
1e660 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
1e670 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a rizer | authoriz
1e680 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68 61 er callback] sha
1e690 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a 2a ll be the name.*
1e6a0 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 * of th
1e6b0 65 20 64 61 74 61 62 61 73 65 20 28 65 78 61 6d e database (exam
1e6c0 70 6c 65 3a 20 22 6d 61 69 6e 22 2c 20 22 74 65 ple: "main", "te
1e6d0 6d 70 22 2c 20 65 74 63 2e 29 20 69 66 20 61 70 mp", etc.) if ap
1e6e0 70 6c 69 63 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 plicable..**.**
1e6f0 7b 48 31 32 35 35 34 7d 20 54 68 65 20 36 74 68 {H12554} The 6th
1e700 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
1e710 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 e.** [s
1e720 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
1e730 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a rizer | authoriz
1e740 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68 61 er callback] sha
1e750 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a 2a ll be the name.*
1e760 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 * of th
1e770 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 e inner-most tri
1e780 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 gger or view tha
1e790 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 t is responsible
1e7a0 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 for.**
1e7b0 20 74 68 65 20 61 63 63 65 73 73 20 61 74 74 65 the access atte
1e7c0 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 mpt or NULL if t
1e7d0 68 69 73 20 61 63 63 65 73 73 20 61 74 74 65 6d his access attem
1e7e0 70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 pt is directly f
1e7f0 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 rom.**
1e800 74 6f 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f top-level SQL co
1e810 64 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a de..*/./********
1e820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e840 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a *** 3rd ********
1e850 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a **** 4th *******
1e860 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ****/.#define SQ
1e870 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 LITE_CREATE_INDE
1e880 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 2f X 1 /
1e890 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 * Index Name
1e8a0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 Table Name
1e8b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1e8c0 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 ITE_CREATE_TABLE
1e8d0 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 2 /*
1e8e0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1e8f0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1e900 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1e910 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 TE_CREATE_TEMP_I
1e920 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20 NDEX 3 /*
1e930 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 Index Name
1e940 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1e950 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1e960 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 E_CREATE_TEMP_TA
1e970 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54 BLE 4 /* T
1e980 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e able Name N
1e990 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1e9a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1e9b0 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 _CREATE_TEMP_TRI
1e9c0 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 GGER 5 /* Tr
1e9d0 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 igger Name Ta
1e9e0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
1e9f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1ea00 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 CREATE_TEMP_VIEW
1ea10 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 6 /* Vie
1ea20 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c w Name NUL
1ea30 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1ea40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1ea50 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20 REATE_TRIGGER
1ea60 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67 7 /* Trig
1ea70 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c ger Name Tabl
1ea80 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 e Name */.#
1ea90 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 define SQLITE_CR
1eaa0 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 20 EATE_VIEW
1eab0 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20 8 /* View
1eac0 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
1ead0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1eae0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c efine SQLITE_DEL
1eaf0 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ETE
1eb00 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 9 /* Table
1eb10 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 Name NULL
1eb20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1eb30 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
1eb40 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 _INDEX
1eb50 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 10 /* Index N
1eb60 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e ame Table N
1eb70 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
1eb80 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
1eb90 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 TABLE
1eba0 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 11 /* Table Na
1ebb0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1ebc0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1ebd0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 ne SQLITE_DROP_T
1ebe0 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31 EMP_INDEX 1
1ebf0 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 2 /* Index Nam
1ec00 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d e Table Nam
1ec10 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
1ec20 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 e SQLITE_DROP_TE
1ec30 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33 MP_TABLE 13
1ec40 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1ec50 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1ec60 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1ec70 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
1ec80 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 20 P_TRIGGER 14
1ec90 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d /* Trigger Nam
1eca0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 e Table Name
1ecb0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1ecc0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
1ecd0 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 20 _VIEW 15
1ece0 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 /* View Name
1ecf0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
1ed00 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1ed10 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 QLITE_DROP_TRIGG
1ed20 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 20 ER 16
1ed30 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 /* Trigger Name
1ed40 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
1ed50 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1ed60 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20 LITE_DROP_VIEW
1ed70 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 2f 17 /
1ed80 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 * View Name
1ed90 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1eda0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1edb0 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20 ITE_INSERT
1edc0 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a 18 /*
1edd0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1ede0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1edf0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1ee00 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 TE_PRAGMA
1ee10 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 19 /*
1ee20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20 Pragma Name
1ee30 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20 1st arg or NULL
1ee40 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1ee50 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 E_READ
1ee60 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54 20 /* T
1ee70 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 able Name C
1ee80 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a olumn Name *
1ee90 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1eea0 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 _SELECT
1eeb0 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 21 /* NU
1eec0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 LL NU
1eed0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1eee0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1eef0 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 TRANSACTION
1ef00 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4e 55 4c 22 /* NUL
1ef10 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c L NUL
1ef20 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1ef30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 #define SQLITE_U
1ef40 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 PDATE
1ef50 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c 23 /* Tabl
1ef60 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 e Name Colu
1ef70 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 mn Name */.#
1ef80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54 define SQLITE_AT
1ef90 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 TACH
1efa0 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e 24 /* Filen
1efb0 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 ame NULL
1efc0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1efd0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54 efine SQLITE_DET
1efe0 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 ACH
1eff0 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61 25 /* Databa
1f000 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20 se Name NULL
1f010 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1f020 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45 fine SQLITE_ALTE
1f030 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 R_TABLE
1f040 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 26 /* Databas
1f050 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e e Name Table N
1f060 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
1f070 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 ine SQLITE_REIND
1f080 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EX
1f090 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 27 /* Index Na
1f0a0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1f0b0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1f0c0 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a ne SQLITE_ANALYZ
1f0d0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 E 2
1f0e0 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 8 /* Table Nam
1f0f0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
1f100 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
1f110 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f e SQLITE_CREATE_
1f120 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 39 VTABLE 29
1f130 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1f140 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d Module Nam
1f150 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 e */.#define
1f160 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 SQLITE_DROP_VTA
1f170 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 20 BLE 30
1f180 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
1f190 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 Module Name
1f1a0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1f1b0 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20 SQLITE_FUNCTION
1f1c0 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 20 31
1f1d0 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d /* Function Nam
1f1e0 65 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 e NULL
1f1f0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1f200 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 QLITE_COPY
1f210 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 0
1f220 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 /* No longer use
1f230 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 d */../*.** CAPI
1f240 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 3REF: Tracing An
1f250 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 d Profiling Func
1f260 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c tions {H12280} <
1f270 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S60400>.** EXPER
1f280 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
1f290 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 ese routines reg
1f2a0 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 ister callback f
1f2b0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 unctions that ca
1f2c0 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a n be used for.**
1f2d0 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f tracing and pro
1f2e0 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 filing the execu
1f2f0 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 tion of SQL stat
1f300 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ements..**.** Th
1f310 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
1f320 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 ion registered b
1f330 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 y sqlite3_trace(
1f340 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a ) is invoked at.
1f350 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 ** various times
1f360 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 when an SQL sta
1f370 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 tement is being
1f380 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f run by [sqlite3_
1f390 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 step()]..** The
1f3a0 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
1f3b0 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 a UTF-8 renderi
1f3c0 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 ng of the SQL st
1f3d0 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 atement text.**
1f3e0 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 as the statement
1f3f0 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 first begins ex
1f400 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 ecuting. Additi
1f410 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f onal callbacks o
1f420 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 ccur.** as each
1f430 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f triggered subpro
1f440 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e gram is entered.
1f450 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 The callbacks
1f460 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 for triggers.**
1f470 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 contain a UTF-8
1f480 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 SQL comment that
1f490 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
1f4a0 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 trigger..**.** T
1f4b0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
1f4c0 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 tion registered
1f4d0 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 by sqlite3_profi
1f4e0 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a le() is invoked.
1f4f0 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 ** as each SQL s
1f500 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 tatement finishe
1f510 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 s. The profile
1f520 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e callback contain
1f530 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 s.** the origina
1f540 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 l statement text
1f550 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 and an estimate
1f560 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 of wall-clock t
1f570 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f ime.** of how lo
1f580 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e ng that statemen
1f590 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a t took to run..*
1f5a0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
1f5b0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38 31 7d 20 .**.** {H12281}
1f5c0 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e The callback fun
1f5d0 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
1f5e0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 72 61 by [sqlite3_tra
1f5f0 63 65 28 29 5d 20 0a 2a 2a 20 20 20 20 20 20 20 ce()] .**
1f600 20 20 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 6f shall be invo
1f610 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ked.**
1f620 77 68 65 6e 65 76 65 72 20 61 6e 20 53 51 4c 20 whenever an SQL
1f630 73 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20 statement first
1f640 62 65 67 69 6e 73 20 74 6f 20 65 78 65 63 75 74 begins to execut
1f650 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 e and.**
1f660 20 20 77 68 65 6e 65 76 65 72 20 61 20 74 72 69 whenever a tri
1f670 67 67 65 72 20 73 75 62 70 72 6f 67 72 61 6d 20 gger subprogram
1f680 66 69 72 73 74 20 62 65 67 69 6e 73 20 74 6f 20 first begins to
1f690 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 run..**.** {H122
1f6a0 38 32 7d 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 82} Each call to
1f6b0 20 5b 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 [sqlite3_trace(
1f6c0 29 5d 20 73 68 61 6c 6c 20 6f 76 65 72 72 69 64 )] shall overrid
1f6d0 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 e the previously
1f6e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67 .** reg
1f6f0 69 73 74 65 72 65 64 20 74 72 61 63 65 20 63 61 istered trace ca
1f700 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 llback..**.** {H
1f710 31 32 32 38 33 7d 20 41 20 4e 55 4c 4c 20 74 72 12283} A NULL tr
1f720 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61 ace callback sha
1f730 6c 6c 20 64 69 73 61 62 6c 65 20 74 72 61 63 69 ll disable traci
1f740 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38 ng..**.** {H1228
1f750 34 7d 20 54 68 65 20 66 69 72 73 74 20 61 72 67 4} The first arg
1f760 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61 ument to the tra
1f770 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61 6c ce callback shal
1f780 6c 20 62 65 20 61 20 63 6f 70 79 20 6f 66 0a 2a l be a copy of.*
1f790 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 70 * the p
1f7a0 6f 69 6e 74 65 72 20 77 68 69 63 68 20 77 61 73 ointer which was
1f7b0 20 74 68 65 20 33 72 64 20 61 72 67 75 6d 65 6e the 3rd argumen
1f7c0 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 72 t to [sqlite3_tr
1f7d0 61 63 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ace()]..**.** {H
1f7e0 31 32 32 38 35 7d 20 54 68 65 20 73 65 63 6f 6e 12285} The secon
1f7f0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 d argument to th
1f800 65 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b e trace callback
1f810 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 is a.**
1f820 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 zero-terminate
1f830 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 d UTF-8 string c
1f840 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72 ontaining the or
1f850 69 67 69 6e 61 6c 20 74 65 78 74 0a 2a 2a 20 20 iginal text.**
1f860 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 53 of the S
1f870 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20 QL statement as
1f880 69 74 20 77 61 73 20 70 61 73 73 65 64 20 69 6e it was passed in
1f890 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 to [sqlite3_prep
1f8a0 61 72 65 5f 76 32 28 29 5d 0a 2a 2a 20 20 20 20 are_v2()].**
1f8b0 20 20 20 20 20 20 6f 72 20 74 68 65 20 65 71 75 or the equ
1f8c0 69 76 61 6c 65 6e 74 2c 20 6f 72 20 61 6e 20 53 ivalent, or an S
1f8d0 51 4c 20 63 6f 6d 6d 65 6e 74 20 69 6e 64 69 63 QL comment indic
1f8e0 61 74 69 6e 67 20 74 68 65 20 62 65 67 69 6e 6e ating the beginn
1f8f0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
1f900 6f 66 20 61 20 74 72 69 67 67 65 72 20 73 75 62 of a trigger sub
1f910 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b program..**.** {
1f920 48 31 32 32 38 37 7d 20 54 68 65 20 63 61 6c 6c H12287} The call
1f930 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 back function re
1f940 67 69 73 74 65 72 65 64 20 62 79 20 5b 73 71 6c gistered by [sql
1f950 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 5d 20 ite3_profile()]
1f960 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 20 20 is invoked.**
1f970 20 20 20 20 20 20 20 61 73 20 65 61 63 68 20 53 as each S
1f980 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69 6e QL statement fin
1f990 69 73 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ishes..**.** {H1
1f9a0 32 32 38 38 7d 20 54 68 65 20 66 69 72 73 74 20 2288} The first
1f9b0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
1f9c0 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 profile callbac
1f9d0 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a k is a copy of.*
1f9e0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 33 * the 3
1f9f0 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 rd parameter to
1fa00 5b 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 [sqlite3_profile
1fa10 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 ()]..**.** {H122
1fa20 38 39 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70 89} The second p
1fa30 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
1fa40 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b profile callback
1fa50 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 is a.**
1fa60 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 zero-terminate
1fa70 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 d UTF-8 string t
1fa80 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
1fa90 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f complete text o
1faa0 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 f.** th
1fab0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
1fac0 61 73 20 69 74 20 77 61 73 20 70 72 6f 63 65 73 as it was proces
1fad0 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f sed by [sqlite3_
1fae0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 0a 2a 2a prepare_v2()].**
1faf0 20 20 20 20 20 20 20 20 20 20 6f 72 20 74 68 65 or the
1fb00 20 65 71 75 69 76 61 6c 65 6e 74 2e 0a 2a 2a 0a equivalent..**.
1fb10 2a 2a 20 7b 48 31 32 32 39 30 7d 20 54 68 65 20 ** {H12290} The
1fb20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
1fb30 74 6f 20 74 68 65 20 70 72 6f 66 69 6c 65 20 63 to the profile c
1fb40 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 65 73 allback is an es
1fb50 74 69 6d 61 74 65 0a 2a 2a 20 20 20 20 20 20 20 timate.**
1fb60 20 20 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 of the number
1fb70 20 6f 66 20 6e 61 6e 6f 73 65 63 6f 6e 64 73 20 of nanoseconds
1fb80 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69 of wall-clock ti
1fb90 6d 65 20 72 65 71 75 69 72 65 64 20 74 6f 0a 2a me required to.*
1fba0 2a 20 20 20 20 20 20 20 20 20 20 72 75 6e 20 74 * run t
1fbb0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1fbc0 20 66 72 6f 6d 20 73 74 61 72 74 20 74 6f 20 66 from start to f
1fbd0 69 6e 69 73 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 inish..*/.SQLITE
1fbe0 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
1fbf0 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73 RIMENTAL void *s
1fc00 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c qlite3_trace(sql
1fc10 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72 ite3*, void(*xTr
1fc20 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 ace)(void*,const
1fc30 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b char*), void*);
1fc40 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
1fc50 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
1fc60 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 void *sqlite3_pr
1fc70 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a ofile(sqlite3*,.
1fc80 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c void(*xProfil
1fc90 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 e)(void*,const c
1fca0 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e har*,sqlite3_uin
1fcb0 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f t64), void*);../
1fcc0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51 *.** CAPI3REF: Q
1fcd0 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43 61 uery Progress Ca
1fce0 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30 7d llbacks {H12910}
1fcf0 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S60400>.**.**
1fd00 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e This routine con
1fd10 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62 61 figures a callba
1fd20 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74 68 ck function - th
1fd30 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63 61 e.** progress ca
1fd40 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69 73 llback - that is
1fd50 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64 69 invoked periodi
1fd60 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f 6e cally during lon
1fd70 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61 6c g.** running cal
1fd80 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 ls to [sqlite3_e
1fd90 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 xec()], [sqlite3
1fda0 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a 20 _step()] and.**
1fdb0 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 [sqlite3_get_tab
1fdc0 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d 70 le()]. An examp
1fdd0 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 0a le use for this.
1fde0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 ** interface is
1fdf0 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 70 to keep a GUI up
1fe00 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c dated during a l
1fe10 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a arge query..**.*
1fe20 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73 * If the progres
1fe30 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 s callback retur
1fe40 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 ns non-zero, the
1fe50 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a operation is.**
1fe60 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 54 interrupted. T
1fe70 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e 20 his feature can
1fe80 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 be used to imple
1fe90 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 65 ment a.** "Cance
1fea0 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 47 l" button on a G
1feb0 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 6c UI progress dial
1fec0 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 og box..**.** Th
1fed0 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c e progress handl
1fee0 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 er must not do a
1fef0 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c nything that wil
1ff00 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 l modify.** the
1ff10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1ff20 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 ion that invoked
1ff30 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 the progress ha
1ff40 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20 74 ndler..** Note t
1ff50 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hat [sqlite3_pre
1ff60 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b pare_v2()] and [
1ff70 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1ff80 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 both modify thei
1ff90 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f r.** database co
1ffa0 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 nnections for th
1ffb0 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f e meaning of "mo
1ffc0 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 dify" in this pa
1ffd0 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 49 ragraph..**.** I
1ffe0 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
1fff0 20 7b 48 31 32 39 31 31 7d 20 54 68 65 20 63 61 {H12911} The ca
20000 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 llback function
20010 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73 71 registered by sq
20020 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 lite3_progress_h
20030 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 20 20 20 20 andler().**
20040 20 20 20 20 20 69 73 20 69 6e 76 6f 6b 65 64 20 is invoked
20050 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 75 72 periodically dur
20060 69 6e 67 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e 67 ing long running
20070 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 20 calls to.**
20080 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 [sqlite3_s
20090 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 tep()]..**.** {H
200a0 31 32 39 31 32 7d 20 54 68 65 20 70 72 6f 67 72 12912} The progr
200b0 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ess callback is
200c0 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 invoked once for
200d0 20 65 76 65 72 79 20 4e 20 76 69 72 74 75 61 6c every N virtual
200e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 61 63 .** mac
200f0 68 69 6e 65 20 6f 70 63 6f 64 65 73 2c 20 77 68 hine opcodes, wh
20100 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 ere N is the sec
20110 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a ond argument to.
20120 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
20130 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 [sqlite3_progres
20140 73 5f 68 61 6e 64 6c 65 72 28 29 5d 20 63 61 6c s_handler()] cal
20150 6c 20 74 68 61 74 20 72 65 67 69 73 74 65 72 65 l that registere
20160 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 d.** th
20170 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 49 66 20 e callback. If
20180 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 31 N is less than 1
20190 2c 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 , sqlite3_progre
201a0 73 73 5f 68 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 ss_handler().**
201b0 20 20 20 20 20 20 20 20 20 61 63 74 73 20 61 73 acts as
201c0 20 69 66 20 61 20 4e 55 4c 4c 20 70 72 6f 67 72 if a NULL progr
201d0 65 73 73 20 68 61 6e 64 6c 65 72 20 68 61 64 20 ess handler had
201e0 62 65 65 6e 20 73 70 65 63 69 66 69 65 64 2e 0a been specified..
201f0 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31 33 7d 20 54 **.** {H12913} T
20200 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c he progress call
20210 62 61 63 6b 20 69 74 73 65 6c 66 20 69 73 20 69 back itself is i
20220 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 dentified by the
20230 20 74 68 69 72 64 0a 2a 2a 20 20 20 20 20 20 20 third.**
20240 20 20 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 argument to s
20250 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f qlite3_progress_
20260 68 61 6e 64 6c 65 72 28 29 2e 0a 2a 2a 0a 2a 2a handler()..**.**
20270 20 7b 48 31 32 39 31 34 7d 20 54 68 65 20 66 6f {H12914} The fo
20280 75 72 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f urth argument to
20290 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 sqlite3_progres
202a0 73 5f 68 61 6e 64 6c 65 72 28 29 20 69 73 20 61 s_handler() is a
202b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 6f 69 .** voi
202c0 64 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 d pointer passed
202d0 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 to the progress
202e0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 callback.**
202f0 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 65 function e
20300 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20 69 ach time it is i
20310 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 nvoked..**.** {H
20320 31 32 39 31 35 7d 20 49 66 20 61 20 63 61 6c 6c 12915} If a call
20330 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 to [sqlite3_ste
20340 70 28 29 5d 20 72 65 73 75 6c 74 73 20 69 6e 20 p()] results in
20350 66 65 77 65 72 20 74 68 61 6e 20 4e 20 6f 70 63 fewer than N opc
20360 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 odes.**
20370 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 2c being executed,
20380 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 65 then the progre
20390 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e ss callback is n
203a0 65 76 65 72 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a ever invoked..**
203b0 0a 2a 2a 20 7b 48 31 32 39 31 36 7d 20 45 76 65 .** {H12916} Eve
203c0 72 79 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ry call to [sqli
203d0 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e te3_progress_han
203e0 64 6c 65 72 28 29 5d 0a 2a 2a 20 20 20 20 20 20 dler()].**
203f0 20 20 20 20 6f 76 65 72 77 72 69 74 65 73 20 61 overwrites a
20400 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 ny previously re
20410 67 69 73 74 65 72 65 64 20 70 72 6f 67 72 65 73 gistered progres
20420 73 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a s handler..**.**
20430 20 7b 48 31 32 39 31 37 7d 20 49 66 20 74 68 65 {H12917} If the
20440 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 progress handle
20450 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 r callback is NU
20460 4c 4c 20 74 68 65 6e 20 6e 6f 20 70 72 6f 67 72 LL then no progr
20470 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ess.**
20480 68 61 6e 64 6c 65 72 20 69 73 20 69 6e 76 6f 6b handler is invok
20490 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31 ed..**.** {H1291
204a0 38 7d 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 8} If the progre
204b0 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 ss callback retu
204c0 72 6e 73 20 61 20 72 65 73 75 6c 74 20 6f 74 68 rns a result oth
204d0 65 72 20 74 68 61 6e 20 30 2c 20 74 68 65 6e 0a er than 0, then.
204e0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
204f0 62 65 68 61 76 69 6f 72 20 69 73 20 61 20 69 66 behavior is a if
20500 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 [sqlite3_interr
20510 75 70 74 28 29 5d 20 68 61 64 20 62 65 65 6e 20 upt()] had been
20520 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 20 20 20 called..**
20530 20 20 20 20 3c 53 33 30 35 30 30 3e 0a 2a 2f 0a <S30500>.*/.
20540 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
20550 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 sqlite3_progress
20560 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 _handler(sqlite3
20570 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76 *, int, int(*)(v
20580 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a oid*), void*);..
20590 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
205a0 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 Opening A New Da
205b0 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f tabase Connectio
205c0 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32 n {H12700} <S402
205d0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
205e0 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e routines open an
205f0 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 SQLite database
20600 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65 file whose name
20610 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 is given by the
20620 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 .** filename arg
20630 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e ument. The filen
20640 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ame argument is
20650 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55 interpreted as U
20660 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 TF-8 for.** sqli
20670 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 te3_open() and s
20680 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
20690 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69 and as UTF-16 i
206a0 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 n the native byt
206b0 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 e.** order for s
206c0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e qlite3_open16().
206d0 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e A [database con
206e0 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 nection] handle
206f0 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65 is usually.** re
20700 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c turned in *ppDb,
20710 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f even if an erro
20720 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f r occurs. The o
20730 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73 nly exception is
20740 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69 that.** if SQLi
20750 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 te is unable to
20760 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 allocate memory
20770 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c to hold the [sql
20780 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a ite3] object,.**
20790 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20 a NULL will be
207a0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70 written into *pp
207b0 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 Db instead of a
207c0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b pointer to the [
207d0 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65 sqlite3].** obje
207e0 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 ct. If the datab
207f0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61 ase is opened (a
20800 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73 nd/or created) s
20810 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65 uccessfully, the
20820 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d n.** [SQLITE_OK]
20830 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f is returned. O
20840 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 therwise an [err
20850 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
20860 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 rned. The.** [s
20870 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d qlite3_errmsg()]
20880 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 or [sqlite3_err
20890 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 msg16()] routine
208a0 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f s can be used to
208b0 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e obtain.** an En
208c0 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64 glish language d
208d0 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 escription of th
208e0 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 e error..**.** T
208f0 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 he default encod
20900 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 ing for the data
20910 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46 base will be UTF
20920 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 -8 if.** sqlite3
20930 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 _open() or sqlit
20940 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 e3_open_v2() is
20950 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 called and.** UT
20960 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 F-16 in the nati
20970 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 ve byte order if
20980 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
20990 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a ) is used..**.**
209a0 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 Whether or not
209b0 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
209c0 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 when it is opene
209d0 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 d, resources.**
209e0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
209f0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
20a00 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 nnection] handle
20a10 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 should be relea
20a20 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e sed by.** passin
20a30 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 g it to [sqlite3
20a40 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69 _close()] when i
20a50 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 t is no longer r
20a60 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 equired..**.** T
20a70 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f he sqlite3_open_
20a80 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77 v2() interface w
20a90 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 orks like sqlite
20aa0 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65 3_open().** exce
20ab0 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70 pt that it accep
20ac0 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 ts two additiona
20ad0 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 l parameters for
20ae0 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 additional cont
20af0 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 rol.** over the
20b00 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e new database con
20b10 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c nection. The fl
20b20 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 ags parameter ca
20b30 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a n take one of.**
20b40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
20b50 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74 hree values, opt
20b60 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64 ionally combined
20b70 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53 with the .** [S
20b80 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 QLITE_OPEN_NOMUT
20b90 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4f EX] or [SQLITE_O
20ba0 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 PEN_FULLMUTEX] f
20bb0 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e lags:.**.** <dl>
20bc0 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f .** <dt>[SQLITE_
20bd0 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f OPEN_READONLY]</
20be0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 dt>.** <dd>The d
20bf0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 atabase is opene
20c00 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d d in read-only m
20c10 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 ode. If the dat
20c20 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a abase does not.*
20c30 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c * already exist,
20c40 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 an error is ret
20c50 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a urned.</dd>.**.*
20c60 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 * <dt>[SQLITE_OP
20c70 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 EN_READWRITE]</d
20c80 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 t>.** <dd>The da
20c90 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
20ca0 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 for reading and
20cb0 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73 writing if poss
20cc0 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 ible, or reading
20cd0 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 .** only if the
20ce0 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 file is write pr
20cf0 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f otected by the o
20d00 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e perating system.
20d10 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 In either.** c
20d20 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ase the database
20d30 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 must already ex
20d40 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61 ist, otherwise a
20d50 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
20d60 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
20d70 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e <dt>[SQLITE_OPEN
20d80 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 _READWRITE] | [S
20d90 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
20da0 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 E]</dt>.** <dd>T
20db0 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f he database is o
20dc0 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e pened for readin
20dd0 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 g and writing, a
20de0 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74 nd is creates it
20df0 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e if.** it does n
20e00 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ot already exist
20e10 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65 . This is the be
20e20 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 havior that is a
20e30 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a lways used for.*
20e40 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 * sqlite3_open()
20e50 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 and sqlite3_ope
20e60 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c n16().</dd>.** <
20e70 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 /dl>.**.** If th
20e80 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 e 3rd parameter
20e90 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f to sqlite3_open_
20ea0 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 v2() is not one
20eb0 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e of the.** combin
20ec0 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f ations shown abo
20ed0 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 ve or one of the
20ee0 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 combinations sh
20ef0 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e own above combin
20f00 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b ed.** with the [
20f10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 SQLITE_OPEN_NOMU
20f20 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f TEX] or [SQLITE_
20f30 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 OPEN_FULLMUTEX]
20f40 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 flags,.** then t
20f50 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
20f60 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
20f70 49 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f If the [SQLITE_O
20f80 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 PEN_NOMUTEX] fla
20f90 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 g is set, then t
20fa0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
20fb0 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 ection.** opens
20fc0 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 in the multi-thr
20fd0 65 61 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d ead [threading m
20fe0 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20 ode] as long as
20ff0 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 the single-threa
21000 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f d.** mode has no
21010 74 20 62 65 65 6e 20 73 65 74 20 61 74 20 63 6f t been set at co
21020 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 mpile-time or st
21030 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 68 art-time. If th
21040 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 e.** [SQLITE_OPE
21050 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 N_FULLMUTEX] fla
21060 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 74 68 g is set then th
21070 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
21080 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 ction opens.** i
21090 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 n the serialized
210a0 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
210b0 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d ] unless single-
210c0 74 68 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 72 thread was.** pr
210d0 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 65 eviously selecte
210e0 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d d at compile-tim
210f0 65 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e e or start-time.
21100 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
21110 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f lename is ":memo
21120 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 69 ry:", then a pri
21130 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 20 vate, temporary
21140 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
21150 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 64 se.** is created
21160 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 for the connect
21170 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d 65 ion. This in-me
21180 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69 mory database wi
21190 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a 2a ll vanish when.*
211a0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 * the database c
211b0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f onnection is clo
211c0 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72 sed. Future ver
211d0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
211e0 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 73 might.** make us
211f0 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 e of additional
21200 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65 special filename
21210 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 s that begin wit
21220 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63 h the ":" charac
21230 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 65 ter..** It is re
21240 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 77 commended that w
21250 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 hen a database f
21260 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c 79 ilename actually
21270 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 68 does begin with
21280 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 63 .** a ":" charac
21290 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 70 ter you should p
212a0 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61 refix the filena
212b0 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e 61 me with a pathna
212c0 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 2e me such as.** ".
212d0 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 69 /" to avoid ambi
212e0 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 guity..**.** If
212f0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 the filename is
21300 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c an empty string,
21310 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c then a private,
21320 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f 6e temporary.** on
21330 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77 -disk database w
21340 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 20 ill be created.
21350 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 61 This private da
21360 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a 2a tabase will be.*
21370 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
21380 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e 20 deleted as soon
21390 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 as the database
213a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c connection is cl
213b0 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 osed..**.** The
213c0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
213d0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e to sqlite3_open
213e0 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 6d _v2() is the nam
213f0 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c e of the.** [sql
21400 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
21410 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
21420 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
21430 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 em interface tha
21440 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 t.** the new dat
21450 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
21460 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 66 should use. If
21470 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 the fourth para
21480 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e 55 meter is.** a NU
21490 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 LL pointer then
214a0 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c the default [sql
214b0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
214c0 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 is used..**.**
214d0 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 6f <b>Note to Windo
214e0 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54 ws users:</b> T
214f0 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 he encoding used
21500 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d for the filenam
21510 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66 e argument.** of
21520 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 sqlite3_open()
21530 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e and sqlite3_open
21540 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 54 _v2() must be UT
21550 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65 F-8, not whateve
21560 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73 r.** codepage is
21570 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e currently defin
21580 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63 ed. Filenames c
21590 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e ontaining intern
215a0 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61 ational.** chara
215b0 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f cters must be co
215c0 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38 nverted to UTF-8
215d0 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e prior to passin
215e0 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 73 g them into.** s
215f0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72 qlite3_open() or
21600 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
21610 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 ()..**.** INVARI
21620 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ANTS:.**.** {H12
21630 37 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 701} The [sqlite
21640 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 3_open()], [sqli
21650 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 61 te3_open16()], a
21660 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b nd.** [
21670 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
21680 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 63 72 )] interfaces cr
21690 65 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20 20 eate a new.**
216a0 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 [database
216b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73 73 connection] ass
216c0 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 ociated with.**
216d0 20 20 20 20 20 20 20 20 20 74 68 65 20 64 61 74 the dat
216e0 61 62 61 73 65 20 66 69 6c 65 20 67 69 76 65 6e abase file given
216f0 20 69 6e 20 74 68 65 69 72 20 66 69 72 73 74 20 in their first
21700 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a parameter..**.**
21710 20 7b 48 31 32 37 30 32 7d 20 54 68 65 20 66 69 {H12702} The fi
21720 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 lename argument
21730 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 is interpreted a
21740 73 20 55 54 46 2d 38 0a 2a 2a 20 20 20 20 20 20 s UTF-8.**
21750 20 20 20 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 for [sqlite3
21760 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 5b 73 71 _open()] and [sq
21770 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
21780 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 0a 2a and as UTF-16.*
21790 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68 * in th
217a0 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 e native byte or
217b0 64 65 72 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 der for [sqlite3
217c0 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a 2a _open16()]..**.*
217d0 2a 20 7b 48 31 32 37 30 33 7d 20 41 20 73 75 63 * {H12703} A suc
217e0 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 cessful invocati
217f0 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6f on of [sqlite3_o
21800 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 pen()], [sqlite3
21810 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 _open16()],.**
21820 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 or [sqli
21830 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 77 te3_open_v2()] w
21840 72 69 74 65 73 20 61 20 70 6f 69 6e 74 65 72 20 rites a pointer
21850 74 6f 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20 to a new.**
21860 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 [database c
21870 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 6e 74 6f 20 onnection] into
21880 2a 70 70 44 62 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 *ppDb..**.** {H1
21890 32 37 30 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 2704} The [sqlit
218a0 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c e3_open()], [sql
218b0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 ite3_open16()],
218c0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
218d0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
218e0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 72 ()] interfaces r
218f0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b eturn [SQLITE_OK
21900 5d 20 75 70 6f 6e 20 73 75 63 63 65 73 73 2c 0a ] upon success,.
21910 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 61 ** or a
21920 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 n appropriate [e
21930 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 61 rror code] on fa
21940 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ilure..**.** {H1
21950 32 37 30 36 7d 20 54 68 65 20 64 65 66 61 75 6c 2706} The defaul
21960 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 t text encoding
21970 66 6f 72 20 61 20 6e 65 77 20 64 61 74 61 62 61 for a new databa
21980 73 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 se created using
21990 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
219a0 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 6f 72 lite3_open()] or
219b0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
219c0 32 28 29 5d 20 77 69 6c 6c 20 62 65 20 55 54 46 2()] will be UTF
219d0 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 30 -8..**.** {H1270
219e0 37 7d 20 54 68 65 20 64 65 66 61 75 6c 74 20 74 7} The default t
219f0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 ext encoding for
21a00 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 a new database
21a10 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a created using.**
21a20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
21a30 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 77 69 6c e3_open16()] wil
21a40 6c 20 62 65 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a l be UTF-16..**.
21a50 2a 2a 20 7b 48 31 32 37 30 39 7d 20 54 68 65 20 ** {H12709} The
21a60 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 46 2c [sqlite3_open(F,
21a70 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 69 73 D)] interface is
21a80 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 2a equivalent to.*
21a90 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
21aa0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c te3_open_v2(F,D,
21ab0 47 2c 30 29 5d 20 77 68 65 72 65 20 74 68 65 20 G,0)] where the
21ac0 47 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a G parameter is.*
21ad0 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 * [SQLI
21ae0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
21af0 45 5d 7c 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f E]|[SQLITE_OPEN_
21b00 43 52 45 41 54 45 5d 2e 0a 2a 2a 0a 2a 2a 20 7b CREATE]..**.** {
21b10 48 31 32 37 31 31 7d 20 49 66 20 74 68 65 20 47 H12711} If the G
21b20 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
21b30 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 qlite3_open_v2(F
21b40 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74 61 69 6e ,D,G,V)] contain
21b50 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
21b60 20 20 62 69 74 20 76 61 6c 75 65 20 5b 53 51 4c bit value [SQL
21b70 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
21b80 59 5d 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 Y] then the data
21b90 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 0a 2a base is opened.*
21ba0 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 72 * for r
21bb0 65 61 64 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2a 0a eading only..**.
21bc0 2a 2a 20 7b 48 31 32 37 31 32 7d 20 49 66 20 74 ** {H12712} If t
21bd0 68 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74 he G parameter t
21be0 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f o [sqlite3_open_
21bf0 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e v2(F,D,G,V)] con
21c00 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 tains the.**
21c10 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20 bit value
21c20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 [SQLITE_OPEN_REA
21c30 44 57 52 49 54 45 5d 20 74 68 65 6e 20 74 68 65 DWRITE] then the
21c40 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 database is ope
21c50 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ned.**
21c60 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 reading and writ
21c70 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c 65 2c ing if possible,
21c80 20 6f 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 or for reading
21c90 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 20 only if the.**
21ca0 20 20 20 20 20 20 20 20 66 69 6c 65 20 69 73 20 file is
21cb0 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 20 write protected
21cc0 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 by the operating
21cd0 20 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 7b system..**.** {
21ce0 48 31 32 37 31 33 7d 20 49 66 20 74 68 65 20 47 H12713} If the G
21cf0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
21d00 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 qlite3_open_v2(F
21d10 2c 44 2c 47 2c 56 29 5d 20 6f 6d 69 74 73 20 74 ,D,G,V)] omits t
21d20 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 he.** b
21d30 69 74 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 it value [SQLITE
21d40 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 20 61 6e _OPEN_CREATE] an
21d50 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 d the database d
21d60 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 oes not.**
21d70 20 20 20 20 70 72 65 76 69 6f 75 73 6c 79 20 65 previously e
21d80 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20 69 xist, an error i
21d90 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
21da0 2a 20 7b 48 31 32 37 31 34 7d 20 49 66 20 74 68 * {H12714} If th
21db0 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74 6f e G parameter to
21dc0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
21dd0 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74 2(F,D,G,V)] cont
21de0 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 ains the.**
21df0 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20 5b bit value [
21e00 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
21e10 54 45 5d 20 61 6e 64 20 74 68 65 20 64 61 74 61 TE] and the data
21e20 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a base does not.**
21e30 20 20 20 20 20 20 20 20 20 20 70 72 65 76 69 6f previo
21e40 75 73 6c 79 20 65 78 69 73 74 2c 20 74 68 65 6e usly exist, then
21e50 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d an attempt is m
21e60 61 64 65 20 74 6f 20 63 72 65 61 74 65 20 61 6e ade to create an
21e70 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e d.** in
21e80 69 74 69 61 6c 69 7a 65 20 74 68 65 20 64 61 74 itialize the dat
21e90 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 abase..**.** {H1
21ea0 32 37 31 37 7d 20 49 66 20 74 68 65 20 66 69 6c 2717} If the fil
21eb0 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 ename argument t
21ec0 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 o [sqlite3_open(
21ed0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 )], [sqlite3_ope
21ee0 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 n16()],.**
21ef0 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f or [sqlite3_
21f00 6f 70 65 6e 5f 76 32 28 29 5d 20 69 73 20 22 3a open_v2()] is ":
21f10 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 memory:", then a
21f20 6e 20 70 72 69 76 61 74 65 2c 0a 2a 2a 20 20 20 n private,.**
21f30 20 20 20 20 20 20 20 65 70 68 65 6d 65 72 61 6c ephemeral
21f40 2c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 , in-memory data
21f50 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 20 base is created
21f60 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 for the connecti
21f70 6f 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 on..**
21f80 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49 54 45 5f <todo>Is SQLITE_
21f90 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49 OPEN_CREATE|SQLI
21fa0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
21fb0 45 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 20 20 E required.**
21fc0 20 20 20 20 20 20 20 69 6e 20 73 71 6c 69 74 65 in sqlite
21fd0 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c 2f 74 6f 3_open_v2()?</to
21fe0 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 31 do>.**.** {H1271
21ff0 39 7d 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 9} If the filena
22000 6d 65 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e me is NULL or an
22010 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 20 74 empty string, t
22020 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 0a 2a hen a private,.*
22030 2a 20 20 20 20 20 20 20 20 20 20 65 70 68 65 6d * ephem
22040 65 72 61 6c 20 6f 6e 2d 64 69 73 6b 20 64 61 74 eral on-disk dat
22050 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72 abase will be cr
22060 65 61 74 65 64 2e 0a 2a 2a 20 20 20 20 20 20 20 eated..**
22070 20 20 20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49 <todo>Is SQLI
22080 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 TE_OPEN_CREATE|S
22090 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
220a0 52 49 54 45 20 72 65 71 75 69 72 65 64 0a 2a 2a RITE required.**
220b0 20 20 20 20 20 20 20 20 20 20 69 6e 20 73 71 6c in sql
220c0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c ite3_open_v2()?<
220d0 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 /todo>.**.** {H1
220e0 32 37 32 31 7d 20 54 68 65 20 5b 64 61 74 61 62 2721} The [datab
220f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
22100 63 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 created by [sqli
22110 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c te3_open_v2(F,D,
22120 47 2c 56 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 G,V)].**
22130 20 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 5b will use the [
22140 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a sqlite3_vfs] obj
22150 65 63 74 20 69 64 65 6e 74 69 66 69 65 64 20 62 ect identified b
22160 79 20 74 68 65 20 56 20 70 61 72 61 6d 65 74 65 y the V paramete
22170 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f r,.** o
22180 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 r the default [s
22190 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
221a0 63 74 20 69 66 20 56 20 69 73 20 61 20 4e 55 4c ct if V is a NUL
221b0 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a L pointer..**.**
221c0 20 7b 48 31 32 37 32 33 7d 20 54 77 6f 20 5b 64 {H12723} Two [d
221d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
221e0 6f 6e 73 5d 20 77 69 6c 6c 20 73 68 61 72 65 20 ons] will share
221f0 61 20 63 6f 6d 6d 6f 6e 20 63 61 63 68 65 20 69 a common cache i
22200 66 20 62 6f 74 68 20 77 65 72 65 0a 2a 2a 20 20 f both were.**
22210 20 20 20 20 20 20 20 20 6f 70 65 6e 65 64 20 77 opened w
22220 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 46 53 ith the same VFS
22230 20 77 68 69 6c 65 20 5b 73 68 61 72 65 64 20 63 while [shared c
22240 61 63 68 65 20 6d 6f 64 65 5d 20 77 61 73 20 65 ache mode] was e
22250 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20 20 20 nabled and.**
22260 20 20 20 20 20 20 20 69 66 20 62 6f 74 68 20 66 if both f
22270 69 6c 65 6e 61 6d 65 73 20 63 6f 6d 70 61 72 65 ilenames compare
22280 20 65 71 75 61 6c 20 75 73 69 6e 67 20 6d 65 6d equal using mem
22290 63 6d 70 28 29 20 61 66 74 65 72 20 68 61 76 69 cmp() after havi
222a0 6e 67 20 62 65 65 6e 0a 2a 2a 20 20 20 20 20 20 ng been.**
222b0 20 20 20 20 70 72 6f 63 65 73 73 65 64 20 62 79 processed by
222c0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 the [sqlite3_vf
222d0 73 20 7c 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d s | xFullPathnam
222e0 65 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 e] method of the
222f0 20 56 46 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f VFS..*/.SQLITE_
22300 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
22310 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 open(. const ch
22320 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 ar *filename,
22330 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 /* Database file
22340 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a name (UTF-8) */.
22350 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 sqlite3 **ppDb
22360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
22370 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 : SQLite db hand
22380 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f le */.);.SQLITE_
22390 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
223a0 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 open16(. const
223b0 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 void *filename,
223c0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 /* Database fi
223d0 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 20 lename (UTF-16)
223e0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 */. sqlite3 **p
223f0 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pDb /*
22400 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 OUT: SQLite db h
22410 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 andle */.);.SQLI
22420 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
22430 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f e3_open_v2(. co
22440 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 nst char *filena
22450 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 me, /* Databas
22460 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d e filename (UTF-
22470 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 8) */. sqlite3
22480 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20 **ppDb,
22490 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 /* OUT: SQLite d
224a0 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e b handle */. in
224b0 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
224c0 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a /* Flags *
224d0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
224e0 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e zVfs /* N
224f0 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c ame of VFS modul
22500 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a e to use */.);..
22510 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
22520 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 Error Codes And
22530 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 30 Messages {H12800
22540 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 2a } <S60200>.**.**
22550 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 The sqlite3_err
22560 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65 code() interface
22570 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
22580 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f 64 eric [result cod
22590 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 e] or.** [extend
225a0 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 ed result code]
225b0 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 for the most rec
225c0 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74 ent failed sqlit
225d0 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a 2a e3_* API call.**
225e0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
225f0 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e a [database con
22600 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 70 nection]. If a p
22610 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61 rior API call fa
22620 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 iled.** but the
22630 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 20 most recent API
22640 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20 call succeeded,
22650 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
22660 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 from.** sqlite3
22670 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 6e _errcode() is un
22680 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 71 defined. The sq
22690 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 lite3_extended_e
226a0 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65 rrcode().** inte
226b0 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d rface is the sam
226c0 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 e except that it
226d0 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 always returns
226e0 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 the .** [extende
226f0 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 65 d result code] e
22700 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 65 ven when extende
22710 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 d result codes a
22720 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a re.** disabled..
22730 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
22740 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 3_errmsg() and s
22750 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 qlite3_errmsg16(
22760 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 ) return English
22770 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78 -language.** tex
22780 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 t that describes
22790 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65 the error, as e
227a0 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 ither UTF-8 or U
227b0 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 65 TF-16 respective
227c0 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f ly..** Memory to
227d0 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 hold the error
227e0 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 message string i
227f0 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e s managed intern
22800 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70 ally..** The app
22810 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f lication does no
22820 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 t need to worry
22830 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68 about freeing th
22840 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77 e result..** How
22850 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 ever, the error
22860 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20 string might be
22870 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64 overwritten or d
22880 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a eallocated by.**
22890 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
228a0 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 s to other SQLit
228b0 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 e interface func
228c0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 tions..**.** Whe
228d0 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 n the serialized
228e0 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
228f0 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 20 ] is in use, it
22900 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a 20 might be the.**
22910 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63 6f case that a seco
22920 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 nd error occurs
22930 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 68 on a separate th
22940 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e 0a read in between.
22950 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 74 ** the time of t
22960 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 61 he first error a
22970 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 nd the call to t
22980 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 2e hese interfaces.
22990 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61 .** When that ha
229a0 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e ppens, the secon
229b0 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 d error will be
229c0 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74 reported since t
229d0 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 hese.** interfac
229e0 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74 es always report
229f0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
22a00 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f result. To avo
22a10 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68 id.** this, each
22a20 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61 thread can obta
22a30 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65 in exclusive use
22a40 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 of the [databas
22a50 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a e connection] D.
22a60 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 5b ** by invoking [
22a70 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
22a80 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 ter]([sqlite3_db
22a90 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 6f _mutex](D)) befo
22aa0 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 re beginning.**
22ab0 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e 76 to use D and inv
22ac0 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d oking [sqlite3_m
22ad0 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 6c utex_leave]([sql
22ae0 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 ite3_db_mutex](D
22af0 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c 20 )) after.** all
22b00 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e 74 calls to the int
22b10 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 68 erfaces listed h
22b20 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 65 ere are complete
22b30 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 d..**.** If an i
22b40 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77 nterface fails w
22b50 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 ith SQLITE_MISUS
22b60 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 E, that means th
22b70 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 77 e interface.** w
22b80 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f 72 as invoked incor
22b90 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 70 rectly by the ap
22ba0 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 74 plication. In t
22bb0 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a 2a hat case, the.**
22bc0 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 error code and
22bd0 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 6d message may or m
22be0 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a ay not be set..*
22bf0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
22c00 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 31 7d 20 .**.** {H12801}
22c10 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 The [sqlite3_err
22c20 63 6f 64 65 28 44 29 5d 20 69 6e 74 65 72 66 61 code(D)] interfa
22c30 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e ce returns the n
22c40 75 6d 65 72 69 63 0a 2a 2a 20 20 20 20 20 20 20 umeric.**
22c50 20 20 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 5d [result code]
22c60 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 72 65 or [extended re
22c70 73 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 sult code] for t
22c80 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
22c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61 69 .** fai
22ca0 6c 65 64 20 69 6e 74 65 72 66 61 63 65 20 63 61 led interface ca
22cb0 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ll associated wi
22cc0 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 th the [database
22cd0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a connection] D..
22ce0 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 32 7d 20 54 **.** {H12802} T
22cf0 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 he [sqlite3_exte
22d00 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 44 29 5d nded_errcode(D)]
22d10 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
22d20 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 0a 2a ns the numeric.*
22d30 2a 20 20 20 20 20 20 20 20 20 20 5b 65 78 74 65 * [exte
22d40 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 nded result code
22d50 5d 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 ] for the most r
22d60 65 63 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 ecently.**
22d70 20 20 20 20 66 61 69 6c 65 64 20 69 6e 74 65 72 failed inter
22d80 66 61 63 65 20 63 61 6c 6c 20 61 73 73 6f 63 69 face call associ
22d90 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 ated with the [d
22da0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
22db0 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 on] D..**.** {H1
22dc0 32 38 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 2803} The [sqlit
22dd0 65 33 5f 65 72 72 6d 73 67 28 44 29 5d 20 61 6e e3_errmsg(D)] an
22de0 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 d [sqlite3_errms
22df0 67 31 36 28 44 29 5d 0a 2a 2a 20 20 20 20 20 20 g16(D)].**
22e00 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72 interfaces r
22e10 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 eturn English-la
22e20 6e 67 75 61 67 65 20 74 65 78 74 20 74 68 61 74 nguage text that
22e30 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 20 20 describes.**
22e40 20 20 20 20 20 20 20 74 68 65 20 65 72 72 6f 72 the error
22e50 20 69 6e 20 74 68 65 20 6d 6f 73 74 6c 79 20 72 in the mostly r
22e60 65 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 69 ecently failed i
22e70 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 2c 0a 2a nterface call,.*
22e80 2a 20 20 20 20 20 20 20 20 20 20 65 6e 63 6f 64 * encod
22e90 65 64 20 61 73 20 65 69 74 68 65 72 20 55 54 46 ed as either UTF
22ea0 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73 -8 or UTF-16 res
22eb0 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a pectively..**.**
22ec0 20 7b 48 31 32 38 30 37 7d 20 54 68 65 20 73 74 {H12807} The st
22ed0 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 rings returned b
22ee0 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 y [sqlite3_errms
22ef0 67 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 g()] and [sqlite
22f00 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a 3_errmsg16()].**
22f10 20 20 20 20 20 20 20 20 20 20 61 72 65 20 76 61 are va
22f20 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 lid until the ne
22f30 78 74 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 xt SQLite interf
22f40 61 63 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 ace call..**.**
22f50 7b 48 31 32 38 30 38 7d 20 43 61 6c 6c 73 20 74 {H12808} Calls t
22f60 6f 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 74 o API routines t
22f70 68 61 74 20 64 6f 20 6e 6f 74 20 72 65 74 75 72 hat do not retur
22f80 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a n an error code.
22f90 2a 2a 20 20 20 20 20 20 20 20 20 20 28 65 78 61 ** (exa
22fa0 6d 70 6c 65 3a 20 5b 73 71 6c 69 74 65 33 5f 64 mple: [sqlite3_d
22fb0 61 74 61 5f 63 6f 75 6e 74 28 29 5d 29 20 64 6f ata_count()]) do
22fc0 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 not.**
22fd0 20 63 68 61 6e 67 65 20 74 68 65 20 65 72 72 6f change the erro
22fe0 72 20 63 6f 64 65 20 6f 72 20 6d 65 73 73 61 67 r code or messag
22ff0 65 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a e returned by.**
23000 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
23010 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b e3_errcode()], [
23020 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
23030 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 _errcode()],.**
23040 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
23050 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 6f 72 20 3_errmsg()], or
23060 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 [sqlite3_errmsg1
23070 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 6()]..**.** {H12
23080 38 30 39 7d 20 49 6e 74 65 72 66 61 63 65 73 20 809} Interfaces
23090 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 73 73 that are not ass
230a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73 ociated with a s
230b0 70 65 63 69 66 69 63 0a 2a 2a 20 20 20 20 20 20 pecific.**
230c0 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f [database co
230d0 6e 6e 65 63 74 69 6f 6e 5d 20 28 65 78 61 6d 70 nnection] (examp
230e0 6c 65 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 les:.**
230f0 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 [sqlite3_mprint
23100 66 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 f()] or [sqlite3
23110 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 _enable_shared_c
23120 61 63 68 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 ache()].**
23130 20 20 20 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 do not chang
23140 65 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 e the values ret
23150 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 urned by.**
23160 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 [sqlite3_er
23170 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 74 rcode()], [sqlit
23180 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 e3_extended_errc
23190 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 ode()],.**
231a0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 [sqlite3_err
231b0 6d 73 67 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 msg()], or [sqli
231c0 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 2e te3_errmsg16()].
231d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
231e0 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f nt sqlite3_errco
231f0 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b de(sqlite3 *db);
23200 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
23210 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
23220 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 _errcode(sqlite3
23230 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 *db);.SQLITE_AP
23240 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
23250 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c lite3_errmsg(sql
23260 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 ite3*);.SQLITE_A
23270 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
23280 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 qlite3_errmsg16(
23290 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
232a0 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 * CAPI3REF: SQL
232b0 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 Statement Object
232c0 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30 31 {H13000} <H1301
232d0 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
232e0 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d {prepared statem
232f0 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20 73 ent} {prepared s
23300 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a tatements}.**.**
23310 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
23320 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 this object repr
23330 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 esents a single
23340 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a SQL statement..*
23350 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 * This object is
23360 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e variously known
23370 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64 20 as a "prepared
23380 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 0a statement" or a.
23390 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 4c ** "compiled SQL
233a0 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 73 statement" or s
233b0 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 74 imply as a "stat
233c0 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 ement"..**.** Th
233d0 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61 74 e life of a stat
233e0 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f 65 ement object goe
233f0 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 s something like
23400 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c this:.**.** <ol
23410 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74 65 >.** <li> Create
23420 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e the object usin
23430 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 g [sqlite3_prepa
23440 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72 65 re_v2()] or a re
23450 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66 75 lated.** fu
23460 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20 nction..** <li>
23470 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 5b Bind values to [
23480 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 5d host parameters]
23490 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 using the sqlit
234a0 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20 e3_bind_*().**
234b0 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e 0a interfaces..
234c0 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65 20 ** <li> Run the
234d0 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b SQL by calling [
234e0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
234f0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 one or more time
23500 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65 74 s..** <li> Reset
23510 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 75 the statement u
23520 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 sing [sqlite3_re
23530 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20 62 set()] then go b
23540 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 73 ack.** to s
23550 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73 20 tep 2. Do this
23560 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69 6d zero or more tim
23570 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73 74 es..** <li> Dest
23580 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20 75 roy the object u
23590 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
235a0 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f nalize()]..** </
235b0 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 20 ol>.**.** Refer
235c0 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e to documentation
235d0 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6d on individual m
235e0 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f 72 ethods above for
235f0 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 additional.** i
23600 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 nformation..*/.t
23610 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
23620 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69 74 lite3_stmt sqlit
23630 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20 e3_stmt;../*.**
23640 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74 69 CAPI3REF: Run-ti
23650 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37 36 me Limits {H1276
23660 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a 0} <S20600>.**.*
23670 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
23680 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a 65 allows the size
23690 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e 73 of various cons
236a0 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69 6d tructs to be lim
236b0 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e ited.** on a con
236c0 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e 65 nection by conne
236d0 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 68 ction basis. Th
236e0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
236f0 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 74 r is the.** [dat
23700 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
23710 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 73 ] whose limit is
23720 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71 75 to be set or qu
23730 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73 eried. The.** s
23740 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
23750 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b 6c is one of the [l
23760 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 5d imit categories]
23770 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a 2a that define a.*
23780 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 74 * class of const
23790 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a 65 ructs to be size
237a0 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20 74 limited. The t
237b0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 hird parameter i
237c0 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d s the.** new lim
237d0 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 73 it for that cons
237e0 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e 63 truct. The func
237f0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
23800 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a old limit..**.*
23810 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 6d * If the new lim
23820 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 65 it is a negative
23830 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 6d number, the lim
23840 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e it is unchanged.
23850 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d 69 .** For the limi
23860 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53 51 t category of SQ
23870 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 74 LITE_LIMIT_XYZ t
23880 68 65 72 65 20 69 73 20 61 20 68 61 72 64 20 75 here is a hard u
23890 70 70 65 72 0a 2a 2a 20 62 6f 75 6e 64 20 73 65 pper.** bound se
238a0 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74 t by a compile-t
238b0 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 73 ime C preprocess
238c0 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 53 or macro named S
238d0 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 2e 0a 2a QLITE_MAX_XYZ..*
238e0 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22 * (The "_LIMIT_"
238f0 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20 in the name is
23900 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58 changed to "_MAX
23910 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73 _".).** Attempts
23920 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c to increase a l
23930 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 68 imit above its h
23940 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20 ard upper bound
23950 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20 are.** silently
23960 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65 truncated to the
23970 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 hard upper limi
23980 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d t..**.** Run tim
23990 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74 e limits are int
239a0 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e ended for use in
239b0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 applications th
239c0 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74 at manage.** bot
239d0 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65 h their own inte
239e0 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e rnal database an
239f0 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73 d also databases
23a00 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f that are contro
23a10 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75 lled.** by untru
23a20 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f sted external so
23a30 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70 urces. An examp
23a40 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d le application m
23a50 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62 ight be a.** web
23a60 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73 browser that has
23a70 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73 its own databas
23a80 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68 es for storing h
23a90 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 65 istory and.** se
23aa0 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65 73 parate databases
23ab0 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 4a controlled by J
23ac0 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 63 avaScript applic
23ad0 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 65 ations downloade
23ae0 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e 74 d.** off the Int
23af0 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 65 ernet. The inte
23b00 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 63 rnal databases c
23b10 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 0a an be given the.
23b20 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 6c ** large, defaul
23b30 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 62 t limits. Datab
23b40 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20 ases managed by
23b50 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 external sources
23b60 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 6e can.** be given
23b70 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c 69 much smaller li
23b80 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 6f mits designed to
23b90 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 61 prevent a denia
23ba0 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a 20 l of service.**
23bb0 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f 70 attack. Develop
23bc0 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 77 ers might also w
23bd0 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 5b ant to use the [
23be0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
23bf0 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e 74 orizer()].** int
23c00 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 65 erface to furthe
23c10 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 73 r control untrus
23c20 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 69 ted SQL. The si
23c30 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
23c40 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 se.** created by
23c50 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 63 an untrusted sc
23c60 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e 74 ript can be cont
23c70 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 0a ained using the.
23c80 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 ** [max_page_cou
23c90 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a nt] [PRAGMA]..**
23ca0 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d 65 .** New run-time
23cb0 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 limit categorie
23cc0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 s may be added i
23cd0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
23ce0 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 s..**.** INVARIA
23cf0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 NTS:.**.** {H127
23d00 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 62} A successful
23d10 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
23d20 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 3_limit(D,C,V)]
23d30 77 68 65 72 65 20 56 20 69 73 0a 2a 2a 20 20 20 where V is.**
23d40 20 20 20 20 20 20 20 70 6f 73 69 74 69 76 65 20 positive
23d50 63 68 61 6e 67 65 73 20 74 68 65 20 6c 69 6d 69 changes the limi
23d60 74 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 t on the size of
23d70 20 63 6f 6e 73 74 72 75 63 74 20 43 20 69 6e 20 construct C in
23d80 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
23d90 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
23da0 74 69 6f 6e 5d 20 44 20 74 6f 20 74 68 65 20 6c tion] D to the l
23db0 65 73 73 65 72 20 6f 66 20 56 20 61 6e 64 20 74 esser of V and t
23dc0 68 65 20 68 61 72 64 20 75 70 70 65 72 0a 2a 2a he hard upper.**
23dd0 20 20 20 20 20 20 20 20 20 20 62 6f 75 6e 64 20 bound
23de0 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 43 on the size of C
23df0 20 74 68 61 74 20 69 73 20 73 65 74 20 61 74 20 that is set at
23e00 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2a compile-time..**
23e10 0a 2a 2a 20 7b 48 31 32 37 36 36 7d 20 41 20 73 .** {H12766} A s
23e20 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
23e30 6f 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 o [sqlite3_limit
23e40 28 44 2c 43 2c 56 29 5d 20 77 68 65 72 65 20 56 (D,C,V)] where V
23e50 20 69 73 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 is negative.**
23e60 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73 20 leaves
23e70 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
23e80 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
23e90 63 74 69 6f 6e 5d 20 44 20 75 6e 63 68 61 6e 67 ction] D unchang
23ea0 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 36 ed..**.** {H1276
23eb0 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 9} A successful
23ec0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
23ed0 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 72 _limit(D,C,V)] r
23ee0 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 20 20 eturns the.**
23ef0 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 value of
23f00 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 the limit on the
23f10 20 73 69 7a 65 20 6f 66 20 63 6f 6e 73 74 72 75 size of constru
23f20 63 74 20 43 20 69 6e 20 74 68 65 0a 2a 2a 20 20 ct C in the.**
23f30 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 [databas
23f40 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 e connection] D
23f50 61 73 20 69 74 20 77 61 73 20 70 72 69 6f 72 20 as it was prior
23f60 74 6f 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2f 0a to the call..*/.
23f70 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
23f80 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 6c qlite3_limit(sql
23f90 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20 69 ite3*, int id, i
23fa0 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a 0a nt newVal);../*.
23fb0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e ** CAPI3REF: Run
23fc0 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74 65 -Time Limit Cate
23fd0 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d 20 gories {H12790}
23fe0 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59 57 <H12760>.** KEYW
23ff0 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61 74 ORDS: {limit cat
24000 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63 61 egory} {limit ca
24010 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a 20 tegories}.**.**
24020 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 These constants
24030 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 61 define various a
24040 73 70 65 63 74 73 20 6f 66 20 61 20 5b 64 61 74 spects of a [dat
24050 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
24060 5d 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 ].** that can be
24070 20 6c 69 6d 69 74 65 64 20 69 6e 20 73 69 7a 65 limited in size
24080 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 by calls to [sq
24090 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e 0a lite3_limit()]..
240a0 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 ** The meanings
240b0 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c of the various l
240c0 69 6d 69 74 73 20 61 72 65 20 61 73 20 66 6f 6c imits are as fol
240d0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e lows:.**.** <dl>
240e0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
240f0 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e IMIT_LENGTH</dt>
24100 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
24110 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 mum size of any
24120 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f string or BLOB o
24130 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e r table row.<dd>
24140 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
24150 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 E_LIMIT_SQL_LENG
24160 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 TH</dt>.** <dd>T
24170 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
24180 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 h of an SQL stat
24190 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ement.</dd>.**.*
241a0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d * <dt>SQLITE_LIM
241b0 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a IT_COLUMN</dt>.*
241c0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
241d0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 m number of colu
241e0 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 mns in a table d
241f0 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 efinition or in
24200 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 the.** result se
24210 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 6f 72 t of a SELECT or
24220 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d the maximum num
24230 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
24240 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72 n an index.** or
24250 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 in an ORDER BY
24260 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 or GROUP BY clau
24270 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c se.</dd>.**.** <
24280 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
24290 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a EXPR_DEPTH</dt>.
242a0 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
242b0 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 um depth of the
242c0 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e parse tree on an
242d0 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64 y expression.</d
242e0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
242f0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 ITE_LIMIT_COMPOU
24300 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a ND_SELECT</dt>.*
24310 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
24320 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d m number of term
24330 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 s in a compound
24340 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
24350 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
24360 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 >SQLITE_LIMIT_VD
24370 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 BE_OP</dt>.** <d
24380 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 d>The maximum nu
24390 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74 mber of instruct
243a0 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61 ions in a virtua
243b0 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61 l machine progra
243c0 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70 m.** used to imp
243d0 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74 lement an SQL st
243e0 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a atement.</dd>.**
243f0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
24400 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 IMIT_FUNCTION_AR
24410 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 G</dt>.** <dd>Th
24420 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
24430 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e of arguments on
24440 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64 a function.</dd
24450 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
24460 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 TE_LIMIT_ATTACHE
24470 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
24480 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
24490 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 74 of attached dat
244a0 61 62 61 73 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a abases.</dd>.**.
244b0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
244c0 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e MIT_LIKE_PATTERN
244d0 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 _LENGTH</dt>.**
244e0 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
244f0 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61 length of the pa
24500 74 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 ttern argument t
24510 6f 20 74 68 65 20 4c 49 4b 45 20 6f 72 0a 2a 2a o the LIKE or.**
24520 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e GLOB operators.
24530 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
24540 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
24550 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 IABLE_NUMBER</dt
24560 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
24570 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 imum number of v
24580 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 ariables in an S
24590 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 QL statement tha
245a0 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e t can.** be boun
245b0 64 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e d.</dd>.** </dl>
245c0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
245d0 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20 TE_LIMIT_LENGTH
245e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
245f0 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
24600 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 ITE_LIMIT_SQL_LE
24610 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20 20 NGTH
24620 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
24630 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d LITE_LIMIT_COLUM
24640 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
24650 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 2.#define S
24660 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 QLITE_LIMIT_EXPR
24670 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20 20 _DEPTH
24680 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 3.#define
24690 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d SQLITE_LIMIT_COM
246a0 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 20 20 POUND_SELECT
246b0 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 4.#define
246c0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 SQLITE_LIMIT_VD
246d0 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 20 20 BE_OP
246e0 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 5.#defin
246f0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 e SQLITE_LIMIT_F
24700 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20 20 UNCTION_ARG
24710 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6.#defi
24720 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
24730 41 54 54 41 43 48 45 44 20 20 20 20 20 20 20 20 ATTACHED
24740 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 7.#def
24750 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
24760 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 _LIKE_PATTERN_LE
24770 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 64 65 NGTH 8.#de
24780 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
24790 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
247a0 52 20 20 20 20 20 20 20 20 20 20 20 39 0a 0a 2f R 9../
247b0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
247c0 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20 ompiling An SQL
247d0 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 31 Statement {H1301
247e0 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b 0} <S10000>.** K
247f0 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74 EYWORDS: {SQL st
24800 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 atement compiler
24810 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75 }.**.** To execu
24820 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2c te an SQL query,
24830 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20 62 it must first b
24840 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20 e compiled into
24850 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70 a byte-code.** p
24860 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65 rogram using one
24870 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e of these routin
24880 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 es..**.** The fi
24890 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 64 rst argument, "d
248a0 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 62 61 b", is a [databa
248b0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f se connection] o
248c0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a btained from a.*
248d0 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 * prior call to
248e0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
248f0 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f , [sqlite3_open_
24900 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 v2()] or [sqlite
24910 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a 3_open16()]..**.
24920 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 ** The second ar
24930 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20 gument, "zSql",
24940 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 is the statement
24950 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c to be compiled,
24960 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 encoded.** as e
24970 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 ither UTF-8 or U
24980 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69 TF-16. The sqli
24990 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e te3_prepare() an
249a0 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 d sqlite3_prepar
249b0 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 e_v2().** interf
249c0 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20 aces use UTF-8,
249d0 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 and sqlite3_prep
249e0 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69 are16() and sqli
249f0 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 te3_prepare16_v2
24a00 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 ().** use UTF-16
24a10 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e ..**.** If the n
24a20 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73 Byte argument is
24a30 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c less than zero,
24a40 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65 then zSql is re
24a50 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20 ad up to the.**
24a60 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 first zero termi
24a70 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20 nator. If nByte
24a80 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c is non-negative,
24a90 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 then it is the
24aa0 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65 maximum.** numbe
24ab0 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64 r of bytes read
24ac0 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65 from zSql. Whe
24ad0 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e n nByte is non-n
24ae0 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20 egative, the.**
24af0 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73 zSql string ends
24b00 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66 at either the f
24b10 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27 irst '\000' or '
24b20 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65 \u0000' characte
24b30 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74 r or.** the nByt
24b40 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68 e-th byte, which
24b50 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 ever comes first
24b60 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 . If the caller
24b70 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68 knows.** that th
24b80 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e e supplied strin
24b90 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 g is nul-termina
24ba0 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 ted, then there
24bb0 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65 is a small.** pe
24bc0 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74 rformance advant
24bd0 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64 age to be gained
24be0 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e by passing an n
24bf0 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74 Byte parameter t
24c00 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 hat.** is equal
24c10 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
24c20 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e bytes in the in
24c30 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e put string <i>in
24c40 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74 cluding</i>.** t
24c50 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f he nul-terminato
24c60 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 2a r bytes..**.** *
24c70 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 pzTail is made t
24c80 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 o point to the f
24c90 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 irst byte past t
24ca0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a he end of the.**
24cb0 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65 first SQL state
24cc0 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54 ment in zSql. T
24cd0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e hese routines on
24ce0 6c 79 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 ly compile the f
24cf0 69 72 73 74 0a 2a 2a 20 73 74 61 74 65 6d 65 6e irst.** statemen
24d00 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70 t in zSql, so *p
24d10 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f zTail is left po
24d20 69 6e 74 69 6e 67 20 74 6f 20 77 68 61 74 20 72 inting to what r
24d30 65 6d 61 69 6e 73 0a 2a 2a 20 75 6e 63 6f 6d 70 emains.** uncomp
24d40 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 iled..**.** *ppS
24d50 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e tmt is left poin
24d60 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c ting to a compil
24d70 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ed [prepared sta
24d80 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e tement] that can
24d90 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 be.** executed
24da0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 using [sqlite3_s
24db0 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72 tep()]. If ther
24dc0 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a e is an error, *
24dd0 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a ppStmt is set.**
24de0 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 to NULL. If th
24df0 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e e input text con
24e00 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 tains no SQL (if
24e10 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e the input is an
24e20 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67 empty.** string
24e30 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74 or a comment) t
24e40 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73 hen *ppStmt is s
24e50 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 7b et to NULL..** {
24e60 41 31 33 30 31 38 7d 20 54 68 65 20 63 61 6c 6c A13018} The call
24e70 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 73 ing procedure is
24e80 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 responsible for
24e90 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f deleting the co
24ea0 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73 74 mpiled.** SQL st
24eb0 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b 73 atement using [s
24ec0 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
24ed0 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73 20 )] after it has
24ee0 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 finished with it
24ef0 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 ..**.** On succe
24f00 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 ss, [SQLITE_OK]
24f10 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 74 68 is returned, oth
24f20 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72 erwise an [error
24f30 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
24f40 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ed..**.** The sq
24f50 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
24f60 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 () and sqlite3_p
24f70 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 69 6e repare16_v2() in
24f80 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a 2a 20 terfaces are.**
24f90 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 recommended for
24fa0 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d 73 all new programs
24fb0 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 72 20 . The two older
24fc0 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 72 interfaces are r
24fd0 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 etained.** for b
24fe0 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
24ff0 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 65 69 bility, but thei
25000 72 20 75 73 65 20 69 73 20 64 69 73 63 6f 75 72 r use is discour
25010 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 aged..** In the
25020 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73 2c "v2" interfaces,
25030 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 the prepared st
25040 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20 atement.** that
25050 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68 65 is returned (the
25060 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 [sqlite3_stmt]
25070 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e 73 object) contains
25080 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a a copy of the.*
25090 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 * original SQL t
250a0 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 65 73 ext. This causes
250b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 the [sqlite3_st
250c0 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 ep()] interface
250d0 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 64 to.** behave a d
250e0 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 77 ifferently in tw
250f0 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f o ways:.**.** <o
25100 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 l>.** <li>.** If
25110 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
25120 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 6e hema changes, in
25130 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e 69 stead of returni
25140 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d ng [SQLITE_SCHEM
25150 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61 A] as it.** alwa
25160 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b ys used to do, [
25170 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
25180 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c will automatical
25190 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 65 ly recompile the
251a0 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e SQL.** statemen
251b0 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 6e t and try to run
251c0 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 74 it again. If t
251d0 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 he schema has ch
251e0 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61 anged in.** a wa
251f0 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 y that makes the
25200 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f statement no lo
25210 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c nger valid, [sql
25220 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c ite3_step()] wil
25230 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 l still.** retur
25240 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 n [SQLITE_SCHEMA
25250 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 74 ]. But unlike t
25260 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69 he legacy behavi
25270 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 or, [SQLITE_SCHE
25280 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 MA] is.** now a
25290 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 61 fatal error. Ca
252a0 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 lling [sqlite3_p
252b0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67 61 repare_v2()] aga
252c0 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 in will not make
252d0 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f the.** error go
252e0 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73 away. Note: us
252f0 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 e [sqlite3_errms
25300 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68 65 g()] to find the
25310 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20 text.** of the
25320 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 74 68 parsing error th
25330 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e at results in an
25340 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d [SQLITE_SCHEMA]
25350 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 return..** </li
25360 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 >.**.** <li>.**
25370 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 When an error oc
25380 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73 curs, [sqlite3_s
25390 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 tep()] will retu
253a0 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 rn one of the de
253b0 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 tailed.** [error
253c0 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74 65 codes] or [exte
253d0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 nded error codes
253e0 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 20 62 ]. The legacy b
253f0 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61 74 ehavior was that
25400 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 .** [sqlite3_ste
25410 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 p()] would only
25420 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 69 63 return a generic
25430 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
25440 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 result code.** a
25450 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 61 76 nd you would hav
25460 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f e to make a seco
25470 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 nd call to [sqli
25480 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e 20 te3_reset()] in
25490 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64 order.** to find
254a0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
254b0 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72 6f cause of the pro
254c0 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 20 22 blem. With the "
254d0 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a 20 69 v2" prepare.** i
254e0 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 75 nterfaces, the u
254f0 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e nderlying reason
25500 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 69 for the error i
25510 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 s returned immed
25520 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e iately..** </li>
25530 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ol>.**.**
25540 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
25550 2a 20 7b 48 31 33 30 31 31 7d 20 54 68 65 20 5b * {H13011} The [
25560 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
25570 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 61 6e db,zSql,...)] an
25580 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 d.** [s
25590 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
255a0 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 2(db,zSql,...)]
255b0 69 6e 74 65 72 66 61 63 65 73 20 69 6e 74 65 72 interfaces inter
255c0 70 72 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 20 pret the.**
255d0 20 20 20 20 20 74 65 78 74 20 69 6e 20 74 68 65 text in the
255e0 69 72 20 7a 53 71 6c 20 70 61 72 61 6d 65 74 65 ir zSql paramete
255f0 72 20 61 73 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a r as UTF-8..**.*
25600 2a 20 7b 48 31 33 30 31 32 7d 20 54 68 65 20 5b * {H13012} The [
25610 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
25620 36 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 6(db,zSql,...)]
25630 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
25640 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
25650 31 36 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 16_v2(db,zSql,..
25660 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 69 .)] interfaces i
25670 6e 74 65 72 70 72 65 74 20 74 68 65 0a 2a 2a 20 nterpret the.**
25680 20 20 20 20 20 20 20 20 20 74 65 78 74 20 69 6e text in
25690 20 74 68 65 69 72 20 7a 53 71 6c 20 70 61 72 61 their zSql para
256a0 6d 65 74 65 72 20 61 73 20 55 54 46 2d 31 36 20 meter as UTF-16
256b0 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 in the native by
256c0 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 te order..**.**
256d0 7b 48 31 33 30 31 33 7d 20 49 66 20 74 68 65 20 {H13013} If the
256e0 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 74 nByte argument t
256f0 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 o [sqlite3_prepa
25700 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 6e 42 re_v2(db,zSql,nB
25710 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 yte,...)].**
25720 20 20 20 20 20 20 61 6e 64 20 69 74 73 20 76 61 and its va
25730 72 69 61 6e 74 73 20 69 73 20 6c 65 73 73 20 74 riants is less t
25740 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 20 53 51 han zero, the SQ
25750 4c 20 74 65 78 74 20 69 73 0a 2a 2a 20 20 20 20 L text is.**
25760 20 20 20 20 20 20 72 65 61 64 20 66 72 6f 6d 20 read from
25770 7a 53 71 6c 20 69 73 20 72 65 61 64 20 75 70 20 zSql is read up
25780 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 to the first zer
25790 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a o terminator..**
257a0 0a 2a 2a 20 7b 48 31 33 30 31 34 7d 20 49 66 20 .** {H13014} If
257b0 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 the nByte argume
257c0 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 nt to [sqlite3_p
257d0 72 65 70 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 repare_v2(db,zSq
257e0 6c 2c 6e 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a l,nByte,...)].**
257f0 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 69 74 and it
25800 73 20 76 61 72 69 61 6e 74 73 20 69 73 20 6e 6f s variants is no
25810 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e n-negative, then
25820 20 61 74 20 6d 6f 73 74 20 6e 42 79 74 65 73 20 at most nBytes
25830 62 79 74 65 73 20 6f 66 0a 2a 2a 20 20 20 20 20 bytes of.**
25840 20 20 20 20 20 53 51 4c 20 74 65 78 74 20 69 73 SQL text is
25850 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e read from zSql.
25860 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30 31 35 7d 20 .**.** {H13015}
25870 49 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 In [sqlite3_prep
25880 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 4e are_v2(db,zSql,N
25890 2c 50 2c 70 7a 54 61 69 6c 29 5d 20 61 6e 64 20 ,P,pzTail)] and
258a0 69 74 73 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20 its variants.**
258b0 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20 if the
258c0 7a 53 71 6c 20 69 6e 70 75 74 20 74 65 78 74 20 zSql input text
258d0 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 contains more th
258e0 61 6e 20 6f 6e 65 20 53 51 4c 20 73 74 61 74 65 an one SQL state
258f0 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 ment.**
25900 20 61 6e 64 20 70 7a 54 61 69 6c 20 69 73 20 6e and pzTail is n
25910 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70 ot NULL, then *p
25920 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 6f zTail is made to
25930 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a point to the.**
25940 20 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 first
25950 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e byte past the en
25960 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 53 d of the first S
25970 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 QL statement in
25980 7a 53 71 6c 2e 0a 2a 2a 20 20 20 20 20 20 20 20 zSql..**
25990 20 20 3c 74 6f 64 6f 3e 57 68 61 74 20 64 6f 65 <todo>What doe
259a0 73 20 2a 70 7a 54 61 69 6c 20 70 6f 69 6e 74 20 s *pzTail point
259b0 74 6f 20 69 66 20 74 68 65 72 65 20 69 73 20 6f to if there is o
259c0 6e 65 20 73 74 61 74 65 6d 65 6e 74 3f 3c 2f 74 ne statement?</t
259d0 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30 odo>.**.** {H130
259e0 31 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 16} A successful
259f0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
25a00 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 3_prepare_v2(db,
25a10 7a 53 71 6c 2c 4e 2c 70 70 53 74 6d 74 2c 2e 2e zSql,N,ppStmt,..
25a20 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .)].**
25a30 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 or one of its va
25a40 72 69 61 6e 74 73 20 77 72 69 74 65 73 20 69 6e riants writes in
25a50 74 6f 20 2a 70 70 53 74 6d 74 20 61 20 70 6f 69 to *ppStmt a poi
25a60 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 0a 2a 2a nter to a new.**
25a70 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
25a80 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f red statement] o
25a90 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 4e r a pointer to N
25aa0 55 4c 4c 20 69 66 20 7a 53 71 6c 20 63 6f 6e 74 ULL if zSql cont
25ab0 61 69 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ains.**
25ac0 20 6e 6f 74 68 69 6e 67 20 6f 74 68 65 72 20 74 nothing other t
25ad0 68 61 6e 20 77 68 69 74 65 73 70 61 63 65 20 6f han whitespace o
25ae0 72 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a r comments..**.*
25af0 2a 20 7b 48 31 33 30 31 39 7d 20 54 68 65 20 5b * {H13019} The [
25b00 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
25b10 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 v2()] interface
25b20 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 and its variants
25b30 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 return.**
25b40 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 [SQLITE_OK]
25b50 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 or an appropriat
25b60 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 75 e [error code] u
25b70 70 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a pon failure..**.
25b80 2a 2a 20 7b 48 31 33 30 32 31 7d 20 42 65 66 6f ** {H13021} Befo
25b90 72 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 re [sqlite3_prep
25ba0 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 are(db,zSql,nByt
25bb0 65 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 e,ppStmt,pzTail)
25bc0 5d 20 6f 72 20 69 74 73 0a 2a 2a 20 20 20 20 20 ] or its.**
25bd0 20 20 20 20 20 76 61 72 69 61 6e 74 73 20 72 65 variants re
25be0 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 28 turns an error (
25bf0 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20 any value other
25c00 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d than [SQLITE_OK]
25c10 29 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 ),.** t
25c20 68 65 79 20 66 69 72 73 74 20 73 65 74 20 2a 70 hey first set *p
25c30 70 53 74 6d 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a pStmt to NULL..*
25c40 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
25c50 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
25c60 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
25c70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
25c80 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
25c90 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
25ca0 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 zSql, /* S
25cb0 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 QL statement, UT
25cc0 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 F-8 encoded */.
25cd0 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 int nByte,
25ce0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
25cf0 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 mum length of zS
25d00 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a ql in bytes. */.
25d10 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
25d20 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 *ppStmt, /* OUT
25d30 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 : Statement hand
25d40 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 le */. const ch
25d50 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 ar **pzTail
25d60 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 /* OUT: Pointer
25d70 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f to unused portio
25d80 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a n of zSql */.);.
25d90 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
25da0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
25db0 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2(. sqlite3 *db
25dc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
25dd0 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 Database handle
25de0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
25df0 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 *zSql, /*
25e00 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 SQL statement, U
25e10 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a TF-8 encoded */.
25e20 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
25e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
25e40 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a imum length of z
25e50 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f Sql in bytes. */
25e60 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
25e70 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 **ppStmt, /* OU
25e80 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e T: Statement han
25e90 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 dle */. const c
25ea0 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 har **pzTail
25eb0 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 /* OUT: Pointer
25ec0 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 to unused porti
25ed0 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b on of zSql */.);
25ee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
25ef0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
25f00 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 6(. sqlite3 *db
25f10 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
25f20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 Database handle
25f30 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
25f40 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 *zSql, /*
25f50 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 SQL statement, U
25f60 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f TF-16 encoded */
25f70 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 . int nByte,
25f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
25f90 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
25fa0 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a zSql in bytes. *
25fb0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 /. sqlite3_stmt
25fc0 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f **ppStmt, /* O
25fd0 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 UT: Statement ha
25fe0 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
25ff0 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 void **pzTail
26000 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 /* OUT: Pointe
26010 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 r to unused port
26020 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 ion of zSql */.)
26030 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
26040 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
26050 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 16_v2(. sqlite3
26060 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
26070 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e /* Database han
26080 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 dle */. const v
26090 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 oid *zSql,
260a0 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e /* SQL statemen
260b0 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 t, UTF-16 encode
260c0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 d */. int nByte
260d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
260e0 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
260f0 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 of zSql in byte
26100 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f s. */. sqlite3_
26110 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 stmt **ppStmt,
26120 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e /* OUT: Statemen
26130 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f t handle */. co
26140 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 nst void **pzTai
26150 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f l /* OUT: Po
26160 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 inter to unused
26170 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 portion of zSql
26180 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.);../*.** CAP
26190 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 69 6e I3REF: Retrievin
261a0 67 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 g Statement SQL
261b0 7b 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30 30 {H13100} <H13000
261c0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 >.**.** This int
261d0 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 erface can be us
261e0 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 ed to retrieve a
261f0 20 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20 74 saved copy of t
26200 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 he original.** S
26210 51 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f 20 QL text used to
26220 63 72 65 61 74 65 20 61 20 5b 70 72 65 70 61 72 create a [prepar
26230 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 ed statement] if
26240 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 that statement
26250 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 was.** compiled
26260 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 using either [sq
26270 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
26280 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
26290 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e prepare16_v2()].
262a0 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
262b0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 30 31 S:.**.** {H13101
262c0 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70 61 72 } If the [prepar
262d0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 70 61 ed statement] pa
262e0 73 73 65 64 20 61 73 20 74 68 65 20 61 72 67 75 ssed as the argu
262f0 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 ment to.**
26300 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c [sqlite3_sql
26310 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 ()] was compiled
26320 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 using either [s
26330 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
26340 32 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 2()] or.**
26350 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 [sqlite3_pre
26360 70 61 72 65 31 36 5f 76 32 28 29 5d 2c 20 74 68 pare16_v2()], th
26370 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c 28 en [sqlite3_sql(
26380 29 5d 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 )] returns.**
26390 20 20 20 20 20 20 20 61 20 70 6f 69 6e 74 65 72 a pointer
263a0 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 to a zero-termi
263b0 6e 61 74 65 64 20 73 74 72 69 6e 67 20 63 6f 6e nated string con
263c0 74 61 69 6e 69 6e 67 20 61 20 55 54 46 2d 38 20 taining a UTF-8
263d0 72 65 6e 64 65 72 69 6e 67 0a 2a 2a 20 20 20 20 rendering.**
263e0 20 20 20 20 20 20 6f 66 20 74 68 65 20 6f 72 69 of the ori
263f0 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d ginal SQL statem
26400 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 ent..**.** {H131
26410 30 32 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70 02} If the [prep
26420 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
26430 70 61 73 73 65 64 20 61 73 20 74 68 65 20 61 72 passed as the ar
26440 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 gument to.**
26450 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 [sqlite3_s
26460 71 6c 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c ql()] was compil
26470 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 ed using either
26480 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
26490 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 ()] or.**
264a0 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 [sqlite3_prep
264b0 61 72 65 31 36 28 29 5d 2c 20 74 68 65 6e 20 5b are16()], then [
264c0 73 71 6c 69 74 65 33 5f 73 71 6c 28 29 5d 20 72 sqlite3_sql()] r
264d0 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f eturns a NULL po
264e0 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 inter..**.** {H1
264f0 33 31 30 33 7d 20 54 68 65 20 73 74 72 69 6e 67 3103} The string
26500 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 returned by [sq
26510 6c 69 74 65 33 5f 73 71 6c 28 53 29 5d 20 69 73 lite3_sql(S)] is
26520 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 valid until the
26530 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 .** [pr
26540 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
26550 5d 20 53 20 69 73 20 64 65 6c 65 74 65 64 20 75 ] S is deleted u
26560 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
26570 6e 61 6c 69 7a 65 28 53 29 5d 2e 0a 2a 2f 0a 53 nalize(S)]..*/.S
26580 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
26590 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 char *sqlite3_sq
265a0 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a l(sqlite3_stmt *
265b0 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
265c0 41 50 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 63 API3REF: Dynamic
265d0 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65 ally Typed Value
265e0 20 4f 62 6a 65 63 74 20 7b 48 31 35 30 30 30 7d Object {H15000}
265f0 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 <S20200>.** KEY
26600 57 4f 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 65 WORDS: {protecte
26610 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d d sqlite3_value}
26620 20 7b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 {unprotected sq
26630 6c 69 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a lite3_value}.**.
26640 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 ** SQLite uses t
26650 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 he sqlite3_value
26660 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 70 72 65 object to repre
26670 73 65 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a sent all values.
26680 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 73 ** that can be s
26690 74 6f 72 65 64 20 69 6e 20 61 20 64 61 74 61 62 tored in a datab
266a0 61 73 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 74 ase table. SQLit
266b0 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20 74 e uses dynamic t
266c0 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65 yping.** for the
266d0 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65 values it store
266e0 73 2e 20 56 61 6c 75 65 73 20 73 74 6f 72 65 64 s. Values stored
266f0 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 in sqlite3_valu
26700 65 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e e objects.** can
26710 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c be integers, fl
26720 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
26730 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c ues, strings, BL
26740 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a OBs, or NULL..**
26750 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76 .** An sqlite3_v
26760 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20 alue object may
26770 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74 65 be either "prote
26780 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74 cted" or "unprot
26790 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 ected"..** Some
267a0 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69 interfaces requi
267b0 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 re a protected s
267c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f qlite3_value. O
267d0 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a ther interfaces.
267e0 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65 ** will accept e
267f0 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74 65 ither a protecte
26800 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 d or an unprotec
26810 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
26820 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65 e..** Every inte
26830 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65 70 rface that accep
26840 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ts sqlite3_value
26850 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 arguments speci
26860 66 69 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 20 fies.** whether
26870 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69 72 or not it requir
26880 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 es a protected s
26890 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a qlite3_value..**
268a0 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22 70 .** The terms "p
268b0 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22 75 rotected" and "u
268c0 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66 65 nprotected" refe
268d0 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72 20 r to whether or
268e0 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69 not.** a mutex i
268f0 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 72 s held. A inter
26900 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c nal mutex is hel
26910 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74 65 d for a protecte
26920 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c d.** sqlite3_val
26930 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f ue object but no
26940 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 mutex is held f
26950 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 or an unprotecte
26960 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c d.** sqlite3_val
26970 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 53 ue object. If S
26980 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
26990 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74 d to be single-t
269a0 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 68 hreaded.** (with
269b0 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 [SQLITE_THREADS
269c0 41 46 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20 AFE=0] and with
269d0 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 [sqlite3_threads
269e0 61 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 afe()] returning
269f0 20 30 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c 0).** or if SQL
26a00 69 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e ite is run in on
26a10 65 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74 e of reduced mut
26a20 65 78 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 ex modes .** [SQ
26a30 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 LITE_CONFIG_SING
26a40 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51 LETHREAD] or [SQ
26a50 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 LITE_CONFIG_MULT
26a60 49 54 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e ITHREAD].** then
26a70 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 73 there is no dis
26a80 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e tinction between
26a90 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75 protected and u
26aa0 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 nprotected.** sq
26ab0 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
26ac0 63 74 73 20 61 6e 64 20 74 68 65 79 20 63 61 6e cts and they can
26ad0 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63 68 be used interch
26ae0 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 76 angeably. Howev
26af0 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d er,.** for maxim
26b00 75 6d 20 63 6f 64 65 20 70 6f 72 74 61 62 69 6c um code portabil
26b10 69 74 79 20 69 74 20 69 73 20 72 65 63 6f 6d 6d ity it is recomm
26b20 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 ended that appli
26b30 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c cations.** still
26b40 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e make the distin
26b50 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 65 ction between be
26b60 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 tween protected
26b70 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a and unprotected.
26b80 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
26b90 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68 objects even wh
26ba0 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 en not strictly
26bb0 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 required..**.**
26bc0 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 The sqlite3_valu
26bd0 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 e objects that a
26be0 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 72 re passed as par
26bf0 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65 ameters into the
26c00 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
26c10 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 69 on of [applicati
26c20 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 on-defined SQL f
26c30 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 72 unctions] are pr
26c40 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20 otected..** The
26c50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
26c60 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 ject returned by
26c70 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c .** [sqlite3_col
26c80 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 umn_value()] is
26c90 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 unprotected..**
26ca0 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 Unprotected sqli
26cb0 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
26cc0 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 s may only be us
26cd0 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 ed with.** [sqli
26ce0 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
26cf0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
26d00 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a _bind_value()]..
26d10 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
26d20 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c value_blob | sql
26d30 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
26d40 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 )] family of.**
26d50 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69 interfaces requi
26d60 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c re protected sql
26d70 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
26d80 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ts..*/.typedef s
26d90 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65 truct Mem sqlite
26da0 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 3_value;../*.**
26db0 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 46 75 CAPI3REF: SQL Fu
26dc0 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f nction Context O
26dd0 62 6a 65 63 74 20 7b 48 31 36 30 30 31 7d 20 3c bject {H16001} <
26de0 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20200>.**.** Th
26df0 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 e context in whi
26e00 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 ch an SQL functi
26e10 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73 on executes is s
26e20 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 tored in an.** s
26e30 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f qlite3_context o
26e40 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65 bject. A pointe
26e50 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f r to an sqlite3_
26e60 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a context object.*
26e70 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 73 * is always firs
26e80 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b t parameter to [
26e90 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
26ea0 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ned SQL function
26eb0 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 s]..** The appli
26ec0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 cation-defined S
26ed0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c QL function impl
26ee0 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 ementation will
26ef0 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69 pass this.** poi
26f00 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e 74 nter through int
26f10 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 o calls to [sqli
26f20 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c te3_result_int |
26f30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 28 sqlite3_result(
26f40 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f )],.** [sqlite3_
26f50 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
26f60 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75 t()], [sqlite3_u
26f70 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 ser_data()],.**
26f80 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 [sqlite3_context
26f90 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b _db_handle()], [
26fa0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
26fb0 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f ata()],.** and/o
26fc0 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 r [sqlite3_set_a
26fd0 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 uxdata()]..*/.ty
26fe0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
26ff0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c ite3_context sql
27000 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f ite3_context;../
27010 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 42 *.** CAPI3REF: B
27020 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54 6f inding Values To
27030 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d Prepared Statem
27040 65 6e 74 73 20 7b 48 31 33 35 30 30 7d 20 3c 53 ents {H13500} <S
27050 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 70300>.** KEYWOR
27060 44 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 DS: {host parame
27070 74 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d ter} {host param
27080 65 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 eters} {host par
27090 61 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 ameter name}.**
270a0 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 KEYWORDS: {SQL p
270b0 61 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 arameter} {SQL p
270c0 61 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 arameters} {para
270d0 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a meter binding}.*
270e0 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c 20 *.** In the SQL
270f0 73 74 72 69 6e 67 73 20 69 6e 70 75 74 20 74 6f strings input to
27100 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
27110 65 5f 76 32 28 29 5d 20 61 6e 64 20 69 74 73 20 e_v2()] and its
27120 76 61 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 variants,.** lit
27130 65 72 61 6c 73 20 6d 61 79 20 62 65 20 72 65 70 erals may be rep
27140 6c 61 63 65 64 20 62 79 20 61 20 70 61 72 61 6d laced by a param
27150 65 74 65 72 20 69 6e 20 6f 6e 65 20 6f 66 20 74 eter in one of t
27160 68 65 73 65 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a hese forms:.**.*
27170 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
27180 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a ?.** <li> ?NNN.
27190 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a ** <li> :VVV.**
271a0 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c <li> @VVV.** <
271b0 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 li> $VVV.** </u
271c0 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 l>.**.** In the
271d0 70 61 72 61 6d 65 74 65 72 20 66 6f 72 6d 73 20 parameter forms
271e0 73 68 6f 77 6e 20 61 62 6f 76 65 20 4e 4e 4e 20 shown above NNN
271f0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c 69 is an integer li
27200 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 teral,.** and VV
27210 56 20 69 73 20 61 6e 20 61 6c 70 68 61 2d 6e 75 V is an alpha-nu
27220 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65 72 20 meric parameter
27230 6e 61 6d 65 2e 20 54 68 65 20 76 61 6c 75 65 73 name. The values
27240 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 61 72 of these.** par
27250 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63 61 ameters (also ca
27260 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61 6d lled "host param
27270 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 eter names" or "
27280 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 22 29 SQL parameters")
27290 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 75 .** can be set u
272a0 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 sing the sqlite3
272b0 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e _bind_*() routin
272c0 65 73 20 64 65 66 69 6e 65 64 20 68 65 72 65 2e es defined here.
272d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
272e0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
272f0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 sqlite3_bind_*(
27300 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20 61 6c ) routines is al
27310 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 ways.** a pointe
27320 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 r to the [sqlite
27330 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72 3_stmt] object r
27340 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 eturned from.**
27350 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
27360 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76 61 _v2()] or its va
27370 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 riants..**.** Th
27380 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
27390 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f t is the index o
273a0 66 20 74 68 65 20 53 51 4c 20 70 61 72 61 6d 65 f the SQL parame
273b0 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a ter to be set..*
273c0 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 53 * The leftmost S
273d0 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 QL parameter has
273e0 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 20 an index of 1.
273f0 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65 20 6e When the same n
27400 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61 amed.** SQL para
27410 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 6d 6f meter is used mo
27420 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65 re than once, se
27430 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 cond and subsequ
27440 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63 ent.** occurrenc
27450 65 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 es have the same
27460 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 66 69 index as the fi
27470 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a rst occurrence..
27480 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72 ** The index for
27490 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 named parameter
274a0 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 s can be looked
274b0 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 up using the.**
274c0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
274d0 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d rameter_index()]
274e0 20 41 50 49 20 69 66 20 64 65 73 69 72 65 64 2e API if desired.
274f0 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66 The index.** f
27500 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65 or "?NNN" parame
27510 74 65 72 73 20 69 73 20 74 68 65 20 76 61 6c 75 ters is the valu
27520 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 e of NNN..** The
27530 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 74 20 NNN value must
27540 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 be between 1 and
27550 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 the [sqlite3_li
27560 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65 mit()].** parame
27570 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 ter [SQLITE_LIMI
27580 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
27590 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61 6c 75 R] (default valu
275a0 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 e: 999)..**.** T
275b0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
275c0 74 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 74 t is the value t
275d0 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20 70 61 o bind to the pa
275e0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 rameter..**.** I
275f0 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 n those routines
27600 20 74 68 61 74 20 68 61 76 65 20 61 20 66 6f 75 that have a fou
27610 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 rth argument, it
27620 73 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a s value is the.*
27630 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * number of byte
27640 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 s in the paramet
27650 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 61 72 er. To be clear
27660 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 : the value is t
27670 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
27680 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 <u>bytes</u> in
27690 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 the value, not t
276a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 he number of cha
276b0 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74 racters..** If t
276c0 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 he fourth parame
276d0 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 2c ter is negative,
276e0 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 the length of t
276f0 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 he string is.**
27700 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
27710 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 tes up to the fi
27720 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 rst zero termina
27730 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 tor..**.** The f
27740 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f ifth argument to
27750 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c sqlite3_bind_bl
27760 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 ob(), sqlite3_bi
27770 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a nd_text(), and.*
27780 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 * sqlite3_bind_t
27790 65 78 74 31 36 28 29 20 69 73 20 61 20 64 65 73 ext16() is a des
277a0 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f 20 tructor used to
277b0 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 42 dispose of the B
277c0 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 LOB or.** string
277d0 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 68 61 after SQLite ha
277e0 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 s finished with
277f0 69 74 2e 20 49 66 20 74 68 65 20 66 69 66 74 68 it. If the fifth
27800 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 argument is.**
27810 74 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 the special valu
27820 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 e [SQLITE_STATIC
27830 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 ], then SQLite a
27840 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 0a ssumes that the.
27850 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ** information i
27860 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d s in static, unm
27870 61 6e 61 67 65 64 20 73 70 61 63 65 20 61 6e 64 anaged space and
27880 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
27890 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49 o be freed..** I
278a0 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75 f the fifth argu
278b0 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76 61 6c ment has the val
278c0 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 ue [SQLITE_TRANS
278d0 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53 IENT], then.** S
278e0 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 73 20 QLite makes its
278f0 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 own private copy
27900 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 6d 6d of the data imm
27910 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 ediately, before
27920 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f .** the sqlite3_
27930 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 bind_*() routine
27940 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 returns..**.**
27950 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 The sqlite3_bind
27960 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74 _zeroblob() rout
27970 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c 4f 42 ine binds a BLOB
27980 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 68 61 of length N tha
27990 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 t.** is filled w
279a0 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a ith zeroes. A z
279b0 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 20 66 eroblob uses a f
279c0 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d ixed amount of m
279d0 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61 emory.** (just a
279e0 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c n integer to hol
279f0 64 20 69 74 73 20 73 69 7a 65 29 20 77 68 69 6c d its size) whil
27a00 65 20 69 74 20 69 73 20 62 65 69 6e 67 20 70 72 e it is being pr
27a10 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f ocessed..** Zero
27a20 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 6e 64 blobs are intend
27a30 65 64 20 74 6f 20 73 65 72 76 65 20 61 73 20 70 ed to serve as p
27a40 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 laceholders for
27a50 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63 BLOBs whose.** c
27a60 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 72 20 ontent is later
27a70 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a written using.**
27a80 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
27a90 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 pen | incrementa
27aa0 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74 l BLOB I/O] rout
27ab0 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74 ines..** A negat
27ac0 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ive value for th
27ad0 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c e zeroblob resul
27ae0 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e ts in a zero-len
27af0 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 gth BLOB..**.**
27b00 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 The sqlite3_bind
27b10 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 6d 75 _*() routines mu
27b20 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 st be called aft
27b30 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 er.** [sqlite3_p
27b40 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28 61 6e repare_v2()] (an
27b50 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 d its variants)
27b60 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 or [sqlite3_rese
27b70 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f t()] and.** befo
27b80 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 re [sqlite3_step
27b90 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73 ()]..** Bindings
27ba0 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 65 64 are not cleared
27bb0 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 by the [sqlite3
27bc0 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74 69 6e _reset()] routin
27bd0 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61 e..** Unbound pa
27be0 72 61 6d 65 74 65 72 73 20 61 72 65 20 69 6e 74 rameters are int
27bf0 65 72 70 72 65 74 65 64 20 61 73 20 4e 55 4c 4c erpreted as NULL
27c00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
27c10 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 5b 53 utines return [S
27c20 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 QLITE_OK] on suc
27c30 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 cess or an error
27c40 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 code if.** anyt
27c50 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
27c60 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d [SQLITE_RANGE]
27c70 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
27c80 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a the parameter.**
27c90 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66 index is out of
27ca0 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45 range. [SQLITE
27cb0 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 _NOMEM] is retur
27cc0 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 ned if malloc()
27cd0 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54 fails..** [SQLIT
27ce0 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 74 20 E_MISUSE] might
27cf0 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 be returned if t
27d00 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
27d10 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a e called on a.**
27d20 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
27d30 20 74 68 61 74 20 69 73 20 74 68 65 20 77 72 6f that is the wro
27d40 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68 69 63 ng state or whic
27d50 68 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 h has already be
27d60 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a en finalized..**
27d70 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69 Detection of mi
27d80 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69 61 62 suse is unreliab
27d90 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e le. Application
27da0 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70 s should not dep
27db0 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45 end.** on SQLITE
27dc0 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e 73 2e _MISUSE returns.
27dd0 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 SQLITE_MISUSE
27de0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 69 is intended to i
27df0 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c ndicate a.** a l
27e00 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 74 68 ogic error in th
27e10 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 e application.
27e20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
27e30 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a of SQLite might.
27e40 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65 72 20 ** panic rather
27e50 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 than return SQLI
27e60 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a TE_MISUSE..**.**
27e70 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 See also: [sqli
27e80 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
27e90 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 er_count()],.**
27ea0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
27eb0 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c rameter_name()],
27ec0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 and [sqlite3_bi
27ed0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 nd_parameter_ind
27ee0 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 ex()]..**.** INV
27ef0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
27f00 48 31 33 35 30 36 7d 20 54 68 65 20 5b 53 51 4c H13506} The [SQL
27f10 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 statement compi
27f20 6c 65 72 5d 20 72 65 63 6f 67 6e 69 7a 65 73 20 ler] recognizes
27f30 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 20 66 6f tokens of the fo
27f40 72 6d 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 rms.**
27f50 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 24 56 "?", "?NNN", "$V
27f60 56 56 22 2c 20 22 3a 56 56 56 22 2c 20 61 6e 64 VV", ":VVV", and
27f70 20 22 40 56 56 56 22 20 61 73 20 53 51 4c 20 70 "@VVV" as SQL p
27f80 61 72 61 6d 65 74 65 72 73 2c 0a 2a 2a 20 20 20 arameters,.**
27f90 20 20 20 20 20 20 20 77 68 65 72 65 20 4e 4e 4e where NNN
27fa0 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 is any sequence
27fb0 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 of one or more
27fc0 64 69 67 69 74 73 0a 2a 2a 20 20 20 20 20 20 20 digits.**
27fd0 20 20 20 61 6e 64 20 77 68 65 72 65 20 56 56 56 and where VVV
27fe0 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 is any sequence
27ff0 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 of one or more
28000 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a 20 alphanumeric.**
28010 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63 74 charact
28020 65 72 73 20 6f 72 20 22 3a 3a 22 20 6f 70 74 69 ers or "::" opti
28030 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 onally followed
28040 62 79 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 by a string cont
28050 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 aining.**
28060 20 20 20 6e 6f 20 73 70 61 63 65 73 20 61 6e 64 no spaces and
28070 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 contained withi
28080 6e 20 70 61 72 65 6e 74 68 65 73 65 73 2e 0a 2a n parentheses..*
28090 2a 0a 2a 2a 20 7b 48 31 33 35 30 39 7d 20 54 68 *.** {H13509} Th
280a0 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 e initial value
280b0 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 of an SQL parame
280c0 74 65 72 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a ter is NULL..**.
280d0 2a 2a 20 7b 48 31 33 35 31 32 7d 20 54 68 65 20 ** {H13512} The
280e0 69 6e 64 65 78 20 6f 66 20 61 6e 20 22 3f 22 20 index of an "?"
280f0 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 SQL parameter is
28100 20 6f 6e 65 20 6c 61 72 67 65 72 20 74 68 61 6e one larger than
28110 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
28120 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f largest index o
28130 66 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 f SQL parameter
28140 74 6f 20 74 68 65 20 6c 65 66 74 2c 20 6f 72 20 to the left, or
28150 31 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 1 if.**
28160 20 74 68 65 20 22 3f 22 20 69 73 20 74 68 65 20 the "?" is the
28170 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61 72 leftmost SQL par
28180 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ameter..**.** {H
28190 31 33 35 31 35 7d 20 54 68 65 20 69 6e 64 65 78 13515} The index
281a0 20 6f 66 20 61 6e 20 22 3f 4e 4e 4e 22 20 53 51 of an "?NNN" SQ
281b0 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 L parameter is t
281c0 68 65 20 69 6e 74 65 67 65 72 20 4e 4e 4e 2e 0a he integer NNN..
281d0 2a 2a 0a 2a 2a 20 7b 48 31 33 35 31 38 7d 20 54 **.** {H13518} T
281e0 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 22 he index of an "
281f0 3a 56 56 56 22 2c 20 22 24 56 56 56 22 2c 20 6f :VVV", "$VVV", o
28200 72 20 22 40 56 56 56 22 20 53 51 4c 20 70 61 72 r "@VVV" SQL par
28210 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 20 20 20 ameter is.**
28220 20 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 61 the same a
28230 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 6c s the index of l
28240 65 66 74 6d 6f 73 74 20 6f 63 63 75 72 72 65 6e eftmost occurren
28250 63 65 73 20 6f 66 20 74 68 65 20 73 61 6d 65 0a ces of the same.
28260 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61 ** para
28270 6d 65 74 65 72 2c 20 6f 72 20 6f 6e 65 20 6d 6f meter, or one mo
28280 72 65 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 re than the larg
28290 65 73 74 20 69 6e 64 65 78 20 6f 76 65 72 20 61 est index over a
282a0 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 ll.** p
282b0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65 arameters to the
282c0 20 6c 65 66 74 20 69 66 20 74 68 69 73 20 69 73 left if this is
282d0 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 the first occur
282e0 72 65 6e 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 rence.**
282f0 20 20 6f 66 20 74 68 69 73 20 70 61 72 61 6d 65 of this parame
28300 74 65 72 2c 20 6f 72 20 31 20 69 66 20 74 68 69 ter, or 1 if thi
28310 73 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 s is the leftmos
28320 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a t parameter..**.
28330 2a 2a 20 7b 48 31 33 35 32 31 7d 20 54 68 65 20 ** {H13521} The
28340 5b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 [SQL statement c
28350 6f 6d 70 69 6c 65 72 5d 20 66 61 69 6c 73 20 77 ompiler] fails w
28360 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 52 ith an [SQLITE_R
28370 41 4e 47 45 5d 0a 2a 2a 20 20 20 20 20 20 20 20 ANGE].**
28380 20 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 69 error if the i
28390 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20 70 ndex of an SQL p
283a0 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65 73 73 arameter is less
283b0 20 74 68 61 6e 20 31 0a 2a 2a 20 20 20 20 20 20 than 1.**
283c0 20 20 20 20 6f 72 20 67 72 65 61 74 65 72 20 74 or greater t
283d0 68 61 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d han the compile-
283e0 74 69 6d 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f time SQLITE_MAX_
283f0 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a VARIABLE_NUMBER.
28400 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61 ** para
28410 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 meter..**.** {H1
28420 33 35 32 34 7d 20 43 61 6c 6c 73 20 74 6f 20 5b 3524} Calls to [
28430 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
28440 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 t | sqlite3_bind
28450 28 53 2c 4e 2c 56 2c 2e 2e 2e 29 5d 0a 2a 2a 20 (S,N,V,...)].**
28460 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 associa
28470 74 65 20 74 68 65 20 76 61 6c 75 65 20 56 20 77 te the value V w
28480 69 74 68 20 61 6c 6c 20 53 51 4c 20 70 61 72 61 ith all SQL para
28490 6d 65 74 65 72 73 20 68 61 76 69 6e 67 20 61 6e meters having an
284a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 64 .** ind
284b0 65 78 20 6f 66 20 4e 20 69 6e 20 74 68 65 20 5b ex of N in the [
284c0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
284d0 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 nt] S..**.** {H1
284e0 33 35 32 37 7d 20 43 61 6c 6c 73 20 74 6f 20 5b 3527} Calls to [
284f0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
28500 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 t | sqlite3_bind
28510 28 53 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 (S,N,...)].**
28520 20 20 20 20 20 20 20 6f 76 65 72 72 69 64 65 20 override
28530 70 72 69 6f 72 20 63 61 6c 6c 73 20 77 69 74 68 prior calls with
28540 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 73 the same values
28550 20 6f 66 20 53 20 61 6e 64 20 4e 2e 0a 2a 2a 0a of S and N..**.
28560 2a 2a 20 7b 48 31 33 35 33 30 7d 20 42 69 6e 64 ** {H13530} Bind
28570 69 6e 67 73 20 65 73 74 61 62 6c 69 73 68 65 64 ings established
28580 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e by [sqlite3_bin
28590 64 5f 74 65 78 74 20 7c 20 73 71 6c 69 74 65 33 d_text | sqlite3
285a0 5f 62 69 6e 64 28 53 2c 2e 2e 2e 29 5d 0a 2a 2a _bind(S,...)].**
285b0 20 20 20 20 20 20 20 20 20 20 70 65 72 73 69 73 persis
285c0 74 20 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 74 t across calls t
285d0 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 o [sqlite3_reset
285e0 28 53 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 (S)]..**.** {H13
285f0 35 33 33 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 533} In calls to
28600 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
28610 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c lob(S,N,V,L,D)],
28620 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
28630 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 lite3_bind_text(
28640 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a S,N,V,L,D)], or.
28650 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
28660 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 ite3_bind_text16
28670 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 4c (S,N,V,L,D)] SQL
28680 69 74 65 20 62 69 6e 64 73 20 74 68 65 20 66 69 ite binds the fi
28690 72 73 74 20 4c 0a 2a 2a 20 20 20 20 20 20 20 20 rst L.**
286a0 20 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 bytes of the B
286b0 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 20 70 6f LOB or string po
286c0 69 6e 74 65 64 20 74 6f 20 62 79 20 56 2c 20 77 inted to by V, w
286d0 68 65 6e 20 4c 0a 2a 2a 20 20 20 20 20 20 20 20 hen L.**
286e0 20 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 is non-negativ
286f0 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 33 36 e..**.** {H13536
28700 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 } In calls to [s
28710 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
28720 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 6f 72 0a (S,N,V,L,D)] or.
28730 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
28740 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 ite3_bind_text16
28750 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 4c (S,N,V,L,D)] SQL
28760 69 74 65 20 62 69 6e 64 73 20 63 68 61 72 61 63 ite binds charac
28770 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ters.**
28780 20 66 72 6f 6d 20 56 20 74 68 72 6f 75 67 68 20 from V through
28790 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 63 the first zero c
287a0 68 61 72 61 63 74 65 72 20 77 68 65 6e 20 4c 20 haracter when L
287b0 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a is negative..**.
287c0 2a 2a 20 7b 48 31 33 35 33 39 7d 20 49 6e 20 63 ** {H13539} In c
287d0 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
287e0 5f 62 69 6e 64 5f 62 6c 6f 62 28 53 2c 4e 2c 56 _bind_blob(S,N,V
287f0 2c 4c 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 ,L,D)],.**
28800 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e [sqlite3_bin
28810 64 5f 74 65 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44 d_text(S,N,V,L,D
28820 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 )], or.**
28830 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 [sqlite3_bind
28840 5f 74 65 78 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c _text16(S,N,V,L,
28850 44 29 5d 20 77 68 65 6e 20 44 20 69 73 20 74 68 D)] when D is th
28860 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20 20 20 20 e special.**
28870 20 20 20 20 20 20 63 6f 6e 73 74 61 6e 74 20 5b constant [
28880 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20 SQLITE_STATIC],
28890 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74 SQLite assumes t
288a0 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 56 0a hat the value V.
288b0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 68 ** is h
288c0 65 6c 64 20 69 6e 20 73 74 61 74 69 63 20 75 6e eld in static un
288d0 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 74 68 managed space th
288e0 61 74 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e at will not chan
288f0 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 ge.** d
28900 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 uring the lifeti
28910 6d 65 20 6f 66 20 74 68 65 20 62 69 6e 64 69 6e me of the bindin
28920 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 34 32 g..**.** {H13542
28930 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 } In calls to [s
28940 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
28950 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 0a 2a 2a (S,N,V,L,D)],.**
28960 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
28970 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 53 2c 4e e3_bind_text(S,N
28980 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20 ,V,L,D)], or.**
28990 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
289a0 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 53 2c 3_bind_text16(S,
289b0 4e 2c 56 2c 4c 2c 44 29 5d 20 77 68 65 6e 20 44 N,V,L,D)] when D
289c0 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 0a is the special.
289d0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 ** cons
289e0 74 61 6e 74 20 5b 53 51 4c 49 54 45 5f 54 52 41 tant [SQLITE_TRA
289f0 4e 53 49 45 4e 54 5d 2c 20 74 68 65 20 72 6f 75 NSIENT], the rou
28a00 74 69 6e 65 20 6d 61 6b 65 73 20 61 0a 2a 2a 20 tine makes a.**
28a10 20 20 20 20 20 20 20 20 20 70 72 69 76 61 74 65 private
28a20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c copy of the val
28a30 75 65 20 56 20 62 65 66 6f 72 65 20 69 74 20 72 ue V before it r
28a40 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 eturns..**.** {H
28a50 31 33 35 34 35 7d 20 49 6e 20 63 61 6c 6c 73 20 13545} In calls
28a60 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 to [sqlite3_bind
28a70 5f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 _blob(S,N,V,L,D)
28a80 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b ],.** [
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 2c 20 6f t(S,N,V,L,D)], o
28ab0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 r.** [s
28ac0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
28ad0 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 77 16(S,N,V,L,D)] w
28ae0 68 65 6e 20 44 20 69 73 20 61 20 70 6f 69 6e 74 hen D is a point
28af0 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 er to.**
28b00 20 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 a function, SQ
28b10 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 61 Lite invokes tha
28b20 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 t function to de
28b30 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 20 stroy the.**
28b40 20 20 20 20 20 20 76 61 6c 75 65 20 56 20 61 66 value V af
28b50 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69 73 ter it has finis
28b60 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 76 61 hed using the va
28b70 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 lue V..**.** {H1
28b80 33 35 34 38 7d 20 49 6e 20 63 61 6c 6c 73 20 74 3548} In calls t
28b90 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f o [sqlite3_bind_
28ba0 7a 65 72 6f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c zeroblob(S,N,V,L
28bb0 29 5d 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 )] the value bou
28bc0 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 nd.** i
28bd0 73 20 61 20 42 4c 4f 42 20 6f 66 20 4c 20 62 79 s a BLOB of L by
28be0 74 65 73 2c 20 6f 72 20 61 20 7a 65 72 6f 2d 6c tes, or a zero-l
28bf0 65 6e 67 74 68 20 42 4c 4f 42 20 69 66 20 4c 20 ength BLOB if L
28c00 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a is negative..**.
28c10 2a 2a 20 7b 48 31 33 35 35 31 7d 20 49 6e 20 63 ** {H13551} In c
28c20 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
28c30 5f 62 69 6e 64 5f 76 61 6c 75 65 28 53 2c 4e 2c _bind_value(S,N,
28c40 56 29 5d 20 74 68 65 20 56 20 61 72 67 75 6d 65 V)] the V argume
28c50 6e 74 20 6d 61 79 0a 2a 2a 20 20 20 20 20 20 20 nt may.**
28c60 20 20 20 62 65 20 65 69 74 68 65 72 20 61 20 5b be either a [
28c70 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
28c80 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 3_value] object
28c90 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 or an.**
28ca0 20 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 [unprotected s
28cb0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
28cc0 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ject..*/.SQLITE_
28cd0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
28ce0 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65 bind_blob(sqlite
28cf0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
28d00 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e nst void*, int n
28d10 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
28d20 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
28d30 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 t sqlite3_bind_d
28d40 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 ouble(sqlite3_st
28d50 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 mt*, int, double
28d60 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
28d70 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 t sqlite3_bind_i
28d80 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a nt(sqlite3_stmt*
28d90 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
28da0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
28db0 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 te3_bind_int64(s
28dc0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
28dd0 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 t, sqlite3_int64
28de0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
28df0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e t sqlite3_bind_n
28e00 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ull(sqlite3_stmt
28e10 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
28e20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
28e30 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65 bind_text(sqlite
28e40 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
28e50 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e nst char*, int n
28e60 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
28e70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
28e80 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 t sqlite3_bind_t
28e90 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ext16(sqlite3_st
28ea0 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 mt*, int, const
28eb0 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 void*, int, void
28ec0 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
28ed0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
28ee0 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 te3_bind_value(s
28ef0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
28f00 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 t, const sqlite3
28f10 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
28f20 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
28f30 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 _bind_zeroblob(s
28f40 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
28f50 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a t, int n);../*.*
28f60 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 * CAPI3REF: Numb
28f70 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 er Of SQL Parame
28f80 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c 53 ters {H13600} <S
28f90 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 70300>.**.** Thi
28fa0 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 s routine can be
28fb0 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68 used to find th
28fc0 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c e number of [SQL
28fd0 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20 parameters].**
28fe0 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 in a [prepared s
28ff0 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20 tatement]. SQL
29000 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74 parameters are t
29010 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20 okens of the.**
29020 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 form "?", "?NNN"
29030 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 22 , ":AAA", "$AAA"
29040 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 74 , or "@AAA" that
29050 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61 serve as.** pla
29060 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 61 ceholders for va
29070 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b 73 lues that are [s
29080 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
29090 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20 | bound].** to
290a0 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 the parameters a
290b0 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a t a later time..
290c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
290d0 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75 ne actually retu
290e0 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 rns the index of
290f0 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 69 the largest (ri
29100 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61 ghtmost).** para
29110 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66 meter. For all f
29120 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e orms except ?NNN
29130 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72 , this will corr
29140 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a espond to the.**
29150 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75 number of uniqu
29160 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49 e parameters. I
29170 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 f parameters of
29180 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65 the ?NNN are use
29190 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20 d,.** there may
291a0 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c be gaps in the l
291b0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ist..**.** See a
291c0 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 lso: [sqlite3_bi
291d0 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f nd_blob|sqlite3_
291e0 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c bind()],.** [sql
291f0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
29200 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 ter_name()], and
29210 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
29220 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 d_parameter_inde
29230 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 x()]..**.** INVA
29240 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
29250 31 33 36 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 13601} The [sqli
29260 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
29270 65 72 5f 63 6f 75 6e 74 28 53 29 5d 20 69 6e 74 er_count(S)] int
29280 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a 2a erface returns.*
29290 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c * the l
292a0 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f 66 20 argest index of
292b0 61 6c 6c 20 53 51 4c 20 70 61 72 61 6d 65 74 65 all SQL paramete
292c0 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 rs in the.**
292d0 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 [prepared
292e0 73 74 61 74 65 6d 65 6e 74 5d 20 53 2c 20 6f 72 statement] S, or
292f0 20 30 20 69 66 20 53 20 63 6f 6e 74 61 69 6e 73 0 if S contains
29300 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74 65 no SQL paramete
29310 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 rs..*/.SQLITE_AP
29320 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
29330 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 nd_parameter_cou
29340 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a nt(sqlite3_stmt*
29350 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
29360 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f EF: Name Of A Ho
29370 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b 48 31 st Parameter {H1
29380 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 3620} <S70300>.*
29390 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
293a0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
293b0 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 ter to the name
293c0 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b of the n-th.** [
293d0 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 69 SQL parameter] i
293e0 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 n a [prepared st
293f0 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c atement]..** SQL
29400 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 parameters of t
29410 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f he form "?NNN" o
29420 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41 41 r ":AAA" or "@AA
29430 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 A" or "$AAA".**
29440 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69 63 have a name whic
29450 68 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 20 h is the string
29460 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 "?NNN" or ":AAA"
29470 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22 24 or "@AAA" or "$
29480 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74 69 AAA".** respecti
29490 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 vely..** In othe
294a0 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 r words, the ini
294b0 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 tial ":" or "$"
294c0 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a or "@" or "?".**
294d0 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 20 is included as
294e0 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 part of the name
294f0 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 20 ..** Parameters
29500 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 20 of the form "?"
29510 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77 without a follow
29520 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61 76 65 ing integer have
29530 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 no name.** and
29540 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72 72 65 are also referre
29550 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f d to as "anonymo
29560 75 73 20 70 61 72 61 6d 65 74 65 72 73 22 2e 0a us parameters"..
29570 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
29580 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 host parameter h
29590 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 as an index of 1
295a0 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 , not 0..**.** I
295b0 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 73 f the value n is
295c0 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 out of range or
295d0 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 72 if the n-th par
295e0 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d ameter is.** nam
295f0 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c eless, then NULL
29600 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 is returned. T
29610 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 he returned stri
29620 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 ng is.** always
29630 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e in UTF-8 encodin
29640 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e 61 g even if the na
29650 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 61 med parameter wa
29660 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 s.** originally
29670 73 70 65 63 69 66 69 65 64 20 61 73 20 55 54 46 specified as UTF
29680 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f -16 in [sqlite3_
29690 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 0a prepare16()] or.
296a0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
296b0 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a are16_v2()]..**.
296c0 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
296d0 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c lite3_bind_blob|
296e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c sqlite3_bind()],
296f0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
29700 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e d_parameter_coun
29710 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 t()], and.** [sq
29720 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
29730 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a eter_index()]..*
29740 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
29750 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 36 32 31 7d 20 .**.** {H13621}
29760 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e The [sqlite3_bin
29770 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 d_parameter_name
29780 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 (S,N)] interface
29790 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 returns.**
297a0 20 20 20 20 20 61 20 55 54 46 2d 38 20 72 65 6e a UTF-8 ren
297b0 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 6e 61 dering of the na
297c0 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 me of the SQL pa
297d0 72 61 6d 65 74 65 72 20 69 6e 0a 2a 2a 20 20 20 rameter in.**
297e0 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 65 70 the [prep
297f0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
29800 53 20 68 61 76 69 6e 67 20 69 6e 64 65 78 20 4e S having index N
29810 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 , or.**
29820 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 NULL if there i
29830 73 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74 s no SQL paramet
29840 65 72 20 77 69 74 68 20 69 6e 64 65 78 20 4e 20 er with index N
29850 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20 or if the.**
29860 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 20 parameter
29870 77 69 74 68 20 69 6e 64 65 78 20 4e 20 69 73 20 with index N is
29880 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 70 61 72 an anonymous par
29890 61 6d 65 74 65 72 20 22 3f 22 2e 0a 2a 2f 0a 53 ameter "?"..*/.S
298a0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
298b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 char *sqlite3_bi
298c0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d nd_parameter_nam
298d0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
298e0 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 int);../*.** CA
298f0 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66 PI3REF: Index Of
29900 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 74 A Parameter Wit
29910 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b h A Given Name {
29920 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e H13640} <S70300>
29930 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
29940 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 e index of an SQ
29950 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 65 L parameter give
29960 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65 n its name. The
29970 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20 .** index value
29980 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 74 returned is suit
29990 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 able for use as
299a0 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 the second.** pa
299b0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
299c0 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 te3_bind_blob|sq
299d0 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20 lite3_bind()].
299e0 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 A zero.** is ret
299f0 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63 urned if no matc
29a00 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69 hing parameter i
29a10 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61 s found. The pa
29a20 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20 rameter.** name
29a30 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e must be given in
29a40 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74 UTF-8 even if t
29a50 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 he original stat
29a60 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65 ement.** was pre
29a70 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31 pared from UTF-1
29a80 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71 6 text using [sq
29a90 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
29aa0 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 v2()]..**.** See
29ab0 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
29ac0 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 bind_blob|sqlite
29ad0 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 3_bind()],.** [s
29ae0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
29af0 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 meter_count()],
29b00 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f and.** [sqlite3_
29b10 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
29b20 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 ndex()]..**.** I
29b30 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
29b40 20 7b 48 31 33 36 34 31 7d 20 54 68 65 20 5b 73 {H13641} The [s
29b50 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
29b60 6d 65 74 65 72 5f 69 6e 64 65 78 28 53 2c 4e 29 meter_index(S,N)
29b70 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ] interface retu
29b80 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 rns.**
29b90 74 68 65 20 69 6e 64 65 78 20 6f 66 20 53 51 4c the index of SQL
29ba0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 parameter in th
29bb0 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
29bc0 65 6d 65 6e 74 5d 0a 2a 2a 20 20 20 20 20 20 20 ement].**
29bd0 20 20 20 53 20 77 68 6f 73 65 20 6e 61 6d 65 20 S whose name
29be0 6d 61 74 63 68 65 73 20 74 68 65 20 55 54 46 2d matches the UTF-
29bf0 38 20 73 74 72 69 6e 67 20 4e 2c 20 6f 72 20 30 8 string N, or 0
29c00 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 if there is.**
29c10 20 20 20 20 20 20 20 20 20 6e 6f 20 6d 61 74 63 no matc
29c20 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 h..*/.SQLITE_API
29c30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
29c40 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 d_parameter_inde
29c50 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c x(sqlite3_stmt*,
29c60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
29c70 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 me);../*.** CAPI
29c80 33 52 45 46 3a 20 52 65 73 65 74 20 41 6c 6c 20 3REF: Reset All
29c90 42 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 72 Bindings On A Pr
29ca0 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 epared Statement
29cb0 20 7b 48 31 33 36 36 30 7d 20 3c 53 37 30 33 30 {H13660} <S7030
29cc0 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 0>.**.** Contrar
29cd0 79 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69 y to the intuiti
29ce0 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c on of many, [sql
29cf0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f ite3_reset()] do
29d00 65 73 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20 es not reset.**
29d10 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e the [sqlite3_bin
29d20 64 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 d_blob | binding
29d30 73 5d 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 s] on a [prepare
29d40 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a d statement]..**
29d50 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e Use this routin
29d60 65 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68 e to reset all h
29d70 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 ost parameters t
29d80 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e o NULL..**.** IN
29d90 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 VARIANTS:.**.**
29da0 7b 48 31 33 36 36 31 7d 20 54 68 65 20 5b 73 71 {H13661} The [sq
29db0 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 lite3_clear_bind
29dc0 69 6e 67 73 28 53 29 5d 20 69 6e 74 65 72 66 61 ings(S)] interfa
29dd0 63 65 20 72 65 73 65 74 73 20 61 6c 6c 20 53 51 ce resets all SQ
29de0 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 L.** pa
29df0 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 73 rameter bindings
29e00 20 69 6e 20 74 68 65 20 5b 70 72 65 70 61 72 65 in the [prepare
29e10 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 62 d statement] S b
29e20 61 63 6b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a ack to NULL..*/.
29e30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
29e40 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e qlite3_clear_bin
29e50 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 dings(sqlite3_st
29e60 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 mt*);../*.** CAP
29e70 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 I3REF: Number Of
29e80 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 65 Columns In A Re
29e90 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 30 sult Set {H13710
29ea0 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a } <S10700>.**.**
29eb0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
29ec0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
29ed0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
29ee0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a returned by the.
29ef0 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ** [prepared sta
29f00 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 6f tement]. This ro
29f10 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20 utine returns 0
29f20 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 53 if pStmt is an S
29f30 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 QL.** statement
29f40 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 that does not re
29f50 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 65 turn data (for e
29f60 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 54 xample an [UPDAT
29f70 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 E])..**.** INVAR
29f80 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 IANTS:.**.** {H1
29f90 33 37 31 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 3711} The [sqlit
29fa0 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 e3_column_count(
29fb0 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 S)] interface re
29fc0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
29fd0 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
29fe0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 columns in the r
29ff0 65 73 75 6c 74 20 73 65 74 20 67 65 6e 65 72 61 esult set genera
2a000 74 65 64 20 62 79 20 74 68 65 20 5b 70 72 65 70 ted by the [prep
2a010 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
2a020 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f S,.** o
2a030 72 20 30 20 69 66 20 53 20 64 6f 65 73 20 6e 6f r 0 if S does no
2a040 74 20 67 65 6e 65 72 61 74 65 20 61 20 72 65 73 t generate a res
2a050 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 ult set..*/.SQLI
2a060 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2a070 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 e3_column_count(
2a080 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
2a090 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 tmt);../*.** CAP
2a0a0 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 I3REF: Column Na
2a0b0 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 mes In A Result
2a0c0 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c 53 31 Set {H13720} <S1
2a0d0 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 0700>.**.** Thes
2a0e0 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 e routines retur
2a0f0 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67 n the name assig
2a100 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69 63 75 ned to a particu
2a110 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e lar column.** in
2a120 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
2a130 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 of a [SELECT] st
2a140 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73 71 atement. The sq
2a150 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
2a160 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 e().** interface
2a170 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
2a180 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 er to a zero-ter
2a190 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 minated UTF-8 st
2a1a0 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 ring.** and sqli
2a1b0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 te3_column_name1
2a1c0 36 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f 6() returns a po
2a1d0 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d inter to a zero-
2a1e0 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 terminated.** UT
2a1f0 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 F-16 string. Th
2a200 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
2a210 72 20 69 73 20 74 68 65 20 5b 70 72 65 70 61 72 r is the [prepar
2a220 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a ed statement].**
2a230 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
2a240 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 73 74 the [SELECT] st
2a250 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 atement. The sec
2a260 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 ond parameter is
2a270 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e the.** column n
2a280 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 66 74 umber. The left
2a290 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e most column is n
2a2a0 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 umber 0..**.** T
2a2b0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 he returned stri
2a2c0 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 ng pointer is va
2a2d0 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 lid until either
2a2e0 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 the [prepared s
2a2f0 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 tatement].** is
2a300 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 destroyed by [sq
2a310 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
2a320 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 6e ] or until the n
2a330 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 ext call to.** s
2a340 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 qlite3_column_na
2a350 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f me() or sqlite3_
2a360 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 column_name16()
2a370 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 on the same colu
2a380 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c mn..**.** If sql
2a390 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 ite3_malloc() fa
2a3a0 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 20 70 ils during the p
2a3b0 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 69 74 rocessing of eit
2a3c0 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 her routine.** (
2a3d0 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 72 69 for example duri
2a3e0 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ng a conversion
2a3f0 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 from UTF-8 to UT
2a400 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 F-16) then a.**
2a410 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 NULL pointer is
2a420 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
2a430 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 The name of a re
2a440 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 sult column is t
2a450 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
2a460 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f 72 0a "AS" clause for.
2a470 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 ** that column,
2a480 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 41 if there is an A
2a490 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74 68 S clause. If th
2a4a0 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 ere is no AS cla
2a4b0 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 use.** then the
2a4c0 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 name of the colu
2a4d0 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66 69 65 mn is unspecifie
2a4e0 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 d and may change
2a4f0 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c from.** one rel
2a500 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20 74 ease of SQLite t
2a510 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a o the next..**.*
2a520 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
2a530 0a 2a 2a 20 7b 48 31 33 37 32 31 7d 20 41 20 73 .** {H13721} A s
2a540 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 uccessful invoca
2a550 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c tion of the [sql
2a560 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
2a570 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 (S,N)].**
2a580 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 interface ret
2a590 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 urns the name of
2a5a0 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
2a5b0 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20 20 (where 0 is.**
2a5c0 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66 74 the left
2a5d0 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f 72 most column) for
2a5e0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
2a5f0 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 of the.**
2a600 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 [prepared sta
2a610 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20 7a tement] S as a z
2a620 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 ero-terminated U
2a630 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a TF-8 string..**.
2a640 2a 2a 20 7b 48 31 33 37 32 33 7d 20 41 20 73 75 ** {H13723} A su
2a650 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 ccessful invocat
2a660 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 ion of the [sqli
2a670 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 te3_column_name1
2a680 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20 6(S,N)].**
2a690 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65 interface re
2a6a0 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f turns the name o
2a6b0 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
2a6c0 20 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20 (where 0 is.**
2a6d0 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66 the lef
2a6e0 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f tmost column) fo
2a6f0 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 r the result set
2a700 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 of the.**
2a710 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 [prepared st
2a720 61 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20 atement] S as a
2a730 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
2a740 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a 2a UTF-16 string.**
2a750 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68 65 in the
2a760 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
2a770 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32 er..**.** {H1372
2a780 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 4} The [sqlite3_
2a790 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61 column_name()] a
2a7a0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 nd [sqlite3_colu
2a7b0 6d 6e 5f 6e 61 6d 65 31 36 28 29 5d 0a 2a 2a 20 mn_name16()].**
2a7c0 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61 interfa
2a7d0 63 65 73 20 72 65 74 75 72 6e 20 61 20 4e 55 4c ces return a NUL
2a7e0 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 65 L pointer if the
2a7f0 79 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 0a y are unable to.
2a800 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 6f ** allo
2a810 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68 cate memory to h
2a820 6f 6c 64 20 74 68 65 69 72 20 6e 6f 72 6d 61 6c old their normal
2a830 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 73 2e return strings.
2a840 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32 35 7d 20 .**.** {H13725}
2a850 49 66 20 74 68 65 20 4e 20 70 61 72 61 6d 65 74 If the N paramet
2a860 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 er to [sqlite3_c
2a870 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d olumn_name(S,N)]
2a880 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or.**
2a890 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
2a8a0 6e 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 73 20 name16(S,N)] is
2a8b0 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 out of range, th
2a8c0 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 en the.**
2a8d0 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 interfaces re
2a8e0 74 75 72 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e turn a NULL poin
2a8f0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 ter..**.** {H137
2a900 32 36 7d 20 54 68 65 20 73 74 72 69 6e 67 73 20 26} The strings
2a910 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c returned by [sql
2a920 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
2a930 28 53 2c 4e 29 5d 20 61 6e 64 0a 2a 2a 20 20 20 (S,N)] and.**
2a940 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
2a950 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 53 2c column_name16(S,
2a960 4e 29 5d 20 61 72 65 20 76 61 6c 69 64 20 75 6e N)] are valid un
2a970 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 til the next.**
2a980 20 20 20 20 20 20 20 20 20 63 61 6c 6c 20 74 6f call to
2a990 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 20 either routine
2a9a0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 53 20 with the same S
2a9b0 61 6e 64 20 4e 20 70 61 72 61 6d 65 74 65 72 73 and N parameters
2a9c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 .** or
2a9d0 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f 66 until [sqlite3_f
2a9e0 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 63 inalize(S)] is c
2a9f0 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 alled..**.** {H1
2aa00 33 37 32 37 7d 20 57 68 65 6e 20 61 20 72 65 73 3727} When a res
2aa10 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 ult column of a
2aa20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 [SELECT] stateme
2aa30 6e 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 nt contains.**
2aa40 20 20 20 20 20 20 20 20 61 6e 20 41 53 20 63 6c an AS cl
2aa50 61 75 73 65 2c 20 74 68 65 20 6e 61 6d 65 20 6f ause, the name o
2aa60 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 f that column is
2aa70 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a the identifier.
2aa80 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 74 ** to t
2aa90 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 65 20 he right of the
2aaa0 41 53 20 6b 65 79 77 6f 72 64 2e 0a 2a 2f 0a 53 AS keyword..*/.S
2aab0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2aac0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
2aad0 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 lumn_name(sqlite
2aae0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 3_stmt*, int N);
2aaf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
2ab00 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
2ab10 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 column_name16(sq
2ab20 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
2ab30 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 N);../*.** CAPI
2ab40 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20 3REF: Source Of
2ab50 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20 Data In A Query
2ab60 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20 Result {H13740}
2ab70 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10700>.**.** T
2ab80 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 hese routines pr
2ab90 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f ovide a means to
2aba0 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 determine what
2abb0 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a column of what.*
2abc0 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 * table in which
2abd0 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75 database a resu
2abe0 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d lt of a [SELECT]
2abf0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 statement comes
2ac00 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 from..** The na
2ac10 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
2ac20 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63 se or table or c
2ac30 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74 olumn can be ret
2ac40 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68 urned as.** eith
2ac50 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54 er a UTF-8 or UT
2ac60 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 F-16 string. Th
2ac70 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75 e _database_ rou
2ac80 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20 tines return.**
2ac90 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
2aca0 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72 e, the _table_ r
2acb0 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 outines return t
2acc0 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61 he table name, a
2acd0 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e nd.** the origin
2ace0 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 _ routines retur
2acf0 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d n the column nam
2ad00 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e e..** The return
2ad10 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c ed string is val
2ad20 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 id until the [pr
2ad30 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2ad40 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a ] is destroyed.*
2ad50 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 * using [sqlite3
2ad60 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 _finalize()] or
2ad70 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69 until the same i
2ad80 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 nformation is re
2ad90 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e quested.** again
2ada0 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 in a different
2adb0 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 encoding..**.**
2adc0 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e The names return
2add0 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69 ed are the origi
2ade0 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e nal un-aliased n
2adf0 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 ames of the.** d
2ae00 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 atabase, table,
2ae10 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a and column..**.*
2ae20 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 * The first argu
2ae30 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c ment to the foll
2ae40 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 owing calls is a
2ae50 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
2ae60 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 ment]..** These
2ae70 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e functions return
2ae80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
2ae90 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d ut the Nth colum
2aea0 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a n returned by.**
2aeb0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 the statement,
2aec0 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 where N is the s
2aed0 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 econd function a
2aee0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 rgument..**.** I
2aef0 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
2af00 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
2af10 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e statement is an
2af20 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a expression or.*
2af30 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 * subquery and i
2af40 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 s not a column v
2af50 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f alue, then all o
2af60 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e f these function
2af70 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c s return.** NULL
2af80 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
2af90 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 might also retu
2afa0 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d rn NULL if a mem
2afb0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
2afc0 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 rror.** occurs.
2afd0 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 Otherwise, they
2afe0 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 return the name
2aff0 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64 of the attached
2b000 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 database, table
2b010 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 .** and column t
2b020 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74 hat query result
2b030 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72 column was extr
2b040 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a acted from..**.*
2b050 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74 * As with all ot
2b060 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c her SQLite APIs,
2b070 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64 those postfixed
2b080 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72 with "16" retur
2b090 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f n.** UTF-16 enco
2b0a0 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65 ded strings, the
2b0b0 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 other functions
2b0c0 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b return UTF-8. {
2b0d0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 END}.**.** These
2b0e0 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61 APIs are only a
2b0f0 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 vailable if the
2b100 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 library was comp
2b110 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a iled with the.**
2b120 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f [SQLITE_ENABLE_
2b130 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d COLUMN_METADATA]
2b140 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 C-preprocessor
2b150 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a symbol defined..
2b160 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a **.** {A13751}.*
2b170 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 * If two or more
2b180 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e threads call on
2b190 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 e or more of the
2b1a0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 se routines agai
2b1b0 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 nst the same.**
2b1c0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2b1d0 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 nt and column at
2b1e0 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 the same time t
2b1f0 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 hen the results
2b200 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 are.** undefined
2b210 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
2b220 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34 TS:.**.** {H1374
2b230 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 1} The [sqlite3_
2b240 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f column_database_
2b250 6e 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 name(S,N)] inter
2b260 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 face returns eit
2b270 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 her.**
2b280 74 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 the UTF-8 zero-t
2b290 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f erminated name o
2b2a0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
2b2b0 72 6f 6d 20 77 68 69 63 68 20 74 68 65 0a 2a 2a rom which the.**
2b2c0 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 65 Nth re
2b2d0 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 sult column of t
2b2e0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
2b2f0 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78 74 tement] S is ext
2b300 72 61 63 74 65 64 2c 0a 2a 2a 20 20 20 20 20 20 racted,.**
2b310 20 20 20 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 or NULL if t
2b320 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
2b330 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c 20 S is a general
2b340 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 20 expression.**
2b350 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e 61 or if una
2b360 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ble to allocate
2b370 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 20 memory to store
2b380 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 the name..**.**
2b390 7b 48 31 33 37 34 32 7d 20 54 68 65 20 5b 73 71 {H13742} The [sq
2b3a0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 lite3_column_dat
2b3b0 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 53 2c 4e abase_name16(S,N
2b3c0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 )] interface ret
2b3d0 75 72 6e 73 20 65 69 74 68 65 72 0a 2a 2a 20 20 urns either.**
2b3e0 20 20 20 20 20 20 20 20 74 68 65 20 55 54 46 2d the UTF-
2b3f0 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 16 native byte o
2b400 72 64 65 72 20 7a 65 72 6f 2d 74 65 72 6d 69 6e rder zero-termin
2b410 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 ated name of the
2b420 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 database.**
2b430 20 20 20 20 20 20 66 72 6f 6d 20 77 68 69 63 68 from which
2b440 20 74 68 65 20 4e 74 68 20 72 65 73 75 6c 74 20 the Nth result
2b450 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 column of the [p
2b460 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2b470 74 5d 20 53 20 69 73 0a 2a 2a 20 20 20 20 20 20 t] S is.**
2b480 20 20 20 20 65 78 74 72 61 63 74 65 64 2c 20 6f extracted, o
2b490 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 r NULL if the Nt
2b4a0 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 h column of S is
2b4b0 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65 a general expre
2b4c0 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ssion.**
2b4d0 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74 or if unable t
2b4e0 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 o allocate memor
2b4f0 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e y to store the n
2b500 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 ame..**.** {H137
2b510 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 43} The [sqlite3
2b520 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 _column_table_na
2b530 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 me(S,N)] interfa
2b540 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 ce returns eithe
2b550 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 r.** th
2b560 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 e UTF-8 zero-ter
2b570 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 minated name of
2b580 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 the table from w
2b590 68 69 63 68 20 74 68 65 0a 2a 2a 20 20 20 20 20 hich the.**
2b5a0 20 20 20 20 20 4e 74 68 20 72 65 73 75 6c 74 20 Nth result
2b5b0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 column of the [p
2b5c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2b5d0 74 5d 20 53 20 69 73 20 65 78 74 72 61 63 74 65 t] S is extracte
2b5e0 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f d,.** o
2b5f0 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 r NULL if the Nt
2b600 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 h column of S is
2b610 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65 a general expre
2b620 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ssion.**
2b630 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74 or if unable t
2b640 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 o allocate memor
2b650 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e y to store the n
2b660 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 ame..**.** {H137
2b670 34 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 44} The [sqlite3
2b680 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 _column_table_na
2b690 6d 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 me16(S,N)] inter
2b6a0 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 face returns eit
2b6b0 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 her.**
2b6c0 74 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 the UTF-16 nativ
2b6d0 65 20 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72 e byte order zer
2b6e0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d o-terminated nam
2b6f0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a e of the table.*
2b700 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 * from
2b710 77 68 69 63 68 20 74 68 65 20 4e 74 68 20 72 65 which the Nth re
2b720 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 sult column of t
2b730 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
2b740 74 65 6d 65 6e 74 5d 20 53 20 69 73 0a 2a 2a 20 tement] S is.**
2b750 20 20 20 20 20 20 20 20 20 65 78 74 72 61 63 74 extract
2b760 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 ed, or NULL if t
2b770 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
2b780 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c 20 S is a general
2b790 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 20 expression.**
2b7a0 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e 61 or if una
2b7b0 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ble to allocate
2b7c0 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 20 memory to store
2b7d0 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 the name..**.**
2b7e0 7b 48 31 33 37 34 35 7d 20 54 68 65 20 5b 73 71 {H13745} The [sq
2b7f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 lite3_column_ori
2b800 67 69 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 69 gin_name(S,N)] i
2b810 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
2b820 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 either.**
2b830 20 20 20 20 74 68 65 20 55 54 46 2d 38 20 7a 65 the UTF-8 ze
2b840 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 ro-terminated na
2b850 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
2b860 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 column from whic
2b870 68 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 h the.**
2b880 20 20 4e 74 68 20 72 65 73 75 6c 74 20 63 6f 6c Nth result col
2b890 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 72 65 70 umn of the [prep
2b8a0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
2b8b0 53 20 69 73 20 65 78 74 72 61 63 74 65 64 2c 0a S is extracted,.
2b8c0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 4e ** or N
2b8d0 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 68 20 63 ULL if the Nth c
2b8e0 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 20 61 20 olumn of S is a
2b8f0 67 65 6e 65 72 61 6c 20 65 78 70 72 65 73 73 69 general expressi
2b900 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f on.** o
2b910 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 61 r if unable to a
2b920 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 llocate memory t
2b930 6f 20 73 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 o store the name
2b940 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34 36 7d ..**.** {H13746}
2b950 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f The [sqlite3_co
2b960 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 lumn_origin_name
2b970 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 16(S,N)] interfa
2b980 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 ce returns eithe
2b990 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 r.** th
2b9a0 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 e UTF-16 native
2b9b0 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72 6f 2d byte order zero-
2b9c0 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 terminated name
2b9d0 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 of the table.**
2b9e0 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20 column
2b9f0 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 4e from which the N
2ba00 74 68 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e th result column
2ba10 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 of the.**
2ba20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 [prepared st
2ba30 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78 atement] S is ex
2ba40 74 72 61 63 74 65 64 2c 20 6f 72 20 4e 55 4c 4c tracted, or NULL
2ba50 20 69 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 if the Nth colu
2ba60 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f mn.** o
2ba70 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c f S is a general
2ba80 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 69 expression or i
2ba90 66 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f f unable to allo
2baa0 63 61 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 cate memory.**
2bab0 20 20 20 20 20 20 20 20 74 6f 20 73 74 6f 72 65 to store
2bac0 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a the name..**.**
2bad0 20 7b 48 31 33 37 34 38 7d 20 54 68 65 20 72 65 {H13748} The re
2bae0 74 75 72 6e 20 76 61 6c 75 65 73 20 66 72 6f 6d turn values from
2baf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
2bb00 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 lite3_column_dat
2bb10 61 62 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c abase_name | col
2bb20 75 6d 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74 umn metadata int
2bb30 65 72 66 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20 erfaces].**
2bb40 20 20 20 20 20 61 72 65 20 76 61 6c 69 64 20 66 are valid f
2bb50 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 or the lifetime
2bb60 6f 66 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 of the [prepared
2bb70 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 20 statement].**
2bb80 20 20 20 20 20 20 20 20 6f 72 20 75 6e 74 69 6c or until
2bb90 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 the encoding is
2bba0 20 63 68 61 6e 67 65 64 20 62 79 20 61 6e 6f 74 changed by anot
2bbb0 68 65 72 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 her metadata.**
2bbc0 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61 interfa
2bbd0 63 65 20 63 61 6c 6c 20 66 6f 72 20 74 68 65 20 ce call for the
2bbe0 73 61 6d 65 20 70 72 65 70 61 72 65 64 20 73 74 same prepared st
2bbf0 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75 atement and colu
2bc00 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 mn..**.** ASSUMP
2bc10 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 TIONS:.**.** {A1
2bc20 33 37 35 31 7d 20 49 66 20 74 77 6f 20 6f 72 20 3751} If two or
2bc30 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c more threads cal
2bc40 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a l one or more.**
2bc50 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
2bc60 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 e3_column_databa
2bc70 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e se_name | column
2bc80 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 metadata interf
2bc90 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20 aces].**
2bca0 20 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b for the same [
2bcb0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2bcc0 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63 nt] and result c
2bcd0 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 olumn.**
2bce0 20 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 at the same ti
2bcf0 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 me then the resu
2bd00 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 lts are undefine
2bd10 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
2bd20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
2bd30 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 ite3_column_data
2bd40 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 base_name(sqlite
2bd50 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 3_stmt*,int);.SQ
2bd60 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
2bd70 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
2bd80 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d umn_database_nam
2bd90 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 e16(sqlite3_stmt
2bda0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 *,int);.SQLITE_A
2bdb0 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
2bdc0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 qlite3_column_ta
2bdd0 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 ble_name(sqlite3
2bde0 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c _stmt*,int);.SQL
2bdf0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
2be00 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
2be10 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 mn_table_name16(
2be20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
2be30 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 t);.SQLITE_API c
2be40 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
2be50 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e e3_column_origin
2be60 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
2be70 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 mt*,int);.SQLITE
2be80 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
2be90 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
2bea0 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 origin_name16(sq
2beb0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 lite3_stmt*,int)
2bec0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2bed0 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 61 F: Declared Data
2bee0 74 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 20 type Of A Query
2bef0 52 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 Result {H13760}
2bf00 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10700>.**.** T
2bf10 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
2bf20 65 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 65 er is a [prepare
2bf30 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a d statement]..**
2bf40 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 If this stateme
2bf50 6e 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d nt is a [SELECT]
2bf60 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 statement and t
2bf70 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
2bf80 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 the.** returned
2bf90 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 result set of t
2bfa0 68 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 hat [SELECT] is
2bfb0 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 a table column (
2bfc0 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 not an.** expres
2bfd0 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 sion or subquery
2bfe0 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61 ) then the decla
2bff0 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 red type of the
2c000 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 table.** column
2c010 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 is returned. If
2c020 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
2c030 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
2c040 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 t is an.** expre
2c050 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 ssion or subquer
2c060 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 y, then a NULL p
2c070 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e ointer is return
2c080 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ed..** The retur
2c090 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c ned string is al
2c0a0 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 ways UTF-8 encod
2c0b0 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 ed. {END}.**.**
2c0c0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 For example, giv
2c0d0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
2c0e0 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 schema:.**.** CR
2c0f0 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 EATE TABLE t1(c1
2c100 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a VARIANT);.**.**
2c110 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 and the followi
2c120 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 ng statement to
2c130 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a be compiled:.**.
2c140 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31 ** SELECT c1 + 1
2c150 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a , c1 FROM t1;.**
2c160 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
2c170 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 would return th
2c180 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e e string "VARIAN
2c190 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e T" for the secon
2c1a0 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 d result.** colu
2c1b0 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 mn (i==1), and a
2c1c0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f NULL pointer fo
2c1d0 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75 r the first resu
2c1e0 6c 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 lt column (i==0)
2c1f0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 ..**.** SQLite u
2c200 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d ses dynamic run-
2c210 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f time typing. So
2c220 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20 just because a
2c230 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 column.** is dec
2c240 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e lared to contain
2c250 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 a particular ty
2c260 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e pe does not mean
2c270 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 that the.** dat
2c280 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 a stored in that
2c290 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 column is of th
2c2a0 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e e declared type.
2c2b0 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 SQLite is.** s
2c2c0 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 trongly typed, b
2c2d0 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 ut the typing is
2c2e0 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 dynamic not sta
2c2f0 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 tic. Type.** is
2c300 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
2c310 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75 individual valu
2c320 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 es, not with the
2c330 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 containers.** u
2c340 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 sed to hold thos
2c350 65 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 e values..**.**
2c360 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
2c370 2a 20 7b 48 31 33 37 36 31 7d 20 20 41 20 73 75 * {H13761} A su
2c380 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
2c390 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
2c3a0 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d 20 _decltype(S,N)]
2c3b0 72 65 74 75 72 6e 73 20 61 0a 2a 2a 20 20 20 20 returns a.**
2c3c0 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 6d zero-term
2c3d0 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 inated UTF-8 str
2c3e0 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ing containing t
2c3f0 68 65 20 64 65 63 6c 61 72 65 64 20 64 61 74 61 he declared data
2c400 74 79 70 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 type.**
2c410 20 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 of the table c
2c420 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 olumn that appea
2c430 72 73 20 61 73 20 74 68 65 20 4e 74 68 20 63 6f rs as the Nth co
2c440 6c 75 6d 6e 20 28 6e 75 6d 62 65 72 65 64 0a 2a lumn (numbered.*
2c450 2a 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d * from
2c460 20 30 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 0) of the resul
2c470 74 20 73 65 74 20 74 6f 20 74 68 65 20 5b 70 72 t set to the [pr
2c480 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2c490 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 ] S..**.** {H137
2c4a0 36 32 7d 20 20 41 20 73 75 63 63 65 73 73 66 75 62} A successfu
2c4b0 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
2c4c0 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 e3_column_declty
2c4d0 70 65 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 pe16(S,N)].**
2c4e0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 returns
2c4f0 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 a zero-terminate
2c500 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 d UTF-16 native
2c510 62 79 74 65 20 6f 72 64 65 72 20 73 74 72 69 6e byte order strin
2c520 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63 g.** c
2c530 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64 65 ontaining the de
2c540 63 6c 61 72 65 64 20 64 61 74 61 74 79 70 65 20 clared datatype
2c550 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c of the table col
2c560 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72 73 umn that appears
2c570 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 73 .** as
2c580 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
2c590 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 (numbered from 0
2c5a0 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 ) of the result
2c5b0 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 set to the.**
2c5c0 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 [prepare
2c5d0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2e 0a d statement] S..
2c5e0 2a 2a 0a 2a 2a 20 7b 48 31 33 37 36 33 7d 20 20 **.** {H13763}
2c5f0 49 66 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 If N is less tha
2c600 6e 20 30 20 6f 72 20 4e 20 69 73 20 67 72 65 61 n 0 or N is grea
2c610 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
2c620 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 l to.**
2c630 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 the number of
2c640 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 5b columns in the [
2c650 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2c660 6e 74 5d 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 nt] S,.**
2c670 20 20 20 20 6f 72 20 69 66 20 74 68 65 20 4e 74 or if the Nt
2c680 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 h column of S is
2c690 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f an expression o
2c6a0 72 20 73 75 62 71 75 65 72 79 20 72 61 74 68 65 r subquery rathe
2c6b0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 r.** t
2c6c0 68 61 6e 20 61 20 74 61 62 6c 65 20 63 6f 6c 75 han a table colu
2c6d0 6d 6e 2c 20 6f 72 20 69 66 20 61 20 6d 65 6d 6f mn, or if a memo
2c6e0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
2c6f0 69 6c 75 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 ilure.**
2c700 20 20 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 occurs during
2c710 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 encoding conver
2c720 73 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20 20 sions, then.**
2c730 20 20 20 20 20 20 20 20 20 63 61 6c 6c 73 20 74 calls t
2c740 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d o [sqlite3_colum
2c750 6e 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d n_decltype(S,N)]
2c760 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or.**
2c770 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
2c780 5f 64 65 63 6c 74 79 70 65 31 36 28 53 2c 4e 29 _decltype16(S,N)
2c790 5d 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a ] return NULL..*
2c7a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
2c7b0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
2c7c0 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 _column_decltype
2c7d0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 (sqlite3_stmt*,i
2c7e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
2c7f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
2c800 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 te3_column_declt
2c810 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ype16(sqlite3_st
2c820 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a mt*,int);../*.**
2c830 20 43 41 50 49 33 52 45 46 3a 20 45 76 61 6c 75 CAPI3REF: Evalu
2c840 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61 74 65 ate An SQL State
2c850 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20 3c 53 ment {H13200} <S
2c860 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 74 10000>.**.** Aft
2c870 65 72 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 er a [prepared s
2c880 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20 62 65 tatement] has be
2c890 65 6e 20 70 72 65 70 61 72 65 64 20 75 73 69 6e en prepared usin
2c8a0 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c g either.** [sql
2c8b0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
2c8c0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 )] or [sqlite3_p
2c8d0 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f repare16_v2()] o
2c8e0 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 65 67 r one of the leg
2c8f0 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 acy.** interface
2c900 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 s [sqlite3_prepa
2c910 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 re()] or [sqlite
2c920 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c 20 3_prepare16()],
2c930 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a this function.**
2c940 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 must be called
2c950 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 one or more time
2c960 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 s to evaluate th
2c970 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a e statement..**.
2c980 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f ** The details o
2c990 66 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f f the behavior o
2c9a0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 f the sqlite3_st
2c9b0 65 70 28 29 20 69 6e 74 65 72 66 61 63 65 20 64 ep() interface d
2c9c0 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68 65 74 epend.** on whet
2c9d0 68 65 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e her the statemen
2c9e0 74 20 77 61 73 20 70 72 65 70 61 72 65 64 20 75 t was prepared u
2c9f0 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72 20 22 sing the newer "
2ca00 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a v2" interface.**
2ca10 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
2ca20 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c e_v2()] and [sql
2ca30 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 ite3_prepare16_v
2ca40 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c 64 65 2()] or the olde
2ca50 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 r legacy.** inte
2ca60 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33 5f 70 rface [sqlite3_p
2ca70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 repare()] and [s
2ca80 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
2ca90 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20 6f 66 ()]. The use of
2caa0 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76 32 22 the.** new "v2"
2cab0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 interface is re
2cac0 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6e 65 commended for ne
2cad0 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 62 w applications b
2cae0 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a ut the legacy.**
2caf0 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 interface will
2cb00 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 20 73 continue to be s
2cb10 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 upported..**.**
2cb20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e In the legacy in
2cb30 74 65 72 66 61 63 65 2c 20 74 68 65 20 72 65 74 terface, the ret
2cb40 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 urn value will b
2cb50 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 e either [SQLITE
2cb60 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 _BUSY],.** [SQLI
2cb70 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 TE_DONE], [SQLIT
2cb80 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54 45 5f E_ROW], [SQLITE_
2cb90 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c 49 ERROR], or [SQLI
2cba0 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a 20 57 TE_MISUSE]..** W
2cbb0 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e 74 ith the "v2" int
2cbc0 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66 20 74 erface, any of t
2cbd0 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75 6c 74 he other [result
2cbe0 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65 codes] or.** [e
2cbf0 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
2cc00 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65 20 72 odes] might be r
2cc10 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c 6c 2e eturned as well.
2cc20 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 .**.** [SQLITE_B
2cc30 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 USY] means that
2cc40 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67 the database eng
2cc50 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65 20 74 ine was unable t
2cc60 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a 2a 2a o acquire the.**
2cc70 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 20 database locks
2cc80 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f 20 69 it needs to do i
2cc90 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68 65 20 ts job. If the
2cca0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b statement is a [
2ccb0 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63 COMMIT].** or oc
2ccc0 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f 66 20 curs outside of
2ccd0 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e an explicit tran
2cce0 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f saction, then yo
2ccf0 75 20 63 61 6e 20 72 65 74 72 79 20 74 68 65 0a u can retry the.
2cd00 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 ** statement. I
2cd10 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
2cd20 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 54 is not a [COMMIT
2cd30 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77 69 74 ] and occurs wit
2cd40 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69 63 69 hin a.** explici
2cd50 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 t transaction th
2cd60 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72 6f en you should ro
2cd70 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 llback the trans
2cd80 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a action before.**
2cd90 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a continuing..**.
2cda0 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d ** [SQLITE_DONE]
2cdb0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
2cdc0 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66 69 statement has fi
2cdd0 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67 nished executing
2cde0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 .** successfully
2cdf0 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 . sqlite3_step(
2ce00 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ) should not be
2ce10 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 20 called again on
2ce20 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a 20 this virtual.**
2ce30 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74 20 machine without
2ce40 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b 73 first calling [s
2ce50 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
2ce60 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72 to reset the vir
2ce70 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 tual.** machine
2ce80 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 back to its init
2ce90 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a ial state..**.**
2cea0 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 If the SQL stat
2ceb0 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65 63 ement being exec
2cec0 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e 79 uted returns any
2ced0 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 51 4c data, then [SQL
2cee0 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20 72 ITE_ROW].** is r
2cef0 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 69 6d eturned each tim
2cf00 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 20 64 e a new row of d
2cf10 61 74 61 20 69 73 20 72 65 61 64 79 20 66 6f 72 ata is ready for
2cf20 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 20 74 processing by t
2cf30 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 68 he.** caller. Th
2cf40 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20 e values may be
2cf50 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 20 74 accessed using t
2cf60 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 he [column acces
2cf70 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a s functions]..**
2cf80 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
2cf90 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 is called again
2cfa0 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 to retrieve the
2cfb0 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74 61 next row of data
2cfc0 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f ..**.** [SQLITE_
2cfd0 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 68 61 ERROR] means tha
2cfe0 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 t a run-time err
2cff0 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 63 6f or (such as a co
2d000 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 6f 6c nstraint.** viol
2d010 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 75 72 ation) has occur
2d020 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 red. sqlite3_st
2d030 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ep() should not
2d040 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 be called again
2d050 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 4d 6f on.** the VM. Mo
2d060 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d re information m
2d070 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 20 63 ay be found by c
2d080 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f alling [sqlite3_
2d090 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57 69 errmsg()]..** Wi
2d0a0 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e th the legacy in
2d0b0 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 65 20 terface, a more
2d0c0 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 specific error c
2d0d0 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ode (for example
2d0e0 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 ,.** [SQLITE_INT
2d0f0 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 54 45 ERRUPT], [SQLITE
2d100 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 54 _SCHEMA], [SQLIT
2d110 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e 64 20 E_CORRUPT], and
2d120 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63 61 6e so forth).** can
2d130 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 be obtained by
2d140 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 calling [sqlite3
2d150 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74 68 65 _reset()] on the
2d160 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
2d170 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 68 atement]. In th
2d180 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 e "v2" interface
2d190 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 70 ,.** the more sp
2d1a0 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 6f 64 ecific error cod
2d1b0 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 64 69 e is returned di
2d1c0 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69 74 65 rectly by sqlite
2d1d0 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 3_step()..**.**
2d1e0 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 [SQLITE_MISUSE]
2d1f0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 means that the t
2d200 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 his routine was
2d210 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f 70 72 called inappropr
2d220 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 61 iately..** Perha
2d230 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c 65 64 ps it was called
2d240 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 on a [prepared
2d250 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 statement] that
2d260 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 has.** already b
2d270 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e een [sqlite3_fin
2d280 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 alize | finalize
2d290 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74 68 61 d] or on one tha
2d2a0 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69 6f 75 t had.** previou
2d2b0 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b 53 51 sly returned [SQ
2d2c0 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72 20 5b LITE_ERROR] or [
2d2d0 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f SQLITE_DONE]. O
2d2e0 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20 62 65 r it could.** be
2d2f0 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 74 the case that t
2d300 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
2d310 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 62 connection is b
2d320 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 77 6f eing used by two
2d330 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68 72 65 or.** more thre
2d340 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 ads at the same
2d350 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65 2e 0a moment in time..
2d360 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 49 **.** <b>Goofy I
2d370 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74 3a 3c nterface Alert:<
2d380 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67 61 63 /b> In the legac
2d390 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 y interface, the
2d3a0 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a sqlite3_step().
2d3b0 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20 72 65 ** API always re
2d3c0 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 63 20 turns a generic
2d3d0 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53 51 4c error code, [SQL
2d3e0 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c ITE_ERROR], foll
2d3f0 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65 72 72 owing any.** err
2d400 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 or other than [S
2d410 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e 64 20 QLITE_BUSY] and
2d420 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e [SQLITE_MISUSE].
2d430 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c 6c 0a You must call.
2d440 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 ** [sqlite3_rese
2d450 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 t()] or [sqlite3
2d460 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 6e 20 _finalize()] in
2d470 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20 6f 6e order to find on
2d480 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70 65 63 e of the.** spec
2d490 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 ific [error code
2d4a0 73 5d 20 74 68 61 74 20 62 65 74 74 65 72 20 64 s] that better d
2d4b0 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72 escribes the err
2d4c0 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69 74 20 or..** We admit
2d4d0 74 68 61 74 20 74 68 69 73 20 69 73 20 61 20 67 that this is a g
2d4e0 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20 54 68 oofy design. Th
2d4f0 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20 62 65 e problem has be
2d500 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69 74 68 en fixed.** with
2d510 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 the "v2" interf
2d520 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70 72 65 ace. If you pre
2d530 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f 75 72 pare all of your
2d540 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a SQL statements.
2d550 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 ** using either
2d560 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
2d570 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 _v2()] or [sqlit
2d580 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 e3_prepare16_v2(
2d590 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 )] instead.** of
2d5a0 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73 71 6c the legacy [sql
2d5b0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
2d5c0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 and [sqlite3_pre
2d5d0 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65 72 66 pare16()] interf
2d5e0 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 aces,.** then th
2d5f0 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20 e more specific
2d600 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 61 72 [error codes] ar
2d610 65 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 e returned direc
2d620 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 tly.** by sqlite
2d630 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65 20 75 3_step(). The u
2d640 73 65 20 6f 66 20 74 68 65 20 22 76 32 22 20 69 se of the "v2" i
2d650 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63 6f nterface is reco
2d660 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 mmended..**.** I
2d670 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
2d680 20 7b 48 31 33 32 30 32 7d 20 20 49 66 20 74 68 {H13202} If th
2d690 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
2d6a0 65 6d 65 6e 74 5d 20 53 20 69 73 20 72 65 61 64 ement] S is read
2d6b0 79 20 74 6f 20 62 65 20 72 75 6e 2c 20 74 68 65 y to be run, the
2d6c0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b n.** [
2d6d0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d sqlite3_step(S)]
2d6e0 20 61 64 76 61 6e 63 65 73 20 74 68 61 74 20 70 advances that p
2d6f0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2d700 74 20 75 6e 74 69 6c 0a 2a 2a 20 20 20 20 20 20 t until.**
2d710 20 20 20 20 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 completion
2d720 6f 72 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 or until it is r
2d730 65 61 64 79 20 74 6f 20 72 65 74 75 72 6e 20 61 eady to return a
2d740 6e 6f 74 68 65 72 20 72 6f 77 20 6f 66 20 74 68 nother row of th
2d750 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72 e.** r
2d760 65 73 75 6c 74 20 73 65 74 2c 20 6f 72 20 75 6e esult set, or un
2d770 74 69 6c 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f til an [sqlite3_
2d780 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65 interrupt | inte
2d790 72 72 75 70 74 5d 0a 2a 2a 20 20 20 20 20 20 20 rrupt].**
2d7a0 20 20 20 20 6f 72 20 61 20 72 75 6e 2d 74 69 6d or a run-tim
2d7b0 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a e error occurs..
2d7c0 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30 34 7d 20 20 **.** {H15304}
2d7d0 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b When a call to [
2d7e0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d sqlite3_step(S)]
2d7f0 20 63 61 75 73 65 73 20 74 68 65 20 5b 70 72 65 causes the [pre
2d800 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
2d810 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 53 20 .** S
2d820 74 6f 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 to run to comple
2d830 74 69 6f 6e 2c 20 74 68 65 20 66 75 6e 63 74 69 tion, the functi
2d840 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 on returns [SQLI
2d850 54 45 5f 44 4f 4e 45 5d 2e 0a 2a 2a 0a 2a 2a 20 TE_DONE]..**.**
2d860 7b 48 31 35 33 30 36 7d 20 20 57 68 65 6e 20 61 {H15306} When a
2d870 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
2d880 33 5f 73 74 65 70 28 53 29 5d 20 73 74 6f 70 73 3_step(S)] stops
2d890 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 72 because it is r
2d8a0 65 61 64 79 20 74 6f 0a 2a 2a 20 20 20 20 20 20 eady to.**
2d8b0 20 20 20 20 20 72 65 74 75 72 6e 20 61 6e 6f 74 return anot
2d8c0 68 65 72 20 72 6f 77 20 6f 66 20 74 68 65 20 72 her row of the r
2d8d0 65 73 75 6c 74 20 73 65 74 2c 20 69 74 20 72 65 esult set, it re
2d8e0 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f turns [SQLITE_RO
2d8f0 57 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30 W]..**.** {H1530
2d900 38 7d 20 20 49 66 20 61 20 63 61 6c 6c 20 74 6f 8} If a call to
2d910 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
2d920 29 5d 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 6e )] encounters an
2d930 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 .** [s
2d940 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
2d950 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 20 6f 72 | interrupt] or
2d960 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f a run-time erro
2d970 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 r,.**
2d980 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 61 70 it returns an ap
2d990 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 propriate error
2d9a0 63 6f 64 65 20 74 68 61 74 20 69 73 20 6e 6f 74 code that is not
2d9b0 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 one of.**
2d9c0 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d [SQLITE_OK]
2d9d0 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 , [SQLITE_ROW],
2d9e0 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d or [SQLITE_DONE]
2d9f0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 31 30 7d ..**.** {H15310}
2da00 20 20 49 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 If an [sqlite3
2da10 5f 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 _interrupt | int
2da20 65 72 72 75 70 74 5d 20 6f 72 20 61 20 72 75 6e errupt] or a run
2da30 2d 74 69 6d 65 20 65 72 72 6f 72 0a 2a 2a 20 20 -time error.**
2da40 20 20 20 20 20 20 20 20 20 6f 63 63 75 72 73 20 occurs
2da50 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f during a call to
2da60 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
2da70 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 )].**
2da80 66 6f 72 20 61 20 5b 70 72 65 70 61 72 65 64 20 for a [prepared
2da90 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 63 72 65 statement] S cre
2daa0 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 ated using.**
2dab0 20 20 20 20 20 20 20 20 6c 65 67 61 63 79 20 69 legacy i
2dac0 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69 74 nterfaces [sqlit
2dad0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 e3_prepare()] or
2dae0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 .** [s
2daf0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
2db00 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 66 75 ()], then the fu
2db10 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 65 nction returns e
2db20 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 ither.**
2db30 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 [SQLITE_ERROR
2db40 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d ], [SQLITE_BUSY]
2db50 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53 , or [SQLITE_MIS
2db60 55 53 45 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f USE]..*/.SQLITE_
2db70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
2db80 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d step(sqlite3_stm
2db90 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t*);../*.** CAPI
2dba0 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20 3REF: Number of
2dbb0 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 73 columns in a res
2dbc0 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 7d ult set {H13770}
2dbd0 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
2dbe0 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 Returns the numb
2dbf0 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 20 er of values in
2dc00 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 the current row
2dc10 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
2dc20 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 t..**.** INVARIA
2dc30 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 NTS:.**.** {H137
2dc40 37 31 7d 20 20 41 66 74 65 72 20 61 20 63 61 6c 71} After a cal
2dc50 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 l to [sqlite3_st
2dc60 65 70 28 53 29 5d 20 74 68 61 74 20 72 65 74 75 ep(S)] that retu
2dc70 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d rns [SQLITE_ROW]
2dc80 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 ,.** t
2dc90 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 61 74 61 he [sqlite3_data
2dca0 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f 75 74 69 _count(S)] routi
2dcb0 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74 ne will return t
2dcc0 68 65 20 73 61 6d 65 20 76 61 6c 75 65 0a 2a 2a he same value.**
2dcd0 20 20 20 20 20 20 20 20 20 20 20 61 73 20 74 68 as th
2dce0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e [sqlite3_colum
2dcf0 6e 5f 63 6f 75 6e 74 28 53 29 5d 20 66 75 6e 63 n_count(S)] func
2dd00 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 tion..**.** {H13
2dd10 37 37 32 7d 20 20 41 66 74 65 72 20 5b 73 71 6c 772} After [sql
2dd20 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 ite3_step(S)] ha
2dd30 73 20 72 65 74 75 72 6e 65 64 20 61 6e 79 20 76 s returned any v
2dd40 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 0a alue other than.
2dd50 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 53 51 ** [SQ
2dd60 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 62 65 66 LITE_ROW] or bef
2dd70 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 ore [sqlite3_ste
2dd80 70 28 53 29 5d 20 68 61 73 20 62 65 65 6e 20 63 p(S)] has been c
2dd90 61 6c 6c 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 alled on the.**
2dda0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
2ddb0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 66 red statement] f
2ddc0 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 6d or the first tim
2ddd0 65 20 73 69 6e 63 65 20 69 74 20 77 61 73 0a 2a e since it was.*
2dde0 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c * [sql
2ddf0 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 ite3_prepare | p
2de00 72 65 70 61 72 65 64 5d 20 6f 72 20 5b 73 71 6c repared] or [sql
2de10 69 74 65 33 5f 72 65 73 65 74 20 7c 20 72 65 73 ite3_reset | res
2de20 65 74 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 et],.**
2de30 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 the [sqlite3_d
2de40 61 74 61 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f ata_count(S)] ro
2de50 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a 65 utine returns ze
2de60 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ro..*/.SQLITE_AP
2de70 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 61 I int sqlite3_da
2de80 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 ta_count(sqlite3
2de90 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a _stmt *pStmt);..
2dea0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2deb0 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74 61 Fundamental Data
2dec0 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20 3c types {H10265} <
2ded0 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e 0a S10110><S10120>.
2dee0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c ** KEYWORDS: SQL
2def0 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20 7b ITE_TEXT.**.** {
2df00 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76 61 H10266} Every va
2df10 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68 61 lue in SQLite ha
2df20 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66 75 s one of five fu
2df30 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74 79 ndamental dataty
2df40 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a pes:.**.** <ul>.
2df50 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 73 ** <li> 64-bit s
2df60 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
2df70 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45 45 <li> 64-bit IEE
2df80 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 E floating point
2df90 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e 20 number.** <li>
2dfa0 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 42 string.** <li> B
2dfb0 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c 4c LOB.** <li> NULL
2dfc0 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a .** </ul> {END}.
2dfd0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 **.** These cons
2dfe0 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73 20 tants are codes
2dff0 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f 73 for each of thos
2e000 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e e types..**.** N
2e010 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51 4c ote that the SQL
2e020 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 6e ITE_TEXT constan
2e030 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64 20 t was also used
2e040 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f in SQLite versio
2e050 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d n 2.** for a com
2e060 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65 6e pletely differen
2e070 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 74 t meaning. Soft
2e080 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 ware that links
2e090 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a 20 against both.**
2e0a0 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 SQLite version 2
2e0b0 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72 73 and SQLite vers
2e0c0 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 65 ion 3 should use
2e0d0 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20 6e SQLITE3_TEXT, n
2e0e0 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 58 ot.** SQLITE_TEX
2e0f0 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 T..*/.#define SQ
2e100 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31 0a LITE_INTEGER 1.
2e110 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
2e120 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69 6e LOAT 2.#defin
2e130 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20 20 e SQLITE_BLOB
2e140 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 4.#define SQLI
2e150 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23 69 TE_NULL 5.#i
2e160 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 fdef SQLITE_TEXT
2e170 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f .# undef SQLITE_
2e180 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65 66 TEXT.#else.# def
2e190 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20 ine SQLITE_TEXT
2e1a0 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64 65 3.#endif.#de
2e1b0 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45 58 fine SQLITE3_TEX
2e1c0 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 T 3../*.** C
2e1d0 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 API3REF: Result
2e1e0 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 75 Values From A Qu
2e1f0 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53 31 ery {H13800} <S1
2e200 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0700>.** KEYWORD
2e210 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 S: {column acces
2e220 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a s functions}.**.
2e230 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
2e240 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73 75 s form the "resu
2e250 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69 6e lt set query" in
2e260 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 terface..**.** T
2e270 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 hese routines re
2e280 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e turn information
2e290 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20 about a single
2e2a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 75 column of the cu
2e2b0 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74 20 rrent.** result
2e2c0 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e 20 row of a query.
2e2d0 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20 74 In every case t
2e2e0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
2e2f0 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a t is a pointer.*
2e300 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61 72 * to the [prepar
2e310 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 ed statement] th
2e320 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61 6c at is being eval
2e330 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c 69 uated (the [sqli
2e340 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74 68 te3_stmt*].** th
2e350 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 at was returned
2e360 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70 72 from [sqlite3_pr
2e370 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 6f epare_v2()] or o
2e380 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61 6e ne of its varian
2e390 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 ts).** and the s
2e3a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
2e3b0 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
2e3c0 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 he column for wh
2e3d0 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a ich information.
2e3e0 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 ** should be ret
2e3f0 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66 74 urned. The left
2e400 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 most column of t
2e410 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68 61 he result set ha
2e420 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a 2a s the index 0..*
2e430 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 *.** If the SQL
2e440 73 74 61 74 65 6d 65 6e 74 20 64 6f 65 73 20 6e statement does n
2e450 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 ot currently poi
2e460 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f nt to a valid ro
2e470 77 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 w, or if the.**
2e480 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 73 20 column index is
2e490 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 out of range, th
2e4a0 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65 e result is unde
2e4b0 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 fined..** These
2e4c0 72 6f 75 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c routines may onl
2e4d0 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e y be called when
2e4e0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
2e4f0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c call to.** [sql
2e500 69 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73 ite3_step()] has
2e510 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 returned [SQLIT
2e520 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68 E_ROW] and neith
2e530 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 er.** [sqlite3_r
2e540 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c eset()] nor [sql
2e550 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d ite3_finalize()]
2e560 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 have been calle
2e570 64 20 73 75 62 73 65 71 75 65 6e 74 6c 79 2e 0a d subsequently..
2e580 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 ** If any of the
2e590 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
2e5a0 63 61 6c 6c 65 64 20 61 66 74 65 72 20 5b 73 71 called after [sq
2e5b0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f lite3_reset()] o
2e5c0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 r.** [sqlite3_fi
2e5d0 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 nalize()] or aft
2e5e0 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 er [sqlite3_step
2e5f0 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 ()] has returned
2e600 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 .** something ot
2e610 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 her than [SQLITE
2e620 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 73 75 6c _ROW], the resul
2e630 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 ts are undefined
2e640 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 ..** If [sqlite3
2e650 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c _step()] or [sql
2e660 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 ite3_reset()] or
2e670 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
2e680 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 63 61 6c ze()].** are cal
2e690 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 66 66 65 led from a diffe
2e6a0 72 65 6e 74 20 74 68 72 65 61 64 20 77 68 69 6c rent thread whil
2e6b0 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 e any of these r
2e6c0 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 70 outines.** are p
2e6d0 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 ending, then the
2e6e0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 results are und
2e6f0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 efined..**.** Th
2e700 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
2e710 5f 74 79 70 65 28 29 20 72 6f 75 74 69 6e 65 20 _type() routine
2e720 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 5b returns the.** [
2e730 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c SQLITE_INTEGER |
2e740 20 64 61 74 61 74 79 70 65 20 63 6f 64 65 5d 20 datatype code]
2e750 66 6f 72 20 74 68 65 20 69 6e 69 74 69 61 6c 20 for the initial
2e760 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 6f 66 20 data type.** of
2e770 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d the result colum
2e780 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 n. The returned
2e790 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66 value is one of
2e7a0 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 [SQLITE_INTEGER
2e7b0 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 4c ],.** [SQLITE_FL
2e7c0 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 OAT], [SQLITE_TE
2e7d0 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 4c 4f XT], [SQLITE_BLO
2e7e0 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4e B], or [SQLITE_N
2e7f0 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 ULL]. The value
2e800 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79 20 .** returned by
2e810 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
2e820 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 20 6d 65 ype() is only me
2e830 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e 6f 20 74 aningful if no t
2e840 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f ype.** conversio
2e850 6e 73 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 ns have occurred
2e860 20 61 73 20 64 65 73 63 72 69 62 65 64 20 62 65 as described be
2e870 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 20 74 79 low. After a ty
2e880 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a pe conversion,.*
2e890 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 * the value retu
2e8a0 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
2e8b0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 column_type() is
2e8c0 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 46 75 74 undefined. Fut
2e8d0 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 ure.** versions
2e8e0 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68 of SQLite may ch
2e8f0 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69 6f ange the behavio
2e900 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c r of sqlite3_col
2e910 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a 20 66 6f umn_type().** fo
2e920 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 65 20 63 llowing a type c
2e930 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a onversion..**.**
2e940 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 If the result i
2e950 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 54 46 2d s a BLOB or UTF-
2e960 38 20 73 74 72 69 6e 67 20 74 68 65 6e 20 74 68 8 string then th
2e970 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
2e980 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72 6f 75 74 _bytes().** rout
2e990 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
2e9a0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
2e9b0 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f 72 20 in that BLOB or
2e9c0 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 20 74 68 string..** If th
2e9d0 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 55 54 e result is a UT
2e9e0 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 74 68 65 F-16 string, the
2e9f0 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e n sqlite3_column
2ea00 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 65 72 74 _bytes() convert
2ea10 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 s.** the string
2ea20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 74 68 65 to UTF-8 and the
2ea30 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 n returns the nu
2ea40 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e 0a 2a mber of bytes..*
2ea50 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 * If the result
2ea60 69 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c is a numeric val
2ea70 75 65 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f ue then sqlite3_
2ea80 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 75 column_bytes() u
2ea90 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ses.** [sqlite3_
2eaa0 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 63 snprintf()] to c
2eab0 6f 6e 76 65 72 74 20 74 68 61 74 20 76 61 6c 75 onvert that valu
2eac0 65 20 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 e to a UTF-8 str
2ead0 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 0a ing and returns.
2eae0 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
2eaf0 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 73 bytes in that s
2eb00 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 76 61 tring..** The va
2eb10 6c 75 65 20 72 65 74 75 72 6e 65 64 20 64 6f 65 lue returned doe
2eb20 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 s not include th
2eb30 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f e zero terminato
2eb40 72 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 r at the end.**
2eb50 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 of the string.
2eb60 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74 68 65 For clarity: the
2eb70 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
2eb80 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
2eb90 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65 .** bytes in the
2eba0 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74 68 65 string, not the
2ebb0 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 number of chara
2ebc0 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 cters..**.** Str
2ebd0 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 ings returned by
2ebe0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
2ebf0 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c 69 74 text() and sqlit
2ec00 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
2ec10 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 6d 70 74 (),.** even empt
2ec20 79 20 73 74 72 69 6e 67 73 2c 20 61 72 65 20 61 y strings, are a
2ec30 6c 77 61 79 73 20 7a 65 72 6f 20 74 65 72 6d 69 lways zero termi
2ec40 6e 61 74 65 64 2e 20 20 54 68 65 20 72 65 74 75 nated. The retu
2ec50 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 72 6f 6d rn.** value from
2ec60 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
2ec70 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 7a 65 72 blob() for a zer
2ec80 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 20 69 73 o-length BLOB is
2ec90 20 61 6e 20 61 72 62 69 74 72 61 72 79 0a 2a 2a an arbitrary.**
2eca0 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73 73 69 62 pointer, possib
2ecb0 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c 4c 20 70 ly even a NULL p
2ecc0 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ointer..**.** Th
2ecd0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
2ece0 5f 62 79 74 65 73 31 36 28 29 20 72 6f 75 74 69 _bytes16() routi
2ecf0 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f ne is similar to
2ed00 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
2ed10 62 79 74 65 73 28 29 0a 2a 2a 20 62 75 74 20 6c bytes().** but l
2ed20 65 61 76 65 73 20 74 68 65 20 72 65 73 75 6c 74 eaves the result
2ed30 20 69 6e 20 55 54 46 2d 31 36 20 69 6e 20 6e 61 in UTF-16 in na
2ed40 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 tive byte order
2ed50 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d 38 instead of UTF-8
2ed60 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 20 74 65 ..** The zero te
2ed70 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e 6f 74 20 rminator is not
2ed80 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 69 73 included in this
2ed90 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 count..**.** Th
2eda0 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 e object returne
2edb0 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f d by [sqlite3_co
2edc0 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 lumn_value()] is
2edd0 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 an.** [unprotec
2ede0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
2edf0 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75 e] object. An u
2ee00 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 nprotected sqlit
2ee10 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a e3_value object.
2ee20 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 ** may only be u
2ee30 73 65 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 sed with [sqlite
2ee40 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20 3_bind_value()]
2ee50 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 and [sqlite3_res
2ee60 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a ult_value()]..**
2ee70 20 49 66 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 If the [unprote
2ee80 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2ee90 75 65 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 72 ue] object retur
2eea0 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 ned by.** [sqlit
2eeb0 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 e3_column_value(
2eec0 29 5d 20 69 73 20 75 73 65 64 20 69 6e 20 61 6e )] is used in an
2eed0 79 20 6f 74 68 65 72 20 77 61 79 2c 20 69 6e 63 y other way, inc
2eee0 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 luding calls.**
2eef0 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 to routines like
2ef00 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
2ef10 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 int()], [sqlite3
2ef20 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a _value_text()],.
2ef30 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 ** or [sqlite3_v
2ef40 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 74 alue_bytes()], t
2ef50 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 hen the behavior
2ef60 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
2ef70 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
2ef80 6e 65 73 20 61 74 74 65 6d 70 74 20 74 6f 20 63 nes attempt to c
2ef90 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c 75 65 onvert the value
2efa0 20 77 68 65 72 65 20 61 70 70 72 6f 70 72 69 61 where appropria
2efb0 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d te. For.** exam
2efc0 70 6c 65 2c 20 69 66 20 74 68 65 20 69 6e 74 65 ple, if the inte
2efd0 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 rnal representat
2efe0 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e 64 ion is FLOAT and
2eff0 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 0a 2a a text result.*
2f000 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 * is requested,
2f010 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 [sqlite3_snprint
2f020 66 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e 74 f()] is used int
2f030 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f ernally to perfo
2f040 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 rm the.** conver
2f050 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c sion automatical
2f060 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ly. The followi
2f070 6e 67 20 74 61 62 6c 65 20 64 65 74 61 69 6c 73 ng table details
2f080 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 the conversions
2f090 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 61 70 70 .** that are app
2f0a0 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f lied:.**.** <blo
2f0b0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 ckquote>.** <tab
2f0c0 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a le border="1">.*
2f0d0 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72 * <tr><th> Inter
2f0e0 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e nal<br>Type <th>
2f0f0 20 52 65 71 75 65 73 74 65 64 3c 62 72 3e 54 79 Requested<br>Ty
2f100 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73 pe <th> Convers
2f110 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 ion.**.** <tr><t
2f120 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e d> NULL <td>
2f130 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 INTEGER <td>
2f140 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c Result is 0.** <
2f150 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 tr><td> NULL
2f160 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 <td> FLOAT
2f170 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 <td> Result is 0
2f180 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 .0.** <tr><td>
2f190 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 54 NULL <td> T
2f1a0 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 EXT <td> Resu
2f1b0 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 lt is NULL point
2f1c0 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 er.** <tr><td>
2f1d0 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 42 NULL <td> B
2f1e0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73 75 LOB <td> Resu
2f1f0 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 lt is NULL point
2f200 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 er.** <tr><td> I
2f210 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 4c NTEGER <td> FL
2f220 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 OAT <td> Conv
2f230 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 72 ert from integer
2f240 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 to float.** <tr
2f250 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c ><td> INTEGER <
2f260 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 td> TEXT <t
2f270 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 d> ASCII renderi
2f280 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ng of the intege
2f290 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e r.** <tr><td> IN
2f2a0 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42 4c TEGER <td> BL
2f2b0 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 OB <td> Same
2f2c0 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58 54 as INTEGER->TEXT
2f2d0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c .** <tr><td> FL
2f2e0 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 OAT <td> INTEG
2f2f0 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 ER <td> Conver
2f300 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 t from float to
2f310 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c integer.** <tr><
2f320 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 td> FLOAT <td
2f330 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e > TEXT <td>
2f340 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 ASCII rendering
2f350 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a of the float.**
2f360 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 <tr><td> FLOAT
2f370 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 <td> BLOB
2f380 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 46 <td> Same as F
2f390 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 LOAT->TEXT.** <t
2f3a0 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 r><td> TEXT
2f3b0 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c <td> INTEGER <
2f3c0 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a 2a td> Use atoi().*
2f3d0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 * <tr><td> TEXT
2f3e0 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <td> FLOAT
2f3f0 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 66 <td> Use atof
2f400 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 ().** <tr><td>
2f410 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20 42 TEXT <td> B
2f420 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63 LOB <td> No c
2f430 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 hange.** <tr><td
2f440 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 > BLOB <td>
2f450 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 INTEGER <td> C
2f460 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 onvert to TEXT t
2f470 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a 2a hen use atoi().*
2f480 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 * <tr><td> BLOB
2f490 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <td> FLOAT
2f4a0 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 <td> Convert
2f4b0 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65 to TEXT then use
2f4c0 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c atof().** <tr><
2f4d0 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 td> BLOB <td
2f4e0 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e > TEXT <td>
2f4f0 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72 6d Add a zero term
2f500 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65 64 inator if needed
2f510 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 .** </table>.**
2f520 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a </blockquote>.**
2f530 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61 62 .** The table ab
2f540 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 72 65 ove makes refere
2f550 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 64 20 nce to standard
2f560 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 C library functi
2f570 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20 61 6e ons atoi().** an
2f580 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c 69 74 d atof(). SQLit
2f590 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c e does not reall
2f5a0 79 20 75 73 65 20 74 68 65 73 65 20 66 75 6e 63 y use these func
2f5b0 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 20 69 tions. It has i
2f5c0 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 69 76 61 ts.** own equiva
2f5d0 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20 72 6f lent internal ro
2f5e0 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61 74 6f utines. The ato
2f5f0 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29 20 6e i() and atof() n
2f600 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 ames are.** used
2f610 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 66 6f in the table fo
2f620 72 20 62 72 65 76 69 74 79 20 61 6e 64 20 62 65 r brevity and be
2f630 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20 66 cause they are f
2f640 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73 74 0a amiliar to most.
2f650 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65 72 73 ** C programmers
2f660 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
2f670 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f 6e 76 t when type conv
2f680 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c 20 70 ersions occur, p
2f690 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 ointers returned
2f6a0 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c by prior.** cal
2f6b0 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ls to sqlite3_co
2f6c0 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 6c lumn_blob(), sql
2f6d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
2f6e0 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71 (), and/or.** sq
2f6f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
2f700 74 31 36 28 29 20 6d 61 79 20 62 65 20 69 6e 76 t16() may be inv
2f710 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 54 79 70 alidated..** Typ
2f720 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 6e e conversions an
2f730 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61 6c 69 d pointer invali
2f740 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6f 63 dations might oc
2f750 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f cur.** in the fo
2f760 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a 0a 2a llowing cases:.*
2f770 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
2f780 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f > The initial co
2f790 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f 42 20 ntent is a BLOB
2f7a0 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 and sqlite3_colu
2f7b0 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a 2a 20 mn_text() or.**
2f7c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
2f7d0 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 umn_text16() is
2f7e0 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d called. A zero-
2f7f0 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74 terminator might
2f800 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 20 74 6f .** need to
2f810 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 be added to the
2f820 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a string.</li>.**
2f830 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 <li> The initia
2f840 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 l content is UTF
2f850 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69 -8 text and sqli
2f860 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
2f870 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 16() or.**
2f880 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
2f890 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c 65 ext16() is calle
2f8a0 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 d. The content
2f8b0 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 must be converte
2f8c0 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 d.** to UTF
2f8d0 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 -16.</li>.** <li
2f8e0 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f > The initial co
2f8f0 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 31 36 20 ntent is UTF-16
2f900 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 text and sqlite3
2f910 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 _column_bytes()
2f920 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 or.** sqlit
2f930 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
2f940 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 is called. The
2f950 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 content must be
2f960 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 converted.**
2f970 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69 to UTF-8.</li
2f980 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a >.** </ul>.**.**
2f990 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 74 Conversions bet
2f9a0 77 65 65 6e 20 55 54 46 2d 31 36 62 65 20 61 6e ween UTF-16be an
2f9b0 64 20 55 54 46 2d 31 36 6c 65 20 61 72 65 20 61 d UTF-16le are a
2f9c0 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e 20 70 6c lways done in pl
2f9d0 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f ace and do.** no
2f9e0 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 20 70 t invalidate a p
2f9f0 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c 20 74 68 rior pointer, th
2fa00 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74 ough of course t
2fa10 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 he content of th
2fa20 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 68 61 74 e buffer.** that
2fa30 20 74 68 65 20 70 72 69 6f 72 20 70 6f 69 6e 74 the prior point
2fa40 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 77 69 6c er points to wil
2fa50 6c 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 l have been modi
2fa60 66 69 65 64 2e 20 20 4f 74 68 65 72 20 6b 69 6e fied. Other kin
2fa70 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 73 ds.** of convers
2fa80 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20 69 6e 20 ion are done in
2fa90 70 6c 61 63 65 20 77 68 65 6e 20 69 74 20 69 73 place when it is
2faa0 20 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 73 possible, but s
2fab0 6f 6d 65 74 69 6d 65 73 20 74 68 65 79 0a 2a 2a ometimes they.**
2fac0 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c are not possibl
2fad0 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 65 20 63 e and in those c
2fae0 61 73 65 73 20 70 72 69 6f 72 20 70 6f 69 6e 74 ases prior point
2faf0 65 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 ers are invalida
2fb00 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ted..**.** The s
2fb10 61 66 65 73 74 20 61 6e 64 20 65 61 73 69 65 73 afest and easies
2fb20 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 70 6f t to remember po
2fb30 6c 69 63 79 20 69 73 20 74 6f 20 69 6e 76 6f 6b licy is to invok
2fb40 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 e these routines
2fb50 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 .** in one of th
2fb60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 61 79 73 e following ways
2fb70 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
2fb80 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c <li>sqlite3_col
2fb90 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f 6c 6c 6f umn_text() follo
2fba0 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 wed by sqlite3_c
2fbb0 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c olumn_bytes()</l
2fbc0 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 i>.** <li>sqlit
2fbd0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 e3_column_blob()
2fbe0 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c followed by sql
2fbf0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
2fc00 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 s()</li>.** <li
2fc10 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f >sqlite3_column_
2fc20 74 65 78 74 31 36 28 29 20 66 6f 6c 6c 6f 77 65 text16() followe
2fc30 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c d by sqlite3_col
2fc40 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 3c 2f 6c umn_bytes16()</l
2fc50 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a i>.** </ul>.**.*
2fc60 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
2fc70 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20 63 61 6c , you should cal
2fc80 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e l sqlite3_column
2fc90 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73 71 6c 69 _text(),.** sqli
2fca0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
2fcb0 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f ), or sqlite3_co
2fcc0 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 69 lumn_text16() fi
2fcd0 72 73 74 20 74 6f 20 66 6f 72 63 65 20 74 68 65 rst to force the
2fce0 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20 result.** into
2fcf0 74 68 65 20 64 65 73 69 72 65 64 20 66 6f 72 6d the desired form
2fd00 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 at, then invoke
2fd10 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
2fd20 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 73 71 6c ytes() or.** sql
2fd30 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
2fd40 73 31 36 28 29 20 74 6f 20 66 69 6e 64 20 74 68 s16() to find th
2fd50 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 72 65 e size of the re
2fd60 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74 20 6d 69 sult. Do not mi
2fd70 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 x calls.** to sq
2fd80 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
2fd90 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 t() or sqlite3_c
2fda0 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 74 olumn_blob() wit
2fdb0 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 h calls to.** sq
2fdc0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
2fdd0 65 73 31 36 28 29 2c 20 61 6e 64 20 64 6f 20 6e es16(), and do n
2fde0 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20 74 6f 20 ot mix calls to
2fdf0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
2fe00 65 78 74 31 36 28 29 0a 2a 2a 20 77 69 74 68 20 ext16().** with
2fe10 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
2fe20 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 2e _column_bytes().
2fe30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 .**.** The point
2fe40 65 72 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 ers returned are
2fe50 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 61 20 74 valid until a t
2fe60 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f ype conversion o
2fe70 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 65 73 63 ccurs as.** desc
2fe80 72 69 62 65 64 20 61 62 6f 76 65 2c 20 6f 72 20 ribed above, or
2fe90 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f 73 until [sqlite3_s
2fea0 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 tep()] or [sqlit
2feb0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a e3_reset()] or.*
2fec0 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c * [sqlite3_final
2fed0 69 7a 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 ize()] is called
2fee0 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 73 70 . The memory sp
2fef0 61 63 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 ace used to hold
2ff00 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 6e 64 20 strings.** and
2ff10 42 4c 4f 42 73 20 69 73 20 66 72 65 65 64 20 61 BLOBs is freed a
2ff20 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 44 utomatically. D
2ff30 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73 o <b>not</b> pas
2ff40 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 72 s the pointers r
2ff50 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69 eturned.** [sqli
2ff60 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
2ff70 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c )], [sqlite3_col
2ff80 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 65 74 63 umn_text()], etc
2ff90 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 . into.** [sqlit
2ffa0 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a e3_free()]..**.*
2ffb0 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c * If a memory al
2ffc0 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f location error o
2ffd0 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 ccurs during the
2ffe0 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 61 evaluation of a
2fff0 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 72 ny.** of these r
30000 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 66 61 75 outines, a defau
30010 6c 74 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 lt value is retu
30020 72 6e 65 64 2e 20 20 54 68 65 20 64 65 66 61 75 rned. The defau
30030 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 65 lt value.** is e
30040 69 74 68 65 72 20 74 68 65 20 69 6e 74 65 67 65 ither the intege
30050 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 74 69 6e r 0, the floatin
30060 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 20 30 g point number 0
30070 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a .0, or a NULL.**
30080 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 62 73 65 pointer. Subse
30090 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b quent calls to [
300a0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
300b0 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a )] will return.*
300c0 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d * [SQLITE_NOMEM]
300d0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
300e0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 30 TS:.**.** {H1380
300f0 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 3} The [sqlite3_
30100 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e 29 column_blob(S,N)
30110 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 ] interface conv
30120 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 erts the.**
30130 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 Nth column
30140 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 in the current r
30150 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ow of the result
30160 20 73 65 74 20 66 6f 72 0a 2a 2a 20 20 20 20 20 set for.**
30170 20 20 20 20 20 74 68 65 20 5b 70 72 65 70 61 72 the [prepar
30180 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 ed statement] S
30190 69 6e 74 6f 20 61 20 42 4c 4f 42 20 61 6e 64 20 into a BLOB and
301a0 74 68 65 6e 20 72 65 74 75 72 6e 73 20 61 0a 2a then returns a.*
301b0 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74 * point
301c0 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 er to the conver
301d0 74 65 64 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a ted value..**.**
301e0 20 7b 48 31 33 38 30 36 7d 20 54 68 65 20 5b 73 {H13806} The [s
301f0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
30200 74 65 73 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 tes(S,N)] interf
30210 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a ace returns the.
30220 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 ** numb
30230 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
30240 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e he BLOB or strin
30250 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 g (exclusive of
30260 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
30270 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 zero terminator
30280 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 20 74 on the string) t
30290 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 hat was returned
302a0 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 by the.**
302b0 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 most recent
302c0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
302d0 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e _column_blob(S,N
302e0 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 )] or.**
302f0 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d [sqlite3_colum
30300 6e 5f 74 65 78 74 28 53 2c 4e 29 5d 2e 0a 2a 2a n_text(S,N)]..**
30310 0a 2a 2a 20 7b 48 31 33 38 30 39 7d 20 54 68 65 .** {H13809} The
30320 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
30330 5f 62 79 74 65 73 31 36 28 53 2c 4e 29 5d 20 69 _bytes16(S,N)] i
30340 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
30350 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
30360 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
30370 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20 28 in the string (
30380 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 exclusive of the
30390 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72 .** zer
303a0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f 6e 20 o terminator on
303b0 74 68 65 20 73 74 72 69 6e 67 29 20 74 68 61 74 the string) that
303c0 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 62 79 was returned by
303d0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
303e0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c most recent cal
303f0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f l to [sqlite3_co
30400 6c 75 6d 6e 5f 74 65 78 74 31 36 28 53 2c 4e 29 lumn_text16(S,N)
30410 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 31 32 ]..**.** {H13812
30420 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 } The [sqlite3_c
30430 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 53 2c 4e olumn_double(S,N
30440 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e )] interface con
30450 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 verts the.**
30460 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e Nth column
30470 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 in the current
30480 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c row of the resul
30490 74 20 73 65 74 20 66 6f 72 20 74 68 65 0a 2a 2a t set for the.**
304a0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
304b0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 red statement] S
304c0 20 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 into a floating
304d0 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 61 6e 64 point value and
304e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 .** ret
304f0 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 urns a copy of t
30500 68 61 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a hat value..**.**
30510 20 7b 48 31 33 38 31 35 7d 20 54 68 65 20 5b 73 {H13815} The [s
30520 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e qlite3_column_in
30530 74 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 t(S,N)] interfac
30540 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a e converts the.*
30550 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 * Nth c
30560 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 olumn in the cur
30570 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 rent row of the
30580 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74 result set for t
30590 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
305a0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
305b0 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 36 34 2d nt] S into a 64-
305c0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
305d0 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 er and.**
305e0 20 20 20 72 65 74 75 72 6e 73 20 74 68 65 20 6c returns the l
305f0 6f 77 65 72 20 33 32 20 62 69 74 73 20 6f 66 20 ower 32 bits of
30600 74 68 61 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a that integer..**
30610 0a 2a 2a 20 7b 48 31 33 38 31 38 7d 20 54 68 65 .** {H13818} The
30620 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
30630 5f 69 6e 74 36 34 28 53 2c 4e 29 5d 20 69 6e 74 _int64(S,N)] int
30640 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20 erface converts
30650 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
30660 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 Nth column in th
30670 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 e current row of
30680 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
30690 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 for the.**
306a0 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 [prepared st
306b0 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20 atement] S into
306c0 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 a 64-bit signed
306d0 69 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 integer and.**
306e0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 returns
306f0 61 20 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69 a copy of that i
30700 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 nteger..**.** {H
30710 31 33 38 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 13821} The [sqli
30720 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 te3_column_text(
30730 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 S,N)] interface
30740 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 converts the.**
30750 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f 6c Nth col
30760 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 umn in the curre
30770 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 nt row of the re
30780 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a 20 sult set for.**
30790 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 the [pr
307a0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
307b0 5d 20 53 20 69 6e 74 6f 20 61 20 7a 65 72 6f 2d ] S into a zero-
307c0 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
307d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72 .** str
307e0 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ing and returns
307f0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 a pointer to tha
30800 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 t string..**.**
30810 7b 48 31 33 38 32 34 7d 20 54 68 65 20 5b 73 71 {H13824} The [sq
30820 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
30830 74 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 t16(S,N)] interf
30840 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 ace converts the
30850 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 .** Nth
30860 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 column in the c
30870 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 urrent row of th
30880 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 e result set for
30890 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
308a0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
308b0 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 7a ment] S into a z
308c0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 32 ero-terminated 2
308d0 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 -byte.**
308e0 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 36 aligned UTF-16
308f0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
30900 65 72 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 er string and re
30910 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 turns.**
30920 20 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 a pointer to t
30930 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a hat string..**.*
30940 2a 20 7b 48 31 33 38 32 37 7d 20 54 68 65 20 5b * {H13827} The [
30950 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
30960 79 70 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 ype(S,N)] interf
30970 61 63 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 ace returns.**
30980 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 5b one of [
30990 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 SQLITE_NULL], [S
309a0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 20 QLITE_INTEGER],
309b0 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a [SQLITE_FLOAT],.
309c0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c ** [SQL
309d0 49 54 45 5f 54 45 58 54 5d 2c 20 6f 72 20 5b 53 ITE_TEXT], or [S
309e0 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 61 73 20 61 QLITE_BLOB] as a
309f0 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a ppropriate for.*
30a00 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 4e * the N
30a10 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 th column in the
30a20 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 current row of
30a30 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 the result set f
30a40 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 or.** t
30a50 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
30a60 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a tement] S..**.**
30a70 20 7b 48 31 33 38 33 30 7d 20 54 68 65 20 5b 73 {H13830} The [s
30a80 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 qlite3_column_va
30a90 6c 75 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 lue(S,N)] interf
30aa0 61 63 65 20 72 65 74 75 72 6e 73 20 61 0a 2a 2a ace returns a.**
30ab0 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74 65 pointe
30ac0 72 20 74 6f 20 61 6e 20 5b 75 6e 70 72 6f 74 65 r to an [unprote
30ad0 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
30ae0 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 ue] object for t
30af0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e he.** N
30b00 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 th column in the
30b10 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 current row of
30b20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 the result set f
30b30 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 or.** t
30b40 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
30b50 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 tement] S..*/.SQ
30b60 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
30b70 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
30b80 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 umn_blob(sqlite3
30b90 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
30ba0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
30bb0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
30bc0 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 _bytes(sqlite3_s
30bd0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
30be0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
30bf0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
30c00 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 ytes16(sqlite3_s
30c10 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
30c20 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 .SQLITE_API doub
30c30 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d le sqlite3_colum
30c40 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 n_double(sqlite3
30c50 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
30c60 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
30c70 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
30c80 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d _int(sqlite3_stm
30c90 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
30ca0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
30cb0 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
30cc0 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c column_int64(sql
30cd0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
30ce0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 iCol);.SQLITE_AP
30cf0 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
30d00 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
30d10 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 olumn_text(sqlit
30d20 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 e3_stmt*, int iC
30d30 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ol);.SQLITE_API
30d40 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
30d50 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
30d60 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 6(sqlite3_stmt*,
30d70 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 int iCol);.SQLI
30d80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
30d90 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 e3_column_type(s
30da0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
30db0 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f t iCol);.SQLITE_
30dc0 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 API sqlite3_valu
30dd0 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e *sqlite3_colum
30de0 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f n_value(sqlite3_
30df0 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
30e00 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
30e10 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 F: Destroy A Pre
30e20 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 pared Statement
30e30 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 Object {H13300}
30e40 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e <S70300><S30100>
30e50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
30e60 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 e3_finalize() fu
30e70 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
30e80 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 to delete a [pr
30e90 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
30ea0 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 ]..** If the sta
30eb0 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 tement was execu
30ec0 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ted successfully
30ed0 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 or not executed
30ee0 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a at all, then.**
30ef0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
30f00 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 turned. If execu
30f10 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 tion of the stat
30f20 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 ement failed the
30f30 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 n an.** [error c
30f40 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 ode] or [extende
30f50 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 d error code] is
30f60 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
30f70 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
30f80 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 n be called at a
30f90 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 ny point during
30fa0 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 the execution of
30fb0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
30fc0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 d statement]. I
30fd0 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 f the virtual ma
30fe0 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a chine has not.**
30ff0 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 completed execu
31000 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 tion when this r
31010 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
31020 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a , that is like.*
31030 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 * encountering a
31040 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 n error or an [s
31050 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
31060 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a | interrupt]..*
31070 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 * Incomplete upd
31080 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c ates may be roll
31090 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e ed back and tran
310a0 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 sactions cancele
310b0 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 d,.** depending
310c0 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 on the circumsta
310d0 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a nces, and the.**
310e0 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 [error code] re
310f0 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b turned will be [
31100 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a SQLITE_ABORT]..*
31110 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
31120 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 32 7d 20 .**.** {H11302}
31130 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e The [sqlite3_fin
31140 61 6c 69 7a 65 28 53 29 5d 20 69 6e 74 65 72 66 alize(S)] interf
31150 61 63 65 20 64 65 73 74 72 6f 79 73 20 74 68 65 ace destroys the
31160 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 .** [pr
31170 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
31180 5d 20 53 20 61 6e 64 20 72 65 6c 65 61 73 65 73 ] S and releases
31190 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 all.**
311a0 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c 65 memory and file
311b0 20 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20 resources held
311c0 62 79 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 0a by that object..
311d0 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 34 7d 20 49 **.** {H11304} I
311e0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e f the most recen
311f0 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 t call to [sqlit
31200 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 e3_step(S)] for
31210 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
31220 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
31230 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20 ent] S returned
31240 61 6e 20 65 72 72 6f 72 2c 0a 2a 2a 20 20 20 20 an error,.**
31250 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c 69 then [sqli
31260 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29 5d te3_finalize(S)]
31270 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 73 61 returns that sa
31280 6d 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c me error..*/.SQL
31290 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
312a0 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c te3_finalize(sql
312b0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
312c0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
312d0 45 46 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 EF: Reset A Prep
312e0 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f ared Statement O
312f0 62 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c bject {H13330} <
31300 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S70300>.**.** Th
31310 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 e sqlite3_reset(
31320 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 ) function is ca
31330 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61 20 lled to reset a
31340 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
31350 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 ent].** object b
31360 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 ack to its initi
31370 61 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79 20 al state, ready
31380 74 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74 65 to be re-execute
31390 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 d..** Any SQL st
313a0 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 atement variable
313b0 73 20 74 68 61 74 20 68 61 64 20 76 61 6c 75 65 s that had value
313c0 73 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 s bound to them
313d0 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 using.** the [sq
313e0 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 lite3_bind_blob
313f0 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a | sqlite3_bind_*
31400 28 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74 () API] retain t
31410 68 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 heir values..**
31420 55 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 Use [sqlite3_cle
31430 61 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 ar_bindings()] t
31440 6f 20 72 65 73 65 74 20 74 68 65 20 62 69 6e 64 o reset the bind
31450 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 ings..**.** {H11
31460 33 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 332} The [sqlite
31470 33 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65 3_reset(S)] inte
31480 72 66 61 63 65 20 72 65 73 65 74 73 20 74 68 65 rface resets the
31490 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
314a0 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 ment] S.**
314b0 20 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20 back to the
314c0 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 beginning of its
314d0 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 program..**.**
314e0 7b 48 31 31 33 33 34 7d 20 49 66 20 74 68 65 20 {H11334} If the
314f0 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c most recent call
31500 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 to [sqlite3_ste
31510 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a p(S)] for the.**
31520 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
31530 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 red statement] S
31540 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 returned [SQLIT
31550 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 E_ROW] or [SQLIT
31560 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 E_DONE],.**
31570 20 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 or if [sqli
31580 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 te3_step(S)] has
31590 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 never before be
315a0 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a en called on S,.
315b0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e ** then
315c0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
315d0 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c S)] returns [SQL
315e0 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b ITE_OK]..**.** {
315f0 48 31 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d H11336} If the m
31600 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 ost recent call
31610 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 to [sqlite3_step
31620 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 (S)] for the.**
31630 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 [prepar
31640 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 ed statement] S
31650 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 indicated an err
31660 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 or, then.**
31670 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 [sqlite3_re
31680 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 set(S)] returns
31690 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b an appropriate [
316a0 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a error code]..**.
316b0 2a 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20 ** {H11338} The
316c0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 [sqlite3_reset(S
316d0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 )] interface doe
316e0 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 s not change the
316f0 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 values.**
31700 20 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 of any [sqli
31710 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 te3_bind_blob|bi
31720 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b ndings] on the [
31730 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
31740 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nt] S..*/.SQLITE
31750 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
31760 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 _reset(sqlite3_s
31770 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a tmt *pStmt);../*
31780 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 .** CAPI3REF: Cr
31790 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 eate Or Redefine
317a0 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b SQL Functions {
317b0 48 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e H16100} <S20200>
317c0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 .** KEYWORDS: {f
317d0 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e unction creation
317e0 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 routines}.** KE
317f0 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 YWORDS: {applica
31800 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c tion-defined SQL
31810 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 function}.** KE
31820 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 YWORDS: {applica
31830 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c tion-defined SQL
31840 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a functions}.**.*
31850 2a 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 * These two func
31860 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 tions (collectiv
31870 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 ely known as "fu
31880 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 nction creation
31890 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 routines").** ar
318a0 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 e used to add SQ
318b0 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 L functions or a
318c0 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 ggregates or to
318d0 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 redefine the beh
318e0 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 avior.** of exis
318f0 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f ting SQL functio
31900 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 ns or aggregates
31910 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 . The only diff
31920 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 erence between t
31930 68 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 he.** two is tha
31940 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 t the second par
31950 61 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 ameter, the name
31960 20 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 of the (scalar)
31970 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 function or.**
31980 61 67 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e aggregate, is en
31990 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66 coded in UTF-8 f
319a0 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 or sqlite3_creat
319b0 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 e_function() and
319c0 20 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 UTF-16.** for s
319d0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
319e0 6e 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a nction16()..**.*
319f0 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 * The first para
31a00 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 61 meter is the [da
31a10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
31a20 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 n] to which the
31a30 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 SQL.** function
31a40 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20 is to be added.
31a50 20 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f If a single pro
31a60 67 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 gram uses more t
31a70 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 han one database
31a80 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 .** connection i
31a90 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 nternally, then
31aa0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 SQL functions mu
31ab0 73 74 20 62 65 20 61 64 64 65 64 20 69 6e 64 69 st be added indi
31ac0 76 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 vidually to.** e
31ad0 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e ach database con
31ae0 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 nection..**.** T
31af0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
31b00 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ter is the name
31b10 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 of the SQL funct
31b20 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 ion to be create
31b30 64 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 d or.** redefine
31b40 64 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f d. The length o
31b50 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 f the name is li
31b60 6d 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 mited to 255 byt
31b70 65 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 es, exclusive of
31b80 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 .** the zero-ter
31b90 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 minator. Note t
31ba0 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e hat the name len
31bb0 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 gth limit is in
31bc0 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 bytes, not.** ch
31bd0 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61 aracters. Any a
31be0 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65 ttempt to create
31bf0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 a function with
31c00 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a a longer name.*
31c10 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e * will result in
31c20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
31c30 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a being returned..
31c40 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 **.** The third
31c50 70 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29 parameter (nArg)
31c60 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 .** is the numbe
31c70 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 r of arguments t
31c80 68 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 hat the SQL func
31c90 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 tion or.** aggre
31ca0 67 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 gate takes. If t
31cb0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 his parameter is
31cc0 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 negative, then
31cd0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
31ce0 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 or.** aggregate
31cf0 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75 may take any nu
31d00 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
31d10 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 s..**.** The fou
31d20 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 65 rth parameter, e
31d30 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 69 TextRep, specifi
31d40 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c 49 es what.** [SQLI
31d50 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 65 TE_UTF8 | text e
31d60 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 51 ncoding] this SQ
31d70 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 L function prefe
31d80 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 61 rs for.** its pa
31d90 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 53 rameters. Any S
31da0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c QL function impl
31db0 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c ementation shoul
31dc0 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f 72 d be able to wor
31dd0 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 55 k.** work with U
31de0 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20 TF-8, UTF-16le,
31df0 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75 or UTF-16be. Bu
31e00 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 t some implement
31e10 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a ations may be.**
31e20 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 more efficient
31e30 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e with one encodin
31e40 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20 g than another.
31e50 20 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 It is allowed t
31e60 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 o.** invoke sqli
31e70 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
31e80 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 ion() or sqlite3
31e90 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
31ea0 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 16() multiple.**
31eb0 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 20 times with the
31ec0 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62 75 same function bu
31ed0 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74 t with different
31ee0 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78 74 values of eText
31ef0 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c Rep..** When mul
31f00 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 tiple implementa
31f10 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d tions of the sam
31f20 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 61 e function are a
31f30 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74 65 vailable, SQLite
31f40 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74 68 .** will pick th
31f50 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f 6c e one that invol
31f60 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d ves the least am
31f70 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f 6e ount of data con
31f80 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 version..** If t
31f90 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 here is only a s
31fa0 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 ingle implementa
31fb0 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20 tion which does
31fc0 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74 65 not care what te
31fd0 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 xt.** encoding i
31fe0 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 s used, then the
31ff0 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 fourth argument
32000 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c 49 should be [SQLI
32010 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 TE_ANY]..**.** T
32020 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65 74 he fifth paramet
32030 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 er is an arbitra
32040 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 ry pointer. The
32050 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
32060 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74 69 of the.** functi
32070 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 65 on can gain acce
32080 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 ss to this point
32090 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 er using [sqlite
320a0 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e 0a 3_user_data()]..
320b0 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e 74 **.** The sevent
320c0 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e 69 h, eighth and ni
320d0 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c 20 nth parameters,
320e0 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e 64 xFunc, xStep and
320f0 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a 20 xFinal, are.**
32100 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c 61 pointers to C-la
32110 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e 73 nguage functions
32120 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
32130 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
32140 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 or.** aggregate
32150 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20 66 . A scalar SQL f
32160 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73 unction requires
32170 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 an implementati
32180 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63 0a on of the xFunc.
32190 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c 79 ** callback only
321a0 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 , NULL pointers
321b0 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 should be passed
321c0 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61 6e as the xStep an
321d0 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72 61 d xFinal.** para
321e0 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72 65 meters. An aggre
321f0 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f gate SQL functio
32200 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d n requires an im
32210 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
32220 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46 69 xStep.** and xFi
32230 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68 6f nal and NULL sho
32240 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66 6f uld be passed fo
32250 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c 65 r xFunc. To dele
32260 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a te an existing.*
32270 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f * SQL function o
32280 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 73 r aggregate, pas
32290 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20 74 s NULL for all t
322a0 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 hree function ca
322b0 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 llbacks..**.** I
322c0 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 t is permitted t
322d0 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74 69 o register multi
322e0 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ple implementati
322f0 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 0a ons of the same.
32300 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 ** functions wit
32310 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 h the same name
32320 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72 20 but with either
32330 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65 72 differing number
32340 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 s of.** argument
32350 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20 70 s or differing p
32360 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 6e referred text en
32370 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74 65 codings. SQLite
32380 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68 65 will use.** the
32390 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
323a0 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 most closely mat
323b0 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 ches the way in
323c0 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c which the.** SQL
323d0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
323e0 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 d. A function i
323f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 mplementation wi
32400 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 th a non-negativ
32410 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65 e.** nArg parame
32420 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20 ter is a better
32430 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e match than a fun
32440 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ction implementa
32450 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e tion with.** a n
32460 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41 egative nArg. A
32470 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 function where
32480 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65 the preferred te
32490 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d xt encoding.** m
324a0 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62 atches the datab
324b0 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 ase encoding is
324c0 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63 a better.** matc
324d0 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f h than a functio
324e0 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f n where the enco
324f0 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e ding is differen
32500 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69 t. .** A functi
32510 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 on where the enc
32520 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 oding difference
32530 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31 is between UTF1
32540 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a 6le and UTF16be.
32550 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d ** is a closer m
32560 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 atch than a func
32570 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 tion where the e
32580 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e ncoding differen
32590 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e ce is.** between
325a0 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e UTF8 and UTF16.
325b0 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 .**.** Built-in
325c0 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 functions may be
325d0 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e overloaded by n
325e0 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 ew application-d
325f0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 efined functions
32600 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 ..** The first a
32610 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
32620 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 ed function with
32630 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76 a given name ov
32640 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 errides all.** b
32650 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e uilt-in function
32660 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 s in the same [d
32670 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
32680 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d on] with the sam
32690 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 e name..** Subse
326a0 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f quent applicatio
326b0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
326c0 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ons of the same
326d0 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69 name only overri
326e0 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70 de .** prior app
326f0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
32700 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
32710 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74 are an exact mat
32720 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 ch for the.** nu
32730 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 mber of paramete
32740 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64 rs and preferred
32750 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a encoding..**.**
32760 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d An application-
32770 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
32780 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f is permitted to
32790 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 call other.** S
327a0 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 QLite interfaces
327b0 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68 . However, such
327c0 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a calls must not.
327d0 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 ** close the dat
327e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
327f0 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72 nor finalize or
32800 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61 reset the prepa
32810 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 red.** statement
32820 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75 in which the fu
32830 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
32840 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 g..**.** INVARIA
32850 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 NTS:.**.** {H161
32860 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 03} The [sqlite3
32870 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
32880 31 36 28 44 2c 58 2c 2e 2e 2e 29 5d 20 69 6e 74 16(D,X,...)] int
32890 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 68 erface shall beh
328a0 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ave.**
328b0 61 73 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 as [sqlite3_crea
328c0 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c te_function(D,X,
328d0 2e 2e 2e 29 5d 20 69 6e 20 65 76 65 72 79 20 77 ...)] in every w
328e0 61 79 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ay except that i
328f0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e t.** in
32900 74 65 72 70 72 65 74 73 20 74 68 65 20 58 20 61 terprets the X a
32910 72 67 75 6d 65 6e 74 20 61 73 20 7a 65 72 6f 2d rgument as zero-
32920 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 31 terminated UTF-1
32930 36 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 61 6.** na
32940 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 tive byte order
32950 69 6e 73 74 65 61 64 20 6f 66 20 61 73 20 7a 65 instead of as ze
32960 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ro-terminated UT
32970 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 F-8..**.** {H161
32980 30 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 06} A successful
32990 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 invocation of t
329a0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
329b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
329c0 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c unction(D,X,N,E,
329d0 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 ...)] interface
329e0 73 68 61 6c 6c 20 72 65 67 69 73 74 65 72 0a 2a shall register.*
329f0 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 72 65 * or re
32a00 70 6c 61 63 65 73 20 63 61 6c 6c 62 61 63 6b 20 places callback
32a10 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 functions in the
32a20 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
32a30 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 ction] D.**
32a40 20 20 20 20 20 75 73 65 64 20 74 6f 20 69 6d 70 used to imp
32a50 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 lement the SQL f
32a60 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20 unction named X
32a70 77 69 74 68 20 4e 20 70 61 72 61 6d 65 74 65 72 with N parameter
32a80 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e s.** an
32a90 64 20 68 61 76 69 6e 67 20 61 20 70 72 65 66 65 d having a prefe
32aa0 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 rred text encodi
32ab0 6e 67 20 6f 66 20 45 2e 0a 2a 2a 0a 2a 2a 20 7b ng of E..**.** {
32ac0 48 31 36 31 30 39 7d 20 41 20 73 75 63 63 65 73 H16109} A succes
32ad0 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 sful call to [sq
32ae0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
32af0 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c ction(D,X,N,E,P,
32b00 46 2c 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 F,S,L)].**
32b10 20 20 20 20 73 68 61 6c 6c 20 72 65 70 6c 61 63 shall replac
32b20 65 20 74 68 65 20 50 2c 20 46 2c 20 53 2c 20 61 e the P, F, S, a
32b30 6e 64 20 4c 20 76 61 6c 75 65 73 20 66 72 6f 6d nd L values from
32b40 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 73 any prior calls
32b50 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20 20 20 with.**
32b60 20 20 74 68 65 20 73 61 6d 65 20 44 2c 20 58 2c the same D, X,
32b70 20 4e 2c 20 61 6e 64 20 45 20 76 61 6c 75 65 73 N, and E values
32b80 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 31 32 7d ..**.** {H16112}
32b90 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 The [sqlite3_cr
32ba0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c eate_function(D,
32bb0 58 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 X,...)] interfac
32bc0 65 20 73 68 61 6c 6c 20 66 61 69 6c 0a 2a 2a 20 e shall fail.**
32bd0 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20 if the
32be0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d SQL function nam
32bf0 65 20 58 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 e X is.**
32c00 20 20 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 32 longer than 2
32c10 35 35 20 62 79 74 65 73 20 65 78 63 6c 75 73 69 55 bytes exclusi
32c20 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f 20 74 ve of the zero t
32c30 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a erminator..**.**
32c40 20 7b 48 31 36 31 31 38 7d 20 54 68 65 20 5b 73 {H16118} The [s
32c50 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
32c60 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 nction(D,X,N,E,P
32c70 2c 46 2c 53 2c 4c 29 5d 20 69 6e 74 65 72 66 61 ,F,S,L)] interfa
32c80 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 ce.** s
32c90 68 61 6c 6c 20 66 61 69 6c 20 75 6e 6c 65 73 73 hall fail unless
32ca0 20 65 69 74 68 65 72 20 46 20 69 73 20 4e 55 4c either F is NUL
32cb0 4c 20 61 6e 64 20 53 20 61 6e 64 20 4c 20 61 72 L and S and L ar
32cc0 65 20 6e 6f 6e 2d 4e 55 4c 4c 20 6f 72 0a 2a 2a e non-NULL or.**
32cd0 2a 20 20 20 20 20 20 20 20 20 46 20 69 73 20 6e * F is n
32ce0 6f 6e 2d 4e 55 4c 4c 20 61 6e 64 20 53 20 61 6e on-NULL and S an
32cf0 64 20 4c 20 61 72 65 20 4e 55 4c 4c 2e 0a 2a 2a d L are NULL..**
32d00 0a 2a 2a 20 7b 48 31 36 31 32 31 7d 20 54 68 65 .** {H16121} The
32d10 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
32d20 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 2e 2e 2e 29 _function(D,...)
32d30 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c ] interface shal
32d40 6c 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a l fails with an.
32d50 2a 2a 20 20 20 20 20 20 20 20 20 20 65 72 72 6f ** erro
32d60 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 r code of [SQLIT
32d70 45 5f 42 55 53 59 5d 20 69 66 20 74 68 65 72 65 E_BUSY] if there
32d80 20 65 78 69 73 74 20 5b 70 72 65 70 61 72 65 64 exist [prepared
32d90 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 statements].**
32da0 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 associa
32db0 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 ted with the [da
32dc0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
32dd0 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 n] D..**.** {H16
32de0 31 32 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 124} The [sqlite
32df0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
32e00 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 20 69 6e n(D,X,N,...)] in
32e10 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 66 61 terface shall fa
32e20 69 6c 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20 il with.**
32e30 20 20 20 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 an error cod
32e40 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 45 52 52 e of [SQLITE_ERR
32e50 4f 52 5d 20 69 66 20 70 61 72 61 6d 65 74 65 72 OR] if parameter
32e60 20 4e 20 69 73 20 6c 65 73 73 0a 2a 2a 20 20 20 N is less.**
32e70 20 20 20 20 20 20 20 74 68 61 6e 20 2d 31 20 6f than -1 o
32e80 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 r greater than 1
32e90 32 37 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 32 27..**.** {H1612
32ea0 37 7d 20 57 68 65 6e 20 4e 20 69 73 20 6e 6f 6e 7} When N is non
32eb0 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 5b -negative, the [
32ec0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
32ed0 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e unction(D,X,N,..
32ee0 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .)].**
32ef0 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 interface shall
32f00 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 register callbac
32f10 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 ks to be invoked
32f20 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 for the.**
32f30 20 20 20 20 20 53 51 4c 20 66 75 6e 63 74 69 6f SQL functio
32f40 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 61 n.** na
32f50 6d 65 64 20 58 20 77 68 65 6e 20 74 68 65 20 6e med X when the n
32f60 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
32f70 74 73 20 74 6f 20 74 68 65 20 53 51 4c 20 66 75 ts to the SQL fu
32f80 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 nction is.**
32f90 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 4e 2e exactly N.
32fa0 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 30 7d 20 .**.** {H16130}
32fb0 57 68 65 6e 20 4e 20 69 73 20 2d 31 2c 20 74 68 When N is -1, th
32fc0 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 e [sqlite3_creat
32fd0 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e e_function(D,X,N
32fe0 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,...)].**
32ff0 20 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 interface sha
33000 6c 6c 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c ll register call
33010 62 61 63 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f backs to be invo
33020 6b 65 64 20 66 6f 72 20 74 68 65 20 53 51 4c 0a ked for the SQL.
33030 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 ** func
33040 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20 77 69 74 tion named X wit
33050 68 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 h any number of
33060 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a arguments..**.**
33070 20 7b 48 31 36 31 33 33 7d 20 57 68 65 6e 20 63 {H16133} When c
33080 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
33090 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
330a0 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 (D,X,N,...)].**
330b0 20 20 20 20 20 20 20 20 20 73 70 65 63 69 66 79 specify
330c0 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d multiple implem
330d0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
330e0 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 58 same function X
330f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 .** and
33100 20 77 68 65 6e 20 6f 6e 65 20 69 6d 70 6c 65 6d when one implem
33110 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 4e 3e 3d entation has N>=
33120 30 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 0 and the other
33130 68 61 73 20 4e 3d 28 2d 31 29 0a 2a 2a 20 20 20 has N=(-1).**
33140 20 20 20 20 20 20 20 74 68 65 20 69 6d 70 6c 65 the imple
33150 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 20 61 mentation with a
33160 20 6e 6f 6e 2d 7a 65 72 6f 20 4e 20 73 68 61 6c non-zero N shal
33170 6c 20 62 65 20 70 72 65 66 65 72 72 65 64 2e 0a l be preferred..
33180 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 36 7d 20 57 **.** {H16136} W
33190 68 65 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 hen calls to [sq
331a0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
331b0 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 2e 2e ction(D,X,N,E,..
331c0 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .)].**
331d0 73 70 65 63 69 66 79 20 6d 75 6c 74 69 70 6c 65 specify multiple
331e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
331f0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e of the same fun
33200 63 74 69 6f 6e 20 58 20 77 69 74 68 0a 2a 2a 20 ction X with.**
33210 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61 6d the sam
33220 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 e number of argu
33230 6d 65 6e 74 73 20 4e 20 62 75 74 20 77 69 74 68 ments N but with
33240 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 20 20 different.**
33250 20 20 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 encodings
33260 20 45 2c 20 74 68 65 6e 20 74 68 65 20 69 6d 70 E, then the imp
33270 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 72 lementation wher
33280 65 20 45 20 6d 61 74 63 68 65 73 20 74 68 65 0a e E matches the.
33290 2a 2a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 ** data
332a0 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 73 68 base encoding sh
332b0 61 6c 6c 20 70 72 65 66 65 72 72 65 64 2e 0a 2a all preferred..*
332c0 2a 0a 2a 2a 20 7b 48 31 36 31 33 39 7d 20 46 6f *.** {H16139} Fo
332d0 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 53 r an aggregate S
332e0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 QL function crea
332f0 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 ted using.**
33300 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 [sqlite3_c
33310 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 reate_function(D
33320 2c 58 2c 4e 2c 45 2c 50 2c 30 2c 53 2c 4c 29 5d ,X,N,E,P,0,S,L)]
33330 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 0a 2a the finalizer.*
33340 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 74 * funct
33350 69 6f 6e 20 4c 20 73 68 61 6c 6c 20 61 6c 77 61 ion L shall alwa
33360 79 73 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 78 ys be invoked ex
33370 61 63 74 6c 79 20 6f 6e 63 65 20 69 66 20 74 68 actly once if th
33380 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 e.** st
33390 65 70 20 66 75 6e 63 74 69 6f 6e 20 53 20 69 73 ep function S is
333a0 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d called one or m
333b0 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 0a 2a 2a ore times..**.**
333c0 20 7b 48 31 36 31 34 32 7d 20 57 68 65 6e 20 53 {H16142} When S
333d0 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 65 69 QLite invokes ei
333e0 74 68 65 72 20 74 68 65 20 78 46 75 6e 63 20 6f ther the xFunc o
333f0 72 20 78 53 74 65 70 20 66 75 6e 63 74 69 6f 6e r xStep function
33400 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
33410 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 an application-d
33420 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 efined SQL funct
33430 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65 ion or aggregate
33440 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20 20 created.**
33450 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 65 33 by [sqlite3
33460 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
33470 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
33480 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 create_function1
33490 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 6()],.**
334a0 20 20 74 68 65 6e 20 74 68 65 20 61 72 72 61 79 then the array
334b0 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c of [sqlite3_val
334c0 75 65 5d 20 6f 62 6a 65 63 74 73 20 70 61 73 73 ue] objects pass
334d0 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 ed as the.**
334e0 20 20 20 20 20 20 74 68 69 72 64 20 70 61 72 61 third para
334f0 6d 65 74 65 72 20 73 68 61 6c 6c 20 62 65 20 5b meter shall be [
33500 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
33510 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 3_value] objects
33520 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
33530 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
33540 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 te_function(. s
33550 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f qlite3 *db,. co
33560 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 nst char *zFunct
33570 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e ionName,. int n
33580 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 Arg,. int eText
33590 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 Rep,. void *pAp
335a0 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e p,. void (*xFun
335b0 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 c)(sqlite3_conte
335c0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
335d0 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 value**),. void
335e0 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 (*xStep)(sqlite
335f0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
33600 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c qlite3_value**),
33610 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c . void (*xFinal
33620 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
33630 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 t*).);.SQLITE_AP
33640 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 I int sqlite3_cr
33650 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 eate_function16(
33660 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a . sqlite3 *db,.
33670 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 const void *zF
33680 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 unctionName,. i
33690 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 nt nArg,. int e
336a0 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 TextRep,. void
336b0 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a *pApp,. void (*
336c0 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 xFunc)(sqlite3_c
336d0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
336e0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 te3_value**),.
336f0 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 void (*xStep)(sq
33700 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 lite3_context*,i
33710 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt,sqlite3_value
33720 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 **),. void (*xF
33730 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f inal)(sqlite3_co
33740 6e 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a ntext*).);../*.*
33750 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 74 * CAPI3REF: Text
33760 20 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 32 Encodings {H102
33770 36 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 31 67} <S50200> <H1
33780 36 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 6100>.**.** Thes
33790 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e e constant defin
337a0 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20 e integer codes
337b0 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 74 that represent t
337c0 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65 he various.** te
337d0 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 70 xt encodings sup
337e0 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 ported by SQLite
337f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
33800 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 ITE_UTF8
33810 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
33820 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 ITE_UTF16LE
33830 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 2.#define SQL
33840 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 ITE_UTF16BE
33850 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 3.#define SQL
33860 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 20 ITE_UTF16
33870 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 6e 4 /* Use n
33880 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
33890 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
338a0 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20 TE_ANY
338b0 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 5 /* sqlite
338c0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
338d0 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e n only */.#defin
338e0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 e SQLITE_UTF16_A
338f0 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 LIGNED 8 /*
33900 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
33910 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f ollation only */
33920 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
33930 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75 6e : Deprecated Fun
33940 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43 ctions.** DEPREC
33950 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ATED.**.** These
33960 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b functions are [
33970 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 6e deprecated]. In
33980 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 order to mainta
33990 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 in.** backwards
339a0 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 compatibility wi
339b0 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74 th older code, t
339c0 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 hese functions c
339d0 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 ontinue .** to b
339e0 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 6f e supported. Ho
339f0 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c 69 wever, new appli
33a00 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61 cations should a
33a10 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 20 void.** the use
33a20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f of these functio
33a30 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e 63 ns. To help enc
33a40 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 6f ourage people to
33a50 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20 avoid.** using
33a60 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c these functions,
33a70 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e we are not goin
33a80 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 61 g to tell you wa
33a90 6e 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 nt they do..*/.#
33aa0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
33ab0 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 51 IT_DEPRECATED.SQ
33ac0 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
33ad0 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 DEPRECATED int s
33ae0 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 qlite3_aggregate
33af0 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 63 _count(sqlite3_c
33b00 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 ontext*);.SQLITE
33b10 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 _API SQLITE_DEPR
33b20 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 ECATED int sqlit
33b30 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 e3_expired(sqlit
33b40 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 e3_stmt*);.SQLIT
33b50 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
33b60 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
33b70 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e te3_transfer_bin
33b80 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 dings(sqlite3_st
33b90 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d mt*, sqlite3_stm
33ba0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 t*);.SQLITE_API
33bb0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
33bc0 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c D int sqlite3_gl
33bd0 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 obal_recover(voi
33be0 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 d);.SQLITE_API S
33bf0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 QLITE_DEPRECATED
33c00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68 void sqlite3_th
33c10 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 read_cleanup(voi
33c20 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 d);.SQLITE_API S
33c30 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 QLITE_DEPRECATED
33c40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d int sqlite3_mem
33c50 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a ory_alarm(void(*
33c60 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f )(void*,sqlite3_
33c70 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a int64,int),void*
33c80 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b ,sqlite3_int64);
33c90 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
33ca0 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 69 API3REF: Obtaini
33cb0 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 ng SQL Function
33cc0 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 73 Parameter Values
33cd0 20 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 30 {H15100} <S2020
33ce0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 0>.**.** The C-l
33cf0 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e anguage implemen
33d00 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 tation of SQL fu
33d10 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 nctions and aggr
33d20 65 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 egates uses.** t
33d30 68 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72 his set of inter
33d40 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f face routines to
33d50 20 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61 access the para
33d60 6d 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a meter values on.
33d70 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 ** the function
33d80 6f 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a or aggregate..**
33d90 0a 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66 .** The xFunc (f
33da0 6f 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 or scalar functi
33db0 6f 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66 ons) or xStep (f
33dc0 6f 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70 or aggregates) p
33dd0 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 arameters.** to
33de0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
33df0 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 function()] and
33e00 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
33e10 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a function16()].**
33e20 20 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b define callback
33e30 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
33e40 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
33e50 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 ns and aggregate
33e60 73 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 61 s..** The 4th pa
33e70 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65 rameter to these
33e80 20 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e callbacks is an
33e90 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 array of pointe
33ea0 72 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 rs to.** [protec
33eb0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
33ec0 65 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 e] objects. The
33ed0 72 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 re is one [sqlit
33ee0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
33ef0 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 for.** each par
33f00 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51 ameter to the SQ
33f10 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 L function. The
33f20 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
33f30 75 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 used to.** extra
33f40 63 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 ct values from t
33f50 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 he [sqlite3_valu
33f60 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a e] objects..**.*
33f70 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
33f80 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 work only with
33f90 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
33fa0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
33fb0 73 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 s..** Any attemp
33fc0 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72 t to use these r
33fd0 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 outines on an [u
33fe0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 nprotected sqlit
33ff0 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a e3_value].** obj
34000 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75 ect results in u
34010 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f ndefined behavio
34020 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 r..**.** These r
34030 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73 outines work jus
34040 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65 t like the corre
34050 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e sponding [column
34060 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e access function
34070 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 s].** except tha
34080 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 t these routine
34090 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 s take a single
340a0 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
340b0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
340c0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 .** pointer inst
340d0 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65 ead of a [sqlite
340e0 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 3_stmt*] pointer
340f0 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20 and an integer
34100 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a column number..*
34110 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
34120 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 20 _value_text16()
34130 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 63 interface extrac
34140 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 ts a UTF-16 stri
34150 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74 ng.** in the nat
34160 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 6f ive byte-order o
34170 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 69 f the host machi
34180 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 ne. The.** sqli
34190 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
341a0 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 be() and sqlite3
341b0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 _value_text16le(
341c0 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 ) interfaces.**
341d0 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 73 extract UTF-16 s
341e0 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 6e trings as big-en
341f0 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d dian and little-
34200 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 76 endian respectiv
34210 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ely..**.** The s
34220 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d qlite3_value_num
34230 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 65 eric_type() inte
34240 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 rface attempts t
34250 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 o apply.** numer
34260 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 74 ic affinity to t
34270 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 he value. This
34280 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 74 means that an at
34290 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65 tempt is.** made
342a0 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 to convert the
342b0 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 65 value to an inte
342c0 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 ger or floating
342d0 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75 point. If.** su
342e0 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ch a conversion
342f0 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68 is possible with
34300 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f out loss of info
34310 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65 rmation (in othe
34320 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 74 r.** words, if t
34330 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 he value is a st
34340 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20 ring that looks
34350 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a like a number).*
34360 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 65 * then the conve
34370 72 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d rsion is perform
34380 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6e ed. Otherwise n
34390 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 o conversion occ
343a0 75 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c urs..** The [SQL
343b0 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 ITE_INTEGER | da
343c0 74 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 6f tatype] after co
343d0 6e 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 75 nversion is retu
343e0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 rned..**.** Plea
343f0 73 65 20 70 61 79 20 70 61 72 74 69 63 75 6c 61 se pay particula
34400 72 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74 r attention to t
34410 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 he fact that the
34420 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 pointer returne
34430 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 d.** from [sqlit
34440 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d e3_value_blob()]
34450 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 , [sqlite3_value
34460 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 _text()], or.**
34470 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
34480 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 20 ext16()] can be
34490 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 61 invalidated by a
344a0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
344b0 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
344c0 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 value_bytes()],
344d0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 [sqlite3_value_b
344e0 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69 ytes16()], [sqli
344f0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 te3_value_text()
34500 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 ],.** or [sqlite
34510 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 3_value_text16()
34520 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 ]..**.** These r
34530 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 outines must be
34540 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 called from the
34550 73 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a 2a same thread as.*
34560 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 * the SQL functi
34570 6f 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 64 on that supplied
34580 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 the [sqlite3_va
34590 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 73 lue*] parameters
345a0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
345b0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30 TS:.**.** {H1510
345c0 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 3} The [sqlite3_
345d0 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29 5d 20 69 value_blob(V)] i
345e0 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 nterface convert
345f0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
34600 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c [protected sql
34610 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
34620 63 74 20 56 20 69 6e 74 6f 20 61 20 42 4c 4f 42 ct V into a BLOB
34630 20 61 6e 64 20 74 68 65 6e 0a 2a 2a 20 20 20 20 and then.**
34640 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 20 returns a
34650 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 pointer to the c
34660 6f 6e 76 65 72 74 65 64 20 76 61 6c 75 65 2e 0a onverted value..
34670 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30 36 7d 20 54 **.** {H15106} T
34680 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 he [sqlite3_valu
34690 65 5f 62 79 74 65 73 28 56 29 5d 20 69 6e 74 65 e_bytes(V)] inte
346a0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 rface returns th
346b0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 e.** nu
346c0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
346d0 20 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 the BLOB or str
346e0 69 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f ing (exclusive o
346f0 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 f the.**
34700 20 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f zero terminato
34710 72 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 r on the string)
34720 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e that was return
34730 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 ed by the.**
34740 20 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e most recen
34750 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 t call to [sqlit
34760 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29 e3_value_blob(V)
34770 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ] or.**
34780 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
34790 74 65 78 74 28 56 29 5d 2e 0a 2a 2a 0a 2a 2a 20 text(V)]..**.**
347a0 7b 48 31 35 31 30 39 7d 20 54 68 65 20 5b 73 71 {H15109} The [sq
347b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
347c0 73 31 36 28 56 29 5d 20 69 6e 74 65 72 66 61 63 s16(V)] interfac
347d0 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a e returns the.**
347e0 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 number
347f0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
34800 20 73 74 72 69 6e 67 20 28 65 78 63 6c 75 73 69 string (exclusi
34810 76 65 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 ve of the.**
34820 20 20 20 20 20 20 7a 65 72 6f 20 74 65 72 6d 69 zero termi
34830 6e 61 74 6f 72 20 6f 6e 20 74 68 65 20 73 74 72 nator on the str
34840 69 6e 67 29 20 74 68 61 74 20 77 61 73 20 72 65 ing) that was re
34850 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a turned by the.**
34860 20 20 20 20 20 20 20 20 20 20 6d 6f 73 74 20 72 most r
34870 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 ecent call to [s
34880 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
34890 74 31 36 28 56 29 5d 2c 0a 2a 2a 20 20 20 20 20 t16(V)],.**
348a0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 61 [sqlite3_va
348b0 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56 29 5d lue_text16be(V)]
348c0 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 , or [sqlite3_va
348d0 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 56 29 5d lue_text16le(V)]
348e0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 31 32 7d ..**.** {H15112}
348f0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 The [sqlite3_va
34900 6c 75 65 5f 64 6f 75 62 6c 65 28 56 29 5d 20 69 lue_double(V)] i
34910 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 nterface convert
34920 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
34930 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c [protected sql
34940 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
34950 63 74 20 56 20 69 6e 74 6f 20 61 20 66 6c 6f 61 ct V into a floa
34960 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
34970 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 and.**
34980 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 returns a copy
34990 6f 66 20 74 68 61 74 20 76 61 6c 75 65 2e 0a 2a of that value..*
349a0 2a 0a 2a 2a 20 7b 48 31 35 31 31 35 7d 20 54 68 *.** {H15115} Th
349b0 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
349c0 5f 69 6e 74 28 56 29 5d 20 69 6e 74 65 72 66 61 _int(V)] interfa
349d0 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a ce converts the.
349e0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 6f ** [pro
349f0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
34a00 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 alue] object V i
34a10 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69 67 nto a 64-bit sig
34a20 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 0a ned integer and.
34a30 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ** retu
34a40 72 6e 73 20 74 68 65 20 6c 6f 77 65 72 20 33 32 rns the lower 32
34a50 20 62 69 74 73 20 6f 66 20 74 68 61 74 20 69 6e bits of that in
34a60 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 teger..**.** {H1
34a70 35 31 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74 5118} The [sqlit
34a80 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 56 e3_value_int64(V
34a90 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e )] interface con
34aa0 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 verts the.**
34ab0 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 [protected
34ac0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
34ad0 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 object V into a
34ae0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
34af0 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 teger and.**
34b00 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 20 returns a
34b10 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69 6e 74 copy of that int
34b20 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 eger..**.** {H15
34b30 31 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 121} The [sqlite
34b40 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 56 29 5d 3_value_text(V)]
34b50 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 interface conve
34b60 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 rts the.**
34b70 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 [protected s
34b80 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
34b90 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 7a 65 ject V into a ze
34ba0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ro-terminated UT
34bb0 46 2d 38 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 F-8.**
34bc0 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 string and retur
34bd0 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
34be0 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a that string..**.
34bf0 2a 2a 20 7b 48 31 35 31 32 34 7d 20 54 68 65 20 ** {H15124} The
34c00 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
34c10 65 78 74 31 36 28 56 29 5d 20 69 6e 74 65 72 66 ext16(V)] interf
34c20 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 ace converts the
34c30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 .** [pr
34c40 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
34c50 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 value] object V
34c60 69 6e 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d into a zero-term
34c70 69 6e 61 74 65 64 20 32 2d 62 79 74 65 0a 2a 2a inated 2-byte.**
34c80 20 20 20 20 20 20 20 20 20 20 61 6c 69 67 6e 65 aligne
34c90 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 d UTF-16 native
34ca0 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 byte order.**
34cb0 20 20 20 20 20 20 20 73 74 72 69 6e 67 20 61 6e string an
34cc0 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e d returns a poin
34cd0 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 72 69 ter to that stri
34ce0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 32 ng..**.** {H1512
34cf0 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 7} The [sqlite3_
34d00 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56 value_text16be(V
34d10 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e )] interface con
34d20 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 verts the.**
34d30 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 [protected
34d40 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
34d50 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 object V into a
34d60 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
34d70 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 2-byte.**
34d80 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 aligned UTF-1
34d90 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a 20 6 big-endian.**
34da0 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 20 string
34db0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
34dc0 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 inter to that st
34dd0 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 ring..**.** {H15
34de0 31 33 30 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 130} The [sqlite
34df0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 3_value_text16le
34e00 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 (V)] interface c
34e10 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 onverts the.**
34e20 20 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 [protect
34e30 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
34e40 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 ] object V into
34e50 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 a zero-terminate
34e60 64 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 d 2-byte.**
34e70 20 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 aligned UTF
34e80 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 -16 little-endia
34e90 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 n.** st
34ea0 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 ring and returns
34eb0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
34ec0 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a at string..**.**
34ed0 20 7b 48 31 35 31 33 33 7d 20 54 68 65 20 5b 73 {H15133} The [s
34ee0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
34ef0 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 e(V)] interface
34f00 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 returns.**
34f10 20 20 20 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 one of [SQLI
34f20 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 TE_NULL], [SQLIT
34f30 45 5f 49 4e 54 45 47 45 52 5d 2c 20 5b 53 51 4c E_INTEGER], [SQL
34f40 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a 2a 2a 20 20 ITE_FLOAT],.**
34f50 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f [SQLITE_
34f60 54 45 58 54 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 TEXT], or [SQLIT
34f70 45 5f 42 4c 4f 42 5d 20 61 73 20 61 70 70 72 6f E_BLOB] as appro
34f80 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 20 20 priate for.**
34f90 20 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69 the [sqli
34fa0 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
34fb0 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 t V..**.** {H151
34fc0 33 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 36} The [sqlite3
34fd0 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 _value_numeric_t
34fe0 79 70 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 ype(V)] interfac
34ff0 65 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 20 20 e converts.**
35000 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 6f 74 the [prot
35010 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
35020 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e lue] object V in
35030 74 6f 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 to either an int
35040 65 67 65 72 20 6f 72 0a 2a 2a 20 20 20 20 20 20 eger or.**
35050 20 20 20 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 a floating p
35060 6f 69 6e 74 20 76 61 6c 75 65 20 69 66 20 69 74 oint value if it
35070 20 63 61 6e 20 64 6f 20 73 6f 20 77 69 74 68 6f can do so witho
35080 75 74 20 6c 6f 73 73 20 6f 66 0a 2a 2a 20 20 20 ut loss of.**
35090 20 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 informati
350a0 6f 6e 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 on, and returns
350b0 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4e one of [SQLITE_N
350c0 55 4c 4c 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 ULL],.**
350d0 20 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 [SQLITE_INTEGE
350e0 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 R], [SQLITE_FLOA
350f0 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 T], [SQLITE_TEXT
35100 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 ], or.**
35110 20 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 [SQLITE_BLOB]
35120 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 as appropriate f
35130 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 or the.**
35140 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 [protected sq
35150 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
35160 65 63 74 20 56 20 61 66 74 65 72 20 74 68 65 20 ect V after the
35170 63 6f 6e 76 65 72 73 69 6f 6e 20 61 74 74 65 6d conversion attem
35180 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 pt..*/.SQLITE_AP
35190 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
351a0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
351b0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
351c0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
351d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
351e0 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c ytes(sqlite3_val
351f0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
35200 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
35210 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 ue_bytes16(sqlit
35220 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.SQLI
35230 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 TE_API double sq
35240 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 lite3_value_doub
35250 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
35260 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
35270 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
35280 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c _int(sqlite3_val
35290 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
352a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
352b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
352c0 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 64(sqlite3_value
352d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
352e0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
352f0 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 ar *sqlite3_valu
35300 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 e_text(sqlite3_v
35310 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
35320 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
35330 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
35340 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 t16(sqlite3_valu
35350 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
35360 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
35370 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
35380 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
35390 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
353a0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
353b0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 e3_value_text16b
353c0 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a e(sqlite3_value*
353d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
353e0 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
353f0 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c type(sqlite3_val
35400 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
35410 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
35420 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 ue_numeric_type(
35430 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
35440 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
35450 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67 61 : Obtain Aggrega
35460 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 te Function Cont
35470 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53 32 ext {H16210} <S2
35480 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0200>.**.** The
35490 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
354a0 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 f aggregate SQL
354b0 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68 functions use th
354c0 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61 6c is routine to al
354d0 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72 75 locate.** a stru
354e0 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69 6e cture for storin
354f0 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a 2a g their state..*
35500 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 *.** The first t
35510 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f ime the sqlite3_
35520 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
35530 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63 t() routine is c
35540 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20 70 alled for a.** p
35550 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 67 articular aggreg
35560 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c 6f ate, SQLite allo
35570 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66 20 cates nBytes of
35580 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20 6f memory, zeroes o
35590 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f 72 ut that.** memor
355a0 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 y, and returns a
355b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
355c0 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 On second and su
355d0 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
355e0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67 67 o.** sqlite3_agg
355f0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 regate_context()
35600 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61 67 for the same ag
35610 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
35620 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20 73 index,.** the s
35630 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 65 ame buffer is re
35640 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70 6c turned. The impl
35650 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
35660 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e 20 e aggregate can
35670 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 use.** the retur
35680 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61 63 ned buffer to ac
35690 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a 2a cumulate data..*
356a0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74 6f *.** SQLite auto
356b0 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73 20 matically frees
356c0 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 the allocated bu
356d0 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61 67 ffer when the ag
356e0 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72 79 gregate.** query
356f0 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a concludes..**.*
35700 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 * The first para
35710 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 meter should be
35720 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a a copy of the.**
35730 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 [sqlite3_contex
35740 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e t | SQL function
35750 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 69 context] that i
35760 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61 s the first para
35770 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 meter.** to the
35780 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 callback routine
35790 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
357a0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 66 the aggregate f
357b0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 unction..**.** T
357c0 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 his routine must
357d0 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 be called from
357e0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
357f0 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 in which.** the
35800 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 aggregate SQL fu
35810 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
35820 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 g..**.** INVARIA
35830 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 NTS:.**.** {H162
35840 31 31 7d 20 54 68 65 20 66 69 72 73 74 20 69 6e 11} The first in
35850 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c vocation of [sql
35860 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
35870 6f 6e 74 65 78 74 28 43 2c 4e 29 5d 20 66 6f 72 ontext(C,N)] for
35880 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 70 .** a p
35890 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e articular instan
358a0 63 65 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 ce of an aggrega
358b0 74 65 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 te function (for
358c0 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
358d0 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 contex
358e0 74 20 43 29 20 63 61 75 73 65 73 20 53 51 4c 69 t C) causes SQLi
358f0 74 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 4e te to allocate N
35900 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
35910 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 ,.** ze
35920 72 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2c 20 ro that memory,
35930 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 and return a poi
35940 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f nter to the allo
35950 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2a cated memory..**
35960 0a 2a 2a 20 7b 48 31 36 32 31 33 7d 20 49 66 20 .** {H16213} If
35970 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
35980 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ion error occurs
35990 20 64 75 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 during.**
359a0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 [sqlite3_agg
359b0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 regate_context(C
359c0 2c 4e 29 5d 20 74 68 65 6e 20 74 68 65 20 66 75 ,N)] then the fu
359d0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 30 nction returns 0
359e0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31 35 7d ..**.** {H16215}
359f0 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 Second and subs
35a00 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f equent invocatio
35a10 6e 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 ns of.**
35a20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65 [sqlite3_aggre
35a30 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c 4e gate_context(C,N
35a40 29 5d 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 )] for the same
35a50 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 context pointer
35a60 43 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 67 C.** ig
35a70 6e 6f 72 65 20 74 68 65 20 4e 20 70 61 72 61 6d nore the N param
35a80 65 74 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 eter and return
35a90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
35aa0 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 same.**
35ab0 20 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 block of memor
35ac0 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 y returned by th
35ad0 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69 e first invocati
35ae0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31 on..**.** {H1621
35af0 37 7d 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 7} The memory al
35b00 6c 6f 63 61 74 65 64 20 62 79 20 5b 73 71 6c 69 located by [sqli
35b10 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
35b20 6e 74 65 78 74 28 43 2c 4e 29 5d 20 69 73 0a 2a ntext(C,N)] is.*
35b30 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d * autom
35b40 61 74 69 63 61 6c 6c 79 20 66 72 65 65 64 20 6f atically freed o
35b50 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 n the next call
35b60 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 to [sqlite3_rese
35b70 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 t()].**
35b80 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e or [sqlite3_fin
35b90 61 6c 69 7a 65 28 29 5d 20 66 6f 72 20 74 68 65 alize()] for the
35ba0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
35bb0 6d 65 6e 74 5d 20 63 6f 6e 74 61 69 6e 69 6e 67 ment] containing
35bc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
35bd0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
35be0 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 ion associated w
35bf0 69 74 68 20 63 6f 6e 74 65 78 74 20 43 2e 0a 2a ith context C..*
35c00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
35c10 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 d *sqlite3_aggre
35c20 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c gate_context(sql
35c30 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 ite3_context*, i
35c40 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a nt nBytes);../*.
35c50 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65 ** CAPI3REF: Use
35c60 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74 r Data For Funct
35c70 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c 53 ions {H16240} <S
35c80 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20200>.**.** The
35c90 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 sqlite3_user_da
35ca0 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ta() interface r
35cb0 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 eturns a copy of
35cc0 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 .** the pointer
35cd0 74 68 61 74 20 77 61 73 20 74 68 65 20 70 55 73 that was the pUs
35ce0 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 72 erData parameter
35cf0 20 28 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 (the 5th parame
35d00 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b ter).** of the [
35d10 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
35d20 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e unction()].** an
35d30 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 d [sqlite3_creat
35d40 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 e_function16()]
35d50 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f 72 routines that or
35d60 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 iginally.** regi
35d70 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c 69 stered the appli
35d80 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 cation defined f
35d90 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a unction. {END}.*
35da0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
35db0 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
35dc0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 from the same t
35dd0 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a hread in which.*
35de0 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f * the applicatio
35df0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
35e00 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a on is running..*
35e10 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
35e20 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 34 33 7d 20 .**.** {H16243}
35e30 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 The [sqlite3_use
35e40 72 5f 64 61 74 61 28 43 29 5d 20 69 6e 74 65 72 r_data(C)] inter
35e50 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 face returns a c
35e60 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 opy of the.**
35e70 20 20 20 20 20 20 20 50 20 70 6f 69 6e 74 65 72 P pointer
35e80 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 from the [sqlit
35e90 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
35ea0 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 on(D,X,N,E,P,F,S
35eb0 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ,L)].**
35ec0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 or [sqlite3_cre
35ed0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 ate_function16(D
35ee0 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d ,X,N,E,P,F,S,L)]
35ef0 20 63 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 call that.**
35f00 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 registere
35f10 64 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 d the SQL functi
35f20 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 on associated wi
35f30 74 68 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 th [sqlite3_cont
35f40 65 78 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 ext] C..*/.SQLIT
35f50 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
35f60 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 te3_user_data(sq
35f70 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b lite3_context*);
35f80 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
35f90 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 : Database Conne
35fa0 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 ction For Functi
35fb0 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36 ons {H16250} <S6
35fc0 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a 0600><S20200>.**
35fd0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
35fe0 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
35ff0 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 e() interface re
36000 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a turns a copy of.
36010 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 ** the pointer t
36020 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 o the [database
36030 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65 connection] (the
36040 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a 1st parameter).
36050 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ** of the [sqlit
36060 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
36070 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 on()].** and [sq
36080 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
36090 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 ction16()] routi
360a0 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 nes that origina
360b0 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 lly.** registere
360c0 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f d the applicatio
360d0 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n defined functi
360e0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 on..**.** INVARI
360f0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ANTS:.**.** {H16
36100 32 35 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 253} The [sqlite
36110 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
36120 64 6c 65 28 43 29 5d 20 69 6e 74 65 72 66 61 63 dle(C)] interfac
36130 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 e returns a copy
36140 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 of the.**
36150 20 20 20 20 44 20 70 6f 69 6e 74 65 72 20 66 72 D pointer fr
36160 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f om the [sqlite3_
36170 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
36180 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 D,X,N,E,P,F,S,L)
36190 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 ].** or
361a0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
361b0 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 2c 58 2c _function16(D,X,
361c0 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d 20 63 61 N,E,P,F,S,L)] ca
361d0 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 ll that.**
361e0 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20 74 registered t
361f0 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 he SQL function
36200 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
36210 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 [sqlite3_context
36220 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ] C..*/.SQLITE_A
36230 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 PI sqlite3 *sqli
36240 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
36250 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f andle(sqlite3_co
36260 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 ntext*);../*.**
36270 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 CAPI3REF: Functi
36280 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 on Auxiliary Dat
36290 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 a {H16270} <S202
362a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 00>.**.** The fo
362b0 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 llowing two func
362c0 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 tions may be use
362d0 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 d by scalar SQL
362e0 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 functions to.**
362f0 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 associate metada
36300 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 ta with argument
36310 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 values. If the
36320 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 same value is pa
36330 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 ssed to.** multi
36340 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 ple invocations
36350 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 of the same SQL
36360 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 function during
36370 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c query execution,
36380 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 under.** some c
36390 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 ircumstances the
363a0 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 associated meta
363b0 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 data may be pres
363c0 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a erved. This may.
363d0 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 ** be used, for
363e0 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 example, to add
363f0 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 a regular-expres
36400 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 sion matching sc
36410 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e alar.** function
36420 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 . The compiled v
36430 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 ersion of the re
36440 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e gular expression
36450 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a is stored as.**
36460 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 metadata associ
36470 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51 ated with the SQ
36480 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 L value passed a
36490 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 s the regular ex
364a0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 pression.** patt
364b0 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c ern. The compil
364c0 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 ed regular expre
364d0 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 ssion can be reu
364e0 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a sed on multiple.
364f0 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f ** invocations o
36500 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 f the same funct
36510 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 ion so that the
36520 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e original pattern
36530 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 string.** does
36540 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 not need to be r
36550 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 ecompiled on eac
36560 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a h invocation..**
36570 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
36580 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e get_auxdata() in
36590 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
365a0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
365b0 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 metadata.** ass
365c0 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73 ociated by the s
365d0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
365e0 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 ta() function wi
365f0 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d th the Nth argum
36600 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 ent.** value to
36610 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
36620 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
36630 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 . If no metadata
36640 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a has been ever.*
36650 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 * been set for t
36660 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 he Nth argument
36670 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c of the function,
36680 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 or if the corre
36690 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 sponding.** func
366a0 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 tion parameter h
366b0 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 as changed since
366c0 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 the meta-data w
366d0 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 as set,.** then
366e0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
366f0 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 ata() returns a
36700 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
36710 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
36720 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e set_auxdata() in
36730 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68 terface saves th
36740 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f e metadata.** po
36750 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 inted to by its
36760 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 3rd parameter as
36770 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f the metadata fo
36780 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 r the N-th.** ar
36790 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 gument of the ap
367a0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
367b0 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 d function. Sub
367c0 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 sequent.** calls
367d0 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f to sqlite3_get_
367e0 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 auxdata() might
367f0 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 return this data
36800 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e , if it has.** n
36810 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 ot been destroye
36820 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e d..** If it is n
36830 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 ot NULL, SQLite
36840 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 will invoke the
36850 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 destructor.** fu
36860 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 nction given by
36870 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
36880 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 r to sqlite3_set
36890 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a _auxdata() on.**
368a0 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 the metadata wh
368b0 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e en the correspon
368c0 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 ding function pa
368d0 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a rameter changes.
368e0 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 ** or when the S
368f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d QL statement com
36900 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 pletes, whicheve
36910 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a r comes first..*
36920 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 *.** SQLite is f
36930 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 ree to call the
36940 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 destructor and d
36950 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 rop metadata on
36960 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 any.** parameter
36970 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e of any function
36980 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 at any time. T
36990 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 he only guarante
369a0 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 e is that.** the
369b0 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c destructor will
369c0 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 be called befor
369d0 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 e the metadata i
369e0 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a s dropped..**.**
369f0 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 In practice, me
36a00 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72 tadata is preser
36a10 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 ved between func
36a20 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a tion calls for.*
36a30 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 * expressions th
36a40 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 at are constant
36a50 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e at compile time.
36a60 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c This includes l
36a70 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 iteral.** values
36a80 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c and SQL variabl
36a90 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 es..**.** These
36aa0 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 routines must be
36ab0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
36ac0 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 same thread in
36ad0 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c which.** the SQL
36ae0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e function is run
36af0 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 ning..**.** INVA
36b00 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
36b10 31 36 32 37 32 7d 20 54 68 65 20 5b 73 71 6c 69 16272} The [sqli
36b20 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
36b30 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 C,N)] interface
36b40 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
36b50 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f r.** to
36b60 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 metadata associ
36b70 61 74 65 64 20 77 69 74 68 20 74 68 65 20 4e 74 ated with the Nt
36b80 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 h parameter of t
36b90 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 0a he SQL function.
36ba0 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 6f 73 ** whos
36bb0 65 20 63 6f 6e 74 65 78 74 20 69 73 20 43 2c 20 e context is C,
36bc0 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 or NULL if there
36bd0 20 69 73 20 6e 6f 20 6d 65 74 61 64 61 74 61 20 is no metadata
36be0 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 20 20 associated.**
36bf0 20 20 20 20 20 20 20 77 69 74 68 20 74 68 61 74 with that
36c00 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a parameter..**.*
36c10 2a 20 7b 48 31 36 32 37 34 7d 20 54 68 65 20 5b * {H16274} The [
36c20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 sqlite3_set_auxd
36c30 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 69 6e ata(C,N,P,D)] in
36c40 74 65 72 66 61 63 65 20 61 73 73 69 67 6e 73 20 terface assigns
36c50 61 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 20 20 a metadata.**
36c60 20 20 20 20 20 20 20 70 6f 69 6e 74 65 72 20 50 pointer P
36c70 20 74 6f 20 74 68 65 20 4e 74 68 20 70 61 72 61 to the Nth para
36c80 6d 65 74 65 72 20 6f 66 20 74 68 65 20 53 51 4c meter of the SQL
36c90 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 63 function with c
36ca0 6f 6e 74 65 78 74 20 43 2e 0a 2a 2a 0a 2a 2a 20 ontext C..**.**
36cb0 7b 48 31 36 32 37 36 7d 20 53 51 4c 69 74 65 20 {H16276} SQLite
36cc0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 will invoke the
36cd0 64 65 73 74 72 75 63 74 6f 72 20 44 20 77 69 74 destructor D wit
36ce0 68 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d h a single argum
36cf0 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ent.**
36d00 77 68 69 63 68 20 69 73 20 74 68 65 20 6d 65 74 which is the met
36d10 61 64 61 74 61 20 70 6f 69 6e 74 65 72 20 50 20 adata pointer P
36d20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c following a call
36d30 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 to.**
36d40 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 [sqlite3_set_aux
36d50 64 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 77 data(C,N,P,D)] w
36d60 68 65 6e 20 53 51 4c 69 74 65 20 63 65 61 73 65 hen SQLite cease
36d70 73 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 20 20 20 s to hold.**
36d80 20 20 20 20 20 20 74 68 65 20 6d 65 74 61 64 61 the metada
36d90 74 61 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 37 ta..**.** {H1627
36da0 37 7d 20 53 51 4c 69 74 65 20 63 65 61 73 65 73 7} SQLite ceases
36db0 20 74 6f 20 68 6f 6c 64 20 6d 65 74 61 64 61 74 to hold metadat
36dc0 61 20 66 6f 72 20 61 6e 20 53 51 4c 20 66 75 6e a for an SQL fun
36dd0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 0a ction parameter.
36de0 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 6e ** when
36df0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
36e00 61 74 20 70 61 72 61 6d 65 74 65 72 20 63 68 61 at parameter cha
36e10 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 nges..**.** {H16
36e20 32 37 38 7d 20 57 68 65 6e 20 5b 73 71 6c 69 74 278} When [sqlit
36e30 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 43 e3_set_auxdata(C
36e40 2c 4e 2c 50 2c 44 29 5d 20 69 73 20 69 6e 76 6f ,N,P,D)] is invo
36e50 6b 65 64 2c 20 74 68 65 20 64 65 73 74 72 75 63 ked, the destruc
36e60 74 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 tor.**
36e70 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 6e is called for an
36e80 79 20 70 72 69 6f 72 20 6d 65 74 61 64 61 74 61 y prior metadata
36e90 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
36ea0 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 the same functi
36eb0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 on.** c
36ec0 6f 6e 74 65 78 74 20 43 20 61 6e 64 20 70 61 72 ontext C and par
36ed0 61 6d 65 74 65 72 20 4e 2e 0a 2a 2a 0a 2a 2a 20 ameter N..**.**
36ee0 7b 48 31 36 32 37 39 7d 20 53 51 4c 69 74 65 20 {H16279} SQLite
36ef0 77 69 6c 6c 20 63 61 6c 6c 20 64 65 73 74 72 75 will call destru
36f00 63 74 6f 72 73 20 66 6f 72 20 61 6e 79 20 6d 65 ctors for any me
36f10 74 61 64 61 74 61 20 69 74 20 69 73 20 68 6f 6c tadata it is hol
36f20 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 ding.**
36f30 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 in a particular
36f40 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
36f50 6d 65 6e 74 5d 20 53 20 77 68 65 6e 20 65 69 74 ment] S when eit
36f60 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 her.**
36f70 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 [sqlite3_reset(S
36f80 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 )] or [sqlite3_f
36f90 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 63 inalize(S)] is c
36fa0 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 alled..*/.SQLITE
36fb0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
36fc0 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 73 e3_get_auxdata(s
36fd0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
36fe0 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
36ff0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
37000 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c _set_auxdata(sql
37010 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 ite3_context*, i
37020 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 nt N, void*, voi
37030 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 0a d (*)(void*));..
37040 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
37050 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69 6e Constants Defin
37060 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73 74 ing Special Dest
37070 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72 20 ructor Behavior
37080 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 30 30 {H10280} <S30100
37090 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72 >.**.** These ar
370a0 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 73 e special values
370b0 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 75 63 for the destruc
370c0 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 73 73 tor that is pass
370d0 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a 20 ed in as the.**
370e0 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 74 final argument t
370f0 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 o routines like
37100 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
37110 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 68 65 blob()]. If the
37120 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 61 destructor.** a
37130 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 rgument is SQLIT
37140 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d 65 61 E_STATIC, it mea
37150 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 ns that the cont
37160 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20 63 ent pointer is c
37170 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 onstant.** and w
37180 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e 67 65 ill never change
37190 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 6e . It does not n
371a0 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 72 6f eed to be destro
371b0 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c yed. The.** SQL
371c0 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76 61 ITE_TRANSIENT va
371d0 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 74 lue means that t
371e0 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 he content will
371f0 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69 6e likely change in
37200 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75 74 .** the near fut
37210 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 51 4c ure and that SQL
37220 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 ite should make
37230 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 its own private
37240 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 copy of.** the c
37250 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72 65 ontent before re
37260 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 turning..**.** T
37270 68 65 20 74 79 70 65 64 65 66 20 69 73 20 6e 65 he typedef is ne
37280 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b 20 cessary to work
37290 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73 20 around problems
372a0 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43 2b in certain.** C+
372b0 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53 65 + compilers. Se
372c0 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e 0a e ticket #2191..
372d0 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 20 */.typedef void
372e0 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 (*sqlite3_destru
372f0 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64 2a ctor_type)(void*
37300 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 );.#define SQLIT
37310 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28 28 E_STATIC ((
37320 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 sqlite3_destruct
37330 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66 69 or_type)0).#defi
37340 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 ne SQLITE_TRANSI
37350 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33 5f ENT ((sqlite3_
37360 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 destructor_type)
37370 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 -1)../*.** CAPI3
37380 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68 65 REF: Setting The
37390 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53 51 Result Of An SQ
373a0 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 36 34 L Function {H164
373b0 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 00} <S20200>.**.
373c0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
373d0 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 s are used by th
373e0 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 6e 61 e xFunc or xFina
373f0 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 l callbacks that
37400 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51 .** implement SQ
37410 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 L functions and
37420 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 65 65 aggregates. See
37430 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 .** [sqlite3_cre
37440 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 ate_function()]
37450 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 and [sqlite3_cre
37460 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 ate_function16()
37470 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 6f ].** for additio
37480 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
37490 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e .**.** These fun
374a0 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 72 79 ctions work very
374b0 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20 5b much like the [
374c0 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e parameter bindin
374d0 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 g] family of.**
374e0 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 functions used t
374f0 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 74 6f o bind values to
37500 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 host parameters
37510 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 74 61 in prepared sta
37520 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 66 65 tements..** Refe
37530 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 70 61 r to the [SQL pa
37540 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d 65 6e rameter] documen
37550 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 tation for addit
37560 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
37570 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c n..**.** The sql
37580 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
37590 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 () interface set
375a0 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f s the result fro
375b0 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74 m.** an applicat
375c0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
375d0 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 42 tion to be the B
375e0 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e LOB whose conten
375f0 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 t is pointed.**
37600 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 to by the second
37610 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 77 parameter and w
37620 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73 20 hich is N bytes
37630 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73 20 long where N is
37640 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 61 72 the.** third par
37650 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ameter..**.** Th
37660 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
37670 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 74 65 _zeroblob() inte
37680 72 66 61 63 65 73 20 73 65 74 20 74 68 65 20 72 rfaces set the r
37690 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 esult of.** the
376a0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
376b0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 ned function to
376c0 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61 69 be a BLOB contai
376d0 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a ning all zero.**
376e0 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79 74 bytes and N byt
376f0 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65 72 es in size, wher
37700 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75 65 e N is the value
37710 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72 61 of the 2nd para
37720 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 meter..**.** The
37730 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
37740 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66 61 double() interfa
37750 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75 ce sets the resu
37760 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 lt from.** an ap
37770 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
37780 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
37790 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e a floating poin
377a0 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 t value specifie
377b0 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64 20 d.** by its 2nd
377c0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
377d0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
377e0 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 lt_error() and s
377f0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
37800 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f 6e ror16() function
37810 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20 69 s.** cause the i
37820 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 66 mplemented SQL f
37830 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77 unction to throw
37840 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a an exception..*
37850 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 * SQLite uses th
37860 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 e string pointed
37870 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32 6e to by the.** 2n
37880 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 d parameter of s
37890 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
378a0 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65 33 ror() or sqlite3
378b0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
378c0 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78 74 ).** as the text
378d0 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 of an error mes
378e0 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69 6e sage. SQLite in
378f0 74 65 72 70 72 65 74 73 20 74 68 65 20 65 72 72 terprets the err
37900 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73 74 or.** message st
37910 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 ring from sqlite
37920 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
37930 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c 69 74 as UTF-8. SQLit
37940 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 73 20 e.** interprets
37950 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 the string from
37960 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
37970 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 46 2d rror16() as UTF-
37980 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 16 in native.**
37990 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 66 20 byte order. If
379a0 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 the third parame
379b0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
379c0 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a esult_error().**
379d0 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 or sqlite3_resu
379e0 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73 20 lt_error16() is
379f0 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 51 negative then SQ
37a00 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 74 68 Lite takes as th
37a10 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 e error.** messa
37a20 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 20 74 ge all text up t
37a30 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 74 hrough the first
37a40 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e zero character.
37a50 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 72 64 .** If the third
37a60 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
37a70 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
37a80 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 or() or.** sqlit
37a90 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 e3_result_error1
37aa0 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 6() is non-negat
37ab0 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 ive then SQLite
37ac0 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e 79 0a takes that many.
37ad0 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68 ** bytes (not ch
37ae0 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20 74 aracters) from t
37af0 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 he 2nd parameter
37b00 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 as the error me
37b10 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71 ssage..** The sq
37b20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
37b30 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 or() and sqlite3
37b40 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
37b50 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61 ).** routines ma
37b60 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f 70 ke a private cop
37b70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 6d y of the error m
37b80 65 73 73 61 67 65 20 74 65 78 74 20 62 65 66 6f essage text befo
37b90 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 75 72 re.** they retur
37ba0 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 63 n. Hence, the c
37bb0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
37bc0 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20 6f can deallocate o
37bd0 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 65 20 r.** modify the
37be0 74 65 78 74 20 61 66 74 65 72 20 74 68 65 79 20 text after they
37bf0 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 68 return without h
37c00 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 arm..** The sqli
37c10 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
37c20 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e _code() function
37c30 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 72 72 changes the err
37c40 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75 72 or code.** retur
37c50 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 73 ned by SQLite as
37c60 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 a result of an
37c70 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e 63 74 error in a funct
37c80 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 6c 74 ion. By default
37c90 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 ,.** the error c
37ca0 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45 52 ode is SQLITE_ER
37cb0 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 75 65 ROR. A subseque
37cc0 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 nt call to sqlit
37cd0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
37ce0 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f ).** or sqlite3_
37cf0 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 result_error16()
37d00 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72 6f resets the erro
37d10 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 r code to SQLITE
37d20 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 _ERROR..**.** Th
37d30 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
37d40 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 72 66 _toobig() interf
37d50 61 63 65 20 63 61 75 73 65 73 20 53 51 4c 69 74 ace causes SQLit
37d60 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 e to throw an er
37d70 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e ror.** indicatin
37d80 67 20 74 68 61 74 20 61 20 73 74 72 69 6e 67 20 g that a string
37d90 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 6c 6f or BLOB is to lo
37da0 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74 2e ng to represent.
37db0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
37dc0 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28 e3_result_nomem(
37dd0 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 ) interface caus
37de0 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72 es SQLite to thr
37df0 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 ow an error.** i
37e00 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 ndicating that a
37e10 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
37e20 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a on failed..**.**
37e30 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
37e40 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72 66 ult_int() interf
37e50 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 ace sets the ret
37e60 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 urn value.** of
37e70 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
37e80 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
37e90 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62 69 to be the 32-bi
37ea0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
37eb0 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e 20 .** value given
37ec0 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 6d in the 2nd argum
37ed0 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ent..** The sqli
37ee0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 te3_result_int64
37ef0 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 () interface set
37f00 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c s the return val
37f10 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 ue.** of the app
37f20 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
37f30 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 function to be
37f40 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 the 64-bit signe
37f50 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c d integer.** val
37f60 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 ue given in the
37f70 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 2nd argument..**
37f80 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
37f90 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e result_null() in
37fa0 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 terface sets the
37fb0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
37fc0 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 of the applicat
37fd0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
37fe0 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c 2e tion to be NULL.
37ff0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
38000 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 29 e3_result_text()
38010 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 , sqlite3_result
38020 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 73 71 _text16(),.** sq
38030 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
38040 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 71 6c t16le(), and sql
38050 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
38060 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 63 65 16be() interface
38070 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 65 74 s.** set the ret
38080 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 urn value of the
38090 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
380a0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f ined function to
380b0 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 73 74 be.** a text st
380c0 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 72 65 ring which is re
380d0 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 54 46 presented as UTF
380e0 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 69 76 -8, UTF-16 nativ
380f0 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a 2a 2a e byte order,.**
38100 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 65 UTF-16 little e
38110 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 ndian, or UTF-16
38120 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 65 73 big endian, res
38130 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 pectively..** SQ
38140 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 20 74 Lite takes the t
38150 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 ext result from
38160 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
38170 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e 64 20 from.** the 2nd
38180 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 parameter of the
38190 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
381a0 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 text* interfaces
381b0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 ..** If the 3rd
381c0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
381d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
381e0 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 text* interfaces
381f0 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 65 2c .** is negative,
38200 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b then SQLite tak
38210 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 20 66 es result text f
38220 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 61 rom the 2nd para
38230 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 67 68 meter.** through
38240 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 the first zero
38250 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 character..** If
38260 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 the 3rd paramet
38270 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 er to the sqlite
38280 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 3_result_text* i
38290 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73 20 nterfaces.** is
382a0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 non-negative, th
382b0 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 65 73 en as many bytes
382c0 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73 (not characters
382d0 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a 2a 2a ) of the text.**
382e0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 pointed to by t
382f0 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 he 2nd parameter
38300 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 74 68 are taken as th
38310 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
38320 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f fined.** functio
38330 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 n result..** If
38340 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
38350 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 r to the sqlite3
38360 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e _result_text* in
38370 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 terfaces.** or s
38380 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c qlite3_result_bl
38390 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c ob is a non-NULL
383a0 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 pointer, then S
383b0 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 61 74 QLite calls that
383c0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 .** function as
383d0 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 6f the destructor o
383e0 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c n the text or BL
383f0 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e 20 69 OB result when i
38400 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65 t has.** finishe
38410 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65 73 d using that res
38420 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 ult..** If the 4
38430 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
38440 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 the sqlite3_resu
38450 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 lt_text* interfa
38460 63 65 73 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 ces or.** sqlite
38470 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 3_result_blob is
38480 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e the special con
38490 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 stant SQLITE_STA
384a0 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 TIC, then SQLite
384b0 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 .** assumes that
384c0 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f the text or BLO
384d0 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 B result is in c
384e0 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e onstant space an
384f0 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f d does not.** co
38500 70 79 20 74 68 65 20 69 74 20 6f 72 20 63 61 6c py the it or cal
38510 6c 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 77 l a destructor w
38520 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 hen it has finis
38530 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 hed using that r
38540 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 esult..** If the
38550 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 4th parameter t
38560 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 o the sqlite3_re
38570 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 sult_text* inter
38580 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 faces.** or sqli
38590 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 te3_result_blob
385a0 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 is the special c
385b0 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54 onstant SQLITE_T
385c0 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e RANSIENT.** then
385d0 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 SQLite makes a
385e0 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 copy of the resu
385f0 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 lt into space ob
38600 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66 tained from.** f
38610 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c rom [sqlite3_mal
38620 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74 loc()] before it
38630 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 returns..**.**
38640 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
38650 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 lt_value() inter
38660 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 face sets the re
38670 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 sult of.** the a
38680 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
38690 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ed function to b
386a0 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 e a copy the.**
386b0 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c [unprotected sql
386c0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
386d0 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ct specified by
386e0 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
386f0 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 r. The.** sqlit
38700 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
38710 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 ) interface make
38720 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
38730 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a [sqlite3_value].
38740 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b ** so that the [
38750 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73 sqlite3_value] s
38760 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 pecified in the
38770 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68 parameter may ch
38780 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65 ange or.** be de
38790 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20 allocated after
387a0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
387b0 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 77 alue() returns w
387c0 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 ithout harm..**
387d0 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c A [protected sql
387e0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
387f0 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65 ct may always be
38800 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a used where an.*
38810 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 * [unprotected s
38820 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
38830 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64 ject is required
38840 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b , so either.** k
38850 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f ind of [sqlite3_
38860 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61 value] object ca
38870 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 n be used with t
38880 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a his interface..*
38890 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f *.** If these ro
388a0 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 utines are calle
388b0 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 d from within th
388c0 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 e different thre
388d0 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f ad.** than the o
388e0 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ne containing th
388f0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
38900 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
38910 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20 hat received.**
38920 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e the [sqlite3_con
38930 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 text] pointer, t
38940 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 he results are u
38950 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
38960 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
38970 2a 20 7b 48 31 36 34 30 33 7d 20 54 68 65 20 64 * {H16403} The d
38980 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 76 61 efault return va
38990 6c 75 65 20 66 72 6f 6d 20 61 6e 79 20 53 51 4c lue from any SQL
389a0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c function is NUL
389b0 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 30 36 L..**.** {H16406
389c0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 } The [sqlite3_r
389d0 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e esult_blob(C,V,N
389e0 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 ,D)] interface c
389f0 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 hanges the.**
38a00 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 return va
38a10 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 lue of function
38a20 43 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 74 C to be a BLOB t
38a30 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 0a 2a hat is N bytes.*
38a40 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 6c 65 * in le
38a50 6e 67 74 68 20 61 6e 64 20 77 69 74 68 20 63 6f ngth and with co
38a60 6e 74 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 6f ntent pointed to
38a70 20 62 79 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 by V..**.** {H1
38a80 36 34 30 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 6409} The [sqlit
38a90 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 e3_result_double
38aa0 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 61 63 65 (C,V)] interface
38ab0 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 changes the.**
38ac0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
38ad0 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f value of functio
38ae0 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 66 6c n C to be the fl
38af0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
38b00 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ue V..**.** {H16
38b10 34 31 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 412} The [sqlite
38b20 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 43 3_result_error(C
38b30 2c 56 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 ,V,N)] interface
38b40 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 74 changes the ret
38b50 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 urn.**
38b60 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f value of functio
38b70 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63 n C to be an exc
38b80 65 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f eption with erro
38b90 72 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20 r code.**
38ba0 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 [SQLITE_ERROR
38bb0 5d 20 61 6e 64 20 61 20 55 54 46 2d 38 20 65 72 ] and a UTF-8 er
38bc0 72 6f 72 20 6d 65 73 73 61 67 65 20 63 6f 70 69 ror message copi
38bd0 65 64 20 66 72 6f 6d 20 56 20 75 70 20 74 6f 20 ed from V up to
38be0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
38bf0 66 69 72 73 74 20 7a 65 72 6f 20 62 79 74 65 20 first zero byte
38c00 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65 73 or until N bytes
38c10 20 61 72 65 20 72 65 61 64 20 69 66 20 4e 20 69 are read if N i
38c20 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a s positive..**.*
38c30 2a 20 7b 48 31 36 34 31 35 7d 20 54 68 65 20 5b * {H16415} The [
38c40 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
38c50 72 72 6f 72 31 36 28 43 2c 56 2c 4e 29 5d 20 69 rror16(C,V,N)] i
38c60 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 nterface changes
38c70 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 the return.**
38c80 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 value of
38c90 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 function C to b
38ca0 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 77 e an exception w
38cb0 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a ith error code.*
38cc0 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 * [SQLI
38cd0 54 45 5f 45 52 52 4f 52 5d 20 61 6e 64 20 61 20 TE_ERROR] and a
38ce0 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 UTF-16 native by
38cf0 74 65 20 6f 72 64 65 72 20 65 72 72 6f 72 20 6d te order error m
38d00 65 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20 essage.**
38d10 20 20 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 56 copied from V
38d20 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 up to the first
38d30 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
38d40 20 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65 or until N byte
38d50 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 s.** ar
38d60 65 20 72 65 61 64 20 69 66 20 4e 20 69 73 20 70 e read if N is p
38d70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b ositive..**.** {
38d80 48 31 36 34 31 38 7d 20 54 68 65 20 5b 73 71 6c H16418} The [sql
38d90 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
38da0 72 5f 74 6f 6f 62 69 67 28 43 29 5d 20 69 6e 74 r_toobig(C)] int
38db0 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 erface changes t
38dc0 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 he return.**
38dd0 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 74 value of t
38de0 68 65 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f he function C to
38df0 20 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e be an exception
38e00 20 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65 with error code
38e10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 .** [SQ
38e20 4c 49 54 45 5f 54 4f 4f 42 49 47 5d 20 61 6e 64 LITE_TOOBIG] and
38e30 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
38e40 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a error message..*
38e50 2a 0a 2a 2a 20 7b 48 31 36 34 32 31 7d 20 54 68 *.** {H16421} Th
38e60 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
38e70 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 43 29 t_error_nomem(C)
38e80 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e ] interface chan
38e90 67 65 73 20 74 68 65 20 72 65 74 75 72 6e 0a 2a ges the return.*
38ea0 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 * value
38eb0 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
38ec0 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63 65 C to be an exce
38ed0 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f 72 ption with error
38ee0 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20 20 code.**
38ef0 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d [SQLITE_NOMEM]
38f00 20 61 6e 64 20 61 6e 20 61 70 70 72 6f 70 72 69 and an appropri
38f10 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ate error messag
38f20 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 34 e..**.** {H16424
38f30 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 } The [sqlite3_r
38f40 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 esult_error_code
38f50 28 43 2c 45 29 5d 20 69 6e 74 65 72 66 61 63 65 (C,E)] interface
38f60 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 74 changes the ret
38f70 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 urn.**
38f80 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 75 6e value of the fun
38f90 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e ction C to be an
38fa0 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68 20 exception with
38fb0 65 72 72 6f 72 20 63 6f 64 65 20 45 2e 0a 2a 2a error code E..**
38fc0 20 20 20 20 20 20 20 20 20 20 54 68 65 20 65 72 The er
38fd0 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 ror message text
38fe0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a is unchanged..*
38ff0 2a 0a 2a 2a 20 7b 48 31 36 34 32 37 7d 20 54 68 *.** {H16427} Th
39000 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
39010 74 5f 69 6e 74 28 43 2c 56 29 5d 20 69 6e 74 65 t_int(C,V)] inte
39020 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 rface changes th
39030 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 e.** re
39040 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 turn value of fu
39050 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 nction C to be t
39060 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 he 32-bit intege
39070 72 20 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a r value V..**.**
39080 20 7b 48 31 36 34 33 30 7d 20 54 68 65 20 5b 73 {H16430} The [s
39090 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
390a0 74 36 34 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 t64(C,V)] interf
390b0 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a ace changes the.
390c0 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ** retu
390d0 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 rn value of func
390e0 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 tion C to be the
390f0 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 64-bit integer
39100 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b value V..**.** {
39110 48 31 36 34 33 33 7d 20 54 68 65 20 5b 73 71 6c H16433} The [sql
39120 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c ite3_result_null
39130 28 43 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 (C)] interface c
39140 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 hanges the.**
39150 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 return va
39160 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 lue of function
39170 43 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a C to be NULL..**
39180 0a 2a 2a 20 7b 48 31 36 34 33 36 7d 20 54 68 65 .** {H16436} The
39190 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
391a0 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 20 _text(C,V,N,D)]
391b0 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 interface change
391c0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
391d0 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f return value o
391e0 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 f function C to
391f0 62 65 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 be the UTF-8 str
39200 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
39210 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 V up to the firs
39220 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20 6e t zero if N is n
39230 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20 20 egative.**
39240 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73 74 or the first
39250 20 4e 20 62 79 74 65 73 20 6f 66 20 56 20 69 66 N bytes of V if
39260 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 N is non-negati
39270 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 33 ve..**.** {H1643
39280 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 9} The [sqlite3_
39290 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43 2c result_text16(C,
392a0 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 V,N,D)] interfac
392b0 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a e changes the.**
392c0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
392d0 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 value of functi
392e0 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 55 on C to be the U
392f0 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 TF-16 native byt
39300 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 20 20 e order.**
39310 20 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20 string V up
39320 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 to the first zer
39330 6f 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69 o if N is negati
39340 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f ve.** o
39350 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 r the first N by
39360 74 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73 tes of V if N is
39370 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a non-negative..*
39380 2a 0a 2a 2a 20 7b 48 31 36 34 34 32 7d 20 54 68 *.** {H16442} Th
39390 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
393a0 74 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e t_text16be(C,V,N
393b0 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 ,D)] interface c
393c0 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 hanges the.**
393d0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 return va
393e0 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 lue of function
393f0 43 20 74 6f 20 62 65 20 74 68 65 20 55 54 46 2d C to be the UTF-
39400 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a 16 big-endian.**
39410 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 string
39420 20 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 V up to the fir
39430 73 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20 st zero if N is
39440 6e 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20 negative.**
39450 20 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73 or the firs
39460 74 20 4e 20 62 79 74 65 73 20 6f 72 20 56 20 69 t N bytes or V i
39470 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 f N is non-negat
39480 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 ive..**.** {H164
39490 34 35 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 45} The [sqlite3
394a0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 _result_text16le
394b0 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72 (C,V,N,D)] inter
394c0 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 face changes the
394d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 .** ret
394e0 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e urn value of fun
394f0 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 ction C to be th
39500 65 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d e UTF-16 little-
39510 65 6e 64 69 61 6e 0a 2a 2a 20 20 20 20 20 20 20 endian.**
39520 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20 74 string V up t
39530 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f o the first zero
39540 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69 76 if N is negativ
39550 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 e.** or
39560 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 74 the first N byt
39570 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73 20 es of V if N is
39580 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a non-negative..**
39590 0a 2a 2a 20 7b 48 31 36 34 34 38 7d 20 54 68 65 .** {H16448} The
395a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
395b0 5f 76 61 6c 75 65 28 43 2c 56 29 5d 20 69 6e 74 _value(C,V)] int
395c0 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 erface changes t
395d0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 he.** r
395e0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 eturn value of f
395f0 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 unction C to be
39600 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 the [unprotected
39610 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a sqlite3_value].
39620 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65 ** obje
39630 63 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ct V..**.** {H16
39640 34 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 451} The [sqlite
39650 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 3_result_zeroblo
39660 62 28 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 b(C,N)] interfac
39670 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a e changes the.**
39680 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
39690 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 value of functi
396a0 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 4e 2d on C to be an N-
396b0 62 79 74 65 20 42 4c 4f 42 20 6f 66 20 61 6c 6c byte BLOB of all
396c0 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 zeros..**.** {H
396d0 31 36 34 35 34 7d 20 54 68 65 20 5b 73 71 6c 69 16454} The [sqli
396e0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
396f0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
39700 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
39710 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 )].** i
39720 6e 74 65 72 66 61 63 65 73 20 6d 61 6b 65 20 61 nterfaces make a
39730 20 63 6f 70 79 20 6f 66 20 74 68 65 69 72 20 65 copy of their e
39740 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 rror message str
39750 69 6e 67 73 20 62 65 66 6f 72 65 0a 2a 2a 20 20 ings before.**
39760 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 69 6e returnin
39770 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 35 37 g..**.** {H16457
39780 7d 20 49 66 20 74 68 65 20 44 20 64 65 73 74 72 } If the D destr
39790 75 63 74 6f 72 20 70 61 72 61 6d 65 74 65 72 20 uctor parameter
397a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 to [sqlite3_resu
397b0 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e 2c 44 29 lt_blob(C,V,N,D)
397c0 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b ],.** [
397d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
397e0 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 2c 20 5b ext(C,V,N,D)], [
397f0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
39800 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 29 5d 2c ext16(C,V,N,D)],
39810 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
39820 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
39830 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44 29 5d 2c t16be(C,V,N,D)],
39840 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or.**
39850 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
39860 74 65 78 74 31 36 6c 65 28 43 2c 56 2c 4e 2c 44 text16le(C,V,N,D
39870 29 5d 20 69 73 20 74 68 65 20 63 6f 6e 73 74 61 )] is the consta
39880 6e 74 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 nt [SQLITE_STATI
39890 43 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 C].** t
398a0 68 65 6e 20 6e 6f 20 64 65 73 74 72 75 63 74 6f hen no destructo
398b0 72 20 69 73 20 65 76 65 72 20 63 61 6c 6c 65 64 r is ever called
398c0 20 6f 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 on the pointer
398d0 56 20 61 6e 64 20 53 51 4c 69 74 65 0a 2a 2a 20 V and SQLite.**
398e0 20 20 20 20 20 20 20 20 20 61 73 73 75 6d 65 73 assumes
398f0 20 74 68 61 74 20 56 20 69 73 20 69 6d 6d 75 74 that V is immut
39900 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 able..**.** {H16
39910 34 36 30 7d 20 49 66 20 74 68 65 20 44 20 64 65 460} If the D de
39920 73 74 72 75 63 74 6f 72 20 70 61 72 61 6d 65 74 structor paramet
39930 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 er to [sqlite3_r
39940 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e esult_blob(C,V,N
39950 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 ,D)],.**
39960 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c [sqlite3_resul
39970 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d t_text(C,V,N,D)]
39980 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c , [sqlite3_resul
39990 74 5f 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 t_text16(C,V,N,D
399a0 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 )],.**
399b0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
399c0 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44 text16be(C,V,N,D
399d0 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 )], or.**
399e0 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 [sqlite3_resu
399f0 6c 74 5f 74 65 78 74 31 36 6c 65 28 43 2c 56 2c lt_text16le(C,V,
39a00 4e 2c 44 29 5d 20 69 73 20 74 68 65 20 63 6f 6e N,D)] is the con
39a10 73 74 61 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 stant.**
39a20 20 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 [SQLITE_TRANSI
39a30 45 4e 54 5d 20 74 68 65 6e 20 74 68 65 20 69 6e ENT] then the in
39a40 74 65 72 66 61 63 65 73 20 6d 61 6b 65 73 20 61 terfaces makes a
39a50 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 copy of the.**
39a60 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 content
39a70 20 6f 66 20 56 20 61 6e 64 20 72 65 74 61 69 6e of V and retain
39a80 73 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 0a 2a s the copy..**.*
39a90 2a 20 7b 48 31 36 34 36 33 7d 20 49 66 20 74 68 * {H16463} If th
39aa0 65 20 44 20 64 65 73 74 72 75 63 74 6f 72 20 70 e D destructor p
39ab0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
39ac0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
39ad0 28 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 (C,V,N,D)],.**
39ae0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
39af0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 43 2c 56 _result_text(C,V
39b00 2c 4e 2c 44 29 5d 2c 20 5b 73 71 6c 69 74 65 33 ,N,D)], [sqlite3
39b10 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43 _result_text16(C
39b20 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 ,V,N,D)],.**
39b30 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 [sqlite3_r
39b40 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 43 esult_text16be(C
39b50 2c 56 2c 4e 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20 ,V,N,D)], or.**
39b60 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
39b70 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 3_result_text16l
39b80 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 73 20 73 e(C,V,N,D)] is s
39b90 6f 6d 65 20 76 61 6c 75 65 20 6f 74 68 65 72 20 ome value other
39ba0 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 than.**
39bb0 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b the constants [
39bc0 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 20 61 SQLITE_STATIC] a
39bd0 6e 64 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 nd [SQLITE_TRANS
39be0 49 45 4e 54 5d 20 74 68 65 6e 0a 2a 2a 20 20 20 IENT] then.**
39bf0 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 77 69 SQLite wi
39c00 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65 ll invoke the de
39c10 73 74 72 75 63 74 6f 72 20 44 20 77 69 74 68 20 structor D with
39c20 56 20 61 73 20 69 74 73 20 6f 6e 6c 79 20 61 72 V as its only ar
39c30 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 gument.**
39c40 20 20 20 77 68 65 6e 20 69 74 20 68 61 73 20 66 when it has f
39c50 69 6e 69 73 68 65 64 20 77 69 74 68 20 74 68 65 inished with the
39c60 20 56 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c V value..*/.SQL
39c70 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
39c80 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
39c90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
39ca0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
39cb0 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 int, void(*)(voi
39cc0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
39cd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
39ce0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 sult_double(sqli
39cf0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f te3_context*, do
39d00 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 uble);.SQLITE_AP
39d10 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
39d20 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 esult_error(sqli
39d30 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
39d40 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b nst char*, int);
39d50 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
39d60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
39d70 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f error16(sqlite3_
39d80 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 context*, const
39d90 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c void*, int);.SQL
39da0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
39db0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
39dc0 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 r_toobig(sqlite3
39dd0 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 _context*);.SQLI
39de0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
39df0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
39e00 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 _nomem(sqlite3_c
39e10 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 ontext*);.SQLITE
39e20 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
39e30 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
39e40 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ode(sqlite3_cont
39e50 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ext*, int);.SQLI
39e60 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
39e70 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 te3_result_int(s
39e80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
39e90 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 int);.SQLITE_AP
39ea0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
39eb0 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 esult_int64(sqli
39ec0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 te3_context*, sq
39ed0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 lite3_int64);.SQ
39ee0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
39ef0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
39f00 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 l(sqlite3_contex
39f10 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 t*);.SQLITE_API
39f20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
39f30 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33 ult_text(sqlite3
39f40 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 _context*, const
39f50 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69 char*, int, voi
39f60 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
39f70 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
39f80 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
39f90 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 t16(sqlite3_cont
39fa0 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ext*, const void
39fb0 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 *, int, void(*)(
39fc0 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f void*));.SQLITE_
39fd0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
39fe0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 _result_text16le
39ff0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
3a000 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
3a010 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 int,void(*)(void
3a020 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *));.SQLITE_API
3a030 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
3a040 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c ult_text16be(sql
3a050 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 ite3_context*, c
3a060 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c onst void*, int,
3a070 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
3a080 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
3a090 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
3a0a0 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f value(sqlite3_co
3a0b0 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f ntext*, sqlite3_
3a0c0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
3a0d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
3a0e0 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 _result_zeroblob
3a0f0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
3a100 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a *, int n);../*.*
3a110 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69 * CAPI3REF: Defi
3a120 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67 ne New Collating
3a130 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 36 Sequences {H166
3a140 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 00} <S20300>.**.
3a150 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f ** These functio
3a160 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 ns are used to a
3a170 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e dd new collation
3a180 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68 sequences to th
3a190 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
3a1a0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 onnection] speci
3a1b0 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 73 fied as the firs
3a1c0 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a t argument..**.*
3a1d0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
3a1e0 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 e new collation
3a1f0 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 63 sequence is spec
3a200 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d 38 ified as a UTF-8
3a210 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73 string.** for s
3a220 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
3a230 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71 llation() and sq
3a240 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
3a250 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61 lation_v2().** a
3a260 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 nd a UTF-16 stri
3a270 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 ng for sqlite3_c
3a280 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 reate_collation1
3a290 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 65 6(). In all case
3a2a0 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73 s.** the name is
3a2b0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
3a2c0 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 econd function a
3a2d0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
3a2e0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
3a2f0 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 t may be one of
3a300 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53 the constants [S
3a310 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20 QLITE_UTF8],.**
3a320 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d [SQLITE_UTF16LE]
3a330 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 or [SQLITE_UTF1
3a340 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 67 6BE], indicating
3a350 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d 73 that the user-s
3a360 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74 69 upplied.** routi
3a370 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62 65 ne expects to be
3a380 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72 73 passed pointers
3a390 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63 6f to strings enco
3a3a0 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 2c ded using UTF-8,
3a3b0 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c .** UTF-16 littl
3a3c0 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 e-endian, or UTF
3a3d0 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c 20 -16 big-endian,
3a3e0 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 respectively. Th
3a3f0 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 6d e.** third argum
3a400 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 ent might also b
3a410 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 5f e [SQLITE_UTF16_
3a420 41 4c 49 47 4e 45 44 5d 20 74 6f 20 69 6e 64 69 ALIGNED] to indi
3a430 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 cate that.** the
3a440 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73 routine expects
3a450 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 2d pointers to 16-
3a460 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 64 bit word aligned
3a470 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 55 strings.** of U
3a480 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 TF-16 in the nat
3a490 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 6f ive byte order o
3a4a0 66 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 75 f the host compu
3a4b0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 ter..**.** A poi
3a4c0 6e 74 65 72 20 74 6f 20 74 68 65 20 75 73 65 72 nter to the user
3a4d0 20 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e supplied routin
3a4e0 65 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 e must be passed
3a4f0 20 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a as the fifth.**
3a500 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 argument. If i
3a510 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 t is NULL, this
3a520 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 64 is the same as d
3a530 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c eleting the coll
3a540 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 ation.** sequenc
3a550 65 20 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74 e (so that SQLit
3a560 65 20 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 e cannot call it
3a570 20 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 anymore)..** Ea
3a580 63 68 20 74 69 6d 65 20 74 68 65 20 61 70 70 6c ch time the appl
3a590 69 63 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 ication supplied
3a5a0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 function is inv
3a5b0 6f 6b 65 64 2c 20 69 74 20 69 73 20 70 61 73 73 oked, it is pass
3a5c0 65 64 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 72 ed.** as its fir
3a5d0 73 74 20 70 61 72 61 6d 65 74 65 72 20 61 20 63 st parameter a c
3a5e0 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a opy of the void*
3a5f0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 passed as the f
3a600 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a ourth argument.*
3a610 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 * to sqlite3_cre
3a620 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 ate_collation()
3a630 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 or sqlite3_creat
3a640 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e e_collation16().
3a650 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 .**.** The remai
3a660 6e 69 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74 ning arguments t
3a670 6f 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f o the applicatio
3a680 6e 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 n-supplied routi
3a690 6e 65 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e ne are two strin
3a6a0 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 gs,.** each repr
3a6b0 65 73 65 6e 74 65 64 20 62 79 20 61 20 28 6c 65 esented by a (le
3a6c0 6e 67 74 68 2c 20 64 61 74 61 29 20 70 61 69 72 ngth, data) pair
3a6d0 20 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 and encoded in
3a6e0 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 the encoding.**
3a6f0 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20 that was passed
3a700 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67 as the third arg
3a710 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63 ument when the c
3a720 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
3a730 65 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 e was.** registe
3a740 72 65 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 red. {END} The
3a750 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 application defi
3a760 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f ned collation ro
3a770 75 74 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 utine should.**
3a780 72 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c return negative,
3a790 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 zero or positiv
3a7a0 65 20 69 66 20 74 68 65 20 66 69 72 73 74 20 73 e if the first s
3a7b0 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68 tring is less th
3a7c0 61 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c an,.** equal to,
3a7d0 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e or greater than
3a7e0 20 74 68 65 20 73 65 63 6f 6e 64 20 73 74 72 69 the second stri
3a7f0 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 ng. i.e. (STRING
3a800 31 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 1 - STRING2)..**
3a810 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
3a820 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
3a830 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 _v2() works like
3a840 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
3a850 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 collation().** e
3a860 78 63 65 70 74 20 74 68 61 74 20 69 74 20 74 61 xcept that it ta
3a870 6b 65 73 20 61 6e 20 65 78 74 72 61 20 61 72 67 kes an extra arg
3a880 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 ument which is a
3a890 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a destructor for.
3a8a0 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ** the collation
3a8b0 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f . The destructo
3a8c0 72 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e r is called when
3a8d0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 the collation i
3a8e0 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61 s.** destroyed a
3a8f0 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 20 63 nd is passed a c
3a900 6f 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74 opy of the fourt
3a910 68 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64 h parameter void
3a920 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 * pointer.** of
3a930 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 the sqlite3_crea
3a940 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 te_collation_v2(
3a950 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 )..** Collations
3a960 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 77 are destroyed w
3a970 68 65 6e 20 74 68 65 79 20 61 72 65 20 6f 76 65 hen they are ove
3a980 72 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72 rridden by later
3a990 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a calls to the.**
3a9a0 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 collation creat
3a9b0 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 ion functions or
3a9c0 20 77 68 65 6e 20 74 68 65 20 5b 64 61 74 61 62 when the [datab
3a9d0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
3a9e0 69 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 is closed.** usi
3a9f0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 ng [sqlite3_clos
3aa00 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 e()]..**.** INVA
3aa10 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
3aa20 31 36 36 30 33 7d 20 41 20 73 75 63 63 65 73 73 16603} A success
3aa30 66 75 6c 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a ful call to the.
3aa40 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
3aa50 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
3aa60 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50 ation_v2(B,X,E,P
3aa70 2c 46 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 ,F,D)] interface
3aa80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67 .** reg
3aa90 69 73 74 65 72 73 20 66 75 6e 63 74 69 6f 6e 20 isters function
3aaa0 46 20 61 73 20 74 68 65 20 63 6f 6d 70 61 72 69 F as the compari
3aab0 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 son function use
3aac0 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 d to.**
3aad0 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 6c 6c 61 implement colla
3aae0 74 69 6f 6e 20 58 20 6f 6e 20 74 68 65 20 5b 64 tion X on the [d
3aaf0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3ab00 6f 6e 5d 20 42 20 66 6f 72 0a 2a 2a 20 20 20 20 on] B for.**
3ab10 20 20 20 20 20 20 64 61 74 61 62 61 73 65 73 20 databases
3ab20 68 61 76 69 6e 67 20 65 6e 63 6f 64 69 6e 67 20 having encoding
3ab30 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30 34 E..**.** {H16604
3ab40 7d 20 53 51 4c 69 74 65 20 75 6e 64 65 72 73 74 } SQLite underst
3ab50 61 6e 64 73 20 74 68 65 20 58 20 70 61 72 61 6d ands the X param
3ab60 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 eter to.**
3ab70 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 [sqlite3_cre
3ab80 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 ate_collation_v2
3ab90 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 20 61 (B,X,E,P,F,D)] a
3aba0 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 s a zero-termina
3abb0 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ted.**
3abc0 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 6e 20 UTF-8 string in
3abd0 77 68 69 63 68 20 63 61 73 65 20 69 73 20 69 67 which case is ig
3abe0 6e 6f 72 65 64 20 66 6f 72 20 41 53 43 49 49 20 nored for ASCII
3abf0 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a characters and.*
3ac00 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 73 69 * is si
3ac10 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 20 6e 6f gnificant for no
3ac20 6e 2d 41 53 43 49 49 20 63 68 61 72 61 63 74 65 n-ASCII characte
3ac30 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30 rs..**.** {H1660
3ac40 36 7d 20 53 75 63 63 65 73 73 69 76 65 20 63 61 6} Successive ca
3ac50 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
3ac60 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
3ac70 5f 76 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 _v2(B,X,E,P,F,D)
3ac80 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 ].** wi
3ac90 74 68 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 th the same valu
3aca0 65 73 20 66 6f 72 20 42 2c 20 58 2c 20 61 6e 64 es for B, X, and
3acb0 20 45 2c 20 6f 76 65 72 72 69 64 65 20 70 72 69 E, override pri
3acc0 6f 72 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 or values.**
3acd0 20 20 20 20 20 20 6f 66 20 50 2c 20 46 2c 20 61 of P, F, a
3ace0 6e 64 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 nd D..**.** {H16
3acf0 36 30 39 7d 20 49 66 20 74 68 65 20 64 65 73 74 609} If the dest
3ad00 72 75 63 74 6f 72 20 44 20 69 6e 20 5b 73 71 6c ructor D in [sql
3ad10 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
3ad20 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50 ation_v2(B,X,E,P
3ad30 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,F,D)].**
3ad40 20 20 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 is not NULL t
3ad50 68 65 6e 20 69 74 20 69 73 20 63 61 6c 6c 65 64 hen it is called
3ad60 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 50 with argument P
3ad70 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 when the.**
3ad80 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67 20 collating
3ad90 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f 70 function is drop
3ada0 70 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a ped by SQLite..*
3adb0 2a 0a 2a 2a 20 7b 48 31 36 36 31 32 7d 20 41 20 *.** {H16612} A
3adc0 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 collating functi
3add0 6f 6e 20 69 73 20 64 72 6f 70 70 65 64 20 77 68 on is dropped wh
3ade0 65 6e 20 69 74 20 69 73 20 6f 76 65 72 6c 6f 61 en it is overloa
3adf0 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 ded..**.** {H166
3ae00 31 35 7d 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20 15} A collating
3ae10 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f 70 function is drop
3ae20 70 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 ped when the dat
3ae30 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3ae40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 .** is
3ae50 63 6c 6f 73 65 64 20 75 73 69 6e 67 20 5b 73 71 closed using [sq
3ae60 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a lite3_close()]..
3ae70 2a 2a 0a 2a 2a 20 7b 48 31 36 36 31 38 7d 20 54 **.** {H16618} T
3ae80 68 65 20 70 6f 69 6e 74 65 72 20 50 20 69 6e 20 he pointer P in
3ae90 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
3aea0 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58 collation_v2(B,X
3aeb0 2c 45 2c 50 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20 ,E,P,F,D)].**
3aec0 20 20 20 20 20 20 20 69 73 20 70 61 73 73 65 64 is passed
3aed0 20 74 68 72 6f 75 67 68 20 61 73 20 74 68 65 20 through as the
3aee0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
3aef0 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f to the compariso
3af00 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 n.** fu
3af10 6e 63 74 69 6f 6e 20 46 20 66 6f 72 20 61 6c 6c nction F for all
3af20 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f subsequent invo
3af30 63 61 74 69 6f 6e 73 20 6f 66 20 46 2e 0a 2a 2a cations of F..**
3af40 0a 2a 2a 20 7b 48 31 36 36 32 31 7d 20 41 20 63 .** {H16621} A c
3af50 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
3af60 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
3af70 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 69 73 20 (B,X,E,P,F)] is
3af80 65 78 61 63 74 6c 79 0a 2a 2a 20 20 20 20 20 20 exactly.**
3af90 20 20 20 20 74 68 65 20 73 61 6d 65 20 61 73 20 the same as
3afa0 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 a call to [sqlit
3afb0 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
3afc0 69 6f 6e 5f 76 32 28 29 5d 20 77 69 74 68 0a 2a ion_v2()] with.*
3afd0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 * the s
3afe0 61 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 ame parameters a
3aff0 6e 64 20 61 20 4e 55 4c 4c 20 64 65 73 74 72 75 nd a NULL destru
3b000 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ctor..**.** {H16
3b010 36 32 34 7d 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 624} Following a
3b020 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
3b030 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c _collation_v2(B,
3b040 58 2c 45 2c 50 2c 46 2c 44 29 5d 2c 0a 2a 2a 20 X,E,P,F,D)],.**
3b050 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 SQLite
3b060 75 73 65 73 20 74 68 65 20 63 6f 6d 70 61 72 69 uses the compari
3b070 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 46 20 66 son function F f
3b080 6f 72 20 61 6c 6c 20 74 65 78 74 20 63 6f 6d 70 or all text comp
3b090 61 72 69 73 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 arison.**
3b0a0 20 20 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e operations on
3b0b0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
3b0c0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 42 20 6f 6e 20 onnection] B on
3b0d0 74 65 78 74 20 76 61 6c 75 65 73 20 74 68 61 74 text values that
3b0e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 65 .** use
3b0f0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 the collating s
3b100 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 58 2e equence named X.
3b110 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 32 37 7d 20 .**.** {H16627}
3b120 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 The [sqlite3_cre
3b130 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 ate_collation16(
3b140 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 77 6f 72 6b B,X,E,P,F)] work
3b150 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 s the same.**
3b160 20 20 20 20 20 20 20 61 73 20 5b 73 71 6c 69 74 as [sqlit
3b170 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
3b180 69 6f 6e 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 ion(B,X,E,P,F)]
3b190 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 0a except that the.
3b1a0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6c 6c ** coll
3b1b0 61 74 69 6f 6e 20 6e 61 6d 65 20 58 20 69 73 20 ation name X is
3b1c0 75 6e 64 65 72 73 74 6f 6f 64 20 61 73 20 55 54 understood as UT
3b1d0 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 20 62 F-16 in native b
3b1e0 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 yte order.**
3b1f0 20 20 20 20 20 20 69 6e 73 74 65 61 64 20 6f 66 instead of
3b200 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 UTF-8..**.** {H
3b210 31 36 36 33 30 7d 20 57 68 65 6e 20 6d 75 6c 74 16630} When mult
3b220 69 70 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 iple comparison
3b230 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 61 76 functions are av
3b240 61 69 6c 61 62 6c 65 20 66 6f 72 20 74 68 65 20 ailable for the
3b250 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 same.**
3b260 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
3b270 6e 63 65 2c 20 53 51 4c 69 74 65 20 63 68 6f 6f nce, SQLite choo
3b280 73 65 73 20 74 68 65 20 6f 6e 65 20 77 68 6f 73 ses the one whos
3b290 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a e text encoding.
3b2a0 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 71 75 ** requ
3b2b0 69 72 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 ires the least a
3b2c0 6d 6f 75 6e 74 20 6f 66 20 63 6f 6e 76 65 72 73 mount of convers
3b2d0 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 64 65 66 ion from the def
3b2e0 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 ault.**
3b2f0 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f text encoding o
3b300 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a f the database..
3b310 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
3b320 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
3b330 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 _collation(. sq
3b340 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 lite3*, . const
3b350 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 char *zName, .
3b360 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
3b370 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a void*,. int(*
3b380 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c xCompare)(void*,
3b390 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
3b3a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 int,const void*)
3b3b0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
3b3c0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
3b3d0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a e_collation_v2(.
3b3e0 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 sqlite3*, . c
3b3f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
3b400 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 , . int eTextRe
3b410 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 p, . void*,. i
3b420 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f nt(*xCompare)(vo
3b430 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
3b440 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
3b450 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 id*),. void(*xD
3b460 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29 estroy)(void*).)
3b470 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
3b480 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
3b490 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 collation16(. s
3b4a0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 qlite3*, . cons
3b4b0 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 t void *zName,.
3b4c0 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
3b4d0 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a void*,. int(*
3b4e0 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c xCompare)(void*,
3b4f0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
3b500 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 int,const void*)
3b510 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
3b520 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e REF: Collation N
3b530 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20 eeded Callbacks
3b540 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30 {H16700} <S20300
3b550 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 >.**.** To avoid
3b560 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73 having to regis
3b570 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f ter all collatio
3b580 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f n sequences befo
3b590 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a re a database.**
3b5a0 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20 can be used, a
3b5b0 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20 single callback
3b5c0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 function may be
3b5d0 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 registered with
3b5e0 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 the.** [database
3b5f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 connection] to
3b600 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 be called whenev
3b610 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20 er an undefined
3b620 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 collation.** seq
3b630 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 65 uence is require
3b640 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
3b650 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69 function is regi
3b660 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 stered using the
3b670 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 sqlite3_collati
3b680 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c on_needed() API,
3b690 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70 .** then it is p
3b6a0 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20 assed the names
3b6b0 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c of undefined col
3b6c0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 lation sequences
3b6d0 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65 as strings.** e
3b6e0 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e ncoded in UTF-8.
3b6f0 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c {H16703} If sql
3b700 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e ite3_collation_n
3b710 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65 eeded16() is use
3b720 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 d,.** the names
3b730 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54 are passed as UT
3b740 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 F-16 in machine
3b750 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
3b760 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 r..** A call to
3b770 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 either function
3b780 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69 replaces any exi
3b790 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a sting callback..
3b7a0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 **.** When the c
3b7b0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
3b7c0 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 72 ed, the first ar
3b7d0 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73 gument passed is
3b7e0 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68 a copy.** of th
3b7f0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
3b800 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c t to sqlite3_col
3b810 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 lation_needed()
3b820 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f or.** sqlite3_co
3b830 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 llation_needed16
3b840 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 (). The second
3b850 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
3b860 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
3b870 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69 ection. The thi
3b880 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f rd argument is o
3b890 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54 ne of [SQLITE_UT
3b8a0 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46 F8], [SQLITE_UTF
3b8b0 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 16BE],.** or [SQ
3b8c0 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69 LITE_UTF16LE], i
3b8d0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f ndicating the mo
3b8e0 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72 st desirable for
3b8f0 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 m of the collati
3b900 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66 on.** sequence f
3b910 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64 unction required
3b920 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 . The fourth pa
3b930 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e rameter is the n
3b940 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 ame of the.** re
3b950 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e quired collation
3b960 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a sequence..**.**
3b970 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 The callback fu
3b980 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 nction should re
3b990 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72 gister the desir
3b9a0 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69 ed collation usi
3b9b0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 ng.** [sqlite3_c
3b9c0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 reate_collation(
3b9d0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 )], [sqlite3_cre
3b9e0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 ate_collation16(
3b9f0 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 )], or.** [sqlit
3ba00 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
3ba10 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a ion_v2()]..**.**
3ba20 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
3ba30 2a 2a 20 7b 48 31 36 37 30 32 7d 20 41 20 73 75 ** {H16702} A su
3ba40 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
3ba50 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 [sqlite3_collat
3ba60 69 6f 6e 5f 6e 65 65 64 65 64 28 44 2c 50 2c 46 ion_needed(D,P,F
3ba70 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f )].** o
3ba80 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 r [sqlite3_colla
3ba90 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 44 2c tion_needed16(D,
3baa0 50 2c 46 29 5d 20 63 61 75 73 65 73 0a 2a 2a 20 P,F)] causes.**
3bab0 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 64 61 the [da
3bac0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3bad0 6e 5d 20 44 20 74 6f 20 69 6e 76 6f 6b 65 20 63 n] D to invoke c
3bae0 61 6c 6c 62 61 63 6b 20 46 20 77 69 74 68 20 66 allback F with f
3baf0 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 irst.**
3bb00 20 70 61 72 61 6d 65 74 65 72 20 50 20 77 68 65 parameter P whe
3bb10 6e 65 76 65 72 20 69 74 20 6e 65 65 64 73 20 61 never it needs a
3bb20 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 comparison func
3bb30 74 69 6f 6e 20 66 6f 72 20 61 0a 2a 2a 20 20 20 tion for a.**
3bb40 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67 collating
3bb50 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 69 sequence that i
3bb60 74 20 64 6f 65 73 20 6e 6f 74 20 6b 6e 6f 77 20 t does not know
3bb70 61 62 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 about..**.** {H1
3bb80 36 37 30 34 7d 20 45 61 63 68 20 73 75 63 63 65 6704} Each succe
3bb90 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
3bba0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
3bbb0 5f 6e 65 65 64 65 64 28 29 5d 20 6f 72 0a 2a 2a _needed()] or.**
3bbc0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
3bbd0 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 e3_collation_nee
3bbe0 64 65 64 31 36 28 29 5d 20 6f 76 65 72 72 69 64 ded16()] overrid
3bbf0 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 es the callback
3bc00 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 registered.**
3bc10 20 20 20 20 20 20 20 6f 6e 20 74 68 65 20 73 61 on the sa
3bc20 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e me [database con
3bc30 6e 65 63 74 69 6f 6e 5d 20 62 79 20 70 72 69 6f nection] by prio
3bc40 72 20 63 61 6c 6c 73 20 74 6f 20 65 69 74 68 65 r calls to eithe
3bc50 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e r.** in
3bc60 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 7b terface..**.** {
3bc70 48 31 36 37 30 36 7d 20 54 68 65 20 6e 61 6d 65 H16706} The name
3bc80 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 of the requeste
3bc90 64 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 d collating func
3bca0 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 20 74 tion passed in t
3bcb0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 34 he.** 4
3bcc0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
3bcd0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 the callback is
3bce0 69 6e 20 55 54 46 2d 38 20 69 66 20 74 68 65 20 in UTF-8 if the
3bcf0 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 callback.**
3bd00 20 20 20 20 20 77 61 73 20 72 65 67 69 73 74 65 was registe
3bd10 72 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 red using [sqlit
3bd20 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 e3_collation_nee
3bd30 64 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20 ded()] and.**
3bd40 20 20 20 20 20 20 20 69 73 20 69 6e 20 55 54 46 is in UTF
3bd50 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 -16 native byte
3bd60 6f 72 64 65 72 20 69 66 20 74 68 65 20 63 61 6c order if the cal
3bd70 6c 62 61 63 6b 20 77 61 73 0a 2a 2a 20 20 20 20 lback was.**
3bd80 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64 registered
3bd90 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
3bda0 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
3bdb0 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 16()]..*/.SQLITE
3bdc0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
3bdd0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
3bde0 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a d(. sqlite3*, .
3bdf0 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64 void*, . void
3be00 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 (*)(void*,sqlite
3be10 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 3*,int eTextRep,
3be20 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a const char*).);.
3be30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3be40 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
3be50 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c _needed16(. sql
3be60 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c ite3*, . void*,
3be70 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a . void(*)(void*
3be80 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 ,sqlite3*,int eT
3be90 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 extRep,const voi
3bea0 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70 d*).);../*.** Sp
3beb0 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f ecify the key fo
3bec0 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 r an encrypted d
3bed0 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 atabase. This r
3bee0 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 outine should be
3bef0 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74 .** called right
3bf00 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f after sqlite3_o
3bf10 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 pen()..**.** The
3bf20 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 code to impleme
3bf30 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e nt this API is n
3bf40 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 ot available in
3bf50 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 the public relea
3bf60 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e se.** of SQLite.
3bf70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
3bf80 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a nt sqlite3_key(.
3bf90 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
3bfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3bfb0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 /* Database to
3bfc0 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 be rekeyed */.
3bfd0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
3bfe0 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f , int nKey /
3bff0 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a * The key */.);.
3c000 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
3c010 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e e key on an open
3c020 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74 database. If t
3c030 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 he current datab
3c040 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e ase is not.** en
3c050 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f crypted, this ro
3c060 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79 utine will encry
3c070 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d pt it. If pNew=
3c080 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74 =0 or nNew==0, t
3c090 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 he.** database i
3c0a0 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a s decrypted..**.
3c0b0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 ** The code to i
3c0c0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 mplement this AP
3c0d0 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 I is not availab
3c0e0 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 le in the public
3c0f0 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 release.** of S
3c100 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 QLite..*/.SQLITE
3c110 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
3c120 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 _rekey(. sqlite
3c130 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
3c140 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
3c150 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 base to be rekey
3c160 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f ed */. const vo
3c170 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b id *pKey, int nK
3c180 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 ey /* The ne
3c190 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a w key */.);../*.
3c1a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75 73 ** CAPI3REF: Sus
3c1b0 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46 pend Execution F
3c1c0 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20 or A Short Time
3c1d0 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30 {H10530} <S40410
3c1e0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
3c1f0 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63 te3_sleep() func
3c200 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 tion causes the
3c210 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 74 current thread t
3c220 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 74 o suspend execut
3c230 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65 ion.** for at le
3c240 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 ast a number of
3c250 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65 milliseconds spe
3c260 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70 61 cified in its pa
3c270 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 rameter..**.** I
3c280 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 f the operating
3c290 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20 system does not
3c2a0 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65 support sleep re
3c2b0 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d quests with.** m
3c2c0 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20 illisecond time
3c2d0 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e resolution, then
3c2e0 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62 the time will b
3c2f0 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a e rounded up to.
3c300 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20 73 ** the nearest s
3c310 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65 econd. The numbe
3c320 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 r of millisecond
3c330 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 s of sleep actua
3c340 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 lly.** requested
3c350 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74 from the operat
3c360 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72 65 ing system is re
3c370 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 turned..**.** SQ
3c380 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 Lite implements
3c390 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 62 this interface b
3c3a0 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53 y calling the xS
3c3b0 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 leep().** method
3c3c0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 of the default
3c3d0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
3c3e0 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 ject..**.** INVA
3c3f0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
3c400 31 30 35 33 33 7d 20 54 68 65 20 5b 73 71 6c 69 10533} The [sqli
3c410 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 6e te3_sleep(M)] in
3c420 74 65 72 66 61 63 65 20 69 6e 76 6f 6b 65 73 20 terface invokes
3c430 74 68 65 20 78 53 6c 65 65 70 0a 2a 2a 20 20 20 the xSleep.**
3c440 20 20 20 20 20 20 20 6d 65 74 68 6f 64 20 6f 66 method of
3c450 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 the default [sq
3c460 6c 69 74 65 33 5f 76 66 73 7c 56 46 53 5d 20 69 lite3_vfs|VFS] i
3c470 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 20 20 n order to.**
3c480 20 20 20 20 20 20 20 73 75 73 70 65 6e 64 20 65 suspend e
3c490 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 xecution of the
3c4a0 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 66 current thread f
3c4b0 6f 72 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 20 or at least.**
3c4c0 20 20 20 20 20 20 20 20 4d 20 6d 69 6c 6c 69 73 M millis
3c4d0 65 63 6f 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 econds..**.** {H
3c4e0 31 30 35 33 36 7d 20 54 68 65 20 5b 73 71 6c 69 10536} The [sqli
3c4f0 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 6e te3_sleep(M)] in
3c500 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
3c510 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
3c520 20 20 20 20 20 20 20 20 20 20 6d 69 6c 6c 69 73 millis
3c530 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 econds of sleep
3c540 61 63 74 75 61 6c 6c 79 20 72 65 71 75 65 73 74 actually request
3c550 65 64 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 ed of the operat
3c560 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
3c570 73 79 73 74 65 6d 2c 20 77 68 69 63 68 20 6d 69 system, which mi
3c580 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 ght be larger th
3c590 61 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 an the parameter
3c5a0 20 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 M..*/.SQLITE_AP
3c5b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c I int sqlite3_sl
3c5c0 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a eep(int);../*.**
3c5d0 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 CAPI3REF: Name
3c5e0 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f Of The Folder Ho
3c5f0 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 lding Temporary
3c600 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c Files {H10310} <
3c610 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 S20000>.**.** If
3c620 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 this global var
3c630 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f iable is made to
3c640 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 point to a stri
3c650 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 ng which is.** t
3c660 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c he name of a fol
3c670 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 der (a.k.a. dire
3c680 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c ctory), then all
3c690 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
3c6a0 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 .** created by S
3c6b0 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c QLite will be pl
3c6c0 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 aced in that dir
3c6d0 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 ectory. If this
3c6e0 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 variable.** is
3c6f0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
3c700 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 then SQLite perf
3c710 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f orms a search fo
3c720 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 r an appropriate
3c730 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 .** temporary fi
3c740 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a le directory..**
3c750 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 .** It is not sa
3c760 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 69 fe to modify thi
3c770 73 20 76 61 72 69 61 62 6c 65 20 6f 6e 63 65 20 s variable once
3c780 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a [database conn
3c790 65 63 74 69 6f 6e 5d 0a 2a 2a 20 68 61 73 20 62 ection].** has b
3c7a0 65 65 6e 20 6f 70 65 6e 65 64 2e 20 20 49 74 20 een opened. It
3c7b0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61 74 is intended that
3c7c0 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 62 this variable b
3c7d0 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 73 e set once.** as
3c7e0 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 73 part of process
3c7f0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
3c800 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20 53 and before any S
3c810 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 0a QLite interface.
3c820 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 ** routines have
3c830 20 62 65 65 6e 20 63 61 6c 6c 20 61 6e 64 20 72 been call and r
3c840 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 emain unchanged
3c850 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2f 0a 53 thereafter..*/.S
3c860 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
3c870 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
3c880 65 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ectory;../*.** C
3c890 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 46 6f API3REF: Test Fo
3c8a0 72 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f r Auto-Commit Mo
3c8b0 64 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36 30 de {H12930} <S60
3c8c0 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 200>.** KEYWORDS
3c8d0 3a 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f : {autocommit mo
3c8e0 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 de}.**.** The sq
3c8f0 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f lite3_get_autoco
3c900 6d 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 mmit() interface
3c910 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
3c920 6f 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 o or.** zero if
3c930 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 the given databa
3c940 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
3c950 20 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 or is not in au
3c960 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a tocommit mode,.*
3c970 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 * respectively.
3c980 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 Autocommit mode
3c990 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c is on by defaul
3c9a0 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 t..** Autocommit
3c9b0 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65 mode is disable
3c9c0 64 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 d by a [BEGIN] s
3c9d0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 tatement..** Aut
3c9e0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 ocommit mode is
3c9f0 72 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20 re-enabled by a
3ca00 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c [COMMIT] or [ROL
3ca10 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 LBACK]..**.** If
3ca20 20 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f certain kinds o
3ca30 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f f errors occur o
3ca40 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 n a statement wi
3ca50 74 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 thin a multi-sta
3ca60 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 tement.** transa
3ca70 63 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e ction (errors in
3ca80 63 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f cluding [SQLITE_
3ca90 46 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 FULL], [SQLITE_I
3caa0 4f 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 OERR],.** [SQLIT
3cab0 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 E_NOMEM], [SQLIT
3cac0 45 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 E_BUSY], and [SQ
3cad0 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 LITE_INTERRUPT])
3cae0 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 then the.** tra
3caf0 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 nsaction might b
3cb00 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 e rolled back au
3cb10 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 tomatically. Th
3cb20 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a e only way to.**
3cb30 20 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65 find out whethe
3cb40 72 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 r SQLite automat
3cb50 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 ically rolled ba
3cb60 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ck the transacti
3cb70 6f 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 on after.** an e
3cb80 72 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74 rror is to use t
3cb90 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a his function..**
3cba0 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
3cbb0 2a 2a 0a 2a 2a 20 7b 48 31 32 39 33 31 7d 20 54 **.** {H12931} T
3cbc0 68 65 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f he [sqlite3_get_
3cbd0 61 75 74 6f 63 6f 6d 6d 69 74 28 44 29 5d 20 69 autocommit(D)] i
3cbe0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
3cbf0 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 non-zero or.**
3cc00 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 69 66 zero if
3cc10 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
3cc20 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 69 73 20 onnection] D is
3cc30 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74 or is not in aut
3cc40 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 20 20 20 20 20 ocommit.**
3cc50 20 20 20 20 6d 6f 64 65 2c 20 72 65 73 70 65 63 mode, respec
3cc60 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 tively..**.** {H
3cc70 31 32 39 33 32 7d 20 41 75 74 6f 63 6f 6d 6d 69 12932} Autocommi
3cc80 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20 t mode is on by
3cc90 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b default..**.** {
3cca0 48 31 32 39 33 33 7d 20 41 75 74 6f 63 6f 6d 6d H12933} Autocomm
3ccb0 69 74 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 it mode is disab
3ccc0 6c 65 64 20 62 79 20 61 20 73 75 63 63 65 73 73 led by a success
3ccd0 66 75 6c 20 5b 42 45 47 49 4e 5d 20 73 74 61 74 ful [BEGIN] stat
3cce0 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ement..**.** {H1
3ccf0 32 39 33 34 7d 20 41 75 74 6f 63 6f 6d 6d 69 74 2934} Autocommit
3cd00 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65 64 mode is enabled
3cd10 20 62 79 20 61 20 73 75 63 63 65 73 73 66 75 6c by a successful
3cd20 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f [COMMIT] or [RO
3cd30 4c 4c 42 41 43 4b 5d 0a 2a 2a 20 20 20 20 20 20 LLBACK].**
3cd40 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a statement..*
3cd50 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 *.** ASSUMPTIONS
3cd60 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 39 33 36 7d :.**.** {A12936}
3cd70 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 If another thre
3cd80 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61 ad changes the a
3cd90 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73 utocommit status
3cda0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
3cdb0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e .** con
3cdc0 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 nection while th
3cdd0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 is routine is ru
3cde0 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 nning, then the
3cdf0 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
3ce00 20 20 20 20 20 20 20 20 20 69 73 20 75 6e 64 65 is unde
3ce10 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fined..*/.SQLITE
3ce20 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
3ce30 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 _get_autocommit(
3ce40 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
3ce50 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 * CAPI3REF: Find
3ce60 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61 The Database Ha
3ce70 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 ndle Of A Prepar
3ce80 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 ed Statement {H1
3ce90 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 3120} <S60600>.*
3cea0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
3ceb0 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 _db_handle inter
3cec0 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
3ced0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
3cee0 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a ction] handle.**
3cef0 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 to which a [pre
3cf00 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
3cf10 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 64 belongs. The d
3cf20 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 72 atabase handle r
3cf30 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 eturned by.** sq
3cf40 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 lite3_db_handle
3cf50 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 is the same data
3cf60 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 base handle that
3cf70 20 77 61 73 20 74 68 65 20 66 69 72 73 74 20 61 was the first a
3cf80 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 rgument.** to th
3cf90 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 e [sqlite3_prepa
3cfa0 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f re_v2()] call (o
3cfb0 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 r its variants)
3cfc0 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f that was used to
3cfd0 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20 73 .** create the s
3cfe0 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 tatement in the
3cff0 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a first place..**.
3d000 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
3d010 2a 0a 2a 2a 20 7b 48 31 33 31 32 33 7d 20 54 68 *.** {H13123} Th
3d020 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 e [sqlite3_db_ha
3d030 6e 64 6c 65 28 53 29 5d 20 69 6e 74 65 72 66 61 ndle(S)] interfa
3d040 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
3d050 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 nter.**
3d060 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 to the [databas
3d070 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73 e connection] as
3d080 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
3d090 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 e.** [p
3d0a0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3d0b0 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f t] S..*/.SQLITE_
3d0c0 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c API sqlite3 *sql
3d0d0 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 ite3_db_handle(s
3d0e0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a qlite3_stmt*);..
3d0f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3d100 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 Find the next pr
3d110 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
3d120 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 {H13140} <S6060
3d130 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 0>.**.** This in
3d140 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
3d150 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
3d160 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 next [prepared
3d170 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 statement] after
3d180 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 .** pStmt associ
3d190 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 ated with the [d
3d1a0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3d1b0 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 on] pDb. If pSt
3d1c0 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 mt is NULL.** th
3d1d0 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 en this interfac
3d1e0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
3d1f0 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 ter to the first
3d200 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
3d210 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 ent.** associate
3d220 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 d with the datab
3d230 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ase connection p
3d240 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 Db. If no prepa
3d250 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a red statement.**
3d260 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63 satisfies the c
3d270 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 onditions of thi
3d280 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 s routine, it re
3d290 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a turns NULL..**.*
3d2a0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
3d2b0 0a 2a 2a 20 7b 48 31 33 31 34 33 7d 20 49 66 20 .** {H13143} If
3d2c0 44 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65 D is a [database
3d2d0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 connection] tha
3d2e0 74 20 68 6f 6c 64 73 20 6f 6e 65 20 6f 72 20 6d t holds one or m
3d2f0 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ore.**
3d300 75 6e 66 69 6e 61 6c 69 7a 65 64 20 5b 70 72 65 unfinalized [pre
3d310 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
3d320 5d 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c ] and S is a NUL
3d330 4c 20 70 6f 69 6e 74 65 72 2c 0a 2a 2a 20 20 20 L pointer,.**
3d340 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c then [sql
3d350 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 ite3_next_stmt(D
3d360 2c 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 , S)] routine sh
3d370 61 6c 6c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 all return a poi
3d380 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 nter.**
3d390 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 70 to one of the p
3d3a0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3d3b0 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ts associated wi
3d3c0 74 68 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 th D..**.** {H13
3d3d0 31 34 36 7d 20 49 66 20 44 20 69 73 20 61 20 5b 146} If D is a [
3d3e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3d3f0 69 6f 6e 5d 20 74 68 61 74 20 68 6f 6c 64 73 20 ion] that holds
3d400 6e 6f 20 75 6e 66 69 6e 61 6c 69 7a 65 64 0a 2a no unfinalized.*
3d410 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 * [prep
3d420 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d ared statements]
3d430 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c 4c and S is a NULL
3d440 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 0a 2a pointer, then.*
3d450 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
3d460 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c te3_next_stmt(D,
3d470 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 S)] routine sha
3d480 6c 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c ll return a NULL
3d490 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
3d4a0 7b 48 31 33 31 34 39 7d 20 49 66 20 53 20 69 73 {H13149} If S is
3d4b0 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
3d4c0 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65 20 5b tement] in the [
3d4d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3d4e0 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 20 20 ion] D.**
3d4f0 20 20 20 61 6e 64 20 53 20 69 73 20 6e 6f 74 20 and S is not
3d500 74 68 65 20 6c 61 73 74 20 70 72 65 70 61 72 65 the last prepare
3d510 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 44 d statement in D
3d520 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 , then.**
3d530 20 20 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74 [sqlite3_next
3d540 5f 73 74 6d 74 28 44 2c 20 53 29 5d 20 72 6f 75 _stmt(D, S)] rou
3d550 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 75 72 tine shall retur
3d560 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 n a pointer.**
3d570 20 20 20 20 20 20 20 20 74 6f 20 74 68 65 20 6e to the n
3d580 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61 ext prepared sta
3d590 74 65 6d 65 6e 74 20 69 6e 20 44 20 61 66 74 65 tement in D afte
3d5a0 72 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 r S..**.** {H131
3d5b0 35 32 7d 20 49 66 20 53 20 69 73 20 74 68 65 20 52} If S is the
3d5c0 6c 61 73 74 20 5b 70 72 65 70 61 72 65 64 20 73 last [prepared s
3d5d0 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65 tatement] in the
3d5e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61 .** [da
3d5f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3d600 6e 5d 20 44 20 74 68 65 6e 20 74 68 65 20 5b 73 n] D then the [s
3d610 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 qlite3_next_stmt
3d620 28 44 2c 20 53 29 5d 0a 2a 2a 20 20 20 20 20 20 (D, S)].**
3d630 20 20 20 20 72 6f 75 74 69 6e 65 20 73 68 61 6c routine shal
3d640 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20 l return a NULL
3d650 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 pointer..**.** A
3d660 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a SSUMPTIONS:.**.*
3d670 2a 20 7b 41 31 33 31 35 34 7d 20 54 68 65 20 5b * {A13154} The [
3d680 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3d690 69 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 ion] pointer D i
3d6a0 6e 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 n a call to.**
3d6b0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
3d6c0 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d _next_stmt(D,S)]
3d6d0 20 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 must refer to a
3d6e0 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a n open database.
3d6f0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 6e ** conn
3d700 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61 ection and in pa
3d710 72 74 69 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f rticular must no
3d720 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e t be a NULL poin
3d730 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ter..*/.SQLITE_A
3d740 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 PI sqlite3_stmt
3d750 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 *sqlite3_next_st
3d760 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c mt(sqlite3 *pDb,
3d770 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
3d780 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 Stmt);../*.** CA
3d790 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 PI3REF: Commit A
3d7a0 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 nd Rollback Noti
3d7b0 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 fication Callbac
3d7c0 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 ks {H12950} <S60
3d7d0 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 400>.**.** The s
3d7e0 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f qlite3_commit_ho
3d7f0 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ok() interface r
3d800 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 egisters a callb
3d810 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ack.** function
3d820 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 to be invoked wh
3d830 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 enever a transac
3d840 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 tion is committe
3d850 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 d..** Any callba
3d860 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 ck set by a prev
3d870 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c ious call to sql
3d880 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b ite3_commit_hook
3d890 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 ().** for the sa
3d8a0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
3d8b0 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 ection is overri
3d8c0 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c dden..** The sql
3d8d0 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f ite3_rollback_ho
3d8e0 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ok() interface r
3d8f0 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 egisters a callb
3d900 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ack.** function
3d910 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 to be invoked wh
3d920 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 enever a transac
3d930 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 tion is committe
3d940 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 d..** Any callba
3d950 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 ck set by a prev
3d960 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c ious call to sql
3d970 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b ite3_commit_hook
3d980 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 ().** for the sa
3d990 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
3d9a0 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 ection is overri
3d9b0 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 dden..** The pAr
3d9c0 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61 g argument is pa
3d9d0 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 ssed through to
3d9e0 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a the callback..**
3d9f0 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b If the callback
3da00 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f on a commit hoo
3da10 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 k function retur
3da20 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 ns non-zero,.**
3da30 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 then the commit
3da40 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 is converted int
3da50 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a o a rollback..**
3da60 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 .** If another f
3da70 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 unction was prev
3da80 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 iously registere
3da90 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 d, its.** pArg v
3daa0 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 alue is returned
3dab0 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c . Otherwise NUL
3dac0 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a L is returned..*
3dad0 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 *.** The callbac
3dae0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
3daf0 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 must not do any
3db00 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 thing that will
3db10 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 modify.** the da
3db20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3db30 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 n that invoked t
3db40 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e he callback. An
3db50 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 y actions.** to
3db60 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 modify the datab
3db70 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d ase connection m
3db80 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 ust be deferred
3db90 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a until after the.
3dba0 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 ** completion of
3dbb0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 the [sqlite3_st
3dbc0 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 ep()] call that
3dbd0 74 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f triggered the co
3dbe0 6d 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 mmit.** or rollb
3dbf0 61 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 ack hook in the
3dc00 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 first place..**
3dc10 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 Note that [sqlit
3dc20 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
3dc30 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 and [sqlite3_st
3dc40 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 ep()] both modif
3dc50 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 y their.** datab
3dc60 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ase connections
3dc70 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 for the meaning
3dc80 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 of "modify" in t
3dc90 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a his paragraph..*
3dca0 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67 *.** Registering
3dcb0 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e a NULL function
3dcc0 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61 disables the ca
3dcd0 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f llback..**.** Fo
3dce0 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f r the purposes o
3dcf0 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 72 f this API, a tr
3dd00 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 69 ansaction is sai
3dd10 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a 2a d to have been.*
3dd20 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 * rolled back if
3dd30 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 4f an explicit "RO
3dd40 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 6e LLBACK" statemen
3dd50 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 6f t is executed, o
3dd60 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 72 r.** an error or
3dd70 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 73 constraint caus
3dd80 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 72 es an implicit r
3dd90 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 72 ollback to occur
3dda0 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 ..** The rollbac
3ddb0 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f k callback is no
3ddc0 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 t invoked if a t
3ddd0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a ransaction is.**
3dde0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 automatically r
3ddf0 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 olled back becau
3de00 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 se the database
3de10 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c connection is cl
3de20 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c osed..** The rol
3de30 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 lback callback i
3de40 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 s not invoked if
3de50 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
3de60 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b s.** rolled back
3de70 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d 69 because a commi
3de80 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 t callback retur
3de90 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a ned non-zero..**
3dea0 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f 6e <todo> Check on
3deb0 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a this </todo>.**
3dec0 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
3ded0 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 31 7d 20 54 **.** {H12951} T
3dee0 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d he [sqlite3_comm
3def0 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 it_hook(D,F,P)]
3df00 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
3df10 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 ers the.**
3df20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e callback fun
3df30 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 6e ction F to be in
3df40 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67 75 6d voked with argum
3df50 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72 0a 2a ent P whenever.*
3df60 2a 20 20 20 20 20 20 20 20 20 20 61 20 74 72 61 * a tra
3df70 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 nsaction commits
3df80 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 on the [databas
3df90 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e e connection] D.
3dfa0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 32 7d 20 .**.** {H12952}
3dfb0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d The [sqlite3_com
3dfc0 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d mit_hook(D,F,P)]
3dfd0 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
3dfe0 6e 73 20 74 68 65 20 50 20 61 72 67 75 6d 65 6e ns the P argumen
3dff0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72 t.** fr
3e000 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 om the previous
3e010 63 61 6c 6c 20 77 69 74 68 20 74 68 65 20 73 61 call with the sa
3e020 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e me [database con
3e030 6e 65 63 74 69 6f 6e 5d 20 44 2c 0a 2a 2a 20 20 nection] D,.**
3e040 20 20 20 20 20 20 20 20 6f 72 20 4e 55 4c 4c 20 or NULL
3e050 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c on the first cal
3e060 6c 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c l for a particul
3e070 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ar database conn
3e080 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a 2a 2a 20 ection D..**.**
3e090 7b 48 31 32 39 35 33 7d 20 45 61 63 68 20 63 61 {H12953} Each ca
3e0a0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
3e0b0 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 6f 76 ommit_hook()] ov
3e0c0 65 72 77 72 69 74 65 73 20 74 68 65 20 63 61 6c erwrites the cal
3e0d0 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 lback.**
3e0e0 20 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 registered by
3e0f0 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2a 0a prior calls..**.
3e100 2a 2a 20 7b 48 31 32 39 35 34 7d 20 49 66 20 74 ** {H12954} If t
3e110 68 65 20 46 20 61 72 67 75 6d 65 6e 74 20 74 6f he F argument to
3e120 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 [sqlite3_commit
3e130 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 73 _hook(D,F,P)] is
3e140 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 NULL.**
3e150 20 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 then the commi
3e160 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 t hook callback
3e170 69 73 20 63 61 6e 63 65 6c 65 64 20 61 6e 64 20 is canceled and
3e180 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 no callback.**
3e190 20 20 20 20 20 20 20 20 69 73 20 69 6e 76 6f 6b is invok
3e1a0 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 ed when a transa
3e1b0 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 2e 0a 2a ction commits..*
3e1c0 2a 0a 2a 2a 20 7b 48 31 32 39 35 35 7d 20 49 66 *.** {H12955} If
3e1d0 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 61 6c 6c the commit call
3e1e0 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e back returns non
3e1f0 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 63 -zero then the c
3e200 6f 6d 6d 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 ommit is.**
3e210 20 20 20 20 20 63 6f 6e 76 65 72 74 65 64 20 69 converted i
3e220 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a nto a rollback..
3e230 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 31 7d 20 54 **.** {H12961} T
3e240 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c 6c he [sqlite3_roll
3e250 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 back_hook(D,F,P)
3e260 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 ] interface regi
3e270 73 74 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 sters the.**
3e280 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66 callback f
3e290 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 unction F to be
3e2a0 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67 invoked with arg
3e2b0 75 6d 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72 ument P whenever
3e2c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 74 .** a t
3e2d0 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 73 ransaction rolls
3e2e0 20 62 61 63 6b 20 6f 6e 20 74 68 65 20 5b 64 61 back on the [da
3e2f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3e300 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 n] D..**.** {H12
3e310 39 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 962} The [sqlite
3e320 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 3_rollback_hook(
3e330 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 D,F,P)] interfac
3e340 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 50 0a e returns the P.
3e350 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 67 75 ** argu
3e360 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 72 ment from the pr
3e370 65 76 69 6f 75 73 20 63 61 6c 6c 20 77 69 74 68 evious call with
3e380 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 the same.**
3e390 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 [database
3e3a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2c 20 6f connection] D, o
3e3b0 72 20 4e 55 4c 4c 20 6f 6e 20 74 68 65 20 66 69 r NULL on the fi
3e3c0 72 73 74 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 rst call.**
3e3d0 20 20 20 20 20 66 6f 72 20 61 20 70 61 72 74 69 for a parti
3e3e0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 cular database c
3e3f0 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a onnection D..**.
3e400 2a 2a 20 7b 48 31 32 39 36 33 7d 20 45 61 63 68 ** {H12963} Each
3e410 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
3e420 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 3_rollback_hook(
3e430 29 5d 20 6f 76 65 72 77 72 69 74 65 73 20 74 68 )] overwrites th
3e440 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 e callback.**
3e450 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 registere
3e460 64 20 62 79 20 70 72 69 6f 72 20 63 61 6c 6c 73 d by prior calls
3e470 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 34 7d ..**.** {H12964}
3e480 20 49 66 20 74 68 65 20 46 20 61 72 67 75 6d 65 If the F argume
3e490 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 nt to [sqlite3_r
3e4a0 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46 ollback_hook(D,F
3e4b0 2c 50 29 5d 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 ,P)] is NULL.**
3e4c0 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 74 68 then th
3e4d0 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 e rollback hook
3e4e0 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e 63 callback is canc
3e4f0 65 6c 65 64 20 61 6e 64 20 6e 6f 20 63 61 6c 6c eled and no call
3e500 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 back.**
3e510 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e is invoked when
3e520 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 a transaction r
3e530 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2f 0a 53 51 olls back..*/.SQ
3e540 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
3e550 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f qlite3_commit_ho
3e560 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ok(sqlite3*, int
3e570 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 (*)(void*), void
3e580 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 *);.SQLITE_API v
3e590 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c oid *sqlite3_rol
3e5a0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 lback_hook(sqlit
3e5b0 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 e3*, void(*)(voi
3e5c0 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f d *), void*);../
3e5d0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
3e5e0 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 ata Change Notif
3e5f0 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b ication Callback
3e600 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 34 s {H12970} <S604
3e610 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
3e620 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f lite3_update_hoo
3e630 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 k() interface re
3e640 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 gisters a callba
3e650 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 ck function.** w
3e660 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 ith the [databas
3e670 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 e connection] id
3e680 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 entified by the
3e690 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a first argument.*
3e6a0 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 * to be invoked
3e6b0 77 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 69 whenever a row i
3e6c0 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72 s updated, inser
3e6d0 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a ted or deleted..
3e6e0 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 ** Any callback
3e6f0 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 set by a previou
3e700 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 s call to this f
3e710 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 unction.** for t
3e720 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
3e730 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f connection is o
3e740 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a verridden..**.**
3e750 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
3e760 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 ment is a pointe
3e770 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f r to the functio
3e780 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e n to invoke when
3e790 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 a.** row is upd
3e7a0 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f ated, inserted o
3e7b0 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 r deleted..** Th
3e7c0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
3e7d0 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
3e7e0 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 is a copy of th
3e7f0 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
3e800 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 .** to sqlite3_u
3e810 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a pdate_hook()..**
3e820 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c The second call
3e830 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 back argument is
3e840 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f one of [SQLITE_
3e850 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 INSERT], [SQLITE
3e860 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 _DELETE],.** or
3e870 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c [SQLITE_UPDATE],
3e880 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 depending on th
3e890 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 e operation that
3e8a0 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c caused the call
3e8b0 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e back.** to be in
3e8c0 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 voked..** The th
3e8d0 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 ird and fourth a
3e8e0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
3e8f0 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e callback contain
3e900 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 pointers to the
3e910 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 .** database and
3e920 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 table name cont
3e930 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63 aining the affec
3e940 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 ted row..** The
3e950 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 final callback p
3e960 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 arameter is the
3e970 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77 rowid of the row
3e980 2e 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f 66 . In the case of
3e990 0a 2a 2a 20 61 6e 20 75 70 64 61 74 65 2c 20 74 .** an update, t
3e9a0 68 69 73 20 69 73 20 74 68 65 20 72 6f 77 69 64 his is the rowid
3e9b0 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 after the updat
3e9c0 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a 2a e takes place..*
3e9d0 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 *.** The update
3e9e0 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f hook is not invo
3e9f0 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e 61 ked when interna
3ea00 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 20 l system tables
3ea10 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 are.** modified
3ea20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 (i.e. sqlite_mas
3ea30 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 ter and sqlite_s
3ea40 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20 equence)..**.**
3ea50 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 The update hook
3ea60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d implementation m
3ea70 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 ust not do anyth
3ea80 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f ing that will mo
3ea90 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 dify.** the data
3eaa0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
3eab0 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 that invoked the
3eac0 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 update hook. A
3ead0 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f ny actions.** to
3eae0 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 modify the data
3eaf0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
3eb00 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 must be deferred
3eb10 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 until after the
3eb20 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f .** completion o
3eb30 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 f the [sqlite3_s
3eb40 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 tep()] call that
3eb50 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20 75 triggered the u
3eb60 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e pdate hook..** N
3eb70 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 ote that [sqlite
3eb80 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
3eb90 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 and [sqlite3_ste
3eba0 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 p()] both modify
3ebb0 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 their.** databa
3ebc0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 se connections f
3ebd0 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f or the meaning o
3ebe0 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 f "modify" in th
3ebf0 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a is paragraph..**
3ec00 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 .** If another f
3ec10 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 unction was prev
3ec20 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 iously registere
3ec30 64 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c 75 d, its pArg valu
3ec40 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 e.** is returned
3ec50 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c . Otherwise NUL
3ec60 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a L is returned..*
3ec70 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
3ec80 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 37 31 7d 20 .**.** {H12971}
3ec90 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 70 64 The [sqlite3_upd
3eca0 61 74 65 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d ate_hook(D,F,P)]
3ecb0 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 65 interface cause
3ecc0 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a s the callback.*
3ecd0 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 74 * funct
3ece0 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 6e 76 6f ion F to be invo
3ecf0 6b 65 64 20 77 69 74 68 20 66 69 72 73 74 20 70 ked with first p
3ed00 61 72 61 6d 65 74 65 72 20 50 20 77 68 65 6e 65 arameter P whene
3ed10 76 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ver.**
3ed20 61 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 6d a table row is m
3ed30 6f 64 69 66 69 65 64 2c 20 69 6e 73 65 72 74 65 odified, inserte
3ed40 64 2c 20 6f 72 20 64 65 6c 65 74 65 64 20 6f 6e d, or deleted on
3ed50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
3ed60 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
3ed70 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 ction] D..**.**
3ed80 7b 48 31 32 39 37 33 7d 20 54 68 65 20 5b 73 71 {H12973} The [sq
3ed90 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f lite3_update_hoo
3eda0 6b 28 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 k(D,F,P)] interf
3edb0 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ace returns the
3edc0 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20 value.**
3edd0 20 20 6f 66 20 50 20 66 6f 72 20 74 68 65 20 70 of P for the p
3ede0 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 6f 6e 20 revious call on
3edf0 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61 the same [databa
3ee00 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 se connection] D
3ee10 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 ,.** or
3ee20 20 4e 55 4c 4c 20 66 6f 72 20 74 68 65 20 66 69 NULL for the fi
3ee30 72 73 74 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 rst call..**.**
3ee40 7b 48 31 32 39 37 35 7d 20 49 66 20 74 68 65 20 {H12975} If the
3ee50 75 70 64 61 74 65 20 68 6f 6f 6b 20 63 61 6c 6c update hook call
3ee60 62 61 63 6b 20 46 20 69 6e 20 5b 73 71 6c 69 74 back F in [sqlit
3ee70 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 44 e3_update_hook(D
3ee80 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,F,P)].**
3ee90 20 20 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 is NULL then
3eea0 74 68 65 20 6e 6f 20 75 70 64 61 74 65 20 63 61 the no update ca
3eeb0 6c 6c 62 61 63 6b 73 20 61 72 65 20 6d 61 64 65 llbacks are made
3eec0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 37 37 7d ..**.** {H12977}
3eed0 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 Each call to [s
3eee0 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f qlite3_update_ho
3eef0 6f 6b 28 44 2c 46 2c 50 29 5d 20 6f 76 65 72 72 ok(D,F,P)] overr
3ef00 69 64 65 73 20 70 72 69 6f 72 20 63 61 6c 6c 73 ides prior calls
3ef10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 .** to
3ef20 74 68 65 20 73 61 6d 65 20 69 6e 74 65 72 66 61 the same interfa
3ef30 63 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b ce on the same [
3ef40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3ef50 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ion] D..**.** {H
3ef60 31 32 39 37 39 7d 20 54 68 65 20 75 70 64 61 74 12979} The updat
3ef70 65 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 e hook callback
3ef80 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 77 is not invoked w
3ef90 68 65 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79 73 hen internal sys
3efa0 74 65 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 tem.**
3efb0 74 61 62 6c 65 73 20 73 75 63 68 20 61 73 20 73 tables such as s
3efc0 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 qlite_master and
3efd0 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 sqlite_sequence
3efe0 20 61 72 65 20 6d 6f 64 69 66 69 65 64 2e 0a 2a are modified..*
3eff0 2a 0a 2a 2a 20 7b 48 31 32 39 38 31 7d 20 54 68 *.** {H12981} Th
3f000 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
3f010 65 72 20 74 6f 20 74 68 65 20 75 70 64 61 74 65 er to the update
3f020 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 callback.**
3f030 20 20 20 20 20 20 69 73 20 6f 6e 65 20 6f 66 20 is one of
3f040 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 5d 2c [SQLITE_INSERT],
3f050 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d [SQLITE_DELETE]
3f060 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44 41 or [SQLITE_UPDA
3f070 54 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 TE],.**
3f080 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 depending on th
3f090 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 e operation that
3f0a0 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c caused the call
3f0b0 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b back to be invok
3f0c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 38 ed..**.** {H1298
3f0d0 33 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 3} The third and
3f0e0 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 fourth argument
3f0f0 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 s to the callbac
3f100 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65 k contain pointe
3f110 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 rs.** t
3f120 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 o zero-terminate
3f130 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 d UTF-8 strings
3f140 77 68 69 63 68 20 61 72 65 20 74 68 65 20 6e 61 which are the na
3f150 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 mes of the.**
3f160 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 database
3f170 61 6e 64 20 74 61 62 6c 65 20 74 68 61 74 20 69 and table that i
3f180 73 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e s being updated.
3f190 0a 0a 2a 2a 20 7b 48 31 32 39 38 35 7d 20 54 68 ..** {H12985} Th
3f1a0 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b e final callback
3f1b0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 parameter is th
3f1c0 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 e rowid of the r
3f1d0 6f 77 20 61 66 74 65 72 0a 2a 2a 20 20 20 20 20 ow after.**
3f1e0 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65 20 the change
3f1f0 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 occurs..*/.SQLIT
3f200 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
3f210 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 te3_update_hook(
3f220 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 . sqlite3*, .
3f230 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 2c 69 void(*)(void *,i
3f240 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a nt ,char const *
3f250 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 ,char const *,sq
3f260 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a 20 20 lite3_int64),.
3f270 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 void*.);../*.**
3f280 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 CAPI3REF: Enable
3f290 20 4f 72 20 44 69 73 61 62 6c 65 20 53 68 61 72 Or Disable Shar
3f2a0 65 64 20 50 61 67 65 72 20 43 61 63 68 65 20 7b ed Pager Cache {
3f2b0 48 31 30 33 33 30 7d 20 3c 53 33 30 39 30 30 3e H10330} <S30900>
3f2c0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 73 .** KEYWORDS: {s
3f2d0 68 61 72 65 64 20 63 61 63 68 65 7d 20 7b 73 68 hared cache} {sh
3f2e0 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 7d ared cache mode}
3f2f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
3f300 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 ine enables or d
3f310 69 73 61 62 6c 65 73 20 74 68 65 20 73 68 61 72 isables the shar
3f320 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 ing of the datab
3f330 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64 ase cache.** and
3f340 20 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 72 schema data str
3f350 75 63 74 75 72 65 73 20 62 65 74 77 65 65 6e 20 uctures between
3f360 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
3f370 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f tion | connectio
3f380 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 ns].** to the sa
3f390 6d 65 20 64 61 74 61 62 61 73 65 2e 20 53 68 61 me database. Sha
3f3a0 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 ring is enabled
3f3b0 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 if the argument
3f3c0 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64 is true.** and d
3f3d0 69 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 isabled if the a
3f3e0 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 rgument is false
3f3f0 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 68 ..**.** Cache sh
3f400 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 aring is enabled
3f410 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 66 6f and disabled fo
3f420 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 6f 63 r an entire proc
3f430 65 73 73 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 ess. {END}.** Th
3f440 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20 61 is is a change a
3f450 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 73 s of SQLite vers
3f460 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70 72 ion 3.5.0. In pr
3f470 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ior versions of
3f480 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72 69 SQLite,.** shari
3f490 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f ng was enabled o
3f4a0 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 65 r disabled for e
3f4b0 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61 72 ach thread separ
3f4c0 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ately..**.** The
3f4d0 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20 6d cache sharing m
3f4e0 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73 20 ode set by this
3f4f0 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63 74 interface effect
3f500 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 s all subsequent
3f510 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 .** calls to [sq
3f520 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b lite3_open()], [
3f530 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
3f540 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 )], and [sqlite3
3f550 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 45 _open16()]..** E
3f560 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 xisting database
3f570 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f 6e connections con
3f580 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73 68 tinue use the sh
3f590 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 68 aring mode.** th
3f5a0 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63 74 at was in effect
3f5b0 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68 65 at the time the
3f5c0 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a 2a y were opened..*
3f5d0 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 *.** Virtual tab
3f5e0 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 les cannot be us
3f5f0 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65 64 ed with a shared
3f600 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73 68 cache. When sh
3f610 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 ared.** cache is
3f620 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b 73 enabled, the [s
3f630 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f qlite3_create_mo
3f640 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65 64 dule()] API used
3f650 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 to register.**
3f660 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77 virtual tables w
3f670 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 ill always retur
3f680 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a n an error..**.*
3f690 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
3f6a0 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f eturns [SQLITE_O
3f6b0 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61 63 K] if shared cac
3f6c0 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f he was enabled o
3f6d0 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73 75 r disabled.** su
3f6e0 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e 20 ccessfully. An
3f6f0 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 [error code] is
3f700 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77 69 returned otherwi
3f710 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 se..**.** Shared
3f720 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62 6c cache is disabl
3f730 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 42 ed by default. B
3f740 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63 68 ut this might ch
3f750 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75 72 ange in.** futur
3f760 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 e releases of SQ
3f770 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74 69 Lite. Applicati
3f780 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61 62 ons that care ab
3f790 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 out shared.** ca
3f7a0 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f 75 che setting shou
3f7b0 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69 63 ld set it explic
3f7c0 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 itly..**.** INVA
3f7d0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
3f7e0 31 30 33 33 31 7d 20 41 20 73 75 63 63 65 73 73 10331} A success
3f7f0 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f ful invocation o
3f800 66 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c f [sqlite3_enabl
3f810 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 42 e_shared_cache(B
3f820 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 )].** w
3f830 69 6c 6c 20 65 6e 61 62 6c 65 20 6f 72 20 64 69 ill enable or di
3f840 73 61 62 6c 65 20 73 68 61 72 65 64 20 63 61 63 sable shared cac
3f850 68 65 20 6d 6f 64 65 20 66 6f 72 20 61 6e 79 20 he mode for any
3f860 73 75 62 73 65 71 75 65 6e 74 6c 79 0a 2a 2a 20 subsequently.**
3f870 20 20 20 20 20 20 20 20 20 63 72 65 61 74 65 64 created
3f880 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
3f890 63 74 69 6f 6e 5d 20 69 6e 20 74 68 65 20 73 61 ction] in the sa
3f8a0 6d 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a me process..**.*
3f8b0 2a 20 7b 48 31 30 33 33 36 7d 20 57 68 65 6e 20 * {H10336} When
3f8c0 73 68 61 72 65 64 20 63 61 63 68 65 20 69 73 20 shared cache is
3f8d0 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b 73 71 enabled, the [sq
3f8e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
3f8f0 75 6c 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ule()].**
3f900 20 20 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c interface wil
3f910 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 l always return
3f920 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 an error..**.**
3f930 7b 48 31 30 33 33 37 7d 20 54 68 65 20 5b 73 71 {H10337} The [sq
3f940 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 lite3_enable_sha
3f950 72 65 64 5f 63 61 63 68 65 28 42 29 5d 20 69 6e red_cache(B)] in
3f960 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a terface returns.
3f970 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c ** [SQL
3f980 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 ITE_OK] if share
3f990 64 20 63 61 63 68 65 20 77 61 73 20 65 6e 61 62 d cache was enab
3f9a0 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 20 led or disabled
3f9b0 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a successfully..**
3f9c0 0a 2a 2a 20 7b 48 31 30 33 33 39 7d 20 53 68 61 .** {H10339} Sha
3f9d0 72 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73 red cache is dis
3f9e0 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
3f9f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3fa00 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 int sqlite3_enab
3fa10 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 le_shared_cache(
3fa20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 int);../*.** CAP
3fa30 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74 20 54 I3REF: Attempt T
3fa40 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65 6d 6f o Free Heap Memo
3fa50 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53 33 30 ry {H17340} <S30
3fa60 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 220>.**.** The s
3fa70 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
3fa80 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61 63 emory() interfac
3fa90 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 72 e attempts to fr
3faa0 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20 6f 66 ee N bytes.** of
3fab0 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 20 heap memory by
3fac0 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e deallocating non
3fad0 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 -essential memor
3fae0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a y allocations.**
3faf0 20 68 65 6c 64 20 62 79 20 74 68 65 20 64 61 74 held by the dat
3fb00 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e 20 7b abase library. {
3fb10 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75 73 65 END} Memory use
3fb20 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 d to cache datab
3fb30 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 6f 20 ase.** pages to
3fb40 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d 61 improve performa
3fb50 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70 6c nce is an exampl
3fb60 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 e of non-essenti
3fb70 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 73 71 al memory..** sq
3fb80 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
3fb90 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20 74 mory() returns t
3fba0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
3fbb0 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 65 es actually free
3fbc0 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67 68 d,.** which migh
3fbd0 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65 73 t be more or les
3fbe0 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75 6e s than the amoun
3fbf0 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2a 0a t requested..**.
3fc00 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
3fc10 2a 0a 2a 2a 20 7b 48 31 37 33 34 31 7d 20 54 68 *.** {H17341} Th
3fc20 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 e [sqlite3_relea
3fc30 73 65 5f 6d 65 6d 6f 72 79 28 4e 29 5d 20 69 6e se_memory(N)] in
3fc40 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 terface attempts
3fc50 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 to.**
3fc60 66 72 65 65 20 4e 20 62 79 74 65 73 20 6f 66 20 free N bytes of
3fc70 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 20 64 heap memory by d
3fc80 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e 2d eallocating non-
3fc90 65 73 73 65 6e 74 69 61 6c 0a 2a 2a 20 20 20 20 essential.**
3fca0 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 61 6c 6c memory all
3fcb0 6f 63 61 74 69 6f 6e 73 20 68 65 6c 64 20 62 79 ocations held by
3fcc0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 69 the database li
3fcd0 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 brary..**.** {H1
3fce0 36 33 34 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 6342} The [sqlit
3fcf0 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
3fd00 79 28 4e 29 5d 20 72 65 74 75 72 6e 73 20 74 68 y(N)] returns th
3fd10 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 e number.**
3fd20 20 20 20 20 20 6f 66 20 62 79 74 65 73 20 61 63 of bytes ac
3fd30 74 75 61 6c 6c 79 20 66 72 65 65 64 2c 20 77 68 tually freed, wh
3fd40 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f 72 ich might be mor
3fd50 65 20 6f 72 20 6c 65 73 73 0a 2a 2a 20 20 20 20 e or less.**
3fd60 20 20 20 20 20 20 74 68 61 6e 20 74 68 65 20 61 than the a
3fd70 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65 64 2e mount requested.
3fd80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
3fd90 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 nt sqlite3_relea
3fda0 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a se_memory(int);.
3fdb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3fdc0 20 49 6d 70 6f 73 65 20 41 20 4c 69 6d 69 74 20 Impose A Limit
3fdd0 4f 6e 20 48 65 61 70 20 53 69 7a 65 20 7b 48 31 On Heap Size {H1
3fde0 37 33 35 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 7350} <S30220>.*
3fdf0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
3fe00 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 _soft_heap_limit
3fe10 28 29 20 69 6e 74 65 72 66 61 63 65 20 70 6c 61 () interface pla
3fe20 63 65 73 20 61 20 22 73 6f 66 74 22 20 6c 69 6d ces a "soft" lim
3fe30 69 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 61 6d 6f it.** on the amo
3fe40 75 6e 74 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f unt of heap memo
3fe50 72 79 20 74 68 61 74 20 6d 61 79 20 62 65 20 61 ry that may be a
3fe60 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69 llocated by SQLi
3fe70 74 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 te..** If an int
3fe80 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e ernal allocation
3fe90 20 69 73 20 72 65 71 75 65 73 74 65 64 20 74 68 is requested th
3fea0 61 74 20 77 6f 75 6c 64 20 65 78 63 65 65 64 20 at would exceed
3feb0 74 68 65 0a 2a 2a 20 73 6f 66 74 20 68 65 61 70 the.** soft heap
3fec0 20 6c 69 6d 69 74 2c 20 5b 73 71 6c 69 74 65 33 limit, [sqlite3
3fed0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
3fee0 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e )] is invoked on
3fef0 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d e or.** more tim
3ff00 65 73 20 74 6f 20 66 72 65 65 20 75 70 20 73 6f es to free up so
3ff10 6d 65 20 73 70 61 63 65 20 62 65 66 6f 72 65 20 me space before
3ff20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
3ff30 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a s performed..**.
3ff40 2a 2a 20 54 68 65 20 6c 69 6d 69 74 20 69 73 20 ** The limit is
3ff50 63 61 6c 6c 65 64 20 22 73 6f 66 74 22 2c 20 62 called "soft", b
3ff60 65 63 61 75 73 65 20 69 66 20 5b 73 71 6c 69 74 ecause if [sqlit
3ff70 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
3ff80 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 y()].** cannot f
3ff90 72 65 65 20 73 75 66 66 69 63 69 65 6e 74 20 6d ree sufficient m
3ffa0 65 6d 6f 72 79 20 74 6f 20 70 72 65 76 65 6e 74 emory to prevent
3ffb0 20 74 68 65 20 6c 69 6d 69 74 20 66 72 6f 6d 20 the limit from
3ffc0 62 65 69 6e 67 20 65 78 63 65 65 64 65 64 2c 0a being exceeded,.
3ffd0 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 73 ** the memory is
3ffe0 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79 77 61 allocated anywa
3fff0 79 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e y and the curren
40000 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 t operation proc
40010 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 eeds..**.** A ne
40020 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 76 gative or zero v
40030 61 6c 75 65 20 66 6f 72 20 4e 20 6d 65 61 6e 73 alue for N means
40040 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e that there is n
40050 6f 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 o soft heap limi
40060 74 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 t and.** [sqlite
40070 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
40080 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 ()] will only be
40090 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65 6d called when mem
400a0 6f 72 79 20 69 73 20 65 78 68 61 75 73 74 65 64 ory is exhausted
400b0 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 ..** The default
400c0 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73 value for the s
400d0 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69 oft heap limit i
400e0 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 s zero..**.** SQ
400f0 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 62 65 73 Lite makes a bes
40100 74 20 65 66 66 6f 72 74 20 74 6f 20 68 6f 6e 6f t effort to hono
40110 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 r the soft heap
40120 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75 74 20 69 66 limit..** But if
40130 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c the soft heap l
40140 69 6d 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 68 imit cannot be h
40150 6f 6e 6f 72 65 64 2c 20 65 78 65 63 75 74 69 6f onored, executio
40160 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e n will.** contin
40170 75 65 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72 ue without error
40180 20 6f 72 20 6e 6f 74 69 66 69 63 61 74 69 6f 6e or notification
40190 2e 20 20 54 68 69 73 20 69 73 20 77 68 79 20 74 . This is why t
401a0 68 65 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 63 he limit is.** c
401b0 61 6c 6c 65 64 20 61 20 22 73 6f 66 74 22 20 6c alled a "soft" l
401c0 69 6d 69 74 2e 20 20 49 74 20 69 73 20 61 64 76 imit. It is adv
401d0 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a isory only..**.*
401e0 2a 20 50 72 69 6f 72 20 74 6f 20 53 51 4c 69 74 * Prior to SQLit
401f0 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c e version 3.5.0,
40200 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e this routine on
40210 6c 79 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 ly constrained t
40220 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c he memory.** all
40230 6f 63 61 74 65 64 20 62 79 20 61 20 73 69 6e 67 ocated by a sing
40240 6c 65 20 74 68 72 65 61 64 20 2d 20 74 68 65 20 le thread - the
40250 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 same thread in w
40260 68 69 63 68 20 74 68 69 73 20 72 6f 75 74 69 6e hich this routin
40270 65 0a 2a 2a 20 72 75 6e 73 2e 20 20 42 65 67 69 e.** runs. Begi
40280 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 69 74 nning with SQLit
40290 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c e version 3.5.0,
402a0 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c the soft heap l
402b0 69 6d 69 74 20 69 73 0a 2a 2a 20 61 70 70 6c 69 imit is.** appli
402c0 65 64 20 74 6f 20 61 6c 6c 20 74 68 72 65 61 64 ed to all thread
402d0 73 2e 20 54 68 65 20 76 61 6c 75 65 20 73 70 65 s. The value spe
402e0 63 69 66 69 65 64 20 66 6f 72 20 74 68 65 20 73 cified for the s
402f0 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a oft heap limit.*
40300 2a 20 69 73 20 61 6e 20 75 70 70 65 72 20 62 6f * is an upper bo
40310 75 6e 64 20 6f 6e 20 74 68 65 20 74 6f 74 61 6c und on the total
40320 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
40330 6f 6e 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 61 on for all threa
40340 64 73 2e 20 49 6e 0a 2a 2a 20 76 65 72 73 69 6f ds. In.** versio
40350 6e 20 33 2e 35 2e 30 20 74 68 65 72 65 20 69 73 n 3.5.0 there is
40360 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f no mechanism fo
40370 72 20 6c 69 6d 69 74 69 6e 67 20 74 68 65 20 68 r limiting the h
40380 65 61 70 20 75 73 61 67 65 20 66 6f 72 0a 2a 2a eap usage for.**
40390 20 69 6e 64 69 76 69 64 75 61 6c 20 74 68 72 65 individual thre
403a0 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 ads..**.** INVAR
403b0 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 IANTS:.**.** {H1
403c0 36 33 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 6351} The [sqlit
403d0 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d e3_soft_heap_lim
403e0 69 74 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 it(N)] interface
403f0 20 70 6c 61 63 65 73 20 61 20 73 6f 66 74 20 6c places a soft l
40400 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 imit.**
40410 20 6f 66 20 4e 20 62 79 74 65 73 20 6f 6e 20 74 of N bytes on t
40420 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 he amount of hea
40430 70 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61 p memory that ma
40440 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a y be allocated.*
40450 2a 20 20 20 20 20 20 20 20 20 20 75 73 69 6e 67 * using
40460 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
40470 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
40480 72 65 61 6c 6c 6f 63 28 29 5d 20 61 74 20 61 6e realloc()] at an
40490 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 y point.**
404a0 20 20 20 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a in time..**.
404b0 2a 2a 20 7b 48 31 36 33 35 32 7d 20 49 66 20 61 ** {H16352} If a
404c0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
404d0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 3_malloc()] or [
404e0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
404f0 29 5d 20 77 6f 75 6c 64 0a 2a 2a 20 20 20 20 20 )] would.**
40500 20 20 20 20 20 63 61 75 73 65 20 74 68 65 20 74 cause the t
40510 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 61 otal amount of a
40520 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 llocated memory
40530 74 6f 20 65 78 63 65 65 64 20 74 68 65 0a 2a 2a to exceed the.**
40540 20 20 20 20 20 20 20 20 20 20 73 6f 66 74 20 68 soft h
40550 65 61 70 20 6c 69 6d 69 74 2c 20 74 68 65 6e 20 eap limit, then
40560 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 [sqlite3_release
40570 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 6e _memory()] is in
40580 76 6f 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 voked.**
40590 20 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 in an attempt
405a0 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 6d 65 to reduce the me
405b0 6d 6f 72 79 20 75 73 61 67 65 20 70 72 69 6f 72 mory usage prior
405c0 20 74 6f 20 70 72 6f 63 65 65 64 69 6e 67 0a 2a to proceeding.*
405d0 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68 20 * with
405e0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
405f0 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 2e 0a 2a ation attempt..*
40600 2a 0a 2a 2a 20 7b 48 31 36 33 35 33 7d 20 43 61 *.** {H16353} Ca
40610 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
40620 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 malloc()] or [sq
40630 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d lite3_realloc()]
40640 20 74 68 61 74 20 74 72 69 67 67 65 72 0a 2a 2a that trigger.**
40650 20 20 20 20 20 20 20 20 20 20 61 74 74 65 6d 70 attemp
40660 74 73 20 74 6f 20 72 65 64 75 63 65 20 6d 65 6d ts to reduce mem
40670 6f 72 79 20 75 73 61 67 65 20 74 68 72 6f 75 67 ory usage throug
40680 68 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 h the soft heap
40690 6c 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 limit.**
406a0 20 20 6d 65 63 68 61 6e 69 73 6d 20 63 6f 6e 74 mechanism cont
406b0 69 6e 75 65 20 65 76 65 6e 20 69 66 20 74 68 65 inue even if the
406c0 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64 75 attempt to redu
406d0 63 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 ce memory.**
406e0 20 20 20 20 20 20 75 73 61 67 65 20 69 73 20 75 usage is u
406f0 6e 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a nsuccessful..**.
40700 2a 2a 20 7b 48 31 36 33 35 34 7d 20 41 20 6e 65 ** {H16354} A ne
40710 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 76 gative or zero v
40720 61 6c 75 65 20 66 6f 72 20 4e 20 69 6e 20 61 20 alue for N in a
40730 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 call to.**
40740 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 [sqlite3_sof
40750 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 4e 29 5d t_heap_limit(N)]
40760 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 means that ther
40770 65 20 69 73 20 6e 6f 20 73 6f 66 74 0a 2a 2a 20 e is no soft.**
40780 20 20 20 20 20 20 20 20 20 68 65 61 70 20 6c 69 heap li
40790 6d 69 74 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 mit and [sqlite3
407a0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
407b0 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a )] will only be.
407c0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c ** call
407d0 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69 ed when memory i
407e0 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 68 s completely exh
407f0 61 75 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 austed..**.** {H
40800 31 36 33 35 35 7d 20 54 68 65 20 64 65 66 61 75 16355} The defau
40810 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
40820 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
40830 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 is zero..**.**
40840 7b 48 31 36 33 35 38 7d 20 45 61 63 68 20 63 61 {H16358} Each ca
40850 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 ll to [sqlite3_s
40860 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 4e oft_heap_limit(N
40870 29 5d 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 )] overrides the
40880 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c .** val
40890 75 65 73 20 73 65 74 20 62 79 20 61 6c 6c 20 70 ues set by all p
408a0 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53 rior calls..*/.S
408b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
408c0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 qlite3_soft_heap
408d0 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f 2a _limit(int);../*
408e0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 78 .** CAPI3REF: Ex
408f0 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20 41 tract Metadata A
40900 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f 66 bout A Column Of
40910 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35 30 A Table {H12850
40920 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 2a } <S60300>.**.**
40930 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
40940 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20 61 turns metadata a
40950 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 20 bout a specific
40960 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65 63 column of a spec
40970 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73 65 ific.** database
40980 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62 6c table accessibl
40990 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61 74 e using the [dat
409a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
409b0 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 73 ] handle.** pass
409c0 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
409d0 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
409e0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c t..**.** The col
409f0 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69 65 umn is identifie
40a00 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 2c d by the second,
40a10 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 third and fourt
40a20 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a h parameters to.
40a30 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
40a40 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 . The second par
40a50 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65 72 ameter is either
40a60 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
40a70 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69 2e database.** (i.
40a80 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 e. "main", "temp
40a90 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65 64 " or an attached
40aa0 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74 61 database) conta
40ab0 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69 66 ining the specif
40ac0 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 ied.** table or
40ad0 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 4e NULL. If it is N
40ae0 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74 ULL, then all at
40af0 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 tached databases
40b00 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a 2a are searched.**
40b10 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 75 for the table u
40b20 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 6c sing the same al
40b30 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 20 gorithm used by
40b40 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67 the database eng
40b50 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c 76 ine to.** resolv
40b60 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 e unqualified ta
40b70 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0a ble references..
40b80 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 **.** The third
40b90 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d and fourth param
40ba0 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75 eters to this fu
40bb0 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 nction are the t
40bc0 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a able and column.
40bd0 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 ** name of the d
40be0 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 72 esired column, r
40bf0 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 69 espectively. Nei
40c00 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 61 ther of these pa
40c10 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 20 rameters.** may
40c20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4d be NULL..**.** M
40c30 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75 72 etadata is retur
40c40 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 74 ned by writing t
40c50 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 o the memory loc
40c60 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 73 ations passed as
40c70 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64 20 the 5th.** and
40c80 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d subsequent param
40c90 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75 eters to this fu
40ca0 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 74 nction. Any of t
40cb0 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20 6d hese arguments m
40cc0 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 69 ay be.** NULL, i
40cd0 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
40ce0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 corresponding e
40cf0 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64 61 lement of metada
40d00 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a ta is omitted..*
40d10 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
40d20 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 >.** <table bord
40d30 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c er="1">.** <tr><
40d40 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c 74 th> Parameter <t
40d50 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79 70 h> Output<br>Typ
40d60 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70 74 e <th> Descript
40d70 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 ion.**.** <tr><t
40d80 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e 73 d> 5th <td> cons
40d90 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61 74 t char* <td> Dat
40da0 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c 74 a type.** <tr><t
40db0 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e 73 d> 6th <td> cons
40dc0 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61 6d t char* <td> Nam
40dd0 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f 6c e of default col
40de0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a lation sequence.
40df0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68 20 ** <tr><td> 7th
40e00 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 <td> int
40e10 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f <td> True if co
40e20 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20 4e lumn has a NOT N
40e30 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a ULL constraint.*
40e40 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20 3c * <tr><td> 8th <
40e50 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20 td> int
40e60 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c <td> True if col
40e70 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74 umn is part of t
40e80 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a 2a he PRIMARY KEY.*
40e90 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20 3c * <tr><td> 9th <
40ea0 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20 td> int
40eb0 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c <td> True if col
40ec0 75 6d 6e 20 69 73 20 41 55 54 4f 49 4e 43 52 45 umn is AUTOINCRE
40ed0 4d 45 4e 54 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e MENT.** </table>
40ee0 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 .** </blockquote
40ef0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f >.**.** The memo
40f00 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ry pointed to by
40f10 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20 70 the character p
40f20 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 ointers returned
40f30 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 65 63 6c for the.** decl
40f40 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 aration type and
40f50 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
40f60 6e 63 65 20 69 73 20 76 61 6c 69 64 20 6f 6e 6c nce is valid onl
40f70 79 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 y until the next
40f80 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 .** call to any
40f90 53 51 4c 69 74 65 20 41 50 49 20 66 75 6e 63 74 SQLite API funct
40fa0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ion..**.** If th
40fb0 65 20 73 70 65 63 69 66 69 65 64 20 74 61 62 6c e specified tabl
40fc0 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 e is actually a
40fd0 76 69 65 77 2c 20 61 6e 20 5b 65 72 72 6f 72 20 view, an [error
40fe0 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 code] is returne
40ff0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
41000 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e specified column
41010 20 69 73 20 22 72 6f 77 69 64 22 2c 20 22 6f 69 is "rowid", "oi
41020 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 20 d" or "_rowid_"
41030 61 6e 64 20 61 6e 0a 2a 2a 20 49 4e 54 45 47 45 and an.** INTEGE
41040 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f R PRIMARY KEY co
41050 6c 75 6d 6e 20 68 61 73 20 62 65 65 6e 20 65 78 lumn has been ex
41060 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 plicitly declare
41070 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74 70 d, then the outp
41080 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 ut.** parameters
41090 20 61 72 65 20 73 65 74 20 66 6f 72 20 74 68 65 are set for the
410a0 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c explicitly decl
410b0 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 ared column. If
410c0 74 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 65 there is no.** e
410d0 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 xplicitly declar
410e0 65 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 ed INTEGER PRIMA
410f0 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20 74 RY KEY column, t
41100 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a hen the output.*
41110 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 * parameters are
41120 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a set as follows:
41130 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 .**.** <pre>.**
41140 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20 22 data type: "
41150 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 20 INTEGER".**
41160 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
41170 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a 20 ce: "BINARY".**
41180 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a not null: 0.
41190 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20 6b ** primary k
411a0 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 74 ey: 1.** aut
411b0 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a 2a o increment: 0.*
411c0 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54 * </pre>.**.** T
411d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
411e0 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 load one or mor
411f0 65 20 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 64 e schemas from d
41200 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 49 atabase files. I
41210 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 f an.** error oc
41220 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 69 73 curs during this
41230 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 process, or if
41240 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 61 the requested ta
41250 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a ble or column.**
41260 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 cannot be found
41270 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 , an [error code
41280 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
41290 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 d an error messa
412a0 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74 68 ge left.** in th
412b0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
412c0 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20 72 ection] (to be r
412d0 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 73 etrieved using s
412e0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 29 qlite3_errmsg())
412f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 ..**.** This API
41300 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
41310 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 le if the librar
41320 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 y was compiled w
41330 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 ith the.** [SQLI
41340 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
41350 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 _METADATA] C-pre
41360 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c processor symbol
41370 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c defined..*/.SQL
41380 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
41390 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e te3_table_column
413a0 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c _metadata(. sql
413b0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
413c0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e /* Conn
413d0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f ection handle */
413e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
413f0 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f DbName, /
41400 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20 * Database name
41410 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e or NULL */. con
41420 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e st char *zTableN
41430 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c ame, /* Tabl
41440 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 e name */. cons
41450 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e t char *zColumnN
41460 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d ame, /* Colum
41470 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 n name */. char
41480 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54 const **pzDataT
41490 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55 ype, /* OUTPU
414a0 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74 61 T: Declared data
414b0 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20 type */. char
414c0 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 const **pzCollSe
414d0 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 q, /* OUTPUT
414e0 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 : Collation sequ
414f0 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 ence name */. i
41500 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 nt *pNotNull,
41510 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
41520 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f TPUT: True if NO
41530 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e T NULL constrain
41540 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e t exists */. in
41550 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20 t *pPrimaryKey,
41560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
41570 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c PUT: True if col
41580 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a umn part of PK *
41590 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e /. int *pAutoin
415a0 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c
415b0 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 /* OUTPUT: True
415c0 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74 if column is aut
415d0 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 o-increment */.)
415e0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
415f0 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e F: Load An Exten
41600 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c 53 sion {H12600} <S
41610 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 20500>.**.** Thi
41620 73 20 69 6e 74 65 72 66 61 63 65 20 6c 6f 61 64 s interface load
41630 73 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65 s an SQLite exte
41640 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 66 72 nsion library fr
41650 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c om the named fil
41660 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 31 e..**.** {H12601
41670 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f } The sqlite3_lo
41680 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 ad_extension() i
41690 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 nterface attempt
416a0 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20 s to load an.**
416b0 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 SQLite
416c0 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 extension librar
416d0 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 y contained in t
416e0 68 65 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a he file zFile..*
416f0 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54 68 *.** {H12602} Th
41700 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 e entry point is
41710 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 zProc..**.** {H
41720 31 32 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61 79 12603} zProc may
41730 20 62 65 20 30 2c 20 69 6e 20 77 68 69 63 68 20 be 0, in which
41740 63 61 73 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 case the name of
41750 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 the entry point
41760 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 65 66 .** def
41770 61 75 6c 74 73 20 74 6f 20 22 73 71 6c 69 74 65 aults to "sqlite
41780 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 3_extension_init
41790 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 34 "..**.** {H12604
417a0 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f } The sqlite3_lo
417b0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 ad_extension() i
417c0 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 nterface shall r
417d0 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 eturn.**
417e0 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e [SQLITE_OK] on
417f0 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53 51 success and [SQ
41800 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 73 LITE_ERROR] if s
41810 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 omething goes wr
41820 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 ong..**.** {H126
41830 30 35 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 05} If an error
41840 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72 occurs and pzErr
41850 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 Msg is not 0, th
41860 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 en the.**
41870 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 [sqlite3_load
41880 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e _extension()] in
41890 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 61 74 terface shall at
418a0 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 20 tempt to.**
418b0 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72 72 fill *pzErr
418c0 4d 73 67 20 77 69 74 68 20 65 72 72 6f 72 20 6d Msg with error m
418d0 65 73 73 61 67 65 20 74 65 78 74 20 73 74 6f 72 essage text stor
418e0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ed in memory.**
418f0 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65 obtaine
41900 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f d from [sqlite3_
41910 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d malloc()]. {END}
41920 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 The calling fu
41930 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 nction.**
41940 20 20 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74 should free t
41950 68 69 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61 his memory by ca
41960 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 lling [sqlite3_f
41970 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ree()]..**.** {H
41980 31 32 36 30 36 7d 20 45 78 74 65 6e 73 69 6f 6e 12606} Extension
41990 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 20 62 65 loading must be
419a0 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a 2a enabled using.*
419b0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
419c0 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f te3_enable_load_
419d0 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 extension()] pri
419e0 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 or to calling th
419f0 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20 20 is API,.**
41a00 20 20 20 20 6f 74 68 65 72 77 69 73 65 20 61 6e otherwise an
41a10 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 error will be r
41a20 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 eturned..*/.SQLI
41a30 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
41a40 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f e3_load_extensio
41a50 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 n(. sqlite3 *db
41a60 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f , /* Lo
41a70 61 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e ad the extension
41a80 20 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 into this datab
41a90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
41aa0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
41ab0 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d zFile, /* Nam
41ac0 65 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 e of the shared
41ad0 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 library containi
41ae0 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a ng extension */.
41af0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
41b00 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 roc, /* Entry
41b10 20 70 6f 69 6e 74 2e 20 20 44 65 72 69 76 65 64 point. Derived
41b20 20 66 72 6f 6d 20 7a 46 69 6c 65 20 69 66 20 30 from zFile if 0
41b30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 */. char **pzE
41b40 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50 rrMsg /* P
41b50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 ut error message
41b60 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a here if not 0 *
41b70 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.);../*.** CAPI
41b80 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 3REF: Enable Or
41b90 44 69 73 61 62 6c 65 20 45 78 74 65 6e 73 69 6f Disable Extensio
41ba0 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 32 n Loading {H1262
41bb0 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 0} <S20500>.**.*
41bc0 2a 20 53 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f * So as not to o
41bd0 70 65 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c pen security hol
41be0 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c es in older appl
41bf0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 61 72 ications that ar
41c00 65 0a 2a 2a 20 75 6e 70 72 65 70 61 72 65 64 20 e.** unprepared
41c10 74 6f 20 64 65 61 6c 20 77 69 74 68 20 65 78 74 to deal with ext
41c20 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 2c 20 ension loading,
41c30 61 6e 64 20 61 73 20 61 20 6d 65 61 6e 73 20 6f and as a means o
41c40 66 20 64 69 73 61 62 6c 69 6e 67 0a 2a 2a 20 65 f disabling.** e
41c50 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 xtension loading
41c60 20 77 68 69 6c 65 20 65 76 61 6c 75 61 74 69 6e while evaluatin
41c70 67 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 g user-entered S
41c80 51 4c 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e QL, the followin
41c90 67 20 41 50 49 0a 2a 2a 20 69 73 20 70 72 6f 76 g API.** is prov
41ca0 69 64 65 64 20 74 6f 20 74 75 72 6e 20 74 68 65 ided to turn the
41cb0 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 [sqlite3_load_e
41cc0 78 74 65 6e 73 69 6f 6e 28 29 5d 20 6d 65 63 68 xtension()] mech
41cd0 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 20 6f 66 66 anism on and off
41ce0 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f ..**.** Extensio
41cf0 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 66 n loading is off
41d00 20 62 79 20 64 65 66 61 75 6c 74 2e 20 53 65 65 by default. See
41d10 20 74 69 63 6b 65 74 20 23 31 38 36 33 2e 0a 2a ticket #1863..*
41d20 2a 0a 2a 2a 20 7b 48 31 32 36 32 31 7d 20 43 61 *.** {H12621} Ca
41d30 6c 6c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 ll the sqlite3_e
41d40 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e nable_load_exten
41d50 73 69 6f 6e 28 29 20 72 6f 75 74 69 6e 65 20 77 sion() routine w
41d60 69 74 68 20 6f 6e 6f 66 66 3d 3d 31 0a 2a 2a 20 ith onoff==1.**
41d70 20 20 20 20 20 20 20 20 20 74 6f 20 74 75 72 6e to turn
41d80 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 extension loadi
41d90 6e 67 20 6f 6e 20 61 6e 64 20 63 61 6c 6c 20 69 ng on and call i
41da0 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30 20 t with onoff==0
41db0 74 6f 20 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 to turn.**
41dc0 20 20 20 20 69 74 20 62 61 63 6b 20 6f 66 66 20 it back off
41dd0 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 again..**.** {H1
41de0 32 36 32 32 7d 20 45 78 74 65 6e 73 69 6f 6e 20 2622} Extension
41df0 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62 loading is off b
41e00 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51 y default..*/.SQ
41e10 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
41e20 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 ite3_enable_load
41e30 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 _extension(sqlit
41e40 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 e3 *db, int onof
41e50 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 f);../*.** CAPI3
41e60 52 45 46 3a 20 41 75 74 6f 6d 61 74 69 63 61 6c REF: Automatical
41e70 6c 79 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e ly Load An Exten
41e80 73 69 6f 6e 73 20 7b 48 31 32 36 34 30 7d 20 3c sions {H12640} <
41e90 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20500>.**.** Th
41ea0 69 73 20 41 50 49 20 63 61 6e 20 62 65 20 69 6e is API can be in
41eb0 76 6f 6b 65 64 20 61 74 20 70 72 6f 67 72 61 6d voked at program
41ec0 20 73 74 61 72 74 75 70 20 69 6e 20 6f 72 64 65 startup in orde
41ed0 72 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a r to register.**
41ee0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 74 61 one or more sta
41ef0 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 tically linked e
41f00 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 77 xtensions that w
41f10 69 6c 6c 20 62 65 20 61 76 61 69 6c 61 62 6c 65 ill be available
41f20 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e 65 77 20 5b .** to all new [
41f30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
41f40 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a ions]. {END}.**.
41f50 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
41f60 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 stores a pointer
41f70 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f to the extensio
41f80 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 20 74 68 n in an array th
41f90 61 74 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 at is.** obtaine
41fa0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f d from [sqlite3_
41fb0 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 49 66 20 79 malloc()]. If y
41fc0 6f 75 20 72 75 6e 20 61 20 6d 65 6d 6f 72 79 20 ou run a memory
41fd0 6c 65 61 6b 20 63 68 65 63 6b 65 72 0a 2a 2a 20 leak checker.**
41fe0 6f 6e 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 on your program
41ff0 61 6e 64 20 69 74 20 72 65 70 6f 72 74 73 20 61 and it reports a
42000 20 6c 65 61 6b 20 62 65 63 61 75 73 65 20 6f 66 leak because of
42010 20 74 68 69 73 20 61 72 72 61 79 2c 20 69 6e 76 this array, inv
42020 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f oke.** [sqlite3_
42030 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e reset_auto_exten
42040 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f sion()] prior to
42050 20 73 68 75 74 64 6f 77 6e 20 74 6f 20 66 72 65 shutdown to fre
42060 65 20 74 68 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a e the memory..**
42070 0a 2a 2a 20 7b 48 31 32 36 34 31 7d 20 54 68 69 .** {H12641} Thi
42080 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 s function regis
42090 74 65 72 73 20 61 6e 20 65 78 74 65 6e 73 69 6f ters an extensio
420a0 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 74 68 n entry point th
420b0 61 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 at is.**
420c0 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 automatically
420d0 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 invoked whenever
420e0 20 61 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 a new [database
420f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 connection].**
42100 20 20 20 20 20 20 20 20 20 69 73 20 6f 70 65 6e is open
42110 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ed using [sqlite
42120 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 3_open()], [sqli
42130 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a te3_open16()],.*
42140 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 5b 73 * or [s
42150 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
42160 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 32 ]..**.** {H12642
42170 7d 20 44 75 70 6c 69 63 61 74 65 20 65 78 74 65 } Duplicate exte
42180 6e 73 69 6f 6e 73 20 61 72 65 20 64 65 74 65 63 nsions are detec
42190 74 65 64 20 73 6f 20 63 61 6c 6c 69 6e 67 20 74 ted so calling t
421a0 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 his routine.**
421b0 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 multiple
421c0 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 20 times with the
421d0 73 61 6d 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 same extension i
421e0 73 20 68 61 72 6d 6c 65 73 73 2e 0a 2a 2a 0a 2a s harmless..**.*
421f0 2a 20 7b 48 31 32 36 34 33 7d 20 54 68 69 73 20 * {H12643} This
42200 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 routine stores a
42210 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
42220 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 extension in an
42230 61 72 72 61 79 0a 2a 2a 20 20 20 20 20 20 20 20 array.**
42240 20 20 74 68 61 74 20 69 73 20 6f 62 74 61 69 6e that is obtain
42250 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
42260 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a _malloc()]..**.*
42270 2a 20 7b 48 31 32 36 34 34 7d 20 41 75 74 6f 6d * {H12644} Autom
42280 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 atic extensions
42290 61 70 70 6c 79 20 61 63 72 6f 73 73 20 61 6c 6c apply across all
422a0 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c threads..*/.SQL
422b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
422c0 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 te3_auto_extensi
422d0 6f 6e 28 76 6f 69 64 20 2a 78 45 6e 74 72 79 50 on(void *xEntryP
422e0 6f 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oint);../*.** CA
422f0 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 PI3REF: Reset Au
42300 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f tomatic Extensio
42310 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 n Loading {H1266
42320 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 0} <S20500>.**.*
42330 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
42340 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 disables all pre
42350 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 viously register
42360 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 ed automatic.**
42370 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 extensions. {END
42380 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 } It undoes the
42390 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 effect of all p
423a0 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 rior.** [sqlite3
423b0 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 _auto_extension(
423c0 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 )] calls..**.**
423d0 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 {H12661} This fu
423e0 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 nction disables
423f0 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 all previously r
42400 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 egistered.**
42410 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 automatic
42420 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a extensions..**.*
42430 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 * {H12662} This
42440 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 function disable
42450 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 s automatic exte
42460 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 nsions in all th
42470 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 reads..*/.SQLITE
42480 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
42490 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 3_reset_auto_ext
424a0 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f ension(void);../
424b0 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d *.****** EXPERIM
424c0 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 ENTAL - subject
424d0 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 to change withou
424e0 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a t notice *******
424f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
42500 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 e interface to t
42510 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
42520 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 mechanism is cu
42530 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 rrently consider
42540 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 ed.** to be expe
42550 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 rimental. The i
42560 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 nterface might c
42570 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 hange in incompa
42580 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 tible ways..** I
42590 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 f this is a prob
425a0 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 lem for you, do
425b0 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 not use the inte
425c0 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69 rface at this ti
425d0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 me..**.** When t
425e0 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
425f0 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 mechanism stabi
42600 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 lizes, we will d
42610 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e eclare the.** in
42620 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 terface fixed, s
42630 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 upport it indefi
42640 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f nitely, and remo
42650 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e ve this comment.
42660 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 .*/../*.** Struc
42670 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 tures used by th
42680 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
42690 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 interface.*/.typ
426a0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
426b0 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 te3_vtab sqlite3
426c0 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 _vtab;.typedef s
426d0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
426e0 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 dex_info sqlite3
426f0 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 _index_info;.typ
42700 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
42710 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
42720 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
42730 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 sor;.typedef str
42740 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 uct sqlite3_modu
42750 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c le sqlite3_modul
42760 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 e;../*.** CAPI3R
42770 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c EF: Virtual Tabl
42780 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 e Object {H18000
42790 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 } <S20400>.** KE
427a0 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f YWORDS: sqlite3_
427b0 6d 6f 64 75 6c 65 0a 2a 2a 20 45 58 50 45 52 49 module.** EXPERI
427c0 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 20 6d MENTAL.**.** A m
427d0 6f 64 75 6c 65 20 69 73 20 61 20 63 6c 61 73 73 odule is a class
427e0 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c of virtual tabl
427f0 65 73 2e 20 20 45 61 63 68 20 6d 6f 64 75 6c 65 es. Each module
42800 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 62 is defined.** b
42810 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 y an instance of
42820 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
42830 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 tructure. This
42840 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73 structure consis
42850 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79 20 6f 66 20 ts.** mostly of
42860 6d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 methods for the
42870 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 module..**.** Th
42880 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 is interface is
42890 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 experimental and
428a0 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 is subject to c
428b0 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f hange or.** remo
428c0 76 61 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 val in future re
428d0 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
428e0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
428f0 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 te3_module {. i
42900 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 nt iVersion;. i
42910 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 71 nt (*xCreate)(sq
42920 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 lite3*, void *pA
42930 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ux,.
42940 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e int argc, con
42950 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 st char *const*a
42960 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rgv,.
42970 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 sqlite3_vtab
42980 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a **ppVTab, char*
42990 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e *);. int (*xCon
429a0 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20 nect)(sqlite3*,
429b0 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 void *pAux,.
429c0 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 int a
429d0 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 rgc, const char
429e0 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 *const*argv,.
429f0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
42a00 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 te3_vtab **ppVTa
42a10 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e b, char**);. in
42a20 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28 t (*xBestIndex)(
42a30 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
42a40 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 Tab, sqlite3_ind
42a50 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 ex_info*);. int
42a60 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28 (*xDisconnect)(
42a70 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
42a80 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 Tab);. int (*xD
42a90 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f estroy)(sqlite3_
42aa0 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 vtab *pVTab);.
42ab0 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c int (*xOpen)(sql
42ac0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 ite3_vtab *pVTab
42ad0 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 , sqlite3_vtab_c
42ae0 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 ursor **ppCursor
42af0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 );. int (*xClos
42b00 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f e)(sqlite3_vtab_
42b10 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 cursor*);. int
42b20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74 (*xFilter)(sqlit
42b30 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c e3_vtab_cursor*,
42b40 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e int idxNum, con
42b50 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c st char *idxStr,
42b60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
42b70 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 int argc, sqlit
42b80 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 e3_value **argv)
42b90 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 ;. int (*xNext)
42ba0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 (sqlite3_vtab_cu
42bb0 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a rsor*);. int (*
42bc0 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74 xEof)(sqlite3_vt
42bd0 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 ab_cursor*);. i
42be0 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 nt (*xColumn)(sq
42bf0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
42c00 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 r*, sqlite3_cont
42c10 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e ext*, int);. in
42c20 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69 t (*xRowid)(sqli
42c30 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a te3_vtab_cursor*
42c40 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
42c50 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20 *pRowid);. int
42c60 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 74 (*xUpdate)(sqlit
42c70 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20 e3_vtab *, int,
42c80 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
42c90 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
42ca0 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67 *);. int (*xBeg
42cb0 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 in)(sqlite3_vtab
42cc0 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 *pVTab);. int
42cd0 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33 (*xSync)(sqlite3
42ce0 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 _vtab *pVTab);.
42cf0 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28 int (*xCommit)(
42d00 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
42d10 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 Tab);. int (*xR
42d20 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33 ollback)(sqlite3
42d30 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 _vtab *pVTab);.
42d40 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63 int (*xFindFunc
42d50 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 tion)(sqlite3_vt
42d60 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e ab *pVtab, int n
42d70 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Arg, const char
42d80 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 *zName,.
42d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 v
42da0 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 oid (**pxFunc)(s
42db0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
42dc0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
42dd0 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 e**),.
42de0 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 voi
42df0 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e d **ppArg);. in
42e00 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c t (*xRename)(sql
42e10 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
42e20 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e , const char *zN
42e30 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ew);.};../*.** C
42e40 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c API3REF: Virtual
42e50 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 20 Table Indexing
42e60 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38 Information {H18
42e70 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 100} <S20400>.**
42e80 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 KEYWORDS: sqlit
42e90 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a e3_index_info.**
42ea0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
42eb0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
42ec0 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 index_info struc
42ed0 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 62 ture and its sub
42ee0 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75 73 structures is us
42ef0 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e ed to.** pass in
42f00 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61 formation into a
42f10 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 72 nd receive the r
42f20 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 78 42 eply from the xB
42f30 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 68 estIndex.** meth
42f40 6f 64 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 od of an sqlite3
42f50 5f 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 66 69 _module. The fi
42f60 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49 6e 70 elds under **Inp
42f70 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a 2a 2a uts** are the.**
42f80 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65 73 74 inputs to xBest
42f90 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20 72 65 Index and are re
42fa0 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73 74 49 ad-only. xBestI
42fb0 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69 74 73 ndex inserts its
42fc0 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f .** results into
42fd0 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73 2a 2a the **Outputs**
42fe0 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 fields..**.** T
42ff0 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d he aConstraint[]
43000 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 20 57 array records W
43010 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 HERE clause cons
43020 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 20 66 traints of the f
43030 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e orm:.**.** <pre>
43040 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72 3c 2f column OP expr</
43050 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 pre>.**.** where
43060 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b 2c 20 OP is =, <,
43070 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f 72 20 <=, >, or
43080 26 67 74 3b 3d 2e 20 20 54 68 65 20 70 61 72 74 >=. The part
43090 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f 72 20 icular operator
430a0 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 is.** stored in
430b0 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 6f 70 aConstraint[].op
430c0 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 . The index of
430d0 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73 74 the column is st
430e0 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f 6e 73 ored in.** aCons
430f0 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d 6e traint[].iColumn
43100 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d . aConstraint[]
43110 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55 45 20 .usable is TRUE
43120 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 20 6f if the.** expr o
43130 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 n the right-hand
43140 20 73 69 64 65 20 63 61 6e 20 62 65 20 65 76 61 side can be eva
43150 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68 75 73 luated (and thus
43160 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a the constraint.
43170 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20 61 6e ** is usable) an
43180 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 63 61 d false if it ca
43190 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nnot..**.** The
431a0 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f 6d 61 optimizer automa
431b0 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74 73 20 tically inverts
431c0 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 72 terms of the for
431d0 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c 75 6d m "expr OP colum
431e0 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 73 20 n".** and makes
431f0 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69 63 61 other simplifica
43200 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 57 48 45 tions to the WHE
43210 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61 6e 20 RE clause in an
43220 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 67 65 attempt to.** ge
43230 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 45 20 t as many WHERE
43240 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e 74 clause terms int
43250 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f 77 6e o the form shown
43260 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 69 62 above as possib
43270 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73 le..** The aCons
43280 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 6f traint[] array o
43290 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48 45 52 nly reports WHER
432a0 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 E clause terms i
432b0 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a n the correct.**
432c0 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66 65 72 form that refer
432d0 20 74 6f 20 74 68 65 20 70 61 72 74 69 63 75 6c to the particul
432e0 61 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 ar virtual table
432f0 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 2e 0a being queried..
43300 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 6f **.** Informatio
43310 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 44 45 n about the ORDE
43320 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 73 R BY clause is s
43330 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65 72 42 tored in aOrderB
43340 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74 65 72 y[]..** Each ter
43350 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20 72 65 m of aOrderBy re
43360 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20 6f cords a column o
43370 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 f the ORDER BY c
43380 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 lause..**.** The
43390 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 xBestIndex meth
433a0 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f od must fill aCo
433b0 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 nstraintUsage[]
433c0 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e with information
433d0 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 20 70 .** about what p
433e0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 61 73 arameters to pas
433f0 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 20 49 s to xFilter. I
43400 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 74 68 f argvIndex>0 th
43410 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68 74 2d en.** the right-
43420 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 hand side of the
43430 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 corresponding a
43440 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73 20 Constraint[] is
43450 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64 evaluated.** and
43460 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61 72 67 becomes the arg
43470 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72 79 20 vIndex-th entry
43480 69 6e 20 61 72 67 76 2e 20 20 49 66 20 61 43 6f in argv. If aCo
43490 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e nstraintUsage[].
434a0 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c omit.** is true,
434b0 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 then the constr
434c0 61 69 6e 74 20 69 73 20 61 73 73 75 6d 65 64 20 aint is assumed
434d0 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 61 6e 64 to be fully hand
434e0 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 76 69 led by the.** vi
434f0 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 rtual table and
43500 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 61 is not checked a
43510 67 61 69 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a gain by SQLite..
43520 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d **.** The idxNum
43530 20 61 6e 64 20 69 64 78 50 74 72 20 76 61 6c 75 and idxPtr valu
43540 65 73 20 61 72 65 20 72 65 63 6f 72 64 65 64 20 es are recorded
43550 61 6e 64 20 70 61 73 73 65 64 20 69 6e 74 6f 20 and passed into
43560 78 46 69 6c 74 65 72 2e 0a 2a 2a 20 73 71 6c 69 xFilter..** sqli
43570 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 75 73 te3_free() is us
43580 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 ed to free idxPt
43590 72 20 69 66 20 6e 65 65 64 54 6f 46 72 65 65 49 r if needToFreeI
435a0 64 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a dxPtr is true..*
435b0 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 *.** The orderBy
435c0 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 Consumed means t
435d0 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 hat output from
435e0 78 46 69 6c 74 65 72 20 77 69 6c 6c 20 6f 63 63 xFilter will occ
435f0 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63 6f 72 ur in.** the cor
43600 72 65 63 74 20 6f 72 64 65 72 20 74 6f 20 73 61 rect order to sa
43610 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 tisfy the ORDER
43620 42 59 20 63 6c 61 75 73 65 20 73 6f 20 74 68 61 BY clause so tha
43630 74 20 6e 6f 20 73 65 70 61 72 61 74 65 0a 2a 2a t no separate.**
43640 20 73 6f 72 74 69 6e 67 20 73 74 65 70 20 69 73 sorting step is
43650 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a required..**.**
43660 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 43 6f The estimatedCo
43670 73 74 20 76 61 6c 75 65 20 69 73 20 61 6e 20 65 st value is an e
43680 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 63 stimate of the c
43690 6f 73 74 20 6f 66 20 64 6f 69 6e 67 20 74 68 65 ost of doing the
436a0 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 6c .** particular l
436b0 6f 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c 20 73 ookup. A full s
436c0 63 61 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 77 can of a table w
436d0 69 74 68 20 4e 20 65 6e 74 72 69 65 73 20 73 68 ith N entries sh
436e0 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61 20 63 ould have.** a c
436f0 6f 73 74 20 6f 66 20 4e 2e 20 20 41 20 62 69 6e ost of N. A bin
43700 61 72 79 20 73 65 61 72 63 68 20 6f 66 20 61 20 ary search of a
43710 74 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74 72 69 table of N entri
43720 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 es should have a
43730 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70 70 72 .** cost of appr
43740 6f 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28 4e 29 oximately log(N)
43750 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 ..**.** This int
43760 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69 erface is experi
43770 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75 mental and is su
43780 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
43790 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e or.** removal in
437a0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
437b0 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 of SQLite..*/.s
437c0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
437d0 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 dex_info {. /*
437e0 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 Inputs */. int
437f0 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 nConstraint;
43800 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
43810 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
43820 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 Constraint */.
43830 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
43840 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 ndex_constraint
43850 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 {. int iColu
43860 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 mn;
43870 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 /* Column on le
43880 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 ft-hand side of
43890 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 constraint */.
438a0 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 unsigned char
438b0 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 op; /*
438c0 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 Constraint opera
438d0 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 tor */. unsi
438e0 67 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 gned char usable
438f0 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 ; /* True if
43900 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 this constraint
43910 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 is usable */.
43920 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 int iTermOffs
43930 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 et; /*
43940 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 Used internally
43950 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f - xBestIndex sho
43960 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 uld ignore */.
43970 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 } *aConstraint;
43980 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 /* Ta
43990 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 ble of WHERE cla
439a0 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 use constraints
439b0 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 */. int nOrderB
439c0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
439d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 /* Number of ter
439e0 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 ms in the ORDER
439f0 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 BY clause */. s
43a00 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
43a10 64 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 dex_orderby {.
43a20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 int iColumn;
43a30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
43a40 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f Column number */
43a50 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 . unsigned c
43a60 68 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 har desc;
43a70 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 /* True for DESC
43a80 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 . False for ASC
43a90 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 . */. } *aOrder
43aa0 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 By;
43ab0 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 /* The ORDER B
43ac0 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a Y clause */. /*
43ad0 20 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 Outputs */. st
43ae0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
43af0 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 ex_constraint_us
43b00 61 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 age {. int ar
43b10 67 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 gvIndex;
43b20 20 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e /* if >0, con
43b30 73 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 straint is part
43b40 6f 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 of argv to xFilt
43b50 65 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e er */. unsign
43b60 65 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 ed char omit;
43b70 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 /* Do not cod
43b80 65 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 e a test for thi
43b90 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a s constraint */.
43ba0 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 } *aConstraint
43bb0 55 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 Usage;. int idx
43bc0 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Num;
43bd0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 /* Number us
43be0 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 ed to identify t
43bf0 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 he index */. ch
43c00 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 ar *idxStr;
43c10 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 /* Stri
43c20 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 ng, possibly obt
43c30 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
43c40 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 e3_malloc */. i
43c50 6e 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 nt needToFreeIdx
43c60 53 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 Str; /* Fre
43c70 65 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 e idxStr using s
43c80 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 qlite3_free() if
43c90 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f true */. int o
43ca0 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 rderByConsumed;
43cb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
43cc0 20 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 output is alrea
43cd0 64 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 dy ordered */.
43ce0 64 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 double estimated
43cf0 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 Cost; /* Es
43d00 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 timated cost of
43d10 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 using this index
43d20 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 */.};.#define S
43d30 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 QLITE_INDEX_CONS
43d40 54 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 TRAINT_EQ 2.#
43d50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
43d60 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 DEX_CONSTRAINT_G
43d70 54 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 T 4.#define S
43d80 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 QLITE_INDEX_CONS
43d90 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 TRAINT_LE 8.#
43da0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
43db0 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c DEX_CONSTRAINT_L
43dc0 54 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 T 16.#define
43dd0 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e SQLITE_INDEX_CON
43de0 53 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 STRAINT_GE 32
43df0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
43e00 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 INDEX_CONSTRAINT
43e10 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a _MATCH 64../*.**
43e20 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 CAPI3REF: Regis
43e30 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 ter A Virtual Ta
43e40 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 ble Implementati
43e50 6f 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 on {H18200} <S20
43e60 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 400>.** EXPERIME
43e70 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 NTAL.**.** This
43e80 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
43e90 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 to register a ne
43ea0 77 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 77 69 w module name wi
43eb0 74 68 20 61 0a 2a 2a 20 5b 64 61 74 61 62 61 73 th a.** [databas
43ec0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 e connection].
43ed0 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 Module names mus
43ee0 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 t be registered
43ef0 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69 before.** creati
43f00 6e 67 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 ng new virtual t
43f10 61 62 6c 65 73 20 6f 6e 20 74 68 65 20 6d 6f 64 ables on the mod
43f20 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65 20 75 ule, or before u
43f30 73 69 6e 67 0a 2a 2a 20 70 72 65 65 78 69 73 74 sing.** preexist
43f40 69 6e 67 20 76 69 72 74 75 61 6c 20 74 61 62 6c ing virtual tabl
43f50 65 73 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 es of the module
43f60 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 ..**.** This int
43f70 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69 erface is experi
43f80 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75 mental and is su
43f90 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
43fa0 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e or.** removal in
43fb0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
43fc0 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 of SQLite..*/.S
43fd0 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
43fe0 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e _EXPERIMENTAL in
43ff0 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
44000 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 _module(. sqlit
44010 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
44020 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 /* SQLite
44030 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 connection to re
44040 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 gister module wi
44050 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 th */. const ch
44060 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 ar *zName,
44070 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
44080 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f e module */. co
44090 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
440a0 6c 65 20 2a 2c 20 20 20 20 2f 2a 20 4d 65 74 68 le *, /* Meth
440b0 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 ods for the modu
440c0 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 20 20 le */. void *
440d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
440e0 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 /* Client dat
440f0 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 a for xCreate/xC
44100 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a onnect */.);../*
44110 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
44120 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c gister A Virtual
44130 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 Table Implement
44140 61 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c ation {H18210} <
44150 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S20400>.** EXPER
44160 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
44170 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 is routine is id
44180 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b entical to the [
44190 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
441a0 6f 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 20 odule()] method
441b0 61 62 6f 76 65 2c 0a 2a 2a 20 65 78 63 65 70 74 above,.** except
441c0 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 77 73 20 that it allows
441d0 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e a destructor fun
441e0 63 74 69 6f 6e 20 74 6f 20 62 65 20 73 70 65 63 ction to be spec
441f0 69 66 69 65 64 2e 20 49 74 20 69 73 0a 2a 2a 20 ified. It is.**
44200 65 76 65 6e 20 6d 6f 72 65 20 65 78 70 65 72 69 even more experi
44210 6d 65 6e 74 61 6c 20 74 68 61 6e 20 74 68 65 20 mental than the
44220 72 65 73 74 20 6f 66 20 74 68 65 20 76 69 72 74 rest of the virt
44230 75 61 6c 20 74 61 62 6c 65 73 20 41 50 49 2e 0a ual tables API..
44240 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
44250 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
44260 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 L int sqlite3_cr
44270 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a eate_module_v2(.
44280 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
44290 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
442a0 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f SQLite connectio
442b0 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d 6f n to register mo
442c0 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 dule with */. c
442d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
442e0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d , /* Nam
442f0 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 e of the module
44300 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 */. const sqlit
44310 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20 20 20 20 e3_module *,
44320 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 /* Methods for t
44330 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 he module */. v
44340 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 20 oid *,
44350 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 /* Cli
44360 65 6e 74 20 64 61 74 61 20 66 6f 72 20 78 43 72 ent data for xCr
44370 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f eate/xConnect */
44380 0a 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f . void(*xDestro
44390 79 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a y)(void*) /*
443a0 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 Module destruct
443b0 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 or function */.)
443c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
443d0 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 F: Virtual Table
443e0 20 49 6e 73 74 61 6e 63 65 20 4f 62 6a 65 63 74 Instance Object
443f0 20 7b 48 31 38 30 31 30 7d 20 3c 53 32 30 34 30 {H18010} <S2040
44400 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
44410 73 71 6c 69 74 65 33 5f 76 74 61 62 0a 2a 2a 20 sqlite3_vtab.**
44420 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
44430 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75 6c 65 20 ** Every module
44440 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 implementation u
44450 73 65 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f ses a subclass o
44460 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
44470 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 6f 20 structure.** to
44480 64 65 73 63 72 69 62 65 20 61 20 70 61 72 74 69 describe a parti
44490 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 65 20 6f cular instance o
444a0 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 f the module. E
444b0 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 6c ach subclass wil
444c0 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 64 l.** be tailored
444d0 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63 to the specific
444e0 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d 6f needs of the mo
444f0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 dule implementat
44500 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72 70 ion..** The purp
44510 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 70 65 ose of this supe
44520 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65 66 rclass is to def
44530 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65 6c ine certain fiel
44540 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63 ds that are.** c
44550 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f 64 ommon to all mod
44560 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ule implementati
44570 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 ons..**.** Virtu
44580 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f 64 al tables method
44590 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72 72 s can set an err
445a0 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 61 73 or message by as
445b0 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74 72 signing a.** str
445c0 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ing obtained fro
445d0 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e m [sqlite3_mprin
445e0 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73 67 tf()] to zErrMsg
445f0 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73 68 . The method sh
44600 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61 72 ould.** take car
44610 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f 72 e that any prior
44620 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 64 string is freed
44630 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 by a call to [s
44640 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a 2a qlite3_free()].*
44650 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 67 * prior to assig
44660 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 6e ning a new strin
44670 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 41 g to zErrMsg. A
44680 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d fter the error m
44690 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 6c essage.** is del
446a0 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 65 ivered up to the
446b0 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 74 client applicat
446c0 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 20 ion, the string
446d0 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 will be automati
446e0 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 62 cally.** freed b
446f0 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 y sqlite3_free()
44700 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 67 and the zErrMsg
44710 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 7a field will be z
44720 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a 2a 2a 20 eroed. Note.**
44730 74 68 61 74 20 73 71 6c 69 74 65 33 5f 6d 70 72 that sqlite3_mpr
44740 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74 intf() and sqlit
44750 65 33 5f 66 72 65 65 28 29 20 61 72 65 20 75 73 e3_free() are us
44760 65 64 20 6f 6e 20 74 68 65 20 7a 45 72 72 4d 73 ed on the zErrMs
44770 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69 6e 63 65 g field.** since
44780 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 virtual tables
44790 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 69 6d 70 are commonly imp
447a0 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c 6f 61 64 lemented in load
447b0 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 able extensions
447c0 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e 6f 74 20 which.** do not
447d0 68 61 76 65 20 61 63 63 65 73 73 20 74 6f 20 73 have access to s
447e0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 20 qlite3MPrintf()
447f0 6f 72 20 73 71 6c 69 74 65 33 46 72 65 65 28 29 or sqlite3Free()
44800 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 ..**.** This int
44810 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69 erface is experi
44820 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75 mental and is su
44830 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
44840 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e or.** removal in
44850 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
44860 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 of SQLite..*/.s
44870 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 truct sqlite3_vt
44880 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c ab {. const sql
44890 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
448a0 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f dule; /* The mo
448b0 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 dule for this vi
448c0 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 rtual table */.
448d0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
448e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
448f0 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61 /* Used interna
44900 6c 6c 79 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a lly */. char *z
44910 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 ErrMsg;
44920 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f /* Erro
44930 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 r message from s
44940 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 qlite3_mprintf()
44950 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c */. /* Virtual
44960 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 table implement
44970 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 ations will typi
44980 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 cally add additi
44990 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d onal fields */.}
449a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
449b0 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 F: Virtual Table
449c0 20 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 Cursor Object
449d0 7b 48 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 {H18020} <S20400
449e0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 >.** KEYWORDS: s
449f0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
44a00 6f 72 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 or.** EXPERIMENT
44a10 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 6d AL.**.** Every m
44a20 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 odule implementa
44a30 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 tion uses a subc
44a40 6c 61 73 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c lass of the foll
44a50 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a owing structure.
44a60 2a 2a 20 74 6f 20 64 65 73 63 72 69 62 65 20 63 ** to describe c
44a70 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69 6e ursors that poin
44a80 74 20 69 6e 74 6f 20 74 68 65 20 76 69 72 74 75 t into the virtu
44a90 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 61 72 65 al table and are
44aa0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 6f 70 used.** to loop
44ab0 20 74 68 72 6f 75 67 68 20 74 68 65 20 76 69 72 through the vir
44ac0 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 43 75 72 tual table. Cur
44ad0 73 6f 72 73 20 61 72 65 20 63 72 65 61 74 65 64 sors are created
44ae0 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 78 4f using the.** xO
44af0 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 pen method of th
44b00 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 e module. Each
44b10 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
44b20 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e ation will defin
44b30 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 e.** the content
44b40 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 72 of a cursor str
44b50 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 69 ucture to suit i
44b60 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a ts own needs..**
44b70 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 6c .** This supercl
44b80 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f 72 ass exists in or
44b90 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 69 der to define fi
44ba0 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73 elds of the curs
44bb0 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 or that.** are c
44bc0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 ommon to all imp
44bd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a lementations..**
44be0 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
44bf0 63 65 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 ce is experiment
44c00 61 6c 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 al and is subjec
44c10 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a t to change or.*
44c20 2a 20 72 65 6d 6f 76 61 6c 20 69 6e 20 66 75 74 * removal in fut
44c30 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 ure releases of
44c40 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 SQLite..*/.struc
44c50 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 t sqlite3_vtab_c
44c60 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 ursor {. sqlite
44c70 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 3_vtab *pVtab;
44c80 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 /* Virtual t
44c90 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72 able of this cur
44ca0 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 sor */. /* Virt
44cb0 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d ual table implem
44cc0 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 entations will t
44cd0 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 ypically add add
44ce0 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a itional fields *
44cf0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.};../*.** CAPI
44d00 33 52 45 46 3a 20 44 65 63 6c 61 72 65 20 54 68 3REF: Declare Th
44d10 65 20 53 63 68 65 6d 61 20 4f 66 20 41 20 56 69 e Schema Of A Vi
44d20 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 rtual Table {H18
44d30 32 38 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 280} <S20400>.**
44d40 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
44d50 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74 65 20 .** The xCreate
44d60 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 and xConnect met
44d70 68 6f 64 73 20 6f 66 20 61 20 6d 6f 64 75 6c 65 hods of a module
44d80 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 use the followi
44d90 6e 67 20 41 50 49 0a 2a 2a 20 74 6f 20 64 65 63 ng API.** to dec
44da0 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61 74 20 lare the format
44db0 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 64 (the names and d
44dc0 61 74 61 74 79 70 65 73 20 6f 66 20 74 68 65 20 atatypes of the
44dd0 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 columns) of.** t
44de0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
44df0 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65 6e 74 s they implement
44e00 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 ..**.** This int
44e10 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69 erface is experi
44e20 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75 mental and is su
44e30 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
44e40 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e or.** removal in
44e50 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
44e60 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 of SQLite..*/.S
44e70 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
44e80 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e _EXPERIMENTAL in
44e90 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 t sqlite3_declar
44ea0 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c e_vtab(sqlite3*,
44eb0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 72 const char *zCr
44ec0 65 61 74 65 54 61 62 6c 65 29 3b 0a 0a 2f 2a 0a eateTable);../*.
44ed0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 76 65 ** CAPI3REF: Ove
44ee0 72 6c 6f 61 64 20 41 20 46 75 6e 63 74 69 6f 6e rload A Function
44ef0 20 46 6f 72 20 41 20 56 69 72 74 75 61 6c 20 54 For A Virtual T
44f00 61 62 6c 65 20 7b 48 31 38 33 30 30 7d 20 3c 53 able {H18300} <S
44f10 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 20400>.** EXPERI
44f20 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 56 69 72 MENTAL.**.** Vir
44f30 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 20 tual tables can
44f40 70 72 6f 76 69 64 65 20 61 6c 74 65 72 6e 61 74 provide alternat
44f50 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ive implementati
44f60 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 ons of functions
44f70 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 78 46 .** using the xF
44f80 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 indFunction meth
44f90 6f 64 2e 20 20 42 75 74 20 67 6c 6f 62 61 6c 20 od. But global
44fa0 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 versions of thos
44fb0 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d e functions.** m
44fc0 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 ust exist in ord
44fd0 65 72 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 er to be overloa
44fe0 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ded..**.** This
44ff0 41 50 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 API makes sure a
45000 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 global version
45010 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 of a function wi
45020 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a th a particular.
45030 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 ** name and numb
45040 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 er of parameters
45050 20 65 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 exists. If no
45060 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 such function ex
45070 69 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 ists.** before t
45080 68 69 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 his API is calle
45090 64 2c 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f d, a new functio
450a0 6e 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 n is created. T
450b0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
450c0 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 n.** of the new
450d0 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 function always
450e0 63 61 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 causes an except
450f0 69 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e ion to be thrown
45100 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 . So.** the new
45110 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 function is not
45120 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 good for anythi
45130 6e 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 ng by itself. I
45140 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f ts only.** purpo
45150 73 65 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c se is to be a pl
45160 61 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 aceholder functi
45170 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f on that can be o
45180 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 verloaded.** by
45190 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a virtual tables..
451a0 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 73 **.** This API s
451b0 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64 65 hould be conside
451c0 72 65 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 red part of the
451d0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e virtual table in
451e0 74 65 72 66 61 63 65 2c 0a 2a 2a 20 77 68 69 63 terface,.** whic
451f0 68 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 h is experimenta
45200 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f l and subject to
45210 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 53 51 4c 49 change..*/.SQLI
45220 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
45230 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 PERIMENTAL int s
45240 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f qlite3_overload_
45250 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 function(sqlite3
45260 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
45270 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 FuncName, int nA
45280 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 rg);../*.** The
45290 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 interface to the
452a0 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d virtual-table m
452b0 65 63 68 61 6e 69 73 6d 20 64 65 66 69 6e 65 64 echanism defined
452c0 20 61 62 6f 76 65 20 28 62 61 63 6b 20 75 70 0a above (back up.
452d0 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d 65 6e 74 20 ** to a comment
452e0 72 65 6d 61 72 6b 61 62 6c 79 20 73 69 6d 69 6c remarkably simil
452f0 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20 ar to this one)
45300 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e is currently con
45310 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 sidered.** to be
45320 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 experimental.
45330 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69 The interface mi
45340 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e ght change in in
45350 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e compatible ways.
45360 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 .** If this is a
45370 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 problem for you
45380 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 , do not use the
45390 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 68 interface at th
453a0 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 is time..**.** W
453b0 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d hen the virtual-
453c0 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 table mechanism
453d0 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 77 stabilizes, we w
453e0 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a ill declare the.
453f0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69 78 ** interface fix
45400 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69 ed, support it i
45410 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 ndefinitely, and
45420 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d remove this com
45430 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 20 ment..**.******
45440 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 EXPERIMENTAL - s
45450 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 ubject to change
45460 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 without notice
45470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
45480 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 /../*.** CAPI3RE
45490 46 3a 20 41 20 48 61 6e 64 6c 65 20 54 6f 20 41 F: A Handle To A
454a0 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 n Open BLOB {H17
454b0 38 30 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 800} <S30230>.**
454c0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 42 4c 4f 42 KEYWORDS: {BLOB
454d0 20 68 61 6e 64 6c 65 7d 20 7b 42 4c 4f 42 20 68 handle} {BLOB h
454e0 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e andles}.**.** An
454f0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
45500 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 s object represe
45510 6e 74 73 20 61 6e 20 6f 70 65 6e 20 42 4c 4f 42 nts an open BLOB
45520 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 5b 73 71 on which.** [sq
45530 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 lite3_blob_open
45540 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c | incremental BL
45550 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 62 65 20 70 OB I/O] can be p
45560 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f 62 6a erformed..** Obj
45570 65 63 74 73 20 6f 66 20 74 68 69 73 20 74 79 70 ects of this typ
45580 65 20 61 72 65 20 63 72 65 61 74 65 64 20 62 79 e are created by
45590 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
455a0 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 64 65 pen()].** and de
455b0 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 stroyed by [sqli
455c0 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 te3_blob_close()
455d0 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 ]..** The [sqlit
455e0 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 e3_blob_read()]
455f0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f and [sqlite3_blo
45600 62 5f 77 72 69 74 65 28 29 5d 20 69 6e 74 65 72 b_write()] inter
45610 66 61 63 65 73 0a 2a 2a 20 63 61 6e 20 62 65 20 faces.** can be
45620 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 used to read or
45630 77 72 69 74 65 20 73 6d 61 6c 6c 20 73 75 62 73 write small subs
45640 65 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 42 ections of the B
45650 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c LOB..** The [sql
45660 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 ite3_blob_bytes(
45670 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 )] interface ret
45680 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 6f 66 urns the size of
45690 20 74 68 65 20 42 4c 4f 42 20 69 6e 20 62 79 74 the BLOB in byt
456a0 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 es..*/.typedef s
456b0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 6c truct sqlite3_bl
456c0 6f 62 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 3b ob sqlite3_blob;
456d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
456e0 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46 6f : Open A BLOB Fo
456f0 72 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f r Incremental I/
45700 4f 20 7b 48 31 37 38 31 30 7d 20 3c 53 33 30 32 O {H17810} <S302
45710 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 30>.**.** This i
45720 6e 74 65 72 66 61 63 65 73 20 6f 70 65 6e 73 20 nterfaces opens
45730 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 20 7c a [BLOB handle |
45740 20 68 61 6e 64 6c 65 5d 20 74 6f 20 74 68 65 20 handle] to the
45750 42 4c 4f 42 20 6c 6f 63 61 74 65 64 0a 2a 2a 20 BLOB located.**
45760 69 6e 20 72 6f 77 20 69 52 6f 77 2c 20 63 6f 6c in row iRow, col
45770 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 61 62 umn zColumn, tab
45780 6c 65 20 7a 54 61 62 6c 65 20 69 6e 20 64 61 74 le zTable in dat
45790 61 62 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 69 6e abase zDb;.** in
457a0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 other words, th
457b0 65 20 73 61 6d 65 20 42 4c 4f 42 20 74 68 61 74 e same BLOB that
457c0 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74 would be select
457d0 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 ed by:.**.** <pr
457e0 65 3e 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 e>.** SELECT
457f0 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a 44 zColumn FROM zD
45800 62 2e 7a 54 61 62 6c 65 20 57 48 45 52 45 20 72 b.zTable WHERE r
45810 6f 77 69 64 20 3d 20 69 52 6f 77 3b 0a 2a 2a 20 owid = iRow;.**
45820 3c 2f 70 72 65 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a </pre> {END}.**.
45830 2a 2a 20 49 66 20 74 68 65 20 66 6c 61 67 73 20 ** If the flags
45840 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e parameter is non
45850 2d 7a 65 72 6f 2c 20 74 68 65 20 74 68 65 20 42 -zero, the the B
45860 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f LOB is opened fo
45870 72 20 72 65 61 64 0a 2a 2a 20 61 6e 64 20 77 72 r read.** and wr
45880 69 74 65 20 61 63 63 65 73 73 2e 20 49 66 20 69 ite access. If i
45890 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 42 t is zero, the B
458a0 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f LOB is opened fo
458b0 72 20 72 65 61 64 20 61 63 63 65 73 73 2e 0a 2a r read access..*
458c0 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 *.** Note that t
458d0 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 he database name
458e0 20 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6c 65 is not the file
458f0 6e 61 6d 65 20 74 68 61 74 20 63 6f 6e 74 61 69 name that contai
45900 6e 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ns.** the databa
45910 73 65 20 62 75 74 20 72 61 74 68 65 72 20 74 68 se but rather th
45920 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 e symbolic name
45930 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
45940 74 68 61 74 0a 2a 2a 20 69 73 20 61 73 73 69 67 that.** is assig
45950 6e 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 ned when the dat
45960 61 62 61 73 65 20 69 73 20 63 6f 6e 6e 65 63 74 abase is connect
45970 65 64 20 75 73 69 6e 67 20 5b 41 54 54 41 43 48 ed using [ATTACH
45980 5d 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6d 61 ]..** For the ma
45990 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
459a0 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e , the database n
459b0 61 6d 65 20 69 73 20 22 6d 61 69 6e 22 2e 0a 2a ame is "main"..*
459c0 2a 20 46 6f 72 20 54 45 4d 50 20 74 61 62 6c 65 * For TEMP table
459d0 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 s, the database
459e0 6e 61 6d 65 20 69 73 20 22 74 65 6d 70 22 2e 0a name is "temp"..
459f0 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 **.** On success
45a00 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 , [SQLITE_OK] is
45a10 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 returned and th
45a20 65 20 6e 65 77 20 5b 42 4c 4f 42 20 68 61 6e 64 e new [BLOB hand
45a30 6c 65 5d 20 69 73 20 77 72 69 74 74 65 6e 0a 2a le] is written.*
45a40 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 2e 20 4f 74 * to *ppBlob. Ot
45a50 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f herwise an [erro
45a60 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 r code] is retur
45a70 6e 65 64 20 61 6e 64 20 61 6e 79 20 76 61 6c 75 ned and any valu
45a80 65 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 e written.** to
45a90 2a 70 70 42 6c 6f 62 20 73 68 6f 75 6c 64 20 6e *ppBlob should n
45aa0 6f 74 20 62 65 20 75 73 65 64 20 62 79 20 74 68 ot be used by th
45ab0 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 54 68 69 e caller..** Thi
45ac0 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 s function sets
45ad0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
45ae0 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 nnection] error
45af0 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 code and message
45b00 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 .** accessible v
45b10 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 ia [sqlite3_errc
45b20 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 ode()] and [sqli
45b30 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a te3_errmsg()]..*
45b40 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 77 20 *.** If the row
45b50 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 6e 64 that a BLOB hand
45b60 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 le points to is
45b70 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a modified by an.*
45b80 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45 4c * [UPDATE], [DEL
45b90 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e 20 ETE], or by [ON
45ba0 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d 65 CONFLICT] side-e
45bb0 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20 74 ffects.** then t
45bc0 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69 he BLOB handle i
45bd0 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70 s marked as "exp
45be0 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20 69 ired"..** This i
45bf0 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 63 6f s true if any co
45c00 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77 20 lumn of the row
45c10 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 65 6e is changed, even
45c20 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74 68 a column.** oth
45c30 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 er than the one
45c40 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 the BLOB handle
45c50 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20 43 is open on..** C
45c60 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
45c70 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e _blob_read()] an
45c80 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f d [sqlite3_blob_
45c90 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a 20 write()] for.**
45ca0 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42 20 68 a expired BLOB h
45cb0 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74 68 20 andle fail with
45cc0 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f an return code o
45cd0 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d f [SQLITE_ABORT]
45ce0 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77 72 69 ..** Changes wri
45cf0 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f 42 tten into a BLOB
45d00 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 42 4c prior to the BL
45d10 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72 65 20 OB expiring are
45d20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 not.** rollback
45d30 62 79 20 74 68 65 20 65 78 70 69 72 61 74 69 6f by the expiratio
45d40 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20 20 n of the BLOB.
45d50 53 75 63 68 20 63 68 61 6e 67 65 73 20 77 69 6c Such changes wil
45d60 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 l eventually.**
45d70 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20 74 72 commit if the tr
45d80 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69 6e ansaction contin
45d90 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f ues to completio
45da0 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 n..**.** INVARIA
45db0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 NTS:.**.** {H178
45dc0 31 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 13} A successful
45dd0 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 invocation of t
45de0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
45df0 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c 43 2c 52 2c _open(D,B,T,C,R,
45e00 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 F,P)].**
45e10 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c interface shal
45e20 6c 20 6f 70 65 6e 20 61 6e 20 5b 73 71 6c 69 74 l open an [sqlit
45e30 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 20 e3_blob] object
45e40 50 20 6f 6e 20 74 68 65 20 42 4c 4f 42 0a 2a 2a P on the BLOB.**
45e50 20 20 20 20 20 20 20 20 20 20 69 6e 20 63 6f 6c in col
45e60 75 6d 6e 20 43 20 6f 66 20 74 68 65 20 74 61 62 umn C of the tab
45e70 6c 65 20 54 20 69 6e 20 74 68 65 20 64 61 74 61 le T in the data
45e80 62 61 73 65 20 42 20 6f 6e 0a 2a 2a 20 20 20 20 base B on.**
45e90 20 20 20 20 20 20 74 68 65 20 5b 64 61 74 61 62 the [datab
45ea0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
45eb0 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 34 D..**.** {H17814
45ec0 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 69 } A successful i
45ed0 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 nvocation of [sq
45ee0 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 lite3_blob_open(
45ef0 44 2c 2e 2e 2e 29 5d 20 73 68 61 6c 6c 20 73 74 D,...)] shall st
45f00 61 72 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 art.**
45f10 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f a new transactio
45f20 6e 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 n on the [databa
45f30 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 se connection] D
45f40 20 69 66 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 if that.**
45f50 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 connection
45f60 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 is not already i
45f70 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e n a transaction.
45f80 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 36 7d 20 .**.** {H17816}
45f90 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f The [sqlite3_blo
45fa0 62 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c 43 2c 52 b_open(D,B,T,C,R
45fb0 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 ,F,P)] interface
45fc0 20 73 68 61 6c 6c 20 6f 70 65 6e 0a 2a 2a 20 20 shall open.**
45fd0 20 20 20 20 20 20 20 20 74 68 65 20 42 4c 4f 42 the BLOB
45fe0 20 66 6f 72 20 72 65 61 64 20 61 6e 64 20 77 72 for read and wr
45ff0 69 74 65 20 61 63 63 65 73 73 20 69 66 20 61 6e ite access if an
46000 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 46 0a d only if the F.
46010 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61 ** para
46020 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 meter is non-zer
46030 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 39 o..**.** {H17819
46040 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 } The [sqlite3_b
46050 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 69 6e 74 65 lob_open()] inte
46060 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 75 rface shall retu
46070 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f rn [SQLITE_OK] o
46080 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 75 n.** su
46090 63 63 65 73 73 20 61 6e 64 20 61 6e 20 61 70 70 ccess and an app
460a0 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20 ropriate [error
460b0 63 6f 64 65 5d 20 6f 6e 20 66 61 69 6c 75 72 65 code] on failure
460c0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 32 31 7d ..**.** {H17821}
460d0 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
460e0 75 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75 urs during evalu
460f0 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 ation of [sqlite
46100 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 2e 2e 3_blob_open(D,..
46110 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .)].**
46120 74 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 then subsequent
46130 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
46140 33 5f 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a 2a 3_errcode(D)],.*
46150 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
46160 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 te3_extended_err
46170 63 6f 64 65 28 29 5d 2c 20 0a 2a 2a 20 20 20 20 code()], .**
46180 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 [sqlite3_e
46190 72 72 6d 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b rrmsg(D)], and [
461a0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 sqlite3_errmsg16
461b0 28 44 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 (D)] shall retur
461c0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e n.** in
461d0 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 formation approp
461e0 72 69 61 74 65 20 66 6f 72 20 74 68 61 74 20 65 riate for that e
461f0 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 rror..**.** {H17
46200 38 32 34 7d 20 49 66 20 61 6e 79 20 63 6f 6c 75 824} If any colu
46210 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 20 74 68 mn in the row th
46220 61 74 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 6c at a [sqlite3_bl
46230 6f 62 5d 20 68 61 73 20 6f 70 65 6e 20 69 73 0a ob] has open is.
46240 2a 2a 20 20 20 20 20 20 20 20 20 20 63 68 61 6e ** chan
46250 67 65 64 20 62 79 20 61 20 73 65 70 61 72 61 74 ged by a separat
46260 65 20 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b 44 e [UPDATE] or [D
46270 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 ELETE] statement
46280 20 6f 72 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 or by.**
46290 20 20 20 61 6e 20 5b 4f 4e 20 43 4f 4e 46 4c 49 an [ON CONFLI
462a0 43 54 5d 20 73 69 64 65 20 65 66 66 65 63 74 2c CT] side effect,
462b0 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74 then the [sqlit
462c0 65 33 5f 62 6c 6f 62 5d 20 73 68 61 6c 6c 0a 2a e3_blob] shall.*
462d0 2a 20 20 20 20 20 20 20 20 20 20 62 65 20 6d 61 * be ma
462e0 72 6b 65 64 20 61 73 20 69 6e 76 61 6c 69 64 2e rked as invalid.
462f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
46300 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
46310 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a open(. sqlite3*
46320 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
46330 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 zDb,. const cha
46340 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20 63 6f 6e r *zTable,. con
46350 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e st char *zColumn
46360 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ,. sqlite3_int6
46370 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74 20 66 6c 4 iRow,. int fl
46380 61 67 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 ags,. sqlite3_b
46390 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a 29 3b 0a lob **ppBlob.);.
463a0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
463b0 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42 20 48 61 Close A BLOB Ha
463c0 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d 20 3c 53 ndle {H17830} <S
463d0 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 30230>.**.** Clo
463e0 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f ses an open [BLO
463f0 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a 0a 2a 2a B handle]..**.**
46400 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c 4f 42 20 Closing a BLOB
46410 73 68 61 6c 6c 20 63 61 75 73 65 20 74 68 65 20 shall cause the
46420 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
46430 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a ion to commit.**
46440 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f if there are no
46450 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c 20 6e 6f other BLOBs, no
46460 20 70 65 6e 64 69 6e 67 20 70 72 65 70 61 72 65 pending prepare
46470 64 20 73 74 61 74 65 6d 65 6e 74 73 2c 20 61 6e d statements, an
46480 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 d the.** databas
46490 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
464a0 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d in [autocommit m
464b0 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 ode]..** If any
464c0 77 72 69 74 65 73 20 77 65 72 65 20 6d 61 64 65 writes were made
464d0 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c 20 74 68 to the BLOB, th
464e0 65 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 ey might be held
464f0 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 75 6e 74 in cache.** unt
46500 69 6c 20 74 68 65 20 63 6c 6f 73 65 20 6f 70 65 il the close ope
46510 72 61 74 69 6f 6e 20 69 66 20 74 68 65 79 20 77 ration if they w
46520 69 6c 6c 20 66 69 74 2e 20 7b 45 4e 44 7d 0a 2a ill fit. {END}.*
46530 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68 65 *.** Closing the
46540 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72 63 BLOB often forc
46550 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a 2a es the changes.*
46560 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e * out to disk an
46570 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f 20 d so if any I/O
46580 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 errors occur, th
46590 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 6f ey will likely o
465a0 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 74 ccur.** at the t
465b0 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c 4f ime when the BLO
465c0 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 7b 48 B is closed. {H
465d0 31 37 38 33 33 7d 20 41 6e 79 20 65 72 72 6f 72 17833} Any error
465e0 73 20 74 68 61 74 20 6f 63 63 75 72 20 64 75 72 s that occur dur
465f0 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 61 ing.** closing a
46600 72 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 61 re reported as a
46610 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e non-zero return
46620 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 value..**.** Th
46630 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 e BLOB is closed
46640 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 unconditionally
46650 2e 20 20 45 76 65 6e 20 69 66 20 74 68 69 73 20 . Even if this
46660 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a routine returns.
46670 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 ** an error code
46680 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 73 74 , the BLOB is st
46690 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a ill closed..**.*
466a0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
466b0 0a 2a 2a 20 7b 48 31 37 38 33 33 7d 20 54 68 65 .** {H17833} The
466c0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 [sqlite3_blob_c
466d0 6c 6f 73 65 28 50 29 5d 20 69 6e 74 65 72 66 61 lose(P)] interfa
466e0 63 65 20 63 6c 6f 73 65 73 20 61 6e 20 5b 73 71 ce closes an [sq
466f0 6c 69 74 65 33 5f 62 6c 6f 62 5d 0a 2a 2a 20 20 lite3_blob].**
46700 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 50 object P
46710 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e previously open
46720 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ed using [sqlite
46730 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 2e 0a 3_blob_open()]..
46740 2a 2a 0a 2a 2a 20 7b 48 31 37 38 33 36 7d 20 43 **.** {H17836} C
46750 6c 6f 73 69 6e 67 20 61 6e 20 5b 73 71 6c 69 74 losing an [sqlit
46760 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 20 e3_blob] object
46770 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 using.**
46780 20 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f [sqlite3_blob_
46790 63 6c 6f 73 65 28 29 5d 20 73 68 61 6c 6c 20 63 close()] shall c
467a0 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 ause the current
467b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 0a transaction to.
467c0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6d 6d ** comm
467d0 69 74 20 69 66 20 74 68 65 72 65 20 61 72 65 20 it if there are
467e0 6e 6f 20 6f 74 68 65 72 20 6f 70 65 6e 20 5b 73 no other open [s
467f0 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a qlite3_blob] obj
46800 65 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ects.**
46810 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 or [prepared st
46820 61 74 65 6d 65 6e 74 73 5d 20 6f 6e 20 74 68 65 atements] on the
46830 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 same [database
46840 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 0a connection] and.
46850 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
46860 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
46870 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 ion is in [autoc
46880 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 0a ommit mode]..**.
46890 2a 2a 20 7b 48 31 37 38 33 39 7d 20 54 68 65 20 ** {H17839} The
468a0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c [sqlite3_blob_cl
468b0 6f 73 65 28 50 29 5d 20 69 6e 74 65 72 66 61 63 ose(P)] interfac
468c0 65 73 20 73 68 61 6c 6c 20 63 6c 6f 73 65 20 74 es shall close t
468d0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
468e0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 sqlite3_blob] ob
468f0 6a 65 63 74 20 50 20 75 6e 63 6f 6e 64 69 74 69 ject P unconditi
46900 6f 6e 61 6c 6c 79 2c 20 65 76 65 6e 20 69 66 0a onally, even if.
46910 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
46920 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 ite3_blob_close(
46930 50 29 5d 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 P)] returns some
46940 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e thing other than
46950 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2f [SQLITE_OK]..*/
46960 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
46970 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
46980 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 se(sqlite3_blob
46990 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
469a0 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20 REF: Return The
469b0 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e 20 Size Of An Open
469c0 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d 20 3c 53 BLOB {H17840} <S
469d0 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 30230>.**.** Ret
469e0 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e urns the size in
469f0 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c bytes of the BL
46a00 4f 42 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 OB accessible vi
46a10 61 20 74 68 65 20 6f 70 65 6e 0a 2a 2a 20 5b 5d a the open.** []
46a20 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 20 BLOB handle] in
46a30 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e its only argumen
46a40 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 t..**.** INVARIA
46a50 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 NTS:.**.** {H178
46a60 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 43} The [sqlite3
46a70 5f 62 6c 6f 62 5f 62 79 74 65 73 28 50 29 5d 20 _blob_bytes(P)]
46a80 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
46a90 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 20 20 s the size.**
46aa0 20 20 20 20 20 20 20 69 6e 20 62 79 74 65 73 20 in bytes
46ab0 6f 66 20 74 68 65 20 42 4c 4f 42 20 74 68 61 74 of the BLOB that
46ac0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c the [sqlite3_bl
46ad0 6f 62 5d 20 6f 62 6a 65 63 74 20 50 0a 2a 2a 20 ob] object P.**
46ae0 20 20 20 20 20 20 20 20 20 72 65 66 65 72 73 20 refers
46af0 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 to..*/.SQLITE_AP
46b00 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
46b10 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 ob_bytes(sqlite3
46b20 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _blob *);../*.**
46b30 20 43 41 50 49 33 52 45 46 3a 20 52 65 61 64 20 CAPI3REF: Read
46b40 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f 42 Data From A BLOB
46b50 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b Incrementally {
46b60 48 31 37 38 35 30 7d 20 3c 53 33 30 32 33 30 3e H17850} <S30230>
46b70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
46b80 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
46b90 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 read data from a
46ba0 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e n open [BLOB han
46bb0 64 6c 65 5d 20 69 6e 74 6f 20 61 0a 2a 2a 20 63 dle] into a.** c
46bc0 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62 aller-supplied b
46bd0 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f uffer. N bytes o
46be0 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 f data are copie
46bf0 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20 5a 0a d into buffer Z.
46c00 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 6e ** from the open
46c10 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 BLOB, starting
46c20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 at offset iOffse
46c30 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 66 66 73 t..**.** If offs
46c40 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65 et iOffset is le
46c50 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 ss than N bytes
46c60 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 from the end of
46c70 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 the BLOB,.** [SQ
46c80 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 LITE_ERROR] is r
46c90 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 eturned and no d
46ca0 61 74 61 20 69 73 20 72 65 61 64 2e 20 20 49 66 ata is read. If
46cb0 20 4e 20 6f 72 20 69 4f 66 66 73 65 74 20 69 73 N or iOffset is
46cc0 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 .** less than ze
46cd0 72 6f 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f ro, [SQLITE_ERRO
46ce0 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 R] is returned a
46cf0 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65 nd no data is re
46d00 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 ad..**.** An att
46d10 65 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f empt to read fro
46d20 6d 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c m an expired [BL
46d30 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 OB handle] fails
46d40 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f with an.** erro
46d50 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 r code of [SQLIT
46d60 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 E_ABORT]..**.**
46d70 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 On success, SQLI
46d80 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
46d90 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c d..** Otherwise,
46da0 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d an [error code]
46db0 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 or an [extended
46dc0 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 error code] is
46dd0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
46de0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
46df0 2a 20 7b 48 31 37 38 35 33 7d 20 41 20 73 75 63 * {H17853} A suc
46e00 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 cessful invocati
46e10 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 62 on of [sqlite3_b
46e20 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 lob_read(P,Z,N,X
46e30 29 5d 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 )] .**
46e40 73 68 61 6c 6c 20 72 65 61 64 73 20 4e 20 62 79 shall reads N by
46e50 74 65 73 20 6f 66 20 64 61 74 61 20 6f 75 74 20 tes of data out
46e60 6f 66 20 74 68 65 20 42 4c 4f 42 20 72 65 66 65 of the BLOB refe
46e70 72 65 6e 63 65 64 20 62 79 0a 2a 2a 20 20 20 20 renced by.**
46e80 20 20 20 20 20 20 5b 42 4c 4f 42 20 68 61 6e 64 [BLOB hand
46e90 6c 65 5d 20 50 20 62 65 67 69 6e 6e 69 6e 67 20 le] P beginning
46ea0 61 74 20 6f 66 66 73 65 74 20 58 20 61 6e 64 20 at offset X and
46eb0 73 74 6f 72 65 20 74 68 6f 73 65 20 62 79 74 65 store those byte
46ec0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e s.** in
46ed0 74 6f 20 62 75 66 66 65 72 20 5a 2e 0a 2a 2a 0a to buffer Z..**.
46ee0 2a 2a 20 7b 48 31 37 38 35 36 7d 20 49 6e 20 5b ** {H17856} In [
46ef0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 sqlite3_blob_rea
46f00 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 66 20 74 d(P,Z,N,X)] if t
46f10 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 he size of the B
46f20 4c 4f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 LOB.**
46f30 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 2b 58 is less than N+X
46f40 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74 68 65 bytes, then the
46f50 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 function shall
46f60 6c 65 61 76 65 20 74 68 65 0a 2a 2a 20 20 20 20 leave the.**
46f70 20 20 20 20 20 20 5a 20 62 75 66 66 65 72 20 75 Z buffer u
46f80 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 72 65 74 nchanged and ret
46f90 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f urn [SQLITE_ERRO
46fa0 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 35 R]..**.** {H1785
46fb0 39 7d 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f 62 9} In [sqlite3_b
46fc0 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 lob_read(P,Z,N,X
46fd0 29 5d 20 69 66 20 58 20 6f 72 20 4e 20 69 73 20 )] if X or N is
46fe0 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 0a 2a less than zero.*
46ff0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 * then
47000 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 the function sha
47010 6c 6c 20 6c 65 61 76 65 20 74 68 65 20 5a 20 62 ll leave the Z b
47020 75 66 66 65 72 20 75 6e 63 68 61 6e 67 65 64 0a uffer unchanged.
47030 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 ** and
47040 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 return [SQLITE_E
47050 52 52 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 RROR]..**.** {H1
47060 37 38 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 7862} The [sqlit
47070 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a e3_blob_read(P,Z
47080 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65 ,N,X)] interface
47090 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 5b 53 shall return [S
470a0 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20 20 QLITE_OK].**
470b0 20 20 20 20 20 20 69 66 20 4e 20 62 79 74 65 73 if N bytes
470c0 20 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 6c are successfull
470d0 79 20 72 65 61 64 20 69 6e 74 6f 20 62 75 66 66 y read into buff
470e0 65 72 20 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 er Z..**.** {H17
470f0 38 36 33 7d 20 49 66 20 74 68 65 20 5b 42 4c 4f 863} If the [BLO
47100 42 20 68 61 6e 64 6c 65 5d 20 50 20 69 73 20 65 B handle] P is e
47110 78 70 69 72 65 64 20 61 6e 64 20 58 20 61 6e 64 xpired and X and
47120 20 4e 20 61 72 65 20 77 69 74 68 69 6e 20 62 6f N are within bo
47130 75 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 unds.**
47140 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 62 then [sqlite3_b
47150 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 lob_read(P,Z,N,X
47160 29 5d 20 73 68 61 6c 6c 20 6c 65 61 76 65 20 74 )] shall leave t
47170 68 65 20 5a 20 62 75 66 66 65 72 0a 2a 2a 20 20 he Z buffer.**
47180 20 20 20 20 20 20 20 20 75 6e 63 68 61 6e 67 65 unchange
47190 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 d and return [SQ
471a0 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a LITE_ABORT]..**.
471b0 2a 2a 20 7b 48 31 37 38 36 35 7d 20 49 66 20 74 ** {H17865} If t
471c0 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 61 he requested rea
471d0 64 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 d could not be c
471e0 6f 6d 70 6c 65 74 65 64 2c 0a 2a 2a 20 20 20 20 ompleted,.**
471f0 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69 74 the [sqlit
47200 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a e3_blob_read(P,Z
47210 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65 ,N,X)] interface
47220 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 61 6e shall return an
47230 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 70 70 .** app
47240 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20 ropriate [error
47250 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 code] or [extend
47260 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a ed error code]..
47270 2a 2a 0a 2a 2a 20 7b 48 31 37 38 36 38 7d 20 49 **.** {H17868} I
47280 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
47290 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75 61 74 s during evaluat
472a0 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f ion of [sqlite3_
472b0 62 6c 6f 62 5f 72 65 61 64 28 50 2c 2e 2e 2e 29 blob_read(P,...)
472c0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 ].** th
472d0 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 en subsequent ca
472e0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
472f0 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a 2a 2a 20 errcode(D)],.**
47300 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
47310 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 3_extended_errco
47320 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 de()],.**
47330 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d [sqlite3_errm
47340 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b 73 71 6c sg(D)], and [sql
47350 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 44 29 ite3_errmsg16(D)
47360 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a ] shall return.*
47370 2a 20 20 20 20 20 20 20 20 20 20 69 6e 66 6f 72 * infor
47380 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69 61 mation appropria
47390 74 65 20 66 6f 72 20 74 68 61 74 20 65 72 72 6f te for that erro
473a0 72 2c 20 77 68 65 72 65 20 44 20 69 73 20 74 68 r, where D is th
473b0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 e.** [d
473c0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
473d0 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 75 73 65 on] that was use
473e0 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 5b 42 d to open the [B
473f0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 2e 0a 2a LOB handle] P..*
47400 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
47410 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 sqlite3_blob_re
47420 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 ad(sqlite3_blob
47430 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69 6e 74 20 *, void *Z, int
47440 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 3b N, int iOffset);
47450 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
47460 3a 20 57 72 69 74 65 20 44 61 74 61 20 49 6e 74 : Write Data Int
47470 6f 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 o A BLOB Increme
47480 6e 74 61 6c 6c 79 20 7b 48 31 37 38 37 30 7d 20 ntally {H17870}
47490 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S30230>.**.** T
474a0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
474b0 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 64 61 used to write da
474c0 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 65 6e 20 ta into an open
474d0 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 72 [BLOB handle] fr
474e0 6f 6d 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 om a.** caller-s
474f0 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e 20 upplied buffer.
47500 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 N bytes of data
47510 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 are copied from
47520 74 68 65 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 the buffer Z.**
47530 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e 20 42 4c into the open BL
47540 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 61 74 20 OB, starting at
47550 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e 0a offset iOffset..
47560 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 42 4c **.** If the [BL
47570 4f 42 20 68 61 6e 64 6c 65 5d 20 70 61 73 73 65 OB handle] passe
47580 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
47590 72 67 75 6d 65 6e 74 20 77 61 73 20 6e 6f 74 20 rgument was not
475a0 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 77 72 opened for.** wr
475b0 69 74 69 6e 67 20 28 74 68 65 20 66 6c 61 67 73 iting (the flags
475c0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
475d0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
475e0 28 29 5d 20 77 61 73 20 7a 65 72 6f 29 2c 0a 2a ()] was zero),.*
475f0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
47600 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
47610 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a READONLY]..**.**
47620 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
47630 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66 79 20 74 ay only modify t
47640 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
47650 68 65 20 42 4c 4f 42 3b 20 69 74 20 69 73 0a 2a he BLOB; it is.*
47660 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 * not possible t
47670 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 o increase the s
47680 69 7a 65 20 6f 66 20 61 20 42 4c 4f 42 20 75 73 ize of a BLOB us
47690 69 6e 67 20 74 68 69 73 20 41 50 49 2e 0a 2a 2a ing this API..**
476a0 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 If offset iOffs
476b0 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 et is less than
476c0 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 N bytes from the
476d0 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 end of the BLOB
476e0 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 ,.** [SQLITE_ERR
476f0 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 OR] is returned
47700 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 and no data is w
47710 72 69 74 74 65 6e 2e 20 20 49 66 20 4e 20 69 73 ritten. If N is
47720 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 .** less than ze
47730 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 ro [SQLITE_ERROR
47740 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
47750 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 d no data is wri
47760 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 tten..**.** An a
47770 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 ttempt to write
47780 74 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 to an expired [B
47790 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c LOB handle] fail
477a0 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 s with an.** err
477b0 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 or code of [SQLI
477c0 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74 TE_ABORT]. Writ
477d0 65 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74 es to the BLOB t
477e0 68 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 hat occurred.**
477f0 62 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 before the [BLOB
47800 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 handle] expired
47810 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 are not rolled
47820 62 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65 back by the.** e
47830 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 xpiration of the
47840 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 handle, though
47850 6f 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 of course those
47860 63 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a changes might.**
47870 20 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 have been overw
47880 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 ritten by the st
47890 61 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 atement that exp
478a0 69 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 ired the BLOB ha
478b0 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74 ndle.** or by ot
478c0 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 her independent
478d0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a statements..**.*
478e0 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 * On success, SQ
478f0 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
47900 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ned..** Otherwis
47910 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f e, an [error co
47920 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e de] or an [exten
47930 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 ded error code]
47940 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
47950 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
47960 2a 0a 2a 2a 20 7b 48 31 37 38 37 33 7d 20 41 20 *.** {H17873} A
47970 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 successful invoc
47980 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 ation of [sqlite
47990 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 3_blob_write(P,Z
479a0 2c 4e 2c 58 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,N,X)].**
479b0 20 20 20 73 68 61 6c 6c 20 77 72 69 74 65 20 4e shall write N
479c0 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 66 bytes of data f
479d0 72 6f 6d 20 62 75 66 66 65 72 20 5a 20 69 6e 74 rom buffer Z int
479e0 6f 20 74 68 65 20 42 4c 4f 42 20 0a 2a 2a 20 20 o the BLOB .**
479f0 20 20 20 20 20 20 20 20 72 65 66 65 72 65 6e 63 referenc
47a00 65 64 20 62 79 20 5b 42 4c 4f 42 20 68 61 6e 64 ed by [BLOB hand
47a10 6c 65 5d 20 50 20 62 65 67 69 6e 6e 69 6e 67 20 le] P beginning
47a20 61 74 20 6f 66 66 73 65 74 20 58 20 69 6e 74 6f at offset X into
47a30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
47a40 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 BLOB..**.** {H1
47a50 37 38 37 34 7d 20 49 6e 20 74 68 65 20 61 62 73 7874} In the abs
47a60 65 6e 63 65 20 6f 66 20 6f 74 68 65 72 20 6f 76 ence of other ov
47a70 65 72 72 69 64 64 69 6e 67 20 63 68 61 6e 67 65 erridding change
47a80 73 2c 20 74 68 65 20 63 68 61 6e 67 65 73 0a 2a s, the changes.*
47a90 2a 20 20 20 20 20 20 20 20 20 20 77 72 69 74 74 * writt
47aa0 65 6e 20 74 6f 20 61 20 42 4c 4f 42 20 62 79 20 en to a BLOB by
47ab0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 [sqlite3_blob_wr
47ac0 69 74 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20 ite()] shall.**
47ad0 20 20 20 20 20 20 20 20 20 72 65 6d 61 69 6e 20 remain
47ae0 69 6e 20 65 66 66 65 63 74 20 61 66 74 65 72 20 in effect after
47af0 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 5b the associated [
47b00 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 70 BLOB handle] exp
47b10 69 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 ires..**.** {H17
47b20 38 37 35 7d 20 49 66 20 74 68 65 20 5b 42 4c 4f 875} If the [BLO
47b30 42 20 68 61 6e 64 6c 65 5d 20 50 20 77 61 73 20 B handle] P was
47b40 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 opened for readi
47b50 6e 67 20 6f 6e 6c 79 20 74 68 65 6e 0a 2a 2a 20 ng only then.**
47b60 20 20 20 20 20 20 20 20 20 61 6e 20 69 6e 76 6f an invo
47b70 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 cation of [sqlit
47b80 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c e3_blob_write(P,
47b90 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 20 6c 65 Z,N,X)] shall le
47ba0 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ave.**
47bb0 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42 the referenced B
47bc0 4c 4f 42 20 75 6e 63 68 61 6e 67 65 64 20 61 6e LOB unchanged an
47bd0 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 d return [SQLITE
47be0 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a _READONLY]..**.*
47bf0 2a 20 7b 48 31 37 38 37 36 7d 20 49 66 20 74 68 * {H17876} If th
47c00 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c e size of the BL
47c10 4f 42 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 OB referenced by
47c20 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 [BLOB handle] P
47c30 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 is.**
47c40 6c 65 73 73 20 74 68 61 6e 20 4e 2b 58 20 62 79 less than N+X by
47c50 74 65 73 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 tes then [sqlite
47c60 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 3_blob_write(P,Z
47c70 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20 ,N,X)] shall.**
47c80 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 20 74 leave t
47c90 68 65 20 42 4c 4f 42 20 75 6e 63 68 61 6e 67 65 he BLOB unchange
47ca0 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 d and return [SQ
47cb0 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a 2a 2a 0a LITE_ERROR]..**.
47cc0 2a 2a 20 7b 48 31 37 38 37 37 7d 20 49 66 20 74 ** {H17877} If t
47cd0 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d he [BLOB handle]
47ce0 20 50 20 69 73 20 65 78 70 69 72 65 64 20 61 6e P is expired an
47cf0 64 20 58 20 61 6e 64 20 4e 20 61 72 65 20 77 69 d X and N are wi
47d00 74 68 69 6e 20 62 6f 75 6e 64 73 0a 2a 2a 20 20 thin bounds.**
47d10 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 then [sq
47d20 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 lite3_blob_read(
47d30 50 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 20 P,Z,N,X)] shall
47d40 6c 65 61 76 65 20 74 68 65 20 42 4c 4f 42 0a 2a leave the BLOB.*
47d50 2a 20 20 20 20 20 20 20 20 20 20 75 6e 63 68 61 * uncha
47d60 6e 67 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20 nged and return
47d70 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a [SQLITE_ABORT]..
47d80 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 39 7d 20 49 **.** {H17879} I
47d90 66 20 58 20 6f 72 20 4e 20 61 72 65 20 6c 65 73 f X or N are les
47da0 73 20 74 68 61 6e 20 7a 65 72 6f 20 74 68 65 6e s than zero then
47db0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 [sqlite3_blob_w
47dc0 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 0a 2a rite(P,Z,N,X)].*
47dd0 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c * shall
47de0 20 6c 65 61 76 65 20 74 68 65 20 42 4c 4f 42 20 leave the BLOB
47df0 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 5b 42 referenced by [B
47e00 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 20 75 6e LOB handle] P un
47e10 63 68 61 6e 67 65 64 0a 2a 2a 20 20 20 20 20 20 changed.**
47e20 20 20 20 20 61 6e 64 20 72 65 74 75 72 6e 20 5b and return [
47e30 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a 2a SQLITE_ERROR]..*
47e40 2a 0a 2a 2a 20 7b 48 31 37 38 38 32 7d 20 54 68 *.** {H17882} Th
47e50 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
47e60 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20 write(P,Z,N,X)]
47e70 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 interface shall
47e80 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 return.**
47e90 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 [SQLITE_OK] i
47ea0 66 20 4e 20 62 79 74 65 73 20 77 68 65 72 65 20 f N bytes where
47eb0 73 75 63 63 65 73 73 66 75 6c 6c 79 20 77 72 69 successfully wri
47ec0 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 42 4c tten into the BL
47ed0 4f 42 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 38 OB..**.** {H1788
47ee0 35 7d 20 49 66 20 74 68 65 20 72 65 71 75 65 73 5} If the reques
47ef0 74 65 64 20 77 72 69 74 65 20 63 6f 75 6c 64 20 ted write could
47f00 6e 6f 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 64 not be completed
47f10 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 ,.** th
47f20 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
47f30 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20 write(P,Z,N,X)]
47f40 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 interface shall
47f50 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20 20 20 20 return an.**
47f60 20 20 20 20 20 20 61 70 70 72 6f 70 72 69 61 74 appropriat
47f70 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f e [error code] o
47f80 72 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f r [extended erro
47f90 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b r code]..**.** {
47fa0 48 31 37 38 38 38 7d 20 49 66 20 61 6e 20 65 72 H17888} If an er
47fb0 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
47fc0 67 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 g evaluation of
47fd0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 [sqlite3_blob_wr
47fe0 69 74 65 28 44 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 ite(D,...)].**
47ff0 20 20 20 20 20 20 20 20 74 68 65 6e 20 73 75 62 then sub
48000 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f sequent calls to
48010 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 [sqlite3_errcod
48020 65 28 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 e(D)],.**
48030 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 [sqlite3_exte
48040 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c nded_errcode()],
48050 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
48060 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 44 29 5d lite3_errmsg(D)]
48070 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 , and [sqlite3_e
48080 72 72 6d 73 67 31 36 28 44 29 5d 20 73 68 61 6c rrmsg16(D)] shal
48090 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 l return.**
480a0 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e information
480b0 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 appropriate for
480c0 20 74 68 61 74 20 65 72 72 6f 72 2e 0a 2a 2f 0a that error..*/.
480d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
480e0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 qlite3_blob_writ
480f0 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a e(sqlite3_blob *
48100 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c , const void *z,
48110 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 int n, int iOff
48120 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 set);../*.** CAP
48130 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 46 I3REF: Virtual F
48140 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 63 ile System Objec
48150 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32 30 ts {H11200} <S20
48160 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 100>.**.** A vir
48170 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d 20 tual filesystem
48180 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71 6c (VFS) is an [sql
48190 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
481a0 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65 20 .** that SQLite
481b0 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63 74 uses to interact
481c0 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e 64 .** with the und
481d0 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e erlying operatin
481e0 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74 20 g system. Most
481f0 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63 6f SQLite builds co
48200 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69 6e me with a.** sin
48210 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 gle default VFS
48220 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 that is appropri
48230 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73 74 ate for the host
48240 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e 65 computer..** Ne
48250 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20 72 w VFSes can be r
48260 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65 78 egistered and ex
48270 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61 6e isting VFSes can
48280 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65 64 be unregistered
48290 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ..** The followi
482a0 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 ng interfaces ar
482b0 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a 2a e provided..**.*
482c0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 66 * The sqlite3_vf
482d0 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66 61 s_find() interfa
482e0 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
482f0 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67 69 nter to a VFS gi
48300 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a 2a ven its name..**
48310 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65 20 Names are case
48320 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e 61 sensitive..** Na
48330 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 mes are zero-ter
48340 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 minated UTF-8 st
48350 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65 rings..** If the
48360 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 re is no match,
48370 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 a NULL pointer i
48380 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 s returned..** I
48390 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e 55 f zVfsName is NU
483a0 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 LL then the defa
483b0 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75 72 ult VFS is retur
483c0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 56 ned..**.** New V
483d0 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74 65 FSes are registe
483e0 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33 red with sqlite3
483f0 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 2e _vfs_register().
48400 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46 53 .** Each new VFS
48410 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 becomes the def
48420 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65 20 ault VFS if the
48430 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 73 makeDflt flag is
48440 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 6d set..** The sam
48450 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65 67 e VFS can be reg
48460 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 istered multiple
48470 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20 69 times without i
48480 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 6b njury..** To mak
48490 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56 46 e an existing VF
484a0 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75 S into the defau
484b0 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65 72 lt VFS, register
484c0 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 74 it again.** wit
484d0 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 h the makeDflt f
484e0 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77 6f lag set. If two
484f0 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65 73 different VFSes
48500 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 6d with the.** sam
48510 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69 73 e name are regis
48520 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61 76 tered, the behav
48530 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
48540 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 69 . If a.** VFS i
48550 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 s registered wit
48560 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69 73 h a name that is
48570 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 NULL or an empt
48580 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 65 y string,.** the
48590 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 n the behavior i
485a0 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a s undefined..**.
485b0 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20 ** Unregister a
485c0 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71 6c VFS with the sql
485d0 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 ite3_vfs_unregis
485e0 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e ter() interface.
485f0 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 .** If the defau
48600 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67 69 lt VFS is unregi
48610 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 20 stered, another
48620 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61 73 VFS is chosen as
48630 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e .** the default.
48640 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f 72 The choice for
48650 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73 20 the new VFS is
48660 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a 2a arbitrary..**.**
48670 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
48680 2a 2a 20 7b 48 31 31 32 30 33 7d 20 54 68 65 20 ** {H11203} The
48690 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e [sqlite3_vfs_fin
486a0 64 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 d(N)] interface
486b0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
486c0 72 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 r to the.**
486d0 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20 registered
486e0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
486f0 6a 65 63 74 20 77 68 6f 73 65 20 6e 61 6d 65 20 ject whose name
48700 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 0a exactly matches.
48710 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
48720 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
48730 55 54 46 2d 38 20 73 74 72 69 6e 67 20 4e 2c 20 UTF-8 string N,
48740 6f 72 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 or it returns NU
48750 4c 4c 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 LL if.**
48760 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 there is no ma
48770 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 tch..**.** {H112
48780 30 36 7d 20 49 66 20 74 68 65 20 4e 20 70 61 72 06} If the N par
48790 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 ameter to [sqlit
487a0 65 33 5f 76 66 73 5f 66 69 6e 64 28 4e 29 5d 20 e3_vfs_find(N)]
487b0 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20 is NULL then.**
487c0 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e the fun
487d0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 ction returns a
487e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 64 pointer to the d
487f0 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f efault [sqlite3_
48800 76 66 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 vfs].**
48810 20 6f 62 6a 65 63 74 20 69 66 20 74 68 65 72 65 object if there
48820 20 69 73 20 6f 6e 65 2c 20 6f 72 20 4e 55 4c 4c is one, or NULL
48830 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
48840 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 default.**
48850 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 [sqlite3_vfs
48860 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 ] object..**.**
48870 7b 48 31 31 32 30 39 7d 20 54 68 65 20 5b 73 71 {H11209} The [sq
48880 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
48890 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66 61 er(P,F)] interfa
488a0 63 65 20 72 65 67 69 73 74 65 72 73 20 74 68 65 ce registers the
488b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 65 6c .** wel
488c0 6c 2d 66 6f 72 6d 65 64 20 5b 73 71 6c 69 74 65 l-formed [sqlite
488d0 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 50 20 3_vfs] object P
488e0 75 73 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 67 using the name g
488f0 69 76 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 iven.**
48900 20 62 79 20 74 68 65 20 7a 4e 61 6d 65 20 66 69 by the zName fi
48910 65 6c 64 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 eld of the objec
48920 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 31 32 t..**.** {H11212
48930 7d 20 55 73 69 6e 67 20 74 68 65 20 5b 73 71 6c } Using the [sql
48940 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 ite3_vfs_registe
48950 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66 61 63 r(P,F)] interfac
48960 65 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a e to register.**
48970 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61 the sa
48980 6d 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d me [sqlite3_vfs]
48990 20 6f 62 6a 65 63 74 20 6d 75 6c 74 69 70 6c 65 object multiple
489a0 20 74 69 6d 65 73 20 69 73 20 61 20 68 61 72 6d times is a harm
489b0 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a less no-op..**.*
489c0 2a 20 7b 48 31 31 32 31 35 7d 20 54 68 65 20 5b * {H11215} The [
489d0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
489e0 73 74 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 ster(P,F)] inter
489f0 66 61 63 65 20 6d 61 6b 65 73 20 74 68 65 20 5b face makes the [
48a00 73 71 6c 69 74 65 33 5f 76 66 73 5d 0a 2a 2a 20 sqlite3_vfs].**
48a10 20 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 object
48a20 50 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 P the default [s
48a30 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
48a40 63 74 20 69 66 20 46 20 69 73 20 6e 6f 6e 2d 7a ct if F is non-z
48a50 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 ero..**.** {H112
48a60 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 18} The [sqlite3
48a70 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28 _vfs_unregister(
48a80 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 75 6e P)] interface un
48a90 72 65 67 69 73 74 65 72 73 20 74 68 65 0a 2a 2a registers the.**
48aa0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
48ab0 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 50 e3_vfs] object P
48ac0 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e so that it is n
48ad0 6f 20 6c 6f 6e 67 65 72 20 72 65 74 75 72 6e 65 o longer returne
48ae0 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 d by.**
48af0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
48b00 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 76 66 s to [sqlite3_vf
48b10 73 5f 66 69 6e 64 28 29 5d 2e 0a 2a 2f 0a 53 51 s_find()]..*/.SQ
48b20 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
48b30 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 _vfs *sqlite3_vf
48b40 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 s_find(const cha
48b50 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51 r *zVfsName);.SQ
48b60 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
48b70 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 ite3_vfs_registe
48b80 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 r(sqlite3_vfs*,
48b90 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53 int makeDflt);.S
48ba0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
48bb0 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 lite3_vfs_unregi
48bc0 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 ster(sqlite3_vfs
48bd0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
48be0 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b 48 31 REF: Mutexes {H1
48bf0 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 7000} <S20000>.*
48c00 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 *.** The SQLite
48c10 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 65 20 core uses these
48c20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 68 72 routines for thr
48c30 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 ead.** synchroni
48c40 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 zation. Though t
48c50 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 65 64 hey are intended
48c60 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a for internal.**
48c70 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2c 20 use by SQLite,
48c80 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 code that links
48c90 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 20 69 against SQLite i
48ca0 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 74 s.** permitted t
48cb0 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 o use any of the
48cc0 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a se routines..**.
48cd0 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73 6f ** The SQLite so
48ce0 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 69 urce code contai
48cf0 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c ns multiple impl
48d00 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f ementations.** o
48d10 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 72 6f f these mutex ro
48d20 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 utines. An appr
48d30 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e opriate implemen
48d40 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c tation.** is sel
48d50 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 ected automatica
48d60 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 lly at compile-t
48d70 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 ime. The follow
48d80 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 ing.** implement
48d90 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69 6c ations are avail
48da0 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c 69 able in the SQLi
48db0 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c te core:.**.** <
48dc0 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 ul>.** <li> SQ
48dd0 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 2a LITE_MUTEX_OS2.*
48de0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f * <li> SQLITE_
48df0 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a 2a 2a MUTEX_PTHREAD.**
48e00 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d <li> SQLITE_M
48e10 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 3e UTEX_W32.** <li>
48e20 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f SQLITE_MUTEX_
48e30 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a NOOP.** </ul>.**
48e40 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4d .** The SQLITE_M
48e50 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 6d UTEX_NOOP implem
48e60 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 73 65 entation is a se
48e70 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a 2a t of routines.**
48e80 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 65 that does no re
48e90 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 69 al locking and i
48ea0 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f s appropriate fo
48eb0 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73 69 r use in.** a si
48ec0 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 ngle-threaded ap
48ed0 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 plication. The
48ee0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 SQLITE_MUTEX_OS2
48ef0 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 ,.** SQLITE_MUTE
48f00 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20 53 X_PTHREAD, and S
48f10 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 QLITE_MUTEX_W32
48f20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a implementations.
48f30 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69 61 ** are appropria
48f40 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f 53 te for use on OS
48f50 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57 69 /2, Unix, and Wi
48f60 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ndows..**.** If
48f70 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
48f80 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 ed with the SQLI
48f90 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 20 TE_MUTEX_APPDEF
48fa0 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20 preprocessor.**
48fb0 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28 77 macro defined (w
48fc0 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55 ith "-DSQLITE_MU
48fd0 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c 20 TEX_APPDEF=1"),
48fe0 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a then no mutex.**
48ff0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
49000 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74 68 is included with
49010 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49 6e the library. In
49020 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a 2a this case the.*
49030 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 * application mu
49040 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73 74 st supply a cust
49050 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 om mutex impleme
49060 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 ntation using th
49070 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e e.** [SQLITE_CON
49080 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f FIG_MUTEX] optio
49090 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
490a0 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74 69 _config() functi
490b0 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c on.** before cal
490c0 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 ling sqlite3_ini
490d0 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e 79 tialize() or any
490e0 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 73 71 other public sq
490f0 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 69 lite3_.** functi
49100 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 73 71 on that calls sq
49110 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
49120 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 ()..**.** {H1701
49130 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 1} The sqlite3_m
49140 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 utex_alloc() rou
49150 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 tine allocates a
49160 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e new.** mutex an
49170 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e d returns a poin
49180 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 37 30 ter to it. {H170
49190 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 72 6e 12} If it return
491a0 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d s NULL.** that m
491b0 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 eans that a mute
491c0 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 x could not be a
491d0 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 30 31 llocated. {H1701
491e0 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 3} SQLite.** wil
491f0 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 l unwind its sta
49200 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e ck and return an
49210 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 34 7d error. {H17014}
49220 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a The argument.**
49230 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
49240 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 x_alloc() is one
49250 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 of these intege
49260 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a r constants:.**.
49270 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
49280 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
49290 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 ST.** <li> SQLI
492a0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
492b0 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 VE.** <li> SQLI
492c0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
492d0 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 MASTER.** <li>
492e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
492f0 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 TIC_MEM.** <li>
49300 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
49310 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 ATIC_MEM2.** <li
49320 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
49330 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c STATIC_PRNG.** <
49340 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
49350 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 X_STATIC_LRU.**
49360 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
49370 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a EX_STATIC_LRU2.*
49380 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 * </ul>.**.** {H
49390 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 73 74 17015} The first
493a0 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 two constants c
493b0 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 ause sqlite3_mut
493c0 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 ex_alloc() to cr
493d0 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 eate.** a new mu
493e0 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 tex. The new mu
493f0 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65 tex is recursive
49400 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 when SQLITE_MUT
49410 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
49420 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 is used but not
49430 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 necessarily so w
49440 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 hen SQLITE_MUTEX
49450 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 20 7b _FAST is used. {
49460 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 65 END}.** The mute
49470 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
49480 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
49490 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 o make a distinc
494a0 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 tion.** between
494b0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
494c0 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 URSIVE and SQLIT
494d0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 E_MUTEX_FAST if
494e0 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 it does.** not w
494f0 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 31 36 ant to. {H17016
49500 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c } But SQLite wil
49510 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 l only request a
49520 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
49530 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 in.** cases whe
49540 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 re it really nee
49550 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 49 ds one. {END} I
49560 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 f a faster non-r
49570 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a ecursive mutex.*
49580 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
49590 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e is available on
495a0 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f the host platfo
495b0 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 rm, the mutex su
495c0 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 bsystem.** might
495d0 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d return such a m
495e0 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 utex in response
495f0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 to SQLITE_MUTEX
49600 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 _FAST..**.** {H1
49610 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 72 20 7017} The other
49620 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 allowed paramete
49630 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 rs to sqlite3_mu
49640 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 tex_alloc() each
49650 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 return.** a poi
49660 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 nter to a static
49670 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 preexisting mut
49680 65 78 2e 20 7b 45 4e 44 7d 20 20 46 6f 75 72 20 ex. {END} Four
49690 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 static mutexes a
496a0 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 re.** used by th
496b0 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f e current versio
496c0 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 n of SQLite. Fu
496d0 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 ture versions of
496e0 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 SQLite.** may a
496f0 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 dd additional st
49700 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 atic mutexes. S
49710 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 tatic mutexes ar
49720 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a e for internal.*
49730 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 * use by SQLite
49740 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 only. Applicati
49750 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c ons that use SQL
49760 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 ite mutexes shou
49770 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 ld.** use only t
49780 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 he dynamic mutex
49790 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 es returned by S
497a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
497b0 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 or.** SQLITE_MU
497c0 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a TEX_RECURSIVE..*
497d0 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f *.** {H17018} No
497e0 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f te that if one o
497f0 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 f the dynamic mu
49800 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 tex parameters (
49810 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
49820 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d T.** or SQLITE_M
49830 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 UTEX_RECURSIVE)
49840 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c is used then sql
49850 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
49860 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
49870 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 different mutex
49880 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 on every call.
49890 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f 72 {H17034} But for
498a0 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d the static.** m
498b0 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 utex types, the
498c0 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 same mutex is re
498d0 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 turned on every
498e0 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a call that has.**
498f0 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e the same type n
49900 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 umber..**.** {H1
49910 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 65 7019} The sqlite
49920 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 72 3_mutex_free() r
49930 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 outine deallocat
49940 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a es a previously.
49950 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e ** allocated dyn
49960 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31 37 amic mutex. {H17
49970 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 63 020} SQLite is c
49980 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f areful to deallo
49990 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 79 cate every.** dy
499a0 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 74 namic mutex that
499b0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b it allocates. {
499c0 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e 61 A17021} The dyna
499d0 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 74 mic mutexes must
499e0 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 not be in.** us
499f0 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 e when they are
49a00 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31 deallocated. {A1
49a10 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e 67 7022} Attempting
49a20 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 to deallocate a
49a30 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 static.** mutex
49a40 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 results in unde
49a50 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 20 fined behavior.
49a60 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65 20 {H17023} SQLite
49a70 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 65 never deallocate
49a80 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d 75 s.** a static mu
49a90 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a tex. {END}.**.**
49aa0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
49ab0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 ex_enter() and s
49ac0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
49ad0 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 () routines atte
49ae0 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 mpt.** to enter
49af0 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 34 a mutex. {H17024
49b00 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 } If another thr
49b10 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
49b20 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
49b30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
49b40 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
49b50 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
49b60 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
49b70 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
49b80 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32 35 TE_BUSY. {H17025
49b90 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d } The sqlite3_m
49ba0 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 utex_try() inter
49bb0 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 51 face returns [SQ
49bc0 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e LITE_OK].** upon
49bd0 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 successful entr
49be0 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75 74 y. {H17026} Mut
49bf0 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 exes created usi
49c00 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 ng.** SQLITE_MUT
49c10 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
49c20 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 be entered mult
49c30 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 iple times by th
49c40 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a e same thread..*
49c50 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73 75 * {H17027} In su
49c60 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a ch cases the,.**
49c70 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 mutex must be e
49c80 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e xited an equal n
49c90 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 umber of times b
49ca0 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 efore another th
49cb0 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 read.** can ente
49cc0 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66 20 r. {A17028} If
49cd0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
49ce0 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 tries to enter a
49cf0 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 ny other.** kind
49d00 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 74 of mutex more t
49d10 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 han once, the be
49d20 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
49d30 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d ned..** {H17029}
49d40 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
49d50 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 er exhibit.** su
49d60 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 69 ch behavior in i
49d70 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 ts own use of mu
49d80 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d texes..**.** Som
49d90 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20 65 e systems (for e
49da0 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20 xample, Windows
49db0 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 95) do not suppo
49dc0 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e rt the operation
49dd0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 .** implemented
49de0 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 by sqlite3_mutex
49df0 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73 _try(). On thos
49e00 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69 74 e systems, sqlit
49e10 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a e3_mutex_try().*
49e20 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 * will always re
49e30 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
49e40 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65 20 . {H17030} The
49e50 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79 SQLite core only
49e60 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73 71 ever uses.** sq
49e70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
49e80 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 ) as an optimiza
49e90 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73 20 tion so this is
49ea0 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61 76 acceptable behav
49eb0 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 ior..**.** {H170
49ec0 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 31} The sqlite3_
49ed0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f mutex_leave() ro
49ee0 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 utine exits a mu
49ef0 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 tex that was.**
49f00 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 previously enter
49f10 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 ed by the same t
49f20 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32 7d hread. {A17032}
49f30 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a The behavior.**
49f40 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 is undefined if
49f50 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f the mutex is no
49f60 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 t currently ente
49f70 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 red by the.** ca
49f80 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 20 lling thread or
49f90 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
49fa0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31 allocated. {H1
49fb0 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69 6c 7033} SQLite wil
49fc0 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 69 l.** never do ei
49fd0 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a ther. {END}.**.*
49fe0 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e * If the argumen
49ff0 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 t to sqlite3_mut
4a000 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c 69 ex_enter(), sqli
4a010 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c te3_mutex_try(),
4a020 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d or.** sqlite3_m
4a030 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 20 utex_leave() is
4a040 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
4a050 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 72 then all three r
4a060 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76 outines.** behav
4a070 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a e as no-ops..**.
4a080 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
4a090 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
4a0a0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
4a0b0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
4a0c0 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
4a0d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
4a0e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
4a0f0 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 loc(int);.SQLITE
4a100 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
4a110 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c 3_mutex_free(sql
4a120 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 ite3_mutex*);.SQ
4a130 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
4a140 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
4a150 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a r(sqlite3_mutex*
4a160 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
4a170 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f t sqlite3_mutex_
4a180 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 try(sqlite3_mute
4a190 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 x*);.SQLITE_API
4a1a0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
4a1b0 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 ex_leave(sqlite3
4a1c0 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _mutex*);../*.**
4a1d0 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
4a1e0 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 Methods Object
4a1f0 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33 30 {H17120} <S20130
4a200 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
4a210 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 L.**.** An insta
4a220 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
4a230 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 cture defines th
4a240 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 e low-level rout
4a250 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 ines.** used to
4a260 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 65 allocate and use
4a270 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 mutexes..**.**
4a280 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 Usually, the def
4a290 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 ault mutex imple
4a2a0 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 mentations provi
4a2b0 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 ded by SQLite ar
4a2c0 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 2c e.** sufficient,
4a2d0 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 65 however the use
4a2e0 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f 6e r has the option
4a2f0 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e 67 of substituting
4a300 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 a custom.** imp
4a310 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 lementation for
4a320 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 6c specialized depl
4a330 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 65 oyments or syste
4a340 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 4c ms for which SQL
4a350 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 ite.** does not
4a360 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 62 provide a suitab
4a370 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le implementatio
4a380 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c n. In this case,
4a390 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 the user.** cre
4a3a0 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 ates and populat
4a3b0 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f es an instance o
4a3c0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
4a3d0 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 to pass.** to s
4a3e0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
4a3f0 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 5b along with the [
4a400 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
4a410 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 TEX] option..**
4a420 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e Additionally, an
4a430 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
4a440 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 s structure can
4a450 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a be used as an.**
4a460 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 output variable
4a470 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 74 when querying t
4a480 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 68 he system for th
4a490 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 0a e current mutex.
4a4a0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
4a4b0 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 51 n, using the [SQ
4a4c0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d LITE_CONFIG_GETM
4a4d0 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a UTEX] option..**
4a4e0 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 6e .** The xMutexIn
4a4f0 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 it method define
4a500 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 d by this struct
4a510 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 ure is invoked a
4a520 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73 s.** part of sys
4a530 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 tem initializati
4a540 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 on by the sqlite
4a550 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 3_initialize() f
4a560 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 unction..** {H17
4a570 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78 49 001} The xMutexI
4a580 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61 6c nit routine shal
4a590 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 53 l be called by S
4a5a0 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20 65 QLite once for e
4a5b0 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 ach.** effective
4a5c0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
4a5d0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e 3_initialize()].
4a5e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 .**.** The xMute
4a5f0 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66 69 xEnd method defi
4a600 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 ned by this stru
4a610 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 cture is invoked
4a620 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 as.** part of s
4a630 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20 62 ystem shutdown b
4a640 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 68 y the sqlite3_sh
4a650 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f utdown() functio
4a660 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d n. The.** implem
4a670 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 entation of this
4a680 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65 63 method is expec
4a690 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 61 ted to release a
4a6a0 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a ll outstanding.*
4a6b0 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74 61 * resources obta
4a6c0 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74 65 ined by the mute
4a6d0 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d x methods implem
4a6e0 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63 69 entation, especi
4a6f0 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62 ally.** those ob
4a700 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78 4d tained by the xM
4a710 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e utexInit method.
4a720 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78 4d {H17003} The xM
4a730 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74 utexEnd().** int
4a740 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 20 erface shall be
4a750 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 invoked once for
4a760 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 each call to [s
4a770 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
4a780 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 )]..**.** The re
4a790 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65 maining seven me
4a7a0 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62 79 thods defined by
4a7b0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
4a7c0 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a (xMutexAlloc,.**
4a7d0 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d 75 xMutexFree, xMu
4a7e0 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65 78 texEnter, xMutex
4a7f0 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76 65 Try, xMutexLeave
4a800 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e 64 , xMutexHeld and
4a810 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c .** xMutexNothel
4a820 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 d) implement the
4a830 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 following inter
4a840 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69 76 faces (respectiv
4a850 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ely):.**.** <ul>
4a860 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c .** <li> [sql
4a870 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
4a880 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c ()] </li>.** <
4a890 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 li> [sqlite3_mu
4a8a0 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69 tex_free()] </li
4a8b0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
4a8c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
4a8d0 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 r()] </li>.**
4a8e0 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
4a8f0 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69 utex_try()] </li
4a900 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
4a910 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
4a920 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 e()] </li>.**
4a930 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
4a940 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c utex_held()] </l
4a950 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
4a960 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
4a970 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a held()] </li>.**
4a980 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
4a990 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 only difference
4a9a0 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75 62 is that the pub
4a9b0 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58 20 lic sqlite3_XXX
4a9c0 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72 functions enumer
4a9d0 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69 ated.** above si
4a9e0 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e lently ignore an
4a9f0 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68 y invocations th
4aa00 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20 70 at pass a NULL p
4aa10 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a 2a ointer instead.*
4aa20 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74 * of a valid mut
4aa30 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 69 ex handle. The i
4aa40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
4aa50 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64 65 f the methods de
4aa60 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69 73 fined.** by this
4aa70 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 6e structure are n
4aa80 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 68 ot required to h
4aa90 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 2c andle this case,
4aaa0 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 the results.**
4aab0 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 4c of passing a NUL
4aac0 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 L pointer instea
4aad0 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74 d of a valid mut
4aae0 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 6e ex handle are un
4aaf0 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e defined.** (i.e.
4ab00 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62 6c it is acceptabl
4ab10 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 e to provide an
4ab20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 implementation t
4ab30 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69 66 hat segfaults if
4ab40 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65 64 .** it is passed
4ab50 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29 a NULL pointer)
4ab60 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
4ab70 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 uct sqlite3_mute
4ab80 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 x_methods sqlite
4ab90 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 3b 3_mutex_methods;
4aba0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
4abb0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 7b 0a mutex_methods {.
4abc0 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 49 6e int (*xMutexIn
4abd0 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 it)(void);. int
4abe0 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28 76 6f (*xMutexEnd)(vo
4abf0 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d id);. sqlite3_m
4ac00 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78 41 6c utex *(*xMutexAl
4ac10 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 loc)(int);. voi
4ac20 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65 29 28 d (*xMutexFree)(
4ac30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 sqlite3_mutex *)
4ac40 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 ;. void (*xMute
4ac50 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65 33 5f xEnter)(sqlite3_
4ac60 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 mutex *);. int
4ac70 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73 71 6c (*xMutexTry)(sql
4ac80 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 ite3_mutex *);.
4ac90 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 4c 65 void (*xMutexLe
4aca0 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 ave)(sqlite3_mut
4acb0 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 ex *);. int (*x
4acc0 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c 69 74 MutexHeld)(sqlit
4acd0 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 e3_mutex *);. i
4ace0 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74 68 65 nt (*xMutexNothe
4acf0 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ld)(sqlite3_mute
4ad00 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 x *);.};../*.**
4ad10 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 CAPI3REF: Mutex
4ad20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52 6f 75 Verification Rou
4ad30 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d 20 3c tines {H17080} <
4ad40 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30 30 3e S20130> <S30800>
4ad50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
4ad60 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 e3_mutex_held()
4ad70 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
4ad80 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 x_notheld() rout
4ad90 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 ines.** are inte
4ada0 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 nded for use ins
4adb0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
4adc0 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30 38 31 tements. {H17081
4add0 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 } The SQLite cor
4ade0 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65 73 20 e.** never uses
4adf0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 65 these routines e
4ae00 78 63 65 70 74 20 69 6e 73 69 64 65 20 61 6e 20 xcept inside an
4ae10 61 73 73 65 72 74 28 29 20 61 6e 64 20 61 70 70 assert() and app
4ae20 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 lications.** are
4ae30 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f 6c 6c advised to foll
4ae40 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66 20 74 ow the lead of t
4ae50 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37 30 38 he core. {H1708
4ae60 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e 6c 79 2} The core only
4ae70 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 6d 70 .** provides imp
4ae80 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 lementations for
4ae90 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
4aea0 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70 69 when it is compi
4aeb0 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 led.** with the
4aec0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 6c 61 SQLITE_DEBUG fla
4aed0 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45 78 74 g. {A17087} Ext
4aee0 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d 70 6c ernal mutex impl
4aef0 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 ementations.** a
4af00 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 re only required
4af10 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 73 to provide thes
4af20 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20 53 51 e routines if SQ
4af30 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a 2a 2a LITE_DEBUG is.**
4af40 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69 66 20 defined and if
4af50 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 NDEBUG is not de
4af60 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 fined..**.** {H1
4af70 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f 75 74 7083} These rout
4af80 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 74 75 ines should retu
4af90 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 6d rn true if the m
4afa0 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 61 72 utex in their ar
4afb0 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 65 6c gument.** is hel
4afc0 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c 20 72 d or not held, r
4afd0 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 79 20 espectively, by
4afe0 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 the calling thre
4aff0 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 30 38 ad..**.** {X1708
4b000 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 4} The implement
4b010 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71 ation is not req
4b020 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 65 uired to provide
4b030 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 d versions of th
4b040 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 ese.** routines
4b050 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 77 6f that actually wo
4b060 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70 6c 65 rk. If the imple
4b070 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e mentation does n
4b080 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72 6b 69 ot provide worki
4b090 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f ng.** versions o
4b0a0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
4b0b0 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74 20 6c , it should at l
4b0c0 65 61 73 74 20 70 72 6f 76 69 64 65 20 73 74 75 east provide stu
4b0d0 62 73 20 74 68 61 74 20 61 6c 77 61 79 73 0a 2a bs that always.*
4b0e0 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 73 6f * return true so
4b0f0 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 20 6e that one does n
4b100 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75 73 20 ot get spurious
4b110 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 75 72 assertion failur
4b120 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 es..**.** {H1708
4b130 35 7d 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 5} If the argume
4b140 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 nt to sqlite3_mu
4b150 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 61 20 tex_held() is a
4b160 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 NULL pointer the
4b170 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e 65 n.** the routine
4b180 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 31 should return 1
4b190 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20 73 65 . {END} This se
4b1a0 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e 74 75 ems counter-intu
4b1b0 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a 20 63 itive since.** c
4b1c0 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74 65 78 learly the mutex
4b1d0 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c 64 20 cannot be held
4b1e0 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 if it does not e
4b1f0 78 69 73 74 2e 20 20 42 75 74 20 74 68 65 0a 2a xist. But the.*
4b200 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 65 * the reason the
4b210 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f 74 20 mutex does not
4b220 65 78 69 73 74 20 69 73 20 62 65 63 61 75 73 65 exist is because
4b230 20 74 68 65 20 62 75 69 6c 64 20 69 73 20 6e 6f the build is no
4b240 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74 65 78 t.** using mutex
4b250 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f 20 6e es. And we do n
4b260 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73 73 65 ot want the asse
4b270 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 rt() containing
4b280 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 the.** call to s
4b290 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
4b2a0 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73 6f 20 d() to fail, so
4b2b0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 a non-zero retur
4b2c0 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70 70 72 n is.** the appr
4b2d0 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20 74 6f opriate thing to
4b2e0 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d 20 54 do. {H17086} T
4b2f0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
4b300 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 69 6e _notheld().** in
4b310 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20 61 terface should a
4b320 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68 65 lso return 1 whe
4b330 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20 70 n given a NULL p
4b340 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 ointer..*/.SQLIT
4b350 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
4b360 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 3_mutex_held(sql
4b370 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 ite3_mutex*);.SQ
4b380 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
4b390 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 ite3_mutex_nothe
4b3a0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
4b3b0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
4b3c0 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70 65 73 REF: Mutex Types
4b3d0 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37 30 30 {H17001} <H1700
4b3e0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 0>.**.** The [sq
4b3f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
4b400 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74 c()] interface t
4b410 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 akes a single ar
4b420 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63 68 20 gument.** which
4b430 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 is one of these
4b440 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
4b450 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 74 s..**.** The set
4b460 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74 65 78 of static mutex
4b470 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72 es may change fr
4b480 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20 72 65 om one SQLite re
4b490 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20 lease to the.**
4b4a0 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61 74 69 next. Applicati
4b4b0 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72 69 64 ons that overrid
4b4c0 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d e the built-in m
4b4d0 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73 74 20 utex logic must
4b4e0 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 74 be.** prepared t
4b4f0 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 61 64 o accommodate ad
4b500 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 ditional static
4b510 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64 65 66 mutexes..*/.#def
4b520 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
4b530 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20 20 _FAST
4b540 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 0.#define SQLI
4b550 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
4b560 56 45 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 VE 1.#def
4b570 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
4b580 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 20 _STATIC_MASTER
4b590 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 2.#define SQLI
4b5a0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
4b5b0 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f 2a 20 MEM 3 /*
4b5c0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
4b5d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
4b5e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
4b5f0 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f 2a 20 MEM2 4 /*
4b600 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
4b610 6d 65 6d 6f 72 79 28 29 20 2a 2f 0a 23 64 65 66 memory() */.#def
4b620 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
4b630 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 _STATIC_PRNG
4b640 20 20 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 5 /* sqlite3_
4b650 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66 random() */.#def
4b660 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
4b670 5f 53 54 41 54 49 43 5f 4c 52 55 20 20 20 20 20 _STATIC_LRU
4b680 20 20 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 6 /* lru page
4b690 20 6c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 list */.#define
4b6a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
4b6b0 41 54 49 43 5f 4c 52 55 32 20 20 20 20 20 20 37 ATIC_LRU2 7
4b6c0 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 /* lru page li
4b6d0 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 st */../*.** CAP
4b6e0 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 65 20 I3REF: Retrieve
4b6f0 74 68 65 20 6d 75 74 65 78 20 66 6f 72 20 61 20 the mutex for a
4b700 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
4b710 69 6f 6e 20 7b 48 31 37 30 30 32 7d 20 3c 48 31 ion {H17002} <H1
4b720 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 7000>.**.** This
4b730 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
4b740 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 68 65 ns a pointer the
4b750 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d [sqlite3_mutex]
4b760 20 6f 62 6a 65 63 74 20 74 68 61 74 20 0a 2a 2a object that .**
4b770 20 73 65 72 69 61 6c 69 7a 65 73 20 61 63 63 65 serializes acce
4b780 73 73 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 ss to the [datab
4b790 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
4b7a0 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 given in the arg
4b7b0 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 68 ument.** when th
4b7c0 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 e [threading mod
4b7d0 65 5d 20 69 73 20 53 65 72 69 61 6c 69 7a 65 64 e] is Serialized
4b7e0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 74 68 72 ..** If the [thr
4b7f0 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 eading mode] is
4b800 53 69 6e 67 6c 65 2d 74 68 72 65 61 64 20 6f 72 Single-thread or
4b810 20 4d 75 6c 74 69 2d 74 68 72 65 61 64 20 74 68 Multi-thread th
4b820 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 en this.** routi
4b830 6e 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c ne returns a NUL
4b840 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 L pointer..*/.SQ
4b850 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
4b860 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f _mutex *sqlite3_
4b870 64 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 db_mutex(sqlite3
4b880 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
4b890 52 45 46 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43 REF: Low-Level C
4b8a0 6f 6e 74 72 6f 6c 20 4f 66 20 44 61 74 61 62 61 ontrol Of Databa
4b8b0 73 65 20 46 69 6c 65 73 20 7b 48 31 31 33 30 30 se Files {H11300
4b8c0 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a } <S30800>.**.**
4b8d0 20 7b 48 31 31 33 30 31 7d 20 54 68 65 20 5b 73 {H11301} The [s
4b8e0 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 qlite3_file_cont
4b8f0 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 rol()] interface
4b900 20 6d 61 6b 65 73 20 61 20 64 69 72 65 63 74 20 makes a direct
4b910 63 61 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 78 call to the.** x
4b920 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
4b930 6f 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 od for the [sqli
4b940 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 te3_io_methods]
4b950 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 object associate
4b960 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 d.** with a part
4b970 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 icular database
4b980 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 identified by th
4b990 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
4b9a0 74 2e 20 7b 48 31 31 33 30 32 7d 20 54 68 65 0a t. {H11302} The.
4b9b0 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 ** name of the d
4b9c0 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 6e atabase is the n
4b9d0 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 ame assigned to
4b9e0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 79 20 the database by
4b9f0 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 the.** <a href="
4ba00 6c 61 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d 6c lang_attach.html
4ba10 22 3e 41 54 54 41 43 48 3c 2f 61 3e 20 53 51 4c ">ATTACH</a> SQL
4ba20 20 63 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f 70 command that op
4ba30 65 6e 65 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 ened the.** data
4ba40 62 61 73 65 2e 20 7b 48 31 31 33 30 33 7d 20 54 base. {H11303} T
4ba50 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6d 61 o control the ma
4ba60 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
4ba70 2c 20 75 73 65 20 74 68 65 20 6e 61 6d 65 20 22 , use the name "
4ba80 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e 55 main".** or a NU
4ba90 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 7b 48 31 31 LL pointer. {H11
4baa0 33 30 34 7d 20 54 68 65 20 74 68 69 72 64 20 61 304} The third a
4bab0 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 nd fourth parame
4bac0 74 65 72 73 20 74 6f 20 74 68 69 73 20 72 6f 75 ters to this rou
4bad0 74 69 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73 73 tine.** are pass
4bae0 65 64 20 64 69 72 65 63 74 6c 79 20 74 68 72 6f ed directly thro
4baf0 75 67 68 20 74 6f 20 74 68 65 20 73 65 63 6f 6e ugh to the secon
4bb00 64 20 61 6e 64 20 74 68 69 72 64 20 70 61 72 61 d and third para
4bb10 6d 65 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68 65 meters of.** the
4bb20 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 xFileControl me
4bb30 74 68 6f 64 2e 20 20 7b 48 31 31 33 30 35 7d 20 thod. {H11305}
4bb40 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
4bb50 20 6f 66 20 74 68 65 20 78 46 69 6c 65 43 6f 6e of the xFileCon
4bb60 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 trol.** method b
4bb70 65 63 6f 6d 65 73 20 74 68 65 20 72 65 74 75 72 ecomes the retur
4bb80 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 n value of this
4bb90 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 7b routine..**.** {
4bba0 48 31 31 33 30 36 7d 20 49 66 20 74 68 65 20 73 H11306} If the s
4bbb0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
4bbc0 28 7a 44 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e (zDbName) does n
4bbd0 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e 61 6d ot match the nam
4bbe0 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e e of any.** open
4bbf0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
4bc00 74 68 65 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f then SQLITE_ERRO
4bc10 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 7b R is returned. {
4bc20 48 31 31 33 30 37 7d 20 54 68 69 73 20 65 72 72 H11307} This err
4bc30 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 6e 6f or.** code is no
4bc40 74 20 72 65 6d 65 6d 62 65 72 65 64 20 61 6e 64 t remembered and
4bc50 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63 will not be rec
4bc60 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65 alled by [sqlite
4bc70 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a 2a 2a 20 3_errcode()].**
4bc80 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d or [sqlite3_errm
4bc90 73 67 28 29 5d 2e 20 7b 41 31 31 33 30 38 7d 20 sg()]. {A11308}
4bca0 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 78 The underlying x
4bcb0 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
4bcc0 6f 64 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f od might.** also
4bcd0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
4bce0 52 52 4f 52 2e 20 20 7b 41 31 31 33 30 39 7d 20 RROR. {A11309}
4bcf0 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 There is no way
4bd00 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 20 62 to distinguish b
4bd10 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e 63 etween.** an inc
4bd20 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d 65 20 61 orrect zDbName a
4bd30 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 nd an SQLITE_ERR
4bd40 4f 52 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 OR return from t
4bd50 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a he underlying.**
4bd60 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 xFileControl me
4bd70 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a thod. {END}.**.*
4bd80 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c * See also: [SQL
4bd90 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 ITE_FCNTL_LOCKST
4bda0 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ATE].*/.SQLITE_A
4bdb0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
4bdc0 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 ile_control(sqli
4bdd0 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 te3*, const char
4bde0 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f *zDbName, int o
4bdf0 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a p, void*);../*.*
4be00 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 * CAPI3REF: Test
4be10 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 7b 48 ing Interface {H
4be20 31 31 34 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 11400} <S30800>.
4be30 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
4be40 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 3_test_control()
4be50 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 interface is us
4be60 65 64 20 74 6f 20 72 65 61 64 20 6f 75 74 20 69 ed to read out i
4be70 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 65 nternal.** state
4be80 20 6f 66 20 53 51 4c 69 74 65 20 61 6e 64 20 74 of SQLite and t
4be90 6f 20 69 6e 6a 65 63 74 20 66 61 75 6c 74 73 20 o inject faults
4bea0 69 6e 74 6f 20 53 51 4c 69 74 65 20 66 6f 72 20 into SQLite for
4beb0 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f testing.** purpo
4bec0 73 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 ses. The first
4bed0 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 parameter is an
4bee0 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 74 operation code t
4bef0 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a hat determines.*
4bf00 2a 20 74 68 65 20 6e 75 6d 62 65 72 2c 20 6d 65 * the number, me
4bf10 61 6e 69 6e 67 2c 20 61 6e 64 20 6f 70 65 72 61 aning, and opera
4bf20 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 75 62 73 tion of all subs
4bf30 65 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72 equent parameter
4bf40 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e s..**.** This in
4bf50 74 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 66 terface is not f
4bf60 6f 72 20 75 73 65 20 62 79 20 61 70 70 6c 69 63 or use by applic
4bf70 61 74 69 6f 6e 73 2e 20 20 49 74 20 65 78 69 73 ations. It exis
4bf80 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 ts solely.** for
4bf90 20 76 65 72 69 66 79 69 6e 67 20 74 68 65 20 63 verifying the c
4bfa0 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e orrect operation
4bfb0 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c of the SQLite l
4bfc0 69 62 72 61 72 79 2e 20 20 44 65 70 65 6e 64 69 ibrary. Dependi
4bfd0 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65 ng.** on how the
4bfe0 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 SQLite library
4bff0 69 73 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69 is compiled, thi
4c000 73 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 s interface migh
4c010 74 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a t not exist..**.
4c020 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f ** The details o
4c030 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 f the operation
4c040 63 6f 64 65 73 2c 20 74 68 65 69 72 20 6d 65 61 codes, their mea
4c050 6e 69 6e 67 73 2c 20 74 68 65 20 70 61 72 61 6d nings, the param
4c060 65 74 65 72 73 0a 2a 2a 20 74 68 65 79 20 74 61 eters.** they ta
4c070 6b 65 2c 20 61 6e 64 20 77 68 61 74 20 74 68 65 ke, and what the
4c080 79 20 64 6f 20 61 72 65 20 61 6c 6c 20 73 75 62 y do are all sub
4c090 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 ject to change w
4c0a0 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 0a 2a ithout notice..*
4c0b0 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 * Unlike most of
4c0c0 20 74 68 65 20 53 51 4c 69 74 65 20 41 50 49 2c the SQLite API,
4c0d0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
4c0e0 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 s not guaranteed
4c0f0 20 74 6f 0a 2a 2a 20 6f 70 65 72 61 74 65 20 63 to.** operate c
4c100 6f 6e 73 69 73 74 65 6e 74 6c 79 20 66 72 6f 6d onsistently from
4c110 20 6f 6e 65 20 72 65 6c 65 61 73 65 20 74 6f 20 one release to
4c120 74 68 65 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c the next..*/.SQL
4c130 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
4c140 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c te3_test_control
4c150 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a (int op, ...);..
4c160 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
4c170 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 Testing Interfac
4c180 65 20 4f 70 65 72 61 74 69 6f 6e 20 43 6f 64 65 e Operation Code
4c190 73 20 7b 48 31 31 34 31 30 7d 20 3c 48 31 31 34 s {H11410} <H114
4c1a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
4c1b0 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 constants are th
4c1c0 65 20 76 61 6c 69 64 20 6f 70 65 72 61 74 69 6f e valid operatio
4c1d0 6e 20 63 6f 64 65 20 70 61 72 61 6d 65 74 65 72 n code parameter
4c1e0 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 s used.** as the
4c1f0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
4c200 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 to [sqlite3_test
4c210 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a _control()]..**.
4c220 2a 2a 20 54 68 65 73 65 20 70 61 72 61 6d 65 74 ** These paramet
4c230 65 72 73 20 61 6e 64 20 74 68 65 69 72 20 6d 65 ers and their me
4c240 61 6e 69 6e 67 73 20 61 72 65 20 73 75 62 6a 65 anings are subje
4c250 63 74 20 74 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 ct to change.**
4c260 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20 without notice.
4c270 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 These values ar
4c280 65 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 e for testing pu
4c290 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20 rposes only..**
4c2a0 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f Applications sho
4c2b0 75 6c 64 20 6e 6f 74 20 75 73 65 20 61 6e 79 20 uld not use any
4c2c0 6f 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 of these paramet
4c2d0 65 72 73 20 6f 72 20 74 68 65 0a 2a 2a 20 5b 73 ers or the.** [s
4c2e0 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 qlite3_test_cont
4c2f0 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 rol()] interface
4c300 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
4c310 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e ITE_TESTCTRL_PRN
4c320 47 5f 53 41 56 45 20 20 20 20 20 20 20 20 20 20 G_SAVE
4c330 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 5.#define
4c340 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
4c350 50 52 4e 47 5f 52 45 53 54 4f 52 45 20 20 20 20 PRNG_RESTORE
4c360 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6.#defi
4c370 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
4c380 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 20 20 20 RL_PRNG_RESET
4c390 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 7.#d
4c3a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
4c3b0 54 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 TCTRL_BITVEC_TES
4c3c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 T 8
4c3d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
4c3e0 54 45 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 TESTCTRL_FAULT_I
4c3f0 4e 53 54 41 4c 4c 20 20 20 20 20 20 20 20 20 20 NSTALL
4c400 20 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 9.#define SQLI
4c410 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 TE_TESTCTRL_BENI
4c420 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 GN_MALLOC_HOOKS
4c430 20 20 20 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 10../*.** CA
4c440 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20 52 PI3REF: SQLite R
4c450 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b 48 untime Status {H
4c460 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 17200} <S60200>.
4c470 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
4c480 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
4c490 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 face is used to
4c4a0 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 retrieve runtime
4c4b0 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 status informat
4c4c0 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 ion.** about the
4c4d0 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66 20 preformance of
4c4e0 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74 69 SQLite, and opti
4c4f0 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74 20 onally to reset
4c500 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68 77 various.** highw
4c510 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68 65 ater marks. The
4c520 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
4c530 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f is an integer co
4c540 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 de for.** the sp
4c550 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65 72 ecific parameter
4c560 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52 65 to measure. Re
4c570 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65 72 cognized integer
4c580 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66 codes.** are of
4c590 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49 54 the form [SQLIT
4c5a0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
4c5b0 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53 54 USED | SQLITE_ST
4c5c0 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54 68 ATUS_...]..** Th
4c5d0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
4c5e0 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 of the parameter
4c5f0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 74 is returned int
4c600 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 20 o *pCurrent..**
4c610 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f The highest reco
4c620 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72 65 rded value is re
4c630 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67 68 turned in *pHigh
4c640 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a 2a water. If the.*
4c650 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 74 * resetFlag is t
4c660 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 rue, then the hi
4c670 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61 6c ghest record val
4c680 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74 65 ue is reset afte
4c690 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 r.** *pHighwater
4c6a0 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f 6d is written. Som
4c6b0 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f 20 e parameters do
4c6c0 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20 68 not record the h
4c6d0 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e ighest.** value.
4c6e0 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72 61 For those para
4c6f0 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e meters.** nothin
4c700 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 g is written int
4c710 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 6e o *pHighwater an
4c720 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67 20 d the resetFlag
4c730 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 4f is ignored..** O
4c740 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 ther parameters
4c750 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 20 record only the
4c760 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61 highwater mark a
4c770 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 65 nd not the curre
4c780 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f nt.** value. Fo
4c790 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 70 r these latter p
4c7a0 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 6e arameters nothin
4c7b0 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 g is written int
4c7c0 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 0a o *pCurrent..**.
4c7d0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
4c7e0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
4c7f0 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 K on success and
4c800 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 5b a non-zero.** [
4c810 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 error code] on f
4c820 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ailure..**.** Th
4c830 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
4c840 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73 20 readsafe but is
4c850 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68 69 not atomic. Thi
4c860 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 0a 2a 2a s routine can.**
4c870 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20 6f 74 called while ot
4c880 68 65 72 20 74 68 72 65 61 64 73 20 61 72 65 20 her threads are
4c890 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 61 6d 65 running the same
4c8a0 20 6f 72 20 64 69 66 66 65 72 65 6e 74 20 53 51 or different SQ
4c8b0 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 Lite.** interfac
4c8c0 65 73 2e 20 20 48 6f 77 65 76 65 72 20 74 68 65 es. However the
4c8d0 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 values returned
4c8e0 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e in *pCurrent an
4c8f0 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 d.** *pHighwater
4c900 20 72 65 66 6c 65 63 74 20 74 68 65 20 73 74 61 reflect the sta
4c910 74 75 73 20 6f 66 20 53 51 4c 69 74 65 20 61 74 tus of SQLite at
4c920 20 64 69 66 66 65 72 65 6e 74 20 70 6f 69 6e 74 different point
4c930 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 61 6e 64 s in time.** and
4c940 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
4c950 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 that another thr
4c960 65 61 64 20 6d 69 67 68 74 20 63 68 61 6e 67 65 ead might change
4c970 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a the parameter.*
4c980 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 * in between the
4c990 20 74 69 6d 65 73 20 77 68 65 6e 20 2a 70 43 75 times when *pCu
4c9a0 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 69 67 68 rrent and *pHigh
4c9b0 77 61 74 65 72 20 61 72 65 20 77 72 69 74 74 65 water are writte
4c9c0 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 n..**.** See als
4c9d0 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 o: [sqlite3_db_s
4c9e0 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 51 4c 49 tatus()].*/.SQLI
4c9f0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
4ca00 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 PERIMENTAL int s
4ca10 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e qlite3_status(in
4ca20 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 t op, int *pCurr
4ca30 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 ent, int *pHighw
4ca40 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 ater, int resetF
4ca50 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 lag);.../*.** CA
4ca60 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 PI3REF: Status P
4ca70 61 72 61 6d 65 74 65 72 73 20 7b 48 31 37 32 35 arameters {H1725
4ca80 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 2a 20 45 0} <H17200>.** E
4ca90 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
4caa0 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20 * These integer
4cab0 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 67 6e constants design
4cac0 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 6e 2d ate various run-
4cad0 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 72 61 time status para
4cae0 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 63 meters.** that c
4caf0 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 an be returned b
4cb00 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 y [sqlite3_statu
4cb10 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e s()]..**.** <dl>
4cb20 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
4cb30 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
4cb40 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
4cb50 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 is parameter is
4cb60 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d 6f 75 the current amou
4cb70 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 68 65 nt of memory che
4cb80 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73 69 6e cked out.** usin
4cb90 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f g [sqlite3_mallo
4cba0 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64 69 72 c()], either dir
4cbb0 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 ectly or indirec
4cbc0 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66 69 67 tly. The.** fig
4cbd0 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63 61 6c ure includes cal
4cbe0 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71 6c 69 ls made to [sqli
4cbf0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 79 te3_malloc()] by
4cc00 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
4cc10 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c .** and internal
4cc20 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 62 79 memory usage by
4cc30 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
4cc40 61 72 79 2e 20 20 53 63 72 61 74 63 68 20 6d 65 ary. Scratch me
4cc50 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 6c mory.** controll
4cc60 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f ed by [SQLITE_CO
4cc70 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61 6e NFIG_SCRATCH] an
4cc80 64 20 61 75 78 69 6c 69 61 72 79 20 70 61 67 65 d auxiliary page
4cc90 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 -cache.** memory
4cca0 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b controlled by [
4ccb0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
4ccc0 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f 74 20 GECACHE] is not
4ccd0 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a 20 74 included in.** t
4cce0 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e 20 20 his parameter.
4ccf0 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74 75 72 The amount retur
4cd00 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d 20 6f ned is the sum o
4cd10 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
4cd20 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72 65 70 .** sizes as rep
4cd30 6f 72 74 65 64 20 62 79 20 74 68 65 20 78 53 69 orted by the xSi
4cd40 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b 73 71 ze method in [sq
4cd50 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
4cd60 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c s].</dd>.**.** <
4cd70 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dt>SQLITE_STATUS
4cd80 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64 74 _MALLOC_SIZE</dt
4cd90 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 >.** <dd>This pa
4cda0 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 rameter records
4cdb0 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f the largest memo
4cdc0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 ry allocation re
4cdd0 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 quest.** handed
4cde0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c to [sqlite3_mall
4cdf0 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 oc()] or [sqlite
4ce00 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28 6f 72 3_realloc()] (or
4ce10 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 65 72 6e their.** intern
4ce20 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 73 29 2e al equivalents).
4ce30 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 Only the value
4ce40 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 returned in the
4ce50 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 .** *pHighwater
4ce60 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
4ce70 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 lite3_status()]
4ce80 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 is of interest.
4ce90 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 .** The value w
4cea0 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
4ceb0 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 *pCurrent parame
4cec0 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ter is undefined
4ced0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
4cee0 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 >SQLITE_STATUS_P
4cef0 41 47 45 43 41 43 48 45 5f 55 53 45 44 3c 2f 64 AGECACHE_USED</d
4cf00 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 t>.** <dd>This p
4cf10 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 arameter returns
4cf20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
4cf30 61 67 65 73 20 75 73 65 64 20 6f 75 74 20 6f 66 ages used out of
4cf40 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 63 61 63 the.** [pagecac
4cf50 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
4cf60 74 6f 72 5d 20 74 68 61 74 20 77 61 73 20 63 6f tor] that was co
4cf70 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 20 0a nfigured using .
4cf80 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
4cf90 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 20 20 54 G_PAGECACHE]. T
4cfa0 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 65 74 75 he.** value retu
4cfb0 72 6e 65 64 20 69 73 20 69 6e 20 70 61 67 65 73 rned is in pages
4cfc0 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 73 2e 3c , not in bytes.<
4cfd0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
4cfe0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
4cff0 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 3c ECACHE_OVERFLOW<
4d000 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
4d010 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 parameter retur
4d020 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
4d030 20 62 79 74 65 73 20 6f 66 20 70 61 67 65 20 63 bytes of page c
4d040 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 ache.** allocati
4d050 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 20 6e on which could n
4d060 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 65 64 ot be statisfied
4d070 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f by the [SQLITE_
4d080 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
4d090 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64 20 ].** buffer and
4d0a0 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f 20 where forced to
4d0b0 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71 6c overflow to [sql
4d0c0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 ite3_malloc()].
4d0d0 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 The.** returned
4d0e0 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 73 20 value includes
4d0f0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 allocations that
4d100 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63 61 overflowed beca
4d110 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 68 65 72 use they.** wher
4d120 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 74 68 65 e too large (the
4d130 79 20 77 65 72 65 20 6c 61 72 67 65 72 20 74 68 y were larger th
4d140 61 6e 20 74 68 65 20 22 73 7a 22 20 70 61 72 61 an the "sz" para
4d150 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 53 51 4c meter to.** [SQL
4d160 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
4d170 41 43 48 45 5d 29 20 61 6e 64 20 61 6c 6c 6f 63 ACHE]) and alloc
4d180 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 ations that over
4d190 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 0a 2a flowed because.*
4d1a0 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 73 20 6c * no space was l
4d1b0 65 66 74 20 69 6e 20 74 68 65 20 70 61 67 65 20 eft in the page
4d1c0 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a cache.</dd>.**.*
4d1d0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 * <dt>SQLITE_STA
4d1e0 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 TUS_PAGECACHE_SI
4d1f0 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ZE</dt>.** <dd>T
4d200 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
4d210 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 cords the larges
4d220 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 t memory allocat
4d230 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 ion request.** h
4d240 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 65 63 61 anded to [pageca
4d250 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 che memory alloc
4d260 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 ator]. Only the
4d270 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
4d280 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 in the.** *pHigh
4d290 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 water parameter
4d2a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 to [sqlite3_stat
4d2b0 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 us()] is of inte
4d2c0 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 rest. .** The v
4d2d0 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 alue written int
4d2e0 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 o the *pCurrent
4d2f0 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 parameter is und
4d300 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a efined.</dd>.**.
4d310 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
4d320 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 ATUS_SCRATCH_USE
4d330 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
4d340 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 is parameter ret
4d350 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
4d360 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 75 of allocations u
4d370 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a sed out of the.*
4d380 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 * [scratch memor
4d390 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e y allocator] con
4d3a0 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a figured using.**
4d3b0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
4d3c0 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20 76 SCRATCH]. The v
4d3d0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
4d3e0 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c in allocations,
4d3f0 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 65 73 not.** in bytes
4d400 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e 67 6c . Since a singl
4d410 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c e thread may onl
4d420 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 61 74 y have one scrat
4d430 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a ch allocation.**
4d440 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 outstanding at
4d450 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 61 6d time, this param
4d460 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f 72 74 eter also report
4d470 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
4d480 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 6e 67 threads.** using
4d490 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 scratch memory
4d4a0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
4d4b0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
4d4c0 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 >SQLITE_STATUS_S
4d4d0 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 3c CRATCH_OVERFLOW<
4d4e0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
4d4f0 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 parameter retur
4d500 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
4d510 20 62 79 74 65 73 20 6f 66 20 73 63 72 61 74 63 bytes of scratc
4d520 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f h memory.** allo
4d530 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 cation which cou
4d540 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73 ld not be statis
4d550 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c fied by the [SQL
4d560 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
4d570 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e CH].** buffer an
4d580 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 d where forced t
4d590 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 o overflow to [s
4d5a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
4d5b0 2e 20 20 54 68 65 20 76 61 6c 75 65 73 0a 2a 2a . The values.**
4d5c0 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 returned includ
4d5d0 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65 63 61 e overflows beca
4d5e0 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65 use the requeste
4d5f0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 d allocation was
4d600 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 72 20 28 too.** larger (
4d610 74 68 61 74 20 69 73 2c 20 62 65 63 61 75 73 65 that is, because
4d620 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 61 the requested a
4d630 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6c 61 llocation was la
4d640 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a rger than the.**
4d650 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20 "sz" parameter
4d660 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 to [SQLITE_CONFI
4d670 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e 64 20 G_SCRATCH]) and
4d680 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72 61 74 because no scrat
4d690 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 73 6c 6f ch buffer.** slo
4d6a0 74 73 20 77 65 72 65 20 61 76 61 69 6c 61 62 6c ts were availabl
4d6b0 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a e..** </dd>.**.*
4d6c0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 * <dt>SQLITE_STA
4d6d0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 TUS_SCRATCH_SIZE
4d6e0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
4d6f0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f s parameter reco
4d700 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 rds the largest
4d710 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
4d720 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e n request.** han
4d730 64 65 64 20 74 6f 20 5b 73 63 72 61 74 63 68 20 ded to [scratch
4d740 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
4d750 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c ]. Only the val
4d760 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 ue returned in t
4d770 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 he.** *pHighwate
4d780 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b r parameter to [
4d790 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 sqlite3_status()
4d7a0 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 ] is of interest
4d7b0 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 . .** The value
4d7c0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
4d7d0 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 e *pCurrent para
4d7e0 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e meter is undefin
4d7f0 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ed.</dd>.**.** <
4d800 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dt>SQLITE_STATUS
4d810 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c 2f 64 _PARSER_STACK</d
4d820 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 t>.** <dd>This p
4d830 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 arameter records
4d840 20 74 68 65 20 64 65 65 70 65 73 74 20 70 61 72 the deepest par
4d850 73 65 72 20 73 74 61 63 6b 2e 20 20 49 74 20 69 ser stack. It i
4d860 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 6e s only.** meanin
4d870 67 66 75 6c 20 69 66 20 53 51 4c 69 74 65 20 69 gful if SQLite i
4d880 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 s compiled with
4d890 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b [YYTRACKMAXSTACK
4d8a0 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a 2a 2a 20 DEPTH].</dd>.**
4d8b0 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 </dl>.**.** New
4d8c0 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 status parameter
4d8d0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 66 s may be added f
4d8e0 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d 65 rom time to time
4d8f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
4d900 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
4d910 59 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 20 Y_USED
4d920 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
4d930 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
4d940 45 5f 55 53 45 44 20 20 20 20 20 20 20 31 0a 23 E_USED 1.#
4d950 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
4d960 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f ATUS_PAGECACHE_O
4d970 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 65 66 VERFLOW 2.#def
4d980 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
4d990 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 20 20 S_SCRATCH_USED
4d9a0 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 3.#define
4d9b0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 SQLITE_STATUS_S
4d9c0 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20 CRATCH_OVERFLOW
4d9d0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4.#define SQ
4d9e0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c LITE_STATUS_MALL
4d9f0 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 OC_SIZE
4da00 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 5.#define SQLIT
4da10 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f E_STATUS_PARSER_
4da20 53 54 41 43 4b 20 20 20 20 20 20 20 20 20 36 0a STACK 6.
4da30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
4da40 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
4da50 53 49 5a 45 20 20 20 20 20 20 20 37 0a 23 64 65 SIZE 7.#de
4da60 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
4da70 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 20 US_SCRATCH_SIZE
4da80 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a 0a 2a 2a 8../*.**
4da90 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62 CAPI3REF: Datab
4daa0 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 53 ase Connection S
4dab0 74 61 74 75 73 20 7b 48 31 37 35 30 30 7d 20 3c tatus {H17500} <
4dac0 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S60200>.** EXPER
4dad0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
4dae0 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 is interface is
4daf0 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 used to retrieve
4db00 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 runtime status
4db10 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20 information .**
4db20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20 5b about a single [
4db30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
4db40 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72 73 74 ion]. The first
4db50 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
4db60 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
4db70 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 20 74 nection object t
4db80 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 o be interrogate
4db90 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 d. The second a
4dba0 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68 rgument.** is th
4dbb0 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 69 e parameter to i
4dbc0 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 43 75 72 nterrogate. Cur
4dbd0 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 rently, the only
4dbe0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 0a 2a allowed value.*
4dbf0 2a 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 * for the second
4dc00 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 5b 53 parameter is [S
4dc10 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c QLITE_DBSTATUS_L
4dc20 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d 2e 0a OOKASIDE_USED]..
4dc30 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6f 70 ** Additional op
4dc40 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c tions will likel
4dc50 79 20 61 70 70 65 61 72 20 69 6e 20 66 75 74 75 y appear in futu
4dc60 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 re releases of S
4dc70 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 QLite..**.** The
4dc80 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f current value o
4dc90 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
4dca0 70 61 72 61 6d 65 74 65 72 20 69 73 20 77 72 69 parameter is wri
4dcb0 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 0a tten into *pCur.
4dcc0 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67 68 65 ** and the highe
4dcd0 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 st instantaneous
4dce0 20 76 61 6c 75 65 20 69 73 20 77 72 69 74 74 65 value is writte
4dcf0 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72 2e 20 n into *pHiwtr.
4dd00 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 73 65 74 If.** the reset
4dd10 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 Flg is true, the
4dd20 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 69 6e n the highest in
4dd30 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 stantaneous valu
4dd40 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 20 62 61 e is.** reset ba
4dd50 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 63 ck down to the c
4dd60 75 72 72 65 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a urrent value..**
4dd70 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
4dd80 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d qlite3_status()]
4dd90 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 and [sqlite3_st
4dda0 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2f mt_status()]..*/
4ddb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
4ddc0 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
4ddd0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 int sqlite3_db_s
4dde0 74 61 74 75 73 28 73 71 6c 69 74 65 33 2a 2c 20 tatus(sqlite3*,
4ddf0 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 int op, int *pCu
4de00 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 72 2c 20 r, int *pHiwtr,
4de10 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a int resetFlg);..
4de20 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
4de30 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 Status Parameter
4de40 73 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 63 s for database c
4de50 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 31 37 35 onnections {H175
4de60 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 2a 2a 20 20} <H17500>.**
4de70 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
4de80 2a 2a 20 53 74 61 74 75 73 20 76 65 72 62 73 20 ** Status verbs
4de90 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f for [sqlite3_db_
4dea0 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a status()]..**.**
4deb0 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c <dl>.** <dt>SQL
4dec0 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f ITE_DBSTATUS_LOO
4ded0 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e KASIDE_USED</dt>
4dee0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 .** <dd>This par
4def0 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 ameter returns t
4df00 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f he number of loo
4df10 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c kaside memory sl
4df20 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a ots currently.**
4df30 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 checked out.</d
4df40 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 d>.** </dl>.*/.#
4df50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 define SQLITE_DB
4df60 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 STATUS_LOOKASIDE
4df70 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f 2a _USED 0.../*
4df80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 72 .** CAPI3REF: Pr
4df90 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 epared Statement
4dfa0 20 53 74 61 74 75 73 20 7b 48 31 37 35 35 30 7d Status {H17550}
4dfb0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 <S60200>.** EXP
4dfc0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
4dfd0 45 61 63 68 20 70 72 65 70 61 72 65 64 20 73 74 Each prepared st
4dfe0 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69 6e atement maintain
4dff0 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53 51 s various.** [SQ
4e000 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f LITE_STMTSTATUS_
4e010 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73 5d SORT | counters]
4e020 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68 that measure th
4e030 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 e number.** of t
4e040 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66 imes it has perf
4e050 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f ormed specific o
4e060 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65 73 perations. Thes
4e070 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a 2a e counters can.*
4e080 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f 6e * be used to mon
4e090 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72 6d itor the perform
4e0a0 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 69 73 ance characteris
4e0b0 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 65 70 tics of the prep
4e0c0 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e ared.** statemen
4e0d0 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 ts. For example
4e0e0 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 , if the number
4e0f0 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 20 67 of table steps g
4e100 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a 2a reatly exceeds.*
4e110 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
4e120 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 6f table searches o
4e130 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20 74 r result rows, t
4e140 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20 74 hat would tend t
4e150 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 o indicate.** th
4e160 61 74 20 74 68 65 20 70 72 65 70 61 72 65 64 20 at the prepared
4e170 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73 69 statement is usi
4e180 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 ng a full table
4e190 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 61 6e scan rather than
4e1a0 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 0a .** an index. .
4e1b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
4e1c0 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 face is used to
4e1d0 72 65 74 72 69 65 76 65 20 61 6e 64 20 72 65 73 retrieve and res
4e1e0 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 et counter value
4e1f0 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72 65 s from.** a [pre
4e200 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
4e210 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 . The first arg
4e220 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72 65 ument is the pre
4e230 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a pared statement.
4e240 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 ** object to be
4e250 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54 interrogated. T
4e260 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
4e270 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 nt.** is an inte
4e280 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 73 ger code for a s
4e290 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 5f pecific [SQLITE_
4e2a0 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 STMTSTATUS_SORT
4e2b0 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f | counter].** to
4e2c0 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 be interrogated
4e2d0 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e . .** The curren
4e2e0 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 t value of the r
4e2f0 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72 equested counter
4e300 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
4e310 20 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67 If the resetFlg
4e320 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
4e330 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 he counter is re
4e340 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 set to zero afte
4e350 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 r this.** interf
4e360 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 ace call returns
4e370 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
4e380 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 : [sqlite3_statu
4e390 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 s()] and [sqlite
4e3a0 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a 3_db_status()]..
4e3b0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
4e3c0 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
4e3d0 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 L int sqlite3_st
4e3e0 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 mt_status(sqlite
4e3f0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c 3_stmt*, int op,
4e400 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a int resetFlg);..
4e410 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
4e420 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 Status Parameter
4e430 73 20 66 6f 72 20 70 72 65 70 61 72 65 64 20 73 s for prepared s
4e440 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35 37 tatements {H1757
4e450 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20 45 0} <H17550>.** E
4e460 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
4e470 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65 * These preproce
4e480 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69 ssor macros defi
4e490 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 ne integer codes
4e4a0 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74 that name count
4e4b0 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73 er.** values ass
4e4c0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
4e4d0 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 [sqlite3_stmt_s
4e4e0 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 tatus()] interfa
4e4f0 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69 ce..** The meani
4e500 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f ngs of the vario
4e510 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20 us counters are
4e520 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
4e530 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
4e540 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f LITE_STMTSTATUS_
4e550 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f 64 FULLSCAN_STEP</d
4e560 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 t>.** <dd>This i
4e570 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
4e580 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69 74 times that SQLit
4e590 65 20 68 61 73 20 73 74 65 70 70 65 64 20 66 6f e has stepped fo
4e5a0 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74 61 rward in.** a ta
4e5b0 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20 61 ble as part of a
4e5c0 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e full table scan
4e5d0 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72 73 . Large numbers
4e5e0 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74 65 for this counte
4e5f0 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61 74 r.** may indicat
4e600 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 20 e opportunities
4e610 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 for performance
4e620 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72 6f improvement thro
4e630 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c 20 ugh .** careful
4e640 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c use of indices.<
4e650 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
4e660 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
4e670 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 _SORT</dt>.** <d
4e680 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 d>This is the nu
4e690 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 65 mber of sort ope
4e6a0 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 rations that hav
4e6b0 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 41 e occurred..** A
4e6c0 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 non-zero value
4e6d0 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 in this counter
4e6e0 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e 20 may indicate an
4e6f0 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a 2a opportunity to.*
4e700 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 65 * improvement pe
4e710 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 67 rformance throug
4e720 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66 h careful use of
4e730 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a indices.</dd>.*
4e740 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 *.** </dl>.*/.#d
4e750 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d efine SQLITE_STM
4e760 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e TSTATUS_FULLSCAN
4e770 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64 65 66 _STEP 1.#def
4e780 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 ine SQLITE_STMTS
4e790 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20 20 TATUS_SORT
4e7a0 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 2../*.**
4e7b0 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74 Undo the hack t
4e7c0 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f hat converts flo
4e7d0 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65 ating point type
4e7e0 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72 s to integer for
4e7f0 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 72 .** builds on pr
4e800 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75 74 ocessors without
4e810 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
4e820 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 support..*/.#ifd
4e830 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
4e840 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 LOATING_POINT.#
4e850 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 6e undef double.#en
4e860 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a dif..#if 0.} /*
4e870 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78 74 End of the 'ext
4e880 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 2a ern "C"' block *
4e890 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a /.#endif.#endif.
4e8a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4e8b0 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e End of sqlite3.
4e8c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4e8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4e8f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4e900 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
4e910 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
4e920 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
4e930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4e940 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4e950 20 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 20 Include hash.h
4e960 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
4e970 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
4e980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4e990 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4e9a0 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 Begin file hash
4e9b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
4e9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4e9e0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
4e9f0 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 ember 22.**.** T
4ea00 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
4ea10 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
4ea20 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
4ea30 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
4ea40 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
4ea50 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
4ea60 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
4ea70 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
4ea80 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
4ea90 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
4eaa0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
4eab0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
4eac0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
4ead0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
4eae0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
4eaf0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
4eb00 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
4eb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4eb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4eb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4eb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4eb50 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
4eb60 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 is the header fi
4eb70 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 le for the gener
4eb80 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d ic hash-table im
4eb90 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 plemenation.** u
4eba0 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a sed in SQLite..*
4ebb0 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 68 *.** $Id: hash.h
4ebc0 2c 76 20 31 2e 31 32 20 32 30 30 38 2f 31 30 2f ,v 1.12 2008/10/
4ebd0 31 30 20 31 37 3a 34 31 3a 32 39 20 64 72 68 20 10 17:41:29 drh
4ebe0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
4ebf0 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f _SQLITE_HASH_H_
4ec00 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 .#define _SQLITE
4ec10 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 _HASH_H_../* For
4ec20 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e ward declaration
4ec30 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e s of structures.
4ec40 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 */.typedef stru
4ec50 63 74 20 48 61 73 68 20 48 61 73 68 3b 0a 74 79 ct Hash Hash;.ty
4ec60 70 65 64 65 66 20 73 74 72 75 63 74 20 48 61 73 pedef struct Has
4ec70 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d 3b 0a hElem HashElem;.
4ec80 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 68 ./* A complete h
4ec90 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 ash table is an
4eca0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
4ecb0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
4ecc0 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ure..** The inte
4ecd0 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 rnals of this st
4ece0 72 75 63 74 75 72 65 20 61 72 65 20 69 6e 74 65 ructure are inte
4ecf0 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 nded to be opaqu
4ed00 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 e -- client.** c
4ed10 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 ode should not a
4ed20 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73 73 ttempt to access
4ed30 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20 66 or modify the f
4ed40 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 ields of this st
4ed50 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65 63 ructure.** direc
4ed60 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68 69 tly. Change thi
4ed70 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 s structure only
4ed80 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 72 6f by using the ro
4ed90 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a utines below..**
4eda0 20 48 6f 77 65 76 65 72 2c 20 6d 61 6e 79 20 6f However, many o
4edb0 66 20 74 68 65 20 22 70 72 6f 63 65 64 75 72 65 f the "procedure
4edc0 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e s" and "function
4edd0 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67 s" for modifying
4ede0 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e and.** accessin
4edf0 67 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 g this structure
4ee00 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63 72 are really macr
4ee10 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74 20 os, so we can't
4ee20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 really make.** t
4ee30 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 70 his structure op
4ee40 61 71 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 aque..*/.struct
4ee50 48 61 73 68 20 7b 0a 20 20 75 6e 73 69 67 6e 65 Hash {. unsigne
4ee60 64 20 69 6e 74 20 63 6f 70 79 4b 65 79 3a 20 31 d int copyKey: 1
4ee70 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f ; /* True if co
4ee80 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65 20 6f py of key made o
4ee90 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 75 6e n insert */. un
4eea0 73 69 67 6e 65 64 20 69 6e 74 20 68 74 73 69 7a signed int htsiz
4eeb0 65 20 3a 20 33 31 3b 20 2f 2a 20 4e 75 6d 62 65 e : 31; /* Numbe
4eec0 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 r of buckets in
4eed0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a the hash table *
4eee0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
4eef0 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a count; /*
4ef00 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
4ef10 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 es in this table
4ef20 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
4ef30 66 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 first;
4ef40 2f 2a 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 /* The first ele
4ef50 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 ment of the arra
4ef60 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 y */. struct _h
4ef70 74 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 t {
4ef80 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 /* the hash tab
4ef90 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f le */. int co
4efa0 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 unt;
4efb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
4efc0 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 f entries with t
4efd0 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 his hash */.
4efe0 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b HashElem *chain;
4eff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
4f000 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 65 inter to first e
4f010 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20 68 ntry with this h
4f020 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a ash */. } *ht;.
4f030 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d };../* Each elem
4f040 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68 20 ent in the hash
4f050 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 table is an inst
4f060 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
4f070 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 owing .** struct
4f080 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e ure. All elemen
4f090 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e ts are stored on
4f0a0 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 a single doubly
4f0b0 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a -linked list..**
4f0c0 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20 .** Again, this
4f0d0 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 74 structure is int
4f0e0 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 ended to be opaq
4f0f0 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74 ue, but it can't
4f100 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 really.** be op
4f110 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74 20 aque because it
4f120 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f is used by macro
4f130 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 s..*/.struct Has
4f140 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c hElem {. HashEl
4f150 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b em *next, *prev;
4f160 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20 70 /* Next and p
4f170 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 73 revious elements
4f180 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a 2f in the table */
4f190 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 20 20 . void *data;
4f1a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
4f1b0 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
4f1c0 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 ith this element
4f1d0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 */. void *pKey
4f1e0 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 2f ; int nKey; /
4f1f0 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64 * Key associated
4f200 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 with this eleme
4f210 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 nt */.};../*.**
4f220 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e Access routines.
4f230 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 To delete, ins
4f240 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ert a NULL point
4f250 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
4f260 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
4f270 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 2a e3HashInit(Hash*
4f280 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 3b 0a , int copyKey);.
4f290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4f2a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 oid *sqlite3Hash
4f2b0 49 6e 73 65 72 74 28 48 61 73 68 2a 2c 20 63 6f Insert(Hash*, co
4f2c0 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
4f2d0 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a int nKey, void *
4f2e0 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50 pData);.SQLITE_P
4f2f0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
4f300 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e ite3HashFind(con
4f310 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 st Hash*, const
4f320 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 void *pKey, int
4f330 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nKey);.SQLITE_PR
4f340 49 56 41 54 45 20 48 61 73 68 45 6c 65 6d 20 2a IVATE HashElem *
4f350 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 45 sqlite3HashFindE
4f360 6c 65 6d 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c lem(const Hash*,
4f370 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
4f380 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 y, int nKey);.SQ
4f390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4f3a0 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 d sqlite3HashCle
4f3b0 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a ar(Hash*);../*.*
4f3c0 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f * Macros for loo
4f3d0 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c ping over all el
4f3e0 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68 ements of a hash
4f3f0 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69 table. The idi
4f400 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 om is.** like th
4f410 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 is:.**.** Hash
4f420 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65 h;.** HashEle
4f430 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a m *p;.** ....*
4f440 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 * for(p=sqlite
4f450 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70 HashFirst(&h); p
4f460 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 ; p=sqliteHashNe
4f470 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 xt(p)){.** S
4f480 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44 omeStructure *pD
4f490 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68 ata = sqliteHash
4f4a0 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20 Data(p);.**
4f4b0 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 // do something
4f4c0 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20 with pData.**
4f4d0 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c }.*/.#define sql
4f4e0 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29 20 iteHashFirst(H)
4f4f0 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64 ((H)->first).#d
4f500 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 efine sqliteHash
4f510 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e Next(E) ((E)->
4f520 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71 next).#define sq
4f530 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29 20 liteHashData(E)
4f540 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23 64 ((E)->data).#d
4f550 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 efine sqliteHash
4f560 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e Key(E) ((E)->
4f570 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 73 71 pKey).#define sq
4f580 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65 28 liteHashKeysize(
4f590 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a E) ((E)->nKey)..
4f5a0 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /*.** Number of
4f5b0 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61 73 entries in a has
4f5c0 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 h table.*/.#defi
4f5d0 6e 65 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75 ne sqliteHashCou
4f5e0 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 nt(H) ((H)->cou
4f5f0 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f nt)..#endif /* _
4f600 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a SQLITE_HASH_H_ *
4f610 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
4f620 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 68 ** End of hash.h
4f630 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
4f640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f660 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4f670 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
4f680 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
4f690 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a in sqliteInt.h *
4f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f6b0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4f6c0 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 73 65 ** Include parse
4f6d0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
4f6e0 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
4f6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f700 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4f710 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 ** Begin file pa
4f720 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a rse.h **********
4f730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f750 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 */.#define TK_SE
4f760 4d 49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MI
4f770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 1.
4f780 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41 #define TK_EXPLA
4f790 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IN
4f7a0 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 2.#de
4f7b0 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20 20 fine TK_QUERY
4f7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f7d0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 3.#defin
4f7e0 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20 e TK_PLAN
4f7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f800 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 54 4.#define T
4f810 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20 20 K_BEGIN
4f820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f830 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 5.#define TK_T
4f840 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20 RANSACTION
4f850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
4f860 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 .#define TK_DEFE
4f870 52 52 45 44 20 20 20 20 20 20 20 20 20 20 20 20 RRED
4f880 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 7.#d
4f890 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41 efine TK_IMMEDIA
4f8a0 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TE
4f8b0 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 8.#defi
4f8c0 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 ne TK_EXCLUSIVE
4f8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f8e0 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 9.#define
4f8f0 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 20 20 TK_COMMIT
4f900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f910 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 10.#define TK_
4f920 45 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20 END
4f930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4f940 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 1.#define TK_ROL
4f950 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 20 20 LBACK
4f960 20 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 23 12.#
4f970 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54 45 define TK_CREATE
4f980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f990 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 13.#def
4f9a0 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20 20 ine TK_TABLE
4f9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f9c0 20 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 14.#define
4f9d0 20 54 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20 TK_IF
4f9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f9f0 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 15.#define TK
4fa00 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 _NOT
4fa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 16.#define TK_EX
4fa30 49 53 54 53 20 20 20 20 20 20 20 20 20 20 20 20 ISTS
4fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 17.
4fa50 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20 #define TK_TEMP
4fa60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa70 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 18.#de
4fa80 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20 fine TK_LP
4fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4faa0 20 20 20 20 20 20 20 31 39 0a 23 64 65 66 69 6e 19.#defin
4fab0 65 20 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20 e TK_RP
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fad0 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 54 20.#define T
4fae0 4b 5f 41 53 20 20 20 20 20 20 20 20 20 20 20 20 K_AS
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb00 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 21.#define TK_C
4fb10 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 OMMA
4fb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 32 22
4fb30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20 .#define TK_ID
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb50 20 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 23.#d
4fb60 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20 efine TK_ABORT
4fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb80 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 69 24.#defi
4fb90 6e 65 20 54 4b 5f 41 46 54 45 52 20 20 20 20 20 ne TK_AFTER
4fba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbb0 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 25.#define
4fbc0 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 TK_ANALYZE
4fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbe0 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 26.#define TK_
4fbf0 41 53 43 20 20 20 20 20 20 20 20 20 20 20 20 20 ASC
4fc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
4fc10 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 7.#define TK_ATT
4fc20 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 ACH
4fc30 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23 28.#
4fc40 64 65 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 define TK_BEFORE
4fc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc60 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 29.#def
4fc70 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20 ine TK_CASCADE
4fc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc90 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 65 30.#define
4fca0 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20 TK_CAST
4fcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcc0 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 31.#define TK
4fcd0 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 20 20 20 _CONFLICT
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcf0 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 32.#define TK_DA
4fd00 54 41 42 41 53 45 20 20 20 20 20 20 20 20 20 20 TABASE
4fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a 33.
4fd20 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53 43 20 #define TK_DESC
4fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd40 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 64 65 34.#de
4fd50 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48 20 20 fine TK_DETACH
4fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd70 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 35.#defin
4fd80 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20 20 20 e TK_EACH
4fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fda0 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 54 36.#define T
4fdb0 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 20 20 20 K_FAIL
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdd0 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 37.#define TK_F
4fde0 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OR
4fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 38
4fe00 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f .#define TK_IGNO
4fe10 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 RE
4fe20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 39.#d
4fe30 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c efine TK_INITIAL
4fe40 4c 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LY
4fe50 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 66 69 40.#defi
4fe60 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 20 20 20 ne TK_INSTEAD
4fe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe80 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 41.#define
4fe90 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20 TK_LIKE_KW
4fea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4feb0 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42.#define TK_
4fec0 4d 41 54 43 48 20 20 20 20 20 20 20 20 20 20 20 MATCH
4fed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
4fee0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59 3.#define TK_KEY
4fef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23 44.#
4ff10 64 65 66 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20 define TK_OF
4ff20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff30 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 45.#def
4ff40 69 6e 65 20 54 4b 5f 4f 46 46 53 45 54 20 20 20 ine TK_OFFSET
4ff50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff60 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65 46.#define
4ff70 20 54 4b 5f 50 52 41 47 4d 41 20 20 20 20 20 20 TK_PRAGMA
4ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff90 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 47.#define TK
4ffa0 5f 52 41 49 53 45 20 20 20 20 20 20 20 20 20 20 _RAISE
4ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffc0 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 48.#define TK_RE
4ffd0 50 4c 41 43 45 20 20 20 20 20 20 20 20 20 20 20 PLACE
4ffe0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a 49.
4fff0 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 53 54 52 #define TK_RESTR
50000 49 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 ICT
50010 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64 65 50.#de
50020 66 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20 20 fine TK_ROW
50030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50040 20 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 51.#defin
50050 65 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20 20 e TK_TRIGGER
50060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50070 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 54 52.#define T
50080 4b 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20 20 K_VACUUM
50090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500a0 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 53.#define TK_V
500b0 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20 20 IEW
500c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34 54
500d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 52 54 .#define TK_VIRT
500e0 55 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 UAL
500f0 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 55.#d
50100 65 66 69 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58 efine TK_REINDEX
50110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50120 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 56.#defi
50130 6e 65 20 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20 ne TK_RENAME
50140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50150 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20 57.#define
50160 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 20 20 20 TK_CTIME_KW
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50180 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 58.#define TK_
50190 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 20 ANY
501a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
501b0 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20 9.#define TK_OR
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501d0 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23 60.#
501e0 64 65 66 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20 define TK_AND
501f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50200 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 66 61.#def
50210 69 6e 65 20 54 4b 5f 49 53 20 20 20 20 20 20 20 ine TK_IS
50220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50230 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 65 62.#define
50240 20 54 4b 5f 42 45 54 57 45 45 4e 20 20 20 20 20 TK_BETWEEN
50250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50260 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 54 4b 63.#define TK
50270 5f 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 _IN
50280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50290 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 64.#define TK_IS
502a0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
502b0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a 65.
502c0 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 #define TK_NOTNU
502d0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LL
502e0 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 64 65 66.#de
502f0 66 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20 20 fine TK_NE
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50310 20 20 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e 67.#defin
50320 65 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 e TK_EQ
50330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50340 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20 54 68.#define T
50350 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20 K_GT
50360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50370 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 69.#define TK_L
50380 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30 70
503a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20 .#define TK_LT
503b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503c0 20 20 20 20 20 20 20 20 20 20 20 37 31 0a 23 64 71.#d
503d0 65 66 69 6e 65 20 54 4b 5f 47 45 20 20 20 20 20 efine TK_GE
503e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503f0 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69 72.#defi
50400 6e 65 20 54 4b 5f 45 53 43 41 50 45 20 20 20 20 ne TK_ESCAPE
50410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50420 20 20 20 20 20 37 33 0a 23 64 65 66 69 6e 65 20 73.#define
50430 54 4b 5f 42 49 54 41 4e 44 20 20 20 20 20 20 20 TK_BITAND
50440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50450 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 74.#define TK_
50460 42 49 54 4f 52 20 20 20 20 20 20 20 20 20 20 20 BITOR
50470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
50480 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48 5.#define TK_LSH
50490 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20 IFT
504a0 20 20 20 20 20 20 20 20 20 20 20 20 37 36 0a 23 76.#
504b0 64 65 66 69 6e 65 20 54 4b 5f 52 53 48 49 46 54 define TK_RSHIFT
504c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504d0 20 20 20 20 20 20 20 20 20 37 37 0a 23 64 65 66 77.#def
504e0 69 6e 65 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 ine TK_PLUS
504f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50500 20 20 20 20 20 20 37 38 0a 23 64 65 66 69 6e 65 78.#define
50510 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 20 20 20 TK_MINUS
50520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50530 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20 54 4b 79.#define TK
50540 5f 53 54 41 52 20 20 20 20 20 20 20 20 20 20 20 _STAR
50550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50560 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c 80.#define TK_SL
50570 41 53 48 20 20 20 20 20 20 20 20 20 20 20 20 20 ASH
50580 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31 0a 81.
50590 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20 #define TK_REM
505a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505b0 20 20 20 20 20 20 20 20 20 20 38 32 0a 23 64 65 82.#de
505c0 66 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20 fine TK_CONCAT
505d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505e0 20 20 20 20 20 20 20 38 33 0a 23 64 65 66 69 6e 83.#defin
505f0 65 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20 e TK_COLLATE
50600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50610 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20 54 84.#define T
50620 4b 5f 55 4d 49 4e 55 53 20 20 20 20 20 20 20 20 K_UMINUS
50630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50640 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 85.#define TK_U
50650 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 20 PLUS
50660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36 86
50670 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4e .#define TK_BITN
50680 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OT
50690 20 20 20 20 20 20 20 20 20 20 20 38 37 0a 23 64 87.#d
506a0 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47 20 efine TK_STRING
506b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506c0 20 20 20 20 20 20 20 20 38 38 0a 23 64 65 66 69 88.#defi
506d0 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20 ne TK_JOIN_KW
506e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506f0 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 20 89.#define
50700 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 20 TK_CONSTRAINT
50710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50720 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 90.#define TK_
50730 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 DEFAULT
50740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
50750 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55 4c 1.#define TK_NUL
50760 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 L
50770 20 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 92.#
50780 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52 define TK_PRIMAR
50790 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Y
507a0 20 20 20 20 20 20 20 20 20 39 33 0a 23 64 65 66 93.#def
507b0 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20 20 ine TK_UNIQUE
507c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507d0 20 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e 65 94.#define
507e0 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 20 20 TK_CHECK
507f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50800 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 54 4b 95.#define TK
50810 5f 52 45 46 45 52 45 4e 43 45 53 20 20 20 20 20 _REFERENCES
50820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50830 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55 96.#define TK_AU
50840 54 4f 49 4e 43 52 20 20 20 20 20 20 20 20 20 20 TOINCR
50850 20 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a 97.
50860 23 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 #define TK_ON
50870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50880 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 98.#de
50890 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20 fine TK_DELETE
508a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508b0 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69 6e 99.#defin
508c0 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20 20 e TK_UPDATE
508d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508e0 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20 100.#define
508f0 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 TK_INSERT
50900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50910 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 54 4b 101.#define TK
50920 5f 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20 _SET
50930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50940 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 102.#define TK_D
50950 45 46 45 52 52 41 42 4c 45 20 20 20 20 20 20 20 EFERRABLE
50960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
50970 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 3.#define TK_FOR
50980 45 49 47 4e 20 20 20 20 20 20 20 20 20 20 20 20 EIGN
50990 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34 0a 104.
509a0 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20 #define TK_DROP
509b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509c0 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 105.#d
509d0 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 efine TK_UNION
509e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509f0 20 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 106.#def
50a00 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20 ine TK_ALL
50a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 69 6e 107.#defin
50a30 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20 20 20 e TK_EXCEPT
50a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a50 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20 108.#define
50a60 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 20 20 20 TK_INTERSECT
50a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a80 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 54 4b 109.#define TK
50a90 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 _SELECT
50aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ab0 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 110.#define TK_D
50ac0 49 53 54 49 4e 43 54 20 20 20 20 20 20 20 20 20 ISTINCT
50ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
50ae0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54 1.#define TK_DOT
50af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b00 20 20 20 20 20 20 20 20 20 20 20 20 31 31 32 0a 112.
50b10 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 #define TK_FROM
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b30 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64 113.#d
50b40 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 efine TK_JOIN
50b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b60 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 66 114.#def
50b70 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20 ine TK_INDEXED
50b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b90 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e 115.#defin
50ba0 65 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 e TK_BY
50bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bc0 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65 20 116.#define
50bd0 54 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20 20 TK_USING
50be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bf0 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 117.#define TK
50c00 5f 4f 52 44 45 52 20 20 20 20 20 20 20 20 20 20 _ORDER
50c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 118.#define TK_G
50c30 52 4f 55 50 20 20 20 20 20 20 20 20 20 20 20 20 ROUP
50c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
50c50 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41 56 9.#define TK_HAV
50c60 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 ING
50c70 20 20 20 20 20 20 20 20 20 20 20 20 31 32 30 0a 120.
50c80 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 #define TK_LIMIT
50c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ca0 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 23 64 121.#d
50cb0 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20 20 efine TK_WHERE
50cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cd0 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 122.#def
50ce0 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20 20 ine TK_INTO
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d00 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 123.#defin
50d10 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20 20 e TK_VALUES
50d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d30 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20 124.#define
50d40 54 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20 20 TK_INTEGER
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d60 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 125.#define TK
50d70 5f 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20 20 _FLOAT
50d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d90 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 126.#define TK_B
50da0 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 20 LOB
50db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
50dc0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 47 7.#define TK_REG
50dd0 49 53 54 45 52 20 20 20 20 20 20 20 20 20 20 20 ISTER
50de0 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 128.
50df0 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49 41 #define TK_VARIA
50e00 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 BLE
50e10 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 23 64 129.#d
50e20 65 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20 20 efine TK_CASE
50e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e40 20 20 20 20 20 20 20 20 31 33 30 0a 23 64 65 66 130.#def
50e50 69 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20 20 ine TK_WHEN
50e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e70 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e 131.#defin
50e80 65 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20 20 e TK_THEN
50e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ea0 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 132.#define
50eb0 54 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20 20 TK_ELSE
50ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ed0 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 133.#define TK
50ee0 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 _INDEX
50ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f00 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 134.#define TK_A
50f10 4c 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 LTER
50f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
50f30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20 5.#define TK_TO
50f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f50 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 136.
50f60 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 44 20 20 #define TK_ADD
50f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f80 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 23 64 137.#d
50f90 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b efine TK_COLUMNK
50fa0 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 W
50fb0 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 138.#def
50fc0 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 ine TK_TO_TEXT
50fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fe0 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69 6e 139.#defin
50ff0 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 e TK_TO_BLOB
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51010 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65 20 140.#define
51020 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20 TK_TO_NUMERIC
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51040 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 141.#define TK
51050 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20 20 20 20 _TO_INT
51060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51070 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 142.#define TK_T
51080 4f 5f 52 45 41 4c 20 20 20 20 20 20 20 20 20 20 O_REAL
51090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
510a0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 3.#define TK_END
510b0 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20 20 20 _OF_FILE
510c0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 34 0a 144.
510d0 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 #define TK_ILLEG
510e0 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AL
510f0 20 20 20 20 20 20 20 20 20 20 31 34 35 0a 23 64 145.#d
51100 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45 20 20 efine TK_SPACE
51110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51120 20 20 20 20 20 20 20 20 31 34 36 0a 23 64 65 66 146.#def
51130 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f ine TK_UNCLOSED_
51140 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 20 STRING
51150 20 20 20 20 20 20 31 34 37 0a 23 64 65 66 69 6e 147.#defin
51160 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 20 e TK_FUNCTION
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51180 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e 65 20 148.#define
51190 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 TK_COLUMN
511a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511b0 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 149.#define TK
511c0 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20 20 _AGG_FUNCTION
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511e0 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 150.#define TK_A
511f0 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 GG_COLUMN
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 15
51210 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 1.#define TK_CON
51220 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20 20 ST_FUNC
51230 20 20 20 20 20 20 20 20 20 20 20 20 31 35 32 0a 152.
51240 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
51250 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 68 20 End of parse.h
51260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
51290 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
512a0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
512b0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
512c0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
512d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
512e0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f .#include <stdio
512f0 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 .h>.#include <st
51300 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 dlib.h>.#include
51310 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 <string.h>.#inc
51320 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a lude <assert.h>.
51330 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 64 65 66 #include <stddef
51340 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f .h>../*.** If co
51350 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 mpiling for a pr
51360 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63 ocessor that lac
51370 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e ks floating poin
51380 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 t support,.** su
51390 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72 bstitute integer
513a0 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f for floating-po
513b0 69 6e 74 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 int.*/.#ifdef SQ
513c0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
513d0 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e NG_POINT.# defin
513e0 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f e double sqlite_
513f0 69 6e 74 36 34 0a 23 20 64 65 66 69 6e 65 20 4c int64.# define L
51400 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 ONGDOUBLE_TYPE s
51410 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 69 66 qlite_int64.# if
51420 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 5f ndef SQLITE_BIG_
51430 44 42 4c 0a 23 20 20 20 64 65 66 69 6e 65 20 53 DBL.# define S
51440 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 30 QLITE_BIG_DBL (0
51450 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 x7ffffffffffffff
51460 66 29 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 f).# endif.# def
51470 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ine SQLITE_OMIT_
51480 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 DATETIME_FUNCS 1
51490 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
514a0 5f 4f 4d 49 54 5f 54 52 41 43 45 20 31 0a 23 20 _OMIT_TRACE 1.#
514b0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 undef SQLITE_MIX
514c0 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f ED_ENDIAN_64BIT_
514d0 46 4c 4f 41 54 0a 23 65 6e 64 69 66 0a 23 69 66 FLOAT.#endif.#if
514e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 5f ndef SQLITE_BIG_
514f0 44 42 4c 0a 23 20 64 65 66 69 6e 65 20 53 51 4c DBL.# define SQL
51500 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 31 65 39 ITE_BIG_DBL (1e9
51510 39 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 9).#endif../*.**
51520 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 20 OMIT_TEMPDB is
51530 73 65 74 20 74 6f 20 31 20 69 66 20 53 51 4c 49 set to 1 if SQLI
51540 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 TE_OMIT_TEMPDB i
51550 73 20 64 65 66 69 6e 65 64 2c 20 6f 72 20 30 0a s defined, or 0.
51560 2a 2a 20 61 66 74 65 72 77 61 72 64 2e 20 48 61 ** afterward. Ha
51570 76 69 6e 67 20 74 68 69 73 20 6d 61 63 72 6f 20 ving this macro
51580 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 63 61 75 allows us to cau
51590 73 65 20 74 68 65 20 43 20 63 6f 6d 70 69 6c 65 se the C compile
515a0 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69 74 20 63 6f r .** to omit co
515b0 64 65 20 75 73 65 64 20 62 79 20 54 45 4d 50 20 de used by TEMP
515c0 74 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20 6d tables without m
515d0 65 73 73 79 20 23 69 66 6e 64 65 66 20 73 74 61 essy #ifndef sta
515e0 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66 64 tements..*/.#ifd
515f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
51600 45 4d 50 44 42 0a 23 64 65 66 69 6e 65 20 4f 4d EMPDB.#define OM
51610 49 54 5f 54 45 4d 50 44 42 20 31 0a 23 65 6c 73 IT_TEMPDB 1.#els
51620 65 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 e.#define OMIT_T
51630 45 4d 50 44 42 20 30 0a 23 65 6e 64 69 66 0a 0a EMPDB 0.#endif..
51640 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c /*.** If the fol
51650 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 lowing macro is
51660 73 65 74 20 74 6f 20 31 2c 20 74 68 65 6e 20 4e set to 1, then N
51670 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 ULL values are c
51680 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 64 69 73 onsidered.** dis
51690 74 69 6e 63 74 20 77 68 65 6e 20 64 65 74 65 72 tinct when deter
516a0 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 6f mining whether o
516b0 72 20 6e 6f 74 20 74 77 6f 20 65 6e 74 72 69 65 r not two entrie
516c0 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 0a 2a s are the same.*
516d0 2a 20 69 6e 20 61 20 55 4e 49 51 55 45 20 69 6e * in a UNIQUE in
516e0 64 65 78 2e 20 20 54 68 69 73 20 69 73 20 74 68 dex. This is th
516f0 65 20 77 61 79 20 50 6f 73 74 67 72 65 53 51 4c e way PostgreSQL
51700 2c 20 4f 72 61 63 6c 65 2c 20 44 42 32 2c 20 4d , Oracle, DB2, M
51710 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 2c ySQL,.** OCELOT,
51720 20 61 6e 64 20 46 69 72 65 62 69 72 64 20 61 6c and Firebird al
51730 6c 20 77 6f 72 6b 2e 20 20 54 68 65 20 53 51 4c l work. The SQL
51740 39 32 20 73 70 65 63 20 65 78 70 6c 69 63 69 74 92 spec explicit
51750 6c 79 20 73 61 79 73 20 74 68 69 73 0a 2a 2a 20 ly says this.**
51760 69 73 20 74 68 65 20 77 61 79 20 74 68 69 6e 67 is the way thing
51770 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f s are suppose to
51780 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 work..**.** If
51790 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
517a0 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 30 2c cro is set to 0,
517b0 20 74 68 65 20 4e 55 4c 4c 73 20 61 72 65 20 69 the NULLs are i
517c0 6e 64 69 73 74 69 6e 63 74 20 66 6f 72 0a 2a 2a ndistinct for.**
517d0 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e a UNIQUE index.
517e0 20 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20 In this mode,
517f0 79 6f 75 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76 you can only hav
51800 65 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20 e a single NULL
51810 65 6e 74 72 79 0a 2a 2a 20 66 6f 72 20 61 20 63 entry.** for a c
51820 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20 55 olumn declared U
51830 4e 49 51 55 45 2e 20 20 54 68 69 73 20 69 73 20 NIQUE. This is
51840 74 68 65 20 77 61 79 20 49 6e 66 6f 72 6d 69 78 the way Informix
51850 20 61 6e 64 20 53 51 4c 20 53 65 72 76 65 72 0a and SQL Server.
51860 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 66 ** work..*/.#def
51870 69 6e 65 20 4e 55 4c 4c 5f 44 49 53 54 49 4e 43 ine NULL_DISTINC
51880 54 5f 46 4f 52 5f 55 4e 49 51 55 45 20 31 0a 0a T_FOR_UNIQUE 1..
51890 2f 2a 0a 2a 2a 20 54 68 65 20 22 66 69 6c 65 20 /*.** The "file
518a0 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65 72 20 69 format" number i
518b0 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 s an integer tha
518c0 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 t is incremented
518d0 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 74 68 65 whenever.** the
518e0 20 56 44 42 45 2d 6c 65 76 65 6c 20 66 69 6c 65 VDBE-level file
518f0 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 73 2e format changes.
51900 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
51910 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68 macros define th
51920 65 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 e.** the default
51930 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 file format for
51940 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 61 new databases a
51950 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 66 nd the maximum f
51960 69 6c 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 74 68 ile format.** th
51970 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 63 at the library c
51980 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a 23 64 65 66 an read..*/.#def
51990 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 ine SQLITE_MAX_F
519a0 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 69 66 ILE_FORMAT 4.#if
519b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
519c0 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 0a ULT_FILE_FORMAT.
519d0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
519e0 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 DEFAULT_FILE_FOR
519f0 4d 41 54 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a MAT 1.#endif../*
51a00 0a 2a 2a 20 50 72 6f 76 69 64 65 20 61 20 64 65 .** Provide a de
51a10 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 fault value for
51a20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 SQLITE_TEMP_STOR
51a30 45 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 20 E in case it is
51a40 6e 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a 2a not specified.**
51a50 20 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d on the command-
51a60 6c 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 line.*/.#ifndef
51a70 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 SQLITE_TEMP_STOR
51a80 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
51a90 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23 E_TEMP_STORE 1.#
51aa0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 endif../*.** GCC
51ab0 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 does not define
51ac0 20 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 20 the offsetof()
51ad0 6d 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68 macro so we'll h
51ae0 61 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 ave to do it.**
51af0 6f 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 ourselves..*/.#i
51b00 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a 23 fndef offsetof.#
51b10 64 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66 28 define offsetof(
51b20 53 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44 29 STRUCTURE,FIELD)
51b30 20 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29 26 ((int)((char*)&
51b40 28 28 53 54 52 55 43 54 55 52 45 2a 29 30 29 2d ((STRUCTURE*)0)-
51b50 3e 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a >FIELD)).#endif.
51b60 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
51b70 73 65 65 20 69 66 20 74 68 69 73 20 6d 61 63 68 see if this mach
51b80 69 6e 65 20 75 73 65 73 20 45 42 43 44 49 43 2e ine uses EBCDIC.
51b90 20 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65 20 (Yes, believe
51ba0 69 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 it or.** not, th
51bb0 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 ere are still ma
51bc0 63 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72 65 chines out there
51bd0 20 74 68 61 74 20 75 73 65 20 45 42 43 44 49 43 that use EBCDIC
51be0 2e 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d .).*/.#if 'A' ==
51bf0 20 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e 65 '\301'.# define
51c00 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20 31 SQLITE_EBCDIC 1
51c10 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
51c20 53 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a 23 SQLITE_ASCII 1.#
51c30 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 endif../*.** Int
51c40 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 egers of known s
51c50 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79 70 izes. These typ
51c60 65 64 65 66 73 20 6d 69 67 68 74 20 63 68 61 6e edefs might chan
51c70 67 65 20 66 6f 72 20 61 72 63 68 69 74 65 63 74 ge for architect
51c80 75 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74 68 ures.** where th
51c90 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20 50 e sizes very. P
51ca0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 reprocessor macr
51cb0 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 os are available
51cc0 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 so that the.**
51cd0 74 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f 6e types can be con
51ce0 76 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66 69 veniently redefi
51cf0 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 ned at compile-t
51d00 79 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a ype. Like this:
51d10 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 .**.** c
51d20 63 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59 50 c '-DUINTPTR_TYP
51d30 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 E=long long int'
51d40 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ....*/.#ifndef
51d50 55 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69 66 UINT32_TYPE.# if
51d60 64 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32 5f def HAVE_UINT32_
51d70 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 T.# define UINT
51d80 33 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f 74 32_TYPE uint32_t
51d90 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e .# else.# defin
51da0 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 6e e UINT32_TYPE un
51db0 73 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64 signed int.# end
51dc0 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 if.#endif.#ifnde
51dd0 66 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 f UINT16_TYPE.#
51de0 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 31 ifdef HAVE_UINT1
51df0 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 6_T.# define UI
51e00 4e 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 36 NT16_TYPE uint16
51e10 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 _t.# else.# def
51e20 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 ine UINT16_TYPE
51e30 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 unsigned short i
51e40 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 nt.# endif.#endi
51e50 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36 5f f.#ifndef INT16_
51e60 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 TYPE.# ifdef HAV
51e70 45 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 E_INT16_T.# def
51e80 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 69 ine INT16_TYPE i
51e90 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 nt16_t.# else.#
51ea0 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 define INT16_TY
51eb0 50 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 PE short int.# e
51ec0 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e ndif.#endif.#ifn
51ed0 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a 23 def UINT8_TYPE.#
51ee0 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 ifdef HAVE_UINT
51ef0 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 8_T.# define UI
51f00 4e 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f 74 NT8_TYPE uint8_t
51f10 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e .# else.# defin
51f20 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e 73 e UINT8_TYPE uns
51f30 69 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 igned char.# end
51f40 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 if.#endif.#ifnde
51f50 66 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 f INT8_TYPE.# if
51f60 64 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a def HAVE_INT8_T.
51f70 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 # define INT8_T
51f80 59 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73 YPE int8_t.# els
51f90 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 e.# define INT8
51fa0 5f 54 59 50 45 20 73 69 67 6e 65 64 20 63 68 61 _TYPE signed cha
51fb0 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 r.# endif.#endif
51fc0 0a 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 .#ifndef LONGDOU
51fd0 42 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69 6e BLE_TYPE.# defin
51fe0 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 e LONGDOUBLE_TYP
51ff0 45 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65 E long double.#e
52000 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c ndif.typedef sql
52010 69 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20 20 ite_int64 i64;
52020 20 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 /* 8-byt
52030 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 e signed integer
52040 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c 69 */.typedef sqli
52050 74 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 20 te_uint64 u64;
52060 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 /* 8-byte
52070 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 unsigned intege
52080 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e r */.typedef UIN
52090 54 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 20 T32_TYPE u32;
520a0 20 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 74 /* 4-byt
520b0 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 e unsigned integ
520c0 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 er */.typedef UI
520d0 4e 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 20 NT16_TYPE u16;
520e0 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 /* 2-by
520f0 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 te unsigned inte
52100 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 ger */.typedef I
52110 4e 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20 20 NT16_TYPE i16;
52120 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 /* 2-b
52130 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
52140 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 er */.typedef UI
52150 4e 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20 20 NT8_TYPE u8;
52160 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 /* 1-by
52170 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 te unsigned inte
52180 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 ger */.typedef I
52190 4e 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20 20 NT8_TYPE i8;
521a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 /* 1-b
521b0 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
521c0 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 er */../*.** Mac
521d0 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ros to determine
521e0 20 77 68 65 74 68 65 72 20 74 68 65 20 6d 61 63 whether the mac
521f0 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 20 6c hine is big or l
52200 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a ittle endian,.**
52210 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 72 75 evaluated at ru
52220 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 ntime..*/.#ifdef
52230 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 SQLITE_AMALGAMA
52240 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 TION.SQLITE_PRIV
52250 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 ATE const int sq
52260 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6c 73 65 0a lite3one;.#else.
52270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
52280 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 onst int sqlite3
52290 6f 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 one;.#endif.#if
522a0 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c defined(i386) ||
522b0 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f defined(__i386_
522c0 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
522d0 5f 49 58 38 36 29 5c 0a 20 20 20 20 20 20 20 20 _IX86)\.
522e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
522f0 20 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 28 || defined(
52300 5f 5f 78 38 36 5f 36 34 29 20 7c 7c 20 64 65 66 __x86_64) || def
52310 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 ined(__x86_64__)
52320 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
52330 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 30 0a _BIGENDIAN 0.
52340 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
52350 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 31 0a 23 LITTLEENDIAN 1.#
52360 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 define SQLITE_U
52370 54 46 31 36 4e 41 54 49 56 45 20 20 53 51 4c 49 TF16NATIVE SQLI
52380 54 45 5f 55 54 46 31 36 4c 45 0a 23 65 6c 73 65 TE_UTF16LE.#else
52390 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
523a0 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 28 2a _BIGENDIAN (*
523b0 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 (char *)(&sqlite
523c0 33 6f 6e 65 29 3d 3d 30 29 0a 23 20 64 65 66 69 3one)==0).# defi
523d0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 ne SQLITE_LITTLE
523e0 45 4e 44 49 41 4e 20 28 2a 28 63 68 61 72 20 2a ENDIAN (*(char *
523f0 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d )(&sqlite3one)==
52400 31 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 1).# define SQLI
52410 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 28 TE_UTF16NATIVE (
52420 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e SQLITE_BIGENDIAN
52430 3f 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3a ?SQLITE_UTF16BE:
52440 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 0a SQLITE_UTF16LE).
52450 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f #endif../*.** Co
52460 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 20 nstants for the
52470 6c 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61 6c largest and smal
52480 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36 34 lest possible 64
52490 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
524a0 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 6d gers..** These m
524b0 61 63 72 6f 73 20 61 72 65 20 64 65 73 69 67 6e acros are design
524c0 65 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 ed to work corre
524d0 63 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32 2d ctly on both 32-
524e0 62 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0a 2a bit and 64-bit.*
524f0 2a 20 63 6f 6d 70 69 6c 65 72 73 2e 0a 2a 2f 0a * compilers..*/.
52500 23 64 65 66 69 6e 65 20 4c 41 52 47 45 53 54 5f #define LARGEST_
52510 49 4e 54 36 34 20 20 28 30 78 66 66 66 66 66 66 INT64 (0xffffff
52520 66 66 7c 28 28 28 69 36 34 29 30 78 37 66 66 66 ff|(((i64)0x7fff
52530 66 66 66 66 29 3c 3c 33 32 29 29 0a 23 64 65 66 ffff)<<32)).#def
52540 69 6e 65 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 ine SMALLEST_INT
52550 36 34 20 28 28 28 69 36 34 29 2d 31 29 20 2d 20 64 (((i64)-1) -
52560 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 0a 0a LARGEST_INT64)..
52570 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
52580 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
52590 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ng structure is
525a0 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
525b0 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a e busy-handler.*
525c0 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 * callback for a
525d0 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61 given sqlite ha
525e0 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 ndle. .**.** The
525f0 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 sqlite.busyHand
52600 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 ler member of th
52610 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 20 e sqlite struct
52620 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73 contains the bus
52630 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f y.** callback fo
52640 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 r the database h
52650 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65 andle. Each page
52660 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65 r opened via the
52670 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c sqlite.** handl
52680 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 6f e is passed a po
52690 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e inter to sqlite.
526a0 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65 busyHandler. The
526b0 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a busy-handler.**
526c0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72 callback is cur
526d0 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f rently invoked o
526e0 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 nly from within
526f0 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 pager.c..*/.type
52700 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 48 def struct BusyH
52710 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c andler BusyHandl
52720 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48 er;.struct BusyH
52730 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28 andler {. int (
52740 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69 *xFunc)(void *,i
52750 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73 nt); /* The bus
52760 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 y callback */.
52770 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20 void *pArg;
52780 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
52790 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 20 rst arg to busy
527a0 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e callback */. in
527b0 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20 t nBusy;
527c0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 /* Incr
527d0 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 emented with eac
527e0 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d h busy call */.}
527f0 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 ;../*.** Name of
52800 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 the master data
52810 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 65 base table. The
52820 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 master database
52830 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 73 table.** is a s
52840 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 61 pecial table tha
52850 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 t holds the name
52860 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 73 s and attributes
52870 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 20 of all.** user
52880 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 tables and indic
52890 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d es..*/.#define M
528a0 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 20 ASTER_NAME
528b0 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 "sqlite_master"
528c0 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41 .#define TEMP_MA
528d0 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69 STER_NAME "sqli
528e0 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0a te_temp_master".
528f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 2d ./*.** The root-
52900 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 74 page of the mast
52910 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c er database tabl
52920 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 e..*/.#define MA
52930 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 20 STER_ROOT
52940 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 1../*.** The nam
52950 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 e of the schema
52960 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e table..*/.#defin
52970 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 78 e SCHEMA_TABLE(x
52980 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 44 ) ((!OMIT_TEMPD
52990 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 5f B)&&(x==1)?TEMP_
529a0 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 54 MASTER_NAME:MAST
529b0 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 ER_NAME)../*.**
529c0 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d 61 A convenience ma
529d0 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e 73 cro that returns
529e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
529f0 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e lements in.** an
52a00 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 69 array..*/.#defi
52a10 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 20 ne ArraySize(X)
52a20 20 20 20 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 (sizeof(X)/si
52a30 7a 65 6f 66 28 58 5b 30 5d 29 29 0a 0a 2f 2a 0a zeof(X[0]))../*.
52a40 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
52a50 20 76 61 6c 75 65 20 61 73 20 61 20 64 65 73 74 value as a dest
52a60 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 ructor means to
52a70 75 73 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 use sqlite3DbFre
52a80 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 e()..** This is
52a90 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 an internal exte
52aa0 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f nsion to SQLITE_
52ab0 53 54 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54 STATIC and SQLIT
52ac0 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a E_TRANSIENT..*/.
52ad0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
52ae0 59 4e 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74 YNAMIC ((sqlit
52af0 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 e3_destructor_ty
52b00 70 65 29 73 71 6c 69 74 65 33 44 62 46 72 65 65 pe)sqlite3DbFree
52b10 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 )../*.** When SQ
52b20 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 LITE_OMIT_WSD is
52b30 20 64 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 defined, it mea
52b40 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 72 67 ns that the targ
52b50 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 et platform does
52b60 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 .** not support
52b70 57 72 69 74 61 62 6c 65 20 53 74 61 74 69 63 20 Writable Static
52b80 44 61 74 61 20 28 57 53 44 29 20 73 75 63 68 20 Data (WSD) such
52b90 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 as global and st
52ba0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a atic variables..
52bb0 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 ** All variables
52bc0 20 6d 75 73 74 20 65 69 74 68 65 72 20 62 65 20 must either be
52bd0 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20 on the stack or
52be0 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f dynamically allo
52bf0 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 cated from.** th
52c00 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53 e heap. When WS
52c10 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 D is unsupported
52c20 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 64 , the variable d
52c30 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74 eclarations scat
52c40 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 tered.** through
52c50 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65 20 63 out the SQLite c
52c60 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 ode must become
52c70 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61 constants instea
52c80 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57 d. The SQLITE_W
52c90 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 SD.** macro is u
52ca0 73 65 64 20 66 6f 72 20 74 68 69 73 20 70 75 72 sed for this pur
52cb0 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 pose. And inste
52cc0 61 64 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e ad of referencin
52cd0 67 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a g the variable.*
52ce0 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75 * directly, we u
52cf0 73 65 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 se its constant
52d00 61 73 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b as a key to look
52d10 75 70 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 up the run-time
52d20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 allocated.** buf
52d30 66 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72 fer that holds r
52d40 65 61 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54 eal variable. T
52d50 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 he constant is a
52d60 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69 lso the initiali
52d70 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 zer.** for the r
52d80 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 un-time allocate
52d90 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 d buffer..**.**
52da0 49 6e 20 74 68 65 20 75 73 75 61 6c 6c 79 20 63 In the usually c
52db0 61 73 65 20 77 68 65 72 65 20 57 53 44 20 69 73 ase where WSD is
52dc0 20 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 supported, the
52dd0 53 51 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 47 SQLITE_WSD and G
52de0 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 LOBAL.** macros
52df0 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e become no-ops an
52e00 64 20 68 61 76 65 20 7a 65 72 6f 20 70 65 72 66 d have zero perf
52e10 6f 72 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e 0a ormance impact..
52e20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
52e30 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 66 _OMIT_WSD. #def
52e40 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 63 ine SQLITE_WSD c
52e50 6f 6e 73 74 0a 20 20 23 64 65 66 69 6e 65 20 47 onst. #define G
52e60 4c 4f 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 2a LOBAL(t,v) (*(t*
52e70 29 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e )sqlite3_wsd_fin
52e80 64 28 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 73 d((void*)&(v), s
52e90 69 7a 65 6f 66 28 76 29 29 29 0a 20 20 23 64 65 izeof(v))). #de
52ea0 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 fine sqlite3Glob
52eb0 61 6c 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c 28 alConfig GLOBAL(
52ec0 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f struct Sqlite3Co
52ed0 6e 66 69 67 2c 20 73 71 6c 69 74 65 33 43 6f 6e nfig, sqlite3Con
52ee0 66 69 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 20 fig).SQLITE_API
52ef0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 73 int sqlite3_ws
52f00 64 5f 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e d_init(int N, in
52f10 74 20 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 t J);.SQLITE_API
52f20 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 void *sqlite3
52f30 5f 77 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a _wsd_find(void *
52f40 4b 2c 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 K, int L);.#else
52f50 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 . #define SQLIT
52f60 45 5f 57 53 44 20 0a 20 20 23 64 65 66 69 6e 65 E_WSD . #define
52f70 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a 20 GLOBAL(t,v) v.
52f80 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
52f90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 6c GlobalConfig sql
52fa0 69 74 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 69 ite3Config.#endi
52fb0 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 f../*.** Forward
52fc0 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73 references to s
52fd0 74 72 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70 tructures.*/.typ
52fe0 65 64 65 66 20 73 74 72 75 63 74 20 41 67 67 49 edef struct AggI
52ff0 6e 66 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70 nfo AggInfo;.typ
53000 65 64 65 66 20 73 74 72 75 63 74 20 41 75 74 68 edef struct Auth
53010 43 6f 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74 Context AuthCont
53020 65 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ext;.typedef str
53030 75 63 74 20 42 69 74 76 65 63 20 42 69 74 76 65 uct Bitvec Bitve
53040 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 c;.typedef struc
53050 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 t CollSeq CollSe
53060 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 q;.typedef struc
53070 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b t Column Column;
53080 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
53090 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 Db Db;.typedef s
530a0 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 truct Schema Sch
530b0 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ema;.typedef str
530c0 75 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 uct Expr Expr;.t
530d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 ypedef struct Ex
530e0 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b prList ExprList;
530f0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
53100 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 64 FKey FKey;.typed
53110 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 ef struct FuncDe
53120 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 64 f FuncDef;.typed
53130 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 ef struct FuncDe
53140 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 61 73 fHash FuncDefHas
53150 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 h;.typedef struc
53160 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74 3b t IdList IdList;
53170 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
53180 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 79 70 Index Index;.typ
53190 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 43 edef struct KeyC
531a0 6c 61 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a 74 lass KeyClass;.t
531b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 ypedef struct Ke
531c0 79 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 yInfo KeyInfo;.t
531d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f ypedef struct Lo
531e0 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64 okaside Lookasid
531f0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 e;.typedef struc
53200 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 t LookasideSlot
53210 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a 74 LookasideSlot;.t
53220 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f ypedef struct Mo
53230 64 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 dule Module;.typ
53240 65 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65 edef struct Name
53250 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74 Context NameCont
53260 65 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ext;.typedef str
53270 75 63 74 20 50 61 72 73 65 20 50 61 72 73 65 3b uct Parse Parse;
53280 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
53290 53 65 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 Select Select;.t
532a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 72 ypedef struct Sr
532b0 63 4c 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 cList SrcList;.t
532c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74 ypedef struct St
532d0 72 41 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b rAccum StrAccum;
532e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
532f0 54 61 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 Table Table;.typ
53300 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62 6c edef struct Tabl
53310 65 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b eLock TableLock;
53320 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
53330 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 Token Token;.typ
53340 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 edef struct Trig
53350 67 65 72 53 74 61 63 6b 20 54 72 69 67 67 65 72 gerStack Trigger
53360 53 74 61 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 Stack;.typedef s
53370 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 truct TriggerSte
53380 70 20 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 p TriggerStep;.t
53390 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72 ypedef struct Tr
533a0 69 67 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 igger Trigger;.t
533b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e ypedef struct Un
533c0 70 61 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 packedRecord Unp
533d0 61 63 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 ackedRecord;.typ
533e0 65 64 65 66 20 73 74 72 75 63 74 20 57 61 6c 6b edef struct Walk
533f0 65 72 20 57 61 6c 6b 65 72 3b 0a 74 79 70 65 64 er Walker;.typed
53400 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 49 ef struct WhereI
53410 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 nfo WhereInfo;.t
53420 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 ypedef struct Wh
53430 65 72 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 ereLevel WhereLe
53440 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 vel;../*.** Defe
53450 72 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e r sourcing vdbe.
53460 68 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e h and btree.h un
53470 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 til after the "u
53480 38 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 8" and .** "Busy
53490 48 61 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66 Handler" typedef
534a0 73 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72 s. vdbe.h also r
534b0 65 71 75 69 72 65 73 20 61 20 66 65 77 20 6f 66 equires a few of
534c0 20 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70 the opaque.** p
534d0 6f 69 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e ointer types (i.
534e0 65 2e 20 46 75 6e 63 44 65 66 29 20 64 65 66 69 e. FuncDef) defi
534f0 6e 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a ned above..*/./*
53500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
53510 63 6c 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e clude btree.h in
53520 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 the middle of s
53530 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
53540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
53550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
53560 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68 gin file btree.h
53570 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
53580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
535a0 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
535b0 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
535c0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
535d0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
535e0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
535f0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
53600 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
53610 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
53620 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
53630 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
53640 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
53650 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
53660 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
53670 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
53680 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
53690 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
536a0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
536b0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
536c0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
536d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
536e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
536f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53710 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 ****.** This hea
53720 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 der file defines
53730 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 the interface t
53740 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 42 hat the sqlite B
53750 2d 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75 -Tree file.** su
53760 62 73 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f bsystem. See co
53770 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f mments in the so
53780 75 72 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20 urce code for a
53790 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 detailed descrip
537a0 74 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20 tion.** of what
537b0 65 61 63 68 20 69 6e 74 65 72 66 61 63 65 20 72 each interface r
537c0 6f 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a outine does..**.
537d0 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 62 74 72 ** @(#) $Id: btr
537e0 65 65 2e 68 2c 76 20 31 2e 31 30 35 20 32 30 30 ee.h,v 1.105 200
537f0 38 2f 31 30 2f 32 37 20 31 33 3a 35 39 3a 33 34 8/10/27 13:59:34
53800 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
53810 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 $.*/.#ifndef _B
53820 54 52 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 TREE_H_.#define
53830 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f _BTREE_H_../* TO
53840 44 4f 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74 DO: This definit
53850 69 6f 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c ion is just incl
53860 75 64 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f uded so other mo
53870 64 75 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 dules compile. I
53880 74 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 t.** needs to be
53890 20 72 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 revisited..*/.#
538a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f define SQLITE_N_
538b0 42 54 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f BTREE_META 10../
538c0 2a 0a 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20 *.** If defined
538d0 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 as non-zero, aut
538e0 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 o-vacuum is enab
538f0 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 led by default.
53900 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 Otherwise.** it
53910 6d 75 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f must be turned o
53920 6e 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 n for each datab
53930 61 73 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d ase using "PRAGM
53940 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 A auto_vacuum =
53950 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 1"..*/.#ifndef S
53960 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
53970 54 4f 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69 TOVACUUM. #defi
53980 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ne SQLITE_DEFAUL
53990 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 T_AUTOVACUUM 0.#
539a0 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 endif..#define B
539b0 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
539c0 4e 4f 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a NONE 0 /*
539d0 20 44 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d Do not do auto-
539e0 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e vacuum */.#defin
539f0 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 e BTREE_AUTOVACU
53a00 55 4d 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20 UM_FULL 1
53a10 20 2f 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f /* Do full auto
53a20 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 -vacuum */.#defi
53a30 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 ne BTREE_AUTOVAC
53a40 55 55 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20 UUM_INCR 2
53a50 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c /* Incremental
53a60 20 76 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a vacuum */../*.*
53a70 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 * Forward declar
53a80 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 ations of struct
53a90 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ure.*/.typedef s
53aa0 74 72 75 63 74 20 42 74 72 65 65 20 42 74 72 65 truct Btree Btre
53ab0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 e;.typedef struc
53ac0 74 20 42 74 43 75 72 73 6f 72 20 42 74 43 75 72 t BtCursor BtCur
53ad0 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 sor;.typedef str
53ae0 75 63 74 20 42 74 53 68 61 72 65 64 20 42 74 53 uct BtShared BtS
53af0 68 61 72 65 64 3b 0a 74 79 70 65 64 65 66 20 73 hared;.typedef s
53b00 74 72 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 truct BtreeMutex
53b10 41 72 72 61 79 20 42 74 72 65 65 4d 75 74 65 78 Array BtreeMutex
53b20 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 Array;../*.** Th
53b30 69 73 20 73 74 72 75 63 74 75 72 65 20 72 65 63 is structure rec
53b40 6f 72 64 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 ords all of the
53b50 42 74 72 65 65 73 20 74 68 61 74 20 6e 65 65 64 Btrees that need
53b60 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 to hold.** a mu
53b70 74 65 78 20 62 65 66 6f 72 65 20 77 65 20 65 6e tex before we en
53b80 74 65 72 20 73 71 6c 69 74 65 33 56 64 62 65 45 ter sqlite3VdbeE
53b90 78 65 63 28 29 2e 20 20 54 68 65 20 42 74 72 65 xec(). The Btre
53ba0 65 73 20 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c es are.** are pl
53bb0 61 63 65 64 20 69 6e 20 61 42 74 72 65 65 5b 5d aced in aBtree[]
53bc0 20 69 6e 20 6f 72 64 65 72 20 6f 66 20 61 42 74 in order of aBt
53bd0 72 65 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 ree[]->pBt. Tha
53be0 74 20 77 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e t way,.** we can
53bf0 20 61 6c 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 always lock and
53c00 20 75 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c unlock them all
53c10 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 quickly..*/.str
53c20 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 uct BtreeMutexAr
53c30 72 61 79 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 ray {. int nMut
53c40 65 78 3b 0a 20 20 42 74 72 65 65 20 2a 61 42 74 ex;. Btree *aBt
53c50 72 65 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 ree[SQLITE_MAX_A
53c60 54 54 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a TTACHED+1];.};..
53c70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53c80 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
53c90 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 Open(. const ch
53ca0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
53cb0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 /* Name of data
53cc0 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 base file to ope
53cd0 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a n */. sqlite3 *
53ce0 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 db,
53cf0 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 /* Associated da
53d00 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
53d10 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 2c n */. Btree **,
53d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53d30 2f 2a 20 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 /* Return open B
53d40 74 72 65 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 tree* here */.
53d50 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
53d60 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
53d70 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c s */. int vfsFl
53d80 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 ags
53d90 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 /* Flags passed
53da0 74 68 72 6f 75 67 68 20 74 6f 20 56 46 53 20 6f through to VFS o
53db0 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 pen */.);../* Th
53dc0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 e flags paramete
53dd0 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 r to sqlite3Btre
53de0 65 4f 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65 eOpen can be the
53df0 20 62 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74 bitwise or of t
53e00 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 he.** following
53e10 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f values..**.** NO
53e20 54 45 3a 20 20 54 68 65 73 65 20 76 61 6c 75 65 TE: These value
53e30 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 s must match the
53e40 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 corresponding P
53e50 41 47 45 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a AGER_ values in.
53e60 2a 2a 20 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 ** pager.h..*/.#
53e70 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 define BTREE_OMI
53e80 54 5f 4a 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a T_JOURNAL 1 /*
53e90 20 44 6f 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 Do not use jour
53ea0 6e 61 6c 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e nal. No argumen
53eb0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 t */.#define BTR
53ec0 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 EE_NO_READLOCK
53ed0 20 32 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 2 /* Omit read
53ee0 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c locks on readonl
53ef0 79 20 66 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 y files */.#defi
53f00 6e 65 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 ne BTREE_MEMORY
53f10 20 20 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 4 /* In-
53f20 6d 65 6d 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 memory DB. No a
53f30 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 rgument */.#defi
53f40 6e 65 20 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c ne BTREE_READONL
53f50 59 20 20 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 Y 8 /* Ope
53f60 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 n the database i
53f70 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 n read-only mode
53f80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
53f90 45 5f 52 45 41 44 57 52 49 54 45 20 20 20 20 31 E_READWRITE 1
53fa0 36 20 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6 /* Open for b
53fb0 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 oth reading and
53fc0 77 72 69 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 writing */.#defi
53fd0 6e 65 20 42 54 52 45 45 5f 43 52 45 41 54 45 20 ne BTREE_CREATE
53fe0 20 20 20 20 20 20 33 32 20 20 2f 2a 20 43 72 65 32 /* Cre
53ff0 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ate the database
54000 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 if it does not
54010 65 78 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 exist */..SQLITE
54020 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
54030 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 ite3BtreeClose(B
54040 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
54050 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54060 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 e3BtreeSetCacheS
54070 69 7a 65 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b ize(Btree*,int);
54080 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54090 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
540a0 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 SetSafetyLevel(B
540b0 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a tree*,int,int);.
540c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
540d0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
540e0 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 yncDisabled(Btre
540f0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
54100 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
54110 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 treeSetPageSize(
54120 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b Btree*,int,int);
54130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54140 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
54150 47 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 GetPageSize(Btre
54160 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
54170 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
54180 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 treeMaxPageCount
54190 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 (Btree*,int);.SQ
541a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
541b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
541c0 52 65 73 65 72 76 65 28 42 74 72 65 65 2a 29 3b Reserve(Btree*);
541d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
541e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
541f0 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 SetAutoVacuum(Bt
54200 72 65 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ree *, int);.SQL
54210 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54220 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 sqlite3BtreeGetA
54230 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 utoVacuum(Btree
54240 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
54250 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
54260 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 reeBeginTrans(Bt
54270 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ree*,int);.SQLIT
54280 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
54290 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
542a0 50 68 61 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c PhaseOne(Btree*,
542b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 const char *zMa
542c0 73 74 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ster);.SQLITE_PR
542d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
542e0 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
542f0 65 54 77 6f 28 42 74 72 65 65 2a 29 3b 0a 53 51 eTwo(Btree*);.SQ
54300 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
54310 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
54320 6d 69 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c mit(Btree*);.SQL
54330 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54340 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c sqlite3BtreeRoll
54350 62 61 63 6b 28 42 74 72 65 65 2a 29 3b 0a 53 51 back(Btree*);.SQ
54360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
54370 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 sqlite3BtreeBeg
54380 69 6e 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a inStmt(Btree*);.
54390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
543a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
543b0 6f 6d 6d 69 74 53 74 6d 74 28 42 74 72 65 65 2a ommitStmt(Btree*
543c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
543d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
543e0 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 28 42 eeRollbackStmt(B
543f0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
54400 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54410 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 e3BtreeCreateTab
54420 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a 2c le(Btree*, int*,
54430 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51 4c int flags);.SQL
54440 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54450 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e sqlite3BtreeIsIn
54460 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 Trans(Btree*);.S
54470 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54480 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
54490 49 6e 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a InStmt(Btree*);.
544a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
544b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
544c0 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 sInReadTrans(Btr
544d0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
544e0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
544f0 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 e3BtreeSchema(Bt
54500 72 65 65 20 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 ree *, int, void
54510 28 2a 29 28 76 6f 69 64 20 2a 29 29 3b 0a 53 51 (*)(void *));.SQ
54520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
54530 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 sqlite3BtreeSch
54540 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 emaLocked(Btree
54550 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
54560 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
54570 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 reeLockTable(Btr
54580 65 65 20 2a 2c 20 69 6e 74 2c 20 75 38 29 3b 0a ee *, int, u8);.
54590 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
545a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
545b0 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e te3BtreeGetFilen
545c0 61 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 ame(Btree *);.SQ
545d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
545e0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
545f0 42 74 72 65 65 47 65 74 44 69 72 6e 61 6d 65 28 BtreeGetDirname(
54600 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Btree *);.SQLITE
54610 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
54620 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 har *sqlite3Btre
54630 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 eGetJournalname(
54640 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Btree *);.SQLITE
54650 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
54660 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c ite3BtreeCopyFil
54670 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65 65 e(Btree *, Btree
54680 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 *);..SQLITE_PRI
54690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
546a0 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 BtreeIncrVacuum(
546b0 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54 68 Btree *);../* Th
546c0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 e flags paramete
546d0 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 r to sqlite3Btre
546e0 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 61 6e eCreateTable can
546f0 20 62 65 20 74 68 65 20 62 69 74 77 69 73 65 20 be the bitwise
54700 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c OR.** of the fol
54710 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a 2f lowing flags:.*/
54720 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 49 .#define BTREE_I
54730 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 20 2f NTKEY 1 /
54740 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c 79 * Table has only
54750 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
54760 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 23 nteger keys */.#
54770 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45 52 define BTREE_ZER
54780 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a 20 ODATA 2 /*
54790 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20 6f Table has keys o
547a0 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a 2f nly - no data */
547b0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4c .#define BTREE_L
547c0 45 41 46 44 41 54 41 20 20 20 34 20 20 20 20 2f EAFDATA 4 /
547d0 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 69 6e * Data stored in
547e0 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20 49 leaves only. I
547f0 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a 2f mplies INTKEY */
54800 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
54810 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
54820 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65 eDropTable(Btree
54830 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 *, int, int*);.S
54840 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54850 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c t sqlite3BtreeCl
54860 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a 2c earTable(Btree*,
54870 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c int, int*);.SQL
54880 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54890 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
548a0 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 eta(Btree*, int
548b0 69 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 idx, u32 *pValue
548c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
548d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
548e0 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 eeUpdateMeta(Btr
548f0 65 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 ee*, int idx, u3
54900 32 20 76 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 2 value);.SQLITE
54910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
54920 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c lite3BtreeTripAl
54930 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 2a 2c lCursors(Btree*,
54940 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 int);..SQLITE_P
54950 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54960 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 e3BtreeCursor(.
54970 20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20 Btree*,
54980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54990 20 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63 /* BTree c
549a0 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 ontaining table
549b0 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 to open */. int
549c0 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 iTable,
549d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
549e0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f /* Index of ro
549f0 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ot page */. int
54a00 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 wrFlag,
54a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54a20 20 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69 /* 1 for writi
54a30 6e 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d ng. 0 for read-
54a40 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 only */. struct
54a50 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20 KeyInfo*,
54a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
54a70 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 * First argument
54a80 20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63 to compare func
54a90 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 tion */. BtCurs
54aa0 6f 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 or *pCursor
54ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
54ac0 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 * Space to write
54ad0 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 cursor structur
54ae0 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 e */.);.SQLITE_P
54af0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54b00 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a e3BtreeCursorSiz
54b10 65 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 45 e(void);..SQLITE
54b20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
54b30 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 ite3BtreeCloseCu
54b40 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b rsor(BtCursor*);
54b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54b60 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
54b70 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73 Moveto(. BtCurs
54b80 6f 72 2a 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 or*,. const voi
54b90 64 20 2a 70 4b 65 79 2c 0a 20 20 69 36 34 20 6e d *pKey,. i64 n
54ba0 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c Key,. int bias,
54bb0 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a . int *pRes.);.
54bc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
54bd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
54be0 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 ovetoUnpacked(.
54bf0 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e BtCursor*,. Un
54c00 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 packedRecord *pU
54c10 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b nKey,. i64 intK
54c20 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a ey,. int bias,.
54c30 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 int *pRes.);.S
54c40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54c50 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
54c60 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 rsorHasMoved(BtC
54c70 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 ursor*, int*);.S
54c80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54c90 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 t sqlite3BtreeDe
54ca0 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b lete(BtCursor*);
54cb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54cc0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
54cd0 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a Insert(BtCursor*
54ce0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b , const void *pK
54cf0 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 ey, i64 nKey,.
54d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54d20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 const void *pDat
54d30 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 a, int nData,.
54d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54d60 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 int nZero, int b
54d70 69 61 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ias);.SQLITE_PRI
54d80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
54d90 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72 BtreeFirst(BtCur
54da0 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 sor*, int *pRes)
54db0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54dc0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
54dd0 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c eLast(BtCursor*,
54de0 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c int *pRes);.SQL
54df0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54e00 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
54e10 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 (BtCursor*, int
54e20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 *pRes);.SQLITE_P
54e30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54e40 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 e3BtreeEof(BtCur
54e50 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sor*);.SQLITE_PR
54e60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
54e70 33 42 74 72 65 65 46 6c 61 67 73 28 42 74 43 75 3BtreeFlags(BtCu
54e80 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
54e90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54ea0 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 e3BtreePrevious(
54eb0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
54ec0 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pRes);.SQLITE_PR
54ed0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
54ee0 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 3BtreeKeySize(Bt
54ef0 43 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 Cursor*, i64 *pS
54f00 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ize);.SQLITE_PRI
54f10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
54f20 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f BtreeKey(BtCurso
54f30 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 r*, u32 offset,
54f40 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b u32 amt, void*);
54f50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54f60 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 sqlite3 *sqlite3
54f70 42 74 72 65 65 43 75 72 73 6f 72 44 62 28 63 6f BtreeCursorDb(co
54f80 6e 73 74 20 42 74 43 75 72 73 6f 72 2a 29 3b 0a nst BtCursor*);.
54f90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
54fa0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
54fb0 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 e3BtreeKeyFetch(
54fc0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
54fd0 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pAmt);.SQLITE_PR
54fe0 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 IVATE const void
54ff0 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 *sqlite3BtreeDa
55000 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 taFetch(BtCursor
55010 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 *, int *pAmt);.S
55020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
55030 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 t sqlite3BtreeDa
55040 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a taSize(BtCursor*
55050 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 , u32 *pSize);.S
55060 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
55070 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 t sqlite3BtreeDa
55080 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 ta(BtCursor*, u3
55090 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 2 offset, u32 am
550a0 74 2c 20 76 6f 69 64 2a 29 3b 0a 0a 53 51 4c 49 t, void*);..SQLI
550b0 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
550c0 2a 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 *sqlite3BtreeInt
550d0 65 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 egrityCheck(Btre
550e0 65 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 e*, int *aRoot,
550f0 69 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 int nRoot, int,
55100 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
55110 49 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67 IVATE struct Pag
55120 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 er *sqlite3Btree
55130 50 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a Pager(Btree*);..
55140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
55150 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 nt sqlite3BtreeP
55160 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a utData(BtCursor*
55170 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 , u32 offset, u3
55180 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 2 amt, void*);.S
55190 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
551a0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 id sqlite3BtreeC
551b0 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 acheOverflow(BtC
551c0 75 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 ursor *);.SQLITE
551d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
551e0 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 lite3BtreeClearC
551f0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a ursor(BtCursor *
55200 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
55210 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
55220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
55230 33 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 3BtreeCursorInfo
55240 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a (BtCursor*, int*
55250 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
55260 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
55270 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 te3BtreeCursorLi
55280 73 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 st(Btree*);.#end
55290 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 if../*.** If we
552a0 61 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 are not using sh
552b0 61 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e ared cache, then
552c0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
552d0 64 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 d to.** use mute
552e0 78 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 xes to access th
552f0 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 e BtShared struc
55300 74 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 tures. So make
55310 74 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 the.** Enter and
55320 20 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 Leave procedure
55330 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 s no-ops..*/.#if
55340 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
55350 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
55360 48 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 HE) && SQLITE_TH
55370 52 45 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f READSAFE.SQLITE_
55380 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
55390 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
553a0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
553b0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
553c0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
553d0 65 28 42 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 e(Btree*);.#ifnd
553e0 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 ef NDEBUG. /* T
553f0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
55400 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 sed inside asser
55410 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f t() statements o
55420 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 nly. */.SQLITE_P
55430 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
55440 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
55450 74 65 78 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e tex(Btree*);.#en
55460 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
55470 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
55480 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 3BtreeEnterCurso
55490 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 r(BtCursor*);.SQ
554a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
554b0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
554c0 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 LeaveCursor(BtCu
554d0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
554e0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
554f0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
55500 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 ll(sqlite3*);.SQ
55510 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
55520 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
55530 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 LeaveAll(sqlite3
55540 2a 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 *);.#ifndef NDEB
55550 55 47 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 UG. /* This rou
55560 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 tine is used ins
55570 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
55580 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f tements only. */
55590 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
555a0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 int sqlite3Btr
555b0 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 eeHoldsAllMutexe
555c0 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6e s(sqlite3*);.#en
555d0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
555e0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
555f0 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3BtreeMutexArray
55600 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 Enter(BtreeMutex
55610 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f Array*);.SQLITE_
55620 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
55630 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
55640 41 72 72 61 79 4c 65 61 76 65 28 42 74 72 65 65 ArrayLeave(Btree
55650 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 MutexArray*);.SQ
55660 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
55670 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
55680 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 MutexArrayInsert
55690 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 (BtreeMutexArray
556a0 2a 2c 20 42 74 72 65 65 2a 29 3b 0a 23 65 6c 73 *, Btree*);.#els
556b0 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 e.# define sqlit
556c0 65 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 0a e3BtreeEnter(X).
556d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
556e0 42 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 69 BtreeLeave(X).#i
556f0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f fndef NDEBUG. /
55700 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
55710 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 s used inside as
55720 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
55730 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 23 20 64 65 66 s only. */.# def
55740 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
55750 48 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 0a HoldsMutex(X) 1.
55760 23 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 #endif.# define
55770 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
55780 72 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 rCursor(X).# def
55790 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
557a0 4c 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23 LeaveCursor(X).#
557b0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
557c0 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 58 29 0a treeEnterAll(X).
557d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
557e0 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 BtreeLeaveAll(X)
557f0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
55800 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e /* This routin
55810 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 e is used inside
55820 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
55830 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 23 20 ents only. */.#
55840 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
55850 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 reeHoldsAllMutex
55860 65 73 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 23 es(X) 1.#endif.#
55870 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
55880 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e treeMutexArrayEn
55890 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 ter(X).# define
558a0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
558b0 78 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23 xArrayLeave(X).#
558c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
558d0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e treeMutexArrayIn
558e0 73 65 72 74 28 58 2c 59 29 0a 23 65 6e 64 69 66 sert(X,Y).#endif
558f0 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54 ...#endif /* _BT
55900 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a REE_H_ */../****
55910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
55920 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a f btree.h ******
55930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
55960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
55970 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
55980 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 eft off in sqlit
55990 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
559a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
559b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
559c0 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65 de vdbe.h in the
559d0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
559e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
559f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
55a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
55a10 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a file vdbe.h ***
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 2a 2a 2a 2a 2a ****************
55a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
55a50 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
55a60 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
55a70 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
55a80 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
55a90 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
55aa0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
55ab0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
55ac0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
55ad0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
55ae0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
55af0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
55b00 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
55b10 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
55b20 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
55b30 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
55b40 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
55b50 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
55b60 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
55b70 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55bc0 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65 *.** Header file
55bd0 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c for the Virtual
55be0 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65 DataBase Engine
55bf0 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 (VDBE).**.** Th
55c00 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65 is header define
55c10 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 s the interface
55c20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64 to the virtual d
55c30 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a atabase engine.*
55c40 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20 * or VDBE. The
55c50 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20 VDBE implements
55c60 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68 an abstract mach
55c70 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a ine that runs a.
55c80 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61 ** simple progra
55c90 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20 m to access and
55ca0 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72 modify the under
55cb0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a lying database..
55cc0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e **.** $Id: vdbe.
55cd0 68 2c 76 20 31 2e 31 33 39 20 32 30 30 38 2f 31 h,v 1.139 2008/1
55ce0 30 2f 33 31 20 31 30 3a 35 33 3a 32 33 20 64 61 0/31 10:53:23 da
55cf0 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
55d00 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 */.#ifndef _SQLI
55d10 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 TE_VDBE_H_.#defi
55d20 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f ne _SQLITE_VDBE_
55d30 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 H_../*.** A sing
55d40 6c 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70 le VDBE is an op
55d50 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e aque structure n
55d60 61 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e amed "Vdbe". On
55d70 6c 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 ly routines.** i
55d80 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c n the source fil
55d90 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61 e sqliteVdbe.c a
55da0 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 re allowed to se
55db0 65 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a e the insides.**
55dc0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
55dd0 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 re..*/.typedef s
55de0 74 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b truct Vdbe Vdbe;
55df0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 ../*.** The name
55e00 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 s of the followi
55e10 6e 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65 ng types declare
55e20 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61 d in vdbeInt.h a
55e30 72 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66 re required.** f
55e40 6f 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65 or the VdbeOp de
55e50 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 finition..*/.typ
55e60 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
55e70 46 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74 Func VdbeFunc;.t
55e80 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 ypedef struct Me
55e90 6d 20 4d 65 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 m Mem;../*.** A
55ea0 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 single instructi
55eb0 6f 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 on of the virtua
55ec0 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e l machine has an
55ed0 20 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 opcode.** and a
55ee0 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 s many as three
55ef0 6f 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69 operands. The i
55f00 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65 nstruction is re
55f10 63 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 corded.** as an
55f20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
55f30 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
55f40 75 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 ure:.*/.struct V
55f50 64 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 dbeOp {. u8 opc
55f60 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ode; /*
55f70 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 What operation
55f80 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 to perform */.
55f90 73 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79 signed char p4ty
55fa0 70 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 pe; /* One of th
55fb0 65 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e e P4_xxx constan
55fc0 74 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 ts for p4 */. u
55fd0 38 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20 8 opflags;
55fe0 20 20 20 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e /* Not curren
55ff0 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 tly used */. u8
56000 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 p5;
56010 20 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d /* Fifth param
56020 65 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 eter is an unsig
56030 6e 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f ned character */
56040 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 . int p1;
56050 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
56060 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 operand */. int
56070 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 p2;
56080 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d /* Second param
56090 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 eter (often the
560a0 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e jump destination
560b0 29 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 ) */. int p3;
560c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
560d0 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
560e0 72 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 r */. union {
560f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f /* fo
56100 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f rth parameter */
56110 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 . int i;
56120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
56130 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 nteger value if
56140 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 p4type==P4_INT32
56150 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b */. void *p;
56160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
56170 2a 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 * Generic pointe
56180 72 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a r */. char *z
56190 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
561a0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 /* Pointer to da
561b0 74 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 ta for string (c
561c0 68 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73 har array) types
561d0 20 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 */. i64 *pI6
561e0 34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 4; /
561f0 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
56200 70 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a pe is P4_INT64 *
56210 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 /. double *pR
56220 65 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 eal; /*
56230 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 Used when p4type
56240 20 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 is P4_REAL */.
56250 20 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e FuncDef *pFun
56260 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 c; /* Use
56270 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
56280 20 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 P4_FUNCDEF */.
56290 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 VdbeFunc *pVd
562a0 62 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 beFunc; /* Use
562b0 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
562c0 20 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a P4_VDBEFUNC */.
562d0 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f CollSeq *pCo
562e0 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 ll; /* Us
562f0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
56300 73 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a s P4_COLLSEQ */.
56310 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 Mem *pMem;
56320 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
56330 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
56340 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 s P4_MEM */.
56350 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
56360 74 61 62 3b 20 20 20 2f 2a 20 55 73 65 64 20 77 tab; /* Used w
56370 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 hen p4type is P4
56380 5f 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 _VTAB */. Key
56390 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
563a0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
563b0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 p4type is P4_KE
563c0 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 YINFO */. int
563d0 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 *ai;
563e0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
563f0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e p4type is P4_IN
56400 54 41 52 52 41 59 20 2a 2f 0a 20 20 7d 20 70 34 TARRAY */. } p4
56410 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
56420 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a 43 DEBUG. char *zC
56430 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 omment;
56440 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69 /* Comment to i
56450 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c 69 mprove readabili
56460 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 ty */.#endif.#if
56470 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 def VDBE_PROFILE
56480 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 . int cnt;
56490 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
564a0 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 umber of times t
564b0 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
564c0 77 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f 0a was executed */.
564d0 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 20 u64 cycles;
564e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f /* To
564f0 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 65 tal time spent e
56500 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e xecuting this in
56510 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e struction */.#en
56520 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 dif.};.typedef s
56530 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62 truct VdbeOp Vdb
56540 65 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d eOp;../*.** A sm
56550 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 aller version of
56560 20 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72 VdbeOp used for
56570 20 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69 the VdbeAddOpLi
56580 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 st() function be
56590 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65 cause.** it take
565a0 73 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e s up less space.
565b0 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f .*/.struct VdbeO
565c0 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63 pList {. u8 opc
565d0 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ode; /*
565e0 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 What operation
565f0 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 to perform */.
56600 73 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20 signed char p1;
56610 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65 /* First ope
56620 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 64 rand */. signed
56630 20 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f 2a char p2; /*
56640 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 Second paramete
56650 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d r (often the jum
56660 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a p destination) *
56670 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 /. signed char
56680 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 64 p3; /* Third
56690 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b parameter */.};
566a0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
566b0 56 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f VdbeOpList VdbeO
566c0 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c pList;../*.** Al
566d0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 lowed values of
566e0 56 64 62 65 4f 70 2e 70 33 74 79 70 65 0a 2a 2f VdbeOp.p3type.*/
566f0 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 55 .#define P4_NOTU
56700 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68 SED 0 /* Th
56710 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69 e P4 parameter i
56720 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64 s not used */.#d
56730 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 43 efine P4_DYNAMIC
56740 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74 (-1) /* Point
56750 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f er to a string o
56760 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
56770 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 iteMalloc() */.#
56780 64 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 43 define P4_STATIC
56790 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e (-2) /* Poin
567a0 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 ter to a static
567b0 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
567c0 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 2d e P4_COLLSEQ (-
567d0 34 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 4) /* P4 is a p
567e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c ointer to a Coll
567f0 53 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f Seq structure */
56800 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e 43 .#define P4_FUNC
56810 44 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 34 DEF (-5) /* P4
56820 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
56830 20 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 a FuncDef struc
56840 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
56850 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29 P4_KEYINFO (-6)
56860 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
56870 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 nter to a KeyInf
56880 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 o structure */.#
56890 64 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 55 define P4_VDBEFU
568a0 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 69 NC (-7) /* P4 i
568b0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
568c0 20 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74 VdbeFunc struct
568d0 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
568e0 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20 4_MEM (-8)
568f0 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
56900 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20 ter to a Mem*
56910 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
56920 65 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 45 efine P4_TRANSIE
56930 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 73 NT (-9) /* P4 is
56940 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
56950 74 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67 transient string
56960 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 */.#define P4_V
56970 54 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a TAB (-10) /*
56980 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
56990 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 to an sqlite3_v
569a0 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f tab structure */
569b0 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 49 .#define P4_MPRI
569c0 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 34 NTF (-11) /* P4
569d0 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74 is a string obt
569e0 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
569f0 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a e3_mprintf() */.
56a00 23 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c 20 #define P4_REAL
56a10 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 20 (-12) /* P4
56a20 69 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 is a 64-bit floa
56a30 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
56a40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 */.#define P4_I
56a50 4e 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a NT64 (-13) /*
56a60 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 P4 is a 64-bit
56a70 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
56a80 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 /.#define P4_INT
56a90 33 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50 32 (-14) /* P
56aa0 34 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69 4 is a 32-bit si
56ab0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
56ac0 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52 #define P4_INTAR
56ad0 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20 RAY (-15) /* P4
56ae0 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33 is a vector of 3
56af0 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a 2-bit integers *
56b00 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e /../* When addin
56b10 67 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20 g a P4 argument
56b20 75 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f using P4_KEYINFO
56b30 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 , a copy of the
56b40 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
56b50 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 e.** is made. T
56b60 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65 hat copy is free
56b70 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 d when the Vdbe
56b80 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 is finalized. B
56b90 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 ut if the.** arg
56ba0 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 ument is P4_KEYI
56bb0 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 NFO_HANDOFF, the
56bc0 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 passed in point
56bd0 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 20 er is used. It
56be0 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 still.** gets fr
56bf0 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 eed when the Vdb
56c00 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 e is finalized s
56c10 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c o it still shoul
56c20 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a d be obtained.**
56c30 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 from a single s
56c40 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 qliteMalloc().
56c50 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d But no copy is m
56c60 61 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c ade and the call
56c70 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ing.** function
56c80 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 should *not* try
56c90 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79 to free the Key
56ca0 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 Info..*/.#define
56cb0 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 P4_KEYINFO_HAND
56cc0 4f 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e OFF (-16).#defin
56cd0 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 e P4_KEYINFO_STA
56ce0 54 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a TIC (-17)../*.*
56cf0 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e * The Vdbe.aColN
56d00 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69 ame array contai
56d10 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 ns 5n Mem struct
56d20 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73 ures, where n is
56d30 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 the .** number
56d40 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 of columns of da
56d50 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ta returned by t
56d60 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f he statement..*/
56d70 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 .#define COLNAME
56d80 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66 _NAME 0.#def
56d90 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c ine COLNAME_DECL
56da0 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43 TYPE 1.#define C
56db0 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20 OLNAME_DATABASE
56dc0 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 2.#define COLNAM
56dd0 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65 E_TABLE 3.#de
56de0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c fine COLNAME_COL
56df0 55 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53 UMN 4.#ifdef S
56e00 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
56e10 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 UMN_METADATA.# d
56e20 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
56e30 20 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a 5 /*
56e40 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 Number of COLNA
56e50 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a ME_xxx symbols *
56e60 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 /.#else.# ifdef
56e70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
56e80 54 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20 TYPE.# define
56e90 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 COLNAME_N 1
56ea0 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f /* Store o
56eb0 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a nly the name */.
56ec0 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e # else.# defin
56ed0 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 e COLNAME_N
56ee0 20 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 2 /* Store
56ef0 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 the name and de
56f00 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 cltype */.# endi
56f10 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 f.#endif../*.**
56f20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 The following ma
56f30 63 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72 cro converts a r
56f40 65 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 20 elative address
56f50 69 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a in the p2 field.
56f60 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 ** of a VdbeOp s
56f70 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 tructure into a
56f80 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 negative number
56f90 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 so that .** sqli
56fa0 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
56fb0 28 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 () knows that th
56fc0 65 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c e address is rel
56fd0 61 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a ative. Calling.
56fe0 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61 ** the macro aga
56ff0 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 in restores the
57000 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 address..*/.#def
57010 69 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d 31 ine ADDR(X) (-1
57020 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 -(X))../*.** The
57030 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 makefile scans
57040 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 the vdbe.c sourc
57050 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74 e file and creat
57060 65 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e es the "opcodes.
57070 68 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c h".** header fil
57080 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 61 e that defines a
57090 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68 number for each
570a0 20 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 20 opcode used by
570b0 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a the VDBE..*/./**
570c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
570d0 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 lude opcodes.h i
570e0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
570f0 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h *********
57100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
57110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
57120 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e in file opcodes.
57130 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
57140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
57150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 ***********/./*
57160 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 Automatically ge
57170 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 nerated. Do not
57180 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 edit */./* See
57190 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 the mkopcodeh.aw
571a0 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 k script for det
571b0 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ails */.#define
571c0 4f 50 5f 56 4e 65 78 74 20 20 20 20 20 20 20 20 OP_VNext
571d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
571e0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
571f0 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20 e OP_Affinity
57200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57210 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 2.#def
57220 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 ine OP_Column
57230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57240 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 3.#d
57250 65 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f 6f 6b efine OP_SetCook
57260 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ie
57270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 4.
57280 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20 #define OP_Real
57290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
572a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
572b0 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 6 /* same as T
572c0 4b 5f 46 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64 K_FLOAT */.#d
572d0 65 66 69 6e 65 20 4f 50 5f 53 65 71 75 65 6e 63 efine OP_Sequenc
572e0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
572f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 5.
57300 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 47 #define OP_MoveG
57310 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
57320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57330 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 65 20 6.#define OP_Ge
57340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57360 20 37 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 72 /* same as
57370 20 54 4b 5f 47 45 20 20 20 20 20 20 20 2a 2f 0a TK_GE */.
57380 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 #define OP_RowKe
57390 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
573a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
573b0 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 43 6f 7.#define OP_SCo
573c0 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 py
573d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
573e0 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 8.#define OP_E
573f0 71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 q
57400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57410 20 20 20 36 38 20 20 20 2f 2a 20 73 61 6d 65 20 68 /* same
57420 61 73 20 54 4b 5f 45 51 20 20 20 20 20 20 20 2a as TK_EQ *
57430 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 /.#define OP_Ope
57440 6e 57 72 69 74 65 20 20 20 20 20 20 20 20 20 20 nWrite
57450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57460 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 9.#define OP_N
57470 6f 74 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 otNull
57480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57490 20 20 20 36 36 20 20 20 2f 2a 20 73 61 6d 65 20 66 /* same
574a0 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 2a as TK_NOTNULL *
574b0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 20 /.#define OP_If
574c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
574d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
574e0 20 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 10.#define OP_T
574f0 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 oInt
57500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57510 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 142 /* same
57520 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 2a as TK_TO_INT *
57530 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 /.#define OP_Str
57540 69 6e 67 38 20 20 20 20 20 20 20 20 20 20 20 20 ing8
57550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57560 20 38 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 88 /* same as
57570 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a 2f 0a TK_STRING */.
57580 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 6f 77 69 #define OP_VRowi
57590 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
575a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
575b0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 1.#define OP_Col
575c0 6c 53 65 71 20 20 20 20 20 20 20 20 20 20 20 20 lSeq
575d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
575e0 20 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 12.#define OP_O
575f0 70 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 20 penRead
57600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57610 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 13.#define OP
57620 5f 45 78 70 69 72 65 20 20 20 20 20 20 20 20 20 _Expire
57630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57640 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 14.#define
57650 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20 OP_AutoCommit
57660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57670 20 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 15.#defin
57680 65 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20 e OP_Gt
57690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
576a0 20 20 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a 69 /*
576b0 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20 same as TK_GT
576c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
576d0 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20 OP_Pagecount
576e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
576f0 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 17.#defin
57700 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b e OP_IntegrityCk
57710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57720 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 18.#def
57730 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 20 20 ine OP_Sort
57740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57750 20 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 19.#d
57760 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20 efine OP_Copy
57770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57780 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 20.
57790 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 63 65 #define OP_Trace
577a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
577b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
577c0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 1.#define OP_Fun
577d0 63 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 ction
577e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
577f0 20 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 22.#define OP_I
57800 66 4e 65 67 20 20 20 20 20 20 20 20 20 20 20 20 fNeg
57810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57820 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 23.#define OP
57830 5f 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 _And
57840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57850 20 20 20 20 20 36 31 20 20 20 2f 2a 20 73 61 6d 61 /* sam
57860 65 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 20 20 e as TK_AND
57870 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 */.#define OP_S
57880 75 62 74 72 61 63 74 20 20 20 20 20 20 20 20 20 ubtract
57890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
578a0 20 20 20 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 79 /* same
578b0 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 2a as TK_MINUS *
578c0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f /.#define OP_Noo
578d0 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p
578e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
578f0 20 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 24.#define OP_R
57900 65 74 75 72 6e 20 20 20 20 20 20 20 20 20 20 20 eturn
57910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57920 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 4f 50 25.#define OP
57930 5f 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20 20 _Remainder
57940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57950 20 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61 6d 82 /* sam
57960 65 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20 20 e as TK_REM
57970 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e */.#define OP_N
57980 65 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 ewRowid
57990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
579a0 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 26.#define OP
579b0 5f 4d 75 6c 74 69 70 6c 79 20 20 20 20 20 20 20 _Multiply
579c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
579d0 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 80 /* sam
579e0 65 20 61 73 20 54 4b 5f 53 54 41 52 20 20 20 20 e as TK_STAR
579f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 */.#define OP_V
57a00 61 72 69 61 62 6c 65 20 20 20 20 20 20 20 20 20 ariable
57a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57a20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 27.#define OP
57a30 5f 53 74 72 69 6e 67 20 20 20 20 20 20 20 20 20 _String
57a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57a50 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 28.#define
57a60 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 OP_RealAffinity
57a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57a80 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 29.#defin
57a90 65 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20 20 20 e OP_VRename
57aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57ab0 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 30.#def
57ac0 69 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 ine OP_ParseSche
57ad0 6d 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ma
57ae0 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 31.#d
57af0 65 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 efine OP_VOpen
57b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57b10 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 32.
57b20 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 #define OP_Close
57b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
57b50 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 3.#define OP_Cre
57b60 61 74 65 49 6e 64 65 78 20 20 20 20 20 20 20 20 ateIndex
57b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57b80 20 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 34.#define OP_I
57b90 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 20 sUnique
57ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57bb0 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 35.#define OP
57bc0 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 20 20 _NotFound
57bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57be0 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 36.#define
57bf0 4f 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 OP_Int64
57c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57c10 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 37.#defin
57c20 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20 20 e OP_MustBeInt
57c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57c40 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 38.#def
57c50 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20 20 20 20 ine OP_Halt
57c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57c70 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 39.#d
57c80 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20 efine OP_Rowid
57c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 40.
57cb0 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54 #define OP_IdxLT
57cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
57ce0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 1.#define OP_Add
57cf0 49 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 Imm
57d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57d10 20 34 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 42.#define OP_S
57d20 74 61 74 65 6d 65 6e 74 20 20 20 20 20 20 20 20 tatement
57d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57d40 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 4f 50 43.#define OP
57d50 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 20 _RowData
57d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57d70 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 44.#define
57d80 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20 20 OP_MemMax
57d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57da0 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 45.#defin
57db0 65 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20 e OP_Or
57dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57dd0 20 20 20 20 20 20 20 20 20 36 30 20 20 20 2f 2a 60 /*
57de0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20 same as TK_OR
57df0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
57e00 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 OP_NotExists
57e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57e20 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 46.#defin
57e30 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20 e OP_Gosub
57e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57e50 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 47.#def
57e60 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20 ine OP_Divide
57e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57e80 20 20 20 20 20 20 20 20 20 20 20 38 31 20 20 20 81
57e90 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c /* same as TK_SL
57ea0 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e ASH */.#defin
57eb0 65 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20 e OP_Integer
57ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57ed0 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 48.#def
57ee0 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 ine OP_ToNumeric
57ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57f00 20 20 20 20 20 20 20 20 20 20 31 34 31 20 20 20 141
57f10 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
57f20 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69 _NUMERIC*/.#defi
57f30 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 ne OP_Prev
57f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57f50 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 49.#de
57f60 66 69 6e 65 20 4f 50 5f 43 6f 6e 63 61 74 20 20 fine OP_Concat
57f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57f80 20 20 20 20 20 20 20 20 20 20 20 20 38 33 20 20 83
57f90 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 /* same as TK_C
57fa0 4f 4e 43 41 54 20 20 20 2a 2f 0a 23 64 65 66 69 ONCAT */.#defi
57fb0 6e 65 20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20 ne OP_BitAnd
57fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57fd0 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 74 /
57fe0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 * same as TK_BIT
57ff0 41 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 AND */.#define
58000 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 OP_VColumn
58010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58020 20 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 50.#defi
58030 6e 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c ne OP_CreateTabl
58040 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
58050 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 51.#de
58060 66 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 fine OP_Last
58070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58080 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 52.#
58090 64 65 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c define OP_IsNull
580a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
580b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 65
580c0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
580d0 5f 49 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 _ISNULL */.#de
580e0 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 fine OP_IncrVacu
580f0 75 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 um
58100 20 20 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 53.#
58110 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77 define OP_IdxRow
58120 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 id
58130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34 54
58140 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 .#define OP_Shif
58150 74 52 69 67 68 74 20 20 20 20 20 20 20 20 20 20 tRight
58160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58170 37 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 77 /* same as
58180 54 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23 TK_RSHIFT */.#
58190 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 define OP_ResetC
581a0 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 ount
581b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 55
581c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f .#define OP_Fifo
581d0 57 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20 Write
581e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
581f0 35 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 56.#define OP_Co
58200 6e 74 65 78 74 50 75 73 68 20 20 20 20 20 20 20 ntextPush
58210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58220 20 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 57.#define OP_
58230 59 69 65 6c 64 20 20 20 20 20 20 20 20 20 20 20 Yield
58240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58250 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f 58.#define O
58260 50 5f 44 72 6f 70 54 72 69 67 67 65 72 20 20 20 P_DropTrigger
58270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58280 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 59.#define
58290 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 OP_DropIndex
582a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
582b0 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 62.#defi
582c0 6e 65 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20 ne OP_IdxGE
582d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
582e0 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 63.#de
582f0 66 69 6e 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 fine OP_IdxDelet
58300 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
58310 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64.#
58320 64 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d define OP_Vacuum
58330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 33 73
58350 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 .#define OP_Move
58360 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Le
58370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58380 38 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 84.#define OP_If
58390 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 Not
583a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
583b0 20 20 38 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 85.#define OP_
583c0 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20 DropTable
583d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
583e0 20 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 4f 86.#define O
583f0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20 P_MakeRecord
58400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58410 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 89.#define
58420 20 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 OP_ToBlob
58430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58440 20 20 20 20 20 20 20 31 34 30 20 20 20 2f 2a 20 140 /*
58450 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c same as TK_TO_BL
58460 4f 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f OB */.#define O
58470 50 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20 20 P_ResultRow
58480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58490 20 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 90.#define
584a0 20 4f 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 OP_Delete
584b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
584c0 20 20 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 91.#defi
584d0 6e 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 ne OP_AggFinal
584e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
584f0 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 92.#de
58500 66 69 6e 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 fine OP_Compare
58510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58520 20 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 93.#
58530 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c define OP_ShiftL
58540 65 66 74 20 20 20 20 20 20 20 20 20 20 20 20 20 eft
58550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 76
58560 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
58570 5f 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 _LSHIFT */.#de
58580 66 69 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 fine OP_Goto
58590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
585a0 20 20 20 20 20 20 20 20 20 20 20 20 39 34 0a 23 94.#
585b0 64 65 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c define OP_TableL
585c0 6f 63 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 ock
585d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 35 95
585e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f .#define OP_Fifo
585f0 52 65 61 64 20 20 20 20 20 20 20 20 20 20 20 20 Read
58600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58610 39 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 96.#define OP_Cl
58620 65 61 72 20 20 20 20 20 20 20 20 20 20 20 20 20 ear
58630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58640 20 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 97.#define OP_
58650 4d 6f 76 65 4c 74 20 20 20 20 20 20 20 20 20 20 MoveLt
58660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58670 20 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 98.#define O
58680 50 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 P_Le
58690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
586a0 20 20 20 20 20 20 37 30 20 20 20 2f 2a 20 73 61 70 /* sa
586b0 6d 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20 20 me as TK_LE
586c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
586d0 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 VerifyCookie
586e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
586f0 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 4f 99.#define O
58700 50 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20 P_AggStep
58710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58720 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 100.#define
58730 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20 OP_ToText
58740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58750 20 20 20 20 20 20 20 31 33 39 20 20 20 2f 2a 20 139 /*
58760 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 same as TK_TO_TE
58770 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f XT */.#define O
58780 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 P_Not
58790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
587a0 20 20 20 20 20 20 31 36 20 20 20 2f 2a 20 73 61 16 /* sa
587b0 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 me as TK_NOT
587c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
587d0 54 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 ToReal
587e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
587f0 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 143 /* same
58800 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 as TK_TO_REAL
58810 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 */.#define OP_Se
58820 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 20 20 20 20 tNumColumns
58830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58840 20 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 101.#define OP_
58850 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 Transaction
58860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58870 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f 102.#define O
58880 50 5f 56 46 69 6c 74 65 72 20 20 20 20 20 20 20 P_VFilter
58890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
588a0 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 103.#define
588b0 20 4f 50 5f 4e 65 20 20 20 20 20 20 20 20 20 20 OP_Ne
588c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
588d0 20 20 20 20 20 20 20 20 36 37 20 20 20 2f 2a 20 67 /*
588e0 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20 20 20 same as TK_NE
588f0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
58900 50 5f 56 44 65 73 74 72 6f 79 20 20 20 20 20 20 P_VDestroy
58910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58920 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 104.#define
58930 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 20 20 OP_ContextPop
58940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58950 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 105.#defi
58960 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 ne OP_BitOr
58970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58980 20 20 20 20 20 20 20 20 20 20 37 35 20 20 20 2f 75 /
58990 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 * same as TK_BIT
589a0 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 OR */.#define
589b0 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 20 20 OP_Next
589c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
589d0 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 106.#defi
589e0 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 20 ne OP_IdxInsert
589f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58a00 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 107.#de
58a10 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20 20 fine OP_Lt
58a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58a30 20 20 20 20 20 20 20 20 20 20 20 20 37 31 20 20 71
58a40 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
58a50 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 T */.#defi
58a60 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20 20 20 20 ne OP_Insert
58a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58a80 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 65 108.#de
58a90 66 69 6e 65 20 4f 50 5f 44 65 73 74 72 6f 79 20 fine OP_Destroy
58aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58ab0 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 109.#
58ac0 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 64 43 6f define OP_ReadCo
58ad0 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 okie
58ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 110
58af0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 72 63 .#define OP_Forc
58b00 65 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 eInt
58b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
58b20 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 6f 11.#define OP_Lo
58b30 61 64 41 6e 61 6c 79 73 69 73 20 20 20 20 20 20 adAnalysis
58b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58b50 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 112.#define OP_
58b60 45 78 70 6c 61 69 6e 20 20 20 20 20 20 20 20 20 Explain
58b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58b80 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 4f 113.#define O
58b90 50 5f 4f 70 65 6e 50 73 65 75 64 6f 20 20 20 20 P_OpenPseudo
58ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58bb0 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 114.#define
58bc0 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 OP_OpenEphemera
58bd0 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l
58be0 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 115.#defi
58bf0 6e 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20 20 20 ne OP_Null
58c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58c10 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65 116.#de
58c20 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20 fine OP_Move
58c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58c40 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 117.#
58c50 64 65 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 define OP_Blob
58c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58c70 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 118
58c80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 20 .#define OP_Add
58c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58cb0 37 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 78 /* same as
58cc0 54 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0a 23 TK_PLUS */.#
58cd0 64 65 66 69 6e 65 20 4f 50 5f 52 65 77 69 6e 64 define OP_Rewind
58ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39 119
58d00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 .#define OP_Move
58d10 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Ge
58d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
58d30 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 42 20.#define OP_VB
58d40 65 67 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 egin
58d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58d60 20 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 121.#define OP_
58d70 56 55 70 64 61 74 65 20 20 20 20 20 20 20 20 20 VUpdate
58d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58d90 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 122.#define O
58da0 50 5f 49 66 5a 65 72 6f 20 20 20 20 20 20 20 20 P_IfZero
58db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58dc0 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 123.#define
58dd0 20 4f 50 5f 42 69 74 4e 6f 74 20 20 20 20 20 20 OP_BitNot
58de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58df0 20 20 20 20 20 20 20 20 38 37 20 20 20 2f 2a 20 87 /*
58e00 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f same as TK_BITNO
58e10 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f T */.#define O
58e20 50 5f 56 43 72 65 61 74 65 20 20 20 20 20 20 20 P_VCreate
58e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58e40 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 124.#define
58e50 20 4f 50 5f 46 6f 75 6e 64 20 20 20 20 20 20 20 OP_Found
58e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58e70 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69 125.#defi
58e80 6e 65 20 4f 50 5f 49 66 50 6f 73 20 20 20 20 20 ne OP_IfPos
58e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58ea0 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 127.#de
58eb0 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 fine OP_NullRow
58ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58ed0 20 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 128.#
58ee0 64 65 66 69 6e 65 20 4f 50 5f 4a 75 6d 70 20 20 define OP_Jump
58ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58f00 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 129
58f10 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 65 72 6d .#define OP_Perm
58f20 75 74 61 74 69 6f 6e 20 20 20 20 20 20 20 20 20 utation
58f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
58f40 33 30 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 30../* The follo
58f50 77 69 6e 67 20 6f 70 63 6f 64 65 20 76 61 6c 75 wing opcode valu
58f60 65 73 20 61 72 65 20 6e 65 76 65 72 20 75 73 65 es are never use
58f70 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f d */.#define OP_
58f80 4e 6f 74 55 73 65 64 5f 31 33 31 20 20 20 20 20 NotUsed_131
58f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58fa0 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 131.#define O
58fb0 50 5f 4e 6f 74 55 73 65 64 5f 31 33 32 20 20 20 P_NotUsed_132
58fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58fd0 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 132.#define
58fe0 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 33 20 OP_NotUsed_133
58ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59000 20 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 133.#defi
59010 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 ne OP_NotUsed_13
59020 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4
59030 20 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 134.#de
59040 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
59050 31 33 35 20 20 20 20 20 20 20 20 20 20 20 20 20 135
59060 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 135.#
59070 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 define OP_NotUse
59080 64 5f 31 33 36 20 20 20 20 20 20 20 20 20 20 20 d_136
59090 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 136
590a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
590b0 73 65 64 5f 31 33 37 20 20 20 20 20 20 20 20 20 sed_137
590c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
590d0 33 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 37.#define OP_No
590e0 74 55 73 65 64 5f 31 33 38 20 20 20 20 20 20 20 tUsed_138
590f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59100 20 31 33 38 0a 0a 0a 2f 2a 20 50 72 6f 70 65 72 138.../* Proper
59110 74 69 65 73 20 73 75 63 68 20 61 73 20 22 6f 75 ties such as "ou
59120 74 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 74 68 t2" or "jump" th
59130 61 74 20 61 72 65 20 73 70 65 63 69 66 69 65 64 at are specified
59140 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 in.** comments
59150 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 following the "c
59160 61 73 65 22 20 66 6f 72 20 65 61 63 68 20 6f 70 ase" for each op
59170 63 6f 64 65 20 69 6e 20 74 68 65 20 76 64 62 65 code in the vdbe
59180 2e 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f 64 65 .c.** are encode
59190 64 20 69 6e 74 6f 20 62 69 74 76 65 63 74 6f 72 d into bitvector
591a0 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2f s as follows:.*/
591b0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4a .#define OPFLG_J
591c0 55 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 30 UMP 0
591d0 78 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70 3a 20 x0001 /* jump:
591e0 20 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20 74 61 P2 holds jmp ta
591f0 72 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 rget */.#define
59200 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 OPFLG_OUT2_PRERE
59210 4c 45 41 53 45 20 30 78 30 30 30 32 20 20 2f 2a LEASE 0x0002 /*
59220 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
59230 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 : */.#define OPF
59240 4c 47 5f 49 4e 31 20 20 20 20 20 20 20 20 20 20 LG_IN1
59250 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 69 6e 0x0004 /* in
59260 31 3a 20 20 20 50 31 20 69 73 20 61 6e 20 69 6e 1: P1 is an in
59270 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f put */.#define O
59280 50 46 4c 47 5f 49 4e 32 20 20 20 20 20 20 20 20 PFLG_IN2
59290 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 0x0008 /*
592a0 69 6e 32 3a 20 20 20 50 32 20 69 73 20 61 6e 20 in2: P2 is an
592b0 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 input */.#define
592c0 20 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20 20 20 OPFLG_IN3
592d0 20 20 20 20 20 20 20 30 78 30 30 31 30 20 20 2f 0x0010 /
592e0 2a 20 69 6e 33 3a 20 20 20 50 33 20 69 73 20 61 * in3: P3 is a
592f0 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 n input */.#defi
59300 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20 ne OPFLG_OUT3
59310 20 20 20 20 20 20 20 20 20 30 78 30 30 32 30 20 0x0020
59320 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69 73 /* out3: P3 is
59330 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64 an output */.#d
59340 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54 efine OPFLG_INIT
59350 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20 IALIZER {\./*
59360 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 0 */ 0x00, 0x01,
59370 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 31 0x00, 0x00, 0x1
59380 30 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 20 30 0, 0x02, 0x11, 0
59390 78 30 30 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20 x00,\./* 8 */
593a0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 0x00, 0x00, 0x05
593b0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 , 0x02, 0x00, 0x
593c0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 00, 0x00, 0x00,\
593d0 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 34 2c ./* 16 */ 0x04,
593e0 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 0x02, 0x00, 0x0
593f0 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 1, 0x00, 0x00, 0
59400 78 30 30 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 x00, 0x05,\./*
59410 32 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 34 24 */ 0x00, 0x04
59420 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
59430 30 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 02, 0x04, 0x00,
59440 30 78 30 30 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f 0x00,\./* 32 */
59450 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
59460 32 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 2, 0x11, 0x11, 0
59470 78 30 32 2c 20 30 78 30 35 2c 20 30 78 30 30 2c x02, 0x05, 0x00,
59480 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 30 32 \./* 40 */ 0x02
59490 2c 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78 , 0x11, 0x04, 0x
594a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 63 2c 20 00, 0x00, 0x0c,
594b0 30 78 31 31 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 0x11, 0x01,\./*
594c0 20 34 38 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 48 */ 0x02, 0x0
594d0 31 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 1, 0x00, 0x02, 0
594e0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 32 2c x01, 0x01, 0x02,
594f0 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 35 36 20 2a 0x00,\./* 56 *
59500 2f 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 / 0x04, 0x00, 0x
59510 30 30 2c 20 30 78 30 30 2c 20 30 78 32 63 2c 20 00, 0x00, 0x2c,
59520 30 78 32 63 2c 20 30 78 30 30 2c 20 30 78 31 31 0x2c, 0x00, 0x11
59530 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78 30 ,\./* 64 */ 0x0
59540 30 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30 0, 0x05, 0x05, 0
59550 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c x15, 0x15, 0x15,
59560 20 30 78 31 35 2c 20 30 78 31 35 2c 5c 0a 2f 2a 0x15, 0x15,\./*
59570 20 20 37 32 20 2a 2f 20 30 78 31 35 2c 20 30 78 72 */ 0x15, 0x
59580 30 30 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 00, 0x2c, 0x2c,
59590 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 0x2c, 0x2c, 0x2c
595a0 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 38 30 20 , 0x2c,\./* 80
595b0 2a 2f 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 */ 0x2c, 0x2c, 0
595c0 78 32 63 2c 20 30 78 32 63 2c 20 30 78 31 31 2c x2c, 0x2c, 0x11,
595d0 20 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 0x05, 0x00, 0x0
595e0 34 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 78 4,\./* 88 */ 0x
595f0 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 02, 0x00, 0x00,
59600 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
59610 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 5c 0a 2f , 0x01, 0x00,\./
59620 2a 20 20 39 36 20 2a 2f 20 30 78 30 31 2c 20 30 * 96 */ 0x01, 0
59630 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 30 2c x00, 0x11, 0x00,
59640 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
59650 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 31 30 34 0, 0x01,\./* 104
59660 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 */ 0x00, 0x00,
59670 30 78 30 31 2c 20 30 78 30 38 2c 20 30 78 30 30 0x01, 0x08, 0x00
59680 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
59690 30 35 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 05,\./* 112 */ 0
596a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
596b0 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 0x00, 0x02, 0x0
596c0 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 5c 0a 0, 0x02, 0x01,\.
596d0 2f 2a 20 31 32 30 20 2a 2f 20 30 78 31 31 2c 20 /* 120 */ 0x11,
596e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 0x00, 0x00, 0x05
596f0 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 , 0x00, 0x11, 0x
59700 30 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 31 32 02, 0x05,\./* 12
59710 38 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 8 */ 0x00, 0x01,
59720 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
59730 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
59740 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20 x00,\./* 136 */
59750 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
59760 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78 , 0x04, 0x04, 0x
59770 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 5c 04, 0x04, 0x04,\
59780 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
59790 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 *** End of opcod
597a0 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a es.h ***********
597b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
597c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
597d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
597e0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
597f0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
59800 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a in vdbe.h *****
59810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59820 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f **/../*.** Proto
59830 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 types for the VD
59840 42 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 BE interface. S
59850 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 ee comments on t
59860 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
59870 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 n.** for a descr
59880 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 iption of what e
59890 61 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 ach of these rou
598a0 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 tines does..*/.S
598b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
598c0 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 be *sqlite3VdbeC
598d0 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b reate(sqlite3*);
598e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
598f0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
59900 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 ddOp0(Vdbe*,int)
59910 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
59920 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
59930 41 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 AddOp1(Vdbe*,int
59940 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
59950 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
59960 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 3VdbeAddOp2(Vdbe
59970 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a *,int,int,int);.
59980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
59990 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
599a0 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 dOp3(Vdbe*,int,i
599b0 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c nt,int,int);.SQL
599c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
599d0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
599e0 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 4(Vdbe*,int,int,
599f0 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 int,int,const ch
59a00 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 ar *zP4,int);.SQ
59a10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
59a20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
59a30 70 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 pList(Vdbe*, int
59a40 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 nOp, VdbeOpList
59a50 20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 const *aOp);.SQ
59a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
59a70 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 d sqlite3VdbeCha
59a80 6e 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 ngeP1(Vdbe*, int
59a90 20 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a addr, int P1);.
59aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
59ab0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
59ac0 68 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 hangeP2(Vdbe*, i
59ad0 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 nt addr, int P2)
59ae0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
59af0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
59b00 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c eChangeP3(Vdbe*,
59b10 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 int addr, int P
59b20 33 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 3);.SQLITE_PRIVA
59b30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
59b40 64 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 dbeChangeP5(Vdbe
59b50 2a 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 *, u8 P5);.SQLIT
59b60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
59b70 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
59b80 72 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 re(Vdbe*, int ad
59b90 64 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 dr);.SQLITE_PRIV
59ba0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
59bb0 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 VdbeChangeToNoop
59bc0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 (Vdbe*, int addr
59bd0 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 , int N);.SQLITE
59be0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
59bf0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
59c00 34 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 4(Vdbe*, int add
59c10 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a r, const char *z
59c20 50 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 P4, int N);.SQLI
59c30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
59c40 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 sqlite3VdbeUsesB
59c50 74 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 tree(Vdbe*, int)
59c60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
59c70 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 VdbeOp *sqlite3
59c80 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c VdbeGetOp(Vdbe*,
59c90 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
59ca0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
59cb0 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 3VdbeMakeLabel(V
59cc0 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
59cd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
59ce0 65 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 e3VdbeDelete(Vdb
59cf0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
59d00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
59d10 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 VdbeMakeReady(Vd
59d20 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c be*,int,int,int,
59d30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
59d40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
59d50 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 VdbeFinalize(Vdb
59d60 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
59d70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
59d80 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c VdbeResolveLabel
59d90 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 (Vdbe*, int);.SQ
59da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
59db0 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
59dc0 65 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a entAddr(Vdbe*);.
59dd0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
59de0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
59df0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
59e00 33 56 64 62 65 54 72 61 63 65 28 56 64 62 65 2a 3VdbeTrace(Vdbe*
59e10 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a ,FILE*);.#endif.
59e20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
59e30 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 oid sqlite3VdbeR
59e40 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 esetStepResult(V
59e50 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
59e60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
59e70 33 56 64 62 65 52 65 73 65 74 28 56 64 62 65 2a 3VdbeReset(Vdbe*
59e80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
59e90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
59ea0 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 beSetNumCols(Vdb
59eb0 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
59ec0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
59ed0 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d te3VdbeSetColNam
59ee0 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e e(Vdbe*, int, in
59ef0 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c t, const char *,
59f00 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 void(*)(void*))
59f10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
59f20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
59f30 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 eCountChanges(Vd
59f40 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
59f50 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 VATE sqlite3 *sq
59f60 6c 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65 lite3VdbeDb(Vdbe
59f70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
59f80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
59f90 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 2a 2c dbeSetSql(Vdbe*,
59fa0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
59fb0 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 int n);.SQLITE_P
59fc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
59fd0 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 te3VdbeSwap(Vdbe
59fe0 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66 64 65 *,Vdbe*);..#ifde
59ff0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
5a000 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e MEMORY_MANAGEMEN
5a010 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
5a020 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
5a030 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e ReleaseMemory(in
5a040 74 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 t);.#endif.SQLIT
5a050 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b E_PRIVATE Unpack
5a060 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 edRecord *sqlite
5a070 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 3VdbeRecordUnpac
5a080 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 2c 63 k(KeyInfo*,int,c
5a090 6f 6e 73 74 20 76 6f 69 64 2a 2c 0a 20 20 20 20 onst void*,.
5a0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a0c0 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f UnpackedReco
5a0d0 72 64 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 rd*,int);.SQLITE
5a0e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5a0f0 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 lite3VdbeDeleteU
5a100 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e npackedRecord(Un
5a110 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a packedRecord*);.
5a120 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5a130 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 nt sqlite3VdbeRe
5a140 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c cordCompare(int,
5a150 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61 const void*,Unpa
5a160 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a ckedRecord*);...
5a170 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 #ifndef NDEBUG.S
5a180 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
5a190 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
5a1a0 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63 Comment(Vdbe*, c
5a1b0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
5a1c0 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 ;.# define VdbeC
5a1d0 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 omment(X) sqlit
5a1e0 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a e3VdbeComment X.
5a1f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
5a200 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
5a210 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 eNoopComment(Vdb
5a220 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c e*, const char*,
5a230 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 ...);.# define
5a240 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 VdbeNoopComment(
5a250 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 4e X) sqlite3VdbeN
5a260 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c oopComment X.#el
5a270 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 se.# define Vdbe
5a280 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 66 Comment(X).# def
5a290 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ine VdbeNoopComm
5a2a0 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 ent(X).#endif..#
5a2b0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
5a2c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 ****** End of vd
5a2d0 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a be.h ***********
5a2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5a2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5a300 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
5a310 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
5a320 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
5a330 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 off in sqliteInt
5a340 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
5a350 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
5a360 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 ****** Include p
5a370 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 ager.h in the mi
5a380 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
5a390 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
5a3a0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
5a3b0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
5a3c0 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a e pager.h ******
5a3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5a3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5a3f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
5a400 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
5a410 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
5a420 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
5a430 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
5a440 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
5a450 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
5a460 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
5a470 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
5a480 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
5a490 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
5a4a0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
5a4b0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
5a4c0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
5a4d0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
5a4e0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
5a4f0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
5a500 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
5a510 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
5a520 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
5a530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5a540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5a550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5a560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
5a570 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c This header fil
5a580 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e e defines the in
5a590 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 terface that the
5a5a0 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 sqlite page cac
5a5b0 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e he.** subsystem.
5a5c0 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 The page cache
5a5d0 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 73 subsystem reads
5a5e0 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 69 and writes a fi
5a5f0 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 20 le a page.** at
5a600 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 69 a time and provi
5a610 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 6f des a journal fo
5a620 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a r rollback..**.*
5a630 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 * @(#) $Id: page
5a640 72 2e 68 2c 76 20 31 2e 38 36 20 32 30 30 38 2f r.h,v 1.86 2008/
5a650 31 30 2f 31 37 20 31 38 3a 35 31 3a 35 33 20 64 10/17 18:51:53 d
5a660 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
5a670 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 .*/..#ifndef _PA
5a680 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f GER_H_.#define _
5a690 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 PAGER_H_../*.**
5a6a0 49 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f If defined as no
5a6b0 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 n-zero, auto-vac
5a6c0 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 uum is enabled b
5a6d0 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 y default. Other
5a6e0 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 wise.** it must
5a6f0 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 be turned on for
5a700 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 each database u
5a710 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 sing "PRAGMA aut
5a720 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a o_vacuum = 1"..*
5a730 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
5a740 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c _DEFAULT_JOURNAL
5a750 5f 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 _SIZE_LIMIT. #d
5a760 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
5a770 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a AULT_JOURNAL_SIZ
5a780 45 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 E_LIMIT -1.#endi
5a790 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 f../*.** The typ
5a7a0 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 e used to repres
5a7b0 65 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65 ent a page numbe
5a7c0 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 r. The first pa
5a7d0 67 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 ge in a file.**
5a7e0 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 is called page 1
5a7f0 2e 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20 . 0 is used to
5a800 72 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 represent "not a
5a810 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 page"..*/.typed
5a820 65 66 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a ef u32 Pgno;../*
5a830 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69 .** Each open fi
5a840 6c 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 le is managed by
5a850 20 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 a separate inst
5a860 61 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67 ance of the "Pag
5a870 65 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a er" structure..*
5a880 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
5a890 20 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f Pager Pager;../
5a8a0 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 *.** Handle type
5a8b0 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 for pages..*/.t
5a8c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 ypedef struct Pg
5a8d0 48 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a Hdr DbPage;../*.
5a8e0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
5a8f0 73 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 20 s for the flags
5a900 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
5a910 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 29 2e ite3PagerOpen().
5a920 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 68 69 .**.** NOTE: Thi
5a930 73 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 s values must ma
5a940 74 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f tch the correspo
5a950 6e 64 69 6e 67 20 42 54 52 45 45 5f 20 76 61 6c nding BTREE_ val
5a960 75 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a ues in btree.h..
5a970 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
5a980 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30 _OMIT_JOURNAL 0
5a990 78 30 30 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e x0001 /* Do n
5a9a0 6f 74 20 75 73 65 20 61 20 72 6f 6c 6c 62 61 63 ot use a rollbac
5a9b0 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 k journal */.#de
5a9c0 66 69 6e 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45 fine PAGER_NO_RE
5a9d0 41 44 4c 4f 43 4b 20 20 20 30 78 30 30 30 32 20 ADLOCK 0x0002
5a9e0 20 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c /* Omit readl
5a9f0 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 ocks on readonly
5aa00 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a files */../*.**
5aa10 20 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f Valid values fo
5aa20 72 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 r the second arg
5aa30 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
5aa40 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 PagerLockingMode
5aa50 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ()..*/.#define P
5aa60 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
5aa70 5f 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23 _QUERY -1.#
5aa80 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 define PAGER_LOC
5aa90 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 KINGMODE_NORMAL
5aaa0 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 0.#define P
5aab0 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
5aac0 5f 45 58 43 4c 55 53 49 56 45 20 20 20 31 0a 0a _EXCLUSIVE 1..
5aad0 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 /*.** Valid valu
5aae0 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e es for the secon
5aaf0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 d argument to sq
5ab00 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
5ab10 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 lMode()..*/.#def
5ab20 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
5ab30 4c 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 LMODE_QUERY
5ab40 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 -1.#define PAGE
5ab50 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 R_JOURNALMODE_DE
5ab60 4c 45 54 45 20 20 20 20 20 20 30 20 20 20 2f 2a LETE 0 /*
5ab70 20 43 6f 6d 6d 69 74 20 62 79 20 64 65 6c 65 74 Commit by delet
5ab80 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ing journal file
5ab90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
5aba0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 R_JOURNALMODE_PE
5abb0 52 53 49 53 54 20 20 20 20 20 31 20 20 20 2f 2a RSIST 1 /*
5abc0 20 43 6f 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69 Commit by zeroi
5abd0 6e 67 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 ng journal heade
5abe0 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 r */.#define PAG
5abf0 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
5ac00 46 46 20 20 20 20 20 20 20 20 20 32 20 20 20 2f FF 2 /
5ac10 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65 * Journal omitte
5ac20 64 2e 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 d. */.#define P
5ac30 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
5ac40 5f 54 52 55 4e 43 41 54 45 20 20 20 20 33 20 20 _TRUNCATE 3
5ac50 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 74 72 /* Commit by tr
5ac60 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c uncating journal
5ac70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
5ac80 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
5ac90 4d 4f 52 59 20 20 20 20 20 20 34 20 20 20 2f 2a MORY 4 /*
5aca0 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e In-memory journ
5acb0 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a al file */../*.*
5acc0 2a 20 53 65 65 20 73 6f 75 72 63 65 20 63 6f 64 * See source cod
5acd0 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 61 e comments for a
5ace0 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 detailed descri
5acf0 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 6c ption of the fol
5ad00 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e lowing.** routin
5ad10 65 73 3a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es:.*/.SQLITE_PR
5ad20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5ad30 33 50 61 67 65 72 4f 70 65 6e 28 73 71 6c 69 74 3PagerOpen(sqlit
5ad40 65 33 5f 76 66 73 20 2a 2c 20 50 61 67 65 72 20 e3_vfs *, Pager
5ad50 2a 2a 70 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 **ppPager, const
5ad60 20 63 68 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c char*, int,int,
5ad70 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
5ad80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5ad90 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 3PagerSetBusyhan
5ada0 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 42 75 73 dler(Pager*, Bus
5adb0 79 48 61 6e 64 6c 65 72 20 2a 70 42 75 73 79 48 yHandler *pBusyH
5adc0 61 6e 64 6c 65 72 29 3b 0a 53 51 4c 49 54 45 5f andler);.SQLITE_
5add0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
5ade0 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69 6e ite3PagerSetRein
5adf0 69 74 65 72 28 50 61 67 65 72 2a 2c 20 76 6f 69 iter(Pager*, voi
5ae00 64 28 2a 29 28 44 62 50 61 67 65 2a 29 29 3b 0a d(*)(DbPage*));.
5ae10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5ae20 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
5ae30 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 etPagesize(Pager
5ae40 2a 2c 20 75 31 36 2a 29 3b 0a 53 51 4c 49 54 45 *, u16*);.SQLITE
5ae50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
5ae60 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 ite3PagerMaxPage
5ae70 43 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e Count(Pager*, in
5ae80 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
5ae90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
5aea0 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 gerReadFileheade
5aeb0 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 r(Pager*, int, u
5aec0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a nsigned char*);.
5aed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5aee0 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
5aef0 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 SetCachesize(Pag
5af00 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 er*, int);.SQLIT
5af10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5af20 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 lite3PagerClose(
5af30 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a Pager *pPager);.
5af40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5af50 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 nt sqlite3PagerA
5af60 63 71 75 69 72 65 28 50 61 67 65 72 20 2a 70 50 cquire(Pager *pP
5af70 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c ager, Pgno pgno,
5af80 20 44 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 DbPage **ppPage
5af90 2c 20 69 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a , int clrFlag);.
5afa0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 #define sqlite3P
5afb0 61 67 65 72 47 65 74 28 41 2c 42 2c 43 29 20 73 agerGet(A,B,C) s
5afc0 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 qlite3PagerAcqui
5afd0 72 65 28 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 re(A,B,C,0).SQLI
5afe0 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 TE_PRIVATE DbPag
5aff0 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c e *sqlite3PagerL
5b000 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 ookup(Pager *pPa
5b010 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b ger, Pgno pgno);
5b020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5b030 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
5b040 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 PageRefcount(DbP
5b050 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 age*);.SQLITE_PR
5b060 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5b070 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 65 3PagerRef(DbPage
5b080 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5b090 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
5b0a0 67 65 72 55 6e 72 65 66 28 44 62 50 61 67 65 2a gerUnref(DbPage*
5b0b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5b0c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
5b0d0 65 72 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 erWrite(DbPage*)
5b0e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5b0f0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
5b100 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 rPagecount(Pager
5b110 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 *, int*);.SQLITE
5b120 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
5b130 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
5b140 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a e(Pager*,Pgno);.
5b150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5b160 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 nt sqlite3PagerB
5b170 65 67 69 6e 28 44 62 50 61 67 65 2a 2c 20 69 6e egin(DbPage*, in
5b180 74 20 65 78 46 6c 61 67 29 3b 0a 53 51 4c 49 54 t exFlag);.SQLIT
5b190 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5b1a0 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
5b1b0 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c PhaseOne(Pager*,
5b1c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
5b1d0 74 65 72 2c 20 50 67 6e 6f 2c 20 69 6e 74 29 3b ter, Pgno, int);
5b1e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5b1f0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
5b200 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 CommitPhaseTwo(P
5b210 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
5b220 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5b230 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
5b240 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
5b250 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5b260 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e te3PagerIsreadon
5b270 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 ly(Pager*);.SQLI
5b280 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5b290 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 42 qlite3PagerStmtB
5b2a0 65 67 69 6e 28 50 61 67 65 72 2a 29 3b 0a 53 51 egin(Pager*);.SQ
5b2b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5b2c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d sqlite3PagerStm
5b2d0 74 43 6f 6d 6d 69 74 28 50 61 67 65 72 2a 29 3b tCommit(Pager*);
5b2e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5b2f0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
5b300 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 50 61 67 StmtRollback(Pag
5b310 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
5b320 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5b330 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 3PagerDontRollba
5b340 63 6b 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c ck(DbPage*);.SQL
5b350 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5b360 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
5b370 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a Write(DbPage*);.
5b380 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5b390 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 nt sqlite3PagerR
5b3a0 65 66 63 6f 75 6e 74 28 50 61 67 65 72 2a 29 3b efcount(Pager*);
5b3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5b3c0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
5b3d0 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 rSetSafetyLevel(
5b3e0 50 61 67 65 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b Pager*,int,int);
5b3f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5b400 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
5b410 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 te3PagerFilename
5b420 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
5b430 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
5b440 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 qlite3_vfs *sqli
5b450 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65 te3PagerVfs(Page
5b460 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
5b470 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ATE sqlite3_file
5b480 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
5b490 6c 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 le(Pager*);.SQLI
5b4a0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
5b4b0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 char *sqlite3Pa
5b4c0 67 65 72 44 69 72 6e 61 6d 65 28 50 61 67 65 72 gerDirname(Pager
5b4d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5b4e0 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
5b4f0 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
5b500 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a alname(Pager*);.
5b510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5b520 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e nt sqlite3PagerN
5b530 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a 53 osync(Pager*);.S
5b540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5b550 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f t sqlite3PagerMo
5b560 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62 vepage(Pager*,Db
5b570 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b Page*,Pgno,int);
5b580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5b590 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
5b5a0 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67 65 erGetData(DbPage
5b5b0 20 2a 29 3b 20 0a 53 51 4c 49 54 45 5f 50 52 49 *); .SQLITE_PRI
5b5c0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
5b5d0 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
5b5e0 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 DbPage *); .SQLI
5b5f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5b600 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 qlite3PagerLocki
5b610 6e 67 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 ngMode(Pager *,
5b620 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
5b630 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5b640 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 PagerJournalMode
5b650 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a (Pager *, int);.
5b660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5b670 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 64 sqlite3PagerJ
5b680 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 ournalSizeLimit(
5b690 50 61 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 Pager *, i64);.S
5b6a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
5b6b0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
5b6c0 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a TempSpace(Pager*
5b6d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5b6e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
5b6f0 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 erSync(Pager *pP
5b700 61 67 65 72 29 3b 0a 0a 23 69 66 64 65 66 20 53 ager);..#ifdef S
5b710 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a QLITE_HAS_CODEC.
5b720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
5b730 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
5b740 65 72 53 65 74 43 6f 64 65 63 28 50 61 67 65 72 erSetCodec(Pager
5b750 2a 2c 76 6f 69 64 2a 28 2a 29 28 76 6f 69 64 2a *,void*(*)(void*
5b760 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 ,void*,Pgno,int)
5b770 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a ,void*);.#endif.
5b780 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
5b790 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 EBUG) || defined
5b7a0 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 (SQLITE_TEST).SQ
5b7b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 50 LITE_PRIVATE P
5b7c0 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 gno sqlite3Pager
5b7d0 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67 Pagenumber(DbPag
5b7e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
5b7f0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
5b800 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
5b810 65 28 44 62 50 61 67 65 2a 29 3b 0a 23 65 6e 64 e(DbPage*);.#end
5b820 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
5b830 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
5b840 49 56 41 54 45 20 20 20 69 6e 74 20 2a 73 71 6c IVATE int *sql
5b850 69 74 65 33 50 61 67 65 72 53 74 61 74 73 28 50 ite3PagerStats(P
5b860 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
5b870 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
5b880 6c 69 74 65 33 50 61 67 65 72 52 65 66 64 75 6d lite3PagerRefdum
5b890 70 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 p(Pager*);.SQLIT
5b8a0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
5b8b0 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 sqlite3PagerIsMe
5b8c0 6d 64 62 28 50 61 67 65 72 2a 29 3b 0a 23 65 6e mdb(Pager*);.#en
5b8d0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
5b8e0 54 45 5f 54 45 53 54 0a 76 6f 69 64 20 64 69 73 TE_TEST.void dis
5b8f0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
5b900 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a o_errors(void);.
5b910 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 void enable_simu
5b920 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
5b930 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 void);.#else.# d
5b940 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 efine disable_si
5b950 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
5b960 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 s().# define ena
5b970 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
5b980 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 _errors().#endif
5b990 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 ..#endif /* _PAG
5b9a0 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a ER_H_ */../*****
5b9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
5b9c0 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a pager.h *******
5b9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5b9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5b9f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
5ba00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
5ba10 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
5ba20 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
5ba30 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
5ba40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
5ba50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
5ba60 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 e pcache.h in th
5ba70 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
5ba80 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
5ba90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
5baa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
5bab0 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a file pcache.h **
5bac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bae0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
5baf0 32 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2008 August 05.*
5bb00 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
5bb10 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
5bb20 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
5bb30 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
5bb40 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
5bb50 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
5bb60 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
5bb70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
5bb80 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
5bb90 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
5bba0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
5bbb0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
5bbc0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
5bbd0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
5bbe0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
5bbf0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
5bc00 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
5bc10 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
5bc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
5bc60 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c This header fil
5bc70 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e e defines the in
5bc80 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 terface that the
5bc90 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 sqlite page cac
5bca0 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e he.** subsystem.
5bcb0 20 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 .**.** @(#) $Id
5bcc0 3a 20 70 63 61 63 68 65 2e 68 2c 76 20 31 2e 31 : pcache.h,v 1.1
5bcd0 34 20 32 30 30 38 2f 31 30 2f 31 37 20 31 38 3a 4 2008/10/17 18:
5bce0 35 31 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 51:53 danielk197
5bcf0 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 7 Exp $.*/..#ifn
5bd00 64 65 66 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a def _PCACHE_H_..
5bd10 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 typedef struct P
5bd20 67 48 64 72 20 50 67 48 64 72 3b 0a 74 79 70 65 gHdr PgHdr;.type
5bd30 64 65 66 20 73 74 72 75 63 74 20 50 43 61 63 68 def struct PCach
5bd40 65 20 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a e PCache;../*.**
5bd50 20 45 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 Every page in t
5bd60 68 65 20 63 61 63 68 65 20 69 73 20 63 6f 6e 74 he cache is cont
5bd70 72 6f 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 rolled by an ins
5bd80 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
5bd90 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 lowing.** struct
5bda0 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 ure..*/.struct P
5bdb0 67 48 64 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 gHdr {. void *p
5bdc0 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
5bdd0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 /* Conte
5bde0 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 nt of this page
5bdf0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 */. void *pExtr
5be00 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
5be10 20 20 20 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e /* Extra con
5be20 74 65 6e 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 tent */. PgHdr
5be30 2a 70 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 *pDirty;
5be40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e /* Tran
5be50 73 69 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 sient list of di
5be60 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 rty pages */. P
5be70 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 gno pgno;
5be80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5be90 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 Page number for
5bea0 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 this page */.
5beb0 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 Pager *pPager;
5bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5bed0 2a 20 54 68 65 20 70 61 67 65 72 20 74 68 69 73 * The pager this
5bee0 20 70 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 page is part of
5bef0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
5bf00 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
5bf10 75 33 32 20 70 61 67 65 48 61 73 68 3b 20 20 20 u32 pageHash;
5bf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5bf30 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65 20 63 * Hash of page c
5bf40 6f 6e 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 ontent */.#endif
5bf50 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 . u16 flags;
5bf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bf70 20 20 2f 2a 20 50 47 48 44 52 20 66 6c 61 67 73 /* PGHDR flags
5bf80 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a defined below *
5bf90 2f 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /. /***********
5bfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a ***********. **
5bfe0 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 Elements above
5bff0 61 72 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c are public. All
5c000 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 that follows is
5c010 20 70 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 private to pcac
5c020 68 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 he.c. ** and sh
5c030 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 ould not be acce
5c040 73 73 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f ssed by other mo
5c050 64 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 dules.. */. i1
5c060 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 6 nRef;
5c070 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5c080 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 Number of users
5c090 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a of this page */.
5c0a0 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 PCache *pCache
5c0b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5c0c0 20 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f /* Cache that o
5c0d0 77 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f wns this page */
5c0e0 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .. /***********
5c0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a ***********. **
5c130 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 Elements above
5c140 61 72 65 20 61 63 63 65 73 73 69 62 6c 65 20 61 are accessible a
5c150 74 20 61 6e 79 20 74 69 6d 65 20 62 79 20 74 68 t any time by th
5c160 65 20 6f 77 6e 65 72 20 6f 66 20 74 68 65 20 63 e owner of the c
5c170 61 63 68 65 0a 20 20 2a 2a 20 77 69 74 68 6f 75 ache. ** withou
5c180 74 20 74 68 65 20 6e 65 65 64 20 66 6f 72 20 61 t the need for a
5c190 20 6d 75 74 65 78 2e 20 20 54 68 65 20 65 6c 65 mutex. The ele
5c1a0 6d 65 6e 74 73 20 74 68 61 74 20 66 6f 6c 6c 6f ments that follo
5c1b0 77 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 20 20 w can only be.
5c1c0 2a 2a 20 61 63 63 65 73 73 65 64 20 77 68 69 6c ** accessed whil
5c1d0 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 53 51 e holding the SQ
5c1e0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
5c1f0 43 5f 4c 52 55 20 6d 75 74 65 78 2e 0a 20 20 2a C_LRU mutex.. *
5c200 2f 0a 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 /. PgHdr *pNext
5c210 48 61 73 68 2c 20 2a 70 50 72 65 76 48 61 73 68 Hash, *pPrevHash
5c220 3b 20 20 2f 2a 20 48 61 73 68 20 63 6f 6c 6c 69 ; /* Hash colli
5c230 73 69 6f 6e 20 63 68 61 69 6e 20 66 6f 72 20 50 sion chain for P
5c240 67 48 64 72 2e 70 67 6e 6f 20 2a 2f 0a 20 20 50 gHdr.pgno */. P
5c250 67 48 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 gHdr *pNext, *pP
5c260 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a rev; /*
5c270 20 4c 69 73 74 20 6f 66 20 63 6c 65 61 6e 20 6f List of clean o
5c280 72 20 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f r dirty pages */
5c290 0a 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 4c . PgHdr *pNextL
5c2a0 72 75 2c 20 2a 70 50 72 65 76 4c 72 75 3b 20 20 ru, *pPrevLru;
5c2b0 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 67 6c 6f /* Part of glo
5c2c0 62 61 6c 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a bal LRU list */.
5c2d0 7d 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65 };../* Bit value
5c2e0 73 20 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67 s for PgHdr.flag
5c2f0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 s */.#define PGH
5c300 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 DR_IN_JOURNAL
5c310 20 20 20 20 20 30 78 30 30 31 20 20 2f 2a 20 50 0x001 /* P
5c320 61 67 65 20 69 73 20 69 6e 20 72 6f 6c 6c 62 61 age is in rollba
5c330 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 ck journal */.#d
5c340 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54 efine PGHDR_DIRT
5c350 59 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 Y 0x
5c360 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73 002 /* Page has
5c370 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 changed */.#def
5c380 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 ine PGHDR_NEED_S
5c390 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 30 YNC 0x00
5c3a0 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20 4 /* Fsync the
5c3b0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
5c3c0 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20 before.
5c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
5c3f0 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 70 * writing this p
5c400 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 age to the datab
5c410 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ase */.#define P
5c420 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20 GHDR_NEED_READ
5c430 20 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a 0x008 /*
5c440 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65 Content is unre
5c450 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 ad */.#define PG
5c460 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 HDR_REUSE_UNLIKE
5c470 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20 LY 0x010 /*
5c480 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 73 A hint that reus
5c490 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f e is unlikely */
5c4a0 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 .#define PGHDR_D
5c4b0 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 20 ONT_WRITE
5c4c0 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 0x020 /* Do no
5c4d0 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 t write content
5c4e0 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 to disk */../* I
5c4f0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 nitialize and sh
5c500 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 utdown the page
5c510 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 cache subsystem
5c520 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
5c530 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
5c540 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f cheInitialize(vo
5c550 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
5c560 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5c570 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 PcacheShutdown(v
5c580 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 oid);../* Page c
5c590 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61 ache buffer mana
5c5a0 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 gement:.** These
5c5b0 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d routines implem
5c5c0 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ent SQLITE_CONFI
5c5d0 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a G_PAGECACHE..*/.
5c5e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5c5f0 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 oid sqlite3PCach
5c600 65 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 eBufferSetup(voi
5c610 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 d *, int sz, int
5c620 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n);.SQLITE_PRIV
5c630 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
5c640 33 50 43 61 63 68 65 4d 61 6c 6c 6f 63 28 69 6e 3PCacheMalloc(in
5c650 74 20 73 7a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 t sz);.SQLITE_PR
5c660 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
5c670 65 33 50 43 61 63 68 65 46 72 65 65 28 76 6f 69 e3PCacheFree(voi
5c680 64 2a 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20 d*);../* Create
5c690 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 68 a new pager cach
5c6a0 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f e..** Under memo
5c6b0 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b ry stress, invok
5c6c0 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 79 e xStress to try
5c6d0 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 to make pages c
5c6e0 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c lean..** Only cl
5c6f0 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64 ean and unpinned
5c700 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 65 pages can be re
5c710 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 claimed..*/.SQLI
5c720 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
5c730 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 sqlite3PcacheOpe
5c740 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c n(. int szPage,
5c750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c760 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 /* Size of e
5c770 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 very page */. i
5c780 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20 nt szExtra,
5c790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5c7a0 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 Extra space ass
5c7b0 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 ociated with eac
5c7c0 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 h page */. int
5c7d0 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 bPurgeable,
5c7e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
5c7f0 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20 ue if pages are
5c800 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 on backing store
5c810 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 */. int (*xStr
5c820 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64 ess)(void*, PgHd
5c830 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 r*), /* Call to
5c840 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 try to make page
5c850 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 s clean */. voi
5c860 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 d *pStress,
5c870 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5c880 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 rgument to xStre
5c890 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a ss */. PCache *
5c8a0 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20 pToInit
5c8b0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c /* Preall
5c8c0 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 ocated space for
5c8d0 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 the PCache */.)
5c8e0 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 ;../* Modify the
5c8f0 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72 page-size after
5c900 20 74 68 65 20 63 61 63 68 65 20 68 61 73 20 62 the cache has b
5c910 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a een created. */.
5c920 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5c930 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
5c940 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 eSetPageSize(PCa
5c950 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a che *, int);../*
5c960 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
5c970 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 in bytes of a P
5c980 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55 Cache object. U
5c990 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61 sed to prealloca
5c9a0 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70 te.** storage sp
5c9b0 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ace..*/.SQLITE_P
5c9c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5c9d0 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 e3PcacheSize(voi
5c9e0 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 d);../* One rele
5c9f0 61 73 65 20 70 65 72 20 73 75 63 63 65 73 73 66 ase per successf
5ca00 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 20 ul fetch. Page
5ca10 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20 is pinned until
5ca20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66 released..** Ref
5ca30 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20 erence counted.
5ca40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
5ca50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
5ca60 61 63 68 65 46 65 74 63 68 28 50 43 61 63 68 65 acheFetch(PCache
5ca70 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65 *, Pgno, int cre
5ca80 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a ateFlag, PgHdr**
5ca90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5caa0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
5cab0 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 acheRelease(PgHd
5cac0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 r*);..SQLITE_PRI
5cad0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5cae0 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 3PcacheDrop(PgHd
5caf0 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 r*); /*
5cb00 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d Remove page from
5cb10 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 cache */.SQLITE
5cb20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5cb30 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
5cb40 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20 irty(PgHdr*);
5cb50 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61 /* Make sure pa
5cb60 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72 ge is marked dir
5cb70 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ty */.SQLITE_PRI
5cb80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5cb90 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 3PcacheMakeClean
5cba0 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 (PgHdr*); /*
5cbb0 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61 Mark a single pa
5cbc0 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 ge as clean */.S
5cbd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
5cbe0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
5cbf0 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a CleanAll(PCache*
5cc00 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c ); /* Mark al
5cc10 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 67 l dirty list pag
5cc20 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a es as clean */..
5cc30 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 65 /* Change a page
5cc40 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62 number. Used b
5cc50 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a y incr-vacuum. *
5cc60 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
5cc70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
5cc80 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20 cheMove(PgHdr*,
5cc90 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 Pgno);../* Remov
5cca0 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68 e all pages with
5ccb0 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20 pgno>x. Reset
5ccc0 74 68 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d the cache if x==
5ccd0 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 0 */.SQLITE_PRIV
5cce0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5ccf0 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 PcacheTruncate(P
5cd00 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b Cache*, Pgno x);
5cd10 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20 ../* Get a list
5cd20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 of all dirty pag
5cd30 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c es in the cache,
5cd40 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 sorted by page
5cd50 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45 number */.SQLITE
5cd60 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a _PRIVATE PgHdr *
5cd70 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 sqlite3PcacheDir
5cd80 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b tyList(PCache*);
5cd90 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63 ../* Reset and c
5cda0 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f lose the cache o
5cdb0 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f bject */.SQLITE_
5cdc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
5cdd0 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 ite3PcacheClose(
5cde0 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c PCache*);../* Cl
5cdf0 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70 ear flags from p
5ce00 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 ages of the page
5ce10 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 cache */.SQLITE
5ce20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5ce30 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 lite3PcacheClear
5ce40 46 6c 61 67 73 28 50 43 61 63 68 65 2a 2c 20 69 Flags(PCache*, i
5ce50 6e 74 20 6d 61 73 6b 29 3b 0a 0a 2f 2a 20 41 73 nt mask);../* As
5ce60 73 65 72 74 20 66 6c 61 67 73 20 73 65 74 74 69 sert flags setti
5ce70 6e 67 73 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 ngs on all pages
5ce80 2e 20 20 44 65 62 75 67 67 69 6e 67 20 6f 6e 6c . Debugging onl
5ce90 79 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 y */.#ifndef NDE
5cea0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
5ceb0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
5cec0 33 50 63 61 63 68 65 41 73 73 65 72 74 46 6c 61 3PcacheAssertFla
5ced0 67 73 28 50 43 61 63 68 65 2a 2c 20 69 6e 74 20 gs(PCache*, int
5cee0 74 72 75 65 4d 61 73 6b 2c 20 69 6e 74 20 66 61 trueMask, int fa
5cef0 6c 73 65 4d 61 73 6b 29 3b 0a 23 65 6c 73 65 0a lseMask);.#else.
5cf00 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
5cf10 50 63 61 63 68 65 41 73 73 65 72 74 46 6c 61 67 PcacheAssertFlag
5cf20 73 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 0a s(A,B,C).#endif.
5cf30 0a 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 ./* Return true
5cf40 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 if the number of
5cf50 20 64 69 72 74 79 20 70 61 67 65 73 20 69 73 20 dirty pages is
5cf60 30 20 6f 72 20 31 20 2a 2f 0a 53 51 4c 49 54 45 0 or 1 */.SQLITE
5cf70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
5cf80 69 74 65 33 50 63 61 63 68 65 5a 65 72 6f 4f 72 ite3PcacheZeroOr
5cf90 4f 6e 65 44 69 72 74 79 50 61 67 65 73 28 50 43 OneDirtyPages(PC
5cfa0 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 ache*);../* Disc
5cfb0 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ard the contents
5cfc0 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f of the cache */
5cfd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5cfe0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
5cff0 65 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b eClear(PCache*);
5d000 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ../* Return the
5d010 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
5d020 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
5d030 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 references */.S
5d040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5d050 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 t sqlite3PcacheR
5d060 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 efCount(PCache*)
5d070 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 ;../* Increment
5d080 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
5d090 75 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69 unt of an existi
5d0a0 6e 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 ng page */.SQLIT
5d0b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
5d0c0 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28 qlite3PcacheRef(
5d0d0 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 PgHdr*);..SQLITE
5d0e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
5d0f0 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 ite3PcachePageRe
5d100 66 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a fcount(PgHdr*);.
5d110 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 ./* Return the t
5d120 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
5d130 61 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 ages stored in t
5d140 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 he cache */.SQLI
5d150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5d160 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
5d170 63 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a count(PCache*);.
5d180 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
5d190 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 20 49 74 HECK_PAGES./* It
5d1a0 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c erate through al
5d1b0 6c 20 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c l pages currentl
5d1c0 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 y stored in the
5d1d0 63 61 63 68 65 2e 20 54 68 69 73 20 69 6e 74 65 cache. This inte
5d1e0 72 66 61 63 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79 rface.** is only
5d1f0 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 53 51 available if SQ
5d200 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
5d210 20 69 73 20 64 65 66 69 6e 65 64 20 77 68 65 6e is defined when
5d220 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 the library is
5d230 0a 2a 2a 20 62 75 69 6c 74 2e 0a 2a 2f 0a 53 51 .** built..*/.SQ
5d240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
5d250 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 d sqlite3PcacheI
5d260 74 65 72 61 74 65 28 50 43 61 63 68 65 20 2a 70 terate(PCache *p
5d270 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49 Cache, void (*xI
5d280 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0a ter)(PgHdr *));.
5d290 23 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 61 #endif../* Set a
5d2a0 6e 64 20 67 65 74 20 74 68 65 20 73 75 67 67 65 nd get the sugge
5d2b0 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 sted cache-size
5d2c0 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 for the specifie
5d2d0 64 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 2a d pager-cache..*
5d2e0 2a 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 61 *.** If no globa
5d2f0 6c 20 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f 6e l maximum is con
5d300 66 69 67 75 72 65 64 2c 20 74 68 65 6e 20 74 68 figured, then th
5d310 65 20 73 79 73 74 65 6d 20 61 74 74 65 6d 70 74 e system attempt
5d320 73 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 68 s to limit.** th
5d330 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
5d340 66 20 70 61 67 65 73 20 63 61 63 68 65 64 20 62 f pages cached b
5d350 79 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 y purgeable page
5d360 72 2d 63 61 63 68 65 73 20 74 6f 20 74 68 65 20 r-caches to the
5d370 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 sum.** of the su
5d380 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 ggested cache-si
5d390 7a 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 zes..*/.SQLITE_P
5d3a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5d3b0 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 e3PcacheGetCache
5d3c0 73 69 7a 65 28 50 43 61 63 68 65 20 2a 29 3b 0a size(PCache *);.
5d3d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5d3e0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
5d3f0 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 eSetCachesize(PC
5d400 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 ache *, int);..#
5d410 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
5d420 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 BLE_MEMORY_MANAG
5d430 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74 6f 20 EMENT./* Try to
5d440 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20 75 73 return memory us
5d450 65 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 ed by the pcache
5d460 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65 20 6d module to the m
5d470 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61 70 20 ain memory heap
5d480 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
5d490 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
5d4a0 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 cheReleaseMemory
5d4b0 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 (int);.#endif..#
5d4c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
5d4d0 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
5d4e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
5d4f0 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c 69 6e cheStats(int*,in
5d500 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 23 t*,int*,int*);.#
5d510 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
5d520 20 5f 50 43 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a _PCACHE_H_ */..
5d530 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
5d540 45 6e 64 20 6f 66 20 70 63 61 63 68 65 2e 68 20 End of pcache.h
5d550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
5d580 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
5d590 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
5d5a0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
5d5b0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
5d5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
5d5d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
5d5e0 20 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e Include os.h in
5d5f0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 the middle of s
5d600 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
5d610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
5d620 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
5d630 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68 Begin file os.h
5d640 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
5d650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
5d670 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
5d680 65 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 ember 16.**.** T
5d690 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
5d6a0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
5d6b0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
5d6c0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
5d6d0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
5d6e0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
5d6f0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
5d700 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
5d710 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
5d720 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
5d730 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
5d740 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
5d750 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
5d760 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
5d770 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
5d780 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
5d790 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
5d7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
5d7f0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 ** This header f
5d800 69 6c 65 20 28 74 6f 67 65 74 68 65 72 20 77 69 ile (together wi
5d810 74 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 th is companion
5d820 43 20 73 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 C source-code fi
5d830 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 le.** "os.c") at
5d840 74 65 6d 70 74 20 74 6f 20 61 62 73 74 72 61 63 tempt to abstrac
5d850 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 t the underlying
5d860 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
5d870 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 m so that.** the
5d880 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 SQLite library
5d890 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 will work on bot
5d8a0 68 20 50 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 h POSIX and wind
5d8b0 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a ows systems..**.
5d8c0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 ** This header f
5d8d0 69 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 65 2d ile is #include-
5d8e0 65 64 20 62 79 20 73 71 6c 69 74 65 49 6e 74 2e ed by sqliteInt.
5d8f0 68 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20 h and thus ends
5d900 75 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c up.** being incl
5d910 75 64 65 64 20 62 79 20 65 76 65 72 79 20 73 6f uded by every so
5d920 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a urce file..**.**
5d930 20 24 49 64 3a 20 6f 73 2e 68 2c 76 20 31 2e 31 $Id: os.h,v 1.1
5d940 30 35 20 32 30 30 38 2f 30 36 2f 32 36 20 31 30 05 2008/06/26 10
5d950 3a 34 31 3a 31 39 20 64 61 6e 69 65 6c 6b 31 39 :41:19 danielk19
5d960 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 77 Exp $.*/.#ifn
5d970 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 def _SQLITE_OS_H
5d980 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 _.#define _SQLIT
5d990 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 E_OS_H_../*.** F
5d9a0 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 igure out if we
5d9b0 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
5d9c0 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 Unix, Windows,
5d9d0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a or some other.**
5d9e0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
5d9f0 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f m. After the fo
5da00 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 llowing block of
5da10 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 preprocess macr
5da20 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 os,.** all of SQ
5da30 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 LITE_OS_UNIX, SQ
5da40 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c LITE_OS_WIN, SQL
5da50 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 ITE_OS_OS2, and
5da60 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 SQLITE_OS_OTHER
5da70 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 .** will defined
5da80 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 to either 1 or
5da90 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 0. One of the f
5daa0 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 our will be 1.
5dab0 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 The other .** th
5dac0 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a ree will be 0..*
5dad0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
5dae0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 LITE_OS_OTHER).#
5daf0 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 if SQLITE_OS_OT
5db00 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 HER==1.# undef
5db10 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a SQLITE_OS_UNIX.
5db20 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 # define SQLIT
5db30 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 E_OS_UNIX 0.#
5db40 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f undef SQLITE_OS_
5db50 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 WIN.# define S
5db60 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 QLITE_OS_WIN 0.#
5db70 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f undef SQLITE_
5db80 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e OS_OS2.# defin
5db90 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 e SQLITE_OS_OS2
5dba0 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64 0.# else.# und
5dbb0 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 ef SQLITE_OS_OTH
5dbc0 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 ER.# endif.#endi
5dbd0 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 f.#if !defined(S
5dbe0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26 QLITE_OS_UNIX) &
5dbf0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
5dc00 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65 E_OS_OTHER).# de
5dc10 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f fine SQLITE_OS_O
5dc20 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20 THER 0.# ifndef
5dc30 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 SQLITE_OS_WIN.#
5dc40 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 if defined(_WI
5dc50 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 N32) || defined(
5dc60 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 WIN32) || define
5dc70 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c d(__CYGWIN__) ||
5dc80 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 defined(__MINGW
5dc90 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 32__) || defined
5dca0 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 (__BORLANDC__).#
5dcb0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
5dcc0 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20 TE_OS_WIN 1.#
5dcd0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
5dce0 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 OS_UNIX 0.#
5dcf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
5dd00 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20 _OS2 0.# elif
5dd10 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 defined(__EMX__)
5dd20 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 || defined(_OS2
5dd30 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32 ) || defined(OS2
5dd40 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 ) || defined(_OS
5dd50 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 2_) || defined(_
5dd60 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65 _OS2__).# de
5dd70 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
5dd80 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e IN 0.# defin
5dd90 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 e SQLITE_OS_UNIX
5dda0 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 0.# define
5ddb0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a SQLITE_OS_OS2 1.
5ddc0 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64 # else.# d
5ddd0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
5dde0 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 WIN 0.# defi
5ddf0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 ne SQLITE_OS_UNI
5de00 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 X 1.# define
5de10 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 SQLITE_OS_OS2 0
5de20 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65 .# endif.# else
5de30 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 .# define SQLIT
5de40 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64 E_OS_UNIX 0.# d
5de50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
5de60 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 OS2 0.# endif.#e
5de70 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c lse.# ifndef SQL
5de80 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 ITE_OS_WIN.# de
5de90 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
5dea0 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e IN 0.# endif.#en
5deb0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 dif../*.** Deter
5dec0 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 mine if we are d
5ded0 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 ealing with Wind
5dee0 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61 owsCE - which ha
5def0 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75 s a much.** redu
5df00 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 ced API..*/.#if
5df10 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57 defined(_WIN32_W
5df20 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c CE).# define SQL
5df30 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23 ITE_OS_WINCE 1.#
5df40 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
5df50 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a LITE_OS_WINCE 0.
5df60 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 #endif.../*.** D
5df70 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 efine the maximu
5df80 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70 m size of a temp
5df90 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a orary filename.*
5dfa0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
5dfb0 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77 WIN.# include <w
5dfc0 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69 indows.h>.# defi
5dfd0 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 ne SQLITE_TEMPNA
5dfe0 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54 ME_SIZE (MAX_PAT
5dff0 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49 H+50).#elif SQLI
5e000 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28 TE_OS_OS2.# if (
5e010 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 __GNUC__ > 3 ||
5e020 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26 __GNUC__ == 3 &&
5e030 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 __GNUC_MINOR__
5e040 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 64 >= 3) && defined
5e050 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 (OS2_HIGH_MEMORY
5e060 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73 ).# include <os
5e070 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20 2safe.h> /* has
5e080 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62 to be included b
5e090 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20 efore os2.h for
5e0a0 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20 linking to work
5e0b0 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 */.# endif.# def
5e0c0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45 ine INCL_DOSDATE
5e0d0 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e TIME.# define IN
5e0e0 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20 CL_DOSFILEMGR.#
5e0f0 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 define INCL_DOSE
5e100 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49 RRORS.# define I
5e110 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65 NCL_DOSMISC.# de
5e120 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f fine INCL_DOSPRO
5e130 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e CESS.# define IN
5e140 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a CL_DOSMODULEMGR.
5e150 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f # define INCL_DO
5e160 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e SSEMAPHORES.# in
5e170 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 clude <os2.h>.#
5e180 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68 include <uconv.h
5e190 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 >.# define SQLIT
5e1a0 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 E_TEMPNAME_SIZE
5e1b0 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29 (CCHMAXPATHCOMP)
5e1c0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
5e1d0 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f SQLITE_TEMPNAME_
5e1e0 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a SIZE 200.#endif.
5e1f0 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46 ./* If the SET_F
5e200 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73 ULLSYNC macro is
5e210 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f not defined abo
5e220 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74 ve, then make it
5e230 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 .** a no-op.*/.#
5e240 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53 ifndef SET_FULLS
5e250 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54 YNC.# define SET
5e260 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23 _FULLSYNC(x,y).#
5e270 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
5e280 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 default size of
5e290 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a a disk sector.*
5e2a0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
5e2b0 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
5e2c0 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
5e2d0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 LITE_DEFAULT_SEC
5e2e0 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e TOR_SIZE 512.#en
5e2f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f dif../*.** Tempo
5e300 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e rary files are n
5e310 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 amed starting wi
5e320 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20 66 th this prefix f
5e330 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61 ollowed by 16 ra
5e340 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d ndom.** alphanum
5e350 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 2c eric characters,
5e360 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74 and no file ext
5e370 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65 ension. They are
5e380 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a stored in the.*
5e390 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20 * OS's standard
5e3a0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 temporary file d
5e3b0 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72 irectory, and ar
5e3c0 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20 e deleted prior
5e3d0 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73 to exit..** If s
5e3e0 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 65 qlite is being e
5e3f0 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68 mbedded in anoth
5e400 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 er program, you
5e410 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e may wish to chan
5e420 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 ge the.** prefix
5e430 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72 to reflect your
5e440 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c program's name,
5e450 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72 so that if your
5e460 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a program exits.*
5e470 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f * prematurely, o
5e480 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c ld temporary fil
5e490 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79 es can be easily
5e4a0 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69 identified. Thi
5e4b0 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a s can be done.**
5e4c0 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f using -DSQLITE_
5e4d0 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 TEMP_FILE_PREFIX
5e4e0 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68 =myprefix_ on th
5e4f0 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 e compiler comma
5e500 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 nd line..**.** 2
5e510 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20 006-10-31: The
5e520 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 75 default prefix u
5e530 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 sed to be "sqlit
5e540 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a e_". But then.*
5e550 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 64 * Mcafee started
5e560 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e using SQLite in
5e570 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75 their anti-viru
5e580 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74 s product and it
5e590 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 74 .** started putt
5e5a0 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 74 ing files with t
5e5b0 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65 he "sqlite" name
5e5c0 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20 in the c:/temp
5e5d0 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20 folder..** This
5e5e0 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e annoyed many win
5e5f0 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f dows users. Tho
5e600 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74 se users would t
5e610 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f hen do a .** Goo
5e620 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22 gle search for "
5e630 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68 sqlite", find th
5e640 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 e telephone numb
5e650 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 ers of the.** de
5e660 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c velopers and cal
5e670 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75 l to wake them u
5e680 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63 p at night and c
5e690 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 omplain..** For
5e6a0 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 this reason, the
5e6b0 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72 default name pr
5e6c0 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 20 efix is changed
5e6d0 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a to be "sqlite" .
5e6e0 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77 ** spelled backw
5e6f0 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65 ards. So the te
5e700 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 69 mp files are sti
5e710 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62 ll identified, b
5e720 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d ut.** anybody sm
5e730 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69 art enough to fi
5e740 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64 gure out the cod
5e750 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 e is also likely
5e760 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 smart.** enough
5e770 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61 to know that ca
5e780 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f lling the develo
5e790 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c per will not hel
5e7a0 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20 p get rid.** of
5e7b0 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 the file..*/.#if
5e7c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 ndef SQLITE_TEMP
5e7d0 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64 _FILE_PREFIX.# d
5e7e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d efine SQLITE_TEM
5e7f0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65 P_FILE_PREFIX "e
5e800 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a tilqs_".#endif..
5e810 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
5e820 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62 ing values may b
5e830 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
5e840 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
5e850 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c to.** sqlite3OsL
5e860 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f ock(). The vario
5e870 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74 us locks exhibit
5e880 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
5e890 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 emantics:.**.**
5e8a0 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e SHARED: Any n
5e8b0 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73 umber of process
5e8c0 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48 es may hold a SH
5e8d0 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 ARED lock simult
5e8e0 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 aneously..** RES
5e8f0 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65 ERVED: A single
5e900 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c process may hol
5e910 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 d a RESERVED loc
5e920 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a k on a file at.*
5e930 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 * any
5e940 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f time. Other pro
5e950 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 cesses may hold
5e960 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53 and obtain new S
5e970 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 HARED locks..**
5e980 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e PENDING: A sin
5e990 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 gle process may
5e9a0 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c hold a PENDING l
5e9b0 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 ock on a file at
5e9c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 .** a
5e9d0 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69 ny one time. Exi
5e9e0 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 sting SHARED loc
5e9f0 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20 ks may persist,
5ea00 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 but no new.**
5ea10 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 20 SHARED
5ea20 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74 locks may be obt
5ea30 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70 ained by other p
5ea40 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43 rocesses..** EXC
5ea50 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55 LUSIVE: An EXCLU
5ea60 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75 SIVE lock preclu
5ea70 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f des all other lo
5ea80 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 cks..**.** PENDI
5ea90 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 NG_LOCK may not
5eaa0 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74 be passed direct
5eab0 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c ly to sqlite3OsL
5eac0 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 ock(). Instead,
5ead0 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61 a.** process tha
5eae0 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 58 t requests an EX
5eaf0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79 CLUSIVE lock may
5eb00 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e actually obtain
5eb10 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f a PENDING.** lo
5eb20 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 ck. This can be
5eb30 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 upgraded to an E
5eb40 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79 XCLUSIVE lock by
5eb50 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 a subsequent ca
5eb60 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ll to.** sqlite3
5eb70 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 OsLock()..*/.#de
5eb80 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 fine NO_LOCK
5eb90 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 0.#define S
5eba0 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31 HARED_LOCK 1
5ebb0 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 .#define RESERVE
5ebc0 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69 D_LOCK 2.#defi
5ebd0 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 ne PENDING_LOCK
5ebe0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43 3.#define EXC
5ebf0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a LUSIVE_LOCK 4..
5ec00 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 /*.** File Locki
5ec10 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74 ng Notes: (Most
5ec20 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73 ly about windows
5ec30 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69 but also some i
5ec40 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a nfo for Unix).**
5ec50 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73 .** We cannot us
5ec60 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f e LockFileEx() o
5ec70 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 r UnlockFileEx()
5ec80 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 on Win95/98/ME
5ec90 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65 because.** those
5eca0 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e functions are n
5ecb0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53 ot available. S
5ecc0 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f o we use only Lo
5ecd0 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 ckFile() and.**
5ece0 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a UnlockFile()..**
5ecf0 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70 .** LockFile() p
5ed00 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74 revents not just
5ed10 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73 writing but als
5ed20 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68 o reading by oth
5ed30 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a er processes..**
5ed40 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69 A SHARED_LOCK i
5ed50 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f s obtained by lo
5ed60 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 cking a single r
5ed70 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a andomly-chosen .
5ed80 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61 ** byte out of a
5ed90 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 20 specific range
5eda0 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f of bytes. The lo
5edb0 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 69 ck byte is obtai
5edc0 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f ned at .** rando
5edd0 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74 m so two separat
5ede0 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 72 e readers can pr
5edf0 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 68 obably access th
5ee00 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a e file at the .*
5ee10 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c * same time, unl
5ee20 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c ess they are unl
5ee30 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20 ucky and choose
5ee40 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79 the same lock by
5ee50 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53 te..** An EXCLUS
5ee60 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 IVE_LOCK is obta
5ee70 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 ined by locking
5ee80 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65 all bytes in the
5ee90 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65 range..** There
5eea0 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 can only be one
5eeb0 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45 writer. A RESE
5eec0 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 RVED_LOCK is obt
5eed0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 ained by locking
5eee0 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74 .** a single byt
5eef0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68 e of the file th
5ef00 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 64 at is designated
5ef10 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65 64 as the reserved
5ef20 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 lock byte..** A
5ef30 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 PENDING_LOCK is
5ef40 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 obtained by loc
5ef50 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65 king a designate
5ef60 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e 74 d byte different
5ef70 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53 from.** the RES
5ef80 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e ERVED_LOCK byte.
5ef90 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f .**.** On WinNT/
5efa0 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c 2K/XP systems, L
5efb0 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20 ockFileEx() and
5efc0 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 UnlockFileEx() a
5efd0 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a re available,.**
5efe0 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20 which means we
5eff0 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77 can use reader/w
5f000 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68 riter locks. Wh
5f010 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 en reader/writer
5f020 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73 locks.** are us
5f030 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 ed, the lock is
5f040 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61 placed on the sa
5f050 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 me range of byte
5f060 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a s that is used.*
5f070 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73 * for probabilis
5f080 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 tic locking in W
5f090 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e in95/98/ME. Hen
5f0a0 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ce, the locking
5f0b0 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 scheme.** will s
5f0c0 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f upport two or mo
5f0d0 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 re Win95 readers
5f0e0 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 or two or more
5f0f0 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a WinNT readers..*
5f100 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57 * But a single W
5f110 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c in95 reader will
5f120 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69 lock out all Wi
5f130 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 20 nNT readers and
5f140 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e a single.** WinN
5f150 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f T reader will lo
5f160 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72 ck out all other
5f170 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a Win95 readers..
5f180 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
5f190 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 65 ing #defines spe
5f1a0 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f cify the range o
5f1b0 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 f bytes used for
5f1c0 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 locking..** SHA
5f1d0 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20 RED_SIZE is the
5f1e0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
5f1f0 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 available in the
5f200 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68 pool from which
5f210 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 .** a random byt
5f220 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f e is selected fo
5f230 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e r a shared lock.
5f240 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79 The pool of by
5f250 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65 tes for.** share
5f260 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61 d locks begins a
5f270 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20 t SHARED_FIRST.
5f280 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 .**.** These #de
5f290 66 69 6e 65 73 20 61 72 65 20 61 76 61 69 6c 61 fines are availa
5f2a0 62 6c 65 20 69 6e 20 73 71 6c 69 74 65 5f 61 75 ble in sqlite_au
5f2b0 78 2e 68 20 73 6f 20 74 68 61 74 20 61 64 61 70 x.h so that adap
5f2c0 74 6f 72 73 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e tors for.** conn
5f2d0 65 63 74 69 6e 67 20 53 51 4c 69 74 65 20 74 6f ecting SQLite to
5f2e0 20 6f 74 68 65 72 20 6f 70 65 72 61 74 69 6e 67 other operating
5f2f0 20 73 79 73 74 65 6d 73 20 63 61 6e 20 75 73 65 systems can use
5f300 20 74 68 65 20 73 61 6d 65 20 62 79 74 65 0a 2a the same byte.*
5f310 2a 20 72 61 6e 67 65 73 20 66 6f 72 20 6c 6f 63 * ranges for loc
5f320 6b 69 6e 67 2e 20 20 49 6e 20 70 61 72 74 69 63 king. In partic
5f330 75 6c 61 72 2c 20 74 68 65 20 73 61 6d 65 20 6c ular, the same l
5f340 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 ocking strategy
5f350 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e 67 and.** byte rang
5f360 65 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 es are used for
5f370 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61 76 Unix. This leav
5f380 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73 73 es open the poss
5f390 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e 67 iblity of having
5f3a0 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20 77 .** clients on w
5f3b0 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e 64 in95, winNT, and
5f3c0 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69 6e unix all talkin
5f3d0 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 68 g to the same sh
5f3e0 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 ared file.** and
5f3f0 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 72 all locking cor
5f400 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20 73 rectly. To do s
5f410 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65 20 o would require
5f420 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20 77 that samba (or w
5f430 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20 hatever.** tool
5f440 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f is being used fo
5f450 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29 20 r file sharing)
5f460 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b 73 implements locks
5f470 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77 65 correctly betwe
5f480 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61 6e en.** windows an
5f490 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75 65 d unix. I'm gue
5f4a0 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27 74 ssing that isn't
5f4b0 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70 65 likely to happe
5f4c0 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73 69 n, but by.** usi
5f4d0 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b ng the same lock
5f4e0 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72 65 ing range we are
5f4f0 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20 74 at least open t
5f500 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 o the possibilit
5f510 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 y..**.** Locking
5f520 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20 6d in windows is m
5f530 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20 74 anditory. For t
5f540 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20 63 his reason, we c
5f550 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 61 annot store.** a
5f560 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74 68 ctual data in th
5f570 65 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 e bytes used for
5f580 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20 70 locking. The p
5f590 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63 ager never alloc
5f5a0 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 ates.** the page
5f5b0 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f s involved in lo
5f5c0 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65 2e cking therefore.
5f5d0 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73 SHARED_SIZE is
5f5e0 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a 20 selected so.**
5f5f0 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 that all locks w
5f600 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e ill fit on a sin
5f610 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61 74 gle page even at
5f620 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67 the minimum pag
5f630 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49 e size..** PENDI
5f640 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 20 NG_BYTE defines
5f650 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
5f660 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20 the locks. By
5f670 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f default PENDING_
5f680 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20 68 BYTE.** is set h
5f690 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 64 igh so that we d
5f6a0 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c 6c on't have to all
5f6b0 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 20 ocate an unused
5f6c0 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20 66 page except.** f
5f6d0 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64 61 or very large da
5f6e0 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f 6e tabases. But on
5f6f0 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74 68 e should test th
5f700 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67 20 e page skipping
5f710 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65 74 logic .** by set
5f720 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 ting PENDING_BYT
5f730 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69 6e E low and runnin
5f740 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 67 g the entire reg
5f750 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a 2a ression suite..*
5f760 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 *.** Changing th
5f770 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44 49 e value of PENDI
5f780 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73 20 NG_BYTE results
5f790 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63 6f in a subtly inco
5f7a0 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c 65 mpatible.** file
5f7b0 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e 64 format. Depend
5f7c0 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69 73 ing on how it is
5f7d0 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d 69 changed, you mi
5f7e0 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a ght not notice.*
5f7f0 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69 62 * the incompatib
5f800 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 61 79 ility right away
5f810 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20 61 , even running a
5f820 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f 6e full regression
5f830 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64 65 test..** The de
5f840 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 6f fault location o
5f850 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 69 f PENDING_BYTE i
5f860 73 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 s the first byte
5f870 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47 42 past the.** 1GB
5f880 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f boundary..**.*/
5f890 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
5f8a0 54 45 53 54 0a 23 64 65 66 69 6e 65 20 50 45 4e TEST.#define PEN
5f8b0 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 30 DING_BYTE 0
5f8c0 78 34 30 30 30 30 30 30 30 20 20 2f 2a 20 46 69 x40000000 /* Fi
5f8d0 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 rst byte past th
5f8e0 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 20 2a e 1GB boundary *
5f8f0 2f 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 41 /.#else.SQLITE_A
5f900 50 49 20 65 78 74 65 72 6e 20 75 6e 73 69 67 6e PI extern unsign
5f910 65 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 ed int sqlite3_p
5f920 65 6e 64 69 6e 67 5f 62 79 74 65 3b 0a 23 64 65 ending_byte;.#de
5f930 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 fine PENDING_BYT
5f940 45 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e E sqlite3_pendin
5f950 67 5f 62 79 74 65 0a 23 65 6e 64 69 66 0a 0a 23 g_byte.#endif..#
5f960 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f define RESERVED_
5f970 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e BYTE (PENDIN
5f980 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e G_BYTE+1).#defin
5f990 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 20 e SHARED_FIRST
5f9a0 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 (PENDING_BYT
5f9b0 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41 E+2).#define SHA
5f9c0 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35 RED_SIZE 5
5f9d0 31 30 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 10../* .** Funct
5f9e0 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69 ions for accessi
5f9f0 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ng sqlite3_file
5fa00 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 methods .*/.SQLI
5fa10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5fa20 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 qlite3OsClose(sq
5fa30 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 51 lite3_file*);.SQ
5fa40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5fa50 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 sqlite3OsRead(s
5fa60 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f qlite3_file*, vo
5fa70 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 id*, int amt, i6
5fa80 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 4 offset);.SQLIT
5fa90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5faa0 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c lite3OsWrite(sql
5fab0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 ite3_file*, cons
5fac0 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 t void*, int amt
5fad0 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a 53 , i64 offset);.S
5fae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5faf0 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 t sqlite3OsTrunc
5fb00 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
5fb10 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 53 51 *, i64 size);.SQ
5fb20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5fb30 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 sqlite3OsSync(s
5fb40 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
5fb50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
5fb60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
5fb70 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 FileSize(sqlite3
5fb80 5f 66 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53 69 _file*, i64 *pSi
5fb90 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ze);.SQLITE_PRIV
5fba0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
5fbb0 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 sLock(sqlite3_fi
5fbc0 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 le*, int);.SQLIT
5fbd0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5fbe0 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 lite3OsUnlock(sq
5fbf0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
5fc00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5fc10 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
5fc20 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
5fc30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
5fc40 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 d, int *pResOut)
5fc50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5fc60 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 int sqlite3OsFi
5fc70 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
5fc80 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 3_file*,int,void
5fc90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5fca0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
5fcb0 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 SectorSize(sqlit
5fcc0 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 53 51 e3_file *id);.SQ
5fcd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5fce0 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 sqlite3OsDevice
5fcf0 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
5fd00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
5fd10 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 );../* .** Funct
5fd20 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69 ions for accessi
5fd30 6e 67 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d ng sqlite3_vfs m
5fd40 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 ethods .*/.SQLIT
5fd50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5fd60 6c 69 74 65 33 4f 73 4f 70 65 6e 28 73 71 6c 69 lite3OsOpen(sqli
5fd70 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 te3_vfs *, const
5fd80 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 char *, sqlite3
5fd90 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 _file*, int, int
5fda0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
5fdb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
5fdc0 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f sDelete(sqlite3_
5fdd0 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 vfs *, const cha
5fde0 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 r *, int);.SQLIT
5fdf0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5fe00 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 73 71 lite3OsAccess(sq
5fe10 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
5fe20 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 st char *, int,
5fe30 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53 int *pResOut);.S
5fe40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5fe50 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 t sqlite3OsFullP
5fe60 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f athname(sqlite3_
5fe70 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 vfs *, const cha
5fe80 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a r *, int, char *
5fe90 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
5fea0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
5feb0 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 NSION.SQLITE_PRI
5fec0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
5fed0 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 e3OsDlOpen(sqlit
5fee0 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 e3_vfs *, const
5fef0 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f char *);.SQLITE_
5ff00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
5ff10 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 ite3OsDlError(sq
5ff20 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 lite3_vfs *, int
5ff30 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 , char *);.SQLIT
5ff40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
5ff50 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 sqlite3OsDlSym(s
5ff60 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f qlite3_vfs *, vo
5ff70 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 id *, const char
5ff80 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
5ff90 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5ffa0 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 OsDlClose(sqlite
5ffb0 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3_vfs *, void *)
5ffc0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ;.#endif /* SQLI
5ffd0 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
5ffe0 45 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 ENSION */.SQLITE
5fff0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
60000 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 ite3OsRandomness
60010 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
60020 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 int, char *);.SQ
60030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
60040 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 sqlite3OsSleep(
60050 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 sqlite3_vfs *, i
60060 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
60070 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
60080 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c sCurrentTime(sql
60090 69 74 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 ite3_vfs *, doub
600a0 6c 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e le*);../*.** Con
600b0 76 65 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f venience functio
600c0 6e 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 ns for opening a
600d0 6e 64 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 nd closing files
600e0 20 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 using .** sqlit
600f0 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f e3_malloc() to o
60100 62 74 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 btain space for
60110 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 the file-handle
60120 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 structure..*/.SQ
60130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
60140 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 sqlite3OsOpenMa
60150 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 lloc(sqlite3_vfs
60160 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
60170 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a , sqlite3_file *
60180 2a 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 *, int,int*);.SQ
60190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
601a0 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 sqlite3OsCloseF
601b0 72 65 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ree(sqlite3_file
601c0 20 2a 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 *);..#endif /*
601d0 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f _SQLITE_OS_H_ */
601e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
601f0 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a * End of os.h **
60200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60230 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
60240 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
60250 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
60260 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
60270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60280 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
60290 2a 20 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e * Include mutex.
602a0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
602b0 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a of sqliteInt.h *
602c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
602d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
602e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 * Begin file mut
602f0 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.h ***********
60300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60320 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 /./*.** 2007 Aug
60330 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 28.**.** The
60340 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
60350 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
60360 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
60370 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
60380 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
60390 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
603a0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
603b0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
603c0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
603d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
603e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
603f0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
60400 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
60410 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
60420 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
60430 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
60440 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
60450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60490 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
604a0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 file contains t
604b0 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 he common header
604c0 20 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 for all mutex i
604d0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a mplementations..
604e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 ** The sqliteInt
604f0 2e 68 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 .h header #inclu
60500 64 65 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f des this file so
60510 20 74 68 61 74 20 69 74 20 69 73 20 61 76 61 69 that it is avai
60520 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 lable.** to all
60530 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 source files. W
60540 65 20 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 e break it out i
60550 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b n an effort to k
60560 65 65 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 eep the code.**
60570 62 65 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 better organized
60580 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 ..**.** NOTE: s
60590 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 ource files shou
605a0 6c 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 ld *not* #includ
605b0 65 20 74 68 69 73 20 68 65 61 64 65 72 20 66 69 e this header fi
605c0 6c 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 le directly..**
605d0 53 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f Source files sho
605e0 75 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 uld #include the
605f0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c sqliteInt.h fil
60600 65 20 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 e and let that f
60610 69 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 ile.** include t
60620 68 69 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 his one indirect
60630 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d ly..**.** $Id: m
60640 75 74 65 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 utex.h,v 1.9 200
60650 38 2f 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 8/10/07 15:25:48
60660 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
60670 2f 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 /*.** Figure out
60680 20 77 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 what version of
60690 20 74 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 the code to use
606a0 2e 20 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 . The choices a
606b0 72 65 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 re.**.** SQLIT
606c0 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 E_MUTEX_OMIT
606d0 20 20 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f No mutex lo
606e0 67 69 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 gic. Not even s
606f0 74 75 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 tubs. The.**
60700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
60710 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 mutexe
60720 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 s implemention c
60730 61 6e 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 annot be overrid
60740 64 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 den.**
60750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
60760 20 20 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 at start-time
60770 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 ..**.** SQLITE
60780 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 _MUTEX_NOOP
60790 20 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 For single-t
607a0 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 hreaded applicat
607b0 69 6f 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 ions. No.**
607c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
607d0 20 20 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 mutual
607e0 65 78 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f exclusion is pro
607f0 76 69 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 vided. But this
60800 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
60810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
60820 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 implementation c
60830 61 6e 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e an be overridden
60840 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 at.**
60850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
60860 20 20 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a start-time..*
60870 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 *.** SQLITE_MU
60880 54 45 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 TEX_PTHREADS
60890 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 For multi-threa
608a0 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 ded applications
608b0 20 6f 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 on Unix..**.**
608c0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 SQLITE_MUTEX_W
608d0 33 32 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 32 For
608e0 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 multi-threaded a
608f0 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 pplications on W
60900 69 6e 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 in32..**.** SQ
60910 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 LITE_MUTEX_OS2
60920 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 For mult
60930 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 i-threaded appli
60940 63 61 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e cations on OS/2.
60950 0a 2a 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f .*/.#if !SQLITE_
60960 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 THREADSAFE.# def
60970 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
60980 5f 4f 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 _OMIT.#endif.#if
60990 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
609a0 46 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 FE && !defined(S
609b0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 QLITE_MUTEX_NOOP
609c0 29 0a 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f ).# if SQLITE_O
609d0 53 5f 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 S_UNIX.# defi
609e0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
609f0 50 54 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 PTHREADS.# elif
60a00 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 SQLITE_OS_WIN.#
60a10 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
60a20 45 5f 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 E_MUTEX_W32.# e
60a30 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 lif SQLITE_OS_OS
60a40 32 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 2.# define SQ
60a50 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 LITE_MUTEX_OS2.#
60a60 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 else.# defi
60a70 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
60a80 4e 4f 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 NOOP.# endif.#e
60a90 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
60aa0 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f ITE_MUTEX_OMIT./
60ab0 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 *.** If this is
60ac0 61 20 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e a no-op implemen
60ad0 74 61 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e tation, implemen
60ae0 74 20 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 t everything as
60af0 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 macros..*/.#defi
60b00 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
60b10 5f 61 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 _alloc(X) ((s
60b20 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 qlite3_mutex*)8)
60b30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
60b40 5f 6d 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 _mutex_free(X).#
60b50 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d define sqlite3_m
60b60 75 74 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 utex_enter(X).#d
60b70 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 efine sqlite3_mu
60b80 74 65 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 tex_try(X)
60b90 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e SQLITE_OK.#defin
60ba0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
60bb0 6c 65 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 leave(X).#define
60bc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
60bd0 65 6c 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 eld(X) 1.#de
60be0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 fine sqlite3_mut
60bf0 65 78 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 ex_notheld(X) 1
60c00 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
60c10 4d 75 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 MutexAlloc(X)
60c20 20 20 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 ((sqlite3_mut
60c30 65 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 ex*)8).#define s
60c40 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 qlite3MutexInit(
60c50 29 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f ) SQLITE_
60c60 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 OK.#define sqlit
60c70 65 33 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e e3MutexEnd().#en
60c80 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 dif /* defined(S
60c90 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 QLITE_OMIT_MUTEX
60ca0 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
60cb0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 ***** End of mut
60cc0 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.h ***********
60cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60cf0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
60d00 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
60d10 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
60d20 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
60d30 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
60d40 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 ****/.../*.** Ea
60d50 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ch database file
60d60 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 to be accessed
60d70 62 79 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 by the system is
60d80 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 an instance.**
60d90 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
60da0 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
60db0 72 65 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 re are normally
60dc0 74 77 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 two of these str
60dd0 75 63 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 uctures.** in th
60de0 65 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 e sqlite.aDb[] a
60df0 72 72 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 rray. aDb[0] is
60e00 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
60e10 73 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 se file and.** a
60e20 44 62 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 Db[1] is the dat
60e30 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 abase file used
60e40 74 6f 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 to hold temporar
60e50 79 20 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 y tables. Addit
60e60 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 ional.** databas
60e70 65 73 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 es may be attach
60e80 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 ed..*/.struct Db
60e90 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 {. char *zName
60ea0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d ; /* Nam
60eb0 65 20 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 e of this databa
60ec0 73 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 se */. Btree *p
60ed0 42 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 Bt; /*
60ee0 54 68 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 The B*Tree struc
60ef0 74 75 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 ture for this da
60f00 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
60f10 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 u8 inTrans;
60f20 20 20 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 /* 0: not
60f30 77 72 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 writable. 1: Tr
60f40 61 6e 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 ansaction. 2: C
60f50 68 65 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 heckpoint */. u
60f60 38 20 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 8 safety_level;
60f70 20 20 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 /* How aggre
60f80 73 73 69 76 65 20 61 74 20 73 79 6e 63 68 69 6e ssive at synchin
60f90 67 20 64 61 74 61 20 74 6f 20 64 69 73 6b 20 2a g data to disk *
60fa0 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 /. void *pAux;
60fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
60fc0 20 41 75 78 69 6c 69 61 72 79 20 64 61 74 61 2e Auxiliary data.
60fd0 20 20 55 73 75 61 6c 6c 79 20 4e 55 4c 4c 20 2a Usually NULL *
60fe0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 /. void (*xFree
60ff0 41 75 78 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a Aux)(void*); /*
61000 20 52 6f 75 74 69 6e 65 20 74 6f 20 66 72 65 65 Routine to free
61010 20 70 41 75 78 20 2a 2f 0a 20 20 53 63 68 65 6d pAux */. Schem
61020 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 a *pSchema;
61030 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 /* Pointer to da
61040 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 28 70 tabase schema (p
61050 6f 73 73 69 62 6c 79 20 73 68 61 72 65 64 29 20 ossibly shared)
61060 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
61070 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
61080 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
61090 75 72 65 20 73 74 6f 72 65 73 20 61 20 64 61 74 ure stores a dat
610a0 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a abase schema..**
610b0 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 .** If there are
610c0 20 6e 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c no virtual tabl
610d0 65 73 20 63 6f 6e 66 69 67 75 72 65 64 20 69 6e es configured in
610e0 20 74 68 69 73 20 73 63 68 65 6d 61 2c 20 74 68 this schema, th
610f0 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64 62 20 76 e.** Schema.db v
61100 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 ariable is set t
61110 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72 20 74 68 o NULL. After th
61120 65 20 66 69 72 73 74 20 76 69 72 74 75 61 6c 20 e first virtual
61130 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 table.** has bee
61140 6e 20 61 64 64 65 64 2c 20 69 74 20 69 73 20 73 n added, it is s
61150 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 et to point to t
61160 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
61170 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73 65 64 20 ection .** used
61180 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 63 6f to create the co
61190 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63 65 20 61 nnection. Once a
611a0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 virtual table h
611b0 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64 64 65 64 as been.** added
611c0 20 74 6f 20 74 68 65 20 53 63 68 65 6d 61 20 73 to the Schema s
611d0 74 72 75 63 74 75 72 65 20 61 6e 64 20 74 68 65 tructure and the
611e0 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72 69 61 Schema.db varia
611f0 62 6c 65 20 70 6f 70 75 6c 61 74 65 64 2c 20 0a ble populated, .
61200 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20 64 61 74 ** only that dat
61210 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
61220 20 6d 61 79 20 75 73 65 20 74 68 65 20 53 63 68 may use the Sch
61230 65 6d 61 20 74 6f 20 70 72 65 70 61 72 65 20 0a ema to prepare .
61240 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a ** statements..*
61250 2f 0a 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 /.struct Schema
61260 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d 61 5f 63 {. int schema_c
61270 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44 61 74 61 ookie; /* Data
61280 62 61 73 65 20 73 63 68 65 6d 61 20 76 65 72 73 base schema vers
61290 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 ion number for t
612a0 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 48 61 his file */. Ha
612b0 73 68 20 74 62 6c 48 61 73 68 3b 20 20 20 20 20 sh tblHash;
612c0 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 /* All tables
612d0 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 indexed by name
612e0 20 2a 2f 0a 20 20 48 61 73 68 20 69 64 78 48 61 */. Hash idxHa
612f0 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c sh; /* Al
61300 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64 69 63 65 l (named) indice
61310 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d s indexed by nam
61320 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 72 69 67 e */. Hash trig
61330 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 Hash; /* A
61340 6c 6c 20 74 72 69 67 67 65 72 73 20 69 6e 64 65 ll triggers inde
61350 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 xed by name */.
61360 20 48 61 73 68 20 61 46 4b 65 79 3b 20 20 20 20 Hash aFKey;
61370 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e /* Foreign
61380 20 6b 65 79 73 20 69 6e 64 65 78 65 64 20 62 79 keys indexed by
61390 20 74 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 54 to-table */. T
613a0 61 62 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 able *pSeqTab;
613b0 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 /* The sqlit
613c0 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 e_sequence table
613d0 20 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 used by AUTOINC
613e0 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 REMENT */. u8 f
613f0 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 ile_format;
61400 20 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 /* Schema forma
61410 74 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 t version for th
61420 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 is file */. u8
61430 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 enc;
61440 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 /* Text encodi
61450 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 ng used by this
61460 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 database */. u1
61470 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 6 flags;
61480 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f /* Flags asso
61490 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
614a0 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 schema */. int
614b0 20 63 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 cache_size;
614c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
614d0 61 67 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 ages to use in t
614e0 68 65 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e he cache */.#ifn
614f0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
61500 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 VIRTUALTABLE. s
61510 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
61520 20 20 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 /* "Owner" c
61530 6f 6e 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 onnection. See c
61540 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a omment above */.
61550 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
61560 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 These macros ca
61570 6e 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 n be used to tes
61580 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 t, set, or clear
61590 20 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a bits in the .**
615a0 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e Db.flags field.
615b0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 .*/.#define DbHa
615c0 73 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 sProperty(D,I,P)
615d0 20 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b (((D)->aDb[
615e0 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 I].pSchema->flag
615f0 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 s&(P))==(P)).#de
61600 66 69 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f fine DbHasAnyPro
61610 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 perty(D,I,P) ((
61620 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 (D)->aDb[I].pSch
61630 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 ema->flags&(P))!
61640 3d 30 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 =0).#define DbSe
61650 74 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 tProperty(D,I,P)
61660 20 20 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d (D)->aDb[I]
61670 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c .pSchema->flags|
61680 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 =(P).#define DbC
61690 6c 65 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 learProperty(D,I
616a0 2c 50 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 ,P) (D)->aDb[I
616b0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 ].pSchema->flags
616c0 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c &=~(P)../*.** Al
616d0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 lowed values for
616e0 20 74 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 the DB.flags fi
616f0 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 eld..**.** The D
61700 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 B_SchemaLoaded f
61710 6c 61 67 20 69 73 20 73 65 74 20 61 66 74 65 72 lag is set after
61720 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
61730 68 65 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a hema has been.**
61740 20 72 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 read into inter
61750 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e nal hash tables.
61760 0a 2a 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 .**.** DB_Unrese
61770 74 56 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 tViews means tha
61780 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 t one or more vi
61790 65 77 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 ews have column
617a0 6e 61 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 names that.** ha
617b0 76 65 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f ve been filled o
617c0 75 74 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 ut. If the sche
617d0 6d 61 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 ma changes, thes
617e0 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d e column names m
617f0 69 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 ight.** changes
61800 61 6e 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 and so the view
61810 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 will need to be
61820 72 65 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e reset..*/.#defin
61830 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 e DB_SchemaLoade
61840 64 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 d 0x0001 /*
61850 54 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 The schema has b
61860 65 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 een loaded */.#d
61870 65 66 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 efine DB_Unreset
61880 56 69 65 77 73 20 20 20 20 30 78 30 30 30 32 20 Views 0x0002
61890 20 2f 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 /* Some views h
618a0 61 76 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 ave defined colu
618b0 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 mn names */.#def
618c0 69 6e 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 ine DB_Empty
618d0 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 0x0004 /
618e0 2a 20 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d * The file is em
618f0 70 74 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 pty (length 0 by
61900 74 65 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 tes) */../*.** T
61910 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 he number of dif
61920 66 65 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 ferent kinds of
61930 74 68 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 things that can
61940 62 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 be limited.** us
61950 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f ing the sqlite3_
61960 6c 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 limit() interfac
61970 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e..*/.#define SQ
61980 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 LITE_N_LIMIT (SQ
61990 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 LITE_LIMIT_VARIA
619a0 42 4c 45 5f 4e 55 4d 42 45 52 2b 31 29 0a 0a 2f BLE_NUMBER+1)../
619b0 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d *.** Lookaside m
619c0 61 6c 6c 6f 63 20 69 73 20 61 20 73 65 74 20 6f alloc is a set o
619d0 66 20 66 69 78 65 64 2d 73 69 7a 65 20 62 75 66 f fixed-size buf
619e0 66 65 72 73 20 74 68 61 74 20 63 61 6e 20 62 65 fers that can be
619f0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 74 69 used.** to sati
61a00 73 69 66 79 20 73 6d 61 6c 6c 20 74 72 61 6e 73 sify small trans
61a10 69 65 6e 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ient memory allo
61a20 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 73 20 cation requests
61a30 66 6f 72 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 for objects.** a
61a40 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
61a50 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 particular data
61a60 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
61a70 20 20 54 68 65 20 75 73 65 20 6f 66 0a 2a 2a 20 The use of.**
61a80 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 lookaside malloc
61a90 20 70 72 6f 76 69 64 65 73 20 61 20 73 69 67 6e provides a sign
61aa0 69 66 69 63 61 6e 74 20 70 65 72 66 6f 72 6d 61 ificant performa
61ab0 6e 63 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 0a nce enhancement.
61ac0 2a 2a 20 28 61 70 70 72 6f 78 20 31 30 25 29 20 ** (approx 10%)
61ad0 62 79 20 61 76 6f 69 64 69 6e 67 20 6e 75 6d 65 by avoiding nume
61ae0 72 6f 75 73 20 6d 61 6c 6c 6f 63 2f 66 72 65 65 rous malloc/free
61af0 20 72 65 71 75 65 73 74 73 20 77 68 69 6c 65 20 requests while
61b00 70 61 72 73 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 parsing.** SQL s
61b10 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
61b20 20 54 68 65 20 4c 6f 6f 6b 61 73 69 64 65 20 73 The Lookaside s
61b30 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 63 tructure holds c
61b40 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 6e 66 onfiguration inf
61b50 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
61b60 68 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 he.** lookaside
61b70 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d malloc subsystem
61b80 2e 20 20 45 61 63 68 20 61 76 61 69 6c 61 62 6c . Each availabl
61b90 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
61ba0 69 6f 6e 20 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f ion in.** the lo
61bb0 6f 6b 61 73 69 64 65 20 73 75 62 73 79 73 74 65 okaside subsyste
61bc0 6d 20 69 73 20 73 74 6f 72 65 64 20 6f 6e 20 61 m is stored on a
61bd0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
61be0 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 0a 2a 2a LookasideSlot.**
61bf0 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 73 74 72 objects..*/.str
61c00 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 7b 0a uct Lookaside {.
61c10 20 20 75 31 36 20 73 7a 3b 20 20 20 20 20 20 20 u16 sz;
61c20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
61c30 65 20 6f 66 20 65 61 63 68 20 62 75 66 66 65 72 e of each buffer
61c40 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 in bytes */. u
61c50 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 8 bEnabled;
61c60 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
61c70 66 20 75 73 65 20 6c 6f 6f 6b 61 73 69 64 65 2e f use lookaside.
61c80 20 20 46 61 6c 73 65 20 74 6f 20 69 67 6e 6f 72 False to ignor
61c90 65 20 69 74 20 2a 2f 0a 20 20 75 38 20 62 4d 61 e it */. u8 bMa
61ca0 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 20 20 20 lloced;
61cb0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 53 74 /* True if pSt
61cc0 61 72 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f art obtained fro
61cd0 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
61ce0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 () */. int nOut
61cf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
61d00 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 66 /* Number of buf
61d10 66 65 72 73 20 63 75 72 72 65 6e 74 6c 79 20 63 fers currently c
61d20 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 hecked out */.
61d30 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 20 20 20 int mxOut;
61d40 20 20 20 20 20 20 20 20 2f 2a 20 48 69 67 68 77 /* Highw
61d50 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 4f ater mark for nO
61d60 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 69 64 ut */. Lookasid
61d70 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 eSlot *pFree;
61d80 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 61 69 6c /* List of avail
61d90 61 62 6c 65 20 62 75 66 66 65 72 73 20 2a 2f 0a able buffers */.
61da0 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74 3b 20 void *pStart;
61db0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
61dc0 73 74 20 62 79 74 65 20 6f 66 20 61 76 61 69 6c st byte of avail
61dd0 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 able memory spac
61de0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e e */. void *pEn
61df0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f d; /
61e00 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 * First byte pas
61e10 74 20 65 6e 64 20 6f 66 20 61 76 61 69 6c 61 62 t end of availab
61e20 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 73 le space */.};.s
61e30 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 truct LookasideS
61e40 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61 73 69 64 lot {. Lookasid
61e50 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20 eSlot *pNext;
61e60 20 2f 2a 20 4e 65 78 74 20 62 75 66 66 65 72 20 /* Next buffer
61e70 69 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 20 66 in the list of f
61e80 72 65 65 20 62 75 66 66 65 72 73 20 2a 2f 0a 7d ree buffers */.}
61e90 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61 73 68 20 ;../*.** A hash
61ea0 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 table for functi
61eb0 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a on definitions..
61ec0 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61 63 68 20 **.** Hash each
61ed0 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 FuncDef structur
61ee0 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68 e into one of th
61ef0 65 20 46 75 6e 63 44 65 66 48 61 73 68 2e 61 5b e FuncDefHash.a[
61f00 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43 6f 6c 6c ] slots..** Coll
61f10 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68 isions are on th
61f20 65 20 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20 e FuncDef.pHash
61f30 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 chain..*/.struct
61f40 20 46 75 6e 63 44 65 66 48 61 73 68 20 7b 0a 20 FuncDefHash {.
61f50 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 33 5d 3b FuncDef *a[23];
61f60 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 /* Hash t
61f70 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 6f able for functio
61f80 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ns */.};../*.**
61f90 45 61 63 68 20 64 61 74 61 62 61 73 65 20 69 73 Each database is
61fa0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
61fb0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
61fc0 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
61fd0 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 74 52 6f he sqlite.lastRo
61fe0 77 69 64 20 72 65 63 6f 72 64 73 20 74 68 65 20 wid records the
61ff0 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69 last insert rowi
62000 64 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 d generated by a
62010 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73 74 61 74 n.** insert stat
62020 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72 74 73 20 ement. Inserts
62030 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 20 on views do not
62040 61 66 66 65 63 74 20 69 74 73 20 76 61 6c 75 65 affect its value
62050 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72 69 67 67 . Each.** trigg
62060 65 72 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63 er has its own c
62070 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68 61 74 20 ontext, so that
62080 6c 61 73 74 52 6f 77 69 64 20 63 61 6e 20 62 65 lastRowid can be
62090 20 75 70 64 61 74 65 64 20 69 6e 73 69 64 65 0a updated inside.
620a0 2a 2a 20 74 72 69 67 67 65 72 73 20 61 73 20 75 ** triggers as u
620b0 73 75 61 6c 2e 20 20 54 68 65 20 70 72 65 76 69 sual. The previ
620c0 6f 75 73 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 ous value will b
620d0 65 20 72 65 73 74 6f 72 65 64 20 6f 6e 63 65 20 e restored once
620e0 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 65 the trigger.** e
620f0 78 69 74 73 2e 20 20 55 70 6f 6e 20 65 6e 74 65 xits. Upon ente
62100 72 69 6e 67 20 61 20 62 65 66 6f 72 65 20 6f 72 ring a before or
62110 20 69 6e 73 74 65 61 64 20 6f 66 20 74 72 69 67 instead of trig
62120 67 65 72 2c 20 6c 61 73 74 52 6f 77 69 64 20 69 ger, lastRowid i
62130 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65 72 20 28 s no.** longer (
62140 73 69 6e 63 65 20 61 66 74 65 72 20 76 65 72 73 since after vers
62150 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72 65 73 65 ion 2.8.12) rese
62160 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a 20 54 t to -1..**.** T
62170 68 65 20 73 71 6c 69 74 65 2e 6e 43 68 61 6e 67 he sqlite.nChang
62180 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 75 6e 74 e does not count
62190 20 63 68 61 6e 67 65 73 20 77 69 74 68 69 6e 20 changes within
621a0 74 72 69 67 67 65 72 73 20 61 6e 64 20 6b 65 65 triggers and kee
621b0 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74 65 78 74 ps no.** context
621c0 2e 20 20 49 74 20 69 73 20 72 65 73 65 74 20 61 . It is reset a
621d0 74 20 73 74 61 72 74 20 6f 66 20 73 71 6c 69 74 t start of sqlit
621e0 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54 68 65 20 e3_exec..** The
621f0 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e 67 65 20 sqlite.lsChange
62200 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6e represents the n
62210 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 umber of changes
62220 20 6d 61 64 65 20 62 79 20 74 68 65 20 6c 61 73 made by the las
62230 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20 75 70 64 t.** insert, upd
62240 61 74 65 2c 20 6f 72 20 64 65 6c 65 74 65 20 73 ate, or delete s
62250 74 61 74 65 6d 65 6e 74 2e 20 20 49 74 20 72 65 tatement. It re
62260 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e 74 20 74 mains constant t
62270 68 72 6f 75 67 68 6f 75 74 20 74 68 65 0a 2a 2a hroughout the.**
62280 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73 74 61 length of a sta
62290 74 65 6d 65 6e 74 20 61 6e 64 20 69 73 20 74 68 tement and is th
622a0 65 6e 20 75 70 64 61 74 65 64 20 62 79 20 4f 50 en updated by OP
622b0 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20 49 74 20 _SetCounts. It
622c0 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f 6e 74 65 keeps a.** conte
622d0 78 74 20 73 74 61 63 6b 20 6a 75 73 74 20 6c 69 xt stack just li
622e0 6b 65 20 6c 61 73 74 52 6f 77 69 64 20 73 6f 20 ke lastRowid so
622f0 74 68 61 74 20 74 68 65 20 63 6f 75 6e 74 20 6f that the count o
62300 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 77 69 74 f changes.** wit
62310 68 69 6e 20 61 20 74 72 69 67 67 65 72 20 69 73 hin a trigger is
62320 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74 73 69 64 not seen outsid
62330 65 20 74 68 65 20 74 72 69 67 67 65 72 2e 20 20 e the trigger.
62340 43 68 61 6e 67 65 73 20 74 6f 20 76 69 65 77 73 Changes to views
62350 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66 66 65 63 do not.** affec
62360 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6c t the value of l
62370 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 20 sChange..** The
62380 73 71 6c 69 74 65 2e 63 73 43 68 61 6e 67 65 20 sqlite.csChange
62390 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74 keeps track of t
623a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75 72 he number of cur
623b0 72 65 6e 74 20 63 68 61 6e 67 65 73 20 28 73 69 rent changes (si
623c0 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 nce.** the last
623d0 73 74 61 74 65 6d 65 6e 74 29 20 61 6e 64 20 69 statement) and i
623e0 73 20 75 73 65 64 20 74 6f 20 75 70 64 61 74 65 s used to update
623f0 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 6e 67 65 sqlite_lsChange
62400 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 62 ..**.** The memb
62410 65 72 20 76 61 72 69 61 62 6c 65 73 20 73 71 6c er variables sql
62420 69 74 65 2e 65 72 72 43 6f 64 65 2c 20 73 71 6c ite.errCode, sql
62430 69 74 65 2e 7a 45 72 72 4d 73 67 20 61 6e 64 20 ite.zErrMsg and
62440 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 31 36 sqlite.zErrMsg16
62450 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 6d 6f .** store the mo
62460 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 st recent error
62470 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 61 70 70 code and, if app
62480 6c 69 63 61 62 6c 65 2c 20 73 74 72 69 6e 67 2e licable, string.
62490 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c The.** internal
624a0 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 function sqlite
624b0 33 45 72 72 6f 72 28 29 20 69 73 20 75 73 65 64 3Error() is used
624c0 20 74 6f 20 73 65 74 20 74 68 65 73 65 20 76 61 to set these va
624d0 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f 6e 73 69 riables.** consi
624e0 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73 74 72 75 stently..*/.stru
624f0 63 74 20 73 71 6c 69 74 65 33 20 7b 0a 20 20 73 ct sqlite3 {. s
62500 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
62510 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
62520 4f 53 20 49 6e 74 65 72 66 61 63 65 20 2a 2f 0a OS Interface */.
62530 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 int nDb;
62540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62550 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 /* Number of bac
62560 6b 65 6e 64 73 20 63 75 72 72 65 6e 74 6c 79 20 kends currently
62570 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44 62 20 2a in use */. Db *
62580 61 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 aDb;
62590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c /* All
625a0 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 69 backends */. i
625b0 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 nt flags;
625c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
625d0 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61 Miscellanous fla
625e0 67 73 2e 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f gs. See below */
625f0 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 . int openFlags
62600 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
62610 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 /* Flags passed
62620 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e to sqlite3_vfs.
62630 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 xOpen() */. int
62640 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20 20 errCode;
62650 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f /* Mo
62660 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 st recent error
62670 63 6f 64 65 20 28 53 51 4c 49 54 45 5f 2a 29 20 code (SQLITE_*)
62680 2a 2f 0a 20 20 69 6e 74 20 65 72 72 4d 61 73 6b */. int errMask
62690 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
626a0 20 20 20 2f 2a 20 26 20 72 65 73 75 6c 74 20 63 /* & result c
626b0 6f 64 65 73 20 77 69 74 68 20 74 68 69 73 20 62 odes with this b
626c0 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 efore returning
626d0 2a 2f 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d 6d */. u8 autoComm
626e0 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 it;
626f0 20 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 /* The auto-c
62700 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a 20 ommit flag. */.
62710 20 75 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b 20 u8 temp_store;
62720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
62730 2a 20 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65 6d * 1: file 2: mem
62740 6f 72 79 20 30 3a 20 64 65 66 61 75 6c 74 20 2a ory 0: default *
62750 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f 63 46 61 69 /. u8 mallocFai
62760 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 led;
62770 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 /* True if we
62780 68 61 76 65 20 73 65 65 6e 20 61 20 6d 61 6c 6c have seen a mall
62790 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 oc failure */.
627a0 75 38 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b u8 dfltLockMode;
627b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
627c0 20 44 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 Default locking
627d0 2d 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 -mode for attach
627e0 65 64 20 64 62 73 20 2a 2f 0a 20 20 75 38 20 64 ed dbs */. u8 d
627f0 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 fltJournalMode;
62800 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 /* Def
62810 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 ault journal mod
62820 65 20 66 6f 72 20 61 74 74 61 63 68 65 64 20 64 e for attached d
62830 62 73 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 bs */. signed c
62840 68 61 72 20 6e 65 78 74 41 75 74 6f 76 61 63 3b har nextAutovac;
62850 20 20 20 20 20 20 2f 2a 20 41 75 74 6f 76 61 63 /* Autovac
62860 20 73 65 74 74 69 6e 67 20 61 66 74 65 72 20 56 setting after V
62870 41 43 55 55 4d 20 69 66 20 3e 3d 30 20 2a 2f 0a ACUUM if >=0 */.
62880 20 20 69 6e 74 20 6e 65 78 74 50 61 67 65 73 69 int nextPagesi
62890 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
628a0 2f 2a 20 50 61 67 65 73 69 7a 65 20 61 66 74 65 /* Pagesize afte
628b0 72 20 56 41 43 55 55 4d 20 69 66 20 3e 30 20 2a r VACUUM if >0 *
628c0 2f 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 3b 20 /. int nTable;
628d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
628e0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
628f0 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74 ables in the dat
62900 61 62 61 73 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 abase */. CollS
62910 65 71 20 2a 70 44 66 6c 74 43 6f 6c 6c 3b 20 20 eq *pDfltColl;
62920 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
62930 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e default collatin
62940 67 20 73 65 71 75 65 6e 63 65 20 28 42 49 4e 41 g sequence (BINA
62950 52 59 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 RY) */. i64 las
62960 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 tRowid;
62970 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 /* ROWID
62980 6f 66 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 of most recent i
62990 6e 73 65 72 74 20 28 73 65 65 20 61 62 6f 76 65 nsert (see above
629a0 29 20 2a 2f 0a 20 20 69 36 34 20 70 72 69 6f 72 ) */. i64 prior
629b0 4e 65 77 52 6f 77 69 64 3b 20 20 20 20 20 20 20 NewRowid;
629c0 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 61 6e /* Last ran
629d0 64 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65 64 20 domly generated
629e0 52 4f 57 49 44 20 2a 2f 0a 20 20 69 6e 74 20 6d ROWID */. int m
629f0 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 agic;
62a00 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 /* Magi
62a10 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74 c number for det
62a20 65 63 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75 ect library misu
62a30 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 se */. int nCha
62a40 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 nge;
62a50 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 /* Value r
62a60 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
62a70 65 33 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a e3_changes() */.
62a80 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e int nTotalChan
62a90 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
62aa0 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 /* Value returne
62ab0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 d by sqlite3_tot
62ac0 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a al_changes() */.
62ad0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
62ae0 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 *mutex;
62af0 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 /* Connection mu
62b00 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 tex */. int aLi
62b10 6d 69 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d mit[SQLITE_N_LIM
62b20 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73 IT]; /* Limits
62b30 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c */. struct sql
62b40 69 74 65 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20 ite3InitInfo {
62b50 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 /* Informati
62b60 6f 6e 20 75 73 65 64 20 64 75 72 69 6e 67 20 69 on used during i
62b70 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f nitialization */
62b80 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 . int iDb;
62b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62ba0 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b 20 69 73 /* When back is
62bb0 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a being initializ
62bc0 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 ed */. int ne
62bd0 77 54 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 wTnum;
62be0 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 /* Rootpag
62bf0 65 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 e of table being
62c00 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
62c10 20 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20 u8 busy;
62c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62c30 2f 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65 /* TRUE if curre
62c40 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e ntly initializin
62c50 67 20 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 g */. } init;.
62c60 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b int nExtension;
62c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
62c80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64 * Number of load
62c90 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f ed extensions */
62ca0 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e . void **aExten
62cb0 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 sion;
62cc0 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68 61 /* Array of sha
62cd0 72 65 64 20 6c 69 62 72 61 72 61 79 20 68 61 6e red libraray han
62ce0 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 dles */. struct
62cf0 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20 Vdbe *pVdbe;
62d00 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
62d10 6f 66 20 61 63 74 69 76 65 20 76 69 72 74 75 61 of active virtua
62d20 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 l machines */.
62d30 69 6e 74 20 61 63 74 69 76 65 56 64 62 65 43 6e int activeVdbeCn
62d40 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
62d50 20 4e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 73 Number of vdbes
62d60 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 currently execu
62d70 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 ting */. int wr
62d80 69 74 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 iteVdbeCnt;
62d90 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
62da0 72 20 6f 66 20 61 63 74 69 76 65 20 56 44 42 45 r of active VDBE
62db0 73 20 74 68 61 74 20 61 72 65 20 77 72 69 74 69 s that are writi
62dc0 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 ng */. void (*x
62dd0 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e Trace)(void*,con
62de0 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20 20 20 st char*);
62df0 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e 63 74 /* Trace funct
62e00 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ion */. void *p
62e10 54 72 61 63 65 41 72 67 3b 20 20 20 20 20 20 20 TraceArg;
62e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62e30 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
62e40 6f 20 74 68 65 20 74 72 61 63 65 20 66 75 6e 63 o the trace func
62e50 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 tion */. void (
62e60 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a *xProfile)(void*
62e70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 36 34 ,const char*,u64
62e80 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 ); /* Profiling
62e90 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
62ea0 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 41 72 67 oid *pProfileArg
62eb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
62ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
62ed0 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c 65 20 ment to profile
62ee0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f function */. vo
62ef0 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20 id *pCommitArg;
62f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62f10 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 /* Argument to x
62f20 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29 CommitCallback()
62f30 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28 2a 78 */ . int (*x
62f40 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28 CommitCallback)(
62f50 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e void*); /* In
62f60 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63 voked at every c
62f70 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 ommit. */. void
62f80 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 *pRollbackArg;
62f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
62fa0 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f Argument to xRo
62fb0 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29 llbackCallback()
62fc0 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20 28 2a */ . void (*
62fd0 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 xRollbackCallbac
62fe0 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e k)(void*); /* In
62ff0 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63 voked at every c
63000 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 ommit. */. void
63010 20 2a 70 55 70 64 61 74 65 41 72 67 3b 0a 20 20 *pUpdateArg;.
63020 76 6f 69 64 20 28 2a 78 55 70 64 61 74 65 43 61 void (*xUpdateCa
63030 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e llback)(void*,in
63040 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 t, const char*,c
63050 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 onst char*,sqlit
63060 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 e_int64);. void
63070 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 (*xCollNeeded)(v
63080 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e oid*,sqlite3*,in
63090 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 t eTextRep,const
630a0 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 char*);. void(
630b0 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 *xCollNeeded16)(
630c0 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 void*,sqlite3*,i
630d0 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 nt eTextRep,cons
630e0 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 t void*);. void
630f0 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 *pCollNeededArg
63100 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ;. sqlite3_valu
63110 65 20 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20 e *pErr;
63120 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 /* Most recent
63130 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a error message *
63140 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 /. char *zErrMs
63150 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
63160 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 /* Most recent
63170 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 error message (
63180 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a UTF-8 encoded) *
63190 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 /. char *zErrMs
631a0 67 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 g16;
631b0 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 /* Most recent
631c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 error message (
631d0 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 UTF-16 encoded)
631e0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 */. union {.
631f0 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 volatile int is
63200 49 6e 74 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 Interrupted; /*
63210 54 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f True if sqlite3_
63220 69 6e 74 65 72 72 75 70 74 20 68 61 73 20 62 65 interrupt has be
63230 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 en called */.
63240 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 double notUsed1
63250 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
63260 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 Spacer */. } u1
63270 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f ;. Lookaside lo
63280 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 okaside;
63290 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d /* Lookaside m
632a0 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 alloc configurat
632b0 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ion */.#ifndef S
632c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
632d0 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 RIZATION. int (
632e0 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e *xAuth)(void*,in
632f0 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f t,const char*,co
63300 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 nst char*,const
63310 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
63320 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 *);.
63330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63340 20 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75 /* Access au
63350 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 thorization func
63360 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a tion */. void *
63370 70 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20 pAuthArg;
63380 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61 /* 1st a
63390 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 rgument to the a
633a0 63 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74 ccess auth funct
633b0 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ion */.#endif.#i
633c0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
633d0 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 T_PROGRESS_CALLB
633e0 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f ACK. int (*xPro
633f0 67 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 gress)(void *);
63400 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 /* The progr
63410 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a ess callback */.
63420 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 void *pProgres
63430 73 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 sArg;
63440 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 /* Argument to t
63450 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c he progress call
63460 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 back */. int nP
63470 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20 rogressOps;
63480 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
63490 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 r of opcodes for
634a0 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 progress callba
634b0 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 ck */.#endif.#if
634c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
634d0 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
634e0 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 Hash aModule;
634f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
63500 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71 populated by sq
63510 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
63520 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 ule() */. Table
63530 20 2a 70 56 54 61 62 3b 20 20 20 20 20 20 20 20 *pVTab;
63540 20 20 20 20 20 20 20 20 20 2f 2a 20 76 74 61 62 /* vtab
63550 20 77 69 74 68 20 61 63 74 69 76 65 20 43 6f 6e with active Con
63560 6e 65 63 74 2f 43 72 65 61 74 65 20 6d 65 74 68 nect/Create meth
63570 6f 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f od */. sqlite3_
63580 76 74 61 62 20 2a 2a 61 56 54 72 61 6e 73 3b 20 vtab **aVTrans;
63590 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c /* Virtual
635a0 20 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65 tables with ope
635b0 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a n transactions *
635c0 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b /. int nVTrans;
635d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
635e0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 /* Allocated s
635f0 69 7a 65 20 6f 66 20 61 56 54 72 61 6e 73 20 2a ize of aVTrans *
63600 2f 0a 23 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 /.#endif. FuncD
63610 65 66 48 61 73 68 20 61 46 75 6e 63 3b 20 20 20 efHash aFunc;
63620 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 /* Hash
63630 20 74 61 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 table of connec
63640 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a tion functions *
63650 2f 0a 20 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 /. Hash aCollSe
63660 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 q;
63670 20 20 2f 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 /* All collati
63680 6e 67 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a ng sequences */.
63690 20 20 42 75 73 79 48 61 6e 64 6c 65 72 20 62 75 BusyHandler bu
636a0 73 79 48 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 syHandler;
636b0 2f 2a 20 42 75 73 79 20 63 61 6c 6c 62 61 63 6b /* Busy callback
636c0 20 2a 2f 0a 20 20 69 6e 74 20 62 75 73 79 54 69 */. int busyTi
636d0 6d 65 6f 75 74 3b 20 20 20 20 20 20 20 20 20 20 meout;
636e0 20 20 20 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 /* Busy hand
636f0 6c 65 72 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 ler timeout, in
63700 6d 73 65 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 msec */. Db aDb
63710 53 74 61 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 Static[2];
63720 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 69 /* Stati
63730 63 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 c space for the
63740 32 20 64 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 2 default backen
63750 64 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ds */.#ifdef SQL
63760 49 54 45 5f 53 53 45 0a 20 20 73 71 6c 69 74 65 ITE_SSE. sqlite
63770 33 5f 73 74 6d 74 20 2a 70 46 65 74 63 68 3b 20 3_stmt *pFetch;
63780 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
63790 62 79 20 53 53 45 20 74 6f 20 66 65 74 63 68 20 by SSE to fetch
637a0 73 74 6f 72 65 64 20 73 74 61 74 65 6d 65 6e 74 stored statement
637b0 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a s */.#endif.};..
637c0 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 74 6f /*.** A macro to
637d0 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 65 6e discover the en
637e0 63 6f 64 69 6e 67 20 6f 66 20 61 20 64 61 74 61 coding of a data
637f0 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 base..*/.#define
63800 20 45 4e 43 28 64 62 29 20 28 28 64 62 29 2d 3e ENC(db) ((db)->
63810 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 2d 3e aDb[0].pSchema->
63820 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 enc)../*.** Poss
63830 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 ible values for
63840 74 68 65 20 73 71 6c 69 74 65 2e 66 6c 61 67 73 the sqlite.flags
63850 20 61 6e 64 20 6f 72 20 44 62 2e 66 6c 61 67 73 and or Db.flags
63860 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f fields..**.** O
63870 6e 20 73 71 6c 69 74 65 2e 66 6c 61 67 73 2c 20 n sqlite.flags,
63880 74 68 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 the SQLITE_InTra
63890 6e 73 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74 ns value means t
638a0 68 61 74 20 77 65 20 68 61 76 65 0a 2a 2a 20 65 hat we have.** e
638b0 78 65 63 75 74 65 64 20 61 20 42 45 47 49 4e 2e xecuted a BEGIN.
638c0 20 20 4f 6e 20 44 62 2e 66 6c 61 67 73 2c 20 53 On Db.flags, S
638d0 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 6d 65 QLITE_InTrans me
638e0 61 6e 73 20 61 20 73 74 61 74 65 6d 65 6e 74 0a ans a statement.
638f0 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ** transaction i
63900 73 20 61 63 74 69 76 65 20 6f 6e 20 74 68 61 74 s active on that
63910 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 particular data
63920 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64 base file..*/.#d
63930 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 efine SQLITE_Vdb
63940 65 54 72 61 63 65 20 20 20 20 20 20 30 78 30 30 eTrace 0x00
63950 30 30 30 30 30 31 20 20 2f 2a 20 54 72 75 65 20 000001 /* True
63960 74 6f 20 74 72 61 63 65 20 56 44 42 45 20 65 78 to trace VDBE ex
63970 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 ecution */.#defi
63980 6e 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e ne SQLITE_InTran
63990 73 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 s 0x00000
639a0 30 30 38 20 20 2f 2a 20 54 72 75 65 20 69 66 20 008 /* True if
639b0 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e in a transaction
639c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
639d0 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 TE_InternChanges
639e0 20 20 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 0x00000010 /*
639f0 20 55 6e 63 6f 6d 6d 69 74 74 65 64 20 48 61 73 Uncommitted Has
63a00 68 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20 h table changes
63a10 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
63a20 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20 E_FullColNames
63a30 20 30 78 30 30 30 30 30 30 32 30 20 20 2f 2a 20 0x00000020 /*
63a40 53 68 6f 77 20 66 75 6c 6c 20 63 6f 6c 75 6d 6e Show full column
63a50 20 6e 61 6d 65 73 20 6f 6e 20 53 45 4c 45 43 54 names on SELECT
63a60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
63a70 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 TE_ShortColNames
63a80 20 20 30 78 30 30 30 30 30 30 34 30 20 20 2f 2a 0x00000040 /*
63a90 20 53 68 6f 77 20 73 68 6f 72 74 20 63 6f 6c 75 Show short colu
63aa0 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 mns names */.#de
63ab0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e fine SQLITE_Coun
63ac0 74 52 6f 77 73 20 20 20 20 20 20 30 78 30 30 30 tRows 0x000
63ad0 30 30 30 38 30 20 20 2f 2a 20 43 6f 75 6e 74 20 00080 /* Count
63ae0 72 6f 77 73 20 63 68 61 6e 67 65 64 20 62 79 20 rows changed by
63af0 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 20 20 20 20 INSERT, */.
63b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63b20 20 20 20 20 20 2f 2a 20 20 20 44 45 4c 45 54 45 /* DELETE
63b30 2c 20 6f 72 20 55 50 44 41 54 45 20 61 6e 64 20 , or UPDATE and
63b40 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 20 20 return */.
63b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63b70 20 20 20 20 2f 2a 20 20 20 74 68 65 20 63 6f 75 /* the cou
63b80 6e 74 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 62 nt using a callb
63b90 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ack. */.#define
63ba0 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 SQLITE_NullCallb
63bb0 61 63 6b 20 20 20 30 78 30 30 30 30 30 31 30 30 ack 0x00000100
63bc0 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 /* Invoke the
63bd0 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 69 66 callback once if
63be0 20 74 68 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 the */.
63bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63c10 20 20 2f 2a 20 20 20 72 65 73 75 6c 74 20 73 65 /* result se
63c20 74 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64 t is empty */.#d
63c30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 71 6c efine SQLITE_Sql
63c40 54 72 61 63 65 20 20 20 20 20 20 20 30 78 30 30 Trace 0x00
63c50 30 30 30 32 30 30 20 20 2f 2a 20 44 65 62 75 67 000200 /* Debug
63c60 20 70 72 69 6e 74 20 53 51 4c 20 61 73 20 69 74 print SQL as it
63c70 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 23 64 65 executes */.#de
63c80 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 65 fine SQLITE_Vdbe
63c90 4c 69 73 74 69 6e 67 20 20 20 20 30 78 30 30 30 Listing 0x000
63ca0 30 30 34 30 30 20 20 2f 2a 20 44 65 62 75 67 20 00400 /* Debug
63cb0 6c 69 73 74 69 6e 67 73 20 6f 66 20 56 44 42 45 listings of VDBE
63cc0 20 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 23 64 65 programs */.#de
63cd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 72 69 74 fine SQLITE_Writ
63ce0 65 53 63 68 65 6d 61 20 20 20 20 30 78 30 30 30 eSchema 0x000
63cf0 30 30 38 30 30 20 20 2f 2a 20 4f 4b 20 74 6f 20 00800 /* OK to
63d00 75 70 64 61 74 65 20 53 51 4c 49 54 45 5f 4d 41 update SQLITE_MA
63d10 53 54 45 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20 STER */.#define
63d20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 SQLITE_NoReadloc
63d30 6b 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30 k 0x00001000
63d40 20 20 2f 2a 20 52 65 61 64 6c 6f 63 6b 73 20 61 /* Readlocks a
63d50 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65 6e 20 re omitted when
63d60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
63d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63d80 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 63 ** ac
63d90 63 65 73 73 69 6e 67 20 72 65 61 64 2d 6f 6e 6c cessing read-onl
63da0 79 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 23 y databases */.#
63db0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 67 define SQLITE_Ig
63dc0 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30 78 30 noreChecks 0x0
63dd0 30 30 30 32 30 30 30 20 20 2f 2a 20 44 6f 20 6e 0002000 /* Do n
63de0 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65 63 6b ot enforce check
63df0 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a constraints */.
63e00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
63e10 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 30 eadUncommitted 0
63e20 78 30 30 30 30 34 30 30 30 20 2f 2a 20 46 6f 72 x00004000 /* For
63e30 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f shared-cache mo
63e40 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 de */.#define SQ
63e50 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 LITE_LegacyFileF
63e60 6d 74 20 20 30 78 30 30 30 30 38 30 30 30 20 20 mt 0x00008000
63e70 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 64 61 /* Create new da
63e80 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72 6d 61 tabases in forma
63e90 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 t 1 */.#define S
63ea0 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 QLITE_FullFSync
63eb0 20 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20 0x00010000
63ec0 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 73 79 /* Use full fsy
63ed0 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b 65 6e nc on the backen
63ee0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
63ef0 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f ITE_LoadExtensio
63f00 6e 20 20 30 78 30 30 30 32 30 30 30 30 20 20 2f n 0x00020000 /
63f10 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f 65 78 * Enable load_ex
63f20 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a 23 64 65 66 tension */..#def
63f30 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 ine SQLITE_Recov
63f40 65 72 79 4d 6f 64 65 20 20 20 30 78 30 30 30 34 eryMode 0x0004
63f50 30 30 30 30 20 20 2f 2a 20 49 67 6e 6f 72 65 20 0000 /* Ignore
63f60 73 63 68 65 6d 61 20 65 72 72 6f 72 73 20 2a 2f schema errors */
63f70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
63f80 53 68 61 72 65 64 43 61 63 68 65 20 20 20 20 30 SharedCache 0
63f90 78 30 30 30 38 30 30 30 30 20 20 2f 2a 20 43 61 x00080000 /* Ca
63fa0 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 65 che sharing is e
63fb0 6e 61 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e nabled */.#defin
63fc0 65 20 53 51 4c 49 54 45 5f 56 74 61 62 20 20 20 e SQLITE_Vtab
63fd0 20 20 20 20 20 20 20 20 30 78 30 30 31 30 30 30 0x001000
63fe0 30 30 20 20 2f 2a 20 54 68 65 72 65 20 65 78 69 00 /* There exi
63ff0 73 74 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 sts a virtual ta
64000 62 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f ble */../*.** Po
64010 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f ssible values fo
64020 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 r the sqlite.mag
64030 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65 ic field..** The
64040 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 numbers are obt
64050 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20 ained at random
64060 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63 and have no spec
64070 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68 ial meaning, oth
64080 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67 er.** than being
64090 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f distinct from o
640a0 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23 ne another..*/.#
640b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
640c0 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61 GIC_OPEN 0xa
640d0 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61 029a697 /* Data
640e0 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a base is open */.
640f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
64100 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78 AGIC_CLOSED 0x
64110 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74 9f3c2d33 /* Dat
64120 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 abase is closed
64130 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
64140 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20 E_MAGIC_SICK
64150 20 30 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20 0x4b771290 /*
64160 45 72 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69 Error and awaiti
64170 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 ng close */.#def
64180 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 ine SQLITE_MAGIC
64190 5f 42 55 53 59 20 20 20 20 20 30 78 66 30 33 62 _BUSY 0xf03b
641a0 37 39 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73 7906 /* Databas
641b0 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 e currently in u
641c0 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 se */.#define SQ
641d0 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 LITE_MAGIC_ERROR
641e0 20 20 20 20 30 78 62 35 33 35 37 39 33 30 20 20 0xb5357930
641f0 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53 /* An SQLITE_MIS
64200 55 53 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72 USE error occurr
64210 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 ed */../*.** Eac
64220 68 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 h SQL function i
64230 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 s defined by an
64240 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
64250 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 following.** str
64260 75 63 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74 ucture. A point
64270 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 er to this struc
64280 74 75 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 ture is stored i
64290 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75 n the sqlite.aFu
642a0 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 nc.** hash table
642b0 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 . When multiple
642c0 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 functions have
642d0 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 the same name, t
642e0 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a he hash table.**
642f0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e points to a lin
64300 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 ked list of thes
64310 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f e structures..*/
64320 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 .struct FuncDef
64330 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20 {. i16 nArg;
64340 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
64350 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e er of arguments.
64360 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d -1 means unlim
64370 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72 ited */. u8 iPr
64380 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f efEnc; /
64390 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 78 74 * Preferred text
643a0 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54 encoding (SQLIT
643b0 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36 E_UTF8, 16LE, 16
643c0 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 BE) */. u8 flag
643d0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s; /*
643e0 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f Some combinatio
643f0 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43 n of SQLITE_FUNC
64400 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 _* */. void *pU
64410 73 65 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 serData; /*
64420 55 73 65 72 20 64 61 74 61 20 70 61 72 61 6d 65 User data parame
64430 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 ter */. FuncDef
64440 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a *pNext; /*
64450 20 4e 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77 Next function w
64460 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f ith same name */
64470 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 . void (*xFunc)
64480 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
64490 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
644a0 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c lue**); /* Regul
644b0 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 ar function */.
644c0 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 void (*xStep)(s
644d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
644e0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
644f0 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61 e**); /* Aggrega
64500 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69 te step */. voi
64510 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 d (*xFinalize)(s
64520 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
64530 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
64540 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 69 /* Aggregate fi
64550 6e 69 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 nializer */. ch
64560 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 ar *zName;
64570 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f /* SQL name o
64580 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 f the function.
64590 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 48 */. FuncDef *pH
645a0 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 ash; /* Nex
645b0 74 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 t with a differe
645c0 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68 65 20 nt name but the
645d0 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d 3b 0a same hash */.};.
645e0 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 ./*.** Possible
645f0 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63 44 values for FuncD
64600 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 ef.flags.*/.#def
64610 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f ine SQLITE_FUNC_
64620 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20 2f 2a LIKE 0x01 /*
64630 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72 20 74 Candidate for t
64640 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 he LIKE optimiza
64650 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tion */.#define
64660 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 SQLITE_FUNC_CASE
64670 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43 61 73 0x02 /* Cas
64680 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 4b 45 e-sensitive LIKE
64690 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e 20 2a -type function *
646a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
646b0 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 20 30 _FUNC_EPHEM 0
646c0 78 30 34 20 2f 2a 20 45 70 68 65 72 6d 65 72 61 x04 /* Ephermera
646d0 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68 20 l. Delete with
646e0 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 VDBE */.#define
646f0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 SQLITE_FUNC_NEED
64700 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 6c COLL 0x08 /* sql
64710 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 ite3GetFuncCollS
64720 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63 61 eq() might be ca
64730 6c 6c 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 lled */../*.** T
64740 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 he following thr
64750 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43 54 ee macros, FUNCT
64760 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43 28 ION(), LIKEFUNC(
64770 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 45 28 ) and AGGREGATE(
64780 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f ) are.** used to
64790 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 69 74 create the init
647a0 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68 65 ializers for the
647b0 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 FuncDef structu
647c0 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e res..**.** FUN
647d0 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 CTION(zName, nAr
647e0 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 g, iArg, bNC, xF
647f0 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 unc).** Used
64800 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61 to create a sca
64810 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 lar function def
64820 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e inition of a fun
64830 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 ction zName .**
64840 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 implemented
64850 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 46 by C function xF
64860 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 73 unc that accepts
64870 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 2e nArg arguments.
64880 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c 75 The.** valu
64890 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72 67 e passed as iArg
648a0 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 is cast to a (v
648b0 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20 61 oid*) and made a
648c0 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 20 vailable.**
648d0 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 61 as the user-data
648e0 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 (sqlite3_user_d
648f0 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 20 66 ata()) for the f
64900 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20 unction. If .**
64910 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e 43 argument bNC
64920 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
64930 68 65 20 46 75 6e 63 44 65 66 2e 6e 65 65 64 43 he FuncDef.needC
64940 6f 6c 6c 61 74 65 20 66 6c 61 67 20 69 73 20 73 ollate flag is s
64950 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 52 et..**.** AGGR
64960 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 EGATE(zName, nAr
64970 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 53 g, iArg, bNC, xS
64980 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a 20 tep, xFinal).**
64990 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 Used to crea
649a0 74 65 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 te an aggregate
649b0 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 function definit
649c0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 ion implemented
649d0 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20 43 20 by.** the C
649e0 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65 70 20 functions xStep
649f0 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68 65 20 and xFinal. The
64a00 66 69 72 73 74 20 66 6f 75 72 20 70 61 72 61 6d first four param
64a10 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 72 65 eters.** are
64a20 20 69 6e 74 65 72 70 72 65 74 65 64 20 69 6e 20 interpreted in
64a30 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 73 20 the same way as
64a40 74 68 65 20 66 69 72 73 74 20 34 20 70 61 72 61 the first 4 para
64a50 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20 meters to.**
64a60 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a 0a FUNCTION()..**.
64a70 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28 7a 4e ** LIKEFUNC(zN
64a80 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c ame, nArg, pArg,
64a90 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20 20 55 flags).** U
64aa0 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 sed to create a
64ab0 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 scalar function
64ac0 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 definition of a
64ad0 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a function zName .
64ae0 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63 63 65 ** that acce
64af0 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e pts nArg argumen
64b00 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d ts and is implem
64b10 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c 6c 20 ented by a call
64b20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66 75 6e to C .** fun
64b30 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20 ction likeFunc.
64b40 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20 69 73 Argument pArg is
64b50 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64 cast to a (void
64b60 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a 2a 20 *) and made.**
64b70 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 61 73 available as
64b80 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 the function us
64b90 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 er-data (sqlite3
64ba0 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e 20 54 _user_data()). T
64bb0 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44 65 he.** FuncDe
64bc0 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c 65 f.flags variable
64bd0 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 is set to the v
64be0 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 alue passed as t
64bf0 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20 20 20 he flags.**
64c00 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 64 parameter..*/.#d
64c10 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e 28 7a efine FUNCTION(z
64c20 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 Name, nArg, iArg
64c30 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a , bNC, xFunc) \.
64c40 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f {nArg, SQLITE_
64c50 55 54 46 38 2c 20 62 4e 43 2a 38 2c 20 53 51 4c UTF8, bNC*8, SQL
64c60 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 69 ITE_INT_TO_PTR(i
64c70 41 72 67 29 2c 20 30 2c 20 78 46 75 6e 63 2c 20 Arg), 0, xFunc,
64c80 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 7d 0a 23 64 0, 0, #zName}.#d
64c90 65 66 69 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 efine STR_FUNCTI
64ca0 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 ON(zName, nArg,
64cb0 70 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 pArg, bNC, xFunc
64cc0 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c ) \. {nArg, SQL
64cd0 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a 38 2c ITE_UTF8, bNC*8,
64ce0 20 70 41 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c pArg, 0, xFunc,
64cf0 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 7d 0a 23 0, 0, #zName}.#
64d00 64 65 66 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 define LIKEFUNC(
64d10 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 zName, nArg, arg
64d20 2c 20 66 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 , flags) \. {nA
64d30 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c rg, SQLITE_UTF8,
64d40 20 66 6c 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 flags, (void *)
64d50 61 72 67 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 arg, 0, likeFunc
64d60 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 7d 0a , 0, 0, #zName}.
64d70 23 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 #define AGGREGAT
64d80 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 E(zName, nArg, a
64d90 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 rg, nc, xStep, x
64da0 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 Final) \. {nArg
64db0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e , SQLITE_UTF8, n
64dc0 63 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f c*8, SQLITE_INT_
64dd0 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 TO_PTR(arg), 0,
64de0 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 0, xStep,xFinal,
64df0 20 23 7a 4e 61 6d 65 7d 0a 0a 0a 2f 2a 0a 2a 2a #zName}.../*.**
64e00 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 Each SQLite mod
64e10 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 ule (virtual tab
64e20 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 le definition) i
64e30 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a s defined by an.
64e40 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
64e50 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
64e60 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 ucture, stored i
64e70 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d n the sqlite3.aM
64e80 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 odule.** hash ta
64e90 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d ble..*/.struct M
64ea0 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 odule {. const
64eb0 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
64ec0 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f pModule; /
64ed0 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 * Callback point
64ee0 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ers */. const c
64ef0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
64f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
64f10 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 Name passed to
64f20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 create_module()
64f30 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b */. void *pAux;
64f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64f50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 /* pAu
64f60 78 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 x passed to crea
64f70 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 te_module() */.
64f80 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 void (*xDestroy
64f90 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 )(void *);
64fa0 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 /* Module
64fb0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 destructor funct
64fc0 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ion */.};../*.**
64fd0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
64fe0 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f ut each column o
64ff0 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 f an SQL table i
65000 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 s held in an ins
65010 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 tance.** of this
65020 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
65030 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20 truct Column {.
65040 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
65050 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 /* Name of thi
65060 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 s column */. Ex
65070 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f pr *pDflt; /
65080 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 * Default value
65090 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a of this column *
650a0 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b /. char *zType;
650b0 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 /* Data typ
650c0 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d e for this colum
650d0 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f n */. char *zCo
650e0 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 ll; /* Colla
650f0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 ting sequence.
65100 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 If NULL, use the
65110 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 default */. u8
65120 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f notNull; /
65130 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 * True if there
65140 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f is a NOT NULL co
65150 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 nstraint */. u8
65160 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f isPrimKey; /
65170 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63 * True if this c
65180 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 olumn is part of
65190 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
651a0 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e */. char affin
651b0 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 ity; /* One of
651c0 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f the SQLITE_AFF_
651d0 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 ... values */.#i
651e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
651f0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
65200 20 75 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20 u8 isHidden;
65210 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
65220 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 s column is 'hid
65230 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d den' */.#endif.}
65240 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c ;../*.** A "Coll
65250 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 ating Sequence"
65260 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e is defined by an
65270 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
65280 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 following.** st
65290 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 ructure. Concept
652a0 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 ually, a collati
652b0 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 ng sequence cons
652c0 69 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 ists of a name a
652d0 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 nd.** a comparis
652e0 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 on routine that
652f0 64 65 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 defines the orde
65300 72 20 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e r of that sequen
65310 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 ce..**.** There
65320 6d 61 79 20 74 77 6f 20 73 65 70 65 72 61 74 65 may two seperate
65330 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
65340 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f of the collatio
65350 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a n function, one.
65360 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 ** that processe
65370 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 s text in UTF-8
65380 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 encoding (CollSe
65390 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 q.xCmp) and anot
653a0 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 her that.** proc
653b0 65 73 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 esses text encod
653c0 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f ed in UTF-16 (Co
653d0 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 llSeq.xCmp16), u
653e0 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 sing the machine
653f0 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 .** native byte
65400 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f order. When a co
65410 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
65420 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c is invoked, SQL
65430 69 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 ite selects.** t
65440 68 65 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 he version that
65450 77 69 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 will require the
65460 20 6c 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 least expensive
65470 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 encoding.** tra
65480 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e nslations, if an
65490 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c y..**.** The Col
654a0 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 lSeq.pUser membe
654b0 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e r variable is an
654c0 20 65 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 extra parameter
654d0 20 74 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a that passed in.
654e0 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ** as the first
654f0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
65500 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e UTF-8 comparison
65510 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e function, xCmp.
65520 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 .** CollSeq.pUse
65530 72 31 36 20 69 73 20 74 68 65 20 65 71 75 69 76 r16 is the equiv
65540 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 alent for the UT
65550 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 F-16 comparison
65560 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d function,.** xCm
65570 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f p16..**.** If bo
65580 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 th CollSeq.xCmp
65590 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 and CollSeq.xCmp
655a0 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 16 are NULL, it
655b0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a means that the.*
655c0 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 * collating sequ
655d0 65 6e 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 ence is undefine
655e0 64 2e 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c d. Indices buil
655f0 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 t on an undefine
65600 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 d.** collating s
65610 65 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 equence may not
65620 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 be read or writt
65630 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f en..*/.struct Co
65640 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a llSeq {. char *
65650 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
65660 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
65670 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
65680 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 e, UTF-8 encoded
65690 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 */. u8 enc;
656a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
656b0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e ext encoding han
656c0 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a dled by xCmp() *
656d0 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 20 20 20 /. u8 type;
656e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
656f0 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 of the SQLITE_C
65700 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 OLL_... values b
65710 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a elow */. void *
65720 70 55 73 65 72 3b 20 20 20 20 20 20 20 20 20 20 pUser;
65730 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e /* First argumen
65740 74 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 t to xCmp() */.
65750 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 int (*xCmp)(voi
65760 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f d*,int, const vo
65770 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 id*, int, const
65780 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 void*);. void (
65790 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 *xDel)(void*);
657a0 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f /* Destructor fo
657b0 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f r pUser */.};../
657c0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
657d0 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 ues of CollSeq.t
657e0 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ype:.*/.#define
657f0 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 SQLITE_COLL_BINA
65800 52 59 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65 RY 1 /* The de
65810 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 fault memcmp() c
65820 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
65830 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
65840 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 ITE_COLL_NOCASE
65850 20 32 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2 /* The built
65860 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 -in NOCASE colla
65870 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f ting sequence */
65880 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
65890 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20 COLL_REVERSE 3
658a0 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 /* The built-in
658b0 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e REVERSE collatin
658c0 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 g sequence */.#d
658d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c efine SQLITE_COL
658e0 4c 5f 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20 L_USER 0 /*
658f0 41 6e 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64 Any other user-d
65900 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 efined collating
65910 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a sequence */../*
65920 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72 .** A sort order
65930 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 41 can be either A
65940 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 SC or DESC..*/.#
65950 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f define SQLITE_SO
65960 5f 41 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a _ASC 0 /*
65970 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 Sort in ascendi
65980 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 ng order */.#def
65990 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 ine SQLITE_SO_DE
659a0 53 43 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f SC 1 /* So
659b0 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 rt in ascending
659c0 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 order */../*.**
659d0 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 Column affinity
659e0 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 types..**.** The
659f0 73 65 20 75 73 65 64 20 74 6f 20 68 61 76 65 20 se used to have
65a00 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 mnemonic name li
65a10 6b 65 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 ke 'i' for SQLIT
65a20 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e E_AFF_INTEGER an
65a30 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c d.** 't' for SQL
65a40 49 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 ITE_AFF_TEXT. B
65a50 75 74 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 ut we can save a
65a60 20 6c 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e little space an
65a70 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 d improve.** the
65a80 20 73 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20 speed a little
65a90 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 by numbering the
65aa0 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 values consecut
65ab0 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 ively. .**.** B
65ac0 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 ut rather than s
65ad0 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31 tart with 0 or 1
65ae0 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20 , we begin with
65af0 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 'a'. That way,.
65b00 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 ** when multiple
65b10 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 20 affinity types
65b20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 are concatenated
65b30 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61 into a string a
65b40 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68 nd.** used as th
65b50 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 e P4 operand, th
65b60 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 ey will be more
65b70 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 readable..**.**
65b80 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74 Note also that t
65b90 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73 he numeric types
65ba0 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 are grouped tog
65bb0 65 74 68 65 72 20 73 6f 20 74 68 61 74 20 74 65 ether so that te
65bc0 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e sting.** for a n
65bd0 75 6d 65 72 69 63 20 74 79 70 65 20 69 73 20 61 umeric type is a
65be0 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 single comparis
65bf0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 on..*/.#define S
65c00 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 QLITE_AFF_TEXT
65c10 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 'a'.#define S
65c20 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 QLITE_AFF_NONE
65c30 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53 'b'.#define S
65c40 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
65c50 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53 C 'c'.#define S
65c60 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
65c70 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53 R 'd'.#define S
65c80 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 QLITE_AFF_REAL
65c90 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 'e'..#define
65ca0 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 sqlite3IsNumeric
65cb0 41 66 66 69 6e 69 74 79 28 58 29 20 20 28 28 58 Affinity(X) ((X
65cc0 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 )>=SQLITE_AFF_NU
65cd0 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 MERIC)../*.** Th
65ce0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 e SQLITE_AFF_MAS
65cf0 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f K values masks o
65d00 66 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61 ff the significa
65d10 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a nt bits of an.**
65d20 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e affinity value.
65d30 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c .*/.#define SQL
65d40 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 ITE_AFF_MASK
65d50 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 0x67../*.** Add
65d60 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 itional bit valu
65d70 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f es that can be O
65d80 52 65 64 20 77 69 74 68 20 61 6e 20 61 66 66 69 Red with an affi
65d90 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 nity without.**
65da0 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66 changing the aff
65db0 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e inity..*/.#defin
65dc0 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e e SQLITE_JUMPIFN
65dd0 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a ULL 0x08 /* j
65de0 75 6d 70 73 20 69 66 20 65 69 74 68 65 72 20 6f umps if either o
65df0 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a perand is NULL *
65e00 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
65e10 5f 53 54 4f 52 45 50 32 20 20 20 20 20 20 30 78 _STOREP2 0x
65e20 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 10 /* Store res
65e30 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 ult in reg[P2] r
65e40 61 74 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 ather than jump
65e50 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 */../*.** Each S
65e60 51 4c 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 QL table is repr
65e70 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 esented in memor
65e80 79 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 y by an instance
65e90 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f of the.** follo
65ea0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
65eb0 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d **.** Table.zNam
65ec0 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 e is the name of
65ed0 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 the table. The
65ee0 20 63 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 case of the ori
65ef0 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 ginal.** CREATE
65f00 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 TABLE statement
65f10 69 73 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 is stored, but c
65f20 61 73 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 ase is not signi
65f30 66 69 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f ficant for.** co
65f40 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a mparisons..**.**
65f50 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 Table.nCol is t
65f60 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
65f70 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 umns in this tab
65f80 6c 65 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 le. Table.aCol
65f90 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 is a.** pointer
65fa0 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 to an array of C
65fb0 6f 6c 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 olumn structures
65fc0 2c 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 , one for each c
65fd0 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 olumn..**.** If
65fe0 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e the table has an
65ff0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
66000 20 4b 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 KEY, then Table
66010 2e 69 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e .iPKey is the in
66020 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f dex of.** the co
66030 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 lumn that is tha
66040 74 20 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 t key. Otherwi
66050 73 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 se Table.iPKey i
66060 73 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 s negative. Not
66070 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 e.** that the da
66080 74 61 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 tatype of the PR
66090 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 IMARY KEY must b
660a0 65 20 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 e INTEGER for th
660b0 69 73 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 is field to.** b
660c0 65 20 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 e set. An INTEG
660d0 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 ER PRIMARY KEY i
660e0 73 20 75 73 65 64 20 61 73 20 74 68 65 20 72 6f s used as the ro
660f0 77 69 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 wid for each row
66100 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 of.** the table
66110 2e 20 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 . If a table ha
66120 73 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 s no INTEGER PRI
66130 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 MARY KEY, then a
66140 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a random rowid.**
66150 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f is generated fo
66160 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 r each row of th
66170 65 20 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 e table. TF_Has
66180 50 72 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65 PrimaryKey is se
66190 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c t if.** the tabl
661a0 65 20 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 e has any PRIMAR
661b0 59 20 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f Y KEY, INTEGER o
661c0 72 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a r otherwise..**.
661d0 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 ** Table.tnum is
661e0 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
661f0 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 for the root BT
66200 72 65 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 ree page of the
66210 74 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 table in the.**
66220 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
66230 49 66 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 If Table.iDb is
66240 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
66250 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 database table
66260 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 backend.** in sq
66270 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 lite.aDb[]. 0 i
66280 73 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 s for the main d
66290 61 74 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 atabase and 1 is
662a0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 for the file th
662b0 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 at.** holds temp
662c0 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 orary tables and
662d0 20 69 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46 indices. If TF
662e0 5f 45 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65 _Ephemeral is se
662f0 74 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61 t.** then the ta
66300 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e ble is stored in
66310 20 61 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 a file that is
66320 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 automatically de
66330 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 leted.** when th
66340 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f e VDBE cursor to
66350 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c the table is cl
66360 6f 73 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 osed. In this c
66370 61 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a ase Table.tnum .
66380 2a 2a 20 72 65 66 65 72 73 20 56 44 42 45 20 63 ** refers VDBE c
66390 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 ursor number tha
663a0 74 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c t holds the tabl
663b0 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 e open, not to t
663c0 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 he root.** page
663d0 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65 number. Transie
663e0 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 nt tables are us
663f0 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ed to hold the r
66400 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 esults of a.** s
66410 75 62 2d 71 75 65 72 79 20 74 68 61 74 20 61 70 ub-query that ap
66420 70 65 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66 pears instead of
66430 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 a real table na
66440 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 me in the FROM c
66450 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 lause .** of a S
66460 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
66470 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 .*/.struct Table
66480 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
66490 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 ; /* Ass
664a0 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 ociated database
664b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 4d 69 connection. Mi
664c0 67 68 74 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a ght be NULL. */.
664d0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
664e0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
664f0 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 f the table or v
66500 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b iew */. int iPK
66510 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ey; /*
66520 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 If not negative
66530 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79 , use aCol[iPKey
66540 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79 ] as the primary
66550 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 key */. int nC
66560 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ol; /
66570 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
66580 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c mns in this tabl
66590 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 e */. Column *a
665a0 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 Col; /* I
665b0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
665c0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a each column */.
665d0 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b Index *pIndex;
665e0 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f /* List o
665f0 66 20 53 51 4c 20 69 6e 64 65 78 65 73 20 6f 6e f SQL indexes on
66600 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a this table. */.
66610 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 int tnum;
66620 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42 /* Root B
66630 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 74 68 Tree node for th
66640 69 73 20 74 61 62 6c 65 20 28 73 65 65 20 6e 6f is table (see no
66650 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53 te above) */. S
66660 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 elect *pSelect;
66670 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 /* NULL for
66680 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20 tables. Points
66690 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 66 to definition if
666a0 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 75 31 a view. */. u1
666b0 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 6 nRef;
666c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
666d0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 pointers to this
666e0 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 74 Table */. u8 t
666f0 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 abFlags;
66700 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a /* Mask of TF_*
66710 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 values */. u8
66720 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20 20 20 keyConf;
66730 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 /* What to do
66740 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71 75 in case of uniqu
66750 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f eness conflict o
66760 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 54 72 69 n iPKey */. Tri
66770 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 gger *pTrigger;
66780 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c /* List of SQL
66790 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 triggers on thi
667a0 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 46 4b 65 s table */. FKe
667b0 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20 y *pFKey;
667c0 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 /* Linked list
667d0 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 of all foreign
667e0 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74 61 62 keys in this tab
667f0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 le */. char *zC
66800 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 2f 2a 20 olAff; /*
66810 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 String defining
66820 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 the affinity of
66830 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 each column */.#
66840 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
66850 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70 72 20 IT_CHECK. Expr
66860 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 *pCheck;
66870 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 61 6c /* The AND of al
66880 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 l CHECK constrai
66890 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 nts */.#endif.#i
668a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
668b0 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 69 T_ALTERTABLE. i
668c0 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b nt addColOffset;
668d0 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
668e0 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
668f0 6d 74 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20 mt to add a new
66900 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66 column */.#endif
66910 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
66920 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
66930 45 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 E. Module *pMod
66940 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
66950 74 65 72 20 74 6f 20 74 68 65 20 69 6d 70 6c 65 ter to the imple
66960 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
66970 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71 6c module */. sql
66980 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
66990 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 ; /* Pointer to
669a0 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e 73 74 61 the module insta
669b0 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f nce */. int nMo
669c0 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 2f 2a duleArg; /*
669d0 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d Number of argum
669e0 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75 ents to the modu
669f0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 le */. char **a
66a00 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20 zModuleArg; /*
66a10 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 Text of all modu
66a20 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 73 20 le args. [0] is
66a30 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23 module name */.#
66a40 65 6e 64 69 66 0a 20 20 53 63 68 65 6d 61 20 2a endif. Schema *
66a50 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 pSchema; /*
66a60 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e 74 Schema that cont
66a70 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c 65 20 ains this table
66a80 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 78 */. Table *pNex
66a90 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e 65 78 tZombie; /* Nex
66aa0 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65 2e 70 t on the Parse.p
66ab0 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74 20 2a ZombieTab list *
66ac0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f /.};../*.** Allo
66ad0 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 54 wed values for T
66ae0 61 62 65 2e 74 61 62 46 6c 61 67 73 2e 0a 2a 2f abe.tabFlags..*/
66af0 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 65 61 64 .#define TF_Read
66b00 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 78 30 31 only 0x01
66b10 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79 /* Read-only
66b20 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f system table */
66b30 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 70 68 65 .#define TF_Ephe
66b40 6d 65 72 61 6c 20 20 20 20 20 20 20 30 78 30 32 meral 0x02
66b50 20 20 20 20 2f 2a 20 41 6e 20 65 6d 70 68 65 72 /* An empher
66b60 6d 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 mal table */.#de
66b70 66 69 6e 65 20 54 46 5f 48 61 73 50 72 69 6d 61 fine TF_HasPrima
66b80 72 79 4b 65 79 20 20 20 30 78 30 34 20 20 20 20 ryKey 0x04
66b90 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 61 20 70 /* Table has a p
66ba0 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 23 64 rimary key */.#d
66bb0 65 66 69 6e 65 20 54 46 5f 41 75 74 6f 69 6e 63 efine TF_Autoinc
66bc0 72 65 6d 65 6e 74 20 20 20 30 78 30 38 20 20 20 rement 0x08
66bd0 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 72 69 6d /* Integer prim
66be0 61 72 79 20 6b 65 79 20 69 73 20 61 75 74 6f 69 ary key is autoi
66bf0 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 ncrement */.#def
66c00 69 6e 65 20 54 46 5f 56 69 72 74 75 61 6c 20 20 ine TF_Virtual
66c10 20 20 20 20 20 20 20 30 78 31 30 20 20 20 20 2f 0x10 /
66c20 2a 20 49 73 20 61 20 76 69 72 74 75 61 6c 20 74 * Is a virtual t
66c30 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 able */.#define
66c40 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 74 61 20 TF_NeedMetadata
66c50 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 61 43 0x20 /* aC
66c60 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e 64 20 61 ol[].zType and a
66c70 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73 Col[].pColl miss
66c80 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 ing */..../*.**
66c90 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74 Test to see whet
66ca0 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74 61 62 her or not a tab
66cb0 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 le is a virtual
66cc0 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a table. This is.
66cd0 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d 61 63 ** done as a mac
66ce0 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 ro so that it wi
66cf0 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 ll be optimized
66d00 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75 61 6c out when virtual
66d10 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 70 6f 72 .** table suppor
66d20 74 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f t is omitted fro
66d30 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a m the build..*/.
66d40 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
66d50 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
66d60 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72 .# define IsVir
66d70 74 75 61 6c 28 58 29 20 20 20 20 20 20 28 28 28 tual(X) (((
66d80 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 X)->tabFlags & T
66d90 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 29 0a 23 F_Virtual)!=0).#
66da0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65 define IsHidde
66db0 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d nColumn(X) ((X)-
66dc0 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 6c 73 65 >isHidden).#else
66dd0 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72 .# define IsVir
66de0 74 75 61 6c 28 58 29 20 20 20 20 20 20 30 0a 23 tual(X) 0.#
66df0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65 define IsHidde
66e00 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e nColumn(X) 0.#en
66e10 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 dif../*.** Each
66e20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
66e30 74 72 61 69 6e 74 20 69 73 20 61 6e 20 69 6e 73 traint is an ins
66e40 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
66e50 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
66e60 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67 ..**.** A foreig
66e70 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63 69 61 n key is associa
66e80 74 65 64 20 77 69 74 68 20 74 77 6f 20 74 61 62 ted with two tab
66e90 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f 6d 22 les. The "from"
66ea0 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 table is.** the
66eb0 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 table that cont
66ec0 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 45 4e ains the REFEREN
66ed0 43 45 53 20 63 6c 61 75 73 65 20 74 68 61 74 20 CES clause that
66ee0 63 72 65 61 74 65 73 20 74 68 65 20 66 6f 72 65 creates the fore
66ef0 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65 ign.** key. The
66f00 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73 20 74 "to" table is t
66f10 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 he table that is
66f20 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 52 45 named in the RE
66f30 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 2e FERENCES clause.
66f40 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 69 .** Consider thi
66f50 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a s example:.**.**
66f60 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c CREATE TABL
66f70 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 20 20 20 E ex1(.**
66f80 61 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 a INTEGER PRIMAR
66f90 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20 Y KEY,.**
66fa0 62 20 49 4e 54 45 47 45 52 20 43 4f 4e 53 54 52 b INTEGER CONSTR
66fb0 41 49 4e 54 20 66 6b 31 20 52 45 46 45 52 45 4e AINT fk1 REFEREN
66fc0 43 45 53 20 65 78 32 28 78 29 0a 2a 2a 20 20 20 CES ex2(x).**
66fd0 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 );.**.** For f
66fe0 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 6b 31 22 oreign key "fk1"
66ff0 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 , the from-table
67000 20 69 73 20 22 65 78 31 22 20 61 6e 64 20 74 68 is "ex1" and th
67010 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 22 65 e to-table is "e
67020 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 x2"..**.** Each
67030 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 REFERENCES claus
67040 65 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 69 e generates an i
67050 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
67060 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
67070 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 re.** which is a
67080 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 66 ttached to the f
67090 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 68 65 20 rom-table. The
670a0 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f to-table need no
670b0 74 20 65 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20 t exist when.**
670c0 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 the from-table i
670d0 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 s created. The
670e0 65 78 69 73 74 61 6e 63 65 20 6f 66 20 74 68 65 existance of the
670f0 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74 to-table is not
67100 20 63 68 65 63 6b 65 64 0a 2a 2a 20 75 6e 74 69 checked.** unti
67110 6c 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 l an attempt is
67120 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 64 made to insert d
67130 61 74 61 20 69 6e 74 6f 20 74 68 65 20 66 72 6f ata into the fro
67140 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 m-table..**.** T
67150 68 65 20 73 71 6c 69 74 65 2e 61 46 4b 65 79 20 he sqlite.aFKey
67160 68 61 73 68 20 74 61 62 6c 65 20 73 74 6f 72 65 hash table store
67170 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 s pointers to th
67180 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 is structure.**
67190 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f given the name o
671a0 66 20 61 20 74 6f 2d 74 61 62 6c 65 2e 20 20 46 f a to-table. F
671b0 6f 72 20 65 61 63 68 20 74 6f 2d 74 61 62 6c 65 or each to-table
671c0 2c 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 , all foreign ke
671d0 79 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 ys.** associated
671e0 20 77 69 74 68 20 74 68 61 74 20 74 61 62 6c 65 with that table
671f0 20 61 72 65 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 are on a linked
67200 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 20 list using the
67210 46 4b 65 79 2e 70 4e 65 78 74 54 6f 0a 2a 2a 20 FKey.pNextTo.**
67220 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 field..*/.struct
67230 20 46 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 FKey {. Table
67240 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 *pFrom; /* T
67250 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f he table that co
67260 6e 73 74 61 69 6e 73 20 74 68 65 20 52 45 46 45 nstains the REFE
67270 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 2a 2f RENCES clause */
67280 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 46 72 . FKey *pNextFr
67290 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 om; /* Next for
672a0 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f eign key in pFro
672b0 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f m */. char *zTo
672c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ; /* Name
672d0 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 20 74 of table that t
672e0 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f he key points to
672f0 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 */. FKey *pNex
67300 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 tTo; /* Next
67310 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 68 61 74 foreign key that
67320 20 70 6f 69 6e 74 73 20 74 6f 20 7a 54 6f 20 2a points to zTo *
67330 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 /. int nCol;
67340 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
67350 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
67360 69 73 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 is key */. stru
67370 63 74 20 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a ct sColMap { /*
67380 20 4d 61 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75 Mapping of colu
67390 6d 6e 73 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20 mns in pFrom to
673a0 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a columns in zTo *
673b0 2f 0a 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b /. int iFrom;
673c0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
673d0 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 x of column in p
673e0 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 From */. char
673f0 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f *zCol; /
67400 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e * Name of column
67410 20 69 6e 20 7a 54 6f 2e 20 20 49 66 20 30 20 75 in zTo. If 0 u
67420 73 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a se PRIMARY KEY *
67430 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 20 20 20 20 /. } *aCol;
67440 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 /* One ent
67450 72 79 20 66 6f 72 20 65 61 63 68 20 6f 66 20 6e ry for each of n
67460 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a Col column s */.
67470 20 20 75 38 20 69 73 44 65 66 65 72 72 65 64 3b u8 isDeferred;
67480 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 /* True if c
67490 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 69 onstraint checki
674a0 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 20 74 ng is deferred t
674b0 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 ill COMMIT */.
674c0 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b 20 20 u8 updateConf;
674d0 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f /* How to reso
674e0 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 lve conflicts th
674f0 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 44 41 at occur on UPDA
67500 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74 TE */. u8 delet
67510 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77 eConf; /* How
67520 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66 to resolve conf
67530 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 75 72 licts that occur
67540 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 on DELETE */.
67550 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20 u8 insertConf;
67560 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f /* How to reso
67570 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 lve conflicts th
67580 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45 at occur on INSE
67590 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 RT */.};../*.**
675a0 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20 SQLite supports
675b0 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 20 77 many different w
675c0 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 ays to resolve a
675d0 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 constraint.** e
675e0 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b 20 rror. ROLLBACK
675f0 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 processing means
67600 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 61 69 that a constrai
67610 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20 nt violation.**
67620 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 72 61 causes the opera
67630 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 20 tion in process
67640 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 20 to fail and for
67650 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e the current tran
67660 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 saction.** to be
67670 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 rolled back. A
67680 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 20 BORT processing
67690 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74 means the operat
676a0 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a 2a ion in process.*
676b0 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 20 * fails and any
676c0 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 66 72 prior changes fr
676d0 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 72 om that one oper
676e0 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 64 ation are backed
676f0 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 65 out,.** but the
67700 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
67710 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e not rolled back.
67720 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e FAIL processin
67730 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 g means that.**
67740 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e the operation in
67750 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20 progress stops
67760 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 and returns an e
67770 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20 rror code. But
67780 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 73 prior.** changes
67790 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65 due to the same
677a0 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e operation are n
677b0 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e ot backed out an
677c0 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a d no rollback.**
677d0 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 45 occurs. IGNORE
677e0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
677f0 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20 74 particular row t
67800 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63 hat caused the c
67810 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 onstraint.** err
67820 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 74 or is not insert
67830 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 ed or updated.
67840 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 Processing conti
67850 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f nues and no erro
67860 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 r.** is returned
67870 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 6e 73 . REPLACE means
67880 20 74 68 61 74 20 70 72 65 65 78 69 73 74 69 6e that preexistin
67890 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 g database rows
678a0 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 61 that caused.** a
678b0 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 UNIQUE constrai
678c0 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 65 nt violation are
678d0 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 74 removed so that
678e0 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 74 20 the new insert
678f0 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 6e or.** update can
67900 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 65 proceed. Proce
67910 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 ssing continues
67920 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 and no error is
67930 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 reported..**.**
67940 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e 55 4c RESTRICT, SETNUL
67950 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 20 61 L, and CASCADE a
67960 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e 6c ctions apply onl
67970 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79 y to foreign key
67980 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 69 s..** RESTRICT i
67990 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42 s the same as AB
679a0 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 ORT for IMMEDIAT
679b0 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 E foreign keys a
679c0 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 nd the.** same a
679d0 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 44 s ROLLBACK for D
679e0 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 20 53 EFERRED keys. S
679f0 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 ETNULL means tha
67a00 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a t the foreign.**
67a10 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f 20 4e key is set to N
67a20 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 6d 65 ULL. CASCADE me
67a30 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c 45 54 ans that a DELET
67a40 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 20 74 E or UPDATE of t
67a50 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 he.** referenced
67a60 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 70 72 table row is pr
67a70 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 74 68 opagated into th
67a80 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 73 e row that holds
67a90 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e 20 the.** foreign
67aa0 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 key..** .** The
67ab0 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c following symbol
67ac0 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 75 73 ic values are us
67ad0 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 ed to record whi
67ae0 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 63 ch type.** of ac
67af0 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f tion to take..*/
67b00 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65 .#define OE_None
67b10 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65 72 0 /* Ther
67b20 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 69 e is no constrai
67b30 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 23 nt to check */.#
67b40 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61 define OE_Rollba
67b50 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 74 ck 1 /* Fail t
67b60 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 he operation and
67b70 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 rollback the tr
67b80 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 ansaction */.#de
67b90 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 20 fine OE_Abort
67ba0 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 74 2 /* Back out
67bb0 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 6f 20 changes but do
67bc0 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e no rollback tran
67bd0 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 saction */.#defi
67be0 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 33 ne OE_Fail 3
67bf0 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 6f /* Stop the o
67c00 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 61 peration but lea
67c10 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 61 ve all prior cha
67c20 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nges */.#define
67c30 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 20 OE_Ignore 4
67c40 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 65 72 /* Ignore the er
67c50 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 ror. Do not do t
67c60 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 he INSERT or UPD
67c70 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f ATE */.#define O
67c80 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 20 2f E_Replace 5 /
67c90 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 69 6e * Delete existin
67ca0 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 64 g record, then d
67cb0 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 o INSERT or UPDA
67cc0 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f TE */..#define O
67cd0 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 20 2f E_Restrict 6 /
67ce0 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 49 * OE_Abort for I
67cf0 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f 6c MMEDIATE, OE_Rol
67d00 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 52 52 lback for DEFERR
67d10 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 ED */.#define OE
67d20 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f 2a _SetNull 7 /*
67d30 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e Set the foreign
67d40 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 4e 55 key value to NU
67d50 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 LL */.#define OE
67d60 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 2f 2a _SetDflt 8 /*
67d70 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e Set the foreign
67d80 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 69 74 key value to it
67d90 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 65 s default */.#de
67da0 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 20 fine OE_Cascade
67db0 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 65 20 9 /* Cascade
67dc0 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 0a the changes */..
67dd0 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 75 #define OE_Defau
67de0 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 68 lt 99 /* Do wh
67df0 61 74 65 76 65 72 20 74 68 65 20 64 65 66 61 75 atever the defau
67e00 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0a lt action is */.
67e10 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
67e20 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
67e30 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 wing structure i
67e40 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 s passed as the
67e50 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e first.** argumen
67e60 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 t to sqlite3Vdbe
67e70 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 69 KeyCompare and i
67e80 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f s used to contro
67e90 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 72 l the .** compar
67ea0 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20 ison of the two
67eb0 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a 73 index keys..*/.s
67ec0 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a truct KeyInfo {.
67ed0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
67ee0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
67ef0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
67f00 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 */. u8 enc;
67f10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 /* Tex
67f20 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65 t encoding - one
67f30 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74 66 of the TEXT_Utf
67f40 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 31 * values */. u1
67f50 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 6 nField;
67f60 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
67f70 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c 5b ntries in aColl[
67f80 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 ] */. u8 *aSort
67f90 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 66 Order; /* If
67fa0 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53 6f 72 defined an aSor
67fb0 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 72 75 tOrder[i] is tru
67fc0 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a 2f 0a e, sort DESC */.
67fd0 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c CollSeq *aColl
67fe0 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 [1]; /* Collati
67ff0 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 ng sequence for
68000 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 each term of the
68010 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a key */.};../*.*
68020 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
68030 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
68040 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 tructure holds i
68050 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
68060 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64 a.** single ind
68070 65 78 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 ex record that h
68080 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
68090 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20 parsed out into
680a0 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61 individual.** va
680b0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 lues..**.** A re
680c0 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 cord is an objec
680d0 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 t that contains
680e0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c one or more fiel
680f0 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52 ds of data..** R
68100 65 63 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 ecords are used
68110 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e to store the con
68120 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 tent of a table
68130 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 row and to store
68140 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61 .** the key of a
68150 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62 n index. A blob
68160 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 encoding of a r
68170 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 65 64 ecord is created
68180 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61 by.** the OP_Ma
68190 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 keRecord opcode
681a0 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e 64 20 of the VDBE and
681b0 69 73 20 64 69 73 61 73 73 65 6d 62 6c 69 65 64 is disassemblied
681c0 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f by the.** OP_Co
681d0 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a lumn opcode..**.
681e0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
681f0 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 e holds a record
68200 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 that has alread
68210 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d 62 y been disassemb
68220 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 led.** into its
68230 63 6f 6e 73 74 69 74 75 74 65 6e 74 20 66 69 65 constitutent fie
68240 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 lds..*/.struct U
68250 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a npackedRecord {.
68260 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
68270 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 nfo; /* Collati
68280 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65 on and sort-orde
68290 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f r information */
682a0 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 . u16 nField;
682b0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
682c0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
682d0 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 pMem[] */. u16
682e0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
682f0 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69 /* Boolean setti
68300 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e ngs. UNPACKED_.
68310 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 4d 65 .. below */. Me
68320 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *aMem;
68330 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d /* Values */.}
68340 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 ;../*.** Allowed
68350 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 values of Unpac
68360 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a kedRecord.flags.
68370 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 */.#define UNPAC
68380 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 KED_NEED_FREE
68390 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 0x0001 /* Mem
683a0 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 ory is from sqli
683b0 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 te3Malloc() */.#
683c0 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f define UNPACKED_
683d0 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 78 NEED_DESTROY 0x
683e0 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 0002 /* apMem[]
683f0 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 s should all be
68400 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 destroyed */.#de
68410 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 fine UNPACKED_IG
68420 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 30 NORE_ROWID 0x00
68430 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 04 /* Ignore tr
68440 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 ailing rowid on
68450 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 key1 */.#define
68460 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
68470 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 0x0008 /
68480 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20 * Make this key
68490 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 an epsilon large
684a0 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 r */.#define UNP
684b0 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 ACKED_PREFIX_MAT
684c0 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 CH 0x0010 /* A
684d0 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 prefix match is
684e0 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a considered OK *
684f0 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 /../*.** Each SQ
68500 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 65 L index is repre
68510 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 sented in memory
68520 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e by an.** instan
68530 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
68540 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
68550 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 *.** The columns
68560 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 of the table th
68570 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64 at are to be ind
68580 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62 exed are describ
68590 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43 ed.** by the aiC
685a0 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 olumn[] field of
685b0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e this structure.
685c0 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 For example, s
685d0 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76 uppose.** we hav
685e0 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 e the following
685f0 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a table and index:
68600 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 .**.** CREAT
68610 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 69 E TABLE Ex1(c1 i
68620 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74 nt, c2 int, c3 t
68630 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45 ext);.** CRE
68640 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e ATE INDEX Ex2 ON
68650 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a Ex1(c3,c1);.**.
68660 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20 ** In the Table
68670 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 structure descri
68680 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d bing Ex1, nCol==
68690 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20 3 because there
686a0 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c are.** three col
686b0 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c umns in the tabl
686c0 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78 e. In the Index
686d0 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 structure descr
686e0 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 ibing.** Ex2, nC
686f0 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32 olumn==2 since 2
68700 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e of the 3 column
68710 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64 s of Ex1 are ind
68720 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c exed..** The val
68730 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69 ue of aiColumn i
68740 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c s {2, 0}. aiCol
68750 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73 umn[0]==2 becaus
68760 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 e the .** first
68770 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 column to be ind
68780 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 6e exed (c3) has an
68790 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45 index of 2 in E
687a0 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 x1.aCol[]..** Th
687b0 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 e second column
687c0 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 to be indexed (c
687d0 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 1) has an index
687e0 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 of 0 in.** Ex1.a
687f0 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32 Col[], hence Ex2
68800 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e .aiColumn[1]==0.
68810 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78 .**.** The Index
68820 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64 .onError field d
68830 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 etermines whethe
68840 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 r or not the ind
68850 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 exed columns.**
68860 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61 must be unique a
68870 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66 nd what to do if
68880 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 they are not.
68890 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72 When Index.onErr
688a0 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 or=OE_None,.** i
688b0 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 t means this is
688c0 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64 not a unique ind
688d0 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 ex. Otherwise i
688e0 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e t is a unique in
688f0 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 dex.** and the v
68900 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e alue of Index.on
68910 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74 Error indicate t
68920 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63 he which conflic
68930 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a t resolution .**
68940 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d algorithm to em
68950 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e ploy whenever an
68960 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
68970 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e to insert a non
68980 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 -unique.** eleme
68990 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e nt..*/.struct In
689a0 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e dex {. char *zN
689b0 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ame; /* Name
689c0 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a of this index *
689d0 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b /. int nColumn;
689e0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
689f0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
68a00 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 table used by t
68a10 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 his index */. i
68a20 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 nt *aiColumn;
68a30 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73 /* Which columns
68a40 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 69 are used by thi
68a50 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73 s index. 1st is
68a60 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 0 */. unsigned
68a70 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52 *aiRowEst; /* R
68a80 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45 esult of ANALYZE
68a90 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65 : Est. rows sele
68aa0 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c cted by each col
68ab0 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a umn */. Table *
68ac0 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65 pTable; /* The
68ad0 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67 SQL table being
68ae0 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e indexed */. in
68af0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f t tnum; /
68b00 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e * Page containin
68b10 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69 g root of this i
68b20 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 ndex in database
68b30 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e file */. u8 on
68b40 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f Error; /* O
68b50 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f E_Abort, OE_Igno
68b60 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 re, OE_Replace,
68b70 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 or OE_None */.
68b80 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20 u8 autoIndex;
68b90 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 /* True if is a
68ba0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 utomatically cre
68bb0 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49 ated (ex: by UNI
68bc0 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a QUE) */. char *
68bd0 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74 zColAff; /* St
68be0 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 ring defining th
68bf0 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 e affinity of ea
68c00 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 ch column */. I
68c10 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20 ndex *pNext;
68c20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65 /* The next inde
68c30 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 x associated wit
68c40 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 h the same table
68c50 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 */. Schema *pS
68c60 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61 chema; /* Schema
68c70 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 containing this
68c80 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a index */. u8 *
68c90 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20 aSortOrder; /*
68ca0 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e Array of size In
68cb0 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 dex.nColumn. Tru
68cc0 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d e==DESC, False==
68cd0 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a ASC */. char **
68ce0 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 azColl; /* Arr
68cf0 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 ay of collation
68d00 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66 sequence names f
68d10 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a or index */.};..
68d20 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e /*.** Each token
68d30 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 coming out of t
68d40 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69 he lexer is an i
68d50 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 nstance of.** th
68d60 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 is structure. T
68d70 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75 okens are also u
68d80 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 sed as part of a
68d90 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a n expression..**
68da0 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65 .** Note if Toke
68db0 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65 n.z==0 then Toke
68dc0 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e n.dyn and Token.
68dd0 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 n are undefined
68de0 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 and.** may conta
68df0 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73 in random values
68e00 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61 . Do not make a
68e10 6e 79 20 61 73 73 75 70 74 69 6f 6e 73 20 61 62 ny assuptions ab
68e20 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a out Token.dyn.**
68e30 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 and Token.n whe
68e40 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f n Token.z==0..*/
68e50 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a .struct Token {.
68e60 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
68e70 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 78 char *z; /* Tex
68e80 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 t of the token.
68e90 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e Not NULL-termin
68ea0 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 ated! */. unsig
68eb0 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 20 20 ned dyn : 1;
68ec0 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6d /* True for m
68ed0 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c 20 alloced memory,
68ee0 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 63 false for static
68ef0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6e */. unsigned n
68f00 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 2f 2a : 31; /*
68f10 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 Number of chara
68f20 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f cters in this to
68f30 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ken */.};../*.**
68f40 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
68f50 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 this structure c
68f60 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 ontains informat
68f70 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 ion needed to ge
68f80 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 nerate.** code f
68f90 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 74 or a SELECT that
68fa0 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 contains aggreg
68fb0 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a ate functions..*
68fc0 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d *.** If Expr.op=
68fd0 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f =TK_AGG_COLUMN o
68fe0 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f r TK_AGG_FUNCTIO
68ff0 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67 N then Expr.pAgg
69000 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 Info is a.** poi
69010 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 nter to this str
69020 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 70 ucture. The Exp
69030 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 r.iColumn field
69040 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a is the index in.
69050 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b ** AggInfo.aCol[
69060 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 ] or AggInfo.aFu
69070 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 nc[] of informat
69080 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 ion needed to ge
69090 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 nerate.** code f
690a0 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a or that node..**
690b0 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f .** AggInfo.pGro
690c0 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f upBy and AggInfo
690d0 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 .aFunc.pExpr poi
690e0 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74 nt to fields wit
690f0 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 hin the.** origi
69100 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63 nal Select struc
69110 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 ture that descri
69120 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 73 bes the SELECT s
69130 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 tatement. These
69140 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f .** fields do no
69150 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 t need to be fre
69160 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 ed when dealloca
69170 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f ting the AggInfo
69180 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
69190 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a truct AggInfo {.
691a0 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b u8 directMode;
691b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 /* Dir
691c0 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f ect rendering mo
691d0 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 de means take da
691e0 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20 ta directly.
691f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
69200 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f ** from so
69210 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68 urce tables rath
69220 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 er than from acc
69230 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 umulators */. u
69240 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 8 useSortingIdx;
69250 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 /* In dir
69260 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 ect mode, refere
69270 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20 nce the sorting
69280 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 20 index rather.
69290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
692a0 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 ** than t
692b0 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 he source table
692c0 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 */. int sorting
692d0 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Idx; /*
692e0 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 Cursor number of
692f0 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 the sorting ind
69300 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 ex */. ExprList
69310 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 *pGroupBy;
69320 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 /* The group by
69330 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 clause */. int
69340 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 nSortingColumn;
69350 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
69360 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
69370 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f sorting index */
69380 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 . struct AggInf
69390 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f o_col { /* Fo
693a0 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 r each column us
693b0 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62 ed in source tab
693c0 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 les */. Table
693d0 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 *pTab;
693e0 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 /* Source ta
693f0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ble */. int i
69400 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 Table;
69410 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 /* Cursor nu
69420 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72 mber of the sour
69430 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 ce table */.
69440 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 int iColumn;
69450 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 /* Colu
69460 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e mn number within
69470 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c the source tabl
69480 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f e */. int iSo
69490 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 rterColumn;
694a0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 /* Column numb
694b0 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e er in the sortin
694c0 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 g index */. i
694d0 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 nt iMem;
694e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
694f0 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 y location that
69500 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61 acts as accumula
69510 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 tor */. Expr
69520 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 *pExpr;
69530 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 /* The origi
69540 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a nal expression *
69550 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 /. } *aCol;. i
69560 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 nt nColumn;
69570 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
69580 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 73 of used entries
69590 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 in aCol[] */.
695a0 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 int nColumnAlloc
695b0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
695c0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
695d0 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 ated for aCol[]
695e0 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 */. int nAccumu
695f0 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 lator; /*
69600 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e Number of column
69610 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f s that show thro
69620 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75 ugh to the outpu
69630 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t..
69640 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
69650 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d Additional colum
69660 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 ns are used only
69670 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74 as parameters t
69680 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o.
69690 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 ** a
696a0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
696b0 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 ns */. struct A
696c0 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 ggInfo_func {
696d0 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 72 /* For each aggr
696e0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a egate function *
696f0 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 /. Expr *pExp
69700 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f r; /
69710 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 * Expression enc
69720 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69 oding the functi
69730 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 on */. FuncDe
69740 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 f *pFunc;
69750 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67 /* The aggreg
69760 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 ate function imp
69770 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 lementation */.
69780 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 int iMem;
69790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
697a0 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 emory location t
697b0 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75 hat acts as accu
697c0 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 mulator */. i
697d0 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20 nt iDistinct;
697e0 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 72 /* Epher
697f0 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64 meral table used
69800 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54 to enforce DIST
69810 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 INCT */. } *aFu
69820 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b nc;. int nFunc;
69830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
69840 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
69850 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f es in aFunc[] */
69860 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f . int nFuncAllo
69870 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 c; /* Nu
69880 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c mber of slots al
69890 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e located for aFun
698a0 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a c[] */.};../*.**
698b0 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e Each node of an
698c0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 expression in t
698d0 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 73 he parse tree is
698e0 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 an instance.**
698f0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
69900 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 e..**.** Expr.op
69910 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 is the opcode.
69920 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 The integer par
69930 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20 ser token codes
69940 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73 are reused.** as
69950 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 20 opcodes here.
69960 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 For example, the
69970 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 parser defines
69980 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 TK_GE to be an i
69990 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 nteger.** code r
699a0 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
699b0 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 20 ">=" operator.
699c0 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 This same intege
699d0 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 r code is reused
699e0 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 .** to represent
699f0 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 the greater-tha
69a00 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 n-or-equal-to op
69a10 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 erator in the ex
69a20 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 pression.** tree
69a30 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69 ..**.** Expr.pRi
69a40 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65 ght and Expr.pLe
69a50 66 74 20 61 72 65 20 73 75 62 65 78 70 72 65 73 ft are subexpres
69a60 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e 70 4c 69 sions. Expr.pLi
69a70 73 74 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20 st is a list.**
69a80 6f 66 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 of argument if t
69a90 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
69aa0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a a function..**.
69ab0 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 20 69 73 ** Expr.token is
69ac0 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f the operator to
69ad0 6b 65 6e 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 ken for this nod
69ae0 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20 65 78 70 e. For some exp
69af0 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 ressions.** that
69b00 20 68 61 76 65 20 73 75 62 65 78 70 72 65 73 73 have subexpress
69b10 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f 6b 65 6e ions, Expr.token
69b20 20 63 61 6e 20 62 65 20 74 68 65 20 63 6f 6d 70 can be the comp
69b30 6c 65 74 65 20 74 65 78 74 20 74 68 61 74 20 67 lete text that g
69b40 61 76 65 0a 2a 2a 20 72 69 73 65 20 74 6f 20 74 ave.** rise to t
69b50 68 65 20 45 78 70 72 2e 20 20 49 6e 20 74 68 65 he Expr. In the
69b60 20 6c 61 74 74 65 72 20 63 61 73 65 2c 20 74 68 latter case, th
69b70 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61 72 6b 65 e token is marke
69b80 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a 20 61 20 d as being.** a
69b90 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65 6e 2e 0a compound token..
69ba0 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73 **.** An express
69bb0 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ion of the form
69bc0 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65 ID or ID.ID refe
69bd0 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 rs to a column i
69be0 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f n a table..** Fo
69bf0 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f r such expressio
69c00 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73 ns, Expr.op is s
69c10 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 et to TK_COLUMN
69c20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20 and Expr.iTable
69c30 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 is.** the intege
69c40 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 r cursor number
69c50 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72 of a VDBE cursor
69c60 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61 pointing to tha
69c70 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45 t table and.** E
69c80 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74 xpr.iColumn is t
69c90 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 he column number
69ca0 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
69cb0 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 c column. If th
69cc0 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 e.** expression
69cd0 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73 is used as a res
69ce0 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67 ult in an aggreg
69cf0 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e ate SELECT, then
69d00 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73 the.** value is
69d10 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 also stored in
69d20 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f the Expr.iAgg co
69d30 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72 lumn in the aggr
69d40 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a egate so that.**
69d50 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73 it can be acces
69d60 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67 sed after all ag
69d70 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d gregates are com
69d80 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 puted..**.** If
69d90 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
69da0 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 s a function, th
69db0 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 e Expr.iTable is
69dc0 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 an integer code
69dd0 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e 67 .** representing
69de0 20 77 68 69 63 68 20 66 75 6e 63 74 69 6f 6e 2e which function.
69df0 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 If the express
69e00 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e ion is an unboun
69e10 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 d variable.** ma
69e20 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e rker (a question
69e30 20 6d 61 72 6b 20 63 68 61 72 61 63 74 65 72 20 mark character
69e40 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 '?' in the origi
69e50 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 nal SQL) then th
69e60 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61 62 6c 65 e.** Expr.iTable
69e70 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 holds the index
69e80 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 number for that
69e90 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a variable..**.**
69ea0 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
69eb0 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79 on is a subquery
69ec0 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 then Expr.iColu
69ed0 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 mn holds an inte
69ee0 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 ger.** register
69ef0 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e number containin
69f00 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 g the result of
69f10 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 the subquery. I
69f20 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 f the.** subquer
69f30 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 y gives a consta
69f40 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 nt result, then
69f50 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 iTable is -1. I
69f60 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a f the subquery.*
69f70 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72 * gives a differ
69f80 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 ent answer at di
69f90 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 fferent times du
69fa0 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 ring statement p
69fb0 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 rocessing.** the
69fc0 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 n iTable is the
69fd0 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 address of a sub
69fe0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d routine that com
69ff0 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65 putes the subque
6a000 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78 ry..**.** The Ex
6a010 70 72 2e 70 53 65 6c 65 63 74 20 66 69 65 6c 64 pr.pSelect field
6a020 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 53 45 4c points to a SEL
6a030 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ECT statement.
6a040 54 68 65 20 53 45 4c 45 43 54 20 6d 69 67 68 74 The SELECT might
6a050 0a 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74 .** be the right
6a060 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 49 operand of an I
6a070 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20 4f 72 2c N operator. Or,
6a080 20 69 66 20 61 20 73 63 61 6c 61 72 20 53 45 4c if a scalar SEL
6a090 45 43 54 20 61 70 70 65 61 72 73 0a 2a 2a 20 69 ECT appears.** i
6a0a0 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 n an expression
6a0b0 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b the opcode is TK
6a0c0 5f 53 45 4c 45 43 54 20 61 6e 64 20 45 78 70 72 _SELECT and Expr
6a0d0 2e 70 53 65 6c 65 63 74 20 69 73 20 74 68 65 20 .pSelect is the
6a0e0 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e only.** operand.
6a0f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 .**.** If the Ex
6a100 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50 pr is of type OP
6a110 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65 _Column, and the
6a120 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c table it is sel
6a130 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69 ecting from.** i
6a140 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f s a disk table o
6a150 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73 r the "old.*" ps
6a160 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e eudo-table, then
6a170 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20 pTab points to
6a180 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e the.** correspon
6a190 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e ding table defin
6a1a0 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ition..*/.struct
6a1b0 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b Expr {. u8 op;
6a1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6a1d0 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65 /* Operation pe
6a1e0 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20 rformed by this
6a1f0 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 node */. char a
6a200 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 ffinity;
6a210 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 /* The affinity
6a220 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f of the column o
6a230 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c r 0 if not a col
6a240 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 umn */. u16 fla
6a250 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
6a260 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73 /* Various flags
6a270 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a . See below */.
6a280 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
6a290 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
6a2a0 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f collation type o
6a2b0 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 f the column or
6a2c0 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 0 */. Expr *pLe
6a2d0 66 74 2c 20 2a 70 52 69 67 68 74 3b 20 20 2f 2a ft, *pRight; /*
6a2e0 20 4c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 Left and right
6a2f0 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20 20 45 78 subnodes */. Ex
6a300 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 prList *pList;
6a310 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f /* A list o
6a320 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73 f expressions us
6a330 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 20 61 ed as function a
6a340 72 67 75 6d 65 6e 74 73 0a 20 20 20 20 20 20 20 rguments.
6a350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6a360 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c 65 78 70 ** or in "<exp
6a370 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73 r> IN (<expr-lis
6a380 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 t)" */. Token t
6a390 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 oken;
6a3a0 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64 20 74 6f /* An operand to
6a3b0 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 ken */. Token s
6a3c0 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 pan;
6a3d0 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 /* Complete text
6a3e0 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
6a3f0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 on */. int iTab
6a400 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f le, iColumn; /
6a410 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f * When op==TK_CO
6a420 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 20 LUMN, then this
6a430 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20 expr node means
6a440 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 the.
6a450 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
6a460 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64 iColumn-th field
6a470 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d 74 of the iTable-t
6a480 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67 h table. */. Ag
6a490 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b gInfo *pAggInfo;
6a4a0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 /* Used by
6a4b0 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e TK_AGG_COLUMN an
6a4c0 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f d TK_AGG_FUNCTIO
6a4d0 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b N */. int iAgg;
6a4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6a4f0 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20 Which entry in
6a500 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d pAggInfo->aCol[]
6a510 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f or ->aFunc[] */
6a520 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69 . int iRightJoi
6a530 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 nTable; /* If
6a540 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 EP_FromJoin, the
6a550 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 right table of
6a560 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 53 65 the join */. Se
6a570 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 lect *pSelect;
6a580 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 /* When the
6a590 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
6a5a0 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 20 41 6c sub-select. Al
6a5b0 73 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 so the.
6a5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6a5d0 2a 2a 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 ** right side of
6a5e0 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65 "<expr> IN (<se
6a5f0 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 54 61 62 lect>)" */. Tab
6a600 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 le *pTab;
6a610 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 /* Table for
6a620 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 TK_COLUMN expre
6a630 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 ssions. */.#if S
6a640 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
6a650 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 EPTH>0. int nHe
6a660 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 ight;
6a670 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 /* Height of the
6a680 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20 tree headed by
6a690 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e this node */.#en
6a6a0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 dif.};../*.** Th
6a6b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
6a6c0 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 the meanings of
6a6d0 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 bits in the Expr
6a6e0 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f .flags field..*/
6a6f0 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d .#define EP_From
6a700 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f Join 0x0001 /
6a710 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 * Originated in
6a720 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 ON or USING clau
6a730 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a se of a join */.
6a740 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20 #define EP_Agg
6a750 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 0x0002 /*
6a760 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 Contains one or
6a770 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 more aggregate
6a780 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 functions */.#de
6a790 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 fine EP_Resolved
6a7a0 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44 0x0004 /* ID
6a7b0 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f s have been reso
6a7c0 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 lved to COLUMNs
6a7d0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 */.#define EP_Er
6a7e0 72 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20 ror 0x0008
6a7f0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 /* Expression c
6a800 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d ontains one or m
6a810 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 ore errors */.#d
6a820 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 efine EP_Distinc
6a830 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 t 0x0010 /* A
6a840 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
6a850 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 n with DISTINCT
6a860 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 keyword */.#defi
6a870 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20 ne EP_VarSelect
6a880 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c 0x0020 /* pSel
6a890 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65 ect is correlate
6a8a0 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 d, not constant
6a8b0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 65 */.#define EP_De
6a8c0 71 75 6f 74 65 64 20 20 20 30 78 30 30 34 30 20 quoted 0x0040
6a8d0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
6a8e0 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20 string has been
6a8f0 64 65 71 75 6f 74 65 64 20 2a 2f 0a 23 64 65 66 dequoted */.#def
6a900 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 ine EP_InfixFunc
6a910 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75 0x0080 /* Tru
6a920 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 e for an infix f
6a930 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 unction: LIKE, G
6a940 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66 LOB, etc */.#def
6a950 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 ine EP_ExpCollat
6a960 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c e 0x0100 /* Col
6a970 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
6a980 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 specified explic
6a990 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 itly */.#define
6a9a0 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78 EP_AnyAff 0x
6a9b0 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b 0200 /* Can tak
6a9c0 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d e a cached colum
6a9d0 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74 n of any affinit
6a9e0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f y */.#define EP_
6a9f0 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 30 FixedDest 0x040
6aa00 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65 0 /* Result nee
6aa10 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 69 ded in a specifi
6aa20 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 64 c register */.#d
6aa30 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75 efine EP_IntValu
6aa40 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20 49 e 0x0800 /* I
6aa50 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f 6e nteger value con
6aa60 74 61 69 6e 65 64 20 69 6e 20 69 54 61 62 6c 65 tained in iTable
6aa70 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 */./*.** These
6aa80 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 macros can be us
6aa90 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c ed to test, set,
6aaa0 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 or clear bits i
6aab0 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66 n the .** Expr.f
6aac0 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 lags field..*/.#
6aad0 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72 define ExprHasPr
6aae0 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 operty(E,P)
6aaf0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 (((E)->flags&(P)
6ab00 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 )==(P)).#define
6ab10 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
6ab20 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e ty(E,P) (((E)->
6ab30 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 flags&(P))!=0).#
6ab40 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72 define ExprSetPr
6ab50 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 operty(E,P)
6ab60 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a (E)->flags|=(P).
6ab70 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61 #define ExprClea
6ab80 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 rProperty(E,P)
6ab90 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 (E)->flags&=~(P
6aba0 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 )../*.** A list
6abb0 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 of expressions.
6abc0 20 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e Each expression
6abd0 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 may optionally
6abe0 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 have a.** name.
6abf0 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f An expr/name co
6ac00 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 mbination can be
6ac10 20 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c used in several
6ac20 20 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 ways, such.** a
6ac30 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 s the list of "e
6ac40 78 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 xpr AS ID" field
6ac50 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 s following a "S
6ac60 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 ELECT" or in the
6ac70 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 .** list of "ID
6ac80 3d 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e = expr" items in
6ac90 20 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c an UPDATE. A l
6aca0 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f ist of expressio
6acb0 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 ns can.** also b
6acc0 65 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72 e used as the ar
6acd0 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 gument to a func
6ace0 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 tion, in which c
6acf0 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a ase the a.zName.
6ad00 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 ** field is not
6ad10 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 used..*/.struct
6ad20 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 ExprList {. int
6ad30 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 nExpr;
6ad40 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6ad50 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 expressions on
6ad60 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e the list */. in
6ad70 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 t nAlloc;
6ad80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6ad90 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 f entries alloca
6ada0 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 ted below */. i
6adb0 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 nt iECursor;
6adc0 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 /* VDBE Cu
6add0 72 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 rsor associated
6ade0 77 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 with this ExprLi
6adf0 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 st */. struct E
6ae00 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 xprList_item {.
6ae10 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 Expr *pExpr;
6ae20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6ae30 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
6ae40 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 ions */. char
6ae50 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 *zName;
6ae60 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f /* Token asso
6ae70 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
6ae80 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
6ae90 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b u8 sortOrder;
6aea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 /* 1 f
6aeb0 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 or DESC or 0 for
6aec0 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 ASC */. u8 d
6aed0 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 one;
6aee0 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 /* A flag to
6aef0 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 indicate when pr
6af00 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 ocessing is fini
6af10 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 shed */. u16
6af20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 iCol;
6af30 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 /* For ORDER
6af40 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 BY, column numbe
6af50 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 r in result set
6af60 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 */. u16 iAlia
6af70 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s; /*
6af80 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 Index into Pars
6af90 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a e.aAlias[] for z
6afa0 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 Name */. } *a;
6afb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6afc0 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f /* One entry fo
6afd0 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f r each expressio
6afe0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 n */.};../*.** A
6aff0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
6b000 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e is structure can
6b010 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c hold a simple l
6b020 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 ist of identifie
6b030 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 rs,.** such as t
6b040 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 he list "a,b,c"
6b050 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 in the following
6b060 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a statements:.**.
6b070 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 ** INSERT I
6b080 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c NTO t(a,b,c) VAL
6b090 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 UES ...;.**
6b0a0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 CREATE INDEX id
6b0b0 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a x ON t(a,b,c);.*
6b0c0 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 * CREATE TR
6b0d0 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 IGGER trig BEFOR
6b0e0 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c E UPDATE ON t(a,
6b0f0 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 b,c) ...;.**.**
6b100 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 The IdList.a.idx
6b110 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 field is used w
6b120 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 hen the IdList r
6b130 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 epresents the li
6b140 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 st of.** column
6b150 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 names after a ta
6b160 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 ble name in an I
6b170 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e NSERT statement.
6b180 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 In the stateme
6b190 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 nt.**.** INS
6b1a0 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 ERT INTO t(a,b,c
6b1b0 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 ) ....**.** If "
6b1c0 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 a" is the k-th c
6b1d0 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 olumn of table "
6b1e0 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e t", then IdList.
6b1f0 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a a[0].idx==k..*/.
6b200 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a struct IdList {.
6b210 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f struct IdList_
6b220 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 item {. char
6b230 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 *zName; /*
6b240 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e Name of the iden
6b250 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e tifier */. in
6b260 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 t idx;
6b270 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 /* Index in some
6b280 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 Table.aCol[] of
6b290 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 a column named
6b2a0 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b zName */. } *a;
6b2b0 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 . int nId;
6b2c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6b2d0 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 identifiers on
6b2e0 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e the list */. in
6b2f0 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f t nAlloc; /
6b300 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
6b310 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ies allocated fo
6b320 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d r a[] below */.}
6b330 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 ;../*.** The bit
6b340 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 mask datatype de
6b350 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 fined below is u
6b360 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 sed for various
6b370 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a optimizations..*
6b380 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 *.** Changing th
6b390 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 is from a 64-bit
6b3a0 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 to a 32-bit typ
6b3b0 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d e limits the num
6b3c0 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 ber of.** tables
6b3d0 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 in a join to 32
6b3e0 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 instead of 64.
6b3f0 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 But it also red
6b400 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a uces the size.**
6b410 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 of the library
6b420 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 by 738 bytes on
6b430 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ix86..*/.typedef
6b440 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f u64 Bitmask;../
6b450 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
6b460 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 ng structure des
6b470 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 cribes the FROM
6b480 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 clause of a SELE
6b490 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a CT statement..**
6b4a0 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 Each table or s
6b4b0 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 ubquery in the F
6b4c0 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 ROM clause is a
6b4d0 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 separate element
6b4e0 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 of.** the SrcLi
6b4f0 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a st.a[] array..**
6b500 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 .** With the add
6b510 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c ition of multipl
6b520 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
6b530 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e rt, the followin
6b540 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 g structure.** c
6b550 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 an also be used
6b560 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 to describe a pa
6b570 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 rticular table s
6b580 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 uch as the table
6b590 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 that.** is modi
6b5a0 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 fied by an INSER
6b5b0 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 T, DELETE, or UP
6b5c0 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 DATE statement.
6b5d0 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c In standard SQL
6b5e0 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c ,.** such a tabl
6b5f0 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 e must be a simp
6b600 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 le name: ID. Bu
6b610 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 t in SQLite, the
6b620 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f table can.** no
6b630 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 w be identified
6b640 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 by a database na
6b650 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 me, a dot, then
6b660 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 the table name:
6b670 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ID.ID..**.** The
6b680 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 jointype starts
6b690 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 out showing the
6b6a0 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 join type betwe
6b6b0 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 en the current t
6b6c0 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 able.** and the
6b6d0 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 next table on th
6b6e0 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 e list. The par
6b6f0 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c ser builds the l
6b700 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a ist this way..**
6b710 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c But sqlite3SrcL
6b720 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 istShiftJoinType
6b730 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 () later shifts
6b740 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f the jointypes so
6b750 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f that each.** jo
6b760 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 intype expresses
6b770 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 the join betwee
6b780 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 n the table and
6b790 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 the previous tab
6b7a0 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 le..*/.struct Sr
6b7b0 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 cList {. i16 nS
6b7c0 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 rc; /* Nu
6b7d0 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f mber of tables o
6b7e0 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 r subqueries in
6b7f0 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
6b800 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b */. i16 nAlloc;
6b810 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6b820 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 of entries alloc
6b830 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f ated in a[] belo
6b840 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 w */. struct Sr
6b850 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 cList_item {.
6b860 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 char *zDatabase
6b870 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 ; /* Name of da
6b880 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 tabase holding t
6b890 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 his table */.
6b8a0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
6b8b0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
6b8c0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 e table */. c
6b8d0 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 har *zAlias;
6b8e0 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 /* The "B" part
6b8f0 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 of a "A AS B" p
6b900 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 hrase. zName is
6b910 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 the "A" */.
6b920 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 Table *pTab;
6b930 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c /* An SQL tabl
6b940 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
6b950 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 to zName */.
6b960 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b Select *pSelect;
6b970 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 /* A SELECT st
6b980 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 atement used in
6b990 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 place of a table
6b9a0 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 name */. u8
6b9b0 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f isPopulated; /
6b9c0 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c * Temporary tabl
6b9d0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
6b9e0 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 h SELECT is popu
6b9f0 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 lated */. u8
6ba00 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f jointype; /
6ba10 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 * Type of join b
6ba20 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 etween this able
6ba30 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 and the previou
6ba40 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75 s */. int iCu
6ba50 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 rsor; /* Th
6ba60 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 e VDBE cursor nu
6ba70 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63 63 mber used to acc
6ba80 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a ess this table *
6ba90 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e 3b /. Expr *pOn;
6baa0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f /* The O
6bab0 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f N clause of a jo
6bac0 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 74 in */. IdList
6bad0 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 54 *pUsing; /* T
6bae0 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 he USING clause
6baf0 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 of a join */.
6bb00 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 64 Bitmask colUsed
6bb10 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c 3c ; /* Bit N (1<<
6bb20 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d 6e N) set if column
6bb30 20 4e 20 6f 72 20 70 54 61 62 20 69 73 20 75 73 N or pTab is us
6bb40 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 ed */. u8 not
6bb50 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 Indexed; /* T
6bb60 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 rue if there is
6bb70 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c a NOT INDEXED cl
6bb80 61 75 73 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 ause */. char
6bb90 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a *zIndex; /*
6bba0 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d Identifier from
6bbb0 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 "INDEXED BY <zI
6bbc0 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f ndex>" clause */
6bbd0 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 . Index *pInd
6bbe0 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 ex; /* Index
6bbf0 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 structure corres
6bc00 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 ponding to zInde
6bc10 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d x, if any */. }
6bc20 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 a[1];
6bc30 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 /* One entry
6bc40 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 for each identif
6bc50 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 ier on the list
6bc60 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 */.};../*.** Per
6bc70 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 mitted values of
6bc80 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a the SrcList.a.j
6bc90 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f ointype field.*/
6bca0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 .#define JT_INNE
6bcb0 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 R 0x0001
6bcc0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 /* Any kind of i
6bcd0 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f nner or cross jo
6bce0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
6bcf0 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 _CROSS 0x000
6bd00 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 2 /* Explicit
6bd10 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 use of the CROS
6bd20 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 S keyword */.#de
6bd30 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 fine JT_NATURAL
6bd40 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 0x0004 /* T
6bd50 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 rue for a "natur
6bd60 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 al" join */.#def
6bd70 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 ine JT_LEFT
6bd80 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 0x0008 /* Le
6bd90 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f ft outer join */
6bda0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 .#define JT_RIGH
6bdb0 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 T 0x0010
6bdc0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a /* Right outer j
6bdd0 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a oin */.#define J
6bde0 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 T_OUTER 0x00
6bdf0 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 20 /* The "OU
6be00 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 TER" keyword is
6be10 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 present */.#defi
6be20 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 ne JT_ERROR
6be30 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 0x0040 /* unk
6be40 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 nown or unsuppor
6be50 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f ted join type */
6be60 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 ../*.** For each
6be70 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20 nested loop in
6be80 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 a WHERE clause i
6be90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 mplementation, t
6bea0 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20 he WhereInfo.**
6beb0 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
6bec0 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 ns a single inst
6bed0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
6bee0 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73 74 ucture. This st
6bef0 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e ructure.** is in
6bf00 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69 tended to be pri
6bf10 76 61 74 65 20 74 68 65 20 74 68 65 20 77 68 65 vate the the whe
6bf20 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 re.c module and
6bf30 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a should not be.**
6bf40 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 access or modif
6bf50 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 ied by other mod
6bf60 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ules..**.** The
6bf70 70 49 64 78 49 6e 66 6f 20 61 6e 64 20 70 42 65 pIdxInfo and pBe
6bf80 73 74 49 64 78 20 66 69 65 6c 64 73 20 61 72 65 stIdx fields are
6bf90 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69 used to help pi
6bfa0 63 6b 20 74 68 65 20 62 65 73 74 0a 2a 2a 20 69 ck the best.** i
6bfb0 6e 64 65 78 20 6f 6e 20 61 20 76 69 72 74 75 61 ndex on a virtua
6bfc0 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49 l table. The pI
6bfd0 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63 dxInfo pointer c
6bfe0 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67 ontains indexing
6bff0 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
6c000 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 for the i-th tab
6c010 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 le in the FROM c
6c020 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f lause before reo
6c030 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 rdering..** All
6c040 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 the pIdxInfo poi
6c050 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64 20 nters are freed
6c060 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 by whereInfoFree
6c070 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a () in where.c..*
6c080 2a 20 54 68 65 20 70 42 65 73 74 49 64 78 20 70 * The pBestIdx p
6c090 6f 69 6e 74 65 72 20 69 73 20 61 20 63 6f 70 79 ointer is a copy
6c0a0 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72 of pIdxInfo for
6c0b0 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20 the i-th table
6c0c0 61 66 74 65 72 0a 2a 2a 20 46 52 4f 4d 20 63 6c after.** FROM cl
6c0d0 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 20 20 ause ordering.
6c0e0 54 68 69 73 20 69 73 20 61 20 6c 69 74 74 6c 65 This is a little
6c0f0 20 63 6f 6e 66 75 73 69 6e 67 20 73 6f 20 49 20 confusing so I
6c100 77 69 6c 6c 20 72 65 70 65 61 74 0a 2a 2a 20 69 will repeat.** i
6c110 74 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77 t in different w
6c120 6f 72 64 73 2e 20 20 57 68 65 72 65 49 6e 66 6f ords. WhereInfo
6c130 2e 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 20 69 .a[i].pIdxInfo i
6c140 73 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 s index informat
6c150 69 6f 6e 20 0a 2a 2a 20 66 6f 72 20 57 68 65 72 ion .** for Wher
6c160 65 49 6e 66 6f 2e 70 54 61 62 4c 69 73 74 2e 61 eInfo.pTabList.a
6c170 5b 69 5d 2e 20 20 57 68 65 72 65 49 6e 66 6f 2e [i]. WhereInfo.
6c180 61 5b 69 5d 2e 70 42 65 73 74 49 6e 66 6f 20 69 a[i].pBestInfo i
6c190 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 69 s the.** index i
6c1a0 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 nformation for t
6c1b0 68 65 20 69 2d 74 68 20 6c 6f 6f 70 20 6f 66 20 he i-th loop of
6c1c0 74 68 65 20 6a 6f 69 6e 2e 20 20 70 42 65 73 74 the join. pBest
6c1d0 49 6e 66 6f 20 69 73 20 61 6c 77 61 79 73 0a 2a Info is always.*
6c1e0 2a 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 * either NULL or
6c1f0 20 61 20 63 6f 70 79 20 6f 66 20 73 6f 6d 65 20 a copy of some
6c200 70 49 64 78 49 6e 66 6f 2e 20 20 53 6f 20 66 6f pIdxInfo. So fo
6c210 72 20 63 6c 65 61 6e 75 70 20 69 74 20 69 73 20 r cleanup it is
6c220 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 74 .** sufficient t
6c230 6f 20 66 72 65 65 20 61 6c 6c 20 6f 66 20 74 68 o free all of th
6c240 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 e pIdxInfo point
6c250 65 72 73 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 72 75 ers..** .*/.stru
6c260 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a ct WhereLevel {.
6c270 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 int iFrom;
6c280 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 /* Which
6c290 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52 entry in the FR
6c2a0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 OM clause */. i
6c2b0 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 nt flags;
6c2c0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 /* Flags as
6c2d0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
6c2e0 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e is level */. in
6c2f0 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t iMem;
6c300 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6d 65 6d /* First mem
6c310 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 62 79 ory cell used by
6c320 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 this level */.
6c330 20 69 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 int iLeftJoin;
6c340 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
6c350 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d cell used to im
6c360 70 6c 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 plement LEFT OUT
6c370 45 52 20 4a 4f 49 4e 20 2a 2f 0a 20 20 49 6e 64 ER JOIN */. Ind
6c380 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 ex *pIdx;
6c390 20 20 20 2f 2a 20 49 6e 64 65 78 20 75 73 65 64 /* Index used
6c3a0 2e 20 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 69 6e . NULL if no in
6c3b0 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 dex */. int iTa
6c3c0 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f bCur; /
6c3d0 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f * The VDBE curso
6c3e0 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 r used to access
6c3f0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
6c400 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 int iIdxCur;
6c410 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 /* The VDB
6c420 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f E cursor used to
6c430 20 61 63 65 73 73 73 20 70 49 64 78 20 2a 2f 0a acesss pIdx */.
6c440 20 20 69 6e 74 20 62 72 6b 3b 20 20 20 20 20 20 int brk;
6c450 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
6c460 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 here to break ou
6c470 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f t of the loop */
6c480 0a 20 20 69 6e 74 20 6e 78 74 3b 20 20 20 20 20 . int nxt;
6c490 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 /* Jump
6c4a0 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74 here to start t
6c4b0 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69 he next IN combi
6c4c0 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nation */. int
6c4d0 63 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 cont;
6c4e0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
6c4f0 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 o continue with
6c500 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79 the next loop cy
6c510 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 cle */. int top
6c520 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
6c530 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74 * First instruct
6c540 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 ion of interior
6c550 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 of the loop */.
6c560 20 69 6e 74 20 6f 70 2c 20 70 31 2c 20 70 32 2c int op, p1, p2,
6c570 20 70 35 3b 20 20 20 2f 2a 20 4f 70 63 6f 64 65 p5; /* Opcode
6c580 20 75 73 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 used to termina
6c590 74 65 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 te the loop */.
6c5a0 20 69 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20 int nEq;
6c5b0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6c5c0 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e of == or IN con
6c5d0 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68 69 73 straints on this
6c5e0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e loop */. int n
6c5f0 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 In;
6c600 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 49 4e /* Number of IN
6c610 20 6f 70 65 72 61 74 6f 72 73 20 63 6f 6e 73 74 operators const
6c620 72 61 69 6e 69 6e 67 20 74 68 69 73 20 6c 6f 6f raining this loo
6c630 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 49 6e p */. struct In
6c640 4c 6f 6f 70 20 7b 0a 20 20 20 20 69 6e 74 20 69 Loop {. int i
6c650 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 Cur;
6c660 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 /* The VDBE cu
6c670 72 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 69 rsor used by thi
6c680 73 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f s IN operator */
6c690 0a 20 20 20 20 69 6e 74 20 74 6f 70 41 64 64 72 . int topAddr
6c6a0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
6c6b0 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f op of the IN loo
6c6c0 70 20 2a 2f 0a 20 20 7d 20 2a 61 49 6e 4c 6f 6f p */. } *aInLoo
6c6d0 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p; /*
6c6e0 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
6c6f0 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e t each nested IN
6c700 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 73 operator */. s
6c710 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
6c720 6f 20 2a 70 42 65 73 74 49 64 78 3b 20 20 2f 2a o *pBestIdx; /*
6c730 20 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 Index informati
6c740 6f 6e 20 66 6f 72 20 74 68 69 73 20 6c 65 76 65 on for this leve
6c750 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 l */.. /* The f
6c760 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69 ollowing field i
6c770 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72 s really not par
6c780 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 t of the current
6c790 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a level. But. *
6c7a0 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63 * we need a plac
6c7b0 65 20 74 6f 20 63 61 63 68 65 20 69 6e 64 65 78 e to cache index
6c7c0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 information for
6c7d0 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74 each table in t
6c7e0 68 65 0a 20 20 2a 2a 20 46 52 4f 4d 20 63 6c 61 he. ** FROM cla
6c7f0 75 73 65 20 61 6e 64 20 74 68 65 20 57 68 65 72 use and the Wher
6c800 65 4c 65 76 65 6c 20 73 74 72 75 63 74 75 72 65 eLevel structure
6c810 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 is a convenient
6c820 20 70 6c 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 73 place.. */. s
6c830 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
6c840 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a o *pIdxInfo; /*
6c850 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 Index info for
6c860 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c n-th source tabl
6c870 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 e */.};../*.** F
6c880 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 lags appropriate
6c890 20 66 6f 72 20 74 68 65 20 77 66 6c 61 67 73 20 for the wflags
6c8a0 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c parameter of sql
6c8b0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
6c8c0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 ..*/.#define WHE
6c8d0 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 RE_ORDERBY_NORMA
6c8e0 4c 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 2d L 0 /* No-
6c8f0 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 op */.#define WH
6c900 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 ERE_ORDERBY_MIN
6c910 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 4f 52 1 /* OR
6c920 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e DER BY processin
6c930 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63 g for min() func
6c940 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 */.#define WHER
6c950 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20 E_ORDERBY_MAX
6c960 20 20 20 20 20 32 20 20 20 2f 2a 20 4f 52 44 45 2 /* ORDE
6c970 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 R BY processing
6c980 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a for max() func *
6c990 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
6c9a0 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 20 ONEPASS_DESIRED
6c9b0 20 20 20 34 20 20 20 2f 2a 20 57 61 6e 74 20 74 4 /* Want t
6c9c0 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50 o do one-pass UP
6c9d0 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 0a DATE/DELETE */..
6c9e0 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 /*.** The WHERE
6c9f0 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e clause processin
6ca00 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 g routine has tw
6ca10 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a o halves. The.*
6ca20 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 * first part doe
6ca30 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 s the start of t
6ca40 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e he WHERE loop an
6ca50 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 d the second.**
6ca60 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 half does the ta
6ca70 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 il of the WHERE
6ca80 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e loop. An instan
6ca90 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 ce of.** this st
6caa0 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 ructure is retur
6cab0 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 ned by the first
6cac0 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 half and passed
6cad0 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 .** into the sec
6cae0 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 ond half to give
6caf0 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 some continuity
6cb00 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 ..*/.struct Wher
6cb10 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 eInfo {. Parse
6cb20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f *pParse; /
6cb30 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
6cb40 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
6cb50 6e 74 65 78 74 20 2a 2f 0a 20 20 75 38 20 6f 6b ntext */. u8 ok
6cb60 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 OnePass;
6cb70 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 /* Ok to use one
6cb80 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 -pass algorithm
6cb90 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45 for UPDATE or DE
6cba0 4c 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73 LETE */. SrcLis
6cbb0 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 2f t *pTabList; /
6cbc0 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 * List of tables
6cbd0 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a in the join */.
6cbe0 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20 int iTop;
6cbf0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 /* The ve
6cc00 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 ry beginning of
6cc10 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a the WHERE loop *
6cc20 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 /. int iContinu
6cc30 65 3b 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 e; /* Jump
6cc40 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 here to continu
6cc50 65 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f e with next reco
6cc60 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 rd */. int iBre
6cc70 61 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ak; /*
6cc80 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 Jump here to bre
6cc90 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f ak out of the lo
6cca0 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76 op */. int nLev
6ccb0 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 el; /*
6ccc0 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 Number of nested
6ccd0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 71 6c 69 74 loop */. sqlit
6cce0 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a e3_index_info **
6ccf0 61 70 49 6e 66 6f 3b 20 20 2f 2a 20 41 72 72 61 apInfo; /* Arra
6cd00 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f y of pointers to
6cd10 20 69 6e 64 65 78 20 69 6e 66 6f 20 73 74 72 75 index info stru
6cd20 63 74 75 72 65 73 20 2a 2f 0a 20 20 57 68 65 72 ctures */. Wher
6cd30 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20 20 20 eLevel a[1];
6cd40 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
6cd50 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 about each nest
6cd60 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45 52 loop in the WHER
6cd70 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 E */.};../*.** A
6cd80 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66 NameContext def
6cd90 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69 ines a context i
6cda0 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c n which to resol
6cdb0 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c ve table and col
6cdc0 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 umn.** names. T
6cdd0 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69 he context consi
6cde0 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 sts of a list of
6cdf0 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 72 tables (the pSr
6ce00 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64 cList) field and
6ce10 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 .** a list of na
6ce20 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28 med expression (
6ce30 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61 pEList). The na
6ce40 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c med expression l
6ce50 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 ist may.** be NU
6ce60 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f LL. The pSrc co
6ce70 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 rresponds to the
6ce80 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 FROM clause of
6ce90 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 a SELECT or.** t
6cea0 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e o the table bein
6ceb0 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79 g operated on by
6cec0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
6ced0 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65 or DELETE. The
6cee0 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65 .** pEList corre
6cef0 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65 sponds to the re
6cf00 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 sult set of a SE
6cf10 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c LECT and is NULL
6cf20 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74 for.** other st
6cf30 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 atements..**.**
6cf40 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e NameContexts can
6cf50 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65 be nested. Whe
6cf60 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 n resolving name
6cf70 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 s, the inner-mos
6cf80 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 t .** context is
6cf90 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74 2e searched first.
6cfa0 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 If no match is
6cfb0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74 found, the next
6cfc0 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 outer.** contex
6cfd0 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49 t is checked. I
6cfe0 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c f there is still
6cff0 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e no match, the n
6d000 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 ext context.** i
6d010 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73 s checked. This
6d020 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75 process continu
6d030 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 es until either
6d040 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 a match is found
6d050 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 .** or all conte
6d060 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20 xts are check.
6d070 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20 When a match is
6d080 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20 found, the nRef
6d090 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65 member of.** the
6d0a0 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e context contain
6d0b0 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 73 ing the match is
6d0c0 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a incremented. .*
6d0d0 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65 *.** Each subque
6d0e0 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61 ry gets a new Na
6d0f0 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 meContext. The
6d100 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e pNext field poin
6d110 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d ts to the.** Nam
6d120 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 eContext in the
6d130 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54 parent query. T
6d140 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 20 hus the process
6d150 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a of scanning the.
6d160 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c ** NameContext l
6d170 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 ist corresponds
6d180 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 72 to searching thr
6d190 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65 6c ough successivel
6d1a0 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75 y outer.** subqu
6d1b0 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f eries looking fo
6d1c0 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 r a match..*/.st
6d1d0 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 ruct NameContext
6d1e0 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 {. Parse *pPar
6d1f0 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 se; /* The
6d200 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63 parser */. Src
6d210 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 List *pSrcList;
6d220 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 /* One or more
6d230 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 tables used to
6d240 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f resolve names */
6d250 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c . ExprList *pEL
6d260 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f ist; /* Optio
6d270 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 nal list of name
6d280 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f d expressions */
6d290 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
6d2a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6d2b0 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c r of names resol
6d2c0 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74 ved by this cont
6d2d0 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 ext */. int nEr
6d2e0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r; /*
6d2f0 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 Number of error
6d300 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 s encountered wh
6d310 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 ile resolving na
6d320 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f mes */. u8 allo
6d330 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a wAgg; /*
6d340 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 Aggregate funct
6d350 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72 ions allowed her
6d360 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67 e */. u8 hasAgg
6d370 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
6d380 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74 65 rue if aggregate
6d390 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20 s are seen */.
6d3a0 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20 u8 isCheck;
6d3b0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
6d3c0 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 resolving names
6d3d0 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 in a CHECK const
6d3e0 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e raint */. int n
6d3f0 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 Depth;
6d400 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 71 /* Depth of subq
6d410 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20 uery recursion.
6d420 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69 1 for no recursi
6d430 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 on */. AggInfo
6d440 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 *pAggInfo; /*
6d450 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
6d460 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74 20 t aggregates at
6d470 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 this level */.
6d480 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 NameContext *pNe
6d490 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 xt; /* Next out
6d4a0 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e er name context.
6d4b0 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 NULL for outer
6d4c0 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a most */.};../*.*
6d4d0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
6d4e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
6d4f0 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
6d500 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f s all informatio
6d510 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67 n.** needed to g
6d520 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 enerate code for
6d530 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54 a single SELECT
6d540 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a statement..**.*
6d550 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20 * nLimit is set
6d560 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 to -1 if there i
6d570 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 s no LIMIT claus
6d580 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73 e. nOffset is s
6d590 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 et to 0..** If t
6d5a0 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20 here is a LIMIT
6d5b0 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 73 clause, the pars
6d5c0 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 er sets nLimit t
6d5d0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 o the value of t
6d5e0 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20 he.** limit and
6d5f0 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 76 nOffset to the v
6d600 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73 alue of the offs
6d610 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 72 et (or 0 if ther
6d620 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 e is not.** offs
6d630 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20 et). But later
6d640 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e on, nLimit and n
6d650 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68 Offset become th
6d660 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f e memory locatio
6d670 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42 ns.** in the VDB
6d680 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 E that record th
6d690 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 e limit and offs
6d6a0 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a et counters..**.
6d6b0 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b ** addrOpenEphm[
6d6c0 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69 ] entries contai
6d6d0 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 n the address of
6d6e0 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 OP_OpenEphemera
6d6f0 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 l opcodes..** Th
6d700 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d 75 ese addresses mu
6d710 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20 st be stored so
6d720 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62 that we can go b
6d730 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a ack and fill in.
6d740 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46 ** the P4_KEYINF
6d750 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74 O and P2 paramet
6d760 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74 ers later. Neit
6d770 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 her the KeyInfo
6d780 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 nor.** the numbe
6d790 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
6d7a0 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74 P2 can be comput
6d7b0 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ed at the same t
6d7c0 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50 ime.** as the OP
6d7d0 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75 _OpenEphm instru
6d7e0 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62 ction is coded b
6d7f0 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e ecause not.** en
6d800 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ough information
6d810 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f about the compo
6d820 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f und query is kno
6d830 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 wn at that point
6d840 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f ..** The KeyInfo
6d850 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 for addrOpenTra
6d860 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e n[0] and [1] con
6d870 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20 tains collating
6d880 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 sequences.** for
6d890 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e the result set.
6d8a0 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f The KeyInfo fo
6d8b0 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32 r addrOpenTran[2
6d8c0 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 ] contains colla
6d8d0 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 ting.** sequence
6d8e0 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20 s for the ORDER
6d8f0 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 BY clause..*/.st
6d900 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20 ruct Select {.
6d910 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
6d920 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 ; /* The fi
6d930 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 75 elds of the resu
6d940 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 lt */. u8 op;
6d950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6d960 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 * One of: TK_UNI
6d970 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 ON TK_ALL TK_INT
6d980 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54 ERSECT TK_EXCEPT
6d990 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e */. char affin
6d9a0 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ity; /*
6d9b0 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 20 MakeRecord with
6d9c0 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66 6f this affinity fo
6d9d0 72 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75 r SRT_Set */. u
6d9e0 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 20 16 selFlags;
6d9f0 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 /* Various
6da00 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a SF_* values */.
6da10 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b SrcList *pSrc;
6da20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
6da30 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 FROM clause */.
6da40 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 Expr *pWhere;
6da50 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 /* The W
6da60 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
6da70 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 ExprList *pGrou
6da80 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 pBy; /* The G
6da90 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a ROUP BY clause *
6daa0 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e /. Expr *pHavin
6dab0 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 g; /* Th
6dac0 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 e HAVING clause
6dad0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
6dae0 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54 OrderBy; /* T
6daf0 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
6db00 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a se */. Select *
6db10 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20 2f pPrior; /
6db20 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20 69 * Prior select i
6db30 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c n a compound sel
6db40 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f ect statement */
6db50 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 74 . Select *pNext
6db60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 ; /* Nex
6db70 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65 20 t select to the
6db80 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 left in a compou
6db90 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a nd */. Select *
6dba0 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f pRightmost; /
6dbb0 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c * Right-most sel
6dbc0 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e ect in a compoun
6dbd0 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 d select stateme
6dbe0 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c nt */. Expr *pL
6dbf0 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f imit; /
6dc00 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69 * LIMIT expressi
6dc10 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e on. NULL means n
6dc20 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 ot used. */. Ex
6dc30 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 pr *pOffset;
6dc40 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 /* OFFSET e
6dc50 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 xpression. NULL
6dc60 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 means not used.
6dc70 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c */. int iLimit,
6dc80 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d iOffset; /* M
6dc90 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 emory registers
6dca0 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 holding LIMIT &
6dcb0 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20 OFFSET counters
6dcc0 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65 */. int addrOpe
6dcd0 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f nEphm[3]; /* O
6dce0 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f P_OpenEphem opco
6dcf0 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 des related to t
6dd00 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b his select */.};
6dd10 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 ../*.** Allowed
6dd20 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 63 values for Selec
6dd30 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 65 t.selFlags. The
6dd40 20 22 53 46 22 20 70 72 65 66 69 78 20 73 74 61 "SF" prefix sta
6dd50 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 nds for.** "Sele
6dd60 63 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 ct Flag"..*/.#de
6dd70 66 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 74 fine SF_Distinct
6dd80 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 0x0001
6dd90 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 /* Output should
6dda0 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a be DISTINCT */.
6ddb0 23 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c #define SF_Resol
6ddc0 76 65 64 20 20 20 20 20 20 20 20 30 78 30 30 30 ved 0x000
6ddd0 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 2 /* Identifier
6dde0 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f s have been reso
6ddf0 6c 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 lved */.#define
6de00 53 46 5f 41 67 67 72 65 67 61 74 65 20 20 20 20 SF_Aggregate
6de10 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43 6f 0x0004 /* Co
6de20 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 ntains aggregate
6de30 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 functions */.#d
6de40 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70 68 efine SF_UsesEph
6de50 65 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38 20 emeral 0x0008
6de60 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65 /* Uses the Ope
6de70 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 nEphemeral opcod
6de80 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f e */.#define SF_
6de90 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20 20 Expanded
6dea0 30 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 74 0x0010 /* sqlit
6deb0 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 e3SelectExpand()
6dec0 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 called on this
6ded0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61 */.#define SF_Ha
6dee0 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78 sTypeInfo 0x
6def0 30 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 0020 /* FROM su
6df00 62 71 75 65 72 69 65 73 20 68 61 76 65 20 54 61 bqueries have Ta
6df10 62 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a ble metadata */.
6df20 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 ../*.** The resu
6df30 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20 lts of a select
6df40 63 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 74 can be distribut
6df50 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 ed in several wa
6df60 79 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 54 ys. The.** "SRT
6df70 22 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22 " prefix means "
6df80 53 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79 SELECT Result Ty
6df90 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 pe"..*/.#define
6dfa0 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 SRT_Union
6dfb0 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 1 /* Store res
6dfc0 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61 ult as keys in a
6dfd0 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 n index */.#defi
6dfe0 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 ne SRT_Except
6dff0 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 2 /* Remove
6e000 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 result from a U
6e010 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 NION index */.#d
6e020 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74 73 efine SRT_Exists
6e030 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74 6f 3 /* Sto
6e040 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73 75 re 1 if the resu
6e050 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 lt is not empty
6e060 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44 */.#define SRT_D
6e070 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20 2f iscard 4 /
6e080 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 * Do not save th
6e090 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65 e results anywhe
6e0a0 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 re */../* The OR
6e0b0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 DER BY clause is
6e0c0 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c ignored for all
6e0d0 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f of the above */
6e0e0 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 .#define Ignorab
6e0f0 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28 58 leOrderby(X) ((X
6e100 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 69 ->eDest)<=SRT_Di
6e110 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20 scard)..#define
6e120 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20 SRT_Output
6e130 20 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 61 5 /* Output ea
6e140 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 ch row of result
6e150 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
6e160 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20 20 Mem 6
6e170 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 /* Store result
6e180 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c in a memory cell
6e190 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
6e1a0 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20 20 Set 7
6e1b0 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 /* Store results
6e1c0 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 as keys in an i
6e1d0 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ndex */.#define
6e1e0 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 SRT_Table
6e1f0 20 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 8 /* Store res
6e200 75 6c 74 20 61 73 20 64 61 74 61 20 77 69 74 68 ult as data with
6e210 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f an automatic ro
6e220 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 wid */.#define S
6e230 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20 RT_EphemTab
6e240 39 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 9 /* Create tra
6e250 6e 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73 nsient tab and s
6e260 74 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 tore like SRT_Ta
6e270 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ble */.#define S
6e280 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 RT_Coroutine 1
6e290 30 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 0 /* Generate a
6e2a0 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 single row of r
6e2b0 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 esult */../*.**
6e2c0 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64 A structure used
6e2d0 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 to customize th
6e2e0 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 73 e behaviour of s
6e2f0 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20 qlite3Select().
6e300 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 See.** comments
6e310 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c above sqlite3Sel
6e320 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c ect() for detail
6e330 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s..*/.typedef st
6e340 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 ruct SelectDest
6e350 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75 SelectDest;.stru
6e360 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a ct SelectDest {.
6e370 20 20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20 u8 eDest;
6e380 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 /* How to di
6e390 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 spose of the res
6e3a0 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 ults */. u8 aff
6e3b0 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 inity; /* A
6e3c0 66 66 69 6e 69 74 79 20 75 73 65 64 20 77 68 65 ffinity used whe
6e3d0 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 n eDest==SRT_Set
6e3e0 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b */. int iParm;
6e3f0 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 /* A par
6e400 61 6d 65 74 65 72 20 75 73 65 64 20 62 79 20 74 ameter used by t
6e410 68 65 20 65 44 65 73 74 20 64 69 73 70 6f 73 61 he eDest disposa
6e420 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e l method */. in
6e430 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t iMem;
6e440 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 /* Base register
6e450 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 where results a
6e460 72 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 re written */.
6e470 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 int nMem;
6e480 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
6e490 65 67 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 egisters allocat
6e4a0 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ed */.};../*.**
6e4b0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f An SQL parser co
6e4c0 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f ntext. A copy o
6e4d0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
6e4e0 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 is passed throu
6e4f0 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72 gh.** the parser
6e500 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61 and down into a
6e510 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63 ll the parser ac
6e520 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20 tion routine in
6e530 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72 order to.** carr
6e540 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 y around informa
6e550 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f tion that is glo
6e560 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72 bal to the entir
6e570 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 e parse..**.** T
6e580 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 he structure is
6e590 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f divided into two
6e5a0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68 parts. When th
6e5b0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 e parser and cod
6e5c0 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61 e.** generate ca
6e5d0 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65 ll themselves re
6e5e0 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66 cursively, the f
6e5f0 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 irst part of the
6e600 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 structure.** is
6e610 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68 constant but th
6e620 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73 e second part is
6e630 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65 reset at the be
6e640 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 ginning and end
6e650 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72 of.** each recur
6e660 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sion..**.** The
6e670 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 nTableLock and a
6e680 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62 TableLock variab
6e690 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 les are only use
6e6a0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d d if the shared-
6e6b0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72 cache .** featur
6e6c0 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66 e is enabled (if
6e6d0 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75 sqlite3Tsd()->u
6e6e0 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20 seSharedData is
6e6f0 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a true). They are.
6e700 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ** used to store
6e710 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c the set of tabl
6e720 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64 e-locks required
6e730 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e by the statemen
6e740 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 t being.** compi
6e750 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71 led. Function sq
6e760 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 lite3TableLock()
6e770 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 is used to add
6e780 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a entries to the.*
6e790 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 * list..*/.struc
6e7a0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69 t Parse {. sqli
6e7b0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
6e7c0 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 /* The main dat
6e7d0 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 abase structure
6e7e0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
6e7f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
6e800 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78 urn code from ex
6e810 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 ecution */. cha
6e820 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 r *zErrMsg;
6e830 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 /* An error me
6e840 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20 ssage */. Vdbe
6e850 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20 *pVdbe;
6e860 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72 /* An engine for
6e870 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62 executing datab
6e880 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a ase bytecode */.
6e890 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74 u8 colNamesSet
6e8a0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61 ; /* TRUE a
6e8b0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 fter OP_ColumnNa
6e8c0 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75 me has been issu
6e8d0 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20 ed to pVdbe */.
6e8e0 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20 u8 nameClash;
6e8f0 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61 /* A perma
6e900 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20 nent table name
6e910 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d clashes with tem
6e920 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a p table name */.
6e930 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61 u8 checkSchema
6e940 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73 ; /* Causes
6e950 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
6e960 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 heck after an er
6e970 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74 ror */. u8 nest
6e980 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ed; /*
6e990 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
6e9a0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70 d calls to the p
6e9b0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72 arser/code gener
6e9c0 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72 ator */. u8 par
6e9d0 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f seError; /
6e9e0 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70 * True after a p
6e9f0 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54 arsing error. T
6ea00 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20 icket #1794 */.
6ea10 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20 u8 nTempReg;
6ea20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6ea30 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 of temporary reg
6ea40 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52 isters in aTempR
6ea50 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65 eg[] */. u8 nTe
6ea60 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f mpInUse; /
6ea70 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d * Number of aTem
6ea80 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79 pReg[] currently
6ea90 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
6eaa0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 int aTempReg[8
6eab0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e ]; /* Holdin
6eac0 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f g area for tempo
6ead0 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a rary registers *
6eae0 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 /. int nRangeRe
6eaf0 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 g; /* Size
6eb00 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 of the temporar
6eb10 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b y register block
6eb20 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 */. int iRange
6eb30 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 Reg; /* Fi
6eb40 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 rst register in
6eb50 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
6eb60 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e er block */. in
6eb70 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 t nErr;
6eb80 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6eb90 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 errors seen */.
6eba0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 int nTab;
6ebb0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6ebc0 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c of previously al
6ebd0 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72 located VDBE cur
6ebe0 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d sors */. int nM
6ebf0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
6ec00 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
6ec10 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f ry cells used so
6ec20 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 far */. int nS
6ec30 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f et; /
6ec40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 * Number of sets
6ec50 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a used so far */.
6ec60 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20 int ckBase;
6ec70 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 /* Base r
6ec80 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20 egister of data
6ec90 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e during check con
6eca0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e straints */. in
6ecb0 74 20 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 t disableColCach
6ecc0 65 3b 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69 e; /* True to di
6ecd0 73 61 62 6c 65 20 61 64 64 69 6e 67 20 74 6f 20 sable adding to
6ece0 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a column cache */.
6ecf0 20 20 69 6e 74 20 6e 43 6f 6c 43 61 63 68 65 3b int nColCache;
6ed00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6ed10 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 of entries in t
6ed20 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 he column cache
6ed30 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 43 61 63 */. int iColCac
6ed40 68 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 he; /* Nex
6ed50 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 t entry of the c
6ed60 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20 ache to replace
6ed70 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c */. struct yCol
6ed80 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20 Cache {. int
6ed90 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 iTable;
6eda0 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f /* Table curso
6edb0 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 r number */.
6edc0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 int iColumn;
6edd0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 /* Table c
6ede0 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a olumn number */.
6edf0 20 20 20 20 63 68 61 72 20 61 66 66 43 68 61 6e char affChan
6ee00 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ge; /* Tru
6ee10 65 20 69 66 20 74 68 69 73 20 72 65 67 69 73 74 e if this regist
6ee20 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 61 66 er has had an af
6ee30 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f finity change */
6ee40 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 . int iReg;
6ee50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
6ee60 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 76 gister holding v
6ee70 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c alue of this col
6ee80 75 6d 6e 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 umn */. } aColC
6ee90 61 63 68 65 5b 31 30 5d 3b 20 20 20 20 20 2f 2a ache[10]; /*
6eea0 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 20 76 61 One for each va
6eeb0 6c 69 64 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 lid column cache
6eec0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20 entry */. u32
6eed0 77 72 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20 writeMask;
6eee0 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69 74 /* Start a writ
6eef0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e e transaction on
6ef00 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65 73 these databases
6ef10 20 2a 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65 */. u32 cookie
6ef20 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69 Mask; /* Bi
6ef30 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20 tmask of schema
6ef40 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61 73 verified databas
6ef50 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b es */. int cook
6ef60 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20 ieGoto; /*
6ef70 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f Address of OP_Go
6ef80 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72 to to cookie ver
6ef90 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65 ifier subroutine
6efa0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 */. int cookie
6efb0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58 Value[SQLITE_MAX
6efc0 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f _ATTACHED+2]; /
6efd0 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b * Values of cook
6efe0 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f ies to verify */
6eff0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
6f000 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
6f010 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f E. int nTableLo
6f020 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ck; /* Nu
6f030 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e mber of locks in
6f040 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20 aTableLock */.
6f050 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62 TableLock *aTab
6f060 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69 leLock; /* Requi
6f070 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 red table locks
6f080 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 for shared-cache
6f090 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a mode */.#endif.
6f0a0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 int regRowid;
6f0b0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
6f0c0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 er holding rowid
6f0d0 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45 of CREATE TABLE
6f0e0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 entry */. int
6f0f0 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 regRoot;
6f100 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
6f110 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e ding root page n
6f120 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62 umber for new ob
6f130 6a 65 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41 jects */.. /* A
6f140 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e 74 bove is constant
6f150 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 69 between recursi
6f160 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 72 ons. Below is r
6f170 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 20 eset before and
6f180 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 20 after. ** each
6f190 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20 recursion */..
6f1a0 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 int nVar;
6f1b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6f1c0 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 20 f '?' variables
6f1d0 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c 20 seen in the SQL
6f1e0 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
6f1f0 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 20 nVarExpr;
6f200 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 /* Number of us
6f210 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 ed slots in apVa
6f220 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74 rExpr[] */. int
6f230 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20 nVarExprAlloc;
6f240 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
6f250 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 69 llocated slots i
6f260 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f n apVarExpr[] */
6f270 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 45 . Expr **apVarE
6f280 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 xpr; /* Point
6f290 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 20 ers to :aaa and
6f2a0 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 65 $aaaa wildcard e
6f2b0 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 xpressions */.
6f2c0 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 int nAlias;
6f2d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6f2e0 66 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 f aliased result
6f2f0 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a set columns */.
6f300 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20 int *aAlias;
6f310 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
6f320 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 er used to hold
6f330 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 2a aliased result *
6f340 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 /. u8 explain;
6f350 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
6f360 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 if the EXPLAIN
6f370 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e flag is found on
6f380 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 the query */.
6f390 54 6f 6b 65 6e 20 73 45 72 72 54 6f 6b 65 6e 3b Token sErrToken;
6f3a0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 /* The toke
6f3b0 6e 20 61 74 20 77 68 69 63 68 20 74 68 65 20 65 n at which the e
6f3c0 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f rror occurred */
6f3d0 0a 20 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f . Token sNameTo
6f3e0 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e ken; /* Token
6f3f0 20 77 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 with unqualifie
6f400 64 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 d schema object
6f410 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 name */. Token
6f420 73 4c 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f sLastToken; /
6f430 2a 20 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e * The last token
6f440 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e parsed */. con
6f450 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 st char *zSql;
6f460 20 20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 /* All SQL tex
6f470 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 t */. const cha
6f480 72 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 r *zTail; /* A
6f490 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 ll SQL text past
6f4a0 20 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f the last semico
6f4b0 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 lon parsed */.
6f4c0 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 Table *pNewTable
6f4d0 3b 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 ; /* A table
6f4e0 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 being constructe
6f4f0 64 20 62 79 20 43 52 45 41 54 45 20 54 41 42 4c d by CREATE TABL
6f500 45 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a E */. Trigger *
6f510 70 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 pNewTrigger;
6f520 20 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 /* Trigger unde
6f530 72 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 r construct by a
6f540 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 CREATE TRIGGER
6f550 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 61 63 */. TriggerStac
6f560 6b 20 2a 74 72 69 67 53 74 61 63 6b 3b 20 20 2f k *trigStack; /
6f570 2a 20 54 72 69 67 67 65 72 20 61 63 74 69 6f 6e * Trigger action
6f580 73 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f s being coded */
6f590 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
6f5a0 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20 AuthContext; /*
6f5b0 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 The 6th paramete
6f5c0 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63 r to db->xAuth c
6f5d0 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e allbacks */.#ifn
6f5e0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
6f5f0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 VIRTUALTABLE. T
6f600 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20 oken sArg;
6f610 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d /* Com
6f620 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20 plete text of a
6f630 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 module argument
6f640 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56 */. u8 declareV
6f650 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 tab;
6f660 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64 /* True if insid
6f670 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 e sqlite3_declar
6f680 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e e_vtab() */. in
6f690 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 t nVtabLock;
6f6a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
6f6b0 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 er of virtual ta
6f6c0 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a bles to lock */.
6f6d0 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 Table **apVtab
6f6e0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 Lock; /*
6f6f0 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75 Pointer to virtu
6f700 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e al tables needin
6f710 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e g locking */.#en
6f720 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 dif. int nHeigh
6f730 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
6f740 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 Expression tree
6f750 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 65 height of curre
6f760 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f nt sub-select */
6f770 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69 . Table *pZombi
6f780 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69 eTab; /* Li
6f790 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65 st of Table obje
6f7a0 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 66 cts to delete af
6f7b0 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a ter code gen */.
6f7c0 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 };..#ifdef SQLIT
6f7d0 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
6f7e0 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20 49 4e BLE. #define IN
6f7f0 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 30 0a _DECLARE_VTAB 0.
6f800 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
6f810 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
6f820 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 (pParse->declare
6f830 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a Vtab).#endif../*
6f840 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
6f850 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
6f860 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 structure can b
6f870 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20 61 20 e declared on a
6f880 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a stack and used.*
6f890 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20 50 61 * to save the Pa
6f8a0 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 rse.zAuthContext
6f8b0 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74 20 69 value so that i
6f8c0 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 t can be restore
6f8d0 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75 d later..*/.stru
6f8e0 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20 7b ct AuthContext {
6f8f0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
6f900 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f AuthContext; /
6f910 2a 20 50 75 74 20 73 61 76 65 64 20 50 61 72 73 * Put saved Pars
6f920 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 68 e.zAuthContext h
6f930 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a ere */. Parse *
6f940 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
6f950 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61 72 73 /* The Pars
6f960 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d e structure */.}
6f970 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c ;../*.** Bitfiel
6f980 64 20 66 6c 61 67 73 20 66 6f 72 20 50 32 20 76 d flags for P2 v
6f990 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73 65 72 alue in OP_Inser
6f9a0 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74 65 0a t and OP_Delete.
6f9b0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 */.#define OPFLA
6f9c0 47 5f 4e 43 48 41 4e 47 45 20 20 20 31 20 20 20 G_NCHANGE 1
6f9d0 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 /* Set to updat
6f9e0 65 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f e db->nChange */
6f9f0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
6fa00 4c 41 53 54 52 4f 57 49 44 20 32 20 20 20 20 2f LASTROWID 2 /
6fa10 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 * Set to update
6fa20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f db->lastRowid */
6fa30 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
6fa40 49 53 55 50 44 41 54 45 20 20 34 20 20 20 20 2f ISUPDATE 4 /
6fa50 2a 20 54 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 * This OP_Insert
6fa60 20 69 73 20 61 6e 20 73 71 6c 20 55 50 44 41 54 is an sql UPDAT
6fa70 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 E */.#define OPF
6fa80 4c 41 47 5f 41 50 50 45 4e 44 20 20 20 20 38 20 LAG_APPEND 8
6fa90 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 /* This is li
6faa0 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 kely to be an ap
6fab0 70 65 6e 64 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 pend */../*. * E
6fac0 61 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73 ach trigger pres
6fad0 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 ent in the datab
6fae0 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74 ase schema is st
6faf0 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 ored as an insta
6fb00 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 nce of. * struct
6fb10 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a Trigger. . *. *
6fb20 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 Pointers to ins
6fb30 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 tances of struct
6fb40 20 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f Trigger are sto
6fb50 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e red in two ways.
6fb60 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74 . * 1. In the "t
6fb70 72 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61 rigHash" hash ta
6fb80 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65 ble (part of the
6fb90 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72 sqlite3* that r
6fba0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20 epresents the .
6fbb0 2a 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20 * database).
6fbc0 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 This allows Trig
6fbd0 67 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74 ger structures t
6fbe0 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62 o be retrieved b
6fbf0 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c y name.. * 2. Al
6fc00 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 l triggers assoc
6fc10 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e iated with a sin
6fc20 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 gle table form a
6fc30 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 linked list, us
6fc40 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e ing the. * pN
6fc50 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 ext member of st
6fc60 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20 ruct Trigger. A
6fc70 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 pointer to the f
6fc80 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
6fc90 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 the. * linked
6fca0 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 list is stored
6fcb0 61 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72 as the "pTrigger
6fcc0 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 " member of the
6fcd0 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 associated. *
6fce0 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 struct Table..
6fcf0 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c *. * The "step_l
6fd00 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e ist" member poin
6fd10 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 ts to the first
6fd20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e element of a lin
6fd30 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 ked list. * cont
6fd40 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 aining the SQL s
6fd50 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 tatements specif
6fd60 69 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67 ied as the trigg
6fd70 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a er program.. */.
6fd80 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b struct Trigger {
6fd90 0a 20 20 63 68 61 72 20 2a 6e 61 6d 65 3b 20 20 . char *name;
6fda0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
6fdb0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 e name of the tr
6fdc0 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 igger
6fdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a */.
6fde0 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 char *table;
6fdf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6fe00 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74 table or view t
6fe10 6f 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67 o which the trig
6fe20 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 ger applies */.
6fe30 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 u8 op;
6fe40 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
6fe50 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b of TK_DELETE, TK
6fe60 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 _UPDATE, TK_INSE
6fe70 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 RT */.
6fe80 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 u8 tr_tm;
6fe90 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
6fea0 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 f TRIGGER_BEFORE
6feb0 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 , TRIGGER_AFTER
6fec0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e */. Expr *pWhen
6fed0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
6fee0 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20 The WHEN clause
6fef0 6f 66 20 74 68 65 20 65 78 70 72 65 73 69 6f 6e of the expresion
6ff00 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a (may be NULL) *
6ff10 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c /. IdList *pCol
6ff20 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49 umns; /* I
6ff30 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 44 f this is an UPD
6ff40 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c ATE OF <column-l
6ff50 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20 ist> trigger,.
6ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ff70 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 3c the <
6ff80 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 column-list> is
6ff90 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 stored here */.
6ffa0 20 54 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e Token nameToken
6ffb0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 ; /* Toke
6ffc0 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 4e 61 n containing zNa
6ffd0 6d 65 2e 20 55 73 65 20 64 75 72 69 6e 67 20 70 me. Use during p
6ffe0 61 72 73 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20 arsing only */.
6fff0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
70000 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 ; /* Sche
70010 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ma containing th
70020 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 e trigger */. S
70030 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d chema *pTabSchem
70040 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 a; /* Schema
70050 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
70060 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67 table */. Trigg
70070 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73 erStep *step_lis
70080 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 t; /* Link list
70090 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 of trigger progr
700a0 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 20 am steps
700b0 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65 */. Trigge
700c0 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 r *pNext;
700d0 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 /* Next trigge
700e0 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 r associated wit
700f0 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d h the table */.}
70100 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67 ;../*.** A trigg
70110 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 42 er is either a B
70120 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45 EFORE or an AFTE
70130 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 R trigger. The
70140 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 following consta
70150 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 nts.** determine
70160 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 which. .**.** I
70170 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74 f there are mult
70180 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79 iple triggers, y
70190 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65 ou might of some
701a0 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65 BEFORE and some
701b0 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 AFTER..** In th
701c0 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f at cases, the co
701d0 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61 nstants below ca
701e0 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68 n be ORed togeth
701f0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 er..*/.#define T
70200 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31 RIGGER_BEFORE 1
70210 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 .#define TRIGGER
70220 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 _AFTER 2../*.
70230 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
70240 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 struct TriggerS
70250 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 73 tep is used to s
70260 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51 tore a single SQ
70270 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74 L statement. * t
70280 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f 66 hat is a part of
70290 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 a trigger-progr
702a0 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 am. . *. * Insta
702b0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 nces of struct T
702c0 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 73 riggerStep are s
702d0 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c tored in a singl
702e0 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c y linked list (l
702f0 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74 inked. * using t
70300 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 he "pNext" membe
70310 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 r) referenced by
70320 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 the "step_list"
70330 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a member of the .
70340 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 * associated st
70350 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73 ruct Trigger ins
70360 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74 tance. The first
70370 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
70380 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20 linked list is.
70390 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70 * the first step
703a0 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2d of the trigger-
703b0 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 program.. * . *
703c0 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20 The "op" member
703d0 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65 indicates whethe
703e0 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 4c r this is a "DEL
703f0 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20 ETE", "INSERT",
70400 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22 "UPDATE" or. * "
70410 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e SELECT" statemen
70420 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 t. The meanings
70430 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d of the other mem
70440 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e bers is determin
70450 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61 ed by the . * va
70460 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66 lue of "op" as f
70470 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f ollows:. *. * (o
70480 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a p == TK_INSERT).
70490 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20 * orconf ->
704a0 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f stores the ON CO
704b0 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d NFLICT algorithm
704c0 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e . * pSelect ->
704d0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 If this is an I
704e0 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 NSERT INTO ... S
704f0 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d ELECT ... statem
70500 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 ent, then. *
70510 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73 this s
70520 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 tores a pointer
70530 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 to the SELECT st
70540 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69 atement. Otherwi
70550 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 se NULL.. * targ
70560 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e et -> A token
70570 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d holding the nam
70580 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 e of the table t
70590 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 o insert into..
705a0 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 * pExprList -> I
705b0 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 f this is an INS
705c0 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c ERT INTO ... VAL
705d0 55 45 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e UES ... statemen
705e0 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 t, then. *
705f0 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74 6f this sto
70600 72 65 73 20 76 61 6c 75 65 73 20 74 6f 20 62 65 res values to be
70610 20 69 6e 73 65 72 74 65 64 2e 20 4f 74 68 65 72 inserted. Other
70620 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 wise NULL.. * pI
70630 64 4c 69 73 74 20 20 20 2d 3e 20 49 66 20 74 68 dList -> If th
70640 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 is is an INSERT
70650 49 4e 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d INTO ... (<colum
70660 6e 2d 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 n-names>) VALUES
70670 20 2e 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 ... . *
70680 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c statement,
70690 20 74 68 65 6e 20 74 68 69 73 20 73 74 6f 72 65 then this store
706a0 73 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d s the column-nam
706b0 65 73 20 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 es to be. *
706c0 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 65 inserte
706d0 64 20 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f d into.. *. * (o
706e0 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a p == TK_DELETE).
706f0 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 * target ->
70700 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 A token holding
70710 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
70720 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 table to delete
70730 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 from.. * pWhere
70740 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 -> The WHERE
70750 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 clause of the DE
70760 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 LETE statement i
70770 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 f one is specifi
70780 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 ed.. *
70790 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 Otherwise NU
707a0 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d LL.. * . * (op =
707b0 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 = TK_UPDATE). *
707c0 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 target -> A t
707d0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 oken holding the
707e0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
707f0 6c 65 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77 le to update row
70800 73 20 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20 s of.. * pWhere
70810 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 -> The WHERE
70820 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 clause of the UP
70830 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 DATE statement i
70840 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 f one is specifi
70850 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 ed.. *
70860 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 Otherwise NU
70870 4c 4c 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 LL.. * pExprList
70880 20 2d 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68 -> A list of th
70890 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64 e columns to upd
708a0 61 74 65 20 61 6e 64 20 74 68 65 20 65 78 70 72 ate and the expr
708b0 65 73 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74 essions to updat
708c0 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 e. *
708d0 20 20 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73 them to. See s
708e0 71 6c 69 74 65 33 55 70 64 61 74 65 28 29 20 64 qlite3Update() d
708f0 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 ocumentation of
70900 22 70 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20 "pChanges". *
70910 20 20 20 20 20 20 20 20 20 20 20 61 72 67 75 6d argum
70920 65 6e 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 ent.. * . */.str
70930 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 uct TriggerStep
70940 7b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 {. int op;
70950 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
70960 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b of TK_DELETE, TK
70970 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 _UPDATE, TK_INSE
70980 52 54 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f RT, TK_SELECT */
70990 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 . int orconf;
709a0 20 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f /* OE_Ro
709b0 6c 6c 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 llback etc. */.
709c0 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b Trigger *pTrig;
709d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69 /* The tri
709e0 67 67 65 72 20 74 68 61 74 20 74 68 69 73 20 73 gger that this s
709f0 74 65 70 20 69 73 20 61 20 70 61 72 74 20 6f 66 tep is a part of
70a00 20 2a 2f 0a 0a 20 20 53 65 6c 65 63 74 20 2a 70 */.. Select *p
70a10 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 56 Select; /* V
70a20 61 6c 69 64 20 66 6f 72 20 53 45 4c 45 43 54 20 alid for SELECT
70a30 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a 20 and sometimes .
70a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70a50 20 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 INSERT
70a60 73 74 65 70 73 20 28 77 68 65 6e 20 70 45 78 70 steps (when pExp
70a70 72 4c 69 73 74 20 3d 3d 20 30 29 20 2a 2f 0a 20 rList == 0) */.
70a80 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20 Token target;
70a90 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 /* Valid f
70aa0 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 41 54 or DELETE, UPDAT
70ab0 45 2c 20 49 4e 53 45 52 54 20 73 74 65 70 73 20 E, INSERT steps
70ac0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 */. Expr *pWher
70ad0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c e; /* Val
70ae0 69 64 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 id for DELETE, U
70af0 50 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 PDATE steps */.
70b00 20 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 ExprList *pExpr
70b10 4c 69 73 74 3b 20 2f 2a 20 56 61 6c 69 64 20 66 List; /* Valid f
70b20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d or UPDATE statem
70b30 65 6e 74 73 20 61 6e 64 20 73 6f 6d 65 74 69 6d ents and sometim
70b40 65 73 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 es .
70b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49 I
70b60 4e 53 45 52 54 20 73 74 65 70 73 20 28 77 68 65 NSERT steps (whe
70b70 6e 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 20 n pSelect == 0)
70b80 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 49 64 4c */. IdL
70b90 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20 ist *pIdList;
70ba0 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 49 /* Valid for I
70bb0 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73 NSERT statements
70bc0 20 6f 6e 6c 79 20 2a 2f 0a 20 20 54 72 69 67 67 only */. Trigg
70bd0 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b 20 20 erStep *pNext;
70be0 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65 20 6c /* Next in the l
70bf0 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 54 72 ink-list */. Tr
70c00 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61 73 74 iggerStep *pLast
70c10 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65 6d 65 ; /* Last eleme
70c20 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e nt in link-list.
70c30 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74 20 65 Valid for 1st e
70c40 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a lem only */.};..
70c50 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*. * An instanc
70c60 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 e of struct Trig
70c70 67 65 72 53 74 61 63 6b 20 73 74 6f 72 65 73 20 gerStack stores
70c80 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 72 65 71 75 information requ
70c90 69 72 65 64 20 64 75 72 69 6e 67 20 63 6f 64 65 ired during code
70ca0 0a 20 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f . * generation o
70cb0 66 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67 f a single trigg
70cc0 65 72 20 70 72 6f 67 72 61 6d 2e 20 57 68 69 6c er program. Whil
70cd0 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 e the trigger pr
70ce0 6f 67 72 61 6d 20 69 73 20 62 65 69 6e 67 0a 20 ogram is being.
70cf0 2a 20 63 6f 64 65 64 2c 20 69 74 73 20 61 73 73 * coded, its ass
70d00 6f 63 69 61 74 65 64 20 54 72 69 67 67 65 72 53 ociated TriggerS
70d10 74 61 63 6b 20 69 6e 73 74 61 6e 63 65 20 69 73 tack instance is
70d20 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 pointed to by t
70d30 68 65 0a 20 2a 20 22 70 54 72 69 67 67 65 72 53 he. * "pTriggerS
70d40 74 61 63 6b 22 20 6d 65 6d 62 65 72 20 6f 66 20 tack" member of
70d50 74 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74 the Parse struct
70d60 75 72 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 70 ure.. *. * The p
70d70 54 61 62 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 Tab member point
70d80 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 74 s to the table t
70d90 68 61 74 20 74 72 69 67 67 65 72 73 20 61 72 65 hat triggers are
70da0 20 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e 2e being coded on.
70db0 20 54 68 65 20 0a 20 2a 20 6e 65 77 49 64 78 20 The . * newIdx
70dc0 6d 65 6d 62 65 72 20 63 6f 6e 74 61 69 6e 73 20 member contains
70dd0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
70de0 20 76 64 62 65 20 63 75 72 73 6f 72 20 74 68 61 vdbe cursor tha
70df0 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 t points at the
70e00 74 65 6d 70 0a 20 2a 20 74 61 62 6c 65 20 74 68 temp. * table th
70e10 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 6e 65 at stores the ne
70e20 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20 w.* references.
70e30 49 66 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e If new.* referen
70e40 63 65 73 20 61 72 65 20 6e 6f 74 20 76 61 6c 69 ces are not vali
70e50 64 0a 20 2a 20 66 6f 72 20 74 68 65 20 74 72 69 d. * for the tri
70e60 67 67 65 72 20 62 65 69 6e 67 20 63 6f 64 65 64 gger being coded
70e70 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e (for example an
70e80 20 4f 4e 20 44 45 4c 45 54 45 20 74 72 69 67 67 ON DELETE trigg
70e90 65 72 29 2c 20 74 68 65 6e 20 6e 65 77 49 64 78 er), then newIdx
70ea0 0a 20 2a 20 69 73 20 73 65 74 20 74 6f 20 2d 31 . * is set to -1
70eb0 2e 20 54 68 65 20 6f 6c 64 49 64 78 20 6d 65 6d . The oldIdx mem
70ec0 62 65 72 20 69 73 20 61 6e 61 6c 6f 67 6f 75 73 ber is analogous
70ed0 20 74 6f 20 6e 65 77 49 64 78 2c 20 66 6f 72 20 to newIdx, for
70ee0 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 old.* references
70ef0 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 4f 4e 20 43 .. *. * The ON C
70f00 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 ONFLICT policy t
70f10 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 o be used for th
70f20 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 e trigger progra
70f30 6d 20 73 74 65 70 73 20 69 73 20 73 74 6f 72 65 m steps is store
70f40 64 20 0a 20 2a 20 61 73 20 74 68 65 20 6f 72 63 d . * as the orc
70f50 6f 6e 66 20 6d 65 6d 62 65 72 2e 20 49 66 20 74 onf member. If t
70f60 68 69 73 20 69 73 20 4f 45 5f 44 65 66 61 75 6c his is OE_Defaul
70f70 74 2c 20 74 68 65 6e 20 74 68 65 20 4f 4e 20 43 t, then the ON C
70f80 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 20 0a ONFLICT clause .
70f90 20 2a 20 73 70 65 63 69 66 69 65 64 20 66 6f 72 * specified for
70fa0 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72 69 67 individual trig
70fb0 67 65 72 73 20 73 74 65 70 73 20 69 73 20 75 73 gers steps is us
70fc0 65 64 2e 0a 20 2a 0a 20 2a 20 73 74 72 75 63 74 ed.. *. * struct
70fd0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 68 61 TriggerStack ha
70fe0 73 20 61 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 s a "pNext" memb
70ff0 65 72 2c 20 74 6f 20 61 6c 6c 6f 77 20 6c 69 6e er, to allow lin
71000 6b 65 64 20 6c 69 73 74 73 20 74 6f 20 62 65 0a ked lists to be.
71010 20 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 * constructed.
71020 57 68 65 6e 20 63 6f 64 69 6e 67 20 6e 65 73 74 When coding nest
71030 65 64 20 74 72 69 67 67 65 72 73 20 28 74 72 69 ed triggers (tri
71040 67 67 65 72 73 20 66 69 72 65 64 20 62 79 20 6f ggers fired by o
71050 74 68 65 72 20 74 72 69 67 67 65 72 73 29 0a 20 ther triggers).
71060 2a 20 65 61 63 68 20 6e 65 73 74 65 64 20 74 72 * each nested tr
71070 69 67 67 65 72 20 73 74 6f 72 65 73 20 69 74 73 igger stores its
71080 20 70 61 72 65 6e 74 20 74 72 69 67 67 65 72 27 parent trigger'
71090 73 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 61 s TriggerStack a
710a0 73 20 74 68 65 20 22 70 4e 65 78 74 22 20 0a 20 s the "pNext" .
710b0 2a 20 70 6f 69 6e 74 65 72 2e 20 4f 6e 63 65 20 * pointer. Once
710c0 74 68 65 20 6e 65 73 74 65 64 20 74 72 69 67 67 the nested trigg
710d0 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 er has been code
710e0 64 2c 20 74 68 65 20 70 4e 65 78 74 20 76 61 6c d, the pNext val
710f0 75 65 20 69 73 20 72 65 73 74 6f 72 65 64 0a 20 ue is restored.
71100 2a 20 74 6f 20 74 68 65 20 70 54 72 69 67 67 65 * to the pTrigge
71110 72 53 74 61 63 6b 20 6d 65 6d 62 65 72 20 6f 66 rStack member of
71120 20 74 68 65 20 50 61 72 73 65 20 73 74 75 63 74 the Parse stuct
71130 75 72 65 20 61 6e 64 20 63 6f 64 69 6e 67 20 6f ure and coding o
71140 66 20 74 68 65 20 70 61 72 65 6e 74 0a 20 2a 20 f the parent. *
71150 74 72 69 67 67 65 72 20 63 6f 6e 74 69 6e 75 65 trigger continue
71160 73 2e 0a 20 2a 0a 20 2a 20 42 65 66 6f 72 65 20 s.. *. * Before
71170 61 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 a nested trigger
71180 20 69 73 20 63 6f 64 65 64 2c 20 74 68 65 20 6c is coded, the l
71190 69 6e 6b 65 64 20 6c 69 73 74 20 70 6f 69 6e 74 inked list point
711a0 65 64 20 74 6f 20 62 79 20 74 68 65 20 0a 20 2a ed to by the . *
711b0 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 20 69 pTriggerStack i
711c0 73 20 73 63 61 6e 6e 65 64 20 74 6f 20 65 6e 73 s scanned to ens
711d0 75 72 65 20 74 68 61 74 20 74 68 65 20 74 72 69 ure that the tri
711e0 67 67 65 72 20 69 73 20 6e 6f 74 20 61 62 6f 75 gger is not abou
711f0 74 20 74 6f 20 62 65 20 63 6f 64 65 64 0a 20 2a t to be coded. *
71200 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 49 66 recursively. If
71210 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 this condition
71220 69 73 20 64 65 74 65 63 74 65 64 2c 20 74 68 65 is detected, the
71230 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 20 nested trigger
71240 69 73 20 6e 6f 74 20 63 6f 64 65 64 2e 0a 20 2a is not coded.. *
71250 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72 /.struct Trigger
71260 53 74 61 63 6b 20 7b 0a 20 20 54 61 62 6c 65 20 Stack {. Table
71270 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f *pTab; /
71280 2a 20 54 61 62 6c 65 20 74 68 61 74 20 74 72 69 * Table that tri
71290 67 67 65 72 73 20 61 72 65 20 63 75 72 72 65 6e ggers are curren
712a0 74 6c 79 20 62 65 69 6e 67 20 63 6f 64 65 64 20 tly being coded
712b0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 on */. int newI
712c0 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 dx; /*
712d0 49 6e 64 65 78 20 6f 66 20 76 64 62 65 20 63 75 Index of vdbe cu
712e0 72 73 6f 72 20 74 6f 20 22 6e 65 77 22 20 74 65 rsor to "new" te
712f0 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e mp table */. in
71300 74 20 6f 6c 64 49 64 78 3b 20 20 20 20 20 20 20 t oldIdx;
71310 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76 /* Index of v
71320 64 62 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6f dbe cursor to "o
71330 6c 64 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a ld" temp table *
71340 2f 0a 20 20 75 33 32 20 6e 65 77 43 6f 6c 4d 61 /. u32 newColMa
71350 73 6b 3b 0a 20 20 75 33 32 20 6f 6c 64 43 6f 6c sk;. u32 oldCol
71360 4d 61 73 6b 3b 0a 20 20 69 6e 74 20 6f 72 63 6f Mask;. int orco
71370 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 nf; /*
71380 43 75 72 72 65 6e 74 20 6f 72 63 6f 6e 66 20 70 Current orconf p
71390 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 olicy */. int i
713a0 67 6e 6f 72 65 4a 75 6d 70 3b 20 20 20 20 20 20 gnoreJump;
713b0 2f 2a 20 77 68 65 72 65 20 74 6f 20 6a 75 6d 70 /* where to jump
713c0 20 74 6f 20 66 6f 72 20 61 20 52 41 49 53 45 28 to for a RAISE(
713d0 49 47 4e 4f 52 45 29 20 2a 2f 0a 20 20 54 72 69 IGNORE) */. Tri
713e0 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 gger *pTrigger;
713f0 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 /* The trigger
71400 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 currently being
71410 20 63 6f 64 65 64 20 2a 2f 0a 20 20 54 72 69 67 coded */. Trig
71420 67 65 72 53 74 61 63 6b 20 2a 70 4e 65 78 74 3b gerStack *pNext;
71430 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72 /* Next trigger
71440 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20 74 72 69 down on the tri
71450 67 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b gger stack */.};
71460 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
71470 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
71480 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 contains informa
71490 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 tion used by the
714a0 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a sqliteFix....**
714b0 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65 routines as the
714c0 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65 y walk the parse
714d0 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61 tree to make da
714e0 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 tabase reference
714f0 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20 s.** explicit.
71500 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
71510 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78 ct DbFixer DbFix
71520 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78 er;.struct DbFix
71530 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 er {. Parse *pP
71540 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 arse; /* Th
71550 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 e parsing contex
71560 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67 t. Error messag
71570 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 es written here
71580 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
71590 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 *zDb; /* Make
715a0 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 sure all object
715b0 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 s are contained
715c0 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 in this database
715d0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
715e0 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 *zType; /* Typ
715f0 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e e of the contain
71600 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 er - used for er
71610 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a ror messages */.
71620 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 const Token *p
71630 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 Name; /* Name of
71640 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d the container -
71650 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 used for error
71660 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a messages */.};..
71670 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65 /*.** An objecte
71680 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75 d used to accumu
71690 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 late the text of
716a0 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 a string where
716b0 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 we.** do not nec
716c0 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f essarily know ho
716d0 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67 w big the string
716e0 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20 will be in the
716f0 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 end..*/.struct S
71700 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69 trAccum {. sqli
71710 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
71720 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 /* Optional dat
71730 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 abase for lookas
71740 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c ide. Can be NUL
71750 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61 L */. char *zBa
71760 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 se; /* A
71770 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e base allocation
71780 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c . Not from mall
71790 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a oc. */. char *z
717a0 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Text; /*
717b0 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c The string coll
717c0 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a ected so far */.
717d0 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20 int nChar;
717e0 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
717f0 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 of the string s
71800 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 o far */. int
71810 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 nAlloc;
71820 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 /* Amount of spa
71830 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 ce allocated in
71840 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20 zText */. int
71850 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 mxAlloc;
71860 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 /* Maximum allow
71870 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 ed string length
71880 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f */. u8 mallo
71890 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65 cFailed; /* Be
718a0 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e comes true if an
718b0 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 y memory allocat
718c0 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75 ion fails */. u
718d0 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 8 useMalloc;
718e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a /* True if z
718f0 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 61 62 Text is enlargab
71900 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 le using realloc
71910 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42 69 */. u8 tooBi
71920 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 g; /* Be
71930 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73 74 comes true if st
71940 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65 64 ring size exceed
71950 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a 0a s limits */.};..
71960 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 /*.** A pointer
71970 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
71980 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d e is used to com
71990 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d 61 municate informa
719a0 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c tion.** from sql
719b0 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f ite3Init and OP_
719c0 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 6f ParseSchema into
719d0 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 74 the sqlite3Init
719e0 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70 Callback..*/.typ
719f0 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 20 edef struct {.
71a00 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
71a10 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
71a20 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 ase being initia
71a30 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 lized */. int i
71a40 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f Db; /
71a50 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 74 * 0 for main dat
71a60 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 45 abase. 1 for TE
71a70 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 41 MP, 2.. for ATTA
71a80 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a CHed */. char *
71a90 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a *pzErrMsg; /*
71aa0 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 Error message s
71ab0 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 tored here */.
71ac0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
71ad0 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f /* Result co
71ae0 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a de stored here *
71af0 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f /.} InitData;../
71b00 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 63 *.** Structure c
71b10 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c ontaining global
71b20 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 64 configuration d
71b30 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c 69 ata for the SQLi
71b40 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a te library..**.*
71b50 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 * This structure
71b60 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73 also contains s
71b70 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 6d ome state inform
71b80 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ation..*/.struct
71b90 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 7b Sqlite3Config {
71ba0 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74 3b . int bMemstat;
71bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71bc0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
71bd0 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 74 enable memory st
71be0 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62 43 atus */. int bC
71bf0 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20 20 oreMutex;
71c00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
71c10 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63 6f rue to enable co
71c20 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 re mutexing */.
71c30 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78 3b int bFullMutex;
71c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71c50 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e /* True to en
71c60 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78 69 able full mutexi
71c70 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 74 ng */. int mxSt
71c80 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 rlen;
71c90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
71ca0 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e 67 imum string leng
71cb0 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c 6f th */. int szLo
71cc0 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 okaside;
71cd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 /* Def
71ce0 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 ault lookaside b
71cf0 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20 uffer size */.
71d00 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b 20 int nLookaside;
71d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71d20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f /* Default loo
71d30 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 63 6f kaside buffer co
71d40 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 unt */. sqlite3
71d50 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 20 _mem_methods m;
71d60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
71d70 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
71d80 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72 66 llocation interf
71d90 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ace */. sqlite3
71da0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 6d _mutex_methods m
71db0 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c 6f utex; /* Lo
71dc0 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69 6e w-level mutex in
71dd0 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 terface */. voi
71de0 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20 d *pHeap;
71df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
71e00 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73 * Heap storage s
71e10 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 pace */. int nH
71e20 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 eap;
71e30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
71e40 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a ize of pHeap[] *
71e50 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d /. int mnReq, m
71e60 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 xReq;
71e70 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 /* Min and
71e80 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73 max heap reques
71e90 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f ts sizes */. vo
71ea0 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20 id *pScratch;
71eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71ec0 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 /* Scratch memor
71ed0 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 y */. int szScr
71ee0 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
71ef0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
71f00 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68 of each scratch
71f10 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 buffer */. int
71f20 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 nScratch;
71f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
71f40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 * Number of scra
71f50 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 tch buffers */.
71f60 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20 void *pPage;
71f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71f80 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65 /* Page cache
71f90 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 memory */. int
71fa0 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 szPage;
71fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
71fc0 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70 * Size of each p
71fd0 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a age in pPage[] *
71fe0 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 /. int nPage;
71ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72000 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
72010 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67 of pages in pPag
72020 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 e[] */. int isI
72030 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 nit;
72040 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
72050 75 65 20 61 66 74 65 72 20 69 6e 69 74 69 61 6c ue after initial
72060 69 7a 61 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 ization has fini
72070 73 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e shed */. int in
72080 50 72 6f 67 72 65 73 73 3b 20 20 20 20 20 20 20 Progress;
72090 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
720a0 72 75 65 20 77 68 69 6c 65 20 69 6e 69 74 69 61 rue while initia
720b0 6c 69 7a 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67 lization in prog
720c0 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 ress */. int is
720d0 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 20 20 20 MallocInit;
720e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
720f0 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 rue after malloc
72100 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 is initialized
72110 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
72120 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 ex *pInitMutex;
72130 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 /* Mutex
72140 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f used by sqlite3_
72150 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a initialize() */.
72160 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 int nRefInitMu
72170 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 tex;
72180 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
72190 20 75 73 65 72 73 20 6f 66 20 70 49 6e 69 74 4d users of pInitM
721a0 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 utex */. int nS
721b0 6d 61 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 mall;
721c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 /* a
721d0 6c 6c 6f 63 20 73 69 7a 65 20 74 68 72 65 73 68 lloc size thresh
721e0 6f 6c 64 20 75 73 65 64 20 62 79 20 6d 65 6d 36 old used by mem6
721f0 2e 63 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 .c */. int mxPa
72200 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 20 rserStack;
72210 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 78 /* max
72220 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 imum depth of th
72230 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 2a e parser stack *
72240 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 61 /. int sharedCa
72250 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 cheEnabled;
72260 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 /* true if
72270 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f shared-cache mo
72280 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 7d 3b de enabled */.};
72290 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 ../*.** Context
722a0 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 20 64 pointer passed d
722b0 6f 77 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 own through the
722c0 74 72 65 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 tree-walk..*/.st
722d0 72 75 63 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 ruct Walker {.
722e0 69 6e 74 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 int (*xExprCallb
722f0 61 63 6b 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 ack)(Walker*, Ex
72300 70 72 2a 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c pr*); /* Cal
72310 6c 62 61 63 6b 20 66 6f 72 20 65 78 70 72 65 73 lback for expres
72320 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 sions */. int (
72330 2a 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b *xSelectCallback
72340 29 28 57 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 )(Walker*,Select
72350 2a 29 3b 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b *); /* Callback
72360 20 66 6f 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a for SELECTs */.
72370 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b Parse *pParse;
72380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72390 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
723a0 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 arser context.
723b0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 */. union {
723c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
723d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
723e0 2a 20 45 78 74 72 61 20 64 61 74 61 20 66 6f 72 * Extra data for
723f0 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 callback */.
72400 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e NameContext *pN
72410 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 C;
72420 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
72430 61 6d 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f aming context */
72440 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 . int i;
72450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72470 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 /* Integer value
72480 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f */. } u;.};../
72490 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 * Forward declar
724a0 61 74 69 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 ations */.SQLITE
724b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
724c0 69 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c ite3WalkExpr(Wal
724d0 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 ker*, Expr*);.SQ
724e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
724f0 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
72500 4c 69 73 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 List(Walker*, Ex
72510 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 prList*);.SQLITE
72520 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72530 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 ite3WalkSelect(W
72540 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 alker*, Select*)
72550 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72560 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b int sqlite3Walk
72570 53 65 6c 65 63 74 45 78 70 72 28 57 61 6c 6b 65 SelectExpr(Walke
72580 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 r*, Select*);.SQ
72590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
725a0 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
725b0 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 ctFrom(Walker*,
725c0 53 65 6c 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a Select*);../*.**
725d0 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f Return code fro
725e0 6d 20 74 68 65 20 70 61 72 73 65 2d 74 72 65 65 m the parse-tree
725f0 20 77 61 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 walking primiti
72600 76 65 73 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a ves and their.**
72610 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 callbacks..*/.#
72620 64 65 66 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 define WRC_Conti
72630 6e 75 65 20 20 20 20 30 0a 23 64 65 66 69 6e 65 nue 0.#define
72640 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 WRC_Prune
72650 20 31 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41 1.#define WRC_A
72660 62 6f 72 74 20 20 20 20 20 20 20 32 0a 0a 2f 2a bort 2../*
72670 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e .** Assuming zIn
72680 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 points to the f
72690 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20 55 irst byte of a U
726a0 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c 0a TF-8 character,.
726b0 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 ** advance zIn t
726c0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 o point to the f
726d0 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 irst byte of the
726e0 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61 72 next UTF-8 char
726f0 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e acter..*/.#defin
72700 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 e SQLITE_SKIP_UT
72710 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 F8(zIn) {
72720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72730 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b \. if( (*(zIn+
72740 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 +))>=0xc0 ){
72750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72760 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
72770 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 while( (*zIn & 0
72780 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 xc0)==0x80 ){ zI
72790 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 20 n++; }
727a0 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 \. }
727b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
727c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
727d0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a \.}.
727e0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 ./*.** The SQLIT
727f0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d E_CORRUPT_BKPT m
72800 61 63 72 6f 20 63 61 6e 20 62 65 20 65 69 74 68 acro can be eith
72810 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 er a constant (f
72820 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a or production.**
72830 20 62 75 69 6c 64 73 29 20 6f 72 20 61 20 66 75 builds) or a fu
72840 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 nction call (for
72850 20 64 65 62 75 67 67 69 6e 67 29 2e 20 20 49 66 debugging). If
72860 20 69 74 20 69 73 20 61 20 66 75 6e 63 74 69 6f it is a functio
72870 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c n call,.** it al
72880 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 61 74 6f lows the operato
72890 72 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b r to set a break
728a0 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 70 6f point at the spo
728b0 74 20 77 68 65 72 65 20 64 61 74 61 62 61 73 65 t where database
728c0 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 .** corruption i
728d0 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65 64 s first detected
728e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
728f0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
72900 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
72910 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69 lite3Corrupt(voi
72920 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c d);.# define SQL
72930 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
72940 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 sqlite3Corrupt(
72950 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
72960 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
72970 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 BKPT SQLITE_CORR
72980 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a UPT.#endif../*.*
72990 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 * Internal funct
729a0 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a ion prototypes.*
729b0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
729c0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 int sqlite3StrI
729d0 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a Cmp(const char *
729e0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b , const char *);
729f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72a00 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 4e 49 int sqlite3StrNI
72a10 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a Cmp(const char *
72a20 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
72a30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
72a40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72a50 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 IsNumber(const c
72a60 68 61 72 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b har*, int*, u8);
72a70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72a80 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 int sqlite3Strle
72a90 6e 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 n(sqlite3*, cons
72aa0 74 20 63 68 61 72 2a 29 3b 0a 0a 53 51 4c 49 54 t char*);..SQLIT
72ab0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
72ac0 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 lite3MallocInit(
72ad0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
72ae0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
72af0 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 e3MallocEnd(void
72b00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72b10 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d E void *sqlite3M
72b20 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 alloc(int);.SQLI
72b30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
72b40 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 *sqlite3MallocZe
72b50 72 6f 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f ro(int);.SQLITE_
72b60 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
72b70 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
72b80 6f 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 o(sqlite3*, int)
72b90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72ba0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
72bb0 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 MallocRaw(sqlite
72bc0 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 3*, int);.SQLITE
72bd0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
72be0 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 73 qlite3DbStrDup(s
72bf0 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 qlite3*,const ch
72c00 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
72c10 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
72c20 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 e3DbStrNDup(sqli
72c30 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a te3*,const char*
72c40 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
72c50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
72c60 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 ite3Realloc(void
72c70 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
72c80 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
72c90 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 lite3DbReallocOr
72ca0 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 Free(sqlite3 *,
72cb0 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 void *, int);.SQ
72cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
72cd0 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c d *sqlite3DbReal
72ce0 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 loc(sqlite3 *, v
72cf0 6f 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c oid *, int);.SQL
72d00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
72d10 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 73 sqlite3DbFree(s
72d20 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b qlite3*, void*);
72d30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72d40 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f int sqlite3Mallo
72d50 63 53 69 7a 65 28 76 6f 69 64 2a 29 3b 0a 53 51 cSize(void*);.SQ
72d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
72d70 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
72d80 53 69 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 Size(sqlite3*, v
72d90 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 oid*);.SQLITE_PR
72da0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
72db0 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 te3ScratchMalloc
72dc0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
72dd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
72de0 65 33 53 63 72 61 74 63 68 46 72 65 65 28 76 6f e3ScratchFree(vo
72df0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 id*);.SQLITE_PRI
72e00 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
72e10 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 e3PageMalloc(int
72e20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72e30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
72e40 67 65 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 geFree(void*);.S
72e50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
72e60 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 id sqlite3MemSet
72e70 44 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 53 Default(void);.S
72e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
72e90 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
72ea0 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
72eb0 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f MemGetMemsys3(vo
72ec0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
72ed0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
72ee0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
72ef0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 qlite3MemGetMems
72f00 79 73 35 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ys5(void);.SQLIT
72f10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
72f20 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c qlite3BenignMall
72f30 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 ocHooks(void (*)
72f40 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 (void), void (*)
72f50 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f (void));.SQLITE_
72f60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72f70 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 te3MemoryAlarm(v
72f80 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 oid (*)(void*, s
72f90 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e qlite3_int64, in
72fa0 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 t), void*, sqlit
72fb0 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 23 69 66 6e e3_int64);..#ifn
72fc0 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 def SQLITE_MUTEX
72fd0 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f 50 52 49 _OMIT.SQLITE_PRI
72fe0 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 5f 6d VATE sqlite3_m
72ff0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 utex_methods *sq
73000 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 lite3DefaultMute
73010 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f x(void);.SQLITE_
73020 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 PRIVATE sqlite
73030 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 3_mutex *sqlite3
73040 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 29 3b MutexAlloc(int);
73050 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73060 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 int sqlite3Mut
73070 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 exInit(void);.SQ
73080 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
73090 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 nt sqlite3MutexE
730a0 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 nd(void);.#endif
730b0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
730c0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 int sqlite3Stat
730d0 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a 53 51 usValue(int);.SQ
730e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
730f0 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 d sqlite3StatusA
73100 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 dd(int, int);.SQ
73110 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
73120 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 d sqlite3StatusS
73130 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 et(int, int);..S
73140 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73150 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 64 t sqlite3IsNaN(d
73160 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f ouble);..SQLITE_
73170 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
73180 69 74 65 33 56 58 50 72 69 6e 74 66 28 53 74 72 ite3VXPrintf(Str
73190 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 6f 6e Accum*, int, con
731a0 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 st char*, va_lis
731b0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
731c0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
731d0 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a MPrintf(sqlite3*
731e0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e ,const char*, ..
731f0 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .);.SQLITE_PRIVA
73200 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
73210 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 VMPrintf(sqlite3
73220 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 *,const char*, v
73230 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f a_list);.SQLITE_
73240 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
73250 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 lite3MAppendf(sq
73260 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e lite3*,char*,con
73270 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 st char*,...);.#
73280 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
73290 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e E_TEST) || defin
732a0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
732b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
732c0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 void sqlite3De
732d0 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 bugPrintf(const
732e0 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e char*, ...);.#en
732f0 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 dif.#if defined(
73300 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c SQLITE_TEST).SQL
73310 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
73320 69 64 20 2a 73 71 6c 69 74 65 33 54 65 73 74 54 id *sqlite3TestT
73330 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 74 20 63 extToPtr(const c
73340 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 har*);.#endif.SQ
73350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
73360 64 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 d sqlite3SetStri
73370 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73 71 6c 69 ng(char **, sqli
73380 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 te3*, const char
73390 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f *, ...);.SQLITE_
733a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
733b0 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 ite3ErrorMsg(Par
733c0 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a se*, const char*
733d0 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 , ...);.SQLITE_P
733e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
733f0 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 te3ErrorClear(Pa
73400 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 rse*);.SQLITE_PR
73410 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
73420 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 2a 29 e3Dequote(char*)
73430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73440 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 71 void sqlite3Deq
73450 75 6f 74 65 45 78 70 72 28 73 71 6c 69 74 65 33 uoteExpr(sqlite3
73460 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
73470 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
73480 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 lite3KeywordCode
73490 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
734a0 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c char*, int);.SQL
734b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
734c0 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 sqlite3RunParser
734d0 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 (Parse*, const c
734e0 68 61 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a har*, char **);.
734f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73500 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 oid sqlite3Finis
73510 68 43 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b hCoding(Parse*);
73520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73530 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 int sqlite3GetTe
73540 6d 70 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53 mpReg(Parse*);.S
73550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73560 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 id sqlite3Releas
73570 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c eTempReg(Parse*,
73580 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
73590 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
735a0 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 GetTempRange(Par
735b0 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 se*,int);.SQLITE
735c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
735d0 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
735e0 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 Range(Parse*,int
735f0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
73600 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
73610 74 65 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a te3Expr(sqlite3*
73620 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78 , int, Expr*, Ex
73630 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e pr*, const Token
73640 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73650 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
73660 50 45 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e PExpr(Parse*, in
73670 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c t, Expr*, Expr*,
73680 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a const Token*);.
73690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
736a0 78 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69 xpr *sqlite3Regi
736b0 73 74 65 72 45 78 70 72 28 50 61 72 73 65 2a 2c sterExpr(Parse*,
736c0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
736d0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
736e0 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c lite3ExprAnd(sql
736f0 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78 70 ite3*,Expr*, Exp
73700 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
73710 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73720 45 78 70 72 53 70 61 6e 28 45 78 70 72 2a 2c 54 ExprSpan(Expr*,T
73730 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 oken*,Token*);.S
73740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
73750 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 46 pr *sqlite3ExprF
73760 75 6e 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 45 unction(Parse*,E
73770 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a xprList*, Token*
73780 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73790 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
737a0 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 prAssignVarNumbe
737b0 72 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 r(Parse*, Expr*)
737c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
737d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
737e0 72 43 6c 65 61 72 28 73 71 6c 69 74 65 33 2a 2c rClear(sqlite3*,
737f0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
73800 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
73810 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 73 ite3ExprDelete(s
73820 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b qlite3*, Expr*);
73830 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73840 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 ExprList *sqlite
73850 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 3ExprListAppend(
73860 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a Parse*,ExprList*
73870 2c 45 78 70 72 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a ,Expr*,Token*);.
73880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73890 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c oid sqlite3ExprL
738a0 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 istDelete(sqlite
738b0 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 3*, ExprList*);.
738c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
738d0 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 nt sqlite3Init(s
738e0 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29 qlite3*, char**)
738f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73900 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 int sqlite3Init
73910 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20 Callback(void*,
73920 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61 int, char**, cha
73930 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r**);.SQLITE_PRI
73940 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
73950 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54 3Pragma(Parse*,T
73960 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b oken*,Token*,Tok
73970 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 en*,int);.SQLITE
73980 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
73990 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e lite3ResetIntern
739a0 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 alSchema(sqlite3
739b0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
739c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
739d0 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 ite3BeginParse(P
739e0 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 arse*,int);.SQLI
739f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73a00 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 sqlite3CommitInt
73a10 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c ernalChanges(sql
73a20 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
73a30 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 RIVATE Table *sq
73a40 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 lite3ResultSetOf
73a50 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 53 65 Select(Parse*,Se
73a60 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 lect*);.SQLITE_P
73a70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
73a80 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 te3OpenMasterTab
73a90 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 29 le(Parse *, int)
73aa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73ab0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 void sqlite3Sta
73ac0 72 74 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 rtTable(Parse*,T
73ad0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 oken*,Token*,int
73ae0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
73af0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73b00 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c id sqlite3AddCol
73b10 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e umn(Parse*,Token
73b20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73b30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
73b40 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a ddNotNull(Parse*
73b50 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
73b60 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
73b70 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 te3AddPrimaryKey
73b80 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 (Parse*, ExprLis
73b90 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e t*, int, int, in
73ba0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
73bb0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
73bc0 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e ddCheckConstrain
73bd0 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 t(Parse*, Expr*)
73be0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73bf0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
73c00 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 ColumnType(Parse
73c10 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 *,Token*);.SQLIT
73c20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
73c30 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 qlite3AddDefault
73c40 56 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 78 70 Value(Parse*,Exp
73c50 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
73c60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73c70 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 AddCollateType(P
73c80 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a arse*, Token*);.
73c90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73ca0 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61 oid sqlite3EndTa
73cb0 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e ble(Parse*,Token
73cc0 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a *,Token*,Select*
73cd0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
73ce0 54 45 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74 TE Bitvec *sqlit
73cf0 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 75 e3BitvecCreate(u
73d00 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 32);.SQLITE_PRIV
73d10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
73d20 69 74 76 65 63 54 65 73 74 28 42 69 74 76 65 63 itvecTest(Bitvec
73d30 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f *, u32);.SQLITE_
73d40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73d50 74 65 33 42 69 74 76 65 63 53 65 74 28 42 69 74 te3BitvecSet(Bit
73d60 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 vec*, u32);.SQLI
73d70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73d80 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 sqlite3BitvecCle
73d90 61 72 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 ar(Bitvec*, u32)
73da0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73db0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 void sqlite3Bit
73dc0 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 65 vecDestroy(Bitve
73dd0 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 c*);.SQLITE_PRIV
73de0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
73df0 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 itvecBuiltinTest
73e00 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c (int,int*);..SQL
73e10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73e20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 sqlite3CreateVi
73e30 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a ew(Parse*,Token*
73e40 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 ,Token*,Token*,S
73e50 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b elect*,int,int);
73e60 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
73e70 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 QLITE_OMIT_VIEW)
73e80 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
73e90 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
73ea0 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 TABLE).SQLITE_PR
73eb0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
73ec0 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e te3ViewGetColumn
73ed0 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 61 62 Names(Parse*,Tab
73ee0 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 le*);.#else.# de
73ef0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 69 65 77 fine sqlite3View
73f00 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 GetColumnNames(A
73f10 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 ,B) 0.#endif..SQ
73f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
73f30 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 d sqlite3DropTab
73f40 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 le(Parse*, SrcLi
73f50 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a st*, int, int);.
73f60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73f70 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 oid sqlite3Delet
73f80 65 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a eTable(Table*);.
73f90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73fa0 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73 65 72 oid sqlite3Inser
73fb0 74 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 t(Parse*, SrcLis
73fc0 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 53 t*, ExprList*, S
73fd0 65 6c 65 63 74 2a 2c 20 49 64 4c 69 73 74 2a 2c elect*, IdList*,
73fe0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
73ff0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
74000 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 te3ArrayAllocate
74010 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c (sqlite3*,void*,
74020 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 int,int,int*,int
74030 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f *,int*);.SQLITE_
74040 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a PRIVATE IdList *
74050 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 sqlite3IdListApp
74060 65 6e 64 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 end(sqlite3*, Id
74070 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a List*, Token*);.
74080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74090 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 nt sqlite3IdList
740a0 49 6e 64 65 78 28 49 64 4c 69 73 74 2a 2c 63 6f Index(IdList*,co
740b0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
740c0 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 TE_PRIVATE SrcLi
740d0 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 st *sqlite3SrcLi
740e0 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 65 33 stAppend(sqlite3
740f0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b *, SrcList*, Tok
74100 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 en*, Token*);.SQ
74110 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 LITE_PRIVATE Src
74120 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 List *sqlite3Src
74130 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 ListAppendFromTe
74140 72 6d 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 rm(Parse*, SrcLi
74150 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b st*, Token*, Tok
74160 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 en*,.
74170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
74180 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b 65 6e Token
74190 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 *, Select*, Expr
741a0 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c *, IdList*);.SQL
741b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
741c0 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 sqlite3SrcListI
741d0 6e 64 65 78 65 64 42 79 28 50 61 72 73 65 20 2a ndexedBy(Parse *
741e0 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 54 6f 6b , SrcList *, Tok
741f0 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 en *);.SQLITE_PR
74200 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
74210 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 3IndexedByLookup
74220 28 50 61 72 73 65 20 2a 2c 20 73 74 72 75 63 74 (Parse *, struct
74230 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 29 SrcList_item *)
74240 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74250 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 void sqlite3Src
74260 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 ListShiftJoinTyp
74270 65 28 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c e(SrcList*);.SQL
74280 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74290 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 sqlite3SrcListA
742a0 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50 61 72 ssignCursors(Par
742b0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a se*, SrcList*);.
742c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
742d0 6f 69 64 20 73 71 6c 69 74 65 33 49 64 4c 69 73 oid sqlite3IdLis
742e0 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a tDelete(sqlite3*
742f0 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 , IdList*);.SQLI
74300 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
74310 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 sqlite3SrcListDe
74320 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 lete(sqlite3*, S
74330 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 rcList*);.SQLITE
74340 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74350 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 lite3CreateIndex
74360 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 (Parse*,Token*,T
74370 6f 6b 65 6e 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 oken*,SrcList*,E
74380 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 54 6f 6b xprList*,int,Tok
74390 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 en*,.
743a0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b Tok
743b0 65 6e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a en*, int, int);.
743c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
743d0 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49 oid sqlite3DropI
743e0 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 53 72 63 ndex(Parse*, Src
743f0 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c List*, int);.SQL
74400 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
74410 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 50 61 sqlite3Select(Pa
74420 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 53 rse*, Select*, S
74430 65 6c 65 63 74 44 65 73 74 2a 29 3b 0a 53 51 4c electDest*);.SQL
74440 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 ITE_PRIVATE Sele
74450 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 ct *sqlite3Selec
74460 74 4e 65 77 28 50 61 72 73 65 2a 2c 45 78 70 72 tNew(Parse*,Expr
74470 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 List*,SrcList*,E
74480 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 0a xpr*,ExprList*,.
74490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
744a0 20 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 45 Expr*,E
744b0 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 45 78 70 xprList*,int,Exp
744c0 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 r*,Expr*);.SQLIT
744d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
744e0 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
744f0 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c te(sqlite3*, Sel
74500 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ect*);.SQLITE_PR
74510 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c IVATE Table *sql
74520 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 ite3SrcListLooku
74530 70 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 p(Parse*, SrcLis
74540 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
74550 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
74560 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73 65 2a sReadOnly(Parse*
74570 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a , Table*, int);.
74580 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74590 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 oid sqlite3OpenT
745a0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 able(Parse*, int
745b0 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62 2c 20 iCur, int iDb,
745c0 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 Table*, int);.#i
745d0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
745e0 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 _ENABLE_UPDATE_D
745f0 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 ELETE_LIMIT) &&
74600 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
74610 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 53 OMIT_SUBQUERY).S
74620 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
74630 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d 69 74 pr *sqlite3Limit
74640 57 68 65 72 65 28 50 61 72 73 65 20 2a 2c 20 53 Where(Parse *, S
74650 72 63 4c 69 73 74 20 2a 2c 20 45 78 70 72 20 2a rcList *, Expr *
74660 2c 20 45 78 70 72 4c 69 73 74 20 2a 2c 20 45 78 , ExprList *, Ex
74670 70 72 20 2a 2c 20 45 78 70 72 20 2a 2c 20 63 68 pr *, Expr *, ch
74680 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 ar *);.#endif.SQ
74690 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
746a0 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46 d sqlite3DeleteF
746b0 72 6f 6d 28 50 61 72 73 65 2a 2c 20 53 72 63 4c rom(Parse*, SrcL
746c0 69 73 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 ist*, Expr*);.SQ
746d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
746e0 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 d sqlite3Update(
746f0 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a Parse*, SrcList*
74700 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 , ExprList*, Exp
74710 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
74720 5f 50 52 49 56 41 54 45 20 57 68 65 72 65 49 6e _PRIVATE WhereIn
74730 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65 fo *sqlite3Where
74740 42 65 67 69 6e 28 50 61 72 73 65 2a 2c 20 53 72 Begin(Parse*, Sr
74750 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 45 cList*, Expr*, E
74760 78 70 72 4c 69 73 74 2a 2a 2c 20 75 38 29 3b 0a xprList**, u8);.
74770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74780 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72 65 oid sqlite3Where
74790 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a 29 3b End(WhereInfo*);
747a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
747b0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
747c0 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50 61 72 odeGetColumn(Par
747d0 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 se*, Table*, int
747e0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 , int, int, int)
747f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74800 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
74810 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 2a rCodeMove(Parse*
74820 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 , int, int, int)
74830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
74850 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65 2a rCodeCopy(Parse*
74860 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 , int, int, int)
74870 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74880 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
74890 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 rClearColumnCach
748a0 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a e(Parse*, int);.
748b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
748c0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
748d0 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e acheAffinityChan
748e0 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 ge(Parse*, int,
748f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
74900 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
74910 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 69 ExprWritableRegi
74920 73 74 65 72 28 50 61 72 73 65 2a 2c 69 6e 74 2c ster(Parse*,int,
74930 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
74940 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74950 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 3ExprHardCopy(Pa
74960 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 rse*,int,int);.S
74970 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
74980 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
74990 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c e(Parse*, Expr*,
749a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
749b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
749c0 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 3ExprCodeTemp(Pa
749d0 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
749e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
749f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
74a00 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 prCodeTarget(Par
74a10 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 se*, Expr*, int)
74a20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74a30 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
74a40 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 CodeAndCache(Par
74a50 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 se*, Expr*, int)
74a60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74a70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
74a80 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 rCodeConstants(P
74a90 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 arse*, Expr*);.S
74aa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
74ab0 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
74ac0 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a eExprList(Parse*
74ad0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 , ExprList*, int
74ae0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
74af0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74b00 74 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61 te3ExprIfTrue(Pa
74b10 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
74b20 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
74b30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74b40 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 te3ExprIfFalse(P
74b50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e arse*, Expr*, in
74b60 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
74b70 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 PRIVATE Table *s
74b80 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
74b90 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
74ba0 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 har*, const char
74bb0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74bc0 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 TE Table *sqlite
74bd0 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 3LocateTable(Par
74be0 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 se*,int isView,c
74bf0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 onst char*, cons
74c00 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
74c10 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a _PRIVATE Index *
74c20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 sqlite3FindIndex
74c30 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 (sqlite3*,const
74c40 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 char*, const cha
74c50 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
74c60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
74c70 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 UnlinkAndDeleteT
74c80 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e able(sqlite3*,in
74c90 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a t,const char*);.
74ca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74cb0 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e oid sqlite3Unlin
74cc0 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 kAndDeleteIndex(
74cd0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e sqlite3*,int,con
74ce0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
74cf0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
74d00 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 qlite3Vacuum(Par
74d10 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
74d20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
74d30 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a RunVacuum(char**
74d40 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c , sqlite3*);.SQL
74d50 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
74d60 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f *sqlite3NameFro
74d70 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c mToken(sqlite3*,
74d80 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
74d90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
74da0 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 ite3ExprCompare(
74db0 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 Expr*, Expr*);.S
74dc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74dd0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e id sqlite3ExprAn
74de0 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 alyzeAggregates(
74df0 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 NameContext*, Ex
74e00 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
74e10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74e20 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 3ExprAnalyzeAggL
74e30 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a ist(NameContext*
74e40 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c ,ExprList*);.SQL
74e50 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 ITE_PRIVATE Vdbe
74e60 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 *sqlite3GetVdbe
74e70 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
74e80 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
74e90 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78 qlite3CreateIdEx
74ea0 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 pr(Parse *, cons
74eb0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
74ec0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74ed0 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 lite3PrngSaveSta
74ee0 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 te(void);.SQLITE
74ef0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74f00 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 lite3PrngRestore
74f10 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c State(void);.SQL
74f20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74f30 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 sqlite3PrngRese
74f40 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 tState(void);.SQ
74f50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
74f60 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 d sqlite3Rollbac
74f70 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a kAll(sqlite3*);.
74f80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74f90 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 oid sqlite3CodeV
74fa0 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 erifySchema(Pars
74fb0 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 e*, int);.SQLITE
74fc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74fd0 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 lite3BeginTransa
74fe0 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e ction(Parse*, in
74ff0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
75000 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
75010 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e ommitTransaction
75020 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
75030 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
75040 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 lite3RollbackTra
75050 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 nsaction(Parse*)
75060 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75070 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
75080 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 2a IsConstant(Expr*
75090 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
750a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
750b0 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f rIsConstantNotJo
750c0 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 in(Expr*);.SQLIT
750d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
750e0 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
750f0 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 antOrFunction(Ex
75100 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
75110 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
75120 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78 ExprIsInteger(Ex
75130 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 pr*, int*);.SQLI
75140 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
75150 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f qlite3IsRowid(co
75160 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
75170 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
75180 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 sqlite3GenerateR
75190 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65 2a 2c owDelete(Parse*,
751a0 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e Table*, int, in
751b0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
751c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
751d0 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49 ite3GenerateRowI
751e0 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 73 65 ndexDelete(Parse
751f0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 *, Table*, int,
75200 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
75210 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
75220 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 3GenerateIndexKe
75230 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 78 2a y(Parse*, Index*
75240 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 , int, int, int)
75250 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75260 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e void sqlite3Gen
75270 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 erateConstraintC
75280 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54 61 62 hecks(Parse*,Tab
75290 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20 20 20 le*,int,int,.
752a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
752b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
752c0 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c 69 int*,int,int,i
752d0 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
752e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
752f0 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 ite3CompleteInse
75300 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 54 61 rtion(Parse*, Ta
75310 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 ble*, int, int,
75320 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 int*,int,int,int
75330 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
75340 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
75350 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 3OpenTableAndInd
75360 69 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 62 ices(Parse*, Tab
75370 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a le*, int, int);.
75380 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
75390 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e oid sqlite3Begin
753a0 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 WriteOperation(P
753b0 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 arse*, int, int)
753c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
753d0 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
753e0 70 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 prDup(sqlite3*,E
753f0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
75400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
75410 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69 e3TokenCopy(sqli
75420 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 54 6f 6b te3*,Token*, Tok
75430 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
75440 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 VATE ExprList *s
75450 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 qlite3ExprListDu
75460 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c p(sqlite3*,ExprL
75470 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
75480 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 IVATE SrcList *s
75490 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 qlite3SrcListDup
754a0 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69 73 (sqlite3*,SrcLis
754b0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
754c0 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 ATE IdList *sqli
754d0 74 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c te3IdListDup(sql
754e0 69 74 65 33 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a ite3*,IdList*);.
754f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
75500 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 elect *sqlite3Se
75510 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 2a lectDup(sqlite3*
75520 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 ,Select*);.SQLIT
75530 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
75540 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 qlite3FuncDefIns
75550 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 2a ert(FuncDefHash*
75560 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c , FuncDef*);.SQL
75570 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 ITE_PRIVATE Func
75580 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 Def *sqlite3Find
75590 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 Function(sqlite3
755a0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e *,const char*,in
755b0 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a 53 t,int,u8,int);.S
755c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
755d0 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 id sqlite3Regist
755e0 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f erBuiltinFunctio
755f0 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 ns(sqlite3*);.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 52 65 67 69 73 74 65 d sqlite3Registe
75620 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f rDateTimeFunctio
75630 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 ns(void);.SQLITE
75640 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
75650 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f lite3RegisterGlo
75660 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 balFunctions(voi
75670 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
75680 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
75690 74 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e tBuiltinFunction
756a0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 (const char *, i
756b0 6e 74 2c 20 46 75 6e 63 44 65 66 20 2a 2a 29 3b nt, FuncDef **);
756c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
756d0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
756e0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
756f0 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 3SafetyOn(sqlite
75700 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 3*);.SQLITE_PRIV
75710 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
75720 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 3SafetyOff(sqlit
75730 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 e3*);.#else.# de
75740 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 fine sqlite3Safe
75750 74 79 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 tyOn(A) 0.# defi
75760 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 ne sqlite3Safety
75770 4f 66 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a Off(A) 0.#endif.
75780 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
75790 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 nt sqlite3Safety
757a0 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a CheckOk(sqlite3*
757b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
757c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
757d0 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b etyCheckSickOrOk
757e0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
757f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
75800 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f sqlite3ChangeCoo
75810 6b 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 kie(Parse*, int)
75820 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 ;..#if !defined(
75830 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
75840 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
75850 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
75860 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 R).SQLITE_PRIVAT
75870 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 E void sqlite3Ma
75880 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 terializeView(Pa
75890 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 rse*, Table*, Ex
758a0 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 pr*, int);.#endi
758b0 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
758c0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 E_OMIT_TRIGGER.S
758d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
758e0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 void sqlite3Begi
758f0 6e 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c nTrigger(Parse*,
75900 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 Token*,Token*,i
75910 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 nt,int,IdList*,S
75920 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 rcList*,.
75930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75940 20 20 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 Expr*,int, i
75950 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
75960 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
75970 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 e3FinishTrigger(
75980 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 Parse*, TriggerS
75990 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 tep*, Token*);.S
759a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
759b0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 void sqlite3Drop
759c0 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 Trigger(Parse*,
759d0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a SrcList*, int);.
759e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
759f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f void sqlite3Dro
75a00 70 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73 pTriggerPtr(Pars
75a10 65 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 e*, Trigger*);.S
75a20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
75a30 69 6e 74 20 73 71 6c 69 74 65 33 54 72 69 67 67 int sqlite3Trigg
75a40 65 72 73 45 78 69 73 74 28 50 61 72 73 65 2a 2c ersExist(Parse*,
75a50 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 45 78 Table*, int, Ex
75a60 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 prList*);.SQLITE
75a70 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
75a80 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 qlite3CodeRowTri
75a90 67 67 65 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 gger(Parse*, int
75aa0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 , ExprList*, int
75ab0 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 , Table *, int,
75ac0 69 6e 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 int, .
75ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75ae0 20 69 6e 74 2c 20 69 6e 74 2c 20 75 33 32 2a 2c int, int, u32*,
75af0 20 75 33 32 2a 29 3b 0a 20 20 76 6f 69 64 20 73 u32*);. void s
75b00 71 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 qliteViewTrigger
75b10 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a s(Parse*, Table*
75b20 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 , Expr*, int, Ex
75b30 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 prList*);.SQLITE
75b40 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
75b50 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 sqlite3DeleteTri
75b60 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 ggerStep(sqlite3
75b70 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 *, TriggerStep*)
75b80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75b90 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a TriggerStep *
75ba0 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 sqlite3TriggerSe
75bb0 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 lectStep(sqlite3
75bc0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 *,Select*);.SQLI
75bd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
75be0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 ggerStep *sqlite
75bf0 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 3TriggerInsertSt
75c00 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 ep(sqlite3*,Toke
75c10 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 n*, IdList*,.
75c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75c40 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 ExprList*,S
75c50 65 6c 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c elect*,int);.SQL
75c60 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 ITE_PRIVATE Tr
75c70 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 iggerStep *sqlit
75c80 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 e3TriggerUpdateS
75c90 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b tep(sqlite3*,Tok
75ca0 65 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 en*,ExprList*, E
75cb0 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 xpr*, int);.SQLI
75cc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
75cd0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 ggerStep *sqlite
75ce0 33 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 3TriggerDeleteSt
75cf0 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 ep(sqlite3*,Toke
75d00 6e 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 n*, Expr*);.SQLI
75d10 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75d20 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 d sqlite3DeleteT
75d30 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c rigger(sqlite3*,
75d40 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 Trigger*);.SQLI
75d50 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75d60 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 d sqlite3UnlinkA
75d70 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 ndDeleteTrigger(
75d80 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e sqlite3*,int,con
75d90 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6c 73 65 st char*);.#else
75da0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
75db0 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 41 3TriggersExist(A
75dc0 2c 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 ,B,C,D,E,F) 0.#
75dd0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 define sqlite3De
75de0 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 leteTrigger(A,B)
75df0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
75e00 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 3DropTriggerPtr(
75e10 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 A,B).# define sq
75e20 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 lite3UnlinkAndDe
75e30 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c leteTrigger(A,B,
75e40 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 C).# define sqli
75e50 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 te3CodeRowTrigge
75e60 72 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c r(A,B,C,D,E,F,G,
75e70 48 2c 49 2c 4a 2c 4b 29 20 30 0a 23 65 6e 64 69 H,I,J,K) 0.#endi
75e80 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
75e90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 E int sqlite3Joi
75ea0 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f nType(Parse*, To
75eb0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f ken*, Token*, To
75ec0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
75ed0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
75ee0 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b e3CreateForeignK
75ef0 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c ey(Parse*, ExprL
75f00 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 ist*, Token*, Ex
75f10 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 prList*, int);.S
75f20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
75f30 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72 46 id sqlite3DeferF
75f40 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a oreignKey(Parse*
75f50 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66 20 , int);.#ifndef
75f60 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
75f70 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 45 ORIZATION.SQLITE
75f80 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
75f90 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 sqlite3AuthRead(
75fa0 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63 68 Parse*,Expr*,Sch
75fb0 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a ema*,SrcList*);.
75fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
75fd0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68 int sqlite3Auth
75fe0 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74 Check(Parse*,int
75ff0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 , const char*, c
76000 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 onst char*, cons
76010 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
76020 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
76030 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 sqlite3AuthConte
76040 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20 41 xtPush(Parse*, A
76050 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e uthContext*, con
76060 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
76070 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
76080 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 sqlite3AuthCont
76090 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 extPop(AuthConte
760a0 78 74 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 xt*);.#else.# de
760b0 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 fine sqlite3Auth
760c0 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 Read(a,b,c,d).#
760d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 define sqlite3Au
760e0 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c thCheck(a,b,c,d,
760f0 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a e) SQLITE_OK.
76100 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
76110 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 AuthContextPush(
76120 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 a,b,c).# define
76130 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 sqlite3AuthConte
76140 78 74 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64 xtPop(a) ((void
76150 29 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c )(a)).#endif.SQL
76160 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
76170 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 sqlite3Attach(P
76180 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 arse*, Expr*, Ex
76190 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c pr*, Expr*);.SQL
761a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
761b0 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 sqlite3Detach(P
761c0 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 arse*, Expr*);.S
761d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
761e0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 t sqlite3BtreeFa
761f0 63 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 ctory(const sqli
76200 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 te3 *db, const c
76210 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a har *zFilename,.
76220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76230 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a int omitJ
76240 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63 ournal, int nCac
76250 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 42 he, int flags, B
76260 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29 3b tree **ppBtree);
76270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
76280 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e int sqlite3FixIn
76290 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61 72 it(DbFixer*, Par
762a0 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 se*, int, const
762b0 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b char*, const Tok
762c0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
762d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
762e0 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69 78 FixSrcList(DbFix
762f0 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a er*, SrcList*);.
76300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
76310 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c nt sqlite3FixSel
76320 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53 65 ect(DbFixer*, Se
76330 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 lect*);.SQLITE_P
76340 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
76350 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78 65 e3FixExpr(DbFixe
76360 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 r*, Expr*);.SQLI
76370 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
76380 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 qlite3FixExprLis
76390 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 t(DbFixer*, Expr
763a0 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 List*);.SQLITE_P
763b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
763c0 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70 e3FixTriggerStep
763d0 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67 67 (DbFixer*, Trigg
763e0 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 erStep*);.SQLITE
763f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
76400 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 ite3AtoF(const c
76410 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 har *z, double*)
76420 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76430 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 int sqlite3GetI
76440 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 nt32(const char
76450 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 *, int*);.SQLITE
76460 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
76470 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 ite3FitsIn64Bits
76480 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 (const char *, i
76490 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
764a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 ATE int sqlite3U
764b0 74 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 tf16ByteLen(cons
764c0 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 t void *pData, i
764d0 6e 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 nt nChar);.SQLIT
764e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
764f0 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e lite3Utf8CharLen
76500 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 (const char *pDa
76510 74 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a ta, int nByte);.
76520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
76530 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 nt sqlite3Utf8Re
76540 61 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f ad(const u8*, co
76550 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 nst u8*, const u
76560 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 8**);../*.** Rou
76570 74 69 6e 65 73 20 74 6f 20 72 65 61 64 20 61 6e tines to read an
76580 64 20 77 72 69 74 65 20 76 61 72 69 61 62 6c 65 d write variable
76590 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 -length integers
765a0 2e 20 20 54 68 65 73 65 20 75 73 65 64 20 74 6f . These used to
765b0 0a 2a 2a 20 62 65 20 64 65 66 69 6e 65 64 20 6c .** be defined l
765c0 6f 63 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20 ocally, but now
765d0 77 65 20 75 73 65 20 74 68 65 20 76 61 72 69 6e we use the varin
765e0 74 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 t routines in th
765f0 65 20 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 e util.c.** file
76600 2e 20 20 43 6f 64 65 20 73 68 6f 75 6c 64 20 75 . Code should u
76610 73 65 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 se the MACRO for
76620 6d 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 ms below, as the
76630 20 56 61 72 69 6e 74 33 32 20 76 65 72 73 69 6f Varint32 versio
76640 6e 73 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64 20 ns.** are coded
76650 74 6f 20 61 73 73 75 6d 65 20 74 68 65 20 73 69 to assume the si
76660 6e 67 6c 65 20 62 79 74 65 20 63 61 73 65 20 69 ngle byte case i
76670 73 20 61 6c 72 65 61 64 79 20 68 61 6e 64 6c 65 s already handle
76680 64 20 28 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 d (which .** the
76690 20 4d 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73 MACRO form does
766a0 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
766b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
766c0 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e PutVarint(unsign
766d0 65 64 20 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a ed char*, u64);.
766e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
766f0 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 nt sqlite3PutVar
76700 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 int32(unsigned c
76710 68 61 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 har*, u32);.SQLI
76720 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
76730 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 qlite3GetVarint(
76740 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
76750 68 61 72 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 har *, u64 *);.S
76760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
76770 74 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 t sqlite3GetVari
76780 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69 67 nt32(const unsig
76790 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 33 32 20 ned char *, u32
767a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
767b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 TE int sqlite3Va
767c0 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 3b 0a rintLen(u64 v);.
767d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 ./*.** The heade
767e0 72 20 6f 66 20 61 20 72 65 63 6f 72 64 20 63 6f r of a record co
767f0 6e 73 69 73 74 73 20 6f 66 20 61 20 73 65 71 75 nsists of a sequ
76800 65 6e 63 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 ence variable-le
76810 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 0a 2a ngth integers..*
76820 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 73 * These integers
76830 20 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c 77 61 are almost alwa
76840 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 72 65 ys small and are
76850 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 73 69 encoded as a si
76860 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 54 68 ngle byte..** Th
76870 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
76880 6f 73 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 os take advantag
76890 65 20 74 68 69 73 20 66 61 63 74 20 74 6f 20 70 e this fact to p
768a0 72 6f 76 69 64 65 20 61 20 66 61 73 74 20 65 6e rovide a fast en
768b0 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65 63 6f code.** and deco
768c0 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 de of the intege
768d0 72 73 20 69 6e 20 61 20 72 65 63 6f 72 64 20 68 rs in a record h
768e0 65 61 64 65 72 2e 20 20 49 74 20 69 73 20 66 61 eader. It is fa
768f0 73 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6d ster for the com
76900 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 mon.** case wher
76910 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73 e the integer is
76920 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 20 a single byte.
76930 20 49 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 It is a little
76940 73 6c 6f 77 65 72 20 77 68 65 6e 20 74 68 65 0a slower when the.
76950 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74 77 ** integer is tw
76960 6f 20 6f 72 20 6d 6f 72 65 20 62 79 74 65 73 2e o or more bytes.
76970 20 20 42 75 74 20 6f 76 65 72 61 6c 6c 20 69 74 But overall it
76980 20 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a is faster..**.*
76990 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
769a0 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 expressions are
769b0 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a equivalent:.**.*
769c0 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 * x = sqlite
769d0 33 47 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 3GetVarint32( A,
769e0 20 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 &B );.** x
769f0 3d 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 = sqlite3PutVari
76a00 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a nt32( A, B );.**
76a10 0a 2a 2a 20 20 20 20 20 78 20 3d 20 67 65 74 56 .** x = getV
76a20 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b arint32( A, B );
76a30 0a 2a 2a 20 20 20 20 20 78 20 3d 20 70 75 74 56 .** x = putV
76a40 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b arint32( A, B );
76a50 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 .**.*/.#define g
76a60 65 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 etVarint32(A,B)
76a70 20 28 28 2a 28 41 29 3c 28 75 6e 73 69 67 6e 65 ((*(A)<(unsigne
76a80 64 20 63 68 61 72 29 30 78 38 30 29 20 3f 20 28 d char)0x80) ? (
76a90 28 42 29 20 3d 20 28 75 33 32 29 2a 28 41 29 29 (B) = (u32)*(A))
76aa0 2c 31 20 3a 20 73 71 6c 69 74 65 33 47 65 74 56 ,1 : sqlite3GetV
76ab0 61 72 69 6e 74 33 32 28 28 41 29 2c 20 26 28 42 arint32((A), &(B
76ac0 29 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74 56 ))).#define putV
76ad0 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 28 arint32(A,B) ((
76ae0 28 42 29 3c 28 75 33 32 29 30 78 38 30 29 20 3f (B)<(u32)0x80) ?
76af0 20 28 2a 28 41 29 20 3d 20 28 75 6e 73 69 67 6e (*(A) = (unsign
76b00 65 64 20 63 68 61 72 29 28 42 29 29 2c 31 20 3a ed char)(B)),1 :
76b10 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e sqlite3PutVarin
76b20 74 33 32 28 28 41 29 2c 20 28 42 29 29 29 0a 23 t32((A), (B))).#
76b30 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e 74 define getVarint
76b40 20 20 20 20 73 71 6c 69 74 65 33 47 65 74 56 61 sqlite3GetVa
76b50 72 69 6e 74 0a 23 64 65 66 69 6e 65 20 70 75 74 rint.#define put
76b60 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 Varint sqlite
76b70 33 50 75 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 3PutVarint...SQL
76b80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
76b90 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 sqlite3IndexAff
76ba0 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c inityStr(Vdbe *,
76bb0 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 Index *);.SQLIT
76bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
76bd0 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e qlite3TableAffin
76be0 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 54 ityStr(Vdbe *, T
76bf0 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f able *);.SQLITE_
76c00 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c PRIVATE char sql
76c10 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e ite3CompareAffin
76c20 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c ity(Expr *pExpr,
76c30 20 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51 4c char aff2);.SQL
76c40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
76c50 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 sqlite3IndexAffi
76c60 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 nityOk(Expr *pEx
76c70 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66 66 pr, char idx_aff
76c80 69 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50 inity);.SQLITE_P
76c90 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 RIVATE char sqli
76ca0 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 te3ExprAffinity(
76cb0 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 Expr *pExpr);.SQ
76cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
76cd0 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63 sqlite3Atoi64(c
76ce0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34 2a onst char*, i64*
76cf0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
76d00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 E void sqlite3Er
76d10 72 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e ror(sqlite3*, in
76d20 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e t, const char*,.
76d30 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..);.SQLITE_PRIV
76d40 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
76d50 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 3HexToBlob(sqlit
76d60 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 e3*, const char
76d70 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 *z, int n);.SQLI
76d80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
76d90 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d qlite3TwoPartNam
76da0 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e e(Parse *, Token
76db0 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b *, Token *, Tok
76dc0 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 en **);.SQLITE_P
76dd0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
76de0 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72 r *sqlite3ErrStr
76df0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
76e00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
76e10 33 52 65 61 64 53 63 68 65 6d 61 28 50 61 72 73 3ReadSchema(Pars
76e20 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 e *pParse);.SQLI
76e30 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
76e40 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 eq *sqlite3FindC
76e50 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c ollSeq(sqlite3*,
76e60 75 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 u8 enc, const ch
76e70 61 72 20 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ar *,int,int);.S
76e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f QLITE_PRIVATE Co
76e90 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f llSeq *sqlite3Lo
76ea0 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 cateCollSeq(Pars
76eb0 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
76ec0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e char *zName, in
76ed0 74 20 6e 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 t nName);.SQLITE
76ee0 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 _PRIVATE CollSeq
76ef0 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c *sqlite3ExprCol
76f00 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 lSeq(Parse *pPar
76f10 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 se, Expr *pExpr)
76f20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76f30 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
76f40 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 20 prSetColl(Parse
76f50 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 2c *pParse, Expr *,
76f60 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 Token *);.SQLIT
76f70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
76f80 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 lite3CheckCollSe
76f90 71 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 q(Parse *, CollS
76fa0 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 eq *);.SQLITE_PR
76fb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
76fc0 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 3CheckObjectName
76fd0 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 (Parse *, const
76fe0 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f char *);.SQLITE_
76ff0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
77000 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 ite3VdbeSetChang
77010 65 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e es(sqlite3 *, in
77020 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 t);..SQLITE_PRIV
77030 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a ATE const void *
77040 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
77050 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c (sqlite3_value*,
77060 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u8);.SQLITE_PRI
77070 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
77080 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 ValueBytes(sqlit
77090 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a e3_value*, u8);.
770a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
770b0 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 oid sqlite3Value
770c0 53 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f 76 SetStr(sqlite3_v
770d0 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 alue*, int, cons
770e0 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20 t void *,u8, .
770f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77100 20 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76 6f void(*)(vo
77110 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 id*));.SQLITE_PR
77120 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
77130 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 e3ValueFree(sqli
77140 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
77150 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
77160 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 te3_value *sqlit
77170 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 e3ValueNew(sqlit
77180 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 e3 *);.SQLITE_PR
77190 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
771a0 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 te3Utf16to8(sqli
771b0 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 te3 *, const voi
771c0 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 d*, int);.SQLITE
771d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
771e0 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 ite3ValueFromExp
771f0 72 28 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 r(sqlite3 *, Exp
77200 72 20 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c r *, u8, u8, sql
77210 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a ite3_value **);.
77220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
77230 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 oid sqlite3Value
77240 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73 71 ApplyAffinity(sq
77250 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 lite3_value *, u
77260 38 2c 20 75 38 29 3b 0a 23 69 66 6e 64 65 66 20 8, u8);.#ifndef
77270 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 SQLITE_AMALGAMAT
77280 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ION.SQLITE_PRIVA
77290 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 TE const unsigne
772a0 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70 d char sqlite3Up
772b0 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 perToLower[];.SQ
772c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c LITE_PRIVATE SQL
772d0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53 ITE_WSD struct S
772e0 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c qlite3Config sql
772f0 69 74 65 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 ite3Config;.SQLI
77300 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 TE_PRIVATE SQLIT
77310 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 E_WSD FuncDefHas
77320 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 h sqlite3GlobalF
77330 75 6e 63 74 69 6f 6e 73 3b 0a 23 65 6e 64 69 66 unctions;.#endif
77340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
77350 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 void sqlite3Root
77360 50 61 67 65 4d 6f 76 65 64 28 44 62 2a 2c 20 69 PageMoved(Db*, i
77370 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
77380 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
77390 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61 72 lite3Reindex(Par
773a0 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b se*, Token*, Tok
773b0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
773c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
773d0 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 3AlterFunctions(
773e0 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
773f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
77400 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d qlite3AlterRenam
77410 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 eTable(Parse*, S
77420 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 rcList*, Token*)
77430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
77440 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 int sqlite3GetT
77450 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67 oken(const unsig
77460 6e 65 64 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 ned char *, int
77470 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
77480 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e TE void sqlite3N
77490 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73 65 estedParse(Parse
774a0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
774b0 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ...);.SQLITE_PRI
774c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
774d0 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 3ExpirePreparedS
774e0 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 tatements(sqlite
774f0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 3*);.SQLITE_PRIV
77500 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
77510 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 50 61 CodeSubselect(Pa
77520 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 rse *, Expr *, i
77530 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
77540 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
77550 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 lite3SelectPrep(
77560 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c Parse*, Select*,
77570 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a NameContext*);.
77580 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
77590 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 nt sqlite3Resolv
775a0 65 45 78 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43 eExprNames(NameC
775b0 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b ontext*, Expr*);
775c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
775d0 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f void sqlite3Reso
775e0 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 50 lveSelectNames(P
775f0 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 arse*, Select*,
77600 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 NameContext*);.S
77610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
77620 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 t sqlite3Resolve
77630 4f 72 64 65 72 47 72 6f 75 70 42 79 28 50 61 72 OrderGroupBy(Par
77640 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 se*, Select*, Ex
77650 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 prList*, const c
77660 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
77670 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
77680 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 e3ColumnDefault(
77690 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c Vdbe *, Table *,
776a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
776b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
776c0 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 e3AlterFinishAdd
776d0 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 Column(Parse *,
776e0 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 Token *);.SQLITE
776f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
77700 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 lite3AlterBeginA
77710 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a ddColumn(Parse *
77720 2c 20 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51 , SrcList *);.SQ
77730 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c LITE_PRIVATE Col
77740 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 lSeq *sqlite3Get
77750 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a CollSeq(sqlite3*
77760 2c 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e , CollSeq *, con
77770 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b st char *, int);
77780 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
77790 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 char sqlite3Affi
777a0 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54 nityType(const T
777b0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
777c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
777d0 74 65 33 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 te3Analyze(Parse
777e0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
777f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
77800 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e TE int sqlite3In
77810 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 vokeBusyHandler(
77820 42 75 73 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 BusyHandler*);.S
77830 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
77840 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 t sqlite3FindDb(
77850 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a sqlite3*, Token*
77860 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
77870 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 E int sqlite3Ana
77880 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 lysisLoad(sqlite
77890 33 2a 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51 4c 3*,int iDB);.SQL
778a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
778b0 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 sqlite3DefaultR
778c0 6f 77 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 owEst(Index*);.S
778d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
778e0 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 id sqlite3Regist
778f0 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 erLikeFunctions(
77900 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a sqlite3*, int);.
77910 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
77920 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 nt sqlite3IsLike
77930 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 Function(sqlite3
77940 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 *,Expr*,int*,cha
77950 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
77960 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
77970 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 MinimumFileForma
77980 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 t(Parse*, int, i
77990 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
779a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
779b0 53 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 SchemaFree(void
779c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
779d0 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 TE Schema *sqlit
779e0 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69 e3SchemaGet(sqli
779f0 74 65 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b te3 *, Btree *);
77a00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
77a10 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d int sqlite3Schem
77a20 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33 aToIndex(sqlite3
77a30 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b *db, Schema *);
77a40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
77a50 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 KeyInfo *sqlite3
77a60 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 IndexKeyinfo(Par
77a70 73 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a se *, Index *);.
77a80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
77a90 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 nt sqlite3Create
77aa0 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 Func(sqlite3 *,
77ab0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e const char *, in
77ac0 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 t, int, void *,
77ad0 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 . void (*)(sqli
77ae0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 te3_context*,int
77af0 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a ,sqlite3_value *
77b00 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 *),. void (*)(s
77b10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
77b20 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
77b30 65 20 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28 e **), void (*)(
77b40 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
77b50 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ));.SQLITE_PRIVA
77b60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 TE int sqlite3Ap
77b70 69 45 78 69 74 28 73 71 6c 69 74 65 33 20 2a 64 iExit(sqlite3 *d
77b80 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f b, int);.SQLITE_
77b90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
77ba0 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 te3OpenTempDatab
77bb0 61 73 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 ase(Parse *);..S
77bc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
77bd0 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 id sqlite3StrAcc
77be0 75 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d 2a umInit(StrAccum*
77bf0 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e , char*, int, in
77c00 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
77c10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
77c20 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 trAccumAppend(St
77c30 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 rAccum*,const ch
77c40 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ar*,int);.SQLITE
77c50 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
77c60 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
77c70 6e 69 73 68 28 53 74 72 41 63 63 75 6d 2a 29 3b nish(StrAccum*);
77c80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
77c90 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 void sqlite3StrA
77ca0 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 63 ccumReset(StrAcc
77cb0 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 um*);.SQLITE_PRI
77cc0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
77cd0 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 3SelectDestInit(
77ce0 53 65 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74 2c SelectDest*,int,
77cf0 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 int);../*.** The
77d00 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 interface to th
77d10 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 e LEMON-generate
77d20 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 51 4c 49 d parser.*/.SQLI
77d30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
77d40 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c *sqlite3ParserAl
77d50 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a loc(void*(*)(siz
77d60 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 e_t));.SQLITE_PR
77d70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
77d80 65 33 50 61 72 73 65 72 46 72 65 65 28 76 6f 69 e3ParserFree(voi
77d90 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 d*, void(*)(void
77da0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *));.SQLITE_PRIV
77db0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
77dc0 50 61 72 73 65 72 28 76 6f 69 64 2a 2c 20 69 6e Parser(void*, in
77dd0 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 73 65 2a t, Token, Parse*
77de0 29 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 );.#ifdef YYTRAC
77df0 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a 53 KMAXSTACKDEPTH.S
77e00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
77e10 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65 int sqlite3Parse
77e20 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64 2a rStackPeak(void*
77e30 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 );.#endif..SQLIT
77e40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
77e50 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 lite3AutoLoadExt
77e60 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a ensions(sqlite3*
77e70 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
77e80 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
77e90 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 NSION.SQLITE_PRI
77ea0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
77eb0 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f te3CloseExtensio
77ec0 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 ns(sqlite3*);.#e
77ed0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c lse.# define sql
77ee0 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 ite3CloseExtensi
77ef0 6f 6e 73 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 ons(X).#endif..#
77f00 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
77f10 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
77f20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
77f30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 void sqlite3Tab
77f40 6c 65 4c 6f 63 6b 28 50 61 72 73 65 20 2a 2c 20 leLock(Parse *,
77f50 69 6e 74 2c 20 69 6e 74 2c 20 75 38 2c 20 63 6f int, int, u8, co
77f60 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 23 65 6c nst char *);.#el
77f70 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c se. #define sql
77f80 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 76 2c ite3TableLock(v,
77f90 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 0a w,x,y,z).#endif.
77fa0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
77fb0 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 EST.SQLITE_PRIVA
77fc0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
77fd0 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 Utf8To8(unsigned
77fe0 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a char*);.#endif.
77ff0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
78000 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
78010 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
78020 65 33 56 74 61 62 43 6c 65 61 72 28 58 29 0a 23 e3VtabClear(X).#
78030 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
78040 56 74 61 62 53 79 6e 63 28 58 2c 59 29 20 53 51 VtabSync(X,Y) SQ
78050 4c 49 54 45 5f 4f 4b 0a 23 20 20 64 65 66 69 6e LITE_OK.# defin
78060 65 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c e sqlite3VtabRol
78070 6c 62 61 63 6b 28 58 29 0a 23 20 20 64 65 66 69 lback(X).# defi
78080 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f ne sqlite3VtabCo
78090 6d 6d 69 74 28 58 29 0a 23 65 6c 73 65 0a 53 51 mmit(X).#else.SQ
780a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 LITE_PRIVATE
780b0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
780c0 43 6c 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 Clear(Table*);.S
780d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
780e0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 int sqlite3Vtab
780f0 53 79 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 Sync(sqlite3 *db
78100 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 , char **);.SQLI
78110 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e TE_PRIVATE in
78120 74 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c t sqlite3VtabRol
78130 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 lback(sqlite3 *d
78140 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 b);.SQLITE_PRIVA
78150 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 TE int sqlite
78160 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 3VtabCommit(sqli
78170 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e 64 69 66 te3 *db);.#endif
78180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78190 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
781a0 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 MakeWritable(Par
781b0 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c se*,Table*);.SQL
781c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
781d0 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b sqlite3VtabLock
781e0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b (sqlite3_vtab*);
781f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78200 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
78210 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 2a 2c Unlock(sqlite3*,
78220 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b sqlite3_vtab*);
78230 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78240 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
78250 42 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 BeginParse(Parse
78260 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
78270 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
78280 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
78290 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 sqlite3VtabFinis
782a0 68 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 hParse(Parse*, T
782b0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
782c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
782d0 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 te3VtabArgInit(P
782e0 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
782f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
78300 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 te3VtabArgExtend
78310 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 (Parse*, Token*)
78320 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
78330 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 int sqlite3Vtab
78340 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 CallCreate(sqlit
78350 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 e3*, int, const
78360 63 68 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 char *, char **)
78370 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
78380 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 int sqlite3Vtab
78390 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 CallConnect(Pars
783a0 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c e*, Table*);.SQL
783b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
783c0 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 sqlite3VtabCallD
783d0 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c estroy(sqlite3*,
783e0 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 int, const char
783f0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
78400 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
78410 74 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 tabBegin(sqlite3
78420 20 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 *, sqlite3_vtab
78430 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
78440 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c ATE FuncDef *sql
78450 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 ite3VtabOverload
78460 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 Function(sqlite3
78470 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 *,FuncDef*, int
78480 20 6e 41 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53 nArg, Expr*);.S
78490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
784a0 69 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 id sqlite3Invali
784b0 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 dFunction(sqlite
784c0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
784d0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b qlite3_value**);
784e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
784f0 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 int sqlite3Trans
78500 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 ferBindings(sqli
78510 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 te3_stmt *, sqli
78520 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c te3_stmt *);.SQL
78530 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
78540 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 sqlite3Reprepare
78550 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
78560 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
78570 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 ite3ExprListChec
78580 6b 4c 65 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 kLength(Parse*,
78590 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 ExprList*, const
785a0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
785b0 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
785c0 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f *sqlite3BinaryCo
785d0 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 mpareCollSeq(Par
785e0 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 se *, Expr *, Ex
785f0 70 72 20 2a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 pr *);.../*.** A
78600 76 61 69 6c 61 62 6c 65 20 66 61 75 6c 74 20 69 vailable fault i
78610 6e 6a 65 63 74 6f 72 73 2e 20 20 53 68 6f 75 6c njectors. Shoul
78620 64 20 62 65 20 6e 75 6d 62 65 72 65 64 20 62 65 d be numbered be
78630 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 2e 0a ginning with 0..
78640 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
78650 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f E_FAULTINJECTOR_
78660 4d 41 4c 4c 4f 43 20 20 20 20 20 30 0a 23 64 65 MALLOC 0.#de
78670 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c fine SQLITE_FAUL
78680 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 TINJECTOR_COUNT
78690 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 1../*.** Th
786a0 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 e interface to t
786b0 68 65 20 63 6f 64 65 20 69 6e 20 66 61 75 6c 74 he code in fault
786c0 2e 63 20 75 73 65 64 20 66 6f 72 20 69 64 65 6e .c used for iden
786d0 74 69 66 79 69 6e 67 20 22 62 65 6e 69 67 6e 22 tifying "benign"
786e0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 .** malloc failu
786f0 72 65 73 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c res. This is onl
78700 79 20 70 72 65 73 65 6e 74 20 69 66 20 53 51 4c y present if SQL
78710 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
78720 5f 54 45 53 54 0a 2a 2a 20 69 73 20 6e 6f 74 20 _TEST.** is not
78730 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e defined..*/.#ifn
78740 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
78750 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 53 51 4c BUILTIN_TEST.SQL
78760 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
78770 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 id sqlite3BeginB
78780 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 enignMalloc(void
78790 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
787a0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
787b0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
787c0 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 20 20 23 void);.#else. #
787d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 65 define sqlite3Be
787e0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
787f0 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 ). #define sqli
78800 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
78810 6f 63 28 29 0a 23 65 6e 64 69 66 0a 0a 23 64 65 oc().#endif..#de
78820 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 52 4f fine IN_INDEX_RO
78830 57 49 44 20 20 20 20 20 20 20 20 20 20 20 31 0a WID 1.
78840 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 #define IN_INDEX
78850 5f 45 50 48 20 20 20 20 20 20 20 20 20 20 20 20 _EPH
78860 20 32 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 2.#define IN_IN
78870 44 45 58 5f 49 4e 44 45 58 20 20 20 20 20 20 20 DEX_INDEX
78880 20 20 20 20 33 0a 53 51 4c 49 54 45 5f 50 52 49 3.SQLITE_PRI
78890 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
788a0 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 FindInIndex(Pars
788b0 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 e *, Expr *, int
788c0 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 *);..#ifdef SQLI
788d0 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 TE_ENABLE_ATOMIC
788e0 5f 57 52 49 54 45 0a 53 51 4c 49 54 45 5f 50 52 _WRITE.SQLITE_PR
788f0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
78900 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 te3JournalOpen(s
78910 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f qlite3_vfs *, co
78920 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 nst char *, sqli
78930 74 65 33 5f 66 69 6c 65 20 2a 2c 20 69 6e 74 2c te3_file *, int,
78940 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
78950 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
78960 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 te3JournalSize(s
78970 71 6c 69 74 65 33 5f 76 66 73 20 2a 29 3b 0a 53 qlite3_vfs *);.S
78980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
78990 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e int sqlite3Journ
789a0 61 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 alCreate(sqlite3
789b0 5f 66 69 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a _file *);.#else.
789c0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
789d0 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 3JournalSize(pVf
789e0 73 29 20 28 28 70 56 66 73 29 2d 3e 73 7a 4f 73 s) ((pVfs)->szOs
789f0 46 69 6c 65 29 0a 23 65 6e 64 69 66 0a 0a 53 51 File).#endif..SQ
78a00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
78a10 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 d sqlite3MemJour
78a20 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f nalOpen(sqlite3_
78a30 66 69 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f file *);.SQLITE_
78a40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
78a50 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a te3MemJournalSiz
78a60 65 28 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e();.SQLITE_PRIV
78a70 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
78a80 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 sMemJournal(sqli
78a90 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 te3_file *);..#i
78aa0 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 f SQLITE_MAX_EXP
78ab0 52 5f 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 R_DEPTH>0.SQLITE
78ac0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
78ad0 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 sqlite3ExprSetHe
78ae0 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 ight(Parse *pPar
78af0 73 65 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 se, Expr *p);.SQ
78b00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
78b10 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 nt sqlite3Select
78b20 45 78 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 ExprHeight(Selec
78b30 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t *);.SQLITE_PRI
78b40 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
78b50 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 e3ExprCheckHeigh
78b60 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a t(Parse*, int);.
78b70 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
78b80 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 sqlite3ExprSetHe
78b90 69 67 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66 ight(x,y). #def
78ba0 69 6e 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 ine sqlite3Selec
78bb0 74 45 78 70 72 48 65 69 67 68 74 28 78 29 20 30 tExprHeight(x) 0
78bc0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
78bd0 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 e3ExprCheckHeigh
78be0 74 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 t(x,y).#endif..S
78bf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 QLITE_PRIVATE u3
78c00 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 2 sqlite3Get4byt
78c10 65 28 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 e(const u8*);.SQ
78c20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
78c30 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 d sqlite3Put4byt
78c40 65 28 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 e(u8*, u32);..#i
78c50 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a fdef SQLITE_SSE.
78c60 23 69 6e 63 6c 75 64 65 20 22 73 73 65 49 6e 74 #include "sseInt
78c70 2e 68 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 .h".#endif..#ifd
78c80 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
78c90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
78ca0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 void sqlite3Par
78cb0 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 serTrace(FILE*,
78cc0 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a char *);.#endif.
78cd0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 ./*.** If the SQ
78ce0 4c 49 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 LITE_ENABLE IOTR
78cf0 41 43 45 20 65 78 69 73 74 73 20 74 68 65 6e 20 ACE exists then
78d00 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 the global varia
78d10 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f ble.** sqlite3Io
78d20 54 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 Trace is a point
78d30 65 72 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c er to a printf-l
78d40 69 6b 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64 ike routine used
78d50 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f to.** print I/O
78d60 20 74 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65 tracing message
78d70 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 s. .*/.#ifdef SQ
78d80 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 LITE_ENABLE_IOTR
78d90 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 ACE.# define IOT
78da0 52 41 43 45 28 41 29 20 20 69 66 28 20 73 71 6c RACE(A) if( sql
78db0 69 74 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73 ite3IoTrace ){ s
78dc0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b qlite3IoTrace A;
78dd0 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }.SQLITE_PRIVAT
78de0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
78df0 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 VdbeIOTraceSql(V
78e00 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
78e10 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c IVATE void (*sql
78e20 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e ite3IoTrace)(con
78e30 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 st char*,...);.#
78e40 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f else.# define IO
78e50 54 52 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e TRACE(A).# defin
78e60 65 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 e sqlite3VdbeIOT
78e70 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 raceSql(X).#endi
78e80 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a f..#endif../****
78e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
78ea0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a f sqliteInt.h **
78eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
78ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
78ef0 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a file global.c *
78f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
78f30 20 32 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 2008 June 13.**
78f40 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
78f50 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
78f60 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
78f70 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
78f80 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
78f90 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
78fa0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
78fb0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
78fc0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
78fd0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
78fe0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
78ff0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
79000 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
79010 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
79020 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
79030 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
79040 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
79050 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
79060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
790a0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
790b0 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e tains definition
790c0 73 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 s of global vari
790d0 61 62 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e ables and contan
790e0 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 67 ts..**.** $Id: g
790f0 6c 6f 62 61 6c 2e 63 2c 76 20 31 2e 38 20 32 30 lobal.c,v 1.8 20
79100 30 38 2f 30 39 2f 30 34 20 31 37 3a 31 37 3a 33 08/09/04 17:17:3
79110 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 9 danielk1977 Ex
79120 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6e 20 61 p $.*/.../* An a
79130 72 72 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 rray to map all
79140 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 upper-case chara
79150 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72 cters into their
79160 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a corresponding.*
79170 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61 * lower-case cha
79180 72 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 racter. .**.** S
79190 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 QLite only consi
791a0 64 65 72 73 20 55 53 2d 41 53 43 49 49 20 28 6f ders US-ASCII (o
791b0 72 20 45 42 43 44 49 43 29 20 63 68 61 72 61 63 r EBCDIC) charac
791c0 74 65 72 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 ters. We do not
791d0 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 61 73 65 20 .** handle case
791e0 63 6f 6e 76 65 72 73 69 6f 6e 73 20 66 6f 72 20 conversions for
791f0 74 68 65 20 55 54 46 20 63 68 61 72 61 63 74 65 the UTF characte
79200 72 20 73 65 74 20 73 69 6e 63 65 20 74 68 65 20 r set since the
79210 74 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76 tables.** involv
79220 65 64 20 61 72 65 20 6e 65 61 72 6c 79 20 61 73 ed are nearly as
79230 20 62 69 67 20 6f 72 20 62 69 67 67 65 72 20 74 big or bigger t
79240 68 61 6e 20 53 51 4c 69 74 65 20 69 74 73 65 6c han SQLite itsel
79250 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 f..*/.SQLITE_PRI
79260 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 VATE const unsig
79270 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 ned char sqlite3
79280 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d UpperToLower[] =
79290 20 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 {.#ifdef SQLITE
792a0 5f 41 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20 _ASCII. 0,
792b0 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 1, 2, 3, 4,
792c0 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 5, 6, 7, 8,
792d0 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 9, 10, 11, 12,
792e0 31 33 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 13, 14, 15, 16,
792f0 31 37 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 17,. 18, 19,
79300 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20, 21, 22, 23,
79310 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 24, 25, 26, 27,
79320 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 28, 29, 30, 31,
79330 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 32, 33, 34, 35,
79340 0a 20 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38 . 36, 37, 38
79350 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 , 39, 40, 41, 42
79360 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 , 43, 44, 45, 46
79370 2c 20 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 , 47, 48, 49, 50
79380 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 , 51, 52, 53,.
79390 20 20 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 54, 55, 56, 5
793a0 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 7, 58, 59, 60, 6
793b0 31 2c 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 1, 62, 63, 64, 9
793c0 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 7, 98, 99,100,10
793d0 31 2c 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 1,102,103,. 1
793e0 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 04,105,106,107,1
793f0 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 08,109,110,111,1
79400 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 12,113,114,115,1
79410 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 16,117,118,119,1
79420 32 30 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20,121,. 122,
79430 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 91, 92, 93, 94,
79440 20 39 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 95, 96, 97, 98,
79450 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 99,100,101,102,
79460 31 30 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 103,104,105,106,
79470 31 30 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 107,. 108,109
79480 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 ,110,111,112,113
79490 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 ,114,115,116,117
794a0 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 ,118,119,120,121
794b0 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 ,122,123,124,125
794c0 2c 0a 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32 ,. 126,127,12
794d0 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 8,129,130,131,13
794e0 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 2,133,134,135,13
794f0 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 6,137,138,139,14
79500 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 0,141,142,143,.
79510 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 144,145,146,1
79520 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 47,148,149,150,1
79530 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 51,152,153,154,1
79540 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 55,156,157,158,1
79550 35 39 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 59,160,161,.
79560 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 162,163,164,165,
79570 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 166,167,168,169,
79580 31 37 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 170,171,172,173,
79590 31 37 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 174,175,176,177,
795a0 31 37 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 178,179,. 180
795b0 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 ,181,182,183,184
795c0 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 ,185,186,187,188
795d0 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 ,189,190,191,192
795e0 2c 31 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 ,193,194,195,196
795f0 2c 31 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 ,197,. 198,19
79600 39 2c 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 9,200,201,202,20
79610 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 3,204,205,206,20
79620 37 2c 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 7,208,209,210,21
79630 31 2c 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 1,212,213,214,21
79640 35 2c 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32 5,. 216,217,2
79650 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 18,219,220,221,2
79660 32 32 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 22,223,224,225,2
79670 32 36 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 26,227,228,229,2
79680 33 30 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 30,231,232,233,.
79690 20 20 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 234,235,236,
796a0 32 33 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 237,238,239,240,
796b0 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 241,242,243,244,
796c0 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 245,246,247,248,
796d0 32 34 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 249,250,251,.
796e0 20 32 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 252,253,254,255
796f0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
79700 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20 QLITE_EBCDIC.
79710 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 0, 1, 2, 3
79720 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 , 4, 5, 6, 7
79730 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 , 8, 9, 10, 11
79740 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 , 12, 13, 14, 15
79750 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 , /* 0x */.
79760 31 36 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 16, 17, 18, 19,
79770 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 20, 21, 22, 23,
79780 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 24, 25, 26, 27,
79790 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 28, 29, 30, 31,
797a0 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 /* 1x */. 32
797b0 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 , 33, 34, 35, 36
797c0 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 , 37, 38, 39, 40
797d0 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 , 41, 42, 43, 44
797e0 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a , 45, 46, 47, /*
797f0 20 32 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 2x */. 48,
79800 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 49, 50, 51, 52,
79810 35 33 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 53, 54, 55, 56,
79820 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 57, 58, 59, 60,
79830 36 31 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 61, 62, 63, /* 3
79840 78 20 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 x */. 64, 65
79850 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 , 66, 67, 68, 69
79860 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 , 70, 71, 72, 73
79870 2c 20 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 , 74, 75, 76, 77
79880 2c 20 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 , 78, 79, /* 4x
79890 2a 2f 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20 */. 80, 81,
798a0 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 82, 83, 84, 85,
798b0 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 86, 87, 88, 89,
798c0 39 30 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 90, 91, 92, 93,
798d0 39 34 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 94, 95, /* 5x */
798e0 0a 20 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 . 96, 97, 66
798f0 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 , 67, 68, 69, 70
79900 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 , 71, 72, 73,106
79910 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 ,107,108,109,110
79920 2c 31 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 ,111, /* 6x */.
79930 20 20 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 112, 81, 82,
79940 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 83, 84, 85, 86,
79950 38 37 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 87, 88, 89,122,1
79960 32 33 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 23,124,125,126,1
79970 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 27, /* 7x */.
79980 20 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 128,129,130,131
79990 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 ,132,133,134,135
799a0 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 ,136,137,138,139
799b0 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 ,140,141,142,143
799c0 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 , /* 8x */. 1
799d0 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 44,145,146,147,1
799e0 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 48,149,150,151,1
799f0 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 52,153,154,155,1
79a00 35 36 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 56,157,156,159,
79a10 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 /* 9x */. 160
79a20 2c 31 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 ,161,162,163,164
79a30 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 ,165,166,167,168
79a40 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 ,169,170,171,140
79a50 2c 31 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a ,141,142,175, /*
79a60 20 41 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 Ax */. 176,1
79a70 37 37 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 77,178,179,180,1
79a80 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 81,182,183,184,1
79a90 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 85,186,187,188,1
79aa0 38 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 89,190,191, /* B
79ab0 78 20 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 x */. 192,129
79ac0 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 ,130,131,132,133
79ad0 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 ,134,135,136,137
79ae0 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 ,202,203,204,205
79af0 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 ,206,207, /* Cx
79b00 2a 2f 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31 */. 208,145,1
79b10 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 46,147,148,149,1
79b20 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 50,151,152,153,2
79b30 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 18,219,220,221,2
79b40 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 22,223, /* Dx */
79b50 0a 20 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32 . 224,225,162
79b60 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 ,163,164,165,166
79b70 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 ,167,168,169,232
79b80 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 ,203,204,205,206
79b90 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 ,207, /* Ex */.
79ba0 20 20 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 239,240,241,2
79bb0 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 42,243,244,245,2
79bc0 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 46,247,248,249,2
79bd0 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 19,220,221,222,2
79be0 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 55, /* Fx */.#en
79bf0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 dif.};../*.** Th
79c00 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67 e following sing
79c10 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 leton contains t
79c20 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 he global config
79c30 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74 uration for.** t
79c40 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
79c50 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
79c60 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 VATE SQLITE_WSD
79c70 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f struct Sqlite3Co
79c80 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 nfig sqlite3Conf
79c90 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45 ig = {. SQLITE
79ca0 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 _DEFAULT_MEMSTAT
79cb0 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74 US, /* bMemstat
79cc0 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 20 */. 1,
79cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
79ce0 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 20 /* bCoreMutex
79cf0 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 52 */. SQLITE_THR
79d00 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 20 EADSAFE==1,
79d10 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a /* bFullMutex *
79d20 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 65 2c /. 0x7ffffffe,
79d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
79d40 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 /* mxStrlen */.
79d50 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 20 20 100,
79d60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
79d70 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 szLookaside */.
79d80 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 20 20 500,
79d90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
79da0 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 nLookaside */.
79db0 20 2f 2a 20 4f 74 68 65 72 20 66 69 65 6c 64 73 /* Other fields
79dc0 20 61 6c 6c 20 64 65 66 61 75 6c 74 20 74 6f 20 all default to
79dd0 7a 65 72 6f 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a zero */.};.../*.
79de0 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f ** Hash table fo
79df0 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f r global functio
79e00 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63 ns - functions c
79e10 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 ommon to all.**
79e20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
79e30 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e 69 ions. After ini
79e40 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 69 tialization, thi
79e50 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 65 s table is.** re
79e60 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 ad-only..*/.SQLI
79e70 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 TE_PRIVATE SQLIT
79e80 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 E_WSD FuncDefHas
79e90 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 h sqlite3GlobalF
79ea0 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 2a 2a 2a unctions;../****
79eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
79ec0 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a f global.c *****
79ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
79f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
79f10 20 66 69 6c 65 20 73 74 61 74 75 73 2e 63 20 2a file status.c *
79f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
79f50 20 32 30 30 38 20 4a 75 6e 65 20 31 38 0a 2a 2a 2008 June 18.**
79f60 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
79f70 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
79f80 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
79f90 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
79fa0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
79fb0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
79fc0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
79fd0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
79fe0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
79ff0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
7a000 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
7a010 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
7a020 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
7a030 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
7a040 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
7a050 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
7a060 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
7a070 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
7a080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
7a0c0 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 ** This module i
7a0d0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71 mplements the sq
7a0e0 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 20 69 lite3_status() i
7a0f0 6e 74 65 72 66 61 63 65 20 61 6e 64 20 72 65 6c nterface and rel
7a100 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ated.** function
7a110 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 ality..**.** $Id
7a120 3a 20 73 74 61 74 75 73 2e 63 2c 76 20 31 2e 39 : status.c,v 1.9
7a130 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a 35 2008/09/02 00:5
7a140 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2:52 drh Exp $.*
7a150 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c /../*.** Variabl
7a160 65 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 es in which to r
7a170 65 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e 66 ecord status inf
7a180 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 ormation..*/.typ
7a190 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
7a1a0 74 65 33 53 74 61 74 54 79 70 65 20 73 71 6c 69 te3StatType sqli
7a1b0 74 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74 61 te3StatType;.sta
7a1c0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 tic SQLITE_WSD s
7a1d0 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 61 truct sqlite3Sta
7a1e0 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e 6f tType {. int no
7a1f0 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 wValue[9];
7a200 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 61 /* Current va
7a210 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 56 lue */. int mxV
7a220 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 20 alue[9];
7a230 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c /* Maximum val
7a240 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 53 ue */.} sqlite3S
7a250 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b 30 tat = { {0,}, {0
7a260 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 22 ,} };.../* The "
7a270 77 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77 wsdStat" macro w
7a280 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 ill resolve to t
7a290 68 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d he status inform
7a2a0 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76 ation.** state v
7a2b0 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 ector. If writa
7a2c0 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 ble static data
7a2d0 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f is unsupported o
7a2e0 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a n the target,.**
7a2f0 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 we have to loca
7a300 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 te the state vec
7a310 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e tor at run-time.
7a320 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f In the more co
7a330 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 mmon.** case whe
7a340 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 re writable stat
7a350 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f ic data is suppo
7a360 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 61 rted, wsdStat ca
7a370 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 n refer directly
7a380 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 .** to the "sqli
7a390 74 65 33 53 74 61 74 22 20 73 74 61 74 65 20 76 te3Stat" state v
7a3a0 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 ector declared a
7a3b0 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 bove..*/.#ifdef
7a3c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a SQLITE_OMIT_WSD.
7a3d0 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 # define wsdStat
7a3e0 49 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 61 Init sqlite3Sta
7a3f0 74 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f 42 tType *x = &GLOB
7a400 41 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 79 AL(sqlite3StatTy
7a410 70 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 0a pe,sqlite3Stat).
7a420 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 # define wsdStat
7a430 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 x[0].#else.# de
7a440 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 fine wsdStatInit
7a450 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 .# define wsdSta
7a460 74 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 65 t sqlite3Stat.#e
7a470 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
7a480 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 rn the current v
7a490 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 alue of a status
7a4a0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53 parameter..*/.S
7a4b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
7a4c0 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 t sqlite3StatusV
7a4d0 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 20 alue(int op){.
7a4e0 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 wsdStatInit;. a
7a4f0 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 ssert( op>=0 &&
7a500 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 op<ArraySize(wsd
7a510 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 Stat.nowValue) )
7a520 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 74 ;. return wsdSt
7a530 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b at.nowValue[op];
7a540 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e 20 .}../*.** Add N
7a550 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 to the value of
7a560 61 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 2e a status record.
7a570 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 It is assumed
7a580 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c 6c that the.** call
7a590 65 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 72 er holds appropr
7a5a0 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 iate locks..*/.S
7a5b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7a5c0 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 id sqlite3Status
7a5d0 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 Add(int op, int
7a5e0 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 N){. wsdStatIni
7a5f0 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e t;. assert( op>
7a600 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 =0 && op<ArraySi
7a610 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 ze(wsdStat.nowVa
7a620 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 lue) );. wsdSta
7a630 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 2b t.nowValue[op] +
7a640 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 74 = N;. if( wsdSt
7a650 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e at.nowValue[op]>
7a660 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b wsdStat.mxValue[
7a670 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 op] ){. wsdSt
7a680 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d at.mxValue[op] =
7a690 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
7a6a0 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a e[op];. }.}../*
7a6b0 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 .** Set the valu
7a6c0 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 6f e of a status to
7a6d0 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 X..*/.SQLITE_PR
7a6e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
7a6f0 65 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 20 e3StatusSet(int
7a700 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 73 op, int X){. ws
7a710 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 dStatInit;. ass
7a720 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 ert( op>=0 && op
7a730 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 <ArraySize(wsdSt
7a740 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a at.nowValue) );.
7a750 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c wsdStat.nowVal
7a760 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 66 ue[op] = X;. if
7a770 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c ( wsdStat.nowVal
7a780 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d ue[op]>wsdStat.m
7a790 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 xValue[op] ){.
7a7a0 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 wsdStat.mxValu
7a7b0 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e e[op] = wsdStat.
7a7c0 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 nowValue[op];.
7a7d0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 }.}../*.** Query
7a7e0 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 status informat
7a7f0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion..**.** This
7a800 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 implementation a
7a810 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 64 ssumes that read
7a820 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 61 ing or writing a
7a830 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 2d n aligned.** 32-
7a840 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 bit integer is a
7a850 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 n atomic operati
7a860 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 73 on. If that ass
7a870 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74 umption is not t
7a880 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 rue,.** then thi
7a890 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 s routine is not
7a8a0 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a threadsafe..*/.
7a8b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
7a8c0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e qlite3_status(in
7a8d0 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 t op, int *pCurr
7a8e0 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 ent, int *pHighw
7a8f0 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 ater, int resetF
7a900 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 49 lag){. wsdStatI
7a910 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 20 nit;. if( op<0
7a920 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a 65 || op>=ArraySize
7a930 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 (wsdStat.nowValu
7a940 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e e) ){. return
7a950 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
7a960 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 20 }. *pCurrent
7a970 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c = wsdStat.nowVal
7a980 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 68 ue[op];. *pHigh
7a990 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74 2e water = wsdStat.
7a9a0 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 69 mxValue[op];. i
7a9b0 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a f( resetFlag ){.
7a9c0 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 wsdStat.mxVa
7a9d0 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 lue[op] = wsdSta
7a9e0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a t.nowValue[op];.
7a9f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
7aa00 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
7aa10 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69 6e Query status in
7aa20 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 formation for a
7aa30 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 single database
7aa40 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 connection.*/.SQ
7aa50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
7aa60 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 0a ite3_db_status(.
7aa70 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
7aa80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
7aa90 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
7aaa0 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73 20 on whose status
7aab0 69 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 20 is desired */.
7aac0 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 int op,
7aad0 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 /* Status
7aae0 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 verb */. int *p
7aaf0 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20 20 Current,
7ab00 2f 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e 74 /* Write current
7ab10 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 20 value here */.
7ab20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 int *pHighwater
7ab30 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 , /* Write
7ab40 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 high-water mark
7ab50 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 here */. int re
7ab60 73 65 74 46 6c 61 67 20 20 20 20 20 20 20 20 20 setFlag
7ab70 2f 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77 61 /* Reset high-wa
7ab80 74 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75 65 ter mark if true
7ab90 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 */.){. switch(
7aba0 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
7abb0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
7abc0 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20 LOOKASIDE_USED:
7abd0 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e {. *pCurren
7abe0 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 t = db->lookasid
7abf0 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a 70 e.nOut;. *p
7ac00 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d 3e Highwater = db->
7ac10 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 3b lookaside.mxOut;
7ac20 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65 74 . if( reset
7ac30 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 Flag ){.
7ac40 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 db->lookaside.mx
7ac50 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 Out = db->lookas
7ac60 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 ide.nOut;.
7ac70 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
7ac80 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 }. default
7ac90 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e : {. return
7aca0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
7acb0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
7acc0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
7acd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
7ace0 45 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63 20 End of status.c
7acf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ad10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
7ad20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
7ad30 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65 2e Begin file date.
7ad40 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
7ad50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
7ad70 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f 62 /*.** 2003 Octob
7ad80 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 31.**.** The
7ad90 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
7ada0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
7adb0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
7adc0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
7add0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
7ade0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
7adf0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
7ae00 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
7ae10 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
7ae20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
7ae30 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
7ae40 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
7ae50 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
7ae60 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
7ae70 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
7ae80 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
7ae90 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
7aea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7aeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7aec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7aed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7aee0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
7aef0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 e contains the C
7af00 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
7af10 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 61 implement date a
7af20 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 74 nd time.** funct
7af30 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e ions for SQLite.
7af40 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 .**.** There i
7af50 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 s only one expor
7af60 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 ted symbol in th
7af70 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 is file - the fu
7af80 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 nction.** sqlite
7af90 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 3RegisterDateTim
7afa0 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 eFunctions() fou
7afb0 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d nd at the bottom
7afc0 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a of the file..**
7afd0 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 All other code
7afe0 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a has file scope..
7aff0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61 74 65 2e **.** $Id: date.
7b000 63 2c 76 20 31 2e 39 32 20 32 30 30 38 2f 31 30 c,v 1.92 2008/10
7b010 2f 31 33 20 31 35 3a 33 35 3a 30 39 20 64 72 68 /13 15:35:09 drh
7b020 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 53 51 4c Exp $.**.** SQL
7b030 69 74 65 20 70 72 6f 63 65 73 73 65 73 20 61 6c ite processes al
7b040 6c 20 74 69 6d 65 73 20 61 6e 64 20 64 61 74 65 l times and date
7b050 73 20 61 73 20 4a 75 6c 69 61 6e 20 44 61 79 20 s as Julian Day
7b060 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 0a 2a 2a numbers. The.**
7b070 20 64 61 74 65 73 20 61 6e 64 20 74 69 6d 65 73 dates and times
7b080 20 61 72 65 20 73 74 6f 72 65 64 20 61 73 20 74 are stored as t
7b090 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 79 he number of day
7b0a0 73 20 73 69 6e 63 65 20 6e 6f 6f 6e 0a 2a 2a 20 s since noon.**
7b0b0 69 6e 20 47 72 65 65 6e 77 69 63 68 20 6f 6e 20 in Greenwich on
7b0c0 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 34 37 31 November 24, 471
7b0d0 34 20 42 2e 43 2e 20 61 63 63 6f 72 64 69 6e 67 4 B.C. according
7b0e0 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 to the Gregoria
7b0f0 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 72 20 73 79 n.** calendar sy
7b100 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 31 39 37 stem. .**.** 197
7b110 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 0-01-01 00:00:00
7b120 20 69 73 20 4a 44 20 32 34 34 30 35 38 37 2e 35 is JD 2440587.5
7b130 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 30 31 20 30 .** 2000-01-01 0
7b140 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 0:00:00 is JD 24
7b150 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 20 54 68 51544.5.**.** Th
7b160 69 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 is implemention
7b170 72 65 71 75 69 72 65 73 20 79 65 61 72 73 20 74 requires years t
7b180 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20 61 o be expressed a
7b190 73 20 61 20 34 2d 64 69 67 69 74 20 6e 75 6d 62 s a 4-digit numb
7b1a0 65 72 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e er.** which mean
7b1b0 73 20 74 68 61 74 20 6f 6e 6c 79 20 64 61 74 65 s that only date
7b1c0 73 20 62 65 74 77 65 65 6e 20 30 30 30 30 2d 30 s between 0000-0
7b1d0 31 2d 30 31 20 61 6e 64 20 39 39 39 39 2d 31 32 1-01 and 9999-12
7b1e0 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 65 20 72 65 -31 can.** be re
7b1f0 70 72 65 73 65 6e 74 65 64 2c 20 65 76 65 6e 20 presented, even
7b200 74 68 6f 75 67 68 20 6a 75 6c 69 61 6e 20 64 61 though julian da
7b210 79 20 6e 75 6d 62 65 72 73 20 61 6c 6c 6f 77 20 y numbers allow
7b220 61 20 6d 75 63 68 20 77 69 64 65 72 0a 2a 2a 20 a much wider.**
7b230 72 61 6e 67 65 20 6f 66 20 64 61 74 65 73 2e 0a range of dates..
7b240 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 65 67 6f 72 **.** The Gregor
7b250 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 73 79 73 ian calendar sys
7b260 74 65 6d 20 69 73 20 75 73 65 64 20 66 6f 72 20 tem is used for
7b270 61 6c 6c 20 64 61 74 65 73 20 61 6e 64 20 74 69 all dates and ti
7b280 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 6f mes,.** even tho
7b290 73 65 20 74 68 61 74 20 70 72 65 64 61 74 65 20 se that predate
7b2a0 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 the Gregorian ca
7b2b0 6c 65 6e 64 61 72 2e 20 20 48 69 73 74 6f 72 69 lendar. Histori
7b2c0 61 6e 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 75 ans usually.** u
7b2d0 73 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 63 61 se the Julian ca
7b2e0 6c 65 6e 64 61 72 20 66 6f 72 20 64 61 74 65 73 lendar for dates
7b2f0 20 70 72 69 6f 72 20 74 6f 20 31 35 38 32 2d 31 prior to 1582-1
7b300 30 2d 31 35 20 61 6e 64 20 66 6f 72 20 73 6f 6d 0-15 and for som
7b310 65 0a 2a 2a 20 64 61 74 65 73 20 61 66 74 65 72 e.** dates after
7b320 77 61 72 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 wards, depending
7b330 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 20 42 65 77 on locale. Bew
7b340 61 72 65 20 6f 66 20 74 68 69 73 20 64 69 66 66 are of this diff
7b350 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 erence..**.** Th
7b360 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 6c 67 e conversion alg
7b370 6f 72 69 74 68 6d 73 20 61 72 65 20 69 6d 70 6c orithms are impl
7b380 65 6d 65 6e 74 65 64 20 62 61 73 65 64 20 6f 6e emented based on
7b390 20 64 65 73 63 72 69 70 74 69 6f 6e 73 0a 2a 2a descriptions.**
7b3a0 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e in the followin
7b3b0 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 g text:.**.**
7b3c0 20 20 20 4a 65 61 6e 20 4d 65 65 75 73 0a 2a 2a Jean Meeus.**
7b3d0 20 20 20 20 20 20 41 73 74 72 6f 6e 6f 6d 69 63 Astronomic
7b3e0 61 6c 20 41 6c 67 6f 72 69 74 68 6d 73 2c 20 32 al Algorithms, 2
7b3f0 6e 64 20 45 64 69 74 69 6f 6e 2c 20 31 39 39 38 nd Edition, 1998
7b400 0a 2a 2a 20 20 20 20 20 20 49 53 42 4d 20 30 2d .** ISBM 0-
7b410 39 34 33 33 39 36 2d 36 31 2d 31 0a 2a 2a 20 20 943396-61-1.**
7b420 20 20 20 20 57 69 6c 6c 6d 61 6e 6e 2d 42 65 6c Willmann-Bel
7b430 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 20 20 20 52 l, Inc.** R
7b440 69 63 68 6d 6f 6e 64 2c 20 56 69 72 67 69 6e 69 ichmond, Virgini
7b450 61 20 28 55 53 41 29 0a 2a 2f 0a 23 69 6e 63 6c a (USA).*/.#incl
7b460 75 64 65 20 3c 63 74 79 70 65 2e 68 3e 0a 23 69 ude <ctype.h>.#i
7b470 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a nclude <time.h>.
7b480 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
7b490 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 OMIT_DATETIME_FU
7b4a0 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72 65 NCS../*.** On re
7b4b0 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c 61 cent Windows pla
7b4c0 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 61 tforms, the loca
7b4d0 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 69 ltime_s() functi
7b4e0 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a on is available.
7b4f0 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 ** as part of th
7b500 65 20 22 53 65 63 75 72 65 20 43 52 54 22 2e 20 e "Secure CRT".
7b510 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c It is essentiall
7b520 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 y equivalent to
7b530 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 .** localtime_r(
7b540 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64 65 ) available unde
7b550 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c 61 r most POSIX pla
7b560 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20 74 tforms, except t
7b570 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64 65 hat the .** orde
7b580 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 r of the paramet
7b590 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64 2e ers is reversed.
7b5a0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70 3a .**.** See http:
7b5b0 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66 74 //msdn.microsoft
7b5c0 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72 61 .com/en-us/libra
7b5d0 72 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e 38 ry/a442x3ye(VS.8
7b5e0 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20 49 0).aspx..**.** I
7b5f0 66 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e f the user has n
7b600 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f 20 ot indicated to
7b610 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 use localtime_r(
7b620 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 ) or localtime_s
7b630 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20 63 ().** already, c
7b640 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56 43 heck for an MSVC
7b650 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d 65 build environme
7b660 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64 65 73 nt that provides
7b670 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 .** localtime_s
7b680 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 ()..*/.#if !defi
7b690 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 ned(HAVE_LOCALTI
7b6a0 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e 65 ME_R) && !define
7b6b0 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 d(HAVE_LOCALTIME
7b6c0 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64 65 _S) && \. de
7b6d0 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 fined(_MSC_VER)
7b6e0 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54 5f && defined(_CRT_
7b6f0 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43 41 INSECURE_DEPRECA
7b700 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56 45 TE).#define HAVE
7b710 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a 23 _LOCALTIME_S 1.#
7b720 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 endif../*.** A s
7b730 74 72 75 63 74 75 72 65 20 66 6f 72 20 68 6f 6c tructure for hol
7b740 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 64 61 ding a single da
7b750 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a te and time..*/.
7b760 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 typedef struct D
7b770 61 74 65 54 69 6d 65 20 44 61 74 65 54 69 6d 65 ateTime DateTime
7b780 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54 69 6d ;.struct DateTim
7b790 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e e {. sqlite3_in
7b7a0 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 65 20 t64 iJD; /* The
7b7b0 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 julian day numbe
7b7c0 72 20 74 69 6d 65 73 20 38 36 34 30 30 30 30 30 r times 86400000
7b7d0 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 */. int Y, M,
7b7e0 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 61 72 D; /* Year
7b7f0 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61 79 , month, and day
7b800 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b 20 */. int h, m;
7b810 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75 72 /* Hour
7b820 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f 0a and minutes */.
7b830 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20 20 int tz;
7b840 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e 65 /* Timezone
7b850 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e 75 74 offset in minut
7b860 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 73 es */. double s
7b870 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 ; /* Se
7b880 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72 20 conds */. char
7b890 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f 2a validYMD; /*
7b8a0 20 54 72 75 65 20 69 66 20 59 2c 4d 2c 44 20 61 True if Y,M,D a
7b8b0 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 re valid */. ch
7b8c0 61 72 20 76 61 6c 69 64 48 4d 53 3b 20 20 20 20 ar validHMS;
7b8d0 20 2f 2a 20 54 72 75 65 20 69 66 20 68 2c 6d 2c /* True if h,m,
7b8e0 73 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 s are valid */.
7b8f0 20 63 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 char validJD;
7b900 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
7b910 4a 44 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 JD is valid */.
7b920 20 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 char validTZ;
7b930 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
7b940 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d 3b z is valid */.};
7b950 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 .../*.** Convert
7b960 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65 20 zDate into one
7b970 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72 73 or more integers
7b980 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72 . Additional ar
7b990 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65 20 guments.** come
7b9a0 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20 61 in groups of 5 a
7b9b0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
7b9c0 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 20 6e N n
7b9d0 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73 20 umber of digits
7b9e0 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a in the integer.*
7b9f0 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20 20 * min
7ba00 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 minimum allowed
7ba10 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 value of the int
7ba20 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 61 eger.** ma
7ba30 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61 6c x maximum al
7ba40 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 lowed value of t
7ba50 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 he integer.**
7ba60 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 72 73 nextC firs
7ba70 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74 65 t character afte
7ba80 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a r the integer.**
7ba90 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 20 77 pVal w
7baa0 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 here to write th
7bab0 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75 65 e integers value
7bac0 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 ..**.** Conversi
7bad0 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e 74 ons continue unt
7bae0 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78 74 il one with next
7baf0 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74 65 C==0 is encounte
7bb00 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e 63 red..** The func
7bb10 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
7bb20 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63 65 number of succe
7bb30 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f 6e ssful conversion
7bb40 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
7bb50 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73 74 getDigits(const
7bb60 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e 2e char *zDate, ..
7bb70 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 .){. va_list ap
7bb80 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20 69 ;. int val;. i
7bb90 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e 3b nt N;. int min;
7bba0 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69 6e . int max;. in
7bbb0 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20 2a t nextC;. int *
7bbc0 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74 20 pVal;. int cnt
7bbd0 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 = 0;. va_start(
7bbe0 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64 6f ap, zDate);. do
7bbf0 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72 67 {. N = va_arg
7bc00 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d (ap, int);. m
7bc10 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 in = va_arg(ap,
7bc20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d 20 int);. max =
7bc30 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b va_arg(ap, int);
7bc40 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61 5f . nextC = va_
7bc50 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 arg(ap, int);.
7bc60 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67 28 pVal = va_arg(
7bc70 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 76 ap, int*);. v
7bc80 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c al = 0;. whil
7bc90 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20 e( N-- ){.
7bca0 69 66 28 20 21 69 73 64 69 67 69 74 28 2a 28 75 if( !isdigit(*(u
7bcb0 38 2a 29 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 8*)zDate) ){.
7bcc0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 goto end_ge
7bcd0 74 44 69 67 69 74 73 3b 0a 20 20 20 20 20 20 7d tDigits;. }
7bce0 0a 20 20 20 20 20 20 76 61 6c 20 3d 20 76 61 6c . val = val
7bcf0 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20 27 *10 + *zDate - '
7bd00 30 27 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 0';. zDate+
7bd10 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 +;. }. if(
7bd20 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c 3e val<min || val>
7bd30 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43 21 3d 30 max || (nextC!=0
7bd40 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a 44 61 74 && nextC!=*zDat
7bd50 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f e) ){. goto
7bd60 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a end_getDigits;.
7bd70 20 20 20 20 7d 0a 20 20 20 20 2a 70 56 61 6c 20 }. *pVal
7bd80 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44 61 74 65 = val;. zDate
7bd90 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 ++;. cnt++;.
7bda0 20 7d 77 68 69 6c 65 28 20 6e 65 78 74 43 20 29 }while( nextC )
7bdb0 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3a ;.end_getDigits:
7bdc0 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 . va_end(ap);.
7bdd0 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a 0a return cnt;.}..
7bde0 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65 78 74 20 /*.** Read text
7bdf0 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f 6e from z[] and con
7be00 76 65 72 74 20 69 6e 74 6f 20 61 20 66 6c 6f 61 vert into a floa
7be10 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 ting point numbe
7be20 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 r. Return.** th
7be30 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 e number of digi
7be40 74 73 20 63 6f 6e 76 65 72 74 65 64 2e 0a 2a 2f ts converted..*/
7be50 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 6c 75 .#define getValu
7be60 65 20 73 71 6c 69 74 65 33 41 74 6f 46 0a 0a 2f e sqlite3AtoF../
7be70 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 74 69 6d *.** Parse a tim
7be80 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69 6f 6e 20 ezone extension
7be90 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 on the end of a
7bea0 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54 68 date-time..** Th
7beb0 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 6f e extension is o
7bec0 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a f the form:.**.*
7bed0 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d 29 48 48 * (+/-)HH
7bee0 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20 74 68 65 :MM.**.** Or the
7bef0 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74 69 6f 6e "zulu" notation
7bf00 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 5a :.**.** Z
7bf10 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
7bf20 72 73 65 20 69 73 20 73 75 63 63 65 73 73 66 75 rse is successfu
7bf30 6c 2c 20 77 72 69 74 65 20 74 68 65 20 6e 75 6d l, write the num
7bf40 62 65 72 20 6f 66 20 6d 69 6e 75 74 65 73 0a 2a ber of minutes.*
7bf50 2a 20 6f 66 20 63 68 61 6e 67 65 20 69 6e 20 70 * of change in p
7bf60 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75 72 6e 20 ->tz and return
7bf70 30 2e 20 20 49 66 20 61 20 70 61 72 73 65 72 20 0. If a parser
7bf80 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a error occurs,.**
7bf90 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f return non-zero
7bfa0 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73 73 69 6e ..**.** A missin
7bfb0 67 20 73 70 65 63 69 66 69 65 72 20 69 73 20 6e g specifier is n
7bfc0 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 20 61 6e ot considered an
7bfd0 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 error..*/.stati
7bfe0 63 20 69 6e 74 20 70 61 72 73 65 54 69 6d 65 7a c int parseTimez
7bff0 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a one(const char *
7c000 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 zDate, DateTime
7c010 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67 6e 20 3d *p){. int sgn =
7c020 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72 2c 20 6e 0;. int nHr, n
7c030 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 77 Mn;. int c;. w
7c040 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 hile( isspace(*(
7c050 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20 7a 44 u8*)zDate) ){ zD
7c060 61 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d 3e 74 7a ate++; }. p->tz
7c070 20 3d 20 30 3b 0a 20 20 63 20 3d 20 2a 7a 44 61 = 0;. c = *zDa
7c080 74 65 3b 0a 20 20 69 66 28 20 63 3d 3d 27 2d 27 te;. if( c=='-'
7c090 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2d 31 ){. sgn = -1
7c0a0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d ;. }else if( c=
7c0b0 3d 27 2b 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 ='+' ){. sgn
7c0c0 3d 20 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = +1;. }else if
7c0d0 28 20 63 3d 3d 27 5a 27 20 7c 7c 20 63 3d 3d 27 ( c=='Z' || c=='
7c0e0 7a 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b z' ){. zDate+
7c0f0 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 7a 75 6c 75 +;. goto zulu
7c100 5f 74 69 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a _time;. }else{.
7c110 20 20 20 20 72 65 74 75 72 6e 20 63 21 3d 30 3b return c!=0;
7c120 0a 20 20 7d 0a 20 20 7a 44 61 74 65 2b 2b 3b 0a . }. zDate++;.
7c130 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 if( getDigits(
7c140 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 31 34 2c zDate, 2, 0, 14,
7c150 20 27 3a 27 2c 20 26 6e 48 72 2c 20 32 2c 20 30 ':', &nHr, 2, 0
7c160 2c 20 35 39 2c 20 30 2c 20 26 6e 4d 6e 29 21 3d , 59, 0, &nMn)!=
7c170 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2 ){. return
7c180 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 1;. }. zDate +
7c190 3d 20 35 3b 0a 20 20 70 2d 3e 74 7a 20 3d 20 73 = 5;. p->tz = s
7c1a0 67 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72 2a 36 30 gn*(nMn + nHr*60
7c1b0 29 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a 0a 20 20 );.zulu_time:.
7c1c0 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a while( isspace(*
7c1d0 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20 7a (u8*)zDate) ){ z
7c1e0 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 Date++; }. retu
7c1f0 72 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a rn *zDate!=0;.}.
7c200 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69 6d ./*.** Parse tim
7c210 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 48 es of the form H
7c220 48 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 H:MM or HH:MM:SS
7c230 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 or HH:MM:SS.FFF
7c240 46 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d F..** The HH, MM
7c250 2c 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65 61 , and SS must ea
7c260 63 68 20 62 65 20 65 78 61 63 74 6c 79 20 32 20 ch be exactly 2
7c270 64 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a 20 digits. The.**
7c280 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e fractional secon
7c290 64 73 20 46 46 46 46 20 63 61 6e 20 62 65 20 6f ds FFFF can be o
7c2a0 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 ne or more digit
7c2b0 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 s..**.** Return
7c2c0 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 1 if there is a
7c2d0 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61 6e parsing error an
7c2e0 64 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a d 0 on success..
7c2f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
7c300 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74 20 rseHhMmSs(const
7c310 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 char *zDate, Dat
7c320 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 eTime *p){. int
7c330 20 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 h, m, s;. doub
7c340 6c 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 69 le ms = 0.0;. i
7c350 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 f( getDigits(zDa
7c360 74 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27 3a te, 2, 0, 24, ':
7c370 27 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39 2c ', &h, 2, 0, 59,
7c380 20 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20 20 0, &m)!=2 ){.
7c390 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
7c3a0 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 zDate += 5;.
7c3b0 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 if( *zDate==':'
7c3c0 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a ){. zDate++;.
7c3d0 20 20 20 20 69 66 28 20 67 65 74 44 69 67 69 74 if( getDigit
7c3e0 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 35 s(zDate, 2, 0, 5
7c3f0 39 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b 0a 9, 0, &s)!=1 ){.
7c400 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
7c410 20 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65 20 }. zDate
7c420 2b 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a 7a += 2;. if( *z
7c430 44 61 74 65 3d 3d 27 2e 27 20 26 26 20 69 73 64 Date=='.' && isd
7c440 69 67 69 74 28 28 75 38 29 7a 44 61 74 65 5b 31 igit((u8)zDate[1
7c450 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 ]) ){. doub
7c460 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b le rScale = 1.0;
7c470 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a . zDate++;.
7c480 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 while( isd
7c490 69 67 69 74 28 2a 28 75 38 2a 29 7a 44 61 74 65 igit(*(u8*)zDate
7c4a0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 ) ){. ms
7c4b0 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 = ms*10.0 + *zDa
7c4c0 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 te - '0';.
7c4d0 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 rScale *= 10.0
7c4e0 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 2b ;. zDate+
7c4f0 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 +;. }.
7c500 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 ms /= rScale;.
7c510 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
7c520 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 s = 0;. }. p
7c530 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 ->validJD = 0;.
7c540 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 p->validHMS = 1
7c550 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 ;. p->h = h;.
7c560 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 p->m = m;. p->s
7c570 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 = s + ms;. if(
7c580 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a parseTimezone(z
7c590 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 72 Date, p) ) retur
7c5a0 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 n 1;. p->validT
7c5b0 5a 20 3d 20 70 2d 3e 74 7a 21 3d 30 3b 0a 20 20 Z = p->tz!=0;.
7c5c0 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
7c5d0 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 ** Convert from
7c5e0 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d YYYY-MM-DD HH:MM
7c5f0 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e 20 64 61 :SS to julian da
7c600 79 2e 20 20 57 65 20 61 6c 77 61 79 73 20 61 73 y. We always as
7c610 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 sume.** that the
7c620 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 73 20 61 YYYY-MM-DD is a
7c630 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
7c640 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 Gregorian calend
7c650 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 ar..**.** Refere
7c660 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 61 67 65 nce: Meeus page
7c670 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 61.*/.static vo
7c680 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 44 61 74 id computeJD(Dat
7c690 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 eTime *p){. int
7c6a0 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 42 2c 20 Y, M, D, A, B,
7c6b0 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 28 20 70 X1, X2;.. if( p
7c6c0 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 65 74 75 ->validJD ) retu
7c6d0 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c rn;. if( p->val
7c6e0 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 59 20 3d idYMD ){. Y =
7c6f0 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 3d 20 70 p->Y;. M = p
7c700 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 70 2d 3e ->M;. D = p->
7c710 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 D;. }else{.
7c720 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a 20 49 66 Y = 2000; /* If
7c730 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 66 69 65 no YMD specifie
7c740 64 2c 20 61 73 73 75 6d 65 20 32 30 30 30 2d 4a d, assume 2000-J
7c750 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 4d 20 3d an-01 */. M =
7c760 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 3b 0a 20 1;. D = 1;.
7c770 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 20 29 7b }. if( M<=2 ){
7c780 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 20 4d 20 . Y--;. M
7c790 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 41 20 3d += 12;. }. A =
7c7a0 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d 20 32 20 Y/100;. B = 2
7c7b0 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 20 20 58 - A + (A/4);. X
7c7c0 31 20 3d 20 33 36 35 2e 32 35 2a 28 59 2b 34 37 1 = 365.25*(Y+47
7c7d0 31 36 29 3b 0a 20 20 58 32 20 3d 20 33 30 2e 36 16);. X2 = 30.6
7c7e0 30 30 31 2a 28 4d 2b 31 29 3b 0a 20 20 70 2d 3e 001*(M+1);. p->
7c7f0 69 4a 44 20 3d 20 28 58 31 20 2b 20 58 32 20 2b iJD = (X1 + X2 +
7c800 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 29 D + B - 1524.5)
7c810 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 70 2d 3e *86400000;. p->
7c820 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69 validJD = 1;. i
7c830 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 f( p->validHMS )
7c840 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 {. p->iJD +=
7c850 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 70 p->h*3600000 + p
7c860 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 70 2d 3e 73 ->m*60000 + p->s
7c870 2a 31 30 30 30 3b 0a 20 20 20 20 69 66 28 20 70 *1000;. if( p
7c880 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 ->validTZ ){.
7c890 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e p->iJD -= p->
7c8a0 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 20 20 tz*60000;.
7c8b0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b p->validYMD = 0;
7c8c0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 . p->validH
7c8d0 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d MS = 0;. p-
7c8e0 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 >validTZ = 0;.
7c8f0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
7c900 20 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20 Parse dates of
7c910 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 the form.**.**
7c920 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 YYYY-MM-DD HH
7c930 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20 :MM:SS.FFF.**
7c940 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a YYYY-MM-DD HH:
7c950 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59 MM:SS.** YYY
7c960 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a Y-MM-DD HH:MM.**
7c970 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a YYYY-MM-DD.
7c980 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
7c990 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 result into the
7c9a0 44 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 DateTime structu
7c9b0 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a re and return 0.
7c9c0 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e ** on success an
7c9d0 64 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 d 1 if the input
7c9e0 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 string is not a
7c9f0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 well-formed.**
7ca00 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 date..*/.static
7ca10 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44 int parseYyyyMmD
7ca20 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 d(const char *zD
7ca30 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 ate, DateTime *p
7ca40 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 ){. int Y, M, D
7ca50 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44 , neg;.. if( zD
7ca60 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 ate[0]=='-' ){.
7ca70 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
7ca80 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 neg = 1;. }else
7ca90 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 {. neg = 0;.
7caa0 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 }. if( getDigi
7cab0 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39 ts(zDate,4,0,999
7cac0 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c 9,'-',&Y,2,1,12,
7cad0 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c '-',&M,2,1,31,0,
7cae0 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65 &D)!=3 ){. re
7caf0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 turn 1;. }. zD
7cb00 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69 ate += 10;. whi
7cb10 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 le( isspace(*(u8
7cb20 2a 29 7a 44 61 74 65 29 20 7c 7c 20 27 54 27 3d *)zDate) || 'T'=
7cb30 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 7b 20 =*(u8*)zDate ){
7cb40 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 66 28 zDate++; }. if(
7cb50 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 parseHhMmSs(zDa
7cb60 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 te, p)==0 ){.
7cb70 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 20 74 /* We got the t
7cb80 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 ime */. }else i
7cb90 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 7b 0a f( *zDate==0 ){.
7cba0 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 p->validHMS
7cbb0 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
7cbc0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
7cbd0 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 p->validJD = 0
7cbe0 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 ;. p->validYMD
7cbf0 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 6e 65 = 1;. p->Y = ne
7cc00 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 70 2d g ? -Y : Y;. p-
7cc10 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 20 3d >M = M;. p->D =
7cc20 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c D;. if( p->val
7cc30 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f 6d 70 idTZ ){. comp
7cc40 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a 20 20 uteJD(p);. }.
7cc50 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
7cc60 2a 2a 20 53 65 74 20 74 68 65 20 74 69 6d 65 20 ** Set the time
7cc70 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 to the current t
7cc80 69 6d 65 20 72 65 70 6f 72 74 65 64 20 62 79 20 ime reported by
7cc90 74 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61 74 69 the VFS.*/.stati
7cca0 63 20 76 6f 69 64 20 73 65 74 44 61 74 65 54 69 c void setDateTi
7ccb0 6d 65 54 6f 43 75 72 72 65 6e 74 28 73 71 6c 69 meToCurrent(sqli
7ccc0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
7ccd0 74 65 78 74 2c 20 44 61 74 65 54 69 6d 65 20 2a text, DateTime *
7cce0 70 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a p){. double r;.
7ccf0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
7cd00 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f sqlite3_context_
7cd10 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 db_handle(contex
7cd20 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 t);. sqlite3OsC
7cd30 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 urrentTime(db->p
7cd40 56 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d 3e 69 Vfs, &r);. p->i
7cd50 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e JD = (sqlite3_in
7cd60 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e t64)(r*86400000.
7cd70 30 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d 3e 76 0 + 0.5);. p->v
7cd80 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a 0a 2f alidJD = 1;.}../
7cd90 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
7cda0 70 61 72 73 65 20 74 68 65 20 67 69 76 65 6e 20 parse the given
7cdb0 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a 75 string into a Ju
7cdc0 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72 2e lian Day Number.
7cdd0 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 Return.** the
7cde0 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 number of errors
7cdf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ..**.** The foll
7ce00 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65 70 74 owing are accept
7ce10 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20 74 able forms for t
7ce20 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 3a he input string:
7ce30 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59 59 .**.** YYYY
7ce40 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e -MM-DD HH:MM:SS.
7ce50 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a 2a FFF +/-HH:MM.**
7ce60 20 20 20 20 20 20 44 44 44 44 2e 44 44 20 0a 2a DDDD.DD .*
7ce70 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a 2a * now.**.**
7ce80 20 49 6e 20 74 68 65 20 66 69 72 73 74 20 66 6f In the first fo
7ce90 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d 4d rm, the +/-HH:MM
7cea0 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69 6f is always optio
7ceb0 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63 74 69 nal. The fracti
7cec0 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73 20 onal.** seconds
7ced0 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 20 22 extension (the "
7cee0 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f 6e .FFF") is option
7cef0 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 73 al. The seconds
7cf00 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a 53 portion.** (":S
7cf10 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f S.FFF") is optio
7cf20 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61 6e 64 n. The year and
7cf30 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f 6d 69 date can be omi
7cf40 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 tted as long.**
7cf50 61 73 20 74 68 65 72 65 20 69 73 20 61 20 74 69 as there is a ti
7cf60 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 me string. The
7cf70 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61 6e 20 time string can
7cf80 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f be omitted as lo
7cf90 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 ng.** as there i
7cfa0 73 20 61 20 79 65 61 72 20 61 6e 64 20 64 61 74 s a year and dat
7cfb0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
7cfc0 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d 65 parseDateOrTime
7cfd0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7cfe0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 ext *context, .
7cff0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 const char *zDa
7d000 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20 te, . DateTime
7d010 2a 70 0a 29 7b 0a 20 20 69 66 28 20 70 61 72 73 *p.){. if( pars
7d020 65 59 79 79 79 4d 6d 44 64 28 7a 44 61 74 65 2c eYyyyMmDd(zDate,
7d030 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 p)==0 ){. ret
7d040 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 urn 0;. }else i
7d050 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a f( parseHhMmSs(z
7d060 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 Date, p)==0 ){.
7d070 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
7d080 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 else if( sqlite3
7d090 53 74 72 49 43 6d 70 28 7a 44 61 74 65 2c 22 6e StrICmp(zDate,"n
7d0a0 6f 77 22 29 3d 3d 30 29 7b 0a 20 20 20 20 73 65 ow")==0){. se
7d0b0 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 tDateTimeToCurre
7d0c0 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a nt(context, p);.
7d0d0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
7d0e0 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 }else if( sqlite
7d0f0 33 49 73 4e 75 6d 62 65 72 28 7a 44 61 74 65 2c 3IsNumber(zDate,
7d100 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0, SQLITE_UTF8)
7d110 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 ){. double r
7d120 3b 0a 20 20 20 20 67 65 74 56 61 6c 75 65 28 7a ;. getValue(z
7d130 44 61 74 65 2c 20 26 72 29 3b 0a 20 20 20 20 70 Date, &r);. p
7d140 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 ->iJD = (sqlite3
7d150 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 _int64)(r*864000
7d160 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 00.0 + 0.5);.
7d170 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b p->validJD = 1;
7d180 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
7d190 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d }. return 1;.}
7d1a0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 ../*.** Compute
7d1b0 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68 2c the Year, Month,
7d1c0 20 61 6e 64 20 44 61 79 20 66 72 6f 6d 20 74 68 and Day from th
7d1d0 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d e julian day num
7d1e0 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ber..*/.static v
7d1f0 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 28 44 oid computeYMD(D
7d200 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 ateTime *p){. i
7d210 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 44 nt Z, A, B, C, D
7d220 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 28 20 70 , E, X1;. if( p
7d230 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 72 65 74 ->validYMD ) ret
7d240 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e 76 urn;. if( !p->v
7d250 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 70 2d alidJD ){. p-
7d260 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 70 >Y = 2000;. p
7d270 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e ->M = 1;. p->
7d280 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a D = 1;. }else{.
7d290 20 20 20 20 5a 20 3d 20 28 70 2d 3e 69 4a 44 20 Z = (p->iJD
7d2a0 2b 20 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 + 43200000)/8640
7d2b0 30 30 30 30 3b 0a 20 20 20 20 41 20 3d 20 28 5a 0000;. A = (Z
7d2c0 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33 - 1867216.25)/3
7d2d0 36 35 32 34 2e 32 35 3b 0a 20 20 20 20 41 20 3d 6524.25;. A =
7d2e0 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41 2f Z + 1 + A - (A/
7d2f0 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b 20 4);. B = A +
7d300 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28 42 1524;. C = (B
7d310 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e 32 35 - 122.1)/365.25
7d320 3b 0a 20 20 20 20 44 20 3d 20 33 36 35 2e 32 35 ;. D = 365.25
7d330 2a 43 3b 0a 20 20 20 20 45 20 3d 20 28 42 2d 44 *C;. E = (B-D
7d340 29 2f 33 30 2e 36 30 30 31 3b 0a 20 20 20 20 58 )/30.6001;. X
7d350 31 20 3d 20 33 30 2e 36 30 30 31 2a 45 3b 0a 20 1 = 30.6001*E;.
7d360 20 20 20 70 2d 3e 44 20 3d 20 42 20 2d 20 44 20 p->D = B - D
7d370 2d 20 58 31 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d - X1;. p->M =
7d380 20 45 3c 31 34 20 3f 20 45 2d 31 20 3a 20 45 2d E<14 ? E-1 : E-
7d390 31 33 3b 0a 20 20 20 20 70 2d 3e 59 20 3d 20 70 13;. p->Y = p
7d3a0 2d 3e 4d 3e 32 20 3f 20 43 20 2d 20 34 37 31 36 ->M>2 ? C - 4716
7d3b0 20 3a 20 43 20 2d 20 34 37 31 35 3b 0a 20 20 7d : C - 4715;. }
7d3c0 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d . p->validYMD =
7d3d0 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 1;.}../*.** Com
7d3e0 70 75 74 65 20 74 68 65 20 48 6f 75 72 2c 20 4d pute the Hour, M
7d3f0 69 6e 75 74 65 2c 20 61 6e 64 20 53 65 63 6f 6e inute, and Secon
7d400 64 73 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 ds from the juli
7d410 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a an day number..*
7d420 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f /.static void co
7d430 6d 70 75 74 65 48 4d 53 28 44 61 74 65 54 69 6d mputeHMS(DateTim
7d440 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 3b 0a e *p){. int s;.
7d450 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d if( p->validHM
7d460 53 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f S ) return;. co
7d470 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 73 20 mputeJD(p);. s
7d480 3d 20 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 = (p->iJD + 4320
7d490 30 30 30 30 29 20 25 20 38 36 34 30 30 30 30 30 0000) % 86400000
7d4a0 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30 ;. p->s = s/100
7d4b0 30 2e 30 3b 0a 20 20 73 20 3d 20 70 2d 3e 73 3b 0.0;. s = p->s;
7d4c0 0a 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a 20 20 . p->s -= s;.
7d4d0 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b 0a 20 p->h = s/3600;.
7d4e0 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 30 3b s -= p->h*3600;
7d4f0 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30 3b 0a . p->m = s/60;.
7d500 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20 70 2d p->s += s - p-
7d510 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 >m*60;. p->vali
7d520 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a dHMS = 1;.}../*.
7d530 2a 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74 68 20 ** Compute both
7d540 59 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f 0a 73 YMD and HMS.*/.s
7d550 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 tatic void compu
7d560 74 65 59 4d 44 5f 48 4d 53 28 44 61 74 65 54 69 teYMD_HMS(DateTi
7d570 6d 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70 75 74 me *p){. comput
7d580 65 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d 70 75 eYMD(p);. compu
7d590 74 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a teHMS(p);.}../*.
7d5a0 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59 4d 44 ** Clear the YMD
7d5b0 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 68 65 and HMS and the
7d5c0 20 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f TZ.*/.static vo
7d5d0 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f id clearYMD_HMS_
7d5e0 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b TZ(DateTime *p){
7d5f0 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d . p->validYMD =
7d600 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 0;. p->validHM
7d610 53 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 S = 0;. p->vali
7d620 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e dTZ = 0;.}..#ifn
7d630 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
7d640 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a 2a 20 LOCALTIME./*.**
7d650 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69 66 66 Compute the diff
7d660 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c 6c 69 erence (in milli
7d670 73 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65 74 77 seconds).** betw
7d680 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61 6e een localtime an
7d690 64 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d d UTC (a.k.a. GM
7d6a0 54 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 69 T).** for the ti
7d6b0 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65 72 65 me value p where
7d6c0 20 70 20 69 73 20 69 6e 20 55 54 43 2e 0a 2a 2f p is in UTC..*/
7d6d0 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 61 .static int loca
7d6e0 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74 65 ltimeOffset(Date
7d6f0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 65 Time *p){. Date
7d700 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d Time x, y;. tim
7d710 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 3b e_t t;. x = *p;
7d720 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d . computeYMD_HM
7d730 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e 59 S(&x);. if( x.Y
7d740 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 <1971 || x.Y>=20
7d750 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 20 38 ){. x.Y =
7d760 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 2000;. x.M =
7d770 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 0a 1;. x.D = 1;.
7d780 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 20 x.h = 0;.
7d790 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 2e x.m = 0;. x.
7d7a0 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 s = 0.0;. } els
7d7b0 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d 20 e {. int s =
7d7c0 78 2e 73 20 2b 20 30 2e 35 3b 0a 20 20 20 20 78 x.s + 0.5;. x
7d7d0 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 20 78 2e .s = s;. }. x.
7d7e0 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 61 6c 69 tz = 0;. x.vali
7d7f0 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 dJD = 0;. compu
7d800 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 20 3d 20 teJD(&x);. t =
7d810 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 34 34 x.iJD/1000 - 244
7d820 30 35 38 37 2e 35 2a 38 36 34 30 30 2e 30 3b 0a 0587.5*86400.0;.
7d830 23 69 66 64 65 66 20 48 41 56 45 5f 4c 4f 43 41 #ifdef HAVE_LOCA
7d840 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 LTIME_R. {.
7d850 73 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c struct tm sLocal
7d860 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f ;. localtime_
7d870 72 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a r(&t, &sLocal);.
7d880 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c y.Y = sLocal
7d890 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b .tm_year + 1900;
7d8a0 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 . y.M = sLoca
7d8b0 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 l.tm_mon + 1;.
7d8c0 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.D = sLocal.t
7d8d0 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 m_mday;. y.h
7d8e0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 = sLocal.tm_hour
7d8f0 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 ;. y.m = sLoc
7d900 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 al.tm_min;. y
7d910 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 .s = sLocal.tm_s
7d920 65 63 3b 0a 20 20 7d 0a 23 65 6c 69 66 20 64 65 ec;. }.#elif de
7d930 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c fined(HAVE_LOCAL
7d940 54 49 4d 45 5f 53 29 0a 20 20 7b 0a 20 20 20 20 TIME_S). {.
7d950 73 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c struct tm sLocal
7d960 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f ;. localtime_
7d970 73 28 26 73 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a s(&sLocal, &t);.
7d980 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c y.Y = sLocal
7d990 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b .tm_year + 1900;
7d9a0 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 . y.M = sLoca
7d9b0 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 l.tm_mon + 1;.
7d9c0 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.D = sLocal.t
7d9d0 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 m_mday;. y.h
7d9e0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 = sLocal.tm_hour
7d9f0 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 ;. y.m = sLoc
7da00 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 al.tm_min;. y
7da10 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 .s = sLocal.tm_s
7da20 65 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 ec;. }.#else.
7da30 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 {. struct tm
7da40 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 *pTm;. sqlite
7da50 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 3_mutex_enter(sq
7da60 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
7da70 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
7da80 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 TIC_MASTER));.
7da90 20 20 70 54 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d pTm = localtim
7daa0 65 28 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d e(&t);. y.Y =
7dab0 20 70 54 6d 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 pTm->tm_year +
7dac0 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 1900;. y.M =
7dad0 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b pTm->tm_mon + 1;
7dae0 0a 20 20 20 20 79 2e 44 20 3d 20 70 54 6d 2d 3e . y.D = pTm->
7daf0 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 tm_mday;. y.h
7db00 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b = pTm->tm_hour;
7db10 0a 20 20 20 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e . y.m = pTm->
7db20 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 tm_min;. y.s
7db30 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 = pTm->tm_sec;.
7db40 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
7db50 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 _leave(sqlite3Mu
7db60 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
7db70 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
7db80 54 45 52 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 TER));. }.#endi
7db90 66 0a 20 20 79 2e 76 61 6c 69 64 59 4d 44 20 3d f. y.validYMD =
7dba0 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64 48 4d 53 1;. y.validHMS
7dbb0 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64 4a = 1;. y.validJ
7dbc0 44 20 3d 20 30 3b 0a 20 20 79 2e 76 61 6c 69 64 D = 0;. y.valid
7dbd0 54 5a 20 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 74 TZ = 0;. comput
7dbe0 65 4a 44 28 26 79 29 3b 0a 20 20 72 65 74 75 72 eJD(&y);. retur
7dbf0 6e 20 79 2e 69 4a 44 20 2d 20 78 2e 69 4a 44 3b n y.iJD - x.iJD;
7dc00 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
7dc10 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 ITE_OMIT_LOCALTI
7dc20 4d 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f ME */../*.** Pro
7dc30 63 65 73 73 20 61 20 6d 6f 64 69 66 69 65 72 20 cess a modifier
7dc40 74 6f 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73 to a date-time s
7dc50 74 61 6d 70 2e 20 20 54 68 65 20 6d 6f 64 69 66 tamp. The modif
7dc60 69 65 72 73 20 61 72 65 0a 2a 2a 20 61 73 20 66 iers are.** as f
7dc70 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
7dc80 20 20 4e 4e 4e 20 64 61 79 73 0a 2a 2a 20 20 20 NNN days.**
7dc90 20 20 4e 4e 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 NNN hours.**
7dca0 20 20 20 4e 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a NNN minutes.*
7dcb0 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 * NNN.NNNN s
7dcc0 65 63 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 4e 4e econds.** NN
7dcd0 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20 20 20 20 20 N months.**
7dce0 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a 20 20 20 20 NNN years.**
7dcf0 20 73 74 61 72 74 20 6f 66 20 6d 6f 6e 74 68 0a start of month.
7dd00 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 ** start of
7dd10 79 65 61 72 0a 2a 2a 20 20 20 20 20 73 74 61 72 year.** star
7dd20 74 20 6f 66 20 77 65 65 6b 0a 2a 2a 20 20 20 20 t of week.**
7dd30 20 73 74 61 72 74 20 6f 66 20 64 61 79 0a 2a 2a start of day.**
7dd40 20 20 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 2a weekday N.*
7dd50 2a 20 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a * unixepoch.
7dd60 2a 2a 20 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 ** localtime
7dd70 0a 2a 2a 20 20 20 20 20 75 74 63 0a 2a 2a 0a 2a .** utc.**.*
7dd80 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 * Return 0 on su
7dd90 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 ccess and 1 if t
7dda0 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69 6e 64 here is any kind
7ddb0 20 6f 66 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 of error..*/.st
7ddc0 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 4d 6f atic int parseMo
7ddd0 64 69 66 69 65 72 28 63 6f 6e 73 74 20 63 68 61 difier(const cha
7dde0 72 20 2a 7a 4d 6f 64 2c 20 44 61 74 65 54 69 6d r *zMod, DateTim
7ddf0 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 e *p){. int rc
7de00 3d 20 31 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 = 1;. int n;.
7de10 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 double r;. char
7de20 20 2a 7a 2c 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 *z, zBuf[30];.
7de30 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 66 6f 72 z = zBuf;. for
7de40 28 6e 3d 30 3b 20 6e 3c 73 69 7a 65 6f 66 28 7a (n=0; n<sizeof(z
7de50 42 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e Buf)-1 && zMod[n
7de60 5d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e ]; n++){. z[n
7de70 5d 20 3d 20 74 6f 6c 6f 77 65 72 28 7a 4d 6f 64 ] = tolower(zMod
7de80 5b 6e 5d 29 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d [n]);. }. z[n]
7de90 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 = 0;. switch(
7dea0 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20 z[0] ){.#ifndef
7deb0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 SQLITE_OMIT_LOCA
7dec0 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20 27 LTIME. case '
7ded0 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 l': {. /*
7dee0 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20 20 localtime.
7def0 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73 **. ** As
7df00 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72 65 suming the curre
7df10 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69 73 nt time value is
7df20 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 UTC (a.k.a. GMT
7df30 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a 20 ), shift it to.
7df40 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 ** show loc
7df50 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 2a al time.. *
7df60 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 /. if( strc
7df70 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 mp(z, "localtime
7df80 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7df90 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
7dfa0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d p->iJD +=
7dfb0 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 localtimeOffset
7dfc0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 (p);. cle
7dfd0 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
7dfe0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b . rc = 0;
7dff0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
7e000 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 reak;. }.#end
7e010 69 66 0a 20 20 20 20 63 61 73 65 20 27 75 27 3a if. case 'u':
7e020 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 {. /*.
7e030 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f 63 ** unixepoc
7e040 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 h. **.
7e050 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63 75 ** Treat the cu
7e060 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 rrent value of p
7e070 2d 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75 6d ->iJD as the num
7e080 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 ber of. **
7e090 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 seconds since 19
7e0a0 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f 20 70. Convert to
7e0b0 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61 a real julian da
7e0c0 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20 20 y number..
7e0d0 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 */. if( str
7e0e0 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63 cmp(z, "unixepoc
7e0f0 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c h")==0 && p->val
7e100 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20 20 idJD ){.
7e110 70 2d 3e 69 4a 44 20 3d 20 70 2d 3e 69 4a 44 2f p->iJD = p->iJD/
7e120 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 86400.0 + 244058
7e130 37 2e 35 2a 38 36 34 30 30 30 30 30 2e 30 3b 0a 7.5*86400000.0;.
7e140 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 clearYMD
7e150 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 _HMS_TZ(p);.
7e160 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
7e170 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
7e180 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d TE_OMIT_LOCALTIM
7e190 45 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 E. else if(
7e1a0 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22 strcmp(z, "utc"
7e1b0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
7e1c0 69 6e 74 20 63 31 3b 0a 20 20 20 20 20 20 20 20 int c1;.
7e1d0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
7e1e0 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c c1 = local
7e1f0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 timeOffset(p);.
7e200 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d p->iJD -=
7e210 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 c1;. cle
7e220 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
7e230 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
7e240 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d += c1 - localtim
7e250 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 eOffset(p);.
7e260 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
7e270 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
7e280 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
7e290 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 case 'w': {.
7e2a0 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 /*. **
7e2b0 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20 weekday N.
7e2c0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d **. ** M
7e2d0 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20 ove the date to
7e2e0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e the same time on
7e2f0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72 the next occurr
7e300 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a ence of. **
7e310 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65 weekday N where
7e320 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d 0==Sunday, 1==M
7e330 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f onday, and so fo
7e340 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20 rth. If the.
7e350 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c ** date is al
7e360 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70 ready on the app
7e370 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79 ropriate weekday
7e380 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f , this is a no-o
7e390 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 p.. */.
7e3a0 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c if( strncmp(z,
7e3b0 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d "weekday ", 8)=
7e3c0 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26 =0 && getValue(&
7e3d0 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20 z[8],&r)>0.
7e3e0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 && (
7e3f0 6e 3d 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 n=r)==r && n>=0
7e400 26 26 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 && r<7 ){.
7e410 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
7e420 5a 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 Z;. compu
7e430 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 teYMD_HMS(p);.
7e440 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a p->validTZ
7e450 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d = 0;. p-
7e460 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 >validJD = 0;.
7e470 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
7e480 70 29 3b 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 p);. Z =
7e490 28 28 70 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 ((p->iJD + 12960
7e4a0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 20 0000)/86400000)
7e4b0 25 20 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28 % 7;. if(
7e4c0 20 5a 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 Z>n ) Z -= 7;.
7e4d0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d p->iJD +=
7e4e0 20 28 6e 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 (n - Z)*8640000
7e4f0 30 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 0;. clear
7e500 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 YMD_HMS_TZ(p);.
7e510 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
7e520 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
7e530 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
7e540 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 se 's': {.
7e550 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73 /*. ** s
7e560 74 61 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20 tart of TTTTT.
7e570 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
7e580 4d 6f 76 65 20 74 68 65 20 64 61 74 65 20 62 61 Move the date ba
7e590 63 6b 77 61 72 64 73 20 74 6f 20 74 68 65 20 62 ckwards to the b
7e5a0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
7e5b0 63 75 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 current day,.
7e5c0 20 20 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f ** or month o
7e5d0 72 20 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f r year.. */
7e5e0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 . if( strnc
7e5f0 6d 70 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 mp(z, "start of
7e600 22 2c 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b ", 9)!=0 ) break
7e610 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a ;. z += 9;.
7e620 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 computeYMD
7e630 28 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 (p);. p->va
7e640 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 lidHMS = 1;.
7e650 20 20 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 p->h = p->m =
7e660 30 3b 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 0;. p->s =
7e670 30 2e 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 0.0;. p->va
7e680 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 lidTZ = 0;.
7e690 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b p->validJD = 0;
7e6a0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d . if( strcm
7e6b0 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 p(z,"month")==0
7e6c0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 ){. p->D
7e6d0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 = 1;. rc
7e6e0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
7e6f0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 if( strcmp(z,"y
7e700 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 ear")==0 ){.
7e710 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 computeYMD(p
7e720 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 );. p->M
7e730 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e = 1;. p->
7e740 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 D = 1;. r
7e750 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c c = 0;. }el
7e760 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c se if( strcmp(z,
7e770 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 "day")==0 ){.
7e780 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7e790 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
7e7a0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
7e7b0 20 27 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '+':. case '
7e7c0 2d 27 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27 -':. case '0'
7e7d0 3a 0a 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a :. case '1':.
7e7e0 20 20 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 case '2':.
7e7f0 20 20 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20 case '3':.
7e800 63 61 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61 case '4':. ca
7e810 73 65 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65 se '5':. case
7e820 20 27 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '6':. case '
7e830 37 27 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27 7':. case '8'
7e840 3a 0a 20 20 20 20 63 61 73 65 20 27 39 27 3a 20 :. case '9':
7e850 7b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74 56 {. n = getV
7e860 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20 20 alue(z, &r);.
7e870 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 20 assert( n>=1
7e880 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e );. if( z[n
7e890 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 ]==':' ){.
7e8a0 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 20 /* A modifier
7e8b0 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d of the form (+|-
7e8c0 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64 )HH:MM:SS.FFF ad
7e8d0 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 73 ds (or subtracts
7e8e0 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a ) the. **
7e8f0 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 65 specified numbe
7e900 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75 r of hours, minu
7e910 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e tes, seconds, an
7e920 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 d fractional sec
7e930 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 onds. **
7e940 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54 68 to the time. Th
7e950 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65 20 e ".FFF" may be
7e960 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22 3a omitted. The ":
7e970 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a 20 SS.FFF" may be.
7e980 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65 ** omitte
7e990 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 d.. */.
7e9a0 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
7e9b0 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20 *z2 = z;.
7e9c0 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a 20 DateTime tx;.
7e9d0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
7e9e0 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20 20 nt64 day;.
7e9f0 20 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a if( !isdigit(*
7ea00 28 75 38 2a 29 7a 32 29 20 29 20 7a 32 2b 2b 3b (u8*)z2) ) z2++;
7ea10 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 . memset(
7ea20 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 &tx, 0, sizeof(t
7ea30 78 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 x));. if(
7ea40 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c parseHhMmSs(z2,
7ea50 20 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 &tx) ) break;.
7ea60 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
7ea70 28 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 (&tx);. t
7ea80 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 x.iJD -= 4320000
7ea90 30 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 0;. day =
7eaa0 20 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 tx.iJD/86400000
7eab0 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 ;. tx.iJD
7eac0 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 -= day*86400000
7ead0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b ;. if( z[
7eae0 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 0]=='-' ) tx.iJD
7eaf0 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 = -tx.iJD;.
7eb00 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
7eb10 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 ;. clearY
7eb20 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 MD_HMS_TZ(p);.
7eb30 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
7eb40 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 tx.iJD;.
7eb50 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 rc = 0;.
7eb60 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
7eb70 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 z += n;.
7eb80 20 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 while( isspac
7eb90 65 28 2a 28 75 38 2a 29 7a 29 20 29 20 7a 2b 2b e(*(u8*)z) ) z++
7eba0 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 74 72 6c ;. n = strl
7ebb0 65 6e 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 en(z);. if(
7ebc0 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62 n>10 || n<3 ) b
7ebd0 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 reak;. if(
7ebe0 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a z[n-1]=='s' ){ z
7ebf0 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 [n-1] = 0; n--;
7ec00 7d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a }. computeJ
7ec10 44 28 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d D(p);. rc =
7ec20 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 0;. if( n=
7ec30 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 =3 && strcmp(z,"
7ec40 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 day")==0 ){.
7ec50 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 72 2a p->iJD += r*
7ec60 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 86400000.0 + 0.5
7ec70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
7ec80 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 ( n==4 && strcmp
7ec90 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30 20 29 7b (z,"hour")==0 ){
7eca0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
7ecb0 2b 3d 20 72 2a 28 38 36 34 30 30 30 30 30 2e 30 += r*(86400000.0
7ecc0 2f 32 34 2e 30 29 20 2b 20 30 2e 35 3b 0a 20 20 /24.0) + 0.5;.
7ecd0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d }else if( n=
7ece0 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 =6 && strcmp(z,"
7ecf0 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 minute")==0 ){.
7ed00 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d p->iJD +=
7ed10 20 72 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 r*(86400000.0/(
7ed20 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20 30 2e 24.0*60.0)) + 0.
7ed30 35 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 5;. }else i
7ed40 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d f( n==6 && strcm
7ed50 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 p(z,"second")==0
7ed60 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
7ed70 4a 44 20 2b 3d 20 72 2a 28 38 36 34 30 30 30 30 JD += r*(8640000
7ed80 30 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 0.0/(24.0*60.0*6
7ed90 30 2e 30 29 29 20 2b 20 30 2e 35 3b 0a 20 20 20 0.0)) + 0.5;.
7eda0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d }else if( n==
7edb0 35 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 5 && strcmp(z,"m
7edc0 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 onth")==0 ){.
7edd0 20 20 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 int x, y;.
7ede0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d computeYM
7edf0 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 D_HMS(p);.
7ee00 20 20 70 2d 3e 4d 20 2b 3d 20 72 3b 0a 20 20 20 p->M += r;.
7ee10 20 20 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 x = p->M>0
7ee20 3f 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 ? (p->M-1)/12 :
7ee30 28 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 (p->M-12)/12;.
7ee40 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b p->Y += x;
7ee50 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d . p->M -=
7ee60 20 78 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70 x*12;. p
7ee70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 ->validJD = 0;.
7ee80 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
7ee90 28 70 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d (p);. y =
7eea0 20 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 r;. if(
7eeb0 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 y!=r ){.
7eec0 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 72 20 2d p->iJD += (r -
7eed0 20 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 y)*30.0*8640000
7eee0 30 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20 0.0 + 0.5;.
7eef0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
7ef00 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 if( n==4 && str
7ef10 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 cmp(z,"year")==0
7ef20 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 ){. comp
7ef30 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 uteYMD_HMS(p);.
7ef40 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 72 p->Y += r
7ef50 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c ;. p->val
7ef60 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 idJD = 0;.
7ef70 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7ef80 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
7ef90 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 rc = 1;.
7efa0 20 20 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 }. clear
7efb0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 YMD_HMS_TZ(p);.
7efc0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7efd0 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
7efe0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
7eff0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
7f000 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 rc;.}../*.** Pr
7f010 6f 63 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74 ocess time funct
7f020 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 ion arguments.
7f030 61 72 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74 argv[0] is a dat
7f040 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a e-time stamp..**
7f050 20 61 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c argv[1] and fol
7f060 6c 6f 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 lowing are modif
7f070 69 65 72 73 2e 20 20 50 61 72 73 65 20 74 68 65 iers. Parse the
7f080 6d 20 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a m all and write.
7f090 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 ** the resulting
7f0a0 20 74 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 time into the D
7f0b0 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 ateTime structur
7f0c0 65 20 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a e p. Return 0.*
7f0d0 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 * on success and
7f0e0 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20 1 if there are
7f0f0 61 6e 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a any errors..**.*
7f100 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 7a * If there are z
7f110 65 72 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28 ero parameters (
7f120 69 66 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20 if even argv[0]
7f130 69 73 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a is undefined).**
7f140 20 74 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64 then assume a d
7f150 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 efault value of
7f160 22 6e 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30 "now" for argv[0
7f170 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 ]..*/.static int
7f180 20 69 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74 isDate(. sqlit
7f190 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
7f1a0 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 ext, . int argc
7f1b0 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c , . sqlite3_val
7f1c0 75 65 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61 ue **argv, . Da
7f1d0 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 teTime *p.){. i
7f1e0 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e nt i;. const un
7f1f0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a signed char *z;.
7f200 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d int eType;. m
7f210 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 emset(p, 0, size
7f220 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61 of(*p));. if( a
7f230 72 67 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65 rgc==0 ){. se
7f240 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 tDateTimeToCurre
7f250 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a nt(context, p);.
7f260 20 20 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79 }else if( (eTy
7f270 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c pe = sqlite3_val
7f280 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
7f290 29 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a )==SQLITE_FLOAT.
7f2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f2b0 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c || eType==SQL
7f2c0 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 ITE_INTEGER ){.
7f2d0 20 20 20 70 2d 3e 69 4a 44 20 3d 20 73 71 6c 69 p->iJD = sqli
7f2e0 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 te3_value_double
7f2f0 28 61 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30 (argv[0])*864000
7f300 30 30 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 00.0 + 0.5;.
7f310 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a p->validJD = 1;.
7f320 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d }else{. z =
7f330 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
7f340 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
7f350 20 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 73 if( !z || pars
7f360 65 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e 74 eDateOrTime(cont
7f370 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 70 ext, (char*)z, p
7f380 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
7f390 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
7f3a0 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 for(i=1; i<argc
7f3b0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
7f3c0 28 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c (z = sqlite3_val
7f3d0 75 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29 ue_text(argv[i])
7f3e0 29 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64 )==0 || parseMod
7f3f0 69 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c 20 ifier((char*)z,
7f400 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 p) ){. retu
7f410 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 1;. }. }.
7f420 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a return 0;.}...
7f430 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
7f440 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 ing routines imp
7f450 6c 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f lement the vario
7f460 75 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 us date and time
7f470 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 functions.** of
7f480 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a SQLite..*/../*.
7f490 2a 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 ** julianday(
7f4a0 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 TIMESTRING, MOD
7f4b0 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a , MOD, ...).**.*
7f4c0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 6c * Return the jul
7f4d0 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 6f ian day number o
7f4e0 66 20 74 68 65 20 64 61 74 65 20 73 70 65 63 69 f the date speci
7f4f0 66 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 75 fied in the argu
7f500 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 ments.*/.static
7f510 76 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 void juliandayFu
7f520 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
7f530 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
7f540 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
7f550 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
7f560 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 gv.){. DateTime
7f570 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 x;. if( isDate
7f580 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 (context, argc,
7f590 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a argv, &x)==0 ){.
7f5a0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 computeJD(&x
7f5b0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 );. sqlite3_r
7f5c0 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e esult_double(con
7f5d0 74 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 text, x.iJD/8640
7f5e0 30 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 0000.0);. }.}..
7f5f0 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d /*.** datetim
7f600 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d e( TIMESTRING, M
7f610 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a OD, MOD, ...).**
7f620 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d .** Return YYYY-
7f630 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a MM-DD HH:MM:SS.*
7f640 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 /.static void da
7f650 74 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 tetimeFunc(. sq
7f660 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
7f670 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
7f680 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
7f690 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
7f6a0 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 DateTime x;. if
7f6b0 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 ( isDate(context
7f6c0 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 , argc, argv, &x
7f6d0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 )==0 ){. char
7f6e0 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 zBuf[100];.
7f6f0 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 computeYMD_HMS(&
7f700 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f x);. sqlite3_
7f710 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
7f720 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 zBuf), zBuf, "%0
7f730 34 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32 4d-%02d-%02d %02
7f740 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 d:%02d:%02d",.
7f750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f760 20 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 x.Y, x.M, x.D
7f770 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 , x.h, x.m, (int
7f780 29 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c )(x.s));. sql
7f790 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
7f7a0 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 (context, zBuf,
7f7b0 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 -1, SQLITE_TRANS
7f7c0 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a IENT);. }.}../*
7f7d0 0a 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d .** time( TIM
7f7e0 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f ESTRING, MOD, MO
7f7f0 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 D, ...).**.** Re
7f800 74 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f turn HH:MM:SS.*/
7f810 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d .static void tim
7f820 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
7f830 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7f840 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
7f850 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7f860 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 *argv.){. DateT
7f870 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 ime x;. if( isD
7f880 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 ate(context, arg
7f890 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 c, argv, &x)==0
7f8a0 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 ){. char zBuf
7f8b0 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 [100];. compu
7f8c0 74 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 teHMS(&x);. s
7f8d0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7f8e0 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 sizeof(zBuf), zB
7f8f0 75 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25 uf, "%02d:%02d:%
7f900 30 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 02d", x.h, x.m,
7f910 28 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 (int)x.s);. s
7f920 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
7f930 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 xt(context, zBuf
7f940 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 , -1, SQLITE_TRA
7f950 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a NSIENT);. }.}..
7f960 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54 /*.** date( T
7f970 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 IMESTRING, MOD,
7f980 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 MOD, ...).**.**
7f990 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 Return YYYY-MM-D
7f9a0 44 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 D.*/.static void
7f9b0 20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c dateFunc(. sql
7f9c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7f9d0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
7f9e0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
7f9f0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 ue **argv.){. D
7fa00 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 ateTime x;. if(
7fa10 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c isDate(context,
7fa20 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 argc, argv, &x)
7fa30 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 ==0 ){. char
7fa40 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 zBuf[100];. c
7fa50 6f 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 omputeYMD(&x);.
7fa60 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
7fa70 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 ntf(sizeof(zBuf)
7fa80 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 , zBuf, "%04d-%0
7fa90 32 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2d-%02d", x.Y, x
7faa0 2e 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 .M, x.D);. sq
7fab0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
7fac0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
7fad0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
7fae0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f SIENT);. }.}../
7faf0 2a 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65 *.** strftime
7fb00 28 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 ( FORMAT, TIMEST
7fb10 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 RING, MOD, MOD,
7fb20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ...).**.** Retur
7fb30 6e 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72 n a string descr
7fb40 69 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 ibed by FORMAT.
7fb50 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 Conversions as
7fb60 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
7fb70 20 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 %d day of mont
7fb80 68 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 h.** %f ** fr
7fb90 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 actional seconds
7fba0 20 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 SS.SSS.** %H
7fbb0 20 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 hour 00-24.**
7fbc0 20 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61 %j day of yea
7fbd0 72 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 r 000-366.** %
7fbe0 4a 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 J ** Julian day
7fbf0 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 number.** %m
7fc00 20 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 month 01-12.**
7fc10 20 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d %M minute 00-
7fc20 35 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 59.** %s seco
7fc30 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30 nds since 1970-0
7fc40 31 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 1-01.** %S se
7fc50 63 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 conds 00-59.**
7fc60 20 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b %w day of week
7fc70 20 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 0-6 sunday==0.
7fc80 2a 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66 ** %W week of
7fc90 20 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 year 00-53.**
7fca0 20 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39 %Y year 0000-9
7fcb0 39 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 999.** %% %.*
7fcc0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 /.static void st
7fcd0 72 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 rftimeFunc(. sq
7fce0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
7fcf0 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
7fd00 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
7fd10 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
7fd20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 DateTime x;. u6
7fd30 34 20 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 4 n;. int i, j;
7fd40 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 . char *z;. sq
7fd50 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e lite3 *db;. con
7fd60 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 st char *zFmt =
7fd70 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c (const char*)sql
7fd80 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
7fd90 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 argv[0]);. char
7fda0 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 zBuf[100];. if
7fdb0 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 ( zFmt==0 || isD
7fdc0 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 ate(context, arg
7fdd0 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 c-1, argv+1, &x)
7fde0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 ) return;. db
7fdf0 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 = sqlite3_contex
7fe00 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 t_db_handle(cont
7fe10 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c ext);. for(i=0,
7fe20 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 n=1; zFmt[i]; i
7fe30 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, n++){. if
7fe40 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 ( zFmt[i]=='%' )
7fe50 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 {. switch(
7fe60 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 zFmt[i+1] ){.
7fe70 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a 20 case 'd':.
7fe80 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a case 'H':
7fe90 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6d . case 'm
7fea0 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 ':. case
7feb0 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 'M':. cas
7fec0 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 63 e 'S':. c
7fed0 61 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20 20 ase 'W':.
7fee0 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 n++;.
7fef0 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a /* fall thru *
7ff00 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 /. case '
7ff10 77 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 w':. case
7ff20 20 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20 20 '%':.
7ff30 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
7ff40 61 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20 20 ase 'f':.
7ff50 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 n += 8;.
7ff60 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7ff70 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 case 'j':.
7ff80 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a n += 3;.
7ff90 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7ffa0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59 . case 'Y
7ffb0 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b ':. n +
7ffc0 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 = 8;. b
7ffd0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
7ffe0 73 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20 20 se 's':.
7fff0 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 case 'J':.
80000 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 n += 50;.
80010 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
80020 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 default:.
80030 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b return;
80040 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 /* ERROR. ret
80050 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 urn a NULL */.
80060 20 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b }. i++;
80070 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
80080 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 n<sizeof(zBuf)
80090 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b ){. z = zBuf;
800a0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e 64 . }else if( n>d
800b0 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
800c0 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 _LIMIT_LENGTH] )
800d0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 {. sqlite3_re
800e0 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 sult_error_toobi
800f0 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 g(context);.
80100 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b return;. }else{
80110 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 . z = sqlite3
80120 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
80130 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 n);. if( z==0
80140 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
80150 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 3_result_error_n
80160 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 omem(context);.
80170 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
80180 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75 74 65 }. }. compute
80190 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70 75 74 JD(&x);. comput
801a0 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 eYMD_HMS(&x);.
801b0 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d 74 5b for(i=j=0; zFmt[
801c0 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 i]; i++){. if
801d0 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 20 29 ( zFmt[i]!='%' )
801e0 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d {. z[j++] =
801f0 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20 7d 65 zFmt[i];. }e
80200 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a lse{. i++;.
80210 20 20 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 switch( zF
80220 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 mt[i] ){.
80230 20 63 61 73 65 20 27 64 27 3a 20 20 73 71 6c 69 case 'd': sqli
80240 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 te3_snprintf(3,
80250 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 44 &z[j],"%02d",x.D
80260 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a ); j+=2; break;.
80270 20 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 case 'f'
80280 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f : {. do
80290 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a 20 20 uble s = x.s;.
802a0 20 20 20 20 20 20 20 20 69 66 28 20 73 3e 35 39 if( s>59
802b0 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e 39 39 .999 ) s = 59.99
802c0 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 9;. sql
802d0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 37 2c ite3_snprintf(7,
802e0 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66 22 2c &z[j],"%06.3f",
802f0 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a s);. j
80300 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d += strlen(&z[j]
80310 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 );. bre
80320 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
80330 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 20 case 'H':
80340 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
80350 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 f(3, &z[j],"%02d
80360 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 ",x.h); j+=2; br
80370 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
80380 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 e 'W': /* Fall t
80390 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 hru */. c
803a0 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20 ase 'j': {.
803b0 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20 int nDay;
803c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
803d0 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e mber of days sin
803e0 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79 65 ce 1st day of ye
803f0 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ar */.
80400 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a DateTime y = x;.
80410 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69 y.vali
80420 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dJD = 0;.
80430 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 y.M = 1;.
80440 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20 y.D = 1;.
80450 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 compute
80460 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20 JD(&y);.
80470 20 20 6e 44 61 79 20 3d 20 28 78 2e 69 4a 44 20 nDay = (x.iJD
80480 2d 20 79 2e 69 4a 44 29 2f 38 36 34 30 30 30 30 - y.iJD)/8640000
80490 30 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20 0.0 + 0.5;.
804a0 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d if( zFmt[i]
804b0 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 20 20 =='W' ){.
804c0 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 20 2f int wd; /
804d0 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54 75 * 0=Monday, 1=Tu
804e0 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75 6e esday, ... 6=Sun
804f0 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 day */.
80500 20 20 20 77 64 20 3d 20 28 28 78 2e 69 4a 44 2b wd = ((x.iJD+
80510 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 43200000)/864000
80520 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20 20 00) % 7;.
80530 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
80540 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 rintf(3, &z[j],"
80550 25 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 %02d",(nDay+7-wd
80560 29 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 )/7);.
80570 20 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 j += 2;.
80580 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
80590 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
805a0 6e 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d nprintf(4, &z[j]
805b0 2c 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b ,"%03d",nDay+1);
805c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b . j +
805d0 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d = 3;. }
805e0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
805f0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
80600 20 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a case 'J': {.
80610 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
80620 33 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 3_snprintf(20, &
80630 7a 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 z[j],"%.16g",x.i
80640 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a JD/86400000.0);.
80650 20 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 74 72 j+=str
80660 6c 65 6e 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 len(&z[j]);.
80670 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
80680 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 }. c
80690 61 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 ase 'm': sqlite
806a0 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 3_snprintf(3, &z
806b0 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b [j],"%02d",x.M);
806c0 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 j+=2; break;.
806d0 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 case 'M':
806e0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
806f0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 f(3, &z[j],"%02d
80700 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 ",x.m); j+=2; br
80710 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
80720 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 e 's': {.
80730 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
80740 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 64 ntf(30,&z[j],"%d
80750 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
80760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 (i
80770 6e 74 29 28 78 2e 69 4a 44 2f 31 30 30 30 2e 30 nt)(x.iJD/1000.0
80780 20 2d 20 32 31 30 38 36 36 37 36 30 30 30 30 2e - 210866760000.
80790 30 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 0));. j
807a0 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d += strlen(&z[j]
807b0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 );. bre
807c0 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
807d0 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 20 case 'S':
807e0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
807f0 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 f(3,&z[j],"%02d"
80800 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 ,(int)x.s); j+=2
80810 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
80820 20 63 61 73 65 20 27 77 27 3a 20 20 7a 5b 6a 2b case 'w': z[j+
80830 2b 5d 20 3d 20 28 28 28 78 2e 69 4a 44 2b 31 32 +] = (((x.iJD+12
80840 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 9600000)/8640000
80850 30 29 20 25 20 37 29 20 2b 20 27 30 27 3b 20 62 0) % 7) + '0'; b
80860 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
80870 73 65 20 27 59 27 3a 20 20 73 71 6c 69 74 65 33 se 'Y': sqlite3
80880 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a _snprintf(5,&z[j
80890 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a ],"%04d",x.Y); j
808a0 2b 3d 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b +=strlen(&z[j]);
808b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 break;. d
808c0 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d efault: z[j++]
808d0 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 = '%'; break;.
808e0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
808f0 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 . z[j] = 0;. s
80900 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
80910 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d xt(context, z, -
80920 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1,.
80930 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66 z==zBuf
80940 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 ? SQLITE_TRANSI
80950 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e ENT : SQLITE_DYN
80960 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 AMIC);.}../*.**
80970 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a current_time().*
80980 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
80990 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 on returns the s
809a0 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d ame value as tim
809b0 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 e('now')..*/.sta
809c0 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75 tic void ctimeFu
809d0 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
809e0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
809f0 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
80a00 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
80a10 67 76 0a 29 7b 0a 20 20 74 69 6d 65 46 75 6e 63 gv.){. timeFunc
80a20 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b (context, 0, 0);
80a30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e .}../*.** curren
80a40 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 t_date().**.** T
80a50 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
80a60 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 urns the same va
80a70 6c 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 lue as date('now
80a80 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ')..*/.static vo
80a90 69 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 id cdateFunc(.
80aa0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
80ab0 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
80ac0 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f argc,. sqlite3_
80ad0 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
80ae0 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 dateFunc(conte
80af0 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a xt, 0, 0);.}../*
80b00 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 .** current_time
80b10 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 stamp().**.** Th
80b20 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
80b30 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c rns the same val
80b40 75 65 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 ue as datetime('
80b50 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 now')..*/.static
80b60 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 void ctimestamp
80b70 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
80b80 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
80b90 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
80ba0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
80bb0 61 72 67 76 0a 29 7b 0a 20 20 64 61 74 65 74 69 argv.){. dateti
80bc0 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 meFunc(context,
80bd0 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 0, 0);.}.#endif
80be0 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 /* !defined(SQLI
80bf0 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 TE_OMIT_DATETIME
80c00 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 _FUNCS) */..#ifd
80c10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
80c20 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a ATETIME_FUNCS./*
80c30 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72 61 .** If the libra
80c40 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 ry is compiled t
80c50 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d o omit the full-
80c60 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20 74 scale date and t
80c70 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 ime.** handling
80c80 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 (to get a smalle
80c90 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20 66 r binary), the f
80ca0 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c ollowing minimal
80cb0 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 version.** of t
80cc0 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 he functions cur
80cd0 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 rent_time(), cur
80ce0 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 20 rent_date() and
80cf0 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d current_timestam
80d00 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 p().** are inclu
80d10 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68 69 ded instead. Thi
80d20 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 20 s is to support
80d30 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 column declarati
80d40 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c ons that.** incl
80d50 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55 52 ude "DEFAULT CUR
80d60 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a RENT_TIME" etc..
80d70 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
80d80 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d 6c ion uses the C-l
80d90 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 ibrary functions
80da0 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 time(), gmtime(
80db0 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 6d ).** and strftim
80dc0 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 e(). The format
80dd0 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20 74 string to pass t
80de0 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73 20 o strftime() is
80df0 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 supplied.** as t
80e00 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f 72 he user-data for
80e10 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a the function..*
80e20 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 75 /.static void cu
80e30 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 rrentTimeFunc(.
80e40 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
80e50 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
80e60 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
80e70 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
80e80 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 . time_t t;. c
80e90 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 har *zFormat = (
80ea0 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 char *)sqlite3_u
80eb0 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 ser_data(context
80ec0 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 );. sqlite3 *db
80ed0 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 ;. double rT;.
80ee0 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a char zBuf[20];.
80ef0 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f . db = sqlite3_
80f00 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
80f10 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 e(context);. sq
80f20 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
80f30 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 me(db->pVfs, &rT
80f40 29 3b 0a 20 20 74 20 3d 20 38 36 34 30 30 2e 30 );. t = 86400.0
80f50 2a 28 72 54 20 2d 20 32 34 34 30 35 38 37 2e 35 *(rT - 2440587.5
80f60 29 20 2b 20 30 2e 35 3b 0a 23 69 66 64 65 66 20 ) + 0.5;.#ifdef
80f70 48 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 HAVE_GMTIME_R.
80f80 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 {. struct tm
80f90 73 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 sNow;. gmtime
80fa0 5f 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 _r(&t, &sNow);.
80fb0 20 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 strftime(zBuf
80fc0 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 , 20, zFormat, &
80fd0 73 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 sNow);. }.#else
80fe0 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 . {. struct
80ff0 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c tm *pTm;. sql
81000 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
81010 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
81020 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
81030 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b STATIC_MASTER));
81040 0a 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d . pTm = gmtim
81050 65 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 e(&t);. strft
81060 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 ime(zBuf, 20, zF
81070 6f 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 ormat, pTm);.
81080 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
81090 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 eave(sqlite3Mute
810a0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
810b0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
810c0 52 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a R));. }.#endif.
810d0 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
810e0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
810f0 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 zBuf, -1, SQLITE
81100 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 _TRANSIENT);.}.#
81110 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 endif../*.** Thi
81120 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 s function regis
81130 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 tered all of the
81140 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f above C functio
81150 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e ns as SQL.** fun
81160 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 ctions. This sh
81170 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 ould be the only
81180 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 routine in this
81190 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 file with.** ex
811a0 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a ternal linkage..
811b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
811c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
811d0 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 gisterDateTimeFu
811e0 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 nctions(void){.
811f0 20 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 static SQLITE_W
81200 53 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 SD FuncDef aDate
81210 54 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a TimeFuncs[] = {.
81220 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
81230 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e MIT_DATETIME_FUN
81240 43 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 CS. FUNCTION(
81250 6a 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 julianday,
81260 20 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 -1, 0, 0, juli
81270 61 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 andayFunc ),.
81280 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 FUNCTION(date,
81290 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 -1,
812a0 30 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 0, 0, dateFunc
812b0 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
812c0 49 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 ION(time,
812d0 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 -1, 0, 0,
812e0 74 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c timeFunc ),
812f0 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 . FUNCTION(da
81300 74 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 tetime,
81310 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 -1, 0, 0, dateti
81320 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 meFunc ),. F
81330 55 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 UNCTION(strftime
81340 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c , -1, 0,
81350 20 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 0, strftimeFunc
81360 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
81370 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 N(current_time,
81380 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 0, 0, 0, ct
81390 69 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 imeFunc ),.
813a0 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 FUNCTION(curr
813b0 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 ent_timestamp, 0
813c0 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 , 0, 0, ctimesta
813d0 6d 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e mpFunc),. FUN
813e0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 CTION(current_da
813f0 74 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 te, 0, 0, 0
81400 2c 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 , cdateFunc
81410 29 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 ),.#else. STR
81420 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e _FUNCTION(curren
81430 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 t_time, 0,
81440 22 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 "%H:%M:%S",
81450 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 0, currentT
81460 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 imeFunc),. ST
81470 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 R_FUNCTION(curre
81480 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c nt_timestamp, 0,
81490 20 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 "%Y-%m-%d",
814a0 20 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 0, current
814b0 54 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 TimeFunc),. S
814c0 54 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 TR_FUNCTION(curr
814d0 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 ent_date, 0
814e0 2c 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 , "%Y-%m-%d %H:%
814f0 4d 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e M:%S", 0, curren
81500 74 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 tTimeFunc),.#end
81510 69 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b if. };. int i;
81520 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a . FuncDefHash *
81530 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 pHash = &GLOBAL(
81540 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c FuncDefHash, sql
81550 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 ite3GlobalFuncti
81560 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 ons);. FuncDef
81570 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 *aFunc = (FuncDe
81580 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 f*)&GLOBAL(FuncD
81590 65 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e ef, aDateTimeFun
815a0 63 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b cs);.. for(i=0;
815b0 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 i<ArraySize(aDa
815c0 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b teTimeFuncs); i+
815d0 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 +){. sqlite3F
815e0 75 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 uncDefInsert(pHa
815f0 73 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a sh, &aFunc[i]);.
81600 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a }.}../********
81610 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 ****** End of da
81620 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a te.c ***********
81630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81650 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
81660 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
81670 65 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a e os.c *********
81680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
816a0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
816b0 35 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 5 November 29.**
816c0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
816d0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
816e0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
816f0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
81700 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
81710 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
81720 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
81730 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
81740 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
81750 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
81760 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
81770 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
81780 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
81790 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
817a0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
817b0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
817c0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
817d0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
817e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
817f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81820 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
81830 65 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e e contains OS in
81840 74 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 terface code tha
81850 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 t is common to a
81860 6c 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 ll.** architectu
81870 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 res..**.** $Id:
81880 6f 73 2e 63 2c 76 20 31 2e 31 32 34 20 32 30 30 os.c,v 1.124 200
81890 38 2f 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 8/10/07 15:25:48
818a0 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 64 drh Exp $.*/.#d
818b0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 efine _SQLITE_OS
818c0 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 _C_ 1.#undef _SQ
818d0 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a LITE_OS_C_../*.*
818e0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 * The default SQ
818f0 4c 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 Lite sqlite3_vfs
81900 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
81910 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 do not allocate
81920 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 .** memory (actu
81930 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 ally, os_unix.c
81940 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c allocates a smal
81950 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f l amount of memo
81960 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 ry.** from withi
81970 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 n OsOpen()), but
81980 20 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 some third-part
81990 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e y implementation
819a0 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 s may..** So we
819b0 74 65 73 74 20 74 68 65 20 65 66 66 65 63 74 73 test the effects
819c0 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 of a malloc() f
819d0 61 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 ailing and the s
819e0 71 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a qlite3OsXXX().**
819f0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
81a00 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ing SQLITE_IOERR
81a10 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 _NOMEM using the
81a20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
81a30 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 ST macro..**.**
81a40 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 The following fu
81a50 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 nctions are inst
81a60 72 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c rumented for mal
81a70 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a loc() failure .*
81a80 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a * testing:.**.**
81a90 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 sqlite3OsOp
81aa0 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 en().** sqli
81ab0 74 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 te3OsRead().**
81ac0 20 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 sqlite3OsWrit
81ad0 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 e().** sqlit
81ae0 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 e3OsSync().**
81af0 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 sqlite3OsLock(
81b00 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ).**.*/.#if defi
81b10 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
81b20 20 26 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 && (SQLITE_OS_W
81b30 49 4e 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 IN==0). #define
81b40 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
81b50 53 54 20 69 66 20 28 31 29 20 7b 20 20 20 20 20 ST if (1) {
81b60 20 20 20 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 \. voi
81b70 64 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 d *pTstAlloc = s
81b80 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 qlite3Malloc(10)
81b90 3b 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 ; \. if
81ba0 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 (!pTstAlloc) re
81bb0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
81bc0 52 5f 4e 4f 4d 45 4d 3b 20 20 5c 0a 20 20 20 20 R_NOMEM; \.
81bd0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 73 sqlite3_free(pTs
81be0 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 20 tAlloc);
81bf0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
81c00 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e }.#else. #defin
81c10 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 e DO_OS_MALLOC_T
81c20 45 53 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a EST.#endif../*.*
81c30 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
81c40 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e routines are con
81c50 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 venience wrapper
81c60 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 s around methods
81c70 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 .** of the sqlit
81c80 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 e3_file object.
81c90 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 This is mostly
81ca0 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 just syntactic s
81cb0 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 ugar. All.** of
81cc0 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f this would be co
81cd0 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 mpletely automat
81ce0 69 63 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 ic if SQLite wer
81cf0 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a e coded using.**
81d00 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 C++ instead of
81d10 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a plain old C..*/.
81d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
81d30 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 nt sqlite3OsClos
81d40 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
81d50 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d pId){. int rc =
81d60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
81d70 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 ( pId->pMethods
81d80 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d ){. rc = pId-
81d90 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 >pMethods->xClos
81da0 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d e(pId);. pId-
81db0 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 >pMethods = 0;.
81dc0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
81dd0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
81de0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 int sqlite3OsRe
81df0 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ad(sqlite3_file
81e00 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c *id, void *pBuf,
81e10 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 int amt, i64 of
81e20 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d fset){. DO_OS_M
81e30 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 ALLOC_TEST;. re
81e40 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
81e50 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75 s->xRead(id, pBu
81e60 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b f, amt, offset);
81e70 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
81e80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 E int sqlite3OsW
81e90 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c rite(sqlite3_fil
81ea0 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 e *id, const voi
81eb0 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 d *pBuf, int amt
81ec0 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 , i64 offset){.
81ed0 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
81ee0 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d ST;. return id-
81ef0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 74 >pMethods->xWrit
81f00 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c e(id, pBuf, amt,
81f10 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 offset);.}.SQLI
81f20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
81f30 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
81f40 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
81f50 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 20 d, i64 size){.
81f60 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
81f70 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 69 ods->xTruncate(i
81f80 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 d, size);.}.SQLI
81f90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
81fa0 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 6c qlite3OsSync(sql
81fb0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
81fc0 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f 5f nt flags){. DO_
81fd0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a OS_MALLOC_TEST;.
81fe0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
81ff0 74 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c thods->xSync(id,
82000 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 flags);.}.SQLIT
82010 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
82020 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
82030 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
82040 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 , i64 *pSize){.
82050 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
82060 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d ST;. return id-
82070 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 >pMethods->xFile
82080 53 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b Size(id, pSize);
82090 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
820a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c E int sqlite3OsL
820b0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
820c0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 *id, int lockTy
820d0 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c pe){. DO_OS_MAL
820e0 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 LOC_TEST;. retu
820f0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
82100 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 >xLock(id, lockT
82110 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 ype);.}.SQLITE_P
82120 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
82130 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 e3OsUnlock(sqlit
82140 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
82150 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 lockType){. re
82160 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
82170 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c s->xUnlock(id, l
82180 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 ockType);.}.SQLI
82190 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
821a0 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 qlite3OsCheckRes
821b0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
821c0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
821d0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f *pResOut){. DO_
821e0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a OS_MALLOC_TEST;.
821f0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
82200 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 73 thods->xCheckRes
82210 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 52 ervedLock(id, pR
82220 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 esOut);.}.SQLITE
82230 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
82240 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f ite3OsFileContro
82250 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a l(sqlite3_file *
82260 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 id, int op, void
82270 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75 72 *pArg){. retur
82280 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
82290 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c xFileControl(id,
822a0 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 51 op, pArg);.}.SQ
822b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
822c0 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 sqlite3OsSector
822d0 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Size(sqlite3_fil
822e0 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 2a e *id){. int (*
822f0 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c xSectorSize)(sql
82300 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 64 ite3_file*) = id
82310 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 63 ->pMethods->xSec
82320 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 torSize;. retur
82330 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20 3f n (xSectorSize ?
82340 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64 29 xSectorSize(id)
82350 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c : SQLITE_DEFAUL
82360 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b 0a T_SECTOR_SIZE);.
82370 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
82380 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 int sqlite3OsDe
82390 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
823a0 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ics(sqlite3_file
823b0 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 *id){. return
823c0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 44 id->pMethods->xD
823d0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
823e0 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a tics(id);.}../*.
823f0 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 ** The next grou
82400 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 p of routines ar
82410 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 e convenience wr
82420 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68 appers around th
82430 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 73 e.** VFS methods
82440 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
82450 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
82460 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 sOpen(. sqlite3
82470 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 _vfs *pVfs, . c
82480 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
82490 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c , . sqlite3_fil
824a0 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74 e *pFile, . int
824b0 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a flags, . int *
824c0 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 44 pFlagsOut.){. D
824d0 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
824e0 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d ;. return pVfs-
824f0 3e 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 >xOpen(pVfs, zPa
82500 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 th, pFile, flags
82510 2c 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 7d 0a , pFlagsOut);.}.
82520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
82530 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 nt sqlite3OsDele
82540 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a te(sqlite3_vfs *
82550 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 pVfs, const char
82560 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72 *zPath, int dir
82570 53 79 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 Sync){. return
82580 70 56 66 73 2d 3e 78 44 65 6c 65 74 65 28 70 56 pVfs->xDelete(pV
82590 66 73 2c 20 7a 50 61 74 68 2c 20 64 69 72 53 79 fs, zPath, dirSy
825a0 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 nc);.}.SQLITE_PR
825b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
825c0 33 4f 73 41 63 63 65 73 73 28 0a 20 20 73 71 6c 3OsAccess(. sql
825d0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
825e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
825f0 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61 Path, . int fla
82600 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65 73 gs, . int *pRes
82610 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d Out.){. DO_OS_M
82620 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 ALLOC_TEST;. re
82630 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65 turn pVfs->xAcce
82640 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 ss(pVfs, zPath,
82650 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b flags, pResOut);
82660 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
82670 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 E int sqlite3OsF
82680 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 ullPathname(. s
82690 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
826a0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
826b0 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e *zPath, . int n
826c0 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 PathOut, . char
826d0 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 *zPathOut.){.
826e0 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 75 return pVfs->xFu
826f0 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c llPathname(pVfs,
82700 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 74 zPath, nPathOut
82710 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 23 , zPathOut);.}.#
82720 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
82730 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
82740 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 N.SQLITE_PRIVATE
82750 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 void *sqlite3Os
82760 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 DlOpen(sqlite3_v
82770 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 fs *pVfs, const
82780 63 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 20 char *zPath){.
82790 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c return pVfs->xDl
827a0 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 Open(pVfs, zPath
827b0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
827c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
827d0 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 OsDlError(sqlite
827e0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
827f0 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 nByte, char *zB
82800 75 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d 3e ufOut){. pVfs->
82810 78 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e xDlError(pVfs, n
82820 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a Byte, zBufOut);.
82830 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
82840 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 void *sqlite3Os
82850 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 DlSym(sqlite3_vf
82860 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
82870 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 Handle, const ch
82880 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 ar *zSymbol){.
82890 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c return pVfs->xDl
828a0 53 79 6d 28 70 56 66 73 2c 20 70 48 61 6e 64 6c Sym(pVfs, pHandl
828b0 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 7d 0a 53 e, zSymbol);.}.S
828c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
828d0 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c id sqlite3OsDlCl
828e0 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ose(sqlite3_vfs
828f0 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 *pVfs, void *pHa
82900 6e 64 6c 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 ndle){. pVfs->x
82910 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 DlClose(pVfs, pH
82920 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 andle);.}.#endif
82930 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
82940 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a LOAD_EXTENSION *
82950 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
82960 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 int sqlite3OsRa
82970 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 ndomness(sqlite3
82980 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
82990 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 nByte, char *zBu
829a0 66 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 fOut){. return
829b0 70 56 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 pVfs->xRandomnes
829c0 73 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a s(pVfs, nByte, z
829d0 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 BufOut);.}.SQLIT
829e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
829f0 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c lite3OsSleep(sql
82a00 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
82a10 69 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 int nMicro){. r
82a20 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 eturn pVfs->xSle
82a30 65 70 28 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 ep(pVfs, nMicro)
82a40 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
82a50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
82a60 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 CurrentTime(sqli
82a70 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 te3_vfs *pVfs, d
82a80 6f 75 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 ouble *pTimeOut)
82a90 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
82aa0 3e 78 43 75 72 72 65 6e 74 54 69 6d 65 28 70 56 >xCurrentTime(pV
82ab0 66 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d fs, pTimeOut);.}
82ac0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
82ad0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 int sqlite3OsOp
82ae0 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 enMalloc(. sqli
82af0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a te3_vfs *pVfs, .
82b00 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
82b10 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f ile, . sqlite3_
82b20 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a file **ppFile, .
82b30 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 int flags,. i
82b40 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b nt *pOutFlags.){
82b50 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
82b60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 TE_NOMEM;. sqli
82b70 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b te3_file *pFile;
82b80 0a 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 . pFile = (sqli
82b90 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 te3_file *)sqlit
82ba0 65 33 4d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 e3Malloc(pVfs->s
82bb0 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 zOsFile);. if(
82bc0 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 pFile ){. rc
82bd0 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 = sqlite3OsOpen(
82be0 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 pVfs, zFile, pFi
82bf0 6c 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 le, flags, pOutF
82c00 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 lags);. if( r
82c10 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
82c20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
82c30 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d ee(pFile);. }
82c40 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 else{. *ppF
82c50 69 6c 65 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 ile = pFile;.
82c60 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
82c70 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 rc;.}.SQLITE_PRI
82c80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
82c90 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 OsCloseFree(sqli
82ca0 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 te3_file *pFile)
82cb0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
82cc0 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 ITE_OK;. assert
82cd0 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 ( pFile );. rc
82ce0 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 = sqlite3OsClose
82cf0 28 70 46 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 (pFile);. sqlit
82d00 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a e3_free(pFile);.
82d10 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
82d20 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f /*.** The list o
82d30 66 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 f all registered
82d40 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 VFS implementat
82d50 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ions..*/.static
82d60 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 sqlite3_vfs * SQ
82d70 4c 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 LITE_WSD vfsList
82d80 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 = 0;.#define vf
82d90 73 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c sList GLOBAL(sql
82da0 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c ite3_vfs *, vfsL
82db0 69 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 ist)../*.** Loca
82dc0 74 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 te a VFS by name
82dd0 2e 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 . If no name is
82de0 20 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 given, simply r
82df0 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 eturn the.** fir
82e00 73 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 st VFS on the li
82e10 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 st..*/.SQLITE_AP
82e20 49 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 I sqlite3_vfs *s
82e30 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 qlite3_vfs_find(
82e40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 const char *zVfs
82e50 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
82e60 20 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 *pVfs = 0;.#if
82e70 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
82e80 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E. sqlite3_mute
82e90 78 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 x *mutex;.#endif
82ea0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
82eb0 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
82ec0 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
82ed0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 _initialize();.
82ee0 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
82ef0 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 0;.#endif.#if S
82f00 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
82f10 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 . mutex = sqlit
82f20 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
82f30 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
82f40 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 _MASTER);.#endif
82f50 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
82f60 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 _enter(mutex);.
82f70 20 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c for(pVfs = vfsL
82f80 69 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d ist; pVfs; pVfs=
82f90 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 pVfs->pNext){.
82fa0 20 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 if( zVfs==0 )
82fb0 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 break;. if( s
82fc0 74 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 trcmp(zVfs, pVfs
82fd0 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 ->zName)==0 ) br
82fe0 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 eak;. }. sqlit
82ff0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
83000 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
83010 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 pVfs;.}../*.** U
83020 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d nlink a VFS from
83030 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
83040 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
83050 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 vfsUnlink(sqlite
83060 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 3_vfs *pVfs){.
83070 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
83080 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 mutex_held(sqlit
83090 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
830a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
830b0 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 _MASTER)) );. i
830c0 66 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 f( pVfs==0 ){.
830d0 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 /* No-op */.
830e0 7d 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 }else if( vfsLis
830f0 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 t==pVfs ){. v
83100 66 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 fsList = pVfs->p
83110 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Next;. }else if
83120 28 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 ( vfsList ){.
83130 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 sqlite3_vfs *p
83140 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 = vfsList;. w
83150 68 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 hile( p->pNext &
83160 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 & p->pNext!=pVfs
83170 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d ){. p = p-
83180 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
83190 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d if( p->pNext==
831a0 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d pVfs ){. p-
831b0 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 >pNext = pVfs->p
831c0 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Next;. }. }.
831d0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 }../*.** Registe
831e0 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 r a VFS with the
831f0 20 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 system. It is
83200 68 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 harmless to regi
83210 73 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a ster the same.**
83220 20 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 VFS multiple ti
83230 6d 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 mes. The new VF
83240 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 S becomes the de
83250 66 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c fault if makeDfl
83260 74 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f t is.** true..*/
83270 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
83280 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
83290 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 ster(sqlite3_vfs
832a0 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 *pVfs, int make
832b0 44 66 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 Dflt){. sqlite3
832c0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 _mutex *mutex =
832d0 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 0;.#ifndef SQLIT
832e0 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
832f0 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
83300 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b e3_initialize();
83310 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
83320 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 rn rc;.#endif.
83330 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
83340 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
83350 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
83360 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 STER);. sqlite3
83370 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 _mutex_enter(mut
83380 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b ex);. vfsUnlink
83390 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 (pVfs);. if( ma
833a0 6b 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 keDflt || vfsLis
833b0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 t==0 ){. pVfs
833c0 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 ->pNext = vfsLis
833d0 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d t;. vfsList =
833e0 20 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a pVfs;. }else{.
833f0 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 pVfs->pNext
83400 3d 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 = vfsList->pNext
83410 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 ;. vfsList->p
83420 4e 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d Next = pVfs;. }
83430 0a 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 . assert(vfsLis
83440 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 t);. sqlite3_mu
83450 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
83460 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
83470 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 E_OK;.}../*.** U
83480 6e 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 nregister a VFS
83490 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f so that it is no
834a0 20 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 longer accessib
834b0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 le..*/.SQLITE_AP
834c0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 I int sqlite3_vf
834d0 73 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c s_unregister(sql
834e0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b ite3_vfs *pVfs){
834f0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
83500 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 ADSAFE. sqlite3
83510 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 _mutex *mutex =
83520 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
83530 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
83540 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 TATIC_MASTER);.#
83550 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f endif. sqlite3_
83560 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
83570 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 x);. vfsUnlink(
83580 70 56 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 pVfs);. sqlite3
83590 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
835a0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ex);. return SQ
835b0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a LITE_OK;.}../***
835c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
835d0 6f 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a of os.c ********
835e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
835f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
83610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
83620 6e 20 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a n file fault.c *
83630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
83660 2a 20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a * 2008 Jan 22.**
83670 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
83680 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
83690 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
836a0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
836b0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
836c0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
836d0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
836e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
836f0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
83700 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
83710 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
83720 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
83730 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
83740 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
83750 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
83760 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
83770 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
83780 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
83790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
837a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
837b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
837c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
837d0 2a 2a 20 24 49 64 3a 20 66 61 75 6c 74 2e 63 2c ** $Id: fault.c,
837e0 76 20 31 2e 31 31 20 32 30 30 38 2f 30 39 2f 30 v 1.11 2008/09/0
837f0 32 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 2 00:52:52 drh E
83800 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 xp $.*/../*.** T
83810 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
83820 73 20 63 6f 64 65 20 74 6f 20 73 75 70 70 6f 72 s code to suppor
83830 74 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 t the concept of
83840 20 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d 61 "benign" .** ma
83850 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 28 77 lloc failures (w
83860 68 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 hen the xMalloc(
83870 29 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 20 ) or xRealloc()
83880 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a method of the.**
83890 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
838a0 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 20 66 hods structure f
838b0 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 ails to allocate
838c0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f a block of memo
838d0 72 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e ry.** and return
838e0 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 s 0). .**.** Mos
838f0 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 t malloc failure
83900 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e s are non-benign
83910 2e 20 41 66 74 65 72 20 74 68 65 79 20 6f 63 63 . After they occ
83920 75 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 61 62 ur, SQLite.** ab
83930 61 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 72 65 andons the curre
83940 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 nt operation and
83950 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f returns an erro
83960 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 0a r code (usually.
83970 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 ** SQLITE_NOMEM)
83980 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48 6f to the user. Ho
83990 77 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73 wever, sometimes
839a0 20 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 a fault is not
839b0 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 66 necessarily.** f
839c0 61 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c atal. For exampl
839d0 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 e, if a malloc f
839e0 61 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69 7a ails while resiz
839f0 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 ing a hash table
83a00 2c 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 63 6f , this .** is co
83a10 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65 72 mpletely recover
83a20 61 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20 6e able simply by n
83a30 6f 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74 20 ot carrying out
83a40 74 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65 20 the resize. The
83a50 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 77 .** hash table w
83a60 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 ill continue to
83a70 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c function normall
83a80 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 20 y. So a malloc
83a90 66 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 72 69 failure .** duri
83aa0 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 ng a hash table
83ab0 72 65 73 69 7a 65 20 69 73 20 61 20 62 65 6e 69 resize is a beni
83ac0 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 gn fault..*/...#
83ad0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
83ae0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a IT_BUILTIN_TEST.
83af0 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61 ./*.** Global va
83b00 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 riables..*/.type
83b10 64 65 66 20 73 74 72 75 63 74 20 42 65 6e 69 67 def struct Benig
83b20 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e nMallocHooks Ben
83b30 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a ignMallocHooks;.
83b40 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
83b50 44 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d D struct BenignM
83b60 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 76 allocHooks {. v
83b70 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 oid (*xBenignBeg
83b80 69 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 in)(void);. voi
83b90 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 d (*xBenignEnd)(
83ba0 76 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 65 33 void);.} sqlite3
83bb0 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d Hooks = { 0, 0 }
83bc0 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 48 6f ;../* The "wsdHo
83bd0 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 oks" macro will
83be0 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 resolve to the a
83bf0 70 70 72 6f 70 72 69 61 74 65 20 42 65 6e 69 67 ppropriate Benig
83c00 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 nMallocHooks.**
83c10 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 77 structure. If w
83c20 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 ritable static d
83c30 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 ata is unsupport
83c40 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 ed on the target
83c50 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 ,.** we have to
83c60 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 locate the state
83c70 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 vector at run-t
83c80 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 ime. In the mor
83c90 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 e common.** case
83ca0 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 where writable
83cb0 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 static data is s
83cc0 75 70 70 6f 72 74 65 64 2c 20 77 73 64 48 6f 6f upported, wsdHoo
83cd0 6b 73 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 ks can refer dir
83ce0 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 ectly.** to the
83cf0 22 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 20 73 "sqlite3Hooks" s
83d00 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c tate vector decl
83d10 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 ared above..*/.#
83d20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
83d30 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 T_WSD.# define w
83d40 73 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 20 sdHooksInit \.
83d50 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b BenignMallocHook
83d60 73 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 42 s *x = &GLOBAL(B
83d70 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
83d80 2c 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 0a 23 ,sqlite3Hooks).#
83d90 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 define wsdHooks
83da0 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 x[0].#else.# de
83db0 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 fine wsdHooksIni
83dc0 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f t.# define wsdHo
83dd0 6f 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 oks sqlite3Hooks
83de0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
83df0 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 Register hooks t
83e00 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 6c 69 o call when sqli
83e10 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
83e20 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73 71 lloc() and.** sq
83e30 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
83e40 6c 6c 6f 63 28 29 20 61 72 65 20 63 61 6c 6c 65 lloc() are calle
83e50 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e d, respectively.
83e60 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
83e70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
83e80 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
83e90 28 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 (. void (*xBeni
83ea0 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 2c 0a gnBegin)(void),.
83eb0 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e void (*xBenign
83ec0 45 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a 20 20 End)(void).){.
83ed0 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 wsdHooksInit;.
83ee0 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e wsdHooks.xBenign
83ef0 42 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 6e 42 Begin = xBenignB
83f00 65 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 egin;. wsdHooks
83f10 2e 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 78 42 .xBenignEnd = xB
83f20 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a enignEnd;.}../*.
83f30 2a 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65 33 ** This (sqlite3
83f40 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
83f50 29 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 )) is called by
83f60 53 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20 69 SQLite code to i
83f70 6e 64 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 ndicate that.**
83f80 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f subsequent mallo
83f90 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 62 c failures are b
83fa0 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74 6f enign. A call to
83fb0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
83fc0 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e 64 nMalloc().** ind
83fd0 69 63 61 74 65 73 20 74 68 61 74 20 73 75 62 73 icates that subs
83fe0 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 equent malloc fa
83ff0 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 ilures are non-b
84000 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 enign..*/.SQLITE
84010 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
84020 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
84030 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 Malloc(void){.
84040 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 wsdHooksInit;.
84050 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 if( wsdHooks.xBe
84060 6e 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 nignBegin ){.
84070 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 wsdHooks.xBenig
84080 6e 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d 0a nBegin();. }.}.
84090 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
840a0 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 oid sqlite3EndBe
840b0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 nignMalloc(void)
840c0 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 {. wsdHooksInit
840d0 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 ;. if( wsdHooks
840e0 2e 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b 0a 20 .xBenignEnd ){.
840f0 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e wsdHooks.xBen
84100 69 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d 0a ignEnd();. }.}.
84110 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 .#endif /* #if
84120 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
84130 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f _BUILTIN_TEST */
84140 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
84150 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 2e 63 * End of fault.c
84160 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
84170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84190 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
841a0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d * Begin file mem
841b0 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0.c ************
841c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
841d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
841e0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 /./*.** 2008 Oct
841f0 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 ober 28.**.** Th
84200 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
84210 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
84220 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
84230 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
84240 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
84250 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
84260 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
84270 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
84280 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
84290 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
842a0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
842b0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
842c0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
842d0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
842e0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
842f0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
84300 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
84310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84350 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
84360 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
84370 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 61 a no-op memory a
84380 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 llocation driver
84390 73 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0a 2a s for use when.*
843a0 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 * SQLITE_ZERO_MA
843b0 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64 2e LLOC is defined.
843c0 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e The allocation
843d0 20 64 72 69 76 65 72 73 20 69 6d 70 6c 65 6d 65 drivers impleme
843e0 6e 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 6c 77 nted.** here alw
843f0 61 79 73 20 66 61 69 6c 2e 20 20 53 51 4c 69 74 ays fail. SQLit
84400 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 61 e will not opera
84410 74 65 20 77 69 74 68 20 74 68 65 73 65 20 64 72 te with these dr
84420 69 76 65 72 73 2e 20 20 54 68 65 73 65 0a 2a 2a ivers. These.**
84430 20 61 72 65 20 6d 65 72 65 6c 79 20 70 6c 61 63 are merely plac
84440 65 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 6c 20 eholders. Real
84450 64 72 69 76 65 72 73 20 6d 75 73 74 20 62 65 20 drivers must be
84460 73 75 62 73 74 69 74 75 74 65 64 20 75 73 69 6e substituted usin
84470 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e g.** sqlite3_con
84480 66 69 67 28 29 20 62 65 66 6f 72 65 20 53 51 4c fig() before SQL
84490 69 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 74 65 ite will operate
844a0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d ..**.** $Id: mem
844b0 30 2e 63 2c 76 20 31 2e 31 20 32 30 30 38 2f 31 0.c,v 1.1 2008/1
844c0 30 2f 32 38 20 31 38 3a 35 38 3a 32 30 20 64 72 0/28 18:58:20 dr
844d0 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a h Exp $.*/../*.*
844e0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
844f0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
84500 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 ocator is the de
84510 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a fault. It is.**
84520 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 used when no ot
84530 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 her memory alloc
84540 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 ator is specifie
84550 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d d using compile-
84560 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a time.** macros..
84570 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
84580 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a _ZERO_MALLOC../*
84590 0a 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f .** No-op versio
845a0 6e 73 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 ns of all memory
845b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 allocation rout
845c0 69 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ines.*/.static v
845d0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d oid *sqlite3MemM
845e0 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 alloc(int nByte)
845f0 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 { return 0; }.st
84600 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
84610 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 3MemFree(void *p
84620 50 72 69 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 Prior){ return;
84630 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 }.static void *s
84640 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
84650 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 (void *pPrior, i
84660 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 nt nByte){ retur
84670 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e n 0; }.static in
84680 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 t sqlite3MemSize
84690 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 (void *pPrior){
846a0 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 return 0; }.stat
846b0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
846c0 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b mRoundup(int n){
846d0 20 72 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 return n; }.sta
846e0 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d tic int sqlite3M
846f0 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 emInit(void *Not
84700 55 73 65 64 29 7b 20 72 65 74 75 72 6e 20 53 51 Used){ return SQ
84710 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 LITE_OK; }.stati
84720 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 c void sqlite3Me
84730 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a mShutdown(void *
84740 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e NotUsed){ return
84750 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ; }../*.** This
84760 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f routine is the o
84770 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 nly routine in t
84780 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 his file with ex
84790 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a ternal linkage..
847a0 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 **.** Populate t
847b0 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d he low-level mem
847c0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
847d0 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 unction pointers
847e0 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c in.** sqlite3Gl
847f0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 obalConfig.m wit
84800 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 h pointers to th
84810 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 e routines in th
84820 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 is file..*/.SQLI
84830 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
84840 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 sqlite3MemSetDef
84850 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 ault(void){. st
84860 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
84870 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 e3_mem_methods d
84880 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 efaultMethods =
84890 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 {. sqlite3Me
848a0 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 mMalloc,. sq
848b0 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 lite3MemFree,.
848c0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 sqlite3MemRea
848d0 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 lloc,. sqlit
848e0 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 e3MemSize,.
848f0 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 sqlite3MemRoundu
84900 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d p,. sqlite3M
84910 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c emInit,. sql
84920 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c ite3MemShutdown,
84930 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 . 0. };. s
84940 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 qlite3_config(SQ
84950 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c LITE_CONFIG_MALL
84960 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 OC, &defaultMeth
84970 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 ods);.}..#endif
84980 2f 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d /* SQLITE_ZERO_M
84990 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a ALLOC */../*****
849a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
849b0 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mem0.c ********
849c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
849d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
849e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
849f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
84a00 66 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a file mem1.c ****
84a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84a30 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
84a40 32 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2007 August 14.*
84a50 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
84a60 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
84a70 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
84a80 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
84a90 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
84aa0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
84ab0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
84ac0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
84ad0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
84ae0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
84af0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
84b00 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
84b10 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
84b20 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
84b30 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
84b40 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
84b50 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
84b60 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
84b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
84bb0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
84bc0 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c ntains low-level
84bd0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
84be0 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 on drivers for w
84bf0 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 hen.** SQLite wi
84c00 6c 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 ll use the stand
84c10 61 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 ard C-library ma
84c20 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 lloc/realloc/fre
84c30 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 e interface.** t
84c40 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d o obtain the mem
84c50 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a ory it needs..**
84c60 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
84c70 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 ntains implement
84c80 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f ations of the lo
84c90 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
84ca0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 llocation.** rou
84cb0 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 tines specified
84cc0 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d in the sqlite3_m
84cd0 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 em_methods objec
84ce0 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 t..**.** $Id: me
84cf0 6d 31 2e 63 2c 76 20 31 2e 32 37 20 32 30 30 38 m1.c,v 1.27 2008
84d00 2f 31 30 2f 32 38 20 31 38 3a 35 38 3a 32 30 20 /10/28 18:58:20
84d10 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
84d20 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
84d30 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
84d40 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 llocator is the
84d50 64 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a default. It is.
84d60 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 ** used when no
84d70 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c other memory all
84d80 6f 63 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 ocator is specif
84d90 69 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c ied using compil
84da0 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 e-time.** macros
84db0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
84dc0 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 TE_SYSTEM_MALLOC
84dd0 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c ../*.** Like mal
84de0 6c 6f 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d loc(), but remem
84df0 62 65 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ber the size of
84e00 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a the allocation.*
84e10 2a 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e * so that we can
84e20 20 66 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75 find it later u
84e30 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 sing sqlite3MemS
84e40 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ize()..**.** For
84e50 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 this low-level
84e60 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20 routine, we are
84e70 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 guaranteed that
84e80 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a nByte>0 because.
84e90 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74 ** cases of nByt
84ea0 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 e<=0 will be int
84eb0 65 72 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 ercepted and dea
84ec0 6c 74 20 77 69 74 68 20 62 79 20 68 69 67 68 65 lt with by highe
84ed0 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 r level.** routi
84ee0 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 nes..*/.static v
84ef0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d oid *sqlite3MemM
84f00 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 alloc(int nByte)
84f10 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 {. sqlite3_int6
84f20 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4 *p;. assert(
84f30 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 nByte>0 );. nBy
84f40 74 65 20 3d 20 28 6e 42 79 74 65 2b 37 29 26 7e te = (nByte+7)&~
84f50 37 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 7;. p = malloc(
84f60 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 nByte+8 );. if
84f70 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 ( p ){. p[0]
84f80 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b = nByte;. p++
84f90 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
84fa0 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a void *)p;.}../*.
84fb0 2a 2a 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62 ** Like free() b
84fc0 75 74 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c ut works for all
84fd0 6f 63 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 ocations obtaine
84fe0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 d from sqlite3Me
84ff0 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 mMalloc().** or
85000 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f sqlite3MemReallo
85010 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 c()..**.** For t
85020 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f his low-level ro
85030 75 74 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 utine, we alread
85040 79 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 y know that pPri
85050 6f 72 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 or!=0 since.** c
85060 61 73 65 73 20 77 68 65 72 65 20 70 50 72 69 6f ases where pPrio
85070 72 3d 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 r==0 will have b
85080 65 65 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61 een intecepted a
85090 6e 64 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a nd dealt with.**
850a0 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c by higher-level
850b0 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 routines..*/.st
850c0 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
850d0 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 3MemFree(void *p
850e0 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 Prior){. sqlite
850f0 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 3_int64 *p = (sq
85100 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 lite3_int64*)pPr
85110 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ior;. assert( p
85120 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d Prior!=0 );. p-
85130 2d 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a -;. free(p);.}.
85140 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c ./*.** Like real
85150 6c 6f 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 loc(). Resize a
85160 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 n allocation pre
85170 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 viously obtained
85180 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 from.** sqlite3
85190 4d 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a MemMalloc()..**.
851a0 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d ** For this low-
851b0 6c 65 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c level interface,
851c0 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 we know that pP
851d0 72 69 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 rior!=0. Cases
851e0 77 68 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d where.** pPrior=
851f0 3d 30 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 =0 while have be
85200 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 en intercepted b
85210 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 y higher-level r
85220 6f 75 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 outine and.** re
85230 64 69 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c directed to xMal
85240 6c 6f 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c loc. Similarly,
85250 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 we know that nB
85260 79 74 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a yte>0 becauses.*
85270 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 * cases where nB
85280 79 74 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 yte<=0 will have
85290 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 been intercepte
852a0 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 d by higher-leve
852b0 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e l.** routines an
852c0 64 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 d redirected to
852d0 78 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 xFree..*/.static
852e0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 void *sqlite3Me
852f0 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 mRealloc(void *p
85300 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 Prior, int nByte
85310 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 ){. sqlite3_int
85320 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 64 *p = (sqlite3
85330 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a _int64*)pPrior;.
85340 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 assert( pPrior
85350 21 3d 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29 !=0 && nByte>0 )
85360 3b 0a 20 20 6e 42 79 74 65 20 3d 20 28 6e 42 79 ;. nByte = (nBy
85370 74 65 2b 37 29 26 7e 37 3b 0a 20 20 70 20 3d 20 te+7)&~7;. p =
85380 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
85390 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 pPrior;. p--;.
853a0 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 p = realloc(p,
853b0 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 nByte+8 );. if(
853c0 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d p ){. p[0] =
853d0 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b nByte;. p++;
853e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 . }. return (v
853f0 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a oid*)p;.}../*.**
85400 20 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f Report the allo
85410 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20 cated size of a
85420 70 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f prior return fro
85430 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f m xMalloc().** o
85440 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f r xRealloc()..*/
85450 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
85460 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 te3MemSize(void
85470 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 *pPrior){. sqli
85480 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 te3_int64 *p;.
85490 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 if( pPrior==0 )
854a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 return 0;. p =
854b0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
854c0 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 pPrior;. p--;.
854d0 20 72 65 74 75 72 6e 20 70 5b 30 5d 3b 0a 7d 0a return p[0];.}.
854e0 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 ./*.** Round up
854f0 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 a request size t
85500 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 o the next valid
85510 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
85520 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
85530 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 sqlite3MemRoundu
85540 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 p(int n){. retu
85550 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 3b 0a 7d rn (n+7) & ~7;.}
85560 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
85570 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a ze this module..
85580 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
85590 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 lite3MemInit(voi
855a0 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 72 d *NotUsed){. r
855b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
855c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
855d0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 ialize this modu
855e0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
855f0 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 id sqlite3MemShu
85600 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
85610 73 65 64 29 7b 0a 20 20 72 65 74 75 72 6e 3b 0a sed){. return;.
85620 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
85630 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c utine is the onl
85640 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 y routine in thi
85650 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 s file with exte
85660 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a rnal linkage..**
85670 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 .** Populate the
85680 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 low-level memor
85690 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e y allocation fun
856a0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 ction pointers i
856b0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 n.** sqlite3Glob
856c0 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 alConfig.m with
856d0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 pointers to the
856e0 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 routines in this
856f0 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 file..*/.SQLITE
85700 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
85710 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 lite3MemSetDefau
85720 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 lt(void){. stat
85730 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
85740 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 _mem_methods def
85750 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a aultMethods = {.
85760 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d sqlite3MemM
85770 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 alloc,. sqli
85780 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 te3MemFree,.
85790 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c sqlite3MemReall
857a0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
857b0 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 MemSize,. sq
857c0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c lite3MemRoundup,
857d0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
857e0 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 Init,. sqlit
857f0 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 e3MemShutdown,.
85800 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 0. };. sql
85810 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 ite3_config(SQLI
85820 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 TE_CONFIG_MALLOC
85830 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 , &defaultMethod
85840 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a s);.}..#endif /*
85850 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d SQLITE_SYSTEM_M
85860 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a ALLOC */../*****
85870 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
85880 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mem1.c ********
85890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
858a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
858b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
858c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
858d0 66 69 6c 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a file mem2.c ****
858e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
858f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85900 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
85910 32 30 30 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2007 August 15.*
85920 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
85930 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
85940 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
85950 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
85960 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
85970 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
85980 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
85990 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
859a0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
859b0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
859c0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
859d0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
859e0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
859f0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
85a00 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
85a10 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
85a20 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
85a30 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
85a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
85a80 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
85a90 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c ntains low-level
85aa0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
85ab0 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 on drivers for w
85ac0 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 hen.** SQLite wi
85ad0 6c 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 ll use the stand
85ae0 61 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 ard C-library ma
85af0 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 lloc/realloc/fre
85b00 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 e interface.** t
85b10 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d o obtain the mem
85b20 6f 72 79 20 69 74 20 6e 65 65 64 73 20 77 68 69 ory it needs whi
85b30 6c 65 20 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f le adding lots o
85b40 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 f additional deb
85b50 75 67 67 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d ugging.** inform
85b60 61 74 69 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c ation to each al
85b70 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 location in orde
85b80 72 20 74 6f 20 68 65 6c 70 20 64 65 74 65 63 74 r to help detect
85b90 20 61 6e 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a and fix memory.
85ba0 2a 2a 20 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d ** leaks and mem
85bb0 6f 72 79 20 75 73 61 67 65 20 65 72 72 6f 72 73 ory usage errors
85bc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ..**.** This fil
85bd0 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 e contains imple
85be0 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 mentations of th
85bf0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f e low-level memo
85c00 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a ry allocation.**
85c10 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 routines specif
85c20 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 ied in the sqlit
85c30 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f e3_mem_methods o
85c40 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 bject..**.** $Id
85c50 3a 20 6d 65 6d 32 2e 63 2c 76 20 31 2e 34 30 20 : mem2.c,v 1.40
85c60 32 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 2008/10/28 18:58
85c70 3a 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :20 drh Exp $.*/
85c80 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
85c90 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
85ca0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ry allocator is
85cb0 75 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 used only if the
85cc0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 .** SQLITE_MEMDE
85cd0 42 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66 BUG macro is def
85ce0 69 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ined.*/.#ifdef S
85cf0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a QLITE_MEMDEBUG..
85d00 2f 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 /*.** The backtr
85d10 61 63 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 ace functionalit
85d20 79 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 y is only availa
85d30 62 6c 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a ble with GLIBC.*
85d40 2f 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 /.#ifdef __GLIBC
85d50 5f 5f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 __. extern int
85d60 62 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a backtrace(void**
85d70 2c 69 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 ,int);. extern
85d80 76 6f 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73 void backtrace_s
85d90 79 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 ymbols_fd(void*c
85da0 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a onst*,int,int);.
85db0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62 #else.# define b
85dc0 61 63 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a acktrace(A,B) 1.
85dd0 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 # define backtra
85de0 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c ce_symbols_fd(A,
85df0 42 2c 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a B,C).#endif../*.
85e00 2a 2a 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 ** Each memory a
85e10 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 llocation looks
85e20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a like this:.**.**
85e30 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
85e40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85e50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85e60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85e70 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c ----------.** |
85e80 20 54 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 Title | backtr
85e90 61 63 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 ace pointers |
85ea0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 MemBlockHdr | a
85eb0 6c 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 llocation | End
85ec0 47 75 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d Guard |.** ----
85ed0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85ee0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85ef0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85f00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85f10 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 ----.**.** The a
85f20 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 pplication code
85f30 73 65 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e sees only a poin
85f40 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 ter to the alloc
85f50 61 74 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a ation. We have.
85f60 2a 2a 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72 ** to back up fr
85f70 6f 6d 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f om the allocatio
85f80 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e n pointer to fin
85f90 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 d the MemBlockHd
85fa0 72 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c r. The.** MemBl
85fb0 6f 63 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20 ockHdr tells us
85fc0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
85fd0 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 allocation and t
85fe0 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
85ff0 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 backtrace pointe
86000 72 73 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c rs. There is al
86010 73 6f 20 61 20 67 75 61 72 64 20 77 6f 72 64 20 so a guard word
86020 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
86030 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 e.** MemBlockHdr
86040 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 ..*/.struct MemB
86050 6c 6f 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 lockHdr {. i64
86060 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 iSize;
86070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
86080 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 /* Size of this
86090 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 allocation */.
860a0 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
860b0 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 dr *pNext, *pPre
860c0 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 v; /* Linked li
860d0 73 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 st of all unfree
860e0 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 d memory */. ch
860f0 61 72 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 ar nBacktrace;
86100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
86110 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
86120 61 63 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69 acktraces on thi
86130 73 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 s alloc */. cha
86140 72 20 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 r nBacktraceSlot
86150 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
86160 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61 /* Available ba
86170 63 6b 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f cktrace slots */
86180 0a 20 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b . short nTitle;
86190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
861a0 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
861b0 6f 66 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 of title; includ
861c0 65 73 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 es '\0' */. int
861d0 20 69 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20 iForeGuard;
861e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
861f0 20 2f 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66 /* Guard word f
86200 6f 72 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a or sanity */.};.
86210 0a 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 ./*.** Guard wor
86220 64 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f ds.*/.#define FO
86230 52 45 47 55 41 52 44 20 30 78 38 30 46 35 45 31 REGUARD 0x80F5E1
86240 35 33 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 53.#define REARG
86250 55 41 52 44 20 30 78 45 34 36 37 36 42 35 33 0a UARD 0xE4676B53.
86260 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 ./*.** Number of
86270 20 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 malloc size inc
86280 72 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b rements to track
86290 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 ..*/.#define NCS
862a0 49 5a 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a IZE 1000../*.**
862b0 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 All of the stat
862c0 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 ic variables use
862d0 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 d by this module
862e0 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a are collected.*
862f0 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 * into a single
86300 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 structure named
86310 22 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 "mem". This is
86320 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 to keep the.** s
86330 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 tatic variables
86340 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f organized and to
86350 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 reduce namespac
86360 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 e pollution.** w
86370 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 hen this module
86380 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 is combined with
86390 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d other in the am
863a0 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 algamation..*/.s
863b0 74 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 tatic struct {.
863c0 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 . /*. ** Mute
863d0 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 x to control acc
863e0 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 ess to the memor
863f0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
86400 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 system.. */. s
86410 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
86420 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 tex;.. /*. **
86430 48 65 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 Head and tail of
86440 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f a linked list o
86450 66 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e f all outstandin
86460 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 g allocations.
86470 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 */. struct MemB
86480 6c 6f 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b lockHdr *pFirst;
86490 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
864a0 63 6b 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 ckHdr *pLast;.
864b0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e . /*. ** The n
864c0 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 umber of levels
864d0 6f 66 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20 of backtrace to
864e0 73 61 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f save in new allo
864f0 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 cations.. */.
86500 69 6e 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a int nBacktrace;.
86510 20 20 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 void (*xBacktr
86520 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 ace)(int, int, v
86530 6f 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 oid **);.. /*.
86540 20 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 ** Title text t
86550 6f 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e o insert in fron
86560 74 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a t of each block.
86570 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c */. int nTitl
86580 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 e; /* Byt
86590 65 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 es of zTitle to
865a0 73 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 save. Includes
865b0 27 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 '\0' and padding
865c0 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c */. char zTitl
865d0 65 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 e[100]; /* The
865e0 74 69 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 title text */..
865f0 20 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 /* . ** sqlite
86600 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 3MallocDisallow(
86610 29 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 ) increments the
86620 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 following count
86630 65 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 er.. ** sqlite3
86640 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 MallocAllow() de
86650 63 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a crements it.. *
86660 2f 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 /. int disallow
86670 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f ; /* Do not allo
86680 77 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 w memory allocat
86690 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a ion */.. /*. *
866a0 2a 20 47 61 74 68 65 72 20 73 74 61 74 69 73 74 * Gather statist
866b0 69 63 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 ics on the sizes
866c0 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 of memory alloc
866d0 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c ations.. ** nAl
866e0 6c 6f 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 loc[i] is the nu
866f0 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 mber of allocati
86700 6f 6e 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69 on attempts of i
86710 2a 38 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 *8. ** bytes.
86720 69 3d 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65 i==NCSIZE is the
86730 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 number of alloc
86740 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 ation attempts f
86750 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f or. ** sizes mo
86760 72 65 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 re than NCSIZE*8
86770 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 bytes.. */. i
86780 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 nt nAlloc[NCSIZE
86790 5d 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c ]; /* Total
867a0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 number of alloc
867b0 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 ations */. int
867c0 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d nCurrent[NCSIZE]
867d0 3b 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 ; /* Current
867e0 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 number of alloca
867f0 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d tions */. int m
86800 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d xCurrent[NCSIZE]
86810 3b 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72 ; /* Highwater
86820 20 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 mark for nCurre
86830 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a nt */..} mem;...
86840 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d /*.** Adjust mem
86850 6f 72 79 20 75 73 61 67 65 20 73 74 61 74 69 73 ory usage statis
86860 74 69 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tics.*/.static v
86870 6f 69 64 20 61 64 6a 75 73 74 53 74 61 74 73 28 oid adjustStats(
86880 69 6e 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 int iSize, int i
86890 6e 63 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 ncrement){. int
868a0 20 69 20 3d 20 28 28 69 53 69 7a 65 2b 37 29 26 i = ((iSize+7)&
868b0 7e 37 29 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e ~7)/8;. if( i>N
868c0 43 53 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 CSIZE-1 ){. i
868d0 20 3d 20 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 = NCSIZE - 1;.
868e0 20 7d 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65 }. if( increme
868f0 6e 74 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e nt>0 ){. mem.
86900 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 nAlloc[i]++;.
86910 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d mem.nCurrent[i]
86920 2b 2b 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e ++;. if( mem.
86930 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e nCurrent[i]>mem.
86940 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a mxCurrent[i] ){.
86950 20 20 20 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 mem.mxCurr
86960 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 ent[i] = mem.nCu
86970 72 72 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a rrent[i];. }.
86980 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d }else{. mem
86990 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a .nCurrent[i]--;.
869a0 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e assert( mem.
869b0 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 nCurrent[i]>=0 )
869c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 ;. }.}../*.** G
869d0 69 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 iven an allocati
869e0 6f 6e 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d on, find the Mem
869f0 42 6c 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61 BlockHdr for tha
86a00 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a t allocation..**
86a10 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
86a20 20 63 68 65 63 6b 73 20 74 68 65 20 67 75 61 72 checks the guar
86a30 64 73 20 61 74 20 65 69 74 68 65 72 20 65 6e 64 ds at either end
86a40 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 of the allocati
86a50 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 on and.** if the
86a60 79 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 y are incorrect
86a70 69 74 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 it asserts..*/.s
86a80 74 61 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d tatic struct Mem
86a90 42 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 BlockHdr *sqlite
86aa0 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 3MemsysGetHeader
86ab0 28 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 (void *pAllocati
86ac0 6f 6e 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 on){. struct Me
86ad0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 mBlockHdr *p;.
86ae0 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 int *pInt;. u8
86af0 2a 70 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 *pU8;. int nRes
86b00 65 72 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 erve;.. p = (st
86b10 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 ruct MemBlockHdr
86b20 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 *)pAllocation;.
86b30 20 70 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 p--;. assert(
86b40 70 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 46 p->iForeGuard==F
86b50 4f 52 45 47 55 41 52 44 20 29 3b 0a 20 20 6e 52 OREGUARD );. nR
86b60 65 73 65 72 76 65 20 3d 20 28 70 2d 3e 69 53 69 eserve = (p->iSi
86b70 7a 65 2b 37 29 26 7e 37 3b 0a 20 20 70 49 6e 74 ze+7)&~7;. pInt
86b80 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 61 = (int*)pAlloca
86b90 74 69 6f 6e 3b 0a 20 20 70 55 38 20 3d 20 28 75 tion;. pU8 = (u
86ba0 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 8*)pAllocation;.
86bb0 20 20 61 73 73 65 72 74 28 20 70 49 6e 74 5b 6e assert( pInt[n
86bc0 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 69 Reserve/sizeof(i
86bd0 6e 74 29 5d 3d 3d 52 45 41 52 47 55 41 52 44 20 nt)]==REARGUARD
86be0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 52 );. assert( (nR
86bf0 65 73 65 72 76 65 2d 30 29 3c 3d 70 2d 3e 69 53 eserve-0)<=p->iS
86c00 69 7a 65 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65 ize || pU8[nRese
86c10 72 76 65 2d 31 5d 3d 3d 30 78 36 35 20 29 3b 0a rve-1]==0x65 );.
86c20 20 20 61 73 73 65 72 74 28 20 28 6e 52 65 73 65 assert( (nRese
86c30 72 76 65 2d 31 29 3c 3d 70 2d 3e 69 53 69 7a 65 rve-1)<=p->iSize
86c40 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65 72 76 65 || pU8[nReserve
86c50 2d 32 5d 3d 3d 30 78 36 35 20 29 3b 0a 20 20 61 -2]==0x65 );. a
86c60 73 73 65 72 74 28 20 28 6e 52 65 73 65 72 76 65 ssert( (nReserve
86c70 2d 32 29 3c 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c -2)<=p->iSize ||
86c80 20 70 55 38 5b 6e 52 65 73 65 72 76 65 2d 33 5d pU8[nReserve-3]
86c90 3d 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 ==0x65 );. retu
86ca0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn p;.}../*.** R
86cb0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
86cc0 20 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e of bytes curren
86cd0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 tly allocated at
86ce0 20 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 address p..*/.s
86cf0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
86d00 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 3MemSize(void *p
86d10 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 ){. struct MemB
86d20 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 lockHdr *pHdr;.
86d30 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 if( !p ){. r
86d40 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 eturn 0;. }. p
86d50 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d Hdr = sqlite3Mem
86d60 73 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b sysGetHeader(p);
86d70 0a 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e . return pHdr->
86d80 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iSize;.}../*.**
86d90 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d Initialize the m
86da0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
86db0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 subsystem..*/.s
86dc0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
86dd0 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 3MemInit(void *N
86de0 6f 74 55 73 65 64 29 7b 0a 20 20 69 66 28 20 21 otUsed){. if( !
86df0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
86e00 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a fig.bMemstat ){.
86e10 20 20 20 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 /* If memory
86e20 20 73 74 61 74 75 73 20 69 73 20 65 6e 61 62 6c status is enabl
86e30 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 6c ed, then the mal
86e40 6c 6f 63 2e 63 20 77 72 61 70 70 65 72 20 77 69 loc.c wrapper wi
86e50 6c 6c 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a ll already. *
86e60 2a 20 68 6f 6c 64 20 74 68 65 20 53 54 41 54 49 * hold the STATI
86e70 43 5f 4d 45 4d 20 6d 75 74 65 78 20 77 68 65 6e C_MEM mutex when
86e80 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 68 65 the routines he
86e90 72 65 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 re are invoked.
86ea0 2a 2f 0a 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 */. mem.mutex
86eb0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
86ec0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
86ed0 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 X_STATIC_MEM);.
86ee0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
86ef0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
86f00 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 Deinitialize the
86f10 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
86f20 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f on subsystem..*/
86f30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
86f40 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 ite3MemShutdown(
86f50 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
86f60 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 30 3b mem.mutex = 0;
86f70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 .}../*.** Round
86f80 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a up a request siz
86f90 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 e to the next va
86fa0 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 lid allocation s
86fb0 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ize..*/.static i
86fc0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 nt sqlite3MemRou
86fd0 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 ndup(int n){. r
86fe0 65 74 75 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 eturn (n+7) & ~7
86ff0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
87000 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 ate nByte bytes
87010 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 of memory..*/.st
87020 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 atic void *sqlit
87030 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 e3MemMalloc(int
87040 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63 74 nByte){. struct
87050 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 MemBlockHdr *pH
87060 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 dr;. void **pBt
87070 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 ;. char *z;. i
87080 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 64 nt *pInt;. void
87090 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 *p = 0;. int t
870a0 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 20 otalSize;. int
870b0 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 nReserve;. sqli
870c0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
870d0 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 61 73 mem.mutex);. as
870e0 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c sert( mem.disall
870f0 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 73 65 ow==0 );. nRese
87100 72 76 65 20 3d 20 28 6e 42 79 74 65 2b 37 29 26 rve = (nByte+7)&
87110 7e 37 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 ~7;. totalSize
87120 3d 20 6e 52 65 73 65 72 76 65 20 2b 20 73 69 7a = nReserve + siz
87130 65 6f 66 28 2a 70 48 64 72 29 20 2b 20 73 69 7a eof(*pHdr) + siz
87140 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20 20 20 20 eof(int) +.
87150 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e 42 mem.nB
87160 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 acktrace*sizeof(
87170 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e 6e 54 69 void*) + mem.nTi
87180 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f tle;. p = mallo
87190 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 c(totalSize);.
871a0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 7a 20 3d if( p ){. z =
871b0 20 70 3b 0a 20 20 20 20 70 42 74 20 3d 20 28 76 p;. pBt = (v
871c0 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69 oid**)&z[mem.nTi
871d0 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64 72 20 3d tle];. pHdr =
871e0 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 (struct MemBloc
871f0 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e kHdr*)&pBt[mem.n
87200 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20 20 20 20 Backtrace];.
87210 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b pHdr->pNext = 0;
87220 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 . pHdr->pPrev
87230 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20 = mem.pLast;.
87240 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20 if( mem.pLast
87250 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 4c 61 ){. mem.pLa
87260 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 st->pNext = pHdr
87270 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
87280 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 mem.pFirst =
87290 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 pHdr;. }.
872a0 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 mem.pLast = pHdr
872b0 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69 46 6f 72 ;. pHdr->iFor
872c0 65 47 75 61 72 64 20 3d 20 46 4f 52 45 47 55 41 eGuard = FOREGUA
872d0 52 44 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 42 RD;. pHdr->nB
872e0 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 3d 20 acktraceSlots =
872f0 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a mem.nBacktrace;.
87300 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 pHdr->nTitle
87310 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 = mem.nTitle;.
87320 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61 63 6b if( mem.nBack
87330 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 76 trace ){. v
87340 6f 69 64 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0a oid *aAddr[40];.
87350 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 pHdr->nBac
87360 6b 74 72 61 63 65 20 3d 20 62 61 63 6b 74 72 61 ktrace = backtra
87370 63 65 28 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42 ce(aAddr, mem.nB
87380 61 63 6b 74 72 61 63 65 2b 31 29 2d 31 3b 0a 20 acktrace+1)-1;.
87390 20 20 20 20 20 6d 65 6d 63 70 79 28 70 42 74 2c memcpy(pBt,
873a0 20 26 61 41 64 64 72 5b 31 5d 2c 20 70 48 64 72 &aAddr[1], pHdr
873b0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a ->nBacktrace*siz
873c0 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a 20 20 20 eof(void*));.
873d0 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b if( mem.xBack
873e0 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 trace ){.
873f0 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 mem.xBacktrace(
87400 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 nByte, pHdr->nBa
87410 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 cktrace-1, &aAdd
87420 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 r[1]);. }.
87430 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
87440 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
87450 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
87460 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 if( mem.nTitle )
87470 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a {. memcpy(z
87480 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 , mem.zTitle, me
87490 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d m.nTitle);. }
874a0 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 . pHdr->iSize
874b0 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 = nByte;. ad
874c0 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c justStats(nByte,
874d0 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d +1);. pInt =
874e0 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b (int*)&pHdr[1];
874f0 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 . pInt[nReser
87500 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 ve/sizeof(int)]
87510 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 = REARGUARD;.
87520 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 memset(pInt, 0x
87530 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 65, nReserve);.
87540 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 p = (void*)pI
87550 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 nt;. }. sqlite
87560 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
87570 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 m.mutex);. retu
87580 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn p; .}../*.**
87590 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a Free memory..*/.
875a0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
875b0 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 te3MemFree(void
875c0 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 *pPrior){. stru
875d0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
875e0 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 pHdr;. void **p
875f0 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 Bt;. char *z;.
87600 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
87610 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
87620 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 mstat || mem.mut
87630 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 ex!=0 );. pHdr
87640 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 = sqlite3MemsysG
87650 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 etHeader(pPrior)
87660 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a ;. pBt = (void*
87670 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d *)pHdr;. pBt -=
87680 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
87690 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 eSlots;. sqlite
876a0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
876b0 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 m.mutex);. if(
876c0 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 pHdr->pPrev ){.
876d0 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d assert( pHdr-
876e0 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 >pPrev->pNext==p
876f0 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d Hdr );. pHdr-
87700 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
87710 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d pHdr->pNext;. }
87720 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
87730 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 ( mem.pFirst==pH
87740 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 dr );. mem.pF
87750 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 irst = pHdr->pNe
87760 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 xt;. }. if( pH
87770 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 dr->pNext ){.
87780 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 assert( pHdr->p
87790 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 Next->pPrev==pHd
877a0 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 r );. pHdr->p
877b0 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 Next->pPrev = pH
877c0 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c dr->pPrev;. }el
877d0 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
877e0 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 mem.pLast==pHdr
877f0 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 );. mem.pLast
87800 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a = pHdr->pPrev;.
87810 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a }. z = (char*
87820 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 )pBt;. z -= pHd
87830 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a r->nTitle;. adj
87840 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 ustStats(pHdr->i
87850 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d Size, -1);. mem
87860 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a set(z, 0x2b, siz
87870 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d eof(void*)*pHdr-
87880 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
87890 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 + sizeof(*pHdr)
878a0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
878b0 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 pHdr->iSize
878c0 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b + sizeof(int) +
878d0 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a pHdr->nTitle);.
878e0 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c free(z);. sql
878f0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
87900 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d (mem.mutex); .}
87910 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
87920 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 he size of an ex
87930 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c isting memory al
87940 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 location..**.**
87950 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69 For this debuggi
87960 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
87970 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d n, we *always* m
87980 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ake a copy of th
87990 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e.** allocation
879a0 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 into a new place
879b0 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 in memory. In
879c0 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 this way, if the
879d0 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 .** higher leve
879e0 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 l code is using
879f0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f pointer to the o
87a00 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 ld allocation, i
87a10 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f t is .** much mo
87a20 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 re likely to bre
87a30 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 ak and we are mu
87a40 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 ch more liking t
87a50 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 o find.** the er
87a60 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ror..*/.static v
87a70 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 oid *sqlite3MemR
87a80 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
87a90 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b ior, int nByte){
87aa0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
87ab0 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a ckHdr *pOldHdr;.
87ac0 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 void *pNew;.
87ad0 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 assert( mem.disa
87ae0 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c llow==0 );. pOl
87af0 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 dHdr = sqlite3Me
87b00 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50 msysGetHeader(pP
87b10 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 rior);. pNew =
87b20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
87b30 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 (nByte);. if( p
87b40 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 New ){. memcp
87b50 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 y(pNew, pPrior,
87b60 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 nByte<pOldHdr->i
87b70 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 Size ? nByte : p
87b80 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a OldHdr->iSize);.
87b90 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f if( nByte>pO
87ba0 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a ldHdr->iSize ){.
87bb0 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 memset(&((
87bc0 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 char*)pNew)[pOld
87bd0 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 Hdr->iSize], 0x2
87be0 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 b, nByte - pOldH
87bf0 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 dr->iSize);.
87c00 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d }. sqlite3Mem
87c10 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 Free(pPrior);.
87c20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b }. return pNew;
87c30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 .}../*.** Popula
87c40 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c te the low-level
87c50 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
87c60 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e on function poin
87c70 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 ters in.** sqlit
87c80 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
87c90 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
87ca0 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 o the routines i
87cb0 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a n this file..*/.
87cc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
87cd0 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 oid sqlite3MemSe
87ce0 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a tDefault(void){.
87cf0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
87d00 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
87d10 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 ds defaultMethod
87d20 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 s = {. sqlit
87d30 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 e3MemMalloc,.
87d40 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 sqlite3MemFree
87d50 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
87d60 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 mRealloc,. s
87d70 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 qlite3MemSize,.
87d80 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f sqlite3MemRo
87d90 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 undup,. sqli
87da0 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 te3MemInit,.
87db0 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 sqlite3MemShutd
87dc0 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
87dd0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 . sqlite3_confi
87de0 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f g(SQLITE_CONFIG_
87df0 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 MALLOC, &default
87e00 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a Methods);.}../*.
87e10 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 ** Set the numbe
87e20 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c r of backtrace l
87e30 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 evels kept for e
87e40 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a ach allocation..
87e50 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 ** A value of ze
87e60 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 ro turns off bac
87e70 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e ktracing. The n
87e80 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 umber is always
87e90 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f rounded.** up to
87ea0 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 a multiple of 2
87eb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
87ec0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
87ed0 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 MemdebugBacktrac
87ee0 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 e(int depth){.
87ef0 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 if( depth<0 ){ d
87f00 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 epth = 0; }. if
87f10 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 ( depth>20 ){ de
87f20 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 pth = 20; }. de
87f30 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 pth = (depth+1)&
87f40 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 0xfe;. mem.nBac
87f50 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a ktrace = depth;.
87f60 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
87f70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
87f80 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 mdebugBacktraceC
87f90 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 allback(void (*x
87fa0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 Backtrace)(int,
87fb0 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a int, void **)){.
87fc0 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 mem.xBacktrace
87fd0 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d = xBacktrace;.}
87fe0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
87ff0 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 title string for
88000 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f subsequent allo
88010 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 cations..*/.SQLI
88020 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
88030 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 sqlite3MemdebugS
88040 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 ettitle(const ch
88050 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 69 ar *zTitle){. i
88060 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 54 nt n = strlen(zT
88070 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c itle) + 1;. sql
88080 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
88090 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 (mem.mutex);. i
880a0 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d f( n>=sizeof(mem
880b0 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 73 .zTitle) ) n = s
880c0 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 izeof(mem.zTitle
880d0 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65 )-1;. memcpy(me
880e0 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65 m.zTitle, zTitle
880f0 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 , n);. mem.zTit
88100 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d le[n] = 0;. mem
88110 2e 6e 54 69 74 6c 65 20 3d 20 28 6e 2b 37 29 26 .nTitle = (n+7)&
88120 7e 37 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 ~7;. sqlite3_mu
88130 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 tex_leave(mem.mu
88140 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f tex);.}..SQLITE_
88150 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
88160 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 6e 63 ite3MemdebugSync
88170 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d (){. struct Mem
88180 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a BlockHdr *pHdr;.
88190 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 for(pHdr=mem.p
881a0 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48 64 First; pHdr; pHd
881b0 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a r=pHdr->pNext){.
881c0 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 20 3d void **pBt =
881d0 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 (void**)pHdr;.
881e0 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e pBt -= pHdr->
881f0 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b nBacktraceSlots;
88200 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 . mem.xBacktr
88210 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c ace(pHdr->iSize,
88220 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
88230 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b 0a 20 e-1, &pBt[1]);.
88240 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e }.}../*.** Open
88250 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61 the file indica
88260 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20 ted and write a
88270 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 log of all unfre
88280 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c ed memory .** al
88290 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 locations into t
882a0 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 hat log..*/.SQLI
882b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
882c0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 44 sqlite3MemdebugD
882d0 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ump(const char *
882e0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49 zFilename){. FI
882f0 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63 LE *out;. struc
88300 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
88310 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 Hdr;. void **pB
88320 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6f 75 t;. int i;. ou
88330 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e t = fopen(zFilen
88340 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 66 28 ame, "w");. if(
88350 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66 out==0 ){. f
88360 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
88370 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 ** Unable to out
88380 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 put memory debug
88390 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 output log: %s
883a0 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 **\n",.
883b0 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 zFile
883c0 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 name);. retur
883d0 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 48 64 n;. }. for(pHd
883e0 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 r=mem.pFirst; pH
883f0 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 dr; pHdr=pHdr->p
88400 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 72 20 Next){. char
88410 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 64 72 *z = (char*)pHdr
88420 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d ;. z -= pHdr-
88430 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
88440 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b *sizeof(void*) +
88450 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 pHdr->nTitle;.
88460 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
88470 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73 "**** %lld bytes
88480 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 20 2a at %p from %s *
88490 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 ***\n", .
884a0 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 pHdr->iSize
884b0 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64 72 , &pHdr[1], pHdr
884c0 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22 ->nTitle ? z : "
884d0 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 20 70 ???");. if( p
884e0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 Hdr->nBacktrace
884f0 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 68 28 ){. fflush(
88500 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 74 20 out);. pBt
88510 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a = (void**)pHdr;.
88520 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 pBt -= pHd
88530 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f r->nBacktraceSlo
88540 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b 74 72 ts;. backtr
88550 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 70 ace_symbols_fd(p
88560 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 Bt, pHdr->nBackt
88570 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 race, fileno(out
88580 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ));. fprint
88590 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 f(out, "\n");.
885a0 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 }. }. fprint
885b0 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 3a 5c f(out, "COUNTS:\
885c0 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 n");. for(i=0;
885d0 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 i<NCSIZE-1; i++)
885e0 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 41 {. if( mem.nA
885f0 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 20 20 lloc[i] ){.
88600 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
88610 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 30 64 %5d: %10d %10d
88620 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 20 20 %10d\n", .
88630 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 6d 2e i*8, mem.
88640 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e nAlloc[i], mem.n
88650 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e Current[i], mem.
88660 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 mxCurrent[i]);.
88670 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d }. }. if( m
88680 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 em.nAlloc[NCSIZE
88690 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69 6e -1] ){. fprin
886a0 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64 3a tf(out, " %5d:
886b0 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64 5c %10d %10d %10d\
886c0 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
886d0 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d NCSIZE*8-8, mem
886e0 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 .nAlloc[NCSIZE-1
886f0 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ],.
88700 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 53 mem.nCurrent[NCS
88710 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 IZE-1], mem.mxCu
88720 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 rrent[NCSIZE-1])
88730 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f ;. }. fclose(o
88740 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ut);.}../*.** Re
88750 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
88760 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 65 33 of times sqlite3
88770 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 73 20 MemMalloc() has
88780 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a been called..*/.
88790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
887a0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 nt sqlite3Memdeb
887b0 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b ugMallocCount(){
887c0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
887d0 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 66 6f nTotal = 0;. fo
887e0 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b r(i=0; i<NCSIZE;
887f0 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 61 i++){. nTota
88800 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b l += mem.nAlloc[
88810 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e i];. }. return
88820 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e nTotal;.}...#en
88830 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 dif /* SQLITE_ME
88840 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a MDEBUG */../****
88850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
88860 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a f mem2.c *******
88870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
888a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
888b0 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a file mem3.c ***
888c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
888d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
888e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
888f0 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 2007 October 14
88900 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
88910 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
88920 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
88930 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
88940 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
88950 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
88960 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
88970 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
88980 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
88990 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
889a0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
889b0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
889c0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
889d0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
889e0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
889f0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
88a00 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
88a10 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
88a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
88a60 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
88a70 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
88a80 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
88a90 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a ment a memory.**
88aa0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
88ab0 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 ystem for use by
88ac0 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 SQLite. .**.**
88ad0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
88ae0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
88af0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 ation subsystem
88b00 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 omits all.** use
88b10 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 of malloc(). Th
88b20 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 73 75 e SQLite user su
88b30 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f pplies a block o
88b40 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f f memory.** befo
88b50 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 re calling sqlit
88b60 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
88b70 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 from which alloc
88b80 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 ations.** are ma
88b90 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 de and returned
88ba0 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 by the xMalloc()
88bb0 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 and xRealloc()
88bc0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
88bd0 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 ons. Once sqlite
88be0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 3_initialize() h
88bf0 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a as been called,.
88c00 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 ** the amount of
88c10 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c memory availabl
88c20 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 e to SQLite is f
88c30 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a ixed and cannot.
88c40 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a ** be changed..*
88c50 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f *.** This versio
88c60 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 n of the memory
88c70 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
88c80 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 stem is included
88c90 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 .** in the build
88ca0 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f only if SQLITE_
88cb0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 ENABLE_MEMSYS3 i
88cc0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a s defined..**.**
88cd0 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76 20 31 $Id: mem3.c,v 1
88ce0 2e 32 33 20 32 30 30 38 2f 30 39 2f 30 32 20 31 .23 2008/09/02 1
88cf0 37 3a 35 32 3a 35 32 20 64 61 6e 69 65 6c 6b 31 7:52:52 danielk1
88d00 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 977 Exp $.*/../*
88d10 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
88d20 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
88d30 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e 6c 79 llocator is only
88d40 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 built into the
88d50 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c 49 54 library.** SQLIT
88d60 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 E_ENABLE_MEMSYS3
88d70 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44 65 66 is defined. Def
88d80 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d 62 6f ining this symbo
88d90 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 l does not.** me
88da0 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 62 72 an that the libr
88db0 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61 20 6d ary will use a m
88dc0 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 64 65 emory-pool by de
88dd0 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68 61 74 fault, just that
88de0 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69 6c 61 .** it is availa
88df0 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f 6f 6c ble. The mempool
88e00 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 61 63 allocator is ac
88e10 74 69 76 61 74 65 64 20 62 79 20 63 61 6c 6c 69 tivated by calli
88e20 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f ng.** sqlite3_co
88e30 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 nfig()..*/.#ifde
88e40 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
88e50 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d MEMSYS3../*.** M
88e60 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20 aximum size (in
88e70 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61 Mem3Blocks) of a
88e80 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a "small" chunk..
88e90 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d */.#define MX_SM
88ea0 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e ALL 10.../*.** N
88eb0 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73 umber of freelis
88ec0 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a t hash slots.*/.
88ed0 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48 20 20 #define N_HASH
88ee0 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 61../*.** A memo
88ef0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 ry allocation (a
88f00 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22 63 68 lso called a "ch
88f10 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73 20 6f unk") consists o
88f20 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 f two or .** mor
88f30 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65 20 65 e blocks where e
88f40 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38 20 62 ach block is 8 b
88f50 79 74 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 ytes. The first
88f60 20 38 20 62 79 74 65 73 20 61 72 65 20 0a 2a 2a 8 bytes are .**
88f70 20 61 20 68 65 61 64 65 72 20 74 68 61 74 20 69 a header that i
88f80 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 20 74 s not returned t
88f90 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a o the user..**.*
88fa0 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74 77 6f * A chunk is two
88fb0 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 or more blocks
88fc0 74 68 61 74 20 69 73 20 65 69 74 68 65 72 20 63 that is either c
88fd0 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a 2a 2a hecked out or.**
88fe0 20 66 72 65 65 2e 20 20 54 68 65 20 66 69 72 73 free. The firs
88ff0 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d t block has form
89000 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68 64 72 at u.hdr. u.hdr
89010 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74 69 6d .size4x is 4 tim
89020 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f es the.** size o
89030 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
89040 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 in blocks if th
89050 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 e allocation is
89060 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 free..** The u.h
89070 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69 74 20 dr.size4x&1 bit
89080 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 63 is true if the c
89090 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 hunk is checked
890a0 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 out and.** false
890b0 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 if the chunk is
890c0 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 on the freelist
890d0 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a . The u.hdr.siz
890e0 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69 73 20 e4x&2 bit.** is
890f0 74 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76 true if the prev
89100 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 63 68 ious chunk is ch
89110 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 66 61 ecked out and fa
89120 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20 70 72 lse if the.** pr
89130 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 evious chunk is
89140 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68 64 72 free. The u.hdr
89150 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c 64 20 .prevSize field
89160 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a is the size of.*
89170 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 * the previous c
89180 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 20 69 hunk in blocks i
89190 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 f the previous c
891a0 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 0a 2a hunk is on the.*
891b0 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66 20 74 * freelist. If t
891c0 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e he previous chun
891d0 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 k is checked out
891e0 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e , then.** u.hdr.
891f0 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62 65 20 prevSize can be
89200 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
89210 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e 6b 20 for that chunk
89220 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f and should.** no
89230 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 t be read or wri
89240 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f tten..**.** We o
89250 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20 61 20 ften identify a
89260 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69 6e 64 chunk by its ind
89270 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c ex in mem3.aPool
89280 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69 []. When.** thi
89290 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65 20 63 s is done, the c
892a0 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66 65 72 hunk index refer
892b0 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 s to the second
892c0 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 block of.** the
892d0 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69 73 20 chunk. In this
892e0 77 61 79 2c 20 74 68 65 20 66 69 72 73 74 20 63 way, the first c
892f0 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e 64 65 hunk has an inde
89300 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 68 75 x of 1..** A chu
89310 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20 6d 65 nk index of 0 me
89320 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63 68 75 ans "no such chu
89330 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65 20 65 nk" and is the e
89340 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 quivalent.** of
89350 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
89360 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 **.** The second
89370 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65 20 63 block of free c
89380 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68 65 20 hunks is of the
89390 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68 form u.list. Th
893a0 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 73 20 e.** two fields
893b0 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69 form a double-li
893c0 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 68 75 nked list of chu
893d0 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 20 73 nks of related s
893e0 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 izes..** Pointer
893f0 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 s to the head of
89400 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 73 74 the list are st
89410 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53 ored in mem3.aiS
89420 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 mall[] .** for s
89430 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 61 6e maller chunks an
89440 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 20 d mem3.aiHash[]
89450 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 6e 6b for larger chunk
89460 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 s..**.** The sec
89470 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61 20 63 ond block of a c
89480 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64 61 74 hunk is user dat
89490 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 a if the chunk i
894a0 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 s checked .** ou
894b0 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b 20 69 t. If a chunk i
894c0 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 s checked out, t
894d0 68 65 20 75 73 65 72 20 64 61 74 61 20 6d 61 79 he user data may
894e0 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 extend into.**
894f0 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76 53 69 the u.hdr.prevSi
89500 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 ze value of the
89510 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e 6b 2e following chunk.
89520 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
89530 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d ct Mem3Block Mem
89540 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 20 4d 3Block;.struct M
89550 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 em3Block {. uni
89560 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 on {. struct
89570 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72 65 76 {. u32 prev
89580 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a 65 20 Size; /* Size
89590 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e of previous chun
895a0 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 k in Mem3Block e
895b0 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 lements */.
895c0 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20 20 20 u32 size4x;
895d0 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a 65 20 /* 4x the size
895e0 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75 6e 6b of current chunk
895f0 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c in Mem3Block el
89600 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 7d 20 ements */. }
89610 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 hdr;. struct
89620 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65 78 74 {. u32 next
89630 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 ; /* Index
89640 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d in mem3.aPool[]
89650 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 of next free ch
89660 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 unk */. u32
89670 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 prev; /*
89680 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 Index in mem3.aP
89690 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75 ool[] of previou
896a0 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a s free chunk */.
896b0 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 } list;. }
896c0 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c u;.};../*.** All
896d0 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76 of the static v
896e0 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 ariables used by
896f0 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 this module are
89700 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e collected.** in
89710 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 to a single stru
89720 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d cture named "mem
89730 33 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 3". This is to
89740 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 keep the.** stat
89750 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 ic variables org
89760 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 anized and to re
89770 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 duce namespace p
89780 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e ollution.** when
89790 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 this module is
897a0 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 combined with ot
897b0 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 her in the amalg
897c0 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 amation..*/.stat
897d0 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 ic SQLITE_WSD st
897e0 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 ruct Mem3Global
897f0 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f {. /*. ** Memo
89800 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 ry available for
89810 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f allocation. nPo
89820 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f ol is the size o
89830 66 20 74 68 65 20 61 72 72 61 79 0a 20 20 2a 2a f the array. **
89840 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 (in Mem3Blocks)
89850 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 pointed to by a
89860 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 20 2a Pool less 2.. *
89870 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 /. u32 nPool;.
89880 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f Mem3Block *aPoo
89890 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 l;.. /*. ** Tr
898a0 75 65 20 69 66 20 77 65 20 61 72 65 20 65 76 61 ue if we are eva
898b0 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f luating an out-o
898c0 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63 f-memory callbac
898d0 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c k.. */. int al
898e0 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a armBusy;. . /*
898f0 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 . ** Mutex to c
89900 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f ontrol access to
89910 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
89920 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
89930 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
89940 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 _mutex *mutex;.
89950 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 . /*. ** The
89960 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f minimum amount o
89970 66 20 66 72 65 65 20 73 70 61 63 65 20 74 68 61 f free space tha
89980 74 20 77 65 20 68 61 76 65 20 73 65 65 6e 2e 0a t we have seen..
89990 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73 */. u32 mnMas
899a0 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ter;.. /*. **
899b0 69 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 69 iMaster is the i
899c0 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 73 74 ndex of the mast
899d0 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 er chunk. Most
899e0 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a new allocations.
899f0 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f ** occur off o
89a00 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20 20 73 f this chunk. s
89a10 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 73 zMaster is the s
89a20 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 ize (in Mem3Bloc
89a30 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 ks). ** of the
89a40 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72 2e 20 current master.
89a50 20 69 4d 61 73 74 65 72 20 69 73 20 30 20 69 66 iMaster is 0 if
89a60 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 61 there is not ma
89a70 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a ster chunk.. **
89a80 20 54 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e The master chun
89a90 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 74 68 k is not in eith
89aa0 65 72 20 74 68 65 20 61 69 48 61 73 68 5b 5d 20 er the aiHash[]
89ab0 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 or aiSmall[]..
89ac0 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74 65 72 */. u32 iMaster
89ad0 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74 65 72 ;. u32 szMaster
89ae0 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 ;.. /*. ** Arr
89af0 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66 20 66 ay of lists of f
89b00 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72 ree blocks accor
89b10 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c 6f 63 ding to the bloc
89b20 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 k size . ** for
89b30 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c smaller chunks,
89b40 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20 74 68 or a hash on th
89b50 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72 e block size for
89b60 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63 68 75 larger. ** chu
89b70 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 nks.. */. u32
89b80 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c aiSmall[MX_SMALL
89b90 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69 -1]; /* For si
89ba0 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 4d 58 zes 2 through MX
89bb0 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 _SMALL, inclusiv
89bc0 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48 61 73 e */. u32 aiHas
89bd0 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 20 20 h[N_HASH];
89be0 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d /* For sizes M
89bf0 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 X_SMALL+1 and la
89c00 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d rger */.} mem3 =
89c10 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b 0a 0a { 97535575 };..
89c20 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47 4c 4f #define mem3 GLO
89c30 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 33 47 BAL(struct Mem3G
89c40 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a lobal, mem3)../*
89c50 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 .** Unlink the c
89c60 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f hunk at mem3.aPo
89c70 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 ol[i] from list
89c80 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a it is currently.
89c90 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 ** on. *pRoot i
89ca0 73 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20 s the list that
89cb0 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 i is a member of
89cc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
89cd0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
89ce0 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20 75 33 omList(u32 i, u3
89cf0 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 2 *pRoot){. u32
89d00 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f next = mem3.aPo
89d10 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 ol[i].u.list.nex
89d20 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20 3d 20 t;. u32 prev =
89d30 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
89d40 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61 73 73 list.prev;. ass
89d50 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
89d60 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
89d70 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 72 65 ex) );. if( pre
89d80 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 6f v==0 ){. *pRo
89d90 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c ot = next;. }el
89da0 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f se{. mem3.aPo
89db0 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e ol[prev].u.list.
89dc0 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d next = next;. }
89dd0 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b 0a 20 . if( next ){.
89de0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 mem3.aPool[ne
89df0 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 xt].u.list.prev
89e00 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 = prev;. }. me
89e10 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 m3.aPool[i].u.li
89e20 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 20 6d st.next = 0;. m
89e30 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c em3.aPool[i].u.l
89e40 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a 7d 0a ist.prev = 0;.}.
89e50 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 ./*.** Unlink th
89e60 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 e chunk at index
89e70 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 i from .** what
89e80 65 76 65 72 20 6c 69 73 74 20 69 73 20 63 75 72 ever list is cur
89e90 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65 72 20 rently a member
89ea0 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f of..*/.static vo
89eb0 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b id memsys3Unlink
89ec0 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73 (u32 i){. u32 s
89ed0 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 ize, hash;. ass
89ee0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
89ef0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
89f00 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
89f10 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 (mem3.aPool[i-1
89f20 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 ].u.hdr.size4x &
89f30 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 1)==0 );. asse
89f40 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69 rt( i>=1 );. si
89f50 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
89f60 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
89f70 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73 x/4;. assert( s
89f80 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ize==mem3.aPool[
89f90 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e i+size-1].u.hdr.
89fa0 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 prevSize );. as
89fb0 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b sert( size>=2 );
89fc0 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d . if( size <= M
89fd0 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d X_SMALL ){. m
89fe0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d emsys3UnlinkFrom
89ff0 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 List(i, &mem3.ai
8a000 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a Small[size-2]);.
8a010 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 }else{. has
8a020 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 h = size % N_HAS
8a030 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e H;. memsys3Un
8a040 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 linkFromList(i,
8a050 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 &mem3.aiHash[has
8a060 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a h]);. }.}../*.*
8a070 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b * Link the chunk
8a080 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 at mem3.aPool[i
8a090 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 ] so that is on
8a0a0 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 64 0a the list rooted.
8a0b0 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f ** at *pRoot..*/
8a0c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8a0d0 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 sys3LinkIntoList
8a0e0 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f (u32 i, u32 *pRo
8a0f0 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 ot){. assert( s
8a100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
8a110 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
8a120 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d . mem3.aPool[i]
8a130 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 2a .u.list.next = *
8a140 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 pRoot;. mem3.aP
8a150 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 ool[i].u.list.pr
8a160 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20 2a 70 ev = 0;. if( *p
8a170 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33 Root ){. mem3
8a180 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 .aPool[*pRoot].u
8a190 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a .list.prev = i;.
8a1a0 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d 20 69 }. *pRoot = i
8a1b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 ;.}../*.** Link
8a1c0 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 the chunk at ind
8a1d0 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68 65 72 ex i into either
8a1e0 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
8a1f0 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 .** small chunk
8a200 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68 list, or into th
8a210 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 e large chunk ha
8a220 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 sh table..*/.sta
8a230 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
8a240 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 Link(u32 i){. u
8a250 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 32 size, hash;.
8a260 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
8a270 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 _mutex_held(mem3
8a280 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 .mutex) );. ass
8a290 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 ert( i>=1 );. a
8a2a0 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f ssert( (mem3.aPo
8a2b0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
8a2c0 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a ze4x & 1)==0 );.
8a2d0 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 size = mem3.aP
8a2e0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
8a2f0 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 ize4x/4;. asser
8a300 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 t( size==mem3.aP
8a310 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e ool[i+size-1].u.
8a320 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a hdr.prevSize );.
8a330 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d assert( size>=
8a340 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 2 );. if( size
8a350 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 <= MX_SMALL ){.
8a360 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e memsys3LinkIn
8a370 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e toList(i, &mem3.
8a380 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 aiSmall[size-2])
8a390 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 ;. }else{. h
8a3a0 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 ash = size % N_H
8a3b0 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 ASH;. memsys3
8a3c0 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 LinkIntoList(i,
8a3d0 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 &mem3.aiHash[has
8a3e0 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a h]);. }.}../*.*
8a3f0 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f * If the STATIC_
8a400 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 MEM mutex is not
8a410 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f already held, o
8a420 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 btain it now. Th
8a430 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 e mutex.** will
8a440 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64 20 already be held
8a450 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64 (obtained by cod
8a460 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 e in malloc.c) i
8a470 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 f.** sqlite3Glob
8a480 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 alConfig.bMemSta
8a490 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 t is true..*/.st
8a4a0 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
8a4b0 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 3Enter(void){.
8a4c0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
8a4d0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
8a4e0 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75 74 65 ==0 && mem3.mute
8a4f0 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 33 x==0 ){. mem3
8a500 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 .mutex = sqlite3
8a510 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
8a520 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
8a530 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 EM);. }. sqlit
8a540 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
8a550 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 em3.mutex);.}.st
8a560 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
8a570 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 3Leave(void){.
8a580 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
8a590 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b ave(mem3.mutex);
8a5a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 .}../*.** Called
8a5b0 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 6e 61 when we are una
8a5c0 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 20 61 ble to satisfy a
8a5d0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 n allocation of
8a5e0 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 nBytes..*/.stati
8a5f0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75 c void memsys3Ou
8a600 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42 tOfMemory(int nB
8a610 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d yte){. if( !mem
8a620 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20 3.alarmBusy ){.
8a630 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 mem3.alarmBus
8a640 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 y = 1;. asser
8a650 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
8a660 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 _held(mem3.mutex
8a670 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) );. sqlite3
8a680 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
8a690 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 3.mutex);. sq
8a6a0 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
8a6b0 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20 20 20 mory(nByte);.
8a6c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
8a6d0 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 nter(mem3.mutex)
8a6e0 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d ;. mem3.alarm
8a6f0 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a Busy = 0;. }.}.
8a700 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 ../*.** Chunk i
8a710 69 73 20 61 20 66 72 65 65 20 63 68 75 6e 6b 20 is a free chunk
8a720 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 6e that has been un
8a730 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 74 20 linked. Adjust
8a740 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70 61 72 its .** size par
8a750 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 65 63 ameters for chec
8a760 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 72 6e k-out and return
8a770 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
8a780 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72 74 69 e .** user porti
8a790 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e 6b 2e on of the chunk.
8a7a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8a7b0 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 *memsys3Checkout
8a7c0 28 75 33 32 20 69 2c 20 69 6e 74 20 6e 42 6c 6f (u32 i, int nBlo
8a7d0 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20 ck){. u32 x;.
8a7e0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
8a7f0 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e mutex_held(mem3.
8a800 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
8a810 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 rt( i>=1 );. as
8a820 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c sert( mem3.aPool
8a830 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
8a840 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 4x/4==nBlock );.
8a850 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 assert( mem3.a
8a860 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d Pool[i+nBlock-1]
8a870 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3d .u.hdr.prevSize=
8a880 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 20 3d =nBlock );. x =
8a890 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
8a8a0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 .u.hdr.size4x;.
8a8b0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
8a8c0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 .u.hdr.size4x =
8a8d0 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c 20 28 nBlock*4 | 1 | (
8a8e0 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f x&2);. mem3.aPo
8a8f0 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 ol[i+nBlock-1].u
8a900 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 .hdr.prevSize =
8a910 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 nBlock;. mem3.a
8a920 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d Pool[i+nBlock-1]
8a930 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d .u.hdr.size4x |=
8a940 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 2;. return &me
8a950 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a m3.aPool[i];.}..
8a960 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 70 69 /*.** Carve a pi
8a970 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65 ece off of the e
8a980 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 2e 69 nd of the mem3.i
8a990 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 75 6e Master free chun
8a9a0 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 k..** Return a p
8a9b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
8a9c0 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f w allocation. O
8a9d0 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74 65 72 r, if the master
8a9e0 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 chunk.** is not
8a9f0 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 72 large enough, r
8aa00 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 eturn 0..*/.stat
8aa10 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 ic void *memsys3
8aa20 46 72 6f 6d 4d 61 73 74 65 72 28 69 6e 74 20 6e FromMaster(int n
8aa30 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74 Block){. assert
8aa40 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
8aa50 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
8aa60 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 );. assert( me
8aa70 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c m3.szMaster>=nBl
8aa80 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e 42 6c ock );. if( nBl
8aa90 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 ock>=mem3.szMast
8aaa0 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 55 er-1 ){. /* U
8aab0 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 se the entire ma
8aac0 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f 69 64 ster */. void
8aad0 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43 68 65 *p = memsys3Che
8aae0 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 73 74 ckout(mem3.iMast
8aaf0 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 er, mem3.szMaste
8ab00 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 4d 61 r);. mem3.iMa
8ab10 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 ster = 0;. me
8ab20 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b m3.szMaster = 0;
8ab30 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 . mem3.mnMast
8ab40 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 er = 0;. retu
8ab50 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 rn p;. }else{.
8ab60 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 /* Split the
8ab70 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52 master block. R
8ab80 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c 2e 20 eturn the tail.
8ab90 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77 69 2c */. u32 newi,
8aba0 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d 20 6d x;. newi = m
8abb0 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20 6d 65 em3.iMaster + me
8abc0 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20 6e 42 m3.szMaster - nB
8abd0 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 lock;. assert
8abe0 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e 69 4d ( newi > mem3.iM
8abf0 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20 20 6d aster+1 );. m
8ac00 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8ac10 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 Master+mem3.szMa
8ac20 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 ster-1].u.hdr.pr
8ac30 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b evSize = nBlock;
8ac40 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
8ac50 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
8ac60 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 3.szMaster-1].u.
8ac70 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b hdr.size4x |= 2;
8ac80 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
8ac90 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 newi-1].u.hdr.si
8aca0 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 ze4x = nBlock*4
8acb0 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a + 1;. mem3.sz
8acc0 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b Master -= nBlock
8acd0 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ;. mem3.aPool
8ace0 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 [newi-1].u.hdr.p
8acf0 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 revSize = mem3.s
8ad00 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78 20 3d zMaster;. x =
8ad10 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
8ad20 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 .iMaster-1].u.hd
8ad30 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 r.size4x & 2;.
8ad40 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d mem3.aPool[mem
8ad50 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 3.iMaster-1].u.h
8ad60 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 dr.size4x = mem3
8ad70 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b .szMaster*4 | x;
8ad80 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a . if( mem3.sz
8ad90 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e Master < mem3.mn
8ada0 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 Master ){.
8adb0 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 mem3.mnMaster =
8adc0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 mem3.szMaster;.
8add0 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
8ade0 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f (void*)&mem3.aPo
8adf0 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a ol[newi];. }.}.
8ae00 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 ./*.** *pRoot is
8ae10 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c the head of a l
8ae20 69 73 74 20 6f 66 20 66 72 65 65 20 63 68 75 6e ist of free chun
8ae30 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 ks of the same s
8ae40 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73 ize.** or same s
8ae50 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 6f 74 ize hash. In ot
8ae60 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f her words, *pRoo
8ae70 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e t is an entry in
8ae80 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e either.** mem3.
8ae90 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d aiSmall[] or mem
8aea0 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 3.aiHash[]. .**
8aeb0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
8aec0 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e examines all en
8aed0 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67 69 76 tries on the giv
8aee0 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72 69 65 en list and trie
8aef0 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65 s.** to coalesce
8af00 20 65 61 63 68 20 65 6e 74 72 69 65 73 20 77 69 each entries wi
8af10 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 th adjacent free
8af20 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a chunks. .**.**
8af30 20 49 66 20 69 74 20 73 65 65 73 20 61 20 63 68 If it sees a ch
8af40 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61 72 67 unk that is larg
8af50 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 er than mem3.iMa
8af60 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61 63 65 ster, it replace
8af70 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e s .** the curren
8af80 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 77 t mem3.iMaster w
8af90 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61 72 67 ith the new larg
8afa0 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 er chunk. In or
8afb0 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69 73 20 der for.** this
8afc0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72 65 70 mem3.iMaster rep
8afd0 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f 72 6b lacement to work
8afe0 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 , the master chu
8aff0 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20 6c 69 nk must be.** li
8b000 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 68 61 nked into the ha
8b010 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68 61 74 sh tables. That
8b020 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d is not the norm
8b030 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 61 al state of.** a
8b040 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75 72 73 ffairs, of cours
8b050 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 e. The calling
8b060 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e routine must lin
8b070 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 k the master.**
8b080 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76 chunk before inv
8b090 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 oking this routi
8b0a0 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 75 6e ne, then must un
8b0b0 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 69 62 link the (possib
8b0c0 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d ly.** changed) m
8b0d0 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65 aster chunk once
8b0e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 this routine ha
8b0f0 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 s finished..*/.s
8b100 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
8b110 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70 52 6f s3Merge(u32 *pRo
8b120 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65 78 74 ot){. u32 iNext
8b130 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20 69 2c , prev, size, i,
8b140 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 x;.. assert( s
8b150 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
8b160 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
8b170 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b . for(i=*pRoot;
8b180 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29 7b 0a i>0; i=iNext){.
8b190 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 iNext = mem3
8b1a0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
8b1b0 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a 65 20 .next;. size
8b1c0 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 = mem3.aPool[i-1
8b1d0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a ].u.hdr.size4x;.
8b1e0 20 20 20 20 61 73 73 65 72 74 28 20 28 73 69 7a assert( (siz
8b1f0 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 e&1)==0 );. i
8b200 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30 20 29 f( (size&2)==0 )
8b210 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 {. memsys3U
8b220 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c nlinkFromList(i,
8b230 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 61 pRoot);. a
8b240 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d 33 2e ssert( i > mem3.
8b250 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
8b260 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 20 .prevSize );.
8b270 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20 6d 65 prev = i - me
8b280 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
8b290 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 hdr.prevSize;.
8b2a0 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d 69 4e if( prev==iN
8b2b0 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ext ){. i
8b2c0 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f Next = mem3.aPoo
8b2d0 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e l[prev].u.list.n
8b2e0 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ext;. }.
8b2f0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
8b300 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 73 69 (prev);. si
8b310 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f 34 20 ze = i + size/4
8b320 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20 78 20 - prev;. x
8b330 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 = mem3.aPool[pre
8b340 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 v-1].u.hdr.size4
8b350 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d x & 2;. mem
8b360 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 3.aPool[prev-1].
8b370 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 73 u.hdr.size4x = s
8b380 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 ize*4 | x;.
8b390 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 mem3.aPool[prev
8b3a0 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 +size-1].u.hdr.p
8b3b0 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a revSize = size;.
8b3c0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e memsys3Lin
8b3d0 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 69 k(prev);. i
8b3e0 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d 65 6c = prev;. }el
8b3f0 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 2f se{. size /
8b400 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 4;. }. i
8b410 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d f( size>mem3.szM
8b420 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
8b430 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b em3.iMaster = i;
8b440 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 . mem3.szMa
8b450 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20 ster = size;.
8b460 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
8b470 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f Return a block o
8b480 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c f memory of at l
8b490 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 east nBytes in s
8b4a0 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e ize..** Return N
8b4b0 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a ULL if unable..*
8b4c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
8b4d0 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 on assumes that
8b4e0 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75 the necessary mu
8b4f0 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 texes, if any, a
8b500 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 re.** already he
8b510 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ld by the caller
8b520 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22 . Hence "Unsafe"
8b530 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
8b540 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 *memsys3MallocU
8b550 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29 nsafe(int nByte)
8b560 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 69 6e 74 {. u32 i;. int
8b570 20 6e 42 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 74 nBlock;. int t
8b580 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65 72 74 oFree;.. assert
8b590 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
8b5a0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
8b5b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 );. assert( si
8b5c0 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d zeof(Mem3Block)=
8b5d0 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 =8 );. if( nByt
8b5e0 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e 42 6c e<=12 ){. nBl
8b5f0 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 ock = 2;. }else
8b600 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 {. nBlock = (
8b610 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b 0a 20 nByte + 11)/8;.
8b620 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 42 6c }. assert( nBl
8b630 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 ock>=2 );.. /*
8b640 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f STEP 1:. ** Loo
8b650 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 20 6f k for an entry o
8b660 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 f the correct si
8b670 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74 68 65 ze in either the
8b680 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e small. ** chun
8b690 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20 74 68 k table or in th
8b6a0 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 e large chunk ha
8b6b0 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 sh table. This
8b6c0 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 66 is. ** successf
8b6d0 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 ul most of the t
8b6e0 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74 69 6d ime (about 9 tim
8b6f0 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e 0a 20 es out of 10)..
8b700 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b */. if( nBlock
8b710 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a <= MX_SMALL ){.
8b720 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53 i = mem3.aiS
8b730 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a mall[nBlock-2];.
8b740 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 if( i>0 ){.
8b750 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
8b760 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d nkFromList(i, &m
8b770 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f em3.aiSmall[nBlo
8b780 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20 72 65 ck-2]);. re
8b790 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 turn memsys3Chec
8b7a0 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b kout(i, nBlock);
8b7b0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
8b7c0 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d 20 6e int hash = n
8b7d0 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48 3b 0a Block % N_HASH;.
8b7e0 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 for(i=mem3.a
8b7f0 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69 3e 30 iHash[hash]; i>0
8b800 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ; i=mem3.aPool[i
8b810 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a ].u.list.next){.
8b820 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 if( mem3.a
8b830 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
8b840 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b size4x/4==nBlock
8b850 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 ){. mems
8b860 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 ys3UnlinkFromLis
8b870 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 t(i, &mem3.aiHas
8b880 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 20 20 h[hash]);.
8b890 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 return memsys3
8b8a0 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f Checkout(i, nBlo
8b8b0 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ck);. }.
8b8c0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 }. }.. /* STE
8b8d0 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f P 2:. ** Try to
8b8e0 20 73 61 74 69 73 66 79 20 74 68 65 20 61 6c 6c satisfy the all
8b8f0 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 76 69 ocation by carvi
8b900 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 20 6f ng a piece off o
8b910 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f f the end. ** o
8b920 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 f the master chu
8b930 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 20 75 nk. This step u
8b940 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20 sually works if
8b950 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a 20 20 step 1 fails..
8b960 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a */. if( mem3.sz
8b970 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 Master>=nBlock )
8b980 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d {. return mem
8b990 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e sys3FromMaster(n
8b9a0 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 Block);. }...
8b9b0 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 20 2a /* STEP 3: . *
8b9c0 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 * Loop through t
8b9d0 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79 he entire memory
8b9e0 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65 pool. Coalesce
8b9f0 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 0a 20 adjacent free.
8ba00 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 65 63 ** chunks. Rec
8ba10 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73 74 65 ompute the maste
8ba20 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65 20 6c r chunk as the l
8ba30 61 72 67 65 73 74 20 66 72 65 65 20 63 68 75 6e argest free chun
8ba40 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 72 79 k.. ** Then try
8ba50 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69 73 66 again to satisf
8ba60 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e y the allocation
8ba70 20 62 79 20 63 61 72 76 69 6e 67 20 61 20 70 69 by carving a pi
8ba80 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 ece off. ** of
8ba90 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d the end of the m
8baa0 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 aster chunk. Th
8bab0 69 73 20 73 74 65 70 20 68 61 70 70 65 6e 73 20 is step happens
8bac0 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79 very. ** rarely
8bad0 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f (we hope!). */
8bae0 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42 . for(toFree=nB
8baf0 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c lock*16; toFree<
8bb00 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b (mem3.nPool*16);
8bb10 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b 0a 20 toFree *= 2){.
8bb20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d memsys3OutOfM
8bb30 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b 0a 20 emory(toFree);.
8bb40 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 if( mem3.iMas
8bb50 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ter ){. mem
8bb60 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d sys3Link(mem3.iM
8bb70 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 aster);. me
8bb80 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a m3.iMaster = 0;.
8bb90 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 mem3.szMas
8bba0 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 ter = 0;. }.
8bbb0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f for(i=0; i<N_
8bbc0 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 HASH; i++){.
8bbd0 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 memsys3Merge(&
8bbe0 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 29 3b mem3.aiHash[i]);
8bbf0 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
8bc00 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 =0; i<MX_SMALL-1
8bc10 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 ; i++){. me
8bc20 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 msys3Merge(&mem3
8bc30 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 .aiSmall[i]);.
8bc40 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 }. if( mem3
8bc50 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 .szMaster ){.
8bc60 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
8bc70 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a (mem3.iMaster);.
8bc80 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 if( mem3.s
8bc90 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 zMaster>=nBlock
8bca0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
8bcb0 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 n memsys3FromMas
8bcc0 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 ter(nBlock);.
8bcd0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
8bce0 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 /* If none of
8bcf0 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b 65 64 the above worked
8bd00 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c 2e 20 , then we fail.
8bd10 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d */. return 0;.}
8bd20 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 ../*.** Free an
8bd30 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f outstanding memo
8bd40 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a ry allocation..*
8bd50 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
8bd60 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 on assumes that
8bd70 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75 the necessary mu
8bd80 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 texes, if any, a
8bd90 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 re.** already he
8bda0 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ld by the caller
8bdb0 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22 . Hence "Unsafe"
8bdc0 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73 ..*/.void memsys
8bdd0 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64 3FreeUnsafe(void
8bde0 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 *pOld){. Mem3B
8bdf0 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 33 42 lock *p = (Mem3B
8be00 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e lock*)pOld;. in
8be10 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a 65 2c t i;. u32 size,
8be20 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 x;. assert( sq
8be30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
8be40 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a (mem3.mutex) );.
8be50 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65 6d 33 assert( p>mem3
8be60 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d .aPool && p<&mem
8be70 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 3.aPool[mem3.nPo
8be80 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70 20 2d ol] );. i = p -
8be90 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 mem3.aPool;. a
8bea0 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f ssert( (mem3.aPo
8beb0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
8bec0 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a 20 20 ze4x&1)==1 );.
8bed0 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
8bee0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
8bef0 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 e4x/4;. assert(
8bf00 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 i+size<=mem3.nP
8bf10 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d 33 2e ool+1 );. mem3.
8bf20 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
8bf30 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20 .size4x &= ~1;.
8bf40 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 mem3.aPool[i+si
8bf50 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 ze-1].u.hdr.prev
8bf60 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d Size = size;. m
8bf70 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 em3.aPool[i+size
8bf80 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8bf90 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 &= ~2;. memsys
8bfa0 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 3Link(i);.. /*
8bfb0 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20 74 68 Try to expand th
8bfc0 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67 20 74 e master using t
8bfd0 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 20 63 he newly freed c
8bfe0 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65 hunk */. if( me
8bff0 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a 20 20 m3.iMaster ){.
8c000 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 while( (mem3.a
8c010 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8c020 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
8c030 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 x&2)==0 ){.
8c040 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f size = mem3.aPo
8c050 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d ol[mem3.iMaster-
8c060 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
8c070 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d e;. mem3.iM
8c080 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 aster -= size;.
8c090 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 mem3.szMast
8c0a0 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 20 20 er += size;.
8c0b0 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 memsys3Unlink(
8c0c0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 mem3.iMaster);.
8c0d0 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 x = mem3.aP
8c0e0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
8c0f0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8c100 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 & 2;. mem3
8c110 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8c120 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
8c130 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 e4x = mem3.szMas
8c140 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 ter*4 | x;.
8c150 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
8c160 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
8c170 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8c180 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e prevSize = mem3.
8c190 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a szMaster;. }.
8c1a0 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f x = mem3.aPo
8c1b0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d ol[mem3.iMaster-
8c1c0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8c1d0 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 & 2;. while(
8c1e0 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 (mem3.aPool[mem3
8c1f0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
8c200 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8c210 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29 7b 0a size4x&1)==0 ){.
8c220 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c memsys3Unl
8c230 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ink(mem3.iMaster
8c240 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b +mem3.szMaster);
8c250 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 . mem3.szMa
8c260 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f ster += mem3.aPo
8c270 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b ol[mem3.iMaster+
8c280 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d mem3.szMaster-1]
8c290 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b .u.hdr.size4x/4;
8c2a0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f . mem3.aPoo
8c2b0 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 l[mem3.iMaster-1
8c2c0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d ].u.hdr.size4x =
8c2d0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 mem3.szMaster*4
8c2e0 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 | x;. mem3
8c2f0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8c300 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ter+mem3.szMaste
8c310 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 r-1].u.hdr.prevS
8c320 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 ize = mem3.szMas
8c330 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ter;. }. }.}
8c340 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
8c350 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 he size of an ou
8c360 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 tstanding alloca
8c370 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 tion, in bytes.
8c380 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 The.** size ret
8c390 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 urned omits the
8c3a0 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 8-byte header ov
8c3b0 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e erhead. This on
8c3c0 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 ly.** works for
8c3d0 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 chunks that are
8c3e0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 currently checke
8c3f0 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 d out..*/.static
8c400 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69 7a 65 int memsys3Size
8c410 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d 65 6d (void *p){. Mem
8c420 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 3Block *pBlock;.
8c430 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
8c440 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 urn 0;. pBlock
8c450 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b = (Mem3Block*)p;
8c460 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 6c 6f . assert( (pBlo
8c470 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ck[-1].u.hdr.siz
8c480 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20 20 72 e4x&1)!=0 );. r
8c490 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 eturn (pBlock[-1
8c4a0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e ].u.hdr.size4x&~
8c4b0 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 3)*2 - 4;.}../*.
8c4c0 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 ** Round up a re
8c4d0 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 quest size to th
8c4e0 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c e next valid all
8c4f0 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f ocation size..*/
8c500 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 .static int mems
8c510 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e ys3Roundup(int n
8c520 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32 20 29 ){. if( n<=12 )
8c530 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 32 3b {. return 12;
8c540 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
8c550 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e 37 29 turn ((n+11)&~7)
8c560 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a - 4;. }.}../*.
8c570 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 ** Allocate nByt
8c580 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f es of memory..*/
8c590 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
8c5a0 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 msys3Malloc(int
8c5b0 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 nBytes){. sqlit
8c5c0 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 e3_int64 *p;. a
8c5d0 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e 30 20 ssert( nBytes>0
8c5e0 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d ); /* m
8c5f0 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72 73 20 alloc.c filters
8c600 6f 75 74 20 30 20 62 79 74 65 20 72 65 71 75 65 out 0 byte reque
8c610 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 sts */. memsys3
8c620 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d Enter();. p = m
8c630 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 emsys3MallocUnsa
8c640 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 6d 65 fe(nBytes);. me
8c650 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 msys3Leave();.
8c660 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b return (void*)p;
8c670 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 .}../*.** Free
8c680 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 memory..*/.void
8c690 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f 69 64 memsys3Free(void
8c6a0 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 *pPrior){. ass
8c6b0 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b 0a 20 ert( pPrior );.
8c6c0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b memsys3Enter();
8c6d0 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e . memsys3FreeUn
8c6e0 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 safe(pPrior);.
8c6f0 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a memsys3Leave();.
8c700 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
8c710 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 the size of an e
8c720 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 xisting memory a
8c730 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 llocation.*/.voi
8c740 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f d *memsys3Reallo
8c750 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 c(void *pPrior,
8c760 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 int nBytes){. i
8c770 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 nt nOld;. void
8c780 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 *p;. if( pPrior
8c790 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
8c7a0 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 n sqlite3_malloc
8c7b0 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 (nBytes);. }.
8c7c0 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b if( nBytes<=0 ){
8c7d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
8c7e0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 72 e(pPrior);. r
8c7f0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
8c800 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a Old = memsys3Siz
8c810 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 e(pPrior);. if(
8c820 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 nBytes<=nOld &&
8c830 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 nBytes>=nOld-12
8c840 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 8 ){. return
8c850 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 pPrior;. }. me
8c860 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 msys3Enter();.
8c870 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f p = memsys3Mallo
8c880 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b cUnsafe(nBytes);
8c890 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
8c8a0 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 if( nOld<nBytes
8c8b0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
8c8c0 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 p, pPrior, nOld)
8c8d0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
8c8e0 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 memcpy(p, pPr
8c8f0 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 ior, nBytes);.
8c900 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73 33 46 }. memsys3F
8c910 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 reeUnsafe(pPrior
8c920 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 );. }. memsys3
8c930 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72 Leave();. retur
8c940 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e n p;.}../*.** In
8c950 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f itialize this mo
8c960 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dule..*/.static
8c970 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69 74 28 int memsys3Init(
8c980 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
8c990 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c if( !sqlite3Gl
8c9a0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 obalConfig.pHeap
8c9b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
8c9c0 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
8c9d0 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 .. /* Store a p
8c9e0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 ointer to the me
8c9f0 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c mory block in gl
8ca00 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d obal structure m
8ca10 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 em3. */. assert
8ca20 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f ( sizeof(Mem3Blo
8ca30 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 ck)==8 );. mem3
8ca40 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c .aPool = (Mem3Bl
8ca50 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f ock *)sqlite3Glo
8ca60 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b balConfig.pHeap;
8ca70 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 . mem3.nPool =
8ca80 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
8ca90 6e 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a nfig.nHeap / siz
8caa0 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 eof(Mem3Block))
8cab0 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 - 2;.. /* Initi
8cac0 61 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 alize the master
8cad0 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d block. */. mem
8cae0 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 3.szMaster = mem
8caf0 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 3.nPool;. mem3.
8cb00 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e mnMaster = mem3.
8cb10 73 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 szMaster;. mem3
8cb20 2e 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 .iMaster = 1;.
8cb30 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e mem3.aPool[0].u.
8cb40 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 hdr.size4x = (me
8cb50 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 m3.szMaster<<2)
8cb60 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f + 2;. mem3.aPoo
8cb70 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e l[mem3.nPool].u.
8cb80 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d hdr.prevSize = m
8cb90 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d em3.nPool;. mem
8cba0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 3.aPool[mem3.nPo
8cbb0 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 ol].u.hdr.size4x
8cbc0 20 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 = 1;.. return
8cbd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
8cbe0 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 .** Deinitialize
8cbf0 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f this module..*/
8cc00 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8cc10 73 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 sys3Shutdown(voi
8cc20 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 72 d *NotUsed){. r
8cc30 65 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a eturn;.}..../*.*
8cc40 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 * Open the file
8cc50 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 indicated and wr
8cc60 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c ite a log of all
8cc70 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 unfreed memory
8cc80 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 .** allocations
8cc90 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a into that log..*
8cca0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
8ccb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
8ccc0 73 79 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 sys3Dump(const c
8ccd0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
8cce0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
8ccf0 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 EBUG. FILE *out
8cd00 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 ;. int i, j;.
8cd10 75 33 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 u32 size;. if(
8cd20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 zFilename==0 ||
8cd30 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 zFilename[0]==0
8cd40 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 ){. out = std
8cd50 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 out;. }else{.
8cd60 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 out = fopen(zF
8cd70 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 ilename, "w");.
8cd80 20 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b if( out==0 ){
8cd90 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 . fprintf(s
8cda0 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c tderr, "** Unabl
8cdb0 65 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f e to output memo
8cdc0 72 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 ry debug output
8cdd0 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 log: %s **\n",.
8cde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8cdf0 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b zFilename);
8ce00 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
8ce10 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 }. }. memsy
8ce20 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 s3Enter();. fpr
8ce30 69 6e 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b intf(out, "CHUNK
8ce40 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d S:\n");. for(i=
8ce50 31 3b 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 1; i<=mem3.nPool
8ce60 3b 20 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 ; i+=size/4){.
8ce70 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 size = mem3.aP
8ce80 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
8ce90 69 7a 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 ize4x;. if( s
8cea0 69 7a 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 ize/4<=1 ){.
8ceb0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8cec0 25 70 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 %p size error\n"
8ced0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d , &mem3.aPool[i]
8cee0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
8cef0 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 0 );. brea
8cf00 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 k;. }. if(
8cf10 20 28 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 (size&1)==0 &&
8cf20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a mem3.aPool[i+siz
8cf30 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 e/4-1].u.hdr.pre
8cf40 76 53 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b vSize!=size/4 ){
8cf50 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
8cf60 75 74 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a ut, "%p tail siz
8cf70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 e does not match
8cf80 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c \n", &mem3.aPool
8cf90 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 [i]);. asse
8cfa0 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 rt( 0 );. b
8cfb0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
8cfc0 69 66 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c if( ((mem3.aPool
8cfd0 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 [i+size/4-1].u.h
8cfe0 64 72 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 dr.size4x&2)>>1)
8cff0 21 3d 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 !=(size&1) ){.
8d000 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
8d010 20 22 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f "%p tail checko
8d020 75 74 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 ut bit is incorr
8d030 65 63 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 ect\n", &mem3.aP
8d040 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 ool[i]);. a
8d050 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 ssert( 0 );.
8d060 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
8d070 20 20 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b if( size&1 ){
8d080 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
8d090 75 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 ut, "%p %6d byte
8d0a0 73 20 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 s checked out\n"
8d0b0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d , &mem3.aPool[i]
8d0c0 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b , (size/4)*8-8);
8d0d0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
8d0e0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8d0f0 25 70 20 25 36 64 20 62 79 74 65 73 20 66 72 65 %p %6d bytes fre
8d100 65 25 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 e%s\n", &mem3.aP
8d110 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 ool[i], (size/4)
8d120 2a 38 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 *8-8,.
8d130 20 20 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e i==mem3.
8d140 69 4d 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 iMaster ? " **ma
8d150 73 74 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 ster**" : "");.
8d160 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 }. }. for(i
8d170 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 =0; i<MX_SMALL-1
8d180 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
8d190 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d mem3.aiSmall[i]=
8d1a0 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 =0 ) continue;.
8d1b0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
8d1c0 22 73 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 "small(%2d):", i
8d1d0 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d );. for(j = m
8d1e0 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 em3.aiSmall[i];
8d1f0 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f j>0; j=mem3.aPoo
8d200 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 l[j].u.list.next
8d210 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 ){. fprintf
8d220 28 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c (out, " %p(%d)",
8d230 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c &mem3.aPool[j],
8d240 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 . (
8d250 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e mem3.aPool[j-1].
8d260 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a u.hdr.size4x/4)*
8d270 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 8-8);. }.
8d280 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e fprintf(out, "\n
8d290 22 29 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 "); . }. for(i
8d2a0 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b =0; i<N_HASH; i+
8d2b0 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 +){. if( mem3
8d2c0 2e 61 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 .aiHash[i]==0 )
8d2d0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 continue;. fp
8d2e0 72 69 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 rintf(out, "hash
8d2f0 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 (%2d):", i);.
8d300 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 for(j = mem3.ai
8d310 48 61 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d Hash[i]; j>0; j=
8d320 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e mem3.aPool[j].u.
8d330 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 list.next){.
8d340 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8d350 20 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e %p(%d)", &mem3.
8d360 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 aPool[j],.
8d370 20 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 (mem3.aP
8d380 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[j-1].u.hdr.s
8d390 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 ize4x/4)*8-8);.
8d3a0 20 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 }. fprintf
8d3b0 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 (out, "\n"); .
8d3c0 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c }. fprintf(out,
8d3d0 20 22 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 "master=%d\n",
8d3e0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 mem3.iMaster);.
8d3f0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e fprintf(out, "n
8d400 6f 77 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 owUsed=%d\n", me
8d410 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d m3.nPool*8 - mem
8d420 33 2e 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 3.szMaster*8);.
8d430 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
8d440 78 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d xUsed=%d\n", mem
8d450 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 3.nPool*8 - mem3
8d460 2e 6d 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 .mnMaster*8);.
8d470 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
8d480 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b ave(mem3.mutex);
8d490 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f . if( out==stdo
8d4a0 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 ut ){. fflush
8d4b0 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 (stdout);. }els
8d4c0 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 e{. fclose(ou
8d4d0 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d t);. }.#endif.}
8d4e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
8d4f0 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 tine is the only
8d500 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 routine in this
8d510 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 file with exter
8d520 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e nal .** linkage.
8d530 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 .**.** Populate
8d540 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 the low-level me
8d550 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
8d560 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
8d570 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 s in.** sqlite3G
8d580 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 lobalConfig.m wi
8d590 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 th pointers to t
8d5a0 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 he routines in t
8d5b0 68 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a 2a his file. The.**
8d5c0 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 arguments speci
8d5d0 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 fy the block of
8d5e0 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65 memory to manage
8d5f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
8d600 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c tine is only cal
8d610 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 led by sqlite3_c
8d620 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68 65 onfig(), and the
8d630 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 refore.** is not
8d640 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20 required to be
8d650 74 68 72 65 61 64 73 61 66 65 20 28 69 74 20 69 threadsafe (it i
8d660 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 s not)..*/.SQLIT
8d670 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
8d680 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
8d690 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 ods *sqlite3MemG
8d6a0 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 7b etMemsys3(void){
8d6b0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
8d6c0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
8d6d0 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f ods mempoolMetho
8d6e0 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 ds = {. mems
8d6f0 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 ys3Malloc,.
8d700 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20 20 memsys3Free,.
8d710 20 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 memsys3Realloc
8d720 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 69 ,. memsys3Si
8d730 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 ze,. memsys3
8d740 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 Roundup,. me
8d750 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20 20 msys3Init,.
8d760 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e 2c memsys3Shutdown,
8d770 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 . 0. };. r
8d780 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 eturn &mempoolMe
8d790 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 thods;.}..#endif
8d7a0 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
8d7b0 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a E_MEMSYS3 */../*
8d7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
8d7d0 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a d of mem3.c ****
8d7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
8d810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
8d820 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20 gin file mem5.c
8d830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
8d860 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 .** 2007 October
8d870 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 14.**.** The au
8d880 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
8d890 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
8d8a0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
8d8b0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
8d8c0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
8d8d0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
8d8e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
8d8f0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
8d900 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
8d910 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
8d920 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
8d930 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
8d940 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
8d950 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
8d960 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
8d970 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
8d980 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
8d990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d9d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
8d9e0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 contains the C f
8d9f0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d unctions that im
8da00 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 plement a memory
8da10 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 .** allocation s
8da20 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 ubsystem for use
8da30 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a by SQLite. .**.
8da40 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
8da50 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
8da60 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
8da70 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 em omits all.**
8da80 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e use of malloc().
8da90 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 The SQLite user
8daa0 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 supplies a bloc
8dab0 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 k of memory.** b
8dac0 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 efore calling sq
8dad0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
8dae0 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c () from which al
8daf0 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 locations.** are
8db00 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e made and return
8db10 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f ed by the xMallo
8db20 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 c() and xRealloc
8db30 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 () .** implement
8db40 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c ations. Once sql
8db50 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
8db60 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 ) has been calle
8db70 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 d,.** the amount
8db80 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c of memory avail
8db90 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 able to SQLite i
8dba0 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e s fixed and cann
8dbb0 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 ot.** be changed
8dbc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ..**.** This ver
8dbd0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
8dbe0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
8dbf0 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 bsystem is inclu
8dc00 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 ded.** in the bu
8dc10 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 ild only if SQLI
8dc20 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
8dc30 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 5 is defined..**
8dc40 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 35 2e 63 2c .** $Id: mem5.c,
8dc50 76 20 31 2e 31 35 20 32 30 30 38 2f 31 30 2f 32 v 1.15 2008/10/2
8dc60 38 20 31 38 3a 35 38 3a 32 30 20 64 72 68 20 45 8 18:58:20 drh E
8dc70 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 xp $.*/../*.** T
8dc80 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
8dc90 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
8dca0 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 tor is used only
8dcb0 20 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 when .** SQLITE
8dcc0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 _ENABLE_MEMSYS5
8dcd0 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 is defined..*/.#
8dce0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
8dcf0 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a BLE_MEMSYS5../*.
8dd00 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c ** A minimum all
8dd10 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e ocation is an in
8dd20 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
8dd30 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
8dd40 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c e..** Larger all
8dd50 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20 ocations are an
8dd60 61 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73 array of these s
8dd70 74 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20 tructures where
8dd80 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 the.** size of t
8dd90 68 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f he array is a po
8dda0 77 65 72 20 6f 66 20 32 2e 0a 2a 2f 0a 74 79 70 wer of 2..*/.typ
8ddb0 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 35 edef struct Mem5
8ddc0 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0a 73 Link Mem5Link;.s
8ddd0 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 7b truct Mem5Link {
8dde0 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 20 20 . int next;
8ddf0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e /* Index of n
8de00 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20 2a ext free chunk *
8de10 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20 20 20 /. int prev;
8de20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
8de30 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 63 68 previous free ch
8de40 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a unk */.};../*.**
8de50 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 Maximum size of
8de60 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 any allocation
8de70 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58 29 2a is ((1<<LOGMAX)*
8de80 6d 65 6d 35 2e 6e 41 74 6f 6d 29 2e 20 53 69 6e mem5.nAtom). Sin
8de90 63 65 0a 2a 2a 20 6d 65 6d 35 2e 6e 41 74 6f 6d ce.** mem5.nAtom
8dea0 20 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65 is always at le
8deb0 61 73 74 20 38 2c 20 74 68 69 73 20 69 73 20 6e ast 8, this is n
8dec0 6f 74 20 72 65 61 6c 6c 79 20 61 20 70 72 61 63 ot really a prac
8ded0 74 69 63 61 6c 0a 2a 2a 20 6c 69 6d 69 74 61 74 tical.** limitat
8dee0 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ion..*/.#define
8def0 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f 2a 0a 2a 2a LOGMAX 30../*.**
8df00 20 4d 61 73 6b 73 20 75 73 65 64 20 66 6f 72 20 Masks used for
8df10 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 65 6c 65 mem5.aCtrl[] ele
8df20 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e ments..*/.#defin
8df30 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 20 20 e CTRL_LOGSIZE
8df40 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f 67 32 20 0x1f /* Log2
8df50 53 69 7a 65 20 6f 66 20 74 68 69 73 20 62 6c 6f Size of this blo
8df60 63 6b 20 72 65 6c 61 74 69 76 65 20 74 6f 20 50 ck relative to P
8df70 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 23 64 65 66 69 OW2_MIN */.#defi
8df80 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 20 20 ne CTRL_FREE
8df90 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 75 65 0x20 /* True
8dfa0 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 if not checked
8dfb0 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c out */../*.** Al
8dfc0 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 l of the static
8dfd0 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 variables used b
8dfe0 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 y this module ar
8dff0 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 e collected.** i
8e000 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 nto a single str
8e010 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 ucture named "me
8e020 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f m5". This is to
8e030 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 keep the.** sta
8e040 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 tic variables or
8e050 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 ganized and to r
8e060 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 educe namespace
8e070 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 pollution.** whe
8e080 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 n this module is
8e090 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f combined with o
8e0a0 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c ther in the amal
8e0b0 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 gamation..*/.sta
8e0c0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 tic SQLITE_WSD s
8e0d0 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c truct Mem5Global
8e0e0 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d {. /*. ** Mem
8e0f0 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 6f ory available fo
8e100 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 20 2a r allocation. *
8e110 2f 0a 20 20 69 6e 74 20 6e 41 74 6f 6d 3b 20 20 /. int nAtom;
8e120 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 73 74 /* Smallest
8e130 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f 63 61 possible alloca
8e140 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20 2a 2f tion in bytes */
8e150 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20 20 . int nBlock;
8e160 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
8e170 20 6e 41 74 6f 6d 20 73 69 7a 65 64 20 62 6c 6f nAtom sized blo
8e180 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a 2f 0a cks in zPool */.
8e190 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 0a 20 20 0a u8 *zPool;. .
8e1a0 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 /*. ** Mutex
8e1b0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 to control acces
8e1c0 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 s to the memory
8e1d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
8e1e0 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c stem.. */. sql
8e1f0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
8e200 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65 x;.. /*. ** Pe
8e210 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 rformance statis
8e220 74 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20 tics. */. u64
8e230 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 nAlloc;
8e240 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
8e250 6f 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c of calls to mall
8e260 6f 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 oc */. u64 tota
8e270 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54 lAlloc; /* T
8e280 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c otal of all mall
8e290 6f 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75 oc calls - inclu
8e2a0 64 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 des internal fra
8e2b0 67 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c g */. u64 total
8e2c0 45 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f Excess; /* To
8e2d0 74 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 tal internal fra
8e2e0 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 gmentation */.
8e2f0 75 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20 u32 currentOut;
8e300 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63 /* Current c
8e310 68 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69 heckout, includi
8e320 6e 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 ng internal frag
8e330 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 mentation */. u
8e340 33 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 32 currentCount;
8e350 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 /* Current nu
8e360 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74 mber of distinct
8e370 20 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20 checkouts */.
8e380 75 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20 u32 maxOut;
8e390 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 /* Maximum i
8e3a0 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 nstantaneous cur
8e3b0 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32 rentOut */. u32
8e3c0 20 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20 maxCount;
8e3d0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 /* Maximum inst
8e3e0 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e antaneous curren
8e3f0 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20 tCount */. u32
8e400 6d 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20 maxRequest;
8e410 2f 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63 /* Largest alloc
8e420 61 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65 ation (exclusive
8e430 20 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 of internal fra
8e440 67 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 g) */. . /*.
8e450 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65 ** Lists of free
8e460 20 62 6c 6f 63 6b 73 20 6f 66 20 76 61 72 69 6f blocks of vario
8e470 75 73 20 73 69 7a 65 73 2e 0a 20 20 2a 2f 0a 20 us sizes.. */.
8e480 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73 74 5b int aiFreelist[
8e490 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20 2f 2a LOGMAX+1];.. /*
8e4a0 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f 72 20 . ** Space for
8e4b0 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68 20 62 tracking which b
8e4c0 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63 6b 65 locks are checke
8e4d0 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20 73 69 d out and the si
8e4e0 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 ze. ** of each
8e4f0 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79 74 65 block. One byte
8e500 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f per block.. */
8e510 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a 0a 7d . u8 *aCtrl;..}
8e520 20 6d 65 6d 35 20 3d 20 7b 20 31 39 38 30 34 31 mem5 = { 198041
8e530 36 37 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 67 };..#define m
8e540 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 em5 GLOBAL(struc
8e550 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65 t Mem5Global, me
8e560 6d 35 29 0a 0a 23 64 65 66 69 6e 65 20 4d 45 4d m5)..#define MEM
8e570 35 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d 5LINK(idx) ((Mem
8e580 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 5Link *)(&mem5.z
8e590 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e Pool[(idx)*mem5.
8e5a0 6e 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 nAtom]))../*.**
8e5b0 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b Unlink the chunk
8e5c0 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 at mem5.aPool[i
8e5d0 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 ] from list it i
8e5e0 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f s currently.** o
8e5f0 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62 65 n. It should be
8e600 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61 found on mem5.a
8e610 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 iFreelist[iLogsi
8e620 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ze]..*/.static v
8e630 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e oid memsys5Unlin
8e640 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f k(int i, int iLo
8e650 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e 65 gsize){. int ne
8e660 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73 65 xt, prev;. asse
8e670 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 rt( i>=0 && i<me
8e680 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 m5.nBlock );. a
8e690 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e ssert( iLogsize>
8e6a0 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d =0 && iLogsize<=
8e6b0 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 LOGMAX );. asse
8e6c0 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b rt( (mem5.aCtrl[
8e6d0 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a i] & CTRL_LOGSIZ
8e6e0 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a E)==iLogsize );.
8e6f0 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c 49 . next = MEM5LI
8e700 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 70 NK(i)->next;. p
8e710 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 rev = MEM5LINK(i
8e720 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20 70 )->prev;. if( p
8e730 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65 6d rev<0 ){. mem
8e740 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
8e750 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a 20 gsize] = next;.
8e760 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d 35 }else{. MEM5
8e770 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78 74 LINK(prev)->next
8e780 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 = next;. }. i
8e790 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 20 f( next>=0 ){.
8e7a0 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 29 MEM5LINK(next)
8e7b0 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 20 ->prev = prev;.
8e7c0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b }.}../*.** Link
8e7d0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 the chunk at me
8e7e0 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 m5.aPool[i] so t
8e7f0 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69 4c hat is on the iL
8e800 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20 6c ogsize.** free l
8e810 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ist..*/.static v
8e820 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 oid memsys5Link(
8e830 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 int i, int iLogs
8e840 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a 20 ize){. int x;.
8e850 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
8e860 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35 _mutex_held(mem5
8e870 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 .mutex) );. ass
8e880 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d ert( i>=0 && i<m
8e890 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8e8a0 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 assert( iLogsize
8e8b0 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c >=0 && iLogsize<
8e8c0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 =LOGMAX );. ass
8e8d0 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c ert( (mem5.aCtrl
8e8e0 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 [i] & CTRL_LOGSI
8e8f0 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b ZE)==iLogsize );
8e900 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b .. x = MEM5LINK
8e910 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d 35 (i)->next = mem5
8e920 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 .aiFreelist[iLog
8e930 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e size];. MEM5LIN
8e940 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31 3b K(i)->prev = -1;
8e950 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a 20 . if( x>=0 ){.
8e960 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65 6d assert( x<mem
8e970 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 5.nBlock );.
8e980 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 65 MEM5LINK(x)->pre
8e990 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d v = i;. }. mem
8e9a0 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
8e9b0 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f gsize] = i;.}../
8e9c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 *.** If the STAT
8e9d0 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 IC_MEM mutex is
8e9e0 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 not already held
8e9f0 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e , obtain it now.
8ea00 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 The mutex.** wi
8ea10 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 ll already be he
8ea20 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 ld (obtained by
8ea30 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 code in malloc.c
8ea40 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 ) if.** sqlite3G
8ea50 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
8ea60 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f Stat is true..*/
8ea70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8ea80 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29 7b sys5Enter(void){
8ea90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
8eaa0 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 obalConfig.bMems
8eab0 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d 35 2e 6d tat==0 && mem5.m
8eac0 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d utex==0 ){. m
8ead0 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 em5.mutex = sqli
8eae0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
8eaf0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
8eb00 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 C_MEM);. }. sq
8eb10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
8eb20 72 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d r(mem5.mutex);.}
8eb30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8eb40 73 79 73 35 4c 65 61 76 65 28 76 6f 69 64 29 7b sys5Leave(void){
8eb50 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
8eb60 5f 6c 65 61 76 65 28 6d 65 6d 35 2e 6d 75 74 65 _leave(mem5.mute
8eb70 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 x);.}../*.** Ret
8eb80 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 urn the size of
8eb90 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 an outstanding a
8eba0 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 llocation, in by
8ebb0 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a tes. The.** siz
8ebc0 65 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 e returned omits
8ebd0 20 74 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 the 8-byte head
8ebe0 65 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 er overhead. Th
8ebf0 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 is only.** works
8ec00 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 for chunks that
8ec10 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 are currently c
8ec20 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 hecked out..*/.s
8ec30 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
8ec40 35 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 5Size(void *p){.
8ec50 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 30 3b int iSize = 0;
8ec60 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
8ec70 69 6e 74 20 69 20 3d 20 28 28 75 38 20 2a 29 70 int i = ((u8 *)p
8ec80 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d -mem5.zPool)/mem
8ec90 35 2e 6e 41 74 6f 6d 3b 0a 20 20 20 20 61 73 73 5.nAtom;. ass
8eca0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d ert( i>=0 && i<m
8ecb0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8ecc0 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d 35 2e 6e iSize = mem5.n
8ecd0 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 20 28 6d 65 Atom * (1 << (me
8ece0 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 43 54 52 4c m5.aCtrl[i]&CTRL
8ecf0 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a 20 20 7d 0a _LOGSIZE));. }.
8ed00 20 20 72 65 74 75 72 6e 20 69 53 69 7a 65 3b 0a return iSize;.
8ed10 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 }../*.** Find th
8ed20 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f 6e e first entry on
8ed30 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 4c the freelist iL
8ed40 6f 67 73 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b 20 ogsize. Unlink
8ed50 74 68 61 74 0a 2a 2a 20 65 6e 74 72 79 20 61 6e that.** entry an
8ed60 64 20 72 65 74 75 72 6e 20 69 74 73 20 69 6e 64 d return its ind
8ed70 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ex. .*/.static i
8ed80 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b nt memsys5Unlink
8ed90 46 69 72 73 74 28 69 6e 74 20 69 4c 6f 67 73 69 First(int iLogsi
8eda0 7a 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 ze){. int i;.
8edb0 69 6e 74 20 69 46 69 72 73 74 3b 0a 0a 20 20 61 int iFirst;.. a
8edc0 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e ssert( iLogsize>
8edd0 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d =0 && iLogsize<=
8ede0 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 69 20 3d 20 LOGMAX );. i =
8edf0 69 46 69 72 73 74 20 3d 20 6d 65 6d 35 2e 61 69 iFirst = mem5.ai
8ee00 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a Freelist[iLogsiz
8ee10 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 46 e];. assert( iF
8ee20 69 72 73 74 3e 3d 30 20 29 3b 0a 20 20 77 68 69 irst>=0 );. whi
8ee30 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 69 le( i>0 ){. i
8ee40 66 28 20 69 3c 69 46 69 72 73 74 20 29 20 69 46 f( i<iFirst ) iF
8ee50 69 72 73 74 20 3d 20 69 3b 0a 20 20 20 20 69 20 irst = i;. i
8ee60 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e = MEM5LINK(i)->n
8ee70 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 ext;. }. memsy
8ee80 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72 73 74 2c s5Unlink(iFirst,
8ee90 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 72 65 iLogsize);. re
8eea0 74 75 72 6e 20 69 46 69 72 73 74 3b 0a 7d 0a 0a turn iFirst;.}..
8eeb0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 /*.** Return a b
8eec0 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f lock of memory o
8eed0 66 20 61 74 20 6c 65 61 73 74 20 6e 42 79 74 65 f at least nByte
8eee0 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 s in size..** Re
8eef0 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 turn NULL if una
8ef00 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ble..*/.static v
8ef10 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c oid *memsys5Mall
8ef20 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 ocUnsafe(int nBy
8ef30 74 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 te){. int i;
8ef40 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
8ef50 20 6f 66 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c of a mem5.aPool
8ef60 5b 5d 20 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 [] slot */. int
8ef70 20 69 42 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a iBin; /*
8ef80 20 49 6e 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 Index into mem5
8ef90 2e 61 69 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f .aiFreelist[] */
8efa0 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 . int iFullSz;
8efb0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 /* Size of a
8efc0 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 llocation rounde
8efd0 64 20 75 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 d up to power of
8efe0 20 32 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 2 */. int iLog
8eff0 73 69 7a 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 size; /* Log2
8f000 20 6f 66 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 of iFullSz/POW2
8f010 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 4b 65 _MIN */.. /* Ke
8f020 65 70 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 ep track of the
8f030 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 maximum allocati
8f040 6f 6e 20 72 65 71 75 65 73 74 2e 20 20 45 76 65 on request. Eve
8f050 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64 0a 20 20 n unfulfilled.
8f060 2a 2a 20 72 65 71 75 65 73 74 73 20 61 72 65 20 ** requests are
8f070 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 counted */. if(
8f080 20 6e 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 nByte>mem5.maxR
8f090 65 71 75 65 73 74 20 29 7b 0a 20 20 20 20 6d 65 equest ){. me
8f0a0 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 3d 20 m5.maxRequest =
8f0b0 6e 42 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a nByte;. }.. /*
8f0c0 20 52 6f 75 6e 64 20 6e 42 79 74 65 20 75 70 20 Round nByte up
8f0d0 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 to the next vali
8f0e0 64 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 2a d power of two *
8f0f0 2f 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 50 4f /. if( nByte>PO
8f100 57 32 5f 4d 41 58 20 29 20 72 65 74 75 72 6e 20 W2_MAX ) return
8f110 30 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 0;. for(iFullSz
8f120 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 2c 20 69 4c 6f =mem5.nAtom, iLo
8f130 67 73 69 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a gsize=0; iFullSz
8f140 3c 6e 42 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20 <nByte; iFullSz
8f150 2a 3d 20 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b *= 2, iLogsize++
8f160 29 7b 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 ){}.. /* Make s
8f170 75 72 65 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c ure mem5.aiFreel
8f180 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 63 6f ist[iLogsize] co
8f190 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20 ntains at least
8f1a0 6f 6e 65 20 66 72 65 65 0a 20 20 2a 2a 20 62 6c one free. ** bl
8f1b0 6f 63 6b 2e 20 20 49 66 20 6e 6f 74 2c 20 74 68 ock. If not, th
8f1c0 65 6e 20 73 70 6c 69 74 20 61 20 62 6c 6f 63 6b en split a block
8f1d0 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6c 61 72 of the next lar
8f1e0 67 65 72 20 70 6f 77 65 72 20 6f 66 0a 20 20 2a ger power of. *
8f1f0 2a 20 74 77 6f 20 69 6e 20 6f 72 64 65 72 20 74 * two in order t
8f200 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 66 o create a new f
8f210 72 65 65 20 62 6c 6f 63 6b 20 6f 66 20 73 69 7a ree block of siz
8f220 65 20 69 4c 6f 67 73 69 7a 65 2e 0a 20 20 2a 2f e iLogsize.. */
8f230 0a 20 20 66 6f 72 28 69 42 69 6e 3d 69 4c 6f 67 . for(iBin=iLog
8f240 73 69 7a 65 3b 20 6d 65 6d 35 2e 61 69 46 72 65 size; mem5.aiFre
8f250 65 6c 69 73 74 5b 69 42 69 6e 5d 3c 30 20 26 26 elist[iBin]<0 &&
8f260 20 69 42 69 6e 3c 3d 4c 4f 47 4d 41 58 3b 20 69 iBin<=LOGMAX; i
8f270 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 Bin++){}. if( i
8f280 42 69 6e 3e 4c 4f 47 4d 41 58 20 29 20 72 65 74 Bin>LOGMAX ) ret
8f290 75 72 6e 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d urn 0;. i = mem
8f2a0 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 sys5UnlinkFirst(
8f2b0 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20 iBin);. while(
8f2c0 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b iBin>iLogsize ){
8f2d0 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 . int newSize
8f2e0 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20 ;.. iBin--;.
8f2f0 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c newSize = 1 <
8f300 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 35 < iBin;. mem5
8f310 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65 .aCtrl[i+newSize
8f320 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 ] = CTRL_FREE |
8f330 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73 iBin;. memsys
8f340 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c 5Link(i+newSize,
8f350 20 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 iBin);. }. me
8f360 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c m5.aCtrl[i] = iL
8f370 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70 ogsize;.. /* Up
8f380 64 61 74 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70 date allocator p
8f390 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 erformance stati
8f3a0 73 74 69 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 35 stics. */. mem5
8f3b0 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d .nAlloc++;. mem
8f3c0 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20 5.totalAlloc +=
8f3d0 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d 65 6d 35 2e iFullSz;. mem5.
8f3e0 74 6f 74 61 6c 45 78 63 65 73 73 20 2b 3d 20 69 totalExcess += i
8f3f0 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 74 65 3b 0a FullSz - nByte;.
8f400 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f mem5.currentCo
8f410 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 63 75 unt++;. mem5.cu
8f420 72 72 65 6e 74 4f 75 74 20 2b 3d 20 69 46 75 6c rrentOut += iFul
8f430 6c 53 7a 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e lSz;. if( mem5.
8f440 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75 maxCount<mem5.cu
8f450 72 72 65 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d rrentCount ) mem
8f460 35 2e 6d 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d 5.maxCount = mem
8f470 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0a 5.currentCount;.
8f480 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75 if( mem5.maxOu
8f490 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 t<mem5.currentOu
8f4a0 74 20 29 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 t ) mem5.maxOut
8f4b0 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 = mem5.currentOu
8f4c0 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 t;.. /* Return
8f4d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
8f4e0 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
8f4f0 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 y. */. return (
8f500 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f void*)&mem5.zPoo
8f510 6c 5b 69 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b l[i*mem5.nAtom];
8f520 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
8f530 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 n outstanding me
8f540 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e mory allocation.
8f550 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8f560 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 memsys5FreeUnsaf
8f570 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 e(void *pOld){.
8f580 20 75 33 32 20 73 69 7a 65 2c 20 69 4c 6f 67 73 u32 size, iLogs
8f590 69 7a 65 3b 0a 20 20 69 6e 74 20 69 42 6c 6f 63 ize;. int iBloc
8f5a0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0a k; .
8f5b0 0a 20 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b . /* Set iBlock
8f5c0 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 to the index of
8f5d0 20 74 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 the block point
8f5e0 65 64 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e ed to by pOld in
8f5f0 20 0a 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 . ** the array
8f600 20 6f 66 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 62 of mem5.nAtom b
8f610 79 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 yte blocks point
8f620 65 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 ed to by mem5.zP
8f630 6f 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f ool.. */. iBlo
8f640 63 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 ck = ((u8 *)pOld
8f650 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d -mem5.zPool)/mem
8f660 35 2e 6e 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 5.nAtom;.. /* C
8f670 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f heck that the po
8f680 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74 inter pOld point
8f690 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f s to a valid, no
8f6a0 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f n-free block. */
8f6b0 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 . assert( iBloc
8f6c0 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d k>=0 && iBlock<m
8f6d0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8f6e0 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29 70 assert( ((u8 *)p
8f6f0 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 Old-mem5.zPool)%
8f700 6d 65 6d 35 2e 6e 41 74 6f 6d 3d 3d 30 20 29 3b mem5.nAtom==0 );
8f710 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35 . assert( (mem5
8f720 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 .aCtrl[iBlock] &
8f730 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29 CTRL_FREE)==0 )
8f740 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20 ;.. iLogsize =
8f750 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 mem5.aCtrl[iBloc
8f760 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a k] & CTRL_LOGSIZ
8f770 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69 E;. size = 1<<i
8f780 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73 65 72 Logsize;. asser
8f790 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 t( iBlock+size-1
8f7a0 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a <mem5.nBlock );.
8f7b0 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 . mem5.aCtrl[iB
8f7c0 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 lock] |= CTRL_FR
8f7d0 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c EE;. mem5.aCtrl
8f7e0 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 [iBlock+size-1]
8f7f0 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 |= CTRL_FREE;.
8f800 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 assert( mem5.cur
8f810 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 rentCount>0 );.
8f820 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 assert( mem5.cu
8f830 72 72 65 6e 74 4f 75 74 3e 3d 30 20 29 3b 0a 20 rrentOut>=0 );.
8f840 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 mem5.currentCou
8f850 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 nt--;. mem5.cur
8f860 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a rentOut -= size*
8f870 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 61 73 mem5.nAtom;. as
8f880 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
8f890 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e ntOut>0 || mem5.
8f8a0 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 currentCount==0
8f8b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d );. assert( mem
8f8c0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 5.currentCount>0
8f8d0 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 || mem5.current
8f8e0 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d Out==0 );.. mem
8f8f0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
8f900 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c = CTRL_FREE | iL
8f910 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28 ogsize;. while(
8f920 20 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 iLogsize<LOGMAX
8f930 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 64 ){. int iBud
8f940 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 6c dy;. if( (iBl
8f950 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26 ock>>iLogsize) &
8f960 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64 1 ){. iBud
8f970 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69 dy = iBlock - si
8f980 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ze;. }else{.
8f990 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 iBuddy = iB
8f9a0 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20 lock + size;.
8f9b0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 }. assert( i
8f9c0 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20 Buddy>=0 );.
8f9d0 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c 3c if( (iBuddy+(1<<
8f9e0 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e iLogsize))>mem5.
8f9f0 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a nBlock ) break;.
8fa00 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 74 if( mem5.aCt
8fa10 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52 rl[iBuddy]!=(CTR
8fa20 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a L_FREE | iLogsiz
8fa30 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 e) ) break;.
8fa40 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42 memsys5Unlink(iB
8fa50 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b uddy, iLogsize);
8fa60 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b . iLogsize++;
8fa70 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c . if( iBuddy<
8fa80 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 iBlock ){.
8fa90 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 mem5.aCtrl[iBudd
8faa0 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c y] = CTRL_FREE |
8fab0 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 iLogsize;.
8fac0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8fad0 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 ck] = 0;. i
8fae0 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a Block = iBuddy;.
8faf0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
8fb00 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8fb10 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 ck] = CTRL_FREE
8fb20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 | iLogsize;.
8fb30 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 mem5.aCtrl[iBu
8fb40 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ddy] = 0;. }.
8fb50 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 size *= 2;.
8fb60 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b }. memsys5Link
8fb70 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a (iBlock, iLogsiz
8fb80 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c e);.}../*.** All
8fb90 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 ocate nBytes of
8fba0 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63 memory.*/.static
8fbb0 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 void *memsys5Ma
8fbc0 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29 lloc(int nBytes)
8fbd0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 {. sqlite3_int6
8fbe0 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 4 *p = 0;. if(
8fbf0 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 20 nBytes>0 ){.
8fc00 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a memsys5Enter();.
8fc10 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d p = memsys5M
8fc20 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 allocUnsafe(nByt
8fc30 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 es);. memsys5
8fc40 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72 Leave();. }. r
8fc50 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 eturn (void*)p;
8fc60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d .}../*.** Free m
8fc70 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 emory..*/.static
8fc80 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 65 void memsys5Fre
8fc90 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b e(void *pPrior){
8fca0 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 . if( pPrior==0
8fcb0 20 29 7b 0a 61 73 73 65 72 74 28 30 29 3b 0a 20 ){.assert(0);.
8fcc0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
8fcd0 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b memsys5Enter();
8fce0 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e . memsys5FreeUn
8fcf0 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 safe(pPrior);.
8fd00 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 20 memsys5Leave();
8fd10 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 .}../*.** Chang
8fd20 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e e the size of an
8fd30 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 existing memory
8fd40 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 allocation.*/.s
8fd50 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 tatic void *mems
8fd60 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 ys5Realloc(void
8fd70 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 *pPrior, int nBy
8fd80 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 tes){. int nOld
8fd90 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 ;. void *p;. i
8fda0 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a f( pPrior==0 ){.
8fdb0 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 return memsy
8fdc0 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 s5Malloc(nBytes)
8fdd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 ;. }. if( nByt
8fde0 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d es<=0 ){. mem
8fdf0 73 79 73 35 46 72 65 65 28 70 50 72 69 6f 72 29 sys5Free(pPrior)
8fe00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
8fe10 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d }. nOld = mem
8fe20 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f 72 29 sys5Size(pPrior)
8fe30 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d ;. if( nBytes<=
8fe40 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 74 75 nOld ){. retu
8fe50 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 rn pPrior;. }.
8fe60 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b memsys5Enter();
8fe70 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 . p = memsys5Ma
8fe80 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 llocUnsafe(nByte
8fe90 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 s);. if( p ){.
8fea0 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 memcpy(p, pPr
8feb0 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 ior, nOld);.
8fec0 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 memsys5FreeUnsaf
8fed0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 e(pPrior);. }.
8fee0 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b memsys5Leave();
8fef0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
8ff00 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 /*.** Round up a
8ff10 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f request size to
8ff20 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 the next valid
8ff30 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e allocation size.
8ff40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
8ff50 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 6e emsys5Roundup(in
8ff60 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c t n){. int iFul
8ff70 6c 53 7a 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c lSz;. for(iFull
8ff80 53 7a 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 20 69 Sz=mem5.nAtom; i
8ff90 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 FullSz<n; iFullS
8ffa0 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74 75 72 z *= 2);. retur
8ffb0 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a 73 74 n iFullSz;.}..st
8ffc0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 atic int memsys5
8ffd0 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 7b Log(int iValue){
8ffe0 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 . int iLog;. f
8fff0 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 or(iLog=0; (1<<i
90000 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f Log)<iValue; iLo
90010 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 g++);. return i
90020 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e Log;.}../*.** In
90030 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f itialize this mo
90040 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dule..*/.static
90050 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 int memsys5Init(
90060 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
90070 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 int ii;. int
90080 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 47 nByte = sqlite3G
90090 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 lobalConfig.nHea
900a0 70 3b 0a 20 20 75 38 20 2a 7a 42 79 74 65 20 3d p;. u8 *zByte =
900b0 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 47 6c (u8 *)sqlite3Gl
900c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 obalConfig.pHeap
900d0 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b ;. int nMinLog;
900e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
900f0 20 2f 2a 20 4c 6f 67 20 6f 66 20 6d 69 6e 69 6d /* Log of minim
90100 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 um allocation si
90110 7a 65 20 69 6e 20 62 79 74 65 73 2a 2f 0a 20 20 ze in bytes*/.
90120 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 0a 20 20 int iOffset;..
90130 69 66 28 20 21 7a 42 79 74 65 20 29 7b 0a 20 20 if( !zByte ){.
90140 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
90150 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 6e 4d ERROR;. }.. nM
90160 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c inLog = memsys5L
90170 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c og(sqlite3Global
90180 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 Config.mnReq);.
90190 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3d 20 28 31 mem5.nAtom = (1
901a0 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68 <<nMinLog);. wh
901b0 69 6c 65 28 20 73 69 7a 65 6f 66 28 4d 65 6d 35 ile( sizeof(Mem5
901c0 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 6e 41 74 6f 6d Link)>mem5.nAtom
901d0 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6e 41 74 ){. mem5.nAt
901e0 6f 6d 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 om = mem5.nAtom
901f0 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d << 1;. }.. mem
90200 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 5.nBlock = (nByt
90210 65 20 2f 20 28 6d 65 6d 35 2e 6e 41 74 6f 6d 2b e / (mem5.nAtom+
90220 73 69 7a 65 6f 66 28 75 38 29 29 29 3b 0a 20 20 sizeof(u8)));.
90230 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79 mem5.zPool = zBy
90240 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c te;. mem5.aCtrl
90250 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a = (u8 *)&mem5.z
90260 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b Pool[mem5.nBlock
90270 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 0a 20 *mem5.nAtom];..
90280 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c for(ii=0; ii<=L
90290 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 OGMAX; ii++){.
902a0 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 mem5.aiFreelis
902b0 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a t[ii] = -1;. }.
902c0 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a . iOffset = 0;.
902d0 20 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b for(ii=LOGMAX;
902e0 20 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 ii>=0; ii--){.
902f0 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 int nAlloc =
90300 28 31 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28 (1<<ii);. if(
90310 20 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 (iOffset+nAlloc
90320 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 )<=mem5.nBlock )
90330 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 {. mem5.aCt
90340 72 6c 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69 rl[iOffset] = ii
90350 20 7c 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 | CTRL_FREE;.
90360 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 memsys5Link(
90370 69 4f 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20 iOffset, ii);.
90380 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e iOffset += n
90390 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 Alloc;. }.
903a0 20 61 73 73 65 72 74 28 28 69 4f 66 66 73 65 74 assert((iOffset
903b0 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 +nAlloc)>mem5.nB
903c0 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 lock);. }.. re
903d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
903e0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 }../*.** Deiniti
903f0 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c alize this modul
90400 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
90410 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f 77 d memsys5Shutdow
90420 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 n(void *NotUsed)
90430 7b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f {. return;.}../
90440 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 *.** Open the fi
90450 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64 le indicated and
90460 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 write a log of
90470 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f all unfreed memo
90480 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f ry .** allocatio
90490 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 ns into that log
904a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
904b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
904c0 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 6f 6e 73 Memsys5Dump(cons
904d0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
904e0 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 e){.#ifdef SQLIT
904f0 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a E_DEBUG. FILE *
90500 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c out;. int i, j,
90510 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f n;. int nMinLo
90520 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e g;.. if( zFilen
90530 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e ame==0 || zFilen
90540 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 ame[0]==0 ){.
90550 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 out = stdout;.
90560 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 }else{. out
90570 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d = fopen(zFilenam
90580 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 e, "w");. if(
90590 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 out==0 ){.
905a0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
905b0 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f "** Unable to o
905c0 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 utput memory deb
905d0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 ug output log: %
905e0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 s **\n",.
905f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
90600 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Filename);.
90610 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
90620 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 }. memsys5Ente
90630 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d r();. nMinLog =
90640 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 memsys5Log(mem5
90650 2e 6e 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 .nAtom);. for(i
90660 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 =0; i<=LOGMAX &&
90670 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 i+nMinLog<32; i
90680 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30 ++){. for(n=0
90690 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c , j=mem5.aiFreel
906a0 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 ist[i]; j>=0; j
906b0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e = MEM5LINK(j)->n
906c0 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 ext, n++){}.
906d0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72 fprintf(out, "fr
906e0 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20 eelist items of
906f0 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20 size %d: %d\n",
90700 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c 3c 20 69 2c mem5.nAtom << i,
90710 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 69 6e n);. }. fprin
90720 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 tf(out, "mem5.nA
90730 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 6c 6c lloc = %ll
90740 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f u\n", mem5.nAllo
90750 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 c);. fprintf(ou
90760 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c t, "mem5.totalAl
90770 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c loc = %llu\n",
90780 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 mem5.totalAlloc
90790 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
907a0 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 , "mem5.totalExc
907b0 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 ess = %llu\n",
907c0 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 mem5.totalExcess
907d0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
907e0 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f , "mem5.currentO
907f0 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 ut = %u\n", me
90800 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b 0a m5.currentOut);.
90810 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
90820 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e mem5.currentCoun
90830 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e t = %u\n", mem5.
90840 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0a 20 currentCount);.
90850 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
90860 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 20 20 em5.maxOut
90870 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d = %u\n", mem5.m
90880 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 axOut);. fprint
90890 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 f(out, "mem5.max
908a0 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e Count = %u\n
908b0 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 ", mem5.maxCount
908c0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
908d0 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 , "mem5.maxReque
908e0 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 st = %u\n", me
908f0 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 3b 0a m5.maxRequest);.
90900 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 memsys5Leave()
90910 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 ;. if( out==std
90920 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 out ){. fflus
90930 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c h(stdout);. }el
90940 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f se{. fclose(o
90950 75 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ut);. }.#endif.
90960 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
90970 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c utine is the onl
90980 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 y routine in thi
90990 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 s file with exte
909a0 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 rnal .** linkage
909b0 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 70 . It returns a p
909c0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 ointer to a stat
909d0 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d ic sqlite3_mem_m
909e0 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 63 74 ethods.** struct
909f0 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 populated with
90a00 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74 68 the memsys5 meth
90a10 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ods..*/.SQLITE_P
90a20 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c RIVATE const sql
90a30 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
90a40 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d *sqlite3MemGetM
90a50 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a 20 20 emsys5(void){.
90a60 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
90a70 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
90a80 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 20 memsys5Methods
90a90 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 = {. memsys5
90aa0 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d Malloc,. mem
90ab0 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 20 6d sys5Free,. m
90ac0 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c 0a 20 emsys5Realloc,.
90ad0 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a 65 2c memsys5Size,
90ae0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 6f 75 . memsys5Rou
90af0 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 ndup,. memsy
90b00 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d s5Init,. mem
90b10 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a 20 20 sys5Shutdown,.
90b20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 0. };. retu
90b30 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 68 6f rn &memsys5Metho
90b40 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a ds;.}..#endif /*
90b50 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
90b60 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EMSYS5 */../****
90b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
90b80 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a f mem5.c *******
90b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
90bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
90bd0 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 2a 2a file mutex.c **
90be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
90c10 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2007 August 14.
90c20 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
90c30 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
90c40 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
90c50 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
90c60 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
90c70 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
90c80 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
90c90 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
90ca0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
90cb0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
90cc0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
90cd0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
90ce0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
90cf0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
90d00 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
90d10 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
90d20 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
90d30 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
90d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
90d80 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
90d90 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
90da0 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
90db0 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a ent mutexes..**.
90dc0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
90dd0 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 tains code that
90de0 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73 73 is common across
90df0 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 all mutex imple
90e00 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a 2a 0a mentations...**.
90e10 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 63 2c ** $Id: mutex.c,
90e20 76 20 31 2e 32 39 20 32 30 30 38 2f 31 30 2f 30 v 1.29 2008/10/0
90e30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45 7 15:25:48 drh E
90e40 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 xp $.*/..#ifndef
90e50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d SQLITE_MUTEX_OM
90e60 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c IT./*.** Initial
90e70 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 79 ize the mutex sy
90e80 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f stem..*/.SQLITE_
90e90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
90ea0 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 te3MutexInit(voi
90eb0 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 d){ . int rc =
90ec0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 SQLITE_OK;. if(
90ed0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
90ee0 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 nfig.bCoreMutex
90ef0 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 ){. if( !sqli
90f00 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
90f10 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f mutex.xMutexAllo
90f20 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 c ){. /* If
90f30 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f 63 the xMutexAlloc
90f40 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f 74 20 method has not
90f50 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 74 been set, then t
90f60 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f 74 0a he user did not.
90f70 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c 6c ** install
90f80 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 a mutex impleme
90f90 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71 6c 69 ntation via sqli
90fa0 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 72 69 te3_config() pri
90fb0 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 or to . **
90fc0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
90fd0 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 ze() being calle
90fe0 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f d. This block co
90ff0 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20 74 6f pies pointers to
91000 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 . ** the de
91010 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 fault implementa
91020 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73 71 tion into the sq
91030 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
91040 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 g structure..
91050 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 **. ** T
91060 68 65 20 64 61 6e 67 65 72 20 69 73 20 74 68 61 he danger is tha
91070 74 20 61 6c 74 68 6f 75 67 68 20 73 71 6c 69 74 t although sqlit
91080 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 6e e3_config() is n
91090 6f 74 20 61 20 74 68 72 65 61 64 73 61 66 65 0a ot a threadsafe.
910a0 20 20 20 20 20 20 2a 2a 20 41 50 49 2c 20 73 71 ** API, sq
910b0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
910c0 28 29 20 69 73 2c 20 61 6e 64 20 73 6f 20 6d 75 () is, and so mu
910d0 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 6d ltiple threads m
910e0 61 79 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 61 ay be. ** a
910f0 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 72 75 6e ttempting to run
91100 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 this function s
91110 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 20 54 imultaneously. T
91120 6f 20 67 75 61 72 64 20 77 72 69 74 65 0a 20 20 o guard write.
91130 20 20 20 20 2a 2a 20 61 63 63 65 73 73 20 74 6f ** access to
91140 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 the sqlite3Glob
91150 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75 alConfig structu
91160 72 65 2c 20 74 68 65 20 27 4d 41 53 54 45 52 27 re, the 'MASTER'
91170 20 73 74 61 74 69 63 20 6d 75 74 65 78 0a 20 20 static mutex.
91180 20 20 20 20 2a 2a 20 69 73 20 6f 62 74 61 69 6e ** is obtain
91190 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 ed before modify
911a0 69 6e 67 20 69 74 2e 0a 20 20 20 20 20 20 2a 2f ing it.. */
911b0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
911c0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 20 utex_methods *p
911d0 3d 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 = sqlite3Default
911e0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 73 Mutex();. s
911f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d qlite3_mutex *pM
91200 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 0a 20 20 aster = 0;. .
91210 20 20 20 20 72 63 20 3d 20 70 2d 3e 78 4d 75 74 rc = p->xMut
91220 65 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 20 20 exInit();.
91230 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
91240 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 61 K ){. pMa
91250 73 74 65 72 20 3d 20 70 2d 3e 78 4d 75 74 65 78 ster = p->xMutex
91260 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
91270 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
91280 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
91290 74 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 t(pMaster);.
912a0 20 20 20 20 70 2d 3e 78 4d 75 74 65 78 45 6e 74 p->xMutexEnt
912b0 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 er(pMaster);.
912c0 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
912d0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
912e0 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c .mutex.xMutexAll
912f0 6f 63 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20 oc==0 .
91300 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c || sqlite3Gl
91310 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
91320 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 3d 3d 70 2d .xMutexAlloc==p-
91330 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 0a 20 20 20 >xMutexAlloc.
91340 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 );.
91350 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 if( !sqlite3Glob
91360 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91370 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 MutexAlloc ){.
91380 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 sqlite3G
91390 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
913a0 78 20 3d 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 x = *p;.
913b0 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 78 4d 75 }. p->xMu
913c0 74 65 78 4c 65 61 76 65 28 70 4d 61 73 74 65 72 texLeave(pMaster
913d0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
913e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
913f0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
91400 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
91410 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20 xInit();. }.
91420 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
91430 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f .}../*.** Shutdo
91440 77 6e 20 74 68 65 20 6d 75 74 65 78 20 73 79 73 wn the mutex sys
91450 74 65 6d 2e 20 54 68 69 73 20 63 61 6c 6c 20 66 tem. This call f
91460 72 65 65 73 20 72 65 73 6f 75 72 63 65 73 20 61 rees resources a
91470 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 73 llocated by.** s
91480 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 qlite3MutexInit(
91490 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
914a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
914b0 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 0a MutexEnd(void){.
914c0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
914d0 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d 20 73 71 6c E_OK;. rc = sql
914e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
914f0 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 64 .mutex.xMutexEnd
91500 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ();. return rc;
91510 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 .}../*.** Retrie
91520 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ve a pointer to
91530 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 6f a static mutex o
91540 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 r allocate a new
91550 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a 2a 2f dynamic one..*/
91560 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
91570 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 te3_mutex *sqlit
91580 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69 e3_mutex_alloc(i
91590 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65 66 20 nt id){.#ifndef
915a0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
915b0 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 INIT. if( sqlit
915c0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
915d0 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 ) return 0;.#end
915e0 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 if. return sqli
915f0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
91600 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f mutex.xMutexAllo
91610 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 c(id);.}..SQLITE
91620 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
91630 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d _mutex *sqlite3M
91640 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 utexAlloc(int id
91650 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 ){. if( !sqlite
91660 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 3GlobalConfig.bC
91670 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 oreMutex ){.
91680 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
91690 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
916a0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
916b0 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 .xMutexAlloc(id)
916c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
916d0 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e a dynamic mutex.
916e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
916f0 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 oid sqlite3_mute
91700 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d x_free(sqlite3_m
91710 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 utex *p){. if(
91720 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 p ){. sqlite3
91730 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
91740 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29 ex.xMutexFree(p)
91750 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ;. }.}../*.** O
91760 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 btain the mutex
91770 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 p. If some other
91780 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20 thread already
91790 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62 has the mutex, b
917a0 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 lock.** until it
917b0 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 can be obtained
917c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
917d0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
917e0 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
917f0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 _mutex *p){. if
91800 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( p ){. sqlit
91810 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
91820 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72 utex.xMutexEnter
91830 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
91840 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 * Obtain the mut
91850 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73 ex p. If success
91860 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 ful, return SQLI
91870 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 TE_OK. Otherwise
91880 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 , if another.**
91890 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 thread holds the
918a0 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61 mutex and it ca
918b0 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 nnot be obtained
918c0 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
918d0 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f BUSY..*/.SQLITE_
918e0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
918f0 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 mutex_try(sqlite
91900 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
91910 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
91920 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 K;. if( p ){.
91930 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
91940 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
91950 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b ex.xMutexTry(p);
91960 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
91970 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
91980 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
91990 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 ve() routine exi
919a0 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 ts a mutex that
919b0 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a was previously.*
919c0 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 * entered by the
919d0 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 same thread. T
919e0 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
919f0 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
91a00 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 mutex .** is not
91a10 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
91a20 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f ed. If a NULL po
91a30 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20 inter is passed
91a40 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a as an argument.*
91a50 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
91a60 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 is a no-op..*/.S
91a70 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
91a80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
91a90 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ve(sqlite3_mutex
91aa0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
91ab0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
91ac0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91ad0 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20 MutexLeave(p);.
91ae0 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 }.}..#ifndef ND
91af0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
91b00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
91b10 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
91b20 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
91b30 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
91b40 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
91b50 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
91b60 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 statements..*/.S
91b70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
91b80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
91b90 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
91ba0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d p){. return p==
91bb0 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 0 || sqlite3Glob
91bc0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91bd0 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a MutexHeld(p);.}.
91be0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
91bf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
91c00 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
91c10 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ex *p){. return
91c20 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 p==0 || sqlite3
91c30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
91c40 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 ex.xMutexNotheld
91c50 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 (p);.}.#endif..#
91c60 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
91c70 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a 0a 2f OMIT_MUTEX */../
91c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
91c90 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a nd of mutex.c **
91ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
91cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
91ce0 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f egin file mutex_
91cf0 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a noop.c *********
91d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
91d20 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 *.** 2008 Octobe
91d30 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 07.**.** The a
91d40 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
91d50 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
91d60 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
91d70 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
91d80 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
91d90 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
91da0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
91db0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
91dc0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
91dd0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
91de0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
91df0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
91e00 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
91e10 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
91e20 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
91e30 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
91e40 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
91e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91e90 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
91ea0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
91eb0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
91ec0 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 mplement mutexes
91ed0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 ..**.** This imp
91ee0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 lementation in t
91ef0 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f his file does no
91f00 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75 t provide any mu
91f10 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f tual.** exclusio
91f20 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75 n and is thus su
91f30 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f itable for use o
91f40 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69 nly in applicati
91f50 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20 ons.** that use
91f60 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67 SQLite in a sing
91f70 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 le thread. The
91f80 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 routines defined
91f90 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61 .** here are pla
91fa0 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70 ce-holders. App
91fb0 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75 lications can su
91fc0 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67 bstitute working
91fd0 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e .** mutex routin
91fe0 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 es at start-time
91ff0 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a using the.**.**
92000 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e sqlite3_con
92010 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 fig(SQLITE_CONFI
92020 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a G_MUTEX,...).**.
92030 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a ** interface..**
92040 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 .** If compiled
92050 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 with SQLITE_DEBU
92060 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e G, then addition
92070 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65 al logic is inse
92080 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65 rted.** that doe
92090 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 s error checking
920a0 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d on mutexes to m
920b0 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72 ake sure they ar
920c0 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 e being.** calle
920d0 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a d correctly..**.
920e0 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e 6f ** $Id: mutex_no
920f0 6f 70 2e 63 2c 76 20 31 2e 32 20 32 30 30 38 2f op.c,v 1.2 2008/
92100 31 30 2f 31 35 20 31 39 3a 30 33 3a 30 33 20 64 10/15 19:03:03 d
92110 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 rh Exp $.*/...#i
92120 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
92130 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 _MUTEX_NOOP) &&
92140 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
92150 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 DEBUG)./*.** Stu
92160 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 b routines for a
92170 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 ll mutex methods
92180 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
92190 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f tines provide no
921a0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f mutual exclusio
921b0 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b n or error check
921c0 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
921d0 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 nt noopMutexHeld
921e0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
921f0 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a p){ return 1; }.
92200 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d static int noopM
92210 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
92220 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 te3_mutex *p){ r
92230 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 eturn 1; }.stati
92240 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 c int noopMutexI
92250 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 nit(void){ retur
92260 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 n SQLITE_OK; }.s
92270 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
92280 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 texEnd(void){ re
92290 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
922a0 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 }.static sqlite3
922b0 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 _mutex *noopMute
922c0 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 xAlloc(int id){
922d0 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f return (sqlite3_
922e0 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74 mutex*)8; }.stat
922f0 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 ic void noopMute
92300 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 xFree(sqlite3_mu
92310 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b tex *p){ return;
92320 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e }.static void n
92330 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 oopMutexEnter(sq
92340 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
92350 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 return; }.stati
92360 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54 c int noopMutexT
92370 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ry(sqlite3_mutex
92380 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c *p){ return SQL
92390 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
923a0 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c void noopMutexL
923b0 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
923c0 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 ex *p){ return;
923d0 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
923e0 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f E sqlite3_mutex_
923f0 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
92400 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 DefaultMutex(voi
92410 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c d){. static sql
92420 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
92430 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 ds sMutex = {.
92440 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c noopMutexInit,
92450 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e . noopMutexEn
92460 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 d,. noopMutex
92470 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d Alloc,. noopM
92480 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f utexFree,. no
92490 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 opMutexEnter,.
924a0 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a noopMutexTry,.
924b0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 noopMutexLea
924c0 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 ve,.. noopMut
924d0 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 exHeld,. noop
924e0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d MutexNotheld. }
924f0 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 ;.. return &sMu
92500 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a tex;.}.#endif /*
92510 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
92520 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 MUTEX_NOOP) && !
92530 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
92540 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65 EBUG) */..#if de
92550 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 fined(SQLITE_MUT
92560 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 EX_NOOP) && defi
92570 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
92580 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 )./*.** In this
92590 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
925a0 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 error checking i
925b0 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74 s provided for t
925c0 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 esting.** and de
925d0 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 bugging purposes
925e0 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73 . The mutexes s
925f0 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76 till do not prov
92600 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 ide any.** mutua
92610 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a l exclusion..*/.
92620 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 ./*.** The mutex
92630 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63 object.*/.struc
92640 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 t sqlite3_mutex
92650 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 {. int id;
92660 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70 /* The mutex typ
92670 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 e */. int cnt;
92680 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
92690 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 entries without
926a0 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 a matching leave
926b0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
926c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
926d0 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
926e0 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
926f0 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a () routine are.*
92700 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 * intended for u
92710 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
92720 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
92730 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 /.static int deb
92740 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 ugMutexHeld(sqli
92750 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
92760 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
92770 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 p->cnt>0;.}.stat
92780 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
92790 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
927a0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
927b0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e turn p==0 || p->
927c0 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cnt==0;.}../*.**
927d0 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
927e0 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 deinitialize the
927f0 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d mutex subsystem
92800 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
92810 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76 debugMutexInit(v
92820 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c oid){ return SQL
92830 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
92840 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45 int debugMutexE
92850 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e nd(void){ return
92860 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f SQLITE_OK; }../
92870 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
92880 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
92890 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
928a0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
928b0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
928c0 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
928d0 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
928e0 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
928f0 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
92900 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
92910 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ed. .*/.static s
92920 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65 qlite3_mutex *de
92930 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e bugMutexAlloc(in
92940 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 t id){. static
92950 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53 sqlite3_mutex aS
92960 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 tatic[6];. sqli
92970 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 te3_mutex *pNew
92980 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 = 0;. switch( i
92990 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 d ){. case SQ
929a0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a LITE_MUTEX_FAST:
929b0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
929c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
929d0 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d : {. pNew =
929e0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 sqlite3Malloc(s
929f0 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 izeof(*pNew));.
92a00 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b if( pNew ){
92a10 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 . pNew->i
92a20 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 d = id;.
92a30 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 pNew->cnt = 0;.
92a40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
92a50 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
92a60 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 fault: {. a
92a70 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 ssert( id-2 >= 0
92a80 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
92a90 28 20 69 64 2d 32 20 3c 20 73 69 7a 65 6f 66 28 ( id-2 < sizeof(
92aa0 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f 66 28 aStatic)/sizeof(
92ab0 61 53 74 61 74 69 63 5b 30 5d 29 20 29 3b 0a 20 aStatic[0]) );.
92ac0 20 20 20 20 20 70 4e 65 77 20 3d 20 26 61 53 74 pNew = &aSt
92ad0 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 20 20 atic[id-2];.
92ae0 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b pNew->id = id;
92af0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
92b00 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
92b10 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pNew;.}../*.**
92b20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 This routine dea
92b30 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 llocates a previ
92b40 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ously allocated
92b50 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 mutex..*/.static
92b60 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 void debugMutex
92b70 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 Free(sqlite3_mut
92b80 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
92b90 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 ( p->cnt==0 );.
92ba0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d assert( p->id==
92bb0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
92bc0 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 T || p->id==SQLI
92bd0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
92be0 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f VE );. sqlite3_
92bf0 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a free(p);.}../*.*
92c00 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
92c10 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 tex_enter() and
92c20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
92c30 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 y() routines att
92c40 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 empt.** to enter
92c50 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e a mutex. If an
92c60 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 other thread is
92c70 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 already within t
92c80 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c he mutex,.** sql
92c90 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
92ca0 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e () will block an
92cb0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
92cc0 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 try() will retur
92cd0 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 n.** SQLITE_BUSY
92ce0 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d . The sqlite3_m
92cf0 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 utex_try() inter
92d00 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c face returns SQL
92d10 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 ITE_OK.** upon s
92d20 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e uccessful entry.
92d30 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 Mutexes create
92d40 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d d using SQLITE_M
92d50 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 UTEX_RECURSIVE c
92d60 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 an.** be entered
92d70 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 multiple times
92d80 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
92d90 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 ad. In such cas
92da0 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 es the,.** mutex
92db0 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 must be exited
92dc0 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 an equal number
92dd0 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 of times before
92de0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a another thread.*
92df0 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 * can enter. If
92e00 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
92e10 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 tries to enter
92e20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f any other kind o
92e30 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 f mutex.** more
92e40 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 than once, the b
92e50 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
92e60 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ined..*/.static
92e70 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 45 void debugMutexE
92e80 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 nter(sqlite3_mut
92e90 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
92ea0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f ( p->id==SQLITE_
92eb0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
92ec0 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 || debugMutexNot
92ed0 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e held(p) );. p->
92ee0 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 cnt++;.}.static
92ef0 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 54 72 int debugMutexTr
92f00 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 y(sqlite3_mutex
92f10 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
92f20 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
92f30 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 EX_RECURSIVE ||
92f40 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c debugMutexNothel
92f50 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 d(p) );. p->cnt
92f60 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ++;. return SQL
92f70 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
92f80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
92f90 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
92fa0 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
92fb0 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
92fc0 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
92fd0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
92fe0 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f ad. The behavio
92ff0 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 r.** is undefine
93000 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 d if the mutex i
93010 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
93020 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 entered or.** is
93030 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 not currently a
93040 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
93050 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 e will never do
93060 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 either..*/.stati
93070 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 c void debugMute
93080 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d xLeave(sqlite3_m
93090 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 utex *p){. asse
930a0 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 48 65 rt( debugMutexHe
930b0 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e ld(p) );. p->cn
930c0 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t--;. assert( p
930d0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
930e0 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 EX_RECURSIVE ||
930f0 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c debugMutexNothel
93100 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c 49 54 d(p) );.}..SQLIT
93110 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
93120 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
93130 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d *sqlite3DefaultM
93140 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 utex(void){. st
93150 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 atic sqlite3_mut
93160 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 ex_methods sMute
93170 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 75 67 4d x = {. debugM
93180 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 64 65 utexInit,. de
93190 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 bugMutexEnd,.
931a0 20 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 debugMutexAlloc
931b0 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 ,. debugMutex
931c0 46 72 65 65 2c 0a 20 20 20 20 64 65 62 75 67 4d Free,. debugM
931d0 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 64 utexEnter,. d
931e0 65 62 75 67 4d 75 74 65 78 54 72 79 2c 0a 20 20 ebugMutexTry,.
931f0 20 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76 debugMutexLeav
93200 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 4d 75 74 e,.. debugMut
93210 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 65 62 75 exHeld,. debu
93220 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 gMutexNotheld.
93230 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d };.. return &sM
93240 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f utex;.}.#endif /
93250 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 * defined(SQLITE
93260 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 _MUTEX_NOOP) &&
93270 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
93280 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a EBUG) */../*****
93290 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
932a0 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a 2a mutex_noop.c **
932b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
932c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
932d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
932e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
932f0 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e 63 file mutex_os2.c
93300 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
93310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93320 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
93330 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2007 August 28.*
93340 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
93350 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
93360 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
93370 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
93380 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
93390 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
933a0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
933b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
933c0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
933d0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
933e0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
933f0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
93400 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
93410 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
93420 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
93430 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
93440 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
93450 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
93460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
934a0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
934b0 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
934c0 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
934d0 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 4f nt mutexes for O
934e0 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d S/2.**.** $Id: m
934f0 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e 31 utex_os2.c,v 1.1
93500 30 20 32 30 30 38 2f 30 36 2f 32 33 20 32 32 3a 0 2008/06/23 22:
93510 31 33 3a 32 38 20 70 77 65 69 6c 62 61 63 68 65 13:28 pweilbache
93520 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a r Exp $.*/../*.*
93530 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 * The code in th
93540 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 is file is only
93550 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f 4d used if SQLITE_M
93560 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66 69 UTEX_OS2 is defi
93570 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 ned..** See the
93580 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f 72 mutex.h file for
93590 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 details..*/.#if
935a0 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 def SQLITE_MUTEX
935b0 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a _OS2../*********
935c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f 53 ************* OS
935d0 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d 65 /2 Mutex Impleme
935e0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a ntation ********
935f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
93600 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d *.** This implem
93610 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74 65 entation of mute
93620 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 73 69 xes is built usi
93630 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49 2e ng the OS/2 API.
93640 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d .*/../*.** The m
93650 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 45 utex object.** E
93660 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 ach recursive mu
93670 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e tex is an instan
93680 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
93690 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
936a0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
936b0 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 58 20 _mutex {. HMTX
936c0 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 mutex; /*
936d0 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e Mutex controllin
936e0 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 g the lock */.
936f0 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 20 20 int id;
93700 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 /* Mutex type
93710 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b 20 */. int nRef;
93720 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
93730 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 2a of references *
93740 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 3b 20 /. TID owner;
93750 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 /* Thread
93760 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 holding this mut
93770 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e ex */.};..#defin
93780 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 e OS2_MUTEX_INIT
93790 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30 2c IALIZER 0,0,0,
937a0 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 0../*.** Initial
937b0 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 ize and deinitia
937c0 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 lize the mutex s
937d0 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 ubsystem..*/.sta
937e0 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 tic int os2Mutex
937f0 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 Init(void){ retu
93800 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
93810 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 static int os2Mu
93820 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 texEnd(void){ re
93830 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
93840 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
93850 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
93860 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 () routine alloc
93870 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 ates a new.** mu
93880 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 tex and returns
93890 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
938a0 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 If it returns
938b0 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 NULL.** that mea
938c0 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 ns that a mutex
938d0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c could not be all
938e0 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c 69 ocated. .** SQLi
938f0 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 te will unwind i
93900 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 ts stack and ret
93910 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 urn an error. T
93920 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 he argument.** t
93930 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f o sqlite3_mutex_
93940 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f alloc() is one o
93950 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 f these integer
93960 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a constants:.**.**
93970 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 <ul>.** <li> S
93980 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
93990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
939a0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
939b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
939c0 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 3c 1.** <
939d0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
939e0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 X_STATIC_MASTER
939f0 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 20 2.** <li>
93a00 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
93a10 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 20 TIC_MEM
93a20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 3.** <li> SQLIT
93a30 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 E_MUTEX_STATIC_P
93a40 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a 20 RNG 4.**
93a50 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 </ul>.**.** The
93a60 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 first two consta
93a70 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 nts cause sqlite
93a80 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
93a90 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e to create.** a n
93aa0 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e ew mutex. The n
93ab0 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 ew mutex is recu
93ac0 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 rsive when SQLIT
93ad0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
93ae0 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 E.** is used but
93af0 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 not necessarily
93b00 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f so when SQLITE_
93b10 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 MUTEX_FAST is us
93b20 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 ed..** The mutex
93b30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
93b40 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
93b50 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 make a distinct
93b60 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 ion.** between S
93b70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
93b80 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 RSIVE and SQLITE
93b90 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 _MUTEX_FAST if i
93ba0 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 t does.** not wa
93bb0 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 nt to. But SQLi
93bc0 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 te will only req
93bd0 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65 uest a recursive
93be0 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 mutex in.** cas
93bf0 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c es where it real
93c00 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 ly needs one. I
93c10 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 f a faster non-r
93c20 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a ecursive mutex.*
93c30 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
93c40 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e is available on
93c50 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f the host platfo
93c60 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 rm, the mutex su
93c70 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 bsystem.** might
93c80 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d return such a m
93c90 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 utex in response
93ca0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 to SQLITE_MUTEX
93cb0 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 _FAST..**.** The
93cc0 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 other allowed p
93cd0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c arameters to sql
93ce0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
93cf0 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a () each return.*
93d00 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * a pointer to a
93d10 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74 static preexist
93d20 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65 ing mutex. Thre
93d30 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 e static mutexes
93d40 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 are.** used by
93d50 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 the current vers
93d60 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ion of SQLite.
93d70 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
93d80 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 of SQLite.** may
93d90 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 add additional
93da0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 static mutexes.
93db0 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 Static mutexes
93dc0 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c are for internal
93dd0 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 .** use by SQLit
93de0 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 e only. Applica
93df0 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 tions that use S
93e00 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 QLite mutexes sh
93e10 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 ould.** use only
93e20 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 the dynamic mut
93e30 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 exes returned by
93e40 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
93e50 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f ST or.** SQLITE_
93e60 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e MUTEX_RECURSIVE.
93e70 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
93e80 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 if one of the d
93e90 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 ynamic mutex par
93ea0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f ameters (SQLITE_
93eb0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 MUTEX_FAST.** or
93ec0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
93ed0 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 CURSIVE) is used
93ee0 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 then sqlite3_mu
93ef0 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 tex_alloc().** r
93f00 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 eturns a differe
93f10 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 nt mutex on ever
93f20 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 y call. But for
93f30 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d the static.** m
93f40 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 utex types, the
93f50 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 same mutex is re
93f60 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 turned on every
93f70 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a call that has.**
93f80 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e the same type n
93f90 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 umber..*/.static
93fa0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
93fb0 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e os2MutexAlloc(in
93fc0 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c 69 t iType){. sqli
93fd0 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20 4e te3_mutex *p = N
93fe0 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 20 69 ULL;. switch( i
93ff0 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 Type ){. case
94000 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
94010 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c ST:. case SQL
94020 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
94030 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d IVE: {. p =
94040 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 sqlite3MallocZe
94050 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 ro( sizeof(*p) )
94060 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b ;. if( p ){
94070 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d . p->id =
94080 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 iType;.
94090 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d 75 74 if( DosCreateMut
940a0 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d 75 exSem( 0, &p->mu
940b0 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 20 tex, 0, FALSE )
940c0 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 != NO_ERROR ){.
940d0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
940e0 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 20 20 _free( p );.
940f0 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b 0a p = NULL;.
94100 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
94110 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
94120 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 }. default
94130 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 : {. static
94140 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 volatile int is
94150 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 Init = 0;.
94160 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
94170 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 78 utex staticMutex
94180 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 es[] = {.
94190 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 { OS2_MUTEX_INI
941a0 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 TIALIZER, },.
941b0 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 { OS2_MUTEX
941c0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c _INITIALIZER, },
941d0 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d . { OS2_M
941e0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
941f0 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f , },. { O
94200 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c S2_MUTEX_INITIAL
94210 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 IZER, },.
94220 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 { OS2_MUTEX_INI
94230 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 TIALIZER, },.
94240 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 { OS2_MUTEX
94250 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c _INITIALIZER, },
94260 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 . };.
94270 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b 0a if ( !isInit ){.
94280 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20 72 APIRET r
94290 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 42 20 c;. PTIB
942a0 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 50 50 ptib;. PP
942b0 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 20 20 IB ppib;.
942c0 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20 20 HMTX mutex;.
942d0 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 5b 33 char name[3
942e0 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f 73 47 2];. DosG
942f0 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 70 etInfoBlocks( &p
94300 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a 20 20 tib, &ppib );.
94310 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
94320 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 28 6e printf( sizeof(n
94330 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c 53 ame), name, "\\S
94340 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 34 78 EM32\\SQLITE%04x
94350 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
94360 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70 69 ppi
94370 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b 0a b->pib_ulpid );.
94380 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 while( !
94390 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 isInit ){.
943a0 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a 20 mutex = 0;.
943b0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 44 6f rc = Do
943c0 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 sCreateMutexSem(
943d0 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20 30 name, &mutex, 0
943e0 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 20 20 , FALSE);.
943f0 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f if( rc == NO
94400 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 _ERROR ){.
94410 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
94420 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 if( !is
94430 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Init ){.
94440 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30 for( i = 0
94450 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 ; i < sizeof(sta
94460 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 ticMutexes)/size
94470 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 of(staticMutexes
94480 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 [0]); i++ ){.
94490 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 Dos
944a0 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 CreateMutexSem(
944b0 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 0, &staticMutexe
944c0 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46 s[i].mutex, 0, F
944d0 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 ALSE );.
944e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
944f0 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 isInit = 1
94500 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
94510 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43 DosC
94520 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75 loseMutexSem( mu
94530 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20 tex );.
94540 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d }else if( rc ==
94550 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45 ERROR_DUPLICATE
94560 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20 _NAME ){.
94570 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31 DosSleep( 1
94580 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 );. }e
94590 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
945a0 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 return p;.
945b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
945c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
945d0 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e ssert( iType-2 >
945e0 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 = 0 );. ass
945f0 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 ert( iType-2 < s
94600 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 izeof(staticMute
94610 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 xes)/sizeof(stat
94620 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b icMutexes[0]) );
94630 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 . p = &stat
94640 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d icMutexes[iType-
94650 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 2];. p->id
94660 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 = iType;. b
94670 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
94680 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a return p;.}...
94690 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
946a0 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 ne deallocates a
946b0 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f previously allo
946c0 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 cated mutex..**
946d0 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
946e0 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
946f0 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74 every mutex that
94700 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a it allocates..*
94710 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 /.static void os
94720 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 2MutexFree(sqlit
94730 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
94740 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
94750 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e n;. assert( p->
94760 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 nRef==0 );. ass
94770 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
94780 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c TE_MUTEX_FAST ||
94790 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
947a0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 UTEX_RECURSIVE )
947b0 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 ;. DosCloseMute
947c0 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29 xSem( p->mutex )
947d0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
947e0 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ( p );.}../*.**
947f0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
94800 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 x_enter() and sq
94810 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
94820 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d ) routines attem
94830 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 pt.** to enter a
94840 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 mutex. If anot
94850 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c her thread is al
94860 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 ready within the
94870 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 mutex,.** sqlit
94880 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
94890 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 will block and
948a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
948b0 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a y() will return.
948c0 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 ** SQLITE_BUSY.
948d0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
948e0 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 ex_try() interfa
948f0 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ce returns SQLIT
94900 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 E_OK.** upon suc
94910 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 cessful entry.
94920 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 Mutexes created
94930 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 using SQLITE_MUT
94940 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
94950 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d .** be entered m
94960 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 ultiple times by
94970 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
94980 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 . In such cases
94990 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d the,.** mutex m
949a0 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e ust be exited an
949b0 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 equal number of
949c0 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e times before an
949d0 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 other thread.**
949e0 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 can enter. If t
949f0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 he same thread t
94a00 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e ries to enter an
94a10 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 y other kind of
94a20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 mutex.** more th
94a30 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 an once, the beh
94a40 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
94a50 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
94a60 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 id os2MutexEnter
94a70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
94a80 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 p){. TID tid;.
94a90 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 PID holder1;.
94aa0 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 ULONG holder2;.
94ab0 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
94ac0 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rn;. assert( p-
94ad0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
94ae0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f X_RECURSIVE || o
94af0 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 s2MutexNotheld(p
94b00 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73 ) );. DosReques
94b10 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 tMutexSem(p->mut
94b20 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49 ex, SEM_INDEFINI
94b30 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51 TE_WAIT);. DosQ
94b40 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e ueryMutexSem(p->
94b50 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c mutex, &holder1,
94b60 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 &tid, &holder2)
94b70 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 ;. p->owner = t
94b80 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b id;. p->nRef++;
94b90 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 .}.static int os
94ba0 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 2MutexTry(sqlite
94bb0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
94bc0 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64 nt rc;. TID tid
94bd0 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b ;. PID holder1;
94be0 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 . ULONG holder2
94bf0 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
94c00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
94c10 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
94c20 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
94c30 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d ECURSIVE || os2M
94c40 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
94c50 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65 ;. if( DosReque
94c60 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 stMutexSem(p->mu
94c70 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41 tex, SEM_IMMEDIA
94c80 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f TE_RETURN) == NO
94c90 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f _ERROR) {. Do
94ca0 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
94cb0 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 ->mutex, &holder
94cc0 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 1, &tid, &holder
94cd0 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 2);. p->owner
94ce0 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e = tid;. p->n
94cf0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 Ref++;. rc =
94d00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 SQLITE_OK;. } e
94d10 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53 lse {. rc = S
94d20 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a QLITE_BUSY;. }.
94d30 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
94d40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
94d50 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
94d60 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
94d70 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
94d80 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
94d90 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
94da0 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
94db0 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e ehavior.** is un
94dc0 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d defined if the m
94dd0 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 utex is not curr
94de0 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 ently entered or
94df0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
94e00 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ntly allocated.
94e10 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
94e20 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f er do either..*/
94e30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
94e40 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 MutexLeave(sqlit
94e50 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
94e60 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 TID tid;. PID h
94e70 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 older1;. ULONG
94e80 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 holder2;. if( p
94e90 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
94ea0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
94eb0 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 0 );. DosQueryM
94ec0 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 utexSem(p->mutex
94ed0 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 , &holder1, &tid
94ee0 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61 , &holder2);. a
94ef0 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d ssert( p->owner=
94f00 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 =tid );. p->nRe
94f10 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 f--;. assert( p
94f20 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
94f30 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
94f40 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
94f50 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53 DosReleaseMutexS
94f60 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a em(p->mutex);.}.
94f70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
94f80 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
94f90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
94fa0 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
94fb0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
94fc0 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
94fd0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
94fe0 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
94ff0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 statements..*/.s
95000 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 tatic int os2Mut
95010 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d exHeld(sqlite3_m
95020 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 utex *p){. TID
95030 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a tid;. PID pid;.
95040 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b ULONG ulCount;
95050 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 . PTIB ptib;.
95060 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20 if( p!=0 ) {.
95070 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 DosQueryMutexSe
95080 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 m(p->mutex, &pid
95090 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 , &tid, &ulCount
950a0 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 );. } else {.
950b0 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 DosGetInfoBloc
950c0 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b ks(&ptib, NULL);
950d0 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d . tid = ptib-
950e0 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 >tib_ptib2->tib2
950f0 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 _ultid;. }. re
95100 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d turn p==0 || (p-
95110 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f >nRef!=0 && p->o
95120 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 wner==tid);.}.st
95130 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 atic int os2Mute
95140 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
95150 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 _mutex *p){. TI
95160 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 D tid;. PID pid
95170 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e ;. ULONG ulCoun
95180 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a t;. PTIB ptib;.
95190 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b 0a if( p!= 0 ) {.
951a0 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 DosQueryMute
951b0 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 xSem(p->mutex, &
951c0 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f pid, &tid, &ulCo
951d0 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b unt);. } else {
951e0 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 . DosGetInfoB
951f0 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c locks(&ptib, NUL
95200 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 L);. tid = pt
95210 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 ib->tib_ptib2->t
95220 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 ib2_ultid;. }.
95230 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
95240 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d p->nRef==0 || p-
95250 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 >owner!=tid;.}.#
95260 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
95270 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 IVATE sqlite3_mu
95280 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c tex_methods *sql
95290 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 ite3DefaultMutex
952a0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
952b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
952c0 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 ethods sMutex =
952d0 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e {. os2MutexIn
952e0 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 it,. os2Mutex
952f0 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 End,. os2Mute
95300 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d xAlloc,. os2M
95310 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73 utexFree,. os
95320 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 2MutexEnter,.
95330 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20 os2MutexTry,.
95340 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c os2MutexLeave,
95350 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
95360 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65 EBUG. os2Mute
95370 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75 xHeld,. os2Mu
95380 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69 texNotheld.#endi
95390 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
953a0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 &sMutex;.}.#end
953b0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
953c0 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EX_OS2 */../****
953d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
953e0 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a f mutex_os2.c **
953f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
95420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
95430 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78 file mutex_unix
95440 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
95450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
95470 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2007 August 28.
95480 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
95490 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
954a0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
954b0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
954c0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
954d0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
954e0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
954f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
95500 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
95510 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
95520 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
95530 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
95540 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
95550 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
95560 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
95570 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
95580 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
95590 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
955a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
955b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
955c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
955d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
955e0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
955f0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
95600 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
95610 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 ent mutexes for
95620 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24 pthreads.**.** $
95630 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 Id: mutex_unix.c
95640 2c 76 20 31 2e 31 33 20 32 30 30 38 2f 30 37 2f ,v 1.13 2008/07/
95650 31 36 20 31 32 3a 33 33 3a 32 34 20 64 72 68 20 16 12:33:24 drh
95660 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
95670 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 The code in this
95680 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 file is only us
95690 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d ed if we are com
956a0 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66 piling threadsaf
956b0 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 e.** under unix
956c0 77 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a with pthreads..*
956d0 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 *.** Note that t
956e0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
956f0 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65 on requires a ve
95700 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 rsion of pthread
95710 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 s that.** suppor
95720 74 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ts recursive mut
95730 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 exes..*/.#ifdef
95740 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 SQLITE_MUTEX_PTH
95750 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 READS..#include
95760 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a <pthread.h>.../*
95770 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 .** Each recursi
95780 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 ve mutex is an i
95790 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
957a0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
957b0 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 re..*/.struct sq
957c0 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 lite3_mutex {.
957d0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 pthread_mutex_t
957e0 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 mutex; /* Mu
957f0 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 tex controlling
95800 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e the lock */. in
95810 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 t id;
95820 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
95830 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 x type */. int
95840 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
95850 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
95860 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f of entrances */
95870 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e . pthread_t own
95880 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a er; /*
95890 20 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 Thread that is
958a0 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 within this mute
958b0 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 x */.#ifdef SQLI
958c0 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74 TE_DEBUG. int t
958d0 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 race;
958e0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
958f0 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a trace changes *
95900 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 /.#endif.};.#ifd
95910 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
95920 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f #define SQLITE3_
95930 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
95940 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 R { PTHREAD_MUTE
95950 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 X_INITIALIZER, 0
95960 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 , 0, (pthread_t)
95970 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 0, 0 }.#else.#de
95980 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 fine SQLITE3_MUT
95990 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b EX_INITIALIZER {
959a0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 PTHREAD_MUTEX_I
959b0 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 NITIALIZER, 0, 0
959c0 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d , (pthread_t)0 }
959d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
959e0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
959f0 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 _held() and sqli
95a00 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
95a10 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a d() routine are.
95a20 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 ** intended for
95a30 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 use only inside
95a40 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
95a50 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c nts. On some pl
95a60 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 atforms,.** ther
95a70 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20 e might be race
95a80 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 conditions that
95a90 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20 can cause these
95aa0 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 routines to.** d
95ab0 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74 eliver incorrect
95ac0 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 results. In pa
95ad0 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68 rticular, if pth
95ae0 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a read_equal() is.
95af0 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 ** not an atomic
95b00 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e operation, then
95b10 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
95b20 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a might delivery.*
95b30 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 * incorrect resu
95b40 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c lts. On most pl
95b50 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 atforms, pthread
95b60 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a _equal() is a .*
95b70 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 * comparison of
95b80 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64 two integers and
95b90 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74 is therefore at
95ba0 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72 omic. But we ar
95bb0 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 e.** told that H
95bc0 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20 PUX is not such
95bd0 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 a platform. If
95be0 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 so, then these r
95bf0 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 outines.** will
95c00 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 not always work
95c10 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 correctly on HPU
95c20 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 X..**.** On thos
95c30 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 e platforms wher
95c40 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 e pthread_equal(
95c50 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c ) is not atomic,
95c60 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c SQLite.** shoul
95c70 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 d be compiled wi
95c80 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 thout -DSQLITE_D
95c90 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44 EBUG and with -D
95ca0 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b NDEBUG to.** mak
95cb0 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74 e sure no assert
95cc0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 () statements ar
95cd0 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 e evaluated and
95ce0 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 hence these.** r
95cf0 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65 outines are neve
95d00 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 r called..*/.#if
95d10 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 ndef NDEBUG.stat
95d20 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 ic int pthreadMu
95d30 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f texHeld(sqlite3_
95d40 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 mutex *p){. ret
95d50 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 urn (p->nRef!=0
95d60 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c && pthread_equal
95d70 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 (p->owner, pthre
95d80 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 ad_self()));.}.s
95d90 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 tatic int pthrea
95da0 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 dMutexNotheld(sq
95db0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
95dc0 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 . return p->nRe
95dd0 66 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f f==0 || pthread_
95de0 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 equal(p->owner,
95df0 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d pthread_self())=
95e00 3d 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a =0;.}.#endif../*
95e10 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
95e20 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 nd deinitialize
95e30 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
95e40 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 tem..*/.static i
95e50 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 nt pthreadMutexI
95e60 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 nit(void){ retur
95e70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 n SQLITE_OK; }.s
95e80 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 tatic int pthrea
95e90 64 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b dMutexEnd(void){
95ea0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
95eb0 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K; }../*.** The
95ec0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
95ed0 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c loc() routine al
95ee0 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a locates a new.**
95ef0 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 mutex and retur
95f00 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
95f10 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 it. If it retur
95f20 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
95f30 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
95f40 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
95f50 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 allocated. SQLi
95f60 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e te.** will unwin
95f70 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 d its stack and
95f80 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e return an error.
95f90 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a The argument.*
95fa0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 * to sqlite3_mut
95fb0 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e ex_alloc() is on
95fc0 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
95fd0 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a er constants:.**
95fe0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
95ff0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 SQLITE_MUTEX_F
96000 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c AST.** <li> SQL
96010 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
96020 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c IVE.** <li> SQL
96030 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
96040 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 _MASTER.** <li>
96050 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
96060 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e ATIC_MEM.** <li>
96070 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
96080 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c TATIC_MEM2.** <l
96090 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
960a0 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 _STATIC_PRNG.**
960b0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
960c0 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a EX_STATIC_LRU.**
960d0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
960e0 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 first two const
960f0 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 ants cause sqlit
96100 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
96110 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 to create.** a
96120 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 new mutex. The
96130 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 new mutex is rec
96140 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 ursive when SQLI
96150 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
96160 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 VE.** is used bu
96170 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c t not necessaril
96180 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 y so when SQLITE
96190 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 _MUTEX_FAST is u
961a0 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 sed..** The mute
961b0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
961c0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
961d0 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 o make a distinc
961e0 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 tion.** between
961f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
96200 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 URSIVE and SQLIT
96210 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 E_MUTEX_FAST if
96220 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 it does.** not w
96230 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c ant to. But SQL
96240 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 ite will only re
96250 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 quest a recursiv
96260 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 e mutex in.** ca
96270 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 ses where it rea
96280 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 lly needs one.
96290 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
962a0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
962b0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
962c0 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
962d0 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
962e0 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
962f0 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
96300 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
96310 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
96320 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
96330 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 X_FAST..**.** Th
96340 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 e other allowed
96350 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 parameters to sq
96360 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
96370 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a c() each return.
96380 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
96390 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 a static preexis
963a0 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 ting mutex. Thr
963b0 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 ee static mutexe
963c0 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 s are.** used by
963d0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 the current ver
963e0 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 sion of SQLite.
963f0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
96400 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 of SQLite.** ma
96410 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c y add additional
96420 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e static mutexes.
96430 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 Static mutexes
96440 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 are for interna
96450 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 l.** use by SQLi
96460 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 te only. Applic
96470 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 ations that use
96480 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 SQLite mutexes s
96490 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c hould.** use onl
964a0 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 y the dynamic mu
964b0 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 texes returned b
964c0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 y SQLITE_MUTEX_F
964d0 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 AST or.** SQLITE
964e0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
964f0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
96500 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 t if one of the
96510 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 dynamic mutex pa
96520 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 rameters (SQLITE
96530 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f _MUTEX_FAST.** o
96540 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 r SQLITE_MUTEX_R
96550 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 ECURSIVE) is use
96560 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d d then sqlite3_m
96570 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 utex_alloc().**
96580 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 returns a differ
96590 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 ent mutex on eve
965a0 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f ry call. But fo
965b0 72 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a r the static .**
965c0 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 mutex types, th
965d0 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 e same mutex is
965e0 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 returned on ever
965f0 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a y call that has.
96600 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 ** the same type
96610 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
96620 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
96630 20 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c *pthreadMutexAl
96640 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a loc(int iType){.
96650 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
96660 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 _mutex staticMut
96670 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 exes[] = {. S
96680 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 QLITE3_MUTEX_INI
96690 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 TIALIZER,. SQ
966a0 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 LITE3_MUTEX_INIT
966b0 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c IALIZER,. SQL
966c0 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
966d0 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 ALIZER,. SQLI
966e0 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
966f0 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 LIZER,. SQLIT
96700 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
96710 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 IZER,. SQLITE
96720 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
96730 5a 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 ZER. };. sqlit
96740 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 e3_mutex *p;. s
96750 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a witch( iType ){.
96760 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
96770 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a MUTEX_RECURSIVE:
96780 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c {. p = sql
96790 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 ite3MallocZero(
967a0 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 sizeof(*p) );.
967b0 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 if( p ){.#if
967c0 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 def SQLITE_HOMEG
967d0 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d ROWN_RECURSIVE_M
967e0 55 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 UTEX. /*
967f0 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 If recursive mut
96800 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 exes are not ava
96810 69 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 ilable, we will
96820 68 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 have to.
96830 2a 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e ** build our own
96840 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f . See below. */
96850 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
96860 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e _mutex_init(&p->
96870 6d 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 mutex, 0);.#else
96880 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 . /* Use
96890 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 a recursive mute
968a0 78 20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c x if it is avail
968b0 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 able */.
968c0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 pthread_mutexatt
968d0 72 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74 r_t recursiveAtt
968e0 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 r;. pthre
968f0 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 ad_mutexattr_ini
96900 74 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 t(&recursiveAttr
96910 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 );. pthre
96920 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 ad_mutexattr_set
96930 74 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41 type(&recursiveA
96940 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 ttr, PTHREAD_MUT
96950 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 EX_RECURSIVE);.
96960 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
96970 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 utex_init(&p->mu
96980 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 tex, &recursiveA
96990 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 ttr);. pt
969a0 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f hread_mutexattr_
969b0 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 destroy(&recursi
969c0 76 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a veAttr);.#endif.
969d0 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 p->id =
969e0 69 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 iType;. }.
969f0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
96a00 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
96a10 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a E_MUTEX_FAST: {.
96a20 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 p = sqlite
96a30 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 3MallocZero( siz
96a40 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 eof(*p) );.
96a50 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 if( p ){.
96a60 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b p->id = iType;
96a70 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
96a80 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e _mutex_init(&p->
96a90 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 mutex, 0);.
96aa0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
96ab0 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
96ac0 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 t: {. asser
96ad0 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 t( iType-2 >= 0
96ae0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
96af0 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f iType-2 < sizeo
96b00 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 f(staticMutexes)
96b10 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 /sizeof(staticMu
96b20 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 texes[0]) );.
96b30 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 p = &staticMu
96b40 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a texes[iType-2];.
96b50 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
96b60 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ype;. break
96b70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
96b80 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn p;.}.../*.*
96b90 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
96ba0 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 eallocates a pre
96bb0 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 viously.** alloc
96bc0 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 51 4c ated mutex. SQL
96bd0 69 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 ite is careful t
96be0 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 o deallocate eve
96bf0 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 ry.** mutex that
96c00 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a it allocates..*
96c10 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 /.static void pt
96c20 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 28 73 hreadMutexFree(s
96c30 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
96c40 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e {. assert( p->n
96c50 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 Ref==0 );. asse
96c60 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
96c70 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 E_MUTEX_FAST ||
96c80 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
96c90 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b TEX_RECURSIVE );
96ca0 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 . pthread_mutex
96cb0 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e 6d 75 74 _destroy(&p->mut
96cc0 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ex);. sqlite3_f
96cd0 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ree(p);.}../*.**
96ce0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
96cf0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 ex_enter() and s
96d00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
96d10 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 () routines atte
96d20 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 mpt.** to enter
96d30 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f a mutex. If ano
96d40 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 ther thread is a
96d50 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 lready within th
96d60 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 e mutex,.** sqli
96d70 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
96d80 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 ) will block and
96d90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
96da0 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e ry() will return
96db0 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e .** SQLITE_BUSY.
96dc0 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 The sqlite3_mu
96dd0 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 tex_try() interf
96de0 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ace returns SQLI
96df0 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 TE_OK.** upon su
96e00 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 ccessful entry.
96e10 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 Mutexes created
96e20 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 using SQLITE_MU
96e30 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 TEX_RECURSIVE ca
96e40 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 n.** be entered
96e50 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 multiple times b
96e60 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 y the same threa
96e70 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 d. In such case
96e80 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 s the,.** mutex
96e90 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 must be exited a
96ea0 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f n equal number o
96eb0 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 f times before a
96ec0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a nother thread.**
96ed0 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 can enter. If
96ee0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
96ef0 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 tries to enter a
96f00 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 ny other kind of
96f10 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 mutex.** more t
96f20 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 han once, the be
96f30 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
96f40 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ned..*/.static v
96f50 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 oid pthreadMutex
96f60 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 Enter(sqlite3_mu
96f70 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 tex *p){. asser
96f80 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
96f90 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
96fa0 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78 || pthreadMutex
96fb0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23 Notheld(p) );..#
96fc0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d ifdef SQLITE_HOM
96fd0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 EGROWN_RECURSIVE
96fe0 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72 _MUTEX. /* If r
96ff0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 ecursive mutexes
97000 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 are not availab
97010 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65 le, then we have
97020 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 to grow. ** ou
97030 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 r own. This imp
97040 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 lementation assu
97050 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64 mes that pthread
97060 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73 _equal(). ** is
97070 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 atomic - that i
97080 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 t cannot be dece
97090 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 ived into thinki
970a0 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 ng self. ** and
970b0 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71 p->owner are eq
970c0 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20 ual if p->owner
970d0 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20 changes between
970e0 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 two values. **
970f0 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75 that are not equ
97100 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65 al to self while
97110 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
97120 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e is taking place.
97130 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 . ** This imple
97140 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61 mentation also a
97150 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e ssumes a coheren
97160 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a t cache - that .
97170 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72 ** separate pr
97180 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72 ocesses cannot r
97190 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61 ead different va
971a0 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 lues from the sa
971b0 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20 me. ** address
971c0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
971d0 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 . If either of
971e0 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74 these two condit
971f0 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f ions. ** are no
97200 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20 t met, then the
97210 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69 mutexes will fai
97220 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77 l and problems w
97230 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f ill result.. */
97240 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 . {. pthread
97250 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61 _t self = pthrea
97260 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66 d_self();. if
97270 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 ( p->nRef>0 && p
97280 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e thread_equal(p->
97290 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a owner, self) ){.
972a0 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b p->nRef++;
972b0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
972c0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
972d0 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b lock(&p->mutex);
972e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
972f0 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 ->nRef==0 );.
97300 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 p->owner = se
97310 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 lf;. p->nRe
97320 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d f = 1;. }. }
97330 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20 .#else. /* Use
97340 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63 the built-in rec
97350 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69 ursive mutexes i
97360 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c f they are avail
97370 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 74 68 able.. */. pth
97380 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 read_mutex_lock(
97390 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d &p->mutex);. p-
973a0 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64 >owner = pthread
973b0 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d 3e 6e 52 _self();. p->nR
973c0 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 ef++;.#endif..#i
973d0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
973e0 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 G. if( p->trace
973f0 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 ){. printf("
97400 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28 enter mutex %p (
97410 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 %d) with nRef=%d
97420 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 \n", p, p->trace
97430 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a , p->nRef);. }.
97440 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63 20 #endif.}.static
97450 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 int pthreadMutex
97460 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 Try(sqlite3_mute
97470 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b x *p){. int rc;
97480 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
97490 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
974a0 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72 ECURSIVE || pthr
974b0 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 eadMutexNotheld(
974c0 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 p) );..#ifdef SQ
974d0 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 LITE_HOMEGROWN_R
974e0 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 ECURSIVE_MUTEX.
974f0 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 65 /* If recursive
97500 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 mutexes are not
97510 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e available, then
97520 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77 we have to grow
97530 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 . ** our own.
97540 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 This implementat
97550 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 ion assumes that
97560 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 pthread_equal()
97570 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 . ** is atomic
97580 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 - that it cannot
97590 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e 74 be deceived int
975a0 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a o thinking self.
975b0 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 ** and p->owne
975c0 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 70 r are equal if p
975d0 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20 ->owner changes
975e0 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 between two valu
975f0 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65 es. ** that are
97600 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65 not equal to se
97610 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d lf while the com
97620 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e parison is takin
97630 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68 g place.. ** Th
97640 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
97650 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61 n also assumes a
97660 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20 coherent cache
97670 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70 - that . ** sep
97680 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20 arate processes
97690 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66 cannot read diff
976a0 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f erent values fro
976b0 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 m the same. **
976c0 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 73 address at the s
976d0 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69 ame time. If ei
976e0 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 ther of these tw
976f0 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a o conditions. *
97700 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 * are not met, t
97710 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 hen the mutexes
97720 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 will fail and pr
97730 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 oblems will resu
97740 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 lt.. */. {.
97750 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20 pthread_t self
97760 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
97770 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 ;. if( p->nRe
97780 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 f>0 && pthread_e
97790 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 qual(p->owner, s
977a0 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d elf) ){. p-
977b0 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 72 >nRef++;. r
977c0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
977d0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 74 68 }else if( pth
977e0 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 79 6c 6f read_mutex_trylo
977f0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 ck(&p->mutex)==0
97800 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
97810 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a ( p->nRef==0 );.
97820 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d p->owner =
97830 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e self;. p->
97840 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 nRef = 1;.
97850 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
97860 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
97870 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
97880 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c Y;. }. }.#el
97890 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20 se. /* Use the
978a0 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 built-in recursi
978b0 76 65 20 6d 75 74 65 78 65 73 20 69 66 20 74 68 ve mutexes if th
978c0 65 79 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 ey are available
978d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 74 68 .. */. if( pth
978e0 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 79 6c 6f read_mutex_trylo
978f0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 ck(&p->mutex)==0
97900 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 ){. p->owner
97910 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
97920 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b );. p->nRef++
97930 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
97940 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
97950 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
97960 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a USY;. }.#endif.
97970 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
97980 45 42 55 47 0a 20 20 69 66 28 20 72 63 3d 3d 53 EBUG. if( rc==S
97990 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 74 QLITE_OK && p->t
979a0 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e race ){. prin
979b0 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 tf("enter mutex
979c0 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 %p (%d) with nRe
979d0 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 f=%d\n", p, p->t
979e0 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a race, p->nRef);.
979f0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 }.#endif. ret
97a00 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
97a10 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
97a20 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
97a30 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
97a40 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
97a50 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
97a60 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
97a70 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f ad. The behavio
97a80 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 r.** is undefine
97a90 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 d if the mutex i
97aa0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
97ab0 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 entered or.** is
97ac0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 not currently a
97ad0 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
97ae0 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 e will never do
97af0 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 either..*/.stati
97b00 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 c void pthreadMu
97b10 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 texLeave(sqlite3
97b20 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
97b30 73 65 72 74 28 20 70 74 68 72 65 61 64 4d 75 74 sert( pthreadMut
97b40 65 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 exHeld(p) );. p
97b50 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 ->nRef--;. asse
97b60 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c rt( p->nRef==0 |
97b70 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f | p->id==SQLITE_
97b80 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
97b90 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
97ba0 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 E_HOMEGROWN_RECU
97bb0 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 69 66 RSIVE_MUTEX. if
97bc0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a ( p->nRef==0 ){.
97bd0 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
97be0 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 x_unlock(&p->mut
97bf0 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 ex);. }.#else.
97c00 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 pthread_mutex_u
97c10 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 nlock(&p->mutex)
97c20 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
97c30 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
97c40 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a if( p->trace ){.
97c50 20 20 20 20 70 72 69 6e 74 66 28 22 6c 65 61 76 printf("leav
97c60 65 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 e mutex %p (%d)
97c70 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c with nRef=%d\n",
97c80 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d p, p->trace, p-
97c90 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 >nRef);. }.#end
97ca0 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 if.}..SQLITE_PRI
97cb0 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 VATE sqlite3_mut
97cc0 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 ex_methods *sqli
97cd0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
97ce0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
97cf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
97d00 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b thods sMutex = {
97d10 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 . pthreadMute
97d20 78 49 6e 69 74 2c 0a 20 20 20 20 70 74 68 72 65 xInit,. pthre
97d30 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 adMutexEnd,.
97d40 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f pthreadMutexAllo
97d50 63 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 c,. pthreadMu
97d60 74 65 78 46 72 65 65 2c 0a 20 20 20 20 70 74 68 texFree,. pth
97d70 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 2c 0a readMutexEnter,.
97d80 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 pthreadMutex
97d90 54 72 79 2c 0a 20 20 20 20 70 74 68 72 65 61 64 Try,. pthread
97da0 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 MutexLeave,.#ifd
97db0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
97dc0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 pthreadMutex
97dd0 48 65 6c 64 2c 0a 20 20 20 20 70 74 68 72 65 61 Held,. pthrea
97de0 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 dMutexNotheld.#e
97df0 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 ndif. };.. ret
97e00 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a urn &sMutex;.}..
97e10 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
97e20 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 20 2a _MUTEX_PTHREAD *
97e30 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
97e40 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f ** End of mutex_
97e50 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a unix.c *********
97e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97e80 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
97e90 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 ** Begin file mu
97ea0 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a tex_w32.c ******
97eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97ed0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 */./*.** 2007 Au
97ee0 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 gust 14.**.** Th
97ef0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
97f00 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
97f10 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
97f20 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
97f30 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
97f40 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
97f50 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
97f60 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
97f70 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
97f80 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
97f90 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
97fa0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
97fb0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
97fc0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
97fd0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
97fe0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
97ff0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
98000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98040 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
98050 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
98060 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 C functions tha
98070 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 t implement mute
98080 78 65 73 20 66 6f 72 20 77 69 6e 33 32 0a 2a 2a xes for win32.**
98090 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 77 .** $Id: mutex_w
980a0 33 32 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38 32.c,v 1.11 2008
980b0 2f 30 36 2f 32 36 20 31 30 3a 34 31 3a 31 39 20 /06/26 10:41:19
980c0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
980d0 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
980e0 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c code in this fil
980f0 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 e is only used i
98100 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 f we are compili
98110 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 ng multithreaded
98120 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32 20 73 .** on a win32 s
98130 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 ystem..*/.#ifdef
98140 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
98150 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 2../*.** Each re
98160 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 cursive mutex is
98170 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
98180 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
98190 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 ructure..*/.stru
981a0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ct sqlite3_mutex
981b0 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f 53 45 {. CRITICAL_SE
981c0 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20 CTION mutex;
981d0 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c /* Mutex control
981e0 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f ling the lock */
981f0 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 . int id;
98200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
98210 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 Mutex type */.
98220 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
98230 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
98240 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72 61 6e umber of enteran
98250 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6f ces */. DWORD o
98260 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 wner;
98270 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f /* Thread ho
98280 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78 lding this mutex
98290 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 */.};../*.** Re
982a0 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a turn true (non-z
982b0 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72 ero) if we are r
982c0 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e unning under Win
982d0 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 NT, Win2K, WinXP
982e0 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20 ,.** or WinCE.
982f0 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65 Return false (ze
98300 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57 ro) for Win95, W
98310 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a in98, or WinME..
98320 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e **.** Here is an
98330 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73 interesting obs
98340 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35 ervation: Win95
98350 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e , Win98, and Win
98360 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c ME lack.** the L
98370 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
98380 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69 But we can sti
98390 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 ll statically li
983a0 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a nk against that.
983b0 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61 ** API as long a
983c0 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20 s we don't call
983d0 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57 it win running W
983e0 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63 in95/98/ME. A c
983f0 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 all to.** this r
98400 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
98410 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 o determine if t
98420 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35 he host is Win95
98430 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e /98/ME or.** Win
98440 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 NT/2K/XP so that
98450 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 we will know wh
98460 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 ether or not we
98470 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a can safely call.
98480 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 ** the LockFileE
98490 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 x() API..*/.#if
984a0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
984b0 23 20 64 65 66 69 6e 65 20 6d 75 74 65 78 49 73 # define mutexIs
984c0 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a NT() (1).#else.
984d0 20 20 73 74 61 74 69 63 20 69 6e 74 20 6d 75 74 static int mut
984e0 65 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 exIsNT(void){.
984f0 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f 73 54 static int osT
98500 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 ype = 0;. if(
98510 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 osType==0 ){.
98520 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 OSVERSIONINF
98530 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 O sInfo;. s
98540 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e Info.dwOSVersion
98550 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f InfoSize = sizeo
98560 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 f(sInfo);.
98570 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 49 GetVersionEx(&sI
98580 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73 54 79 nfo);. osTy
98590 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 pe = sInfo.dwPla
985a0 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 tformId==VER_PLA
985b0 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f TFORM_WIN32_NT ?
985c0 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 2 : 1;. }.
985d0 20 20 72 65 74 75 72 6e 20 6f 73 54 79 70 65 3d return osType=
985e0 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f =2;. }.#endif /
985f0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 * SQLITE_OS_WINC
98600 45 20 2a 2f 0a 0a 0a 23 69 66 64 65 66 20 53 51 E */...#ifdef SQ
98610 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a LITE_DEBUG./*.**
98620 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
98630 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 ex_held() and sq
98640 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
98650 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 eld() routine ar
98660 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f e.** intended fo
98670 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 r use only insid
98680 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 e assert() state
98690 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ments..*/.static
986a0 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 48 65 6c int winMutexHel
986b0 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
986c0 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
986d0 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f >nRef!=0 && p->o
986e0 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 wner==GetCurrent
986f0 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 73 74 ThreadId();.}.st
98700 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 atic int winMute
98710 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
98720 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
98730 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 turn p->nRef==0
98740 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d 47 65 74 || p->owner!=Get
98750 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 CurrentThreadId(
98760 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a );.}.#endif.../*
98770 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
98780 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 nd deinitialize
98790 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
987a0 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 tem..*/.static i
987b0 6e 74 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 nt winMutexInit(
987c0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 void){ return SQ
987d0 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 LITE_OK; }.stati
987e0 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 45 6e c int winMutexEn
987f0 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 d(void){ return
98800 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a SQLITE_OK; }../*
98810 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
98820 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f mutex_alloc() ro
98830 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 utine allocates
98840 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 a new.** mutex a
98850 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
98860 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 nter to it. If
98870 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a it returns NULL.
98880 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 ** that means th
98890 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 at a mutex could
988a0 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 not be allocate
988b0 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 d. SQLite.** wi
988c0 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 ll unwind its st
988d0 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ack and return a
988e0 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 n error. The ar
988f0 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c gument.** to sql
98900 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
98910 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 () is one of the
98920 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 se integer const
98930 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ants:.**.** <ul>
98940 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
98950 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 _MUTEX_FAST
98960 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 0.** <
98970 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
98980 58 5f 52 45 43 55 52 53 49 56 45 20 20 20 20 20 X_RECURSIVE
98990 20 20 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 1.** <li>
989a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
989b0 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20 TIC_MASTER
989c0 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 2.** <li> SQLIT
989d0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
989e0 45 4d 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 EM 3.**
989f0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
98a00 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 EX_STATIC_PRNG
98a10 20 20 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 4.** </ul>
98a20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
98a30 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 two constants c
98a40 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 ause sqlite3_mut
98a50 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 ex_alloc() to cr
98a60 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 eate.** a new mu
98a70 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 tex. The new mu
98a80 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65 tex is recursive
98a90 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 when SQLITE_MUT
98aa0 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
98ab0 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 is used but not
98ac0 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 necessarily so w
98ad0 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 hen SQLITE_MUTEX
98ae0 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a _FAST is used..*
98af0 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c * The mutex impl
98b00 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 ementation does
98b10 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 not need to make
98b20 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a a distinction.*
98b30 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 * between SQLITE
98b40 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
98b50 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 and SQLITE_MUTE
98b60 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 X_FAST if it doe
98b70 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f s.** not want to
98b80 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 . But SQLite wi
98b90 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 ll only request
98ba0 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 a recursive mute
98bb0 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 x in.** cases wh
98bc0 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 ere it really ne
98bd0 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 eds one. If a f
98be0 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 aster non-recurs
98bf0 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 ive mutex.** imp
98c00 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 lementation is a
98c10 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 vailable on the
98c20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 host platform, t
98c30 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 he mutex subsyst
98c40 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 em.** might retu
98c50 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 rn such a mutex
98c60 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 in response to S
98c70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
98c80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 ..**.** The othe
98c90 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 r allowed parame
98ca0 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f ters to sqlite3_
98cb0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 mutex_alloc() ea
98cc0 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 ch return.** a p
98cd0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 ointer to a stat
98ce0 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d ic preexisting m
98cf0 75 74 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 utex. Three sta
98d00 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a tic mutexes are.
98d10 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 ** used by the c
98d20 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f urrent version o
98d30 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 f SQLite. Futur
98d40 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 e versions of SQ
98d50 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 Lite.** may add
98d60 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 additional stati
98d70 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 c mutexes. Stat
98d80 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 ic mutexes are f
98d90 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
98da0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c se by SQLite onl
98db0 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 y. Applications
98dc0 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 that use SQLite
98dd0 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a mutexes should.
98de0 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 ** use only the
98df0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 dynamic mutexes
98e00 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 returned by SQLI
98e10 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 TE_MUTEX_FAST or
98e20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 .** SQLITE_MUTEX
98e30 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a _RECURSIVE..**.*
98e40 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f * Note that if o
98e50 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 ne of the dynami
98e60 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 c mutex paramete
98e70 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 rs (SQLITE_MUTEX
98e80 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 _FAST.** or SQLI
98e90 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
98ea0 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e VE) is used then
98eb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
98ec0 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e lloc().** return
98ed0 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 s a different mu
98ee0 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c tex on every cal
98ef0 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 l. But for the
98f00 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 static .** mutex
98f10 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 types, the same
98f20 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e mutex is return
98f30 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ed on every call
98f40 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 that has.** the
98f50 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 same type numbe
98f60 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c r..*/.static sql
98f70 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d ite3_mutex *winM
98f80 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 utexAlloc(int iT
98f90 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f ype){. sqlite3_
98fa0 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 mutex *p;.. swi
98fb0 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 tch( iType ){.
98fc0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 case SQLITE_MU
98fd0 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 TEX_FAST:. ca
98fe0 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f se SQLITE_MUTEX_
98ff0 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 RECURSIVE: {.
99000 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 p = sqlite3Ma
99010 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 llocZero( sizeof
99020 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 (*p) );. if
99030 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ( p ){. p
99040 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 ->id = iType;.
99050 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 Initialize
99060 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
99070 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 &p->mutex);.
99080 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
99090 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
990a0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 lt: {. stat
990b0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
990c0 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 staticMutexes[6
990d0 5d 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 ];. static
990e0 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a int isInit = 0;.
990f0 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 69 73 while( !is
99100 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Init ){.
99110 73 74 61 74 69 63 20 6c 6f 6e 67 20 6c 6f 63 6b static long lock
99120 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 = 0;. if
99130 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e 63 ( InterlockedInc
99140 72 65 6d 65 6e 74 28 26 6c 6f 63 6b 29 3d 3d 31 rement(&lock)==1
99150 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e ){. in
99160 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 66 t i;. f
99170 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 or(i=0; i<sizeof
99180 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f (staticMutexes)/
99190 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 sizeof(staticMut
991a0 65 78 65 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a exes[0]); i++){.
991b0 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 69 74 Init
991c0 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 ializeCriticalSe
991d0 63 74 69 6f 6e 28 26 73 74 61 74 69 63 4d 75 74 ction(&staticMut
991e0 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a exes[i].mutex);.
991f0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
99200 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 isInit = 1
99210 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
99220 0a 20 20 20 20 20 20 20 20 20 20 53 6c 65 65 70 . Sleep
99230 28 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 (1);. }.
99240 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
99250 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 ert( iType-2 >=
99260 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0 );. asser
99270 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a t( iType-2 < siz
99280 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 eof(staticMutexe
99290 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 s)/sizeof(static
992a0 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 Mutexes[0]) );.
992b0 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 p = &static
992c0 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d Mutexes[iType-2]
992d0 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 ;. p->id =
992e0 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 iType;. bre
992f0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
99300 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a return p;.}.../*
99310 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
99320 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 deallocates a p
99330 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c reviously.** all
99340 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 ocated mutex. S
99350 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c QLite is careful
99360 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 to deallocate e
99370 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 very.** mutex th
99380 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e at it allocates.
99390 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
993a0 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c winMutexFree(sql
993b0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
993c0 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 assert( p );.
993d0 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
993e0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
993f0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
99400 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e UTEX_FAST || p->
99410 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
99420 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
99430 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65 DeleteCriticalSe
99440 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 ction(&p->mutex)
99450 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
99460 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 (p);.}../*.** Th
99470 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
99480 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 enter() and sqli
99490 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
994a0 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 routines attempt
994b0 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d .** to enter a m
994c0 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 utex. If anothe
994d0 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 r thread is alre
994e0 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d ady within the m
994f0 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 utex,.** sqlite3
99500 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 _mutex_enter() w
99510 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 ill block and sq
99520 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
99530 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a ) will return.**
99540 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 SQLITE_BUSY. T
99550 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
99560 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 _try() interface
99570 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
99580 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 OK.** upon succe
99590 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 ssful entry. Mu
995a0 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 texes created us
995b0 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ing SQLITE_MUTEX
995c0 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a _RECURSIVE can.*
995d0 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c * be entered mul
995e0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 tiple times by t
995f0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
99600 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 In such cases t
99610 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 he,.** mutex mus
99620 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 t be exited an e
99630 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 qual number of t
99640 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 imes before anot
99650 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 her thread.** ca
99660 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 n enter. If the
99670 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 same thread tri
99680 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 es to enter any
99690 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 other kind of mu
996a0 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e tex.** more than
996b0 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 once, the behav
996c0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
996d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
996e0 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73 winMutexEnter(s
996f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
99700 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 {. assert( p->i
99710 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
99720 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e RECURSIVE || win
99730 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
99740 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63 );. EnterCritic
99750 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
99760 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 tex);. p->owner
99770 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 = GetCurrentThr
99780 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e eadId(); . p->n
99790 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 Ref++;.}.static
997a0 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28 int winMutexTry(
997b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
997c0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
997d0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73 LITE_BUSY;. ass
997e0 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
997f0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
99800 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f VE || winMutexNo
99810 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a theld(p) );. /*
99820 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 . ** The sqlite
99830 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
99840 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 61 utine is very ra
99850 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77 rely used, and w
99860 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75 hen it. ** is u
99870 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79 sed it is merely
99880 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e an optimization
99890 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66 . So it is OK f
998a0 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a or it to always.
998b0 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a ** fail. . *
998c0 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e *. ** The TryEn
998d0 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
998e0 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 on() interface i
998f0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
99900 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20 on WinNT.. **
99910 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73 And some windows
99920 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c compilers compl
99930 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74 ain if you try t
99940 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74 o use it without
99950 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e . ** first doin
99960 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20 g some #defines
99970 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c that prevent SQL
99980 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e ite from buildin
99990 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a g on Win98.. **
999a0 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e For that reason
999b0 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74 , we will omit t
999c0 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e his optimization
999d0 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20 for now. See.
999e0 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35 ** ticket #2685
999f0 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69 .. */.#if 0. i
99a00 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26 f( mutexIsNT() &
99a10 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 & TryEnterCritic
99a20 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
99a30 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f tex) ){. p->o
99a40 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e wner = GetCurren
99a50 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20 tThreadId();.
99a60 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
99a70 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
99a80 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 }.#endif. ret
99a90 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
99aa0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
99ab0 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
99ac0 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
99ad0 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
99ae0 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
99af0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
99b00 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f ad. The behavio
99b10 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 r.** is undefine
99b20 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 d if the mutex i
99b30 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
99b40 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 entered or.** is
99b50 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 not currently a
99b60 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
99b70 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 e will never do
99b80 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 either..*/.stati
99b90 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c c void winMutexL
99ba0 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
99bb0 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
99bc0 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
99bd0 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 assert( p->owne
99be0 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 r==GetCurrentThr
99bf0 65 61 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e eadId() );. p->
99c00 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 nRef--;. assert
99c10 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 ( p->nRef==0 ||
99c20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
99c30 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b TEX_RECURSIVE );
99c40 0a 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c . LeaveCritical
99c50 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 Section(&p->mute
99c60 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 x);.}..SQLITE_PR
99c70 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 IVATE sqlite3_mu
99c80 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c tex_methods *sql
99c90 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 ite3DefaultMutex
99ca0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
99cb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
99cc0 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 ethods sMutex =
99cd0 7b 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e {. winMutexIn
99ce0 69 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 it,. winMutex
99cf0 45 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 End,. winMute
99d00 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d xAlloc,. winM
99d10 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 utexFree,. wi
99d20 6e 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 nMutexEnter,.
99d30 20 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 winMutexTry,.
99d40 20 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c winMutexLeave,
99d50 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
99d60 45 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 EBUG. winMute
99d70 78 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 xHeld,. winMu
99d80 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69 texNotheld.#endi
99d90 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
99da0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 &sMutex;.}.#end
99db0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
99dc0 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EX_W32 */../****
99dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
99de0 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a f mutex_w32.c **
99df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
99e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
99e30 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a file malloc.c *
99e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
99e70 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
99e80 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
99e90 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
99ea0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
99eb0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
99ec0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
99ed0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
99ee0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
99ef0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
99f00 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
99f10 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
99f20 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
99f30 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
99f40 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
99f50 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
99f60 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
99f70 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
99f80 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
99f90 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
99fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99fe0 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61 *.**.** Memory a
99ff0 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
9a000 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 ons used through
9a010 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a out sqlite..**.*
9a020 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 2c * $Id: malloc.c,
9a030 76 20 31 2e 34 35 20 32 30 30 38 2f 31 30 2f 31 v 1.45 2008/10/1
9a040 32 20 30 30 3a 32 37 3a 35 33 20 73 68 61 6e 65 2 00:27:53 shane
9a050 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
9a060 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 This routine ru
9a070 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f ns when the memo
9a080 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 65 ry allocator see
9a090 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 6f s that the.** to
9a0a0 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 tal memory alloc
9a0b0 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 74 ation is about t
9a0c0 6f 20 65 78 63 65 65 64 20 74 68 65 20 73 6f 66 o exceed the sof
9a0d0 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 2e t heap.** limit.
9a0e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
9a0f0 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 softHeapLimitEnf
9a100 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 20 2a 4e orcer(. void *N
9a110 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 otUsed, . sqlit
9a120 65 33 5f 69 6e 74 36 34 20 69 6e 55 73 65 2c 0a e3_int64 inUse,.
9a130 20 20 69 6e 74 20 61 6c 6c 6f 63 53 69 7a 65 0a int allocSize.
9a140 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c ){. sqlite3_rel
9a150 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f ease_memory(allo
9a160 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cSize);.}../*.**
9a170 20 53 65 74 20 74 68 65 20 73 6f 66 74 20 68 65 Set the soft he
9a180 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f ap-size limit fo
9a190 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 50 r the library. P
9a1a0 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20 6f 72 assing a zero or
9a1b0 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 76 61 .** negative va
9a1c0 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 6e 6f lue indicates no
9a1d0 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 limit..*/.SQLIT
9a1e0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
9a1f0 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d e3_soft_heap_lim
9a200 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c it(int n){. sql
9a210 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c 69 6d ite3_uint64 iLim
9a220 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61 67 it;. int overag
9a230 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b 0a e;. if( n<0 ){.
9a240 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a iLimit = 0;.
9a250 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 }else{. iLi
9a260 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 73 mit = n;. }. s
9a270 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
9a280 65 28 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 e();. if( iLimi
9a290 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 t>0 ){. sqlit
9a2a0 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f e3MemoryAlarm(so
9a2b0 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 ftHeapLimitEnfor
9a2c0 63 65 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b cer, 0, iLimit);
9a2d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
9a2e0 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d lite3MemoryAlarm
9a2f0 28 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 (0, 0, 0);. }.
9a300 20 6f 76 65 72 61 67 65 20 3d 20 73 71 6c 69 74 overage = sqlit
9a310 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 e3_memory_used()
9a320 20 2d 20 6e 3b 0a 20 20 69 66 28 20 6f 76 65 72 - n;. if( over
9a330 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c age>0 ){. sql
9a340 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
9a350 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 20 ory(overage);.
9a360 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d }.}../*.** Attem
9a370 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 75 70 pt to release up
9a380 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 6e to n bytes of n
9a390 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
9a3a0 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a ory currently.**
9a3b0 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65 2e held by SQLite.
9a3c0 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e An example of n
9a3d0 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
9a3e0 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 73 ory is memory us
9a3f0 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 64 ed to.** cache d
9a400 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68 atabase pages th
9a410 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 at are not curre
9a420 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a ntly in use..*/.
9a430 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
9a440 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
9a450 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69 emory(int n){.#i
9a460 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
9a470 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 LE_MEMORY_MANAGE
9a480 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20 MENT. int nRet
9a490 3d 20 30 3b 0a 23 69 66 20 30 0a 20 20 6e 52 65 = 0;.#if 0. nRe
9a4a0 74 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 t += sqlite3Vdbe
9a4b0 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29 ReleaseMemory(n)
9a4c0 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 52 65 74 20 ;.#endif. nRet
9a4d0 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 += sqlite3Pcache
9a4e0 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d ReleaseMemory(n-
9a4f0 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 nRet);. return
9a500 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 72 65 nRet;.#else. re
9a510 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
9a520 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
9a530 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f State informatio
9a540 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 20 6d n local to the m
9a550 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
9a560 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 subsystem..*/.s
9a570 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
9a580 20 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 struct Mem0Glob
9a590 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 al {. /* Number
9a5a0 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 66 of free pages f
9a5b0 6f 72 20 73 63 72 61 74 63 68 20 61 6e 64 20 70 or scratch and p
9a5c0 61 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f 72 79 age-cache memory
9a5d0 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 61 74 */. u32 nScrat
9a5e0 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 6e 50 chFree;. u32 nP
9a5f0 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 6c 69 ageFree;.. sqli
9a600 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
9a610 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 ; /* Mut
9a620 65 78 20 74 6f 20 73 65 72 69 61 6c 69 7a 65 20 ex to serialize
9a630 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f 2a 0a access */.. /*.
9a640 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20 63 ** The alarm c
9a650 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73 20 allback and its
9a660 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20 arguments. The
9a670 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 6b 20 mem0.mutex lock
9a680 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65 6c will. ** be hel
9a690 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c 6c d while the call
9a6a0 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67 2e back is running.
9a6b0 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c Recursive call
9a6c0 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 s into. ** the
9a6d0 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d memory subsystem
9a6e0 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62 75 are allowed, bu
9a6f0 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61 63 t no new callbac
9a700 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 ks will be. **
9a710 69 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c 61 issued. The ala
9a720 72 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65 20 rmBusy variable
9a730 69 73 20 73 65 74 20 74 6f 20 70 72 65 76 65 6e is set to preven
9a740 74 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a t recursive. **
9a750 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f callbacks.. */
9a760 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
9a770 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b alarmThreshold;
9a780 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 . void (*alarmC
9a790 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 allback)(void*,
9a7a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e sqlite3_int64,in
9a7b0 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 t);. void *alar
9a7c0 6d 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c 61 72 mArg;. int alar
9a7d0 6d 42 75 73 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a mBusy;.. /*. *
9a7e0 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 * Pointers to th
9a7f0 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 e end of sqlite3
9a800 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9a810 72 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73 ratch and. ** s
9a820 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9a830 69 67 2e 70 50 61 67 65 20 74 6f 20 61 20 62 6c ig.pPage to a bl
9a840 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 ock of memory th
9a850 61 74 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20 at records. **
9a860 77 68 69 63 68 20 70 61 67 65 73 20 61 72 65 20 which pages are
9a870 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a available.. */.
9a880 20 20 75 33 32 20 2a 61 53 63 72 61 74 63 68 46 u32 *aScratchF
9a890 72 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 67 ree;. u32 *aPag
9a8a0 65 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 eFree;.} mem0 =
9a8b0 7b 20 36 32 35 36 30 39 35 35 20 7d 3b 0a 0a 23 { 62560955 };..#
9a8c0 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 define mem0 GLOB
9a8d0 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c AL(struct Mem0Gl
9a8e0 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a obal, mem0)../*.
9a8f0 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** Initialize th
9a900 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
9a910 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a ion subsystem..*
9a920 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9a930 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c int sqlite3Mall
9a940 6f 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 ocInit(void){.
9a950 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9a960 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f lConfig.m.xMallo
9a970 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 c==0 ){. sqli
9a980 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 te3MemSetDefault
9a990 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 ();. }. memset
9a9a0 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f (&mem0, 0, sizeo
9a9b0 66 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 f(mem0));. if(
9a9c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9a9d0 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 fig.bCoreMutex )
9a9e0 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 {. mem0.mutex
9a9f0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
9aa00 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
9aa10 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 X_STATIC_MEM);.
9aa20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
9aa30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9aa40 72 61 74 63 68 20 26 26 20 73 71 6c 69 74 65 33 ratch && sqlite3
9aa50 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 GlobalConfig.szS
9aa60 63 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 cratch>=100.
9aa70 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 && sqlite3Glob
9aa80 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 alConfig.nScratc
9aa90 68 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 h>=0 ){. int
9aaa0 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c i;. sqlite3Gl
9aab0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9aac0 61 74 63 68 20 2d 3d 20 34 3b 0a 20 20 20 20 6d atch -= 4;. m
9aad0 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 em0.aScratchFree
9aae0 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 61 72 = (u32*)&((char
9aaf0 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 *)sqlite3GlobalC
9ab00 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 0a onfig.pScratch).
9ab10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ab20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c [sqlite3Global
9ab30 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 Config.szScratch
9ab40 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f *sqlite3GlobalCo
9ab50 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a nfig.nScratch];.
9ab60 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 for(i=0; i<s
9ab70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ab80 69 67 2e 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b ig.nScratch; i++
9ab90 29 7b 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 ){ mem0.aScratch
9aba0 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 Free[i] = i; }.
9abb0 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 mem0.nScratch
9abc0 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c Free = sqlite3Gl
9abd0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
9abe0 74 63 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 tch;. }else{.
9abf0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
9ac00 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d onfig.pScratch =
9ac10 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 0;. sqlite3G
9ac20 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 lobalConfig.szSc
9ac30 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 ratch = 0;. }.
9ac40 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9ac50 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 26 alConfig.pPage &
9ac60 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 & sqlite3GlobalC
9ac70 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31 onfig.szPage>=51
9ac80 32 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 2. && sqlit
9ac90 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9aca0 50 61 67 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69 Page>=1 ){. i
9acb0 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76 nt i;. int ov
9acc0 65 72 68 65 61 64 3b 0a 20 20 20 20 69 6e 74 20 erhead;. int
9acd0 73 7a 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 sz = sqlite3Glob
9ace0 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3b alConfig.szPage;
9acf0 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c . int n = sql
9ad00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9ad10 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72 .nPage;. over
9ad20 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a head = (4*n + sz
9ad30 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71 - 1)/sz;. sq
9ad40 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9ad50 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68 g.nPage -= overh
9ad60 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50 ead;. mem0.aP
9ad70 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29 ageFree = (u32*)
9ad80 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 &((char*)sqlite3
9ad90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 GlobalConfig.pPa
9ada0 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 ge).
9adb0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c [sqlite3Gl
9adc0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 obalConfig.szPag
9add0 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 e*sqlite3GlobalC
9ade0 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20 onfig.nPage];.
9adf0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c for(i=0; i<sql
9ae00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9ae10 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65 .nPage; i++){ me
9ae20 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20 m0.aPageFree[i]
9ae30 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e = i; }. mem0.
9ae40 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69 nPageFree = sqli
9ae50 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9ae60 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a nPage;. }else{.
9ae70 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9ae80 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 lConfig.pPage =
9ae90 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 0;. sqlite3Gl
9aea0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 obalConfig.szPag
9aeb0 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 e = 0;. }. ret
9aec0 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 urn sqlite3Globa
9aed0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28 lConfig.m.xInit(
9aee0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9aef0 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b fig.m.pAppData);
9af00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
9af10 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 ialize the memor
9af20 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
9af30 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 system..*/.SQLIT
9af40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
9af50 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 qlite3MallocEnd(
9af60 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 void){. sqlite3
9af70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9af80 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 Shutdown(sqlite3
9af90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 GlobalConfig.m.p
9afa0 41 70 70 44 61 74 61 29 3b 0a 20 20 6d 65 6d 73 AppData);. mems
9afb0 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a et(&mem0, 0, siz
9afc0 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f eof(mem0));.}../
9afd0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
9afe0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
9aff0 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b currently check
9b000 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 ed out..*/.SQLIT
9b010 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e E_API sqlite3_in
9b020 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f t64 sqlite3_memo
9b030 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 ry_used(void){.
9b040 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 int n, mx;. sq
9b050 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b lite3_int64 res;
9b060 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 . sqlite3_statu
9b070 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f s(SQLITE_STATUS_
9b080 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c MEMORY_USED, &n,
9b090 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 &mx, 0);. res
9b0a0 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 = (sqlite3_int64
9b0b0 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f )n; /* Work aro
9b0c0 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 und bug in Borla
9b0d0 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 nd C. Ticket #32
9b0e0 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 16 */. return r
9b0f0 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 es;.}../*.** Ret
9b100 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 urn the maximum
9b110 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
9b120 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62 that has ever b
9b130 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f een.** checked o
9b140 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 ut since either
9b150 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
9b160 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a this process.**
9b170 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f or since the mo
9b180 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e st recent reset.
9b190 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
9b1a0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c qlite3_int64 sql
9b1b0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
9b1c0 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 water(int resetF
9b1d0 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d lag){. int n, m
9b1e0 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 x;. sqlite3_int
9b1f0 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 64 res;. sqlite
9b200 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 3_status(SQLITE_
9b210 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
9b220 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 ED, &n, &mx, res
9b230 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d etFlag);. res =
9b240 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
9b250 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f mx; /* Work aro
9b260 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 und bug in Borla
9b270 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 nd C. Ticket #32
9b280 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 16 */. return r
9b290 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 es;.}../*.** Cha
9b2a0 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61 nge the alarm ca
9b2b0 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 llback.*/.SQLITE
9b2c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
9b2d0 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 ite3MemoryAlarm(
9b2e0 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 . void(*xCallba
9b2f0 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 ck)(void *pArg,
9b300 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 sqlite3_int64 us
9b310 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 ed,int N),. voi
9b320 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 d *pArg,. sqlit
9b330 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 e3_int64 iThresh
9b340 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 old.){. sqlite3
9b350 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
9b360 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 0.mutex);. mem0
9b370 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d .alarmCallback =
9b380 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 xCallback;. me
9b390 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 m0.alarmArg = pA
9b3a0 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d rg;. mem0.alarm
9b3b0 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 Threshold = iThr
9b3c0 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 eshold;. sqlite
9b3d0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9b3e0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 m0.mutex);. ret
9b3f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
9b400 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
9b410 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 _OMIT_DEPRECATED
9b420 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 ./*.** Deprecate
9b430 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 d external inter
9b440 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f face. Internal/
9b450 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 core SQLite code
9b460 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 .** should call
9b470 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 sqlite3MemoryAla
9b480 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 rm..*/.SQLITE_AP
9b490 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 I int sqlite3_me
9b4a0 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f mory_alarm(. vo
9b4b0 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 id(*xCallback)(v
9b4c0 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 oid *pArg, sqlit
9b4d0 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e e3_int64 used,in
9b4e0 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 t N),. void *pA
9b4f0 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e rg,. sqlite3_in
9b500 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 t64 iThreshold.)
9b510 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
9b520 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 e3MemoryAlarm(xC
9b530 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 allback, pArg, i
9b540 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 Threshold);.}.#e
9b550 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 ndif../*.** Trig
9b560 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a ger the alarm .*
9b570 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
9b580 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d lite3MallocAlarm
9b590 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 (int nByte){. v
9b5a0 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 oid (*xCallback)
9b5b0 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 (void*,sqlite3_i
9b5c0 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c nt64,int);. sql
9b5d0 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 ite3_int64 nowUs
9b5e0 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 ed;. void *pArg
9b5f0 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 ;. if( mem0.ala
9b600 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 7c 7c rmCallback==0 ||
9b610 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 mem0.alarmBusy
9b620 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d ) return;. mem
9b630 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 31 3b 0.alarmBusy = 1;
9b640 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d . xCallback = m
9b650 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
9b660 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 k;. nowUsed = s
9b670 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 qlite3StatusValu
9b680 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e(SQLITE_STATUS_
9b690 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 MEMORY_USED);.
9b6a0 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 pArg = mem0.alar
9b6b0 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f mArg;. sqlite3_
9b6c0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9b6d0 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c .mutex);. xCall
9b6e0 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 back(pArg, nowUs
9b6f0 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 ed, nByte);. sq
9b700 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
9b710 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 r(mem0.mutex);.
9b720 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 mem0.alarmBusy
9b730 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f = 0;.}../*.** Do
9b740 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
9b750 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74 69 73 tion with statis
9b760 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e tics and alarms.
9b770 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20 Assume the.**
9b780 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 lock is already
9b790 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 held..*/.static
9b7a0 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c int mallocWithAl
9b7b0 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20 arm(int n, void
9b7c0 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75 **pp){. int nFu
9b7d0 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 ll;. void *p;.
9b7e0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
9b7f0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 _mutex_held(mem0
9b800 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 46 75 .mutex) );. nFu
9b810 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ll = sqlite3Glob
9b820 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e alConfig.m.xRoun
9b830 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 dup(n);. sqlite
9b840 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 3StatusSet(SQLIT
9b850 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f E_STATUS_MALLOC_
9b860 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20 SIZE, n);. if(
9b870 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
9b880 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 ck!=0 ){. int
9b890 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 nUsed = sqlite3
9b8a0 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 StatusValue(SQLI
9b8b0 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 TE_STATUS_MEMORY
9b8c0 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66 28 20 _USED);. if(
9b8d0 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d nUsed+nFull >= m
9b8e0 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f em0.alarmThresho
9b8f0 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ld ){. sqli
9b900 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e te3MallocAlarm(n
9b910 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Full);. }. }
9b920 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c . p = sqlite3Gl
9b930 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 obalConfig.m.xMa
9b940 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69 lloc(nFull);. i
9b950 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e f( p==0 && mem0.
9b960 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b alarmCallback ){
9b970 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c . sqlite3Mall
9b980 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a ocAlarm(nFull);.
9b990 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 p = sqlite3G
9b9a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d lobalConfig.m.xM
9b9b0 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 alloc(nFull);.
9b9c0 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 }. if( p ){.
9b9d0 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 nFull = sqlite3
9b9e0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 MallocSize(p);.
9b9f0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9ba00 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
9ba10 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e S_MEMORY_USED, n
9ba20 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 Full);. }. *pp
9ba30 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e = p;. return n
9ba40 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 Full;.}../*.** A
9ba50 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20 llocate memory.
9ba60 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
9ba70 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61 like sqlite3_ma
9ba80 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20 74 68 lloc() except th
9ba90 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73 at it.** assumes
9baa0 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 the memory subs
9bab0 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65 61 64 ystem has alread
9bac0 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a y been initializ
9bad0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
9bae0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
9baf0 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 te3Malloc(int n)
9bb00 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 {. void *p;. i
9bb10 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 70 f( n<=0 ){. p
9bb20 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
9bb30 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9bb40 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
9bb50 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
9bb60 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
9bb70 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f utex);. mallo
9bb80 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 cWithAlarm(n, &p
9bb90 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
9bba0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9bbb0 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b mutex);. }else{
9bbc0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
9bbd0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9bbe0 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 Malloc(n);. }.
9bbf0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
9bc00 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
9bc10 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
9bc20 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 llocation is for
9bc30 20 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c use by the appl
9bc40 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 ication..** Firs
9bc50 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 t make sure the
9bc60 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d memory subsystem
9bc70 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c is initialized,
9bc80 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 then do the.**
9bc90 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 allocation..*/.S
9bca0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
9bcb0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 sqlite3_malloc(i
9bcc0 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 nt n){.#ifndef S
9bcd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
9bce0 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 NIT. if( sqlite
9bcf0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 3_initialize() )
9bd00 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
9bd10 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 f. return sqlit
9bd20 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a e3Malloc(n);.}..
9bd30 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61 /*.** Each threa
9bd40 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 d may only have
9bd50 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e a single outstan
9bd60 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 ding allocation
9bd70 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 from.** xScratch
9bd80 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 Malloc(). We ve
9bd90 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 rify this constr
9bda0 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67 aint in the sing
9bdb0 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 le-threaded.** c
9bdc0 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 20 73 ase by setting s
9bdd0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 cratchAllocOut t
9bde0 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f o 1 when an allo
9bdf0 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 cation.** is out
9be00 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e standing clearin
9be10 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c g it when the al
9be20 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
9be30 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 d..*/.#if SQLITE
9be40 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 _THREADSAFE==0 &
9be50 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 & !defined(NDEBU
9be60 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 G).static int sc
9be70 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 ratchAllocOut =
9be80 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 0;.#endif.../*.*
9be90 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
9bea0 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 y that is to be
9beb0 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 used and release
9bec0 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a d right away..**
9bed0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
9bee0 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f similar to allo
9bef0 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 74 20 ca() in that it
9bf00 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a is not intended.
9bf10 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e ** for situation
9bf20 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f s where the memo
9bf30 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 ry might be held
9bf40 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 long-term. Thi
9bf50 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 s.** routine is
9bf60 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20 intended to get
9bf70 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 memory to old la
9bf80 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61 rge transient da
9bf90 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 ta.** structures
9bfa0 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 that would not
9bfb0 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 normally fit on
9bfc0 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a the stack of an.
9bfd0 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63 ** embedded proc
9bfe0 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 essor..*/.SQLITE
9bff0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
9c000 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c qlite3ScratchMal
9c010 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f loc(int n){. vo
9c020 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 id *p;. assert(
9c030 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c n>0 );..#if SQL
9c040 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
9c050 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 0 && !defined(ND
9c060 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 EBUG). /* Verif
9c070 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 y that no more t
9c080 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 han one scratch
9c090 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 allocation per t
9c0a0 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 hread. ** is ou
9c0b0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 tstanding at one
9c0c0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 time. (This is
9c0d0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e only checked in
9c0e0 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 the. ** single
9c0f0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73 -threaded case s
9c100 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e ince checking in
9c110 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 the multi-threa
9c120 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f ded case. ** wo
9c130 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 uld be much more
9c140 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a complicated.) *
9c150 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 72 61 /. assert( scra
9c160 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 tchAllocOut==0 )
9c170 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 ;.#endif.. if(
9c180 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9c190 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 fig.szScratch<n
9c1a0 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61 ){. goto scra
9c1b0 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 tch_overflow;.
9c1c0 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c }else{ . sql
9c1d0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9c1e0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9c1f0 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 if( mem0.nScra
9c200 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 tchFree==0 ){.
9c210 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9c220 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9c230 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ex);. goto
9c240 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 scratch_overflow
9c250 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
9c260 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
9c270 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 i = mem0.aScratc
9c280 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 hFree[--mem0.nSc
9c290 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 ratchFree];.
9c2a0 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c i *= sqlite3Gl
9c2b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9c2c0 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 atch;. sqli
9c2d0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9c2e0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
9c2f0 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 CH_USED, 1);.
9c300 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9c310 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Set(SQLITE_STATU
9c320 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 S_SCRATCH_SIZE,
9c330 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 n);. sqlite
9c340 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9c350 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
9c360 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 p = (void*)&((c
9c370 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 har*)sqlite3Glob
9c380 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9c390 68 29 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d h)[i];. }. }
9c3a0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
9c3b0 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 ADSAFE==0 && !de
9c3c0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 fined(NDEBUG).
9c3d0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 scratchAllocOut
9c3e0 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a = p!=0;.#endif..
9c3f0 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 return p;..scr
9c400 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 atch_overflow:.
9c410 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9c420 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 alConfig.bMemsta
9c430 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
9c440 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
9c450 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 0.mutex);. sq
9c460 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 lite3StatusSet(S
9c470 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
9c480 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 ATCH_SIZE, n);.
9c490 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 n = mallocWit
9c4a0 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 hAlarm(n, &p);.
9c4b0 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74 if( p ) sqlit
9c4c0 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 e3StatusAdd(SQLI
9c4d0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
9c4e0 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a H_OVERFLOW, n);.
9c4f0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9c500 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9c510 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ex);. }else{.
9c520 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f p = sqlite3Glo
9c530 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c balConfig.m.xMal
9c540 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 loc(n);. }.#if
9c550 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
9c560 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 E==0 && !defined
9c570 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 (NDEBUG). scrat
9c580 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d chAllocOut = p!=
9c590 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 0;.#endif. retu
9c5a0 72 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 rn p; .}.SQLI
9c5b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9c5c0 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 sqlite3ScratchFr
9c5d0 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 ee(void *p){. i
9c5e0 66 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c f( p ){..#if SQL
9c5f0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
9c600 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 0 && !defined(ND
9c610 45 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 EBUG). /* Ver
9c620 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 ify that no more
9c630 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 than one scratc
9c640 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 h allocation per
9c650 20 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 thread. ** i
9c660 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 s outstanding at
9c670 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 one time. (Thi
9c680 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 s is only checke
9c690 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 d in the. **
9c6a0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 single-threaded
9c6b0 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b case since check
9c6c0 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 ing in the multi
9c6d0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 -threaded case.
9c6e0 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d ** would be m
9c6f0 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 uch more complic
9c700 61 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 ated.) */. as
9c710 73 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c sert( scratchAll
9c720 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 ocOut==1 );.
9c730 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 scratchAllocOut
9c740 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 = 0;.#endif..
9c750 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9c760 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9c770 68 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 h==0.
9c780 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 || p<sqlite3Glob
9c790 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9c7a0 68 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 h. ||
9c7b0 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 p>=(void*)mem0.a
9c7c0 53 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a 20 ScratchFree ){.
9c7d0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
9c7e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
9c7f0 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 20 mstat ){.
9c800 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c int iSize = sql
9c810 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
9c820 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
9c830 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
9c840 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9c850 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
9c860 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
9c870 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 US_SCRATCH_OVERF
9c880 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 LOW, -iSize);.
9c890 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 sqlite3Sta
9c8a0 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
9c8b0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
9c8c0 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 , -iSize);.
9c8d0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
9c8e0 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 Config.m.xFree(p
9c8f0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
9c900 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
9c910 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9c920 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
9c930 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9c940 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b nfig.m.xFree(p);
9c950 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
9c960 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b se{. int i;
9c970 0a 20 20 20 20 20 20 69 20 3d 20 28 75 38 20 2a . i = (u8 *
9c980 29 70 20 2d 20 28 75 38 20 2a 29 73 71 6c 69 74 )p - (u8 *)sqlit
9c990 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9c9a0 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 69 Scratch;. i
9c9b0 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 /= sqlite3Globa
9c9c0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
9c9d0 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 h;. assert(
9c9e0 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74 i>=0 && i<sqlit
9c9f0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9ca00 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 Scratch );.
9ca10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9ca20 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9ca30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
9ca40 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 mem0.nScratchFre
9ca50 65 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 e<sqlite3GlobalC
9ca60 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20 29 onfig.nScratch )
9ca70 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 61 53 63 ;. mem0.aSc
9ca80 72 61 74 63 68 46 72 65 65 5b 6d 65 6d 30 2e 6e ratchFree[mem0.n
9ca90 53 63 72 61 74 63 68 46 72 65 65 2b 2b 5d 20 3d ScratchFree++] =
9caa0 20 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 i;. sqlite
9cab0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
9cac0 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
9cad0 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 _USED, -1);.
9cae0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9caf0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
9cb00 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
9cb10 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d /*.** Allocate m
9cb20 65 6d 6f 72 79 20 74 6f 20 62 65 20 75 73 65 64 emory to be used
9cb30 20 62 79 20 74 68 65 20 70 61 67 65 20 63 61 63 by the page cac
9cb40 68 65 2e 20 20 4d 61 6b 65 20 75 73 65 20 6f 66 he. Make use of
9cb50 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 62 the.** memory b
9cb60 75 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 62 uffer provided b
9cb70 79 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f y SQLITE_CONFIG_
9cb80 50 41 47 45 43 41 43 48 45 20 69 66 20 74 68 65 PAGECACHE if the
9cb90 72 65 20 69 73 20 6f 6e 65 0a 2a 2a 20 61 6e 64 re is one.** and
9cba0 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 69 73 20 that memory is
9cbb0 6f 66 20 74 68 65 20 72 69 67 68 74 20 73 69 7a of the right siz
9cbc0 65 20 61 6e 64 20 69 73 20 6e 6f 74 20 63 6f 6d e and is not com
9cbd0 70 6c 65 74 65 6c 79 0a 2a 2a 20 63 6f 6e 73 75 pletely.** consu
9cbe0 6d 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 2c med. Otherwise,
9cbf0 20 66 61 69 6c 6f 76 65 72 20 74 6f 20 73 71 6c failover to sql
9cc00 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f ite3Malloc()..*/
9cc10 0a 23 69 66 20 30 0a 53 51 4c 49 54 45 5f 50 52 .#if 0.SQLITE_PR
9cc20 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
9cc30 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e te3PageMalloc(in
9cc40 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b t n){. void *p;
9cc50 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 . assert( n>0 )
9cc60 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 20 26 ;. assert( (n &
9cc70 20 28 6e 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 (n-1))==0 );.
9cc80 61 73 73 65 72 74 28 20 6e 3e 3d 35 31 32 20 26 assert( n>=512 &
9cc90 26 20 6e 3c 3d 33 32 37 36 38 20 29 3b 0a 0a 20 & n<=32768 );..
9cca0 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9ccb0 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3c alConfig.szPage<
9ccc0 6e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 61 n ){. goto pa
9ccd0 67 65 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d ge_overflow;. }
9cce0 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 else{ . sqli
9ccf0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9cd00 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9cd10 20 69 66 28 20 6d 65 6d 30 2e 6e 50 61 67 65 46 if( mem0.nPageF
9cd20 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ree==0 ){.
9cd30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
9cd40 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ave(mem0.mutex);
9cd50 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 . goto page
9cd60 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 7d _overflow;. }
9cd70 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 else{. int
9cd80 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 6d 65 6d i;. i = mem
9cd90 30 2e 61 50 61 67 65 46 72 65 65 5b 2d 2d 6d 65 0.aPageFree[--me
9cda0 6d 30 2e 6e 50 61 67 65 46 72 65 65 5d 3b 0a 20 m0.nPageFree];.
9cdb0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
9cdc0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
9cdd0 74 65 78 29 3b 0a 20 20 20 20 20 20 69 20 2a 3d tex);. i *=
9cde0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9cdf0 6e 66 69 67 2e 73 7a 50 61 67 65 3b 0a 20 20 20 nfig.szPage;.
9ce00 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9ce10 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Set(SQLITE_STATU
9ce20 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 S_PAGECACHE_SIZE
9ce30 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , n);. sqli
9ce40 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9ce50 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
9ce60 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a 20 ACHE_USED, 1);.
9ce70 20 20 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 p = (void*)
9ce80 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 &((char*)sqlite3
9ce90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 GlobalConfig.pPa
9cea0 67 65 29 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 ge)[i];. }.
9ceb0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 70 }. return p;..p
9cec0 61 67 65 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 age_overflow:.
9ced0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9cee0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
9cef0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
9cf00 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
9cf10 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c .mutex);. sql
9cf20 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 ite3StatusSet(SQ
9cf30 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
9cf40 43 41 43 48 45 5f 53 49 5a 45 2c 20 6e 29 3b 0a CACHE_SIZE, n);.
9cf50 20 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 n = mallocWi
9cf60 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a thAlarm(n, &p);.
9cf70 20 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 if( p ) sqli
9cf80 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9cf90 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
9cfa0 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e ACHE_OVERFLOW, n
9cfb0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
9cfc0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9cfd0 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b mutex);. }else{
9cfe0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
9cff0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9d000 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 Malloc(n);. }.
9d010 20 72 65 74 75 72 6e 20 70 3b 20 20 20 20 0a 7d return p; .}
9d020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9d030 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
9d040 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 Free(void *p){.
9d050 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 if( p ){. if
9d060 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9d070 6f 6e 66 69 67 2e 70 50 61 67 65 3d 3d 30 0a 20 onfig.pPage==0.
9d080 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c 73 || p<s
9d090 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9d0a0 69 67 2e 70 50 61 67 65 0a 20 20 20 20 20 20 20 ig.pPage.
9d0b0 20 20 20 20 7c 7c 20 70 3e 3d 28 76 6f 69 64 2a || p>=(void*
9d0c0 29 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 20 )mem0.aPageFree
9d0d0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 ){. /* In t
9d0e0 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 70 61 his case, the pa
9d0f0 67 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 ge allocation wa
9d100 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
9d110 61 20 72 65 67 75 6c 61 72 20 0a 20 20 20 20 20 a regular .
9d120 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ** call to sqli
9d130 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2e te3_mem_methods.
9d140 78 4d 61 6c 6c 6f 63 28 29 20 28 61 20 70 61 67 xMalloc() (a pag
9d150 65 2d 63 61 63 68 65 2d 6d 65 6d 6f 72 79 20 0a e-cache-memory .
9d160 20 20 20 20 20 20 2a 2a 20 22 6f 76 65 72 66 6c ** "overfl
9d170 6f 77 22 29 2e 20 46 72 65 65 20 74 68 65 20 62 ow"). Free the b
9d180 6c 6f 63 6b 20 77 69 74 68 20 73 71 6c 69 74 65 lock with sqlite
9d190 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2e 78 46 3_mem_methods.xF
9d1a0 72 65 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a ree().. */.
9d1b0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
9d1c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 3GlobalConfig.bM
9d1d0 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 emstat ){.
9d1e0 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 int iSize = sq
9d1f0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
9d200 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 p);. sqli
9d210 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9d220 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9d230 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
9d240 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
9d250 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
9d260 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b ERFLOW, -iSize);
9d270 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
9d280 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 StatusAdd(SQLITE
9d290 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
9d2a0 53 45 44 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 SED, -iSize);.
9d2b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f sqlite3Glo
9d2c0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 balConfig.m.xFre
9d2d0 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 e(p);. sq
9d2e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
9d2f0 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
9d300 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
9d310 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9d320 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 lConfig.m.xFree(
9d330 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
9d340 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
9d350 54 68 65 20 70 61 67 65 20 61 6c 6c 6f 63 61 74 The page allocat
9d360 69 6f 6e 20 77 61 73 20 61 6c 6c 6f 63 61 74 65 ion was allocate
9d370 64 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 d from the sqlit
9d380 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9d390 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 75 Page. ** bu
9d3a0 66 66 65 72 2e 20 49 6e 20 74 68 69 73 20 63 61 ffer. In this ca
9d3b0 73 65 20 61 6c 6c 20 74 68 61 74 20 69 73 20 61 se all that is a
9d3c0 64 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 dd the index of
9d3d0 74 68 65 20 70 61 67 65 20 69 6e 0a 20 20 20 20 the page in.
9d3e0 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 ** the sqlite3
9d3f0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 GlobalConfig.pPa
9d400 67 65 20 61 72 72 61 79 20 74 6f 20 74 68 65 20 ge array to the
9d410 73 65 74 20 6f 66 20 66 72 65 65 20 69 6e 64 65 set of free inde
9d420 78 65 73 20 73 74 6f 72 65 64 0a 20 20 20 20 20 xes stored.
9d430 20 2a 2a 20 69 6e 20 74 68 65 20 6d 65 6d 30 2e ** in the mem0.
9d440 61 50 61 67 65 46 72 65 65 5b 5d 20 61 72 72 61 aPageFree[] arra
9d450 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 y.. */.
9d460 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 int i;. i
9d470 20 3d 20 28 75 38 20 2a 29 70 20 2d 20 28 75 38 = (u8 *)p - (u8
9d480 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c *)sqlite3Global
9d490 43 6f 6e 66 69 67 2e 70 50 61 67 65 3b 0a 20 20 Config.pPage;.
9d4a0 20 20 20 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 i /= sqlite3
9d4b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 GlobalConfig.szP
9d4c0 61 67 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 age;. asser
9d4d0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c t( i>=0 && i<sql
9d4e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d4f0 2e 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 .nPage );.
9d500 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
9d510 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ter(mem0.mutex);
9d520 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d . assert( m
9d530 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 3c 73 71 em0.nPageFree<sq
9d540 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9d550 67 2e 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 20 g.nPage );.
9d560 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b mem0.aPageFree[
9d570 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 2b 2b mem0.nPageFree++
9d580 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 71 6c ] = i;. sql
9d590 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
9d5a0 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
9d5b0 43 41 43 48 45 5f 55 53 45 44 2c 20 2d 31 29 3b CACHE_USED, -1);
9d5c0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
9d5d0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9d5e0 6d 75 74 65 78 29 3b 0a 23 69 66 20 21 64 65 66 mutex);.#if !def
9d5f0 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 ined(NDEBUG) &&
9d600 30 0a 20 20 20 20 20 20 2f 2a 20 41 73 73 65 72 0. /* Asser
9d610 74 20 74 68 61 74 20 61 20 64 75 70 6c 69 63 61 t that a duplica
9d620 74 65 20 77 61 73 20 6e 6f 74 20 6a 75 73 74 20 te was not just
9d630 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 50 inserted into aP
9d640 61 67 65 46 72 65 65 5b 5d 2e 20 2a 2f 0a 20 20 ageFree[]. */.
9d650 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6d for(i=0; i<m
9d660 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 2d 31 3b em0.nPageFree-1;
9d670 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 i++){. a
9d680 73 73 65 72 74 28 20 6d 65 6d 30 2e 61 50 61 67 ssert( mem0.aPag
9d690 65 46 72 65 65 5b 69 5d 21 3d 6d 65 6d 30 2e 61 eFree[i]!=mem0.a
9d6a0 50 61 67 65 46 72 65 65 5b 6d 65 6d 30 2e 6e 50 PageFree[mem0.nP
9d6b0 61 67 65 46 72 65 65 2d 31 5d 20 29 3b 0a 20 20 ageFree-1] );.
9d6c0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
9d6d0 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a }. }.}.#endif.
9d6e0 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 69 66 20 70 ./*.** TRUE if p
9d6f0 20 69 73 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 is a lookaside
9d700 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
9d710 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f 0a 23 69 66 n from db.*/.#if
9d720 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
9d730 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 74 61 74 69 _LOOKASIDE.stati
9d740 63 20 69 6e 74 20 69 73 4c 6f 6f 6b 61 73 69 64 c int isLookasid
9d750 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 e(sqlite3 *db, v
9d760 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 oid *p){. retur
9d770 6e 20 64 62 20 26 26 20 70 20 26 26 20 70 3e 3d n db && p && p>=
9d780 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 db->lookaside.pS
9d790 74 61 72 74 20 26 26 20 70 3c 64 62 2d 3e 6c 6f tart && p<db->lo
9d7a0 6f 6b 61 73 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a okaside.pEnd;.}.
9d7b0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 69 73 #else.#define is
9d7c0 4c 6f 6f 6b 61 73 69 64 65 28 41 2c 42 29 20 30 Lookaside(A,B) 0
9d7d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
9d7e0 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f eturn the size o
9d7f0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
9d800 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 ation previously
9d810 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
9d820 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 * sqlite3Malloc(
9d830 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6d 61 6c ) or sqlite3_mal
9d840 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 loc()..*/.SQLITE
9d850 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
9d860 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 ite3MallocSize(v
9d870 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 oid *p){. retur
9d880 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 n sqlite3GlobalC
9d890 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 onfig.m.xSize(p)
9d8a0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
9d8b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 62 TE int sqlite3Db
9d8c0 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74 MallocSize(sqlit
9d8d0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 e3 *db, void *p)
9d8e0 7b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 {. if( isLookas
9d8f0 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 ide(db, p) ){.
9d900 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f return db->loo
9d910 6b 61 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c kaside.sz;. }el
9d920 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 se{. return s
9d930 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9d940 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 20 ig.m.xSize(p);.
9d950 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 }.}../*.** Free
9d960 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f 75 73 memory previous
9d970 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ly obtained from
9d980 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 sqlite3Malloc()
9d990 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
9d9a0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 void sqlite3_fre
9d9b0 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 e(void *p){. if
9d9c0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( p==0 ) return;
9d9d0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
9d9e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 obalConfig.bMems
9d9f0 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 tat ){. sqlit
9da00 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
9da10 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9da20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
9da30 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9da40 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 73 71 6c EMORY_USED, -sql
9da50 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
9da60 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ));. sqlite3G
9da70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 lobalConfig.m.xF
9da80 72 65 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 ree(p);. sqli
9da90 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9daa0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d mem0.mutex);. }
9dab0 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
9dac0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9dad0 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a xFree(p);. }.}.
9dae0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f ./*.** Free memo
9daf0 72 79 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 ry that might be
9db00 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
9db10 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 a particular da
9db20 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
9db30 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
9db40 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
9db50 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 ite3DbFree(sqlit
9db60 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 e3 *db, void *p)
9db70 7b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 {. if( isLookas
9db80 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 ide(db, p) ){.
9db90 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 LookasideSlot
9dba0 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 *pBuf = (Lookasi
9dbb0 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 deSlot*)p;. p
9dbc0 42 75 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d Buf->pNext = db-
9dbd0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 >lookaside.pFree
9dbe0 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 ;. db->lookas
9dbf0 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 ide.pFree = pBuf
9dc00 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 ;. db->lookas
9dc10 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 ide.nOut--;. }e
9dc20 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
9dc30 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a _free(p);. }.}.
9dc40 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
9dc50 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 e size of an exi
9dc60 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c sting memory all
9dc70 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 ocation.*/.SQLIT
9dc80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
9dc90 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 sqlite3Realloc(v
9dca0 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e oid *pOld, int n
9dcb0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f Bytes){. int nO
9dcc0 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 ld, nNew;. void
9dcd0 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f *pNew;. if( pO
9dce0 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ld==0 ){. ret
9dcf0 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f urn sqlite3Mallo
9dd00 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 c(nBytes);. }.
9dd10 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 if( nBytes<=0 )
9dd20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
9dd30 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20 20 72 65 ee(pOld);. re
9dd40 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f turn 0;. }. nO
9dd50 6c 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ld = sqlite3Mall
9dd60 6f 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a 20 20 ocSize(pOld);.
9dd70 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9dd80 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
9dd90 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
9dda0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
9ddb0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c .mutex);. sql
9ddc0 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 ite3StatusSet(SQ
9ddd0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c LITE_STATUS_MALL
9dde0 4f 43 5f 53 49 5a 45 2c 20 6e 42 79 74 65 73 29 OC_SIZE, nBytes)
9ddf0 3b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c ;. nNew = sql
9de00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9de10 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 42 79 74 .m.xRoundup(nByt
9de20 65 73 29 3b 0a 20 20 20 20 69 66 28 20 6e 4f 6c es);. if( nOl
9de30 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20 20 d==nNew ){.
9de40 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20 pNew = pOld;.
9de50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
9de60 66 28 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 f( sqlite3Status
9de70 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 Value(SQLITE_STA
9de80 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 TUS_MEMORY_USED)
9de90 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 +nNew-nOld >= .
9dea0 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 30 2e mem0.
9deb0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 alarmThreshold )
9dec0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
9ded0 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e 65 3MallocAlarm(nNe
9dee0 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 20 20 7d w-nOld);. }
9def0 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 . pNew = sq
9df00 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9df10 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c g.m.xRealloc(pOl
9df20 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 d, nNew);.
9df30 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20 6d if( pNew==0 && m
9df40 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
9df50 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c k ){. sql
9df60 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 ite3MallocAlarm(
9df70 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 20 nBytes);.
9df80 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 pNew = sqlite3G
9df90 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
9dfa0 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 ealloc(pOld, nNe
9dfb0 77 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 w);. }.
9dfc0 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 if( pNew ){.
9dfd0 20 20 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c nNew = sql
9dfe0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
9dff0 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20 73 71 New);. sq
9e000 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
9e010 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
9e020 4f 52 59 5f 55 53 45 44 2c 20 6e 4e 65 77 2d 6e ORY_USED, nNew-n
9e030 4f 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Old);. }.
9e040 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
9e050 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9e060 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 .mutex);. }else
9e070 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c {. pNew = sql
9e080 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9e090 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 .m.xRealloc(pOld
9e0a0 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 , nBytes);. }.
9e0b0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
9e0c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 62 6c 69 ./*.** The publi
9e0d0 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 73 c interface to s
9e0e0 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e 20 20 qlite3Realloc.
9e0f0 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 Make sure that t
9e100 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 75 62 he memory.** sub
9e110 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 system is initia
9e120 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f 20 69 lized prior to i
9e130 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 52 65 nvoking sqliteRe
9e140 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 alloc..*/.SQLITE
9e150 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
9e160 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20 e3_realloc(void
9e170 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0a 23 *pOld, int n){.#
9e180 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
9e190 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 IT_AUTOINIT. if
9e1a0 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 ( sqlite3_initia
9e1b0 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 lize() ) return
9e1c0 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 0;.#endif. retu
9e1d0 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f rn sqlite3Reallo
9e1e0 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a c(pOld, n);.}...
9e1f0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /*.** Allocate a
9e200 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a nd zero memory..
9e210 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */ .SQLITE_PRIVA
9e220 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
9e230 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20 6e MallocZero(int n
9e240 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 ){. void *p = s
9e250 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b qlite3Malloc(n);
9e260 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
9e270 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b memset(p, 0, n);
9e280 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
9e290 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
9e2a0 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f te and zero memo
9e2b0 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f ry. If the allo
9e2c0 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 cation fails, ma
9e2d0 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 ke.** the malloc
9e2e0 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 Failed flag in t
9e2f0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f he connection po
9e300 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 inter..*/.SQLITE
9e310 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
9e320 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
9e330 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ro(sqlite3 *db,
9e340 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a int n){. void *
9e350 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c p = sqlite3DbMal
9e360 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 locRaw(db, n);.
9e370 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 if( p ){. me
9e380 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 mset(p, 0, n);.
9e390 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
9e3a0 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
9e3b0 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 and zero memory
9e3c0 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 . If the alloca
9e3d0 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 tion fails, make
9e3e0 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 .** the mallocFa
9e3f0 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 iled flag in the
9e400 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e connection poin
9e410 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 62 ter..**.** If db
9e420 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c !=0 and db->mall
9e430 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72 75 65 ocFailed is true
9e440 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61 20 70 (indicating a p
9e450 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 rior malloc.** f
9e460 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20 73 61 ailure on the sa
9e470 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
9e480 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61 6c 77 ection) then alw
9e490 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a ays return 0..**
9e4a0 20 48 65 6e 63 65 20 66 6f 72 20 61 20 70 61 72 Hence for a par
9e4b0 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
9e4c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 6e 63 connection, onc
9e4d0 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 74 73 0a e malloc starts.
9e4e0 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 74 20 66 ** failing, it f
9e4f0 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 6e 74 6c ails consistentl
9e500 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 46 61 y until mallocFa
9e510 69 6c 65 64 20 69 73 20 72 65 73 65 74 2e 0a 2a iled is reset..*
9e520 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6d 70 * This is an imp
9e530 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 74 69 6f ortant assumptio
9e540 6e 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 n. There are ma
9e550 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 74 68 65 ny places in the
9e560 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 20 64 6f .** code that do
9e570 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 68 69 things like thi
9e580 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 s:.**.**
9e590 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a 29 int *a = (int*)
9e5a0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
9e5b0 61 77 28 64 62 2c 20 31 30 30 29 3b 0a 2a 2a 20 aw(db, 100);.**
9e5c0 20 20 20 20 20 20 20 20 69 6e 74 20 2a 62 20 3d int *b =
9e5d0 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 (int*)sqlite3Db
9e5e0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32 30 MallocRaw(db, 20
9e5f0 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 0);.** i
9e600 66 28 20 62 20 29 20 61 5b 31 30 5d 20 3d 20 39 f( b ) a[10] = 9
9e610 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 ;.**.** In other
9e620 20 77 6f 72 64 73 2c 20 69 66 20 61 20 73 75 62 words, if a sub
9e630 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 28 sequent malloc (
9e640 65 78 3a 20 22 62 22 29 20 77 6f 72 6b 65 64 2c ex: "b") worked,
9e650 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a it is assumed.*
9e660 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 69 6f 72 * that all prior
9e670 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a 20 22 61 mallocs (ex: "a
9e680 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f 2e 0a 2a ") worked too..*
9e690 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9e6a0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
9e6b0 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 MallocRaw(sqlite
9e6c0 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 3 *db, int n){.
9e6d0 20 76 6f 69 64 20 2a 70 3b 0a 23 69 66 6e 64 65 void *p;.#ifnde
9e6e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
9e6f0 4f 4b 41 53 49 44 45 0a 20 20 69 66 28 20 64 62 OKASIDE. if( db
9e700 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 ){. Lookasid
9e710 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 20 eSlot *pBuf;.
9e720 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
9e730 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 ailed ){. r
9e740 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
9e750 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 if( db->looka
9e760 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 26 26 side.bEnabled &&
9e770 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 n<=db->lookasid
9e780 65 2e 73 7a 0a 20 20 20 20 20 20 20 20 20 26 26 e.sz. &&
9e790 20 28 70 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f (pBuf = db->loo
9e7a0 6b 61 73 69 64 65 2e 70 46 72 65 65 29 21 3d 30 kaside.pFree)!=0
9e7b0 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f ){. db->lo
9e7c0 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 okaside.pFree =
9e7d0 70 42 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pBuf->pNext;.
9e7e0 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 db->lookaside
9e7f0 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 69 .nOut++;. i
9e800 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 f( db->lookaside
9e810 2e 6e 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 .nOut>db->lookas
9e820 69 64 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20 ide.mxOut ){.
9e830 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 db->lookasi
9e840 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c de.mxOut = db->l
9e850 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 ookaside.nOut;.
9e860 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
9e870 75 72 6e 20 28 76 6f 69 64 2a 29 70 42 75 66 3b urn (void*)pBuf;
9e880 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 . }. }.#else
9e890 0a 20 20 69 66 28 20 64 62 20 26 26 20 64 62 2d . if( db && db-
9e8a0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
9e8b0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
9e8c0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 20 3d 20 }.#endif. p =
9e8d0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 sqlite3Malloc(n)
9e8e0 3b 0a 20 20 69 66 28 20 21 70 20 26 26 20 64 62 ;. if( !p && db
9e8f0 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c ){. db->mall
9e900 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
9e910 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
9e920 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 ./*.** Resize th
9e930 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 e block of memor
9e940 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 y pointed to by
9e950 70 20 74 6f 20 6e 20 62 79 74 65 73 2e 20 49 66 p to n bytes. If
9e960 20 74 68 65 0a 2a 2a 20 72 65 73 69 7a 65 20 66 the.** resize f
9e970 61 69 6c 73 2c 20 73 65 74 20 74 68 65 20 6d 61 ails, set the ma
9e980 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
9e990 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f in the connectio
9e9a0 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c n object..*/.SQL
9e9b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9e9c0 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c *sqlite3DbReall
9e9d0 6f 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 oc(sqlite3 *db,
9e9e0 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b void *p, int n){
9e9f0 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d 20 . void *pNew =
9ea00 30 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 0;. if( db->mal
9ea10 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a locFailed==0 ){.
9ea20 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a if( p==0 ){.
9ea30 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c return sql
9ea40 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
9ea50 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 db, n);. }.
9ea60 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 if( isLookasid
9ea70 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 e(db, p) ){.
9ea80 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f if( n<=db->loo
9ea90 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a 20 20 20 kaside.sz ){.
9eaa0 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 return p;.
9eab0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 }. pNe
9eac0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
9ead0 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 locRaw(db, n);.
9eae0 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b if( pNew ){
9eaf0 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
9eb00 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f pNew, p, db->loo
9eb10 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 20 20 20 kaside.sz);.
9eb20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
9eb30 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 e(db, p);.
9eb40 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
9eb50 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 pNew = sqlite
9eb60 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 3_realloc(p, n);
9eb70 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 . if( !pNew
9eb80 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e ){. db->
9eb90 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
9eba0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
9ebb0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
9ebc0 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 w;.}../*.** Atte
9ebd0 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74 mpt to reallocat
9ebe0 65 20 70 2e 20 20 49 66 20 74 68 65 20 72 65 61 e p. If the rea
9ebf0 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
9ec00 20 74 68 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20 then free p.**
9ec10 61 6e 64 20 73 65 74 20 74 68 65 20 6d 61 6c 6c and set the mall
9ec20 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e ocFailed flag in
9ec30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
9ec40 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c nnection..*/.SQL
9ec50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9ec60 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c *sqlite3DbReall
9ec70 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33 ocOrFree(sqlite3
9ec80 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 *db, void *p, i
9ec90 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 nt n){. void *p
9eca0 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 New;. pNew = sq
9ecb0 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 lite3DbRealloc(d
9ecc0 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 b, p, n);. if(
9ecd0 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 73 71 6c !pNew ){. sql
9ece0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
9ecf0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
9ed00 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d pNew;.}../*.** M
9ed10 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61 20 ake a copy of a
9ed20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 string in memory
9ed30 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
9ed40 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 qliteMalloc(). T
9ed50 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f hese .** functio
9ed60 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d ns call sqlite3M
9ed70 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 72 65 63 allocRaw() direc
9ed80 74 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 20 73 tly instead of s
9ed90 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 qliteMalloc(). T
9eda0 68 69 73 0a 2a 2a 20 69 73 20 62 65 63 61 75 73 his.** is becaus
9edb0 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 64 65 e when memory de
9edc0 62 75 67 67 69 6e 67 20 69 73 20 74 75 72 6e 65 bugging is turne
9edd0 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 77 6f 20 d on, these two
9ede0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a functions are .*
9edf0 2a 20 63 61 6c 6c 65 64 20 76 69 61 20 6d 61 63 * called via mac
9ee00 72 6f 73 20 74 68 61 74 20 72 65 63 6f 72 64 20 ros that record
9ee10 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 6c 65 the current file
9ee20 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d 62 65 72 and line number
9ee30 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68 72 65 61 in the.** Threa
9ee40 64 44 61 74 61 20 73 74 72 75 63 74 75 72 65 2e dData structure.
9ee50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9ee60 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
9ee70 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 DbStrDup(sqlite3
9ee80 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
9ee90 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e *z){. char *zN
9eea0 65 77 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 3b 0a ew;. size_t n;.
9eeb0 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 if( z==0 ){.
9eec0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
9eed0 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 2b n = strlen(z)+
9eee0 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 1;. assert( (n&
9eef0 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 0x7fffffff)==n )
9ef00 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 ;. zNew = sqlit
9ef10 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
9ef20 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28 , (int)n);. if(
9ef30 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d zNew ){. mem
9ef40 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b cpy(zNew, z, n);
9ef50 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e . }. return zN
9ef60 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 ew;.}.SQLITE_PRI
9ef70 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
9ef80 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 e3DbStrNDup(sqli
9ef90 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 te3 *db, const c
9efa0 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a har *z, int n){.
9efb0 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 char *zNew;.
9efc0 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 if( z==0 ){.
9efd0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
9efe0 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 assert( (n&0x7ff
9eff0 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a fffff)==n );. z
9f000 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
9f010 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31 allocRaw(db, n+1
9f020 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b );. if( zNew ){
9f030 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 . memcpy(zNew
9f040 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 , z, n);. zNe
9f050 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 w[n] = 0;. }.
9f060 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a return zNew;.}..
9f070 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73 /*.** Create a s
9f080 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a tring from the z
9f090 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 Fromat argument
9f0a0 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20 and the va_list
9f0b0 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a that follows..**
9f0c0 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e Store the strin
9f0d0 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 g in memory obta
9f0e0 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
9f0f0 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b Malloc() and mak
9f100 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74 e *pz.** point t
9f110 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a o that string..*
9f120 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9f130 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 void sqlite3Set
9f140 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a String(char **pz
9f150 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 , sqlite3 *db, c
9f160 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
9f170 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
9f180 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a ist ap;. char *
9f190 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61 z;.. va_start(a
9f1a0 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a p, zFormat);. z
9f1b0 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
9f1c0 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 tf(db, zFormat,
9f1d0 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
9f1e0 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
9f1f0 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a ee(db, *pz);. *
9f200 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a pz = z;.}.../*.*
9f210 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
9f220 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 must be called b
9f230 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e efore exiting an
9f240 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 y API function (
9f250 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69 i.e. .** returni
9f260 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 ng control to th
9f270 65 20 75 73 65 72 29 20 74 68 61 74 20 68 61 73 e user) that has
9f280 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f called sqlite3_
9f290 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c malloc or.** sql
9f2a0 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a ite3_realloc..**
9f2b0 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 .** The returned
9f2c0 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c value is normal
9f2d0 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ly a copy of the
9f2e0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
9f2f0 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 to this.** func
9f300 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69 tion. However, i
9f310 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
9f320 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 65 64 lure has occured
9f330 20 73 69 6e 63 65 20 74 68 65 20 70 72 65 76 69 since the previ
9f340 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f ous.** invocatio
9f350 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 n SQLITE_NOMEM i
9f360 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 s returned inste
9f370 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ad. .**.** If th
9f380 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
9f390 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 4c , db, is not NUL
9f3a0 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 29 L and a malloc()
9f3b0 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 error has occur
9f3c0 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 ed,.** then the
9f3d0 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 6f 72 connection error
9f3e0 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c 75 65 -code (the value
9f3f0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
9f400 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 29 0a ite3_errcode()).
9f410 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c ** is set to SQL
9f420 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 ITE_NOMEM..*/.SQ
9f430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
9f440 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 sqlite3ApiExit(
9f450 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 6e 74 sqlite3* db, int
9f460 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68 rc){. /* If th
9f470 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73 20 6e e db handle is n
9f480 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65 ot NULL, then we
9f490 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63 must hold the c
9f4a0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 onnection handle
9f4b0 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72 65 . ** mutex here
9f4c0 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 . Otherwise the
9f4d0 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69 62 read (and possib
9f4e0 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64 62 2d le write) of db-
9f4f0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0a 20 >mallocFailed .
9f500 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20 61 ** is unsafe, a
9f510 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74 6f s is the call to
9f520 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e sqlite3Error().
9f530 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
9f540 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d !db || sqlite3_m
9f550 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
9f560 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62 tex) );. if( db
9f570 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 && (db->mallocF
9f580 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 51 4c ailed || rc==SQL
9f590 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29 ITE_IOERR_NOMEM)
9f5a0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
9f5b0 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f rror(db, SQLITE_
9f5c0 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64 NOMEM, 0);. d
9f5d0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
9f5e0 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 = 0;. rc = SQ
9f5f0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
9f600 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 28 64 return rc & (d
9f610 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 b ? db->errMask
9f620 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a : 0xff);.}../***
9f630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
9f640 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a of malloc.c ****
9f650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
9f680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
9f690 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63 20 n file printf.c
9f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
9f6d0 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22 20 63 * The "printf" c
9f6e0 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 ode that follows
9f6f0 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20 dates from the
9f700 31 39 38 30 27 73 2e 20 20 49 74 20 69 73 20 69 1980's. It is i
9f710 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 20 n.** the public
9f720 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 domain. The ori
9f730 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 ginal comments a
9f740 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65 re included here
9f750 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 for.** complete
9f760 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 20 ness. They are
9f770 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65 very out-of-date
9f780 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 73 but might be us
9f790 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69 eful as.** an hi
9f7a0 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65 6e storical referen
9f7b0 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 ce. Most of the
9f7c0 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20 "enhancements"
9f7d0 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65 64 have been backed
9f7e0 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74 20 .** out so that
9f7f0 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 the functionalit
9f800 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61 6d y is now the sam
9f810 65 20 61 73 20 73 74 61 6e 64 61 72 64 20 70 72 e as standard pr
9f820 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 24 49 intf()..**.** $I
9f830 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20 31 2e d: printf.c,v 1.
9f840 39 34 20 32 30 30 38 2f 30 38 2f 32 32 20 31 34 94 2008/08/22 14
9f850 3a 30 38 3a 33 36 20 64 72 68 20 45 78 70 20 24 :08:36 drh Exp $
9f860 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
9f870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
9f8b0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
9f8c0 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 61 6e ng modules is an
9f8d0 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c 61 63 enhanced replac
9f8e0 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 22 70 ement for the "p
9f8f0 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 69 6e rintf" subroutin
9f900 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20 74 es.** found in t
9f910 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 he standard C li
9f920 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f 6c 6c brary. The foll
9f930 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d 65 6e owing enhancemen
9f940 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 6f 72 ts are.** suppor
9f950 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ted:.**.**
9f960 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 75 + Additional fu
9f970 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 73 74 nctions. The st
9f980 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 22 70 andard set of "p
9f990 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f 6e 73 rintf" functions
9f9a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 63 6c .** incl
9f9b0 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 70 72 udes printf, fpr
9f9c0 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c 20 76 intf, sprintf, v
9f9d0 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e 74 66 printf, vfprintf
9f9e0 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 , and.**
9f9f0 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 69 73 vsprintf. This
9fa00 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 68 65 module adds the
9fa10 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
9fa20 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 73 * * s
9fa30 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 6b 73 nprintf -- Works
9fa40 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c 20 62 like sprintf, b
9fa50 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 ut has an extra
9fa60 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 argument.**
9fa70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9fa80 20 20 20 20 20 77 68 69 63 68 20 69 73 20 74 68 which is th
9fa90 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 e size of the bu
9faa0 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 6f 2e ffer written to.
9fab0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
9fac0 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d 20 20 * mprintf --
9fad0 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 69 6e Similar to sprin
9fae0 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 74 70 tf. Writes outp
9faf0 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ut to memory.**
9fb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9fb10 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65 obtaine
9fb20 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a d from malloc..*
9fb30 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
9fb40 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 43 61 xprintf -- Ca
9fb50 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 lls a function t
9fb60 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f 75 74 o dispose of out
9fb70 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 put..**.**
9fb80 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 66 20 * nprintf
9fb90 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c 20 62 -- No output, b
9fba0 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e ut returns the n
9fbb0 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
9fbc0 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ers.**
9fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9fbe0 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 20 that would have
9fbf0 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 20 70 been output by p
9fc00 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 rintf..**.**
9fc10 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d 20 76 * A v- v
9fc20 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 6e 70 ersion (ex: vsnp
9fc30 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 79 20 rintf) of every
9fc40 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f function is also
9fc50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9fc60 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a 2a 2a supplied..**.**
9fc70 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 20 65 + A few e
9fc80 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 68 65 xtensions to the
9fc90 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f 74 61 formatting nota
9fca0 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f 72 74 tion are support
9fcb0 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 ed:.**.**
9fcc0 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 20 66 * The "=" f
9fcd0 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 lag (similar to
9fce0 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 65 20 "-") causes the
9fcf0 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a 2a 20 output to be.**
9fd00 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20 be
9fd10 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 65 20 centered in the
9fd20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 73 69 appropriately si
9fd30 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a zed field..**.**
9fd40 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 * Th
9fd50 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 70 75 e %b field outpu
9fd60 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e ts an integer in
9fd70 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 6f 6e binary notation
9fd80 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..**.**
9fd90 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 65 6c * The %c fiel
9fda0 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 61 20 d now accepts a
9fdb0 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 65 20 precision. The
9fdc0 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 75 74 character output
9fdd0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9fde0 20 69 73 20 72 65 70 65 61 74 65 64 20 62 79 20 is repeated by
9fdf0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 the number of ti
9fe00 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 69 6f mes the precisio
9fe10 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a 2a 0a n specifies..**.
9fe20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 ** *
9fe30 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 6f 72 The %' field wor
9fe40 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 74 20 ks like %c, but
9fe50 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 68 61 takes as its cha
9fe60 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 20 20 racter the.**
9fe70 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 20 next
9fe80 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 character of the
9fe90 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c 20 format string,
9fea0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e instead of the n
9feb0 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ext.**
9fec0 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 20 46 argument. F
9fed0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 72 69 or example, pri
9fee0 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 20 70 ntf("%.78'-") p
9fef0 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 0a 2a rints 78 minus.*
9ff00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 * s
9ff10 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 20 61 igns, the same a
9ff20 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 63 s printf("%.78c
9ff30 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 ",'-')..**.**
9ff40 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d 70 69 + When compi
9ff50 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 6f 6e led using GCC on
9ff60 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 20 76 a SPARC, this v
9ff70 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 ersion of printf
9ff80 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 is.** f
9ff90 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 20 6c aster than the l
9ffa0 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 66 6f ibrary printf fo
9ffb0 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a 2a 2a r SUN OS 4.1..**
9ffc0 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c 6c 20 .** + All
9ffd0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 66 75 functions are fu
9ffe0 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e 0a 2a lly reentrant..*
9fff0 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 *.*/../*.** Conv
a0000 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 61 6c ersion types fal
a0010 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 20 63 l into various c
a0020 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 65 66 ategories as def
a0030 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 66 ined by the.** f
a0040 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 72 61 ollowing enumera
a0050 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tion..*/.#define
a0060 20 65 74 52 41 44 49 58 20 20 20 20 20 20 20 31 etRADIX 1
a0070 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 70 65 /* Integer type
a0080 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f 2c 20 s. %d, %x, %o,
a0090 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a 2f 0a and so forth */.
a00a0 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 54 20 #define etFLOAT
a00b0 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f 61 74 2 /* Float
a00c0 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 20 2a ing point. %f *
a00d0 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 50 20 /.#define etEXP
a00e0 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 78 70 3 /* Exp
a00f0 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 61 74 onentional notat
a0100 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 20 2a ion. %e and %E *
a0110 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 4e 45 /.#define etGENE
a0120 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 6c 6f RIC 4 /* Flo
a0130 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e 65 6e ating or exponen
a0140 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 tial, depending
a0150 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 67 20 on exponent. %g
a0160 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 49 5a */.#define etSIZ
a0170 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 52 65 E 5 /* Re
a0180 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63 turn number of c
a0190 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 65 73 haracters proces
a01a0 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e 20 2a sed so far. %n *
a01b0 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 52 49 /.#define etSTRI
a01c0 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 74 72 NG 6 /* Str
a01d0 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 65 66 ings. %s */.#def
a01e0 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e 47 20 ine etDYNSTRING
a01f0 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 61 6c 7 /* Dynamical
a0200 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 ly allocated str
a0210 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 65 66 ings. %z */.#def
a0220 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 20 20 ine etPERCENT
a0230 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 20 73 8 /* Percent s
a0240 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 64 65 ymbol. %% */.#de
a0250 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 20 20 fine etCHARX
a0260 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 74 65 9 /* Characte
a0270 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 68 65 rs. %c */./* The
a0280 20 72 65 73 74 20 61 72 65 20 65 78 74 65 6e 73 rest are extens
a0290 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61 6c ions, not normal
a02a0 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 69 6e ly found in prin
a02b0 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tf() */.#define
a02c0 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 30 20 etSQLESCAPE 10
a02d0 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 /* Strings with
a02e0 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 20 25 '\'' doubled. %
a02f0 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 q */.#define etS
a0300 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f 2a 20 QLESCAPE2 11 /*
a0310 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27 Strings with '\'
a0320 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 65 6e ' doubled and en
a0330 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a 20 20 closed in '',.
a0340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0350 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 6f 69 NULL poi
a0360 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 20 62 nters replaced b
a0370 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 51 20 y SQL NULL. %Q
a0380 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 4f 4b */.#define etTOK
a0390 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 61 20 EN 12 /* a
a03a0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 6f 6b pointer to a Tok
a03b0 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a en structure */.
a03c0 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c 49 53 #define etSRCLIS
a03d0 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 6f 69 T 13 /* a poi
a03e0 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c 69 73 nter to a SrcLis
a03f0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 50 t */.#define etP
a0400 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f 2a 20 OINTER 14 /*
a0410 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 69 6f The %p conversio
a0420 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 n */.#define etS
a0430 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f 2a 20 QLESCAPE3 15 /*
a0440 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 77 69 %w -> Strings wi
a0450 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 64 20 th '\"' doubled
a0460 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f 52 44 */.#define etORD
a0470 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 25 72 INAL 16 /* %r
a0480 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 33 72 -> 1st, 2nd, 3r
a0490 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 45 6e d, 4th, etc. En
a04a0 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a glish only */...
a04b0 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 74 65 /*.** An "etByte
a04c0 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 75 6e " is an 8-bit un
a04d0 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a 2a 2f signed value..*/
a04e0 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 .typedef unsigne
a04f0 64 20 63 68 61 72 20 65 74 42 79 74 65 3b 0a 0a d char etByte;..
a0500 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 6c 74 /*.** Each built
a0510 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 in conversion ch
a0520 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 68 65 aracter (ex: the
a0530 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 69 73 'd' in "%d") is
a0540 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 described.** by
a0550 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
a0560 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
a0570 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 ructure.*/.typed
a0580 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 6e 66 ef struct et_inf
a0590 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 o { /* Informa
a05a0 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 tion about each
a05b0 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a 2f 0a format field */.
a05c0 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 3b 20 char fmttype;
a05d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
a05e0 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 63 e format field c
a05f0 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a 20 20 ode letter */.
a0600 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 20 20 etByte base;
a0610 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
a0620 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 20 63 base for radix c
a0630 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 65 onversion */. e
a0640 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 20 20 tByte flags;
a0650 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
a0660 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 5f 20 r more of FLAG_
a0670 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 constants below
a0680 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 70 65 */. etByte type
a0690 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
a06a0 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 Conversion para
a06b0 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 74 65 digm */. etByte
a06c0 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 20 20 charset;
a06d0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
a06e0 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 20 74 o aDigits[] of t
a06f0 68 65 20 64 69 67 69 74 73 20 73 74 72 69 6e 67 he digits string
a0700 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 72 65 */. etByte pre
a0710 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f fix; /
a0720 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 50 * Offset into aP
a0730 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 20 70 refix[] of the p
a0740 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a 2f 0a refix string */.
a0750 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a } et_info;../*.*
a0760 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 * Allowed values
a0770 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 6c 61 for et_info.fla
a0780 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c gs.*/.#define FL
a0790 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 20 20 AG_SIGNED 1
a07a0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
a07b0 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 72 74 value to convert
a07c0 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a 23 64 is signed */.#d
a07d0 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 45 52 efine FLAG_INTER
a07e0 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 75 65 N 2 /* True
a07f0 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c if for internal
a0800 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 use only */.#de
a0810 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 4e 47 fine FLAG_STRING
a0820 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 4 /* Allow
a0830 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 69 73 infinity precis
a0840 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 ion */.../*.** T
a0850 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 he following tab
a0860 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 20 6c le is searched l
a0870 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 20 69 inearly, so it i
a0880 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 74 68 s good to put th
a0890 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 75 65 e.** most freque
a08a0 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 65 72 ntly used conver
a08b0 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 73 74 sion types first
a08c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
a08d0 74 20 63 68 61 72 20 61 44 69 67 69 74 73 5b 5d t char aDigits[]
a08e0 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 41 42 = "0123456789AB
a08f0 43 44 45 46 30 31 32 33 34 35 36 37 38 39 61 62 CDEF0123456789ab
a0900 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 63 6f cdef";.static co
a0910 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 69 78 nst char aPrefix
a0920 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 30 22 [] = "-x0\000X0"
a0930 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 65 ;.static const e
a0940 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b 5d t_info fmtinfo[]
a0950 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c 20 31 = {. { 'd', 1
a0960 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 0, 1, etRADIX,
a0970 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 0, 0 },. {
a0980 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 65 74 's', 0, 4, et
a0990 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c 20 20 STRING, 0,
a09a0 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 20 20 0 },. { 'g',
a09b0 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 0, 1, etGENERIC,
a09c0 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 30, 0 },. {
a09d0 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 65 74 'z', 0, 4, et
a09e0 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c 20 20 DYNSTRING, 0,
a09f0 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 20 20 0 },. { 'q',
a0a00 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50 0, 4, etSQLESCAP
a0a10 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b E, 0, 0 },. {
a0a20 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 65 74 'Q', 0, 4, et
a0a30 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c 20 20 SQLESCAPE2, 0,
a0a40 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 20 20 0 },. { 'w',
a0a50 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50 0, 4, etSQLESCAP
a0a60 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b E3, 0, 0 },. {
a0a70 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 65 74 'c', 0, 0, et
a0a80 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c 20 20 CHARX, 0,
a0a90 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 20 20 0 },. { 'o',
a0aa0 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 8, 0, etRADIX,
a0ab0 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 20 7b 0, 2 },. {
a0ac0 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 65 74 'u', 10, 0, et
a0ad0 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 RADIX, 0,
a0ae0 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 20 31 0 },. { 'x', 1
a0af0 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 6, 0, etRADIX,
a0b00 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 20 7b 16, 1 },. {
a0b10 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 65 74 'X', 16, 0, et
a0b20 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 RADIX, 0,
a0b30 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 4 },.#ifndef SQL
a0b40 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
a0b50 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 66 27 G_POINT. { 'f'
a0b60 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f 41 54 , 0, 1, etFLOAT
a0b70 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a , 0, 0 },.
a0b80 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 31 2c { 'e', 0, 1,
a0b90 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20 33 etEXP, 3
a0ba0 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 45 27 0, 0 },. { 'E'
a0bb0 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 , 0, 1, etEXP,
a0bc0 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 14, 0 },.
a0bd0 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 31 2c { 'G', 0, 1,
a0be0 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 31 etGENERIC, 1
a0bf0 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 4, 0 },.#endif.
a0c00 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 2c 20 { 'i', 10, 1,
a0c10 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
a0c20 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 27 2c 0 },. { 'n',
a0c30 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 2c 20 0, 0, etSIZE,
a0c40 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
a0c50 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 2c 20 { '%', 0, 0,
a0c60 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 30 2c etPERCENT, 0,
a0c70 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 27 2c 0 },. { 'p',
a0c80 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e 54 45 16, 0, etPOINTE
a0c90 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c 0a 20 R, 0, 1 },.
a0ca0 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 2c 20 { 'T', 0, 2,
a0cb0 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 30 2c etTOKEN, 0,
a0cc0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 27 2c 0 },. { 'S',
a0cd0 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c 49 53 0, 2, etSRCLIS
a0ce0 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 T, 0, 0 },.
a0cf0 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 2c 20 { 'r', 10, 3,
a0d00 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 30 2c etORDINAL, 0,
a0d10 20 20 30 20 7d 2c 0a 7d 3b 0a 23 64 65 66 69 6e 0 },.};.#defin
a0d20 65 20 65 74 4e 49 4e 46 4f 20 20 28 73 69 7a 65 e etNINFO (size
a0d30 6f 66 28 66 6d 74 69 6e 66 6f 29 2f 73 69 7a 65 of(fmtinfo)/size
a0d40 6f 66 28 66 6d 74 69 6e 66 6f 5b 30 5d 29 29 0a of(fmtinfo[0])).
a0d50 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 ./*.** If SQLITE
a0d60 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
a0d70 4f 49 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c OINT is defined,
a0d80 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 then none of th
a0d90 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 e floating point
a0da0 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 .** conversions
a0db0 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 will work..*/.#i
a0dc0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
a0dd0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
a0de0 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69 73 ./*.** "*val" is
a0df0 20 61 20 64 6f 75 62 6c 65 20 73 75 63 68 20 74 a double such t
a0e00 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 hat 0.1 <= *val
a0e10 3c 20 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72 6e < 10.0.** Return
a0e20 20 74 68 65 20 61 73 63 69 69 20 63 6f 64 65 20 the ascii code
a0e30 66 6f 72 20 74 68 65 20 6c 65 61 64 69 6e 67 20 for the leading
a0e40 64 69 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20 74 digit of *val, t
a0e50 68 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 hen.** multiply
a0e60 22 2a 76 61 6c 22 20 62 79 20 31 30 2e 30 20 74 "*val" by 10.0 t
a0e70 6f 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a o renormalize..*
a0e80 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a *.** Example:.**
a0e90 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 input:
a0ea0 2a 76 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a 2a *val = 3.14159.*
a0eb0 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20 * output:
a0ec0 20 2a 76 61 6c 20 3d 20 31 2e 34 31 35 39 20 20 *val = 1.4159
a0ed0 20 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 function retur
a0ee0 6e 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 n = '3'.**.** Th
a0ef0 65 20 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20 69 e counter *cnt i
a0f00 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 s incremented ea
a0f10 63 68 20 74 69 6d 65 2e 20 20 41 66 74 65 72 20 ch time. After
a0f20 63 6f 75 6e 74 65 72 20 65 78 63 65 65 64 73 0a counter exceeds.
a0f30 2a 2a 20 31 36 20 28 74 68 65 20 6e 75 6d 62 65 ** 16 (the numbe
a0f40 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 r of significant
a0f50 20 64 69 67 69 74 73 20 69 6e 20 61 20 36 34 2d digits in a 64-
a0f60 62 69 74 20 66 6c 6f 61 74 29 20 27 30 27 20 69 bit float) '0' i
a0f70 73 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 s.** always retu
a0f80 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rned..*/.static
a0f90 69 6e 74 20 65 74 5f 67 65 74 64 69 67 69 74 28 int et_getdigit(
a0fa0 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 LONGDOUBLE_TYPE
a0fb0 2a 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b *val, int *cnt){
a0fc0 0a 20 20 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 . int digit;.
a0fd0 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 LONGDOUBLE_TYPE
a0fe0 64 3b 0a 20 20 69 66 28 20 28 2a 63 6e 74 29 2b d;. if( (*cnt)+
a0ff0 2b 20 3e 3d 20 31 36 20 29 20 72 65 74 75 72 6e + >= 16 ) return
a1000 20 27 30 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 '0';. digit =
a1010 28 69 6e 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d (int)*val;. d =
a1020 20 64 69 67 69 74 3b 0a 20 20 64 69 67 69 74 20 digit;. digit
a1030 2b 3d 20 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d += '0';. *val =
a1040 20 28 2a 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 (*val - d)*10.0
a1050 3b 0a 20 20 72 65 74 75 72 6e 20 64 69 67 69 74 ;. return digit
a1060 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
a1070 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
a1080 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a NG_POINT */../*.
a1090 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 70 61 63 ** Append N spac
a10a0 65 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 e characters to
a10b0 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 the given string
a10c0 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 buffer..*/.stat
a10d0 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 53 70 ic void appendSp
a10e0 61 63 65 28 53 74 72 41 63 63 75 6d 20 2a 70 41 ace(StrAccum *pA
a10f0 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 ccum, int N){.
a1100 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
a1110 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d 20 22 20 r zSpaces[] = "
a1120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1130 20 20 20 20 20 20 20 20 20 20 20 20 22 3b 0a 20 ";.
a1140 20 77 68 69 6c 65 28 20 4e 3e 3d 73 69 7a 65 6f while( N>=sizeo
a1150 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b 0a f(zSpaces)-1 ){.
a1160 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a1170 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a1180 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 6f , zSpaces, sizeo
a1190 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a 20 f(zSpaces)-1);.
a11a0 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a N -= sizeof(z
a11b0 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 Spaces)-1;. }.
a11c0 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 if( N>0 ){.
a11d0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
a11e0 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 ppend(pAccum, zS
a11f0 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d paces, N);. }.}
a1200 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 ../*.** On machi
a1210 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c 6c nes with a small
a1220 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75 stack size, you
a1230 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74 68 can redefine th
a1240 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e e.** SQLITE_PRIN
a1250 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 65 T_BUF_SIZE to be
a1260 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30 2e 20 less than 350.
a1270 20 42 75 74 20 62 65 77 61 72 65 20 2d 20 66 6f But beware - fo
a1280 72 0a 2a 2a 20 73 6d 61 6c 6c 65 72 20 76 61 6c r.** smaller val
a1290 75 65 73 20 73 6f 6d 65 20 25 66 20 63 6f 6e 76 ues some %f conv
a12a0 65 72 73 69 6f 6e 73 20 6d 61 79 20 67 6f 20 69 ersions may go i
a12b0 6e 74 6f 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 nto an infinite
a12c0 6c 6f 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 loop..*/.#ifndef
a12d0 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 SQLITE_PRINT_BU
a12e0 46 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 F_SIZE.# define
a12f0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
a1300 5f 53 49 5a 45 20 33 35 30 0a 23 65 6e 64 69 66 _SIZE 350.#endif
a1310 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46 53 49 .#define etBUFSI
a1320 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f ZE SQLITE_PRINT_
a1330 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69 7a BUF_SIZE /* Siz
a1340 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 e of the output
a1350 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a buffer */../*.**
a1360 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 72 61 The root progra
a1370 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 69 6f m. All variatio
a1380 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63 6f 72 ns call this cor
a1390 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a e..**.** INPUTS:
a13a0 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 68 69 .** func Thi
a13b0 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 s is a pointer t
a13c0 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b o a function tak
a13d0 69 6e 67 20 74 68 72 65 65 20 61 72 67 75 6d 65 ing three argume
a13e0 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 nts.**
a13f0 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 1. A pointer t
a1400 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 61 6d o anything. Sam
a1410 65 20 61 73 20 74 68 65 20 22 61 72 67 22 20 70 e as the "arg" p
a1420 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 20 arameter..**
a1430 20 20 20 20 20 20 20 20 32 2e 20 41 20 70 6f 69 2. A poi
a1440 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 nter to the list
a1450 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74 of characters t
a1460 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 o be output.**
a1470 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f (No
a1480 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20 69 73 te, this list is
a1490 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69 6e NOT null termin
a14a0 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20 20 ated.).**
a14b0 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 65 67 3. An integ
a14c0 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 er number of cha
a14d0 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 racters to be ou
a14e0 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20 20 tput..**
a14f0 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 54 68 (Note: Th
a1500 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 74 20 is number might
a1510 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20 be zero.).**.**
a1520 20 20 61 72 67 20 20 20 20 54 68 69 73 20 69 73 arg This is
a1530 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 the pointer to
a1540 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68 20 77 anything which w
a1550 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 61 73 ill be passed as
a1560 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
a1570 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
a1580 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 65 20 to "func". Use
a1590 69 74 20 66 6f 72 20 77 68 61 74 65 76 65 72 20 it for whatever
a15a0 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20 you like..**.**
a15b0 20 20 66 6d 74 20 20 20 20 54 68 69 73 20 69 73 fmt This is
a15c0 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 the format stri
a15d0 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20 75 73 ng, as in the us
a15e0 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a ual print..**.**
a15f0 20 20 20 61 70 20 20 20 20 20 54 68 69 73 20 69 ap This i
a1600 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
a1610 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e list of argumen
a1620 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69 6e 0a ts. Same as in.
a1630 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66 70 72 ** vfpr
a1640 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55 int..**.** OUTPU
a1650 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 TS:.**
a1660 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
a1670 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 is the total nu
a1680 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
a1690 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 rs sent to.**
a16a0 20 20 20 20 20 20 20 74 68 65 20 66 75 6e 63 74 the funct
a16b0 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 65 74 ion "func". Ret
a16c0 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72 72 urns -1 on a err
a16d0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 or..**.** Note t
a16e0 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 69 6e hat the order in
a16f0 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74 69 63 which automatic
a1700 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 64 variables are d
a1710 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a 2a eclared below.**
a1720 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20 61 seems to make a
a1730 20 62 69 67 20 64 69 66 66 65 72 65 6e 63 65 20 big difference
a1740 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 68 in determining h
a1750 6f 77 20 66 61 73 74 20 74 68 69 73 20 62 65 61 ow fast this bea
a1760 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a st.** will run..
a1770 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a1780 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 E void sqlite3VX
a1790 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41 63 63 Printf(. StrAcc
a17a0 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 20 20 um *pAccum,
a17b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a17c0 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73 75 6c Accumulate resul
a17d0 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 ts here */. int
a17e0 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20 20 20 useExtended,
a17f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1800 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 /* Allow extende
a1810 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 d %-conversions
a1820 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
a1830 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 *fmt,
a1840 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 /* Forma
a1850 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 t string */. va
a1860 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20 20 20 _list ap
a1870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1880 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f /* arguments */
a1890 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 .){. int c;
a18a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a18b0 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 63 74 /* Next charact
a18c0 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 er in the format
a18d0 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 string */. cha
a18e0 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20 20 20 r *bufpt;
a18f0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
a1900 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 er to the conver
a1910 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 sion buffer */.
a1920 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 int precision;
a1930 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
a1940 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 recision of the
a1950 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f current field */
a1960 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 . int length;
a1970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a1980 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 Length of the f
a1990 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 ield */. int id
a19a0 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
a19b0 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 /* A genera
a19c0 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 l purpose loop c
a19d0 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
a19e0 77 69 64 74 68 3b 20 20 20 20 20 20 20 20 20 20 width;
a19f0 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74 68 20 /* Width
a1a00 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 of the current f
a1a10 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 ield */. etByte
a1a20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 flag_leftjustif
a1a30 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 y; /* True if
a1a40 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 "-" flag is pres
a1a50 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
a1a60 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20 flag_plussign;
a1a70 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 /* True if "
a1a80 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 +" flag is prese
a1a90 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 nt */. etByte f
a1aa0 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20 lag_blanksign;
a1ab0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 20 /* True if "
a1ac0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
a1ad0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
a1ae0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d ag_alternateform
a1af0 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22 23 22 ; /* True if "#"
a1b00 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
a1b10 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a1b20 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20 g_altform2;
a1b30 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21 22 20 /* True if "!"
a1b40 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 flag is present
a1b50 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 */. etByte flag
a1b60 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 20 20 _zeropad;
a1b70 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65 6c 64 /* True if field
a1b80 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e 74 20 width constant
a1b90 73 74 61 72 74 73 20 77 69 74 68 20 7a 65 72 6f starts with zero
a1ba0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a1bb0 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20 20 g_long;
a1bc0 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c 22 20 /* True if "l"
a1bd0 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 flag is present
a1be0 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 */. etByte flag
a1bf0 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20 _longlong;
a1c00 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 22 /* True if the "
a1c10 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 ll" flag is pres
a1c20 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
a1c30 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 done;
a1c40 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d /* Loop term
a1c50 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a ination flag */.
a1c60 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 sqlite_uint64
a1c70 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20 longvalue; /*
a1c80 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 67 65 Value for intege
a1c90 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e r types */. LON
a1ca0 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 65 61 GDOUBLE_TYPE rea
a1cb0 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65 lvalue; /* Value
a1cc0 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65 73 20 for real types
a1cd0 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e */. const et_in
a1ce0 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20 fo *infop;
a1cf0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
a1d00 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e e appropriate in
a1d10 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a fo structure */.
a1d20 20 20 63 68 61 72 20 62 75 66 5b 65 74 42 55 46 char buf[etBUF
a1d30 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 SIZE]; /*
a1d40 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 Conversion buffe
a1d50 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 65 66 r */. char pref
a1d60 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ix;
a1d70 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68 61 72 /* Prefix char
a1d80 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 20 22 acter. "+" or "
a1d90 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27 5c 30 -" or " " or '\0
a1da0 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 '. */. etByte x
a1db0 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 type;
a1dc0 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e /* Conversion
a1dd0 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 paradigm */. c
a1de0 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 20 har *zExtra;
a1df0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 /* Ext
a1e00 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 ra memory used f
a1e10 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 20 63 or etTCLESCAPE c
a1e20 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69 onversions */.#i
a1e30 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
a1e40 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
a1e50 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32 3b . int exp, e2;
a1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a1e70 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61 exponent of rea
a1e80 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64 l numbers */. d
a1e90 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20 ouble rounder;
a1ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
a1eb0 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66 d for rounding f
a1ec0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
a1ed0 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 65 lues */. etByte
a1ee0 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 20 flag_dp;
a1ef0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a1f00 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68 decimal point sh
a1f10 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f ould be shown */
a1f20 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 72 . etByte flag_r
a1f30 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a tz; /*
a1f40 20 54 72 75 65 20 69 66 20 74 72 61 69 6c 69 6e True if trailin
a1f50 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 62 g zeros should b
a1f60 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65 e removed */. e
a1f70 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b 20 tByte flag_exp;
a1f80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
a1f90 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 6c e to force displ
a1fa0 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e 65 ay of the expone
a1fb0 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b nt */. int nsd;
a1fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1fd0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
a1fe0 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 significant digi
a1ff0 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23 ts returned */.#
a2000 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20 endif.. length
a2010 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 30 = 0;. bufpt = 0
a2020 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66 ;. for(; (c=(*f
a2030 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b mt))!=0; ++fmt){
a2040 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 27 20 . if( c!='%'
a2050 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d 74 ){. int amt
a2060 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 ;. bufpt =
a2070 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20 (char *)fmt;.
a2080 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20 amt = 1;.
a2090 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b while( (c=(*++
a20a0 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63 21 fmt))!='%' && c!
a20b0 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20 =0 ) amt++;.
a20c0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
a20d0 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 mAppend(pAccum,
a20e0 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20 bufpt, amt);.
a20f0 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62 72 if( c==0 ) br
a2100 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
a2110 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d f( (c=(*++fmt))=
a2120 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
a2130 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a2140 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c 20 31 d(pAccum, "%", 1
a2150 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
a2160 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69 6e }. /* Fin
a2170 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61 67 73 d out what flags
a2180 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a 2f 0a are present */.
a2190 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 flag_leftjus
a21a0 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73 tify = flag_plus
a21b0 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e sign = flag_blan
a21c0 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 66 6c ksign = . fl
a21d0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d ag_alternateform
a21e0 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 = flag_altform2
a21f0 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 = flag_zeropad
a2200 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d 20 = 0;. done =
a2210 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 0;. do{.
a2220 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a 20 20 switch( c ){.
a2230 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 case '-':
a2240 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 flag_leftjusti
a2250 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72 65 61 fy = 1; brea
a2260 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
a2270 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75 73 '+': flag_plus
a2280 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 sign = 1;
a2290 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
a22a0 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c 61 67 case ' ': flag
a22b0 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b 20 _blanksign = 1;
a22c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a22d0 20 20 20 20 20 63 61 73 65 20 27 23 27 3a 20 20 case '#':
a22e0 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 flag_alternatef
a22f0 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 61 6b orm = 1; break
a2300 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
a2310 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f !': flag_altfo
a2320 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 20 20 rm2 = 1;
a2330 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
a2340 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61 67 5f ase '0': flag_
a2350 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 20 20 zeropad = 1;
a2360 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a2370 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 default:
a2380 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 20 20 done = 1;
a2390 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
a23a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 . }. }wh
a23b0 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 28 63 ile( !done && (c
a23c0 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29 3b =(*++fmt))!=0 );
a23d0 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 . /* Get the
a23e0 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a 20 field width */.
a23f0 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 width = 0;.
a2400 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a if( c=='*' ){.
a2410 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 76 61 width = va
a2420 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 _arg(ap,int);.
a2430 20 20 20 20 69 66 28 20 77 69 64 74 68 3c 30 20 if( width<0
a2440 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f ){. flag_
a2450 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b leftjustify = 1;
a2460 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d . width =
a2470 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 20 7d -width;. }
a2480 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d . c = *++fm
a2490 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
a24a0 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 while( c>='0
a24b0 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 ' && c<='9' ){.
a24c0 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 77 width = w
a24d0 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 27 30 idth*10 + c - '0
a24e0 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a ';. c = *
a24f0 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a 20 ++fmt;. }.
a2500 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 69 64 }. if( wid
a2510 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45 2d 31 th > etBUFSIZE-1
a2520 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68 0 ){. width
a2530 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 30 3b = etBUFSIZE-10;
a2540 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 . }. /* Ge
a2550 74 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20 t the precision
a2560 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2e */. if( c=='.
a2570 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69 ' ){. preci
a2580 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 sion = 0;.
a2590 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 c = *++fmt;.
a25a0 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a if( c=='*' ){.
a25b0 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f precisio
a25c0 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e n = va_arg(ap,in
a25d0 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 t);. if(
a25e0 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 precision<0 ) pr
a25f0 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69 ecision = -preci
a2600 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63 20 sion;. c
a2610 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 = *++fmt;.
a2620 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77 }else{. w
a2630 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 20 hile( c>='0' &&
a2640 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 c<='9' ){.
a2650 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 precision =
a2660 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20 63 precision*10 + c
a2670 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 - '0';.
a2680 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 c = *++fmt;.
a2690 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
a26a0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a26b0 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31 3b precision = -1;
a26c0 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 . }. /* Ge
a26d0 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e t the conversion
a26e0 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72 20 2a type modifier *
a26f0 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 /. if( c=='l'
a2700 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c ){. flag_l
a2710 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 ong = 1;. c
a2720 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 = *++fmt;.
a2730 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 if( c=='l' ){.
a2740 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 flag_long
a2750 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 long = 1;.
a2760 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 c = *++fmt;.
a2770 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a2780 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 flag_longlong
a2790 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
a27a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 }else{. f
a27b0 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f lag_long = flag_
a27c0 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 longlong = 0;.
a27d0 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63 68 }. /* Fetch
a27e0 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 79 20 the info entry
a27f0 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20 2a 2f for the field */
a2800 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 30 3b 0a . infop = 0;.
a2810 20 20 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 for(idx=0; i
a2820 64 78 3c 65 74 4e 49 4e 46 4f 3b 20 69 64 78 2b dx<etNINFO; idx+
a2830 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3d +){. if( c=
a2840 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d =fmtinfo[idx].fm
a2850 74 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 ttype ){.
a2860 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 infop = &fmtinf
a2870 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 o[idx];.
a2880 69 66 28 20 75 73 65 45 78 74 65 6e 64 65 64 20 if( useExtended
a2890 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 || (infop->flags
a28a0 20 26 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d & FLAG_INTERN)=
a28b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
a28c0 78 74 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 xtype = infop->t
a28d0 79 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ype;. }el
a28e0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 se{. re
a28f0 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a turn;. }.
a2900 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a2910 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
a2920 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 20 zExtra = 0;.
a2930 20 69 66 28 20 69 6e 66 6f 70 3d 3d 30 20 29 7b if( infop==0 ){
a2940 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
a2950 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 20 4c 69 }... /* Li
a2960 6d 69 74 20 74 68 65 20 70 72 65 63 69 73 69 6f mit the precisio
a2970 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 6f 76 65 n to prevent ove
a2980 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d 20 64 rflowing buf[] d
a2990 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e uring conversion
a29a0 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 72 65 63 */. if( prec
a29b0 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2d ision>etBUFSIZE-
a29c0 34 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e 66 6c 40 && (infop->fl
a29d0 61 67 73 20 26 20 46 4c 41 47 5f 53 54 52 49 4e ags & FLAG_STRIN
a29e0 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 G)==0 ){. p
a29f0 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 recision = etBUF
a2a00 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d 0a 0a SIZE-40;. }..
a2a10 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 41 74 /*. ** At
a2a20 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76 61 72 this point, var
a2a30 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69 74 69 iables are initi
a2a40 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c 6f 77 alized as follow
a2a50 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s:. **. **
a2a60 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 flag_alternat
a2a70 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20 20 54 eform T
a2a80 52 55 45 20 69 66 20 61 20 27 23 27 20 69 73 20 RUE if a '#' is
a2a90 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 present.. **
a2aa0 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 flag_altform2
a2ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 TR
a2ac0 55 45 20 69 66 20 61 20 27 21 27 20 69 73 20 70 UE if a '!' is p
a2ad0 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 resent.. **
a2ae0 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 20 flag_plussign
a2af0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 TRU
a2b00 45 20 69 66 20 61 20 27 2b 27 20 69 73 20 70 72 E if a '+' is pr
a2b10 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 esent.. **
a2b20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 flag_leftjustify
a2b30 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 TRUE
a2b40 20 69 66 20 61 20 27 2d 27 20 69 73 20 70 72 65 if a '-' is pre
a2b50 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65 0a 20 sent or if the.
a2b60 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
a2b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2b80 20 20 20 20 66 69 65 6c 64 20 77 69 64 74 68 20 field width
a2b90 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a 20 20 was negative..
a2ba0 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65 72 6f ** flag_zero
a2bb0 70 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 pad
a2bc0 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 77 TRUE if the w
a2bd0 69 64 74 68 20 62 65 67 61 6e 20 77 69 74 68 20 idth began with
a2be0 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 0.. ** flag
a2bf0 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 _long
a2c00 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 TRUE if
a2c10 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27 20 28 the letter 'l' (
a2c20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20 ell) prefixed.
a2c30 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 **
a2c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2c50 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f the conversio
a2c60 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 n character..
a2c70 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c ** flag_longl
a2c80 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 20 ong
a2c90 20 20 54 52 55 45 20 69 66 20 74 68 65 20 6c 65 TRUE if the le
a2ca0 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c 20 65 tter 'll' (ell e
a2cb0 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20 20 ll) prefixed.
a2cc0 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 **
a2cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2ce0 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e the conversion
a2cf0 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 character..
a2d00 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 ** flag_blanks
a2d10 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ign
a2d20 20 54 52 55 45 20 69 66 20 61 20 27 20 27 20 69 TRUE if a ' ' i
a2d30 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a s present.. *
a2d40 2a 20 20 20 77 69 64 74 68 20 20 20 20 20 20 20 * width
a2d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2d60 54 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 The specified fi
a2d70 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68 69 73 eld width. This
a2d80 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 is. **
a2d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2da0 20 20 20 20 20 20 20 20 20 61 6c 77 61 79 73 20 always
a2db0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 5a non-negative. Z
a2dc0 65 72 6f 20 69 73 20 74 68 65 20 64 65 66 61 75 ero is the defau
a2dd0 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70 72 65 lt.. ** pre
a2de0 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20 20 20 cision
a2df0 20 20 20 20 20 20 20 20 20 54 68 65 20 73 70 65 The spe
a2e00 63 69 66 69 65 64 20 70 72 65 63 69 73 69 6f 6e cified precision
a2e10 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 20 . The default.
a2e20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
a2e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2e40 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20 20 2a is -1.. *
a2e50 2a 20 20 20 78 74 79 70 65 20 20 20 20 20 20 20 * xtype
a2e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2e70 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74 68 65 The class of the
a2e80 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20 20 20 conversion..
a2e90 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20 20 20 ** infop
a2ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2eb0 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 Pointer to the
a2ec0 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 appropriate inf
a2ed0 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20 2a 2f o struct.. */
a2ee0 0a 20 20 20 20 73 77 69 74 63 68 28 20 78 74 79 . switch( xty
a2ef0 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 pe ){. case
a2f00 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20 20 20 etPOINTER:.
a2f10 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e flag_longlon
a2f20 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a g = sizeof(char*
a2f30 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29 3b 0a )==sizeof(i64);.
a2f40 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e flag_lon
a2f50 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a g = sizeof(char*
a2f60 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67 20 69 )==sizeof(long i
a2f70 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 nt);. /*
a2f80 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 Fall through int
a2f90 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 o the next case
a2fa0 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 */. case et
a2fb0 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20 20 63 ORDINAL:. c
a2fc0 61 73 65 20 65 74 52 41 44 49 58 3a 0a 20 20 20 ase etRADIX:.
a2fd0 20 20 20 20 20 69 66 28 20 69 6e 66 6f 70 2d 3e if( infop->
a2fe0 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 49 47 flags & FLAG_SIG
a2ff0 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 NED ){.
a3000 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20 20 20 i64 v;.
a3010 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c if( flag_longl
a3020 6f 6e 67 20 29 20 20 20 76 20 3d 20 76 61 5f 61 ong ) v = va_a
a3030 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 rg(ap,i64);.
a3040 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 else if( f
a3050 6c 61 67 5f 6c 6f 6e 67 20 29 20 20 76 20 3d 20 lag_long ) v =
a3060 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 va_arg(ap,long i
a3070 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 nt);. e
a3080 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 lse
a3090 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 v = va_arg(
a30a0 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 ap,int);.
a30b0 20 20 20 69 66 28 20 76 3c 30 20 29 7b 0a 20 20 if( v<0 ){.
a30c0 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 longva
a30d0 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 lue = -v;.
a30e0 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 prefix = '
a30f0 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 -';. }e
a3100 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
a3110 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 3b 0a longvalue = v;.
a3120 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
a3130 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 flag_plussign )
a3140 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 prefix =
a3150 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 '+';.
a3160 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 else if( flag_b
a3170 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 72 65 66 lanksign ) pref
a3180 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 ix = ' ';.
a3190 20 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 else
a31a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a31b0 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 prefix = 0;.
a31c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a31d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a31e0 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c if( flag_longl
a31f0 6f 6e 67 20 29 20 20 20 6c 6f 6e 67 76 61 6c 75 ong ) longvalu
a3200 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 e = va_arg(ap,u6
a3210 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 4);. el
a3220 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 se if( flag_long
a3230 20 29 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 ) longvalue =
a3240 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e va_arg(ap,unsign
a3250 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 ed long int);.
a3260 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 20 else
a3270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f lo
a3280 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 ngvalue = va_arg
a3290 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e 74 (ap,unsigned int
a32a0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 );. pre
a32b0 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 fix = 0;.
a32c0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c }. if( l
a32d0 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c ongvalue==0 ) fl
a32e0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d ag_alternateform
a32f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 = 0;. if
a3300 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 ( flag_zeropad &
a3310 26 20 70 72 65 63 69 73 69 6f 6e 3c 77 69 64 74 & precision<widt
a3320 68 2d 28 70 72 65 66 69 78 21 3d 30 29 20 29 7b h-(prefix!=0) ){
a3330 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 69 . preci
a3340 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d 28 70 72 sion = width-(pr
a3350 65 66 69 78 21 3d 30 29 3b 0a 20 20 20 20 20 20 efix!=0);.
a3360 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 }. bufp
a3370 74 20 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 t = &buf[etBUFSI
a3380 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 ZE-1];. i
a3390 66 28 20 78 74 79 70 65 3d 3d 65 74 4f 52 44 49 f( xtype==etORDI
a33a0 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 NAL ){.
a33b0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 static const ch
a33c0 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73 ar zOrd[] = "ths
a33d0 74 6e 64 72 64 22 3b 0a 20 20 20 20 20 20 20 20 tndrd";.
a33e0 20 20 69 6e 74 20 78 20 3d 20 6c 6f 6e 67 76 61 int x = longva
a33f0 6c 75 65 20 25 20 31 30 3b 0a 20 20 20 20 20 20 lue % 10;.
a3400 20 20 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 if( x>=4 ||
a3410 28 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 (longvalue/10)%1
a3420 30 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 0==1 ){.
a3430 20 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 x = 0;.
a3440 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
a3450 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 buf[etBUFSIZE-3
a3460 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 ] = zOrd[x*2];.
a3470 20 20 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 buf[etB
a3480 55 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 UFSIZE-2] = zOrd
a3490 5b 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 [x*2+1];.
a34a0 20 20 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 bufpt -= 2;.
a34b0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a34c0 20 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 {. reg
a34d0 69 73 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 ister const char
a34e0 20 2a 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 *cset; /*
a34f0 55 73 65 20 72 65 67 69 73 74 65 72 73 20 66 6f Use registers fo
a3500 72 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 r speed */.
a3510 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e register in
a3520 74 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 t base;.
a3530 20 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 cset = &aDigit
a3540 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 s[infop->charset
a3550 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 ];. bas
a3560 65 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b e = infop->base;
a3570 0a 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 . do{
a3580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a35a0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 /* Conv
a35b0 65 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a ert to ascii */.
a35c0 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d *(--
a35d0 62 75 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f bufpt) = cset[lo
a35e0 6e 67 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 ngvalue%base];.
a35f0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 longv
a3600 61 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 alue = longvalue
a3610 2f 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 /base;.
a3620 20 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c }while( longval
a3630 75 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 ue>0 );.
a3640 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 }. length
a3650 20 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a = &buf[etBUFSIZ
a3660 45 2d 31 5d 2d 62 75 66 70 74 3b 0a 20 20 20 20 E-1]-bufpt;.
a3670 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 63 for(idx=prec
a3680 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69 64 ision-length; id
a3690 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20 20 x>0; idx--){.
a36a0 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 *(--bufpt
a36b0 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20 20 ) = '0';
a36c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a36d0 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61 64 /* Zero pad
a36e0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 */. }.
a36f0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78 if( prefix
a3700 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 ) *(--bufpt) =
a3710 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 prefix;
a3720 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69 67 /* Add sig
a3730 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 n */. if(
a3740 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 flag_alternatef
a3750 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70 72 orm && infop->pr
a3760 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a 20 efix ){ /*
a3770 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 22 20 Add "0" or "0x"
a3780 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e */. con
a3790 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20 20 st char *pre;.
a37a0 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b 0a char x;.
a37b0 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d 20 pre =
a37c0 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d 3e &aPrefix[infop->
a37d0 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20 20 prefix];.
a37e0 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70 72 for(; (x=(*pr
a37f0 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20 2a e))!=0; pre++) *
a3800 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a 20 (--bufpt) = x;.
a3810 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a3820 20 6c 65 6e 67 74 68 20 3d 20 26 62 75 66 5b 65 length = &buf[e
a3830 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 tBUFSIZE-1]-bufp
a3840 74 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b t;. break
a3850 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 46 ;. case etF
a3860 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63 61 73 65 LOAT:. case
a3870 20 65 74 45 58 50 3a 0a 20 20 20 20 20 20 63 61 etEXP:. ca
a3880 73 65 20 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 se etGENERIC:.
a3890 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 20 realvalue
a38a0 3d 20 76 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 = va_arg(ap,doub
a38b0 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c le);.#ifndef SQL
a38c0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
a38d0 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 20 20 G_POINT.
a38e0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 if( precision<0
a38f0 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b ) precision = 6;
a3900 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 /* Set
a3910 64 65 66 61 75 6c 74 20 70 72 65 63 69 73 69 6f default precisio
a3920 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 n */. if(
a3930 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 precision>etBUF
a3940 53 49 5a 45 2f 32 2d 31 30 20 29 20 70 72 65 63 SIZE/2-10 ) prec
a3950 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a ision = etBUFSIZ
a3960 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20 20 20 20 E/2-10;.
a3970 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e if( realvalue<0.
a3980 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
a3990 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c ealvalue = -real
a39a0 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 20 value;.
a39b0 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 prefix = '-';.
a39c0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a39d0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
a39e0 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20 _plussign )
a39f0 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b prefix = '+
a3a00 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 ';. els
a3a10 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b e if( flag_blank
a3a20 73 69 67 6e 20 29 20 20 20 20 70 72 65 66 69 78 sign ) prefix
a3a30 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 = ' ';.
a3a40 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 else
a3a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
a3a60 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 refix = 0;.
a3a70 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
a3a80 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 xtype==etGENERI
a3a90 43 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 C && precision>0
a3aa0 20 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a ) precision--;.
a3ab0 23 69 66 20 30 0a 20 20 20 20 20 20 20 20 2f 2a #if 0. /*
a3ac0 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20 Rounding works
a3ad0 6c 69 6b 65 20 42 53 44 20 77 68 65 6e 20 74 68 like BSD when th
a3ae0 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39 e constant 0.499
a3af0 39 20 69 73 20 75 73 65 64 2e 20 20 57 69 65 72 9 is used. Wier
a3b00 64 21 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f d! */. fo
a3b10 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c r(idx=precision,
a3b20 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b rounder=0.4999;
a3b30 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 idx>0; idx--, r
a3b40 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 ounder*=0.1);.#e
a3b50 6c 73 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 lse. /* I
a3b60 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e t makes more sen
a3b70 73 65 20 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f se to use 0.5 */
a3b80 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 . for(idx
a3b90 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e =precision, roun
a3ba0 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20 der=0.5; idx>0;
a3bb0 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d idx--, rounder*=
a3bc0 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 0.1){}.#endif.
a3bd0 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d if( xtype=
a3be0 3d 65 74 46 4c 4f 41 54 20 29 20 72 65 61 6c 76 =etFLOAT ) realv
a3bf0 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b alue += rounder;
a3c00 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d . /* Norm
a3c10 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c 75 65 20 alize realvalue
a3c20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e 30 20 3e to within 10.0 >
a3c30 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e realvalue >= 1.
a3c40 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 78 70 0 */. exp
a3c50 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 = 0;. if
a3c60 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 72 ( sqlite3IsNaN(r
a3c70 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 ealvalue) ){.
a3c80 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 bufpt = "
a3c90 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20 NaN";.
a3ca0 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20 length = 3;.
a3cb0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a3cc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a3cd0 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30 f( realvalue>0.0
a3ce0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 ){. wh
a3cf0 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d ile( realvalue>=
a3d00 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30 1e32 && exp<=350
a3d10 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d ){ realvalue *=
a3d20 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b 1e-32; exp+=32;
a3d30 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 }. whi
a3d40 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 le( realvalue>=1
a3d50 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 e8 && exp<=350 )
a3d60 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 { realvalue *= 1
a3d70 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20 e-8; exp+=8; }.
a3d80 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
a3d90 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 realvalue>=10.0
a3da0 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 && exp<=350 ){ r
a3db0 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b ealvalue *= 0.1;
a3dc0 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 exp++; }.
a3dd0 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 while( realv
a3de0 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61 alue<1e-8 ){ rea
a3df0 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65 lvalue *= 1e8; e
a3e00 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 xp-=8; }.
a3e10 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 while( realva
a3e20 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76 lue<1.0 ){ realv
a3e30 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78 alue *= 10.0; ex
a3e40 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 p--; }.
a3e50 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a if( exp>350 ){.
a3e60 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
a3e70 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20 prefix=='-' ){.
a3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 buf
a3e90 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20 pt = "-Inf";.
a3ea0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }else i
a3eb0 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29 f( prefix=='+' )
a3ec0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
a3ed0 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a bufpt = "+Inf";.
a3ee0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }els
a3ef0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e{.
a3f00 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a bufpt = "Inf";.
a3f10 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
a3f20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
a3f30 20 3d 20 73 74 72 6c 65 6e 28 62 75 66 70 74 29 = strlen(bufpt)
a3f40 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 ;. br
a3f50 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d eak;. }
a3f60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a3f70 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a bufpt = buf;.
a3f80 20 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 /*.
a3f90 20 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 ** If the fie
a3fa0 6c 64 20 74 79 70 65 20 69 73 20 65 74 47 45 4e ld type is etGEN
a3fb0 45 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 ERIC, then conve
a3fc0 72 74 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 rt to either etE
a3fd0 58 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 XP. ** or
a3fe0 20 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 etFLOAT, as app
a3ff0 72 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 ropriate..
a4000 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 */. fla
a4010 67 5f 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 g_exp = xtype==e
a4020 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66 tEXP;. if
a4030 28 20 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 ( xtype!=etFLOAT
a4040 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
a4050 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 alvalue += round
a4060 65 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 er;. if
a4070 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e ( realvalue>=10.
a4080 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 0 ){ realvalue *
a4090 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a = 0.1; exp++; }.
a40a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a40b0 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 if( xtype==etG
a40c0 45 4e 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 ENERIC ){.
a40d0 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 flag_rtz = !
a40e0 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f flag_alternatefo
a40f0 72 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 rm;. if
a4100 28 20 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e ( exp<-4 || exp>
a4110 70 72 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 precision ){.
a4120 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d xtype =
a4130 20 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 etEXP;.
a4140 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a4150 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d precision =
a4160 20 70 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 precision - exp
a4170 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 74 ;. xt
a4180 79 70 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 ype = etFLOAT;.
a4190 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a41a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a41b0 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 flag_rtz = 0
a41c0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a41d0 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 if( xtype==e
a41e0 74 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 tEXP ){.
a41f0 20 20 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 e2 = 0;.
a4200 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a4210 20 20 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 e2 = exp;.
a4220 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e }. n
a4230 73 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 sd = 0;.
a4240 66 6c 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69 flag_dp = (preci
a4250 73 69 6f 6e 3e 30 29 20 7c 20 66 6c 61 67 5f 61 sion>0) | flag_a
a4260 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 lternateform | f
a4270 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 lag_altform2;.
a4280 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 /* The sig
a4290 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 n in front of th
a42a0 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 e number */.
a42b0 20 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29 if( prefix )
a42c0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 {. *(bu
a42d0 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b fpt++) = prefix;
a42e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a42f0 20 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 /* Digits pri
a4300 6f 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 or to the decima
a4310 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 l point */.
a4320 20 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 if( e2<0 ){.
a4330 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a4340 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 ++) = '0';.
a4350 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a4360 20 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b for(; e2>=0;
a4370 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 e2--){.
a4380 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a4390 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 et_getdigit(&re
a43a0 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 alvalue,&nsd);.
a43b0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a43c0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
a43d0 54 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e The decimal poin
a43e0 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 t */. if(
a43f0 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 flag_dp ){.
a4400 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
a4410 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 = '.';.
a4420 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 }. /* "0"
a4430 20 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 digits after th
a4440 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 e decimal point
a4450 62 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 but before the f
a4460 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 irst. **
a4470 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 significant digi
a4480 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 t of the number
a4490 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 */. for(e
a44a0 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 2++; e2<0; preci
a44b0 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 sion--, e2++){.
a44c0 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
a44d0 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a precision>0 );.
a44e0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a44f0 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 t++) = '0';.
a4500 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a }. /*
a4510 20 53 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 Significant dig
a4520 69 74 73 20 61 66 74 65 72 20 74 68 65 20 64 65 its after the de
a4530 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 cimal point */.
a4540 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 while( (p
a4550 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b recision--)>0 ){
a4560 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 . *(buf
a4570 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 pt++) = et_getdi
a4580 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 git(&realvalue,&
a4590 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a nsd);. }.
a45a0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 /* Remov
a45b0 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 e trailing zeros
a45c0 20 61 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 and the "." if
a45d0 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 no digits follow
a45e0 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 the "." */.
a45f0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a if( flag_rtz
a4600 20 26 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 && flag_dp ){.
a4610 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
a4620 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 bufpt[-1]=='0' )
a4630 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b *(--bufpt) = 0;
a4640 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
a4650 74 28 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a t( bufpt>buf );.
a4660 20 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 if( bu
a4670 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a fpt[-1]=='.' ){.
a4680 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
a4690 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b flag_altform2 ){
a46a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a . *
a46b0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b (bufpt++) = '0';
a46c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c . }el
a46d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
a46e0 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 *(--bufpt) = 0
a46f0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
a4700 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
a4710 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a }. /*
a4720 20 41 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 Add the "eNNN"
a4730 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 suffix */.
a4740 20 20 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c if( flag_exp |
a4750 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 | xtype==etEXP )
a4760 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 {. *(bu
a4770 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 fpt++) = aDigits
a4780 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d [infop->charset]
a4790 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
a47a0 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 exp<0 ){.
a47b0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a47c0 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 = '-'; exp = -ex
a47d0 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c p;. }el
a47e0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
a47f0 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 *(bufpt++) = '+'
a4800 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a4810 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e if( exp>
a4820 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 =100 ){.
a4830 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a4840 20 28 65 78 70 2f 31 30 30 29 2b 27 30 27 3b 20 (exp/100)+'0';
a4850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a4860 2a 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f * 100's digit */
a4870 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70 . exp
a4880 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 %= 100;.
a4890 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a }. *
a48a0 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 78 70 2f (bufpt++) = exp/
a48b0 31 30 2b 27 30 27 3b 20 20 20 20 20 20 20 20 20 10+'0';
a48c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
a48d0 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 0's digit */.
a48e0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a48f0 29 20 3d 20 65 78 70 25 31 30 2b 27 30 27 3b 20 ) = exp%10+'0';
a4900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4910 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 /* 1's digit
a4920 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 */. }.
a4930 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 *bufpt = 0
a4940 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ;.. /* Th
a4950 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 e converted numb
a4960 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 er is in buf[] a
a4970 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 nd zero terminat
a4980 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 ed. Output it..
a4990 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 ** Note t
a49a0 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 hat the number i
a49b0 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f s in the usual o
a49c0 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 rder, not revers
a49d0 65 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 ed as with.
a49e0 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f ** integer co
a49f0 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 nversions. */.
a4a00 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 62 length = b
a4a10 75 66 70 74 2d 62 75 66 3b 0a 20 20 20 20 20 20 ufpt-buf;.
a4a20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a bufpt = buf;..
a4a30 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 /* Speci
a4a40 61 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65 al case: Add le
a4a50 61 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74 ading zeros if t
a4a60 68 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 he flag_zeropad
a4a70 66 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20 flag is.
a4a80 2a 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72 ** set and we ar
a4a90 65 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69 e not left justi
a4aa0 66 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 fied */.
a4ab0 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 if( flag_zeropad
a4ac0 20 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 && !flag_leftju
a4ad0 73 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20 stify && length
a4ae0 3c 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20 < width){.
a4af0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
a4b00 20 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 int nPad =
a4b10 77 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a width - length;.
a4b20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d for(i=
a4b30 77 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 width; i>=nPad;
a4b40 69 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 i--){.
a4b50 20 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66 bufpt[i] = buf
a4b60 70 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20 pt[i-nPad];.
a4b70 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4b80 20 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b i = prefix!=0;
a4b90 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
a4ba0 28 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74 ( nPad-- ) bufpt
a4bb0 5b 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 [i++] = '0';.
a4bc0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a4bd0 77 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d width;. }
a4be0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
a4bf0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 break;. cas
a4c00 65 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20 e etSIZE:.
a4c10 20 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e *(va_arg(ap,in
a4c20 74 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e t*)) = pAccum->n
a4c30 43 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65 Char;. le
a4c40 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 ngth = width = 0
a4c50 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
a4c60 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45 . case etPE
a4c70 52 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 RCENT:. b
a4c80 75 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 uf[0] = '%';.
a4c90 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 bufpt = buf
a4ca0 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 ;. length
a4cb0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 = 1;. br
a4cc0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 eak;. case
a4cd0 65 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20 etCHARX:.
a4ce0 20 63 20 3d 20 62 75 66 5b 30 5d 20 3d 20 76 61 c = buf[0] = va
a4cf0 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 _arg(ap,int);.
a4d00 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 if( precis
a4d10 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 ion>=0 ){.
a4d20 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69 for(idx=1; i
a4d30 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64 dx<precision; id
a4d40 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20 x++) buf[idx] =
a4d50 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e c;. len
a4d60 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b gth = precision;
a4d70 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
a4d80 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
a4d90 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 =1;. }.
a4da0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
a4db0 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 uf;. brea
a4dc0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 k;. case et
a4dd0 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 STRING:. ca
a4de0 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a se etDYNSTRING:.
a4df0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
a4e00 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 va_arg(ap,char*)
a4e10 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75 ;. if( bu
a4e20 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 fpt==0 ){.
a4e30 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a bufpt = "";.
a4e40 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
a4e50 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 ( xtype==etDYNST
a4e60 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20 RING ){.
a4e70 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74 zExtra = bufpt
a4e80 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a4e90 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
a4ea0 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 n>=0 ){.
a4eb0 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 for(length=0;
a4ec0 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e length<precision
a4ed0 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 && bufpt[length
a4ee0 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 ]; length++){}.
a4ef0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a4f00 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a4f10 20 73 74 72 6c 65 6e 28 62 75 66 70 74 29 3b 0a strlen(bufpt);.
a4f20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a4f30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
a4f40 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a ase etSQLESCAPE:
a4f50 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 . case etSQ
a4f60 4c 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20 LESCAPE2:.
a4f70 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 case etSQLESCAPE
a4f80 33 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 3: {. int
a4f90 20 69 2c 20 6a 2c 20 6e 2c 20 63 68 2c 20 69 73 i, j, n, ch, is
a4fa0 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e null;. in
a4fb0 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 20 t needQuote;.
a4fc0 20 20 20 20 20 63 68 61 72 20 71 20 3d 20 28 28 char q = ((
a4fd0 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 xtype==etSQLESCA
a4fe0 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20 PE3)?'"':'\'');
a4ff0 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 72 61 /* Quote chara
a5000 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 cter */.
a5010 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d 20 76 char *escarg = v
a5020 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b a_arg(ap,char*);
a5030 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c 6c 20 . isnull
a5040 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20 = escarg==0;.
a5050 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c 6c 20 if( isnull
a5060 29 20 65 73 63 61 72 67 20 3d 20 28 78 74 79 70 ) escarg = (xtyp
a5070 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 20 e==etSQLESCAPE2
a5080 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c ? "NULL" : "(NUL
a5090 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20 66 6f L)");. fo
a50a0 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63 r(i=n=0; (ch=esc
a50b0 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 arg[i])!=0; i++)
a50c0 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
a50d0 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20 20 ch==q ) n++;.
a50e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a50f0 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69 73 6e needQuote = !isn
a5100 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d 65 74 ull && xtype==et
a5110 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 20 20 SQLESCAPE2;.
a5120 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 20 2b n += i + 1 +
a5130 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a 20 20 needQuote*2;.
a5140 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74 42 55 if( n>etBU
a5150 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 FSIZE ){.
a5160 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78 74 72 bufpt = zExtr
a5170 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f a = sqlite3Mallo
a5180 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 c( n );.
a5190 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 if( bufpt==0 )
a51a0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 return;.
a51b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a51c0 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 bufpt = buf;.
a51d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a51e0 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 j = 0;.
a51f0 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 if( needQuote )
a5200 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a bufpt[j++] = q;.
a5210 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
a5220 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 (ch=escarg[i])!
a5230 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 =0; i++){.
a5240 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d bufpt[j++] =
a5250 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ch;. i
a5260 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 70 74 f( ch==q ) bufpt
a5270 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 [j++] = ch;.
a5280 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
a5290 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62 75 ( needQuote ) bu
a52a0 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 fpt[j++] = q;.
a52b0 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d 20 3d bufpt[j] =
a52c0 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 0;. leng
a52d0 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 th = j;.
a52e0 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 6f 6e /* The precision
a52f0 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20 25 is ignored on %
a5300 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20 20 q and %Q */.
a5310 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 63 69 /* if( preci
a5320 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 69 sion>=0 && preci
a5330 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c 65 sion<length ) le
a5340 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e ngth = precision
a5350 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 ; */. bre
a5360 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
a5370 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a 20 case etTOKEN:
a5380 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20 {. Token
a5390 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 67 *pToken = va_arg
a53a0 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 20 (ap, Token*);.
a53b0 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e if( pToken
a53c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
a53d0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
a53e0 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f 6e end(pAccum, (con
a53f0 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e 2d st char*)pToken-
a5400 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a >z, pToken->n);.
a5410 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a5420 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 length = width
a5430 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 = 0;. br
a5440 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
a5450 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49 53 case etSRCLIS
a5460 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63 T: {. Src
a5470 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 5f List *pSrc = va_
a5480 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 2a arg(ap, SrcList*
a5490 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b );. int k
a54a0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e = va_arg(ap, in
a54b0 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 t);. stru
a54c0 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
a54d0 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e *pItem = &pSrc->
a54e0 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 a[k];. as
a54f0 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b 3c sert( k>=0 && k<
a5500 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 pSrc->nSrc );.
a5510 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d if( pItem-
a5520 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20 >zDatabase ){.
a5530 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
a5540 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
a5550 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 ccum, pItem->zDa
a5560 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 20 tabase, -1);.
a5570 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
a5580 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 rAccumAppend(pAc
a5590 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 20 cum, ".", 1);.
a55a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a55b0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
a55c0 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 ppend(pAccum, pI
a55d0 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 3b tem->zName, -1);
a55e0 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
a55f0 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 = width = 0;.
a5600 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a5610 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 }. }/* End
a5620 73 77 69 74 63 68 20 6f 76 65 72 20 74 68 65 20 switch over the
a5630 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 format type */.
a5640 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 /*. ** The
a5650 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e text of the con
a5660 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 version is point
a5670 65 64 20 74 6f 20 62 79 20 22 62 75 66 70 74 22 ed to by "bufpt"
a5680 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 and is. ** "
a5690 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63 74 65 length" characte
a56a0 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 rs long. The fi
a56b0 65 6c 64 20 77 69 64 74 68 20 69 73 20 22 77 69 eld width is "wi
a56c0 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a dth". Do. **
a56d0 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 the output..
a56e0 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 6c 61 */. if( !fla
a56f0 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b g_leftjustify ){
a5700 0a 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20 . register
a5710 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 int nspace;.
a5720 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 nspace = width
a5730 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 -length;. i
a5740 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 f( nspace>0 ){.
a5750 20 20 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 appendSpa
a5760 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 ce(pAccum, nspac
a5770 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
a5780 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 }. if( length
a5790 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 >0 ){. sqli
a57a0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a57b0 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c d(pAccum, bufpt,
a57c0 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a length);. }.
a57d0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 if( flag_lef
a57e0 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 tjustify ){.
a57f0 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e register int n
a5800 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 space;. nsp
a5810 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 ace = width-leng
a5820 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 th;. if( ns
a5830 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 pace>0 ){.
a5840 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 appendSpace(pA
a5850 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 ccum, nspace);.
a5860 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
a5870 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 if( zExtra ){.
a5880 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
a5890 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d e(zExtra);. }
a58a0 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c . }/* End for l
a58b0 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72 oop over the for
a58c0 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 mat string */.}
a58d0 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 /* End of functi
a58e0 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 on */../*.** App
a58f0 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 20 74 end N bytes of t
a5900 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 ext from z to th
a5910 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 e StrAccum objec
a5920 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
a5930 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a5940 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
a5950 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e StrAccum *p, con
a5960 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 st char *z, int
a5970 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f N){. if( p->too
a5980 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 Big | p->mallocF
a5990 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 ailed ){. ret
a59a0 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e urn;. }. if( N
a59b0 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 74 <0 ){. N = st
a59c0 72 6c 65 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 rlen(z);. }. i
a59d0 66 28 20 4e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 f( N==0 ){. r
a59e0 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 eturn;. }. if(
a59f0 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 p->nChar+N >= p
a5a00 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 ->nAlloc ){.
a5a10 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 char *zNew;.
a5a20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f if( !p->useMallo
a5a30 63 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f c ){. p->to
a5a40 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 oBig = 1;.
a5a50 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 N = p->nAlloc -
a5a60 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 p->nChar - 1;.
a5a70 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a if( N<=0 ){.
a5a80 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
a5a90 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
a5aa0 65 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 4e e{. i64 szN
a5ab0 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 ew = p->nChar;.
a5ac0 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 szNew += N
a5ad0 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 73 + 1;. if( s
a5ae0 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f zNew > p->mxAllo
a5af0 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c c ){. sql
a5b00 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 ite3StrAccumRese
a5b10 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d t(p);. p-
a5b20 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 >tooBig = 1;.
a5b30 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
a5b40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a5b50 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 7a p->nAlloc = sz
a5b60 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 New;. }.
a5b70 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 zNew = sqlite
a5b80 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 3DbMallocRaw(p->
a5b90 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b db, p->nAlloc );
a5ba0 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 77 20 . if( zNew
a5bb0 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ){. memcp
a5bc0 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 y(zNew, p->zText
a5bd0 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 , p->nChar);.
a5be0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 sqlite3StrA
a5bf0 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 ccumReset(p);.
a5c00 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d p->zText =
a5c10 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c zNew;. }el
a5c20 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d se{. p->m
a5c30 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
a5c40 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
a5c50 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 StrAccumReset(p)
a5c60 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
a5c70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
a5c80 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d }. memcpy(&p-
a5c90 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d >zText[p->nChar]
a5ca0 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 , z, N);. p->nC
a5cb0 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a har += N;.}../*.
a5cc0 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 61 20 ** Finish off a
a5cd0 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e 67 string by making
a5ce0 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 72 6f sure it is zero
a5cf0 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 -terminated..**
a5d00 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
a5d10 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 69 6e to the resultin
a5d20 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72 g string. Retur
a5d30 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e n a NULL.** poin
a5d40 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 ter if any kind
a5d50 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 6e 63 of error was enc
a5d60 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c ountered..*/.SQL
a5d70 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
a5d80 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 *sqlite3StrAccu
a5d90 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d mFinish(StrAccum
a5da0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a *p){. if( p->z
a5db0 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 7a Text ){. p->z
a5dc0 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d Text[p->nChar] =
a5dd0 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 0;. if( p->u
a5de0 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a seMalloc && p->z
a5df0 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 Text==p->zBase )
a5e00 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 {. p->zText
a5e10 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
a5e20 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e ocRaw(p->db, p->
a5e30 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 20 20 nChar+1 );.
a5e40 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b if( p->zText ){
a5e50 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
a5e60 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61 p->zText, p->zBa
a5e70 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b se, p->nChar+1);
a5e80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
a5e90 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 p->mallocF
a5ea0 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 ailed = 1;.
a5eb0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
a5ec0 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a eturn p->zText;.
a5ed0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 61 }../*.** Reset a
a5ee0 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 69 6e n StrAccum strin
a5ef0 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 g. Reclaim all
a5f00 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2e malloced memory.
a5f10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a5f20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
a5f30 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 trAccumReset(Str
a5f40 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 Accum *p){. if(
a5f50 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 p->zText!=p->zB
a5f60 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ase ){. sqlit
a5f70 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 e3DbFree(p->db,
a5f80 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a 20 p->zText);. }.
a5f90 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 7d p->zText = 0;.}
a5fa0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
a5fb0 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 63 75 ze a string accu
a5fc0 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 mulator.*/.SQLIT
a5fd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a5fe0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e qlite3StrAccumIn
a5ff0 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 it(StrAccum *p,
a6000 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e 74 char *zBase, int
a6010 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20 70 n, int mx){. p
a6020 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42 61 ->zText = p->zBa
a6030 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70 2d se = zBase;. p-
a6040 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 >db = 0;. p->nC
a6050 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 har = 0;. p->nA
a6060 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d lloc = n;. p->m
a6070 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 xAlloc = mx;. p
a6080 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b ->useMalloc = 1;
a6090 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 . p->tooBig = 0
a60a0 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 ;. p->mallocFai
a60b0 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a led = 0;.}../*.*
a60c0 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d * Print into mem
a60d0 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
a60e0 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 m sqliteMalloc()
a60f0 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 . Use the inter
a6100 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 nal.** %-convers
a6110 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a ion extensions..
a6120 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a6130 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 E char *sqlite3V
a6140 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20 MPrintf(sqlite3
a6150 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *db, const char
a6160 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 *zFormat, va_lis
a6170 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a t ap){. char *z
a6180 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 ;. char zBase[S
a6190 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f QLITE_PRINT_BUF_
a61a0 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 SIZE];. StrAccu
a61b0 6d 20 61 63 63 3b 0a 20 20 73 71 6c 69 74 65 33 m acc;. sqlite3
a61c0 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 StrAccumInit(&ac
a61d0 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 c, zBase, sizeof
a61e0 28 7a 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20 (zBase),.
a61f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
a6200 62 20 3f 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 b ? db->aLimit[S
a6210 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
a6220 54 48 5d 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 TH] : SQLITE_MAX
a6230 5f 4c 45 4e 47 54 48 29 3b 0a 20 20 61 63 63 2e _LENGTH);. acc.
a6240 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 db = db;. sqlit
a6250 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c e3VXPrintf(&acc,
a6260 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 1, zFormat, ap)
a6270 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 ;. z = sqlite3S
a6280 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 trAccumFinish(&a
a6290 63 63 29 3b 0a 20 20 69 66 28 20 61 63 63 2e 6d cc);. if( acc.m
a62a0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 64 allocFailed && d
a62b0 62 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c b ){. db->mal
a62c0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
a62d0 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d }. return z;.}
a62e0 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e ../*.** Print in
a62f0 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
a6300 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 ed from sqliteMa
a6310 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 65 lloc(). Use the
a6320 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 internal.** %-c
a6330 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 onversion extens
a6340 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
a6350 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
a6360 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c lite3MPrintf(sql
a6370 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
a6380 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
a6390 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
a63a0 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 p;. char *z;.
a63b0 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f va_start(ap, zFo
a63c0 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c rmat);. z = sql
a63d0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c ite3VMPrintf(db,
a63e0 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 zFormat, ap);.
a63f0 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 va_end(ap);. r
a6400 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn z;.}../*.*
a6410 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65 33 4d 50 * Like sqlite3MP
a6420 72 69 6e 74 66 28 29 2c 20 62 75 74 20 63 61 6c rintf(), but cal
a6430 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 l sqlite3DbFree(
a6440 29 20 6f 6e 20 7a 53 74 72 20 61 66 74 65 72 20 ) on zStr after
a6450 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 74 68 formatting.** th
a6460 65 20 73 74 72 69 6e 67 20 61 6e 64 20 62 65 66 e string and bef
a6470 6f 72 65 20 72 65 74 75 72 6e 6e 69 6e 67 2e 20 ore returnning.
a6480 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
a6490 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 intended to be
a64a0 75 73 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 used.** to modif
a64b0 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 74 y an existing st
a64c0 72 69 6e 67 2e 20 20 46 6f 72 20 65 78 61 6d 70 ring. For examp
a64d0 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 le:.**.**
a64e0 78 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e x = sqlite3MPrin
a64f0 74 66 28 64 62 2c 20 78 2c 20 22 70 72 65 66 69 tf(db, x, "prefi
a6500 78 20 25 73 20 73 75 66 66 69 78 22 2c 20 78 29 x %s suffix", x)
a6510 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ;.**.*/.SQLITE_P
a6520 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
a6530 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c ite3MAppendf(sql
a6540 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a ite3 *db, char *
a6550 7a 53 74 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 zStr, const char
a6560 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
a6570 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
a6580 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 char *z;. va_s
a6590 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 tart(ap, zFormat
a65a0 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 );. z = sqlite3
a65b0 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f VMPrintf(db, zFo
a65c0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
a65d0 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 end(ap);. sqlit
a65e0 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 e3DbFree(db, zSt
a65f0 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a r);. return z;.
a6600 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
a6610 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
a6620 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
a6630 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 74 _malloc(). Omit
a6640 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a the internal.**
a6650 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 %-conversion ex
a6660 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c tensions..*/.SQL
a6670 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 ITE_API char *sq
a6680 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 lite3_vmprintf(c
a6690 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a66a0 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b at, va_list ap){
a66b0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 . char *z;. ch
a66c0 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f ar zBase[SQLITE_
a66d0 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b PRINT_BUF_SIZE];
a66e0 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b . StrAccum acc;
a66f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a6700 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
a6710 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 if( sqlite3_init
a6720 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 ialize() ) retur
a6730 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 n 0;.#endif. sq
a6740 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
a6750 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 t(&acc, zBase, s
a6760 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 53 51 izeof(zBase), SQ
a6770 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 LITE_MAX_LENGTH)
a6780 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 ;. sqlite3VXPri
a6790 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f ntf(&acc, 0, zFo
a67a0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d rmat, ap);. z =
a67b0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a67c0 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 Finish(&acc);.
a67d0 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a return z;.}../*.
a67e0 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 ** Print into me
a67f0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
a6800 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
a6810 63 28 29 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 c()(). Omit the
a6820 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 internal.** %-c
a6830 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 onversion extens
a6840 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
a6850 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
a6860 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 3_mprintf(const
a6870 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
a6880 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
a6890 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 23 69 p;. char *z;.#i
a68a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
a68b0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 T_AUTOINIT. if(
a68c0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
a68d0 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 ize() ) return 0
a68e0 3b 0a 23 65 6e 64 69 66 0a 20 20 76 61 5f 73 74 ;.#endif. va_st
a68f0 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
a6900 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f ;. z = sqlite3_
a6910 76 6d 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74 vmprintf(zFormat
a6920 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
a6930 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b ap);. return z;
a6940 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 .}../*.** sqlite
a6950 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 77 6f 72 3_snprintf() wor
a6960 6b 73 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74 66 ks like snprintf
a6970 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 69 () except that i
a6980 74 20 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a 2a t ignores the.**
a6990 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 20 current locale
a69a0 73 65 74 74 69 6e 67 73 2e 20 20 54 68 69 73 20 settings. This
a69b0 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 is important for
a69c0 20 53 51 4c 69 74 65 20 62 65 63 61 75 73 65 20 SQLite because
a69d0 77 65 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 62 we.** are not ab
a69e0 6c 65 20 74 6f 20 75 73 65 20 61 20 22 2c 22 20 le to use a ","
a69f0 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 as the decimal p
a6a00 6f 69 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f 66 oint in place of
a6a10 20 22 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63 69 "." as.** speci
a6a20 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f 63 fied by some loc
a6a30 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ales..*/.SQLITE_
a6a40 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
a6a50 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 20 6e 3_snprintf(int n
a6a60 2c 20 63 68 61 72 20 2a 7a 42 75 66 2c 20 63 6f , char *zBuf, co
a6a70 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
a6a80 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20 t, ...){. char
a6a90 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 *z;. va_list ap
a6aa0 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 ;. StrAccum acc
a6ab0 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29 7b ;.. if( n<=0 ){
a6ac0 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 42 75 66 . return zBuf
a6ad0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 ;. }. sqlite3S
a6ae0 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 trAccumInit(&acc
a6af0 2c 20 7a 42 75 66 2c 20 6e 2c 20 30 29 3b 0a 20 , zBuf, n, 0);.
a6b00 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d acc.useMalloc =
a6b10 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 0;. va_start(a
a6b20 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 p,zFormat);. sq
a6b30 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 lite3VXPrintf(&a
a6b40 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 cc, 0, zFormat,
a6b50 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
a6b60 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 );. z = sqlite3
a6b70 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 StrAccumFinish(&
a6b80 61 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a acc);. return z
a6b90 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 ;.}..#if defined
a6ba0 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f (SQLITE_DEBUG)./
a6bb0 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20 6f *.** A version o
a6bc0 66 20 70 72 69 6e 74 66 28 29 20 74 68 61 74 20 f printf() that
a6bd0 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c 6c 64 understands %lld
a6be0 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62 75 . Used for debu
a6bf0 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70 72 gging..** The pr
a6c00 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69 6e 74 intf() built int
a6c10 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 o some versions
a6c20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 of windows does
a6c30 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 25 not understand %
a6c40 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66 61 lld.** and segfa
a6c50 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69 76 65 ults if you give
a6c60 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67 20 it a long long
a6c70 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 int..*/.SQLITE_P
a6c80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a6c90 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 te3DebugPrintf(c
a6ca0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a6cb0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
a6cc0 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 ist ap;. StrAcc
a6cd0 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72 20 7a um acc;. char z
a6ce0 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c 69 Buf[500];. sqli
a6cf0 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 te3StrAccumInit(
a6d00 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a 65 &acc, zBuf, size
a6d10 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20 20 of(zBuf), 0);.
a6d20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 acc.useMalloc =
a6d30 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 0;. va_start(ap
a6d40 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c ,zFormat);. sql
a6d50 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 ite3VXPrintf(&ac
a6d60 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 c, 0, zFormat, a
a6d70 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 p);. va_end(ap)
a6d80 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 ;. sqlite3StrAc
a6d90 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b cumFinish(&acc);
a6da0 0a 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 . fprintf(stdou
a6db0 74 2c 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 t,"%s", zBuf);.
a6dc0 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b fflush(stdout);
a6dd0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a .}.#endif../****
a6de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
a6df0 66 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a f printf.c *****
a6e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
a6e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
a6e40 20 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20 2a file random.c *
a6e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
a6e80 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
a6e90 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
a6ea0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
a6eb0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
a6ec0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
a6ed0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
a6ee0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
a6ef0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
a6f00 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
a6f10 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
a6f20 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
a6f30 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
a6f40 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
a6f50 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
a6f60 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
a6f70 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
a6f80 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
a6f90 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
a6fa0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
a6fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6ff0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
a7000 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 ontains code to
a7010 69 6d 70 6c 65 6d 65 6e 74 20 61 20 70 73 65 75 implement a pseu
a7020 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 do-random number
a7030 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72 20 28 50 .** generator (P
a7040 52 4e 47 29 20 66 6f 72 20 53 51 4c 69 74 65 2e RNG) for SQLite.
a7050 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 .**.** Random nu
a7060 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 20 62 mbers are used b
a7070 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 64 61 y some of the da
a7080 74 61 62 61 73 65 20 62 61 63 6b 65 6e 64 73 20 tabase backends
a7090 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 67 in order.** to g
a70a0 65 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d 20 69 enerate random i
a70b0 6e 74 65 67 65 72 20 6b 65 79 73 20 66 6f 72 20 nteger keys for
a70c0 74 61 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f 6d tables or random
a70d0 20 66 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a filenames..**.*
a70e0 2a 20 24 49 64 3a 20 72 61 6e 64 6f 6d 2e 63 2c * $Id: random.c,
a70f0 76 20 31 2e 32 37 20 32 30 30 38 2f 31 30 2f 30 v 1.27 2008/10/0
a7100 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45 7 15:25:48 drh E
a7110 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c xp $.*/.../* All
a7120 20 74 68 72 65 61 64 73 20 73 68 61 72 65 20 61 threads share a
a7130 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 6e single random n
a7140 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 2e umber generator.
a7150 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 .** This structu
a7160 72 65 20 69 73 20 74 68 65 20 63 75 72 72 65 6e re is the curren
a7170 74 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 67 t state of the g
a7180 65 6e 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 enerator..*/.sta
a7190 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 tic SQLITE_WSD s
a71a0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a71b0 67 54 79 70 65 20 7b 0a 20 20 75 6e 73 69 67 6e gType {. unsign
a71c0 65 64 20 63 68 61 72 20 69 73 49 6e 69 74 3b 20 ed char isInit;
a71d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
a71e0 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 if initialized
a71f0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
a7200 61 72 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 ar i, j;
a7210 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 /* State var
a7220 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 6e 73 69 iables */. unsi
a7230 67 6e 65 64 20 63 68 61 72 20 73 5b 32 35 36 5d gned char s[256]
a7240 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 ; /* St
a7250 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f ate variables */
a7260 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e 67 20 3d .} sqlite3Prng =
a7270 20 7b 20 30 2c 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 { 0, };../*.**
a7280 47 65 74 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 Get a single 8-b
a7290 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 it random value
a72a0 66 72 6f 6d 20 74 68 65 20 52 43 34 20 50 52 4e from the RC4 PRN
a72b0 47 2e 20 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a G. The Mutex.**
a72c0 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
a72d0 69 6c 65 20 65 78 65 63 75 74 69 6e 67 20 74 68 ile executing th
a72e0 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a is routine..**.*
a72f0 2a 20 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 * Why not just u
a7300 73 65 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e se a library ran
a7310 64 6f 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 dom generator li
a7320 6b 65 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 ke lrand48() for
a7330 20 74 68 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 this?.** Becaus
a7340 65 20 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 e the OP_NewRowi
a7350 64 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 d opcode in the
a7360 56 44 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 VDBE depends on
a7370 68 61 76 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a having a very.**
a7380 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 good source of
a7390 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 random numbers.
a73a0 20 54 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c The lrand48() l
a73b0 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 ibrary function
a73c0 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 may.** well be g
a73d0 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 ood enough. But
a73e0 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 maybe not. Or
a73f0 6d 61 79 62 65 20 6c 72 61 6e 64 34 38 28 29 20 maybe lrand48()
a7400 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 has some.** subt
a7410 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 le problems on s
a7420 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 ome systems that
a7430 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f could cause pro
a7440 62 6c 65 6d 73 2e 20 20 49 74 20 69 73 20 68 61 blems. It is ha
a7450 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 rd.** to know.
a7460 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 To minimize the
a7470 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 risk of problems
a7480 20 64 75 65 20 74 6f 20 62 61 64 20 6c 72 61 6e due to bad lran
a7490 64 34 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 d48().** impleme
a74a0 6e 74 61 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 ntations, SQLite
a74b0 20 75 73 65 73 20 74 68 69 73 20 72 61 6e 64 6f uses this rando
a74c0 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 m number generat
a74d0 6f 72 20 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 or based.** on R
a74e0 43 34 2c 20 77 68 69 63 68 20 77 65 20 6b 6e 6f C4, which we kno
a74f0 77 20 77 6f 72 6b 73 20 76 65 72 79 20 77 65 6c w works very wel
a7500 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 l..**.** (Later)
a7510 3a 20 20 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f : Actually, OP_
a7520 4e 65 77 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f NewRowid does no
a7530 74 20 64 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f t depend on a go
a7540 6f 64 20 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 od source of.**
a7550 72 61 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d randomness any m
a7560 6f 72 65 2e 20 20 42 75 74 20 77 65 20 77 69 6c ore. But we wil
a7570 6c 20 6c 65 61 76 65 20 74 68 69 73 20 63 6f 64 l leave this cod
a7580 65 20 69 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d e in all the sam
a7590 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
a75a0 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 6f 69 64 randomByte(void
a75b0 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ){. unsigned ch
a75c0 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 ar t;... /* The
a75d0 20 22 77 73 64 50 72 6e 67 22 20 6d 61 63 72 6f "wsdPrng" macro
a75e0 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f will resolve to
a75f0 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 the pseudo-rand
a7600 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 om number genera
a7610 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 20 76 tor. ** state v
a7620 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 ector. If writa
a7630 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 ble static data
a7640 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f is unsupported o
a7650 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 20 20 n the target,.
a7660 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f ** we have to lo
a7670 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 cate the state v
a7680 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d ector at run-tim
a7690 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 e. In the more
a76a0 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 common. ** case
a76b0 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 where writable
a76c0 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 static data is s
a76d0 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50 72 6e upported, wsdPrn
a76e0 67 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 g can refer dire
a76f0 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65 ctly. ** to the
a7700 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22 20 73 "sqlite3Prng" s
a7710 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c tate vector decl
a7720 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f ared above.. */
a7730 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
a7740 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 75 63 74 MIT_WSD. struct
a7750 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
a7760 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c 28 73 74 *p = &GLOBAL(st
a7770 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
a7780 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e Type, sqlite3Prn
a7790 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 77 73 64 g);.# define wsd
a77a0 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c 73 65 0a Prng p[0].#else.
a77b0 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67 # define wsdPrng
a77c0 20 73 71 6c 69 74 65 33 50 72 6e 67 0a 23 65 6e sqlite3Prng.#en
a77d0 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 dif... /* Initi
a77e0 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74 65 20 alize the state
a77f0 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 of the random nu
a7800 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 6f mber generator o
a7810 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 nce,. ** the fi
a7820 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f rst time this ro
a7830 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
a7840 20 20 54 68 65 20 73 65 65 64 20 76 61 6c 75 65 The seed value
a7850 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e does. ** not n
a7860 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 eed to contain a
a7870 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 lot of randomne
a7880 73 73 20 73 69 6e 63 65 20 77 65 20 61 72 65 20 ss since we are
a7890 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 not. ** trying
a78a0 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65 6e 63 to do secure enc
a78b0 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 ryption or anyth
a78c0 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e ing like that...
a78d0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 . **. ** Nothi
a78e0 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 ng in this file
a78f0 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c 73 65 or anywhere else
a7900 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 73 20 in SQLite does
a7910 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a any kind of. **
a7920 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 encryption. Th
a7930 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 e RC4 algorithm
a7940 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 61 73 is being used as
a7950 20 61 20 50 52 4e 47 20 28 70 73 65 75 64 6f 2d a PRNG (pseudo-
a7960 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 random. ** numb
a7970 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f er generator) no
a7980 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70 74 69 t as an encrypti
a7990 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a on device.. */.
a79a0 20 20 69 66 28 20 21 77 73 64 50 72 6e 67 2e 69 if( !wsdPrng.i
a79b0 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 sInit ){. int
a79c0 20 69 3b 0a 20 20 20 20 63 68 61 72 20 6b 5b 32 i;. char k[2
a79d0 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 56];. wsdPrng
a79e0 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 73 64 50 .j = 0;. wsdP
a79f0 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 20 20 73 rng.i = 0;. s
a7a00 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 qlite3OsRandomne
a7a10 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 ss(sqlite3_vfs_f
a7a20 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 6b 29 3b ind(0), 256, k);
a7a30 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
a7a40 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 256; i++){.
a7a50 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 wsdPrng.s[i] =
a7a60 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 i;. }. for
a7a70 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b (i=0; i<256; i++
a7a80 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 ){. wsdPrng
a7a90 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b .j += wsdPrng.s[
a7aa0 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 i] + k[i];.
a7ab0 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 t = wsdPrng.s[w
a7ac0 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 sdPrng.j];.
a7ad0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a7ae0 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e ng.j] = wsdPrng.
a7af0 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 s[i];. wsdP
a7b00 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 rng.s[i] = t;.
a7b10 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e }. wsdPrng.
a7b20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a isInit = 1;. }.
a7b30 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 . /* Generate a
a7b40 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 nd return single
a7b50 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a random byte. *
a7b60 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b /. wsdPrng.i++;
a7b70 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 . t = wsdPrng.s
a7b80 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 [wsdPrng.i];. w
a7b90 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 sdPrng.j += t;.
a7ba0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a7bb0 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e ng.i] = wsdPrng.
a7bc0 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 s[wsdPrng.j];.
a7bd0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e wsdPrng.s[wsdPrn
a7be0 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d g.j] = t;. t +=
a7bf0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a7c00 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 ng.i];. return
a7c10 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a wsdPrng.s[t];.}.
a7c20 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 ./*.** Return N
a7c30 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f random bytes..*/
a7c40 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
a7c50 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
a7c60 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 ess(int N, void
a7c70 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e *pBuf){. unsign
a7c80 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 ed char *zBuf =
a7c90 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 pBuf;.#if SQLITE
a7ca0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 _THREADSAFE. sq
a7cb0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
a7cc0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
a7cd0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
a7ce0 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 TEX_STATIC_PRNG)
a7cf0 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 ;.#endif. sqlit
a7d00 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
a7d10 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 utex);. while(
a7d20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 N-- ){. *(zBu
a7d30 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 f++) = randomByt
a7d40 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 e();. }. sqlit
a7d50 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
a7d60 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 utex);.}..#ifnde
a7d70 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
a7d80 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a ILTIN_TEST./*.**
a7d90 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 For testing pur
a7da0 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 poses, we someti
a7db0 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73 mes want to pres
a7dc0 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f erve the state o
a7dd0 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 f.** PRNG and re
a7de0 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74 store the PRNG t
a7df0 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74 o its saved stat
a7e00 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d e at a later tim
a7e10 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 e, or.** to rese
a7e20 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 t the PRNG to it
a7e30 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e s initial state.
a7e40 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
a7e50 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 accomplish.** t
a7e60 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a hose tasks..**.*
a7e70 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 * The sqlite3_te
a7e80 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 st_control() int
a7e90 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65 erface calls the
a7ea0 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a se routines to.*
a7eb0 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 * control the PR
a7ec0 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 NG..*/.static SQ
a7ed0 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 LITE_WSD struct
a7ee0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 sqlite3PrngType
a7ef0 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 sqlite3SavedPrng
a7f00 20 3d 20 7b 20 30 2c 20 7d 3b 0a 53 51 4c 49 54 = { 0, };.SQLIT
a7f10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a7f20 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 qlite3PrngSaveSt
a7f30 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d ate(void){. mem
a7f40 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c cpy(. &GLOBAL
a7f50 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 (struct sqlite3P
a7f60 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 rngType, sqlite3
a7f70 53 61 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 SavedPrng),.
a7f80 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 &GLOBAL(struct s
a7f90 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 qlite3PrngType,
a7fa0 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 sqlite3Prng),.
a7fb0 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 sizeof(sqlite3
a7fc0 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c Prng). );.}.SQL
a7fd0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a7fe0 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 sqlite3PrngRest
a7ff0 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a oreState(void){.
a8000 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 memcpy(. &G
a8010 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c LOBAL(struct sql
a8020 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 ite3PrngType, sq
a8030 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 lite3Prng),.
a8040 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 &GLOBAL(struct s
a8050 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 qlite3PrngType,
a8060 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 sqlite3SavedPrng
a8070 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 ),. sizeof(sq
a8080 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a lite3Prng). );.
a8090 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
a80a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e void sqlite3Prn
a80b0 67 52 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 gResetState(void
a80c0 29 7b 0a 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 ){. GLOBAL(stru
a80d0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 ct sqlite3PrngTy
a80e0 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 pe, sqlite3Prng)
a80f0 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 .isInit = 0;.}.#
a8100 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
a8110 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
a8120 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a T */../*********
a8130 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e ***** End of ran
a8140 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dom.c **********
a8150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8170 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
a8180 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
a8190 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utf.c *********
a81a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a81b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a81c0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
a81d0 20 41 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 April 13.**.**
a81e0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
a81f0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
a8200 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
a8210 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
a8220 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
a8230 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
a8240 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
a8250 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
a8260 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
a8270 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
a8280 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
a8290 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
a82a0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
a82b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
a82c0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
a82d0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
a82e0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
a82f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8330 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
a8340 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 file contains r
a8350 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 outines used to
a8360 74 72 61 6e 73 6c 61 74 65 20 62 65 74 77 65 65 translate betwee
a8370 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 n UTF-8, .** UTF
a8380 2d 31 36 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 -16, UTF-16BE, a
a8390 6e 64 20 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a nd UTF-16LE..**.
a83a0 2a 2a 20 24 49 64 3a 20 75 74 66 2e 63 2c 76 20 ** $Id: utf.c,v
a83b0 31 2e 36 36 20 32 30 30 38 2f 31 31 2f 30 37 20 1.66 2008/11/07
a83c0 30 33 3a 32 39 3a 33 34 20 64 72 68 20 45 78 70 03:29:34 drh Exp
a83d0 20 24 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f $.**.** Notes o
a83e0 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 n UTF-8:.**.**
a83f0 20 42 79 74 65 2d 30 20 20 20 20 42 79 74 65 2d Byte-0 Byte-
a8400 31 20 20 20 20 42 79 74 65 2d 32 20 20 20 20 42 1 Byte-2 B
a8410 79 74 65 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a yte-3 Value.*
a8420 2a 20 20 30 78 78 78 78 78 78 78 20 20 20 20 20 * 0xxxxxxx
a8430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8440 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 0000
a8450 30 30 30 30 20 30 30 30 30 30 30 30 30 20 30 78 0000 00000000 0x
a8460 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 30 79 79 xxxxxx.** 110yy
a8470 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20 yyy 10xxxxxx
a8480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8490 20 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 00000000 000
a84a0 30 30 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 00yyy yyxxxxxx.*
a84b0 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79 * 1110zzzz 10y
a84c0 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 yyyyy 10xxxxxx
a84d0 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 0000
a84e0 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 0000 zzzzyyyy yy
a84f0 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 31 30 xxxxxx.** 11110
a8500 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31 uuu 10uuzzzz 1
a8510 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78 78 0yyyyyy 10xxxxx
a8520 78 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a x 000uuuuu zzz
a8530 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a zyyyy yyxxxxxx.*
a8540 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e *.**.** Notes on
a8550 20 55 54 46 2d 31 36 3a 20 20 28 77 69 74 68 20 UTF-16: (with
a8560 77 77 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a wwww+1==uuuuu).*
a8570 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 *.** Word-0
a8580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57 W
a8590 6f 72 64 2d 31 20 20 20 20 20 20 20 20 20 20 56 ord-1 V
a85a0 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31 31 30 77 alue.** 110110w
a85b0 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 w wwzzzzyy 110
a85c0 31 31 31 79 79 20 79 79 78 78 78 78 78 78 20 20 111yy yyxxxxxx
a85d0 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 000uuuuu zzzzy
a85e0 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 yyy yyxxxxxx.**
a85f0 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 zzzzyyyy yyxxxx
a8600 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xx
a8610 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 000000
a8620 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 00 zzzzyyyy yyxx
a8630 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f xxxx.**.**.** BO
a8640 4d 20 6f 72 20 42 79 74 65 20 4f 72 64 65 72 20 M or Byte Order
a8650 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66 Mark:.** 0xf
a8660 66 20 30 78 66 65 20 20 20 6c 69 74 74 6c 65 2d f 0xfe little-
a8670 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f endian utf-16 fo
a8680 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78 66 llows.** 0xf
a8690 65 20 30 78 66 66 20 20 20 62 69 67 2d 65 6e 64 e 0xff big-end
a86a0 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f ian utf-16 follo
a86b0 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a ws.**.*/./******
a86c0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
a86d0 20 76 64 62 65 49 6e 74 2e 68 20 69 6e 20 74 68 vdbeInt.h in th
a86e0 65 20 6d 69 64 64 6c 65 20 6f 66 20 75 74 66 2e e middle of utf.
a86f0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
a8700 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
a8710 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
a8720 69 6c 65 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a ile vdbeInt.h **
a8730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8750 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
a8760 30 30 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 003 September 6.
a8770 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
a8780 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
a8790 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
a87a0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
a87b0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
a87c0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
a87d0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
a87e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
a87f0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
a8800 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
a8810 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
a8820 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
a8830 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
a8840 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
a8850 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
a8860 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
a8870 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
a8880 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
a8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a88a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a88b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a88c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
a88d0 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 68 65 * This is the he
a88e0 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 69 6e ader file for in
a88f0 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 formation that i
a8900 73 20 70 72 69 76 61 74 65 20 74 6f 20 74 68 65 s private to the
a8910 0a 2a 2a 20 56 44 42 45 2e 20 20 54 68 69 73 20 .** VDBE. This
a8920 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 information used
a8930 20 74 6f 20 61 6c 6c 20 62 65 20 61 74 20 74 68 to all be at th
a8940 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 69 6e e top of the sin
a8950 67 6c 65 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f gle.** source co
a8960 64 65 20 66 69 6c 65 20 22 76 64 62 65 2e 63 22 de file "vdbe.c"
a8970 2e 20 20 57 68 65 6e 20 74 68 61 74 20 66 69 6c . When that fil
a8980 65 20 62 65 63 61 6d 65 20 74 6f 6f 20 62 69 67 e became too big
a8990 20 28 6f 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c (over.** 6000 l
a89a0 69 6e 65 73 20 6c 6f 6e 67 29 20 69 74 20 77 61 ines long) it wa
a89b0 73 20 73 70 6c 69 74 20 75 70 20 69 6e 74 6f 20 s split up into
a89c0 73 65 76 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20 several smaller
a89d0 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69 files and.** thi
a89e0 73 20 68 65 61 64 65 72 20 69 6e 66 6f 72 6d 61 s header informa
a89f0 74 69 6f 6e 20 77 61 73 20 66 61 63 74 6f 72 65 tion was factore
a8a00 64 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 d out..**.** $Id
a8a10 3a 20 76 64 62 65 49 6e 74 2e 68 2c 76 20 31 2e : vdbeInt.h,v 1.
a8a20 31 35 37 20 32 30 30 38 2f 31 31 2f 30 35 20 31 157 2008/11/05 1
a8a30 36 3a 33 37 3a 33 35 20 64 72 68 20 45 78 70 20 6:37:35 drh Exp
a8a40 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 $.*/.#ifndef _VD
a8a50 42 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 BEINT_H_.#define
a8a60 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a _VDBEINT_H_../*
a8a70 0a 2a 2a 20 69 6e 74 54 6f 4b 65 79 28 29 20 61 .** intToKey() a
a8a80 6e 64 20 6b 65 79 54 6f 49 6e 74 28 29 20 75 73 nd keyToInt() us
a8a90 65 64 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 ed to transform
a8aa0 74 68 65 20 72 6f 77 69 64 2e 20 20 42 75 74 20 the rowid. But
a8ab0 77 69 74 68 0a 2a 2a 20 74 68 65 20 6c 61 74 65 with.** the late
a8ac0 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 st versions of t
a8ad0 68 65 20 64 65 73 69 67 6e 20 74 68 65 79 20 61 he design they a
a8ae0 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 64 re no-ops..*/.#d
a8af0 65 66 69 6e 65 20 6b 65 79 54 6f 49 6e 74 28 58 efine keyToInt(X
a8b00 29 20 20 20 28 58 29 0a 23 64 65 66 69 6e 65 20 ) (X).#define
a8b10 69 6e 74 54 6f 4b 65 79 28 58 29 20 20 20 28 58 intToKey(X) (X
a8b20 29 0a 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 ).../*.** SQL is
a8b30 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f translated into
a8b40 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69 a sequence of i
a8b50 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 6f 20 62 nstructions to b
a8b60 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 62 79 e.** executed by
a8b70 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 a virtual machi
a8b80 6e 65 2e 20 20 45 61 63 68 20 69 6e 73 74 72 75 ne. Each instru
a8b90 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 ction is an inst
a8ba0 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 ance.** of the f
a8bb0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
a8bc0 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 re..*/.typedef s
a8bd0 74 72 75 63 74 20 56 64 62 65 4f 70 20 4f 70 3b truct VdbeOp Op;
a8be0 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 ../*.** Boolean
a8bf0 76 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65 values.*/.typede
a8c00 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 f unsigned char
a8c10 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 Bool;../*.** A c
a8c20 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 ursor is a point
a8c30 65 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 er into a single
a8c40 20 42 54 72 65 65 20 77 69 74 68 69 6e 20 61 20 BTree within a
a8c50 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
a8c60 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e * The cursor can
a8c70 20 73 65 65 6b 20 74 6f 20 61 20 42 54 72 65 65 seek to a BTree
a8c80 20 65 6e 74 72 79 20 77 69 74 68 20 61 20 70 61 entry with a pa
a8c90 72 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72 rticular key, or
a8ca0 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c .** loop over al
a8cb0 6c 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 l entries of the
a8cc0 20 42 74 72 65 65 2e 20 20 59 6f 75 20 63 61 6e Btree. You can
a8cd0 20 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65 77 also insert new
a8ce0 20 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65 BTree.** entrie
a8cf0 73 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 s or retrieve th
a8d00 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 72 e key or data fr
a8d10 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 om the entry tha
a8d20 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 t the cursor.**
a8d30 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
a8d40 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a nting to..** .**
a8d50 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 74 68 Every cursor th
a8d60 61 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d at the virtual m
a8d70 61 63 68 69 6e 65 20 68 61 73 20 6f 70 65 6e 20 achine has open
a8d80 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 is represented b
a8d90 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 y an.** instance
a8da0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
a8db0 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
a8dc0 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65 43 75 ** If the VdbeCu
a8dd0 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52 6f rsor.isTriggerRo
a8de0 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 74 w flag is set it
a8df0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 means that this
a8e00 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65 cursor is.** re
a8e10 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f ally a single ro
a8e20 77 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 w that represent
a8e30 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 s the NEW or OLD
a8e40 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66 pseudo-table of
a8e50 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67 65 .** a row trigge
a8e60 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f 72 r. The data for
a8e70 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f 72 the row is stor
a8e80 65 64 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 ed in VdbeCursor
a8e90 2e 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68 .pData and.** th
a8ea0 65 20 72 6f 77 69 64 20 69 73 20 69 6e 20 56 64 e rowid is in Vd
a8eb0 62 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a beCursor.iKey..*
a8ec0 2f 0a 73 74 72 75 63 74 20 56 64 62 65 43 75 72 /.struct VdbeCur
a8ed0 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 6f 72 sor {. BtCursor
a8ee0 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a *pCursor; /*
a8ef0 20 54 68 65 20 63 75 72 73 6f 72 20 73 74 72 75 The cursor stru
a8f00 63 74 75 72 65 20 6f 66 20 74 68 65 20 62 61 63 cture of the bac
a8f10 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 kend */. int iD
a8f20 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
a8f30 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 73 /* Index of curs
a8f40 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 or database in d
a8f50 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29 b->aDb[] (or -1)
a8f60 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f */. i64 lastRo
a8f70 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c wid; /* L
a8f80 61 73 74 20 72 6f 77 69 64 20 66 72 6f 6d 20 61 ast rowid from a
a8f90 20 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 64 78 Next or NextIdx
a8fa0 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 operation */.
a8fb0 69 36 34 20 6e 65 78 74 52 6f 77 69 64 3b 20 20 i64 nextRowid;
a8fc0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 72 6f /* Next ro
a8fd0 77 69 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 wid returned by
a8fe0 4f 50 5f 4e 65 77 52 6f 77 69 64 20 2a 2f 0a 20 OP_NewRowid */.
a8ff0 20 42 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20 Bool zeroed;
a9000 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a9010 66 20 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64 f zeroed out and
a9020 20 72 65 61 64 79 20 66 6f 72 20 72 65 75 73 65 ready for reuse
a9030 20 2a 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64 */. Bool rowid
a9040 49 73 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54 IsValid; /* T
a9050 72 75 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64 rue if lastRowid
a9060 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 is valid */. B
a9070 6f 6f 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20 ool atFirst;
a9080 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a9090 70 6f 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73 pointing to firs
a90a0 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f t entry */. Boo
a90b0 6c 20 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 l useRandomRowid
a90c0 3b 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e ; /* Generate n
a90d0 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 ew record number
a90e0 73 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 s semi-randomly
a90f0 2a 2f 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f */. Bool nullRo
a9100 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 w; /* Tr
a9110 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 ue if pointing t
a9120 6f 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20 o a row with no
a9130 64 61 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e data */. Bool n
a9140 65 78 74 52 6f 77 69 64 56 61 6c 69 64 3b 20 20 extRowidValid;
a9150 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 6e /* True if the n
a9160 65 78 74 52 6f 77 69 64 20 66 69 65 6c 64 20 69 extRowid field i
a9170 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f s valid */. Boo
a9180 6c 20 70 73 65 75 64 6f 54 61 62 6c 65 3b 20 20 l pseudoTable;
a9190 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 /* This is a
a91a0 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 NEW or OLD pseud
a91b0 6f 2d 74 61 62 6c 65 73 20 6f 66 20 61 20 74 72 o-tables of a tr
a91c0 69 67 67 65 72 20 2a 2f 0a 20 20 42 6f 6f 6c 20 igger */. Bool
a91d0 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 ephemPseudoTable
a91e0 3b 0a 20 20 42 6f 6f 6c 20 64 65 66 65 72 72 65 ;. Bool deferre
a91f0 64 4d 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20 63 dMoveto; /* A c
a9200 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 all to sqlite3Bt
a9210 72 65 65 4d 6f 76 65 74 6f 28 29 20 69 73 20 6e reeMoveto() is n
a9220 65 65 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 eeded */. Bool
a9230 69 73 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 isTable;
a9240 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 74 61 /* True if a ta
a9250 62 6c 65 20 72 65 71 75 69 72 69 6e 67 20 69 6e ble requiring in
a9260 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20 20 teger keys */.
a9270 42 6f 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20 20 Bool isIndex;
a9280 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a9290 20 61 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61 69 an index contai
a92a0 6e 69 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20 2d ning keys only -
a92b0 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 36 no data */. i6
a92c0 34 20 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 20 4 movetoTarget;
a92d0 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 /* Argument
a92e0 74 6f 20 74 68 65 20 64 65 66 65 72 72 65 64 20 to the deferred
a92f0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
a9300 74 6f 28 29 20 2a 2f 0a 20 20 42 74 72 65 65 20 to() */. Btree
a9310 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 *pBt;
a9320 2f 2a 20 53 65 70 61 72 61 74 65 20 66 69 6c 65 /* Separate file
a9330 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 holding tempora
a9340 72 79 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e ry table */. in
a9350 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 t nData;
a9360 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
a9370 20 62 79 74 65 73 20 69 6e 20 70 44 61 74 61 20 bytes in pData
a9380 2a 2f 0a 20 20 63 68 61 72 20 2a 70 44 61 74 61 */. char *pData
a9390 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 ; /* Da
a93a0 74 61 20 66 6f 72 20 61 20 4e 45 57 20 6f 72 20 ta for a NEW or
a93b0 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 OLD pseudo-table
a93c0 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 */. i64 iKey;
a93d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b /* K
a93e0 65 79 20 66 6f 72 20 74 68 65 20 4e 45 57 20 6f ey for the NEW o
a93f0 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 r OLD pseudo-tab
a9400 6c 65 20 72 6f 77 20 2a 2f 0a 20 20 4b 65 79 49 le row */. KeyI
a9410 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 nfo *pKeyInfo;
a9420 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 20 /* Info about
a9430 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65 64 65 index keys neede
a9440 64 20 62 79 20 69 6e 64 65 78 20 63 75 72 73 6f d by index curso
a9450 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 rs */. int nFie
a9460 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ld; /*
a9470 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 Number of field
a9480 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 s in the header
a9490 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f 75 6e */. i64 seqCoun
a94a0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 t; /* Se
a94b0 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 2a quence counter *
a94c0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 /. sqlite3_vtab
a94d0 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 _cursor *pVtabCu
a94e0 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 63 75 rsor; /* The cu
a94f0 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72 74 75 rsor for a virtu
a9500 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f al table */. co
a9510 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
a9520 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 le *pModule;
a9530 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 20 63 /* Module for c
a9540 75 72 73 6f 72 20 70 56 74 61 62 43 75 72 73 6f ursor pVtabCurso
a9550 72 20 2a 2f 0a 0a 20 20 2f 2a 20 43 61 63 68 65 r */.. /* Cache
a9560 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 d information ab
a9570 6f 75 74 20 74 68 65 20 68 65 61 64 65 72 20 66 out the header f
a9580 6f 72 20 74 68 65 20 64 61 74 61 20 72 65 63 6f or the data reco
a9590 72 64 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a rd that the. **
a95a0 20 63 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 cursor is curre
a95b0 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f ntly pointing to
a95c0 2e 20 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 . Only valid if
a95d0 20 63 61 63 68 65 56 61 6c 69 64 20 69 73 20 74 cacheValid is t
a95e0 72 75 65 2e 0a 20 20 2a 2a 20 61 52 6f 77 20 6d rue.. ** aRow m
a95f0 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 ight point to (e
a9600 70 68 65 6d 65 72 61 6c 29 20 64 61 74 61 20 66 phemeral) data f
a9610 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 or the current r
a9620 6f 77 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 0a ow, or it might.
a9630 20 20 2a 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 ** be NULL..
a9640 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 53 74 */. int cacheSt
a9650 61 74 75 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 atus; /* Ca
a9660 63 68 65 20 69 73 20 76 61 6c 69 64 20 69 66 20 che is valid if
a9670 74 68 69 73 20 6d 61 74 63 68 65 73 20 56 64 62 this matches Vdb
a9680 65 2e 63 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 e.cacheCtr */.
a9690 69 6e 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b int payloadSize;
a96a0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
a96b0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
a96c0 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a n the record */.
a96d0 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 u32 *aType;
a96e0 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 /* Type
a96f0 76 61 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 values for all e
a9700 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 ntries in the re
a9710 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 cord */. u32 *a
a9720 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
a9730 2f 2a 20 43 61 63 68 65 64 20 6f 66 66 73 65 74 /* Cached offset
a9740 73 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f s to the start o
a9750 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 f each columns d
a9760 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f ata */. u8 *aRo
a9770 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f w; /
a9780 2a 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 63 * Data for the c
a9790 75 72 72 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 urrent row, if a
a97a0 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a ll on one page *
a97b0 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 /.};.typedef str
a97c0 75 63 74 20 56 64 62 65 43 75 72 73 6f 72 20 56 uct VdbeCursor V
a97d0 64 62 65 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a dbeCursor;../*.*
a97e0 2a 20 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64 * A value for Vd
a97f0 62 65 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61 beCursor.cacheVa
a9800 6c 69 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74 lid that means t
a9810 68 65 20 63 61 63 68 65 20 69 73 20 61 6c 77 61 he cache is alwa
a9820 79 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 ys invalid..*/.#
a9830 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41 define CACHE_STA
a9840 4c 45 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 LE 0../*.** Inte
a9850 72 6e 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 rnally, the vdbe
a9860 20 6d 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 manipulates nea
a9870 72 6c 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 rly all SQL valu
a9880 65 73 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 es as Mem.** str
a9890 75 63 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65 uctures. Each Me
a98a0 6d 20 73 74 72 75 63 74 20 6d 61 79 20 63 61 63 m struct may cac
a98b0 68 65 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 he multiple repr
a98c0 65 73 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 esentations (str
a98d0 69 6e 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 ing,.** integer
a98e0 65 74 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d etc.) of the sam
a98f0 65 20 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 e value. A valu
a9900 65 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 e (and therefore
a9910 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a Mem structure).
a9920 2a 2a 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f ** has the follo
a9930 77 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a wing properties:
a9940 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 .**.** Each valu
a9950 65 20 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74 e has a manifest
a9960 20 74 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 type. The manif
a9970 65 73 74 20 74 79 70 65 20 6f 66 20 74 68 65 20 est type of the
a9980 76 61 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 value stored.**
a9990 69 6e 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20 in a Mem struct
a99a0 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 is returned by t
a99b0 68 65 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 he MemType(Mem*)
a99c0 20 6d 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65 macro. The type
a99d0 20 69 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 is.** one of SQ
a99e0 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 LITE_NULL, SQLIT
a99f0 45 5f 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 E_INTEGER, SQLIT
a9a00 45 5f 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 E_REAL, SQLITE_T
a9a10 45 58 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 EXT or.** SQLITE
a9a20 5f 42 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 _BLOB..*/.struct
a9a30 20 4d 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b Mem {. union {
a9a40 0a 20 20 20 20 69 36 34 20 69 3b 20 20 20 20 20 . i64 i;
a9a50 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 /* Inte
a9a60 67 65 72 20 76 61 6c 75 65 2e 20 4f 72 20 46 75 ger value. Or Fu
a9a70 6e 63 44 65 66 2a 20 77 68 65 6e 20 66 6c 61 67 ncDef* when flag
a9a80 73 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 s==MEM_Agg */.
a9a90 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b FuncDef *pDef;
a9aa0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e /* Used on
a9ab0 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d ly when flags==M
a9ac0 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 7d 20 75 3b EM_Agg */. } u;
a9ad0 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 . double r;
a9ae0 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 /* Real v
a9af0 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 alue */. sqlite
a9b00 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 3 *db; /*
a9b10 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 The associated
a9b20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
a9b30 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ion */. char *z
a9b40 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a9b50 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 String or BLOB v
a9b60 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b alue */. int n;
a9b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a9b80 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 Number of chara
a9b90 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e 67 20 cters in string
a9ba0 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69 6e 67 value, excluding
a9bb0 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36 20 66 '\0' */. u16 f
a9bc0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f lags; /
a9bd0 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 * Some combinati
a9be0 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 on of MEM_Null,
a9bf0 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e MEM_Str, MEM_Dyn
a9c00 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20 , etc. */. u8
a9c10 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 type;
a9c20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45 /* One of SQLITE
a9c30 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45 _NULL, SQLITE_TE
a9c40 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 XT, SQLITE_INTEG
a9c50 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75 38 20 ER, etc */. u8
a9c60 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 enc;
a9c70 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c /* SQLITE_UTF8,
a9c80 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c SQLITE_UTF16BE,
a9c90 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 SQLITE_UTF16LE
a9ca0 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c */. void (*xDel
a9cb0 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 )(void *); /* I
a9cc0 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c f not null, call
a9cd0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 this function t
a9ce0 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a o delete Mem.z *
a9cf0 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f /. char *zMallo
a9d00 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d c; /* Dynam
a9d10 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 ic buffer alloca
a9d20 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d ted by sqlite3_m
a9d30 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f alloc() */.};../
a9d40 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 * One or more of
a9d50 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
a9d60 6c 61 67 73 20 61 72 65 20 73 65 74 20 74 6f 20 lags are set to
a9d70 69 6e 64 69 63 61 74 65 20 74 68 65 20 76 61 6c indicate the val
a9d80 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e idOK.** represen
a9d90 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 tations of the v
a9da0 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 74 alue stored in t
a9db0 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a he Mem struct..*
a9dc0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f *.** If the MEM_
a9dd0 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73 65 74 Null flag is set
a9de0 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 , then the value
a9df0 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 is an SQL NULL
a9e00 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 value..** No oth
a9e10 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62 65 20 er flags may be
a9e20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61 73 65 set in this case
a9e30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d ..**.** If the M
a9e40 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73 20 73 EM_Str flag is s
a9e50 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f et then Mem.z po
a9e60 69 6e 74 73 20 61 74 20 61 20 73 74 72 69 6e 67 ints at a string
a9e70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e representation.
a9e80 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68 69 73 .** Usually this
a9e90 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 is encoded in t
a9ea0 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 he same unicode
a9eb0 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20 encoding as the
a9ec0 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 main.** database
a9ed0 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 (see below for
a9ee0 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 exceptions). If
a9ef0 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 the MEM_Term fla
a9f00 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 g is also.** set
a9f10 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 69 6e , then the strin
a9f20 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 g is nul termina
a9f30 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 ted. The MEM_Int
a9f40 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a and MEM_Real .*
a9f50 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 * flags may coex
a9f60 69 73 74 20 77 69 74 68 20 74 68 65 20 4d 45 4d ist with the MEM
a9f70 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a _Str flag..**.**
a9f80 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 Multiple of the
a9f90 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 61 70 se values can ap
a9fa0 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 pear in Mem.flag
a9fb0 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 s. But only one
a9fc0 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 63 61 .** at a time ca
a9fd0 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e n appear in Mem.
a9fe0 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 type..*/.#define
a9ff0 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 MEM_Null 0
aa000 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 x0001 /* Value
aa010 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 is NULL */.#def
aa020 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 ine MEM_Str
aa030 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20 56 61 0x0002 /* Va
aa040 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 lue is a string
aa050 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 */.#define MEM_I
aa060 6e 74 20 20 20 20 20 20 20 30 78 30 30 30 34 20 nt 0x0004
aa070 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 6e /* Value is an
aa080 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 integer */.#def
aa090 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 ine MEM_Real
aa0a0 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20 56 61 0x0008 /* Va
aa0b0 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20 6e 75 lue is a real nu
aa0c0 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mber */.#define
aa0d0 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 MEM_Blob 0x
aa0e0 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 0010 /* Value
aa0f0 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 0a 23 64 is a BLOB */..#d
aa100 65 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65 efine MemSetType
aa110 46 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 28 Flag(p, f) \. (
aa120 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28 70 (p)->flags = ((p
aa130 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f 49 )->flags&~(MEM_I
aa140 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f nt|MEM_Real|MEM_
aa150 4e 75 6c 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 Null|MEM_Blob|ME
aa160 4d 5f 53 74 72 29 29 7c 66 29 0a 0a 2f 2a 20 57 M_Str))|f)../* W
aa170 68 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e 74 henever Mem cont
aa180 61 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74 72 ains a valid str
aa190 69 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70 72 ing or blob repr
aa1a0 65 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65 20 esentation, one
aa1b0 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 of.** the follow
aa1c0 69 6e 67 20 66 6c 61 67 73 20 6d 75 73 74 20 62 ing flags must b
aa1d0 65 20 73 65 74 20 74 6f 20 64 65 74 65 72 6d 69 e set to determi
aa1e0 6e 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d 61 ne the memory ma
aa1f0 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c 69 nagement.** poli
aa200 63 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20 54 cy for Mem.z. T
aa210 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 he MEM_Term flag
aa220 20 74 65 6c 6c 73 20 75 73 20 77 68 65 74 68 65 tells us whethe
aa230 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 2a 2a 20 r or not the.**
aa240 73 74 72 69 6e 67 20 69 73 20 5c 30 30 30 20 6f string is \000 o
aa250 72 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 r \u0000 termina
aa260 74 65 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ted.*/.#define M
aa270 45 4d 5f 54 65 72 6d 20 20 20 20 20 20 30 78 30 EM_Term 0x0
aa280 30 32 30 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 020 /* String
aa290 72 65 70 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 rep is nul termi
aa2a0 6e 61 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 nated */.#define
aa2b0 20 4d 45 4d 5f 44 79 6e 20 20 20 20 20 20 20 30 MEM_Dyn 0
aa2c0 78 30 30 34 30 20 20 20 2f 2a 20 4e 65 65 64 20 x0040 /* Need
aa2d0 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 46 72 to call sqliteFr
aa2e0 65 65 28 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f ee() on Mem.z */
aa2f0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 61 .#define MEM_Sta
aa300 74 69 63 20 20 20 20 30 78 30 30 38 30 20 20 20 tic 0x0080
aa310 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 /* Mem.z points
aa320 74 6f 20 61 20 73 74 61 74 69 63 20 73 74 72 69 to a static stri
aa330 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 ng */.#define ME
aa340 4d 5f 45 70 68 65 6d 20 20 20 20 20 30 78 30 31 M_Ephem 0x01
aa350 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 00 /* Mem.z po
aa360 69 6e 74 73 20 74 6f 20 61 6e 20 65 70 68 65 6d ints to an ephem
aa370 65 72 61 6c 20 73 74 72 69 6e 67 20 2a 2f 0a 23 eral string */.#
aa380 64 65 66 69 6e 65 20 4d 45 4d 5f 41 67 67 20 20 define MEM_Agg
aa390 20 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 0x0400 /*
aa3a0 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f Mem.z points to
aa3b0 20 61 6e 20 61 67 67 20 66 75 6e 63 74 69 6f 6e an agg function
aa3c0 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 64 65 66 context */.#def
aa3d0 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 20 20 20 ine MEM_Zero
aa3e0 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 0x0800 /* Me
aa3f0 6d 2e 69 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 m.i contains cou
aa400 6e 74 20 6f 66 20 30 73 20 61 70 70 65 6e 64 65 nt of 0s appende
aa410 64 20 74 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23 69 d to blob */..#i
aa420 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
aa430 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 23 75 6e 64 _INCRBLOB. #und
aa440 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23 64 ef MEM_Zero. #d
aa450 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 30 efine MEM_Zero 0
aa460 78 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f x0000.#endif.../
aa470 2a 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 * A VdbeFunc is
aa480 6a 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 just a FuncDef (
aa490 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 defined in sqlit
aa4a0 65 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e eInt.h) that con
aa4b0 74 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f tains.** additio
aa4c0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 nal information
aa4d0 61 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 about auxiliary
aa4e0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e information boun
aa4f0 64 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a d to arguments.*
aa500 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f * of the functio
aa510 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 n. This is used
aa520 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
aa530 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 e sqlite3_get_au
aa540 78 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 xdata().** and s
aa550 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
aa560 74 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 ta() APIs. The
aa570 22 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d "auxdata" is som
aa580 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 e auxiliary data
aa590 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 .** that can be
aa5a0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
aa5b0 61 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d a constant argum
aa5c0 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ent to a functio
aa5d0 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f n. This.** allo
aa5e0 77 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 ws functions suc
aa5f0 68 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f h as "regexp" to
aa600 20 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 compile their c
aa610 6f 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a onstant regular.
aa620 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 ** expression ar
aa630 67 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 gument once and
aa640 72 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 reused the compi
aa650 6c 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c led code for mul
aa660 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 tiple.** invocat
aa670 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ions..*/.struct
aa680 56 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e VdbeFunc {. Fun
aa690 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 cDef *pFunc;
aa6a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
aa6b0 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 e definition of
aa6c0 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a the function */.
aa6d0 20 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 int nAux;
aa6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aa6f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
aa700 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 ries allocated f
aa710 6f 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 or apAux[] */.
aa720 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b struct AuxData {
aa730 0a 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b . void *pAux;
aa740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aa750 20 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 /* Aux data f
aa760 6f 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 or the i-th argu
aa770 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 ment */. void
aa780 20 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 (*xDelete)(void
aa790 20 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 *); /* Des
aa7a0 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 tructor for the
aa7b0 61 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 aux data */. }
aa7c0 61 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 apAux[1];
aa7d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
aa7e0 6e 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 ne slot for each
aa7f0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
aa800 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 nt */.};../*.**
aa810 54 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 The "context" ar
aa820 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 gument for a ins
aa830 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f tallable functio
aa840 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f n. A pointer to
aa850 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 an.** instance
aa860 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
aa870 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 e is the first a
aa880 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 rgument to the r
aa890 6f 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 outines used.**
aa8a0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 implement the SQ
aa8b0 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a L functions..**.
aa8c0 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79 ** There is a ty
aa8d0 70 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 pedef for this s
aa8e0 74 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 tructure in sqli
aa8f0 74 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f te.h. So all ro
aa900 75 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 utines,.** even
aa910 74 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 the public inter
aa920 66 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 face to SQLite,
aa930 63 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 can use a pointe
aa940 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 r to this struct
aa950 75 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 ure..** But this
aa960 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c file is the onl
aa970 79 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 y place where th
aa980 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 e internal detai
aa990 6c 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 ls of this.** st
aa9a0 72 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 ructure are know
aa9b0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 n..**.** This st
aa9c0 72 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e ructure is defin
aa9d0 65 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 ed inside of vdb
aa9e0 65 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 eInt.h because i
aa9f0 74 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74 t uses substruct
aaa00 75 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 ures.** (Mem) wh
aaa10 69 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 ich are only def
aaa20 69 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 ined there..*/.s
aaa30 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f truct sqlite3_co
aaa40 6e 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 ntext {. FuncDe
aaa50 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 f *pFunc;
aaa60 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 /* Pointer to fu
aaa70 6e 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 nction informati
aaa80 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 on. MUST BE FIR
aaa90 53 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 ST */. VdbeFunc
aaaa0 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a *pVdbeFunc; /*
aaab0 20 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 Auxilary data,
aaac0 69 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 if created. */.
aaad0 20 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 Mem s;
aaae0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 /* The re
aaaf0 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 turn value is st
aab00 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d ored here */. M
aab10 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 em *pMem;
aab20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 /* Memory c
aab30 65 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 ell used to stor
aab40 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 e aggregate cont
aab50 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 ext */. int isE
aab60 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f rror; /
aab70 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 * Error code ret
aab80 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e urned by the fun
aab90 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c ction. */. Coll
aaba0 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 Seq *pColl;
aabb0 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 /* Collating s
aabc0 65 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f equence */.};../
aabd0 2a 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 *.** A Set struc
aabe0 74 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 ture is used for
aabf0 20 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 quick testing t
aac00 6f 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 o see if a value
aac10 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 .** is part of a
aac20 20 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 small set. Set
aac30 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d s are used to im
aac40 70 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b plement code lik
aac50 65 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 e.** this:.**
aac60 20 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 x.y IN
aac70 28 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d ('hi','hoo','hum
aac80 27 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 ').*/.typedef st
aac90 72 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 ruct Set Set;.st
aaca0 72 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 ruct Set {. Has
aacb0 68 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 h hash;
aacc0 20 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 /* A set is
aacd0 6a 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c just a hash tabl
aace0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 e */. HashElem
aacf0 2a 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a *prev; /*
aad00 20 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 Previously acce
aad10 73 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e ssed hash elemen
aad20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 */.};../*.** A
aad30 46 69 66 6f 50 61 67 65 20 73 74 72 75 63 74 75 FifoPage structu
aad40 72 65 20 68 6f 6c 64 73 20 61 20 73 69 6e 67 6c re holds a singl
aad50 65 20 70 61 67 65 20 6f 66 20 76 61 6c 76 65 73 e page of valves
aad60 2e 20 20 50 61 67 65 73 20 61 72 65 20 61 72 72 . Pages are arr
aad70 61 6e 67 65 64 0a 2a 2a 20 69 6e 20 61 20 6c 69 anged.** in a li
aad80 73 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 st..*/.typedef s
aad90 74 72 75 63 74 20 46 69 66 6f 50 61 67 65 20 46 truct FifoPage F
aada0 69 66 6f 50 61 67 65 3b 0a 73 74 72 75 63 74 20 ifoPage;.struct
aadb0 46 69 66 6f 50 61 67 65 20 7b 0a 20 20 69 6e 74 FifoPage {. int
aadc0 20 6e 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 nSlot;
aadd0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
aade0 72 69 65 73 20 61 53 6c 6f 74 5b 5d 20 2a 2f 0a ries aSlot[] */.
aadf0 20 20 69 6e 74 20 69 57 72 69 74 65 3b 20 20 20 int iWrite;
aae00 20 20 20 20 20 2f 2a 20 50 75 73 68 20 74 68 65 /* Push the
aae10 20 6e 65 78 74 20 76 61 6c 75 65 20 69 6e 74 6f next value into
aae20 20 74 68 69 73 20 65 6e 74 72 79 20 69 6e 20 61 this entry in a
aae30 53 6c 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 Slot[] */. int
aae40 69 52 65 61 64 3b 20 20 20 20 20 20 20 20 20 2f iRead; /
aae50 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78 74 20 * Read the next
aae60 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 20 value from this
aae70 65 6e 74 72 79 20 69 6e 20 61 53 6c 6f 74 5b 5d entry in aSlot[]
aae80 20 2a 2f 0a 20 20 46 69 66 6f 50 61 67 65 20 2a */. FifoPage *
aae90 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 pNext; /* Next
aaea0 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 66 page in the fif
aaeb0 6f 20 2a 2f 0a 20 20 69 36 34 20 61 53 6c 6f 74 o */. i64 aSlot
aaec0 5b 31 5d 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 [1]; /* One
aaed0 20 6f 72 20 6d 6f 72 65 20 73 6c 6f 74 73 20 66 or more slots f
aaee0 6f 72 20 72 6f 77 69 64 20 76 61 6c 75 65 73 20 or rowid values
aaef0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
aaf00 20 46 69 66 6f 20 73 74 72 75 63 74 75 72 65 20 Fifo structure
aaf10 69 73 20 74 79 70 65 64 65 66 2d 65 64 20 69 6e is typedef-ed in
aaf20 20 76 64 62 65 49 6e 74 2e 68 2e 20 20 42 75 74 vdbeInt.h. But
aaf30 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
aaf40 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73 ion.** of that s
aaf50 74 72 75 63 74 75 72 65 20 69 73 20 70 72 69 76 tructure is priv
aaf60 61 74 65 20 74 6f 20 74 68 69 73 20 66 69 6c 65 ate to this file
aaf70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 46 69 66 6f ..**.** The Fifo
aaf80 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 structure descr
aaf90 69 62 65 73 20 74 68 65 20 65 6e 74 69 72 65 20 ibes the entire
aafa0 66 69 66 6f 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 fifo. .*/.typed
aafb0 65 66 20 73 74 72 75 63 74 20 46 69 66 6f 20 46 ef struct Fifo F
aafc0 69 66 6f 3b 0a 73 74 72 75 63 74 20 46 69 66 6f ifo;.struct Fifo
aafd0 20 7b 0a 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b {. int nEntry;
aafe0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
aaff0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 l number of entr
ab000 69 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ies */. sqlite3
ab010 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 *db; /*
ab020 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 The associated d
ab030 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
ab040 6f 6e 20 2a 2f 0a 20 20 46 69 66 6f 50 61 67 65 on */. FifoPage
ab050 20 2a 70 46 69 72 73 74 3b 20 20 20 2f 2a 20 46 *pFirst; /* F
ab060 69 72 73 74 20 70 61 67 65 20 6f 6e 20 74 68 65 irst page on the
ab070 20 6c 69 73 74 20 2a 2f 0a 20 20 46 69 66 6f 50 list */. FifoP
ab080 61 67 65 20 2a 70 4c 61 73 74 3b 20 20 20 20 2f age *pLast; /
ab090 2a 20 4c 61 73 74 20 70 61 67 65 20 6f 6e 20 74 * Last page on t
ab0a0 68 65 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f he list */.};../
ab0b0 2a 0a 2a 2a 20 41 20 43 6f 6e 74 65 78 74 20 73 *.** A Context s
ab0c0 74 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 tores the last i
ab0d0 6e 73 65 72 74 20 72 6f 77 69 64 2c 20 74 68 65 nsert rowid, the
ab0e0 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 20 last statement
ab0f0 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c 0a 2a 2a change count,.**
ab100 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 and the current
ab110 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 statement chang
ab120 65 20 63 6f 75 6e 74 20 28 69 2e 65 2e 20 63 68 e count (i.e. ch
ab130 61 6e 67 65 73 20 73 69 6e 63 65 20 6c 61 73 74 anges since last
ab140 20 73 74 61 74 65 6d 65 6e 74 29 2e 0a 2a 2a 20 statement)..**
ab150 54 68 65 20 63 75 72 72 65 6e 74 20 6b 65 79 6c The current keyl
ab160 69 73 74 20 69 73 20 61 6c 73 6f 20 73 74 6f 72 ist is also stor
ab170 65 64 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 ed in the contex
ab180 74 2e 0a 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 6f t..** Elements o
ab190 66 20 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 f Context struct
ab1a0 75 72 65 20 74 79 70 65 20 6d 61 6b 65 20 75 70 ure type make up
ab1b0 20 74 68 65 20 43 6f 6e 74 65 78 74 53 74 61 63 the ContextStac
ab1c0 6b 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 75 k, which is.** u
ab1d0 70 64 61 74 65 64 20 62 79 20 74 68 65 20 43 6f pdated by the Co
ab1e0 6e 74 65 78 74 50 75 73 68 20 61 6e 64 20 43 6f ntextPush and Co
ab1f0 6e 74 65 78 74 50 6f 70 20 6f 70 63 6f 64 65 73 ntextPop opcodes
ab200 20 28 75 73 65 64 20 62 79 20 74 72 69 67 67 65 (used by trigge
ab210 72 73 29 2e 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 rs)..** The cont
ab220 65 78 74 20 69 73 20 70 75 73 68 65 64 20 62 65 ext is pushed be
ab230 66 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 61 fore executing a
ab240 20 74 72 69 67 67 65 72 20 61 20 70 6f 70 70 65 trigger a poppe
ab250 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 d when the.** tr
ab260 69 67 67 65 72 20 66 69 6e 69 73 68 65 73 2e 0a igger finishes..
ab270 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
ab280 74 20 43 6f 6e 74 65 78 74 20 43 6f 6e 74 65 78 t Context Contex
ab290 74 3b 0a 73 74 72 75 63 74 20 43 6f 6e 74 65 78 t;.struct Contex
ab2a0 74 20 7b 0a 20 20 69 36 34 20 6c 61 73 74 52 6f t {. i64 lastRo
ab2b0 77 69 64 3b 20 20 20 20 2f 2a 20 4c 61 73 74 20 wid; /* Last
ab2c0 69 6e 73 65 72 74 20 72 6f 77 69 64 20 28 73 71 insert rowid (sq
ab2d0 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 69 64 29 lite3.lastRowid)
ab2e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 */. int nChang
ab2f0 65 3b 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 e; /* State
ab300 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 28 56 64 ment changes (Vd
ab310 62 65 2e 6e 43 68 61 6e 67 65 73 29 20 20 20 20 be.nChanges)
ab320 20 2a 2f 0a 20 20 46 69 66 6f 20 73 46 69 66 6f */. Fifo sFifo
ab330 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 72 ; /* Recor
ab340 64 73 20 74 68 61 74 20 77 69 6c 6c 20 70 61 72 ds that will par
ab350 74 69 63 69 70 61 74 65 20 69 6e 20 61 20 44 45 ticipate in a DE
ab360 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 2a LETE or UPDATE *
ab370 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 /.};../*.** An i
ab380 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 76 nstance of the v
ab390 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 irtual machine.
ab3a0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
ab3b0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d contains the com
ab3c0 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65 20 6f plete.** state o
ab3d0 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 f the virtual ma
ab3e0 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 chine..**.** The
ab3f0 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 "sqlite3_stmt"
ab400 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 structure pointe
ab410 72 20 74 68 61 74 20 69 73 20 72 65 74 75 72 6e r that is return
ab420 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f ed by sqlite3_co
ab430 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20 72 65 mpile().** is re
ab440 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 ally a pointer t
ab450 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 o an instance of
ab460 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e this structure.
ab470 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e .**.** The Vdbe.
ab480 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76 61 72 inVtabMethod var
ab490 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 iable is set to
ab4a0 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 68 65 non-zero for the
ab4b0 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 duration of.**
ab4c0 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c any virtual tabl
ab4d0 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63 61 74 e method invocat
ab4e0 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74 68 65 ions made by the
ab4f0 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 49 vdbe program. I
ab500 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 32 t is.** set to 2
ab510 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20 6d 65 for xDestroy me
ab520 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64 20 31 thod calls and 1
ab530 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d for all other m
ab540 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a 2a 20 ethods. This.**
ab550 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 variable is used
ab560 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f 73 65 for two purpose
ab570 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44 65 73 s: to allow xDes
ab580 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74 6f 20 troy methods to
ab590 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52 4f 50 execute.** "DROP
ab5a0 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d 65 6e TABLE" statemen
ab5b0 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76 65 6e ts and to preven
ab5c0 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73 69 64 t some nasty sid
ab5d0 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a 2a 20 e effects of.**
ab5e0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 77 malloc failure w
ab5f0 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e hen SQLite is in
ab600 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76 65 6c voked recursivel
ab610 79 20 62 79 20 61 20 76 69 72 74 75 61 6c 20 74 y by a virtual t
ab620 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f 64 20 able .** method
ab630 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 function..*/.str
ab640 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73 71 6c uct Vdbe {. sql
ab650 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
ab660 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61 /* The whole da
ab670 74 61 62 61 73 65 20 2a 2f 0a 20 20 56 64 62 65 tabase */. Vdbe
ab680 20 2a 70 50 72 65 76 2c 2a 70 4e 65 78 74 3b 20 *pPrev,*pNext;
ab690 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f /* Linked list o
ab6a0 66 20 56 44 42 45 73 20 77 69 74 68 20 74 68 65 f VDBEs with the
ab6b0 20 73 61 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f same Vdbe.db */
ab6c0 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 . int nOp;
ab6d0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
ab6e0 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 of instructions
ab6f0 20 69 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20 in the program
ab700 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 41 6c 6c 6f */. int nOpAllo
ab710 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 c; /* Numb
ab720 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f er of slots allo
ab730 63 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 cated for aOp[]
ab740 2a 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 */. Op *aOp;
ab750 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 /* Spac
ab760 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 69 e to hold the vi
ab770 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 27 73 20 rtual machine's
ab780 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 program */. int
ab790 20 6e 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 nLabel;
ab7a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 61 /* Number of la
ab7b0 62 65 6c 73 20 75 73 65 64 20 2a 2f 0a 20 20 69 bels used */. i
ab7c0 6e 74 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b 20 nt nLabelAlloc;
ab7d0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
ab7e0 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 slots allocated
ab7f0 69 6e 20 61 4c 61 62 65 6c 5b 5d 20 2a 2f 0a 20 in aLabel[] */.
ab800 20 69 6e 74 20 2a 61 4c 61 62 65 6c 3b 20 20 20 int *aLabel;
ab810 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f /* Space to
ab820 20 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73 hold the labels
ab830 20 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 */. Mem **apAr
ab840 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 g; /* Arg
ab850 75 6d 65 6e 74 73 20 74 6f 20 63 75 72 72 65 6e uments to curren
ab860 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 75 73 tly executing us
ab870 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 er function */.
ab880 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b 20 Mem *aColName;
ab890 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e /* Column n
ab8a0 61 6d 65 73 20 74 6f 20 72 65 74 75 72 6e 20 2a ames to return *
ab8b0 2f 0a 20 20 69 6e 74 20 6e 43 75 72 73 6f 72 3b /. int nCursor;
ab8c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ab8d0 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 r of slots in ap
ab8e0 43 73 72 5b 5d 20 2a 2f 0a 20 20 56 64 62 65 43 Csr[] */. VdbeC
ab8f0 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 2f ursor **apCsr; /
ab900 2a 20 4f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 * One element of
ab910 20 74 68 69 73 20 61 72 72 61 79 20 66 6f 72 20 this array for
ab920 65 61 63 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 each open cursor
ab930 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 3b 20 */. int nVar;
ab940 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ab950 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
ab960 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d 65 n aVar[] */. Me
ab970 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20 20 m *aVar;
ab980 20 20 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 /* Values for
ab990 74 68 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 the OP_Variable
ab9a0 6f 70 63 6f 64 65 2e 20 2a 2f 0a 20 20 63 68 61 opcode. */. cha
ab9b0 72 20 2a 2a 61 7a 56 61 72 3b 20 20 20 20 20 20 r **azVar;
ab9c0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 76 61 72 69 /* Name of vari
ab9d0 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6f ables */. int o
ab9e0 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f kVar; /
ab9f0 2a 20 54 72 75 65 20 69 66 20 61 7a 56 61 72 5b * True if azVar[
aba00 5d 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69 ] has been initi
aba10 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 alized */. int
aba20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 magic;
aba30 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d /* Magic num
aba40 62 65 72 20 66 6f 72 20 73 61 6e 69 74 79 20 63 ber for sanity c
aba50 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 hecking */. int
aba60 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 nMem;
aba70 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
aba80 66 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f f memory locatio
aba90 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c ns currently all
abaa0 6f 63 61 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 ocated */. Mem
abab0 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 *aMem;
abac0 20 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d 6f 72 /* The memor
abad0 79 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 y locations */.
abae0 20 69 6e 74 20 6e 43 61 6c 6c 62 61 63 6b 3b 20 int nCallback;
abaf0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
abb00 65 72 20 6f 66 20 63 61 6c 6c 62 61 63 6b 73 20 er of callbacks
abb10 69 6e 76 6f 6b 65 64 20 73 6f 20 66 61 72 20 2a invoked so far *
abb20 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 43 74 72 /. int cacheCtr
abb30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 ; /* V
abb40 64 62 65 43 75 72 73 6f 72 20 72 6f 77 20 63 61 dbeCursor row ca
abb50 63 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 63 che generation c
abb60 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 46 69 66 6f ounter */. Fifo
abb70 20 73 46 69 66 6f 3b 20 20 20 20 20 20 20 20 20 sFifo;
abb80 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 /* A list of
abb90 20 52 4f 57 49 44 73 20 2a 2f 0a 20 20 69 6e 74 ROWIDs */. int
abba0 20 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 contextStackTop
abbb0 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 ; /* Index of
abbc0 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e 20 top element in
abbd0 74 68 65 20 63 6f 6e 74 65 78 74 20 73 74 61 63 the context stac
abbe0 6b 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 74 65 k */. int conte
abbf0 78 74 53 74 61 63 6b 44 65 70 74 68 3b 20 20 2f xtStackDepth; /
abc00 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 * The size of th
abc10 65 20 22 63 6f 6e 74 65 78 74 22 20 73 74 61 63 e "context" stac
abc20 6b 20 2a 2f 0a 20 20 43 6f 6e 74 65 78 74 20 2a k */. Context *
abc30 63 6f 6e 74 65 78 74 53 74 61 63 6b 3b 20 20 2f contextStack; /
abc40 2a 20 53 74 61 63 6b 20 75 73 65 64 20 62 79 20 * Stack used by
abc50 6f 70 63 6f 64 65 73 20 43 6f 6e 74 65 78 74 50 opcodes ContextP
abc60 75 73 68 20 26 20 43 6f 6e 74 65 78 74 50 6f 70 ush & ContextPop
abc70 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 */. int pc;
abc80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
abc90 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e The program coun
abca0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b ter */. int rc;
abcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
abcc0 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 /* Value to ret
abcd0 75 72 6e 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 urn */. unsigne
abce0 64 20 75 6e 69 71 75 65 43 6e 74 3b 20 20 20 20 d uniqueCnt;
abcf0 20 2f 2a 20 55 73 65 64 20 62 79 20 4f 50 5f 4d /* Used by OP_M
abd00 61 6b 65 52 65 63 6f 72 64 20 77 68 65 6e 20 50 akeRecord when P
abd10 32 21 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 65 72 2!=0 */. int er
abd20 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20 rorAction;
abd30 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63 /* Recovery ac
abd40 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61 tion to do in ca
abd50 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a se of an error *
abd60 2f 0a 20 20 69 6e 74 20 69 6e 54 65 6d 70 54 72 /. int inTempTr
abd70 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ans; /* T
abd80 72 75 65 20 69 66 20 74 65 6d 70 20 64 61 74 61 rue if temp data
abd90 62 61 73 65 20 69 73 20 74 72 61 6e 73 61 63 74 base is transact
abda0 69 6f 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ioned */. int n
abdb0 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 ResColumn;
abdc0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
abdd0 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 columns in one r
abde0 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ow of the result
abdf0 20 73 65 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a set */. char *
abe00 2a 61 7a 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 *azResColumn;
abe10 20 20 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 /* Values for
abe20 6f 6e 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c one row of resul
abe30 74 20 2a 2f 20 0a 20 20 63 68 61 72 20 2a 7a 45 t */ . char *zE
abe40 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 rrMsg;
abe50 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 /* Error message
abe60 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
abe70 0a 20 20 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 . Mem *pResultS
abe80 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f et; /* Po
abe90 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 inter to an arra
abea0 79 20 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a y of results */.
abeb0 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 u8 explain;
abec0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
abed0 65 20 69 66 20 45 58 50 4c 41 49 4e 20 70 72 65 e if EXPLAIN pre
abee0 73 65 6e 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d sent on SQL comm
abef0 61 6e 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e and */. u8 chan
abf00 67 65 43 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 geCntOn;
abf10 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 70 64 61 /* True to upda
abf20 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f te the change-co
abf30 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 unter */. u8 ex
abf40 70 69 72 65 64 3b 20 20 20 20 20 20 20 20 20 20 pired;
abf50 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
abf60 65 20 56 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 e VM needs to be
abf70 20 72 65 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 recompiled */.
abf80 20 75 38 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 u8 minWriteFile
abf90 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 Format; /* Mini
abfa0 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 mum file format
abfb0 66 6f 72 20 77 72 69 74 61 62 6c 65 20 64 61 74 for writable dat
abfc0 61 62 61 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 abase files */.
abfd0 20 75 38 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 u8 inVtabMethod
abfe0 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20 ; /* See
abff0 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a comments above *
ac000 2f 0a 20 20 75 38 20 75 73 65 73 53 74 6d 74 4a /. u8 usesStmtJ
ac010 6f 75 72 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 ournal; /* T
ac020 72 75 65 20 69 66 20 75 73 65 73 20 61 20 73 74 rue if uses a st
ac030 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 atement journal
ac040 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 */. u8 readOnly
ac050 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
ac060 54 72 75 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e True for read-on
ac070 6c 79 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f ly statements */
ac080 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 . int nChange;
ac090 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
ac0a0 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67 mber of db chang
ac0b0 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61 es made since la
ac0c0 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 36 st reset */. i6
ac0d0 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 4 startTime;
ac0e0 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 /* Time wh
ac0f0 65 6e 20 71 75 65 72 79 20 73 74 61 72 74 65 64 en query started
ac100 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 - used for prof
ac110 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 iling */. int b
ac120 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 treeMask;
ac130 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 /* Bitmask of
ac140 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 db->aDb[] entri
ac150 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f es referenced */
ac160 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 . BtreeMutexArr
ac170 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e ay aMutex; /* An
ac180 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20 array of Btree
ac190 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65 used here and ne
ac1a0 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 eding locks */.
ac1b0 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d int aCounter[2]
ac1c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e ; /* Coun
ac1d0 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c ters used by sql
ac1e0 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 ite3_stmt_status
ac1f0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 71 6c () */. int nSql
ac200 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
ac210 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
ac220 20 69 6e 20 7a 53 71 6c 20 2a 2f 0a 20 20 63 68 in zSql */. ch
ac230 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20 20 ar *zSql;
ac240 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 /* Text of t
ac250 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
ac260 20 74 68 61 74 20 67 65 6e 65 72 61 74 65 64 20 that generated
ac270 74 68 69 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 this */.#ifdef S
ac280 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 QLITE_DEBUG. FI
ac290 4c 45 20 2a 74 72 61 63 65 3b 20 20 20 20 20 20 LE *trace;
ac2a0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 20 /* Write an
ac2b0 65 78 65 63 75 74 69 6f 6e 20 74 72 61 63 65 20 execution trace
ac2c0 68 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55 4c here, if not NUL
ac2d0 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e L */.#endif. in
ac2e0 74 20 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e t openedStatemen
ac2f0 74 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 t; /* True if t
ac300 68 69 73 20 56 4d 20 68 61 73 20 6f 70 65 6e 65 his VM has opene
ac310 64 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f d a statement jo
ac320 75 72 6e 61 6c 20 2a 2f 0a 23 69 66 64 65 66 20 urnal */.#ifdef
ac330 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 69 6e 74 SQLITE_SSE. int
ac340 20 66 65 74 63 68 49 64 3b 20 20 20 20 20 20 20 fetchId;
ac350 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 /* Statement
ac360 6e 75 6d 62 65 72 20 75 73 65 64 20 62 79 20 73 number used by s
ac370 71 6c 69 74 65 33 5f 66 65 74 63 68 5f 73 74 61 qlite3_fetch_sta
ac380 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 tement */. int
ac390 6c 72 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 lru;
ac3a0 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 75 73 65 /* Counter use
ac3b0 64 20 66 6f 72 20 4c 52 55 20 63 61 63 68 65 20 d for LRU cache
ac3c0 72 65 70 6c 61 63 65 6d 65 6e 74 20 2a 2f 0a 23 replacement */.#
ac3d0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
ac3e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ITE_ENABLE_MEMOR
ac3f0 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 56 Y_MANAGEMENT. V
ac400 64 62 65 20 2a 70 4c 72 75 50 72 65 76 3b 0a 20 dbe *pLruPrev;.
ac410 20 56 64 62 65 20 2a 70 4c 72 75 4e 65 78 74 3b Vdbe *pLruNext;
ac420 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
ac430 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
ac440 61 72 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 are allowed valu
ac450 65 73 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 es for Vdbe.magi
ac460 63 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 c.*/.#define VDB
ac470 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20 E_MAGIC_INIT
ac480 20 30 78 32 36 62 63 65 61 61 35 20 20 20 20 2f 0x26bceaa5 /
ac490 2a 20 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42 * Building a VDB
ac4a0 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 E program */.#de
ac4b0 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f fine VDBE_MAGIC_
ac4c0 52 55 4e 20 20 20 20 20 20 30 78 62 64 66 32 30 RUN 0xbdf20
ac4d0 64 61 33 20 20 20 20 2f 2a 20 56 44 42 45 20 69 da3 /* VDBE i
ac4e0 73 20 72 65 61 64 79 20 74 6f 20 65 78 65 63 75 s ready to execu
ac4f0 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 te */.#define VD
ac500 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 BE_MAGIC_HALT
ac510 20 20 30 78 35 31 39 63 32 39 37 33 20 20 20 20 0x519c2973
ac520 2f 2a 20 56 44 42 45 20 68 61 73 20 63 6f 6d 70 /* VDBE has comp
ac530 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f 6e 20 leted execution
ac540 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f */.#define VDBE_
ac550 4d 41 47 49 43 5f 44 45 41 44 20 20 20 20 20 30 MAGIC_DEAD 0
ac560 78 62 36 30 36 63 33 63 38 20 20 20 20 2f 2a 20 xb606c3c8 /*
ac570 54 68 65 20 56 44 42 45 20 68 61 73 20 62 65 65 The VDBE has bee
ac580 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f n deallocated */
ac590 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e ../*.** Function
ac5a0 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 prototypes.*/.S
ac5b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
ac5c0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 id sqlite3VdbeFr
ac5d0 65 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 2c eeCursor(Vdbe *,
ac5e0 20 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 VdbeCursor*);.v
ac5f0 6f 69 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f oid sqliteVdbePo
ac600 70 53 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 pStack(Vdbe*,int
ac610 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac620 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
ac630 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 eCursorMoveto(Vd
ac640 62 65 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 beCursor*);.#if
ac650 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
ac660 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 EBUG) || defined
ac670 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 (VDBE_PROFILE).S
ac680 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
ac690 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 id sqlite3VdbePr
ac6a0 69 6e 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 intOp(FILE*, int
ac6b0 2c 20 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 , Op*);.#endif.S
ac6c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ac6d0 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 t sqlite3VdbeSer
ac6e0 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b ialTypeLen(u32);
ac6f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac700 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 u32 sqlite3VdbeS
ac710 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 erialType(Mem*,
ac720 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
ac730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ac740 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e VdbeSerialPut(un
ac750 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e signed char*, in
ac760 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 t, Mem*, int);.S
ac770 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ac780 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 t sqlite3VdbeSer
ac790 69 61 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 ialGet(const uns
ac7a0 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 igned char*, u32
ac7b0 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f , Mem*);.SQLITE_
ac7c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
ac7d0 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 ite3VdbeDeleteAu
ac7e0 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c xData(VdbeFunc*,
ac7f0 20 69 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 int);..int sqli
ac800 74 65 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 te2BtreeKeyCompa
ac810 72 65 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 re(BtCursor *, c
ac820 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 onst void *, int
ac830 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 , int, int *);.S
ac840 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ac850 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 t sqlite3VdbeIdx
ac860 4b 65 79 43 6f 6d 70 61 72 65 28 56 64 62 65 43 KeyCompare(VdbeC
ac870 75 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 ursor*,UnpackedR
ac880 65 63 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 ecord*,int*);.SQ
ac890 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac8a0 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 sqlite3VdbeIdxR
ac8b0 6f 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 2c owid(BtCursor *,
ac8c0 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f i64 *);.SQLITE_
ac8d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac8e0 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f te3MemCompare(co
ac8f0 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 nst Mem*, const
ac900 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c Mem*, const Coll
ac910 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Seq*);.SQLITE_PR
ac920 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ac930 33 56 64 62 65 45 78 65 63 28 56 64 62 65 2a 29 3VdbeExec(Vdbe*)
ac940 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ac950 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
ac960 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c List(Vdbe*);.SQL
ac970 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
ac980 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 sqlite3VdbeHalt(
ac990 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Vdbe*);.SQLITE_P
ac9a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ac9b0 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
ac9c0 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 ding(Mem *, int)
ac9d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ac9e0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
ac9f0 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b MemTooBig(Mem*);
aca00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
aca10 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
aca20 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e emCopy(Mem*, con
aca30 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 st Mem*);.SQLITE
aca40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
aca50 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c lite3VdbeMemShal
aca60 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f lowCopy(Mem*, co
aca70 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a nst Mem*, int);.
aca80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
aca90 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
acaa0 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d emMove(Mem*, Mem
acab0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
acac0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acad0 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 beMemNulTerminat
acae0 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f e(Mem*);.SQLITE_
acaf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
acb00 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
acb10 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 (Mem*, const cha
acb20 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76 6f 69 r*, int, u8, voi
acb30 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
acb40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
acb50 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
acb60 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 SetInt64(Mem*, i
acb70 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
acb80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
acb90 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 VdbeMemSetDouble
acba0 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a (Mem*, double);.
acbb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
acbc0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
acbd0 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b emSetNull(Mem*);
acbe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acbf0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
acc00 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d MemSetZeroBlob(M
acc10 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 em*,int);.SQLITE
acc20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
acc30 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 ite3VdbeMemMakeW
acc40 72 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a riteable(Mem*);.
acc50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
acc60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
acc70 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c mStringify(Mem*,
acc80 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
acc90 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 IVATE i64 sqlite
acca0 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 3VdbeIntValue(Me
accb0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
accc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
accd0 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
acce0 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 (Mem*);.SQLITE_P
accf0 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 RIVATE double sq
acd00 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
acd10 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ue(Mem*);.SQLITE
acd20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
acd30 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 lite3VdbeInteger
acd40 41 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a Affinity(Mem*);.
acd50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
acd60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
acd70 6d 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a mRealify(Mem*);.
acd80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
acd90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
acda0 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b mNumerify(Mem*);
acdb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acdc0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
acdd0 65 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75 emFromBtree(BtCu
acde0 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e rsor*,int,int,in
acdf0 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f t,Mem*);.SQLITE_
ace00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
ace10 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
ace20 73 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 se(Mem *p);.SQLI
ace30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ace40 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
ace50 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 leaseExternal(Me
ace60 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 m *p);.SQLITE_PR
ace70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ace80 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 3VdbeMemFinalize
ace90 28 4d 65 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29 (Mem*, FuncDef*)
acea0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
aceb0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
acec0 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 ite3OpcodeName(i
aced0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
acee0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
acef0 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 dbeOpcodeHasProp
acf00 65 72 74 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a erty(int, int);.
acf10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
acf20 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
acf30 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c mGrow(Mem *pMem,
acf40 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 int n, int pres
acf50 65 72 76 65 29 3b 0a 23 69 66 64 65 66 20 53 51 erve);.#ifdef SQ
acf60 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f LITE_ENABLE_MEMO
acf70 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 RY_MANAGEMENT.SQ
acf80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
acf90 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 sqlite3VdbeRele
acfa0 61 73 65 42 75 66 66 65 72 73 28 56 64 62 65 20 aseBuffers(Vdbe
acfb0 2a 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 *p);.#endif..#if
acfc0 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 ndef NDEBUG.SQLI
acfd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
acfe0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
acff0 53 61 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 23 65 Sanity(Mem*);.#e
ad000 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
ad010 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ad020 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 dbeMemTranslate(
ad030 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65 Mem*, u8);.#ifde
ad040 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 f SQLITE_DEBUG.S
ad050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
ad060 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ad070 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b PrintSql(Vdbe*);
ad080 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ad090 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
ad0a0 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
ad0b0 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 (Mem *pMem, char
ad0c0 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a *zBuf);.#endif.
ad0d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ad0e0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
ad0f0 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a mHandleBom(Mem *
ad100 70 4d 65 6d 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pMem);.SQLITE_PR
ad110 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ad120 65 33 56 64 62 65 46 69 66 6f 49 6e 69 74 28 46 e3VdbeFifoInit(F
ad130 69 66 6f 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b ifo*, sqlite3*);
ad140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ad150 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 int sqlite3VdbeF
ad160 69 66 6f 50 75 73 68 28 46 69 66 6f 2a 2c 20 69 ifoPush(Fifo*, i
ad170 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
ad180 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ad190 64 62 65 46 69 66 6f 50 6f 70 28 46 69 66 6f 2a dbeFifoPop(Fifo*
ad1a0 2c 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f , i64*);.SQLITE_
ad1b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
ad1c0 69 74 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 ite3VdbeFifoClea
ad1d0 72 28 46 69 66 6f 2a 29 3b 0a 0a 23 69 66 6e 64 r(Fifo*);..#ifnd
ad1e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
ad1f0 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 NCRBLOB.SQLITE_P
ad200 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
ad210 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e ite3VdbeMemExpan
ad220 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 dBlob(Mem *);.#e
ad230 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
ad240 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 lite3VdbeMemExpa
ad250 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 ndBlob(x) SQLITE
ad260 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 _OK.#endif..#end
ad270 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
ad280 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a VDBEINT_H_) */..
ad290 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
ad2a0 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 End of vdbeInt.h
ad2b0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
ad2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
ad2e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
ad2f0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
ad300 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
ad310 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a utf.c **********
ad320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
ad330 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
ad340 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 wing constant va
ad350 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 74 lue is used by t
ad360 68 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 he SQLITE_BIGEND
ad370 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 IAN and.** SQLIT
ad380 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d E_LITTLEENDIAN m
ad390 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 acros..*/.SQLITE
ad3a0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 _PRIVATE const i
ad3b0 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 nt sqlite3one =
ad3c0 31 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 1;../*.** This l
ad3d0 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 ookup table is u
ad3e0 73 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63 6f sed to help deco
ad3f0 64 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 de the first byt
ad400 65 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d e of.** a multi-
ad410 62 79 74 65 20 55 54 46 38 20 63 68 61 72 61 63 byte UTF8 charac
ad420 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 ter..*/.static c
ad430 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
ad440 61 72 20 73 71 6c 69 74 65 33 55 74 66 54 72 61 ar sqlite3UtfTra
ad450 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 ns1[] = {. 0x00
ad460 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
ad470 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 03, 0x04, 0x05,
ad480 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 0x06, 0x07,. 0x
ad490 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 08, 0x09, 0x0a,
ad4a0 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 0x0b, 0x0c, 0x0d
ad4b0 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 , 0x0e, 0x0f,.
ad4c0 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32 0x10, 0x11, 0x12
ad4d0 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78 , 0x13, 0x14, 0x
ad4e0 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 15, 0x16, 0x17,.
ad4f0 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78 0x18, 0x19, 0x
ad500 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20 1a, 0x1b, 0x1c,
ad510 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66 0x1d, 0x1e, 0x1f
ad520 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 ,. 0x00, 0x01,
ad530 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 0x02, 0x03, 0x04
ad540 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 , 0x05, 0x06, 0x
ad550 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 07,. 0x08, 0x09
ad560 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 , 0x0a, 0x0b, 0x
ad570 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 0c, 0x0d, 0x0e,
ad580 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 0x0f,. 0x00, 0x
ad590 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 01, 0x02, 0x03,
ad5a0 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 0x04, 0x05, 0x06
ad5b0 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 , 0x07,. 0x00,
ad5c0 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 0x01, 0x02, 0x03
ad5d0 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 , 0x00, 0x01, 0x
ad5e0 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 00, 0x00,.};...#
ad5f0 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 define WRITE_UTF
ad600 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 8(zOut, c) {
ad610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad620 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c \. if( c<
ad630 30 78 30 30 30 38 30 20 29 7b 20 20 20 20 20 20 0x00080 ){
ad640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad660 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
ad670 63 26 30 78 46 46 29 3b 20 20 20 20 20 20 20 20 c&0xFF);
ad680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad690 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 \. }
ad6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6d0 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c \. else if( c<
ad6e0 30 78 30 30 38 30 30 20 29 7b 20 20 20 20 20 20 0x00800 ){
ad6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad700 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ad710 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b *zOut++ = 0xC0 +
ad720 20 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 20 ((c>>6)&0x1F);
ad730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad740 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ad750 20 3d 20 30 78 38 30 20 2b 20 28 63 20 26 20 30 = 0x80 + (c & 0
ad760 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x3F);
ad770 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ad780 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ad790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad7b0 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 \. else if
ad7c0 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20 ( c<0x10000 ){
ad7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad7f0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
ad800 45 30 20 2b 20 28 28 63 3e 3e 31 32 29 26 30 78 E0 + ((c>>12)&0x
ad810 30 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 0F);
ad820 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
ad830 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 28 ut++ = 0x80 + ((
ad840 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 c>>6) & 0x3F);
ad850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad860 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ad870 30 78 38 30 20 2b 20 28 63 20 26 20 30 78 33 46 0x80 + (c & 0x3F
ad880 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ad890 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c \. }el
ad8a0 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 se{
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8d0 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ad8e0 3d 20 30 78 46 30 20 2b 20 28 28 63 3e 3e 31 38 = 0xF0 + ((c>>18
ad8f0 29 20 26 20 30 78 30 37 29 3b 20 20 20 20 20 20 ) & 0x07);
ad900 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ad910 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 *zOut++ = 0x80
ad920 2b 20 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33 + ((c>>12) & 0x3
ad930 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
ad940 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
ad950 2b 20 3d 20 30 78 38 30 20 2b 20 28 28 63 3e 3e + = 0x80 + ((c>>
ad960 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 6) & 0x3F);
ad970 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad980 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
ad990 30 20 2b 20 28 63 20 26 20 30 78 33 46 29 3b 20 0 + (c & 0x3F);
ad9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9b0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
ad9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad9f0 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 .}..#define WRIT
ada00 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 E_UTF16LE(zOut,
ada10 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 c) {
ada20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada30 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 \. if( c<=0
ada40 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 xFFFF ){
ada50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada70 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
ada80 74 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29 t++ = (c&0x00FF)
ada90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
adaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adab0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
adac0 4f 75 74 2b 2b 20 3d 20 28 28 63 3e 3e 38 29 26 Out++ = ((c>>8)&
adad0 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 0x00FF);
adae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adaf0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 \. }e
adb00 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 lse{
adb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
adb40 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28 28 63 *zOut++ = (((c
adb50 3e 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 >>10)&0x003F) +
adb60 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 (((c-0x10000)>>1
adb70 30 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 0)&0x00C0)); \.
adb80 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30 *zOut++ = (0
adb90 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 x00D8 + (((c-0x1
adba0 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 0000)>>18)&0x03)
adbb0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
adbc0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
adbd0 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 (c&0x00FF);
adbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc00 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
adc10 3d 20 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e = (0x00DC + ((c>
adc20 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 >8)&0x03));
adc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc40 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 \. }
adc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc80 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 \.}..#defi
adc90 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36 42 45 ne WRITE_UTF16BE
adca0 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 (zOut, c) {
adcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adcc0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 \. i
adcd0 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 f( c<=0xFFFF ){
adce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
add10 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28 63 *zOut++ = ((c
add20 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 20 >>8)&0x00FF);
add30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
add50 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
add60 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 c&0x00FF);
add70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add90 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 \. }else{
adda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addd0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
adde0 20 3d 20 28 30 78 30 30 44 38 20 2b 20 28 28 28 = (0x00D8 + (((
addf0 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26 c-0x10000)>>18)&
ade00 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 0x03));
ade10 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
ade20 2b 2b 20 3d 20 28 28 28 63 3e 3e 31 30 29 26 30 ++ = (((c>>10)&0
ade30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 x003F) + (((c-0x
ade40 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 10000)>>10)&0x00
ade50 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f C0)); \. *zO
ade60 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 43 20 2b ut++ = (0x00DC +
ade70 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b ((c>>8)&0x03));
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade90 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
adea0 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30 78 30 30 zOut++ = (c&0x00
adeb0 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 FF);
adec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aded0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
adee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf10 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
adf20 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 ..#define READ_U
adf30 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 7b 20 TF16LE(zIn, c){
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf60 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 \. c =
adf70 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 (*zIn++);
adf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfb0 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b \. c += ((*zIn+
adfc0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 +)<<8);
adfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adff0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
ae000 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 c>=0xD800 && c<0
ae010 78 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 xE000 ){
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ae040 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a . int c2 = (*
ae050 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 zIn++);
ae060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae080 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 \. c2
ae090 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 += ((*zIn++)<<8)
ae0a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ae0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ae0d0 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 . c = (c2&0x0
ae0e0 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 3FF) + ((c&0x003
ae0f0 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 F)<<10) + (((c&0
ae100 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c x03C0)+0x0040)<<
ae110 31 30 29 3b 20 20 20 5c 0a 20 20 20 20 69 66 28 10); \. if(
ae120 20 28 63 20 26 20 30 78 46 46 46 46 30 30 30 30 (c & 0xFFFF0000
ae130 29 3d 3d 30 20 29 20 63 20 3d 20 30 78 46 46 46 )==0 ) c = 0xFFF
ae140 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D;
ae150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ae160 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 . }
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1a0 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 \.}..#def
ae1b0 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 42 45 ine READ_UTF16BE
ae1c0 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20 (zIn, c){
ae1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1f0 20 20 5c 0a 20 20 63 20 3d 20 28 28 2a 7a 49 6e \. c = ((*zIn
ae200 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 ++)<<8);
ae210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae230 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 \. c
ae240 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 += (*zIn++);
ae250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae280 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 \. if( c>=0xD
ae290 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 800 && c<0xE000
ae2a0 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ae2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae2c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 \. i
ae2d0 6e 74 20 63 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b nt c2 = ((*zIn++
ae2e0 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 )<<8);
ae2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae310 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 2a 7a \. c2 += (*z
ae320 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 In++);
ae330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae350 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 \. c
ae360 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b = (c2&0x03FF) +
ae370 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30 ((c&0x003F)<<10
ae380 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29 ) + (((c&0x03C0)
ae390 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 +0x0040)<<10);
ae3a0 20 5c 0a 20 20 20 20 69 66 28 20 28 63 20 26 20 \. if( (c &
ae3b0 30 78 46 46 46 46 30 30 30 30 29 3d 3d 30 20 29 0xFFFF0000)==0 )
ae3c0 20 63 20 3d 20 30 78 46 46 46 44 3b 20 20 20 20 c = 0xFFFD;
ae3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae3e0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 \. }
ae3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae430 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e \.}../*.** Tran
ae440 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 55 slate a single U
ae450 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 20 TF-8 character.
ae460 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 63 Return the unic
ae470 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a ode value..**.**
ae480 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 During translat
ae490 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 74 ion, assume that
ae4a0 20 74 68 65 20 62 79 74 65 20 74 68 61 74 20 7a the byte that z
ae4b0 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 Term points.** i
ae4c0 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 s a 0x00..**.**
ae4d0 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 Write a pointer
ae4e0 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e 72 65 to the next unre
ae4f0 61 64 20 62 79 74 65 20 62 61 63 6b 20 69 6e 74 ad byte back int
ae500 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a o *pzNext..**.**
ae510 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 Notes On Invali
ae520 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 d UTF-8:.**.**
ae530 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 * This routine
ae540 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 never allows a 7
ae550 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 20 28 -bit character (
ae560 30 78 30 30 20 74 68 72 6f 75 67 68 20 30 78 37 0x00 through 0x7
ae570 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 f) to.** be
ae580 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c encoded as a mul
ae590 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 ti-byte characte
ae5a0 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 r. Any multi-by
ae5b0 74 65 20 63 68 61 72 61 63 74 65 72 20 74 68 61 te character tha
ae5c0 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 74 t.** attempt
ae5d0 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 s to encode a va
ae5e0 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78 30 30 lue between 0x00
ae5f0 20 61 6e 64 20 30 78 37 66 20 69 73 20 72 65 6e and 0x7f is ren
ae600 64 65 72 65 64 20 61 73 20 30 78 66 66 66 64 2e dered as 0xfffd.
ae610 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 .**.** * This
ae620 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c routine never al
ae630 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73 75 72 lows a UTF16 sur
ae640 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74 6f 20 rogate value to
ae650 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 be encoded..**
ae660 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 If a multi-by
ae670 74 65 20 63 68 61 72 61 63 74 65 72 20 61 74 74 te character att
ae680 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 empts to encode
ae690 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e 0a a value between.
ae6a0 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20 61 6e ** 0xd800 an
ae6b0 64 20 30 78 65 30 30 30 20 74 68 65 6e 20 69 74 d 0xe000 then it
ae6c0 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 is rendered as
ae6d0 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 0xfffd..**.** *
ae6e0 20 20 42 79 74 65 73 20 69 6e 20 74 68 65 20 72 Bytes in the r
ae6f0 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74 68 72 ange of 0x80 thr
ae700 6f 75 67 68 20 30 78 62 66 20 77 68 69 63 68 20 ough 0xbf which
ae710 6f 63 63 75 72 20 61 73 20 74 68 65 20 66 69 72 occur as the fir
ae720 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 6f st.** byte o
ae730 66 20 61 20 63 68 61 72 61 63 74 65 72 20 61 72 f a character ar
ae740 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 e interpreted as
ae750 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 single-byte cha
ae760 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 racters.** a
ae770 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73 20 74 nd rendered as t
ae780 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 74 hemselves even t
ae790 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 20 74 hough they are t
ae7a0 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 echnically.**
ae7b0 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 invalid charac
ae7c0 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 ters..**.** *
ae7d0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 63 This routine acc
ae7e0 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 epts an infinite
ae7f0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 number of diffe
ae800 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 69 rent UTF8 encodi
ae810 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 ngs.** for u
ae820 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20 30 78 nicode values 0x
ae830 38 30 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20 80 and greater.
ae840 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 It do not chang
ae850 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a e over-length.**
ae860 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 encodings t
ae870 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65 o 0xfffd as some
ae880 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 systems recomme
ae890 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 nd..*/.#define R
ae8a0 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 EAD_UTF8(zIn, zT
ae8b0 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20 erm, c)
ae8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae8d0 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b \. c = *(zIn+
ae8e0 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 +);
ae8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ae910 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 . if( c>=0xc0 )
ae920 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
ae930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae940 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ae950 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 c = sqlite3Utf
ae960 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 Trans1[c-0xc0];
ae970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae980 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 \. w
ae990 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d hile( zIn!=zTerm
ae9a0 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 && (*zIn & 0xc0
ae9b0 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 20 20 )==0x80 ){
ae9c0 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 63 20 \. c
ae9d0 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 = (c<<6) + (0x3f
ae9e0 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 & *(zIn++));
ae9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aea00 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 \. }
aea10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aea20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aea30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aea40 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 \. if( c<0x80
aea50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aea60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aea70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
aea80 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 || (c&0xF
aea90 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30 FFFF800)==0xD800
aeaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeab0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
aeac0 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 || (c&0xFFFF
aead0 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b FFFE)==0xFFFE ){
aeae0 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 c = 0xFFFD; }
aeaf0 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c \. }.SQL
aeb00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
aeb10 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
aeb20 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
aeb30 64 20 63 68 61 72 20 2a 7a 2c 20 20 20 20 20 20 d char *z,
aeb40 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
aeb50 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 of UTF-8 charac
aeb60 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 ter */. const u
aeb70 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 nsigned char *zT
aeb80 65 72 6d 2c 20 20 20 20 20 2f 2a 20 50 72 65 74 erm, /* Pret
aeb90 65 6e 64 20 74 68 69 73 20 62 79 74 65 20 69 73 end this byte is
aeba0 20 30 78 30 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 0x00 */. const
aebb0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
aebc0 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 *pzNext /* Wr
aebd0 69 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70 ite first byte p
aebe0 61 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68 ast UTF-8 char h
aebf0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
aec00 63 3b 0a 20 20 52 45 41 44 5f 55 54 46 38 28 7a c;. READ_UTF8(z
aec10 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 2a , zTerm, c);. *
aec20 70 7a 4e 65 78 74 20 3d 20 7a 3b 0a 20 20 72 65 pzNext = z;. re
aec30 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a turn c;.}...../*
aec40 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 .** If the TRANS
aec50 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f LATE_TRACE macro
aec60 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 is defined, the
aec70 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d value of each M
aec80 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 em is.** printed
aec90 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 on stderr on th
aeca0 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f e way into and o
aecb0 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 ut of sqlite3Vdb
aecc0 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e eMemTranslate().
aecd0 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 .*/ ./* #define
aece0 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 TRANSLATE_TRACE
aecf0 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 1 */..#ifndef SQ
aed00 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
aed10 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
aed20 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 ne transforms th
aed30 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 e internal text
aed40 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 encoding used by
aed50 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 pMem to.** desi
aed60 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e redEnc. It is an
aed70 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 error if the st
aed80 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 ring is already
aed90 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a of the desired.*
aeda0 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 * encoding, or i
aedb0 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 f *pMem does not
aedc0 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e contain a strin
aedd0 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 g value..*/.SQLI
aede0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
aedf0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 qlite3VdbeMemTra
aee00 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d nslate(Mem *pMem
aee10 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 , u8 desiredEnc)
aee20 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 {. int len;
aee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aee40 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 /* Maximum lengt
aee50 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 h of output stri
aee60 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 ng in bytes */.
aee70 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
aee80 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 zOut;
aee90 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 /* Output
aeea0 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 buffer */. uns
aeeb0 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b igned char *zIn;
aeec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeed0 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 /* Input iter
aeee0 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e ator */. unsign
aeef0 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 ed char *zTerm;
aef00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aef10 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 /* End of input
aef20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
aef30 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 ar *z;
aef40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 /* Ou
aef50 74 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f tput iterator */
aef60 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
aef70 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d c;.. assert( pM
aef80 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
aef90 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
aefa0 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
aefb0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
aefc0 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 em->flags&MEM_St
aefd0 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
aefe0 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 Mem->enc!=desire
aeff0 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 dEnc );. assert
af000 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 ( pMem->enc!=0 )
af010 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d ;. assert( pMem
af020 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 ->n>=0 );..#if d
af030 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 efined(TRANSLATE
af040 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e _TRACE) && defin
af050 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
af060 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 . {. char zB
af070 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c uf[100];. sql
af080 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 ite3VdbeMemPrett
af090 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 yPrint(pMem, zBu
af0a0 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 f);. fprintf(
af0b0 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 stderr, "INPUT:
af0c0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 %s\n", zBuf);.
af0d0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
af0e0 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 If the translati
af0f0 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 on is between UT
af100 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 F-16 little and
af110 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e big endian, then
af120 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 . ** all that
af130 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74 is required is t
af140 6f 20 73 77 61 70 20 74 68 65 20 62 79 74 65 20 o swap the byte
af150 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 order. This case
af160 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a is handled. **
af170 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f differently fro
af180 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 m the others..
af190 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 */. if( pMem->e
af1a0 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc!=SQLITE_UTF8
af1b0 26 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 && desiredEnc!=S
af1c0 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 QLITE_UTF8 ){.
af1d0 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 u8 temp;. i
af1e0 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 nt rc;. rc =
af1f0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 sqlite3VdbeMemMa
af200 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d keWriteable(pMem
af210 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
af220 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
af230 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
af240 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 LITE_NOMEM );.
af250 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
af260 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
af270 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d zIn = (u8*)pM
af280 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d em->z;. zTerm
af290 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 = &zIn[pMem->n&
af2a0 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ~1];. while(
af2b0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 zIn<zTerm ){.
af2c0 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a temp = *zIn;.
af2d0 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a *zIn = *(z
af2e0 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e In+1);. zIn
af2f0 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b ++;. *zIn++
af300 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 = temp;. }.
af310 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 pMem->enc = d
af320 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 esiredEnc;. g
af330 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 oto translate_ou
af340 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 t;. }.. /* Set
af350 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 len to the maxi
af360 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 mum number of by
af370 74 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 tes required in
af380 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
af390 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 r. */. if( desi
af3a0 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 redEnc==SQLITE_U
af3b0 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 TF8 ){. /* Wh
af3c0 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 en converting fr
af3d0 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d om UTF-16, the m
af3e0 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 aximum growth re
af3f0 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a sults from. *
af400 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 * translating a
af410 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 2-byte character
af420 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 to a 4-byte UTF
af430 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 -8 character..
af440 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 ** A single by
af450 74 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 te is required f
af460 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 or the output st
af470 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d ring. ** nul-
af480 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 terminator..
af490 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 */. pMem->n &
af4a0 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 = ~1;. len =
af4b0 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b pMem->n * 2 + 1;
af4c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
af4d0 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 When converting
af4e0 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 from UTF-8 to U
af4f0 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 TF-16 the maximu
af500 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 m growth is caus
af510 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 ed. ** when a
af520 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 1-byte UTF-8 ch
af530 61 72 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 aracter is trans
af540 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 lated into a 2-b
af550 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a yte UTF-16. *
af560 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f * character. Two
af570 20 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69 bytes are requi
af580 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 red in the outpu
af590 74 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 t buffer for the
af5a0 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d . ** nul-term
af5b0 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 inator.. */.
af5c0 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e len = pMem->n
af5d0 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 * 2 + 2;. }..
af5e0 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 /* Set zIn to p
af5f0 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 oint at the star
af600 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 t of the input b
af610 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 uffer and zTerm
af620 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 to point 1. **
af630 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e byte past the en
af640 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 d.. **. ** Var
af650 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 iable zOut is se
af660 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 t to point at th
af670 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c e output buffer,
af680 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a space obtained.
af690 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 ** from sqlite
af6a0 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 3_malloc().. */
af6b0 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d . zIn = (u8*)pM
af6c0 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d em->z;. zTerm =
af6d0 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a &zIn[pMem->n];.
af6e0 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 zOut = sqlite3
af6f0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d DbMallocRaw(pMem
af700 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 ->db, len);. if
af710 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 ( !zOut ){. r
af720 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
af730 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f EM;. }. z = zO
af740 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d ut;.. if( pMem-
af750 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 >enc==SQLITE_UTF
af760 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 8 ){. if( des
af770 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
af780 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 UTF16LE ){.
af790 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 /* UTF-8 -> UTF
af7a0 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 -16 Little-endia
af7b0 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 n */. while
af7c0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
af7d0 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 /* c = sq
af7e0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 lite3Utf8Read(zI
af7f0 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 n, zTerm, (const
af800 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a u8**)&zIn); */.
af810 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 READ_UTF
af820 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 8(zIn, zTerm, c)
af830 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f ;. WRITE_
af840 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 UTF16LE(z, c);.
af850 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
af860 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
af870 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 desiredEnc==SQLI
af880 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 TE_UTF16BE );.
af890 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 /* UTF-8 ->
af8a0 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 UTF-16 Big-endia
af8b0 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 n */. while
af8c0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
af8d0 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 /* c = sq
af8e0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 lite3Utf8Read(zI
af8f0 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 n, zTerm, (const
af900 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a u8**)&zIn); */.
af910 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 READ_UTF
af920 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 8(zIn, zTerm, c)
af930 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f ;. WRITE_
af940 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 UTF16BE(z, c);.
af950 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
af960 20 70 4d 65 6d 2d 3e 6e 20 3d 20 7a 20 2d 20 7a pMem->n = z - z
af970 4f 75 74 3b 0a 20 20 20 20 2a 7a 2b 2b 20 3d 20 Out;. *z++ =
af980 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
af990 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 45 assert( desiredE
af9a0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc==SQLITE_UTF8
af9b0 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d );. if( pMem-
af9c0 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 >enc==SQLITE_UTF
af9d0 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 16LE ){. /*
af9e0 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 UTF-16 Little-e
af9f0 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38 20 2a ndian -> UTF-8 *
afa00 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a /. while( z
afa10 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 In<zTerm ){.
afa20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 READ_UTF16LE
afa30 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20 20 20 20 (zIn, c); .
afa40 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c WRITE_UTF8(z,
afa50 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 c);. }.
afa60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a }else{. /*
afa70 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 UTF-16 Big-endi
afa80 61 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 an -> UTF-8 */.
afa90 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c while( zIn<
afaa0 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 zTerm ){.
afab0 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 49 READ_UTF16BE(zI
afac0 6e 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20 20 n, c); .
afad0 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 29 WRITE_UTF8(z, c)
afae0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
afaf0 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 7a 20 pMem->n = z
afb00 2d 20 7a 4f 75 74 3b 0a 20 20 7d 0a 20 20 2a 7a - zOut;. }. *z
afb10 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
afb20 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 (pMem->n+(desire
afb30 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
afb40 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 8?1:2))<=len );.
afb50 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
afb60 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
afb70 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d pMem->flags &=
afb80 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 ~(MEM_Static|ME
afb90 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 M_Dyn|MEM_Ephem)
afba0 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 ;. pMem->enc =
afbb0 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d desiredEnc;. pM
afbc0 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 em->flags |= (ME
afbd0 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b M_Term|MEM_Dyn);
afbe0 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 . pMem->z = (ch
afbf0 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d ar*)zOut;. pMem
afc00 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d ->zMalloc = pMem
afc10 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f ->z;..translate_
afc20 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 out:.#if defined
afc30 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 (TRANSLATE_TRACE
afc40 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
afc50 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 ITE_DEBUG). {.
afc60 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 char zBuf[100
afc70 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
afc80 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
afc90 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 (pMem, zBuf);.
afca0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
afcb0 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 , "OUTPUT: %s\n"
afcc0 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e , zBuf);. }.#en
afcd0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c dif. return SQL
afce0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
afcf0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
afd00 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d ecks for a byte-
afd10 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 order mark at th
afd20 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
afd30 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 he .** UTF-16 st
afd40 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a ring stored in *
afd50 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 pMem. If one is
afd60 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 present, it is r
afd70 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 emoved and.** th
afd80 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 e encoding of th
afd90 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 e Mem adjusted.
afda0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
afdb0 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 s not do any.**
afdc0 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 byte-swapping, i
afdd0 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e t just sets Mem.
afde0 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c enc appropriatel
afdf0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c y..**.** The all
afe00 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c ocation (static,
afe10 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 dynamic etc.) a
afe20 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 nd encoding of t
afe30 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a he Mem may be.**
afe40 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 changed by this
afe50 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 function..*/.SQ
afe60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
afe70 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 sqlite3VdbeMemH
afe80 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d andleBom(Mem *pM
afe90 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 em){. int rc =
afea0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 SQLITE_OK;. u8
afeb0 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 bom = 0;.. if(
afec0 70 4d 65 6d 2d 3e 6e 3c 30 20 7c 7c 20 70 4d 65 pMem->n<0 || pMe
afed0 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 20 20 75 38 m->n>1 ){. u8
afee0 20 62 31 20 3d 20 2a 28 75 38 20 2a 29 70 4d 65 b1 = *(u8 *)pMe
afef0 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 20 62 32 20 m->z;. u8 b2
aff00 3d 20 2a 28 28 28 75 38 20 2a 29 70 4d 65 6d 2d = *(((u8 *)pMem-
aff10 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 >z) + 1);. if
aff20 28 20 62 31 3d 3d 30 78 46 45 20 26 26 20 62 32 ( b1==0xFE && b2
aff30 3d 3d 30 78 46 46 20 29 7b 0a 20 20 20 20 20 20 ==0xFF ){.
aff40 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 bom = SQLITE_UTF
aff50 31 36 42 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 16BE;. }.
aff60 69 66 28 20 62 31 3d 3d 30 78 46 46 20 26 26 20 if( b1==0xFF &&
aff70 62 32 3d 3d 30 78 46 45 20 29 7b 0a 20 20 20 20 b2==0xFE ){.
aff80 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55 bom = SQLITE_U
aff90 54 46 31 36 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 TF16LE;. }.
affa0 7d 0a 20 20 0a 20 20 69 66 28 20 62 6f 6d 20 29 }. . if( bom )
affb0 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
affc0 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 e3VdbeMemMakeWri
affd0 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 teable(pMem);.
affe0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
afff0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 _OK ){. pMe
b0000 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 20 20 20 20 m->n -= 2;.
b0010 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a memmove(pMem->z
b0020 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 , &pMem->z[2], p
b0030 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 70 Mem->n);. p
b0040 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 Mem->z[pMem->n]
b0050 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70 4d = '\0';. pM
b0060 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d em->z[pMem->n+1]
b0070 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70 = '\0';. p
b0080 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
b0090 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d M_Term;. pM
b00a0 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f 6d 3b 0a 20 em->enc = bom;.
b00b0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
b00c0 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f n rc;.}.#endif /
b00d0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
b00e0 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a F16 */../*.** pZ
b00f0 20 69 73 20 61 20 55 54 46 2d 38 20 65 6e 63 6f is a UTF-8 enco
b0100 64 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 ded unicode stri
b0110 6e 67 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20 ng. If nByte is
b0120 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 0a less than zero,.
b0130 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 ** return the nu
b0140 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 mber of unicode
b0150 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 70 5a characters in pZ
b0160 20 75 70 20 74 6f 20 28 62 75 74 20 6e 6f 74 20 up to (but not
b0170 69 6e 63 6c 75 64 69 6e 67 29 0a 2a 2a 20 74 68 including).** th
b0180 65 20 66 69 72 73 74 20 30 78 30 30 20 62 79 74 e first 0x00 byt
b0190 65 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20 6e e. If nByte is n
b01a0 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 ot less than zer
b01b0 6f 2c 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a o, return the.**
b01c0 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f number of unico
b01d0 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e de characters in
b01e0 20 74 68 65 20 66 69 72 73 74 20 6e 42 79 74 65 the first nByte
b01f0 20 6f 66 20 70 5a 20 28 6f 72 20 75 70 20 74 6f of pZ (or up to
b0200 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30 .** the first 0
b0210 78 30 30 2c 20 77 68 69 63 68 65 76 65 72 20 63 x00, whichever c
b0220 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a omes first)..*/.
b0230 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b0240 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 nt sqlite3Utf8Ch
b0250 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 arLen(const char
b0260 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 42 79 74 65 *zIn, int nByte
b0270 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a ){. int r = 0;.
b0280 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 20 3d 20 const u8 *z =
b0290 28 63 6f 6e 73 74 20 75 38 2a 29 7a 49 6e 3b 0a (const u8*)zIn;.
b02a0 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 54 65 72 const u8 *zTer
b02b0 6d 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 3d m;. if( nByte>=
b02c0 30 20 29 7b 0a 20 20 20 20 7a 54 65 72 6d 20 3d 0 ){. zTerm =
b02d0 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a 20 20 7d 65 &z[nByte];. }e
b02e0 6c 73 65 7b 0a 20 20 20 20 7a 54 65 72 6d 20 3d lse{. zTerm =
b02f0 20 28 63 6f 6e 73 74 20 75 38 2a 29 28 2d 31 29 (const u8*)(-1)
b0300 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
b0310 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a 20 20 77 68 z<=zTerm );. wh
b0320 69 6c 65 28 20 2a 7a 21 3d 30 20 26 26 20 7a 3c ile( *z!=0 && z<
b0330 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 53 51 4c zTerm ){. SQL
b0340 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 ITE_SKIP_UTF8(z)
b0350 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 20 7d 0a 20 ;. r++;. }.
b0360 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a return r;.}../*
b0370 20 54 68 69 73 20 74 65 73 74 20 66 75 6e 63 74 This test funct
b0380 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75 72 72 65 ion is not curre
b0390 6e 74 6c 79 20 75 73 65 64 20 62 79 20 74 68 65 ntly used by the
b03a0 20 61 75 74 6f 6d 61 74 65 64 20 74 65 73 74 2d automated test-
b03b0 73 75 69 74 65 2e 20 0a 2a 2a 20 48 65 6e 63 65 suite. .** Hence
b03c0 20 69 74 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 it is only avai
b03d0 6c 61 62 6c 65 20 69 6e 20 64 65 62 75 67 20 62 lable in debug b
b03e0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 uilds..*/.#if de
b03f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
b0400 54 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 T) && defined(SQ
b0410 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a LITE_DEBUG)./*.*
b0420 2a 20 54 72 61 6e 73 6c 61 74 65 20 55 54 46 2d * Translate UTF-
b0430 38 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 8 to UTF-8..**.*
b0440 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20 65 * This has the e
b0450 66 66 65 63 74 20 6f 66 20 6d 61 6b 69 6e 67 20 ffect of making
b0460 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 74 sure that the st
b0470 72 69 6e 67 20 69 73 20 77 65 6c 6c 2d 66 6f 72 ring is well-for
b0480 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 2e 20 20 4d med.** UTF-8. M
b0490 69 73 63 6f 64 65 64 20 63 68 61 72 61 63 74 65 iscoded characte
b04a0 72 73 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a rs are removed..
b04b0 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 6c **.** The transl
b04c0 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e ation is done in
b04d0 2d 70 6c 61 63 65 20 28 73 69 6e 63 65 20 69 74 -place (since it
b04e0 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 is impossible f
b04f0 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63 or the.** correc
b0500 74 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 t UTF-8 encoding
b0510 20 74 6f 20 62 65 20 6c 6f 6e 67 65 72 20 74 68 to be longer th
b0520 61 6e 20 61 20 6d 61 6c 66 6f 72 6d 65 64 20 65 an a malformed e
b0530 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f 0a 53 51 4c ncoding)..*/.SQL
b0540 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b0550 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75 sqlite3Utf8To8(u
b0560 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 nsigned char *zI
b0570 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 n){. unsigned c
b0580 68 61 72 20 2a 7a 4f 75 74 20 3d 20 7a 49 6e 3b har *zOut = zIn;
b0590 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
b05a0 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49 6e 3b 0a *zStart = zIn;.
b05b0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
b05c0 2a 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 73 74 *zTerm = &zIn[st
b05d0 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 7a 49 6e rlen((char *)zIn
b05e0 29 5d 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20 )];. u32 c;..
b05f0 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b while( zIn[0] ){
b0600 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 . c = sqlite3
b0610 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 Utf8Read(zIn, zT
b0620 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a erm, (const u8**
b0630 29 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 )&zIn);. if(
b0640 63 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 c!=0xfffd ){.
b0650 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f WRITE_UTF8(zO
b0660 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 ut, c);. }.
b0670 7d 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 }. *zOut = 0;.
b0680 20 72 65 74 75 72 6e 20 7a 4f 75 74 20 2d 20 7a return zOut - z
b0690 53 74 61 72 74 3b 0a 7d 0a 23 65 6e 64 69 66 0a Start;.}.#endif.
b06a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
b06b0 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a OMIT_UTF16./*.**
b06c0 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31 Convert a UTF-1
b06d0 36 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6 string in the
b06e0 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 native encoding
b06f0 69 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 into a UTF-8 str
b0700 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 ing..** Memory t
b0710 6f 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38 o hold the UTF-8
b0720 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 string is obtai
b0730 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
b0740 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 _malloc and must
b0750 0a 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20 .** be freed by
b0760 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 the calling func
b0770 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c tion..**.** NULL
b0780 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
b0790 74 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f there is an allo
b07a0 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f cation error..*/
b07b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b07c0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 char *sqlite3Utf
b07d0 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 16to8(sqlite3 *d
b07e0 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a b, const void *z
b07f0 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 , int nByte){.
b0800 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 Mem m;. memset(
b0810 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 &m, 0, sizeof(m)
b0820 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a );. m.db = db;.
b0830 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
b0840 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 SetStr(&m, z, nB
b0850 79 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 yte, SQLITE_UTF1
b0860 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 6NATIVE, SQLITE_
b0870 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 STATIC);. sqlit
b0880 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
b0890 64 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f ding(&m, SQLITE_
b08a0 55 54 46 38 29 3b 0a 20 20 69 66 28 20 64 62 2d UTF8);. if( db-
b08b0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
b08c0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
b08d0 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a MemRelease(&m);.
b08e0 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d m.z = 0;. }
b08f0 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c . assert( (m.fl
b0900 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 ags & MEM_Term)!
b0910 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 =0 || db->malloc
b0920 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 Failed );. asse
b0930 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d rt( (m.flags & M
b0940 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c 20 64 62 EM_Str)!=0 || db
b0950 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
b0960 3b 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c ;. return (m.fl
b0970 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d ags & MEM_Dyn)!=
b0980 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65 0 ? m.z : sqlite
b0990 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 6d 2e 3DbStrDup(db, m.
b09a0 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 z);.}../*.** pZ
b09b0 69 73 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f is a UTF-16 enco
b09c0 64 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 ded unicode stri
b09d0 6e 67 2e 20 49 66 20 6e 43 68 61 72 20 69 73 20 ng. If nChar is
b09e0 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 0a less than zero,.
b09f0 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 ** return the nu
b0a00 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 75 70 mber of bytes up
b0a10 20 74 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 to (but not inc
b0a20 6c 75 64 69 6e 67 29 2c 20 74 68 65 20 66 69 72 luding), the fir
b0a30 73 74 20 70 61 69 72 0a 2a 2a 20 6f 66 20 63 6f st pair.** of co
b0a40 6e 73 65 63 75 74 69 76 65 20 30 78 30 30 20 62 nsecutive 0x00 b
b0a50 79 74 65 73 20 69 6e 20 70 5a 2e 20 49 66 20 6e ytes in pZ. If n
b0a60 43 68 61 72 20 69 73 20 6e 6f 74 20 6c 65 73 73 Char is not less
b0a70 20 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 74 than zero,.** t
b0a80 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65 20 6e hen return the n
b0a90 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
b0aa0 6e 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 61 n the first nCha
b0ab0 72 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 r unicode charac
b0ac0 74 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 20 28 6f ters.** in pZ (o
b0ad0 72 20 75 70 20 75 6e 74 69 6c 20 74 68 65 20 66 r up until the f
b0ae0 69 72 73 74 20 70 61 69 72 20 6f 66 20 30 78 30 irst pair of 0x0
b0af0 30 20 62 79 74 65 73 2c 20 77 68 69 63 68 65 76 0 bytes, whichev
b0b00 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e er comes first).
b0b10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b0b20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 TE int sqlite3Ut
b0b30 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 f16ByteLen(const
b0b40 20 76 6f 69 64 20 2a 7a 49 6e 2c 20 69 6e 74 20 void *zIn, int
b0b50 6e 43 68 61 72 29 7b 0a 20 20 75 6e 73 69 67 6e nChar){. unsign
b0b60 65 64 20 69 6e 74 20 63 20 3d 20 31 3b 0a 20 20 ed int c = 1;.
b0b70 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 3d 20 char const *z =
b0b80 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 zIn;. int n = 0
b0b90 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 55 ;. if( SQLITE_U
b0ba0 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49 TF16NATIVE==SQLI
b0bb0 54 45 5f 55 54 46 31 36 42 45 20 29 7b 0a 20 20 TE_UTF16BE ){.
b0bc0 20 20 2f 2a 20 55 73 69 6e 67 20 61 6e 20 22 69 /* Using an "i
b0bd0 66 20 28 53 51 4c 49 54 45 5f 55 54 46 31 36 4e f (SQLITE_UTF16N
b0be0 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 ATIVE==SQLITE_UT
b0bf0 46 31 36 42 45 29 22 20 63 6f 6e 73 74 72 75 63 F16BE)" construc
b0c00 74 20 68 65 72 65 0a 20 20 20 20 2a 2a 20 61 6e t here. ** an
b0c10 64 20 69 6e 20 6f 74 68 65 72 20 70 61 72 74 73 d in other parts
b0c20 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 6d 65 of this file me
b0c30 61 6e 73 20 74 68 61 74 20 61 74 20 6f 6e 65 20 ans that at one
b0c40 62 72 61 6e 63 68 20 77 69 6c 6c 0a 20 20 20 20 branch will.
b0c50 2a 2a 20 6e 6f 74 20 62 65 20 63 6f 76 65 72 65 ** not be covere
b0c60 64 20 62 79 20 63 6f 76 65 72 61 67 65 20 74 65 d by coverage te
b0c70 73 74 69 6e 67 20 6f 6e 20 61 6e 79 20 73 69 6e sting on any sin
b0c80 67 6c 65 20 68 6f 73 74 2e 20 42 75 74 20 63 6f gle host. But co
b0c90 76 65 72 61 67 65 0a 20 20 20 20 2a 2a 20 77 69 verage. ** wi
b0ca0 6c 6c 20 62 65 20 63 6f 6d 70 6c 65 74 65 20 69 ll be complete i
b0cb0 66 20 74 68 65 20 74 65 73 74 73 20 61 72 65 20 f the tests are
b0cc0 72 75 6e 20 6f 6e 20 62 6f 74 68 20 61 20 6c 69 run on both a li
b0cd0 74 74 6c 65 2d 65 6e 64 69 61 6e 20 61 6e 64 20 ttle-endian and
b0ce0 0a 20 20 20 20 2a 2a 20 62 69 67 2d 65 6e 64 69 . ** big-endi
b0cf0 61 6e 20 68 6f 73 74 2e 20 42 65 63 61 75 73 65 an host. Because
b0d00 20 62 6f 74 68 20 74 68 65 20 55 54 46 31 36 4e both the UTF16N
b0d10 41 54 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 ATIVE and SQLITE
b0d20 5f 55 54 46 31 36 42 45 0a 20 20 20 20 2a 2a 20 _UTF16BE. **
b0d30 6d 61 63 72 6f 73 20 61 72 65 20 63 6f 6e 73 74 macros are const
b0d40 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 ant at compile t
b0d50 69 6d 65 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 ime the compiler
b0d60 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65 0a 20 can determine.
b0d70 20 20 20 2a 2a 20 77 68 69 63 68 20 62 72 61 6e ** which bran
b0d80 63 68 20 77 69 6c 6c 20 62 65 20 66 6f 6c 6c 6f ch will be follo
b0d90 77 65 64 2e 20 49 74 20 69 73 20 74 68 65 72 65 wed. It is there
b0da0 66 6f 72 65 20 61 73 73 75 6d 65 64 20 74 68 61 fore assumed tha
b0db0 74 20 6e 6f 20 72 75 6e 74 69 6d 65 0a 20 20 20 t no runtime.
b0dc0 20 2a 2a 20 70 65 6e 61 6c 74 79 20 69 73 20 70 ** penalty is p
b0dd0 61 69 64 20 66 6f 72 20 74 68 69 73 20 22 69 66 aid for this "if
b0de0 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 " statement..
b0df0 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 63 */. while( c
b0e00 20 26 26 20 28 28 6e 43 68 61 72 3c 30 29 20 7c && ((nChar<0) |
b0e10 7c 20 6e 3c 6e 43 68 61 72 29 20 29 7b 0a 20 20 | n<nChar) ){.
b0e20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42 45 READ_UTF16BE
b0e30 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b (z, c);. n+
b0e40 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 +;. }. }else
b0e50 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 20 26 {. while( c &
b0e60 26 20 28 28 6e 43 68 61 72 3c 30 29 20 7c 7c 20 & ((nChar<0) ||
b0e70 6e 3c 6e 43 68 61 72 29 20 29 7b 0a 20 20 20 20 n<nChar) ){.
b0e80 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a READ_UTF16LE(z
b0e90 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b , c);. n++;
b0ea0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
b0eb0 75 72 6e 20 28 7a 2d 28 63 68 61 72 20 63 6f 6e urn (z-(char con
b0ec0 73 74 20 2a 29 7a 49 6e 29 2d 28 28 63 3d 3d 30 st *)zIn)-((c==0
b0ed0 29 3f 32 3a 30 29 3b 0a 7d 0a 0a 23 69 66 20 64 )?2:0);.}..#if d
b0ee0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 efined(SQLITE_TE
b0ef0 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 ST)./*.** This r
b0f00 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
b0f10 20 66 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 from the TCL te
b0f20 73 74 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 st function "tra
b0f30 6e 73 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 nslate_selftest"
b0f40 2e 0a 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 ..** It checks t
b0f50 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 hat the primitiv
b0f60 65 73 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 es for serializi
b0f70 6e 67 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 ng and deseriali
b0f80 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 zing.** characte
b0f90 72 73 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 rs in each encod
b0fa0 69 6e 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 ing are inverses
b0fb0 20 6f 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a of each other..
b0fc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b0fd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 E void sqlite3Ut
b0fe0 66 53 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b fSelfTest(void){
b0ff0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
b1000 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 i, t;. unsigned
b1010 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a char zBuf[20];.
b1020 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
b1030 2a 7a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 *z;. unsigned c
b1040 68 61 72 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 6e har *zTerm;. in
b1050 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 t n;. unsigned
b1060 69 6e 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69 3d int c;.. for(i=
b1070 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 0; i<0x00110000;
b1080 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 7a i++){. z = z
b1090 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 Buf;. WRITE_U
b10a0 54 46 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e TF8(z, i);. n
b10b0 20 3d 20 7a 2d 7a 42 75 66 3b 0a 20 20 20 20 7a = z-zBuf;. z
b10c0 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 54 65 [0] = 0;. zTe
b10d0 72 6d 20 3d 20 7a 3b 0a 20 20 20 20 7a 20 3d 20 rm = z;. z =
b10e0 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71 zBuf;. c = sq
b10f0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c lite3Utf8Read(z,
b1100 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 zTerm, (const u
b1110 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20 3d 8**)&z);. t =
b1120 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 i;. if( i>=0
b1130 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44 46 xD800 && i<=0xDF
b1140 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b FF ) t = 0xFFFD;
b1150 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 46 46 . if( (i&0xFF
b1160 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 FFFFFE)==0xFFFE
b1170 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 20 ) t = 0xFFFD;.
b1180 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20 29 assert( c==t )
b1190 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a ;. assert( (z
b11a0 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d -zBuf)==n );. }
b11b0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 . for(i=0; i<0x
b11c0 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 00110000; i++){.
b11d0 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30 if( i>=0xD80
b11e0 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 29 20 0 && i<0xE000 )
b11f0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 continue;. z
b1200 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 = zBuf;. WRIT
b1210 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29 3b E_UTF16LE(z, i);
b1220 0a 20 20 20 20 6e 20 3d 20 7a 2d 7a 42 75 66 3b . n = z-zBuf;
b1230 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 . z[0] = 0;.
b1240 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 z = zBuf;.
b1250 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c READ_UTF16LE(z,
b1260 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 c);. assert(
b1270 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 c==i );. ass
b1280 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e ert( (z-zBuf)==n
b1290 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d );. }. for(i=
b12a0 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 0; i<0x00110000;
b12b0 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 i++){. if( i
b12c0 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78 >=0xD800 && i<0x
b12d0 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b E000 ) continue;
b12e0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 . z = zBuf;.
b12f0 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 42 45 WRITE_UTF16BE
b1300 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 (z, i);. n =
b1310 7a 2d 7a 42 75 66 3b 0a 20 20 20 20 7a 5b 30 5d z-zBuf;. z[0]
b1320 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 = 0;. z = zB
b1330 75 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 uf;. READ_UTF
b1340 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 16BE(z, c);.
b1350 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a assert( c==i );.
b1360 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a assert( (z-z
b1370 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 7d Buf)==n );. }.}
b1380 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
b1390 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 69 66 E_TEST */.#endif
b13a0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
b13b0 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a UTF16 */../*****
b13c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
b13d0 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utf.c *********
b13e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b13f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1400 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
b1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
b1420 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a file util.c ****
b1430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1450 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
b1460 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
b1470 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
b1480 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
b1490 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
b14a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
b14b0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
b14c0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
b14d0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
b14e0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
b14f0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
b1500 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
b1510 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
b1520 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
b1530 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
b1540 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
b1550 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
b1560 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
b1570 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
b1580 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
b1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b15c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b15d0 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 6e 63 .** Utility func
b15e0 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 tions used throu
b15f0 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a ghout sqlite..**
b1600 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
b1610 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73 ntains functions
b1620 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e 67 20 for allocating
b1630 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 69 6e memory, comparin
b1640 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 61 6e g.** strings, an
b1650 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 68 61 d stuff like tha
b1660 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 74 t..**.** $Id: ut
b1670 69 6c 2e 63 2c 76 20 31 2e 32 34 31 20 32 30 30 il.c,v 1.241 200
b1680 38 2f 30 37 2f 32 38 20 31 39 3a 33 34 3a 35 34 8/07/28 19:34:54
b1690 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
b16a0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
b16b0 65 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e e if the floatin
b16c0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 g point value is
b16d0 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e Not a Number (N
b16e0 61 4e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 aN)..*/.SQLITE_P
b16f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b1700 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 e3IsNaN(double x
b1710 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20 4e 61 4e ){. /* This NaN
b1720 20 74 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 test sometimes
b1730 66 61 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 fails if compile
b1740 64 20 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 d on GCC with -f
b1750 66 61 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 fast-math.. **
b1760 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
b1770 64 2c 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 d, the use of -f
b1780 66 61 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 fast-math comes
b1790 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 with the followi
b17a0 6e 67 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a ng. ** warning:
b17b0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 . **. **
b17c0 54 68 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 This option [-ff
b17d0 61 73 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 ast-math] should
b17e0 20 6e 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 never be turned
b17f0 20 6f 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 on by any. **
b1800 20 20 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 -O option s
b1810 69 6e 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 ince it can resu
b1820 6c 74 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 lt in incorrect
b1830 6f 75 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 output for progr
b1840 61 6d 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 ams. ** wh
b1850 69 63 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e ich depend on an
b1860 20 65 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 exact implement
b1870 61 74 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 ation of IEEE or
b1880 20 49 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 ISO . **
b1890 72 75 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 rules/specificat
b18a0 69 6f 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 ions for math fu
b18b0 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 nctions.. **.
b18c0 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 ** Under MSVC, t
b18d0 68 69 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 his NaN test may
b18e0 20 66 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 fail if compile
b18f0 64 20 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e d with a floatin
b1900 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 g-. ** point pr
b1910 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 ecision mode oth
b1920 65 72 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 er than /fp:prec
b1930 69 73 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d ise. From the M
b1940 53 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 SDN . ** docume
b1950 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 ntation:. **.
b1960 2a 2a 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 ** The comp
b1970 69 6c 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 iler [with /fp:p
b1980 72 65 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f recise] will pro
b1990 70 65 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d perly handle com
b19a0 70 61 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 parisons . **
b19b0 20 20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 involving Na
b19c0 4e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 N. For example,
b19d0 78 20 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 x != x evaluates
b19e0 20 74 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 to true if x is
b19f0 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 NaN . **
b1a00 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 .... */.#ifdef
b1a10 5f 5f 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 __FAST_MATH__.#
b1a20 65 72 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c error SQLite wil
b1a30 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 l not work corre
b1a40 63 74 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 ctly with the -f
b1a50 66 61 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e fast-math option
b1a60 20 6f 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a of GCC..#endif.
b1a70 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c volatile doubl
b1a80 65 20 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 e y = x;. volat
b1a90 69 6c 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 ile double z = y
b1aa0 3b 0a 20 20 72 65 74 75 72 6e 20 79 21 3d 7a 3b ;. return y!=z;
b1ab0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
b1ac0 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 61 the length of a
b1ad0 20 73 74 72 69 6e 67 2c 20 65 78 63 65 70 74 20 string, except
b1ae0 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 65 do not allow the
b1af0 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 0a 2a string length.*
b1b00 2a 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 * to exceed the
b1b10 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
b1b20 47 54 48 20 73 65 74 74 69 6e 67 2e 0a 2a 2f 0a GTH setting..*/.
b1b30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b1b40 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e nt sqlite3Strlen
b1b50 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
b1b60 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 nst char *z){.
b1b70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d const char *z2 =
b1b80 20 7a 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 z;. int len;.
b1b90 20 73 69 7a 65 5f 74 20 78 3b 0a 20 20 77 68 69 size_t x;. whi
b1ba0 6c 65 28 20 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b le( *z2 ){ z2++;
b1bb0 20 7d 0a 20 20 78 20 3d 20 7a 32 20 2d 20 7a 3b }. x = z2 - z;
b1bc0 0a 20 20 6c 65 6e 20 3d 20 30 78 37 66 66 66 66 . len = 0x7ffff
b1bd0 66 66 66 20 26 20 78 3b 0a 20 20 69 66 28 20 6c fff & x;. if( l
b1be0 65 6e 21 3d 78 20 7c 7c 20 6c 65 6e 20 3e 20 64 en!=x || len > d
b1bf0 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
b1c00 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 _LIMIT_LENGTH] )
b1c10 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 62 2d {. return db-
b1c20 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
b1c30 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 IMIT_LENGTH];.
b1c40 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
b1c50 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a n len;. }.}../*
b1c60 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 6f 73 74 .** Set the most
b1c70 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f recent error co
b1c80 64 65 20 61 6e 64 20 65 72 72 6f 72 20 73 74 72 de and error str
b1c90 69 6e 67 20 66 6f 72 20 74 68 65 20 73 71 6c 69 ing for the sqli
b1ca0 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 22 64 62 te.** handle "db
b1cb0 22 2e 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 ". The error cod
b1cc0 65 20 69 73 20 73 65 74 20 74 6f 20 22 65 72 72 e is set to "err
b1cd0 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 _code"..**.** If
b1ce0 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c it is not NULL,
b1cf0 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d 61 74 20 string zFormat
b1d00 73 70 65 63 69 66 69 65 73 20 74 68 65 20 66 6f specifies the fo
b1d10 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 rmat of the.** e
b1d20 72 72 6f 72 20 73 74 72 69 6e 67 20 69 6e 20 74 rror string in t
b1d30 68 65 20 73 74 79 6c 65 20 6f 66 20 74 68 65 20 he style of the
b1d40 70 72 69 6e 74 66 20 66 75 6e 63 74 69 6f 6e 73 printf functions
b1d50 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a : The following.
b1d60 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61 72 61 63 ** format charac
b1d70 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 ters are allowed
b1d80 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 :.**.** %s
b1d90 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 Insert a st
b1da0 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 ring.** %z
b1db0 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68 A string th
b1dc0 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 at should be fre
b1dd0 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 ed after use.**
b1de0 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e 73 %d Ins
b1df0 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a ert an integer.*
b1e00 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 20 49 * %T I
b1e10 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a nsert a token.**
b1e20 20 20 20 20 20 20 25 53 20 20 20 20 20 20 49 6e %S In
b1e30 73 65 72 74 20 74 68 65 20 66 69 72 73 74 20 65 sert the first e
b1e40 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c lement of a SrcL
b1e50 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 6d 61 ist.**.** zForma
b1e60 74 20 61 6e 64 20 61 6e 79 20 73 74 72 69 6e 67 t and any string
b1e70 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 66 6f 6c tokens that fol
b1e80 6c 6f 77 20 69 74 20 61 72 65 20 61 73 73 75 6d low it are assum
b1e90 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65 6e 63 6f ed to be.** enco
b1ea0 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a ded in UTF-8..**
b1eb0 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20 74 68 65 .** To clear the
b1ec0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 most recent err
b1ed0 6f 72 20 66 6f 72 20 73 71 6c 69 74 65 20 68 61 or for sqlite ha
b1ee0 6e 64 6c 65 20 22 64 62 22 2c 20 73 71 6c 69 74 ndle "db", sqlit
b1ef0 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68 6f 75 6c e3Error.** shoul
b1f00 64 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 d be called with
b1f10 20 65 72 72 5f 63 6f 64 65 20 73 65 74 20 74 6f err_code set to
b1f20 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 7a SQLITE_OK and z
b1f30 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a 20 74 6f Format set.** to
b1f40 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 NULL..*/.SQLITE
b1f50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
b1f60 6c 69 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 lite3Error(sqlit
b1f70 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 72 72 5f e3 *db, int err_
b1f80 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 code, const char
b1f90 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
b1fa0 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 64 62 . if( db && (db
b1fb0 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62 2d 3e 70 ->pErr || (db->p
b1fc0 45 72 72 20 3d 20 73 71 6c 69 74 65 33 56 61 6c Err = sqlite3Val
b1fd0 75 65 4e 65 77 28 64 62 29 29 21 3d 30 29 20 29 ueNew(db))!=0) )
b1fe0 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 {. db->errCod
b1ff0 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b 0a 20 20 e = err_code;.
b2000 20 20 69 66 28 20 7a 46 6f 72 6d 61 74 20 29 7b if( zFormat ){
b2010 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a . char *z;.
b2020 20 20 20 20 20 20 76 61 5f 6c 69 73 74 20 61 70 va_list ap
b2030 3b 0a 20 20 20 20 20 20 76 61 5f 73 74 61 72 74 ;. va_start
b2040 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
b2050 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 z = sqlite3
b2060 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f VMPrintf(db, zFo
b2070 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 20 rmat, ap);.
b2080 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 20 va_end(ap);.
b2090 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 sqlite3ValueS
b20a0 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 etStr(db->pErr,
b20b0 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 5f 55 54 -1, z, SQLITE_UT
b20c0 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d F8, SQLITE_DYNAM
b20d0 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a IC);. }else{.
b20e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c sqlite3Val
b20f0 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 ueSetStr(db->pEr
b2100 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f r, 0, 0, SQLITE_
b2110 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 UTF8, SQLITE_STA
b2120 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TIC);. }. }.
b2130 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 }../*.** Add an
b2140 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f error message to
b2150 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 pParse->zErrMsg
b2160 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 70 and increment p
b2170 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a 20 Parse->nErr..**
b2180 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f The following fo
b2190 72 6d 61 74 74 69 6e 67 20 63 68 61 72 61 63 74 rmatting charact
b21a0 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a ers are allowed:
b21b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 .**.** %s
b21c0 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 72 Insert a str
b21d0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 ing.** %z
b21e0 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68 61 A string tha
b21f0 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 t should be free
b2200 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 d after use.**
b2210 20 20 20 20 25 64 20 20 20 20 20 20 49 6e 73 65 %d Inse
b2220 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a rt an integer.**
b2230 20 20 20 20 20 20 25 54 20 20 20 20 20 20 49 6e %T In
b2240 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 sert a token.**
b2250 20 20 20 20 20 25 53 20 20 20 20 20 20 49 6e 73 %S Ins
b2260 65 72 74 20 74 68 65 20 66 69 72 73 74 20 65 6c ert the first el
b2270 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 ement of a SrcLi
b2280 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 st.**.** This fu
b2290 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 nction should be
b22a0 20 75 73 65 64 20 74 6f 20 72 65 70 6f 72 74 20 used to report
b22b0 61 6e 79 20 65 72 72 6f 72 20 74 68 61 74 20 6f any error that o
b22c0 63 63 75 72 73 20 77 68 69 6c 73 74 0a 2a 2a 20 ccurs whilst.**
b22d0 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 53 51 4c compiling an SQL
b22e0 20 73 74 61 74 65 6d 65 6e 74 20 28 69 2e 65 2e statement (i.e.
b22f0 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f within sqlite3_
b2300 70 72 65 70 61 72 65 28 29 29 2e 20 54 68 65 0a prepare()). The.
b2310 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 20 74 68 ** last thing th
b2320 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 e sqlite3_prepar
b2330 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 e() function doe
b2340 73 20 69 73 20 63 6f 70 79 20 74 68 65 20 65 72 s is copy the er
b2350 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 20 62 79 ror.** stored by
b2360 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
b2370 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
b2380 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 20 73 71 handle using sq
b2390 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a 2a 2a lite3Error()..**
b23a0 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 Function sqlite
b23b0 33 45 72 72 6f 72 28 29 20 73 68 6f 75 6c 64 20 3Error() should
b23c0 62 65 20 75 73 65 64 20 64 75 72 69 6e 67 20 73 be used during s
b23d0 74 61 74 65 6d 65 6e 74 20 65 78 65 63 75 74 69 tatement executi
b23e0 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 33 5f 73 on.** (sqlite3_s
b23f0 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 2a 2f 0a tep() etc.)..*/.
b2400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
b2410 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 oid sqlite3Error
b2420 4d 73 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 Msg(Parse *pPars
b2430 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
b2440 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 Format, ...){.
b2450 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 73 71 va_list ap;. sq
b2460 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
b2470 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61 72 73 65 se->db;. pParse
b2480 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 73 71 6c 69 ->nErr++;. sqli
b2490 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 te3DbFree(db, pP
b24a0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a arse->zErrMsg);.
b24b0 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
b24c0 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50 61 72 73 Format);. pPars
b24d0 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c e->zErrMsg = sql
b24e0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c ite3VMPrintf(db,
b24f0 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 zFormat, ap);.
b2500 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 va_end(ap);. i
b2510 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 f( pParse->rc==S
b2520 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
b2530 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c pParse->rc = SQL
b2540 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 7d ITE_ERROR;. }.}
b2550 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
b2560 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
b2570 69 6e 20 70 50 61 72 73 65 2c 20 69 66 20 61 6e in pParse, if an
b2580 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 y.*/.SQLITE_PRIV
b2590 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
b25a0 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65 ErrorClear(Parse
b25b0 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c *pParse){. sql
b25c0 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73 ite3DbFree(pPars
b25d0 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a e->db, pParse->z
b25e0 45 72 72 4d 73 67 29 3b 0a 20 20 70 50 61 72 73 ErrMsg);. pPars
b25f0 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a e->zErrMsg = 0;.
b2600 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d pParse->nErr =
b2610 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 0;.}../*.** Con
b2620 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c vert an SQL-styl
b2630 65 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 e quoted string
b2640 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 into a normal st
b2650 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 ring by removing
b2660 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 .** the quote ch
b2670 61 72 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 aracters. The c
b2680 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e onversion is don
b2690 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 e in-place. If
b26a0 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 the.** input doe
b26b0 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 s not begin with
b26c0 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 a quote charact
b26d0 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f er, then this ro
b26e0 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f utine.** is a no
b26f0 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d -op..**.** 2002-
b2700 46 65 62 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 Feb-14: This rou
b2710 74 69 6e 65 20 69 73 20 65 78 74 65 6e 64 65 64 tine is extended
b2720 20 74 6f 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 to remove MS-Ac
b2730 63 65 73 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 cess style.** br
b2740 61 63 6b 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 ackets from arou
b2750 6e 64 20 69 64 65 6e 74 69 66 65 72 73 2e 20 20 nd identifers.
b2760 46 6f 72 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b For example: "[
b2770 61 2d 62 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a a-b-c]" becomes.
b2780 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 ** "a-b-c"..*/.S
b2790 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
b27a0 69 64 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 id sqlite3Dequot
b27b0 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e e(char *z){. in
b27c0 74 20 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 t quote;. int i
b27d0 2c 20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 , j;. if( z==0
b27e0 29 20 72 65 74 75 72 6e 3b 0a 20 20 71 75 6f 74 ) return;. quot
b27f0 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 e = z[0];. swit
b2800 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20 ch( quote ){.
b2810 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65 case '\'': bre
b2820 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27 ak;. case '"'
b2830 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 : break;. c
b2840 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b ase '`': break
b2850 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b2860 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f /* For MySQL co
b2870 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 mpatibility */.
b2880 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 case '[': q
b2890 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 uote = ']'; bre
b28a0 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 ak; /* For MS S
b28b0 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 qlServer compati
b28c0 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65 bility */. de
b28d0 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e fault: return
b28e0 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c ;. }. for(i=1,
b28f0 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 j=0; z[i]; i++)
b2900 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d {. if( z[i]==
b2910 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 69 quote ){. i
b2920 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 f( z[i+1]==quote
b2930 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b ){. z[j+
b2940 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20 20 20 +] = quote;.
b2950 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d i++;. }
b2960 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b else{. z[
b2970 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 j++] = 0;.
b2980 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
b2990 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
b29a0 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b z[j++] = z[i];
b29b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
b29c0 20 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f 72 Convenient shor
b29d0 74 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 6e t-hand */.#defin
b29e0 65 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 73 e UpperToLower s
b29f0 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 qlite3UpperToLow
b2a00 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 73 er../*.** Some s
b2a10 79 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 69 ystems have stri
b2a20 63 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 68 cmp(). Others h
b2a30 61 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 29 ave strcasecmp()
b2a40 2e 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68 . Because.** th
b2a50 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 73 ere is no consis
b2a60 74 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 64 tency, we will d
b2a70 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a 2a efine our own..*
b2a80 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b2a90 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 int sqlite3StrI
b2aa0 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a Cmp(const char *
b2ab0 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 zLeft, const cha
b2ac0 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 72 65 r *zRight){. re
b2ad0 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 gister unsigned
b2ae0 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 char *a, *b;. a
b2af0 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
b2b00 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d r *)zLeft;. b =
b2b10 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
b2b20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c *)zRight;. whil
b2b30 65 28 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65 e( *a!=0 && Uppe
b2b40 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 rToLower[*a]==Up
b2b50 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b perToLower[*b]){
b2b60 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 a++; b++; }. r
b2b70 65 74 75 72 6e 20 55 70 70 65 72 54 6f 4c 6f 77 eturn UpperToLow
b2b80 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f er[*a] - UpperTo
b2b90 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c Lower[*b];.}.SQL
b2ba0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b2bb0 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 sqlite3StrNICmp(
b2bc0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 const char *zLef
b2bd0 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a t, const char *z
b2be0 52 69 67 68 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 Right, int N){.
b2bf0 20 72 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e register unsign
b2c00 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a ed char *a, *b;.
b2c10 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 a = (unsigned
b2c20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 char *)zLeft;.
b2c30 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 b = (unsigned ch
b2c40 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 ar *)zRight;. w
b2c50 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 hile( N-- > 0 &&
b2c60 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65 72 54 *a!=0 && UpperT
b2c70 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 oLower[*a]==Uppe
b2c80 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 rToLower[*b]){ a
b2c90 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 ++; b++; }. ret
b2ca0 75 72 6e 20 4e 3c 30 20 3f 20 30 20 3a 20 55 70 urn N<0 ? 0 : Up
b2cb0 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d perToLower[*a] -
b2cc0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 UpperToLower[*b
b2cd0 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ];.}../*.** Retu
b2ce0 72 6e 20 54 52 55 45 20 69 66 20 7a 20 69 73 20 rn TRUE if z is
b2cf0 61 20 70 75 72 65 20 6e 75 6d 65 72 69 63 20 73 a pure numeric s
b2d00 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 46 tring. Return F
b2d10 41 4c 53 45 20 69 66 20 74 68 65 0a 2a 2a 20 73 ALSE if the.** s
b2d20 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 61 tring contains a
b2d30 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 68 69 ny character whi
b2d40 63 68 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f ch is not part o
b2d50 66 20 61 20 6e 75 6d 62 65 72 2e 20 49 66 0a 2a f a number. If.*
b2d60 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 * the string is
b2d70 6e 75 6d 65 72 69 63 20 61 6e 64 20 63 6f 6e 74 numeric and cont
b2d80 61 69 6e 73 20 74 68 65 20 27 2e 27 20 63 68 61 ains the '.' cha
b2d90 72 61 63 74 65 72 2c 20 73 65 74 20 2a 72 65 61 racter, set *rea
b2da0 6c 6e 75 6d 0a 2a 2a 20 74 6f 20 54 52 55 45 20 lnum.** to TRUE
b2db0 28 6f 74 68 65 72 77 69 73 65 20 46 41 4c 53 45 (otherwise FALSE
b2dc0 29 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6d 70 74 )..**.** An empt
b2dd0 79 20 73 74 72 69 6e 67 20 69 73 20 63 6f 6e 73 y string is cons
b2de0 69 64 65 72 65 64 20 6e 6f 6e 2d 6e 75 6d 65 72 idered non-numer
b2df0 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ic..*/.SQLITE_PR
b2e00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b2e10 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 3IsNumber(const
b2e20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 72 65 char *z, int *re
b2e30 61 6c 6e 75 6d 2c 20 75 38 20 65 6e 63 29 7b 0a alnum, u8 enc){.
b2e40 20 20 69 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e int incr = (en
b2e50 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 c==SQLITE_UTF8?1
b2e60 3a 32 29 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d :2);. if( enc==
b2e70 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 SQLITE_UTF16BE )
b2e80 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d z++;. if( *z==
b2e90 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 20 29 '-' || *z=='+' )
b2ea0 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 z += incr;. if
b2eb0 28 20 21 69 73 64 69 67 69 74 28 2a 28 75 38 2a ( !isdigit(*(u8*
b2ec0 29 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 )z) ){. retur
b2ed0 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d 20 n 0;. }. z +=
b2ee0 69 6e 63 72 3b 0a 20 20 69 66 28 20 72 65 61 6c incr;. if( real
b2ef0 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d 20 3d num ) *realnum =
b2f00 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 64 0;. while( isd
b2f10 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b igit(*(u8*)z) ){
b2f20 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 z += incr; }.
b2f30 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 if( *z=='.' ){.
b2f40 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 z += incr;.
b2f50 20 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a if( !isdigit(*
b2f60 28 75 38 2a 29 7a 29 20 29 20 72 65 74 75 72 6e (u8*)z) ) return
b2f70 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 0;. while( i
b2f80 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 sdigit(*(u8*)z)
b2f90 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a ){ z += incr; }.
b2fa0 20 20 20 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 if( realnum
b2fb0 29 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a ) *realnum = 1;.
b2fc0 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 }. if( *z=='e
b2fd0 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a ' || *z=='E' ){.
b2fe0 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 z += incr;.
b2ff0 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c if( *z=='+' |
b3000 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d | *z=='-' ) z +=
b3010 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 incr;. if( !
b3020 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 isdigit(*(u8*)z)
b3030 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
b3040 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28 while( isdigit(
b3050 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a 20 2b 3d *(u8*)z) ){ z +=
b3060 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 69 66 28 incr; }. if(
b3070 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65 61 6c realnum ) *real
b3080 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 num = 1;. }. r
b3090 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a eturn *z==0;.}..
b30a0 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 /*.** The string
b30b0 20 7a 5b 5d 20 69 73 20 61 6e 20 61 73 63 69 69 z[] is an ascii
b30c0 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
b30d0 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 of a real number
b30e0 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 ..** Convert thi
b30f0 73 20 73 74 72 69 6e 67 20 74 6f 20 61 20 64 6f s string to a do
b3100 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 uble..**.** This
b3110 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73 routine assumes
b3120 20 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c 6c 79 that z[] really
b3130 20 69 73 20 61 20 76 61 6c 69 64 20 6e 75 6d 62 is a valid numb
b3140 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 69 73 er. If it.** is
b3150 20 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 6c 74 not, the result
b3160 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
b3170 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
b3180 65 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 e is used instea
b3190 64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 d of the library
b31a0 20 61 74 6f 66 28 29 20 66 75 6e 63 74 69 6f 6e atof() function
b31b0 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 because.** the
b31c0 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 6d library atof() m
b31d0 69 67 68 74 20 77 61 6e 74 20 74 6f 20 75 73 65 ight want to use
b31e0 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69 "," as the deci
b31f0 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 65 61 mal point instea
b3200 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 70 65 d.** of "." depe
b3210 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f 63 nding on how loc
b3220 61 6c 65 20 69 73 20 73 65 74 2e 20 20 42 75 74 ale is set. But
b3230 20 74 68 61 74 20 77 6f 75 6c 64 20 63 61 75 73 that would caus
b3240 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 6f e problems.** fo
b3250 72 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 73 20 r SQL. So this
b3260 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20 75 routine always u
b3270 73 65 73 20 22 2e 22 20 72 65 67 61 72 64 6c 65 ses "." regardle
b3280 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f ss of locale..*/
b3290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b32a0 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 int sqlite3AtoF(
b32b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 const char *z, d
b32c0 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 29 7b ouble *pResult){
b32d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
b32e0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
b32f0 49 4e 54 0a 20 20 69 6e 74 20 73 69 67 6e 20 3d INT. int sign =
b3300 20 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 1;. const char
b3310 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 20 *zBegin = z;.
b3320 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 LONGDOUBLE_TYPE
b3330 76 31 20 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20 v1 = 0.0;. int
b3340 6e 53 69 67 6e 69 66 69 63 61 6e 74 20 3d 20 30 nSignificant = 0
b3350 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 ;. while( isspa
b3360 63 65 28 2a 28 75 38 2a 29 7a 29 20 29 20 7a 2b ce(*(u8*)z) ) z+
b3370 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 +;. if( *z=='-'
b3380 20 29 7b 0a 20 20 20 20 73 69 67 6e 20 3d 20 2d ){. sign = -
b3390 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 1;. z++;. }e
b33a0 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 lse if( *z=='+'
b33b0 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a ){. z++;. }.
b33c0 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 while( z[0]=='
b33d0 30 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 0' ){. z++;.
b33e0 20 7d 0a 20 20 77 68 69 6c 65 28 20 69 73 64 69 }. while( isdi
b33f0 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a git(*(u8*)z) ){.
b3400 20 20 20 20 76 31 20 3d 20 76 31 2a 31 30 2e 30 v1 = v1*10.0
b3410 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 + (*z - '0');.
b3420 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 6e 53 69 67 z++;. nSig
b3430 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 7d 0a nificant++;. }.
b3440 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b if( *z=='.' ){
b3450 0a 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f . LONGDOUBLE_
b3460 54 59 50 45 20 64 69 76 69 73 6f 72 20 3d 20 31 TYPE divisor = 1
b3470 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 .0;. z++;.
b3480 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63 61 6e if( nSignifican
b3490 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 68 t==0 ){. wh
b34a0 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 ile( z[0]=='0' )
b34b0 7b 0a 20 20 20 20 20 20 20 20 64 69 76 69 73 6f {. diviso
b34c0 72 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 r *= 10.0;.
b34d0 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a z++;. }.
b34e0 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 }. while(
b34f0 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a isdigit(*(u8*)z
b3500 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e ) ){. if( n
b3510 53 69 67 6e 69 66 69 63 61 6e 74 3c 31 38 20 29 Significant<18 )
b3520 7b 0a 20 20 20 20 20 20 20 20 76 31 20 3d 20 76 {. v1 = v
b3530 31 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27 1*10.0 + (*z - '
b3540 30 27 29 3b 0a 20 20 20 20 20 20 20 20 64 69 76 0');. div
b3550 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 isor *= 10.0;.
b3560 20 20 20 20 20 20 6e 53 69 67 6e 69 66 69 63 61 nSignifica
b3570 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 nt++;. }.
b3580 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 z++;. }.
b3590 20 20 20 76 31 20 2f 3d 20 64 69 76 69 73 6f 72 v1 /= divisor
b35a0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d ;. }. if( *z==
b35b0 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 'e' || *z=='E' )
b35c0 7b 0a 20 20 20 20 69 6e 74 20 65 73 69 67 6e 20 {. int esign
b35d0 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20 65 76 61 = 1;. int eva
b35e0 6c 20 3d 20 30 3b 0a 20 20 20 20 4c 4f 4e 47 44 l = 0;. LONGD
b35f0 4f 55 42 4c 45 5f 54 59 50 45 20 73 63 61 6c 65 OUBLE_TYPE scale
b3600 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b = 1.0;. z++;
b3610 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 . if( *z=='-'
b3620 20 29 7b 0a 20 20 20 20 20 20 65 73 69 67 6e 20 ){. esign
b3630 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b = -1;. z++;
b3640 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 2a . }else if( *
b3650 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 z=='+' ){.
b3660 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 z++;. }. w
b3670 68 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a 28 hile( isdigit(*(
b3680 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 20 20 u8*)z) ){.
b3690 65 76 61 6c 20 3d 20 65 76 61 6c 2a 31 30 20 2b eval = eval*10 +
b36a0 20 2a 7a 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 *z - '0';.
b36b0 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 z++;. }.
b36c0 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 36 34 20 while( eval>=64
b36d0 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 ){ scale *= 1.0e
b36e0 2b 36 34 3b 20 65 76 61 6c 20 2d 3d 20 36 34 3b +64; eval -= 64;
b36f0 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76 }. while( ev
b3700 61 6c 3e 3d 31 36 20 29 7b 20 73 63 61 6c 65 20 al>=16 ){ scale
b3710 2a 3d 20 31 2e 30 65 2b 31 36 3b 20 65 76 61 6c *= 1.0e+16; eval
b3720 20 2d 3d 20 31 36 3b 20 7d 0a 20 20 20 20 77 68 -= 16; }. wh
b3730 69 6c 65 28 20 65 76 61 6c 3e 3d 34 20 29 7b 20 ile( eval>=4 ){
b3740 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 34 3b scale *= 1.0e+4;
b3750 20 65 76 61 6c 20 2d 3d 20 34 3b 20 7d 0a 20 20 eval -= 4; }.
b3760 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31 while( eval>=1
b3770 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 ){ scale *= 1.0
b3780 65 2b 31 3b 20 65 76 61 6c 20 2d 3d 20 31 3b 20 e+1; eval -= 1;
b3790 7d 0a 20 20 20 20 69 66 28 20 65 73 69 67 6e 3c }. if( esign<
b37a0 30 20 29 7b 0a 20 20 20 20 20 20 76 31 20 2f 3d 0 ){. v1 /=
b37b0 20 73 63 61 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 scale;. }els
b37c0 65 7b 0a 20 20 20 20 20 20 76 31 20 2a 3d 20 73 e{. v1 *= s
b37d0 63 61 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a cale;. }. }.
b37e0 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 73 69 67 *pResult = sig
b37f0 6e 3c 30 20 3f 20 2d 76 31 20 3a 20 76 31 3b 0a n<0 ? -v1 : v1;.
b3800 20 20 72 65 74 75 72 6e 20 7a 20 2d 20 7a 42 65 return z - zBe
b3810 67 69 6e 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 gin;.#else. ret
b3820 75 72 6e 20 73 71 6c 69 74 65 33 41 74 6f 69 36 urn sqlite3Atoi6
b3830 34 28 7a 2c 20 70 52 65 73 75 6c 74 29 3b 0a 23 4(z, pResult);.#
b3840 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
b3850 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
b3860 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 INT */.}../*.**
b3870 43 6f 6d 70 61 72 65 20 74 68 65 20 31 39 2d 63 Compare the 19-c
b3880 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 haracter string
b3890 7a 4e 75 6d 20 61 67 61 69 6e 73 74 20 74 68 65 zNum against the
b38a0 20 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 text representa
b38b0 74 69 6f 6e 0a 2a 2a 20 76 61 6c 75 65 20 32 5e tion.** value 2^
b38c0 36 33 3a 20 20 39 32 32 33 33 37 32 30 33 36 38 63: 92233720368
b38d0 35 34 37 37 35 38 30 38 2e 20 20 52 65 74 75 72 54775808. Retur
b38e0 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f n negative, zero
b38f0 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 0a 2a 2a , or positive.**
b3900 20 69 66 20 7a 4e 75 6d 20 69 73 20 6c 65 73 73 if zNum is less
b3910 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c than, equal to,
b3920 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e or greater than
b3930 20 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a the string..**.
b3940 2a 2a 20 55 6e 6c 69 6b 65 20 6d 65 6d 63 6d 70 ** Unlike memcmp
b3950 28 29 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 () this routine
b3960 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f is guaranteed to
b3970 20 72 65 74 75 72 6e 20 74 68 65 20 64 69 66 66 return the diff
b3980 65 72 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 erence.** in the
b3990 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 6c values of the l
b39a0 61 73 74 20 64 69 67 69 74 20 69 66 20 74 68 65 ast digit if the
b39b0 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 only difference
b39c0 20 69 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 61 is in the.** la
b39d0 73 74 20 64 69 67 69 74 2e 20 20 53 6f 2c 20 66 st digit. So, f
b39e0 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a or example,.**.*
b39f0 2a 20 20 20 20 20 20 63 6f 6d 70 61 72 65 32 70 * compare2p
b3a00 6f 77 36 33 28 22 39 32 32 33 33 37 32 30 33 36 ow63("9223372036
b3a10 38 35 34 37 37 35 38 30 30 22 29 0a 2a 2a 0a 2a 854775800").**.*
b3a20 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 2d 38 * will return -8
b3a30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
b3a40 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 63 6f compare2pow63(co
b3a50 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 29 7b nst char *zNum){
b3a60 0a 20 20 69 6e 74 20 63 3b 0a 20 20 63 20 3d 20 . int c;. c =
b3a70 6d 65 6d 63 6d 70 28 7a 4e 75 6d 2c 22 39 32 32 memcmp(zNum,"922
b3a80 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 22 337203685477580"
b3a90 2c 31 38 29 3b 0a 20 20 69 66 28 20 63 3d 3d 30 ,18);. if( c==0
b3aa0 20 29 7b 0a 20 20 20 20 63 20 3d 20 7a 4e 75 6d ){. c = zNum
b3ab0 5b 31 38 5d 20 2d 20 27 38 27 3b 0a 20 20 7d 0a [18] - '8';. }.
b3ac0 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a return c;.}...
b3ad0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
b3ae0 45 20 69 66 20 7a 4e 75 6d 20 69 73 20 61 20 36 E if zNum is a 6
b3af0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 4-bit signed int
b3b00 65 67 65 72 20 61 6e 64 20 77 72 69 74 65 0a 2a eger and write.*
b3b10 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 * the value of t
b3b20 68 65 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 he integer into
b3b30 2a 70 4e 75 6d 2e 20 20 49 66 20 7a 4e 75 6d 20 *pNum. If zNum
b3b40 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 is not an intege
b3b50 72 0a 2a 2a 20 6f 72 20 69 73 20 61 6e 20 69 6e r.** or is an in
b3b60 74 65 67 65 72 20 74 68 61 74 20 69 73 20 74 6f teger that is to
b3b70 6f 20 6c 61 72 67 65 20 74 6f 20 62 65 20 65 78 o large to be ex
b3b80 70 72 65 73 73 65 64 20 77 69 74 68 20 36 34 20 pressed with 64
b3b90 62 69 74 73 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 bits,.** then re
b3ba0 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a turn false..**.*
b3bb0 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f 75 74 * When this rout
b3bc0 69 6e 65 20 77 61 73 20 6f 72 69 67 69 6e 61 6c ine was original
b3bd0 6c 79 20 77 72 69 74 74 65 6e 20 69 74 20 64 65 ly written it de
b3be0 61 6c 74 20 77 69 74 68 20 6f 6e 6c 79 0a 2a 2a alt with only.**
b3bf0 20 33 32 2d 62 69 74 20 6e 75 6d 62 65 72 73 2e 32-bit numbers.
b3c00 20 20 41 74 20 74 68 61 74 20 74 69 6d 65 2c 20 At that time,
b3c10 69 74 20 77 61 73 20 6d 75 63 68 20 66 61 73 74 it was much fast
b3c20 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a 20 61 er than the.** a
b3c30 74 6f 69 28 29 20 6c 69 62 72 61 72 79 20 72 6f toi() library ro
b3c40 75 74 69 6e 65 20 69 6e 20 52 65 64 48 61 74 20 utine in RedHat
b3c50 37 2e 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 7.2..*/.SQLITE_P
b3c60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b3c70 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20 63 e3Atoi64(const c
b3c80 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34 20 2a har *zNum, i64 *
b3c90 70 4e 75 6d 29 7b 0a 20 20 69 36 34 20 76 20 3d pNum){. i64 v =
b3ca0 20 30 3b 0a 20 20 69 6e 74 20 6e 65 67 3b 0a 20 0;. int neg;.
b3cb0 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 63 6f 6e int i, c;. con
b3cc0 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 3b st char *zStart;
b3cd0 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 . while( isspac
b3ce0 65 28 2a 28 75 38 2a 29 7a 4e 75 6d 29 20 29 20 e(*(u8*)zNum) )
b3cf0 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a zNum++;. if( *z
b3d00 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 Num=='-' ){.
b3d10 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 neg = 1;. zNu
b3d20 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 m++;. }else if(
b3d30 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 *zNum=='+' ){.
b3d40 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 neg = 0;.
b3d50 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b zNum++;. }else{
b3d60 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 . neg = 0;.
b3d70 7d 0a 20 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 }. zStart = zNu
b3d80 6d 3b 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d m;. while( zNum
b3d90 5b 30 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d [0]=='0' ){ zNum
b3da0 2b 2b 3b 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 ++; } /* Skip ov
b3db0 65 72 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 er leading zeros
b3dc0 2e 20 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a . Ticket #2454 *
b3dd0 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d /. for(i=0; (c=
b3de0 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 zNum[i])>='0' &&
b3df0 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 c<='9'; i++){.
b3e00 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 v = v*10 + c
b3e10 2d 20 27 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e - '0';. }. *pN
b3e20 75 6d 20 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 um = neg ? -v :
b3e30 76 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 7c 7c v;. if( c!=0 ||
b3e40 20 28 69 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 (i==0 && zStart
b3e50 3d 3d 7a 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 ==zNum) || i>19
b3e60 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 ){. /* zNum i
b3e70 73 20 65 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 s empty or conta
b3e80 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 ins non-numeric
b3e90 74 65 78 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 text or is longe
b3ea0 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 r. ** than 19
b3eb0 20 64 69 67 69 74 73 20 28 74 68 75 73 20 67 75 digits (thus gu
b3ec0 61 72 61 6e 74 69 6e 67 20 74 68 61 74 20 69 74 aranting that it
b3ed0 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a is too large) *
b3ee0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a /. return 0;.
b3ef0 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3c 31 39 }else if( i<19
b3f00 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 ){. /* Less
b3f10 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 2c 20 than 19 digits,
b3f20 73 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 so we know that
b3f30 69 74 20 66 69 74 73 20 69 6e 20 36 34 20 62 69 it fits in 64 bi
b3f40 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e ts */. return
b3f50 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1;. }else{.
b3f60 20 2f 2a 20 31 39 2d 64 69 67 69 74 20 6e 75 6d /* 19-digit num
b3f70 62 65 72 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 bers must be no
b3f80 6c 61 72 67 65 72 20 74 68 61 6e 20 39 32 32 33 larger than 9223
b3f90 33 37 32 30 33 36 38 35 34 37 37 35 38 30 37 20 372036854775807
b3fa0 69 66 20 70 6f 73 69 74 69 76 65 0a 20 20 20 20 if positive.
b3fb0 2a 2a 20 6f 72 20 39 32 32 33 33 37 32 30 33 36 ** or 9223372036
b3fc0 38 35 34 37 37 35 38 30 38 20 69 66 20 6e 65 67 854775808 if neg
b3fd0 61 74 69 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 ative. Note tha
b3fe0 74 20 39 32 32 33 33 37 32 30 33 36 38 35 34 36 t 92233720368546
b3ff0 36 35 38 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 65808. ** is
b4000 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 2^63. */. ret
b4010 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 urn compare2pow6
b4020 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 3(zNum)<neg;. }
b4030 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 .}../*.** The st
b4040 72 69 6e 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 ring zNum repres
b4050 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72 2e ents an integer.
b4060 20 20 54 68 65 72 65 20 6d 69 67 68 74 20 62 65 There might be
b4070 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 69 some other.** i
b4080 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 6c 6c 6f nformation follo
b4090 77 69 6e 67 20 74 68 65 20 69 6e 74 65 67 65 72 wing the integer
b40a0 20 74 6f 6f 2c 20 62 75 74 20 74 68 61 74 20 70 too, but that p
b40b0 61 72 74 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a art is ignored..
b40c0 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 67 65 ** If the intege
b40d0 72 20 74 68 61 74 20 74 68 65 20 70 72 65 66 69 r that the prefi
b40e0 78 20 6f 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 x of zNum repres
b40f0 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20 69 6e ents will fit in
b4100 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73 69 67 a.** 64-bit sig
b4110 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72 65 74 ned integer, ret
b4120 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68 65 72 urn TRUE. Other
b4130 77 69 73 65 20 72 65 74 75 72 6e 20 46 41 4c 53 wise return FALS
b4140 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f E..**.** This ro
b4150 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 46 41 utine returns FA
b4160 4c 53 45 20 66 6f 72 20 74 68 65 20 73 74 72 69 LSE for the stri
b4170 6e 67 20 2d 39 32 32 33 33 37 32 30 33 36 38 35 ng -922337203685
b4180 34 37 37 35 38 30 38 20 65 76 65 6e 20 74 68 61 4775808 even tha
b4190 74 0a 2a 2a 20 74 68 61 74 20 6e 75 6d 62 65 72 t.** that number
b41a0 20 77 69 6c 6c 2c 20 69 6e 20 74 68 65 6f 72 79 will, in theory
b41b0 20 66 69 74 20 69 6e 20 61 20 36 34 2d 62 69 74 fit in a 64-bit
b41c0 20 69 6e 74 65 67 65 72 2e 20 20 50 6f 73 69 74 integer. Posit
b41d0 69 76 65 0a 2a 2a 20 39 32 32 33 33 37 33 30 33 ive.** 922337303
b41e0 36 38 35 34 37 37 35 38 30 38 20 77 69 6c 6c 20 6854775808 will
b41f0 6e 6f 74 20 66 69 74 20 69 6e 20 36 34 20 62 69 not fit in 64 bi
b4200 74 73 2e 20 20 53 6f 20 69 74 20 73 65 65 6d 73 ts. So it seems
b4210 20 73 61 66 65 72 20 74 6f 20 72 65 74 75 72 6e safer to return
b4220 0a 2a 2a 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 .** false..*/.SQ
b4230 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b4240 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 sqlite3FitsIn64
b4250 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 Bits(const char
b4260 2a 7a 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c *zNum, int negFl
b4270 61 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b ag){. int i, c;
b4280 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a . int neg = 0;.
b4290 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 if( *zNum=='-'
b42a0 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b ){. neg = 1;
b42b0 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d . zNum++;. }
b42c0 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d else if( *zNum==
b42d0 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b '+' ){. zNum+
b42e0 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 +;. }. if( neg
b42f0 46 6c 61 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e Flag ) neg = 1-n
b4300 65 67 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e eg;. while( *zN
b4310 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a um=='0' ){. z
b4320 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 Num++; /* Skip
b4330 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 leading zeros.
b4340 20 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f Ticket #2454 */
b4350 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
b4360 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 (c=zNum[i])>='0'
b4370 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 && c<='9'; i++)
b4380 7b 7d 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b {}. if( i<19 ){
b4390 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 . /* Guarante
b43a0 65 64 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73 ed to fit if les
b43b0 73 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 s than 19 digits
b43c0 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 */. return 1
b43d0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e ;. }else if( i>
b43e0 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 19 ){. /* Gua
b43f0 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f ranteed to be to
b4400 6f 20 62 69 67 20 69 66 20 67 72 65 61 74 65 72 o big if greater
b4410 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 than 19 digits
b4420 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b */. return 0;
b4430 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
b4440 20 43 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 Compare against
b4450 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 2^63. */. re
b4460 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 turn compare2pow
b4470 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 63(zNum)<neg;.
b4480 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e }.}../*.** If zN
b4490 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e um represents an
b44a0 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 integer that wi
b44b0 6c 6c 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 ll fit in 32-bit
b44c0 73 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a s, then set.** *
b44d0 70 56 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69 pValue to that i
b44e0 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 nteger and retur
b44f0 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 n true. Otherwi
b4500 73 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e se return false.
b4510 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e .**.** Any non-n
b4520 75 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72 umeric character
b4530 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 s that following
b4540 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 zNum are ignore
b4550 64 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 d..** This is di
b4560 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c fferent from sql
b4570 69 74 65 33 41 74 6f 69 36 34 28 29 20 77 68 69 ite3Atoi64() whi
b4580 63 68 20 72 65 71 75 69 72 65 73 20 74 68 65 0a ch requires the.
b4590 2a 2a 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 ** input number
b45a0 74 6f 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 to be zero-termi
b45b0 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nated..*/.SQLITE
b45c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b45d0 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e ite3GetInt32(con
b45e0 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 st char *zNum, i
b45f0 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 nt *pValue){. s
b4600 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 qlite_int64 v =
b4610 30 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 0;. int i, c;.
b4620 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 int neg = 0;.
b4630 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 if( zNum[0]=='-'
b4640 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b ){. neg = 1;
b4650 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d . zNum++;. }
b4660 65 6c 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d else if( zNum[0]
b4670 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 =='+' ){. zNu
b4680 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 m++;. }. while
b4690 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 ( zNum[0]=='0' )
b46a0 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 zNum++;. for(i
b46b0 3d 30 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d =0; i<11 && (c =
b46c0 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e zNum[i] - '0')>
b46d0 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 =0 && c<=9; i++)
b46e0 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b {. v = v*10 +
b46f0 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 c;. }.. /* Th
b4700 65 20 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 e longest decima
b4710 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e l representation
b4720 20 6f 66 20 61 20 33 32 20 62 69 74 20 69 6e 74 of a 32 bit int
b4730 65 67 65 72 20 69 73 20 31 30 20 64 69 67 69 74 eger is 10 digit
b4740 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 s:. **. **
b4750 20 20 20 20 20 20 20 20 20 31 32 33 34 35 36 37 1234567
b4760 38 39 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 890. ** 2^3
b4770 31 20 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a 1 -> 2147483648.
b4780 20 20 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 */. if( i>10
b4790 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
b47a0 0a 20 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 . }. if( v-neg
b47b0 3e 32 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20 >2147483647 ){.
b47c0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
b47d0 0a 20 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 . if( neg ){.
b47e0 20 20 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 v = -v;. }.
b47f0 2a 70 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 *pValue = (int)v
b4800 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a ;. return 1;.}.
b4810 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 ./*.** The varia
b4820 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
b4830 65 72 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 er encoding is a
b4840 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
b4850 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 KEY:.**
b4860 20 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 A = 0xxxxxxx
b4870 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 7 bits of data
b4880 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 and one flag bit
b4890 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 .** B =
b48a0 31 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69 1xxxxxxx 7 bi
b48b0 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f ts of data and o
b48c0 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 ne flag bit.**
b48d0 20 20 20 20 20 20 20 43 20 3d 20 78 78 78 78 78 C = xxxxx
b48e0 78 78 78 20 20 20 20 38 20 62 69 74 73 20 6f 66 xxx 8 bits of
b48f0 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 data.**.** 7 b
b4900 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 its - A.** 14 bi
b4910 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 ts - BA.** 21 bi
b4920 74 73 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 ts - BBA.** 28 b
b4930 69 74 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 its - BBBA.** 35
b4940 20 62 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a bits - BBBBA.**
b4950 20 34 32 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 bits - BBBBB
b4960 41 0a 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42 A.** 49 bits - B
b4970 42 42 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 BBBBBA.** 56 bit
b4980 73 20 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 s - BBBBBBBA.**
b4990 36 34 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 64 bits - BBBBBB
b49a0 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 BBC.*/../*.** Wr
b49b0 69 74 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 ite a 64-bit var
b49c0 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 iable-length int
b49d0 65 67 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 eger to memory s
b49e0 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e tarting at p[0].
b49f0 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f .** The length o
b4a00 66 20 64 61 74 61 20 77 72 69 74 65 20 77 69 6c f data write wil
b4a10 6c 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 l be between 1 a
b4a20 6e 64 20 39 20 62 79 74 65 73 2e 20 20 54 68 65 nd 9 bytes. The
b4a30 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 number.** of by
b4a40 74 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 tes written is r
b4a50 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 eturned..**.** A
b4a60 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 variable-length
b4a70 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 integer consist
b4a80 73 20 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37 s of the lower 7
b4a90 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 62 79 bits of each by
b4aa0 74 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 te.** for all by
b4ab0 74 65 73 20 74 68 61 74 20 68 61 76 65 20 74 68 tes that have th
b4ac0 65 20 38 74 68 20 62 69 74 20 73 65 74 20 61 6e e 8th bit set an
b4ad0 64 20 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20 d one byte with
b4ae0 74 68 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 the 8th.** bit c
b4af0 6c 65 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69 lear. Except, i
b4b00 66 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 f we get to the
b4b10 39 74 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f 9th byte, it sto
b4b20 72 65 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 res the full.**
b4b30 38 20 62 69 74 73 20 61 6e 64 20 69 73 20 74 68 8 bits and is th
b4b40 65 20 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a e last byte..*/.
b4b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b4b60 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 nt sqlite3PutVar
b4b70 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 int(unsigned cha
b4b80 72 20 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 r *p, u64 v){.
b4b90 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 int i, j, n;. u
b4ba0 38 20 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 8 buf[10];. if(
b4bb0 20 76 20 26 20 28 28 28 75 36 34 29 30 78 66 66 v & (((u64)0xff
b4bc0 30 30 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 000000)<<32) ){.
b4bd0 20 20 20 20 70 5b 38 5d 20 3d 20 76 3b 0a 20 20 p[8] = v;.
b4be0 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 v >>= 8;. f
b4bf0 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d or(i=7; i>=0; i-
b4c00 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20 3d -){. p[i] =
b4c10 20 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 (v & 0x7f) | 0x
b4c20 38 30 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 80;. v >>=
b4c30 37 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 7;. }. ret
b4c40 75 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 urn 9;. } .
b4c50 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 n = 0;. do{.
b4c60 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 76 20 buf[n++] = (v
b4c70 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 3b 0a & 0x7f) | 0x80;.
b4c80 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d v >>= 7;. }
b4c90 77 68 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 while( v!=0 );.
b4ca0 20 62 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b buf[0] &= 0x7f;
b4cb0 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 . assert( n<=9
b4cc0 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d );. for(i=0, j=
b4cd0 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 n-1; j>=0; j--,
b4ce0 69 2b 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d i++){. p[i] =
b4cf0 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 buf[j];. }. r
b4d00 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn n;.}../*.*
b4d10 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
b4d20 73 20 61 20 66 61 73 74 65 72 20 76 65 72 73 69 s a faster versi
b4d30 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 50 75 74 on of sqlite3Put
b4d40 56 61 72 69 6e 74 28 29 20 74 68 61 74 20 6f 6e Varint() that on
b4d50 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 ly.** works for
b4d60 33 32 2d 62 69 74 20 70 6f 73 69 74 69 76 65 20 32-bit positive
b4d70 69 6e 74 65 67 65 72 73 20 61 6e 64 20 77 68 69 integers and whi
b4d80 63 68 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 ch is optimized
b4d90 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f for.** the commo
b4da0 6e 20 63 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 n case of small
b4db0 69 6e 74 65 67 65 72 73 2e 20 20 41 20 4d 41 43 integers. A MAC
b4dc0 52 4f 20 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 RO version, putV
b4dd0 61 72 69 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 arint32,.** is p
b4de0 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e rovided which in
b4df0 6c 69 6e 65 73 20 74 68 65 20 73 69 6e 67 6c 65 lines the single
b4e00 2d 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c -byte case. All
b4e10 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 code should use
b4e20 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 76 65 .** the MACRO ve
b4e30 72 73 69 6f 6e 20 61 73 20 74 68 69 73 20 66 75 rsion as this fu
b4e40 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 nction assumes t
b4e50 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 he single-byte c
b4e60 61 73 65 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 ase has.** alrea
b4e70 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e dy been handled.
b4e80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b4e90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 TE int sqlite3Pu
b4ea0 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e tVarint32(unsign
b4eb0 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 ed char *p, u32
b4ec0 76 29 7b 0a 23 69 66 6e 64 65 66 20 70 75 74 56 v){.#ifndef putV
b4ed0 61 72 69 6e 74 33 32 0a 20 20 69 66 28 20 28 76 arint32. if( (v
b4ee0 20 26 20 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a & ~0x7f)==0 ){.
b4ef0 20 20 20 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 p[0] = v;.
b4f00 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
b4f10 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 76 20 #endif. if( (v
b4f20 26 20 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b & ~0x3fff)==0 ){
b4f30 0a 20 20 20 20 70 5b 30 5d 20 3d 20 28 76 3e 3e . p[0] = (v>>
b4f40 37 29 20 7c 20 30 78 38 30 3b 0a 20 20 20 20 70 7) | 0x80;. p
b4f50 5b 31 5d 20 3d 20 76 20 26 20 30 78 37 66 3b 0a [1] = v & 0x7f;.
b4f60 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 return 2;.
b4f70 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
b4f80 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76 e3PutVarint(p, v
b4f90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 );.}../*.** Read
b4fa0 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 a 64-bit variab
b4fb0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le-length intege
b4fc0 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 r from memory st
b4fd0 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a arting at p[0]..
b4fe0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
b4ff0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
b5000 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 ad. The value i
b5010 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a s stored in *v..
b5020 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b5030 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 E int sqlite3Get
b5040 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 Varint(const uns
b5050 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 igned char *p, u
b5060 36 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 64 *v){. u32 a,
b5070 62 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a b,s;.. a = *p;.
b5080 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 /* a: p0 (unma
b5090 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b50a0 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (a&0x80)). {.
b50b0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
b50c0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70 turn 1;. }.. p
b50d0 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 ++;. b = *p;.
b50e0 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b /* b: p1 (unmask
b50f0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 ed) */. if (!(b
b5100 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 &0x80)). {.
b5110 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 a &= 0x7f;. a
b5120 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = a<<7;. a |
b5130 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b = b;. *v = a;
b5140 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 . return 2;.
b5150 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d }.. p++;. a =
b5160 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a a<<14;. a |= *
b5170 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 p;. /* a: p0<<1
b5180 34 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 4 | p2 (unmasked
b5190 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 ) */. if (!(a&0
b51a0 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 x80)). {. a
b51b0 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 &= (0x7f<<14)|(0
b51c0 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 x7f);. b &= 0
b51d0 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c x7f;. b = b<<
b51e0 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 7;. a |= b;.
b51f0 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 *v = a;. r
b5200 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 eturn 3;. }..
b5210 2f 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c /* CSE1 from bel
b5220 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 ow */. a &= (0x
b5230 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b5240 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c p++;. b = b<<
b5250 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 14;. b |= *p;.
b5260 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 /* b: p1<<14 |
b5270 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p3 (unmasked) */
b5280 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 . if (!(b&0x80)
b5290 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 ). {. b &= (
b52a0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b52b0 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 ;. /* moved C
b52c0 53 45 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a SE1 up */. /*
b52d0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b52e0 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 |(0x7f); */.
b52f0 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 a = a<<7;. a
b5300 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 |= b;. *v = a
b5310 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a ;. return 4;.
b5320 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c }.. /* a: p0<
b5330 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 <14 | p2 (masked
b5340 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c ) */. /* b: p1<
b5350 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b <14 | p3 (unmask
b5360 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61 ed) */. /* 1:sa
b5370 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 ve off p0<<21 |
b5380 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c p1<<14 | p2<<7 |
b5390 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a p3 (masked) */.
b53a0 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 /* moved CSE1
b53b0 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 up */. /* a &=
b53c0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b53d0 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 ); */. b &= (0x
b53e0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b53f0 20 20 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a s = a;. /* s:
b5400 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 p0<<14 | p2 (ma
b5410 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b sked) */.. p++;
b5420 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 . a = a<<14;.
b5430 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a |= *p;. /* a:
b5440 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 p0<<28 | p2<<14
b5450 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 | p4 (unmasked)
b5460 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
b5470 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 80)). {. /*
b5480 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73 we can skip thes
b5490 65 20 63 61 75 73 65 20 74 68 65 79 20 77 65 72 e cause they wer
b54a0 65 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 e (effectively)
b54b0 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 done above in ca
b54c0 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 lc'ing s */.
b54d0 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 /* a &= (0x7f<<2
b54e0 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
b54f0 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20 x7f); */. /*
b5500 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c b &= (0x7f<<14)|
b5510 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62 (0x7f); */. b
b5520 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = b<<7;. a |
b5530 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e = b;. s = s>>
b5540 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 18;. *v = ((u
b5550 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 64)s)<<32 | a;.
b5560 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d return 5;. }
b5570 0a 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 .. /* 2:save of
b5580 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 f p0<<21 | p1<<1
b5590 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 4 | p2<<7 | p3 (
b55a0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d masked) */. s =
b55b0 20 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b s<<7;. s |= b;
b55c0 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20 . /* s: p0<<21
b55d0 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 | p1<<14 | p2<<7
b55e0 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a | p3 (masked) *
b55f0 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 /.. p++;. b =
b5600 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 b<<14;. b |= *p
b5610 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38 ;. /* b: p1<<28
b5620 20 7c 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28 | p3<<14 | p5 (
b5630 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
b5640 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 f (!(b&0x80)).
b5650 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 {. /* we can
b5660 73 6b 69 70 20 74 68 69 73 20 63 61 75 73 65 20 skip this cause
b5670 69 74 20 77 61 73 20 28 65 66 66 65 63 74 69 76 it was (effectiv
b5680 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 ely) done above
b5690 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f in calc'ing s */
b56a0 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 . /* b &= (0x
b56b0 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 7f<<28)|(0x7f<<1
b56c0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 4)|(0x7f); */.
b56d0 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 a &= (0x7f<<14
b56e0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 )|(0x7f);. a
b56f0 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = a<<7;. a |=
b5700 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 b;. s = s>>1
b5710 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 8;. *v = ((u6
b5720 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 4)s)<<32 | a;.
b5730 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a return 6;. }.
b5740 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c . p++;. a = a<
b5750 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a <14;. a |= *p;.
b5760 20 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c /* a: p2<<28 |
b5770 20 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e p4<<14 | p6 (un
b5780 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 masked) */. if
b5790 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(a&0x80)). {.
b57a0 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c a &= (0x7f<<
b57b0 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 28)|(0x7f<<14)|(
b57c0 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 0x7f);. b &=
b57d0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b57e0 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b );. b = b<<7;
b57f0 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
b5800 20 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20 s = s>>11;.
b5810 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 *v = ((u64)s)<<3
b5820 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 2 | a;. retur
b5830 6e 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 n 7;. }.. /* C
b5840 53 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a SE2 from below *
b5850 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c /. a &= (0x7f<<
b5860 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 14)|(0x7f);. p+
b5870 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a +;. b = b<<14;.
b5880 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 b |= *p;. /*
b5890 62 3a 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c b: p3<<28 | p5<<
b58a0 31 34 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 14 | p7 (unmaske
b58b0 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 d) */. if (!(b&
b58c0 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 0x80)). {. b
b58d0 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 &= (0x7f<<28)|(
b58e0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b58f0 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 ;. /* moved C
b5900 53 45 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a SE2 up */. /*
b5910 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b5920 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 |(0x7f); */.
b5930 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 a = a<<7;. a
b5940 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e |= b;. s = s>
b5950 3e 34 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 >4;. *v = ((u
b5960 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 64)s)<<32 | a;.
b5970 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d return 8;. }
b5980 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 .. p++;. a = a
b5990 3c 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b <<15;. a |= *p;
b59a0 0a 20 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20 . /* a: p4<<29
b59b0 7c 20 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75 | p6<<15 | p8 (u
b59c0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f nmasked) */.. /
b59d0 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 * moved CSE2 up
b59e0 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 */. /* a &= (0x
b59f0 37 66 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31 7f<<29)|(0x7f<<1
b5a00 35 29 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20 5)|(0xff); */.
b5a10 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c b &= (0x7f<<14)|
b5a20 28 30 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c (0x7f);. b = b<
b5a30 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20 <8;. a |= b;..
b5a40 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d s = s<<4;. b =
b5a50 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30 p[-4];. b &= 0
b5a60 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b x7f;. b = b>>3;
b5a70 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76 . s |= b;.. *v
b5a80 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 = ((u64)s)<<32
b5a90 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39 | a;.. return 9
b5aa0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
b5ab0 61 20 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c a 32-bit variabl
b5ac0 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
b5ad0 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 from memory sta
b5ae0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a rting at p[0]..*
b5af0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
b5b00 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 ber of bytes rea
b5b10 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 d. The value is
b5b20 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a stored in *v..*
b5b30 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f * A MACRO versio
b5b40 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32 2c 20 n, getVarint32,
b5b50 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 is provided whic
b5b60 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a h inlines the .*
b5b70 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 * single-byte ca
b5b80 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 se. All code sh
b5b90 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 ould use the MAC
b5ba0 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a RO version as .*
b5bb0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
b5bc0 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 assumes the sing
b5bd0 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 le-byte case has
b5be0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 already been ha
b5bf0 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ndled..*/.SQLITE
b5c00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b5c10 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 ite3GetVarint32(
b5c20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
b5c30 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b har *p, u32 *v){
b5c40 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 61 . u32 a,b;.. a
b5c50 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 = *p;. /* a: p
b5c60 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 0 (unmasked) */.
b5c70 23 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e #ifndef getVarin
b5c80 74 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78 t32. if (!(a&0x
b5c90 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 80)). {. *v
b5ca0 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = a;. return
b5cb0 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 1;. }.#endif..
b5cc0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a p++;. b = *p;.
b5cd0 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 /* b: p1 (unma
b5ce0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b5cf0 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (b&0x80)). {.
b5d00 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 a &= 0x7f;.
b5d10 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a a = a<<7;. *
b5d20 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 v = a | b;. r
b5d30 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 eturn 2;. }..
b5d40 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 p++;. a = a<<14
b5d50 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. a |= *p;. /
b5d60 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 * a: p0<<14 | p2
b5d70 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b5d80 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a if (!(a&0x80)).
b5d90 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 {. a &= (0x
b5da0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b5db0 20 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 b &= 0x7f;.
b5dc0 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 b = b<<7;.
b5dd0 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 *v = a | b;.
b5de0 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a return 3;. }..
b5df0 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c p++;. b = b<<
b5e00 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 14;. b |= *p;.
b5e10 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 /* b: p1<<14 |
b5e20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p3 (unmasked) */
b5e30 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 . if (!(b&0x80)
b5e40 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 ). {. b &= (
b5e50 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b5e60 3b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 ;. a &= (0x7f
b5e70 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b5e80 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 a = a<<7;.
b5e90 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 *v = a | b;.
b5ea0 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 return 4;. }..
b5eb0 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 p++;. a = a<<1
b5ec0 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 4;. a |= *p;.
b5ed0 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 /* a: p0<<28 | p
b5ee0 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 2<<14 | p4 (unma
b5ef0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b5f00 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (a&0x80)). {.
b5f10 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 a &= (0x7f<<28
b5f20 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 )|(0x7f<<14)|(0x
b5f30 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 7f);. b &= (0
b5f40 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c x7f<<28)|(0x7f<<
b5f50 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
b5f60 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 b = b<<7;. *v
b5f70 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 = a | b;. re
b5f80 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f turn 5;. }.. /
b5f90 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65 * We can only re
b5fa0 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 20 77 ach this point w
b5fb0 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20 63 6f hen reading a co
b5fc0 72 72 75 70 74 20 64 61 74 61 62 61 73 65 0a 20 rrupt database.
b5fd0 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68 ** file. In th
b5fe0 61 74 20 63 61 73 65 20 77 65 20 61 72 65 20 6e at case we are n
b5ff0 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e ot in any hurry.
b6000 20 20 55 73 65 20 74 68 65 20 28 72 65 6c 61 74 Use the (relat
b6010 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 ively. ** slow)
b6020 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 general-purpose
b6030 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
b6040 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65 t() routine to e
b6050 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a 2a 20 xtract the. **
b6060 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 value. */. {.
b6070 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20 69 u64 v64;. i
b6080 6e 74 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 nt n;.. p -=
b6090 34 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 4;. n = sqlit
b60a0 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 e3GetVarint(p, &
b60b0 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 v64);. assert
b60c0 28 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b ( n>5 && n<=9 );
b60d0 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 . *v = (u32)v
b60e0 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 64;. return n
b60f0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
b6100 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
b6110 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 77 of bytes that w
b6120 69 6c 6c 20 62 65 20 6e 65 65 64 65 64 20 74 6f ill be needed to
b6130 20 73 74 6f 72 65 20 74 68 65 20 67 69 76 65 6e store the given
b6140 0a 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74 65 67 .** 64-bit integ
b6150 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
b6160 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b6170 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 3VarintLen(u64 v
b6180 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a ){. int i = 0;.
b6190 20 20 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 do{. i++;.
b61a0 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 v >>= 7;. }w
b61b0 68 69 6c 65 28 20 76 21 3d 30 20 26 26 20 69 3c hile( v!=0 && i<
b61c0 39 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 9 );. return i;
b61d0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 .}.../*.** Read
b61e0 6f 72 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d or write a four-
b61f0 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
b6200 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a integer value..*
b6210 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b6220 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 u32 sqlite3Get4
b6230 62 79 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 byte(const u8 *p
b6240 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 ){. return (p[0
b6250 5d 3c 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c ]<<24) | (p[1]<<
b6260 31 36 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 16) | (p[2]<<8)
b6270 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 | p[3];.}.SQLITE
b6280 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
b6290 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e lite3Put4byte(un
b62a0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 signed char *p,
b62b0 75 33 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d u32 v){. p[0] =
b62c0 20 76 3e 3e 32 34 3b 0a 20 20 70 5b 31 5d 20 3d v>>24;. p[1] =
b62d0 20 76 3e 3e 31 36 3b 0a 20 20 70 5b 32 5d 20 3d v>>16;. p[2] =
b62e0 20 76 3e 3e 38 3b 0a 20 20 70 5b 33 5d 20 3d 20 v>>8;. p[3] =
b62f0 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 21 64 65 66 v;.}....#if !def
b6300 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
b6310 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c _BLOB_LITERAL) |
b6320 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
b6330 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a _HAS_CODEC)./*.*
b6340 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69 * Translate a si
b6350 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 48 65 78 ngle byte of Hex
b6360 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 into an integer
b6370 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
b6380 65 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 en only works if
b6390 20 68 20 72 65 61 6c 6c 79 20 69 73 20 61 20 76 h really is a v
b63a0 61 6c 69 64 20 68 65 78 61 64 65 63 69 6d 61 6c alid hexadecimal
b63b0 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 3a 20 20 .** character:
b63c0 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 0..9a..fA..F.*/.
b63d0 73 74 61 74 69 63 20 69 6e 74 20 68 65 78 54 6f static int hexTo
b63e0 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20 20 61 73 Int(int h){. as
b63f0 73 65 72 74 28 20 28 68 3e 3d 27 30 27 20 26 26 sert( (h>='0' &&
b6400 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20 28 68 3e h<='9') || (h>
b6410 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 27 29 20 ='a' && h<='f')
b6420 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26 26 20 68 || (h>='A' && h
b6430 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66 64 65 66 <='F') );.#ifdef
b6440 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 SQLITE_ASCII.
b6450 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e 3e 36 29 h += 9*(1&(h>>6)
b6460 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 );.#endif.#ifdef
b6470 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 SQLITE_EBCDIC.
b6480 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28 68 3e 3e h += 9*(1&~(h>>
b6490 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 4));.#endif. re
b64a0 74 75 72 6e 20 68 20 26 20 30 78 66 3b 0a 7d 0a turn h & 0xf;.}.
b64b0 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 #endif /* !SQLIT
b64c0 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
b64d0 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 RAL || SQLITE_HA
b64e0 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 20 S_CODEC */..#if
b64f0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
b6500 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
b6510 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 L) || defined(SQ
b6520 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a LITE_HAS_CODEC).
b6530 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
b6540 42 4c 4f 42 20 6c 69 74 65 72 61 6c 20 6f 66 20 BLOB literal of
b6550 74 68 65 20 66 6f 72 6d 20 22 78 27 68 68 68 68 the form "x'hhhh
b6560 68 68 27 22 20 69 6e 74 6f 20 69 74 73 20 62 69 hh'" into its bi
b6570 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 nary.** value.
b6580 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
b6590 20 74 6f 20 69 74 73 20 62 69 6e 61 72 79 20 76 to its binary v
b65a0 61 6c 75 65 2e 20 20 53 70 61 63 65 20 74 6f 20 alue. Space to
b65b0 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 62 69 6e 61 hold the.** bina
b65c0 72 79 20 76 61 6c 75 65 20 68 61 73 20 62 65 65 ry value has bee
b65d0 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 n obtained from
b65e0 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 malloc and must
b65f0 62 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 74 be freed by.** t
b6600 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 he calling routi
b6610 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ne..*/.SQLITE_PR
b6620 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
b6630 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c te3HexToBlob(sql
b6640 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
b6650 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b char *z, int n){
b6660 0a 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a . char *zBlob;.
b6670 20 20 69 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c 6f int i;.. zBlo
b6680 62 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 b = (char *)sqli
b6690 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
b66a0 62 2c 20 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 6e b, n/2 + 1);. n
b66b0 2d 2d 3b 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20 --;. if( zBlob
b66c0 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ){. for(i=0;
b66d0 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 i<n; i+=2){.
b66e0 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 28 zBlob[i/2] = (
b66f0 68 65 78 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c 3c hexToInt(z[i])<<
b6700 34 29 20 7c 20 68 65 78 54 6f 49 6e 74 28 7a 5b 4) | hexToInt(z[
b6710 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 i+1]);. }.
b6720 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 3b zBlob[i/2] = 0;
b6730 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42 . }. return zB
b6740 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a lob;.}.#endif /*
b6750 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c !SQLITE_OMIT_BL
b6760 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 OB_LITERAL || SQ
b6770 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a LITE_HAS_CODEC *
b6780 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 /.../*.** Change
b6790 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 the sqlite.magi
b67a0 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 c from SQLITE_MA
b67b0 47 49 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49 GIC_OPEN to SQLI
b67c0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a TE_MAGIC_BUSY..*
b67d0 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f * Return an erro
b67e0 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 r (non-zero) if
b67f0 74 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f the magic was no
b6800 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f t SQLITE_MAGIC_O
b6810 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 PEN.** when this
b6820 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
b6830 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
b6840 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
b6850 20 77 68 65 6e 20 65 6e 74 65 72 69 6e 67 20 61 when entering a
b6860 6e 20 53 51 4c 69 74 65 20 41 50 49 2e 20 20 54 n SQLite API. T
b6870 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f he SQLITE_MAGIC_
b6880 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e OPEN.** value in
b6890 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65 dicates that the
b68a0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
b68b0 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f tion passed into
b68c0 20 74 68 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f the API is.** o
b68d0 70 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 62 pen and is not b
b68e0 65 69 6e 67 20 75 73 65 64 20 62 79 20 61 6e 6f eing used by ano
b68f0 74 68 65 72 20 74 68 72 65 61 64 2e 20 20 42 79 ther thread. By
b6900 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61 changing the va
b6910 6c 75 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45 lue.** to SQLITE
b6920 5f 4d 41 47 49 43 5f 42 55 53 59 20 77 65 20 69 _MAGIC_BUSY we i
b6930 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 ndicate that the
b6940 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 connection is i
b6950 6e 20 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 n use..** sqlite
b6960 33 53 61 66 65 74 79 4f 66 66 28 29 20 62 65 6c 3SafetyOff() bel
b6970 6f 77 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 ow will change t
b6980 68 65 20 76 61 6c 75 65 20 62 61 63 6b 20 74 6f he value back to
b6990 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 SQLITE_MAGIC_OP
b69a0 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 EN.** when the A
b69b0 50 49 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a PI exits. .**.**
b69c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
b69d0 20 61 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 a attempt to de
b69e0 74 65 63 74 20 69 66 20 74 77 6f 20 74 68 72 65 tect if two thre
b69f0 61 64 73 20 75 73 65 20 74 68 65 0a 2a 2a 20 73 ads use the.** s
b6a00 61 6d 65 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e ame sqlite* poin
b6a10 74 65 72 20 61 74 20 74 68 65 20 73 61 6d 65 20 ter at the same
b6a20 74 69 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 time. There is
b6a30 61 20 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69 a race .** condi
b6a40 74 69 6f 6e 20 73 6f 20 69 74 20 69 73 20 70 6f tion so it is po
b6a50 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 ssible that the
b6a60 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 64 65 74 error is not det
b6a70 65 63 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73 ected..** But us
b6a80 75 61 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c 65 ually the proble
b6a90 6d 20 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20 m will be seen.
b6aa0 20 54 68 65 20 72 65 73 75 6c 74 20 77 69 6c 6c The result will
b6ab0 20 62 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 be an.** error
b6ac0 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 which can be use
b6ad0 64 20 74 6f 20 64 65 62 75 67 20 74 68 65 20 61 d to debug the a
b6ae0 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 pplication that
b6af0 69 73 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69 is.** using SQLi
b6b00 74 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a te incorrectly..
b6b10 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 30 **.** Ticket #20
b6b20 32 3a 20 20 49 66 20 64 62 2d 3e 6d 61 67 69 63 2: If db->magic
b6b30 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 is not a valid
b6b40 6f 70 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65 open value, take
b6b50 20 63 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20 care not.** to
b6b60 6d 6f 64 69 66 79 20 74 68 65 20 64 62 20 73 74 modify the db st
b6b70 72 75 63 74 75 72 65 20 61 74 20 61 6c 6c 2e 20 ructure at all.
b6b80 20 49 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 It could be tha
b6b90 74 20 64 62 20 69 73 20 61 20 73 74 61 6c 65 0a t db is a stale.
b6ba0 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 ** pointer. In
b6bb0 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74 20 other words, it
b6bc0 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74 68 could be that th
b6bd0 65 72 65 20 68 61 73 20 62 65 65 6e 20 61 20 70 ere has been a p
b6be0 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 rior.** call to
b6bf0 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 sqlite3_close(db
b6c00 29 20 61 6e 64 20 64 62 20 68 61 73 20 62 65 65 ) and db has bee
b6c10 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 n deallocated.
b6c20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74 And we do.** not
b6c30 20 77 61 6e 74 20 74 6f 20 77 72 69 74 65 20 69 want to write i
b6c40 6e 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 nto deallocated
b6c50 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 memory..*/.#ifde
b6c60 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 f SQLITE_DEBUG.S
b6c70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b6c80 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f t sqlite3SafetyO
b6c90 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a n(sqlite3 *db){.
b6ca0 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d if( db->magic=
b6cb0 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 =SQLITE_MAGIC_OP
b6cc0 45 4e 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 EN ){. db->ma
b6cd0 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 gic = SQLITE_MAG
b6ce0 49 43 5f 42 55 53 59 3b 0a 20 20 20 20 61 73 73 IC_BUSY;. ass
b6cf0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
b6d00 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
b6d10 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e x) );. return
b6d20 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
b6d30 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 db->magic==SQLIT
b6d40 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a E_MAGIC_BUSY ){.
b6d50 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 db->magic =
b6d60 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 SQLITE_MAGIC_ERR
b6d70 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 OR;. db->u1.i
b6d80 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 sInterrupted = 1
b6d90 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 ;. }. return 1
b6da0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
b6db0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6d 61 67 * Change the mag
b6dc0 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d ic from SQLITE_M
b6dd0 41 47 49 43 5f 42 55 53 59 20 74 6f 20 53 51 4c AGIC_BUSY to SQL
b6de0 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e 0a ITE_MAGIC_OPEN..
b6df0 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 ** Return an err
b6e00 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 or (non-zero) if
b6e10 20 74 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e the magic was n
b6e20 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f ot SQLITE_MAGIC_
b6e30 42 55 53 59 0a 2a 2a 20 77 68 65 6e 20 74 68 69 BUSY.** when thi
b6e40 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
b6e50 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 led..*/.#ifdef S
b6e60 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
b6e70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b6e80 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
b6e90 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
b6ea0 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 if( db->magic==S
b6eb0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 QLITE_MAGIC_BUSY
b6ec0 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 ){. db->magi
b6ed0 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 c = SQLITE_MAGIC
b6ee0 5f 4f 50 45 4e 3b 0a 20 20 20 20 61 73 73 65 72 _OPEN;. asser
b6ef0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
b6f00 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
b6f10 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 );. return 0
b6f20 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 ;. }else{. d
b6f30 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 b->magic = SQLIT
b6f40 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 E_MAGIC_ERROR;.
b6f50 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 db->u1.isInte
b6f60 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 20 rrupted = 1;.
b6f70 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d return 1;. }.}
b6f80 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
b6f90 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 heck to make sur
b6fa0 65 20 77 65 20 68 61 76 65 20 61 20 76 61 6c 69 e we have a vali
b6fb0 64 20 64 62 20 70 6f 69 6e 74 65 72 2e 20 20 54 d db pointer. T
b6fc0 68 69 73 20 74 65 73 74 20 69 73 20 6e 6f 74 0a his test is not.
b6fd0 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 74 ** foolproof but
b6fe0 20 69 74 20 64 6f 65 73 20 70 72 6f 76 69 64 65 it does provide
b6ff0 20 73 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f 66 some measure of
b7000 20 70 72 6f 74 65 63 74 69 6f 6e 20 61 67 61 69 protection agai
b7010 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f 66 nst.** misuse of
b7020 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 73 the interface s
b7030 75 63 68 20 61 73 20 70 61 73 73 69 6e 67 20 69 uch as passing i
b7040 6e 20 64 62 20 70 6f 69 6e 74 65 72 73 20 74 68 n db pointers th
b7050 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 6f at are.** NULL o
b7060 72 20 77 68 69 63 68 20 68 61 76 65 20 62 65 65 r which have bee
b7070 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f n previously clo
b7080 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 72 6f sed. If this ro
b7090 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a utine returns.**
b70a0 20 31 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 1 it means that
b70b0 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 the db pointer
b70c0 69 73 20 76 61 6c 69 64 20 61 6e 64 20 30 20 69 is valid and 0 i
b70d0 66 20 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 f it should not
b70e0 62 65 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e 63 be.** dereferenc
b70f0 65 64 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f ed for any reaso
b7100 6e 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 n. The calling
b7110 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
b7120 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45 invoke.** SQLITE
b7130 5f 4d 49 53 55 53 45 20 69 6d 6d 65 64 69 61 74 _MISUSE immediat
b7140 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 ely..**.** sqlit
b7150 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 e3SafetyCheckOk(
b7160 29 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20 ) requires that
b7170 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 62 the db pointer b
b7180 65 20 76 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 75 e valid for.** u
b7190 73 65 2e 20 20 73 71 6c 69 74 65 33 53 61 66 65 se. sqlite3Safe
b71a0 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 tyCheckSickOrOk(
b71b0 29 20 61 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f ) allows a db po
b71c0 69 6e 74 65 72 20 74 68 61 74 20 66 61 69 6c 65 inter that faile
b71d0 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f d to.** open pro
b71e0 70 65 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74 perly and is not
b71f0 20 66 69 74 20 66 6f 72 20 67 65 6e 65 72 61 6c fit for general
b7200 20 75 73 65 20 62 75 74 20 77 68 69 63 68 20 63 use but which c
b7210 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20 61 73 an be.** used as
b7220 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 an argument to
b7230 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
b7240 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 or sqlite3_clos
b7250 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 e()..*/.SQLITE_P
b7260 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b7270 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 e3SafetyCheckOk(
b7280 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
b7290 69 6e 74 20 6d 61 67 69 63 3b 0a 20 20 69 66 28 int magic;. if(
b72a0 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 db==0 ) return
b72b0 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 0;. magic = db-
b72c0 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 >magic;. if( ma
b72d0 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 gic!=SQLITE_MAGI
b72e0 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 20 C_OPEN &&.
b72f0 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 magic!=SQLITE_MA
b7300 47 49 43 5f 42 55 53 59 20 29 20 72 65 74 75 72 GIC_BUSY ) retur
b7310 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b n 0;. return 1;
b7320 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
b7330 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
b7340 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b etyCheckSickOrOk
b7350 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
b7360 20 69 6e 74 20 6d 61 67 69 63 3b 0a 20 20 69 66 int magic;. if
b7370 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72 6e ( db==0 ) return
b7380 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 0;. magic = db
b7390 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 6d ->magic;. if( m
b73a0 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 agic!=SQLITE_MAG
b73b0 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 20 20 IC_SICK &&.
b73c0 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d magic!=SQLITE_M
b73d0 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20 AGIC_OPEN &&.
b73e0 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 magic!=SQLITE
b73f0 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72 65 _MAGIC_BUSY ) re
b7400 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e turn 0;. return
b7410 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 1;.}../********
b7420 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 ****** End of ut
b7430 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a il.c ***********
b7440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7460 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
b7470 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
b7480 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a e hash.c *******
b7490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b74a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b74b0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
b74c0 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 1 September 22.*
b74d0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
b74e0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
b74f0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
b7500 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
b7510 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
b7520 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
b7530 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
b7540 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
b7550 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
b7560 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
b7570 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
b7580 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
b7590 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
b75a0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
b75b0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
b75c0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
b75d0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
b75e0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
b75f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
b7630 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70 This is the imp
b7640 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67 lementation of g
b7650 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c eneric hash-tabl
b7660 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 es.** used in SQ
b7670 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a Lite..**.** $Id:
b7680 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 31 20 32 hash.c,v 1.31 2
b7690 30 30 38 2f 31 30 2f 31 30 20 31 37 3a 34 31 3a 008/10/10 17:41:
b76a0 32 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 29 drh Exp $.*/.
b76b0 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 ./* Turn bulk me
b76c0 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68 mory into a hash
b76d0 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 table object by
b76e0 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
b76f0 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 e.** fields of t
b7700 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72 he Hash structur
b7710 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 e..**.** "pNew"
b7720 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
b7730 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 the hash table t
b7740 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69 hat is to be ini
b7750 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 22 63 6f tialized..** "co
b7760 70 79 4b 65 79 22 20 69 73 20 74 72 75 65 20 69 pyKey" is true i
b7770 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 f the hash table
b7780 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 should make its
b7790 20 6f 77 6e 20 70 72 69 76 61 74 65 0a 2a 2a 20 own private.**
b77a0 63 6f 70 79 20 6f 66 20 6b 65 79 73 20 61 6e 64 copy of keys and
b77b0 20 66 61 6c 73 65 20 69 66 20 69 74 20 73 68 6f false if it sho
b77c0 75 6c 64 20 6a 75 73 74 20 75 73 65 20 74 68 65 uld just use the
b77d0 20 73 75 70 70 6c 69 65 64 20 70 6f 69 6e 74 65 supplied pointe
b77e0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
b77f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
b7800 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 20 2a 3HashInit(Hash *
b7810 70 4e 65 77 2c 20 69 6e 74 20 63 6f 70 79 4b 65 pNew, int copyKe
b7820 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e y){. assert( pN
b7830 65 77 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d ew!=0 );. pNew-
b7840 3e 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b >copyKey = copyK
b7850 65 79 21 3d 30 3b 0a 20 20 70 4e 65 77 2d 3e 66 ey!=0;. pNew->f
b7860 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 irst = 0;. pNew
b7870 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 ->count = 0;. p
b7880 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b New->htsize = 0;
b7890 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b . pNew->ht = 0;
b78a0 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c .}../* Remove al
b78b0 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 l entries from a
b78c0 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65 hash table. Re
b78d0 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 claim all memory
b78e0 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 ..** Call this r
b78f0 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 outine to delete
b7900 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72 a hash table or
b7910 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68 to reset a hash
b7920 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 table.** to the
b7930 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f empty state..*/
b7940 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b7950 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 void sqlite3Hash
b7960 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b Clear(Hash *pH){
b7970 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 . HashElem *ele
b7980 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f m; /* Fo
b7990 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 r looping over a
b79a0 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 ll elements of t
b79b0 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 he table */.. a
b79c0 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a ssert( pH!=0 );.
b79d0 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 elem = pH->fir
b79e0 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 st;. pH->first
b79f0 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 = 0;. sqlite3_f
b7a00 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 ree(pH->ht);. p
b7a10 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d H->ht = 0;. pH-
b7a20 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 >htsize = 0;. w
b7a30 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 hile( elem ){.
b7a40 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 HashElem *next
b7a50 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 _elem = elem->ne
b7a60 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e xt;. if( pH->
b7a70 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d copyKey && elem-
b7a80 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73 >pKey ){. s
b7a90 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d qlite3_free(elem
b7aa0 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20 ->pKey);. }.
b7ab0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
b7ac0 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20 elem);. elem
b7ad0 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d = next_elem;. }
b7ae0 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 . pH->count = 0
b7af0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 ;.}../*.** Hash
b7b00 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 and comparison f
b7b10 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 unctions when th
b7b20 65 20 6d 6f 64 65 20 69 73 20 53 51 4c 49 54 45 e mode is SQLITE
b7b30 5f 48 41 53 48 5f 53 54 52 49 4e 47 0a 2a 2f 0a _HASH_STRING.*/.
b7b40 73 74 61 74 69 63 20 69 6e 74 20 73 74 72 48 61 static int strHa
b7b50 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 sh(const void *p
b7b60 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a Key, int nKey){.
b7b70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 const char *z
b7b80 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 = (const char *)
b7b90 70 4b 65 79 3b 0a 20 20 69 6e 74 20 68 20 3d 20 pKey;. int h =
b7ba0 30 3b 0a 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30 0;. if( nKey<=0
b7bb0 20 29 20 6e 4b 65 79 20 3d 20 73 74 72 6c 65 6e ) nKey = strlen
b7bc0 28 7a 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b (z);. while( nK
b7bd0 65 79 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 ey > 0 ){. h
b7be0 20 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 = (h<<3) ^ h ^
b7bf0 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
b7c00 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 wer[(unsigned ch
b7c10 61 72 29 2a 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b ar)*z++];. nK
b7c20 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ey--;. }. retu
b7c30 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66 rn h & 0x7ffffff
b7c40 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 f;.}.static int
b7c50 73 74 72 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 strCompare(const
b7c60 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e void *pKey1, in
b7c70 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 t n1, const void
b7c80 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29 *pKey2, int n2)
b7c90 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29 {. if( n1!=n2 )
b7ca0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 return 1;. ret
b7cb0 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 4e 49 urn sqlite3StrNI
b7cc0 43 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72 2a Cmp((const char*
b7cd0 29 70 4b 65 79 31 2c 28 63 6f 6e 73 74 20 63 68 )pKey1,(const ch
b7ce0 61 72 2a 29 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d ar*)pKey2,n1);.}
b7cf0 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c .../* Link an el
b7d00 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 ement into the h
b7d10 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 ash table.*/.sta
b7d20 74 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45 tic void insertE
b7d30 6c 65 6d 65 6e 74 28 0a 20 20 48 61 73 68 20 2a lement(. Hash *
b7d40 70 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 pH,
b7d50 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 /* The complete
b7d60 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 hash table */.
b7d70 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e struct _ht *pEn
b7d80 74 72 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 65 try, /* The e
b7d90 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20 ntry into which
b7da0 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64 pNew is inserted
b7db0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
b7dc0 70 4e 65 77 20 20 20 20 20 20 20 20 20 2f 2a 20 pNew /*
b7dd0 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 The element to b
b7de0 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b e inserted */.){
b7df0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 . HashElem *pHe
b7e00 61 64 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 ad; /* Fir
b7e10 73 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 st element alrea
b7e20 64 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a dy in pEntry */.
b7e30 20 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 pHead = pEntry
b7e40 2d 3e 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 ->chain;. if( p
b7e50 48 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 Head ){. pNew
b7e60 2d 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a ->next = pHead;.
b7e70 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d pNew->prev =
b7e80 20 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 pHead->prev;.
b7e90 20 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 if( pHead->pre
b7ea0 76 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 v ){ pHead->prev
b7eb0 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d ->next = pNew; }
b7ec0 0a 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 . else
b7ed0 20 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 { pH->firs
b7ee0 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 t = pNew; }.
b7ef0 70 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e pHead->prev = pN
b7f00 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ew;. }else{.
b7f10 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 pNew->next = pH
b7f20 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 ->first;. if(
b7f30 20 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 pH->first ){ pH
b7f40 2d 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 ->first->prev =
b7f50 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 pNew; }. pNew
b7f60 2d 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 ->prev = 0;.
b7f70 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 pH->first = pNew
b7f80 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e ;. }. pEntry->
b7f90 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 count++;. pEntr
b7fa0 79 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b y->chain = pNew;
b7fb0 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 .}.../* Resize t
b7fc0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f he hash table so
b7fd0 20 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e that it cantain
b7fe0 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 s "new_size" buc
b7ff0 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 kets..** "new_si
b8000 7a 65 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f ze" must be a po
b8010 77 65 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68 wer of 2. The h
b8020 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 ash table might
b8030 66 61 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 fail .** to resi
b8040 7a 65 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 ze if sqlite3_ma
b8050 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f lloc() fails..*/
b8060 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 68 .static void reh
b8070 61 73 68 28 48 61 73 68 20 2a 70 48 2c 20 69 6e ash(Hash *pH, in
b8080 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 t new_size){. s
b8090 74 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 truct _ht *new_h
b80a0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
b80b0 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 The new hash ta
b80c0 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 ble */. HashEle
b80d0 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 m *elem, *next_e
b80e0 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c lem; /* For l
b80f0 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 ooping over exis
b8100 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f ting elements */
b8110 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
b8120 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 MALLOC_SOFT_LIMI
b8130 54 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 T. if( new_size
b8140 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f *sizeof(struct _
b8150 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f ht)>SQLITE_MALLO
b8160 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a C_SOFT_LIMIT ){.
b8170 20 20 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 new_size = S
b8180 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 QLITE_MALLOC_SOF
b8190 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 T_LIMIT/sizeof(s
b81a0 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a truct _ht);. }.
b81b0 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d if( new_size==
b81c0 70 48 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74 pH->htsize ) ret
b81d0 75 72 6e 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f urn;.#endif.. /
b81e0 2a 20 54 68 65 72 65 20 69 73 20 61 20 63 61 6c * There is a cal
b81f0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 61 6c l to sqlite3_mal
b8200 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 72 65 68 loc() inside reh
b8210 61 73 68 28 29 2e 20 49 66 20 74 68 65 72 65 20 ash(). If there
b8220 69 73 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 is. ** already
b8230 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 an allocation at
b8240 20 70 48 2d 3e 68 74 2c 20 74 68 65 6e 20 69 66 pH->ht, then if
b8250 20 74 68 69 73 20 6d 61 6c 6c 6f 63 28 29 20 66 this malloc() f
b8260 61 69 6c 73 20 69 74 0a 20 20 2a 2a 20 69 73 20 ails it. ** is
b8270 62 65 6e 69 67 6e 20 28 73 69 6e 63 65 20 66 61 benign (since fa
b8280 69 6c 69 6e 67 20 74 6f 20 72 65 73 69 7a 65 20 iling to resize
b8290 61 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 a hash table is
b82a0 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 20 20 a performance.
b82b0 2a 2a 20 68 69 74 20 6f 6e 6c 79 2c 20 6e 6f 74 ** hit only, not
b82c0 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 29 2e a fatal error).
b82d0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 48 2d 3e . */. if( pH->
b82e0 68 74 73 69 7a 65 3e 30 20 29 20 73 71 6c 69 74 htsize>0 ) sqlit
b82f0 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
b8300 6c 6f 63 28 29 3b 0a 20 20 6e 65 77 5f 68 74 20 loc();. new_ht
b8310 3d 20 28 73 74 72 75 63 74 20 5f 68 74 20 2a 29 = (struct _ht *)
b8320 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
b8330 6f 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 o( new_size*size
b8340 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 20 29 of(struct _ht) )
b8350 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 ;. if( pH->htsi
b8360 7a 65 3e 30 20 29 20 73 71 6c 69 74 65 33 45 6e ze>0 ) sqlite3En
b8370 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
b8380 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d .. if( new_ht==
b8390 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 0 ) return;. sq
b83a0 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e 68 lite3_free(pH->h
b83b0 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e t);. pH->ht = n
b83c0 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 ew_ht;. pH->hts
b83d0 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a ize = new_size;.
b83e0 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 for(elem=pH->f
b83f0 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d irst, pH->first=
b8400 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 0; elem; elem =
b8410 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 next_elem){.
b8420 69 6e 74 20 68 20 3d 20 73 74 72 48 61 73 68 28 int h = strHash(
b8430 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d elem->pKey, elem
b8440 2d 3e 6e 4b 65 79 29 20 26 20 28 6e 65 77 5f 73 ->nKey) & (new_s
b8450 69 7a 65 2d 31 29 3b 0a 20 20 20 20 6e 65 78 74 ize-1);. next
b8460 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 _elem = elem->ne
b8470 78 74 3b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c xt;. insertEl
b8480 65 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68 ement(pH, &new_h
b8490 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d t[h], elem);. }
b84a0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 .}../* This func
b84b0 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e tion (for intern
b84c0 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63 al use only) loc
b84d0 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20 ates an element
b84e0 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61 in an.** hash ta
b84f0 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73 ble that matches
b8500 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20 the given key.
b8510 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 The hash for th
b8520 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c is key has.** al
b8530 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 ready been compu
b8540 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 ted and is passe
b8550 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72 d as the 4th par
b8560 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 ameter..*/.stati
b8570 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 6e 64 c HashElem *find
b8580 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 ElementGivenHash
b8590 28 0a 20 20 63 6f 6e 73 74 20 48 61 73 68 20 2a (. const Hash *
b85a0 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 pH, /* The p
b85b0 48 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 H to be searched
b85c0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
b85d0 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65 *pKey, /* The
b85e0 20 6b 65 79 20 77 65 20 61 72 65 20 73 65 61 72 key we are sear
b85f0 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 69 ching for */. i
b8600 6e 74 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 68 nt nKey,. int h
b8610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
b8620 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 * The hash for t
b8630 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 his key. */.){.
b8640 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b HashElem *elem;
b8650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b8660 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 /* Used to loop
b8670 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 thru the element
b8680 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63 list */. int c
b8690 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 ount;
b86a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
b86b0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
b86c0 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a left to test */.
b86d0 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b . if( pH->ht ){
b86e0 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20 . struct _ht
b86f0 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 *pEntry = &pH->h
b8700 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d t[h];. elem =
b8710 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a pEntry->chain;.
b8720 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 count = pEnt
b8730 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 20 20 77 ry->count;. w
b8740 68 69 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 hile( count-- &&
b8750 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 69 elem ){. i
b8760 66 28 20 73 74 72 43 6f 6d 70 61 72 65 28 65 6c f( strCompare(el
b8770 65 6d 2d 3e 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e em->pKey,elem->n
b8780 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d Key,pKey,nKey)==
b8790 30 20 29 7b 20 0a 20 20 20 20 20 20 20 20 72 65 0 ){ . re
b87a0 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 20 turn elem;.
b87b0 20 7d 0a 20 20 20 20 20 20 65 6c 65 6d 20 3d 20 }. elem =
b87c0 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 elem->next;.
b87d0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
b87e0 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 ;.}../* Remove a
b87f0 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 single entry fr
b8800 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c om the hash tabl
b8810 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 e given a pointe
b8820 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 r to that.** ele
b8830 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 ment and a hash
b8840 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 on the element's
b8850 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 key..*/.static
b8860 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65 void removeEleme
b8870 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48 ntGivenHash(. H
b8880 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 ash *pH,
b8890 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 /* The pH conta
b88a0 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a ining "elem" */.
b88b0 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d HashElem* elem
b88c0 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 , /* The eleme
b88d0 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 nt to be removed
b88e0 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a from the pH */.
b88f0 20 20 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 int h
b8900 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 /* Hash valu
b8910 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e e for the elemen
b8920 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 t */.){. struct
b8930 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 _ht *pEntry;.
b8940 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 if( elem->prev )
b8950 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 {. elem->prev
b8960 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e ->next = elem->n
b8970 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 ext; . }else{.
b8980 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 pH->first = e
b8990 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 lem->next;. }.
b89a0 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 if( elem->next
b89b0 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 ){. elem->nex
b89c0 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e t->prev = elem->
b89d0 70 72 65 76 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 prev;. }. pEnt
b89e0 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b ry = &pH->ht[h];
b89f0 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 . if( pEntry->c
b8a00 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20 hain==elem ){.
b8a10 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 pEntry->chain
b8a20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 = elem->next;.
b8a30 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e }. pEntry->coun
b8a40 74 2d 2d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72 t--;. if( pEntr
b8a50 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 y->count<=0 ){.
b8a60 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e pEntry->chain
b8a70 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
b8a80 70 48 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20 pH->copyKey ){.
b8a90 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
b8aa0 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d elem->pKey);. }
b8ab0 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
b8ac0 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 elem );. pH->c
b8ad0 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 ount--;. if( pH
b8ae0 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 ->count<=0 ){.
b8af0 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69 assert( pH->fi
b8b00 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 rst==0 );. as
b8b10 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d sert( pH->count=
b8b20 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 =0 );. sqlite
b8b30 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 3HashClear(pH);.
b8b40 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 }.}../* Attemp
b8b50 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 t to locate an e
b8b60 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61 lement of the ha
b8b70 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74 68 sh table pH with
b8b80 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d a key.** that m
b8b90 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 atches pKey,nKey
b8ba0 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e . Return a poin
b8bb0 74 65 72 20 74 6f 20 74 68 65 20 63 6f 72 72 65 ter to the corre
b8bc0 73 70 6f 6e 64 69 6e 67 20 0a 2a 2a 20 48 61 73 sponding .** Has
b8bd0 68 45 6c 65 6d 20 73 74 72 75 63 74 75 72 65 20 hElem structure
b8be0 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 for this element
b8bf0 20 69 66 20 69 74 20 69 73 20 66 6f 75 6e 64 2c if it is found,
b8c00 20 6f 72 20 4e 55 4c 4c 0a 2a 2a 20 6f 74 68 65 or NULL.** othe
b8c10 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rwise..*/.SQLITE
b8c20 5f 50 52 49 56 41 54 45 20 48 61 73 68 45 6c 65 _PRIVATE HashEle
b8c30 6d 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 m *sqlite3HashFi
b8c40 6e 64 45 6c 65 6d 28 63 6f 6e 73 74 20 48 61 73 ndElem(const Has
b8c50 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 h *pH, const voi
b8c60 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 d *pKey, int nKe
b8c70 79 29 7b 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 y){. int h;
b8c80 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 68 61 /* A ha
b8c90 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 20 20 48 sh on key */. H
b8ca0 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 ashElem *elem;
b8cb0 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 /* The element
b8cc0 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 that matches ke
b8cd0 79 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 3d 3d y */.. if( pH==
b8ce0 30 20 7c 7c 20 70 48 2d 3e 68 74 3d 3d 30 20 29 0 || pH->ht==0 )
b8cf0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 68 20 3d return 0;. h =
b8d00 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c 6e 4b strHash(pKey,nK
b8d10 65 79 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 69 ey);. elem = fi
b8d20 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 ndElementGivenHa
b8d30 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c sh(pH,pKey,nKey,
b8d40 20 68 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 29 h % pH->htsize)
b8d50 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b ;. return elem;
b8d60 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 .}../* Attempt t
b8d70 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d o locate an elem
b8d80 65 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 ent of the hash
b8d90 74 61 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 table pH with a
b8da0 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 key.** that matc
b8db0 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 hes pKey,nKey.
b8dc0 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 20 Return the data
b8dd0 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 for this element
b8de0 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f 75 if it is.** fou
b8df0 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 nd, or NULL if t
b8e00 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 here is no match
b8e10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b8e20 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
b8e30 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 3HashFind(const
b8e40 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 Hash *pH, const
b8e50 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 void *pKey, int
b8e60 6e 4b 65 79 29 7b 0a 20 20 48 61 73 68 45 6c 65 nKey){. HashEle
b8e70 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 m *elem; /* T
b8e80 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74 20 he element that
b8e90 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a 20 matches key */.
b8ea0 20 65 6c 65 6d 20 3d 20 73 71 6c 69 74 65 33 48 elem = sqlite3H
b8eb0 61 73 68 46 69 6e 64 45 6c 65 6d 28 70 48 2c 20 ashFindElem(pH,
b8ec0 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 72 pKey, nKey);. r
b8ed0 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 eturn elem ? ele
b8ee0 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a m->data : 0;.}..
b8ef0 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 /* Insert an ele
b8f00 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 ment into the ha
b8f10 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 sh table pH. Th
b8f20 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b e key is pKey,nK
b8f30 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 ey.** and the da
b8f40 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a ta is "data"..**
b8f50 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e .** If no elemen
b8f60 74 20 65 78 69 73 74 73 20 77 69 74 68 20 61 20 t exists with a
b8f70 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 matching key, th
b8f80 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d en a new.** elem
b8f90 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 2e 20 ent is created.
b8fa0 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b A copy of the k
b8fb0 65 79 20 69 73 20 6d 61 64 65 20 69 66 20 74 68 ey is made if th
b8fc0 65 20 63 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 e copyKey.** fla
b8fd0 67 20 69 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20 g is set. NULL
b8fe0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
b8ff0 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c ** If another el
b9000 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 ement already ex
b9010 69 73 74 73 20 77 69 74 68 20 74 68 65 20 73 61 ists with the sa
b9020 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 me key, then the
b9030 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 .** new data rep
b9040 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 laces the old da
b9050 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 ta and the old d
b9060 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e ata is returned.
b9070 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e .** The key is n
b9080 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 ot copied in thi
b9090 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 s instance. If
b90a0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 a malloc fails,
b90b0 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 then.** the new
b90c0 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 data is returned
b90d0 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 and the hash ta
b90e0 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ble is unchanged
b90f0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 ..**.** If the "
b9100 64 61 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 data" parameter
b9110 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
b9120 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 is NULL, then t
b9130 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f he.** element co
b9140 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 rresponding to "
b9150 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 key" is removed
b9160 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 from the hash ta
b9170 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ble..*/.SQLITE_P
b9180 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
b9190 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 ite3HashInsert(H
b91a0 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 ash *pH, const v
b91b0 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e oid *pKey, int n
b91c0 4b 65 79 2c 20 76 6f 69 64 20 2a 64 61 74 61 29 Key, void *data)
b91d0 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20 20 20 {. int hraw;
b91e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 61 77 /* Raw
b91f0 20 68 61 73 68 20 76 61 6c 75 65 20 6f 66 20 74 hash value of t
b9200 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 he key */. int
b9210 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
b9220 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66 /* the hash of
b9230 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 the key modulo
b9240 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20 hash table size
b9250 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 */. HashElem *e
b9260 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 lem; /* Us
b9270 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 ed to loop thru
b9280 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 the element list
b9290 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
b92a0 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e new_elem; /* N
b92b0 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64 ew element added
b92c0 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 0a 20 to the pH */..
b92d0 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 assert( pH!=0 )
b92e0 3b 0a 20 20 68 72 61 77 20 3d 20 73 74 72 48 61 ;. hraw = strHa
b92f0 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a sh(pKey, nKey);.
b9300 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 if( pH->htsize
b9310 20 29 7b 0a 20 20 20 20 68 20 3d 20 68 72 61 77 ){. h = hraw
b9320 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 % pH->htsize;.
b9330 20 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c elem = findEl
b9340 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 ementGivenHash(p
b9350 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a H,pKey,nKey,h);.
b9360 20 20 20 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a if( elem ){.
b9370 20 20 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f void *old_
b9380 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 data = elem->dat
b9390 61 3b 0a 20 20 20 20 20 20 69 66 28 20 64 61 74 a;. if( dat
b93a0 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 a==0 ){.
b93b0 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 removeElementGiv
b93c0 65 6e 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 enHash(pH,elem,h
b93d0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
b93e0 20 20 20 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61 elem->da
b93f0 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 20 20 20 ta = data;.
b9400 20 20 20 69 66 28 20 21 70 48 2d 3e 63 6f 70 79 if( !pH->copy
b9410 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Key ){.
b9420 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 elem->pKey = (v
b9430 6f 69 64 20 2a 29 70 4b 65 79 3b 0a 20 20 20 20 oid *)pKey;.
b9440 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 }. as
b9450 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c 65 6d 2d sert(nKey==elem-
b9460 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a >nKey);. }.
b9470 20 20 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 return old
b9480 5f 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d _data;. }. }
b9490 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 . if( data==0 )
b94a0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77 return 0;. new
b94b0 5f 65 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c 65 _elem = (HashEle
b94c0 6d 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 m*)sqlite3Malloc
b94d0 28 20 73 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 ( sizeof(HashEle
b94e0 6d 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f m) );. if( new_
b94f0 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e elem==0 ) return
b9500 20 64 61 74 61 3b 0a 20 20 69 66 28 20 70 48 2d data;. if( pH-
b9510 3e 63 6f 70 79 4b 65 79 20 26 26 20 70 4b 65 79 >copyKey && pKey
b9520 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 77 5f 65 !=0 ){. new_e
b9530 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 73 71 6c 69 lem->pKey = sqli
b9540 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 te3Malloc( nKey
b9550 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 77 5f 65 );. if( new_e
b9560 6c 65 6d 2d 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a lem->pKey==0 ){.
b9570 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
b9580 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 ee(new_elem);.
b9590 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b return data;
b95a0 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 . }. memcp
b95b0 79 28 28 76 6f 69 64 2a 29 6e 65 77 5f 65 6c 65 y((void*)new_ele
b95c0 6d 2d 3e 70 4b 65 79 2c 20 70 4b 65 79 2c 20 6e m->pKey, pKey, n
b95d0 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Key);. }else{.
b95e0 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 new_elem->pKe
b95f0 79 20 3d 20 28 76 6f 69 64 2a 29 70 4b 65 79 3b y = (void*)pKey;
b9600 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d . }. new_elem-
b9610 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 >nKey = nKey;.
b9620 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 pH->count++;. i
b9630 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 f( pH->htsize==0
b9640 20 29 7b 0a 20 20 20 20 72 65 68 61 73 68 28 70 ){. rehash(p
b9650 48 2c 20 31 32 38 2f 73 69 7a 65 6f 66 28 70 48 H, 128/sizeof(pH
b9660 2d 3e 68 74 5b 30 5d 29 29 3b 0a 20 20 20 20 69 ->ht[0]));. i
b9670 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 f( pH->htsize==0
b9680 20 29 7b 0a 20 20 20 20 20 20 70 48 2d 3e 63 6f ){. pH->co
b9690 75 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 unt = 0;. i
b96a0 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 29 f( pH->copyKey )
b96b0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
b96c0 33 5f 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 2d 3_free(new_elem-
b96d0 3e 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a >pKey);. }.
b96e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
b96f0 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 ee(new_elem);.
b9700 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b return data;
b9710 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
b9720 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d pH->count > pH-
b9730 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 72 >htsize ){. r
b9740 65 68 61 73 68 28 70 48 2c 70 48 2d 3e 68 74 73 ehash(pH,pH->hts
b9750 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a 20 20 61 73 ize*2);. }. as
b9760 73 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 sert( pH->htsize
b9770 3e 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 >0 );. h = hraw
b9780 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 % pH->htsize;.
b9790 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 insertElement(p
b97a0 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e H, &pH->ht[h], n
b97b0 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f ew_elem);. new_
b97c0 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 elem->data = dat
b97d0 61 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d a;. return 0;.}
b97e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
b97f0 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 * End of hash.c
b9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9830 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
b9840 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 * Begin file opc
b9850 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a odes.c *********
b9860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9880 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c /./* Automatical
b9890 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 ly generated. D
b98a0 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a o not edit */./*
b98b0 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 See the mkopcod
b98c0 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f ec.awk script fo
b98d0 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 r details. */.#i
b98e0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
b98f0 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 E_OMIT_EXPLAIN)
b9900 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 || !defined(NDEB
b9910 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 UG) || defined(V
b9920 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 DBE_PROFILE) ||
b9930 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
b9940 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 EBUG).SQLITE_PRI
b9950 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
b9960 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 *sqlite3OpcodeNa
b9970 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 me(int i){. stat
b9980 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 ic const char *c
b9990 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 onst azName[] =
b99a0 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 { "?",. /*
b99b0 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20 1 */ "VNext",.
b99c0 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 41 /* 2 */ "A
b99d0 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f ffinity",. /
b99e0 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e * 3 */ "Column
b99f0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a ",. /* 4 *
b9a00 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 / "SetCookie",.
b9a10 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 53 /* 5 */ "S
b9a20 65 71 75 65 6e 63 65 22 2c 0a 20 20 20 20 20 2f equence",. /
b9a30 2a 20 20 20 36 20 2a 2f 20 22 4d 6f 76 65 47 74 * 6 */ "MoveGt
b9a40 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a ",. /* 7 *
b9a50 2f 20 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20 / "RowKey",.
b9a60 20 2f 2a 20 20 20 38 20 2a 2f 20 22 53 43 6f 70 /* 8 */ "SCop
b9a70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39 20 y",. /* 9
b9a80 2a 2f 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a */ "OpenWrite",.
b9a90 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 /* 10 */ "
b9aa0 49 66 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 31 If",. /* 11
b9ab0 20 2a 2f 20 22 56 52 6f 77 69 64 22 2c 0a 20 20 */ "VRowid",.
b9ac0 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 43 6f /* 12 */ "Co
b9ad0 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20 llSeq",. /*
b9ae0 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 13 */ "OpenRead
b9af0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a ",. /* 14 *
b9b00 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20 / "Expire",.
b9b10 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 75 74 6f /* 15 */ "Auto
b9b20 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a Commit",. /*
b9b30 20 20 31 36 20 2a 2f 20 22 4e 6f 74 22 2c 0a 20 16 */ "Not",.
b9b40 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 50 /* 17 */ "P
b9b50 61 67 65 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20 agecount",.
b9b60 2f 2a 20 20 31 38 20 2a 2f 20 22 49 6e 74 65 67 /* 18 */ "Integ
b9b70 72 69 74 79 43 6b 22 2c 0a 20 20 20 20 20 2f 2a rityCk",. /*
b9b80 20 20 31 39 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 19 */ "Sort",.
b9b90 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 22 /* 20 */ "
b9ba0 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Copy",. /*
b9bb0 32 31 20 2a 2f 20 22 54 72 61 63 65 22 2c 0a 20 21 */ "Trace",.
b9bc0 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 46 /* 22 */ "F
b9bd0 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f unction",. /
b9be0 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e 65 67 22 * 23 */ "IfNeg"
b9bf0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a 2f ,. /* 24 */
b9c00 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f 2a "Noop",. /*
b9c10 20 20 32 35 20 2a 2f 20 22 52 65 74 75 72 6e 22 25 */ "Return"
b9c20 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f ,. /* 26 */
b9c30 20 22 4e 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 "NewRowid",.
b9c40 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 56 61 72 /* 27 */ "Var
b9c50 69 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 iable",. /*
b9c60 20 32 38 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c 28 */ "String",
b9c70 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 . /* 29 */
b9c80 22 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a "RealAffinity",.
b9c90 20 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 /* 30 */ "
b9ca0 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f VRename",. /
b9cb0 2a 20 20 33 31 20 2a 2f 20 22 50 61 72 73 65 53 * 31 */ "ParseS
b9cc0 63 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20 chema",. /*
b9cd0 20 33 32 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a 32 */ "VOpen",.
b9ce0 20 20 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22 /* 33 */ "
b9cf0 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20 Close",. /*
b9d00 20 33 34 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 34 */ "CreateIn
b9d10 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 dex",. /* 3
b9d20 35 20 2a 2f 20 22 49 73 55 6e 69 71 75 65 22 2c 5 */ "IsUnique",
b9d30 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 . /* 36 */
b9d40 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20 "NotFound",.
b9d50 20 2f 2a 20 20 33 37 20 2a 2f 20 22 49 6e 74 36 /* 37 */ "Int6
b9d60 34 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 4",. /* 38
b9d70 2a 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a */ "MustBeInt",.
b9d80 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 /* 39 */ "
b9d90 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Halt",. /*
b9da0 34 30 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20 40 */ "Rowid",.
b9db0 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 49 /* 41 */ "I
b9dc0 64 78 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20 dxLT",. /*
b9dd0 34 32 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a 42 */ "AddImm",.
b9de0 20 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 /* 43 */ "
b9df0 53 74 61 74 65 6d 65 6e 74 22 2c 0a 20 20 20 20 Statement",.
b9e00 20 2f 2a 20 20 34 34 20 2a 2f 20 22 52 6f 77 44 /* 44 */ "RowD
b9e10 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 ata",. /* 4
b9e20 35 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 5 */ "MemMax",.
b9e30 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4e /* 46 */ "N
b9e40 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 otExists",.
b9e50 2f 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75 62 /* 47 */ "Gosub
b9e60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a ",. /* 48 *
b9e70 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 / "Integer",.
b9e80 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 50 72 65 /* 49 */ "Pre
b9e90 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 v",. /* 50
b9ea0 2a 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20 */ "VColumn",.
b9eb0 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22 43 72 /* 51 */ "Cr
b9ec0 65 61 74 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 eateTable",.
b9ed0 20 2f 2a 20 20 35 32 20 2a 2f 20 22 4c 61 73 74 /* 52 */ "Last
b9ee0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 33 20 2a ",. /* 53 *
b9ef0 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a / "IncrVacuum",.
b9f00 20 20 20 20 20 2f 2a 20 20 35 34 20 2a 2f 20 22 /* 54 */ "
b9f10 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 IdxRowid",.
b9f20 2f 2a 20 20 35 35 20 2a 2f 20 22 52 65 73 65 74 /* 55 */ "Reset
b9f30 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 Count",. /*
b9f40 20 35 36 20 2a 2f 20 22 46 69 66 6f 57 72 69 74 56 */ "FifoWrit
b9f50 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 e",. /* 57
b9f60 2a 2f 20 22 43 6f 6e 74 65 78 74 50 75 73 68 22 */ "ContextPush"
b9f70 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f ,. /* 58 */
b9f80 20 22 59 69 65 6c 64 22 2c 0a 20 20 20 20 20 2f "Yield",. /
b9f90 2a 20 20 35 39 20 2a 2f 20 22 44 72 6f 70 54 72 * 59 */ "DropTr
b9fa0 69 67 67 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 igger",. /*
b9fb0 20 36 30 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 60 */ "Or",.
b9fc0 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 41 6e 64 /* 61 */ "And
b9fd0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 32 20 2a ",. /* 62 *
b9fe0 2f 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 / "DropIndex",.
b9ff0 20 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 49 /* 63 */ "I
ba000 64 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a 20 20 dxGE",. /*
ba010 36 34 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 64 */ "IdxDelete
ba020 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 35 20 2a ",. /* 65 *
ba030 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 20 / "IsNull",.
ba040 20 2f 2a 20 20 36 36 20 2a 2f 20 22 4e 6f 74 4e /* 66 */ "NotN
ba050 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 ull",. /* 6
ba060 37 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 20 7 */ "Ne",.
ba070 2f 2a 20 20 36 38 20 2a 2f 20 22 45 71 22 2c 0a /* 68 */ "Eq",.
ba080 20 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22 /* 69 */ "
ba090 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 30 Gt",. /* 70
ba0a0 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 2f */ "Le",. /
ba0b0 2a 20 20 37 31 20 2a 2f 20 22 4c 74 22 2c 0a 20 * 71 */ "Lt",.
ba0c0 20 20 20 20 2f 2a 20 20 37 32 20 2a 2f 20 22 47 /* 72 */ "G
ba0d0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 e",. /* 73
ba0e0 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20 20 20 */ "Vacuum",.
ba0f0 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 42 69 74 /* 74 */ "Bit
ba100 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 And",. /* 7
ba110 35 20 2a 2f 20 22 42 69 74 4f 72 22 2c 0a 20 20 5 */ "BitOr",.
ba120 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 53 68 /* 76 */ "Sh
ba130 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 20 20 2f iftLeft",. /
ba140 2a 20 20 37 37 20 2a 2f 20 22 53 68 69 66 74 52 * 77 */ "ShiftR
ba150 69 67 68 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ight",. /*
ba160 37 38 20 2a 2f 20 22 41 64 64 22 2c 0a 20 20 20 78 */ "Add",.
ba170 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 53 75 62 /* 79 */ "Sub
ba180 74 72 61 63 74 22 2c 0a 20 20 20 20 20 2f 2a 20 tract",. /*
ba190 20 38 30 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 79 80 */ "Multiply
ba1a0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 31 20 2a ",. /* 81 *
ba1b0 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 20 20 20 / "Divide",.
ba1c0 20 2f 2a 20 20 38 32 20 2a 2f 20 22 52 65 6d 61 /* 82 */ "Rema
ba1d0 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 inder",. /*
ba1e0 20 38 33 20 2a 2f 20 22 43 6f 6e 63 61 74 22 2c 83 */ "Concat",
ba1f0 0a 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 . /* 84 */
ba200 22 4d 6f 76 65 4c 65 22 2c 0a 20 20 20 20 20 2f "MoveLe",. /
ba210 2a 20 20 38 35 20 2a 2f 20 22 49 66 4e 6f 74 22 * 85 */ "IfNot"
ba220 2c 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f ,. /* 86 */
ba230 20 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 "DropTable",.
ba240 20 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 42 69 /* 87 */ "Bi
ba250 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 tNot",. /*
ba260 38 38 20 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 88 */ "String8",
ba270 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f 20 . /* 89 */
ba280 22 4d 61 6b 65 52 65 63 6f 72 64 22 2c 0a 20 20 "MakeRecord",.
ba290 20 20 20 2f 2a 20 20 39 30 20 2a 2f 20 22 52 65 /* 90 */ "Re
ba2a0 73 75 6c 74 52 6f 77 22 2c 0a 20 20 20 20 20 2f sultRow",. /
ba2b0 2a 20 20 39 31 20 2a 2f 20 22 44 65 6c 65 74 65 * 91 */ "Delete
ba2c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 2a ",. /* 92 *
ba2d0 2f 20 22 41 67 67 46 69 6e 61 6c 22 2c 0a 20 20 / "AggFinal",.
ba2e0 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20 22 43 6f /* 93 */ "Co
ba2f0 6d 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 mpare",. /*
ba300 20 39 34 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 94 */ "Goto",.
ba310 20 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 54 /* 95 */ "T
ba320 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 ableLock",.
ba330 2f 2a 20 20 39 36 20 2a 2f 20 22 46 69 66 6f 52 /* 96 */ "FifoR
ba340 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 ead",. /* 9
ba350 37 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20 7 */ "Clear",.
ba360 20 20 20 2f 2a 20 20 39 38 20 2a 2f 20 22 4d 6f /* 98 */ "Mo
ba370 76 65 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 veLt",. /*
ba380 39 39 20 2a 2f 20 22 56 65 72 69 66 79 43 6f 6f 99 */ "VerifyCoo
ba390 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 kie",. /* 10
ba3a0 30 20 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a 0 */ "AggStep",.
ba3b0 20 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f 20 22 /* 101 */ "
ba3c0 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 22 2c 0a SetNumColumns",.
ba3d0 20 20 20 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22 /* 102 */ "
ba3e0 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20 Transaction",.
ba3f0 20 20 20 2f 2a 20 31 30 33 20 2a 2f 20 22 56 46 /* 103 */ "VF
ba400 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 ilter",. /*
ba410 31 30 34 20 2a 2f 20 22 56 44 65 73 74 72 6f 79 104 */ "VDestroy
ba420 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 35 20 2a ",. /* 105 *
ba430 2f 20 22 43 6f 6e 74 65 78 74 50 6f 70 22 2c 0a / "ContextPop",.
ba440 20 20 20 20 20 2f 2a 20 31 30 36 20 2a 2f 20 22 /* 106 */ "
ba450 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Next",. /* 1
ba460 30 37 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 07 */ "IdxInsert
ba470 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a ",. /* 108 *
ba480 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 / "Insert",.
ba490 20 2f 2a 20 31 30 39 20 2a 2f 20 22 44 65 73 74 /* 109 */ "Dest
ba4a0 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 roy",. /* 11
ba4b0 30 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 65 0 */ "ReadCookie
ba4c0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a ",. /* 111 *
ba4d0 2f 20 22 46 6f 72 63 65 49 6e 74 22 2c 0a 20 20 / "ForceInt",.
ba4e0 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 4c 6f /* 112 */ "Lo
ba4f0 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 adAnalysis",.
ba500 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 45 78 70 /* 113 */ "Exp
ba510 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 lain",. /* 1
ba520 31 34 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 14 */ "OpenPseud
ba530 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 o",. /* 115
ba540 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61 */ "OpenEphemera
ba550 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 l",. /* 116
ba560 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 */ "Null",.
ba570 2f 2a 20 31 31 37 20 2a 2f 20 22 4d 6f 76 65 22 /* 117 */ "Move"
ba580 2c 0a 20 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f ,. /* 118 */
ba590 20 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a "Blob",. /*
ba5a0 20 31 31 39 20 2a 2f 20 22 52 65 77 69 6e 64 22 119 */ "Rewind"
ba5b0 2c 0a 20 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f ,. /* 120 */
ba5c0 20 22 4d 6f 76 65 47 65 22 2c 0a 20 20 20 20 20 "MoveGe",.
ba5d0 2f 2a 20 31 32 31 20 2a 2f 20 22 56 42 65 67 69 /* 121 */ "VBegi
ba5e0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 32 20 n",. /* 122
ba5f0 2a 2f 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20 */ "VUpdate",.
ba600 20 20 20 2f 2a 20 31 32 33 20 2a 2f 20 22 49 66 /* 123 */ "If
ba610 5a 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Zero",. /* 1
ba620 32 34 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 24 */ "VCreate",
ba630 0a 20 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f 20 . /* 125 */
ba640 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a "Found",. /*
ba650 20 31 32 36 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 126 */ "Real",.
ba660 20 20 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 /* 127 */ "
ba670 49 66 50 6f 73 22 2c 0a 20 20 20 20 20 2f 2a 20 IfPos",. /*
ba680 31 32 38 20 2a 2f 20 22 4e 75 6c 6c 52 6f 77 22 128 */ "NullRow"
ba690 2c 0a 20 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f ,. /* 129 */
ba6a0 20 22 4a 75 6d 70 22 2c 0a 20 20 20 20 20 2f 2a "Jump",. /*
ba6b0 20 31 33 30 20 2a 2f 20 22 50 65 72 6d 75 74 61 130 */ "Permuta
ba6c0 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 tion",. /* 1
ba6d0 33 31 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 31 */ "NotUsed_1
ba6e0 33 31 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32 31",. /* 132
ba6f0 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 32 */ "NotUsed_132
ba700 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a ",. /* 133 *
ba710 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22 2c / "NotUsed_133",
ba720 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20 . /* 134 */
ba730 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a 20 "NotUsed_134",.
ba740 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e /* 135 */ "N
ba750 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 otUsed_135",.
ba760 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 /* 136 */ "Not
ba770 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 Used_136",.
ba780 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 /* 137 */ "NotUs
ba790 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a ed_137",. /*
ba7a0 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 138 */ "NotUsed
ba7b0 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 _138",. /* 1
ba7c0 33 39 20 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a 39 */ "ToText",.
ba7d0 20 20 20 20 20 2f 2a 20 31 34 30 20 2a 2f 20 22 /* 140 */ "
ba7e0 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a ToBlob",. /*
ba7f0 20 31 34 31 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 141 */ "ToNumer
ba800 69 63 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 32 ic",. /* 142
ba810 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20 */ "ToInt",.
ba820 20 20 2f 2a 20 31 34 33 20 2a 2f 20 22 54 6f 52 /* 143 */ "ToR
ba830 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 eal",. };. ret
ba840 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d urn azName[i];.}
ba850 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
ba860 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
ba870 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a opcodes.c ******
ba880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba8a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
ba8b0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
ba8c0 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a ile os_os2.c ***
ba8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba8f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
ba900 30 30 36 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 006 Feb 14.**.**
ba910 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
ba920 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
ba930 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
ba940 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
ba950 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
ba960 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
ba970 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
ba980 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
ba990 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
ba9a0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
ba9b0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
ba9c0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
ba9d0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
ba9e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
ba9f0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
baa00 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
baa10 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
baa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
baa70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
baa80 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 ontains code tha
baa90 74 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f t is specific to
baaa0 20 4f 53 2f 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 OS/2..**.** $Id
baab0 3a 20 6f 73 5f 6f 73 32 2e 63 2c 76 20 31 2e 35 : os_os2.c,v 1.5
baac0 38 20 32 30 30 38 2f 31 31 2f 30 37 20 30 30 3a 8 2008/11/07 00:
baad0 30 36 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a 06:18 drh Exp $.
baae0 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f */...#if SQLITE_
baaf0 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 OS_OS2../*.** A
bab00 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 Note About Memor
bab10 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a y Allocation:.**
bab20 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72 20 .** This driver
bab30 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 uses malloc()/fr
bab40 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72 61 ee() directly ra
bab50 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 ther than going
bab60 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 through.** the S
bab70 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20 73 QLite-wrappers s
bab80 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f qlite3_malloc()/
bab90 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20 sqlite3_free().
baba0 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a Those wrappers.
babb0 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 ** are designed
babc0 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 for use on embed
babd0 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65 72 ded systems wher
babe0 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72 e memory is scar
babf0 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 ce and.** malloc
bac00 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e failures happen
bac10 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 4f 53 frequently. OS
bac20 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 /2 does not typi
bac30 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 cally run on.**
bac40 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 embedded systems
bac50 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64 6f , and when it do
bac60 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 es the developer
bac70 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 s normally have
bac80 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 bigger.** proble
bac90 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 ms to worry abou
baca0 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f t than running o
bacb0 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 ut of memory. S
bacc0 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a o there is not.*
bacd0 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e * a compelling n
bace0 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 77 eed to use the w
bacf0 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 rappers..**.** B
bad00 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67 6f ut there is a go
bad10 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 od reason to not
bad20 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72 use the wrapper
bad30 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74 68 s. If we use th
bad40 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74 68 e.** wrappers th
bad50 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20 73 en we will get s
bad60 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 imulated malloc(
bad70 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 ) failures withi
bad80 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 n this.** driver
bad90 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75 73 . And that caus
bada0 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 es all kinds of
badb0 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 problems for our
badc0 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 tests. We.** c
badd0 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 4c ould enhance SQL
bade0 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 ite to deal with
badf0 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f simulated mallo
bae00 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 c failures withi
bae10 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 76 n.** the OS driv
bae20 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64 65 er, but the code
bae30 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 to deal with th
bae40 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75 6c ose failure woul
bae50 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 d not.** be exer
bae60 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 cised on Linux (
bae70 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e which does not n
bae80 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 eed to malloc()
bae90 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a 2a in the driver).*
baea0 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c * and so we woul
baeb0 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c 74 d have difficult
baec0 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72 61 y writing covera
baed0 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68 61 ge tests for tha
baee0 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 t.** code. Bett
baef0 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 er to leave the
baf00 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68 69 code out, we thi
baf10 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f nk..**.** The po
baf20 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73 63 int of this disc
baf30 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c ussion is as fol
baf40 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65 61 lows: When crea
baf50 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 ting a new.** OS
baf60 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65 6d layer for an em
baf70 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20 69 bedded system, i
baf80 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20 66 f you use this f
baf90 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c ile as an exampl
bafa0 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 e,.** avoid the
bafb0 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f use of malloc()/
bafc0 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 72 free(). Those r
bafd0 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 outines work ok
bafe0 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 on OS/2.** deskt
baff0 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 ops but not so w
bb000 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 20 ell in embedded
bb010 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a systems..*/../*.
bb020 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20 74 ** Macros used t
bb030 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 o determine whet
bb040 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 her or not to us
bb050 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 e threads..*/.#i
bb060 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
bb070 5f 54 48 52 45 41 44 53 41 46 45 29 20 26 26 20 _THREADSAFE) &&
bb080 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
bb090 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
bb0a0 45 5f 4f 53 32 5f 54 48 52 45 41 44 53 20 31 0a E_OS2_THREADS 1.
bb0b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
bb0c0 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 clude code that
bb0d0 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c is common to all
bb0e0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f os_*.c files.*/
bb0f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bb100 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d Include os_comm
bb110 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 on.h in the midd
bb120 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a le of os_os2.c *
bb130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bb140 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bb150 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 Begin file os_c
bb160 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
bb170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bb190 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
bb1a0 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 22.**.** The aut
bb1b0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
bb1c0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
bb1d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
bb1e0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
bb1f0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
bb200 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
bb210 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
bb220 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
bb230 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
bb240 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
bb250 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
bb260 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
bb270 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
bb280 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
bb290 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
bb2a0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
bb2b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
bb2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb300 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
bb310 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
bb320 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 macros and a lit
bb330 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 tle bit of code
bb340 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
bb350 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 o.** all of the
bb360 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 platform-specifi
bb370 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 c files (os_*.c)
bb380 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 and is #include
bb390 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 d into those.**
bb3a0 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 files..**.** Thi
bb3b0 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 s file should be
bb3c0 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 #included by th
bb3d0 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f e os_*.c files o
bb3e0 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 nly. It is not
bb3f0 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 a.** general pur
bb400 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 pose header file
bb410 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f ..**.** $Id: os_
bb420 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 37 20 common.h,v 1.37
bb430 32 30 30 38 2f 30 35 2f 32 39 20 32 30 3a 32 32 2008/05/29 20:22
bb440 3a 33 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a :37 shane Exp $.
bb450 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 */.#ifndef _OS_C
bb460 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 OMMON_H_.#define
bb470 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a _OS_COMMON_H_..
bb480 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 /*.** At least t
bb490 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 wo bugs have sli
bb4a0 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 pped in because
bb4b0 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d we changed the M
bb4c0 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d EMORY_DEBUG.** m
bb4d0 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 acro to SQLITE_D
bb4e0 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c EBUG and some ol
bb4f0 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 der makefiles ha
bb500 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 ve not yet made
bb510 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 the.** switch.
bb520 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f The following co
bb530 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 de should catch
bb540 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 this problem at
bb550 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f compile-time..*/
bb560 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 .#ifdef MEMORY_D
bb570 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 EBUG.# error "Th
bb580 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d e MEMORY_DEBUG m
bb590 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 acro is obsolete
bb5a0 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 . Use SQLITE_DE
bb5b0 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 BUG instead.".#e
bb5c0 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 57 68 65 ndif.../*. * Whe
bb5d0 6e 20 74 65 73 74 69 6e 67 2c 20 74 68 69 73 20 n testing, this
bb5e0 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
bb5f0 73 74 6f 72 65 73 20 74 68 65 20 6c 6f 63 61 74 stores the locat
bb600 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a 20 70 65 ion of the. * pe
bb610 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e 20 74 68 nding-byte in th
bb620 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
bb630 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 . */.#ifdef SQLI
bb640 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
bb650 50 49 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 PI unsigned int
bb660 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f sqlite3_pending_
bb670 62 79 74 65 20 3d 20 30 78 34 30 30 30 30 30 30 byte = 0x4000000
bb680 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 0;.#endif..#ifde
bb690 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 f SQLITE_DEBUG.S
bb6a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
bb6b0 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 t sqlite3OSTrace
bb6c0 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 = 0;.#define OS
bb6d0 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20 TRACE1(X)
bb6e0 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
bb6f0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
bb700 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 bugPrintf(X).#de
bb710 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c fine OSTRACE2(X,
bb720 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71 6c Y) if( sql
bb730 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
bb740 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
bb750 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 (X,Y).#define OS
bb760 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 TRACE3(X,Y,Z)
bb770 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
bb780 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
bb790 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 bugPrintf(X,Y,Z)
bb7a0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bb7b0 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 4(X,Y,Z,A) if(
bb7c0 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
bb7d0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
bb7e0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 intf(X,Y,Z,A).#d
bb7f0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 efine OSTRACE5(X
bb800 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 ,Y,Z,A,B) if( sq
bb810 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
bb820 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
bb830 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 f(X,Y,Z,A,B).#de
bb840 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c fine OSTRACE6(X,
bb850 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 Y,Z,A,B,C) \.
bb860 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 if(sqlite3OSTra
bb870 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 ce) sqlite3Debug
bb880 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
bb890 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,C).#define OSTR
bb8a0 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE7(X,Y,Z,A,B,C
bb8b0 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c ,D) \. if(sql
bb8c0 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c ite3OSTrace) sql
bb8d0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
bb8e0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 X,Y,Z,A,B,C,D).#
bb8f0 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 else.#define OST
bb900 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 RACE1(X).#define
bb910 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 OSTRACE2(X,Y).#
bb920 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 define OSTRACE3(
bb930 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f X,Y,Z).#define O
bb940 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 STRACE4(X,Y,Z,A)
bb950 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bb960 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 5(X,Y,Z,A,B).#de
bb970 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c fine OSTRACE6(X,
bb980 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 Y,Z,A,B,C).#defi
bb990 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c ne OSTRACE7(X,Y,
bb9a0 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 Z,A,B,C,D).#endi
bb9b0 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 f../*.** Macros
bb9c0 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 for performance
bb9d0 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c tracing. Normal
bb9e0 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 ly turned off.
bb9f0 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e Only works.** on
bba00 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a i486 hardware..
bba10 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
bba20 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 _PERFORMANCE_TRA
bba30 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d CE../* .** hwtim
bba40 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c e.h contains inl
bba50 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f ine assembler co
bba60 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 de for implement
bba70 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 ing .** high-per
bba80 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 formance timing
bba90 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a routines..*/./**
bbaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
bbab0 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e lude hwtime.h in
bbac0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f the middle of o
bbad0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a s_common.h *****
bbae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
bbaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
bbb00 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 in file hwtime.h
bbb10 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
bbb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
bbb40 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a ** 2008 May 27.*
bbb50 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
bbb60 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
bbb70 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
bbb80 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
bbb90 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
bbba0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
bbbb0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
bbbc0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
bbbd0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
bbbe0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
bbbf0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
bbc00 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
bbc10 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
bbc20 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
bbc30 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
bbc40 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
bbc50 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
bbc60 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
bbc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbcb0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
bbcc0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 le contains inli
bbcd0 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 ne asm code for
bbce0 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 retrieving "high
bbcf0 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a -performance".**
bbd00 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 counters for x8
bbd10 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 6 class CPUs..**
bbd20 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e .** $Id: hwtime.
bbd30 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f h,v 1.3 2008/08/
bbd40 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 01 14:33:15 shan
bbd50 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 e Exp $.*/.#ifnd
bbd60 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 ef _HWTIME_H_.#d
bbd70 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f efine _HWTIME_H_
bbd80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
bbd90 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e owing routine on
bbda0 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 ly works on pent
bbdb0 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 ium-class (or ne
bbdc0 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e wer) processors.
bbdd0 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 .** It uses the
bbde0 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 RDTSC opcode to
bbdf0 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 read the cycle c
bbe00 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f ount value out o
bbe10 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 f the.** process
bbe20 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 or and returns t
bbe30 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 hat value. This
bbe40 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 can be used for
bbe50 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f high-res.** pro
bbe60 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 filing..*/.#if (
bbe70 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
bbe80 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 ) || defined(_MS
bbe90 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 C_VER)) && \.
bbea0 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 (defined(i386
bbeb0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 ) || defined(__i
bbec0 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 386__) || define
bbed0 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 d(_M_IX86)).. #
bbee0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 if defined(__GNU
bbef0 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 C__).. __inline
bbf00 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
bbf10 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
bbf20 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 oid){. unsig
bbf30 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a ned int lo, hi;.
bbf40 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
bbf50 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 olatile__ ("rdts
bbf60 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 c" : "=a" (lo),
bbf70 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 "=d" (hi));.
bbf80 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f return (sqlite_
bbf90 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 uint64)hi << 32
bbfa0 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c | lo;. }.. #el
bbfb0 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f if defined(_MSC_
bbfc0 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 VER).. __declsp
bbfd0 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 ec(naked) __inli
bbfe0 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ne sqlite_uint64
bbff0 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 __cdecl sqlite3
bc000 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
bc010 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 __asm {.
bc020 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 rdtsc.
bc030 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 ret ; ret
bc040 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 urn value at EDX
bc050 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a :EAX. }. }.
bc060 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 . #endif..#elif
bc070 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
bc080 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f __) && defined(_
bc090 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f _x86_64__)).. _
bc0a0 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
bc0b0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
bc0c0 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
bc0d0 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
bc0e0 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 val;. __as
bc0f0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
bc100 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 ("rdtsc" : "=A"
bc110 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 (val));. r
bc120 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 eturn val;. }.
bc130 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 .#elif (defined(
bc140 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 __GNUC__) && def
bc150 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a ined(__ppc__))..
bc160 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
bc170 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
bc180 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
bc190 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c unsigned l
bc1a0 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b ong long retval;
bc1b0 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
bc1c0 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 long junk;.
bc1d0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
bc1e0 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 ile__ ("\n\.
bc1f0 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 1: mf
bc200 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 tbu %1\n\.
bc210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 mf
bc220 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 tb %L0\n\.
bc230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
bc240 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 ftbu %0\n\.
bc250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
bc260 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a mpw %0,%1\n\.
bc270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bc280 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 bne 1b".
bc290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bc2a0 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c : "=r" (retval),
bc2b0 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 "=r" (junk));.
bc2c0 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 return retv
bc2d0 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a al;. }..#else..
bc2e0 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d #error Need im
bc2f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
bc300 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
bc310 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 for your platfor
bc320 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f m... /*. ** To
bc330 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 compile without
bc340 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 implementing sq
bc350 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
bc360 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c r your platform,
bc370 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 . ** you can re
bc380 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 move the above #
bc390 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 error and use th
bc3a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a e following. **
bc3b0 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 stub function.
bc3c0 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 You will lose t
bc3d0 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f iming support fo
bc3e0 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 r many. ** of t
bc3f0 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 he debugging and
bc400 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 testing utiliti
bc410 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c es, but it shoul
bc420 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 d at. ** least
bc430 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e compile and run.
bc440 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 . */.SQLITE_PRI
bc450 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 VATE sqlite_ui
bc460 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
bc470 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e me(void){ return
bc480 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ((sqlite_uint64
bc490 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a )0); }..#endif..
bc4a0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
bc4b0 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a ed(_HWTIME_H_) *
bc4c0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
bc4d0 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 ** End of hwtime
bc4e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
bc4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc510 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
bc520 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
bc530 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
bc540 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a in os_common.h *
bc550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc560 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 */..static sqlit
bc570 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 e_uint64 g_start
bc580 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f ;.static sqlite_
bc590 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 uint64 g_elapsed
bc5a0 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f ;.#define TIMER_
bc5b0 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 START g_st
bc5c0 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d art=sqlite3Hwtim
bc5d0 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 e().#define TIME
bc5e0 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f R_END g_
bc5f0 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 elapsed=sqlite3H
bc600 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a wtime()-g_start.
bc610 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c #define TIMER_EL
bc620 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 APSED g_elap
bc630 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e sed.#else.#defin
bc640 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 e TIMER_START.#d
bc650 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a efine TIMER_END.
bc660 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c #define TIMER_EL
bc670 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 APSED ((sqli
bc680 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e te_uint64)0).#en
bc690 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 dif../*.** If we
bc6a0 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 compile with th
bc6b0 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 e SQLITE_TEST ma
bc6c0 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 cro set, then th
bc6d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
bc6e0 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c k.** of code wil
bc6f0 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61 62 l give us the ab
bc700 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 ility to simulat
bc710 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 e a disk I/O err
bc720 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 or. This.** is
bc730 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
bc740 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 the I/O recover
bc750 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 y logic..*/.#ifd
bc760 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
bc770 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
bc780 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
bc790 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 it = 0;
bc7a0 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 /* Total numb
bc7b0 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 er of I/O Errors
bc7c0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
bc7d0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
bc7e0 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b ror_hardhit = 0;
bc7f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
bc800 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 r of non-benign
bc810 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 errors */.SQLITE
bc820 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
bc830 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
bc840 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a g = 0; /*
bc850 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 Count down to f
bc860 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a irst I/O error *
bc870 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
bc880 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
bc890 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 r_persist = 0;
bc8a0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
bc8b0 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 I/O errors pers
bc8c0 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ist */.SQLITE_AP
bc8d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
bc8e0 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 _error_benign =
bc8f0 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
bc900 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 ue if errors are
bc910 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 benign */.SQLIT
bc920 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
bc930 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
bc940 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 ng = 0;.SQLITE_A
bc950 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 PI int sqlite3_d
bc960 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 iskfull = 0;.#de
bc970 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
bc980 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 rrorBenign(X) sq
bc990 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
bc9a0 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e enign=(X).#defin
bc9b0 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f e SimulateIOErro
bc9c0 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 r(CODE) \. if(
bc9d0 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 (sqlite3_io_err
bc9e0 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 or_persist && sq
bc9f0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
bca00 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 it) \. ||
bca10 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
bca20 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 _pending-- == 1
bca30 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 ) \.
bca40 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 { local_ioerr
bca50 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 (); CODE; }.stat
bca60 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f ic void local_io
bca70 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 err(){. IOTRACE
bca80 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 (("IOERR\n"));.
bca90 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
bcaa0 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 r_hit++;. if( !
bcab0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
bcac0 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 _benign ) sqlite
bcad0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 3_io_error_hardh
bcae0 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 it++;.}.#define
bcaf0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
bcb00 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 Error(CODE) \.
bcb10 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 if( sqlite3_dis
bcb20 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b kfull_pending ){
bcb30 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 \. if( sqli
bcb40 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
bcb50 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 ding == 1 ){ \.
bcb60 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 local_ioer
bcb70 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 r(); \. sq
bcb80 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d lite3_diskfull =
bcb90 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 1; \. sql
bcba0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 ite3_io_error_hi
bcbb0 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 t = 1; \.
bcbc0 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c CODE; \. }el
bcbd0 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c se{ \. sql
bcbe0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
bcbf0 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 nding--; \.
bcc00 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 } \. }.#else.#
bcc10 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
bcc20 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a OErrorBenign(X).
bcc30 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
bcc40 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 IOError(A).#defi
bcc50 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 ne SimulateDiskf
bcc60 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 ullError(A).#end
bcc70 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 if../*.** When t
bcc80 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 esting, keep a c
bcc90 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 ount of the numb
bcca0 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 er of open files
bccb0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
bccc0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
bccd0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
bcce0 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d pen_file_count =
bccf0 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 0;.#define Open
bcd00 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 Counter(X) sqli
bcd10 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f te3_open_file_co
bcd20 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 unt+=(X).#else.#
bcd30 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 define OpenCount
bcd40 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 er(X).#endif..#e
bcd50 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
bcd60 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 (_OS_COMMON_H_)
bcd70 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
bcd80 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f *** End of os_co
bcd90 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
bcda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcdc0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
bcdd0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
bcde0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
bcdf0 20 69 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a in os_os2.c ***
bce00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bce10 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f **/../*.** The o
bce20 73 32 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 s2File structure
bce30 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 is subclass of
bce40 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 sqlite3_file spe
bce50 63 69 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53 cific for the OS
bce60 2f 32 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 /2.** protabilit
bce70 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 y layer..*/.type
bce80 64 65 66 20 73 74 72 75 63 74 20 6f 73 32 46 69 def struct os2Fi
bce90 6c 65 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 le os2File;.stru
bcea0 63 74 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 ct os2File {. c
bceb0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
bcec0 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 methods *pMethod
bced0 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 ; /* Always the
bcee0 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a first entry */.
bcef0 20 20 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20 HFILE h;
bcf00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
bcf10 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 andle for access
bcf20 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a ing the file */.
bcf30 20 20 63 68 61 72 2a 20 70 61 74 68 54 6f 44 65 char* pathToDe
bcf40 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e l; /* N
bcf50 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 ame of file to d
bcf60 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 elete on close,
bcf70 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 NULL if not */.
bcf80 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c unsigned char l
bcf90 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 ocktype; /* Ty
bcfa0 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 pe of lock curre
bcfb0 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 ntly held on thi
bcfc0 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 s file */.};..#d
bcfd0 65 66 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f efine LOCK_TIMEO
bcfe0 55 54 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 UT 10L /* the de
bcff0 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 fault locking ti
bd000 6d 65 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a meout */../*****
bd010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd050 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
bd060 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f next group of ro
bd070 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 utines implement
bd080 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 the I/O methods
bd090 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 specified.** by
bd0a0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f the sqlite3_io_
bd0b0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a methods object..
bd0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bd110 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 ./*.** Close a f
bd120 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
bd130 6e 74 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c nt os2Close( sql
bd140 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b ite3_file *id ){
bd150 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e . APIRET rc = N
bd160 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 O_ERROR;. os2Fi
bd170 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 le *pFile;. if(
bd180 20 69 64 20 26 26 20 28 70 46 69 6c 65 20 3d 20 id && (pFile =
bd190 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 20 21 3d (os2File*)id) !=
bd1a0 20 30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 0 ){. OSTRAC
bd1b0 45 32 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 E2( "CLOSE %d\n"
bd1c0 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 , pFile->h );.
bd1d0 20 20 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 rc = DosClose(
bd1e0 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 pFile->h );.
bd1f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
bd200 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 = NO_LOCK;.
bd210 69 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 if( pFile->pathT
bd220 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a oDel != NULL ){.
bd230 20 20 20 20 20 20 72 63 20 3d 20 44 6f 73 46 6f rc = DosFo
bd240 72 63 65 44 65 6c 65 74 65 28 20 28 50 53 5a 29 rceDelete( (PSZ)
bd250 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
bd260 20 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 20 );. free(
bd270 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
bd280 20 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d );. pFile-
bd290 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c >pathToDel = NUL
bd2a0 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 20 L;. }. id
bd2b0 3d 20 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 = 0;. OpenCou
bd2c0 6e 74 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a nter( -1 );. }.
bd2d0 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 . return rc ==
bd2e0 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 NO_ERROR ? SQLIT
bd2f0 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f E_OK : SQLITE_IO
bd300 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ERR;.}../*.** Re
bd310 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 ad data from a f
bd320 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 ile into a buffe
bd330 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 r. Return SQLIT
bd340 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 E_OK if all.** b
bd350 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73 ytes were read s
bd360 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 uccessfully and
bd370 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 SQLITE_IOERR if
bd380 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a anything goes.**
bd390 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 wrong..*/.stati
bd3a0 63 20 69 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 c int os2Read(.
bd3b0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
bd3c0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
bd3d0 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 /* File to read
bd3e0 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 from */. void
bd3f0 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 *pBuf,
bd400 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
bd410 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f ite content into
bd420 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a this buffer */.
bd430 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 int amt,
bd440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd450 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
bd460 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a ytes to read */.
bd470 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
bd480 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 offset
bd490 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 /* Begin readi
bd4a0 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 ng at this offse
bd4b0 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 t */.){. ULONG
bd4c0 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 fileLocation = 0
bd4d0 4c 3b 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a L;. ULONG got;.
bd4e0 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 os2File *pFile
bd4f0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b = (os2File*)id;
bd500 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
bd510 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f );. SimulateIO
bd520 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
bd530 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 LITE_IOERR_READ
bd540 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 );. OSTRACE3( "
bd550 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c READ %d lock=%d\
bd560 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 n", pFile->h, pF
bd570 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b ile->locktype );
bd580 0a 20 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c . if( DosSetFil
bd590 65 50 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f ePtr(pFile->h, o
bd5a0 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 ffset, FILE_BEGI
bd5b0 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e N, &fileLocation
bd5c0 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b ) != NO_ERROR ){
bd5d0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
bd5e0 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 TE_IOERR;. }.
bd5f0 69 66 28 20 44 6f 73 52 65 61 64 28 20 70 46 69 if( DosRead( pFi
bd600 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 le->h, pBuf, amt
bd610 2c 20 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 , &got ) != NO_E
bd620 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 RROR ){. retu
bd630 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
bd640 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 READ;. }. if(
bd650 67 6f 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d got == (ULONG)am
bd660 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53 t ). return S
bd670 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 QLITE_OK;. else
bd680 20 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 {. /* Unread
bd690 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 portions of the
bd6a0 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 6d 75 input buffer mu
bd6b0 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 st be zero-fille
bd6c0 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 d */. memset(
bd6d0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 &((char*)pBuf)[g
bd6e0 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 ot], 0, amt-got)
bd6f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
bd700 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
bd710 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a READ;. }.}../*.
bd720 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 ** Write data fr
bd730 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f om a buffer into
bd740 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e a file. Return
bd750 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
bd760 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 ccess.** or some
bd770 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 other error cod
bd780 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f e on failure..*/
bd790 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 57 .static int os2W
bd7a0 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f rite(. sqlite3_
bd7b0 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 file *id,
bd7c0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 /* File
bd7d0 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f to write into */
bd7e0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 . const void *p
bd7f0 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 Buf,
bd800 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 /* The bytes
bd810 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f to be written */
bd820 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 . int amt,
bd830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd840 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
bd850 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a bytes to write *
bd860 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 /. sqlite3_int6
bd870 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 4 offset
bd880 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
bd890 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 to the file to b
bd8a0 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 egin writing at
bd8b0 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 */.){. ULONG fi
bd8c0 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b leLocation = 0L;
bd8d0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e . APIRET rc = N
bd8e0 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 O_ERROR;. ULONG
bd8f0 20 77 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c wrote;. os2Fil
bd900 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 e *pFile = (os2F
bd910 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 ile*)id;. asser
bd920 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 t( id!=0 );. Si
bd930 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
bd940 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
bd950 52 52 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 69 RR_WRITE );. Si
bd960 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
bd970 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
bd980 54 45 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 TE_FULL );. OST
bd990 52 41 43 45 33 28 20 22 57 52 49 54 45 20 25 64 RACE3( "WRITE %d
bd9a0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 lock=%d\n", pFi
bd9b0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f le->h, pFile->lo
bd9c0 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20 cktype );. if(
bd9d0 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70 46 DosSetFilePtr(pF
bd9e0 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 ile->h, offset,
bd9f0 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c FILE_BEGIN, &fil
bda00 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f eLocation) != NO
bda10 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 _ERROR ){. re
bda20 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
bda30 52 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 R;. }. assert(
bda40 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c amt>0 );. whil
bda50 65 28 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 e( amt > 0 &&.
bda60 20 20 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f ( rc = Do
bda70 73 57 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 sWrite( pFile->h
bda80 2c 20 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61 , (PVOID)pBuf, a
bda90 6d 74 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d mt, &wrote ) ) =
bdaa0 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 = NO_ERROR &&.
bdab0 20 20 20 20 20 20 20 77 72 6f 74 65 20 3e 20 30 wrote > 0
bdac0 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d . ){. amt -=
bdad0 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 wrote;. pBuf
bdae0 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 = &((char*)pBuf
bdaf0 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 )[wrote];. }..
bdb00 20 72 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20 return ( rc !=
bdb10 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 NO_ERROR || amt
bdb20 3e 20 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f > (int)wrote ) ?
bdb30 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 SQLITE_FULL : S
bdb40 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
bdb50 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f ** Truncate an o
bdb60 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 pen file to a sp
bdb70 65 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a ecified size.*/.
bdb80 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 54 72 static int os2Tr
bdb90 75 6e 63 61 74 65 28 20 73 71 6c 69 74 65 33 5f uncate( sqlite3_
bdba0 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 file *id, i64 nB
bdbb0 79 74 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 yte ){. APIRET
bdbc0 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 rc = NO_ERROR;.
bdbd0 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 os2File *pFile
bdbe0 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a = (os2File*)id;.
bdbf0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 52 55 OSTRACE3( "TRU
bdc00 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 NCATE %d %lld\n"
bdc10 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 , pFile->h, nByt
bdc20 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 e );. SimulateI
bdc30 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
bdc40 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e QLITE_IOERR_TRUN
bdc50 43 41 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 CATE );. rc = D
bdc60 6f 73 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70 osSetFileSize( p
bdc70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 File->h, nByte )
bdc80 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d ;. return rc ==
bdc90 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 NO_ERROR ? SQLI
bdca0 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 TE_OK : SQLITE_I
bdcb0 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d OERR_TRUNCATE;.}
bdcc0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
bdcd0 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 TEST./*.** Count
bdce0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 the number of f
bdcf0 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 ullsyncs and nor
bdd00 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 mal syncs. This
bdd10 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 is used to test
bdd20 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 .** that syncs a
bdd30 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 nd fullsyncs are
bdd40 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 occuring at the
bdd50 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f right times..*/
bdd60 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
bdd70 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 sqlite3_sync_cou
bdd80 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 nt = 0;.SQLITE_A
bdd90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
bdda0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 ullsync_count =
bddb0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
bddc0 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 Make sure all w
bddd0 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 rites to a parti
bdde0 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 cular file are c
bddf0 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b ommitted to disk
bde00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
bde10 6f 73 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33 os2Sync( sqlite3
bde20 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 _file *id, int f
bde30 6c 61 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c lags ){. os2Fil
bde40 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 e *pFile = (os2F
bde50 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 ile*)id;. OSTRA
bde60 43 45 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f CE3( "SYNC %d lo
bde70 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ck=%d\n", pFile-
bde80 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
bde90 79 70 65 20 29 3b 0a 23 69 66 64 65 66 20 53 51 ype );.#ifdef SQ
bdea0 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 LITE_TEST. if(
bdeb0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 flags & SQLITE_S
bdec0 59 4e 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 YNC_FULL){. s
bded0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f qlite3_fullsync_
bdee0 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 count++;. }. s
bdef0 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e qlite3_sync_coun
bdf00 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 t++;.#endif. re
bdf10 74 75 72 6e 20 44 6f 73 52 65 73 65 74 42 75 66 turn DosResetBuf
bdf20 66 65 72 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 fer( pFile->h )
bdf30 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 == NO_ERROR ? SQ
bdf40 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
bdf50 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _IOERR;.}../*.**
bdf60 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 Determine the c
bdf70 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 urrent size of a
bdf80 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a file in bytes.*
bdf90 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
bdfa0 46 69 6c 65 53 69 7a 65 28 20 73 71 6c 69 74 65 FileSize( sqlite
bdfb0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 3_file *id, sqli
bdfc0 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 te3_int64 *pSize
bdfd0 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 ){. APIRET rc
bdfe0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 = NO_ERROR;. FI
bdff0 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 33 46 LESTATUS3 fsts3F
be000 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65 ileInfo;. memse
be010 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f t(&fsts3FileInfo
be020 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 , 0, sizeof(fsts
be030 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61 3FileInfo));. a
be040 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
be050 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
be060 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
be070 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29 3b 0a _IOERR_FSTAT );.
be080 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46 rc = DosQueryF
be090 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69 ileInfo( ((os2Fi
be0a0 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f le*)id)->h, FIL_
be0b0 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33 STANDARD, &fsts3
be0c0 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 FileInfo, sizeof
be0d0 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b (FILESTATUS3) );
be0e0 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f . if( rc == NO_
be0f0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53 ERROR ){. *pS
be100 69 7a 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49 ize = fsts3FileI
be110 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20 nfo.cbFile;.
be120 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
be130 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
be140 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
be150 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 7d 0a RR_FSTAT;. }.}.
be160 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 ./*.** Acquire a
be170 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2f reader lock..*/
be180 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 .static int getR
be190 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 eadLock( os2File
be1a0 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46 49 4c *pFile ){. FIL
be1b0 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c ELOCK LockArea,
be1c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c . Unl
be1d0 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45 ockArea;. APIRE
be1e0 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28 T res;. memset(
be1f0 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 &LockArea, 0, si
be200 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b zeof(LockArea));
be210 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 . memset(&Unloc
be220 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
be230 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 (UnlockArea));.
be240 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
be250 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 t = SHARED_FIRST
be260 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 ;. LockArea.lRa
be270 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a nge = SHARED_SIZ
be280 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e E;. UnlockArea.
be290 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 lOffset = 0L;.
be2a0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
be2b0 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20 3d 20 e = 0L;. res =
be2c0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
be2d0 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
be2e0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
be2f0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
be300 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 1L );. OSTRACE
be310 33 28 20 22 47 45 54 52 45 41 44 4c 4f 43 4b 20 3( "GETREADLOCK
be320 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 %d res=%d\n", pF
be330 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 ile->h, res );.
be340 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a return res;.}..
be350 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 /*.** Undo a rea
be360 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 dlock.*/.static
be370 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f int unlockReadLo
be380 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 64 20 ck( os2File *id
be390 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c ){. FILELOCK L
be3a0 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 ockArea,.
be3b0 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b UnlockArea;
be3c0 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20 . APIRET res;.
be3d0 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 memset(&LockAre
be3e0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 a, 0, sizeof(Loc
be3f0 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 kArea));. memse
be400 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 t(&UnlockArea, 0
be410 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 , sizeof(UnlockA
be420 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 rea));. LockAre
be430 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
be440 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 LockArea.lRang
be450 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b e = 0L;. Unlock
be460 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 Area.lOffset = S
be470 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 55 HARED_FIRST;. U
be480 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
be490 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a = SHARED_SIZE;.
be4a0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 res = DosSetFi
be4b0 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20 leLocks( id->h,
be4c0 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
be4d0 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d ckArea, LOCK_TIM
be4e0 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 EOUT, 1L );. OS
be4f0 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 2d TRACE3( "UNLOCK-
be500 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20 68 61 READLOCK file ha
be510 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 64 3f 5c ndle=%d res=%d?\
be520 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73 20 29 n", id->h, res )
be530 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a ;. return res;.
be540 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
be550 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
be560 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
be570 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
be580 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
be590 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
be5a0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
be5b0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
be5c0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
be5d0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
be5e0 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
be5f0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
be600 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
be610 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
be620 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
be630 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
be640 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
be650 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
be660 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
be670 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
be680 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
be690 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
be6a0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
be6b0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
be6c0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
be6d0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
be6e0 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
be6f0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
be700 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
be710 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
be720 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
be730 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
be740 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
be750 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
be760 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
be770 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
be780 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
be790 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
be7a0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
be7b0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
be7c0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
be7d0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
be7e0 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
be7f0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
be800 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
be810 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
be820 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c 6f 63 k. The os2Unloc
be830 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 k() routine.** e
be840 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 rases all locks
be850 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 at once and retu
be860 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 rns us immediate
be870 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 ly to locking le
be880 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 vel 0..** It is
be890 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
be8a0 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e lower the lockin
be8b0 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 g level one step
be8c0 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 at a time. You
be8d0 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 .** must go stra
be8e0 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 ight to locking
be8f0 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 level 0..*/.stat
be900 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 28 20 ic int os2Lock(
be910 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
be920 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 , int locktype )
be930 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
be940 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a ITE_OK; /*
be950 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f Return code fro
be960 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f m subroutines */
be970 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 3d 20 . APIRET res =
be980 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20 NO_ERROR; /*
be990 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f Result of an OS/
be9a0 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 2 lock call */.
be9b0 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 int newLocktype
be9c0 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 ; /* Set p
be9d0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 File->locktype t
be9e0 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 o this value bef
be9f0 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 ore exiting */.
bea00 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c int gotPendingL
bea10 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 ock = 0;/* True
bea20 69 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 if we acquired a
bea30 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 PENDING lock th
bea40 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49 4c is time */. FIL
bea50 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c ELOCK LockArea,
bea60 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c . Unl
bea70 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46 69 ockArea;. os2Fi
bea80 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 le *pFile = (os2
bea90 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d 73 File*)id;. mems
beaa0 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c et(&LockArea, 0,
beab0 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 sizeof(LockArea
beac0 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e ));. memset(&Un
bead0 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a lockArea, 0, siz
beae0 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 eof(UnlockArea))
beaf0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c ;. assert( pFil
beb00 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 e!=0 );. OSTRAC
beb10 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 E4( "LOCK %d %d
beb20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 was %d\n", pFile
beb30 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 ->h, locktype, p
beb40 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
beb50 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
beb60 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f is already a lo
beb70 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ck of this type
beb80 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 or more restrict
beb90 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ive on the. **
beba0 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 os2File, do noth
bebb0 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 ing. Don't use t
bebc0 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 he end_lock: exi
bebd0 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 t path, as. **
bebe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
bebf0 74 65 72 28 29 20 68 61 73 6e 27 74 20 62 65 65 ter() hasn't bee
bec00 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 n called yet..
bec10 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
bec20 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 locktype>=lockty
bec30 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 pe ){. OSTRAC
bec40 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 E3( "LOCK %d %d
bec50 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 ok (already held
bec60 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
bec70 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 locktype );.
bec80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
bec90 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
beca0 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e sure the lockin
becb0 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f g sequence is co
becc0 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 rrect. */. ass
becd0 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ert( pFile->lock
bece0 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c type!=NO_LOCK ||
becf0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
bed00 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 D_LOCK );. asse
bed10 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 rt( locktype!=PE
bed20 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 NDING_LOCK );.
bed30 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
bed40 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 !=RESERVED_LOCK
bed50 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 || pFile->lockty
bed60 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
bed70 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 );.. /* Lock th
bed80 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 e PENDING_LOCK b
bed90 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 yte if we need t
beda0 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 o acquire a PEND
bedb0 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a ING lock or. **
bedc0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 a SHARED lock.
bedd0 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69 If we are acqui
bede0 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f ring a SHARED lo
bedf0 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 ck, the acquisit
bee00 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 ion of. ** the
bee10 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 PENDING_LOCK byt
bee20 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a e is temporary..
bee30 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 */. newLockty
bee40 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b pe = pFile->lock
bee50 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c type;. if( pFil
bee60 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f e->locktype==NO_
bee70 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28 6c LOCK. || (l
bee80 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
bee90 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 VE_LOCK && pFile
beea0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 ->locktype==RESE
beeb0 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a RVED_LOCK). ){.
beec0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 LockArea.lOf
beed0 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 fset = PENDING_B
beee0 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 YTE;. LockAre
beef0 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 a.lRange = 1L;.
bef00 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
bef10 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
bef20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
bef30 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a 20 e = 0L;.. /*
bef40 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 61 6e wait longer than
bef50 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68 65 LOCK_TIMEOUT he
bef60 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 20 74 re not to have t
bef70 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 20 74 o try multiple t
bef80 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 73 20 imes */. res
bef90 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
befa0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
befb0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
befc0 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29 3b rea, 100L, 0L );
befd0 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 . if( res ==
befe0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
beff0 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b gotPendingLock
bf000 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 54 52 = 1;. OSTR
bf010 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 70 ACE3( "LOCK %d p
bf020 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c ending lock bool
bf030 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d 25 64 ean set. res=%d
bf040 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
bf050 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a es );. }. }.
bf060 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 . /* Acquire a
bf070 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f shared lock. */
bf080 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
bf090 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 =SHARED_LOCK &&
bf0a0 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
bf0b0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
bf0c0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
bf0d0 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 NO_LOCK );. r
bf0e0 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b es = getReadLock
bf0f0 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 (pFile);. if(
bf100 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 res == NO_ERROR
bf110 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 ){. newLoc
bf120 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c ktype = SHARED_L
bf130 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f OCK;. }. O
bf140 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 STRACE3( "LOCK %
bf150 64 20 61 63 71 75 69 72 65 20 73 68 61 72 65 64 d acquire shared
bf160 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 lock. res=%d\n"
bf170 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
bf180 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 );. }.. /* Acq
bf190 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 uire a RESERVED
bf1a0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
bf1b0 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 locktype==RESERV
bf1c0 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d ED_LOCK && res =
bf1d0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
bf1e0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
bf1f0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 >locktype==SHARE
bf200 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c 6f D_LOCK );. Lo
bf210 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
bf220 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a RESERVED_BYTE;.
bf230 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
bf240 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e nge = 1L;. Un
bf250 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
bf260 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
bf270 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
bf280 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 L;. res = Dos
bf290 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
bf2a0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
bf2b0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
bf2c0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
bf2d0 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 );. if( res
bf2e0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
bf2f0 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 newLocktype
bf300 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b = RESERVED_LOCK
bf310 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 ;. }. OSTR
bf320 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 ACE3( "LOCK %d a
bf330 63 71 75 69 72 65 20 72 65 73 65 72 76 65 64 20 cquire reserved
bf340 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c lock. res=%d\n",
bf350 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 pFile->h, res )
bf360 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
bf370 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f ire a PENDING lo
bf380 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f ck. */. if( lo
bf390 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
bf3a0 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d E_LOCK && res ==
bf3b0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
bf3c0 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 newLocktype = P
bf3d0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 ENDING_LOCK;.
bf3e0 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 gotPendingLock
bf3f0 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 = 0;. OSTRACE
bf400 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 2( "LOCK %d acqu
bf410 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b ire pending lock
bf420 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 . pending lock b
bf430 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22 oolean unset.\n"
bf440 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 , pFile->h );.
bf450 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
bf460 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
bf470 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 k. */. if( loc
bf480 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
bf490 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 _LOCK && res ==
bf4a0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
bf4b0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
bf4c0 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f ocktype>=SHARED_
bf4d0 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 LOCK );. res
bf4e0 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b = unlockReadLock
bf4f0 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 (pFile);. OST
bf500 52 41 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f RACE2( "unreadlo
bf510 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 ck = %d\n", res
bf520 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e );. LockArea.
bf530 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 lOffset = SHARED
bf540 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b _FIRST;. Lock
bf550 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 Area.lRange = SH
bf560 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55 ARED_SIZE;. U
bf570 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 nlockArea.lOffse
bf580 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f t = 0L;. Unlo
bf590 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
bf5a0 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 0L;. res = Do
bf5b0 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 sSetFileLocks( p
bf5c0 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b File->h, &Unlock
bf5d0 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c Area, &LockArea,
bf5e0 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 LOCK_TIMEOUT, 0
bf5f0 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 L );. if( res
bf600 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
bf610 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 newLocktyp
bf620 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f e = EXCLUSIVE_LO
bf630 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 CK;. }else{.
bf640 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 OSTRACE2( "
bf650 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65 20 OS/2 error-code
bf660 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a = %d\n", res );.
bf670 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 getReadLoc
bf680 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a k(pFile);. }.
bf690 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c OSTRACE3( "L
bf6a0 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 65 OCK %d acquire e
bf6b0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 xclusive lock.
bf6c0 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 res=%d\n", pFile
bf6d0 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a ->h, res );. }.
bf6e0 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 . /* If we are
bf6f0 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e holding a PENDIN
bf700 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 G lock that ough
bf710 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 t to be released
bf720 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 , then. ** rele
bf730 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f ase it now.. */
bf740 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e . if( gotPendin
bf750 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 gLock && locktyp
bf760 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
bf770 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 {. int r;.
bf780 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
bf790 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b t = 0L;. Lock
bf7a0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
bf7b0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
bf7c0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 .lOffset = PENDI
bf7d0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c NG_BYTE;. Unl
bf7e0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
bf7f0 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f 73 1L;. r = Dos
bf800 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
bf810 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
bf820 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
bf830 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
bf840 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 );. OSTRACE3
bf850 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 ( "LOCK %d unloc
bf860 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73 20 king pending/is
bf870 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22 2c shared. r=%d\n",
bf880 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a pFile->h, r );.
bf890 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 }.. /* Update
bf8a0 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
bf8b0 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 e lock has held
bf8c0 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 in the file desc
bf8d0 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a riptor then. **
bf8e0 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 return the appr
bf8f0 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 opriate result c
bf900 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ode.. */. if(
bf910 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
bf920 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
bf930 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
bf940 20 20 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c OSTRACE4( "L
bf950 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 OCK FAILED %d tr
bf960 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 ying for %d but
bf970 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 got %d\n", pFile
bf980 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
bf990 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 locktype, new
bf9a0 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 Locktype );.
bf9b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
bf9c0 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c ;. }. pFile->l
bf9d0 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 ocktype = newLoc
bf9e0 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 ktype;. OSTRACE
bf9f0 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 3( "LOCK %d now
bfa00 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
bfa10 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
bfa20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
bfa30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
bfa40 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 outine checks if
bfa50 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 there is a RESE
bfa60 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f RVED lock held o
bfa70 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a n the specified.
bfa80 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 ** file by this
bfa90 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f or any other pro
bfaa0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 cess. If such a
bfab0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 lock is held, re
bfac0 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f turn.** non-zero
bfad0 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f , otherwise zero
bfae0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
bfaf0 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 os2CheckReserved
bfb00 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 Lock( sqlite3_fi
bfb10 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75 le *id, int *pOu
bfb20 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 t ){. int r = 0
bfb30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ;. os2File *pFi
bfb40 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
bfb50 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 d;. assert( pFi
bfb60 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 le!=0 );. if( p
bfb70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d File->locktype>=
bfb80 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b RESERVED_LOCK ){
bfb90 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20 . r = 1;.
bfba0 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 OSTRACE3( "TEST
bfbb0 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c WR-LOCK %d %d (l
bfbc0 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d ocal)\n", pFile-
bfbd0 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65 >h, r );. }else
bfbe0 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20 {. FILELOCK
bfbf0 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 LockArea,.
bfc00 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 UnlockAr
bfc10 65 61 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72 ea;. APIRET r
bfc20 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
bfc30 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 memset(&LockAr
bfc40 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f ea, 0, sizeof(Lo
bfc50 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65 ckArea));. me
bfc60 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 mset(&UnlockArea
bfc70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f , 0, sizeof(Unlo
bfc80 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f ckArea));. Lo
bfc90 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
bfca0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a RESERVED_BYTE;.
bfcb0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
bfcc0 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e nge = 1L;. Un
bfcd0 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
bfce0 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
bfcf0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
bfd00 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53 L;. rc = DosS
bfd10 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
bfd20 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
bfd30 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
bfd40 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 OCK_TIMEOUT, 0L
bfd50 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 );. OSTRACE3(
bfd60 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 "TEST WR-LOCK %
bfd70 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 d lock reserved
bfd80 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70 byte rc=%d\n", p
bfd90 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 File->h, rc );.
bfda0 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f if( rc == NO_
bfdb0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41 ERROR ){. A
bfdc0 50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45 PIRET rcu = NO_E
bfdd0 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20 RROR; /* return
bfde0 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69 code for unlocki
bfdf0 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b ng */. Lock
bfe00 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 Area.lOffset = 0
bfe10 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 L;. LockAre
bfe20 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
bfe30 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e UnlockArea.
bfe40 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 lOffset = RESERV
bfe50 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55 ED_BYTE;. U
bfe60 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
bfe70 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75 = 1L;. rcu
bfe80 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 = DosSetFileLoc
bfe90 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 ks( pFile->h, &U
bfea0 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b nlockArea, &Lock
bfeb0 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f Area, LOCK_TIMEO
bfec0 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20 UT, 0L );.
bfed0 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 OSTRACE3( "TEST
bfee0 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 WR-LOCK %d unloc
bfef0 6b 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20 k reserved byte
bff00 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e r=%d\n", pFile->
bff10 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a h, rcu );. }.
bff20 20 20 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20 r = !(rc ==
bff30 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f NO_ERROR);. O
bff40 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 STRACE3( "TEST W
bff50 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 R-LOCK %d %d (re
bff60 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d mote)\n", pFile-
bff70 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 2a >h, r );. }. *
bff80 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65 74 75 pOut = r;. retu
bff90 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
bffa0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 ./*.** Lower the
bffb0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
bffc0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
bffd0 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 r id to locktype
bffe0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
bfff0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
c0000 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
c0010 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
c0020 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
c0030 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
c0040 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
c0050 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
c0060 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
c0070 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
c0080 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
c0090 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 no-op..**.** It
c00a0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
c00b0 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e for this routin
c00c0 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 e to fail if the
c00d0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
c00e0 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 .** is NO_LOCK.
c00f0 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 If the second a
c0100 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 rgument is SHARE
c0110 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 D_LOCK then this
c0120 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 routine.** migh
c0130 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f t return SQLITE_
c0140 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 IOERR;.*/.static
c0150 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20 int os2Unlock(
c0160 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
c0170 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 , int locktype )
c0180 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 {. int type;.
c0190 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d os2File *pFile =
c01a0 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 (os2File*)id;.
c01b0 20 41 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c APIRET rc = SQL
c01c0 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 45 54 ITE_OK;. APIRET
c01d0 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b res = NO_ERROR;
c01e0 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 . FILELOCK Loc
c01f0 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 kArea,.
c0200 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 UnlockArea;.
c0210 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 memset(&LockAre
c0220 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 a, 0, sizeof(Loc
c0230 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 kArea));. memse
c0240 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 t(&UnlockArea, 0
c0250 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 , sizeof(UnlockA
c0260 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 rea));. assert(
c0270 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 pFile!=0 );. a
c0280 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
c0290 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
c02a0 20 20 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c OSTRACE4( "UNL
c02b0 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 OCK %d to %d was
c02c0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
c02d0 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c , locktype, pFil
c02e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
c02f0 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c type = pFile->l
c0300 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 ocktype;. if( t
c0310 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype>=EXCLUSIVE_L
c0320 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 OCK ){. LockA
c0330 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
c0340 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
c0350 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 Range = 0L;.
c0360 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
c0370 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 et = SHARED_FIRS
c0380 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 T;. UnlockAre
c0390 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 a.lRange = SHARE
c03a0 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65 73 20 D_SIZE;. res
c03b0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
c03c0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
c03d0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
c03e0 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
c03f0 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 T, 0L );. OST
c0400 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 RACE3( "UNLOCK %
c0410 64 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b d exclusive lock
c0420 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
c0430 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 e->h, res );.
c0440 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
c0450 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65 HARED_LOCK && ge
c0460 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 tReadLock(pFile)
c0470 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a != NO_ERROR ){.
c0480 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 /* This sh
c0490 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 ould never happe
c04a0 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c n. We should al
c04b0 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a ways be able to.
c04c0 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 ** reacqui
c04d0 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b re the read lock
c04e0 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 */. OSTRAC
c04f0 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74 E3( "UNLOCK %d t
c0500 6f 20 25 64 20 67 65 74 52 65 61 64 4c 6f 63 6b o %d getReadLock
c0510 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20 70 46 () failed\n", pF
c0520 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
c0530 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 );. rc = S
c0540 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
c0550 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 CK;. }. }.
c0560 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 if( type>=RESERV
c0570 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c ED_LOCK ){. L
c0580 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c0590 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 = 0L;. LockAr
c05a0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a ea.lRange = 0L;.
c05b0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c05c0 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 Offset = RESERVE
c05d0 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f D_BYTE;. Unlo
c05e0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c05f0 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 1L;. res = Do
c0600 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 sSetFileLocks( p
c0610 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b File->h, &Unlock
c0620 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c Area, &LockArea,
c0630 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 LOCK_TIMEOUT, 0
c0640 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 L );. OSTRACE
c0650 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 72 65 3( "UNLOCK %d re
c0660 73 65 72 76 65 64 20 72 65 73 3d 25 64 5c 6e 22 served res=%d\n"
c0670 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
c0680 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 );. }. if( loc
c0690 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 ktype==NO_LOCK &
c06a0 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c & type>=SHARED_L
c06b0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d OCK ){. res =
c06c0 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 unlockReadLock(
c06d0 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 pFile);. OSTR
c06e0 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE5( "UNLOCK %d
c06f0 20 69 73 20 25 64 20 77 61 6e 74 20 25 64 20 72 is %d want %d r
c0700 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d es=%d\n", pFile-
c0710 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b 74 79 >h, type, lockty
c0720 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 pe, res );. }.
c0730 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 if( type>=PENDI
c0740 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c NG_LOCK ){. L
c0750 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c0760 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 = 0L;. LockAr
c0770 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a ea.lRange = 0L;.
c0780 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c0790 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 Offset = PENDING
c07a0 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 _BYTE;. Unloc
c07b0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 kArea.lRange = 1
c07c0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 L;. res = Dos
c07d0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
c07e0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
c07f0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
c0800 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
c0810 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 );. OSTRACE3
c0820 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70 65 6e ( "UNLOCK %d pen
c0830 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22 2c 20 ding res=%d\n",
c0840 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
c0850 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f . }. pFile->lo
c0860 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
c0870 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 e;. OSTRACE3( "
c0880 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 UNLOCK %d now %d
c0890 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
c08a0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
c08b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
c08c0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 ../*.** Control
c08d0 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 and query of the
c08e0 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c open file handl
c08f0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
c0900 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 os2FileControl(
c0910 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
c0920 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a , int op, void *
c0930 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 pArg){. switch(
c0940 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
c0950 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 SQLITE_FCNTL_LOC
c0960 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 KSTATE: {.
c0970 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 *(int*)pArg = ((
c0980 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f os2File*)id)->lo
c0990 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 4f 53 cktype;. OS
c09a0 54 52 41 43 45 33 28 20 22 46 43 4e 54 4c 5f 4c TRACE3( "FCNTL_L
c09b0 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f 63 6b OCKSTATE %d lock
c09c0 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c =%d\n", ((os2Fil
c09d0 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32 e*)id)->h, ((os2
c09e0 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 File*)id)->lockt
c09f0 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 65 74 ype );. ret
c0a00 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
c0a10 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
c0a20 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
c0a30 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
c0a40 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
c0a50 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 in bytes of the
c0a60 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b underlying block
c0a70 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 device for.** t
c0a80 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c he specified fil
c0a90 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 e. This is almos
c0aa0 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 t always 512 byt
c0ab0 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a es, but may be.*
c0ac0 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d * larger for som
c0ad0 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a e devices..**.**
c0ae0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 SQLite code ass
c0af0 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 umes this functi
c0b00 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 on cannot fail.
c0b10 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 It also assumes
c0b20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 that.** if two f
c0b30 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 iles are created
c0b40 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c in the same fil
c0b50 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f e-system directo
c0b60 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 ry (i.e..** a da
c0b70 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a tabase and its j
c0b80 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 ournal file) tha
c0b90 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a t the sector siz
c0ba0 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a e will be the.**
c0bb0 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a same for both..
c0bc0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c0bd0 32 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 2SectorSize(sqli
c0be0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
c0bf0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
c0c00 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 EFAULT_SECTOR_SI
c0c10 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ZE;.}../*.** Ret
c0c20 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 urn a vector of
c0c30 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 device character
c0c40 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 istics..*/.stati
c0c50 63 20 69 6e 74 20 6f 73 32 44 65 76 69 63 65 43 c int os2DeviceC
c0c60 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 haracteristics(s
c0c70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
c0c80 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a {. return 0;.}.
c0c90 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 ../*.** Characte
c0ca0 72 20 73 65 74 20 63 6f 6e 76 65 72 73 69 6f 6e r set conversion
c0cb0 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 objects used by
c0cc0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f 75 74 conversion rout
c0cd0 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ines..*/.static
c0ce0 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 55 74 UconvObject ucUt
c0cf0 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 63 6f f8 = NULL; /* co
c0d00 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20 55 54 nvert between UT
c0d10 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f F-8 and UCS-2 */
c0d20 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a .static UconvObj
c0d30 65 63 74 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c ect uclCp = NULL
c0d40 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 ; /* convert be
c0d50 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f 64 65 tween local code
c0d60 70 61 67 65 20 61 6e 64 20 55 43 53 2d 32 20 2a page and UCS-2 *
c0d70 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 /../*.** Helper
c0d80 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 69 74 function to init
c0d90 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 76 65 ialize the conve
c0da0 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72 rsion objects fr
c0db0 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e om and to UTF-8.
c0dc0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
c0dd0 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 initUconvObjects
c0de0 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 28 20 ( void ){. if(
c0df0 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 UniCreateUconvOb
c0e00 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63 ject( UTF_8, &uc
c0e10 55 74 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55 Utf8 ) != ULS_SU
c0e20 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 55 74 CCESS ). ucUt
c0e30 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66 20 f8 = NULL;. if
c0e40 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 ( UniCreateUconv
c0e50 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72 Object( (UniChar
c0e60 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c *)L"@path=yes",
c0e70 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53 &uclCp ) != ULS
c0e80 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 _SUCCESS ). u
c0e90 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a clCp = NULL;.}..
c0ea0 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e /*.** Helper fun
c0eb0 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 20 74 68 ction to free th
c0ec0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a e conversion obj
c0ed0 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f ects from and to
c0ee0 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 UTF-8..*/.stati
c0ef0 63 20 76 6f 69 64 20 66 72 65 65 55 63 6f 6e 76 c void freeUconv
c0f00 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b Objects( void ){
c0f10 0a 20 20 69 66 20 28 20 75 63 55 74 66 38 20 29 . if ( ucUtf8 )
c0f20 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e . UniFreeUcon
c0f30 76 4f 62 6a 65 63 74 28 20 75 63 55 74 66 38 20 vObject( ucUtf8
c0f40 29 3b 0a 20 20 69 66 20 28 20 75 63 6c 43 70 20 );. if ( uclCp
c0f50 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f ). UniFreeUco
c0f60 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c 43 70 20 nvObject( uclCp
c0f70 29 3b 0a 20 20 75 63 55 74 66 38 20 3d 20 4e 55 );. ucUtf8 = NU
c0f80 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55 LL;. uclCp = NU
c0f90 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c LL;.}../*.** Hel
c0fa0 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 per function to
c0fb0 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 66 69 convert UTF-8 fi
c0fc0 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c lenames to local
c0fd0 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a OS/2 codepage..
c0fe0 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 ** The two-step
c0ff0 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 process: first c
c1000 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d onvert the incom
c1010 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67 ing UTF-8 string
c1020 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 .** into UCS-2 a
c1030 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 nd then from UCS
c1040 2d 32 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e -2 to the curren
c1050 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 t codepage..** T
c1060 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 he returned char
c1070 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 pointer has to
c1080 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 be freed..*/.sta
c1090 74 69 63 20 63 68 61 72 20 2a 63 6f 6e 76 65 72 tic char *conver
c10a0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 63 tUtf8PathToCp( c
c10b0 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b onst char *in ){
c10c0 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50 . UniChar tempP
c10d0 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b ath[CCHMAXPATH];
c10e0 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28 . char *out = (
c10f0 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43 char *)calloc( C
c1100 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a CHMAXPATH, 1 );.
c1110 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20 . if( !out ).
c1120 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a return NULL;..
c1130 20 20 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c if( !ucUtf8 ||
c1140 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e !uclCp ). in
c1150 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 itUconvObjects()
c1160 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e ;.. /* determin
c1170 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 e string for the
c1180 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 conversion of U
c1190 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 TF-8 which is CP
c11a0 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 20 55 6e 1208 */. if( Un
c11b0 69 53 74 72 54 6f 55 63 73 28 20 75 63 55 74 66 iStrToUcs( ucUtf
c11c0 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 8, tempPath, (ch
c11d0 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 ar *)in, CCHMAXP
c11e0 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 ATH ) != ULS_SUC
c11f0 43 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 CESS ). retur
c1200 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e n out; /* if con
c1210 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 version fails, r
c1220 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 eturn the empty
c1230 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 string */.. /*
c1240 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 conversion for c
c1250 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 urrent codepage
c1260 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 which can be use
c1270 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 d for paths */.
c1280 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20 UniStrFromUcs(
c1290 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65 6d 70 uclCp, out, temp
c12a0 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48 Path, CCHMAXPATH
c12b0 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75 );.. return ou
c12c0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 t;.}../*.** Help
c12d0 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 er function to c
c12e0 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d 65 73 onvert filenames
c12f0 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f 64 65 from local code
c1300 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e 0a 2a page to UTF-8..*
c1310 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 * The two-step p
c1320 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f rocess: first co
c1330 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 nvert the incomi
c1340 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70 65 63 ng codepage-spec
c1350 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67 20 69 ific.** string i
c1360 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 nto UCS-2 and th
c1370 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f en from UCS-2 to
c1380 20 74 68 65 20 63 6f 64 65 70 61 67 65 20 6f 66 the codepage of
c1390 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 72 UTF-8..** The r
c13a0 65 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 eturned char poi
c13b0 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 nter has to be f
c13c0 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 reed..**.** This
c13d0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e function is non
c13e0 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20 61 62 -static to be ab
c13f0 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 le to use this i
c1400 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a 2a 2a n shell.c and.**
c1410 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 63 61 similar applica
c1420 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 tions that take
c1430 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72 67 command line arg
c1440 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 72 20 uments..*/.char
c1450 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f *convertCpPathTo
c1460 55 74 66 38 28 20 63 6f 6e 73 74 20 63 68 61 72 Utf8( const char
c1470 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 *in ){. UniCha
c1480 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 r tempPath[CCHMA
c1490 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a XPATH];. char *
c14a0 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61 out = (char *)ca
c14b0 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48 lloc( CCHMAXPATH
c14c0 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f , 1 );.. if( !o
c14d0 75 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 ut ). return
c14e0 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 NULL;.. if( !uc
c14f0 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 Utf8 || !uclCp )
c1500 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 . initUconvOb
c1510 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 63 jects();.. /* c
c1520 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 onversion for cu
c1530 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 rrent codepage w
c1540 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 hich can be used
c1550 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 for paths */.
c1560 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 if( UniStrToUcs(
c1570 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68 uclCp, tempPath
c1580 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 , (char *)in, CC
c1590 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c HMAXPATH ) != UL
c15a0 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 S_SUCCESS ).
c15b0 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 return out; /* i
c15c0 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 f conversion fai
c15d0 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 ls, return the e
c15e0 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a mpty string */..
c15f0 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 /* determine s
c1600 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f tring for the co
c1610 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d nversion of UTF-
c1620 38 20 77 68 69 63 68 20 69 73 20 43 50 31 32 30 8 which is CP120
c1630 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 8 */. UniStrFro
c1640 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20 6f 75 mUcs( ucUtf8, ou
c1650 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48 t, tempPath, CCH
c1660 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 MAXPATH );.. re
c1670 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a turn out;.}../*.
c1680 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 ** This vector d
c1690 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d efines all the m
c16a0 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 ethods that can
c16b0 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a operate on an.**
c16c0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f sqlite3_file fo
c16d0 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 r os2..*/.static
c16e0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
c16f0 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49 6f 4d o_methods os2IoM
c1700 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 ethod = {. 1,
c1710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1720 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
c1730 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 65 2c n */. os2Close,
c1740 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 20 6f 73 . os2Read,. os
c1750 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 54 72 75 2Write,. os2Tru
c1760 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79 6e 63 ncate,. os2Sync
c1770 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a 65 2c ,. os2FileSize,
c1780 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73 . os2Lock,. os
c1790 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 43 68 2Unlock,. os2Ch
c17a0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c eckReservedLock,
c17b0 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f . os2FileContro
c17c0 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72 53 69 l,. os2SectorSi
c17d0 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 63 65 43 ze,. os2DeviceC
c17e0 68 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d haracteristics.}
c17f0 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;../************
c1800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
c1840 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 ** Here ends the
c1850 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 I/O methods tha
c1860 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 t form the sqlit
c1870 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
c1880 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ject..**.** The
c1890 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f next block of co
c18a0 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 de implements th
c18b0 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a e VFS methods..*
c18c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c18d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c18e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c18f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
c1910 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d .** Create a tem
c1920 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 porary file name
c1930 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 in zBuf. zBuf
c1940 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 must be big enou
c1950 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 gh to.** hold at
c1960 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
c1970 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f e characters..*/
c1980 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 .static int getT
c1990 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 empname(int nBuf
c19a0 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a , char *zBuf ){.
c19b0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
c19c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 nsigned char zCh
c19d0 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 ars[] =. "abc
c19e0 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 defghijklmnopqrs
c19f0 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 tuvwxyz". "AB
c1a00 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 CDEFGHIJKLMNOPQR
c1a10 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 STUVWXYZ". "0
c1a20 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 123456789";. in
c1a30 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a t i, j;. char z
c1a40 54 65 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a TempPathBuf[3];.
c1a50 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20 PSZ zTempPath
c1a60 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74 = (PSZ)&zTempPat
c1a70 68 42 75 66 3b 0a 20 20 69 66 28 20 73 71 6c 69 hBuf;. if( sqli
c1a80 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f te3_temp_directo
c1a90 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d 70 50 ry ){. zTempP
c1aa0 61 74 68 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 ath = sqlite3_te
c1ab0 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 mp_directory;.
c1ac0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 44 }else{. if( D
c1ad0 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 osScanEnv( (PSZ)
c1ae0 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 "TEMP", &zTempPa
c1af0 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 69 66 th ) ){. if
c1b00 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 ( DosScanEnv( (P
c1b10 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70 SZ)"TMP", &zTemp
c1b20 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 Path ) ){.
c1b30 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 if( DosScanEnv
c1b40 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c ( (PSZ)"TMPDIR",
c1b50 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b &zTempPath ) ){
c1b60 0a 20 20 20 20 20 20 20 20 20 20 20 55 4c 4f 4e . ULON
c1b70 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30 G ulDriveNum = 0
c1b80 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 , ulDriveMap = 0
c1b90 3b 0a 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 ;. Dos
c1ba0 51 75 65 72 79 43 75 72 72 65 6e 74 44 69 73 6b QueryCurrentDisk
c1bb0 28 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 ( &ulDriveNum, &
c1bc0 75 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 ulDriveMap );.
c1bd0 20 20 20 20 20 20 20 20 20 73 70 72 69 6e 74 66 sprintf
c1be0 28 20 28 63 68 61 72 2a 29 7a 54 65 6d 70 50 61 ( (char*)zTempPa
c1bf0 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 68 61 72 th, "%c:", (char
c1c00 29 28 20 27 41 27 20 2b 20 75 6c 44 72 69 76 65 )( 'A' + ulDrive
c1c10 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20 20 20 Num - 1 ) );.
c1c20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
c1c30 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 53 74 }. }. /* St
c1c40 72 69 70 20 6f 66 66 20 61 20 74 72 61 69 6c 69 rip off a traili
c1c50 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 20 62 61 ng slashes or ba
c1c60 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68 65 72 ckslashes, other
c1c70 77 69 73 65 20 77 65 20 77 6f 75 6c 64 20 67 65 wise we would ge
c1c80 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 70 6c t *. * multipl
c1c90 65 20 28 62 61 63 6b 29 73 6c 61 73 68 65 73 20 e (back)slashes
c1ca0 77 68 69 63 68 20 63 61 75 73 65 73 20 44 6f 73 which causes Dos
c1cb0 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 6c 2e 20 Open() to fail.
c1cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 0a 20 *.
c1cd0 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 73 70 61 * Trailing spa
c1ce0 63 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f ces are not allo
c1cf0 77 65 64 2c 20 65 69 74 68 65 72 2e 20 20 20 20 wed, either.
c1d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1d10 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a 20 3d */. j =
c1d20 20 73 74 72 6c 65 6e 28 7a 54 65 6d 70 50 61 74 strlen(zTempPat
c1d30 68 29 3b 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e h);. while( j >
c1d40 20 30 20 26 26 20 28 20 7a 54 65 6d 70 50 61 74 0 && ( zTempPat
c1d50 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c h[j-1] == '\\' |
c1d60 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d | zTempPath[j-1]
c1d70 20 3d 3d 20 27 2f 27 0a 20 20 20 20 20 20 20 20 == '/'.
c1d80 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 7a || z
c1d90 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d TempPath[j-1] ==
c1da0 20 27 20 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d ' ' ) ){. j-
c1db0 2d 3b 0a 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 -;. }. zTempPa
c1dc0 74 68 5b 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 th[j] = '\0';.
c1dd0 69 66 28 20 21 73 71 6c 69 74 65 33 5f 74 65 6d if( !sqlite3_tem
c1de0 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 p_directory ){.
c1df0 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 char *zTempPa
c1e00 74 68 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 thUTF = convertC
c1e10 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 54 65 pPathToUtf8( zTe
c1e20 6d 70 50 61 74 68 20 29 3b 0a 20 20 20 20 73 71 mpPath );. sq
c1e30 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 lite3_snprintf(
c1e40 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 nBuf-30, zBuf,.
c1e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1e60 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 "%s\\"SQLIT
c1e70 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
c1e80 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 55 54 46 IX, zTempPathUTF
c1e90 20 29 3b 0a 20 20 20 20 66 72 65 65 28 20 7a 54 );. free( zT
c1ea0 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20 empPathUTF );.
c1eb0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
c1ec0 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 e3_snprintf( nBu
c1ed0 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 f-30, zBuf,.
c1ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1ef0 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 "%s\\"SQLITE_T
c1f00 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c EMP_FILE_PREFIX,
c1f10 20 7a 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 zTempPath );.
c1f20 7d 0a 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28 20 }. j = strlen(
c1f30 7a 42 75 66 20 29 3b 0a 20 20 73 71 6c 69 74 65 zBuf );. sqlite
c1f40 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 20 32 30 3_randomness( 20
c1f50 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20 20 , &zBuf[j] );.
c1f60 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 for( i = 0; i <
c1f70 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a 20; i++, j++ ){.
c1f80 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 zBuf[j] = (c
c1f90 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e har)zChars[ ((un
c1fa0 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 signed char)zBuf
c1fb0 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 [j])%(sizeof(zCh
c1fc0 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 ars)-1) ];. }.
c1fd0 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 zBuf[j] = 0;.
c1fe0 4f 53 54 52 41 43 45 32 28 20 22 54 45 4d 50 20 OSTRACE2( "TEMP
c1ff0 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c FILENAME: %s\n",
c2000 20 7a 42 75 66 20 29 3b 0a 20 20 72 65 74 75 72 zBuf );. retur
c2010 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
c2020 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 ./*.** Turn a re
c2030 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 lative pathname
c2040 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 into a full path
c2050 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68 65 name. Write the
c2060 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d full.** pathnam
c2070 65 20 69 6e 74 6f 20 7a 46 75 6c 6c 5b 5d 2e 20 e into zFull[].
c2080 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c 6c 20 62 65 zFull[] will be
c2090 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e at least pVfs->
c20a0 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 mxPathname.** by
c20b0 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a tes in size..*/.
c20c0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 75 static int os2Fu
c20d0 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 llPathname(. sq
c20e0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
c20f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
c2100 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 nter to vfs obje
c2110 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ct */. const ch
c2120 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 ar *zRelative,
c2130 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 /* Possibly
c2140 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 relative input p
c2150 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 ath */. int nFu
c2160 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
c2170 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
c2180 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 6e output buffer in
c2190 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 bytes */. char
c21a0 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 20 *zFull
c21b0 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 /* Outpu
c21c0 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 t buffer */.){.
c21d0 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 char *zRelative
c21e0 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 Cp = convertUtf8
c21f0 50 61 74 68 54 6f 43 70 28 20 7a 52 65 6c 61 74 PathToCp( zRelat
c2200 69 76 65 20 29 3b 0a 20 20 63 68 61 72 20 7a 46 ive );. char zF
c2210 75 6c 6c 43 70 5b 43 43 48 4d 41 58 50 41 54 48 ullCp[CCHMAXPATH
c2220 5d 20 3d 20 22 5c 30 22 3b 0a 20 20 63 68 61 72 ] = "\0";. char
c2230 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a 20 20 41 50 *zFullUTF;. AP
c2240 49 52 45 54 20 72 63 20 3d 20 44 6f 73 51 75 65 IRET rc = DosQue
c2250 72 79 50 61 74 68 49 6e 66 6f 28 20 7a 52 65 6c ryPathInfo( zRel
c2260 61 74 69 76 65 43 70 2c 20 46 49 4c 5f 51 55 45 ativeCp, FIL_QUE
c2270 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75 6c RYFULLNAME, zFul
c2280 6c 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 lCp,.
c2290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c22a0 20 20 20 20 20 43 43 48 4d 41 58 50 41 54 48 20 CCHMAXPATH
c22b0 29 3b 0a 20 20 66 72 65 65 28 20 7a 52 65 6c 61 );. free( zRela
c22c0 74 69 76 65 43 70 20 29 3b 0a 20 20 7a 46 75 6c tiveCp );. zFul
c22d0 6c 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 lUTF = convertCp
c22e0 50 61 74 68 54 6f 55 74 66 38 28 20 7a 46 75 6c PathToUtf8( zFul
c22f0 6c 43 70 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 lCp );. sqlite3
c2300 5f 73 6e 70 72 69 6e 74 66 28 20 6e 46 75 6c 6c _snprintf( nFull
c2310 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54 , zFull, zFullUT
c2320 46 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 75 F );. free( zFu
c2330 6c 6c 55 54 46 20 29 3b 0a 20 20 72 65 74 75 72 llUTF );. retur
c2340 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 n rc == NO_ERROR
c2350 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
c2360 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a QLITE_IOERR;.}..
c2370 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 ./*.** Open a fi
c2380 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
c2390 74 20 6f 73 32 4f 70 65 6e 28 0a 20 20 73 71 6c t os2Open(. sql
c23a0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
c23b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f /* No
c23c0 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 t used */. cons
c23d0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 t char *zName,
c23e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
c23f0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f e of the file */
c2400 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
c2410 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 *id,
c2420 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 /* Write the SQ
c2430 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 Lite file handle
c2440 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 here */. int f
c2450 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
c2460 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e /* Open
c2470 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 mode flags */.
c2480 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 int *pOutFlags
c2490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c24a0 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20 * Status return
c24b0 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 46 flags */.){. HF
c24c0 49 4c 45 20 68 3b 0a 20 20 55 4c 4f 4e 47 20 75 ILE h;. ULONG u
c24d0 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d lFileAttribute =
c24e0 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 FILE_NORMAL;.
c24f0 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 67 ULONG ulOpenFlag
c2500 73 20 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47 20 75 s = 0;. ULONG u
c2510 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20 lOpenMode = 0;.
c2520 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 os2File *pFile
c2530 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a = (os2File*)id;.
c2540 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f APIRET rc = NO
c2550 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 _ERROR;. ULONG
c2560 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 63 68 61 72 ulAction;. char
c2570 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20 20 63 68 61 *zNameCp;. cha
c2580 72 20 7a 54 6d 70 6e 61 6d 65 5b 43 43 48 4d 41 r zTmpname[CCHMA
c2590 58 50 41 54 48 2b 31 5d 3b 20 20 20 20 2f 2a 20 XPATH+1]; /*
c25a0 42 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 6e Buffer to hold n
c25b0 61 6d 65 20 6f 66 20 74 65 6d 70 20 66 69 6c 65 ame of temp file
c25c0 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 */.. /* If the
c25d0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
c25e0 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
c25f0 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 n is NULL, gener
c2600 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 ate a . ** temp
c2610 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 orary file name
c2620 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 to use . */. i
c2630 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 f( !zName ){.
c2640 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d int rc = getTem
c2650 70 6e 61 6d 65 28 43 43 48 4d 41 58 50 41 54 48 pname(CCHMAXPATH
c2660 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 +1, zTmpname);.
c2670 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
c2680 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
c2690 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
c26a0 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e zName = zTmpn
c26b0 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20 20 6d 65 6d ame;. }... mem
c26c0 73 65 74 28 20 70 46 69 6c 65 2c 20 30 2c 20 73 set( pFile, 0, s
c26d0 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 20 29 3b izeof(*pFile) );
c26e0 0a 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f .. OSTRACE2( "O
c26f0 50 45 4e 20 77 61 6e 74 20 25 64 5c 6e 22 2c 20 PEN want %d\n",
c2700 66 6c 61 67 73 20 29 3b 0a 0a 20 20 69 66 28 20 flags );.. if(
c2710 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
c2720 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b PEN_READWRITE ){
c2730 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 . ulOpenMode
c2740 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 |= OPEN_ACCESS_R
c2750 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 EADWRITE;. OS
c2760 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 72 65 TRACE1( "OPEN re
c2770 61 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 ad/write\n" );.
c2780 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 }else{. ulOp
c2790 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 enMode |= OPEN_A
c27a0 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a CCESS_READONLY;.
c27b0 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f OSTRACE1( "O
c27c0 50 45 4e 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 PEN read only\n"
c27d0 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 );. }.. if( f
c27e0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
c27f0 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 20 EN_CREATE ){.
c2800 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 ulOpenFlags |=
c2810 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e OPEN_ACTION_OPEN
c2820 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 _IF_EXISTS | OPE
c2830 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f N_ACTION_CREATE_
c2840 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 IF_NEW;. OSTR
c2850 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e ACE1( "OPEN open
c2860 20 6e 65 77 2f 63 72 65 61 74 65 5c 6e 22 20 29 new/create\n" )
c2870 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 ;. }else{. u
c2880 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 lOpenFlags |= OP
c2890 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 EN_ACTION_OPEN_I
c28a0 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f F_EXISTS | OPEN_
c28b0 41 43 54 49 4f 4e 5f 46 41 49 4c 5f 49 46 5f 4e ACTION_FAIL_IF_N
c28c0 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 EW;. OSTRACE1
c28d0 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 65 78 69 ( "OPEN open exi
c28e0 73 74 69 6e 67 5c 6e 22 20 29 3b 0a 20 20 7d 0a sting\n" );. }.
c28f0 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 . if( flags & S
c2900 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
c2910 44 42 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e DB ){. ulOpen
c2920 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 Mode |= OPEN_SHA
c2930 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20 RE_DENYNONE;.
c2940 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
c2950 20 73 68 61 72 65 20 72 65 61 64 2f 77 72 69 74 share read/writ
c2960 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b e\n" );. }else{
c2970 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 . ulOpenMode
c2980 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 |= OPEN_SHARE_DE
c2990 4e 59 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 NYWRITE;. OST
c29a0 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 68 61 RACE1( "OPEN sha
c29b0 72 65 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 re read only\n"
c29c0 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c );. }.. if( fl
c29d0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
c29e0 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 N_DELETEONCLOSE
c29f0 29 7b 0a 20 20 20 20 63 68 61 72 20 70 61 74 68 ){. char path
c2a00 55 74 66 38 5b 43 43 48 4d 41 58 50 41 54 48 5d Utf8[CCHMAXPATH]
c2a10 3b 0a 23 69 66 64 65 66 20 4e 44 45 42 55 47 20 ;.#ifdef NDEBUG
c2a20 2f 2a 20 77 68 65 6e 20 64 65 62 75 67 67 69 6e /* when debuggin
c2a30 67 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b g we want to mak
c2a40 65 20 73 75 72 65 20 69 74 20 69 73 20 64 65 6c e sure it is del
c2a50 65 74 65 64 20 2a 2f 0a 20 20 20 20 75 6c 46 69 eted */. ulFi
c2a60 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46 49 leAttribute = FI
c2a70 4c 45 5f 48 49 44 44 45 4e 3b 0a 23 65 6e 64 69 LE_HIDDEN;.#endi
c2a80 66 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 f. os2FullPat
c2a90 68 6e 61 6d 65 28 20 70 56 66 73 2c 20 7a 4e 61 hname( pVfs, zNa
c2aa0 6d 65 2c 20 43 43 48 4d 41 58 50 41 54 48 2c 20 me, CCHMAXPATH,
c2ab0 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 pathUtf8 );.
c2ac0 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
c2ad0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 = convertUtf8Pa
c2ae0 74 68 54 6f 43 70 28 20 70 61 74 68 55 74 66 38 thToCp( pathUtf8
c2af0 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 );. OSTRACE1
c2b00 28 20 22 4f 50 45 4e 20 68 69 64 64 65 6e 2f 64 ( "OPEN hidden/d
c2b10 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 66 elete on close f
c2b20 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 5c 6e ile attributes\n
c2b30 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 " );. }else{.
c2b40 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 pFile->pathToD
c2b50 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 4f el = NULL;. O
c2b60 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6e STRACE1( "OPEN n
c2b70 6f 72 6d 61 6c 20 66 69 6c 65 20 61 74 74 72 69 ormal file attri
c2b80 62 75 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a bute\n" );. }..
c2b90 20 20 2f 2a 20 61 6c 77 61 79 73 20 6f 70 65 6e /* always open
c2ba0 20 69 6e 20 72 61 6e 64 6f 6d 20 61 63 63 65 73 in random acces
c2bb0 73 20 6d 6f 64 65 20 66 6f 72 20 70 6f 73 73 69 s mode for possi
c2bc0 62 6c 79 20 62 65 74 74 65 72 20 73 70 65 65 64 bly better speed
c2bd0 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 */. ulOpenMode
c2be0 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 |= OPEN_FLAGS_R
c2bf0 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f 70 65 6e 4d ANDOM;. ulOpenM
c2c00 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 ode |= OPEN_FLAG
c2c10 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b S_FAIL_ON_ERROR;
c2c20 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d . ulOpenMode |=
c2c30 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e OPEN_FLAGS_NOIN
c2c40 48 45 52 49 54 3b 0a 0a 20 20 7a 4e 61 6d 65 43 HERIT;.. zNameC
c2c50 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 p = convertUtf8P
c2c60 61 74 68 54 6f 43 70 28 20 7a 4e 61 6d 65 20 29 athToCp( zName )
c2c70 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e ;. rc = DosOpen
c2c80 28 20 28 50 53 5a 29 7a 4e 61 6d 65 43 70 2c 0a ( (PSZ)zNameCp,.
c2c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2ca0 26 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 &h,.
c2cb0 20 20 20 20 26 75 6c 41 63 74 69 6f 6e 2c 0a 20 &ulAction,.
c2cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
c2cd0 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 L,.
c2ce0 20 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 ulFileAttribu
c2cf0 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 te,.
c2d00 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c ulOpenFlags,
c2d10 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c2d20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c 0a 20 20 20 ulOpenMode,.
c2d30 20 20 20 20 20 20 20 20 20 20 20 20 20 28 50 45 (PE
c2d40 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 66 AOP2)NULL );. f
c2d50 72 65 65 28 20 7a 4e 61 6d 65 43 70 20 29 3b 0a ree( zNameCp );.
c2d60 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 if( rc != NO_E
c2d70 52 52 4f 52 20 29 7b 0a 20 20 20 20 4f 53 54 52 RROR ){. OSTR
c2d80 41 43 45 37 28 20 22 4f 50 45 4e 20 49 6e 76 61 ACE7( "OPEN Inva
c2d90 6c 69 64 20 68 61 6e 64 6c 65 20 72 63 3d 25 64 lid handle rc=%d
c2da0 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c 41 63 : zName=%s, ulAc
c2db0 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 6c 41 74 74 tion=%#lx, ulAtt
c2dc0 72 3d 25 23 6c 78 2c 20 75 6c 46 6c 61 67 73 3d r=%#lx, ulFlags=
c2dd0 25 23 6c 78 2c 20 75 6c 4d 6f 64 65 3d 25 23 6c %#lx, ulMode=%#l
c2de0 78 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 x\n",.
c2df0 20 20 20 20 72 63 2c 20 7a 4e 61 6d 65 2c 20 75 rc, zName, u
c2e00 6c 41 63 74 69 6f 6e 2c 20 75 6c 46 69 6c 65 41 lAction, ulFileA
c2e10 74 74 72 69 62 75 74 65 2c 20 75 6c 4f 70 65 6e ttribute, ulOpen
c2e20 46 6c 61 67 73 2c 20 75 6c 4f 70 65 6e 4d 6f 64 Flags, ulOpenMod
c2e30 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 e );. if( pFi
c2e40 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 0a le->pathToDel ).
c2e50 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 6c free( pFil
c2e60 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a e->pathToDel );.
c2e70 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 pFile->pathT
c2e80 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 oDel = NULL;.
c2e90 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
c2ea0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
c2eb0 54 45 20 29 7b 0a 20 20 20 20 20 20 4f 53 54 52 TE ){. OSTR
c2ec0 41 43 45 32 28 20 22 4f 50 45 4e 20 25 64 20 49 ACE2( "OPEN %d I
c2ed0 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 5c 6e 22 nvalid handle\n"
c2ee0 2c 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 , ((flags | SQLI
c2ef0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
c2f00 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e ) & ~SQLITE_OPEN
c2f10 5f 52 45 41 44 57 52 49 54 45 29 20 29 3b 0a 20 _READWRITE) );.
c2f20 20 20 20 20 20 72 65 74 75 72 6e 20 6f 73 32 4f return os2O
c2f30 70 65 6e 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 pen( pVfs, zName
c2f40 2c 20 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 , id,.
c2f50 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66 6c ((fl
c2f60 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 ags | SQLITE_OPE
c2f70 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20 7e 53 N_READONLY) & ~S
c2f80 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
c2f90 52 49 54 45 29 2c 0a 20 20 20 20 20 20 20 20 20 RITE),.
c2fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 75 pOu
c2fb0 74 46 6c 61 67 73 20 29 3b 0a 20 20 20 20 7d 65 tFlags );. }e
c2fc0 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
c2fd0 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 n SQLITE_CANTOPE
c2fe0 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 N;. }. }..
c2ff0 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b if( pOutFlags ){
c3000 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 . *pOutFlags
c3010 3d 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 = flags & SQLITE
c3020 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
c3030 3f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 ? SQLITE_OPEN_RE
c3040 41 44 57 52 49 54 45 20 3a 20 53 51 4c 49 54 45 ADWRITE : SQLITE
c3050 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a _OPEN_READONLY;.
c3060 20 20 7d 0a 0a 20 20 70 46 69 6c 65 2d 3e 70 4d }.. pFile->pM
c3070 65 74 68 6f 64 20 3d 20 26 6f 73 32 49 6f 4d 65 ethod = &os2IoMe
c3080 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 thod;. pFile->h
c3090 20 3d 20 68 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e = h;. OpenCoun
c30a0 74 65 72 28 2b 31 29 3b 0a 20 20 4f 53 54 52 41 ter(+1);. OSTRA
c30b0 43 45 33 28 20 22 4f 50 45 4e 20 25 64 20 70 4f CE3( "OPEN %d pO
c30c0 75 74 46 6c 61 67 73 3d 25 64 5c 6e 22 2c 20 70 utFlags=%d\n", p
c30d0 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 74 46 6c 61 File->h, pOutFla
c30e0 67 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 gs );. return S
c30f0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
c3100 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61 ** Delete the na
c3110 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 med file..*/.sta
c3120 74 69 63 20 69 6e 74 20 6f 73 32 44 65 6c 65 74 tic int os2Delet
c3130 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 e(. sqlite3_vfs
c3140 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 *pVfs,
c3150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
c3160 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a ot used on os2 *
c3170 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
c3180 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 20 zFilename,
c3190 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
c31a0 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 me of file to de
c31b0 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 lete */. int sy
c31c0 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20 ncDir
c31d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c31e0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 /* Not used on
c31f0 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 41 50 49 52 os2 */.){. APIR
c3200 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 ET rc = NO_ERROR
c3210 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e ;. char *zFilen
c3220 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 ameCp = convertU
c3230 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 46 69 tf8PathToCp( zFi
c3240 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 53 69 6d 75 lename );. Simu
c3250 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
c3260 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
c3270 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 72 63 20 _DELETE );. rc
c3280 3d 20 44 6f 73 44 65 6c 65 74 65 28 20 28 50 53 = DosDelete( (PS
c3290 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b Z)zFilenameCp );
c32a0 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 6e 61 . free( zFilena
c32b0 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 43 meCp );. OSTRAC
c32c0 45 32 28 20 22 44 45 4c 45 54 45 20 5c 22 25 73 E2( "DELETE \"%s
c32d0 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 \"\n", zFilename
c32e0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 );. return rc
c32f0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 == NO_ERROR ? SQ
c3300 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
c3310 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d _IOERR_DELETE;.}
c3320 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 ../*.** Check th
c3330 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 e existance and
c3340 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 status of a file
c3350 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c3360 6f 73 32 41 63 63 65 73 73 28 0a 20 20 73 71 6c os2Access(. sql
c3370 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
c3380 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
c3390 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 ed on os2 */. c
c33a0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
c33b0 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 name, /* Name
c33c0 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63 of file to chec
c33d0 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 k */. int flags
c33e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
c33f0 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74 /* Type of test
c3400 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 to make on this
c3410 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a file */. int *
c3420 70 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20 pOut
c3430 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 /* Write re
c3440 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b sults here */.){
c3450 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 . FILESTATUS3 f
c3460 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 3b 0a sts3ConfigInfo;.
c3470 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f APIRET rc = NO
c3480 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a _ERROR;. char *
c3490 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f zFilenameCp = co
c34a0 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 nvertUtf8PathToC
c34b0 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a p( zFilename );.
c34c0 0a 20 20 6d 65 6d 73 65 74 28 20 26 66 73 74 73 . memset( &fsts
c34d0 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 2c 20 3ConfigInfo, 0,
c34e0 73 69 7a 65 6f 66 28 66 73 74 73 33 43 6f 6e 66 sizeof(fsts3Conf
c34f0 69 67 49 6e 66 6f 29 20 29 3b 0a 20 20 72 63 20 igInfo) );. rc
c3500 3d 20 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e = DosQueryPathIn
c3510 66 6f 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 fo( (PSZ)zFilena
c3520 6d 65 43 70 2c 20 46 49 4c 5f 53 54 41 4e 44 41 meCp, FIL_STANDA
c3530 52 44 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 RD,.
c3540 20 20 20 20 20 20 20 20 20 20 20 20 20 26 66 73 &fs
c3550 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 ts3ConfigInfo, s
c3560 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 izeof(FILESTATUS
c3570 33 29 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 3) );. free( zF
c3580 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f ilenameCp );. O
c3590 53 54 52 41 43 45 34 28 20 22 41 43 43 45 53 53 STRACE4( "ACCESS
c35a0 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f fsts3ConfigInfo
c35b0 2e 61 74 74 72 46 69 6c 65 3d 25 64 20 66 6c 61 .attrFile=%d fla
c35c0 67 73 3d 25 64 20 72 63 3d 25 64 5c 6e 22 2c 0a gs=%d rc=%d\n",.
c35d0 20 20 20 20 20 20 20 20 20 20 20 20 66 73 74 73 fsts
c35e0 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 3ConfigInfo.attr
c35f0 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 72 63 20 File, flags, rc
c3600 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c 61 );. switch( fla
c3610 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 gs ){. case S
c3620 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
c3630 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 D:. case SQLI
c3640 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
c3650 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72 63 :. rc = (rc
c3660 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 == NO_ERROR);.
c3670 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 OSTRACE3( "
c3680 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73 73 ACCESS %s access
c3690 20 6f 66 20 72 65 61 64 20 61 6e 64 20 65 78 69 of read and exi
c36a0 73 74 73 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a sts rc=%d\n", z
c36b0 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a Filename, rc );.
c36c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
c36d0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 case SQLITE_ACC
c36e0 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 ESS_READWRITE:.
c36f0 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d 3d rc = (rc ==
c3700 20 4e 4f 5f 45 52 52 4f 52 29 20 26 26 20 28 20 NO_ERROR) && (
c3710 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f (fsts3ConfigInfo
c3720 2e 61 74 74 72 46 69 6c 65 20 26 20 46 49 4c 45 .attrFile & FILE
c3730 5f 52 45 41 44 4f 4e 4c 59 29 20 3d 3d 20 30 20 _READONLY) == 0
c3740 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 );. OSTRACE
c3750 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 63 3( "ACCESS %s ac
c3760 63 65 73 73 20 6f 66 20 72 65 61 64 2f 77 72 69 cess of read/wri
c3770 74 65 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 te rc=%d\n", zF
c3780 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 ilename, rc );.
c3790 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
c37a0 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 default:. a
c37b0 73 73 65 72 74 28 20 21 22 49 6e 76 61 6c 69 64 ssert( !"Invalid
c37c0 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22 flags argument"
c37d0 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20 );. }. *pOut
c37e0 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 = rc;. return S
c37f0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 QLITE_OK;.}...#i
c3800 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
c3810 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
c3820 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 ./*.** Interface
c3830 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 s for opening a
c3840 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 shared library,
c3850 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f finding entry po
c3860 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 ints.** within t
c3870 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 he shared librar
c3880 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 y, and closing t
c3890 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 he shared librar
c38a0 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 y..*/./*.** Inte
c38b0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 rfaces for openi
c38c0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 ng a shared libr
c38d0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 ary, finding ent
c38e0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 ry points.** wit
c38f0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c hin the shared l
c3900 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 ibrary, and clos
c3910 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c ing the shared l
c3920 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 ibrary..*/.stati
c3930 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 4f 70 65 c void *os2DlOpe
c3940 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 n(sqlite3_vfs *p
c3950 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Vfs, const char
c3960 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 *zFilename){. U
c3970 43 48 41 52 20 6c 6f 61 64 45 72 72 5b 32 35 36 CHAR loadErr[256
c3980 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 20 68 6d 6f ];. HMODULE hmo
c3990 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b 0a d;. APIRET rc;.
c39a0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d char *zFilenam
c39b0 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 eCp = convertUtf
c39c0 38 50 61 74 68 54 6f 43 70 28 7a 46 69 6c 65 6e 8PathToCp(zFilen
c39d0 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 ame);. rc = Dos
c39e0 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 50 53 5a 29 LoadModule((PSZ)
c39f0 6c 6f 61 64 45 72 72 2c 20 73 69 7a 65 6f 66 28 loadErr, sizeof(
c3a00 6c 6f 61 64 45 72 72 29 2c 20 7a 46 69 6c 65 6e loadErr), zFilen
c3a10 61 6d 65 43 70 2c 20 26 68 6d 6f 64 29 3b 0a 20 ameCp, &hmod);.
c3a20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 43 free(zFilenameC
c3a30 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 p);. return rc
c3a40 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 != NO_ERROR ? 0
c3a50 3a 20 28 76 6f 69 64 2a 29 68 6d 6f 64 3b 0a 7d : (void*)hmod;.}
c3a60 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 20 73 ./*.** A no-op s
c3a70 69 6e 63 65 20 74 68 65 20 65 72 72 6f 72 20 63 ince the error c
c3a80 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
c3a90 6f 6e 20 74 68 65 20 44 6f 73 4c 6f 61 64 4d 6f on the DosLoadMo
c3aa0 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a 2a 20 6f 73 dule call..** os
c3ab0 32 44 6c 6f 70 65 6e 20 72 65 74 75 72 6e 73 20 2Dlopen returns
c3ac0 7a 65 72 6f 20 69 66 20 44 6f 73 4c 6f 61 64 4d zero if DosLoadM
c3ad0 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 73 75 63 odule is not suc
c3ae0 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74 cessful..*/.stat
c3af0 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 45 72 72 ic void os2DlErr
c3b00 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a or(sqlite3_vfs *
c3b10 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 pVfs, int nBuf,
c3b20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a char *zBufOut){.
c3b30 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 74 /* no-op */.}.st
c3b40 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c atic void *os2Dl
c3b50 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 Sym(sqlite3_vfs
c3b60 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 *pVfs, void *pHa
c3b70 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 ndle, const char
c3b80 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 50 46 *zSymbol){. PF
c3b90 4e 20 70 66 6e 3b 0a 20 20 41 50 49 52 45 54 20 N pfn;. APIRET
c3ba0 72 63 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 rc;. rc = DosQu
c3bb0 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f eryProcAddr((HMO
c3bc0 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c DULE)pHandle, 0L
c3bd0 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 , zSymbol, &pfn)
c3be0 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f ;. if( rc != NO
c3bf0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2f 2a _ERROR ){. /*
c3c00 20 69 66 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 if the symbol i
c3c10 74 73 65 6c 66 20 77 61 73 20 6e 6f 74 20 66 6f tself was not fo
c3c20 75 6e 64 2c 20 73 65 61 72 63 68 20 61 67 61 69 und, search agai
c3c30 6e 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 n for the same.
c3c40 20 20 20 20 2a 20 73 79 6d 62 6f 6c 20 77 69 74 * symbol wit
c3c50 68 20 61 6e 20 65 78 74 72 61 20 75 6e 64 65 72 h an extra under
c3c60 73 63 6f 72 65 2c 20 74 68 61 74 20 6d 69 67 68 score, that migh
c3c70 74 20 62 65 20 6e 65 65 64 65 64 20 64 65 70 65 t be needed depe
c3c80 6e 64 69 6e 67 0a 20 20 20 20 20 2a 20 6f 6e 20 nding. * on
c3c90 74 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 the calling conv
c3ca0 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 ention */. ch
c3cb0 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d ar _zSymbol[256]
c3cc0 20 3d 20 22 5f 22 3b 0a 20 20 20 20 73 74 72 6e = "_";. strn
c3cd0 63 61 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 cat(_zSymbol, zS
c3ce0 79 6d 62 6f 6c 2c 20 32 35 35 29 3b 0a 20 20 20 ymbol, 255);.
c3cf0 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 72 rc = DosQueryPr
c3d00 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 29 ocAddr((HMODULE)
c3d10 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53 pHandle, 0L, _zS
c3d20 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 ymbol, &pfn);.
c3d30 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d }. return rc !=
c3d40 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 NO_ERROR ? 0 :
c3d50 28 76 6f 69 64 2a 29 70 66 6e 3b 0a 7d 0a 73 74 (void*)pfn;.}.st
c3d60 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 43 atic void os2DlC
c3d70 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 lose(sqlite3_vfs
c3d80 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 *pVfs, void *pH
c3d90 61 6e 64 6c 65 29 7b 0a 20 20 44 6f 73 46 72 65 andle){. DosFre
c3da0 65 4d 6f 64 75 6c 65 28 28 48 4d 4f 44 55 4c 45 eModule((HMODULE
c3db0 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c )pHandle);.}.#el
c3dc0 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f se /* if SQLITE_
c3dd0 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
c3de0 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 ION is defined:
c3df0 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 */. #define os2
c3e00 44 6c 4f 70 65 6e 20 30 0a 20 20 23 64 65 66 69 DlOpen 0. #defi
c3e10 6e 65 20 6f 73 32 44 6c 45 72 72 6f 72 20 30 0a ne os2DlError 0.
c3e20 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 53 #define os2DlS
c3e30 79 6d 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f ym 0. #define o
c3e40 73 32 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 s2DlClose 0.#end
c3e50 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 if.../*.** Write
c3e60 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 up to nBuf byte
c3e70 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 s of randomness
c3e80 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 into zBuf..*/.st
c3e90 61 74 69 63 20 69 6e 74 20 6f 73 32 52 61 6e 64 atic int os2Rand
c3ea0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 omness(sqlite3_v
c3eb0 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 fs *pVfs, int nB
c3ec0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 uf, char *zBuf )
c3ed0 7b 0a 20 20 55 4c 4f 4e 47 20 73 69 7a 65 6f 66 {. ULONG sizeof
c3ee0 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 55 ULong = sizeof(U
c3ef0 4c 4f 4e 47 29 3b 0a 20 20 69 6e 74 20 6e 20 3d LONG);. int n =
c3f00 20 30 3b 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 0;. if( sizeof
c3f10 28 44 41 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 (DATETIME) <= nB
c3f20 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 uf - n ){. DA
c3f30 54 45 54 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f TETIME x;. Do
c3f40 73 47 65 74 44 61 74 65 54 69 6d 65 28 26 78 29 sGetDateTime(&x)
c3f50 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 ;. memcpy(&zB
c3f60 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f uf[n], &x, sizeo
c3f70 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 f(x));. n +=
c3f80 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a sizeof(x);. }..
c3f90 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
c3fa0 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
c3fb0 0a 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a . PPIB ppib;.
c3fc0 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
c3fd0 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 ocks(NULL, &ppib
c3fe0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
c3ff0 42 75 66 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 Buf[n], &ppib->p
c4000 69 62 5f 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 ib_ulpid, sizeof
c4010 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d ULong);. n +=
c4020 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 sizeofULong;.
c4030 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 }.. if( sizeofU
c4040 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e Long <= nBuf - n
c4050 20 29 7b 0a 20 20 20 20 50 54 49 42 20 70 74 69 ){. PTIB pti
c4060 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 b;. DosGetInf
c4070 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e oBlocks(&ptib, N
c4080 55 4c 4c 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 ULL);. memcpy
c4090 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 (&zBuf[n], &ptib
c40a0 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 ->tib_ptib2->tib
c40b0 32 5f 75 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 2_ultid, sizeofU
c40c0 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 Long);. n +=
c40d0 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d sizeofULong;. }
c40e0 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 73 74 69 .. /* if we sti
c40f0 6c 6c 20 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 ll haven't fille
c4100 64 20 74 68 65 20 62 75 66 66 65 72 20 79 65 74 d the buffer yet
c4110 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 the following w
c4120 69 6c 6c 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 ill */. /* grab
c4130 20 65 76 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 everything once
c4140 20 69 6e 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 instead of maki
c4150 6e 67 20 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 ng several calls
c4160 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 74 for a single it
c4170 65 6d 20 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 em */. if( size
c4180 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 ofULong <= nBuf
c4190 2d 20 6e 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 - n ){. ULONG
c41a0 20 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d ulSysInfo[QSV_M
c41b0 41 58 5d 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 AX];. DosQuer
c41c0 79 53 79 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 ySysInfo(1L, QSV
c41d0 5f 4d 41 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c _MAX, ulSysInfo,
c41e0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 sizeofULong * Q
c41f0 53 56 5f 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 SV_MAX);.. me
c4200 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
c4210 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 ulSysInfo[QSV_MS
c4220 5f 43 4f 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a _COUNT - 1], siz
c4230 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e eofULong);. n
c4240 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b += sizeofULong;
c4250 0a 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 .. if( sizeof
c4260 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 ULong <= nBuf -
c4270 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 n ){. memcp
c4280 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 y(&zBuf[n], &ulS
c4290 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 ysInfo[QSV_TIMER
c42a0 5f 49 4e 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 _INTERVAL - 1],
c42b0 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 sizeofULong);.
c42c0 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 n += sizeofU
c42d0 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Long;. }.
c42e0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 if( sizeofULong
c42f0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 <= nBuf - n ){.
c4300 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 memcpy(&zBu
c4310 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f f[n], &ulSysInfo
c4320 5b 51 53 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 [QSV_TIME_LOW -
c4330 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 1], sizeofULong)
c4340 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a ;. n += siz
c4350 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a eofULong;. }.
c4360 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c if( sizeofUL
c4370 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ong <= nBuf - n
c4380 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
c4390 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 &zBuf[n], &ulSys
c43a0 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 Info[QSV_TIME_HI
c43b0 47 48 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 GH - 1], sizeofU
c43c0 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b Long);. n +
c43d0 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 = sizeofULong;.
c43e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a }. if( siz
c43f0 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 eofULong <= nBuf
c4400 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 - n ){. me
c4410 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
c4420 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f ulSysInfo[QSV_TO
c4430 54 41 56 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 TAVAILMEM - 1],
c4440 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 sizeofULong);.
c4450 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 n += sizeofU
c4460 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Long;. }. }.
c4470 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
c4480 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 /*.** Sleep for
c4490 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 a little while.
c44a0 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 Return the amou
c44b0 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 nt of time slept
c44c0 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e ..** The argumen
c44d0 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
c44e0 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 of microseconds
c44f0 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 we want to sleep
c4500 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 ..** The return
c4510 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d value is the num
c4520 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
c4530 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 nds of sleep act
c4540 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 ually.** request
c4550 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 ed from the unde
c4560 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 rlying operating
c4570 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 system, a numbe
c4580 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 r which.** might
c4590 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e be greater than
c45a0 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
c45b0 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e argument, but n
c45c0 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 ot less.** than
c45d0 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f the argument..*/
c45e0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 .static int os2S
c45f0 6c 65 65 70 28 20 73 71 6c 69 74 65 33 5f 76 66 leep( sqlite3_vf
c4600 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 s *pVfs, int mic
c4610 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73 53 6c rosec ){. DosSl
c4620 65 65 70 28 20 28 6d 69 63 72 6f 73 65 63 2f 31 eep( (microsec/1
c4630 30 30 30 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 000) );. return
c4640 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a microsec;.}../*
c4650 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
c4660 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 g variable, if s
c4670 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f et to a non-zero
c4680 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 value, becomes
c4690 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 the result.** re
c46a0 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 turned from sqli
c46b0 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
c46c0 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 (). This is use
c46d0 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a d for testing..*
c46e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
c46f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
c4700 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 int sqlite3_curr
c4710 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 ent_time = 0;.#e
c4720 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 ndif../*.** Find
c4730 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d the current tim
c4740 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 e (in Universal
c4750 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 Coordinated Time
c4760 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a ). Write the.**
c4770 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e current time an
c4780 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 d date as a Juli
c4790 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e an Day number in
c47a0 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a to *prNow and.**
c47b0 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 return 0. Retu
c47c0 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 rn 1 if the time
c47d0 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 and date cannot
c47e0 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e be found..*/.in
c47f0 74 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 t os2CurrentTime
c4800 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 ( sqlite3_vfs *p
c4810 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e Vfs, double *prN
c4820 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6e ow ){. double n
c4830 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69 6e 75 ow;. SHORT minu
c4840 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74 6f 20 te; /* needs to
c4850 62 65 20 61 62 6c 65 20 74 6f 20 63 6f 70 65 20 be able to cope
c4860 77 69 74 68 20 6e 65 67 61 74 69 76 65 20 74 69 with negative ti
c4870 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 2a 2f mezone offset */
c4880 0a 20 20 55 53 48 4f 52 54 20 73 65 63 6f 6e 64 . USHORT second
c4890 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 20 20 20 , hour,.
c48a0 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 day, month, yea
c48b0 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 20 64 74 r;. DATETIME dt
c48c0 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 ;. DosGetDateTi
c48d0 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 73 65 63 me( &dt );. sec
c48e0 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 29 64 74 ond = (USHORT)dt
c48f0 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75 .seconds;. minu
c4900 74 65 20 3d 20 28 53 48 4f 52 54 29 64 74 2e 6d te = (SHORT)dt.m
c4910 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 69 6d 65 inutes + dt.time
c4920 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28 zone;. hour = (
c4930 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b USHORT)dt.hours;
c4940 0a 20 20 64 61 79 20 3d 20 28 55 53 48 4f 52 54 . day = (USHORT
c4950 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68 )dt.day;. month
c4960 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f = (USHORT)dt.mo
c4970 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d 20 28 55 nth;. year = (U
c4980 53 48 4f 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a SHORT)dt.year;..
c4990 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e /* Calculation
c49a0 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77 s from http://ww
c49b0 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 w.astro.keele.ac
c49c0 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f .uk/~rno/Astrono
c49d0 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20 my/hjd.html.
c49e0 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 http://www.astr
c49f0 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 o.keele.ac.uk/~r
c4a00 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 no/Astronomy/hjd
c4a10 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43 -0.1.c */. /* C
c4a20 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c alculate the Jul
c4a30 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f ian days */. no
c4a40 77 20 3d 20 64 61 79 20 2d 20 33 32 30 37 36 20 w = day - 32076
c4a50 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 65 61 72 +. 1461*(year
c4a60 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68 + 4800 + (month
c4a70 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20 - 14)/12)/4 +.
c4a80 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20 367*(month -
c4a90 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2 - (month - 14)
c4aa0 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20 /12*12)/12 -.
c4ab0 20 33 2a 28 28 79 65 61 72 20 2b 20 34 39 30 30 3*((year + 4900
c4ac0 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f + (month - 14)/
c4ad0 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f 12)/100)/4;.. /
c4ae0 2a 20 41 64 64 20 74 68 65 20 66 72 61 63 74 69 * Add the fracti
c4af0 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73 onal hours, mins
c4b00 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a and seconds */.
c4b10 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b now += (hour +
c4b20 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e 12.0)/24.0;. n
c4b30 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34 ow += minute/144
c4b40 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65 0.0;. now += se
c4b50 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20 cond/86400.0;.
c4b60 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69 *prNow = now;.#i
c4b70 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
c4b80 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 . if( sqlite3_c
c4b90 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 urrent_time ){.
c4ba0 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 *prNow = sqli
c4bb0 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
c4bc0 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 /86400.0 + 24405
c4bd0 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 87.5;. }.#endif
c4be0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
c4bf0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 47 65 static int os2Ge
c4c00 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 tLastError(sqlit
c4c10 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
c4c20 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
c4c30 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b uf){. return 0;
c4c40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
c4c50 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 lize and deiniti
c4c60 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 alize the operat
c4c70 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 ing system inter
c4c80 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f face..*/.SQLITE_
c4c90 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c4ca0 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 os_init(void){.
c4cb0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
c4cc0 76 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20 vfs os2Vfs = {.
c4cd0 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 1,
c4ce0 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
c4cf0 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 n */. sizeof(
c4d00 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 os2File), /* s
c4d10 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43 zOsFile */. C
c4d20 43 48 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20 CHMAXPATH,
c4d30 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 /* mxPathname
c4d40 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
c4d50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 /* pNe
c4d60 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c xt */. "os2",
c4d70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c4d80 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 zName */. 0,
c4d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4da0 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a /* pAppData */..
c4db0 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20 os2Open,
c4dc0 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 /* xOpen
c4dd0 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65 */. os2Delete
c4de0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 , /* xDe
c4df0 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41 lete */. os2A
c4e00 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f ccess, /
c4e10 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 * xAccess */.
c4e20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 os2FullPathname
c4e30 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 , /* xFullPath
c4e40 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 44 name */. os2D
c4e50 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f lOpen, /
c4e60 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 * xDlOpen */.
c4e70 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20 20 20 os2DlError,
c4e80 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 /* xDlError
c4e90 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79 6d 2c */. os2DlSym,
c4ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
c4eb0 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c Sym */. os2Dl
c4ec0 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Close, /*
c4ed0 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 xDlClose */.
c4ee0 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 os2Randomness,
c4ef0 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 /* xRandomne
c4f00 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53 6c 65 ss */. os2Sle
c4f10 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ep, /*
c4f20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 6f 73 xSleep */. os
c4f30 32 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 2CurrentTime,
c4f40 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 /* xCurrentTime
c4f50 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74 4c 61 */. os2GetLa
c4f60 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 stError /* xG
c4f70 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 etLastError */.
c4f80 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 };. sqlite3_vf
c4f90 73 5f 72 65 67 69 73 74 65 72 28 26 6f 73 32 56 s_register(&os2V
c4fa0 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74 55 63 fs, 1);. initUc
c4fb0 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 onvObjects();.
c4fc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
c4fd0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
c4fe0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nt sqlite3_os_en
c4ff0 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 65 65 55 d(void){. freeU
c5000 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 convObjects();.
c5010 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c5020 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 K;.}..#endif /*
c5030 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f SQLITE_OS_OS2 */
c5040 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
c5050 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e * End of os_os2.
c5060 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
c5070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5090 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c50a0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
c50b0 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a unix.c *********
c50c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c50d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c50e0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
c50f0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
c5100 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
c5110 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
c5120 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
c5130 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
c5140 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
c5150 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
c5160 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
c5170 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
c5180 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
c5190 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
c51a0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
c51b0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
c51c0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
c51d0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
c51e0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
c51f0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
c5200 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
c5210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5250 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
c5260 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
c5270 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 code that is sp
c5280 65 63 69 66 69 63 20 74 6f 20 55 6e 69 78 20 73 ecific to Unix s
c5290 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 ystems..**.** $I
c52a0 64 3a 20 6f 73 5f 75 6e 69 78 2e 63 2c 76 20 31 d: os_unix.c,v 1
c52b0 2e 32 30 38 20 32 30 30 38 2f 31 31 2f 30 37 20 .208 2008/11/07
c52c0 30 30 3a 30 36 3a 31 38 20 64 72 68 20 45 78 70 00:06:18 drh Exp
c52d0 20 24 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 $.*/.#if SQLITE
c52e0 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 _OS_UNIX
c52f0 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 /* This fi
c5300 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e le is used on un
c5310 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a ix only */../*.*
c5320 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 * If SQLITE_ENAB
c5330 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
c5340 20 69 73 20 64 65 66 69 6e 65 64 20 61 6e 64 20 is defined and
c5350 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
c5360 6e 20 73 65 76 65 72 61 6c 0a 2a 2a 20 61 6c 74 n several.** alt
c5370 65 72 6e 61 74 69 76 65 20 6c 6f 63 6b 69 6e 67 ernative locking
c5380 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
c5390 20 61 72 65 20 70 72 6f 76 69 64 65 64 3a 0a 2a are provided:.*
c53a0 2a 0a 2a 2a 20 20 20 2a 20 50 4f 53 49 58 20 6c *.** * POSIX l
c53b0 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 ocking (the defa
c53c0 75 6c 74 29 2c 0a 2a 2a 20 20 20 2a 20 4e 6f 20 ult),.** * No
c53d0 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 2a 20 locking,.** *
c53e0 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 Dot-file locking
c53f0 2c 0a 2a 2a 20 20 20 2a 20 66 6c 6f 63 6b 28 29 ,.** * flock()
c5400 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 2a locking,.** *
c5410 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 28 4f 53 AFP locking (OS
c5420 58 20 6f 6e 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 53 X only)..**.** S
c5430 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
c5440 4b 49 4e 47 5f 53 54 59 4c 45 20 6f 6e 6c 79 20 KING_STYLE only
c5450 77 6f 72 6b 73 20 6f 6e 20 61 20 4d 61 63 2e 20 works on a Mac.
c5460 49 74 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 20 It is turned on
c5470 62 79 0a 2a 2a 20 64 65 66 61 75 6c 74 20 6f 6e by.** default on
c5480 20 61 20 4d 61 63 20 61 6e 64 20 64 69 73 61 62 a Mac and disab
c5490 6c 65 64 20 6f 6e 20 61 6c 6c 20 6f 74 68 65 72 led on all other
c54a0 20 70 6f 73 69 78 20 70 6c 61 74 66 6f 72 6d 73 posix platforms
c54b0 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
c54c0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
c54d0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 LOCKING_STYLE).#
c54e0 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 44 if defined(__D
c54f0 41 52 57 49 4e 5f 5f 29 0a 23 20 20 20 20 64 65 ARWIN__).# de
c5500 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 fine SQLITE_ENAB
c5510 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
c5520 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 1.# else.#
c5530 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e define SQLITE_EN
c5540 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
c5550 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 LE 0.# endif.#e
c5560 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 ndif../*.** Thes
c5570 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c e #defines shoul
c5580 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 d enable >2GB fi
c5590 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f le support on Po
c55a0 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e six if the.** un
c55b0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
c55c0 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 ng system suppor
c55d0 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f ts it. If the O
c55e0 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 S lacks.** large
c55f0 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 file support, t
c5600 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e hese should be n
c5610 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 o-ops..**.** Lar
c5620 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 ge file support
c5630 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 can be disabled
c5640 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 using the -DSQLI
c5650 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 TE_DISABLE_LFS s
c5660 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 witch.** on the
c5670 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 compiler command
c5680 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 line. This is
c5690 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 necessary if you
c56a0 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a are compiling.*
c56b0 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 * on a recent ma
c56c0 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 chine (ex: RedHa
c56d0 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 t 7.2) but you w
c56e0 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f ant your code to
c56f0 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f work.** on an o
c5700 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 lder machine (ex
c5710 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20 : RedHat 6.0).
c5720 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f If you compile o
c5730 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 n RedHat 7.2.**
c5740 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74 without this opt
c5750 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 ion, LFS is enab
c5760 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65 le. But LFS doe
c5770 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 s not exist in t
c5780 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 he kernel.** in
c5790 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 RedHat 6.0, so t
c57a0 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f he code won't wo
c57b0 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 rk. Hence, for
c57c0 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a maximum binary.*
c57d0 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f * portability yo
c57e0 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 u should omit LF
c57f0 53 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 S..*/.#ifndef SQ
c5800 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 LITE_DISABLE_LFS
c5810 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 .# define _LARGE
c5820 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 _FILE 1.#
c5830 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 ifndef _FILE_OFF
c5840 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 SET_BITS.# def
c5850 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 ine _FILE_OFFSET
c5860 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 _BITS 64.# endif
c5870 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 .# define _LARGE
c5880 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 FILE_SOURCE 1.#e
c5890 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e ndif../*.** stan
c58a0 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c dard include fil
c58b0 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 es..*/.#include
c58c0 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 <sys/types.h>.#i
c58d0 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 nclude <sys/stat
c58e0 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 .h>.#include <fc
c58f0 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 ntl.h>.#include
c5900 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c <unistd.h>.#incl
c5910 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e ude <sys/time.h>
c5920 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f .#include <errno
c5930 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f .h>..#if SQLITE_
c5940 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
c5950 54 59 4c 45 0a 23 69 6e 63 6c 75 64 65 20 3c 73 TYLE.#include <s
c5960 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 69 6e 63 ys/ioctl.h>.#inc
c5970 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e lude <sys/param.
c5980 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 h>.#include <sys
c5990 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 /mount.h>.#endif
c59a0 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
c59b0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
c59c0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 */../*.** If we
c59d0 61 72 65 20 74 6f 20 62 65 20 74 68 72 65 61 64 are to be thread
c59e0 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 -safe, include t
c59f0 68 65 20 70 74 68 72 65 61 64 73 20 68 65 61 64 he pthreads head
c5a00 65 72 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a er and define.**
c5a10 20 74 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 the SQLITE_UNIX
c5a20 5f 54 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a _THREADS macro..
c5a30 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
c5a40 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e READSAFE.# defin
c5a50 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 e SQLITE_UNIX_TH
c5a60 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a READS 1.#endif..
c5a70 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 /*.** Default pe
c5a80 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 rmissions when c
c5a90 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69 reating a new fi
c5aa0 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 le.*/.#ifndef SQ
c5ab0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c LITE_DEFAULT_FIL
c5ac0 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 E_PERMISSIONS.#
c5ad0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
c5ae0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
c5af0 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 SSIONS 0644.#end
c5b00 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 if../*.** Maximu
c5b10 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 m supported path
c5b20 2d 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 -length..*/.#def
c5b30 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ine MAX_PATHNAME
c5b40 20 35 31 32 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 512.../*.** The
c5b50 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 unixFile struct
c5b60 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 ure is subclass
c5b70 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 of sqlite3_file
c5b80 73 70 65 63 69 66 69 63 20 66 6f 72 20 74 68 65 specific for the
c5b90 20 75 6e 69 78 0a 2a 2a 20 70 72 6f 74 61 62 69 unix.** protabi
c5ba0 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 lity layer..*/.t
c5bb0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e ypedef struct un
c5bc0 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b ixFile unixFile;
c5bd0 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 .struct unixFile
c5be0 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f {. sqlite3_io_
c5bf0 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 methods const *p
c5c00 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 Method; /* Alwa
c5c10 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 ys the first ent
c5c20 72 79 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ry */.#ifdef SQL
c5c30 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 6e ITE_TEST. /* In
c5c40 20 74 65 73 74 20 6d 6f 64 65 2c 20 69 6e 63 72 test mode, incr
c5c50 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 ease the size of
c5c60 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
c5c70 61 20 62 69 74 20 73 6f 20 74 68 61 74 20 0a 20 a bit so that .
c5c80 20 2a 2a 20 69 74 20 69 73 20 6c 61 72 67 65 72 ** it is larger
c5c90 20 74 68 61 6e 20 74 68 65 20 73 74 72 75 63 74 than the struct
c5ca0 20 43 72 61 73 68 46 69 6c 65 20 64 65 66 69 6e CrashFile defin
c5cb0 65 64 20 69 6e 20 74 65 73 74 36 2e 63 2e 0a 20 ed in test6.c..
c5cc0 20 2a 2f 0a 20 20 63 68 61 72 20 61 50 61 64 64 */. char aPadd
c5cd0 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e 64 69 66 0a ing[32];.#endif.
c5ce0 20 20 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 struct openCnt
c5cf0 20 2a 70 4f 70 65 6e 3b 20 20 20 20 2f 2a 20 49 *pOpen; /* I
c5d00 6e 66 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 nfo about all op
c5d10 65 6e 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 en fd's on this
c5d20 69 6e 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 inode */. struc
c5d30 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 t lockInfo *pLoc
c5d40 6b 3b 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f k; /* Info abo
c5d50 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73 ut locks on this
c5d60 20 69 6e 6f 64 65 20 2a 2f 0a 23 69 66 20 53 51 inode */.#if SQ
c5d70 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
c5d80 49 4e 47 5f 53 54 59 4c 45 0a 20 20 76 6f 69 64 ING_STYLE. void
c5d90 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 *lockingContext
c5da0 3b 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 ; /* Locking
c5db0 20 73 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 style specific
c5dc0 73 74 61 74 65 20 2a 2f 0a 23 65 6e 64 69 66 0a state */.#endif.
c5dd0 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 int h;
c5de0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
c5df0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
c5e00 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 or */. unsigned
c5e10 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 char locktype;
c5e20 20 20 2f 2a 20 54 68 65 20 74 79 70 65 20 6f 66 /* The type of
c5e30 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 lock held on th
c5e40 69 73 20 66 64 20 2a 2f 0a 20 20 69 6e 74 20 64 is fd */. int d
c5e50 69 72 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 irfd;
c5e60 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 /* File des
c5e70 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20 criptor for the
c5e80 64 69 72 65 63 74 6f 72 79 20 2a 2f 0a 23 69 66 directory */.#if
c5e90 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
c5ea0 46 45 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 FE. pthread_t t
c5eb0 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f id; /
c5ec0 2a 20 54 68 65 20 74 68 72 65 61 64 20 74 68 61 * The thread tha
c5ed0 74 20 22 6f 77 6e 73 22 20 74 68 69 73 20 75 6e t "owns" this un
c5ee0 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 ixFile */.#endif
c5ef0 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f . int lastErrno
c5f00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
c5f10 54 68 65 20 75 6e 69 78 20 65 72 72 6e 6f 20 66 The unix errno f
c5f20 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f rom the last I/O
c5f30 20 65 72 72 6f 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a error */.};../*
c5f40 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 .** Include code
c5f50 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
c5f60 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 to all os_*.c fi
c5f70 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a les.*/./********
c5f80 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f ****** Include o
c5f90 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 s_common.h in th
c5fa0 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 75 e middle of os_u
c5fb0 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nix.c **********
c5fc0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
c5fd0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
c5fe0 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a e os_common.h **
c5ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6010 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
c6020 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 4 May 22.**.** T
c6030 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
c6040 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
c6050 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
c6060 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
c6070 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
c6080 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
c6090 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
c60a0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
c60b0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
c60c0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
c60d0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
c60e0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
c60f0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
c6100 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
c6110 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
c6120 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
c6130 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
c6140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
c6190 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
c61a0 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 tains macros and
c61b0 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 a little bit of
c61c0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
c61d0 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f mmon to.** all o
c61e0 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 f the platform-s
c61f0 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f pecific files (o
c6200 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 s_*.c) and is #i
c6210 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f ncluded into tho
c6220 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a se.** files..**.
c6230 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f ** This file sho
c6240 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 uld be #included
c6250 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 by the os_*.c f
c6260 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 iles only. It i
c6270 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 s not a.** gener
c6280 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 al purpose heade
c6290 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 r file..**.** $I
c62a0 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 d: os_common.h,v
c62b0 20 31 2e 33 37 20 32 30 30 38 2f 30 35 2f 32 39 1.37 2008/05/29
c62c0 20 32 30 3a 32 32 3a 33 37 20 73 68 61 6e 65 20 20:22:37 shane
c62d0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
c62e0 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 _OS_COMMON_H_.#
c62f0 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f define _OS_COMMO
c6300 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c N_H_../*.** At l
c6310 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 east two bugs ha
c6320 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 ve slipped in be
c6330 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 cause we changed
c6340 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 the MEMORY_DEBU
c6350 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 G.** macro to SQ
c6360 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 LITE_DEBUG and s
c6370 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 ome older makefi
c6380 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 les have not yet
c6390 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 made the.** swi
c63a0 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 tch. The follow
c63b0 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 ing code should
c63c0 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c catch this probl
c63d0 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 em at compile-ti
c63e0 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 me..*/.#ifdef ME
c63f0 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 MORY_DEBUG.# err
c6400 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 or "The MEMORY_D
c6410 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 EBUG macro is ob
c6420 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c solete. Use SQL
c6430 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 ITE_DEBUG instea
c6440 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a d.".#endif.../*.
c6450 20 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c * When testing,
c6460 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 this global var
c6470 69 61 62 6c 65 20 73 74 6f 72 65 73 20 74 68 65 iable stores the
c6480 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 location of the
c6490 0a 20 2a 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 . * pending-byte
c64a0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
c64b0 20 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 file.. */.#ifde
c64c0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
c64d0 4c 49 54 45 5f 41 50 49 20 75 6e 73 69 67 6e 65 LITE_API unsigne
c64e0 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 65 d int sqlite3_pe
c64f0 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78 34 nding_byte = 0x4
c6500 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0000000;.#endif.
c6510 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
c6520 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
c6530 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
c6540 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 STrace = 0;.#def
c6550 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 ine OSTRACE1(X)
c6560 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
c6570 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
c6580 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
c6590 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 X).#define OSTRA
c65a0 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 CE2(X,Y) i
c65b0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
c65c0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
c65d0 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 Printf(X,Y).#def
c65e0 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 ine OSTRACE3(X,Y
c65f0 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 ,Z) if( sqli
c6600 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
c6610 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
c6620 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f X,Y,Z).#define O
c6630 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 STRACE4(X,Y,Z,A)
c6640 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
c6650 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
c6660 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a ebugPrintf(X,Y,Z
c6670 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,A).#define OSTR
c6680 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 ACE5(X,Y,Z,A,B)
c6690 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
c66a0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
c66b0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c gPrintf(X,Y,Z,A,
c66c0 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 B).#define OSTRA
c66d0 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 CE6(X,Y,Z,A,B,C)
c66e0 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 \. if(sqlite
c66f0 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 3OSTrace) sqlite
c6700 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
c6710 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e ,Z,A,B,C).#defin
c6720 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a e OSTRACE7(X,Y,Z
c6730 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 ,A,B,C,D) \.
c6740 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 if(sqlite3OSTrac
c6750 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 e) sqlite3DebugP
c6760 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c rintf(X,Y,Z,A,B,
c6770 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 C,D).#else.#defi
c6780 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 ne OSTRACE1(X).#
c6790 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 define OSTRACE2(
c67a0 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 X,Y).#define OST
c67b0 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 RACE3(X,Y,Z).#de
c67c0 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c fine OSTRACE4(X,
c67d0 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f Y,Z,A).#define O
c67e0 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c STRACE5(X,Y,Z,A,
c67f0 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 B).#define OSTRA
c6800 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 CE6(X,Y,Z,A,B,C)
c6810 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c6820 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 7(X,Y,Z,A,B,C,D)
c6830 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d .#endif../*.** M
c6840 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 acros for perfor
c6850 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 mance tracing.
c6860 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 Normally turned
c6870 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 off. Only works
c6880 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 .** on i486 hard
c6890 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ware..*/.#ifdef
c68a0 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e SQLITE_PERFORMAN
c68b0 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a CE_TRACE../* .**
c68c0 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 hwtime.h contai
c68d0 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 ns inline assemb
c68e0 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 ler code for imp
c68f0 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 lementing .** hi
c6900 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 gh-performance t
c6910 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a iming routines..
c6920 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
c6930 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d ** Include hwtim
c6940 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c e.h in the middl
c6950 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 e of os_common.h
c6960 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c6970 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
c6980 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 ** Begin file hw
c6990 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a time.h *********
c69a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c69b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c69c0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 */./*.** 2008 Ma
c69d0 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 27.**.** The a
c69e0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
c69f0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
c6a00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
c6a10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
c6a20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
c6a30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
c6a40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
c6a50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
c6a60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
c6a70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
c6a80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
c6a90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
c6aa0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
c6ab0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
c6ac0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
c6ad0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
c6ae0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
c6af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6b30 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
c6b40 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
c6b50 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 s inline asm cod
c6b60 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 e for retrieving
c6b70 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e "high-performan
c6b80 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 ce".** counters
c6b90 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 for x86 class CP
c6ba0 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 Us..**.** $Id: h
c6bb0 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 wtime.h,v 1.3 20
c6bc0 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 08/08/01 14:33:1
c6bd0 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 5 shane Exp $.*/
c6be0 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 .#ifndef _HWTIME
c6bf0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 _H_.#define _HWT
c6c00 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 IME_H_../*.** Th
c6c10 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
c6c20 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f ine only works o
c6c30 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 n pentium-class
c6c40 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 (or newer) proce
c6c50 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 ssors..** It use
c6c60 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f s the RDTSC opco
c6c70 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 de to read the c
c6c80 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 ycle count value
c6c90 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 out of the.** p
c6ca0 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 rocessor and ret
c6cb0 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e urns that value.
c6cc0 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 This can be us
c6cd0 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a ed for high-res.
c6ce0 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f ** profiling..*/
c6cf0 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f .#if (defined(__
c6d00 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e GNUC__) || defin
c6d10 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 ed(_MSC_VER)) &&
c6d20 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 \. (define
c6d30 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e d(i386) || defin
c6d40 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 ed(__i386__) ||
c6d50 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 defined(_M_IX86)
c6d60 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 ).. #if defined
c6d70 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f (__GNUC__).. __
c6d80 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f inline__ sqlite_
c6d90 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
c6da0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
c6db0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f unsigned int lo
c6dc0 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d , hi;. __asm
c6dd0 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 __ __volatile__
c6de0 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 ("rdtsc" : "=a"
c6df0 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 (lo), "=d" (hi))
c6e00 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 ;. return (s
c6e10 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 qlite_uint64)hi
c6e20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a << 32 | lo;. }.
c6e30 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 . #elif defined
c6e40 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f (_MSC_VER).. __
c6e50 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 declspec(naked)
c6e60 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f __inline sqlite_
c6e70 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 uint64 __cdecl s
c6e80 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
c6e90 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b d){. __asm {
c6ea0 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 . rdtsc.
c6eb0 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 ret
c6ec0 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 ; return value
c6ed0 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 at EDX:EAX.
c6ee0 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a }. }.. #endif.
c6ef0 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 .#elif (defined(
c6f00 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 __GNUC__) && def
c6f10 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 ined(__x86_64__)
c6f20 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 ).. __inline__
c6f30 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
c6f40 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
c6f50 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 ){. unsigne
c6f60 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 d long val;.
c6f70 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 __asm__ __vola
c6f80 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 tile__ ("rdtsc"
c6f90 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 : "=A" (val));.
c6fa0 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b return val;
c6fb0 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 . }. .#elif (de
c6fc0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
c6fd0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 && defined(__ppc
c6fe0 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 __)).. __inline
c6ff0 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
c7000 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
c7010 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 oid){. unsi
c7020 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 gned long long r
c7030 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 etval;. uns
c7040 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b igned long junk;
c7050 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f . __asm__ _
c7060 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e _volatile__ ("\n
c7070 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 \. 1:
c7080 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e mftbu %1\n
c7090 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c70a0 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c mftb %L0\
c70b0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
c70c0 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c mftbu %0\
c70d0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
c70e0 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c cmpw %0,
c70f0 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %1\n\.
c7100 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 bne
c7110 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 1b".
c7120 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 : "=r" (re
c7130 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e tval), "=r" (jun
c7140 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 k));. retur
c7150 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 n retval;. }..#
c7160 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e else.. #error N
c7170 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 eed implementati
c7180 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 on of sqlite3Hwt
c7190 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 ime() for your p
c71a0 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 latform... /*.
c71b0 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 ** To compile w
c71c0 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 ithout implement
c71d0 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d ing sqlite3Hwtim
c71e0 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 e() for your pla
c71f0 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 tform,. ** you
c7200 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 can remove the a
c7210 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 bove #error and
c7220 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e use the followin
c7230 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 g. ** stub func
c7240 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 tion. You will
c7250 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 lose timing supp
c7260 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a ort for many. *
c7270 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 * of the debuggi
c7280 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 ng and testing u
c7290 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 tilities, but it
c72a0 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 should at. **
c72b0 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e least compile an
c72c0 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 d run.. */.SQLI
c72d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c TE_PRIVATE sql
c72e0 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
c72f0 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 e3Hwtime(void){
c7300 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f return ((sqlite_
c7310 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 uint64)0); }..#e
c7320 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 ndif..#endif /*
c7330 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 !defined(_HWTIME
c7340 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _H_) */../******
c7350 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
c7360 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a hwtime.h *******
c7370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7390 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
c73a0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
c73b0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
c73c0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d t off in os_comm
c73d0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
c73e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 *******/..static
c73f0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 sqlite_uint64 g
c7400 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 _start;.static s
c7410 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 qlite_uint64 g_e
c7420 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 lapsed;.#define
c7430 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 TIMER_START
c7440 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 g_start=sqlite
c7450 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 3Hwtime().#defin
c7460 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 e TIMER_END
c7470 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 g_elapsed=sq
c7480 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f lite3Hwtime()-g_
c7490 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 start.#define TI
c74a0 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 MER_ELAPSED
c74b0 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a g_elapsed.#else.
c74c0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 #define TIMER_ST
c74d0 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 ART.#define TIME
c74e0 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 R_END.#define TI
c74f0 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 MER_ELAPSED
c7500 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 ((sqlite_uint64)
c7510 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0).#endif../*.**
c7520 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 If we compile w
c7530 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 ith the SQLITE_T
c7540 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 EST macro set, t
c7550 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e hen the followin
c7560 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f g block.** of co
c7570 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 de will give us
c7580 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 the ability to s
c7590 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 imulate a disk I
c75a0 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a /O error. This.
c75b0 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 ** is used for t
c75c0 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 esting the I/O r
c75d0 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a ecovery logic..*
c75e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
c75f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
c7600 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
c7610 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 rror_hit = 0;
c7620 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
c7630 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 l number of I/O
c7640 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 Errors */.SQLITE
c7650 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
c7660 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 _io_error_hardhi
c7670 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a t = 0; /*
c7680 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 Number of non-b
c7690 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a enign errors */.
c76a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
c76b0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c76c0 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 pending = 0;
c76d0 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 /* Count dow
c76e0 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 n to first I/O e
c76f0 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 rror */.SQLITE_A
c7700 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
c7710 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 o_error_persist
c7720 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 = 0; /* T
c7730 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 rue if I/O error
c7740 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c s persist */.SQL
c7750 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c7760 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
c7770 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ign = 0;
c7780 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f /* True if erro
c7790 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f rs are benign */
c77a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
c77b0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
c77c0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 _pending = 0;.SQ
c77d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
c77e0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 ite3_diskfull =
c77f0 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 0;.#define Simul
c7800 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
c7810 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 (X) sqlite3_io_e
c7820 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a rror_benign=(X).
c7830 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
c7840 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c IOError(CODE) \
c7850 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f . if( (sqlite3_
c7860 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 io_error_persist
c7870 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 && sqlite3_io_e
c7880 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 rror_hit) \.
c7890 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f || sqlite3_io
c78a0 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d _error_pending--
c78b0 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 == 1 ) \.
c78c0 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c { local
c78d0 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 _ioerr(); CODE;
c78e0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f }.static void lo
c78f0 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 cal_ioerr(){. I
c7900 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e OTRACE(("IOERR\n
c7910 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 "));. sqlite3_i
c7920 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 o_error_hit++;.
c7930 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f if( !sqlite3_io
c7940 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 _error_benign )
c7950 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c7960 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 _hardhit++;.}.#d
c7970 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 efine SimulateDi
c7980 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 skfullError(CODE
c7990 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 ) \. if( sqlit
c79a0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
c79b0 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 ing ){ \. if
c79c0 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 ( sqlite3_diskfu
c79d0 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 ll_pending == 1
c79e0 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 ){ \. loca
c79f0 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 l_ioerr(); \.
c7a00 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b sqlite3_disk
c7a10 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 full = 1; \.
c7a20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 sqlite3_io_er
c7a30 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 ror_hit = 1; \.
c7a40 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 CODE; \.
c7a50 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 }else{ \.
c7a60 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 sqlite3_diskf
c7a70 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c ull_pending--; \
c7a80 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 . } \. }.#
c7a90 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d else.#define Sim
c7aa0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
c7ab0 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 gn(X).#define Si
c7ac0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 mulateIOError(A)
c7ad0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
c7ae0 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 eDiskfullError(A
c7af0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
c7b00 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 When testing, ke
c7b10 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 ep a count of th
c7b20 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e e number of open
c7b30 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 files..*/.#ifde
c7b40 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
c7b50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
c7b60 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 ite3_open_file_c
c7b70 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e ount = 0;.#defin
c7b80 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 e OpenCounter(X)
c7b90 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 sqlite3_open_f
c7ba0 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 ile_count+=(X).#
c7bb0 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 else.#define Ope
c7bc0 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 nCounter(X).#end
c7bd0 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 if..#endif /* !d
c7be0 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f efined(_OS_COMMO
c7bf0 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a N_H_) */../*****
c7c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
c7c10 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
c7c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7c40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
c7c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
c7c60 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
c7c70 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 ft off in os_uni
c7c80 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
c7c90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
c7ca0 20 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 Define various
c7cb0 6d 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 macros that are
c7cc0 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d missing from som
c7cd0 65 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 e systems..*/.#i
c7ce0 66 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c fndef O_LARGEFIL
c7cf0 45 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 E.# define O_LAR
c7d00 47 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a GEFILE 0.#endif.
c7d10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 #ifdef SQLITE_DI
c7d20 53 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 SABLE_LFS.# unde
c7d30 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 f O_LARGEFILE.#
c7d40 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 define O_LARGEFI
c7d50 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e LE 0.#endif.#ifn
c7d60 64 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 def O_NOFOLLOW.#
c7d70 20 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c define O_NOFOLL
c7d80 4f 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e OW 0.#endif.#ifn
c7d90 64 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 def O_BINARY.# d
c7da0 65 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 efine O_BINARY 0
c7db0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
c7dc0 68 65 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 he DJGPP compile
c7dd0 72 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f r environment lo
c7de0 6f 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 oks mostly like
c7df0 55 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 Unix, but it.**
c7e00 6c 61 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 lacks the fcntl(
c7e10 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 ) system call.
c7e20 53 6f 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 So redefine fcnt
c7e30 6c 28 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 l() to be someth
c7e40 69 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 ing.** that alwa
c7e50 79 73 20 73 75 63 63 65 65 64 73 2e 20 20 54 68 ys succeeds. Th
c7e60 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f is means that lo
c7e70 63 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f cking does not o
c7e80 63 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a ccur under.** DJ
c7e90 47 50 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 GPP. But it is
c7ea0 44 4f 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 DOS - what did y
c7eb0 6f 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 ou expect?.*/.#i
c7ec0 66 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 fdef __DJGPP__.#
c7ed0 20 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c define fcntl(A,
c7ee0 42 2c 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f B,C) 0.#endif../
c7ef0 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69 *.** The threadi
c7f00 64 20 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 d macro resolves
c7f10 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69 to the thread-i
c7f20 64 20 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64 d or to 0. Used
c7f30 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 for.** testing
c7f40 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e and debugging on
c7f50 6c 79 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ly..*/.#if SQLIT
c7f60 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 64 65 E_THREADSAFE.#de
c7f70 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 70 74 fine threadid pt
c7f80 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c hread_self().#el
c7f90 73 65 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 se.#define threa
c7fa0 64 69 64 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a did 0.#endif../*
c7fb0 0a 2a 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b .** Set or check
c7fc0 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69 the unixFile.ti
c7fd0 64 20 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66 d field. This f
c7fe0 69 65 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e ield is set when
c7ff0 20 61 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 an unixFile.**
c8000 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e is first opened.
c8010 20 20 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 All subsequent
c8020 20 75 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69 uses of the uni
c8030 78 46 69 6c 65 20 76 65 72 69 66 79 20 74 68 61 xFile verify tha
c8040 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68 t the.** same th
c8050 72 65 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e read is operatin
c8060 67 20 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c g on the unixFil
c8070 65 2e 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 e. Some operati
c8080 6e 67 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a ng systems do.**
c8090 20 6e 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 not allow locks
c80a0 20 74 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65 to be overridde
c80b0 6e 20 62 79 20 6f 74 68 65 72 20 74 68 72 65 61 n by other threa
c80c0 64 73 20 61 6e 64 20 74 68 61 74 20 72 65 73 74 ds and that rest
c80d0 72 69 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 riction.** means
c80e0 20 74 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64 that sqlite3* d
c80f0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 atabase handles
c8100 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 cannot be moved
c8110 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a from one thread.
c8120 2a 2a 20 74 6f 20 61 6e 6f 74 68 65 72 2e 20 20 ** to another.
c8130 54 68 69 73 20 6c 6f 67 69 63 20 6d 61 6b 65 73 This logic makes
c8140 20 73 75 72 65 20 61 20 75 73 65 72 20 64 6f 65 sure a user doe
c8150 73 20 6e 6f 74 20 74 72 79 20 74 6f 20 64 6f 20 s not try to do
c8160 74 68 61 74 0a 2a 2a 20 62 79 20 6d 69 73 74 61 that.** by mista
c8170 6b 65 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f ke..**.** Versio
c8180 6e 20 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31 n 3.3.1 (2006-01
c8190 2d 31 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20 -15): unixFile
c81a0 63 61 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f can be moved fro
c81b0 6d 20 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a m one thread to.
c81c0 2a 2a 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f ** another as lo
c81d0 6e 67 20 61 73 20 77 65 20 61 72 65 20 72 75 6e ng as we are run
c81e0 6e 69 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d ning on a system
c81f0 20 74 68 61 74 20 73 75 70 70 6f 72 74 73 20 74 that supports t
c8200 68 72 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 hreads.** overri
c8210 64 69 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73 ding each others
c8220 20 6c 6f 63 6b 73 20 28 77 68 69 63 68 20 6e 6f locks (which no
c8230 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f w the most commo
c8240 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f n behavior).** o
c8250 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 r if no locks ar
c8260 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65 e held. But the
c8270 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 unixFile.pLock
c8280 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 field needs to b
c8290 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 e.** recomputed
c82a0 62 65 63 61 75 73 65 20 69 74 73 20 6b 65 79 20 because its key
c82b0 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68 72 includes the thr
c82c0 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65 ead-id. See the
c82d0 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e .** transferOwn
c82e0 65 72 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f ership() functio
c82f0 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 n below for addi
c8300 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
c8310 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 on.*/.#if SQLITE
c8320 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 _THREADSAFE.# de
c8330 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49 fine SET_THREADI
c8340 44 28 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20 D(X) (X)->tid
c8350 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
c8360 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f .# define CHECK_
c8370 54 48 52 45 41 44 49 44 28 58 29 20 28 74 68 72 THREADID(X) (thr
c8380 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 eadsOverrideEach
c8390 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 OthersLocks==0 &
c83a0 26 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 & \.
c83b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c83c0 21 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28 !pthread_equal((
c83d0 58 29 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64 X)->tid, pthread
c83e0 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a _self())).#else.
c83f0 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 # define SET_THR
c8400 45 41 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e EADID(X).# defin
c8410 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 e CHECK_THREADID
c8420 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a (X) 0.#endif../*
c8430 0a 2a 2a 20 48 65 72 65 20 69 73 20 74 68 65 20 .** Here is the
c8440 64 69 72 74 20 6f 6e 20 50 4f 53 49 58 20 61 64 dirt on POSIX ad
c8450 76 69 73 6f 72 79 20 6c 6f 63 6b 73 3a 20 20 41 visory locks: A
c8460 4e 53 49 20 53 54 44 20 31 30 30 33 2e 31 20 28 NSI STD 1003.1 (
c8470 31 39 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 1996).** section
c8480 20 36 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34 6.5.2.2 lines 4
c8490 38 33 20 74 68 72 6f 75 67 68 20 34 39 30 20 73 83 through 490 s
c84a0 70 65 63 69 66 79 20 74 68 61 74 20 77 68 65 6e pecify that when
c84b0 20 61 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65 a process.** se
c84c0 74 73 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c ts or clears a l
c84d0 6f 63 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74 ock, that operat
c84e0 69 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e ion overrides an
c84f0 79 20 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65 y prior locks se
c8500 74 0a 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65 t.** by the same
c8510 20 70 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f process. It do
c8520 65 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c es not explicitl
c8530 79 20 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68 y say so, but th
c8540 69 73 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 is implies.** th
c8550 61 74 20 69 74 20 6f 76 65 72 72 69 64 65 73 20 at it overrides
c8560 6c 6f 63 6b 73 20 73 65 74 20 62 79 20 74 68 65 locks set by the
c8570 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73 same process us
c8580 69 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a ing a different.
c8590 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ** file descript
c85a0 6f 72 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68 or. Consider th
c85b0 69 73 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a is test case:.**
c85c0 20 20 20 20 20 20 20 69 6e 74 20 66 64 32 20 3d int fd2 =
c85d0 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 32 22 2c open("./file2",
c85e0 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c O_RDWR|O_CREAT,
c85f0 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 0644);.**.** Su
c8600 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31 20 61 6e ppose ./file1 an
c8610 64 20 2e 2f 66 69 6c 65 32 20 61 72 65 20 72 65 d ./file2 are re
c8620 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 66 69 ally the same fi
c8630 6c 65 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 6f le (because.** o
c8640 6e 65 20 69 73 20 61 20 68 61 72 64 20 6f 72 20 ne is a hard or
c8650 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f symbolic link to
c8660 20 74 68 65 20 6f 74 68 65 72 29 20 74 68 65 6e the other) then
c8670 20 69 66 20 79 6f 75 20 73 65 74 0a 2a 2a 20 61 if you set.** a
c8680 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
c8690 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e 20 74 72 on fd1, then tr
c86a0 79 20 74 6f 20 67 65 74 20 61 6e 20 65 78 63 6c y to get an excl
c86b0 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e usive lock.** on
c86c0 20 66 64 32 2c 20 69 74 20 77 6f 72 6b 73 2e 20 fd2, it works.
c86d0 20 49 20 77 6f 75 6c 64 20 68 61 76 65 20 65 78 I would have ex
c86e0 70 65 63 74 65 64 20 74 68 65 20 73 65 63 6f 6e pected the secon
c86f0 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20 66 61 69 d lock to.** fai
c8700 6c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 61 l since there wa
c8710 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b s already a lock
c8720 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 64 75 65 on the file due
c8730 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42 75 74 20 to fd1..** But
c8740 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 65 20 62 not so. Since b
c8750 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d 65 20 66 oth locks came f
c8760 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 70 72 6f rom the same pro
c8770 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20 73 65 63 cess, the.** sec
c8780 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 20 74 68 ond overrides th
c8790 65 20 66 69 72 73 74 2c 20 65 76 65 6e 20 74 68 e first, even th
c87a0 6f 75 67 68 20 74 68 65 79 20 77 65 72 65 20 6f ough they were o
c87b0 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 n different.** f
c87c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
c87d0 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66 66 65 72 opened on differ
c87e0 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65 73 2e 0a ent file names..
c87f0 2a 2a 0a 2a 2a 20 42 75 6d 6d 65 72 2e 20 20 49 **.** Bummer. I
c8800 66 20 79 6f 75 20 61 73 6b 20 6d 65 2c 20 74 68 f you ask me, th
c8810 69 73 20 69 73 20 62 72 6f 6b 65 6e 2e 20 20 42 is is broken. B
c8820 61 64 6c 79 20 62 72 6f 6b 65 6e 2e 20 20 49 74 adly broken. It
c8830 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 means.** that w
c8840 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 e cannot use POS
c8850 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 IX locks to sync
c8860 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 hronize file acc
c8870 65 73 73 20 61 6d 6f 6e 67 0a 2a 2a 20 63 6f 6d ess among.** com
c8880 70 65 74 69 6e 67 20 74 68 72 65 61 64 73 20 6f peting threads o
c8890 66 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 f the same proce
c88a0 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 ss. POSIX locks
c88b0 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a will work fine.
c88c0 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a ** to synchroniz
c88d0 65 20 61 63 63 65 73 73 20 66 6f 72 20 74 68 72 e access for thr
c88e0 65 61 64 73 20 69 6e 20 73 65 70 61 72 61 74 65 eads in separate
c88f0 20 70 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20 processes, but
c8900 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 not.** threads w
c8910 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 ithin the same p
c8920 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f rocess..**.** To
c8930 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 work around the
c8940 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 problem, SQLite
c8950 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 has to manage f
c8960 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e ile locks intern
c8970 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f ally.** on its o
c8980 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 61 20 wn. Whenever a
c8990 6e 65 77 20 64 61 74 61 62 61 73 65 20 69 73 20 new database is
c89a0 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76 65 20 opened, we have
c89b0 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 to find the.** s
c89c0 70 65 63 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 pecific inode of
c89d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
c89e0 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 20 69 73 le (the inode is
c89f0 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 determined by t
c8a00 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 he.** st_dev and
c8a10 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f st_ino fields o
c8a20 66 20 74 68 65 20 73 74 61 74 20 73 74 72 75 63 f the stat struc
c8a30 74 75 72 65 20 74 68 61 74 20 66 73 74 61 74 28 ture that fstat(
c8a40 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 ) fills in).** a
c8a50 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 nd check for loc
c8a60 6b 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ks already exist
c8a70 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 ing on that inod
c8a80 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 e. When locks a
c8a90 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 re.** created or
c8aa0 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 removed, we hav
c8ab0 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 e to look at our
c8ac0 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 own internal re
c8ad0 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c cord of the.** l
c8ae0 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 ocks to see if a
c8af0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 68 61 nother thread ha
c8b00 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 s previously set
c8b10 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 a lock on that
c8b20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a same.** inode..*
c8b30 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
c8b40 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 _file structure
c8b50 66 6f 72 20 50 4f 53 49 58 20 69 73 20 6e 6f 20 for POSIX is no
c8b60 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61 6e 20 69 longer just an i
c8b70 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 nteger file.** d
c8b80 65 73 63 72 69 70 74 6f 72 2e 20 20 49 74 20 69 escriptor. It i
c8b90 73 20 6e 6f 77 20 61 20 73 74 72 75 63 74 75 72 s now a structur
c8ba0 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 e that holds the
c8bb0 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a integer file.**
c8bc0 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64 20 descriptor and
c8bd0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 a pointer to a s
c8be0 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 tructure that de
c8bf0 73 63 72 69 62 65 73 20 74 68 65 20 69 6e 74 65 scribes the inte
c8c00 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e rnal.** locks on
c8c10 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
c8c20 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68 65 72 65 ng inode. There
c8c30 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 is one locking
c8c40 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65 72 structure.** per
c8c50 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66 20 74 68 inode, so if th
c8c60 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 69 73 20 e same inode is
c8c70 6f 70 65 6e 65 64 20 74 77 69 63 65 2c 20 62 6f opened twice, bo
c8c80 74 68 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 th unixFile stru
c8c90 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 ctures.** point
c8ca0 74 6f 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b to the same lock
c8cb0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20 20 ing structure.
c8cc0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 The locking stru
c8cd0 63 74 75 72 65 20 6b 65 65 70 73 0a 2a 2a 20 61 cture keeps.** a
c8ce0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
c8cf0 20 28 73 6f 20 77 65 20 77 69 6c 6c 20 6b 6e 6f (so we will kno
c8d00 77 20 77 68 65 6e 20 74 6f 20 64 65 6c 65 74 65 w when to delete
c8d10 20 69 74 29 20 61 6e 64 20 61 20 22 63 6e 74 22 it) and a "cnt"
c8d20 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61 74 20 74 .** field that t
c8d30 65 6c 6c 73 20 75 73 20 69 74 73 20 69 6e 74 65 ells us its inte
c8d40 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 75 73 rnal lock status
c8d50 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 . cnt==0 means
c8d60 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 75 the.** file is u
c8d70 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d nlocked. cnt==-
c8d80 31 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 1 means the file
c8d90 20 68 61 73 20 61 6e 20 65 78 63 6c 75 73 69 76 has an exclusiv
c8da0 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 e lock..** cnt>0
c8db0 20 6d 65 61 6e 73 20 74 68 65 72 65 20 61 72 65 means there are
c8dc0 20 63 6e 74 20 73 68 61 72 65 64 20 6c 6f 63 6b cnt shared lock
c8dd0 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a s on the file..*
c8de0 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 *.** Any attempt
c8df0 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f to lock or unlo
c8e00 63 6b 20 61 20 66 69 6c 65 20 66 69 72 73 74 20 ck a file first
c8e10 63 68 65 63 6b 73 20 74 68 65 20 6c 6f 63 6b 69 checks the locki
c8e20 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ng.** structure.
c8e30 20 20 54 68 65 20 66 63 6e 74 6c 28 29 20 73 79 The fcntl() sy
c8e40 73 74 65 6d 20 63 61 6c 6c 20 69 73 20 6f 6e 6c stem call is onl
c8e50 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 65 74 y invoked to set
c8e60 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 a .** POSIX loc
c8e70 6b 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 k if the interna
c8e80 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74 75 72 65 l lock structure
c8e90 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 65 74 transitions bet
c8ea0 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 ween.** a locked
c8eb0 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 and an unlocked
c8ec0 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 state..**.** 20
c8ed0 30 34 2d 4a 61 6e 2d 31 31 3a 0a 2a 2a 20 4d 6f 04-Jan-11:.** Mo
c8ee0 72 65 20 72 65 63 65 6e 74 20 64 69 73 63 6f 76 re recent discov
c8ef0 65 72 69 65 73 20 61 62 6f 75 74 20 50 4f 53 49 eries about POSI
c8f00 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 X advisory locks
c8f10 2e 20 20 28 54 68 65 20 6d 6f 72 65 0a 2a 2a 20 . (The more.**
c8f20 49 20 64 69 73 63 6f 76 65 72 2c 20 74 68 65 20 I discover, the
c8f30 6d 6f 72 65 20 49 20 72 65 61 6c 69 7a 65 20 74 more I realize t
c8f40 68 65 20 61 20 50 4f 53 49 58 20 61 64 76 69 73 he a POSIX advis
c8f50 6f 72 79 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a ory locks are.**
c8f60 20 61 6e 20 61 62 6f 6d 69 6e 61 74 69 6f 6e 2e an abomination.
c8f70 29 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 63 ).**.** If you c
c8f80 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 63 lose a file desc
c8f90 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69 6e riptor that poin
c8fa0 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68 61 ts to a file tha
c8fb0 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 t has locks,.**
c8fc0 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 all locks on tha
c8fd0 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 t file that are
c8fe0 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63 75 72 owned by the cur
c8ff0 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72 65 rent process are
c9000 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20 54 .** released. T
c9010 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 o work around th
c9020 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63 68 is problem, each
c9030 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 unixFile struct
c9040 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 ure contains.**
c9050 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
c9060 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 openCnt structur
c9070 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 e. There is one
c9080 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 openCnt structu
c9090 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 re.** per open i
c90a0 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 61 6e node, which mean
c90b0 73 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 s that multiple
c90c0 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 unixFile can poi
c90d0 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a nt to a single.*
c90e0 2a 20 6f 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e * openCnt. When
c90f0 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d an attempt is m
c9100 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20 ade to close an
c9110 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 68 65 unixFile, if the
c9120 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20 re are.** other
c9130 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e unixFile open on
c9140 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 the same inode
c9150 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 6e 67 that are holding
c9160 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c locks, the call
c9170 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20 74 .** to close() t
c9180 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
c9190 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 20 75 or is deferred u
c91a0 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 ntil all of the
c91b0 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 locks clear..**
c91c0 54 68 65 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 The openCnt stru
c91d0 63 74 75 72 65 20 6b 65 65 70 73 20 61 20 6c 69 cture keeps a li
c91e0 73 74 20 6f 66 20 66 69 6c 65 20 64 65 73 63 72 st of file descr
c91f0 69 70 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64 iptors that need
c9200 20 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 65 64 to.** be closed
c9210 20 61 6e 64 20 74 68 61 74 20 6c 69 73 74 20 69 and that list i
c9220 73 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 63 6c s walked (and cl
c9230 65 61 72 65 64 29 20 77 68 65 6e 20 74 68 65 20 eared) when the
c9240 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 last lock.** cle
c9250 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 46 69 72 73 74 ars..**.** First
c9260 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 20 74 68 , under Linux th
c9270 72 65 61 64 73 2c 20 62 65 63 61 75 73 65 20 65 reads, because e
c9280 61 63 68 20 74 68 72 65 61 64 20 68 61 73 20 61 ach thread has a
c9290 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 70 72 6f separate.** pro
c92a0 63 65 73 73 20 49 44 2c 20 6c 6f 63 6b 20 6f 70 cess ID, lock op
c92b0 65 72 61 74 69 6f 6e 73 20 69 6e 20 6f 6e 65 20 erations in one
c92c0 74 68 72 65 61 64 20 64 6f 20 6e 6f 74 20 6f 76 thread do not ov
c92d0 65 72 72 69 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20 erride locks.**
c92e0 74 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 to the same file
c92f0 20 69 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64 in other thread
c9300 73 2e 20 20 4c 69 6e 75 78 20 74 68 72 65 61 64 s. Linux thread
c9310 73 20 62 65 68 61 76 65 20 6c 69 6b 65 0a 2a 2a s behave like.**
c9320 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 separate proces
c9330 73 65 73 20 69 6e 20 74 68 69 73 20 72 65 73 70 ses in this resp
c9340 65 63 74 2e 20 20 42 75 74 2c 20 69 66 20 79 6f ect. But, if yo
c9350 75 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 0a 2a u close a file.*
c9360 2a 20 64 65 73 63 72 69 70 74 6f 72 20 69 6e 20 * descriptor in
c9370 6c 69 6e 75 78 20 74 68 72 65 61 64 73 2c 20 61 linux threads, a
c9380 6c 6c 20 6c 6f 63 6b 73 20 61 72 65 20 63 6c 65 ll locks are cle
c9390 61 72 65 64 2c 20 65 76 65 6e 20 6c 6f 63 6b 73 ared, even locks
c93a0 0a 2a 2a 20 6f 6e 20 6f 74 68 65 72 20 74 68 72 .** on other thr
c93b0 65 61 64 73 20 61 6e 64 20 65 76 65 6e 20 74 68 eads and even th
c93c0 6f 75 67 68 20 74 68 65 20 6f 74 68 65 72 20 74 ough the other t
c93d0 68 72 65 61 64 73 20 68 61 76 65 20 64 69 66 66 hreads have diff
c93e0 65 72 65 6e 74 0a 2a 2a 20 70 72 6f 63 65 73 73 erent.** process
c93f0 20 49 44 73 2e 20 20 4c 69 6e 75 78 20 74 68 72 IDs. Linux thr
c9400 65 61 64 73 20 69 73 20 69 6e 63 6f 6e 73 69 73 eads is inconsis
c9410 74 65 6e 74 20 69 6e 20 74 68 69 73 20 72 65 73 tent in this res
c9420 70 65 63 74 2e 0a 2a 2a 20 28 49 27 6d 20 62 65 pect..** (I'm be
c9430 67 69 6e 6e 69 6e 67 20 74 6f 20 74 68 69 6e 6b ginning to think
c9440 20 74 68 61 74 20 6c 69 6e 75 78 20 74 68 72 65 that linux thre
c9450 61 64 73 20 69 73 20 61 6e 20 61 62 6f 6d 69 6e ads is an abomin
c9460 61 74 69 6f 6e 20 74 6f 6f 2e 29 0a 2a 2a 20 54 ation too.).** T
c9470 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 65 20 6f he consequence o
c9480 66 20 74 68 69 73 20 61 6c 6c 20 69 73 20 74 68 f this all is th
c9490 61 74 20 74 68 65 20 68 61 73 68 20 74 61 62 6c at the hash tabl
c94a0 65 20 66 6f 72 20 74 68 65 20 6c 6f 63 6b 49 6e e for the lockIn
c94b0 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 fo.** structure
c94c0 68 61 73 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 has to include t
c94d0 68 65 20 70 72 6f 63 65 73 73 20 69 64 20 61 73 he process id as
c94e0 20 70 61 72 74 20 6f 66 20 69 74 73 20 6b 65 79 part of its key
c94f0 20 62 65 63 61 75 73 65 0a 2a 2a 20 6c 6f 63 6b because.** lock
c9500 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 74 s in different t
c9510 68 72 65 61 64 73 20 61 72 65 20 74 72 65 61 74 hreads are treat
c9520 65 64 20 61 73 20 64 69 73 74 69 6e 63 74 2e 20 ed as distinct.
c9530 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 6f 70 65 But the .** ope
c9540 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 73 nCnt structure s
c9550 68 6f 75 6c 64 20 6e 6f 74 20 69 6e 63 6c 75 64 hould not includ
c9560 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 69 64 e the process id
c9570 20 69 6e 20 69 74 73 0a 2a 2a 20 6b 65 79 20 62 in its.** key b
c9580 65 63 61 75 73 65 20 63 6c 6f 73 65 28 29 20 63 ecause close() c
c9590 6c 65 61 72 73 20 6c 6f 63 6b 20 6f 6e 20 61 6c lears lock on al
c95a0 6c 20 74 68 72 65 61 64 73 2c 20 6e 6f 74 20 6a l threads, not j
c95b0 75 73 74 20 74 68 65 20 63 75 72 72 65 6e 74 0a ust the current.
c95c0 2a 2a 20 74 68 72 65 61 64 2e 20 20 57 65 72 65 ** thread. Were
c95d0 20 69 74 20 6e 6f 74 20 66 6f 72 20 74 68 69 73 it not for this
c95e0 20 67 6f 6f 66 69 6e 65 73 73 20 69 6e 20 6c 69 goofiness in li
c95f0 6e 75 78 20 74 68 72 65 61 64 73 2c 20 77 65 20 nux threads, we
c9600 63 6f 75 6c 64 0a 2a 2a 20 63 6f 6d 62 69 6e 65 could.** combine
c9610 20 74 68 65 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e the lockInfo an
c9620 64 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 d openCnt struct
c9630 75 72 65 73 20 69 6e 74 6f 20 61 20 73 69 6e 67 ures into a sing
c9640 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a le structure..**
c9650 0a 2a 2a 20 32 30 30 34 2d 4a 75 6e 2d 32 38 3a .** 2004-Jun-28:
c9660 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 20 76 65 72 73 .** On some vers
c9670 69 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 2c 20 74 ions of linux, t
c9680 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 hreads can overr
c9690 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 ide each others
c96a0 6c 6f 63 6b 73 2e 0a 2a 2a 20 4f 6e 20 6f 74 68 locks..** On oth
c96b0 65 72 73 20 6e 6f 74 2e 20 20 53 6f 6d 65 74 69 ers not. Someti
c96c0 6d 65 73 20 79 6f 75 20 63 61 6e 20 63 68 61 6e mes you can chan
c96d0 67 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 ge the behavior
c96e0 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 73 on the same.** s
c96f0 79 73 74 65 6d 20 62 79 20 73 65 74 74 69 6e 67 ystem by setting
c9700 20 74 68 65 20 4c 44 5f 41 53 53 55 4d 45 5f 4b the LD_ASSUME_K
c9710 45 52 4e 45 4c 20 65 6e 76 69 72 6f 6e 6d 65 6e ERNEL environmen
c9720 74 20 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65 t variable. The
c9730 0a 2a 2a 20 50 4f 53 49 58 20 73 74 61 6e 64 61 .** POSIX standa
c9740 72 64 20 69 73 20 73 69 6c 65 6e 74 20 61 73 20 rd is silent as
c9750 74 6f 20 77 68 69 63 68 20 62 65 68 61 76 69 6f to which behavio
c9760 72 20 69 73 20 63 6f 72 72 65 63 74 2c 20 61 73 r is correct, as
c9770 20 66 61 72 0a 2a 2a 20 61 73 20 49 20 63 61 6e far.** as I can
c9780 20 74 65 6c 6c 2c 20 73 6f 20 6f 74 68 65 72 20 tell, so other
c9790 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69 78 versions of unix
c97a0 20 6d 69 67 68 74 20 73 68 6f 77 20 74 68 65 20 might show the
c97b0 73 61 6d 65 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 same.** inconsis
c97c0 74 65 6e 63 79 2e 20 20 54 68 65 72 65 20 69 73 tency. There is
c97d0 20 6e 6f 20 6c 69 74 74 6c 65 20 64 6f 75 62 74 no little doubt
c97e0 20 69 6e 20 6d 79 20 6d 69 6e 64 20 74 68 61 74 in my mind that
c97f0 20 70 6f 73 69 78 0a 2a 2a 20 61 64 76 69 73 6f posix.** adviso
c9800 72 79 20 6c 6f 63 6b 73 20 61 6e 64 20 6c 69 6e ry locks and lin
c9810 75 78 20 74 68 72 65 61 64 73 20 61 72 65 20 70 ux threads are p
c9820 72 6f 66 6f 75 6e 64 6c 79 20 62 72 6f 6b 65 6e rofoundly broken
c9830 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 ..**.** To work
c9840 61 72 6f 75 6e 64 20 74 68 65 20 69 6e 63 6f 6e around the incon
c9850 73 69 73 74 65 6e 63 69 65 73 2c 20 77 65 20 68 sistencies, we h
c9860 61 76 65 20 74 6f 20 74 65 73 74 20 61 74 20 72 ave to test at r
c9870 75 6e 74 69 6d 65 20 0a 2a 2a 20 77 68 65 74 68 untime .** wheth
c9880 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 er or not thread
c9890 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 s can override e
c98a0 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 ach others locks
c98b0 2e 20 20 54 68 69 73 20 74 65 73 74 0a 2a 2a 20 . This test.**
c98c0 69 73 20 72 75 6e 20 6f 6e 63 65 2c 20 74 68 65 is run once, the
c98d0 20 66 69 72 73 74 20 74 69 6d 65 20 61 6e 79 20 first time any
c98e0 6c 6f 63 6b 20 69 73 20 61 74 74 65 6d 70 74 65 lock is attempte
c98f0 64 2e 20 20 41 20 73 74 61 74 69 63 20 0a 2a 2a d. A static .**
c9900 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 variable is set
c9910 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 72 to record the r
c9920 65 73 75 6c 74 73 20 6f 66 20 74 68 69 73 20 74 esults of this t
c9930 65 73 74 20 66 6f 72 20 66 75 74 75 72 65 0a 2a est for future.*
c9940 2a 20 75 73 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a * use..*/../*.**
c9950 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
c9960 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
c9970 72 75 63 74 75 72 65 20 73 65 72 76 65 73 20 61 ructure serves a
c9980 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a s the key used.*
c9990 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 * to locate a pa
c99a0 72 74 69 63 75 6c 61 72 20 6c 6f 63 6b 49 6e 66 rticular lockInf
c99b0 6f 20 73 74 72 75 63 74 75 72 65 20 67 69 76 65 o structure give
c99c0 6e 20 69 74 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a n its inode..**.
c99d0 2a 2a 20 49 66 20 74 68 72 65 61 64 73 20 63 61 ** If threads ca
c99e0 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65 61 nnot override ea
c99f0 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2c ch others locks,
c9a00 20 74 68 65 6e 20 77 65 20 73 65 74 20 74 68 65 then we set the
c9a10 0a 2a 2a 20 6c 6f 63 6b 4b 65 79 2e 74 69 64 20 .** lockKey.tid
c9a20 66 69 65 6c 64 20 74 6f 20 74 68 65 20 74 68 72 field to the thr
c9a30 65 61 64 20 49 44 2e 20 20 49 66 20 74 68 72 65 ead ID. If thre
c9a40 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 ads can override
c9a50 0a 2a 2a 20 65 61 63 68 20 6f 74 68 65 72 73 20 .** each others
c9a60 6c 6f 63 6b 73 20 74 68 65 6e 20 74 69 64 20 69 locks then tid i
c9a70 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 s always set to
c9a80 7a 65 72 6f 2e 20 20 74 69 64 20 69 73 20 6f 6d zero. tid is om
c9a90 69 74 74 65 64 0a 2a 2a 20 69 66 20 77 65 20 63 itted.** if we c
c9aa0 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 74 ompile without t
c9ab0 68 72 65 61 64 69 6e 67 20 73 75 70 70 6f 72 74 hreading support
c9ac0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 6c 6f 63 6b ..*/.struct lock
c9ad0 4b 65 79 20 7b 0a 20 20 64 65 76 5f 74 20 64 65 Key {. dev_t de
c9ae0 76 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 v; /* Devi
c9af0 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 ce number */. i
c9b00 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20 20 no_t ino;
c9b10 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 /* Inode number
c9b20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
c9b30 52 45 41 44 53 41 46 45 0a 20 20 70 74 68 72 65 READSAFE. pthre
c9b40 61 64 5f 74 20 74 69 64 3b 20 20 20 2f 2a 20 54 ad_t tid; /* T
c9b50 68 72 65 61 64 20 49 44 20 6f 72 20 7a 65 72 6f hread ID or zero
c9b60 20 69 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 if threads can
c9b70 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 override each ot
c9b80 68 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b her */.#endif.};
c9b90 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
c9ba0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
c9bb0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 wing structure i
c9bc0 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
c9bd0 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f each open.** ino
c9be0 64 65 20 6f 6e 20 65 61 63 68 20 74 68 72 65 61 de on each threa
c9bf0 64 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 d with a differe
c9c00 6e 74 20 70 72 6f 63 65 73 73 20 49 44 2e 20 20 nt process ID.
c9c10 28 54 68 72 65 61 64 73 20 68 61 76 65 0a 2a 2a (Threads have.**
c9c20 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f 63 65 different proce
c9c30 73 73 20 49 44 73 20 6f 6e 20 6c 69 6e 75 78 2c ss IDs on linux,
c9c40 20 62 75 74 20 6e 6f 74 20 6f 6e 20 6d 6f 73 74 but not on most
c9c50 20 6f 74 68 65 72 20 75 6e 69 78 65 73 2e 29 0a other unixes.).
c9c60 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 **.** A single i
c9c70 6e 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 node can have mu
c9c80 6c 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 ltiple file desc
c9c90 72 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 riptors, so each
c9ca0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 unixFile.** str
c9cb0 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 ucture contains
c9cc0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
c9cd0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
c9ce0 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 object and this
c9cf0 0a 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 .** object keeps
c9d00 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 a count of the
c9d10 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 number of unixFi
c9d20 6c 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 le pointing to i
c9d30 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 6c 6f 63 t..*/.struct loc
c9d40 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 kInfo {. struct
c9d50 20 6c 6f 63 6b 4b 65 79 20 6b 65 79 3b 20 20 2f lockKey key; /
c9d60 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 * The lookup key
c9d70 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 */. int cnt;
c9d80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
c9d90 6d 62 65 72 20 6f 66 20 53 48 41 52 45 44 20 6c mber of SHARED l
c9da0 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20 69 ocks held */. i
c9db0 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 nt locktype;
c9dc0 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 /* One of SH
c9dd0 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 ARED_LOCK, RESER
c9de0 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f VED_LOCK etc. */
c9df0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
c9e00 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
c9e10 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f r of pointers to
c9e20 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
c9e30 2a 2f 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b */. struct lock
c9e40 49 6e 66 6f 20 2a 70 4e 65 78 74 2c 20 2a 70 50 Info *pNext, *pP
c9e50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f rev; /* List o
c9e60 66 20 61 6c 6c 20 6c 6f 63 6b 49 6e 66 6f 20 6f f all lockInfo o
c9e70 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a bjects */.};../*
c9e80 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
c9e90 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
c9ea0 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 structure serve
c9eb0 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 s as the key use
c9ec0 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 d.** to locate a
c9ed0 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 6e particular open
c9ee0 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 67 69 Cnt structure gi
c9ef0 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 20 20 ven its inode.
c9f00 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 73 This.** is the s
c9f10 61 6d 65 20 61 73 20 74 68 65 20 6c 6f 63 6b 4b ame as the lockK
c9f20 65 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74 ey except that t
c9f30 68 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20 he thread ID is
c9f40 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 omitted..*/.stru
c9f50 63 74 20 6f 70 65 6e 4b 65 79 20 7b 0a 20 20 64 ct openKey {. d
c9f60 65 76 5f 74 20 64 65 76 3b 20 20 20 2f 2a 20 44 ev_t dev; /* D
c9f70 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a evice number */.
c9f80 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 2f ino_t ino; /
c9f90 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a * Inode number *
c9fa0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 /.};../*.** An i
c9fb0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
c9fc0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
c9fd0 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 re is allocated
c9fe0 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a for each open.**
c9ff0 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74 inode. This st
ca000 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 ructure keeps tr
ca010 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ack of the numbe
ca020 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 r of locks on th
ca030 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66 at.** inode. If
ca040 20 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65 a close is atte
ca050 6d 70 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e mpted against an
ca060 20 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68 inode that is h
ca070 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c olding.** locks,
ca080 20 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65 the close is de
ca090 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c ferred until all
ca0a0 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20 locks clear by
ca0b0 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 adding the.** fi
ca0c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f le descriptor to
ca0d0 20 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68 be closed to th
ca0e0 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a e pending list..
ca0f0 2a 2f 0a 73 74 72 75 63 74 20 6f 70 65 6e 43 6e */.struct openCn
ca100 74 20 7b 0a 20 20 73 74 72 75 63 74 20 6f 70 65 t {. struct ope
ca110 6e 4b 65 79 20 6b 65 79 3b 20 20 20 2f 2a 20 54 nKey key; /* T
ca120 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f he lookup key */
ca130 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
ca140 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
ca150 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 er of pointers t
ca160 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 o this structure
ca170 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b */. int nLock;
ca180 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ca190 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e umber of outstan
ca1a0 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 ding locks */.
ca1b0 69 6e 74 20 6e 50 65 6e 64 69 6e 67 3b 20 20 20 int nPending;
ca1c0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ca1d0 6f 66 20 70 65 6e 64 69 6e 67 20 63 6c 6f 73 65 of pending close
ca1e0 28 29 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f () operations */
ca1f0 0a 20 20 69 6e 74 20 2a 61 50 65 6e 64 69 6e 67 . int *aPending
ca200 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c ; /* Mall
ca210 6f 63 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69 oced space holdi
ca220 6e 67 20 66 64 27 73 20 61 77 61 69 74 69 6e 67 ng fd's awaiting
ca230 20 61 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 20 20 a close() */.
ca240 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a struct openCnt *
ca250 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 pNext, *pPrev;
ca260 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 /* List of all
ca270 6f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 openCnt objects
ca280 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 */.};../*.** Lis
ca290 74 20 6f 66 20 61 6c 6c 20 6c 6f 63 6b 49 6e 66 t of all lockInf
ca2a0 6f 20 61 6e 64 20 6f 70 65 6e 43 6e 74 20 6f 62 o and openCnt ob
ca2b0 6a 65 63 74 73 2e 20 20 54 68 69 73 20 75 73 65 jects. This use
ca2c0 64 20 74 6f 20 62 65 20 61 20 68 61 73 68 0a 2a d to be a hash.*
ca2d0 2a 20 74 61 62 6c 65 2e 20 20 42 75 74 20 74 68 * table. But th
ca2e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 e number of obje
ca2f0 63 74 73 20 69 73 20 72 61 72 65 6c 79 20 6d 6f cts is rarely mo
ca300 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20 re than a dozen
ca310 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 20 65 78 63 and.** never exc
ca320 65 65 64 73 20 61 20 66 65 77 20 74 68 6f 75 73 eeds a few thous
ca330 61 6e 64 2e 20 20 41 6e 64 20 6c 6f 6f 6b 75 70 and. And lookup
ca340 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 63 72 69 is not on a cri
ca350 74 69 63 61 6c 0a 2a 2a 20 70 61 74 68 20 6f 6f tical.** path oo
ca360 20 61 20 73 69 6d 70 6c 65 20 6c 69 6e 6b 65 64 a simple linked
ca370 20 6c 69 73 74 20 77 69 6c 6c 20 73 75 66 66 69 list will suffi
ca380 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 ce..*/.static st
ca390 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 6c ruct lockInfo *l
ca3a0 6f 63 6b 4c 69 73 74 20 3d 20 30 3b 0a 73 74 61 ockList = 0;.sta
ca3b0 74 69 63 20 73 74 72 75 63 74 20 6f 70 65 6e 43 tic struct openC
ca3c0 6e 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 nt *openList = 0
ca3d0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c 6f 63 ;../*.** The loc
ca3e0 6b 69 6e 67 20 73 74 79 6c 65 73 20 61 72 65 20 king styles are
ca3f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
ca400 74 68 65 20 64 69 66 66 65 72 65 6e 74 20 66 69 the different fi
ca410 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63 61 le locking.** ca
ca420 70 61 62 69 6c 69 74 69 65 73 20 73 75 70 70 6f pabilities suppo
ca430 72 74 65 64 20 62 79 20 64 69 66 66 65 72 65 6e rted by differen
ca440 74 20 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20 t file systems.
ca450 20 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f .**.** POSIX lo
ca460 63 6b 69 6e 67 20 73 74 79 6c 65 20 66 75 6c 6c cking style full
ca470 79 20 73 75 70 70 6f 72 74 73 20 73 68 61 72 65 y supports share
ca480 64 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 20 d and exclusive
ca490 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 byte-range locks
ca4a0 20 0a 2a 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67 .** AFP locking
ca4b0 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 73 20 65 only supports e
ca4c0 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 xclusive byte-ra
ca4d0 6e 67 65 20 6c 6f 63 6b 73 0a 2a 2a 20 46 4c 4f nge locks.** FLO
ca4e0 43 4b 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 73 CK only supports
ca4f0 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 2d 67 a single file-g
ca500 6c 6f 62 61 6c 20 65 78 63 6c 75 73 69 76 65 20 lobal exclusive
ca510 6c 6f 63 6b 0a 2a 2a 20 44 4f 54 4c 4f 43 4b 20 lock.** DOTLOCK
ca520 69 73 6e 27 74 20 61 20 74 72 75 65 20 6c 6f 63 isn't a true loc
ca530 6b 69 6e 67 20 73 74 79 6c 65 2c 20 69 74 20 72 king style, it r
ca540 65 66 65 72 73 20 74 6f 20 74 68 65 20 75 73 65 efers to the use
ca550 20 6f 66 20 61 20 73 70 65 63 69 61 6c 0a 2a 2a of a special.**
ca560 20 20 20 66 69 6c 65 20 6e 61 6d 65 64 20 74 68 file named th
ca570 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 64 61 e same as the da
ca580 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 tabase file with
ca590 20 61 20 27 2e 6c 6f 63 6b 27 20 65 78 74 65 6e a '.lock' exten
ca5a0 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 20 20 sion, this.**
ca5b0 63 61 6e 20 62 65 20 75 73 65 64 20 6f 6e 20 66 can be used on f
ca5c0 69 6c 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 ile systems that
ca5d0 20 64 6f 20 6e 6f 74 20 6f 66 66 65 72 20 61 6e do not offer an
ca5e0 79 20 72 65 6c 69 61 62 6c 65 20 66 69 6c 65 20 y reliable file
ca5f0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 4e 4f 20 6c 6f locking.** NO lo
ca600 63 6b 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 cking means that
ca610 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 77 69 6c 6c no locking will
ca620 20 62 65 20 61 74 74 65 6d 70 74 65 64 2c 20 74 be attempted, t
ca630 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 his is only used
ca640 20 66 6f 72 0a 2a 2a 20 20 20 72 65 61 64 2d 6f for.** read-o
ca650 6e 6c 79 20 66 69 6c 65 20 73 79 73 74 65 6d 73 nly file systems
ca660 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 55 4e currently.** UN
ca670 53 55 50 50 4f 52 54 45 44 20 6d 65 61 6e 73 20 SUPPORTED means
ca680 74 68 61 74 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 that no locking
ca690 77 69 6c 6c 20 62 65 20 61 74 74 65 6d 70 74 65 will be attempte
ca6a0 64 2c 20 74 68 69 73 20 69 73 20 6f 6e 6c 79 20 d, this is only
ca6b0 75 73 65 64 20 66 6f 72 0a 2a 2a 20 20 20 66 69 used for.** fi
ca6c0 6c 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 le systems that
ca6d0 61 72 65 20 6b 6e 6f 77 6e 20 74 6f 20 62 65 20 are known to be
ca6e0 75 6e 73 75 70 70 6f 72 74 65 64 0a 2a 2f 0a 23 unsupported.*/.#
ca6f0 64 65 66 69 6e 65 20 4c 4f 43 4b 49 4e 47 5f 53 define LOCKING_S
ca700 54 59 4c 45 5f 50 4f 53 49 58 20 20 20 20 20 20 TYLE_POSIX
ca710 20 20 31 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 1.#define LOCK
ca720 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e 45 20 20 ING_STYLE_NONE
ca730 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 2.#define
ca740 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 44 LOCKING_STYLE_D
ca750 4f 54 46 49 4c 45 20 20 20 20 20 20 33 0a 23 64 OTFILE 3.#d
ca760 65 66 69 6e 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 efine LOCKING_ST
ca770 59 4c 45 5f 46 4c 4f 43 4b 20 20 20 20 20 20 20 YLE_FLOCK
ca780 20 34 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 49 4.#define LOCKI
ca790 4e 47 5f 53 54 59 4c 45 5f 41 46 50 20 20 20 20 NG_STYLE_AFP
ca7a0 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 4f 5../*.** O
ca7b0 6e 6c 79 20 73 65 74 20 74 68 65 20 6c 61 73 74 nly set the last
ca7c0 45 72 72 6e 6f 20 69 66 20 74 68 65 20 65 72 72 Errno if the err
ca7d0 6f 72 20 63 6f 64 65 20 69 73 20 61 20 72 65 61 or code is a rea
ca7e0 6c 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f 74 20 l error and not
ca7f0 0a 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65 78 70 .** a normal exp
ca800 65 63 74 65 64 20 72 65 74 75 72 6e 20 63 6f 64 ected return cod
ca810 65 20 6f 66 20 53 51 4c 49 54 45 5f 42 55 53 59 e of SQLITE_BUSY
ca820 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f or SQLITE_OK.*/
ca830 0a 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f 43 4b .#define IS_LOCK
ca840 5f 45 52 52 4f 52 28 78 29 20 20 28 28 78 20 21 _ERROR(x) ((x !
ca850 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 = SQLITE_OK) &&
ca860 28 78 20 21 3d 20 53 51 4c 49 54 45 5f 42 55 53 (x != SQLITE_BUS
ca870 59 29 29 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 Y))../*.** Helpe
ca880 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 6f r functions to o
ca890 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 69 6e 71 btain and relinq
ca8a0 75 69 73 68 20 74 68 65 20 67 6c 6f 62 61 6c 20 uish the global
ca8b0 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 mutex..*/.static
ca8c0 20 76 6f 69 64 20 65 6e 74 65 72 4d 75 74 65 78 void enterMutex
ca8d0 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 (void){. sqlite
ca8e0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 3_mutex_enter(sq
ca8f0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
ca900 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
ca910 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a TIC_MASTER));.}.
ca920 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 76 static void leav
ca930 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 eMutex(void){.
ca940 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
ca950 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ave(sqlite3Mutex
ca960 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
ca970 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
ca980 29 29 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 ));.}..#if SQLIT
ca990 45 5f 54 48 52 45 41 44 53 41 46 45 0a 2f 2a 0a E_THREADSAFE./*.
ca9a0 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 ** This variable
ca9b0 20 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72 records whether
ca9c0 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73 20 or not threads
ca9d0 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 can override eac
ca9e0 68 20 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f 63 6b h others.** lock
ca9f0 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a 20 20 s..**.** 0:
caa00 4e 6f 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e No. Threads can
caa10 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65 61 63 not override eac
caa20 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 0a h others locks..
caa30 2a 2a 20 20 20 20 31 3a 20 20 59 65 73 2e 20 20 ** 1: Yes.
caa40 54 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 Threads can over
caa50 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 ride each others
caa60 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 2d 31 3a locks..** -1:
caa70 20 20 57 65 20 64 6f 6e 27 74 20 6b 6e 6f 77 20 We don't know
caa80 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 6f yet..**.** On so
caa90 6d 65 20 73 79 73 74 65 6d 73 2c 20 77 65 20 6b me systems, we k
caaa0 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 now at compile-t
caab0 69 6d 65 20 69 66 20 74 68 72 65 61 64 73 20 63 ime if threads c
caac0 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 an override each
caad0 0a 2a 2a 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 .** others locks
caae0 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79 73 74 . On those syst
caaf0 65 6d 73 2c 20 74 68 65 20 53 51 4c 49 54 45 5f ems, the SQLITE_
cab00 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f THREAD_OVERRIDE_
cab10 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20 77 69 LOCK macro.** wi
cab20 6c 6c 20 62 65 20 73 65 74 20 61 70 70 72 6f 70 ll be set approp
cab30 72 69 61 74 65 6c 79 2e 20 20 4f 6e 20 6f 74 68 riately. On oth
cab40 65 72 20 73 79 73 74 65 6d 73 2c 20 77 65 20 68 er systems, we h
cab50 61 76 65 20 74 6f 20 63 68 65 63 6b 20 61 74 0a ave to check at.
cab60 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 20 4f 6e 20 ** runtime. On
cab70 74 68 65 73 65 20 6c 61 74 74 65 72 20 73 79 73 these latter sys
cab80 74 65 6d 73 2c 20 53 51 4c 54 49 45 5f 54 48 52 tems, SQLTIE_THR
cab90 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 EAD_OVERRIDE_LOC
caba0 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 K is.** undefine
cabb0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 61 d..**.** This va
cabc0 72 69 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c 79 20 riable normally
cabd0 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20 6f has file scope o
cabe0 6e 6c 79 2e 20 20 42 75 74 20 64 75 72 69 6e 67 nly. But during
cabf0 20 74 65 73 74 69 6e 67 2c 20 77 65 20 6d 61 6b testing, we mak
cac00 65 0a 2a 2a 20 69 74 20 61 20 67 6c 6f 62 61 6c e.** it a global
cac10 20 73 6f 20 74 68 61 74 20 74 68 65 20 74 65 73 so that the tes
cac20 74 20 63 6f 64 65 20 63 61 6e 20 63 68 61 6e 67 t code can chang
cac30 65 20 69 74 73 20 76 61 6c 75 65 20 69 6e 20 6f e its value in o
cac40 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 0a 2a rder to verify.*
cac50 2a 20 74 68 61 74 20 74 68 65 20 72 69 67 68 74 * that the right
cac60 20 73 74 75 66 66 20 68 61 70 70 65 6e 73 20 69 stuff happens i
cac70 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e 0a 2a n either case..*
cac80 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
cac90 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 _THREAD_OVERRIDE
caca0 5f 4c 4f 43 4b 0a 23 20 64 65 66 69 6e 65 20 53 _LOCK.# define S
cacb0 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 QLITE_THREAD_OVE
cacc0 52 52 49 44 45 5f 4c 4f 43 4b 20 2d 31 0a 23 65 RRIDE_LOCK -1.#e
cacd0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
cace0 54 45 5f 54 45 53 54 0a 69 6e 74 20 74 68 72 65 TE_TEST.int thre
cacf0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f adsOverrideEachO
cad00 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c thersLocks = SQL
cad10 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 ITE_THREAD_OVERR
cad20 49 44 45 5f 4c 4f 43 4b 3b 0a 23 65 6c 73 65 0a IDE_LOCK;.#else.
cad30 73 74 61 74 69 63 20 69 6e 74 20 74 68 72 65 61 static int threa
cad40 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
cad50 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 hersLocks = SQLI
cad60 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 TE_THREAD_OVERRI
cad70 44 45 5f 4c 4f 43 4b 3b 0a 23 65 6e 64 69 66 0a DE_LOCK;.#endif.
cad80 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 ./*.** This stru
cad90 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f cture holds info
cada0 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20 69 rmation passed i
cadb0 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 20 74 nto individual t
cadc0 65 73 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 62 est.** threads b
cadd0 79 20 74 68 65 20 74 65 73 74 54 68 72 65 61 64 y the testThread
cade0 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 LockingBehavior(
cadf0 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 ) routine..*/.st
cae00 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44 ruct threadTestD
cae10 61 74 61 20 7b 0a 20 20 69 6e 74 20 66 64 3b 20 ata {. int fd;
cae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cae30 2a 20 46 69 6c 65 20 74 6f 20 62 65 20 6c 6f 63 * File to be loc
cae40 6b 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ked */. struct
cae50 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 flock lock;
cae60 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6f /* The locking o
cae70 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e peration */. in
cae80 74 20 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20 t result;
cae90 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f /* Result o
caea0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 f the locking op
caeb0 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 23 eration */.};..#
caec0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f 43 ifdef SQLITE_LOC
caed0 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20 50 72 K_TRACE./*.** Pr
caee0 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d 61 74 int out informat
caef0 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 6c 6f ion about all lo
caf00 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 cking operations
caf10 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
caf20 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 tine is used for
caf30 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 troubleshooting
caf40 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 69 74 locks on multit
caf50 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61 74 66 hreaded.** platf
caf60 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20 62 79 orms. Enable by
caf70 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 compiling with
caf80 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c 4f 43 the -DSQLITE_LOC
caf90 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d 6d 61 K_TRACE.** comma
cafa0 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e 20 6f nd-line option o
cafb0 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 n the compiler.
cafc0 20 54 68 69 73 20 63 6f 64 65 20 69 73 20 6e 6f This code is no
cafd0 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e 65 64 rmally.** turned
cafe0 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 off..*/.static
caff0 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28 69 6e int lockTrace(in
cb000 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20 73 74 t fd, int op, st
cb010 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29 7b 0a ruct flock *p){.
cb020 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d 65 2c char *zOpName,
cb030 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74 20 73 *zType;. int s
cb040 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45 72 72 ;. int savedErr
cb050 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 46 5f no;. if( op==F_
cb060 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 GETLK ){. zOp
cb070 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22 3b 0a Name = "GETLK";.
cb080 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d }else if( op==
cb090 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a F_SETLK ){. z
cb0a0 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c 4b 22 OpName = "SETLK"
cb0b0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
cb0c0 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c = fcntl(fd, op,
cb0d0 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 p);. sqlite3
cb0e0 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e DebugPrintf("fcn
cb0f0 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20 25 64 tl unknown %d %d
cb100 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70 2c 20 %d\n", fd, op,
cb110 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 s);. return s
cb120 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6c ;. }. if( p->l
cb130 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 _type==F_RDLCK )
cb140 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52 {. zType = "R
cb150 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 DLCK";. }else i
cb160 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f f( p->l_type==F_
cb170 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 WRLCK ){. zTy
cb180 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 pe = "WRLCK";.
cb190 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74 }else if( p->l_t
cb1a0 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a ype==F_UNLCK ){.
cb1b0 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c zType = "UNL
cb1c0 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 CK";. }else{.
cb1d0 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 assert( 0 );.
cb1e0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e }. assert( p->
cb1f0 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b 5f 53 l_whence==SEEK_S
cb200 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63 6e 74 ET );. s = fcnt
cb210 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 l(fd, op, p);.
cb220 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65 72 72 savedErrno = err
cb230 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 no;. sqlite3Deb
cb240 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 ugPrintf("fcntl
cb250 25 64 20 25 64 20 25 73 20 25 73 20 25 64 20 25 %d %d %s %s %d %
cb260 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 d %d %d\n",.
cb270 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c 20 7a threadid, fd, z
cb280 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c 20 28 OpName, zType, (
cb290 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74 2c 20 int)p->l_start,
cb2a0 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a 20 (int)p->l_len,.
cb2b0 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f 70 69 (int)p->l_pi
cb2c0 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73 3d 3d d, s);. if( s==
cb2d0 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f 53 45 (-1) && op==F_SE
cb2e0 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74 79 70 TLK && (p->l_typ
cb2f0 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20 70 2d e==F_RDLCK || p-
cb300 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b >l_type==F_WRLCK
cb310 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 ) ){. struct
cb320 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20 6c 32 flock l2;. l2
cb330 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e 74 6c = *p;. fcntl
cb340 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c (fd, F_GETLK, &l
cb350 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32 2e 6c 2);. if( l2.l
cb360 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 _type==F_RDLCK )
cb370 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 {. zType =
cb380 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c "RDLCK";. }el
cb390 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 se if( l2.l_type
cb3a0 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 ==F_WRLCK ){.
cb3b0 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 zType = "WRLC
cb3c0 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 K";. }else if
cb3d0 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 ( l2.l_type==F_U
cb3e0 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 NLCK ){. zT
cb3f0 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 ype = "UNLCK";.
cb400 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
cb410 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 assert( 0 );.
cb420 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 }. sqlite3De
cb430 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c bugPrintf("fcntl
cb440 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f 6e 3a -failure-reason:
cb450 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e 22 2c %s %d %d %d\n",
cb460 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c 20 28 . zType, (
cb470 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74 2c 20 int)l2.l_start,
cb480 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 28 (int)l2.l_len, (
cb490 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b 0a 20 int)l2.l_pid);.
cb4a0 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73 61 76 }. errno = sav
cb4b0 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74 75 72 edErrno;. retur
cb4c0 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 66 n s;.}.#define f
cb4d0 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65 0a 23 cntl lockTrace.#
cb4e0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
cb4f0 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a 2f LOCK_TRACE */../
cb500 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 54 68 72 *.** The testThr
cb510 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 eadLockingBehavi
cb520 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 6c 61 75 or() routine lau
cb530 6e 63 68 65 73 20 74 77 6f 20 73 65 70 61 72 61 nches two separa
cb540 74 65 0a 2a 2a 20 74 68 72 65 61 64 73 20 6f 6e te.** threads on
cb550 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 this routine.
cb560 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74 This routine att
cb570 65 6d 70 74 73 20 74 6f 20 6c 6f 63 6b 20 61 20 empts to lock a
cb580 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 file.** descript
cb590 6f 72 20 74 68 65 6e 20 72 65 74 75 72 6e 73 2e or then returns.
cb5a0 20 20 54 68 65 20 73 75 63 63 65 73 73 20 6f 72 The success or
cb5b0 20 66 61 69 6c 75 72 65 20 6f 66 20 74 68 61 74 failure of that
cb5c0 20 61 74 74 65 6d 70 74 0a 2a 2a 20 61 6c 6c 6f attempt.** allo
cb5d0 77 73 20 74 68 65 20 74 65 73 74 54 68 72 65 61 ws the testThrea
cb5e0 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 dLockingBehavior
cb5f0 28 29 20 70 72 6f 63 65 64 75 72 65 20 74 6f 20 () procedure to
cb600 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 determine.** whe
cb610 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 ther or not thre
cb620 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 ads can override
cb630 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 each others loc
cb640 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ks..*/.static vo
cb650 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e id *threadLockin
cb660 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67 gTest(void *pArg
cb670 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 ){. struct thre
cb680 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74 adTestData *pDat
cb690 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65 a = (struct thre
cb6a0 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67 adTestData*)pArg
cb6b0 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c ;. pData->resul
cb6c0 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d t = fcntl(pData-
cb6d0 3e 66 64 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 70 >fd, F_SETLK, &p
cb6e0 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 Data->lock);. r
cb6f0 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 0a 2f eturn pArg;.}../
cb700 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 64 *.** This proced
cb710 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ure attempts to
cb720 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 determine whethe
cb730 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73 r or not threads
cb740 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64 65 .** can override
cb750 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 each others loc
cb760 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 ks then sets the
cb770 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65 72 .** threadsOver
cb780 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f rideEachOthersLo
cb790 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70 70 cks variable app
cb7a0 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73 ropriately..*/.s
cb7b0 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 54 tatic void testT
cb7c0 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 hreadLockingBeha
cb7d0 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69 67 vior(int fd_orig
cb7e0 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 73 ){. int fd;. s
cb7f0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 truct threadTest
cb800 44 61 74 61 20 64 5b 32 5d 3b 0a 20 20 70 74 68 Data d[2];. pth
cb810 72 65 61 64 5f 74 20 74 5b 32 5d 3b 0a 0a 20 20 read_t t[2];..
cb820 66 64 20 3d 20 64 75 70 28 66 64 5f 6f 72 69 67 fd = dup(fd_orig
cb830 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 20 );. if( fd<0 )
cb840 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 return;. memset
cb850 28 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 (d, 0, sizeof(d)
cb860 29 3b 0a 20 20 64 5b 30 5d 2e 66 64 20 3d 20 66 );. d[0].fd = f
cb870 64 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c d;. d[0].lock.l
cb880 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b _type = F_RDLCK;
cb890 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c 5f 6c . d[0].lock.l_l
cb8a0 65 6e 20 3d 20 31 3b 0a 20 20 64 5b 30 5d 2e 6c en = 1;. d[0].l
cb8b0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b ock.l_start = 0;
cb8c0 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c 5f 77 . d[0].lock.l_w
cb8d0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
cb8e0 3b 0a 20 20 64 5b 31 5d 20 3d 20 64 5b 30 5d 3b ;. d[1] = d[0];
cb8f0 0a 20 20 64 5b 31 5d 2e 6c 6f 63 6b 2e 6c 5f 74 . d[1].lock.l_t
cb900 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 ype = F_WRLCK;.
cb910 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 pthread_create(
cb920 26 74 5b 30 5d 2c 20 30 2c 20 74 68 72 65 61 64 &t[0], 0, thread
cb930 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20 26 64 5b LockingTest, &d[
cb940 30 5d 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 63 0]);. pthread_c
cb950 72 65 61 74 65 28 26 74 5b 31 5d 2c 20 30 2c 20 reate(&t[1], 0,
cb960 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 threadLockingTes
cb970 74 2c 20 26 64 5b 31 5d 29 3b 0a 20 20 70 74 68 t, &d[1]);. pth
cb980 72 65 61 64 5f 6a 6f 69 6e 28 74 5b 30 5d 2c 20 read_join(t[0],
cb990 30 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6a 6f 0);. pthread_jo
cb9a0 69 6e 28 74 5b 31 5d 2c 20 30 29 3b 0a 20 20 63 in(t[1], 0);. c
cb9b0 6c 6f 73 65 28 66 64 29 3b 0a 20 20 74 68 72 65 lose(fd);. thre
cb9c0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f adsOverrideEachO
cb9d0 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 20 64 5b thersLocks = d[
cb9e0 30 5d 2e 72 65 73 75 6c 74 3d 3d 30 20 26 26 20 0].result==0 &&
cb9f0 64 5b 31 5d 2e 72 65 73 75 6c 74 3d 3d 30 3b 0a d[1].result==0;.
cba00 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
cba10 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f TE_THREADSAFE */
cba20 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
cba30 61 20 6c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 a lockInfo struc
cba40 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 ture previously
cba50 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e allocated by fin
cba60 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a dLockInfo()..*/.
cba70 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 static void rele
cba80 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 73 74 72 75 aseLockInfo(stru
cba90 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f ct lockInfo *pLo
cbaa0 63 6b 29 7b 0a 20 20 69 66 28 20 70 4c 6f 63 6b ck){. if( pLock
cbab0 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e ){. pLock->n
cbac0 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 Ref--;. if( p
cbad0 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b Lock->nRef==0 ){
cbae0 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b . if( pLock
cbaf0 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 ->pPrev ){.
cbb00 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
cbb10 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d ->pPrev->pNext==
cbb20 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 pLock );.
cbb30 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 pLock->pPrev->p
cbb40 4e 65 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e Next = pLock->pN
cbb50 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ext;. }else
cbb60 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
cbb70 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f 63 ( lockList==pLoc
cbb80 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 k );. loc
cbb90 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 kList = pLock->p
cbba0 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
cbbb0 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 if( pLock->p
cbbc0 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Next ){.
cbbd0 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 assert( pLock->p
cbbe0 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c 6f Next->pPrev==pLo
cbbf0 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c ck );. pL
cbc00 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 ock->pNext->pPre
cbc10 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 v = pLock->pPrev
cbc20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
cbc30 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f sqlite3_free(pLo
cbc40 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ck);. }. }.}
cbc50 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
cbc60 61 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 a openCnt struct
cbc70 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 ure previously a
cbc80 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 llocated by find
cbc90 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a 73 LockInfo()..*/.s
cbca0 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
cbcb0 73 65 4f 70 65 6e 43 6e 74 28 73 74 72 75 63 74 seOpenCnt(struct
cbcc0 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 29 openCnt *pOpen)
cbcd0 7b 0a 20 20 69 66 28 20 70 4f 70 65 6e 20 29 7b {. if( pOpen ){
cbce0 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 . pOpen->nRef
cbcf0 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 65 --;. if( pOpe
cbd00 6e 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 n->nRef==0 ){.
cbd10 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 70 if( pOpen->p
cbd20 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20 Prev ){.
cbd30 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 70 assert( pOpen->p
cbd40 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 4f 70 Prev->pNext==pOp
cbd50 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f en );. pO
cbd60 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 pen->pPrev->pNex
cbd70 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 t = pOpen->pNext
cbd80 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
cbd90 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f assert( o
cbda0 70 65 6e 4c 69 73 74 3d 3d 70 4f 70 65 6e 20 29 penList==pOpen )
cbdb0 3b 0a 20 20 20 20 20 20 20 20 6f 70 65 6e 4c 69 ;. openLi
cbdc0 73 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 st = pOpen->pNex
cbdd0 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
cbde0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 if( pOpen->pNex
cbdf0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 t ){. ass
cbe00 65 72 74 28 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 ert( pOpen->pNex
cbe10 74 2d 3e 70 50 72 65 76 3d 3d 70 4f 70 65 6e 20 t->pPrev==pOpen
cbe20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e );. pOpen
cbe30 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d ->pNext->pPrev =
cbe40 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 3b 0a 20 pOpen->pPrev;.
cbe50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
cbe60 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e 2d ite3_free(pOpen-
cbe70 3e 61 50 65 6e 64 69 6e 67 29 3b 0a 20 20 20 20 >aPending);.
cbe80 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
cbe90 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Open);. }. }
cbea0 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 .}..#if SQLITE_E
cbeb0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
cbec0 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 65 73 74 73 20 YLE./*.** Tests
cbed0 61 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 a byte-range loc
cbee0 6b 69 6e 67 20 71 75 65 72 79 20 74 6f 20 73 65 king query to se
cbef0 65 20 69 66 20 62 79 74 65 20 72 61 6e 67 65 20 e if byte range
cbf00 6c 6f 63 6b 73 20 61 72 65 20 0a 2a 2a 20 73 75 locks are .** su
cbf10 70 70 6f 72 74 65 64 2c 20 69 66 20 6e 6f 74 20 pported, if not
cbf20 77 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 we fall back to
cbf30 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 dotlockLockingSt
cbf40 79 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 yle..*/.static i
cbf50 6e 74 20 74 65 73 74 4c 6f 63 6b 69 6e 67 53 74 nt testLockingSt
cbf60 79 6c 65 28 69 6e 74 20 66 64 29 7b 0a 20 20 73 yle(int fd){. s
cbf70 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b truct flock lock
cbf80 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 Info;.. /* Test
cbf90 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b byte-range lock
cbfa0 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20 using fcntl().
cbfb0 49 66 20 74 68 65 20 63 61 6c 6c 20 73 75 63 63 If the call succ
cbfc0 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75 eeds, . ** assu
cbfd0 6d 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 me that the file
cbfe0 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 -system supports
cbff0 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 POSIX style loc
cc000 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b ks. . */. lock
cc010 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a Info.l_len = 1;.
cc020 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 lockInfo.l_sta
cc030 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e rt = 0;. lockIn
cc040 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 fo.l_whence = SE
cc050 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e EK_SET;. lockIn
cc060 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 fo.l_type = F_RD
cc070 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c LCK;. if( fcntl
cc080 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c (fd, F_GETLK, &l
cc090 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b ockInfo)!=-1 ) {
cc0a0 0a 20 20 20 20 72 65 74 75 72 6e 20 4c 4f 43 4b . return LOCK
cc0b0 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 3b ING_STYLE_POSIX;
cc0c0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 65 73 . }. . /* Tes
cc0d0 74 69 6e 67 20 66 6f 72 20 66 6c 6f 63 6b 28 29 ting for flock()
cc0e0 20 63 61 6e 20 67 69 76 65 20 66 61 6c 73 65 20 can give false
cc0f0 70 6f 73 69 74 69 76 65 73 2e 20 20 53 6f 20 69 positives. So i
cc100 66 20 69 66 20 74 68 65 20 61 62 6f 76 65 20 0a f if the above .
cc110 20 20 2a 2a 20 74 65 73 74 20 66 61 69 6c 73 2c ** test fails,
cc120 20 74 68 65 6e 20 77 65 20 66 61 6c 6c 20 62 61 then we fall ba
cc130 63 6b 20 74 6f 20 75 73 69 6e 67 20 64 6f 74 2d ck to using dot-
cc140 66 69 6c 65 20 73 74 79 6c 65 20 6c 6f 63 6b 69 file style locki
cc150 6e 67 2e 0a 20 20 2a 2f 20 20 0a 20 20 72 65 74 ng.. */ . ret
cc160 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c urn LOCKING_STYL
cc170 45 5f 44 4f 54 46 49 4c 45 3b 0a 7d 0a 23 65 6e E_DOTFILE;.}.#en
cc180 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 49 66 20 53 dif../* .** If S
cc190 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
cc1a0 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 20 64 65 KING_STYLE is de
cc1b0 66 69 6e 65 64 2c 20 74 68 69 73 20 66 75 6e 63 fined, this func
cc1c0 74 69 6f 6e 20 45 78 61 6d 69 6e 65 73 20 74 68 tion Examines th
cc1d0 65 20 0a 2a 2a 20 66 5f 66 73 74 79 70 65 6e 61 e .** f_fstypena
cc1e0 6d 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 me entry in the
cc1f0 73 74 61 74 66 73 20 73 74 72 75 63 74 75 72 65 statfs structure
cc200 20 61 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 as returned by
cc210 73 74 61 74 28 29 20 66 6f 72 20 0a 2a 2a 20 74 stat() for .** t
cc220 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 20 68 he file system h
cc230 6f 73 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 osting the datab
cc240 61 73 65 20 66 69 6c 65 20 61 6e 64 20 73 65 6c ase file and sel
cc250 65 63 74 73 20 20 74 68 65 20 61 70 70 72 6f 70 ects the approp
cc260 72 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 riate.** locking
cc270 20 73 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 style based on
cc280 69 74 73 20 76 61 6c 75 65 2e 20 20 54 68 65 73 its value. Thes
cc290 65 20 76 61 6c 75 65 73 20 61 6e 64 20 61 73 73 e values and ass
cc2a0 69 67 6e 6d 65 6e 74 73 20 61 72 65 20 0a 2a 2a ignments are .**
cc2b0 20 62 61 73 65 64 20 6f 6e 20 44 61 72 77 69 6e based on Darwin
cc2c0 2f 4f 53 58 20 62 65 68 61 76 69 6f 72 20 61 6e /OSX behavior an
cc2d0 64 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 d have not been
cc2e0 74 68 6f 72 6f 75 67 68 6c 79 20 74 65 73 74 65 thoroughly teste
cc2f0 64 20 6f 6e 20 0a 2a 2a 20 6f 74 68 65 72 20 73 d on .** other s
cc300 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 ystems..**.** If
cc310 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
cc320 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 20 OCKING_STYLE is
cc330 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 74 68 69 not defined, thi
cc340 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 s function alway
cc350 73 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4c 4f 43 s.** returns LOC
cc360 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 KING_STYLE_POSIX
cc370 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
cc380 64 65 74 65 63 74 4c 6f 63 6b 69 6e 67 53 74 79 detectLockingSty
cc390 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 le(. sqlite3_vf
cc3a0 73 20 2a 70 56 66 73 2c 0a 20 20 63 6f 6e 73 74 s *pVfs,. const
cc3b0 20 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 2c char *filePath,
cc3c0 20 0a 20 20 69 6e 74 20 66 64 0a 29 7b 0a 23 69 . int fd.){.#i
cc3d0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
cc3e0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 LOCKING_STYLE.
cc3f0 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 20 7b struct Mapping {
cc400 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
cc410 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 0a 20 20 *zFilesystem;.
cc420 20 20 69 6e 74 20 65 4c 6f 63 6b 69 6e 67 53 74 int eLockingSt
cc430 79 6c 65 3b 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 yle;. } aMap[]
cc440 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c = {. { "hfs",
cc450 20 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c LOCKING_STYL
cc460 45 5f 50 4f 53 49 58 20 7d 2c 0a 20 20 20 20 7b E_POSIX },. {
cc470 20 22 75 66 73 22 2c 20 20 20 20 4c 4f 43 4b 49 "ufs", LOCKI
cc480 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 20 7d NG_STYLE_POSIX }
cc490 2c 0a 20 20 20 20 7b 20 22 61 66 70 66 73 22 2c ,. { "afpfs",
cc4a0 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f LOCKING_STYLE_
cc4b0 41 46 50 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 AFP },.#ifdef SQ
cc4c0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 50 5f LITE_ENABLE_AFP_
cc4d0 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 20 20 LOCKING_SMB.
cc4e0 7b 20 22 73 6d 62 66 73 22 2c 20 20 4c 4f 43 4b { "smbfs", LOCK
cc4f0 49 4e 47 5f 53 54 59 4c 45 5f 41 46 50 20 7d 2c ING_STYLE_AFP },
cc500 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d .#else. { "sm
cc510 62 66 73 22 2c 20 20 4c 4f 43 4b 49 4e 47 5f 53 bfs", LOCKING_S
cc520 54 59 4c 45 5f 46 4c 4f 43 4b 20 7d 2c 0a 23 65 TYLE_FLOCK },.#e
cc530 6e 64 69 66 0a 20 20 20 20 7b 20 22 6d 73 64 6f ndif. { "msdo
cc540 73 22 2c 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 s", LOCKING_STY
cc550 4c 45 5f 44 4f 54 46 49 4c 45 20 7d 2c 0a 20 20 LE_DOTFILE },.
cc560 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 4c 4f { "webdav", LO
cc570 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e 45 CKING_STYLE_NONE
cc580 20 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d },. { 0, 0 }
cc590 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 . };. int i;.
cc5a0 20 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66 struct statfs f
cc5b0 73 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 sInfo;.. if( !f
cc5c0 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 72 ilePath ){. r
cc5d0 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f 53 54 eturn LOCKING_ST
cc5e0 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 YLE_NONE;. }.
cc5f0 69 66 28 20 70 56 66 73 2d 3e 70 41 70 70 44 61 if( pVfs->pAppDa
cc600 74 61 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ta ){. return
cc610 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 SQLITE_PTR_TO_I
cc620 4e 54 28 70 56 66 73 2d 3e 70 41 70 70 44 61 74 NT(pVfs->pAppDat
cc630 61 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 a);. }.. if( s
cc640 74 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 tatfs(filePath,
cc650 26 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 &fsInfo) != -1 )
cc660 7b 0a 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f {. if( fsInfo
cc670 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 .f_flags & MNT_R
cc680 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 DONLY ){. r
cc690 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f 53 54 eturn LOCKING_ST
cc6a0 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a YLE_NONE;. }.
cc6b0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 for(i=0; aMa
cc6c0 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d p[i].zFilesystem
cc6d0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
cc6e0 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e ( strcmp(fsInfo.
cc6f0 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 61 4d f_fstypename, aM
cc700 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 ap[i].zFilesyste
cc710 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 m)==0 ){.
cc720 20 72 65 74 75 72 6e 20 61 4d 61 70 5b 69 5d 2e return aMap[i].
cc730 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 eLockingStyle;.
cc740 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
cc750 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 .. /* Default c
cc760 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 61 6d ase. Handles, am
cc770 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 22 6e ongst others, "n
cc780 66 73 22 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e fs". */. return
cc790 20 74 65 73 74 4c 6f 63 6b 69 6e 67 53 74 79 6c testLockingStyl
cc7a0 65 28 66 64 29 3b 20 20 0a 23 65 6e 64 69 66 0a e(fd); .#endif.
cc7b0 20 20 72 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 return LOCKING
cc7c0 5f 53 54 59 4c 45 5f 50 4f 53 49 58 3b 0a 7d 0a _STYLE_POSIX;.}.
cc7d0 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 66 ./*.** Given a f
cc7e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2c 20 ile descriptor,
cc7f0 6c 6f 63 61 74 65 20 6c 6f 63 6b 49 6e 66 6f 20 locate lockInfo
cc800 61 6e 64 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 and openCnt stru
cc810 63 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 ctures that.** d
cc820 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66 69 escribes that fi
cc830 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 le descriptor.
cc840 43 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 Create new ones
cc850 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 if necessary. T
cc860 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c he.** return val
cc870 75 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69 ues might be uni
cc880 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e nitialized if an
cc890 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a error occurs..*
cc8a0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 61 *.** Return an a
cc8b0 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 ppropriate error
cc8c0 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 code..*/.static
cc8d0 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 int findLockInf
cc8e0 6f 28 0a 20 20 69 6e 74 20 66 64 2c 20 20 20 20 o(. int fd,
cc8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cc900 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
cc910 73 63 72 69 70 74 6f 72 20 75 73 65 64 20 69 6e scriptor used in
cc920 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 the key */. st
cc930 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 2a ruct lockInfo **
cc940 70 70 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 52 65 ppLock, /* Re
cc950 74 75 72 6e 20 74 68 65 20 6c 6f 63 6b 49 6e 66 turn the lockInf
cc960 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 o structure here
cc970 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 6f 70 65 */. struct ope
cc980 6e 43 6e 74 20 2a 2a 70 70 4f 70 65 6e 20 20 20 nCnt **ppOpen
cc990 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 /* Return the
cc9a0 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 openCnt structu
cc9b0 72 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 re here */.){.
cc9c0 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 int rc;. struct
cc9d0 20 6c 6f 63 6b 4b 65 79 20 6b 65 79 31 3b 0a 20 lockKey key1;.
cc9e0 20 73 74 72 75 63 74 20 6f 70 65 6e 4b 65 79 20 struct openKey
cc9f0 6b 65 79 32 3b 0a 20 20 73 74 72 75 63 74 20 73 key2;. struct s
cca00 74 61 74 20 73 74 61 74 62 75 66 3b 0a 20 20 73 tat statbuf;. s
cca10 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a truct lockInfo *
cca20 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 pLock;. struct
cca30 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a openCnt *pOpen;.
cca40 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c rc = fstat(fd,
cca50 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 69 66 &statbuf);. if
cca60 28 20 72 63 21 3d 30 20 29 7b 0a 23 69 66 64 65 ( rc!=0 ){.#ifde
cca70 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 f EOVERFLOW.
cca80 69 66 28 20 65 72 72 6e 6f 3d 3d 45 4f 56 45 52 if( errno==EOVER
cca90 46 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 FLOW ) return SQ
ccaa0 4c 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 LITE_NOLFS;.#end
ccab0 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 if. return SQ
ccac0 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a LITE_IOERR;. }.
ccad0 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e . /* On OS X on
ccae0 20 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 an msdos filesy
ccaf0 73 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 stem, the inode
ccb00 6e 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 number is report
ccb10 65 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 ed. ** incorrec
ccb20 74 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a tly for zero-siz
ccb30 65 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 e files. See ti
ccb40 63 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 cket #3260. To
ccb50 77 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 work. ** around
ccb60 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 this problem (w
ccb70 65 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 e consider it a
ccb80 62 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 bug in OS X, not
ccb90 20 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 SQLite). ** we
ccba0 20 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 always increase
ccbb0 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 the file size t
ccbc0 6f 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 o 1 by writing a
ccbd0 20 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a single byte. *
ccbe0 2a 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 * prior to acces
ccbf0 73 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e sing the inode n
ccc00 75 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 umber. The one
ccc10 62 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a byte written is.
ccc20 20 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 ** an ASCII 'S
ccc30 27 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63 ' character whic
ccc40 68 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 h also happens t
ccc50 6f 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62 o be the first b
ccc60 79 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 yte. ** in the
ccc70 68 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 header of every
ccc80 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e SQLite database.
ccc90 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 In this way, i
ccca0 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 f there. ** is
cccb0 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e a race condition
cccc0 20 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 such that anoth
cccd0 65 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c er thread has al
ccce0 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a ready populated.
cccf0 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 ** the first p
ccd00 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
ccd10 61 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 ase, no damage i
ccd20 73 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 s done.. */. i
ccd30 66 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 f( statbuf.st_si
ccd40 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 77 72 69 ze==0 ){. wri
ccd50 74 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b 0a te(fd, "S", 1);.
ccd60 20 20 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 rc = fstat(f
ccd70 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 d, &statbuf);.
ccd80 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 if( rc!=0 ){.
ccd90 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
ccda0 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a TE_IOERR;. }.
ccdb0 20 20 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6b }.. memset(&k
ccdc0 65 79 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b ey1, 0, sizeof(k
ccdd0 65 79 31 29 29 3b 0a 20 20 6b 65 79 31 2e 64 65 ey1));. key1.de
ccde0 76 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 64 v = statbuf.st_d
ccdf0 65 76 3b 0a 20 20 6b 65 79 31 2e 69 6e 6f 20 3d ev;. key1.ino =
cce00 20 73 74 61 74 62 75 66 2e 73 74 5f 69 6e 6f 3b statbuf.st_ino;
cce10 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
cce20 41 44 53 41 46 45 0a 20 20 69 66 28 20 74 68 72 ADSAFE. if( thr
cce30 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 eadsOverrideEach
cce40 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b OthersLocks<0 ){
cce50 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c . testThreadL
cce60 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 ockingBehavior(f
cce70 64 29 3b 0a 20 20 7d 0a 20 20 6b 65 79 31 2e 74 d);. }. key1.t
cce80 69 64 20 3d 20 74 68 72 65 61 64 73 4f 76 65 72 id = threadsOver
cce90 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f rideEachOthersLo
ccea0 63 6b 73 20 3f 20 30 20 3a 20 70 74 68 72 65 61 cks ? 0 : pthrea
cceb0 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e 64 69 66 d_self();.#endif
ccec0 0a 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 32 2c . memset(&key2,
cced0 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79 32 29 0, sizeof(key2)
ccee0 29 3b 0a 20 20 6b 65 79 32 2e 64 65 76 20 3d 20 );. key2.dev =
ccef0 73 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a statbuf.st_dev;.
ccf00 20 20 6b 65 79 32 2e 69 6e 6f 20 3d 20 73 74 61 key2.ino = sta
ccf10 74 62 75 66 2e 73 74 5f 69 6e 6f 3b 0a 20 20 70 tbuf.st_ino;. p
ccf20 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b Lock = lockList;
ccf30 0a 20 20 77 68 69 6c 65 28 20 70 4c 6f 63 6b 20 . while( pLock
ccf40 26 26 20 6d 65 6d 63 6d 70 28 26 6b 65 79 31 2c && memcmp(&key1,
ccf50 20 26 70 4c 6f 63 6b 2d 3e 6b 65 79 2c 20 73 69 &pLock->key, si
ccf60 7a 65 6f 66 28 6b 65 79 31 29 29 20 29 7b 0a 20 zeof(key1)) ){.
ccf70 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b pLock = pLock
ccf80 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 ->pNext;. }. i
ccf90 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 f( pLock==0 ){.
ccfa0 20 20 20 70 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 pLock = sqlit
ccfb0 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f e3_malloc( sizeo
ccfc0 66 28 2a 70 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 f(*pLock) );.
ccfd0 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b if( pLock==0 ){
ccfe0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
ccff0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
cd000 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f goto exit_findlo
cd010 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 7d 0a 20 20 ckinfo;. }.
cd020 20 20 70 4c 6f 63 6b 2d 3e 6b 65 79 20 3d 20 6b pLock->key = k
cd030 65 79 31 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e ey1;. pLock->
cd040 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4c nRef = 1;. pL
cd050 6f 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 ock->cnt = 0;.
cd060 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 pLock->locktyp
cd070 65 20 3d 20 30 3b 0a 20 20 20 20 70 4c 6f 63 6b e = 0;. pLock
cd080 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 ->pNext = lockLi
cd090 73 74 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 st;. pLock->p
cd0a0 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 69 66 Prev = 0;. if
cd0b0 28 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c 6f 63 ( lockList ) loc
cd0c0 6b 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 kList->pPrev = p
cd0d0 4c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 4c 69 Lock;. lockLi
cd0e0 73 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 65 st = pLock;. }e
cd0f0 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e lse{. pLock->
cd100 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 nRef++;. }. *p
cd110 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 pLock = pLock;.
cd120 20 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 if( ppOpen!=0 )
cd130 7b 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70 {. pOpen = op
cd140 65 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c enList;. whil
cd150 65 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63 e( pOpen && memc
cd160 6d 70 28 26 6b 65 79 32 2c 20 26 70 4f 70 65 6e mp(&key2, &pOpen
cd170 2d 3e 6b 65 79 2c 20 73 69 7a 65 6f 66 28 6b 65 ->key, sizeof(ke
cd180 79 32 29 29 20 29 7b 0a 20 20 20 20 20 20 70 4f y2)) ){. pO
cd190 70 65 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 pen = pOpen->pNe
cd1a0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 xt;. }. if
cd1b0 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 ( pOpen==0 ){.
cd1c0 20 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69 pOpen = sqli
cd1d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 te3_malloc( size
cd1e0 6f 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20 of(*pOpen) );.
cd1f0 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 if( pOpen==0
cd200 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 ){. rele
cd210 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 aseLockInfo(pLoc
cd220 6b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d k);. rc =
cd230 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
cd240 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 goto exit
cd250 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 _findlockinfo;.
cd260 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 }. pOp
cd270 65 6e 2d 3e 6b 65 79 20 3d 20 6b 65 79 32 3b 0a en->key = key2;.
cd280 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 pOpen->nRe
cd290 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 f = 1;. pOp
cd2a0 65 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 en->nLock = 0;.
cd2b0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e pOpen->nPen
cd2c0 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 ding = 0;.
cd2d0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 pOpen->aPending
cd2e0 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e = 0;. pOpen
cd2f0 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 ->pNext = openLi
cd300 73 74 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d st;. pOpen-
cd310 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 >pPrev = 0;.
cd320 20 20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 if( openList )
cd330 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 openList->pPrev
cd340 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 = pOpen;.
cd350 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e openList = pOpen
cd360 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
cd370 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b pOpen->nRef++
cd380 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f ;. }. *ppO
cd390 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d pen = pOpen;. }
cd3a0 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 ..exit_findlocki
cd3b0 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 nfo:. return rc
cd3c0 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
cd3d0 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 TE_DEBUG./*.** H
cd3e0 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 elper function f
cd3f0 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 or printing out
cd400 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f trace informatio
cd410 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 n from debugging
cd420 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 .** binaries. Th
cd430 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 is returns the s
cd440 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 tring represetat
cd450 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c ion of the suppl
cd460 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c ied.** integer l
cd470 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 ock-type..*/.sta
cd480 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
cd490 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 locktypeName(int
cd4a0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 locktype){. sw
cd4b0 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 itch( locktype )
cd4c0 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b {. case NO_LOCK
cd4d0 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b : return "NONE";
cd4e0 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c . case SHARED_L
cd4f0 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 OCK: return "SHA
cd500 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45 53 RED";. case RES
cd510 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 ERVED_LOCK: retu
cd520 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20 rn "RESERVED";.
cd530 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f case PENDING_LO
cd540 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44 CK: return "PEND
cd550 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58 43 ING";. case EXC
cd560 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74 LUSIVE_LOCK: ret
cd570 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b urn "EXCLUSIVE";
cd580 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45 . }. return "E
cd590 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a RROR";.}.#endif.
cd5a0 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 ./*.** If we are
cd5b0 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 currently in a
cd5c0 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
cd5d0 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61 64 than the thread
cd5e0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e 69 that the.** uni
cd5f0 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20 62 xFile argument b
cd600 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e 20 elongs to, then
cd610 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 transfer ownersh
cd620 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 ip of the unixFi
cd630 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68 le.** over to th
cd640 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 e current thread
cd650 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 69 ..**.** A unixFi
cd660 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 64 le is only owned
cd670 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e 20 by a thread on
cd680 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6f 6e systems where on
cd690 65 20 74 68 72 65 61 64 20 69 73 0a 2a 2a 20 75 e thread is.** u
cd6a0 6e 61 62 6c 65 20 74 6f 20 6f 76 65 72 72 69 64 nable to overrid
cd6b0 65 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20 e locks created
cd6c0 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 by a different t
cd6d0 68 72 65 61 64 2e 20 20 52 65 64 48 61 74 39 20 hread. RedHat9
cd6e0 69 73 0a 2a 2a 20 61 6e 20 65 78 61 6d 70 6c 65 is.** an example
cd6f0 20 6f 66 20 73 75 63 68 20 61 20 73 79 73 74 65 of such a syste
cd700 6d 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 m..**.** Ownersh
cd710 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f ip transfer is o
cd720 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 nly allowed if t
cd730 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63 he unixFile is c
cd740 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 urrently unlocke
cd750 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 d..** If the uni
cd760 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 xFile is locked
cd770 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 and an ownership
cd780 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 is wrong, then
cd790 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
cd7a0 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45 _MISUSE. SQLITE
cd7b0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
cd7c0 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f if everything wo
cd7d0 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 rks..*/.#if SQLI
cd7e0 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 73 74 TE_THREADSAFE.st
cd7f0 61 74 69 63 20 69 6e 74 20 74 72 61 6e 73 66 65 atic int transfe
cd800 72 4f 77 6e 65 72 73 68 69 70 28 75 6e 69 78 46 rOwnership(unixF
cd810 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 ile *pFile){. i
cd820 6e 74 20 72 63 3b 0a 20 20 70 74 68 72 65 61 64 nt rc;. pthread
cd830 5f 74 20 68 53 65 6c 66 3b 0a 20 20 69 66 28 20 _t hSelf;. if(
cd840 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 threadsOverrideE
cd850 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 29 achOthersLocks )
cd860 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 72 73 68 {. /* Ownersh
cd870 69 70 20 74 72 61 6e 73 66 65 72 73 20 6e 6f 74 ip transfers not
cd880 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 69 73 20 needed on this
cd890 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20 72 65 system */. re
cd8a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
cd8b0 20 20 7d 0a 20 20 68 53 65 6c 66 20 3d 20 70 74 }. hSelf = pt
cd8c0 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 hread_self();.
cd8d0 69 66 28 20 70 74 68 72 65 61 64 5f 65 71 75 61 if( pthread_equa
cd8e0 6c 28 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 l(pFile->tid, hS
cd8f0 65 6c 66 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 elf) ){. /* W
cd900 65 20 61 72 65 20 73 74 69 6c 6c 20 69 6e 20 74 e are still in t
cd910 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 2a he same thread *
cd920 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 22 /. OSTRACE1("
cd930 4e 6f 2d 74 72 61 6e 73 66 65 72 2c 20 73 61 6d No-transfer, sam
cd940 65 20 74 68 72 65 61 64 5c 6e 22 29 3b 0a 20 20 e thread\n");.
cd950 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
cd960 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 OK;. }. if( pF
cd970 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e ile->locktype!=N
cd980 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a O_LOCK ){. /*
cd990 20 57 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 We cannot chang
cd9a0 65 20 6f 77 6e 65 72 73 68 69 70 20 77 68 69 6c e ownership whil
cd9b0 65 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 e we are holding
cd9c0 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 20 20 20 a lock! */.
cd9d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
cd9e0 53 55 53 45 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 SUSE;. }. OSTR
cd9f0 41 43 45 34 28 22 54 72 61 6e 73 66 65 72 20 6f ACE4("Transfer o
cda00 77 6e 65 72 73 68 69 70 20 6f 66 20 25 64 20 66 wnership of %d f
cda10 72 6f 6d 20 25 64 20 74 6f 20 25 64 5c 6e 22 2c rom %d to %d\n",
cda20 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 46 69 . pFi
cda30 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 74 69 le->h, pFile->ti
cda40 64 2c 20 68 53 65 6c 66 29 3b 0a 20 20 70 46 69 d, hSelf);. pFi
cda50 6c 65 2d 3e 74 69 64 20 3d 20 68 53 65 6c 66 3b le->tid = hSelf;
cda60 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 70 4c . if (pFile->pL
cda70 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20 ock != NULL) {.
cda80 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e releaseLockIn
cda90 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 fo(pFile->pLock)
cdaa0 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c ;. rc = findL
cdab0 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 68 ockInfo(pFile->h
cdac0 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2c , &pFile->pLock,
cdad0 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 0);. OSTRACE
cdae0 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69 73 5("LOCK %d is
cdaf0 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c 6e now %s(%s,%d)\n
cdb00 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 ", pFile->h,.
cdb10 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 locktype
cdb20 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b Name(pFile->lock
cdb30 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20 20 type),.
cdb40 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 locktypeName(p
cdb50 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 File->pLock->loc
cdb60 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e 70 ktype), pFile->p
cdb70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20 20 Lock->cnt);.
cdb80 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 return rc;. } e
cdb90 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e lse {. return
cdba0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
cdbb0 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 4f 6e 20 }.#else. /* On
cdbc0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 single-threaded
cdbd0 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68 69 builds, ownershi
cdbe0 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 61 20 p transfer is a
cdbf0 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 6e no-op */.# defin
cdc00 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 e transferOwners
cdc10 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f 4b hip(X) SQLITE_OK
cdc20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 .#endif../*.** S
cdc30 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 eek to the offse
cdc40 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 t passed as the
cdc50 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c second argument,
cdc60 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 0a then read cnt .
cdc70 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 ** bytes into pB
cdc80 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 20 6e uf. Return the n
cdc90 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 umber of bytes a
cdca0 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a ctually read..**
cdcb0 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 20 .** NB: If you
cdcc0 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 define USE_PREAD
cdcd0 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 34 2c or USE_PREAD64,
cdce0 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 61 then it might a
cdcf0 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 lso.** be necess
cdd00 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 ary to define _X
cdd10 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 62 OPEN_SOURCE to b
cdd20 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 61 72 e 500. This var
cdd30 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 ies from.** one
cdd40 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 65 system to anothe
cdd50 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 74 65 r. Since SQLite
cdd60 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 does not define
cdd70 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 6e USE_PREAD.** an
cdd80 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 65 y any form by de
cdd90 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 6e fault, we will n
cdda0 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 ot attempt to de
cddb0 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 fine _XOPEN_SOUR
cddc0 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65 CE..** See ticke
cddd0 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 32 36 ts #2741 and #26
cdde0 38 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 81..*/.static in
cddf0 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e t seekAndRead(un
cde00 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 ixFile *id, sqli
cde10 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
cde20 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e , void *pBuf, in
cde30 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f t cnt){. int go
cde40 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 t;. i64 newOffs
cde50 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 et;. TIMER_STAR
cde60 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 T;.#if defined(U
cde70 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 SE_PREAD). got
cde80 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 = pread(id->h, p
cde90 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
cdea0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
cdeb0 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
cdec0 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 ;.#elif defined(
cded0 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 USE_PREAD64). g
cdee0 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d ot = pread64(id-
cdef0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
cdf00 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 ffset);. Simula
cdf10 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d teIOError( got =
cdf20 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e -1 );.#else. n
cdf30 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b ewOffset = lseek
cdf40 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 (id->h, offset,
cdf50 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d SEEK_SET);. Sim
cdf60 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65 ulateIOError( ne
cdf70 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69 wOffset-- );. i
cdf80 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 f( newOffset!=of
cdf90 66 73 65 74 20 29 7b 0a 20 20 20 20 72 65 74 75 fset ){. retu
cdfa0 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 rn -1;. }. got
cdfb0 20 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 = read(id->h, p
cdfc0 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 Buf, cnt);.#endi
cdfd0 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 f. TIMER_END;.
cdfe0 20 4f 53 54 52 41 43 45 35 28 22 52 45 41 44 20 OSTRACE5("READ
cdff0 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c %-3d %5d %7ll
ce000 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 d %llu\n", id->h
ce010 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 , got, offset, T
ce020 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 IMER_ELAPSED);.
ce030 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a return got;.}..
ce040 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 /*.** Read data
ce050 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f from a file into
ce060 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 75 a buffer. Retu
ce070 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 rn SQLITE_OK if
ce080 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 all.** bytes wer
ce090 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75 e read successfu
ce0a0 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 lly and SQLITE_I
ce0b0 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67 OERR if anything
ce0c0 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a goes.** wrong..
ce0d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
ce0e0 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 ixRead(. sqlite
ce0f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 76 3_file *id, . v
ce100 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 69 6e oid *pBuf, . in
ce110 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65 33 t amt,. sqlite3
ce120 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a 29 7b _int64 offset.){
ce130 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73 . int got;. as
ce140 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 67 6f sert( id );. go
ce150 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 t = seekAndRead(
ce160 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20 6f (unixFile*)id, o
ce170 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 ffset, pBuf, amt
ce180 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 6d );. if( got==am
ce190 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
ce1a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
ce1b0 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a se if( got<0 ){.
ce1c0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ce1d0 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 E_IOERR_READ;.
ce1e0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 6e }else{. /* Un
ce1f0 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 read parts of th
ce200 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 e buffer must be
ce210 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a zero-filled */.
ce220 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 memset(&((ch
ce230 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 ar*)pBuf)[got],
ce240 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 0, amt-got);.
ce250 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
ce260 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b OERR_SHORT_READ;
ce270 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
ce280 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 ek to the offset
ce290 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 in id->offset t
ce2a0 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74 hen read cnt byt
ce2b0 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a es into pBuf..**
ce2c0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
ce2d0 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 er of bytes actu
ce2e0 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61 ally read. Upda
ce2f0 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a te the offset..*
ce300 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65 /.static int see
ce310 6b 41 6e 64 57 72 69 74 65 28 75 6e 69 78 46 69 kAndWrite(unixFi
ce320 6c 65 20 2a 69 64 2c 20 69 36 34 20 6f 66 66 73 le *id, i64 offs
ce330 65 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a et, const void *
ce340 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a pBuf, int cnt){.
ce350 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34 int got;. i64
ce360 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 49 newOffset;. TI
ce370 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 64 MER_START;.#if d
ce380 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 efined(USE_PREAD
ce390 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 ). got = pwrite
ce3a0 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e (id->h, pBuf, cn
ce3b0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 69 t, offset);.#eli
ce3c0 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 f defined(USE_PR
ce3d0 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 70 EAD64). got = p
ce3e0 77 72 69 74 65 36 34 28 69 64 2d 3e 68 2c 20 70 write64(id->h, p
ce3f0 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
ce400 29 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 );.#else. newOf
ce410 66 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d fset = lseek(id-
ce420 3e 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b >h, offset, SEEK
ce430 5f 53 45 54 29 3b 0a 20 20 69 66 28 20 6e 65 77 _SET);. if( new
ce440 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29 Offset!=offset )
ce450 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b {. return -1;
ce460 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 77 72 69 . }. got = wri
ce470 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 te(id->h, pBuf,
ce480 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 cnt);.#endif. T
ce490 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 4f 53 54 52 IMER_END;. OSTR
ce4a0 41 43 45 35 28 22 57 52 49 54 45 20 20 20 25 2d ACE5("WRITE %-
ce4b0 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 3d %5d %7lld %ll
ce4c0 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 u\n", id->h, got
ce4d0 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f , offset, TIMER_
ce4e0 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 ELAPSED);. retu
ce4f0 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a rn got;.}.../*.*
ce500 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f * Write data fro
ce510 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 m a buffer into
ce520 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 a file. Return
ce530 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
ce540 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 cess.** or some
ce550 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 other error code
ce560 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a on failure..*/.
ce570 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57 static int unixW
ce580 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f rite(. sqlite3_
ce590 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e file *id, . con
ce5a0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a st void *pBuf, .
ce5b0 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c int amt,. sql
ce5c0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 ite3_int64 offse
ce5d0 74 20 0a 29 7b 0a 20 20 69 6e 74 20 77 72 6f 74 t .){. int wrot
ce5e0 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 e = 0;. assert(
ce5f0 20 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 id );. assert(
ce600 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c amt>0 );. whil
ce610 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77 72 6f e( amt>0 && (wro
ce620 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69 74 te = seekAndWrit
ce630 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c e((unixFile*)id,
ce640 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 offset, pBuf, a
ce650 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d mt))>0 ){. am
ce660 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 t -= wrote;.
ce670 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b offset += wrote;
ce680 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 . pBuf = &((c
ce690 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 har*)pBuf)[wrote
ce6a0 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 ];. }. Simulat
ce6b0 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65 eIOError(( wrote
ce6c0 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b =(-1), amt=1 ));
ce6d0 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 . SimulateDiskf
ce6e0 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65 ullError(( wrote
ce6f0 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 =0, amt=1 ));.
ce700 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 if( amt>0 ){.
ce710 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a if( wrote<0 ){.
ce720 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
ce730 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b ITE_IOERR_WRITE;
ce740 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
ce750 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ce760 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a FULL;. }. }.
ce770 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ce780 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 OK;.}..#ifdef SQ
ce790 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LITE_TEST./*.**
ce7a0 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 Count the number
ce7b0 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e of fullsyncs an
ce7c0 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 d normal syncs.
ce7d0 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f This is used to
ce7e0 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 test.** that sy
ce7f0 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 ncs and fullsync
ce800 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 s are occuring a
ce810 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 t the right time
ce820 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
ce830 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e int sqlite3_syn
ce840 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c c_count = 0;.SQL
ce850 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
ce860 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
ce870 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
ce880 2f 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 64 /*.** Use the fd
ce890 61 74 61 73 79 6e 63 28 29 20 41 50 49 20 6f 6e atasync() API on
ce8a0 6c 79 20 69 66 20 74 68 65 20 48 41 56 45 5f 46 ly if the HAVE_F
ce8b0 44 41 54 41 53 59 4e 43 20 6d 61 63 72 6f 20 69 DATASYNC macro i
ce8c0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 4f 74 s defined..** Ot
ce8d0 68 65 72 77 69 73 65 20 75 73 65 20 66 73 79 6e herwise use fsyn
ce8e0 63 28 29 20 69 6e 20 69 74 73 20 70 6c 61 63 65 c() in its place
ce8f0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 48 41 56 ..*/.#ifndef HAV
ce900 45 5f 46 44 41 54 41 53 59 4e 43 0a 23 20 64 65 E_FDATASYNC.# de
ce910 66 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20 66 fine fdatasync f
ce920 73 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a sync.#endif../*.
ce930 2a 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f 46 ** Define HAVE_F
ce940 55 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f 72 ULLFSYNC to 0 or
ce950 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 1 depending on
ce960 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a whether or not.*
ce970 2a 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e * the F_FULLFSYN
ce980 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e C macro is defin
ce990 65 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 ed. F_FULLFSYNC
ce9a0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a is currently.**
ce9b0 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
ce9c0 6f 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42 75 on Mac OS X. Bu
ce9d0 74 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68 61 t that could cha
ce9e0 6e 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46 nge..*/.#ifdef F
ce9f0 5f 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66 _FULLFSYNC.# def
cea00 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 ine HAVE_FULLFSY
cea10 4e 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 NC 1.#else.# def
cea20 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 ine HAVE_FULLFSY
cea30 4e 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a NC 0.#endif.../*
cea40 0a 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29 20 .** The fsync()
cea50 73 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73 system call does
cea60 20 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64 76 not work as adv
cea70 65 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a ertised on many.
cea80 2a 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73 2e ** unix systems.
cea90 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
ceaa0 70 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e 20 procedure is an
ceab0 61 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a attempt to make.
ceac0 2a 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74 65 ** it work bette
cead0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c r..**.** The SQL
ceae0 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 ITE_NO_SYNC macr
ceaf0 6f 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 66 o disables all f
ceb00 73 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20 69 sync()s. This i
ceb10 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 s useful.** for
ceb20 74 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65 20 testing when we
ceb30 77 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72 6f want to run thro
ceb40 75 67 68 20 74 68 65 20 74 65 73 74 20 73 75 69 ugh the test sui
ceb50 74 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 te quickly..** Y
ceb60 6f 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79 20 ou are strongly
ceb70 61 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f advised *not* to
ceb80 20 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51 4c deploy with SQL
ceb90 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 ITE_NO_SYNC.** e
ceba0 6e 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c nabled, however,
cebb0 20 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c 49 since with SQLI
cebc0 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c TE_NO_SYNC enabl
cebd0 65 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68 0a ed, an OS crash.
cebe0 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c ** or power fail
cebf0 75 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 ure will likely
cec00 63 6f 72 72 75 70 74 20 74 68 65 20 64 61 74 61 corrupt the data
cec10 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 base file..*/.st
cec20 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 73 atic int full_fs
cec30 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 ync(int fd, int
cec40 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 61 fullSync, int da
cec50 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 72 taOnly){. int r
cec60 63 3b 0a 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20 c;.. /* Record
cec70 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 the number of ti
cec80 6d 65 73 20 74 68 61 74 20 77 65 20 64 6f 20 61 mes that we do a
cec90 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 normal fsync()
ceca0 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c 4c 53 59 and . ** FULLSY
cecb0 4e 43 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 NC. This is use
cecc0 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 d during testing
cecd0 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 to verify that
cece0 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 0a 20 this procedure.
cecf0 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c 65 64 20 ** gets called
ced00 77 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 with the correct
ced10 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 20 2a 2f arguments.. */
ced20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
ced30 45 53 54 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 EST. if( fullSy
ced40 6e 63 20 29 20 73 71 6c 69 74 65 33 5f 66 75 6c nc ) sqlite3_ful
ced50 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 lsync_count++;.
ced60 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f sqlite3_sync_co
ced70 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 20 unt++;.#endif..
ced80 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c /* If we compil
ced90 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 ed with the SQLI
ceda0 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c TE_NO_SYNC flag,
cedb0 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73 then syncing is
cedc0 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 a. ** no-op.
cedd0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
cede0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63 20 3d 20 _NO_SYNC. rc =
cedf0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 SQLITE_OK;.#else
cee00 0a 0a 23 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 ..#if HAVE_FULLF
cee10 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 SYNC. if( fullS
cee20 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ync ){. rc =
cee30 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c fcntl(fd, F_FULL
cee40 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c FSYNC, 0);. }el
cee50 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a se{. rc = 1;.
cee60 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }. /* If the
cee70 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 FULLFSYNC failed
cee80 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 , fall back to a
cee90 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 ttempting an fsy
ceea0 6e 63 28 29 2e 0a 20 20 20 2a 20 49 74 20 73 68 nc().. * It sh
ceeb0 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 ouldn't be possi
ceec0 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e ble for fullfsyn
ceed0 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 c to fail on the
ceee0 20 6c 6f 63 61 6c 20 0a 20 20 20 2a 20 66 69 6c local . * fil
ceef0 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 e system (on OSX
cef00 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e ), so failure in
cef10 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c dicates that FUL
cef20 4c 46 53 59 4e 43 0a 20 20 20 2a 20 69 73 6e 27 LFSYNC. * isn'
cef30 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 t supported for
cef40 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d this file system
cef50 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e . So, attempt an
cef60 20 66 73 79 6e 63 20 0a 20 20 20 2a 20 61 6e 64 fsync . * and
cef70 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 (for now) ignor
cef80 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f e the overhead o
cef90 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 f a superfluous
cefa0 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 fcntl call. .
cefb0 20 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65 * It'd be bette
cefc0 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c r to detect full
cefd0 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e fsync support on
cefe0 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 ce and avoid .
ceff0 20 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c * the fcntl cal
cf000 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e l every time syn
cf010 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 c is called..
cf020 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63 */. if( rc ) rc
cf030 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 = fsync(fd);..#
cf040 65 6c 73 65 20 0a 20 20 69 66 28 20 64 61 74 61 else . if( data
cf050 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d Only ){. rc =
cf060 20 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a fdatasync(fd);.
cf070 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
cf080 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 7d = fsync(fd);. }
cf090 0a 23 65 6e 64 69 66 20 2f 2a 20 48 41 56 45 5f .#endif /* HAVE_
cf0a0 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a 23 65 6e FULLFSYNC */.#en
cf0b0 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 dif /* defined(S
cf0c0 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 29 20 2a QLITE_NO_SYNC) *
cf0d0 2f 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a /.. return rc;.
cf0e0 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 }../*.** Make su
cf0f0 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f re all writes to
cf100 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 a particular fi
cf110 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 le are committed
cf120 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 to disk..**.**
cf130 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 If dataOnly==0 t
cf140 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 6c hen both the fil
cf150 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 73 e itself and its
cf160 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 0a metadata (file.
cf170 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 20 ** size, access
cf180 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 73 time, etc) are s
cf190 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 4f ynced. If dataO
cf1a0 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 nly!=0 then only
cf1b0 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 the.** file dat
cf1c0 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a a is synced..**.
cf1d0 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 ** Under Unix, a
cf1e0 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 lso make sure th
cf1f0 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 at the directory
cf200 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 entry for the f
cf210 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ile.** has been
cf220 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e 63 created by fsync
cf230 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 6f -ing the directo
cf240 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ry that contains
cf250 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 the file..** If
cf260 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 we do not do th
cf270 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e is and we encoun
cf280 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c ter a power fail
cf290 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f ure, the directo
cf2a0 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 ry.** entry for
cf2b0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 the journal migh
cf2c0 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65 t not exist afte
cf2d0 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 r we reboot. Th
cf2e0 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 e next.** SQLite
cf2f0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 66 to access the f
cf300 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f ile will not kno
cf310 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e w that the journ
cf320 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 75 al exists (becau
cf330 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 se.** the direct
cf340 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 ory entry for th
cf350 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 e journal was ne
cf360 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e 64 ver created) and
cf370 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
cf380 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c .** will not rol
cf390 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c l back - possibl
cf3a0 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 74 y leading to dat
cf3b0 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
cf3c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
cf3d0 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65 33 unixSync(sqlite3
cf3e0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 _file *id, int f
cf3f0 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b lags){. int rc;
cf400 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
cf410 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
cf420 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 id;.. int isDat
cf430 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 aOnly = (flags&S
cf440 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f QLITE_SYNC_DATAO
cf450 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 NLY);. int isFu
cf460 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 llsync = (flags&
cf470 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 0x0F)==SQLITE_SY
cf480 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 NC_FULL;.. /* C
cf490 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 heck that one of
cf4a0 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 SQLITE_SYNC_NOR
cf4b0 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 MAL or FULL was
cf4c0 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 passed */. asse
cf4d0 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 29 3d rt((flags&0x0F)=
cf4e0 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 =SQLITE_SYNC_NOR
cf4f0 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c MAL. || (fl
cf500 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 ags&0x0F)==SQLIT
cf510 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b E_SYNC_FULL. );
cf520 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e .. /* Unix cann
cf530 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 73 ot, but some sys
cf540 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e 20 tems may return
cf550 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d SQLITE_FULL from
cf560 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a here. This. **
cf570 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73 74 line is to test
cf580 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 that doing so d
cf590 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61 6e oes not cause an
cf5a0 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f y problems.. */
cf5b0 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 . SimulateDiskf
cf5c0 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e ullError( return
cf5d0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a SQLITE_FULL );.
cf5e0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
cf5f0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 );. OSTRACE2("
cf600 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c SYNC %-3d\n",
cf610 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 pFile->h);. rc
cf620 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 = full_fsync(pF
cf630 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 ile->h, isFullsy
cf640 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b nc, isDataOnly);
cf650 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
cf660 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 or( rc=1 );. if
cf670 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 ( rc ){. retu
cf680 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
cf690 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 FSYNC;. }. if(
cf6a0 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 pFile->dirfd>=0
cf6b0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 ){. OSTRACE4
cf6c0 28 22 44 49 52 53 59 4e 43 20 25 2d 33 64 20 28 ("DIRSYNC %-3d (
cf6d0 68 61 76 65 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 have_fullfsync=%
cf6e0 64 20 66 75 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e d fullsync=%d)\n
cf6f0 22 2c 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c ", pFile->dirfd,
cf700 0a 20 20 20 20 20 20 20 20 20 20 20 20 48 41 56 . HAV
cf710 45 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 69 73 46 E_FULLFSYNC, isF
cf720 75 6c 6c 73 79 6e 63 29 3b 0a 23 69 66 6e 64 65 ullsync);.#ifnde
cf730 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 f SQLITE_DISABLE
cf740 5f 44 49 52 53 59 4e 43 0a 20 20 20 20 2f 2a 20 _DIRSYNC. /*
cf750 54 68 65 20 64 69 72 65 63 74 6f 72 79 20 73 79 The directory sy
cf760 6e 63 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d nc is only attem
cf770 70 74 65 64 20 69 66 20 66 75 6c 6c 5f 66 73 79 pted if full_fsy
cf780 6e 63 20 69 73 0a 20 20 20 20 2a 2a 20 74 75 72 nc is. ** tur
cf790 6e 65 64 20 6f 66 66 20 6f 72 20 75 6e 61 76 61 ned off or unava
cf7a0 69 6c 61 62 6c 65 2e 20 20 49 66 20 61 20 66 75 ilable. If a fu
cf7b0 6c 6c 5f 66 73 79 6e 63 20 6f 63 63 75 72 72 65 ll_fsync occurre
cf7c0 64 20 61 62 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 d above,. **
cf7d0 74 68 65 6e 20 74 68 65 20 64 69 72 65 63 74 6f then the directo
cf7e0 72 79 20 73 79 6e 63 20 69 73 20 73 75 70 65 72 ry sync is super
cf7f0 66 6c 75 6f 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 fluous.. */.
cf800 20 20 20 69 66 28 20 28 21 48 41 56 45 5f 46 55 if( (!HAVE_FU
cf810 4c 4c 46 53 59 4e 43 20 7c 7c 20 21 69 73 46 75 LLFSYNC || !isFu
cf820 6c 6c 73 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f llsync) && full_
cf830 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72 fsync(pFile->dir
cf840 66 64 2c 30 2c 30 29 20 29 7b 0a 20 20 20 20 20 fd,0,0) ){.
cf850 20 20 2f 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57 /*. ** W
cf860 65 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 e have received
cf870 6d 75 6c 74 69 70 6c 65 20 72 65 70 6f 72 74 73 multiple reports
cf880 20 6f 66 20 66 73 79 6e 63 28 29 20 72 65 74 75 of fsync() retu
cf890 72 6e 69 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20 rning. **
cf8a0 65 72 72 6f 72 73 20 77 68 65 6e 20 61 70 70 6c errors when appl
cf8b0 69 65 64 20 74 6f 20 64 69 72 65 63 74 6f 72 69 ied to directori
cf8c0 65 73 20 6f 6e 20 63 65 72 74 61 69 6e 20 66 69 es on certain fi
cf8d0 6c 65 20 73 79 73 74 65 6d 73 2e 0a 20 20 20 20 le systems..
cf8e0 20 20 20 2a 2a 20 41 20 66 61 69 6c 65 64 20 64 ** A failed d
cf8f0 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 irectory sync is
cf900 20 6e 6f 74 20 61 20 62 69 67 20 64 65 61 6c 2e not a big deal.
cf910 20 20 53 6f 20 69 74 20 73 65 65 6d 73 0a 20 20 So it seems.
cf920 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 74 ** better t
cf930 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 65 72 72 o ignore the err
cf940 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31 36 35 or. Ticket #165
cf950 37 0a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 7. */.
cf960 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c /* return SQL
cf970 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 ITE_IOERR; */.
cf980 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 }.#endif. c
cf990 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 lose(pFile->dirf
cf9a0 64 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65 d); /* Only nee
cf9b0 64 20 74 6f 20 73 79 6e 63 20 6f 6e 63 65 2c 20 d to sync once,
cf9c0 73 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 69 72 so close the dir
cf9d0 65 63 74 6f 72 79 20 2a 2f 0a 20 20 20 20 70 46 ectory */. pF
cf9e0 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b ile->dirfd = -1;
cf9f0 20 20 20 20 2f 2a 20 77 68 65 6e 20 77 65 20 61 /* when we a
cfa00 72 65 20 64 6f 6e 65 2e 20 2a 2f 0a 20 20 7d 0a re done. */. }.
cfa10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
cfa20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 OK;.}../*.** Tru
cfa30 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 ncate an open fi
cfa40 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 le to a specifie
cfa50 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 d size.*/.static
cfa60 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 int unixTruncat
cfa70 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
cfa80 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a id, i64 nByte){.
cfa90 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
cfaa0 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 rt( id );. Simu
cfab0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
cfac0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
cfad0 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 _TRUNCATE );. r
cfae0 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 c = ftruncate(((
cfaf0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 unixFile*)id)->h
cfb00 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b , (off_t)nByte);
cfb10 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
cfb20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
cfb30 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20 OERR_TRUNCATE;.
cfb40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
cfb50 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
cfb60 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 }.}../*.** Deter
cfb70 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74 mine the current
cfb80 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 size of a file
cfb90 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 in bytes.*/.stat
cfba0 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 53 ic int unixFileS
cfbb0 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ize(sqlite3_file
cfbc0 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 *id, i64 *pSize
cfbd0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
cfbe0 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a truct stat buf;.
cfbf0 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a assert( id );.
cfc00 20 20 72 63 20 3d 20 66 73 74 61 74 28 28 28 75 rc = fstat(((u
cfc10 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c nixFile*)id)->h,
cfc20 20 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 6c 61 &buf);. Simula
cfc30 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 teIOError( rc=1
cfc40 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 );. if( rc!=0 )
cfc50 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
cfc60 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b ITE_IOERR_FSTAT;
cfc70 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 . }. *pSize =
cfc80 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 buf.st_size;..
cfc90 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 /* When opening
cfca0 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 a zero-size data
cfcb0 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f base, the findLo
cfcc0 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 ckInfo() procedu
cfcd0 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 re. ** writes a
cfce0 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 single byte int
cfcf0 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f o that file in o
cfd00 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f rder to work aro
cfd10 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 und a bug. ** i
cfd20 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 n the OS-X msdos
cfd30 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e filesystem. In
cfd40 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 order to avoid
cfd50 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 problems with up
cfd60 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c per. ** layers,
cfd70 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f we need to repo
cfd80 72 74 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a rt this file siz
cfd90 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 e as zero even t
cfda0 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a hough it is. **
cfdb0 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 really 1. Tic
cfdc0 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a ket #3260.. */.
cfdd0 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 if( *pSize==1
cfde0 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a ) *pSize = 0;...
cfdf0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
cfe00 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 OK;.}../*.** Thi
cfe10 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 6c s routine transl
cfe20 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 64 20 ates a standard
cfe30 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f 64 65 POSIX errno code
cfe40 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a into something.
cfe50 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 68 65 ** useful to the
cfe60 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 65 20 clients of the
cfe70 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 6f 6e sqlite3 function
cfe80 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79 s. Specifically
cfe90 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 65 6e , it is.** inten
cfea0 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 ded to translate
cfeb0 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 22 74 a variety of "t
cfec0 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f 72 73 ry again" errors
cfed0 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 55 53 into SQLITE_BUS
cfee0 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 69 65 Y.** and a varie
cfef0 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 63 6c ty of "please cl
cff00 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 ose the file des
cff10 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 72 72 criptor NOW" err
cff20 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c ors into .** SQL
cff30 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a ITE_IOERR.** .**
cff40 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 20 69 Errors during i
cff50 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 nitialization of
cff60 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20 locks, or file
cff70 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 66 system support f
cff80 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f or locks,.** sho
cff90 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 uld handle ENOLC
cffa0 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e K, ENOTSUP, EOPN
cffb0 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 65 6c OTSUPP separatel
cffc0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 y..*/.static int
cffd0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
cffe0 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20 70 PosixError(int p
cfff0 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20 73 osixError, int s
d0000 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a 20 20 qliteIOErr) {.
d0010 73 77 69 74 63 68 20 28 70 6f 73 69 78 45 72 72 switch (posixErr
d0020 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30 3a 20 or) {. case 0:
d0030 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d0040 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 61 TE_OK;. . ca
d0050 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61 73 se EAGAIN:. cas
d0060 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63 e ETIMEDOUT:. c
d0070 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61 73 ase EBUSY:. cas
d0080 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65 20 e EINTR:. case
d0090 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a ENOLCK: . /*
d00a0 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72 random NFS retr
d00b0 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20 y error, unless
d00c0 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74 during file syst
d00d0 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 20 em support .
d00e0 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e * introspection
d00f0 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 63 , in which it ac
d0100 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 tually means wha
d0110 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20 20 t it says */.
d0120 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
d0130 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 USY;. . case
d0140 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20 2f 2a EACCES: . /*
d0150 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b 65 20 EACCES is like
d0160 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f EAGAIN during lo
d0170 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 cking operations
d0180 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74 , but not any ot
d0190 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 20 69 her time*/. i
d01a0 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72 20 f( (sqliteIOErr
d01b0 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f == SQLITE_IOERR_
d01c0 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 LOCK) || ..(sqli
d01d0 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 teIOErr == SQLIT
d01e0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 E_IOERR_UNLOCK)
d01f0 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 || ..(sqliteIOEr
d0200 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 r == SQLITE_IOER
d0210 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73 R_RDLOCK) ||..(s
d0220 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 qliteIOErr == SQ
d0230 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
d0240 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29 7b RESERVEDLOCK) ){
d0250 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
d0260 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
d0270 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61 6c . /* else fal
d0280 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 63 l through */. c
d0290 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20 20 ase EPERM: .
d02a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 return SQLITE_PE
d02b0 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 RM;. . case
d02c0 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74 EDEADLK:. ret
d02d0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
d02e0 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 0a 23 _BLOCKED;. .#
d02f0 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 3d 45 if EOPNOTSUPP!=E
d0300 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4f NOTSUP. case EO
d0310 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 20 2f PNOTSUPP: . /
d0320 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 * something went
d0330 20 74 65 72 72 69 62 6c 79 20 61 77 72 79 2c 20 terribly awry,
d0340 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 unless during fi
d0350 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 le system suppor
d0360 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 t . * intros
d0370 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 pection, in whic
d0380 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 h it actually me
d0390 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 ans what it says
d03a0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 */.#endif.#ifde
d03b0 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 f ENOTSUP. case
d03c0 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 20 2f ENOTSUP: . /
d03d0 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e * invalid fd, un
d03e0 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 less during file
d03f0 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 system support
d0400 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 introspection, i
d0410 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20 2a 20 n which . *
d0420 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e it actually mean
d0430 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a s what it says *
d0440 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 65 20 /.#endif. case
d0450 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 41 44 EIO:. case EBAD
d0460 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 41 4c F:. case EINVAL
d0470 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 4f 4e :. case ENOTCON
d0480 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 45 56 N:. case ENODEV
d0490 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f 3a 0a :. case ENXIO:.
d04a0 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a 0a 20 case ENOENT:.
d04b0 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a 20 20 case ESTALE:.
d04c0 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20 case ENOSYS:.
d04d0 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 6c 64 /* these should
d04e0 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69 65 6e force the clien
d04f0 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 66 t to close the f
d0500 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63 ile and reconnec
d0510 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 61 t */. . defa
d0520 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 72 6e ult: . return
d0530 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20 sqliteIOErr;.
d0540 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
d0550 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
d0560 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 f there is a RES
d0570 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 ERVED lock held
d0580 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
d0590 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 .** file by this
d05a0 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 or any other pr
d05b0 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 ocess. If such a
d05c0 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 lock is held, s
d05d0 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 et *pResOut.** t
d05e0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c o a non-zero val
d05f0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 ue otherwise *pR
d0600 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
d0610 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 zero. The retur
d0620 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 n value.** is se
d0630 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 t to SQLITE_OK u
d0640 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 nless an I/O err
d0650 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
d0660 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a lock checking..
d0670 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
d0680 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c ixCheckReservedL
d0690 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d06a0 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f *id, int *pResO
d06b0 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ut){. int rc =
d06c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
d06d0 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 reserved = 0;.
d06e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d06f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d0700 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 ;.. SimulateIOE
d0710 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
d0720 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
d0730 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a ESERVEDLOCK; );.
d0740 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
d0750 20 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 );. enterMutex
d0760 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 20 70 (); /* Because p
d0770 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 File->pLock is s
d0780 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 hared across thr
d0790 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 eads */.. /* Ch
d07a0 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 eck if a thread
d07b0 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
d07c0 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 holds such a loc
d07d0 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 k */. if( pFile
d07e0 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 ->pLock->locktyp
d07f0 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b e>SHARED_LOCK ){
d0800 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 . reserved =
d0810 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 68 1;. }.. /* Oth
d0820 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f erwise see if so
d0830 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 me other process
d0840 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a 2f 0a holds it.. */.
d0850 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 20 if( !reserved
d0860 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 66 6c ){. struct fl
d0870 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f ock lock;. lo
d0880 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 ck.l_whence = SE
d0890 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b EK_SET;. lock
d08a0 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 52 .l_start = RESER
d08b0 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f VED_BYTE;. lo
d08c0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 ck.l_len = 1;.
d08d0 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 lock.l_type =
d08e0 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 69 66 20 F_WRLCK;. if
d08f0 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 70 46 69 (-1 == fcntl(pFi
d0900 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 le->h, F_GETLK,
d0910 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 20 20 20 &lock)) {.
d0920 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
d0930 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 no;. rc = s
d0940 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d0950 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d0960 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 SQLITE_IOERR_CH
d0970 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 ECKRESERVEDLOCK)
d0980 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ;. pFile->l
d0990 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d09a0 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 o;. } else if
d09b0 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21 3d 46 ( lock.l_type!=F
d09c0 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 _UNLCK ){.
d09d0 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
d09e0 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 6c 65 61 }. }. . lea
d09f0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 veMutex();. OST
d0a00 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c RACE4("TEST WR-L
d0a10 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c OCK %d %d %d\n",
d0a20 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 pFile->h, rc, r
d0a30 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 eserved);.. *pR
d0a40 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 esOut = reserved
d0a50 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
d0a60 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 ../*.** Lock the
d0a70 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c file with the l
d0a80 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 ock specified by
d0a90 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 parameter lockt
d0aa0 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 ype - one.** of
d0ab0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
d0ac0 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 *.** (1) SHA
d0ad0 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 RED_LOCK.**
d0ae0 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 (2) RESERVED_LOC
d0af0 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e K.** (3) PEN
d0b00 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 DING_LOCK.**
d0b10 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c (4) EXCLUSIVE_L
d0b20 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 OCK.**.** Someti
d0b30 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 mes when request
d0b40 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 ing one lock sta
d0b50 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c te, additional l
d0b60 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 ock states.** ar
d0b70 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 e inserted in be
d0b80 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b tween. The lock
d0b90 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f ing might fail o
d0ba0 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 n one of the lat
d0bb0 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e er.** transition
d0bc0 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f s leaving the lo
d0bd0 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 ck state differe
d0be0 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 nt from what it
d0bf0 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 started but.** s
d0c00 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 till short of it
d0c10 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c s goal. The fol
d0c20 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f lowing chart sho
d0c30 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a ws the allowed.*
d0c40 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e * transitions an
d0c50 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 d the inserted i
d0c60 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 ntermediate stat
d0c70 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c es:.**.** UNL
d0c80 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a OCKED -> SHARED.
d0c90 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
d0ca0 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 RESERVED.** S
d0cb0 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e HARED -> (PENDIN
d0cc0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
d0cd0 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d ** RESERVED -
d0ce0 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
d0cf0 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 XCLUSIVE.** P
d0d00 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 ENDING -> EXCLUS
d0d10 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 IVE.**.** This r
d0d20 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 outine will only
d0d30 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b increase a lock
d0d40 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 . Use the sqlit
d0d50 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 e3OsUnlock().**
d0d60 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 routine to lower
d0d70 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c a locking level
d0d80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d0d90 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33 unixLock(sqlite3
d0da0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
d0db0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a 20 54 ocktype){. /* T
d0dc0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 he following des
d0dd0 63 72 69 62 65 73 20 74 68 65 20 69 6d 70 6c 65 cribes the imple
d0de0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
d0df0 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 61 various locks a
d0e00 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 72 61 nd. ** lock tra
d0e10 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65 72 6d nsitions in term
d0e20 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58 20 61 s of the POSIX a
d0e30 64 76 69 73 6f 72 79 20 73 68 61 72 65 64 20 61 dvisory shared a
d0e40 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20 20 2a nd exclusive. *
d0e50 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69 76 65 * lock primitive
d0e60 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64 2d 6c s (called read-l
d0e70 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65 2d 6c ocks and write-l
d0e80 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f 20 61 ocks below, to a
d0e90 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 75 73 void. ** confus
d0ea0 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74 65 20 ion with SQLite
d0eb0 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54 68 65 lock names). The
d0ec0 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20 algorithms are
d0ed0 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20 2a 2a complicated. **
d0ee0 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f 72 64 slightly in ord
d0ef0 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61 74 69 er to be compati
d0f00 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f 77 73 ble with windows
d0f10 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c 74 61 systems simulta
d0f20 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 63 63 neously. ** acc
d0f30 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 essing the same
d0f40 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 database file, i
d0f50 6e 20 63 61 73 65 20 74 68 61 74 20 69 73 20 65 n case that is e
d0f60 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a 20 20 ver required..
d0f70 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 20 **. ** Symbols
d0f80 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e 68 20 defined in os.h
d0f90 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20 27 70 indentify the 'p
d0fa0 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61 6e 64 ending byte' and
d0fb0 20 74 68 65 20 27 72 65 73 65 72 76 65 64 0a 20 the 'reserved.
d0fc0 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63 68 20 ** byte', each
d0fd0 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61 74 20 single bytes at
d0fe0 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 well known offse
d0ff0 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73 68 61 ts, and the 'sha
d1000 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 red byte. ** ra
d1010 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20 6f 66 nge', a range of
d1020 20 35 31 30 20 62 79 74 65 73 20 61 74 20 61 20 510 bytes at a
d1030 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 well known offse
d1040 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 t.. **. ** To
d1050 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45 44 20 obtain a SHARED
d1060 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63 lock, a read-loc
d1070 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e k is obtained on
d1080 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a 20 20 the 'pending.
d1090 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20 74 68 ** byte'. If th
d10a0 69 73 20 69 73 20 73 75 63 63 65 73 73 66 75 6c is is successful
d10b0 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 , a random byte
d10c0 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72 65 64 from the 'shared
d10d0 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 byte. ** range
d10e0 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b 65 64 ' is read-locked
d10f0 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e and the lock on
d1100 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 the 'pending by
d1110 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 te' released..
d1120 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 **. ** A proces
d1130 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 s may only obtai
d1140 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 n a RESERVED loc
d1150 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20 61 k after it has a
d1160 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a 20 20 SHARED lock..
d1170 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20 6c 6f ** A RESERVED lo
d1180 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 ck is implemente
d1190 64 20 62 79 20 67 72 61 62 62 69 6e 67 20 61 20 d by grabbing a
d11a0 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 write-lock on th
d11b0 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76 65 64 e. ** 'reserved
d11c0 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a 20 20 byte'. . **.
d11d0 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d 61 79 ** A process may
d11e0 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 only obtain a P
d11f0 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66 74 65 ENDING lock afte
d1200 72 20 69 74 20 68 61 73 20 6f 62 74 61 69 6e 65 r it has obtaine
d1210 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45 44 20 d a. ** SHARED
d1220 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e 47 20 lock. A PENDING
d1230 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e lock is implemen
d1240 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 ted by obtaining
d1250 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a 20 20 a write-lock.
d1260 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 ** on the 'pendi
d1270 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73 20 65 ng byte'. This e
d1280 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6e nsures that no n
d1290 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 ew SHARED locks
d12a0 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 74 61 can be. ** obta
d12b0 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73 74 69 ined, but existi
d12c0 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 ng SHARED locks
d12d0 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 70 are allowed to p
d12e0 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63 65 73 ersist. A proces
d12f0 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 s. ** does not
d1300 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 have to obtain a
d1310 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f RESERVED lock o
d1320 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61 20 50 n the way to a P
d1330 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20 2a ENDING lock.. *
d1340 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74 79 20 * This property
d1350 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 61 is used by the a
d1360 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72 6f 6c lgorithm for rol
d1370 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 ling back a jour
d1380 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 61 66 nal file. ** af
d1390 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20 20 2a ter a crash.. *
d13a0 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c 55 53 *. ** An EXCLUS
d13b0 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 6e IVE lock, obtain
d13c0 65 64 20 61 66 74 65 72 20 61 20 50 45 4e 44 49 ed after a PENDI
d13d0 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c NG lock is held,
d13e0 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 is. ** impleme
d13f0 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e nted by obtainin
d1400 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f g a write-lock o
d1410 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27 73 68 n the entire 'sh
d1420 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 ared byte. ** r
d1430 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 6c 6c ange'. Since all
d1440 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 65 71 other locks req
d1450 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b uire a read-lock
d1460 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 62 on one of the b
d1470 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68 69 6e ytes. ** within
d1480 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 68 69 this range, thi
d1490 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e s ensures that n
d14a0 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 61 72 o other locks ar
d14b0 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a 20 20 e held on the.
d14c0 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 ** database. .
d14d0 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65 61 73 **. ** The reas
d14e0 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 on a single byte
d14f0 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 cannot be used
d1500 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 27 instead of the '
d1510 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a shared byte. **
d1520 20 72 61 6e 67 65 27 20 69 73 20 74 68 61 74 20 range' is that
d1530 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 some versions of
d1540 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f 74 20 windows do not
d1550 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c 6f 63 support read-loc
d1560 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f 63 6b ks. By. ** lock
d1570 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 ing a random byt
d1580 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65 2c 20 e from a range,
d1590 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41 52 45 concurrent SHARE
d15a0 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78 69 73 D locks may exis
d15b0 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 t. ** even if t
d15c0 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 he locking primi
d15d0 74 69 76 65 20 75 73 65 64 20 69 73 20 61 6c 77 tive used is alw
d15e0 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ays a write-lock
d15f0 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 .. */. int rc
d1600 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 = SQLITE_OK;. u
d1610 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d1620 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d1630 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 struct lockInf
d1640 6f 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 o *pLock = pFile
d1650 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 ->pLock;. struc
d1660 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 t flock lock;.
d1670 69 6e 74 20 73 3b 0a 0a 20 20 61 73 73 65 72 74 int s;.. assert
d1680 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 ( pFile );. OST
d1690 52 41 43 45 37 28 22 4c 4f 43 4b 20 20 20 20 25 RACE7("LOCK %
d16a0 64 20 25 73 20 77 61 73 20 25 73 28 25 73 2c 25 d %s was %s(%s,%
d16b0 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 d) pid=%d\n", pF
d16c0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 6c 6f ile->h,. lo
d16d0 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 cktypeName(lockt
d16e0 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 ype), locktypeNa
d16f0 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 me(pFile->lockty
d1700 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 pe),. lockt
d1710 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c ypeName(pLock->l
d1720 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63 6b 2d ocktype), pLock-
d1730 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28 29 29 >cnt , getpid())
d1740 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
d1750 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f is already a lo
d1760 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ck of this type
d1770 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 or more restrict
d1780 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ive on the. **
d1790 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 unixFile, do not
d17a0 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 hing. Don't use
d17b0 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 the end_lock: ex
d17c0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a it path, as. **
d17d0 20 65 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 enterMutex() ha
d17e0 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 sn't been called
d17f0 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 yet.. */. if(
d1800 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d1810 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 >=locktype ){.
d1820 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b OSTRACE3("LOCK
d1830 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c %d %s ok (al
d1840 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 ready held)\n",
d1850 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 pFile->h,.
d1860 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 locktypeNa
d1870 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 me(locktype));.
d1880 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d1890 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d _OK;. }.. /* M
d18a0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
d18b0 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 king sequence is
d18c0 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 correct. */.
d18d0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
d18e0 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
d18f0 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 || locktype==SH
d1900 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
d1910 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
d1920 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b =PENDING_LOCK );
d1930 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
d1940 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype!=RESERVED_LO
d1950 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK || pFile->loc
d1960 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d1970 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 CK );.. /* This
d1980 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 mutex is needed
d1990 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e because pFile->
d19a0 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 pLock is shared
d19b0 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 across threads.
d19c0 20 2a 2f 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 */. enterMutex
d19d0 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 ();.. /* Make s
d19e0 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ure the current
d19f0 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 thread owns the
d1a00 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 pFile.. */. rc
d1a10 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 = transferOwner
d1a20 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 ship(pFile);. i
d1a30 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
d1a40 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 4d 75 74 ){. leaveMut
d1a50 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ex();. return
d1a60 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f 63 6b rc;. }. pLock
d1a70 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b = pFile->pLock;
d1a80 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 74 .. /* If some t
d1a90 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 hread using this
d1aa0 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 6b 20 PID has a lock
d1ab0 76 69 61 20 61 20 64 69 66 66 65 72 65 6e 74 20 via a different
d1ac0 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a 20 68 unixFile*. ** h
d1ad0 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 63 6c andle that precl
d1ae0 75 64 65 73 20 74 68 65 20 72 65 71 75 65 73 74 udes the request
d1af0 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 6e 20 ed lock, return
d1b00 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 BUSY.. */. if(
d1b10 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 (pFile->locktyp
d1b20 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 e!=pLock->lockty
d1b30 70 65 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 pe && .
d1b40 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 (pLock->locktyp
d1b50 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 e>=PENDING_LOCK
d1b60 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 || locktype>SHAR
d1b70 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 ED_LOCK)). ){.
d1b80 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
d1b90 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e USY;. goto en
d1ba0 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f d_lock;. }.. /
d1bb0 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 6c 6f * If a SHARED lo
d1bc0 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 2c ck is requested,
d1bd0 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 64 and some thread
d1be0 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44 20 using this PID
d1bf0 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 61 73 already. ** has
d1c00 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 45 53 a SHARED or RES
d1c10 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e ERVED lock, then
d1c20 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 65 72 increment refer
d1c30 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e 64 0a ence counts and.
d1c40 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 ** return SQLI
d1c50 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 TE_OK.. */. if
d1c60 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
d1c70 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20 20 ED_LOCK && .
d1c80 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 (pLock->lockty
d1c90 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
d1ca0 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 || pLock->lockty
d1cb0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe==RESERVED_LOC
d1cc0 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 K) ){. assert
d1cd0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
d1ce0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 ED_LOCK );. a
d1cf0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f ssert( pFile->lo
d1d00 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20 20 20 cktype==0 );.
d1d10 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
d1d20 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70 46 69 cnt>0 );. pFi
d1d30 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 le->locktype = S
d1d40 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 HARED_LOCK;.
d1d50 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 pLock->cnt++;.
d1d60 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e pFile->pOpen->
d1d70 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 6f 74 nLock++;. got
d1d80 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a o end_lock;. }.
d1d90 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 . lock.l_len =
d1da0 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 1L;.. lock.l_wh
d1db0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b ence = SEEK_SET;
d1dc0 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 .. /* A PENDING
d1dd0 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 lock is needed
d1de0 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 before acquiring
d1df0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 a SHARED lock a
d1e00 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 nd before. ** a
d1e10 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c cquiring an EXCL
d1e20 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 USIVE lock. For
d1e30 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b the SHARED lock
d1e40 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 , the PENDING wi
d1e50 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 ll. ** be relea
d1e60 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 sed.. */. if(
d1e70 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
d1e80 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 _LOCK . ||
d1e90 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 (locktype==EXCLU
d1ea0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 SIVE_LOCK && pFi
d1eb0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e le->locktype<PEN
d1ec0 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a DING_LOCK). ){.
d1ed0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 lock.l_type
d1ee0 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 = (locktype==SHA
d1ef0 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43 4b RED_LOCK?F_RDLCK
d1f00 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 20 6c :F_WRLCK);. l
d1f10 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 ock.l_start = PE
d1f20 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 NDING_BYTE;.
d1f30 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d s = fcntl(pFile-
d1f40 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f >h, F_SETLK, &lo
d1f50 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d ck);. if( s==
d1f60 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e (-1) ){. in
d1f70 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f t tErrno = errno
d1f80 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
d1f90 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d1fa0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d1fb0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
d1fc0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f );. if( IS_
d1fd0 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 LOCK_ERROR(rc) )
d1fe0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
d1ff0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d2000 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rno;. }.
d2010 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b goto end_lock
d2020 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 ;. }. }...
d2030 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 /* If control ge
d2040 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 ts to this point
d2050 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 , then actually
d2060 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b go ahead and mak
d2070 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 e. ** operating
d2080 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f system calls fo
d2090 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 r the specified
d20a0 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 lock.. */. if(
d20b0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
d20c0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e D_LOCK ){. in
d20d0 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 t tErrno = 0;.
d20e0 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d assert( pLock-
d20f0 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 >cnt==0 );. a
d2100 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f ssert( pLock->lo
d2110 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20 cktype==0 );..
d2120 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 /* Now get the
d2130 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 read-lock */.
d2140 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d lock.l_start =
d2150 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 SHARED_FIRST;.
d2160 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 lock.l_len =
d2170 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 SHARED_SIZE;.
d2180 20 69 66 28 20 28 73 20 3d 20 66 63 6e 74 6c 28 if( (s = fcntl(
d2190 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c pFile->h, F_SETL
d21a0 4b 2c 20 26 6c 6f 63 6b 29 29 3d 3d 28 2d 31 29 K, &lock))==(-1)
d21b0 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f ){. tErrno
d21c0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a = errno;. }.
d21d0 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 /* Drop the
d21e0 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e temporary PENDIN
d21f0 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f G lock */. lo
d2200 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e ck.l_start = PEN
d2210 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c DING_BYTE;. l
d2220 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a ock.l_len = 1L;.
d2230 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 lock.l_type
d2240 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69 = F_UNLCK;. i
d2250 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e f( fcntl(pFile->
d2260 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 h, F_SETLK, &loc
d2270 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 k)!=0 ){. i
d2280 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a 20 20 f( s != -1 ){.
d2290 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f /* This co
d22a0 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 68 20 uld happen with
d22b0 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20 a network mount
d22c0 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e */. tErrn
d22d0 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 20 20 o = errno; .
d22e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d22f0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d2300 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d2310 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
d2320 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 . if( IS
d2330 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d2340 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ){. pFi
d2350 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d2360 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 tErrno;.
d2370 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 }. goto e
d2380 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d nd_lock;. }
d2390 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
d23a0 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 ==(-1) ){.
d23b0 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
d23c0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
d23d0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
d23e0 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 ERR_LOCK);.
d23f0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d2400 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 OR(rc) ){.
d2410 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d2420 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d2430 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
d2440 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
d2450 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c ktype = SHARED_L
d2460 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 OCK;. pFile
d2470 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b ->pOpen->nLock++
d2480 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 ;. pLock->c
d2490 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 nt = 1;. }.
d24a0 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 }else if( lockty
d24b0 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
d24c0 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e 74 CK && pLock->cnt
d24d0 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 >1 ){. /* We
d24e0 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 are trying for a
d24f0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
d2500 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 but another thr
d2510 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20 ead in this.
d2520 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 ** same process
d2530 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 is still holding
d2540 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 a shared lock.
d2550 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 */. rc = SQLI
d2560 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 TE_BUSY;. }else
d2570 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 {. /* The req
d2580 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20 52 uest was for a R
d2590 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 ESERVED or EXCLU
d25a0 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 SIVE lock. It i
d25b0 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 s. ** assumed
d25c0 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 that there is a
d25d0 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 SHARED or great
d25e0 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 er lock on the f
d25f0 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 ile. ** alrea
d2600 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 dy.. */. a
d2610 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d ssert( 0!=pFile-
d2620 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 >locktype );.
d2630 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 lock.l_type = F
d2640 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69 74 _WRLCK;. swit
d2650 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a ch( locktype ){.
d2660 20 20 20 20 20 20 63 61 73 65 20 52 45 53 45 52 case RESER
d2670 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 VED_LOCK:.
d2680 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d lock.l_start =
d2690 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a RESERVED_BYTE;.
d26a0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
d26b0 20 20 20 20 20 63 61 73 65 20 45 58 43 4c 55 53 case EXCLUS
d26c0 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 IVE_LOCK:.
d26d0 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d lock.l_start =
d26e0 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 SHARED_FIRST;.
d26f0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 lock.l_le
d2700 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b n = SHARED_SIZE;
d2710 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
d2720 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 default:.
d2730 20 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29 assert(0)
d2740 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 20 3d 20 ;. }. s =
d2750 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 fcntl(pFile->h,
d2760 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b F_SETLK, &lock);
d2770 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 . if( s==(-1)
d2780 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 ){. int tE
d2790 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d27a0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d27b0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d27c0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d27d0 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 E_IOERR_LOCK);.
d27e0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d27f0 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d2800 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d2810 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d2820 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d2830 20 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d }. . if( rc==
d2840 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
d2850 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d2860 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 = locktype;.
d2870 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 pLock->locktype
d2880 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d = locktype;. }
d2890 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 else if( locktyp
d28a0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
d28b0 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
d28c0 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 locktype = PENDI
d28d0 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f NG_LOCK;. pLo
d28e0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 ck->locktype = P
d28f0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d ENDING_LOCK;. }
d2900 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 6c 65 ..end_lock:. le
d2910 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 aveMutex();. OS
d2920 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 TRACE4("LOCK
d2930 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 %d %s %s\n", pFi
d2940 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e le->h, locktypeN
d2950 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a ame(locktype), .
d2960 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 rc==SQLITE
d2970 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 _OK ? "ok" : "fa
d2980 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e iled");. return
d2990 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
d29a0 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
d29b0 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 level on file de
d29c0 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 scriptor pFile t
d29d0 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 o locktype. loc
d29e0 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 ktype.** must be
d29f0 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 either NO_LOCK
d2a00 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a or SHARED_LOCK..
d2a10 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 **.** If the loc
d2a20 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 king level of th
d2a30 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
d2a40 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 r is already at
d2a50 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 or below.** the
d2a60 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e requested lockin
d2a70 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f g level, this ro
d2a80 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
d2a90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d2aa0 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 unixUnlock(sqlit
d2ab0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d2ac0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 74 locktype){. st
d2ad0 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 ruct lockInfo *p
d2ae0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 Lock;. struct f
d2af0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 lock lock;. int
d2b00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d2b10 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d2b20 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d2b30 69 64 3b 0a 20 20 69 6e 74 20 68 3b 0a 0a 20 20 id;. int h;..
d2b40 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d2b50 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c . OSTRACE7("UNL
d2b60 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 OCK %d %d was %
d2b70 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c d(%d,%d) pid=%d\
d2b80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
d2b90 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46 cktype,. pF
d2ba0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 ile->locktype, p
d2bb0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 File->pLock->loc
d2bc0 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c ktype, pFile->pL
d2bd0 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64 ock->cnt, getpid
d2be0 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ());.. assert(
d2bf0 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 locktype<=SHARED
d2c00 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 _LOCK );. if( p
d2c10 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d File->locktype<=
d2c20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 locktype ){.
d2c30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d2c40 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43 ;. }. if( CHEC
d2c50 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c 65 K_THREADID(pFile
d2c60 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
d2c70 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
d2c80 20 7d 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 }. enterMutex(
d2c90 29 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e );. h = pFile->
d2ca0 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 h;. pLock = pFi
d2cb0 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 le->pLock;. ass
d2cc0 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 ert( pLock->cnt!
d2cd0 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c =0 );. if( pFil
d2ce0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 e->locktype>SHAR
d2cf0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 ED_LOCK ){. a
d2d00 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f ssert( pLock->lo
d2d10 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c cktype==pFile->l
d2d20 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 53 ocktype );. S
d2d30 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
d2d40 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d nign(1);. Sim
d2d50 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d ulateIOError( h=
d2d60 28 2d 31 29 20 29 0a 20 20 20 20 53 69 6d 75 6c (-1) ). Simul
d2d70 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
d2d80 28 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 (0);. if( loc
d2d90 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d2da0 43 4b 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b CK ){. lock
d2db0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 .l_type = F_RDLC
d2dc0 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f K;. lock.l_
d2dd0 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
d2de0 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f T;. lock.l_
d2df0 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 start = SHARED_F
d2e00 49 52 53 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b IRST;. lock
d2e10 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f .l_len = SHARED_
d2e20 53 49 5a 45 3b 0a 20 20 20 20 20 20 69 66 28 20 SIZE;. if(
d2e30 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b fcntl(h, F_SETLK
d2e40 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29 20 29 , &lock)==(-1) )
d2e50 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 45 {. int tE
d2e60 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d2e70 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
d2e80 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
d2e90 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
d2ea0 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b ITE_IOERR_RDLOCK
d2eb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 );. if( I
d2ec0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d2ed0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 ){. pF
d2ee0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d2ef0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 tErrno;.
d2f00 20 7d 0a 09 09 09 09 67 6f 74 6f 20 65 6e 64 5f }.....goto end_
d2f10 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a unlock;. }.
d2f20 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c }. lock.l
d2f30 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b _type = F_UNLCK;
d2f40 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e . lock.l_when
d2f50 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
d2f60 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
d2f70 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
d2f80 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
d2f90 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20 50 45 2L; assert( PE
d2fa0 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 45 NDING_BYTE+1==RE
d2fb0 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b 0a 20 SERVED_BYTE );.
d2fc0 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 if( fcntl(h,
d2fd0 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 F_SETLK, &lock)!
d2fe0 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 70 =(-1) ){. p
d2ff0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d Lock->locktype =
d3000 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
d3010 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
d3020 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d3030 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 o;. rc = sq
d3040 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d3050 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d3060 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
d3070 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
d3080 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d3090 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 ) ){. pFi
d30a0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d30b0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a tErrno;. }.
d30c0 09 09 09 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f ...goto end_unlo
d30d0 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ck;. }. }.
d30e0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f if( locktype==NO
d30f0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72 _LOCK ){. str
d3100 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 uct openCnt *pOp
d3110 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 en;.. /* Decr
d3120 65 6d 65 6e 74 20 74 68 65 20 73 68 61 72 65 64 ement the shared
d3130 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 lock counter.
d3140 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b Release the lock
d3150 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a using an. **
d3160 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 OS call only wh
d3170 65 6e 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69 en all threads i
d3180 6e 20 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63 n this same proc
d3190 65 73 73 20 68 61 76 65 20 72 65 6c 65 61 73 65 ess have release
d31a0 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 d. ** the loc
d31b0 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c k.. */. pL
d31c0 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 ock->cnt--;.
d31d0 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d if( pLock->cnt==
d31e0 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 0 ){. lock.
d31f0 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b l_type = F_UNLCK
d3200 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 ;. lock.l_w
d3210 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
d3220 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 ;. lock.l_s
d3230 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 tart = lock.l_le
d3240 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 53 69 n = 0L;. Si
d3250 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
d3260 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 53 69 ign(1);. Si
d3270 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 mulateIOError( h
d3280 3d 28 2d 31 29 20 29 0a 20 20 20 20 20 20 53 69 =(-1) ). Si
d3290 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
d32a0 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66 ign(0);. if
d32b0 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 ( fcntl(h, F_SET
d32c0 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 LK, &lock)!=(-1)
d32d0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 ){. pLoc
d32e0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f k->locktype = NO
d32f0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c _LOCK;. }el
d3300 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 se{. int
d3310 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d3320 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
d3330 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d3340 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d3350 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
d3360 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 CK);. if(
d3370 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 IS_LOCK_ERROR(r
d3380 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 c) ){.
d3390 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d33a0 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 = tErrno;.
d33b0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4c 6f }. pLo
d33c0 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 09 09 09 ck->cnt = 1;....
d33d0 09 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b .goto end_unlock
d33e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
d33f0 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e . /* Decremen
d3400 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c t the count of l
d3410 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68 69 ocks against thi
d3420 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 68 s same file. Wh
d3430 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f en the. ** co
d3440 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f unt reaches zero
d3450 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65 , close any othe
d3460 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f r file descripto
d3470 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a 20 rs whose close.
d3480 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72 72 ** was deferr
d3490 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f 75 ed because of ou
d34a0 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e tstanding locks.
d34b0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
d34c0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
d34d0 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 . pOpen = p
d34e0 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 File->pOpen;.
d34f0 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d pOpen->nLock-
d3500 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 -;. assert(
d3510 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 pOpen->nLock>=0
d3520 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f );. if( pO
d3530 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26 pen->nLock==0 &&
d3540 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 pOpen->nPending
d3550 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e >0 ){. in
d3560 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 t i;. for
d3570 28 69 3d 30 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e (i=0; i<pOpen->n
d3580 50 65 6e 64 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 Pending; i++){.
d3590 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 70 close(p
d35a0 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 Open->aPending[i
d35b0 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ]);. }.
d35c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
d35d0 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 ee(pOpen->aPendi
d35e0 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 ng);. pOp
d35f0 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d 20 30 en->nPending = 0
d3600 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d ;. pOpen-
d3610 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 >aPending = 0;.
d3620 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
d3630 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 ...end_unlock:.
d3640 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 leaveMutex();.
d3650 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
d3660 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b OK ) pFile->lock
d3670 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d3680 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d3690 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
d36a0 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 74 68 tion performs th
d36b0 65 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 22 e parts of the "
d36c0 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 65 72 close file" oper
d36d0 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e ation .** common
d36e0 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 to all locking
d36f0 73 63 68 65 6d 65 73 2e 20 49 74 20 63 6c 6f 73 schemes. It clos
d3700 65 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 es the directory
d3710 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 61 6e and file.** han
d3720 64 6c 65 73 2c 20 69 66 20 74 68 65 79 20 61 72 dles, if they ar
d3730 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 65 74 e valid, and set
d3740 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 s all fields of
d3750 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 the unixFile.**
d3760 73 74 72 75 63 74 75 72 65 20 74 6f 20 30 2e 0a structure to 0..
d3770 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c */.static int cl
d3780 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c 69 oseUnixFile(sqli
d3790 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
d37a0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d37b0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d37c0 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b ;. if( pFile ){
d37d0 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
d37e0 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 dirfd>=0 ){.
d37f0 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 close(pFile->d
d3800 69 72 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 irfd);. }.
d3810 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 if( pFile->h>=0
d3820 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 ){. close(
d3830 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 7d pFile->h);. }
d3840 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 43 . OSTRACE2("C
d3850 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 LOSE %-3d\n",
d3860 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f pFile->h);. O
d3870 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a penCounter(-1);.
d3880 20 20 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 memset(pFile
d3890 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 , 0, sizeof(unix
d38a0 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 File));. }. re
d38b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d38c0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
d38d0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
d38e0 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 int unixClose(s
d38f0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
d3900 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 {. if( id ){.
d3910 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d3920 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 e = (unixFile *)
d3930 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 6e 6c 6f id;. unixUnlo
d3940 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b ck(id, NO_LOCK);
d3950 0a 20 20 20 20 65 6e 74 65 72 4d 75 74 65 78 28 . enterMutex(
d3960 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 );. if( pFile
d3970 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 ->pOpen && pFile
d3980 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 ->pOpen->nLock )
d3990 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 {. /* If th
d39a0 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 ere are outstand
d39b0 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f ing locks, do no
d39c0 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 t actually close
d39d0 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 the file just.
d39e0 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 61 ** yet beca
d39f0 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 63 use that would c
d3a00 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 lear those locks
d3a10 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 20 . Instead, add
d3a20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a the file. *
d3a30 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 * descriptor to
d3a40 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e pOpen->aPending.
d3a50 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 It will be aut
d3a60 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 omatically close
d3a70 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 d when. **
d3a80 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 the last lock is
d3a90 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20 cleared..
d3aa0 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 4e */. int *aN
d3ab0 65 77 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 ew;. struct
d3ac0 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 openCnt *pOpen
d3ad0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a = pFile->pOpen;.
d3ae0 20 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c aNew = sql
d3af0 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 70 ite3_realloc(pOp
d3b00 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2c 20 28 70 en->aPending, (p
d3b10 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 31 Open->nPending+1
d3b20 29 2a 73 69 7a 65 6f 66 28 69 6e 74 29 20 29 3b )*sizeof(int) );
d3b30 0a 20 20 20 20 20 20 69 66 28 20 61 4e 65 77 3d . if( aNew=
d3b40 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a =0 ){. /*
d3b50 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 If a malloc fai
d3b60 6c 73 2c 20 6a 75 73 74 20 6c 65 61 6b 20 74 68 ls, just leak th
d3b70 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
d3b80 72 20 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 r */. }else
d3b90 7b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d {. pOpen-
d3ba0 3e 61 50 65 6e 64 69 6e 67 20 3d 20 61 4e 65 77 >aPending = aNew
d3bb0 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d ;. pOpen-
d3bc0 3e 61 50 65 6e 64 69 6e 67 5b 70 4f 70 65 6e 2d >aPending[pOpen-
d3bd0 3e 6e 50 65 6e 64 69 6e 67 5d 20 3d 20 70 46 69 >nPending] = pFi
d3be0 6c 65 2d 3e 68 3b 0a 20 20 20 20 20 20 20 20 70 le->h;. p
d3bf0 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 2b Open->nPending++
d3c00 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d ;. pFile-
d3c10 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d >h = -1;. }
d3c20 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 . }. relea
d3c30 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 seLockInfo(pFile
d3c40 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 ->pLock);. re
d3c50 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 leaseOpenCnt(pFi
d3c60 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 le->pOpen);.
d3c70 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 closeUnixFile(id
d3c80 29 3b 0a 20 20 20 20 6c 65 61 76 65 4d 75 74 65 );. leaveMute
d3c90 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 x();. }. retur
d3ca0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
d3cb0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
d3cc0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
d3cd0 0a 23 70 72 61 67 6d 61 20 6d 61 72 6b 20 41 46 .#pragma mark AF
d3ce0 50 20 53 75 70 70 6f 72 74 0a 0a 2f 2a 0a 20 2a P Support../*. *
d3cf0 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 * The afpLocking
d3d00 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 Context structur
d3d10 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 e contains all a
d3d20 66 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 fp lock specific
d3d30 20 73 74 61 74 65 0a 20 2a 2f 0a 74 79 70 65 64 state. */.typed
d3d40 65 66 20 73 74 72 75 63 74 20 61 66 70 4c 6f 63 ef struct afpLoc
d3d50 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 4c kingContext afpL
d3d60 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 ockingContext;.s
d3d70 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 truct afpLocking
d3d80 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73 69 Context {. unsi
d3d90 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 gned long long s
d3da0 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b 0a 20 haredLockByte;.
d3db0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
d3dc0 65 50 61 74 68 3b 0a 7d 3b 0a 0a 73 74 72 75 63 ePath;.};..struc
d3dd0 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 t ByteRangeLockP
d3de0 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 B2.{. unsigned
d3df0 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 long long offset
d3e00 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 ; /* offs
d3e10 65 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 et to first byte
d3e20 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e to lock */. un
d3e30 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
d3e40 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 length;
d3e50 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 /* nbr of bytes
d3e60 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 to lock */. uns
d3e70 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
d3e80 72 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f retRangeStart; /
d3e90 2a 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 * nbr of 1st byt
d3ea0 65 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 e locked if succ
d3eb0 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 essful */. unsi
d3ec0 67 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b gned char unLock
d3ed0 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a Flag; /*
d3ee0 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 1 = unlock, 0 =
d3ef0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 lock */. unsig
d3f00 6e 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e ned char startEn
d3f10 64 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 dFlag; /*
d3f20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 1=rel to end of
d3f30 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 fork, 0=rel to s
d3f40 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 tart */. int fd
d3f50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
d3f60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 /* f
d3f70 69 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f ile desc to asso
d3f80 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 c this lock with
d3f90 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 */.};..#define
d3fa0 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f afpfsByteRangeLo
d3fb0 63 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 ck2FSCTL
d3fc0 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 _IOWR('z', 23, s
d3fd0 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c truct ByteRangeL
d3fe0 6f 63 6b 50 42 32 29 0a 0a 2f 2a 20 0a 20 2a 2a ockPB2)../* . **
d3ff0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
d4000 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 K on success, SQ
d4010 4c 49 54 45 5f 42 55 53 59 20 6f 6e 20 66 61 69 LITE_BUSY on fai
d4020 6c 75 72 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 lure.. */.static
d4030 20 69 6e 74 20 5f 41 46 50 46 53 53 65 74 4c 6f int _AFPFSSetLo
d4040 63 6b 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 ck(. const char
d4050 20 2a 70 61 74 68 2c 20 0a 20 20 75 6e 69 78 46 *path, . unixF
d4060 69 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 75 ile *pFile, . u
d4070 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
d4080 67 20 6f 66 66 73 65 74 2c 20 0a 20 20 75 6e 73 g offset, . uns
d4090 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
d40a0 6c 65 6e 67 74 68 2c 20 0a 20 20 69 6e 74 20 73 length, . int s
d40b0 65 74 4c 6f 63 6b 46 6c 61 67 0a 29 7b 0a 20 20 etLockFlag.){.
d40c0 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 struct ByteRange
d40d0 4c 6f 63 6b 50 42 32 20 20 20 20 20 20 20 70 62 LockPB2 pb
d40e0 3b 0a 20 20 69 6e 74 20 20 20 20 20 20 20 20 20 ;. int
d40f0 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 3b err;
d4100 0a 20 20 0a 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 . . pb.unLockF
d4110 6c 61 67 20 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 lag = setLockFla
d4120 67 20 3f 20 30 20 3a 20 31 3b 0a 20 20 70 62 2e g ? 0 : 1;. pb.
d4130 73 74 61 72 74 45 6e 64 46 6c 61 67 20 3d 20 30 startEndFlag = 0
d4140 3b 0a 20 20 70 62 2e 6f 66 66 73 65 74 20 3d 20 ;. pb.offset =
d4150 6f 66 66 73 65 74 3b 0a 20 20 70 62 2e 6c 65 6e offset;. pb.len
d4160 67 74 68 20 3d 20 6c 65 6e 67 74 68 3b 20 0a 20 gth = length; .
d4170 20 70 62 2e 66 64 20 3d 20 70 46 69 6c 65 2d 3e pb.fd = pFile->
d4180 68 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 41 h;. OSTRACE5("A
d4190 46 50 4c 4f 43 4b 20 73 65 74 74 69 6e 67 20 6c FPLOCK setting l
d41a0 6f 63 6b 20 25 73 20 66 6f 72 20 25 64 20 69 6e ock %s for %d in
d41b0 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 range %llx:%llx
d41c0 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c 6f \n", . (setLo
d41d0 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 46 ckFlag?"ON":"OFF
d41e0 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 "), pFile->h, of
d41f0 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 fset, length);.
d4200 20 65 72 72 20 3d 20 66 73 63 74 6c 28 70 61 74 err = fsctl(pat
d4210 68 2c 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 h, afpfsByteRang
d4220 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 26 70 62 eLock2FSCTL, &pb
d4230 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 65 72 72 , 0);. if ( err
d4240 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 ==-1 ) {. int
d4250 20 72 63 3b 0a 20 20 20 20 69 6e 74 20 74 45 72 rc;. int tEr
d4260 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d4270 20 4f 53 54 52 41 43 45 34 28 22 41 46 50 4c 4f OSTRACE4("AFPLO
d4280 43 4b 20 66 61 69 6c 65 64 20 74 6f 20 66 73 63 CK failed to fsc
d4290 74 6c 28 29 20 27 25 73 27 20 25 64 20 25 73 5c tl() '%s' %d %s\
d42a0 6e 22 2c 20 70 61 74 68 2c 20 74 45 72 72 6e 6f n", path, tErrno
d42b0 2c 20 73 74 72 65 72 72 6f 72 28 74 45 72 72 6e , strerror(tErrn
d42c0 6f 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 o));. rc = sq
d42d0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d42e0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d42f0 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51 setLockFlag ? SQ
d4300 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 LITE_IOERR_LOCK
d4310 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 : SQLITE_IOERR_U
d4320 4e 4c 4f 43 4b 29 3b 20 2f 2a 20 65 72 72 6f 72 NLOCK); /* error
d4330 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 5f 4c */. if( IS_L
d4340 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
d4350 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
d4360 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d4370 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
d4380 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 rn rc;. } else
d4390 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
d43a0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f ITE_OK;. }.}../
d43b0 2a 20 41 46 50 2d 73 74 79 6c 65 20 72 65 73 65 * AFP-style rese
d43c0 72 76 65 64 20 6c 6f 63 6b 20 63 68 65 63 6b 69 rved lock checki
d43d0 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ng following the
d43e0 20 62 65 68 61 76 69 6f 72 20 6f 66 20 0a 2a 2a behavior of .**
d43f0 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 unixCheckReserv
d4400 65 64 4c 6f 63 6b 2c 20 73 65 65 20 74 68 65 20 edLock, see the
d4410 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 unixCheckReserve
d4420 64 4c 6f 63 6b 20 66 75 6e 63 74 69 6f 6e 20 63 dLock function c
d4430 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 73 74 61 74 69 omments */.stati
d4440 63 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65 c int afpCheckRe
d4450 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
d4460 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d4470 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e *pResOut){. in
d4480 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
d4490 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 ;. int reserved
d44a0 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 = 0;. unixFile
d44b0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
d44c0 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 ile*)id;. . Si
d44d0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
d44e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d44f0 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
d4500 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 LOCK; );. . as
d4510 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
d4520 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 afpLockingConte
d4530 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 xt *context = (a
d4540 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
d4550 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 *) pFile->locki
d4560 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 ngContext;. .
d4570 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 /* Check if a th
d4580 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f read in this pro
d4590 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 cess holds such
d45a0 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 a lock */. if(
d45b0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
d45c0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
d45d0 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b reserved = 1;
d45e0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 . }. . /* Oth
d45f0 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f erwise see if so
d4600 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 me other process
d4610 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f holds it.. */
d4620 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 . if( !reserved
d4630 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 ){. /* lock
d4640 74 68 65 20 52 45 53 45 52 56 45 44 20 62 79 74 the RESERVED byt
d4650 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 e */. int lrc
d4660 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b = _AFPFSSetLock
d4670 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 (context->filePa
d4680 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 th, pFile, RESER
d4690 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 VED_BYTE, 1,1);
d46a0 20 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 . if( SQLITE
d46b0 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20 _OK==lrc ){.
d46c0 20 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 /* if we succe
d46d0 65 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 eded in taking t
d46e0 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b he reserved lock
d46f0 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 , unlock it to r
d4700 65 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 estore. **
d4710 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 the original sta
d4720 74 65 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 te */. lrc
d4730 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 = _AFPFSSetLock(
d4740 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 context->filePat
d4750 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 h, pFile, RESERV
d4760 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a ED_BYTE, 1, 0);.
d4770 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
d4780 20 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c /* if we fail
d4790 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f ed to get the lo
d47a0 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 ck then someone
d47b0 65 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 else must have i
d47c0 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 t */. reser
d47d0 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 ved = 1;. }.
d47e0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d47f0 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 RROR(lrc) ){.
d4800 20 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d rc=lrc;. }
d4810 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 . }. . OSTRAC
d4820 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b E4("TEST WR-LOCK
d4830 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 %d %d %d\n", pF
d4840 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 ile->h, rc, rese
d4850 72 76 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65 rved);. . *pRe
d4860 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b sOut = reserved;
d4870 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d4880 0a 2f 2a 20 41 46 50 2d 73 74 79 6c 65 20 6c 6f ./* AFP-style lo
d4890 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20 cking following
d48a0 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 the behavior of
d48b0 75 6e 69 78 4c 6f 63 6b 2c 20 73 65 65 20 74 68 unixLock, see th
d48c0 65 20 75 6e 69 78 4c 6f 63 6b 20 0a 2a 2a 20 66 e unixLock .** f
d48d0 75 6e 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 unction comments
d48e0 20 66 6f 72 20 64 65 74 61 69 6c 73 20 6f 66 20 for details of
d48f0 6c 6f 63 6b 20 6d 61 6e 61 67 65 6d 65 6e 74 2e lock management.
d4900 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 */.static int a
d4910 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 fpLock(sqlite3_f
d4920 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
d4930 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 ktype){. int rc
d4940 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
d4950 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d4960 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d4970 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e . afpLockingCon
d4980 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 text *context =
d4990 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 (afpLockingConte
d49a0 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 xt *) pFile->loc
d49b0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a kingContext;. .
d49c0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d49d0 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c );. OSTRACE5("L
d49e0 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 OCK %d %s was
d49f0 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %s pid=%d\n", p
d4a00 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
d4a10 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c locktypeName(l
d4a20 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 ocktype), lockty
d4a30 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f peName(pFile->lo
d4a40 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64 28 cktype), getpid(
d4a50 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 ));.. /* If the
d4a60 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
d4a70 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 lock of this typ
d4a80 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 e or more restri
d4a90 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a ctive on the. *
d4aa0 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e * unixFile, do n
d4ab0 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 othing. Don't us
d4ac0 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f e the afp_end_lo
d4ad0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 ck: exit path, a
d4ae0 73 0a 20 20 2a 2a 20 65 6e 74 65 72 4d 75 74 65 s. ** enterMute
d4af0 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 x() hasn't been
d4b00 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f called yet.. */
d4b10 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d4b20 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 cktype>=locktype
d4b30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 ){. OSTRACE3
d4b40 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
d4b50 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 ok (already held
d4b60 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a )\n", pFile->h,.
d4b70 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 lockt
d4b80 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 ypeName(locktype
d4b90 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ));. return S
d4ba0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
d4bb0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 /* Make sure th
d4bc0 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e e locking sequen
d4bd0 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 ce is correct.
d4be0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 */. assert( pFi
d4bf0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f le->locktype!=NO
d4c00 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 _LOCK || locktyp
d4c10 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
d4c20 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
d4c30 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f type!=PENDING_LO
d4c40 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
d4c50 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 locktype!=RESERV
d4c60 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 ED_LOCK || pFile
d4c70 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ->locktype==SHAR
d4c80 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 ED_LOCK );. .
d4c90 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20 69 73 /* This mutex is
d4ca0 20 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20 needed because
d4cb0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 pFile->pLock is
d4cc0 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 shared across th
d4cd0 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 65 6e 74 reads. */. ent
d4ce0 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a erMutex();.. /*
d4cf0 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 Make sure the c
d4d00 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77 urrent thread ow
d4d10 6e 73 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 ns the pFile..
d4d20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66 */. rc = transf
d4d30 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c erOwnership(pFil
d4d40 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 e);. if( rc!=SQ
d4d50 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 6c LITE_OK ){. l
d4d60 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 eaveMutex();.
d4d70 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
d4d80 20 20 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 . /* A PEND
d4d90 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 ING lock is need
d4da0 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 ed before acquir
d4db0 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ing a SHARED loc
d4dc0 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a k and before. *
d4dd0 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 * acquiring an E
d4de0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 XCLUSIVE lock.
d4df0 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c For the SHARED l
d4e00 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 ock, the PENDING
d4e10 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 will. ** be re
d4e20 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 leased.. */. i
d4e30 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
d4e40 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 RED_LOCK .
d4e50 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 || (locktype==EX
d4e60 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 CLUSIVE_LOCK &&
d4e70 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c pFile->locktype<
d4e80 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 PENDING_LOCK).
d4e90 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 ){. int faile
d4ea0 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20 d;. failed =
d4eb0 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f _AFPFSSetLock(co
d4ec0 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c ntext->filePath,
d4ed0 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f pFile, PENDING_
d4ee0 42 59 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 20 BYTE, 1, 1);.
d4ef0 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 if (failed) {.
d4f00 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 rc = failed
d4f10 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 ;. goto afp
d4f20 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d _end_lock;. }
d4f30 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20 . }. . /* If
d4f40 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 control gets to
d4f50 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e this point, then
d4f60 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 actually go ahe
d4f70 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a ad and make. **
d4f80 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
d4f90 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 m calls for the
d4fa0 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a specified lock..
d4fb0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
d4fc0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d4fd0 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c 20 ){. int lk,
d4fe0 6c 72 63 31 2c 20 6c 72 63 32 2c 20 6c 72 63 31 lrc1, lrc2, lrc1
d4ff0 45 72 72 6e 6f 3b 0a 20 20 20 20 0a 20 20 20 20 Errno;. .
d5000 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72 /* Now get the r
d5010 65 61 64 2d 6c 6f 63 6b 20 53 48 41 52 45 44 5f ead-lock SHARED_
d5020 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 2f 2a 20 6e LOCK */. /* n
d5030 6f 74 65 20 74 68 61 74 20 74 68 65 20 71 75 61 ote that the qua
d5040 6c 69 74 79 20 6f 66 20 74 68 65 20 72 61 6e 64 lity of the rand
d5050 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27 74 20 6d omness doesn't m
d5060 61 74 74 65 72 20 74 68 61 74 20 6d 75 63 68 20 atter that much
d5070 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e 64 */. lk = rand
d5080 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74 65 om(); . conte
d5090 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 xt->sharedLockBy
d50a0 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 te = (lk & 0x7ff
d50b0 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 fffff)%(SHARED_S
d50c0 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 IZE - 1);. lr
d50d0 63 31 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f c1 = _AFPFSSetLo
d50e0 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 ck(context->file
d50f0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 Path, pFile, .
d5100 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 SHARED_F
d5110 49 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 IRST+context->sh
d5120 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c aredLockByte, 1,
d5130 20 31 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 1);. if( IS_
d5140 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 LOCK_ERROR(lrc1)
d5150 20 29 7b 0a 20 20 20 20 20 20 6c 72 63 31 45 72 ){. lrc1Er
d5160 72 6e 6f 20 3d 20 70 46 69 6c 65 2d 3e 6c 61 73 rno = pFile->las
d5170 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 tErrno;. }.
d5180 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 65 /* Drop the te
d5190 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47 20 mporary PENDING
d51a0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 72 63 32 lock */. lrc2
d51b0 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b = _AFPFSSetLock
d51c0 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 (context->filePa
d51d0 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 th, pFile, PENDI
d51e0 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a NG_BYTE, 1, 0);.
d51f0 20 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 5f . if( IS_
d5200 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 LOCK_ERROR(lrc1)
d5210 20 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 ) {. pFile
d5220 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72 ->lastErrno = lr
d5230 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 c1Errno;. r
d5240 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 20 c = lrc1;.
d5250 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 goto afp_end_loc
d5260 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 k;. } else if
d5270 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d5280 6c 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 72 lrc2) ){. r
d5290 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 20 c = lrc2;.
d52a0 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 goto afp_end_loc
d52b0 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 k;. } else if
d52c0 28 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 45 ( lrc1 != SQLITE
d52d0 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 63 _OK ) {. rc
d52e0 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 65 = lrc1;. } e
d52f0 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 6c lse {. pFil
d5300 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 e->locktype = SH
d5310 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d ARED_LOCK;. }
d5320 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
d5330 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73 The request was
d5340 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 for a RESERVED
d5350 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 or EXCLUSIVE loc
d5360 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a k. It is. **
d5370 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
d5380 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20 ere is a SHARED
d5390 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 or greater lock
d53a0 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 on the file.
d53b0 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 ** already..
d53c0 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 */. int faile
d53d0 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 d = 0;. asser
d53e0 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 t( 0!=pFile->loc
d53f0 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20 ktype );. if
d5400 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 (locktype >= RES
d5410 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 ERVED_LOCK && pF
d5420 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 ile->locktype <
d5430 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b RESERVED_LOCK) {
d5440 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75 . /* Acqu
d5450 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c ire a RESERVED l
d5460 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 ock */. f
d5470 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65 ailed = _AFPFSSe
d5480 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 tLock(context->f
d5490 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 ilePath, pFile,
d54a0 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 RESERVED_BYTE, 1
d54b0 2c 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ,1);. }. i
d54c0 66 20 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f f (!failed && lo
d54d0 63 6b 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53 cktype == EXCLUS
d54e0 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 IVE_LOCK) {.
d54f0 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 /* Acquire an
d5500 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a EXCLUSIVE lock *
d5510 2f 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 /. .
d5520 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 /* Remove the s
d5530 68 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 hared lock befor
d5540 65 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e e trying the ran
d5550 67 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20 ge. we'll need
d5560 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65 to . ** ree
d5570 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 stablish the sha
d5580 72 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63 red lock if we c
d5590 61 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66 an't get the af
d55a0 70 55 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f pUnlock. */
d55b0 0a 20 20 20 20 20 20 69 66 20 28 21 28 66 61 69 . if (!(fai
d55c0 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c led = _AFPFSSetL
d55d0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c ock(context->fil
d55e0 65 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 ePath, pFile, SH
d55f0 41 52 45 44 5f 46 49 52 53 54 20 2b 0a 20 20 20 ARED_FIRST +.
d5600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d5610 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 context->s
d5620 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 haredLockByte, 1
d5630 2c 20 30 29 29 29 20 7b 0a 20 20 20 20 20 20 20 , 0))) {.
d5640 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70 74 /* now attemmpt
d5650 20 74 6f 20 67 65 74 20 74 68 65 20 65 78 63 6c to get the excl
d5660 75 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67 65 usive lock range
d5670 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c */. fail
d5680 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f ed = _AFPFSSetLo
d5690 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 ck(context->file
d56a0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 Path, pFile, SHA
d56b0 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 RED_FIRST, .
d56c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d56d0 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 SHARE
d56e0 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 D_SIZE, 1);.
d56f0 20 20 20 20 69 66 20 28 66 61 69 6c 65 64 20 26 if (failed &
d5700 26 20 28 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 & (failed = _AFP
d5710 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 FSSetLock(contex
d5720 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 t->filePath, pFi
d5730 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 le, .
d5740 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52 SHAR
d5750 45 44 5f 46 49 52 53 54 20 2b 20 63 6f 6e 74 65 ED_FIRST + conte
d5760 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 xt->sharedLockBy
d5770 74 65 2c 20 31 2c 20 31 29 29 29 20 7b 0a 20 20 te, 1, 1))) {.
d5780 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 rc = fai
d5790 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 led;. }.
d57a0 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 } else {.
d57b0 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 rc = faile
d57c0 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 d; . }.
d57d0 7d 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 }. if( failed
d57e0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 ){. rc = f
d57f0 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d ailed;. }. }
d5800 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 . . if( rc==SQ
d5810 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
d5820 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d5830 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c locktype;. }el
d5840 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d se if( locktype=
d5850 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
d5860 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f ){. pFile->lo
d5870 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 cktype = PENDING
d5880 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 _LOCK;. }. .af
d5890 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 6c 65 p_end_lock:. le
d58a0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 aveMutex();. OS
d58b0 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 TRACE4("LOCK
d58c0 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 %d %s %s\n", pFi
d58d0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e le->h, locktypeN
d58e0 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a ame(locktype), .
d58f0 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c rc==SQL
d5900 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 ITE_OK ? "ok" :
d5910 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 "failed");. ret
d5920 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
d5930 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 Lower the locki
d5940 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 ng level on file
d5950 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c descriptor pFil
d5960 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 e to locktype.
d5970 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 locktype.** must
d5980 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
d5990 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 CK or SHARED_LOC
d59a0 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 K..**.** If the
d59b0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 locking level of
d59c0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
d59d0 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 ptor is already
d59e0 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 at or below.** t
d59f0 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
d5a00 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
d5a10 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
d5a20 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -op..*/.static i
d5a30 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c nt afpUnlock(sql
d5a40 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d5a50 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
d5a60 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d5a70 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 _OK;. unixFile
d5a80 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d5a90 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 le*)id;. afpLoc
d5aa0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e kingContext *con
d5ab0 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 text = (afpLocki
d5ac0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 ngContext *) pFi
d5ad0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
d5ae0 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 xt;.. assert( p
d5af0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
d5b00 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 E5("UNLOCK %d %
d5b10 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c d was %d pid=%d\
d5b20 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
d5b30 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 cktype,.
d5b40 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d5b50 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 , getpid());..
d5b60 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
d5b70 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
d5b80 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d5b90 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 cktype<=locktype
d5ba0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
d5bb0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
d5bc0 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 if( CHECK_THREAD
d5bd0 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 ID(pFile) ){.
d5be0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d return SQLITE_M
d5bf0 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 65 6e 74 ISUSE;. }. ent
d5c00 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 6e 74 erMutex();. int
d5c10 20 66 61 69 6c 65 64 20 3d 20 53 51 4c 49 54 45 failed = SQLITE
d5c20 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 _OK;. if( pFile
d5c30 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 ->locktype>SHARE
d5c40 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 66 D_LOCK ){. if
d5c50 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
d5c60 45 44 5f 4c 4f 43 4b 20 29 7b 0a 0a 20 20 20 20 ED_LOCK ){..
d5c70 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20 74 68 65 20 /* unlock the
d5c80 65 78 63 6c 75 73 69 76 65 20 72 61 6e 67 65 20 exclusive range
d5c90 2d 20 74 68 65 6e 20 72 65 2d 65 73 74 61 62 6c - then re-establ
d5ca0 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c ish the shared l
d5cb0 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 69 66 20 ock */. if
d5cc0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 (pFile->locktype
d5cd0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
d5ce0 29 20 7b 0a 20 20 20 20 20 20 20 20 66 61 69 6c ) {. fail
d5cf0 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f ed = _AFPFSSetLo
d5d00 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 ck(context->file
d5d10 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 Path, pFile, SHA
d5d20 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 RED_FIRST, .
d5d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d5d40 20 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41 SHA
d5d50 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 RED_SIZE, 0);.
d5d60 20 20 20 20 20 20 69 66 20 28 21 66 61 69 6c 65 if (!faile
d5d70 64 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 2f d) {. /
d5d80 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 * successfully r
d5d90 65 6d 6f 76 65 64 20 74 68 65 20 65 78 63 6c 75 emoved the exclu
d5da0 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 sive lock */.
d5db0 20 20 20 20 20 20 20 69 66 20 28 28 66 61 69 6c if ((fail
d5dc0 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f ed = _AFPFSSetLo
d5dd0 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 ck(context->file
d5de0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 Path, pFile, SHA
d5df0 52 45 44 5f 46 49 52 53 54 2b 0a 20 20 20 20 20 RED_FIRST+.
d5e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d5e10 20 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e context->
d5e20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 sharedLockByte,
d5e30 31 2c 20 31 29 29 29 20 7b 0a 20 20 20 20 20 20 1, 1))) {.
d5e40 20 20 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 /* failed
d5e50 74 6f 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 to re-establish
d5e60 6f 75 72 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 our shared lock
d5e70 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 */. r
d5e80 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 c = failed;.
d5e90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
d5ea0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 } else {.
d5eb0 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a rc = failed;.
d5ec0 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 } .
d5ed0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 }. }. if
d5ee0 28 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b (rc == SQLITE_OK
d5ef0 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 && pFile->lockt
d5f00 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype>=PENDING_LOC
d5f10 4b 29 20 7b 0a 20 20 20 20 20 20 69 66 20 28 28 K) {. if ((
d5f20 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 failed = _AFPFSS
d5f30 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
d5f40 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2c filePath, pFile,
d5f50 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
d5f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d5f70 20 20 20 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 PENDING_BYTE
d5f80 2c 20 31 2c 20 30 29 29 29 7b 0a 20 20 20 20 20 , 1, 0))){.
d5f90 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 /* failed to
d5fa0 72 65 6c 65 61 73 65 20 74 68 65 20 70 65 6e 64 release the pend
d5fb0 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ing lock */.
d5fc0 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b rc = failed;
d5fd0 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 . }. }
d5fe0 0a 20 20 20 20 69 66 20 28 72 63 20 3d 3d 20 53 . if (rc == S
d5ff0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c QLITE_OK && pFil
d6000 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 e->locktype>=RES
d6010 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 ERVED_LOCK) {.
d6020 20 20 20 20 69 66 20 28 28 66 61 69 6c 65 64 20 if ((failed
d6030 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 = _AFPFSSetLock(
d6040 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 context->filePat
d6050 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 h, pFile, .
d6060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d6070 20 20 20 20 20 20 20 20 20 20 20 20 20 52 45 53 RES
d6080 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 ERVED_BYTE, 1, 0
d6090 29 29 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a ))) {. /*
d60a0 20 66 61 69 6c 65 64 20 74 6f 20 72 65 6c 65 61 failed to relea
d60b0 73 65 20 74 68 65 20 72 65 73 65 72 76 65 64 20 se the reserved
d60c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 lock */.
d60d0 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 20 0a 20 rc = failed; .
d60e0 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 0a 20 20 }. } .
d60f0 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 }. if( locktype
d6100 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ==NO_LOCK ){.
d6110 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 5f 41 int failed = _A
d6120 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 FPFSSetLock(cont
d6130 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 ext->filePath, p
d6140 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 File, .
d6150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d6160 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 SHARED_FIR
d6170 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 ST + context->sh
d6180 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c aredLockByte, 1,
d6190 20 30 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69 0);. if (fai
d61a0 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20 led) {. rc
d61b0 3d 20 66 61 69 6c 65 64 3b 20 20 0a 20 20 20 20 = failed; .
d61c0 7d 0a 20 20 7d 0a 20 20 69 66 20 28 72 63 20 3d }. }. if (rc =
d61d0 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 = SQLITE_OK).
d61e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d61f0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 6c = locktype;. l
d6200 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
d6210 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d6220 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 ** Close a file
d6230 26 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 & cleanup AFP sp
d6240 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 ecific locking c
d6250 6f 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 ontext .*/.stati
d6260 63 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 c int afpClose(s
d6270 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
d6280 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 {. if( id ){.
d6290 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 unixFile *pFi
d62a0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d62b0 69 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 id;. afpUnloc
d62c0 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a k(id, NO_LOCK);.
d62d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
d62e0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 (pFile->lockingC
d62f0 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 ontext);. }. r
d6300 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 eturn closeUnixF
d6310 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 0a 23 70 72 ile(id);.}...#pr
d6320 61 67 6d 61 20 6d 61 72 6b 20 66 6c 6f 63 6b 28 agma mark flock(
d6330 29 20 73 74 79 6c 65 20 6c 6f 63 6b 69 6e 67 0a ) style locking.
d6340 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6c 6f 63 6b ./*.** The flock
d6350 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 69 LockingContext i
d6360 73 20 6e 6f 74 20 75 73 65 64 0a 2a 2f 0a 74 79 s not used.*/.ty
d6370 70 65 64 65 66 20 76 6f 69 64 20 66 6c 6f 63 6b pedef void flock
d6380 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a LockingContext;.
d6390 0a 2f 2a 20 66 6c 6f 63 6b 2d 73 74 79 6c 65 20 ./* flock-style
d63a0 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 63 68 reserved lock ch
d63b0 65 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 ecking following
d63c0 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 the behavior of
d63d0 20 0a 20 2a 2a 20 75 6e 69 78 43 68 65 63 6b 52 . ** unixCheckR
d63e0 65 73 65 72 76 65 64 4c 6f 63 6b 2c 20 73 65 65 eservedLock, see
d63f0 20 74 68 65 20 75 6e 69 78 43 68 65 63 6b 52 65 the unixCheckRe
d6400 73 65 72 76 65 64 4c 6f 63 6b 20 66 75 6e 63 74 servedLock funct
d6410 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a ion comments */.
d6420 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b static int flock
d6430 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d6440 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d6450 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
d6460 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
d6470 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 LITE_OK;. int r
d6480 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 eserved = 0;. u
d6490 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d64a0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d64b0 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 . SimulateIOE
d64c0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
d64d0 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
d64e0 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a ESERVEDLOCK; );.
d64f0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
d6500 6c 65 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 le );. . /* Ch
d6510 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 eck if a thread
d6520 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
d6530 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 holds such a loc
d6540 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 k */. if( pFile
d6550 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 ->locktype>SHARE
d6560 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 D_LOCK ){. re
d6570 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a served = 1;. }.
d6580 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 . /* Otherwis
d6590 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 e see if some ot
d65a0 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 her process hold
d65b0 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 s it. */. if( !
d65c0 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 reserved ){.
d65d0 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65 /* attempt to ge
d65e0 74 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 t the lock */.
d65f0 20 20 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63 int lrc = floc
d6600 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b k(pFile->h, LOCK
d6610 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a _EX | LOCK_NB);.
d6620 20 20 20 20 69 66 28 20 21 6c 72 63 20 29 7b 0a if( !lrc ){.
d6630 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 65 /* got the
d6640 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 lock, unlock it
d6650 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 */. lrc =
d6660 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 flock(pFile->h,
d6670 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 LOCK_UN);.
d6680 69 66 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 20 if ( lrc ) {.
d6690 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 int tErrno
d66a0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
d66b0 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 /* unlock faile
d66c0 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 d with an error
d66d0 2a 2f 0a 20 20 20 20 20 20 20 20 6c 72 63 20 3d */. lrc =
d66e0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d66f0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d6700 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d6710 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 UNLOCK); .
d6720 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d6730 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 ROR(lrc) ){.
d6740 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d6750 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d6760 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
d6770 6c 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 lrc;. }.
d6780 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 }. } els
d6790 65 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 e {. int tE
d67a0 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d67b0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 reserved = 1
d67c0 3b 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f ;. /* someo
d67d0 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61 ne else might ha
d67e0 76 65 20 69 74 20 72 65 73 65 72 76 65 64 20 2a ve it reserved *
d67f0 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 /. lrc = sq
d6800 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d6810 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d6820 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
d6830 4b 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 49 K); . if( I
d6840 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 S_LOCK_ERROR(lrc
d6850 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 ) ){. pFi
d6860 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d6870 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 tErrno;.
d6880 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 rc = lrc;.
d6890 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 }. }. }. OS
d68a0 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d TRACE4("TEST WR-
d68b0 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 LOCK %d %d %d\n"
d68c0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 , pFile->h, rc,
d68d0 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 reserved);.. *p
d68e0 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 ResOut = reserve
d68f0 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a d;. return rc;.
d6900 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c }..static int fl
d6910 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f ockLock(sqlite3_
d6920 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
d6930 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20 cktype) {. int
d6940 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d6950 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d6960 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d6970 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 d;.. assert( pF
d6980 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 ile );.. /* if
d6990 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 we already have
d69a0 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 a lock, it is ex
d69b0 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 clusive. . **
d69c0 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 Just adjust leve
d69d0 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 l and punt on ou
d69e0 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 tta here. */. i
d69f0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f (pFile->lockty
d6a00 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a pe > NO_LOCK) {.
d6a10 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d6a20 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d6a30 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d6a40 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
d6a50 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 * grab an exclus
d6a60 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20 ive lock */. .
d6a70 20 69 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c 65 if (flock(pFile
d6a80 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c ->h, LOCK_EX | L
d6a90 4f 43 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 69 OCK_NB)) {. i
d6aa0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d6ab0 6f 3b 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 74 o;. /* didn't
d6ac0 20 67 65 74 2c 20 6d 75 73 74 20 62 65 20 62 75 get, must be bu
d6ad0 73 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 sy */. rc = s
d6ae0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d6af0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d6b00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d6b10 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f CK);. if( IS_
d6b20 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 LOCK_ERROR(rc) )
d6b30 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
d6b40 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d6b50 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 o;. }. } els
d6b60 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20 69 e {. /* got i
d6b70 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 t, set the type
d6b80 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f and return ok */
d6b90 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d6ba0 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d6bb0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 . }. OSTRACE4(
d6bc0 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 "LOCK %d %s %
d6bd0 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 s\n", pFile->h,
d6be0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 locktypeName(loc
d6bf0 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 20 ktype), .
d6c00 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rc==SQLITE_O
d6c10 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c K ? "ok" : "fail
d6c20 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ed");. return r
d6c30 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 c;.}..static int
d6c40 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c flockUnlock(sql
d6c50 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d6c60 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
d6c70 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d6c80 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d6c90 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
d6ca0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
d6cb0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 E5("UNLOCK %d %
d6cc0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c d was %d pid=%d\
d6cd0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
d6ce0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 cktype,.
d6cf0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d6d00 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 pe, getpid());.
d6d10 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d6d20 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
d6d30 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 ;. . /* no-op
d6d40 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 if possible */.
d6d50 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d6d60 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 type==locktype )
d6d70 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
d6d80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 ITE_OK;. }. .
d6d90 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a /* shared can j
d6da0 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 ust be set becau
d6db0 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 se we always hav
d6dc0 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a e an exclusive *
d6dd0 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 /. if (locktype
d6de0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b ==SHARED_LOCK) {
d6df0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d6e00 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d6e10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d6e20 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d6e30 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 /* no, really, u
d6e40 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 nlock. */. int
d6e50 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 rc = flock(pFile
d6e60 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 ->h, LOCK_UN);.
d6e70 20 69 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69 if (rc) {. i
d6e80 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65 nt r, tErrno = e
d6e90 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71 rrno;. r = sq
d6ea0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d6eb0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d6ec0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
d6ed0 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 OCK);. if( IS
d6ee0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29 _LOCK_ERROR(r) )
d6ef0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
d6f00 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d6f10 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 o;. }. ret
d6f20 75 72 6e 20 72 3b 0a 20 20 7d 20 65 6c 73 65 20 urn r;. } else
d6f30 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
d6f40 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b ktype = NO_LOCK;
d6f50 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d6f60 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a TE_OK;. }.}../*
d6f70 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 .** Close a file
d6f80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d6f90 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 flockClose(sqlit
d6fa0 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 e3_file *id) {.
d6fb0 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 66 if( id ){. f
d6fc0 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e lockUnlock(id, N
d6fd0 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72 O_LOCK);. }. r
d6fe0 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 eturn closeUnixF
d6ff0 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23 70 72 61 ile(id);.}..#pra
d7000 67 6d 61 20 6d 61 72 6b 20 4f 6c 64 2d 53 63 68 gma mark Old-Sch
d7010 6f 6f 6c 20 2e 6c 6f 63 6b 20 66 69 6c 65 20 62 ool .lock file b
d7020 61 73 65 64 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a ased locking../*
d7030 20 44 6f 74 6c 6f 63 6b 2d 73 74 79 6c 65 20 72 Dotlock-style r
d7040 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 63 68 65 eserved lock che
d7050 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20 cking following
d7060 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 the behavior of
d7070 0a 2a 2a 20 75 6e 69 78 43 68 65 63 6b 52 65 73 .** unixCheckRes
d7080 65 72 76 65 64 4c 6f 63 6b 2c 20 73 65 65 20 74 ervedLock, see t
d7090 68 65 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 he unixCheckRese
d70a0 72 76 65 64 4c 6f 63 6b 20 66 75 6e 63 74 69 6f rvedLock functio
d70b0 6e 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 73 74 n comments */.st
d70c0 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b atic int dotlock
d70d0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d70e0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d70f0 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
d7100 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ) {. int rc = S
d7110 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
d7120 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 reserved = 0;.
d7130 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d7140 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d7150 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 .. SimulateIOEr
d7160 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
d7170 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 TE_IOERR_CHECKRE
d7180 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 SERVEDLOCK; );.
d7190 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c . assert( pFil
d71a0 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b e );.. /* Check
d71b0 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 if a thread in
d71c0 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c this process hol
d71d0 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a ds such a lock *
d71e0 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d71f0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
d7200 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 OCK ){. reser
d7210 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a ved = 1;. }. .
d7220 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 /* Otherwise s
d7230 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ee if some other
d7240 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 process holds i
d7250 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 t. */. if( !res
d7260 65 72 76 65 64 20 29 7b 0a 20 20 20 20 63 68 61 erved ){. cha
d7270 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 r *zLockFile = (
d7280 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f char *)pFile->lo
d7290 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
d72a0 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 74 struct stat st
d72b0 61 74 42 75 66 3b 0a 20 20 20 20 0a 20 20 20 20 atBuf;. .
d72c0 69 66 28 20 6c 73 74 61 74 28 7a 4c 6f 63 6b 46 if( lstat(zLockF
d72d0 69 6c 65 2c 20 26 73 74 61 74 42 75 66 29 3d 3d ile, &statBuf)==
d72e0 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 66 69 0 ){. /* fi
d72f0 6c 65 20 65 78 69 73 74 73 2c 20 73 6f 6d 65 6f le exists, someo
d7300 6e 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20 ne else has the
d7310 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 65 lock */. re
d7320 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 served = 1;.
d7330 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
d7340 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 file does not ex
d7350 69 73 74 2c 20 77 65 20 63 6f 75 6c 64 20 68 61 ist, we could ha
d7360 76 65 20 69 74 20 69 66 20 77 65 20 77 61 6e 74 ve it if we want
d7370 20 69 74 20 2a 2f 0a 09 09 09 69 6e 74 20 74 45 it */....int tE
d7380 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d7390 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 20 21 if( ENOENT !
d73a0 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 = tErrno ){.
d73b0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d73c0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d73d0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d73e0 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
d73f0 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 ERVEDLOCK);.
d7400 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d7410 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
d7420 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
d7430 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 . OSTRACE4("TES
d7440 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
d7450 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
d7460 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a rc, reserved);.
d7470 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 . *pResOut = re
d7480 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e served;. return
d7490 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 rc;.}..static i
d74a0 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28 73 nt dotlockLock(s
d74b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d74c0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
d74d0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d74e0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d74f0 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 id;. int fd;.
d7500 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 char *zLockFile
d7510 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d = (char *)pFile-
d7520 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
d7530 0a 20 20 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 . int rc=SQLITE
d7540 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 _OK;.. /* if we
d7550 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 already have a
d7560 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 63 6c lock, it is excl
d7570 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 usive. . ** Ju
d7580 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 st adjust level
d7590 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 and punt on outt
d75a0 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 a here. */. if
d75b0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 (pFile->locktype
d75c0 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 > NO_LOCK) {.
d75d0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d75e0 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
d75f0 20 20 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 . /* Always
d7600 20 75 70 64 61 74 65 20 74 68 65 20 74 69 6d 65 update the time
d7610 73 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c 64 stamp on the old
d7620 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74 69 file */. uti
d7630 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e mes(zLockFile, N
d7640 55 4c 4c 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 ULL);. rc = S
d7650 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f QLITE_OK;. go
d7660 74 6f 20 64 6f 74 6c 6f 63 6b 5f 65 6e 64 5f 6c to dotlock_end_l
d7670 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a ock;. }. . /*
d7680 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 check to see if
d7690 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 lock file alrea
d76a0 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 73 dy exists */. s
d76b0 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74 42 truct stat statB
d76c0 75 66 3b 0a 20 20 69 66 20 28 6c 73 74 61 74 28 uf;. if (lstat(
d76d0 7a 4c 6f 63 6b 46 69 6c 65 2c 26 73 74 61 74 42 zLockFile,&statB
d76e0 75 66 29 20 3d 3d 20 30 29 7b 0a 20 20 20 20 72 uf) == 0){. r
d76f0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
d7700 20 2f 2a 20 69 74 20 64 6f 65 73 2c 20 62 75 73 /* it does, bus
d7710 79 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 64 6f y */. goto do
d7720 74 6c 6f 63 6b 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a tlock_end_lock;.
d7730 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 }. . /* grab
d7740 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
d7750 63 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 ck */. fd = ope
d7760 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44 n(zLockFile,O_RD
d7770 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 ONLY|O_CREAT|O_E
d7780 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 66 28 XCL,0600);. if(
d7790 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 fd<0 ){. /*
d77a0 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 failed to open/c
d77b0 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20 reate the file,
d77c0 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79 someone else may
d77d0 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 68 65 have stolen the
d77e0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 lock */. int
d77f0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d7800 0a 20 20 20 20 69 66 28 20 45 45 58 49 53 54 20 . if( EEXIST
d7810 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 == tErrno ){.
d7820 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
d7830 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 USY;. } else
d7840 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
d7850 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d7860 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d7870 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
d7880 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f );. if( IS_
d7890 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 LOCK_ERROR(rc) )
d78a0 7b 0a 09 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 {..pFile->lastEr
d78b0 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
d78c0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
d78d0 67 6f 74 6f 20 64 6f 74 6c 6f 63 6b 5f 65 6e 64 goto dotlock_end
d78e0 5f 6c 6f 63 6b 3b 0a 20 20 7d 20 0a 20 20 63 6c _lock;. } . cl
d78f0 6f 73 65 28 66 64 29 3b 0a 20 20 0a 20 20 2f 2a ose(fd);. . /*
d7900 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 got it, set the
d7910 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e type and return
d7920 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e ok */. pFile->
d7930 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
d7940 79 70 65 3b 0a 0a 20 64 6f 74 6c 6f 63 6b 5f 65 ype;.. dotlock_e
d7950 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72 nd_lock:. retur
d7960 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 n rc;.}..static
d7970 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 int dotlockUnloc
d7980 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d7990 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
d79a0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a ) {. unixFile *
d79b0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d79c0 65 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a e*)id;. char *z
d79d0 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 LockFile = (char
d79e0 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
d79f0 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 gContext;.. ass
d7a00 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
d7a10 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b OSTRACE5("UNLOCK
d7a20 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 %d %d was %d p
d7a30 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d id=%d\n", pFile-
d7a40 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 >h, locktype,..
d7a50 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d7a60 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 e, getpid());.
d7a70 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
d7a80 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
d7a90 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 . . /* no-op i
d7aa0 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 f possible */.
d7ab0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
d7ac0 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype==locktype ){
d7ad0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d7ae0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d7af0 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 /* shared can ju
d7b00 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 st be set becaus
d7b10 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 e we always have
d7b20 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f an exclusive */
d7b30 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d . if (locktype=
d7b40 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a =SHARED_LOCK) {.
d7b50 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d7b60 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d7b70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d7b80 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
d7b90 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e * no, really, un
d7ba0 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 75 lock. */. if (u
d7bb0 6e 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 nlink(zLockFile)
d7bc0 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c ) {. int rc,
d7bd0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d7be0 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 20 . if( ENOENT
d7bf0 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 != tErrno ){.
d7c00 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
d7c10 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
d7c20 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
d7c30 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a _IOERR_UNLOCK);.
d7c40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53 }. if( IS
d7c50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d7c60 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
d7c70 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d7c80 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 no;. }. re
d7c90 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 turn rc; . }.
d7ca0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d7cb0 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 = NO_LOCK;. ret
d7cc0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
d7cd0 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 ../*. ** Close a
d7ce0 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 file.. */.stati
d7cf0 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f c int dotlockClo
d7d00 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
d7d10 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 *id) {. if( id
d7d20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 ){. unixFile
d7d30 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d7d40 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f 74 6c le*)id;. dotl
d7d50 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f ockUnlock(id, NO
d7d60 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c 69 _LOCK);. sqli
d7d70 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e te3_free(pFile->
d7d80 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b lockingContext);
d7d90 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6c . }. return cl
d7da0 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b oseUnixFile(id);
d7db0 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 .}...#endif /* S
d7dc0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
d7dd0 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f KING_STYLE */../
d7de0 2a 0a 2a 2a 20 54 68 65 20 6e 6f 6c 6f 63 6b 4c *.** The nolockL
d7df0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 69 73 ockingContext is
d7e00 20 76 6f 69 64 0a 2a 2f 0a 74 79 70 65 64 65 66 void.*/.typedef
d7e10 20 76 6f 69 64 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b void nolockLock
d7e20 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 73 74 61 ingContext;..sta
d7e30 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 tic int nolockCh
d7e40 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
d7e50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d7e60 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 , int *pResOut)
d7e70 7b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 {. *pResOut = 0
d7e80 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
d7e90 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 E_OK;.}..static
d7ea0 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 int nolockLock(s
d7eb0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d7ec0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
d7ed0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
d7ee0 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 _OK;.}..static i
d7ef0 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 nt nolockUnlock(
d7f00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d7f10 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 , int locktype)
d7f20 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 {. return SQLIT
d7f30 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
d7f40 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a lose a file..*/.
d7f50 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 static int noloc
d7f60 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 kClose(sqlite3_f
d7f70 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 72 65 74 ile *id) {. ret
d7f80 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c urn closeUnixFil
d7f90 65 28 69 64 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a e(id);.}.../*.**
d7fa0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 Information and
d7fb0 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e 20 6f control of an o
d7fc0 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e pen file handle.
d7fd0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
d7fe0 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 nixFileControl(s
d7ff0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d8000 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 int op, void *p
d8010 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 Arg){. switch(
d8020 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 op ){. case S
d8030 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b QLITE_FCNTL_LOCK
d8040 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a STATE: {. *
d8050 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 75 (int*)pArg = ((u
d8060 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f nixFile*)id)->lo
d8070 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 cktype;. re
d8080 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d8090 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
d80a0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
d80b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
d80c0 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 the sector size
d80d0 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 in bytes of the
d80e0 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 underlying bloc
d80f0 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 k device for.**
d8100 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 the specified fi
d8110 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f le. This is almo
d8120 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 st always 512 by
d8130 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a tes, but may be.
d8140 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f ** larger for so
d8150 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a me devices..**.*
d8160 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 * SQLite code as
d8170 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 sumes this funct
d8180 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e ion cannot fail.
d8190 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 It also assumes
d81a0 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 that.** if two
d81b0 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 files are create
d81c0 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 d in the same fi
d81d0 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 le-system direct
d81e0 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 ory (i.e..** a d
d81f0 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 atabase and its
d8200 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 journal file) th
d8210 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 at the sector si
d8220 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a ze will be the.*
d8230 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e * same for both.
d8240 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
d8250 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 nixSectorSize(sq
d8260 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
d8270 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
d8280 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
d8290 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 SIZE;.}../*.** R
d82a0 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 65 eturn the device
d82b0 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 characteristics
d82c0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 54 for the file. T
d82d0 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 2e his is always 0.
d82e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
d82f0 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 nixDeviceCharact
d8300 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 eristics(sqlite3
d8310 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 _file *id){. re
d8320 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
d8330 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
d8340 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
d8350 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 unixFile structu
d8360 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 re pointed to by
d8370 20 70 49 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e pId..**.** When
d8380 20 6c 6f 63 6b 69 6e 67 20 65 78 74 65 6e 73 69 locking extensi
d8390 6f 6e 73 20 61 72 65 20 65 6e 61 62 6c 65 64 2c ons are enabled,
d83a0 20 74 68 65 20 66 69 6c 65 70 61 74 68 20 61 6e the filepath an
d83b0 64 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 d locking style
d83c0 0a 2a 2a 20 61 72 65 20 6e 65 65 64 65 64 20 74 .** are needed t
d83d0 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
d83e0 75 6e 69 78 46 69 6c 65 20 70 4d 65 74 68 6f 64 unixFile pMethod
d83f0 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f 63 6b to use for lock
d8400 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a ing operations..
d8410 2a 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 2d 73 ** The locking-s
d8420 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 6c 6f tyle specific lo
d8430 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 64 61 74 ckingContext dat
d8440 61 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 a structure is c
d8450 72 65 61 74 65 64 20 0a 2a 2a 20 61 6e 64 20 61 reated .** and a
d8460 73 73 69 67 6e 65 64 20 68 65 72 65 20 61 6c 73 ssigned here als
d8470 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 o..*/.static int
d8480 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 fillInUnixFile(
d8490 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
d84a0 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f pVfs, /* Po
d84b0 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a inter to vfs obj
d84c0 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 ect */. int h,
d84d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d84e0 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 /* Open file de
d84f0 73 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 scriptor of file
d8500 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f being opened */
d8510 0a 20 20 69 6e 74 20 64 69 72 66 64 2c 20 20 20 . int dirfd,
d8520 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 /* Di
d8530 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 rectory file des
d8540 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c criptor */. sql
d8550 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20 ite3_file *pId,
d8560 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 6f /* Write to
d8570 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 the unixFile st
d8580 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a ructure here */.
d8590 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
d85a0 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d ilename, /* Nam
d85b0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 62 65 e of the file be
d85c0 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 ing opened */.
d85d0 69 6e 74 20 6e 6f 4c 6f 63 6b 20 20 20 20 20 20 int noLock
d85e0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 /* Omit
d85f0 6c 6f 63 6b 69 6e 67 20 69 66 20 74 72 75 65 20 locking if true
d8600 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 65 4c 6f 63 */.){. int eLoc
d8610 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 kingStyle;. uni
d8620 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75 xFile *pNew = (u
d8630 6e 69 78 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20 nixFile *)pId;.
d8640 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d8650 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4d 61 63 72 6f _OK;.. /* Macro
d8660 20 74 6f 20 64 65 66 69 6e 65 20 74 68 65 20 73 to define the s
d8670 74 61 74 69 63 20 63 6f 6e 74 65 6e 74 73 20 6f tatic contents o
d8680 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 69 6f 5f f an sqlite3_io_
d8690 6d 65 74 68 6f 64 73 20 0a 20 20 2a 2a 20 73 74 methods . ** st
d86a0 72 75 63 74 75 72 65 20 66 6f 72 20 61 20 75 6e ructure for a un
d86b0 69 78 20 62 61 63 6b 65 6e 64 20 66 69 6c 65 2e ix backend file.
d86c0 20 44 69 66 66 65 72 65 6e 74 20 6c 6f 63 6b 69 Different locki
d86d0 6e 67 20 6d 65 74 68 6f 64 73 0a 20 20 2a 2a 20 ng methods. **
d86e0 72 65 71 75 69 72 65 20 64 69 66 66 65 72 65 6e require differen
d86f0 74 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 t functions for
d8700 74 68 65 20 78 43 6c 6f 73 65 2c 20 78 4c 6f 63 the xClose, xLoc
d8710 6b 2c 20 78 55 6e 6c 6f 63 6b 20 61 6e 64 0a 20 k, xUnlock and.
d8720 20 2a 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 ** xCheckReserv
d8730 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 73 2e 0a edLock methods..
d8740 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 49 */. #define I
d8750 4f 4d 45 54 48 4f 44 53 28 78 43 6c 6f 73 65 2c OMETHODS(xClose,
d8760 20 78 4c 6f 63 6b 2c 20 78 55 6e 6c 6f 63 6b 2c xLock, xUnlock,
d8770 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
d8780 6f 63 6b 29 20 7b 20 20 20 20 5c 0a 20 20 20 20 ock) { \.
d8790 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
d87a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 /* i
d87b0 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 Version */
d87c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d87d0 20 20 20 20 20 5c 0a 20 20 20 20 78 43 6c 6f 73 \. xClos
d87e0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
d87f0 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
d8800 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
d8810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8820 5c 0a 20 20 20 20 75 6e 69 78 52 65 61 64 2c 20 \. unixRead,
d8830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8840 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 20 20 20 /* xRead */
d8850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8860 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
d8870 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20 20 20 unixWrite,
d8880 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d8890 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20 20 20 xWrite */
d88a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d88b0 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
d88c0 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 Truncate,
d88d0 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e /* xTrun
d88e0 63 61 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 cate */
d88f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8900 20 5c 0a 20 20 20 20 75 6e 69 78 53 79 6e 63 2c \. unixSync,
d8910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8920 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 /* xSync */
d8930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8940 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
d8950 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 20 unixFileSize,
d8960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d8970 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 20 20 20 xFileSize */
d8980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8990 20 20 20 20 20 20 20 5c 0a 20 20 20 20 78 4c 6f \. xLo
d89a0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
d89b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
d89c0 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 k */
d89d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d89e0 20 20 5c 0a 20 20 20 20 78 55 6e 6c 6f 63 6b 2c \. xUnlock,
d89f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8a00 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a /* xUnlock *
d8a10 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
d8a20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
d8a30 20 20 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 xCheckReserve
d8a40 64 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 2f dLock, /
d8a50 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
d8a60 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 Lock */
d8a70 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
d8a80 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 ixFileControl,
d8a90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 /* xFi
d8aa0 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 leControl */
d8ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d8ac0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 65 63 \. unixSec
d8ad0 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20 20 20 torSize,
d8ae0 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 /* xSectorS
d8af0 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ize */
d8b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
d8b10 20 20 20 20 75 6e 69 78 44 65 76 69 63 65 43 68 unixDeviceCh
d8b20 61 72 61 63 74 65 72 69 73 74 69 63 73 20 20 20 aracteristics
d8b30 2f 2a 20 78 44 65 76 69 63 65 43 61 70 61 62 69 /* xDeviceCapabi
d8b40 6c 69 74 69 65 73 20 2a 2f 20 20 20 20 20 20 20 lities */
d8b50 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 20 \. }.
d8b60 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
d8b70 69 6f 5f 6d 65 74 68 6f 64 73 20 61 49 6f 4d 65 io_methods aIoMe
d8b80 74 68 6f 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 49 thod[] = {. I
d8b90 4f 4d 45 54 48 4f 44 53 28 75 6e 69 78 43 6c 6f OMETHODS(unixClo
d8ba0 73 65 2c 20 75 6e 69 78 4c 6f 63 6b 2c 20 75 6e se, unixLock, un
d8bb0 69 78 55 6e 6c 6f 63 6b 2c 20 75 6e 69 78 43 68 ixUnlock, unixCh
d8bc0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 eckReservedLock)
d8bd0 20 0a 20 20 20 2c 49 4f 4d 45 54 48 4f 44 53 28 . ,IOMETHODS(
d8be0 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c 20 6e 6f 6c nolockClose, nol
d8bf0 6f 63 6b 4c 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 55 ockLock, nolockU
d8c00 6e 6c 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 43 68 65 nlock, nolockChe
d8c10 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a ckReservedLock).
d8c20 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
d8c30 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
d8c40 20 20 20 2c 49 4f 4d 45 54 48 4f 44 53 28 64 6f ,IOMETHODS(do
d8c50 74 6c 6f 63 6b 43 6c 6f 73 65 2c 20 64 6f 74 6c tlockClose, dotl
d8c60 6f 63 6b 4c 6f 63 6b 2c 20 64 6f 74 6c 6f 63 6b ockLock, dotlock
d8c70 55 6e 6c 6f 63 6b 2c 64 6f 74 6c 6f 63 6b 43 68 Unlock,dotlockCh
d8c80 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 eckReservedLock)
d8c90 0a 20 20 20 2c 49 4f 4d 45 54 48 4f 44 53 28 66 . ,IOMETHODS(f
d8ca0 6c 6f 63 6b 43 6c 6f 73 65 2c 20 66 6c 6f 63 6b lockClose, flock
d8cb0 4c 6f 63 6b 2c 20 66 6c 6f 63 6b 55 6e 6c 6f 63 Lock, flockUnloc
d8cc0 6b 2c 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73 k, flockCheckRes
d8cd0 65 72 76 65 64 4c 6f 63 6b 29 0a 20 20 20 2c 49 ervedLock). ,I
d8ce0 4f 4d 45 54 48 4f 44 53 28 61 66 70 43 6c 6f 73 OMETHODS(afpClos
d8cf0 65 2c 20 61 66 70 4c 6f 63 6b 2c 20 61 66 70 55 e, afpLock, afpU
d8d00 6e 6c 6f 63 6b 2c 20 61 66 70 43 68 65 63 6b 52 nlock, afpCheckR
d8d10 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 65 6e eservedLock).#en
d8d20 64 69 66 0a 20 20 7d 3b 0a 20 20 2f 2a 20 54 68 dif. };. /* Th
d8d30 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 49 e order of the I
d8d40 4f 4d 45 54 48 4f 44 53 20 6d 61 63 72 6f 73 20 OMETHODS macros
d8d50 61 62 6f 76 65 20 69 73 20 69 6d 70 6f 72 74 61 above is importa
d8d60 6e 74 2e 20 20 49 74 20 6d 75 73 74 20 62 65 20 nt. It must be
d8d70 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 6f 72 the. ** same or
d8d80 64 65 72 20 61 73 20 74 68 65 20 4c 4f 43 4b 49 der as the LOCKI
d8d90 4e 47 5f 53 54 59 4c 45 20 6e 75 6d 62 65 72 73 NG_STYLE numbers
d8da0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 4c . */. assert(L
d8db0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 OCKING_STYLE_POS
d8dc0 49 58 3d 3d 31 29 3b 0a 20 20 61 73 73 65 72 74 IX==1);. assert
d8dd0 28 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e (LOCKING_STYLE_N
d8de0 4f 4e 45 3d 3d 32 29 3b 0a 20 20 61 73 73 65 72 ONE==2);. asser
d8df0 74 28 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f t(LOCKING_STYLE_
d8e00 44 4f 54 46 49 4c 45 3d 3d 33 29 3b 0a 20 20 61 DOTFILE==3);. a
d8e10 73 73 65 72 74 28 4c 4f 43 4b 49 4e 47 5f 53 54 ssert(LOCKING_ST
d8e20 59 4c 45 5f 46 4c 4f 43 4b 3d 3d 34 29 3b 0a 20 YLE_FLOCK==4);.
d8e30 20 61 73 73 65 72 74 28 4c 4f 43 4b 49 4e 47 5f assert(LOCKING_
d8e40 53 54 59 4c 45 5f 41 46 50 3d 3d 35 29 3b 0a 0a STYLE_AFP==5);..
d8e50 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
d8e60 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 pLock==NULL );.
d8e70 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 assert( pNew->p
d8e80 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 Open==NULL );..
d8e90 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 OSTRACE3("OPEN
d8ea0 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 %-3d %s\n", h
d8eb0 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 , zFilename);
d8ec0 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b . pNew->h = h;
d8ed0 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d . pNew->dirfd =
d8ee0 20 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 dirfd;. SET_TH
d8ef0 52 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 0a 20 READID(pNew);..
d8f00 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 if( noLock ){.
d8f10 20 20 20 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 eLockingStyle
d8f20 20 3d 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 = LOCKING_STYLE
d8f30 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a _NONE;. }else{.
d8f40 20 20 20 20 65 4c 6f 63 6b 69 6e 67 53 74 79 6c eLockingStyl
d8f50 65 20 3d 20 64 65 74 65 63 74 4c 6f 63 6b 69 6e e = detectLockin
d8f60 67 53 74 79 6c 65 28 70 56 66 73 2c 20 7a 46 69 gStyle(pVfs, zFi
d8f70 6c 65 6e 61 6d 65 2c 20 68 29 3b 0a 20 20 7d 0a lename, h);. }.
d8f80 0a 20 20 73 77 69 74 63 68 28 20 65 4c 6f 63 6b . switch( eLock
d8f90 69 6e 67 53 74 79 6c 65 20 29 7b 0a 0a 20 20 20 ingStyle ){..
d8fa0 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 case LOCKING_ST
d8fb0 59 4c 45 5f 50 4f 53 49 58 3a 20 7b 0a 20 20 20 YLE_POSIX: {.
d8fc0 20 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b enterMutex();
d8fd0 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e 64 . rc = find
d8fe0 4c 6f 63 6b 49 6e 66 6f 28 68 2c 20 26 70 4e 65 LockInfo(h, &pNe
d8ff0 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d w->pLock, &pNew-
d9000 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 6c >pOpen);. l
d9010 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 eaveMutex();.
d9020 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
d9030 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
d9040 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
d9050 0a 20 20 20 20 63 61 73 65 20 4c 4f 43 4b 49 4e . case LOCKIN
d9060 47 5f 53 54 59 4c 45 5f 41 46 50 3a 20 7b 0a 20 G_STYLE_AFP: {.
d9070 20 20 20 20 20 2f 2a 20 41 46 50 20 6c 6f 63 6b /* AFP lock
d9080 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69 6c ing uses the fil
d9090 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65 e path so it nee
d90a0 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 ds to be include
d90b0 64 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 d in. ** th
d90c0 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 e afpLockingCont
d90d0 65 78 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ext.. */.
d90e0 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f afpLockingCo
d90f0 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 ntext *pCtx;.
d9100 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 pNew->locking
d9110 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 20 3d Context = pCtx =
d9120 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
d9130 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29 sizeof(*pCtx) )
d9140 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 ;. if( pCtx
d9150 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
d9160 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
d9170 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
d9180 20 20 20 20 20 20 20 2f 2a 20 4e 42 3a 20 7a 46 /* NB: zF
d9190 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 73 20 61 ilename exists a
d91a0 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 nd remains valid
d91b0 20 75 6e 74 69 6c 20 74 68 65 20 66 69 6c 65 20 until the file
d91c0 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 20 20 20 is closed.
d91d0 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74 ** according t
d91e0 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 31 o requirement F1
d91f0 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 20 1141. So we do
d9200 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 not need to make
d9210 20 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f a. ** co
d9220 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 py of the filena
d9230 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 me. */. p
d9240 43 74 78 2d 3e 66 69 6c 65 50 61 74 68 20 3d 20 Ctx->filePath =
d9250 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20 zFilename;.
d9260 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 28 29 3b srandomdev();
d9270 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
d9280 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 reak;. }..
d9290 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 case LOCKING_ST
d92a0 59 4c 45 5f 44 4f 54 46 49 4c 45 3a 20 7b 0a 20 YLE_DOTFILE: {.
d92b0 20 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c 65 20 /* Dotfile
d92c0 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 locking uses the
d92d0 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 file path so it
d92e0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 needs to be inc
d92f0 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 20 20 2a luded in. *
d9300 2a 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 * the dotlockLoc
d9310 6b 69 6e 67 43 6f 6e 74 65 78 74 20 0a 20 20 20 kingContext .
d9320 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 */. char
d9330 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20 *zLockFile;.
d9340 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 int nFilename
d9350 3b 0a 20 20 20 20 20 20 6e 46 69 6c 65 6e 61 6d ;. nFilenam
d9360 65 20 3d 20 73 74 72 6c 65 6e 28 7a 46 69 6c 65 e = strlen(zFile
d9370 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 20 20 20 name) + 6;.
d9380 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 zLockFile = (ch
d9390 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c ar *)sqlite3_mal
d93a0 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a loc(nFilename);.
d93b0 20 20 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 if( zLockF
d93c0 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ile==0 ){.
d93d0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
d93e0 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 MEM;. }else
d93f0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
d9400 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65 3_snprintf(nFile
d9410 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 2c name, zLockFile,
d9420 20 22 25 73 2e 6c 6f 63 6b 22 2c 20 7a 46 69 6c "%s.lock", zFil
d9430 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a ename);. }.
d9440 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b pNew->lock
d9450 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f ingContext = zLo
d9460 63 6b 46 69 6c 65 3b 0a 20 20 20 20 20 20 62 72 ckFile;. br
d9470 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 eak;. }..
d9480 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 case LOCKING_STY
d9490 4c 45 5f 46 4c 4f 43 4b 3a 20 0a 20 20 20 20 63 LE_FLOCK: . c
d94a0 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c ase LOCKING_STYL
d94b0 45 5f 4e 4f 4e 45 3a 20 0a 20 20 20 20 20 20 62 E_NONE: . b
d94c0 72 65 61 6b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d reak;.#endif. }
d94d0 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 . . pNew->last
d94e0 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 69 66 28 Errno = 0;. if(
d94f0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
d9500 7b 0a 20 20 20 20 69 66 28 20 64 69 72 66 64 3e {. if( dirfd>
d9510 3d 30 20 29 20 63 6c 6f 73 65 28 64 69 72 66 64 =0 ) close(dirfd
d9520 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 68 29 3b );. close(h);
d9530 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e . }else{. pN
d9540 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 61 ew->pMethod = &a
d9550 49 6f 4d 65 74 68 6f 64 5b 65 4c 6f 63 6b 69 6e IoMethod[eLockin
d9560 67 53 74 79 6c 65 2d 31 5d 3b 0a 20 20 20 20 4f gStyle-1];. O
d9570 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a penCounter(+1);.
d9580 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
d9590 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 .}../*.** Open a
d95a0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d95b0 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 to the director
d95c0 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c y containing fil
d95d0 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 e zFilename..**
d95e0 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a If successful, *
d95f0 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 pFd is set to th
d9600 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 e opened file de
d9610 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 scriptor and.**
d9620 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
d9630 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 urned. If an err
d9640 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 or occurs, eithe
d9650 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a r SQLITE_NOMEM.*
d9660 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 * or SQLITE_CANT
d9670 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 OPEN is returned
d9680 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 and *pFd is set
d9690 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 to an undefined
d96a0 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a .** value..**.**
d96b0 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 If SQLITE_OK is
d96c0 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 returned, the c
d96d0 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 aller is respons
d96e0 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 ible for closing
d96f0 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 .** the file des
d9700 63 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 criptor *pFd usi
d9710 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 ng close()..*/.s
d9720 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 tatic int openDi
d9730 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 rectory(const ch
d9740 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 ar *zFilename, i
d9750 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 nt *pFd){. int
d9760 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d ii;. int fd = -
d9770 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 1;. char zDirna
d9780 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b me[MAX_PATHNAME+
d9790 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 1];.. sqlite3_s
d97a0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 nprintf(MAX_PATH
d97b0 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 NAME, zDirname,
d97c0 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 "%s", zFilename)
d97d0 3b 0a 20 20 66 6f 72 28 69 69 3d 73 74 72 6c 65 ;. for(ii=strle
d97e0 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 69 69 3e n(zDirname); ii>
d97f0 3d 30 20 26 26 20 7a 44 69 72 6e 61 6d 65 5b 69 =0 && zDirname[i
d9800 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 3b 0a i]!='/'; ii--);.
d9810 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a 20 20 if( ii>0 ){.
d9820 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 20 3d zDirname[ii] =
d9830 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20 3d 20 '\0';. fd =
d9840 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 4f open(zDirname, O
d9850 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59 _RDONLY|O_BINARY
d9860 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 66 64 , 0);. if( fd
d9870 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 46 44 >=0 ){.#ifdef FD
d9880 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20 20 66 _CLOEXEC. f
d9890 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 cntl(fd, F_SETFD
d98a0 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 , fcntl(fd, F_GE
d98b0 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f TFD, 0) | FD_CLO
d98c0 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a 20 20 EXEC);.#endif.
d98d0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 OSTRACE3("OP
d98e0 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c 6e 22 ENDIR %-3d %s\n"
d98f0 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65 29 3b , fd, zDirname);
d9900 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 46 . }. }. *pF
d9910 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75 72 6e d = fd;. return
d9920 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45 5f 4f (fd>=0?SQLITE_O
d9930 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 K:SQLITE_CANTOPE
d9940 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 N);.}../*.** Cre
d9950 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ate a temporary
d9960 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 file name in zBu
d9970 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 f. zBuf must be
d9980 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79 allocated.** by
d9990 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f the calling pro
d99a0 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20 62 65 cess and must be
d99b0 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 big enough to h
d99c0 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 old at least.**
d99d0 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
d99e0 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 bytes..*/.stati
d99f0 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d c int getTempnam
d9a00 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 e(int nBuf, char
d9a10 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 *zBuf){. stati
d9a20 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a c const char *az
d9a30 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 Dirs[] = {.
d9a40 30 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 0,. "/var/tm
d9a50 70 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 p",. "/usr/t
d9a60 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 mp",. "/tmp"
d9a70 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b ,. ".",. };
d9a80 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
d9a90 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 unsigned char zC
d9aa0 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 hars[] =. "ab
d9ab0 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 cdefghijklmnopqr
d9ac0 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 stuvwxyz". "A
d9ad0 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 BCDEFGHIJKLMNOPQ
d9ae0 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 RSTUVWXYZ". "
d9af0 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69 0123456789";. i
d9b00 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63 nt i, j;. struc
d9b10 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63 6f t stat buf;. co
d9b20 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20 3d nst char *zDir =
d9b30 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49 74 27 73 ".";.. /* It's
d9b40 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 odd to simulate
d9b50 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 72 an io-error her
d9b60 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 68 e, but really th
d9b70 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a 20 is is just. **
d9b80 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 72 using the io-err
d9b90 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 or infrastructur
d9ba0 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 53 e to test that S
d9bb0 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68 QLite handles th
d9bc0 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e is. ** function
d9bd0 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f 0a failing. . */.
d9be0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
d9bf0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
d9c00 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 61 7a 44 _IOERR );.. azD
d9c10 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 irs[0] = sqlite3
d9c20 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b _temp_directory;
d9c30 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 . for(i=0; i<si
d9c40 7a 65 6f 66 28 61 7a 44 69 72 73 29 2f 73 69 7a zeof(azDirs)/siz
d9c50 65 6f 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b 20 eof(azDirs[0]);
d9c60 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 7a i++){. if( az
d9c70 44 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e Dirs[i]==0 ) con
d9c80 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 73 tinue;. if( s
d9c90 74 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20 26 tat(azDirs[i], &
d9ca0 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b buf) ) continue;
d9cb0 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49 . if( !S_ISDI
d9cc0 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29 R(buf.st_mode) )
d9cd0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 continue;. i
d9ce0 66 28 20 61 63 63 65 73 73 28 61 7a 44 69 72 73 f( access(azDirs
d9cf0 5b 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74 69 [i], 07) ) conti
d9d00 6e 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d 20 nue;. zDir =
d9d10 61 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20 62 azDirs[i];. b
d9d20 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 reak;. }.. /*
d9d30 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6f Check that the o
d9d40 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 20 utput buffer is
d9d50 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 large enough for
d9d60 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 the temporary f
d9d70 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 ile . ** name.
d9d80 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65 If it is not, re
d9d90 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
d9da0 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 73 R.. */. if( (s
d9db0 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 74 trlen(zDir) + st
d9dc0 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d 50 rlen(SQLITE_TEMP
d9dd0 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b 20 _FILE_PREFIX) +
d9de0 31 37 29 20 3e 3d 20 6e 42 75 66 20 29 7b 0a 20 17) >= nBuf ){.
d9df0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d9e00 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 _ERROR;. }.. d
d9e10 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 o{. sqlite3_s
d9e20 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c nprintf(nBuf-17,
d9e30 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 zBuf, "%s/"SQLI
d9e40 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
d9e50 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 FIX, zDir);.
d9e60 6a 20 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 j = strlen(zBuf)
d9e70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 ;. sqlite3_ra
d9e80 6e 64 6f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 ndomness(15, &zB
d9e90 75 66 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28 uf[j]);. for(
d9ea0 69 3d 30 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 i=0; i<15; i++,
d9eb0 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 j++){. zBuf
d9ec0 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 [j] = (char)zCha
d9ed0 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 rs[ ((unsigned c
d9ee0 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 har)zBuf[j])%(si
d9ef0 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 zeof(zChars)-1)
d9f00 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 ];. }. zBu
d9f10 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 77 68 69 f[j] = 0;. }whi
d9f20 6c 65 28 20 61 63 63 65 73 73 28 7a 42 75 66 2c le( access(zBuf,
d9f30 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 0)==0 );. retur
d9f40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
d9f50 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 ./*.** Open the
d9f60 66 69 6c 65 20 7a 50 61 74 68 2e 0a 2a 2a 20 0a file zPath..** .
d9f70 2a 2a 20 50 72 65 76 69 6f 75 73 6c 79 2c 20 74 ** Previously, t
d9f80 68 65 20 53 51 4c 69 74 65 20 4f 53 20 6c 61 79 he SQLite OS lay
d9f90 65 72 20 75 73 65 64 20 74 68 72 65 65 20 66 75 er used three fu
d9fa0 6e 63 74 69 6f 6e 73 20 69 6e 20 70 6c 61 63 65 nctions in place
d9fb0 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 3a of this.** one:
d9fc0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 .**.** sqlit
d9fd0 65 33 4f 73 4f 70 65 6e 52 65 61 64 57 72 69 74 e3OsOpenReadWrit
d9fe0 65 28 29 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 e();.** sqli
d9ff0 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 4f 6e 6c te3OsOpenReadOnl
da000 79 28 29 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 y();.** sqli
da010 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 te3OsOpenExclusi
da020 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ve();.**.** Thes
da030 65 20 63 61 6c 6c 73 20 63 6f 72 72 65 73 70 6f e calls correspo
da040 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 nd to the follow
da050 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 ing combinations
da060 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a of flags:.**.**
da070 20 20 20 20 20 52 65 61 64 57 72 69 74 65 28 29 ReadWrite()
da080 20 2d 3e 20 20 20 20 20 28 52 45 41 44 57 52 49 -> (READWRI
da090 54 45 20 7c 20 43 52 45 41 54 45 29 0a 2a 2a 20 TE | CREATE).**
da0a0 20 20 20 20 52 65 61 64 4f 6e 6c 79 28 29 20 20 ReadOnly()
da0b0 2d 3e 20 20 20 20 20 28 52 45 41 44 4f 4e 4c 59 -> (READONLY
da0c0 29 20 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 45 78 ) .** OpenEx
da0d0 63 6c 75 73 69 76 65 28 29 20 2d 3e 20 28 52 45 clusive() -> (RE
da0e0 41 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 ADWRITE | CREATE
da0f0 20 7c 20 45 58 43 4c 55 53 49 56 45 29 0a 2a 2a | EXCLUSIVE).**
da100 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 4f 70 65 6e .** The old Open
da110 45 78 63 6c 75 73 69 76 65 28 29 20 61 63 63 65 Exclusive() acce
da120 70 74 65 64 20 61 20 62 6f 6f 6c 65 61 6e 20 61 pted a boolean a
da130 72 67 75 6d 65 6e 74 20 2d 20 22 64 65 6c 46 6c rgument - "delFl
da140 61 67 22 2e 20 49 66 0a 2a 2a 20 74 72 75 65 2c ag". If.** true,
da150 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 63 6f the file was co
da160 6e 66 69 67 75 72 65 64 20 74 6f 20 62 65 20 61 nfigured to be a
da170 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c utomatically del
da180 65 74 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a eted when the.**
da190 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 63 6c 6f file handle clo
da1a0 73 65 64 2e 20 54 6f 20 61 63 68 69 65 76 65 20 sed. To achieve
da1b0 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 74 20 the same effect
da1c0 75 73 69 6e 67 20 74 68 69 73 20 6e 65 77 20 0a using this new .
da1d0 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61 64 ** interface, ad
da1e0 64 20 74 68 65 20 44 45 4c 45 54 45 4f 4e 43 4c d the DELETEONCL
da1f0 4f 53 45 20 66 6c 61 67 20 74 6f 20 74 68 6f 73 OSE flag to thos
da200 65 20 73 70 65 63 69 66 69 65 64 20 61 62 6f 76 e specified abov
da210 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 65 6e 45 78 e for .** OpenEx
da220 63 6c 75 73 69 76 65 28 29 2e 0a 2a 2f 0a 73 74 clusive()..*/.st
da230 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4f 70 65 atic int unixOpe
da240 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
da250 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 *pVfs, . const
da260 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 char *zPath, .
da270 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
da280 46 69 6c 65 2c 0a 20 20 69 6e 74 20 66 6c 61 67 File,. int flag
da290 73 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c s,. int *pOutFl
da2a0 61 67 73 0a 29 7b 0a 20 20 69 6e 74 20 66 64 20 ags.){. int fd
da2b0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
da2c0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 /* File
da2d0 64 65 73 63 72 69 70 74 6f 72 20 72 65 74 75 72 descriptor retur
da2e0 6e 65 64 20 62 79 20 6f 70 65 6e 28 29 20 2a 2f ned by open() */
da2f0 0a 20 20 69 6e 74 20 64 69 72 66 64 20 3d 20 2d . int dirfd = -
da300 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
da310 20 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 /* Directory f
da320 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
da330 2f 0a 20 20 69 6e 74 20 6f 66 6c 61 67 73 20 3d /. int oflags =
da340 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
da350 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 /* Flags to p
da360 61 73 73 20 74 6f 20 6f 70 65 6e 28 29 20 2a 2f ass to open() */
da370 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 66 . int eType = f
da380 6c 61 67 73 26 30 78 46 46 46 46 46 46 30 30 3b lags&0xFFFFFF00;
da390 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 66 69 6c /* Type of fil
da3a0 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 e to open */. i
da3b0 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20 20 20 20 nt noLock;
da3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
da3d0 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 6c 6f True to omit lo
da3e0 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 cking primitives
da3f0 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 */.. int isExc
da400 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 lusive = (flags
da410 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 & SQLITE_OPEN_E
da420 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 XCLUSIVE);. int
da430 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 isDelete =
da440 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f (flags & SQLITE_
da450 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
da460 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 SE);. int isCre
da470 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 ate = (flags
da480 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 & SQLITE_OPEN_C
da490 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 REATE);. int is
da4a0 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c Readonly = (fl
da4b0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
da4c0 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 N_READONLY);. i
da4d0 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20 nt isReadWrite
da4e0 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 = (flags & SQLIT
da4f0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
da500 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 );.. /* If crea
da510 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 ting a master or
da520 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e main-file journ
da530 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f al, this functio
da540 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a n will open. **
da550 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 a file-descript
da560 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 or on the direct
da570 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 ory too. The fir
da580 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 st time unixSync
da590 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 (). ** is calle
da5a0 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 d the directory
da5b0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
da5c0 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 will be fsync()e
da5d0 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a d and close()d..
da5e0 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 */. int isOpe
da5f0 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 nDirectory = (is
da600 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 Create && .
da610 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f (eType==SQLITE_
da620 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
da630 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 NAL || eType==SQ
da640 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
da650 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 OURNAL). );..
da660 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a /* If argument z
da670 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 Path is a NULL p
da680 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e ointer, this fun
da690 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 ction is require
da6a0 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 d to open. ** a
da6b0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e temporary file.
da6c0 20 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72 Use this buffer
da6d0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 to store the fi
da6e0 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f le name in.. */
da6f0 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 . char zTmpname
da700 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d [MAX_PATHNAME+1]
da710 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
da720 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a zName = zPath;..
da730 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 /* Check the f
da740 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 ollowing stateme
da750 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 nts are true: .
da760 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 **. ** (a) E
da770 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 xactly one of th
da780 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 e READWRITE and
da790 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d READONLY flags m
da7a0 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 ust be set, and
da7b0 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 . ** (b) if C
da7c0 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 REATE is set, th
da7d0 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 en READWRITE mus
da7e0 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 t also be set, a
da7f0 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 nd. ** (c) if
da800 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 EXCLUSIVE is se
da810 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d t, then CREATE m
da820 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e ust also be set.
da830 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 . ** (d) if D
da840 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 ELETEONCLOSE is
da850 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 set, then CREATE
da860 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 must also be se
da870 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 t.. */. assert
da880 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 ((isReadonly==0
da890 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d || isReadWrite==
da8a0 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69 0) && (isReadWri
da8b0 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 te || isReadonly
da8c0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 ));. assert(isC
da8d0 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 reate==0 || isRe
da8e0 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 adWrite);. asse
da8f0 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d rt(isExclusive==
da900 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 0 || isCreate);.
da910 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 assert(isDelet
da920 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 e==0 || isCreate
da930 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 );.. /* The mai
da940 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e n DB, main journ
da950 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a al, and master j
da960 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 ournal are never
da970 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 automatically.
da980 20 2a 2a 20 64 65 6c 65 74 65 64 0a 20 20 2a 2f ** deleted. */
da990 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 . assert( eType
da9a0 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 !=SQLITE_OPEN_MA
da9b0 49 4e 5f 44 42 20 7c 7c 20 21 69 73 44 65 6c 65 IN_DB || !isDele
da9c0 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 te );. assert(
da9d0 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 eType!=SQLITE_OP
da9e0 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 EN_MAIN_JOURNAL
da9f0 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b 0a || !isDelete );.
daa00 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 assert( eType!
daa10 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 =SQLITE_OPEN_MAS
daa20 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21 TER_JOURNAL || !
daa30 69 73 44 65 6c 65 74 65 20 29 3b 0a 0a 20 20 2f isDelete );.. /
daa40 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 * Assert that th
daa50 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 68 61 e upper layer ha
daa60 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 74 68 65 s set one of the
daa70 20 22 66 69 6c 65 2d 74 79 70 65 22 20 66 6c 61 "file-type" fla
daa80 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 gs. */. assert(
daa90 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
daaa0 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 PEN_MAIN_DB
daab0 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 || eType==SQLIT
daac0 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 0a E_OPEN_TEMP_DB .
daad0 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d || eType=
daae0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
daaf0 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 N_JOURNAL || eTy
dab00 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
dab10 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 TEMP_JOURNAL .
dab20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 || eType==S
dab30 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f QLITE_OPEN_SUBJO
dab40 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70 65 URNAL || eType
dab50 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
dab60 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 STER_JOURNAL .
dab70 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 || eType==S
dab80 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 QLITE_OPEN_TRANS
dab90 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a 0a 20 20 IENT_DB. );..
daba0 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c memset(pFile, 0,
dabb0 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 sizeof(unixFile
dabc0 29 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4e 61 6d ));.. if( !zNam
dabd0 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b e ){. int rc;
dabe0 0a 20 20 20 20 61 73 73 65 72 74 28 69 73 44 65 . assert(isDe
dabf0 6c 65 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 lete && !isOpenD
dac00 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 irectory);. r
dac10 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 c = getTempname(
dac20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 MAX_PATHNAME+1,
dac30 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 zTmpname);. i
dac40 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
dac50 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
dac60 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a rc;. }. z
dac70 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b Name = zTmpname;
dac80 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 52 65 . }.. if( isRe
dac90 61 64 6f 6e 6c 79 20 29 20 20 6f 66 6c 61 67 73 adonly ) oflags
daca0 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 |= O_RDONLY;.
dacb0 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20 if( isReadWrite
dacc0 29 20 6f 66 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 ) oflags |= O_RD
dacd0 57 52 3b 0a 20 20 69 66 28 20 69 73 43 72 65 61 WR;. if( isCrea
dace0 74 65 20 29 20 20 20 20 6f 66 6c 61 67 73 20 7c te ) oflags |
dacf0 3d 20 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 = O_CREAT;. if(
dad00 20 69 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f isExclusive ) o
dad10 66 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c flags |= (O_EXCL
dad20 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 20 |O_NOFOLLOW);.
dad30 6f 66 6c 61 67 73 20 7c 3d 20 28 4f 5f 4c 41 52 oflags |= (O_LAR
dad40 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41 52 59 29 GEFILE|O_BINARY)
dad50 3b 0a 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a ;.. fd = open(z
dad60 4e 61 6d 65 2c 20 6f 66 6c 61 67 73 2c 20 69 73 Name, oflags, is
dad70 44 65 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 Delete?0600:SQLI
dad80 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f TE_DEFAULT_FILE_
dad90 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 PERMISSIONS);.
dada0 69 66 28 20 66 64 3c 30 20 26 26 20 65 72 72 6e if( fd<0 && errn
dadb0 6f 21 3d 45 49 53 44 49 52 20 26 26 20 69 73 52 o!=EISDIR && isR
dadc0 65 61 64 57 72 69 74 65 20 26 26 20 21 69 73 45 eadWrite && !isE
dadd0 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 xclusive ){.
dade0 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 /* Failed to ope
dadf0 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 n the file for r
dae00 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 ead/write access
dae10 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e . Try read-only.
dae20 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 26 3d */. flags &=
dae30 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ~(SQLITE_OPEN_R
dae40 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f EADWRITE|SQLITE_
dae50 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 OPEN_CREATE);.
dae60 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 flags |= SQLIT
dae70 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b E_OPEN_READONLY;
dae80 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78 . return unix
dae90 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 Open(pVfs, zPath
daea0 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 , pFile, flags,
daeb0 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 7d 0a pOutFlags);. }.
daec0 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
daed0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
daee0 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 CANTOPEN;. }.
daef0 69 66 28 20 69 73 44 65 6c 65 74 65 20 29 7b 0a if( isDelete ){.
daf00 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61 6d 65 unlink(zName
daf10 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 );. }. if( pOu
daf20 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 tFlags ){. *p
daf30 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 OutFlags = flags
daf40 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
daf50 66 64 21 3d 30 29 3b 0a 20 20 69 66 28 20 69 73 fd!=0);. if( is
daf60 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 29 7b OpenDirectory ){
daf70 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 6f 70 . int rc = op
daf80 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 enDirectory(zPat
daf90 68 2c 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20 h, &dirfd);.
dafa0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
dafb0 4b 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 65 K ){. close
dafc0 28 66 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 (fd);. retu
dafd0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d rn rc;. }. }
dafe0 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45 ..#ifdef FD_CLOE
daff0 58 45 43 0a 20 20 66 63 6e 74 6c 28 66 64 2c 20 XEC. fcntl(fd,
db000 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 F_SETFD, fcntl(f
db010 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c d, F_GETFD, 0) |
db020 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 FD_CLOEXEC);.#e
db030 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 3d ndif.. noLock =
db040 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f eType!=SQLITE_O
db050 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a 20 20 72 PEN_MAIN_DB;. r
db060 65 74 75 72 6e 20 66 69 6c 6c 49 6e 55 6e 69 78 eturn fillInUnix
db070 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64 File(pVfs, fd, d
db080 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 irfd, pFile, zPa
db090 74 68 2c 20 6e 6f 4c 6f 63 6b 29 3b 0a 7d 0a 0a th, noLock);.}..
db0a0 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 /*.** Delete the
db0b0 20 66 69 6c 65 20 61 74 20 7a 50 61 74 68 2e 20 file at zPath.
db0c0 49 66 20 74 68 65 20 64 69 72 53 79 6e 63 20 61 If the dirSync a
db0d0 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 2c rgument is true,
db0e0 20 66 73 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20 fsync().** the
db0f0 64 69 72 65 63 74 6f 72 79 20 61 66 74 65 72 20 directory after
db100 64 65 6c 65 74 69 6e 67 20 74 68 65 20 66 69 6c deleting the fil
db110 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
db120 20 75 6e 69 78 44 65 6c 65 74 65 28 73 71 6c 69 unixDelete(sqli
db130 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 te3_vfs *pVfs, c
db140 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
db150 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a , int dirSync){.
db160 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
db170 45 5f 4f 4b 3b 0a 20 20 53 69 6d 75 6c 61 74 65 E_OK;. Simulate
db180 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 IOError(return S
db190 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
db1a0 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 TE);. unlink(zP
db1b0 61 74 68 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ath);.#ifndef SQ
db1c0 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 LITE_DISABLE_DIR
db1d0 53 59 4e 43 0a 20 20 69 66 28 20 64 69 72 53 79 SYNC. if( dirSy
db1e0 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64 nc ){. int fd
db1f0 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 ;. rc = openD
db200 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 irectory(zPath,
db210 26 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 &fd);. if( rc
db220 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
db230 20 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 if( fsync(f
db240 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 d) ){. rc
db250 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
db260 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20 20 20 DIR_FSYNC;.
db270 20 7d 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66 }. close(f
db280 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 d);. }. }.#e
db290 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 ndif. return rc
db2a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 ;.}../*.** Test
db2b0 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 the existance of
db2c0 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69 or access permi
db2d0 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a ssions of file z
db2e0 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73 Path. The.** tes
db2f0 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65 t performed depe
db300 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 nds on the value
db310 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a of flags:.**.**
db320 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 SQLITE_ACCE
db330 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72 SS_EXISTS: Retur
db340 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 n 1 if the file
db350 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51 exists.** SQ
db360 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
db370 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20 WRITE: Return 1
db380 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 if the file is r
db390 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65 ead and writable
db3a0 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f ..** SQLITE_
db3b0 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a ACCESS_READONLY:
db3c0 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
db3d0 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c file is readabl
db3e0 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 e..**.** Otherwi
db3f0 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a se return 0..*/.
db400 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41 static int unixA
db410 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 ccess(. sqlite3
db420 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 _vfs *pVfs, . c
db430 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
db440 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 , . int flags,
db450 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a . int *pResOut.
db460 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64 65 20 3d ){. int amode =
db470 20 30 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 0;. SimulateIO
db480 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
db490 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 LITE_IOERR_ACCES
db4a0 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 S; );. switch(
db4b0 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 flags ){. cas
db4c0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
db4d0 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d EXISTS:. am
db4e0 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20 ode = F_OK;.
db4f0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
db500 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
db510 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 READWRITE:.
db520 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f amode = W_OK|R_
db530 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b OK;. break;
db540 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
db550 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 _ACCESS_READ:.
db560 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b amode = R_OK
db570 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a ;. break;..
db580 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
db590 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 assert(!"Inva
db5a0 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 lid flags argume
db5b0 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 nt");. }. *pRe
db5c0 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a sOut = (access(z
db5d0 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29 Path, amode)==0)
db5e0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
db5f0 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 E_OK;.}.../*.**
db600 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 Turn a relative
db610 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 pathname into a
db620 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54 full pathname. T
db630 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 he relative path
db640 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73 .** is stored as
db650 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 a nul-terminate
db660 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 d string in the
db670 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 buffer pointed t
db680 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a o by.** zPath. .
db690 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74 **.** zOut point
db6a0 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 s to a buffer of
db6b0 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65 at least sqlite
db6c0 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 3_vfs.mxPathname
db6d0 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74 bytes .** (in t
db6e0 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41 his case, MAX_PA
db6f0 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54 THNAME bytes). T
db700 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20 he full-path is
db710 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68 written to.** th
db720 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 65 is buffer before
db730 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 returning..*/.s
db740 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75 tatic int unixFu
db750 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 llPathname(. sq
db760 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
db770 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
db780 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 ointer to vfs ob
db790 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ject */. const
db7a0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 char *zPath,
db7b0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 /* Possi
db7c0 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 bly relative inp
db7d0 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 ut path */. int
db7e0 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 nOut,
db7f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
db800 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
db810 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
db820 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20 char *zOut
db830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
db840 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 /* Output buffer
db850 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27 */.){.. /* It'
db860 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 s odd to simulat
db870 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 e an io-error he
db880 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 re, but really t
db890 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a his is just. **
db8a0 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 using the io-er
db8b0 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 ror infrastructu
db8c0 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 re to test that
db8d0 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 SQLite handles t
db8e0 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
db8f0 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20 n failing. This
db900 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66 function could f
db910 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d ail if, for exam
db920 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75 ple, the. ** cu
db930 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69 rrent working di
db940 72 65 63 74 6c 79 20 68 61 73 20 62 65 65 6e 20 rectly has been
db950 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a 20 unlinked.. */.
db960 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
db970 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
db980 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73 65 ERROR );.. asse
db990 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 rt( pVfs->mxPath
db9a0 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e 41 name==MAX_PATHNA
db9b0 4d 45 20 29 3b 0a 20 20 7a 4f 75 74 5b 6e 4f 75 ME );. zOut[nOu
db9c0 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 t-1] = '\0';. i
db9d0 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 f( zPath[0]=='/'
db9e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
db9f0 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a snprintf(nOut, z
dba00 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 Out, "%s", zPath
dba10 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
dba20 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 int nCwd;. if
dba30 28 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e ( getcwd(zOut, n
dba40 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 Out-1)==0 ){.
dba50 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
dba60 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d _CANTOPEN;. }
dba70 0a 20 20 20 20 6e 43 77 64 20 3d 20 73 74 72 6c . nCwd = strl
dba80 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71 en(zOut);. sq
dba90 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
dbaa0 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 5b Out-nCwd, &zOut[
dbab0 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a 50 nCwd], "/%s", zP
dbac0 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ath);. }. retu
dbad0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 23 rn SQLITE_OK;..#
dbae0 69 66 20 30 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 if 0. /*. ** R
dbaf0 65 6d 6f 76 65 20 22 2f 2e 2f 22 20 70 61 74 68 emove "/./" path
dbb00 20 65 6c 65 6d 65 6e 74 73 20 61 6e 64 20 63 6f elements and co
dbb10 6e 76 65 72 74 20 22 2f 41 2f 2e 2f 22 20 70 61 nvert "/A/./" pa
dbb20 74 68 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a th elements. **
dbb30 20 74 6f 20 6a 75 73 74 20 22 2f 22 2e 0a 20 20 to just "/"..
dbb40 2a 2f 0a 20 20 69 66 28 20 7a 46 75 6c 6c 20 29 */. if( zFull )
dbb50 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a {. int i, j;.
dbb60 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a for(i=j=0; z
dbb70 46 75 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 Full[i]; i++){.
dbb80 20 20 20 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69 if( zFull[i
dbb90 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 ]=='/' ){.
dbba0 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69 2b 31 5d if( zFull[i+1]
dbbb0 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75 65 =='/' ) continue
dbbc0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 ;. if( zF
dbbd0 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 ull[i+1]=='.' &&
dbbe0 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27 2f 27 zFull[i+2]=='/'
dbbf0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 20 ){. i
dbc00 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 += 1;.
dbc10 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
dbc20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
dbc30 7a 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20 zFull[i+1]=='.'
dbc40 26 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27 && zFull[i+2]=='
dbc50 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 33 5d .' && zFull[i+3]
dbc60 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 =='/' ){.
dbc70 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 20 26 26 while( j>0 &&
dbc80 20 7a 46 75 6c 6c 5b 6a 2d 31 5d 21 3d 27 2f 27 zFull[j-1]!='/'
dbc90 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 ){ j--; }.
dbca0 20 20 20 20 20 69 20 2b 3d 20 33 3b 0a 20 20 20 i += 3;.
dbcb0 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b continue;
dbcc0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
dbcd0 20 7d 0a 20 20 20 20 20 20 7a 46 75 6c 6c 5b 6a }. zFull[j
dbce0 2b 2b 5d 20 3d 20 7a 46 75 6c 6c 5b 69 5d 3b 0a ++] = zFull[i];.
dbcf0 20 20 20 20 7d 0a 20 20 20 20 7a 46 75 6c 6c 5b }. zFull[
dbd00 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 j] = 0;. }.#end
dbd10 69 66 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 if.}...#ifndef S
dbd20 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
dbd30 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 EXTENSION./*.**
dbd40 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f Interfaces for o
dbd50 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 pening a shared
dbd60 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 library, finding
dbd70 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a entry points.**
dbd80 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 within the shar
dbd90 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 ed library, and
dbda0 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 closing the shar
dbdb0 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 ed library..*/.#
dbdc0 69 6e 63 6c 75 64 65 20 3c 64 6c 66 63 6e 2e 68 include <dlfcn.h
dbdd0 3e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 75 >.static void *u
dbde0 6e 69 78 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 nixDlOpen(sqlite
dbdf0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 3_vfs *pVfs, con
dbe00 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
dbe10 6d 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 6c me){. return dl
dbe20 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 open(zFilename,
dbe30 52 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f RTLD_NOW | RTLD_
dbe40 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a GLOBAL);.}../*.*
dbe50 2a 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 * SQLite calls t
dbe60 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d his function imm
dbe70 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 61 ediately after a
dbe80 20 63 61 6c 6c 20 74 6f 20 75 6e 69 78 44 6c 53 call to unixDlS
dbe90 79 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 ym() or.** unixD
dbea0 6c 4f 70 65 6e 28 29 20 66 61 69 6c 73 20 28 72 lOpen() fails (r
dbeb0 65 74 75 72 6e 73 20 61 20 6e 75 6c 6c 20 70 6f eturns a null po
dbec0 69 6e 74 65 72 29 2e 20 49 66 20 61 20 6d 6f 72 inter). If a mor
dbed0 65 20 64 65 74 61 69 6c 65 64 20 65 72 72 6f 72 e detailed error
dbee0 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20 61 .** message is a
dbef0 76 61 69 6c 61 62 6c 65 2c 20 69 74 20 69 73 20 vailable, it is
dbf00 77 72 69 74 74 65 6e 20 74 6f 20 7a 42 75 66 4f written to zBufO
dbf10 75 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 ut. If no error
dbf20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 61 76 message.** is av
dbf30 61 69 6c 61 62 6c 65 2c 20 7a 42 75 66 4f 75 74 ailable, zBufOut
dbf40 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 is left unmodif
dbf50 69 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 75 ied and SQLite u
dbf60 73 65 73 20 61 20 64 65 66 61 75 6c 74 0a 2a 2a ses a default.**
dbf70 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a error message..
dbf80 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
dbf90 6e 69 78 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 nixDlError(sqlit
dbfa0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
dbfb0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
dbfc0 75 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a ufOut){. char *
dbfd0 7a 45 72 72 3b 0a 20 20 65 6e 74 65 72 4d 75 74 zErr;. enterMut
dbfe0 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 ex();. zErr = d
dbff0 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 lerror();. if(
dc000 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 zErr ){. sqli
dc010 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
dc020 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 f, zBufOut, "%s"
dc030 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 6c , zErr);. }. l
dc040 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 73 eaveMutex();.}.s
dc050 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 tatic void *unix
dc060 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 DlSym(sqlite3_vf
dc070 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
dc080 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 Handle, const ch
dc090 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 ar *zSymbol){.
dc0a0 72 65 74 75 72 6e 20 64 6c 73 79 6d 28 70 48 61 return dlsym(pHa
dc0b0 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a ndle, zSymbol);.
dc0c0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e }.static void un
dc0d0 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 ixDlClose(sqlite
dc0e0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
dc0f0 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 64 d *pHandle){. d
dc100 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 3b lclose(pHandle);
dc110 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 .}.#else /* if S
dc120 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
dc130 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 EXTENSION is def
dc140 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 ined: */. #defi
dc150 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e 20 20 30 ne unixDlOpen 0
dc160 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 . #define unixD
dc170 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 lError 0. #defi
dc180 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20 20 20 30 ne unixDlSym 0
dc190 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 . #define unixD
dc1a0 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a lClose 0.#endif.
dc1b0 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6e 42 75 ./*.** Write nBu
dc1c0 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f f bytes of rando
dc1d0 6d 20 64 61 74 61 20 74 6f 20 74 68 65 20 73 75 m data to the su
dc1e0 70 70 6c 69 65 64 20 62 75 66 66 65 72 20 7a 42 pplied buffer zB
dc1f0 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e uf..*/.static in
dc200 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 t unixRandomness
dc210 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
dc220 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 fs, int nBuf, ch
dc230 61 72 20 2a 7a 42 75 66 29 7b 0a 0a 20 20 61 73 ar *zBuf){.. as
dc240 73 65 72 74 28 6e 42 75 66 3e 3d 28 73 69 7a 65 sert(nBuf>=(size
dc250 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f of(time_t)+sizeo
dc260 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 f(int)));.. /*
dc270 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 We have to initi
dc280 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 alize zBuf to pr
dc290 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 event valgrind f
dc2a0 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 rom reporting.
dc2b0 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 ** errors. The
dc2c0 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 reports issued b
dc2d0 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 y valgrind are i
dc2e0 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f ncorrect - we wo
dc2f0 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 uld. ** prefer
dc300 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e that the randomn
dc310 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 ess be increased
dc320 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f by making use o
dc330 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 f the. ** unini
dc340 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 tialized space i
dc350 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c n zBuf - but val
dc360 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e grind errors ten
dc370 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 d to worry. **
dc380 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 some users. Rat
dc390 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 her than argue,
dc3a0 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 it seems easier
dc3b0 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 just to initiali
dc3c0 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c ze. ** the whol
dc3d0 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 e array and sile
dc3e0 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 nce valgrind, ev
dc3f0 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 en if that means
dc400 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 less randomness
dc410 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e . ** in the ran
dc420 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 dom seed.. **.
dc430 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 ** When testing
dc440 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a , initializing z
dc450 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 Buf[] to zero is
dc460 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 all we do. Tha
dc470 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 t means. ** tha
dc480 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 t we always use
dc490 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 the same random
dc4a0 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e number sequence.
dc4b0 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 This makes the
dc4c0 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 . ** tests repe
dc4d0 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d atable.. */. m
dc4e0 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e emset(zBuf, 0, n
dc4f0 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e Buf);.#if !defin
dc500 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a ed(SQLITE_TEST).
dc510 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c {. int pid,
dc520 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 fd;. fd = op
dc530 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d en("/dev/urandom
dc540 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 ", O_RDONLY);.
dc550 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
dc560 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 time_t t;.
dc570 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 time(&t);.
dc580 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c memcpy(zBuf,
dc590 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b &t, sizeof(t));
dc5a0 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 74 . pid = get
dc5b0 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d pid();. mem
dc5c0 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 cpy(&zBuf[sizeof
dc5d0 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 (t)], &pid, size
dc5e0 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20 of(pid));.
dc5f0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74 assert( sizeof(t
dc600 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 6e )+sizeof(pid)<=n
dc610 42 75 66 20 29 3b 0a 20 20 20 20 20 20 6e 42 75 Buf );. nBu
dc620 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20 2b 20 f = sizeof(t) +
dc630 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 20 sizeof(pid);.
dc640 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 42 }else{. nB
dc650 75 66 20 3d 20 72 65 61 64 28 66 64 2c 20 7a 42 uf = read(fd, zB
dc660 75 66 2c 20 6e 42 75 66 29 3b 0a 20 20 20 20 20 uf, nBuf);.
dc670 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 close(fd);.
dc680 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 }. }.#endif. r
dc690 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a eturn nBuf;.}...
dc6a0 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 /*.** Sleep for
dc6b0 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 a little while.
dc6c0 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 Return the amou
dc6d0 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 nt of time slept
dc6e0 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e ..** The argumen
dc6f0 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
dc700 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 of microseconds
dc710 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 we want to sleep
dc720 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 ..** The return
dc730 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d value is the num
dc740 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
dc750 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 nds of sleep act
dc760 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 ually.** request
dc770 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 ed from the unde
dc780 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 rlying operating
dc790 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 system, a numbe
dc7a0 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 r which.** might
dc7b0 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e be greater than
dc7c0 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
dc7d0 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e argument, but n
dc7e0 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 ot less.** than
dc7f0 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f the argument..*/
dc800 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
dc810 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 Sleep(sqlite3_vf
dc820 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 s *pVfs, int mic
dc830 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20 roseconds){.#if
dc840 64 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c defined(HAVE_USL
dc850 45 45 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c EEP) && HAVE_USL
dc860 45 45 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 EEP. usleep(mic
dc870 72 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 20 72 65 roseconds);. re
dc880 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 turn microsecond
dc890 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 s;.#else. int s
dc8a0 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72 6f 73 econds = (micros
dc8b0 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29 2f 31 econds+999999)/1
dc8c0 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65 70 28 000000;. sleep(
dc8d0 73 65 63 6f 6e 64 73 29 3b 0a 20 20 72 65 74 75 seconds);. retu
dc8e0 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 rn seconds*10000
dc8f0 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 00;.#endif.}../*
dc900 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
dc910 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 g variable, if s
dc920 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f et to a non-zero
dc930 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 value, becomes
dc940 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 the result.** re
dc950 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 turned from sqli
dc960 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
dc970 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 (). This is use
dc980 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a d for testing..*
dc990 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
dc9a0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
dc9b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 int sqlite3_curr
dc9c0 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 ent_time = 0;.#e
dc9d0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 ndif../*.** Find
dc9e0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d the current tim
dc9f0 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 e (in Universal
dca00 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 Coordinated Time
dca10 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a ). Write the.**
dca20 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e current time an
dca30 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 d date as a Juli
dca40 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e an Day number in
dca50 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a to *prNow and.**
dca60 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 return 0. Retu
dca70 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 rn 1 if the time
dca80 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 and date cannot
dca90 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 be found..*/.st
dcaa0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72 atic int unixCur
dcab0 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 rentTime(sqlite3
dcac0 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 _vfs *pVfs, doub
dcad0 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 64 le *prNow){.#ifd
dcae0 65 66 20 4e 4f 5f 47 45 54 54 4f 44 0a 20 20 74 ef NO_GETTOD. t
dcaf0 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 28 ime_t t;. time(
dcb00 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 &t);. *prNow =
dcb10 74 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 t/86400.0 + 2440
dcb20 35 38 37 2e 35 3b 0a 23 65 6c 73 65 0a 20 20 73 587.5;.#else. s
dcb30 74 72 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e truct timeval sN
dcb40 6f 77 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64 ow;. gettimeofd
dcb50 61 79 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20 ay(&sNow, 0);.
dcb60 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 *prNow = 2440587
dcb70 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 .5 + sNow.tv_sec
dcb80 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e /86400.0 + sNow.
dcb90 74 76 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30 tv_usec/86400000
dcba0 30 30 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 23 69 000.0;.#endif.#i
dcbb0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
dcbc0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 . if( sqlite3_c
dcbd0 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 urrent_time ){.
dcbe0 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 *prNow = sqli
dcbf0 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
dcc00 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 /86400.0 + 24405
dcc10 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 87.5;. }.#endif
dcc20 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
dcc30 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47 static int unixG
dcc40 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 etLastError(sqli
dcc50 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
dcc60 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
dcc70 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 Buf){. return 0
dcc80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
dcc90 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 alize the operat
dcca0 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 ing system inter
dccb0 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f face..*/.SQLITE_
dccc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
dccd0 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 20 0a os_init(void){ .
dcce0 20 20 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 64 65 /* Macro to de
dccf0 66 69 6e 65 20 74 68 65 20 73 74 61 74 69 63 20 fine the static
dcd00 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e 20 73 contents of an s
dcd10 71 6c 69 74 65 33 5f 76 66 73 20 73 74 72 75 63 qlite3_vfs struc
dcd20 74 75 72 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 ture for. ** th
dcd30 65 20 75 6e 69 78 20 62 61 63 6b 65 6e 64 2e 20 e unix backend.
dcd40 54 68 65 20 74 77 6f 20 70 61 72 61 6d 65 74 65 The two paramete
dcd50 72 73 20 61 72 65 20 74 68 65 20 76 61 6c 75 65 rs are the value
dcd60 73 20 74 6f 20 75 73 65 20 66 6f 72 0a 20 20 2a s to use for. *
dcd70 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 * the sqlite3_vf
dcd80 73 2e 7a 4e 61 6d 65 20 61 6e 64 20 73 71 6c 69 s.zName and sqli
dcd90 74 65 33 5f 76 66 73 2e 70 41 70 70 44 61 74 61 te3_vfs.pAppData
dcda0 20 66 69 65 6c 64 73 2c 20 72 65 73 70 65 63 74 fields, respect
dcdb0 69 76 65 6c 79 2e 0a 20 20 2a 2a 20 0a 20 20 2a ively.. ** . *
dcdc0 2f 0a 20 20 23 64 65 66 69 6e 65 20 55 4e 49 58 /. #define UNIX
dcdd0 56 46 53 28 7a 56 66 73 4e 61 6d 65 2c 20 70 56 VFS(zVfsName, pV
dcde0 66 73 41 70 70 44 61 74 61 29 20 7b 20 20 20 20 fsAppData) {
dcdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
dce00 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 1,
dce10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
dce20 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 rsion */
dce30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
dce40 20 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c sizeof(unixFil
dce50 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a 4f 73 46 e), /* szOsF
dce60 69 6c 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ile */
dce70 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
dce80 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 20 20 MAX_PATHNAME,
dce90 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e /* mxPathn
dcea0 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ame */
dceb0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 30 2c \. 0,
dcec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dced0 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 20 /* pNext */
dcee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcef0 20 20 20 20 20 20 5c 0a 20 20 20 20 7a 56 66 73 \. zVfs
dcf00 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 Name,
dcf10 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 20 20 20 /* zName */
dcf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcf30 20 20 20 20 5c 0a 20 20 20 20 28 76 6f 69 64 20 \. (void
dcf40 2a 29 70 56 66 73 41 70 70 44 61 74 61 2c 20 20 *)pVfsAppData,
dcf50 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 20 20 /* pAppData */
dcf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcf70 20 20 5c 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e \. unixOpen
dcf80 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
dcf90 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 xOpen */
dcfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcfb0 5c 0a 20 20 20 20 75 6e 69 78 44 65 6c 65 74 65 \. unixDelete
dcfc0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 , /* x
dcfd0 44 65 6c 65 74 65 20 2a 2f 20 20 20 20 20 20 20 Delete */
dcfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
dcff0 20 20 20 20 75 6e 69 78 41 63 63 65 73 73 2c 20 unixAccess,
dd000 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 /* xAc
dd010 63 65 73 73 20 2a 2f 20 20 20 20 20 20 20 20 20 cess */
dd020 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
dd030 20 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 unixFullPathna
dd040 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46 75 6c 6c me, /* xFull
dd050 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 Pathname */
dd060 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
dd070 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 20 20 20 20 unixDlOpen,
dd080 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e /* xDlOpen
dd090 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
dd0a0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
dd0b0 69 78 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 ixDlError,
dd0c0 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 /* xDlError
dd0d0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
dd0e0 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
dd0f0 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 DlSym,
dd100 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 20 20 /* xDlSym */
dd110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd120 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c \. unixDl
dd130 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
dd140 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 20 20 /* xDlClose */
dd150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd160 20 20 5c 0a 20 20 20 20 75 6e 69 78 52 61 6e 64 \. unixRand
dd170 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 20 2f 2a omness, /*
dd180 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 20 xRandomness */
dd190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd1a0 5c 0a 20 20 20 20 75 6e 69 78 53 6c 65 65 70 2c \. unixSleep,
dd1b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
dd1c0 53 6c 65 65 70 20 2a 2f 20 20 20 20 20 20 20 20 Sleep */
dd1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
dd1e0 20 20 20 20 75 6e 69 78 43 75 72 72 65 6e 74 54 unixCurrentT
dd1f0 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20 78 43 75 ime, /* xCu
dd200 72 72 65 6e 74 54 69 6d 65 20 2a 2f 20 20 20 20 rrentTime */
dd210 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
dd220 20 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72 unixGetLastErr
dd230 6f 72 20 20 20 20 20 20 2f 2a 20 78 47 65 74 4c or /* xGetL
dd240 61 73 74 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 astError */
dd250 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a \. }.
dd260 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
dd270 33 5f 76 66 73 20 75 6e 69 78 56 66 73 20 3d 20 3_vfs unixVfs =
dd280 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 UNIXVFS("unix",
dd290 30 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 0);.#if SQLITE_E
dd2a0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
dd2b0 59 4c 45 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 YLE. int i;. s
dd2c0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 tatic sqlite3_vf
dd2d0 73 20 61 56 66 73 5b 5d 20 3d 20 7b 0a 20 20 20 s aVfs[] = {.
dd2e0 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 UNIXVFS("unix-p
dd2f0 6f 73 69 78 22 2c 20 20 20 4c 4f 43 4b 49 4e 47 osix", LOCKING
dd300 5f 53 54 59 4c 45 5f 50 4f 53 49 58 29 2c 20 0a _STYLE_POSIX), .
dd310 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 UNIXVFS("uni
dd320 78 2d 61 66 70 22 2c 20 20 20 20 20 4c 4f 43 4b x-afp", LOCK
dd330 49 4e 47 5f 53 54 59 4c 45 5f 41 46 50 29 2c 20 ING_STYLE_AFP),
dd340 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e . UNIXVFS("un
dd350 69 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 4c 4f 43 ix-flock", LOC
dd360 4b 49 4e 47 5f 53 54 59 4c 45 5f 46 4c 4f 43 4b KING_STYLE_FLOCK
dd370 29 2c 20 0a 20 20 20 20 55 4e 49 58 56 46 53 28 ), . UNIXVFS(
dd380 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c 20 "unix-dotfile",
dd390 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 44 4f LOCKING_STYLE_DO
dd3a0 54 46 49 4c 45 29 2c 20 0a 20 20 20 20 55 4e 49 TFILE), . UNI
dd3b0 58 56 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 XVFS("unix-none"
dd3c0 2c 20 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 , LOCKING_STY
dd3d0 4c 45 5f 4e 4f 4e 45 29 0a 20 20 7d 3b 0a 20 20 LE_NONE). };.
dd3e0 66 6f 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65 for(i=0; i<(size
dd3f0 6f 66 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28 of(aVfs)/sizeof(
dd400 73 71 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69 sqlite3_vfs)); i
dd410 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
dd420 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 61 _vfs_register(&a
dd430 56 66 73 5b 69 5d 2c 20 30 29 3b 0a 20 20 7d 0a Vfs[i], 0);. }.
dd440 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 #endif. sqlite3
dd450 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 75 _vfs_register(&u
dd460 6e 69 78 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 nixVfs, 1);. re
dd470 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
dd480 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f .}../*.** Shutdo
dd490 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 wn the operating
dd4a0 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 system interfac
dd4b0 65 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d e. This is a no-
dd4c0 6f 70 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a op for unix..*/.
dd4d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
dd4e0 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f qlite3_os_end(vo
dd4f0 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 id){ . return S
dd500 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 20 0a 23 QLITE_OK; .}. .#
dd510 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
dd520 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a 2f 2a 2a 2a OS_UNIX */../***
dd530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
dd540 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a of os_unix.c ***
dd550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
dd580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
dd590 6e 20 66 69 6c 65 20 6f 73 5f 77 69 6e 2e 63 20 n file os_win.c
dd5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
dd5d0 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a * 2004 May 22.**
dd5e0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
dd5f0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
dd600 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
dd610 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
dd620 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
dd630 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
dd640 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
dd650 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
dd660 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
dd670 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
dd680 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
dd690 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
dd6a0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
dd6b0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
dd6c0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
dd6d0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
dd6e0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
dd6f0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
dd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd740 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
dd750 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
dd760 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69 63 that is specific
dd770 20 74 6f 20 77 69 6e 64 6f 77 73 2e 0a 2a 2a 0a to windows..**.
dd780 2a 2a 20 24 49 64 3a 20 6f 73 5f 77 69 6e 2e 63 ** $Id: os_win.c
dd790 2c 76 20 31 2e 31 33 37 20 32 30 30 38 2f 31 31 ,v 1.137 2008/11
dd7a0 2f 30 37 20 30 30 3a 30 36 3a 31 38 20 64 72 68 /07 00:06:18 drh
dd7b0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20 53 51 Exp $.*/.#if SQ
dd7c0 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20 20 LITE_OS_WIN
dd7d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 /* Thi
dd7e0 73 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 66 s file is used f
dd7f0 6f 72 20 77 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 or windows only
dd800 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 */.../*.** A Not
dd810 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 e About Memory A
dd820 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a llocation:.**.**
dd830 20 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65 This driver use
dd840 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 s malloc()/free(
dd850 29 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 ) directly rathe
dd860 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 r than going thr
dd870 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 ough.** the SQLi
dd880 74 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 te-wrappers sqli
dd890 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c te3_malloc()/sql
dd8a0 69 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 ite3_free(). Th
dd8b0 6f 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 ose wrappers.**
dd8c0 61 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 are designed for
dd8d0 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 use on embedded
dd8e0 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d systems where m
dd8f0 65 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 emory is scarce
dd900 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 and.** malloc fa
dd910 69 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 ilures happen fr
dd920 65 71 75 65 6e 74 6c 79 2e 20 20 57 69 6e 33 32 equently. Win32
dd930 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 does not typica
dd940 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d lly run on.** em
dd950 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 bedded systems,
dd960 61 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 and when it does
dd970 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 the developers
dd980 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 normally have bi
dd990 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 gger.** problems
dd9a0 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 to worry about
dd9b0 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 than running out
dd9c0 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 of memory. So
dd9d0 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 there is not.**
dd9e0 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 a compelling nee
dd9f0 64 20 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 d to use the wra
dda00 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 ppers..**.** But
dda10 20 74 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 there is a good
dda20 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 reason to not u
dda30 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e se the wrappers.
dda40 20 20 49 66 20 77 65 20 75 73 65 20 74 68 65 0a If we use the.
dda50 2a 2a 20 77 72 61 70 70 65 72 73 20 74 68 65 6e ** wrappers then
dda60 20 77 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d we will get sim
dda70 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 ulated malloc()
dda80 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 failures within
dda90 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 this.** driver.
ddaa0 20 41 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 And that causes
ddab0 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 all kinds of pr
ddac0 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 oblems for our t
ddad0 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 ests. We.** cou
ddae0 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 ld enhance SQLit
ddaf0 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 e to deal with s
ddb00 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 imulated malloc
ddb10 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a failures within.
ddb20 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 ** the OS driver
ddb30 2c 20 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 , but the code t
ddb40 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 o deal with thos
ddb50 65 20 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 e failure would
ddb60 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 not.** be exerci
ddb70 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 sed on Linux (wh
ddb80 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 ich does not nee
ddb90 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e d to malloc() in
ddba0 20 74 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 the driver).**
ddbb0 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 and so we would
ddbc0 68 61 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 have difficulty
ddbd0 77 72 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 writing coverage
ddbe0 20 74 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a tests for that.
ddbf0 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 ** code. Better
ddc00 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f to leave the co
ddc10 64 65 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b de out, we think
ddc20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e ..**.** The poin
ddc30 74 20 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 t of this discus
ddc40 73 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f sion is as follo
ddc50 77 73 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 ws: When creati
ddc60 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c ng a new.** OS l
ddc70 61 79 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 ayer for an embe
ddc80 64 64 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 dded system, if
ddc90 79 6f 75 20 75 73 65 20 74 68 69 73 20 66 69 6c you use this fil
ddca0 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c e as an example,
ddcb0 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 .** avoid the us
ddcc0 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 e of malloc()/fr
ddcd0 65 65 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 ee(). Those rou
ddce0 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e tines work ok on
ddcf0 20 77 69 6e 64 6f 77 73 0a 2a 2a 20 64 65 73 6b windows.** desk
ddd00 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 tops but not so
ddd10 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 well in embedded
ddd20 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 69 systems..*/..#i
ddd30 6e 63 6c 75 64 65 20 3c 77 69 6e 62 61 73 65 2e nclude <winbase.
ddd40 68 3e 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59 47 h>..#ifdef __CYG
ddd50 57 49 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65 20 WIN__.# include
ddd60 3c 73 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 <sys/cygwin.h>.#
ddd70 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 endif../*.** Mac
ddd80 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 ros used to dete
ddd90 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 rmine whether or
ddda0 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 not to use thre
dddb0 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ads..*/.#if defi
dddc0 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29 20 ned(THREADSAFE)
dddd0 26 26 20 54 48 52 45 41 44 53 41 46 45 0a 23 20 && THREADSAFE.#
ddde0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33 define SQLITE_W3
dddf0 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 2_THREADS 1.#end
dde00 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 if../*.** Includ
dde10 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 e code that is c
dde20 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f ommon to all os_
dde30 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a *.c files.*/./**
dde40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
dde50 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 lude os_common.h
dde60 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
dde70 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a f os_win.c *****
dde80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
dde90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
ddea0 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f in file os_commo
ddeb0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
ddec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dded0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
ddee0 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a ** 2004 May 22.*
ddef0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
ddf00 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
ddf10 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
ddf20 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
ddf30 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
ddf40 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
ddf50 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
ddf60 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
ddf70 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
ddf80 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
ddf90 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
ddfa0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
ddfb0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
ddfc0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
ddfd0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
ddfe0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
ddff0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
de000 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
de010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de050 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
de060 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 le contains macr
de070 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 os and a little
de080 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 bit of code that
de090 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a is common to.**
de0a0 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 all of the plat
de0b0 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 form-specific fi
de0c0 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 les (os_*.c) and
de0d0 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e is #included in
de0e0 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 to those.** file
de0f0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 s..**.** This fi
de100 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e le should be #in
de110 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 cluded by the os
de120 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e _*.c files only.
de130 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a It is not a.**
de140 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 general purpose
de150 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a header file..**
de160 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d .** $Id: os_comm
de170 6f 6e 2e 68 2c 76 20 31 2e 33 37 20 32 30 30 38 on.h,v 1.37 2008
de180 2f 30 35 2f 32 39 20 32 30 3a 32 32 3a 33 37 20 /05/29 20:22:37
de190 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 shane Exp $.*/.#
de1a0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f ifndef _OS_COMMO
de1b0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 N_H_.#define _OS
de1c0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a _COMMON_H_../*.*
de1d0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 * At least two b
de1e0 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 ugs have slipped
de1f0 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 in because we c
de200 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 hanged the MEMOR
de210 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f Y_DEBUG.** macro
de220 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 to SQLITE_DEBUG
de230 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 and some older
de240 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e makefiles have n
de250 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a ot yet made the.
de260 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 ** switch. The
de270 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 following code s
de280 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 hould catch this
de290 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 problem at comp
de2a0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 ile-time..*/.#if
de2b0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 def MEMORY_DEBUG
de2c0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 .# error "The ME
de2d0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f MORY_DEBUG macro
de2e0 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 is obsolete. U
de2f0 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 se SQLITE_DEBUG
de300 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 instead.".#endif
de310 0a 0a 0a 2f 2a 0a 20 2a 20 57 68 65 6e 20 74 65 .../*. * When te
de320 73 74 69 6e 67 2c 20 74 68 69 73 20 67 6c 6f 62 sting, this glob
de330 61 6c 20 76 61 72 69 61 62 6c 65 20 73 74 6f 72 al variable stor
de340 65 73 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 es the location
de350 6f 66 20 74 68 65 0a 20 2a 20 70 65 6e 64 69 6e of the. * pendin
de360 67 2d 62 79 74 65 20 69 6e 20 74 68 65 20 64 61 g-byte in the da
de370 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 2a 2f tabase file.. */
de380 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
de390 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 75 EST.SQLITE_API u
de3a0 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 71 6c 69 nsigned int sqli
de3b0 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65 te3_pending_byte
de3c0 20 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a 23 = 0x40000000;.#
de3d0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
de3e0 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 LITE_DEBUG.SQLIT
de3f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
de400 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 lite3OSTrace = 0
de410 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ;.#define OSTRAC
de420 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66 E1(X) if
de430 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
de440 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
de450 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 rintf(X).#define
de460 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 OSTRACE2(X,Y)
de470 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
de480 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
de490 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
de4a0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
de4b0 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 E3(X,Y,Z) if
de4c0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
de4d0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
de4e0 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 rintf(X,Y,Z).#de
de4f0 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c fine OSTRACE4(X,
de500 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c Y,Z,A) if( sql
de510 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
de520 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
de530 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e (X,Y,Z,A).#defin
de540 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a e OSTRACE5(X,Y,Z
de550 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 ,A,B) if( sqlite
de560 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
de570 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
de580 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 Y,Z,A,B).#define
de590 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c OSTRACE6(X,Y,Z,
de5a0 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 A,B,C) \. if(
de5b0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 sqlite3OSTrace)
de5c0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
de5d0 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a tf(X,Y,Z,A,B,C).
de5e0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 #define OSTRACE7
de5f0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 (X,Y,Z,A,B,C,D)
de600 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 \. if(sqlite3
de610 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 OSTrace) sqlite3
de620 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
de630 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 Z,A,B,C,D).#else
de640 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
de650 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 1(X).#define OST
de660 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 RACE2(X,Y).#defi
de670 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c ne OSTRACE3(X,Y,
de680 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
de690 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 CE4(X,Y,Z,A).#de
de6a0 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c fine OSTRACE5(X,
de6b0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 Y,Z,A,B).#define
de6c0 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c OSTRACE6(X,Y,Z,
de6d0 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f A,B,C).#define O
de6e0 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c STRACE7(X,Y,Z,A,
de6f0 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f B,C,D).#endif../
de700 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 *.** Macros for
de710 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 performance trac
de720 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 ing. Normally t
de730 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 urned off. Only
de740 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 works.** on i48
de750 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 6 hardware..*/.#
de760 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 ifdef SQLITE_PER
de770 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a FORMANCE_TRACE..
de780 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 /* .** hwtime.h
de790 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 contains inline
de7a0 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 assembler code f
de7b0 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 or implementing
de7c0 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d .** high-perform
de7d0 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 ance timing rout
de7e0 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a ines..*/./******
de7f0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
de800 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 hwtime.h in the
de810 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f middle of os_co
de820 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
de830 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
de840 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
de850 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a ile hwtime.h ***
de860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de880 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
de890 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 008 May 27.**.**
de8a0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
de8b0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
de8c0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
de8d0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
de8e0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
de8f0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
de900 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
de910 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
de920 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
de930 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
de940 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
de950 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
de960 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
de970 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
de980 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
de990 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
de9a0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
de9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
de9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
dea00 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
dea10 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 ontains inline a
dea20 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 sm code for retr
dea30 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 ieving "high-per
dea40 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 formance".** cou
dea50 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c nters for x86 cl
dea60 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 ass CPUs..**.**
dea70 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 $Id: hwtime.h,v
dea80 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 1.3 2008/08/01 1
dea90 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 4:33:15 shane Ex
deaa0 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f p $.*/.#ifndef _
deab0 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e HWTIME_H_.#defin
deac0 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a e _HWTIME_H_../*
dead0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
deae0 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 g routine only w
deaf0 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d orks on pentium-
deb00 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 class (or newer)
deb10 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 processors..**
deb20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53 It uses the RDTS
deb30 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 C opcode to read
deb40 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 the cycle count
deb50 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 value out of th
deb60 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 e.** processor a
deb70 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 nd returns that
deb80 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e value. This can
deb90 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 be used for hig
deba0 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 h-res.** profili
debb0 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 ng..*/.#if (defi
debc0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c ned(__GNUC__) ||
debd0 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
debe0 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 R)) && \. (
debf0 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c defined(i386) ||
dec00 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f defined(__i386_
dec10 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
dec20 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 _IX86)).. #if d
dec30 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
dec40 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
dec50 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
dec60 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
dec70 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 {. unsigned
dec80 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 int lo, hi;.
dec90 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
deca0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
decb0 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 "=a" (lo), "=d"
decc0 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 (hi));. ret
decd0 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 urn (sqlite_uint
dece0 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 64)hi << 32 | lo
decf0 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 ;. }.. #elif d
ded00 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
ded10 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e .. __declspec(n
ded20 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 aked) __inline s
ded30 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 qlite_uint64 __c
ded40 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 decl sqlite3Hwti
ded50 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f me(void){. _
ded60 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 _asm {. r
ded70 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 dtsc. ret
ded80 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 ; return
ded90 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 value at EDX:EAX
deda0 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 . }. }.. #
dedb0 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 endif..#elif (de
dedc0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
dedd0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 && defined(__x86
dede0 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c _64__)).. __inl
dedf0 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
dee00 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
dee10 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
dee20 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c nsigned long val
dee30 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
dee40 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 __volatile__ ("r
dee50 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 dtsc" : "=A" (va
dee60 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 l));. retur
dee70 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c n val;. }. .#el
dee80 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
dee90 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
deea0 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f (__ppc__)).. __
deeb0 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f inline__ sqlite_
deec0 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
deed0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
deee0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
deef0 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 long retval;.
def00 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
def10 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 junk;. __a
def20 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
def30 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 _ ("\n\.
def40 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 1: mftbu
def50 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 %1\n\.
def60 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 mftb
def70 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 %L0\n\.
def80 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 mftbu
def90 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 %0\n\.
defa0 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 cmpw
defb0 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 %0,%1\n\.
defc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e bn
defd0 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 e 1b".
defe0 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d : "=
deff0 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 r" (retval), "=r
df000 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 " (junk));.
df010 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a return retval;.
df020 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 }..#else.. #e
df030 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d rror Need implem
df040 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
df050 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
df060 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a your platform...
df070 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d /*. ** To com
df080 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 pile without imp
df090 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 lementing sqlite
df0a0 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 3Hwtime() for yo
df0b0 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a ur platform,. *
df0c0 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 * you can remove
df0d0 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f the above #erro
df0e0 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f r and use the fo
df0f0 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 llowing. ** stu
df100 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 b function. You
df110 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e will lose timin
df120 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 g support for ma
df130 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 ny. ** of the d
df140 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 ebugging and tes
df150 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 ting utilities,
df160 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 but it should at
df170 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 . ** least comp
df180 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a ile and run.. *
df190 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
df1a0 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 sqlite_uint64
df1b0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
df1c0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 oid){ return ((s
df1d0 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b qlite_uint64)0);
df1e0 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 }..#endif..#end
df1f0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
df200 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f HWTIME_H_) */../
df210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
df220 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a nd of hwtime.h *
df230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
df240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
df250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
df260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
df270 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
df280 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f we left off in o
df290 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a s_common.h *****
df2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
df2b0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 static sqlite_ui
df2c0 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 nt64 g_start;.st
df2d0 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 atic sqlite_uint
df2e0 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 64 g_elapsed;.#d
df2f0 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 efine TIMER_STAR
df300 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d T g_start=
df310 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a sqlite3Hwtime().
df320 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e #define TIMER_EN
df330 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 D g_elap
df340 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d sed=sqlite3Hwtim
df350 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 e()-g_start.#def
df360 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 ine TIMER_ELAPSE
df370 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a D g_elapsed.
df380 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 #else.#define TI
df390 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e MER_START.#defin
df3a0 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 e TIMER_END.#def
df3b0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 ine TIMER_ELAPSE
df3c0 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 D ((sqlite_u
df3d0 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a int64)0).#endif.
df3e0 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d ./*.** If we com
df3f0 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 pile with the SQ
df400 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 LITE_TEST macro
df410 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f set, then the fo
df420 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a llowing block.**
df430 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 of code will gi
df440 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 ve us the abilit
df450 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 y to simulate a
df460 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 disk I/O error.
df470 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 This.** is used
df480 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 for testing the
df490 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f I/O recovery lo
df4a0 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 gic..*/.#ifdef S
df4b0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
df4c0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
df4d0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 3_io_error_hit =
df4e0 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
df4f0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
df500 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a f I/O Errors */.
df510 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
df520 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
df530 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 hardhit = 0;
df540 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
df550 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f non-benign erro
df560 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 rs */.SQLITE_API
df570 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
df580 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 error_pending =
df590 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 0; /* Cou
df5a0 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 nt down to first
df5b0 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 I/O error */.SQ
df5c0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
df5d0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
df5e0 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 rsist = 0;
df5f0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f /* True if I/O
df600 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 errors persist
df610 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
df620 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
df630 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 or_benign = 0;
df640 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
df650 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e f errors are ben
df660 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ign */.SQLITE_AP
df670 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 I int sqlite3_di
df680 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d skfull_pending =
df690 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 0;.SQLITE_API i
df6a0 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 nt sqlite3_diskf
df6b0 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 ull = 0;.#define
df6c0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
df6d0 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 Benign(X) sqlite
df6e0 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 3_io_error_benig
df6f0 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 n=(X).#define Si
df700 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f mulateIOError(CO
df710 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 DE) \. if( (sq
df720 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
df730 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 ersist && sqlite
df740 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 3_io_error_hit)
df750 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 \. || sqli
df760 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
df770 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c ding-- == 1 ) \
df780 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b . {
df790 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 local_ioerr();
df7a0 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 CODE; }.static v
df7b0 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 oid local_ioerr(
df7c0 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 ){. IOTRACE(("I
df7d0 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c OERR\n"));. sql
df7e0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 ite3_io_error_hi
df7f0 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 t++;. if( !sqli
df800 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
df810 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f ign ) sqlite3_io
df820 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b _error_hardhit++
df830 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 ;.}.#define Simu
df840 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
df850 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 r(CODE) \. if(
df860 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
df870 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 l_pending ){ \.
df880 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f if( sqlite3_
df890 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
df8a0 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 == 1 ){ \.
df8b0 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b local_ioerr();
df8c0 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 \. sqlite
df8d0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 3_diskfull = 1;
df8e0 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 \. sqlite3
df8f0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 _io_error_hit =
df900 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 1; \. CODE
df910 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 ; \. }else{
df920 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 \. sqlite3
df930 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e _diskfull_pendin
df940 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a g--; \. } \.
df950 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 }.#else.#defi
df960 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
df970 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 orBenign(X).#def
df980 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
df990 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 ror(A).#define S
df9a0 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 imulateDiskfullE
df9b0 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a rror(A).#endif..
df9c0 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 /*.** When testi
df9d0 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 ng, keep a count
df9e0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f of the number o
df9f0 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f f open files..*/
dfa00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
dfa10 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
dfa20 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f nt sqlite3_open_
dfa30 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a file_count = 0;.
dfa40 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e #define OpenCoun
dfa50 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f ter(X) sqlite3_
dfa60 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b open_file_count+
dfa70 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 =(X).#else.#defi
dfa80 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 ne OpenCounter(X
dfa90 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 ).#endif..#endif
dfaa0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 /* !defined(_OS
dfab0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a _COMMON_H_) */..
dfac0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
dfad0 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e End of os_common
dfae0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
dfaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dfb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
dfb10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
dfb20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
dfb30 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
dfb40 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_win.c *******
dfb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
dfb60 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 ./*.** Some micr
dfb70 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 osoft compilers
dfb80 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 lack this defini
dfb90 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tion..*/.#ifndef
dfba0 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 INVALID_FILE_AT
dfbb0 54 52 49 42 55 54 45 53 0a 23 20 64 65 66 69 6e TRIBUTES.# defin
dfbc0 65 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 e INVALID_FILE_A
dfbd0 54 54 52 49 42 55 54 45 53 20 28 28 44 57 4f 52 TTRIBUTES ((DWOR
dfbe0 44 29 2d 31 29 20 0a 23 65 6e 64 69 66 0a 0a 2f D)-1) .#endif../
dfbf0 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 *.** Determine i
dfc00 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 f we are dealing
dfc10 20 77 69 74 68 20 57 69 6e 64 6f 77 73 43 45 20 with WindowsCE
dfc20 2d 20 77 68 69 63 68 20 68 61 73 20 61 20 6d 75 - which has a mu
dfc30 63 68 0a 2a 2a 20 72 65 64 75 63 65 64 20 41 50 ch.** reduced AP
dfc40 49 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 I..*/.#if SQLITE
dfc50 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 _OS_WINCE.# defi
dfc60 6e 65 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e ne AreFileApisAN
dfc70 53 49 28 29 20 31 0a 23 65 6e 64 69 66 0a 0a 2f SI() 1.#endif../
dfc80 2a 0a 2a 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73 *.** WinCE lacks
dfc90 20 6e 61 74 69 76 65 20 73 75 70 70 6f 72 74 20 native support
dfca0 66 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 for file locking
dfcb0 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 66 so we have to f
dfcc0 61 6b 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 73 ake it.** with s
dfcd0 6f 6d 65 20 63 6f 64 65 20 6f 66 20 6f 75 72 20 ome code of our
dfce0 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 own..*/.#if SQLI
dfcf0 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 74 79 70 65 TE_OS_WINCE.type
dfd00 64 65 66 20 73 74 72 75 63 74 20 77 69 6e 63 65 def struct wince
dfd10 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 6e 52 65 Lock {. int nRe
dfd20 61 64 65 72 73 3b 20 20 20 20 20 20 20 2f 2a 20 aders; /*
dfd30 4e 75 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72 Number of reader
dfd40 20 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 locks obtained
dfd50 2a 2f 0a 20 20 42 4f 4f 4c 20 62 50 65 6e 64 69 */. BOOL bPendi
dfd60 6e 67 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 ng; /* Indi
dfd70 63 61 74 65 73 20 61 20 70 65 6e 64 69 6e 67 20 cates a pending
dfd80 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 lock has been ob
dfd90 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c tained */. BOOL
dfda0 20 62 52 65 73 65 72 76 65 64 3b 20 20 20 20 20 bReserved;
dfdb0 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 72 /* Indicates a r
dfdc0 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 68 61 73 eserved lock has
dfdd0 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a been obtained *
dfde0 2f 0a 20 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73 /. BOOL bExclus
dfdf0 69 76 65 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63 ive; /* Indic
dfe00 61 74 65 73 20 61 6e 20 65 78 63 6c 75 73 69 76 ates an exclusiv
dfe10 65 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 e lock has been
dfe20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 7d 20 77 69 obtained */.} wi
dfe30 6e 63 65 4c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a nceLock;.#endif.
dfe40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 46 69 ./*.** The winFi
dfe50 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 le structure is
dfe60 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 a subclass of sq
dfe70 6c 69 74 65 33 5f 66 69 6c 65 2a 20 73 70 65 63 lite3_file* spec
dfe80 69 66 69 63 20 74 6f 20 74 68 65 20 77 69 6e 33 ific to the win3
dfe90 32 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 2.** portability
dfea0 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 layer..*/.typed
dfeb0 65 66 20 73 74 72 75 63 74 20 77 69 6e 46 69 6c ef struct winFil
dfec0 65 20 77 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63 e winFile;.struc
dfed0 74 20 77 69 6e 46 69 6c 65 20 7b 0a 20 20 63 6f t winFile {. co
dfee0 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d nst sqlite3_io_m
dfef0 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b ethods *pMethod;
dff00 2f 2a 20 4d 75 73 74 20 62 65 20 66 69 72 73 74 /* Must be first
dff10 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20 */. HANDLE h;
dff20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
dff30 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 Handle for acce
dff40 73 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a ssing the file *
dff50 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
dff60 72 20 6c 6f 63 6b 74 79 70 65 3b 20 2f 2a 20 54 r locktype; /* T
dff70 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 ype of lock curr
dff80 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 ently held on th
dff90 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 73 68 6f is file */. sho
dffa0 72 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 rt sharedLockByt
dffb0 65 3b 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79 e; /* Randomly
dffc0 20 63 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65 chosen byte use
dffd0 64 20 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f d as a shared lo
dffe0 63 6b 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 ck */.#if SQLITE
dfff0 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 57 43 48 41 _OS_WINCE. WCHA
e0000 52 20 2a 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 R *zDeleteOnClos
e0010 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 e; /* Name of f
e0020 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 77 68 ile to delete wh
e0030 65 6e 20 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 en closing */.
e0040 48 41 4e 44 4c 45 20 68 4d 75 74 65 78 3b 20 20 HANDLE hMutex;
e0050 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 /* Mutex
e0060 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c used to control
e0070 20 61 63 63 65 73 73 20 74 6f 20 73 68 61 72 65 access to share
e0080 64 20 6c 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41 d lock */ . HA
e0090 4e 44 4c 45 20 68 53 68 61 72 65 64 3b 20 20 20 NDLE hShared;
e00a0 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 /* Shared
e00b0 6d 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 75 memory segment u
e00c0 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 sed for locking
e00d0 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c */. winceLock l
e00e0 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 ocal; /*
e00f0 4c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 62 Locks obtained b
e0100 79 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65 20 y this instance
e0110 6f 66 20 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 of winFile */.
e0120 77 69 6e 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65 winceLock *share
e0130 64 3b 20 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61 d; /* Globa
e0140 6c 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65 l shared lock me
e0150 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c mory for the fil
e0160 65 20 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a e */.#endif.};.
e0170 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
e0180 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 owing variable i
e0190 73 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 s (normally) set
e01a0 20 6f 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 once and never
e01b0 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 changes.** there
e01c0 61 66 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 after. It recor
e01d0 64 73 20 77 68 65 74 68 65 72 20 74 68 65 20 6f ds whether the o
e01e0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
e01f0 69 73 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 is Win95.** or W
e0200 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 inNT..**.** 0:
e0210 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 Operating syste
e0220 6d 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a m unknown..** 1:
e0230 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 Operating sys
e0240 74 65 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a tem is Win95..**
e0250 20 32 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 2: Operating
e0260 73 79 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e system is WinNT.
e0270 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 .**.** In order
e0280 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 to facilitate te
e0290 73 74 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 sting on a WinNT
e02a0 20 73 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 system, the tes
e02b0 74 20 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e t fixture.** can
e02c0 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 manually set th
e02d0 69 73 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f is value to 1 to
e02e0 20 65 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 emulate Win98 b
e02f0 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 ehavior..*/.#ifd
e0300 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
e0310 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e0320 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 lite3_os_type =
e0330 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 0;.#else.static
e0340 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 int sqlite3_os_t
e0350 79 70 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a ype = 0;.#endif.
e0360 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
e0370 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 ue (non-zero) if
e0380 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 we are running
e0390 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e under WinNT, Win
e03a0 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 2K, WinXP,.** or
e03b0 20 57 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 WinCE. Return
e03c0 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 false (zero) for
e03d0 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f Win95, Win98, o
e03e0 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 r WinME..**.** H
e03f0 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 ere is an intere
e0400 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f sting observatio
e0410 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 n: Win95, Win98
e0420 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b , and WinME lack
e0430 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 .** the LockFile
e0440 45 78 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 Ex() API. But w
e0450 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 e can still stat
e0460 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 ically link agai
e0470 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 nst that.** API
e0480 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f as long as we do
e0490 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 n't call it win
e04a0 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 running Win95/98
e04b0 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a /ME. A call to.
e04c0 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ** this routine
e04d0 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 is used to deter
e04e0 6d 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 mine if the host
e04f0 20 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 is Win95/98/ME
e0500 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 or.** WinNT/2K/X
e0510 50 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c P so that we wil
e0520 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f l know whether o
e0530 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 r not we can saf
e0540 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 ely call.** the
e0550 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 LockFileEx() API
e0560 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
e0570 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e OS_WINCE.# defin
e0580 65 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 e isNT() (1).#e
e0590 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 lse. static int
e05a0 20 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 isNT(void){.
e05b0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f if( sqlite3_os_
e05c0 74 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 type==0 ){.
e05d0 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 OSVERSIONINFO s
e05e0 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 Info;. sInf
e05f0 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 o.dwOSVersionInf
e0600 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 oSize = sizeof(s
e0610 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 Info);. Get
e0620 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f VersionEx(&sInfo
e0630 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
e0640 5f 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f _os_type = sInfo
e0650 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 .dwPlatformId==V
e0660 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 ER_PLATFORM_WIN3
e0670 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 2_NT ? 2 : 1;.
e0680 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 }. return s
e0690 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d qlite3_os_type==
e06a0 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 2;. }.#endif /*
e06b0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
e06c0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 */../*.** Conve
e06d0 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e rt a UTF-8 strin
e06e0 67 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 g to microsoft u
e06f0 6e 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 nicode (UTF-16?)
e0700 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 . .**.** Space t
e0710 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 o hold the retur
e0720 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 ned string is ob
e0730 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c tained from mall
e0740 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 oc..*/.static WC
e0750 48 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f HAR *utf8ToUnico
e0760 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a de(const char *z
e0770 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 Filename){. int
e0780 20 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 nChar;. WCHAR
e0790 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a *zWideFilename;.
e07a0 0a 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 . nChar = Multi
e07b0 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 ByteToWideChar(C
e07c0 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 P_UTF8, 0, zFile
e07d0 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 name, -1, NULL,
e07e0 30 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 0);. zWideFilen
e07f0 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 ame = malloc( nC
e0800 68 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 har*sizeof(zWide
e0810 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a Filename[0]) );.
e0820 20 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e if( zWideFilen
e0830 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ame==0 ){. re
e0840 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 turn 0;. }. nC
e0850 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 har = MultiByteT
e0860 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 oWideChar(CP_UTF
e0870 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 8, 0, zFilename,
e0880 20 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 -1, zWideFilena
e0890 6d 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 me, nChar);. if
e08a0 28 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 ( nChar==0 ){.
e08b0 20 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 free(zWideFile
e08c0 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 name);. zWide
e08d0 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 Filename = 0;.
e08e0 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 }. return zWide
e08f0 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a Filename;.}../*.
e0900 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f ** Convert micro
e0910 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 soft unicode to
e0920 55 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f UTF-8. Space to
e0930 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e hold the return
e0940 65 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 ed string is.**
e0950 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
e0960 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 lloc()..*/.stati
e0970 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 c char *unicodeT
e0980 6f 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 oUtf8(const WCHA
e0990 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 R *zWideFilename
e09a0 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a ){. int nByte;.
e09b0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d char *zFilenam
e09c0 65 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 e;.. nByte = Wi
e09d0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 deCharToMultiByt
e09e0 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 e(CP_UTF8, 0, zW
e09f0 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c ideFilename, -1,
e0a00 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0, 0, 0, 0);.
e0a10 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c zFilename = mall
e0a20 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 oc( nByte );. i
e0a30 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 f( zFilename==0
e0a40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
e0a50 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 . }. nByte = W
e0a60 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 ideCharToMultiBy
e0a70 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a te(CP_UTF8, 0, z
e0a80 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 WideFilename, -1
e0a90 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 , zFilename, nBy
e0aa0 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 te,.
e0ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0ac0 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 0, 0);. if( n
e0ad0 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 Byte == 0 ){.
e0ae0 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 free(zFilename)
e0af0 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 ;. zFilename
e0b00 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
e0b10 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a n zFilename;.}..
e0b20 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e /*.** Convert an
e0b30 20 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 ansi string to
e0b40 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 microsoft unicod
e0b50 65 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a e, based on the.
e0b60 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 ** current codep
e0b70 61 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 age settings for
e0b80 20 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a file apis..** .
e0b90 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 ** Space to hold
e0ba0 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 the returned st
e0bb0 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 ring is obtained
e0bc0 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e .** from malloc.
e0bd0 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 .*/.static WCHAR
e0be0 20 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 *mbcsToUnicode(
e0bf0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
e0c00 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 ename){. int nB
e0c10 79 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d yte;. WCHAR *zM
e0c20 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 bcsFilename;. i
e0c30 6e 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 nt codepage = Ar
e0c40 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 eFileApisANSI()
e0c50 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 ? CP_ACP : CP_OE
e0c60 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 MCP;.. nByte =
e0c70 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 MultiByteToWideC
e0c80 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c har(codepage, 0,
e0c90 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 zFilename, -1,
e0ca0 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 NULL,0)*sizeof(W
e0cb0 43 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 CHAR);. zMbcsFi
e0cc0 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 lename = malloc(
e0cd0 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d nByte*sizeof(zM
e0ce0 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 bcsFilename[0])
e0cf0 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 );. if( zMbcsFi
e0d00 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 lename==0 ){.
e0d10 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
e0d20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 nByte = MultiBy
e0d30 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 teToWideChar(cod
e0d40 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e epage, 0, zFilen
e0d50 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 ame, -1, zMbcsFi
e0d60 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a lename, nByte);.
e0d70 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 if( nByte==0 )
e0d80 7b 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 {. free(zMbcs
e0d90 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a Filename);. z
e0da0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 MbcsFilename = 0
e0db0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
e0dc0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a MbcsFilename;.}.
e0dd0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d ./*.** Convert m
e0de0 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
e0df0 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 to multibyte ch
e0e00 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 aracter string,
e0e10 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 based on the.**
e0e20 75 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 user's Ansi code
e0e30 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 page..**.** Spac
e0e40 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 e to hold the re
e0e50 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 turned string is
e0e60 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
e0e70 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 * malloc()..*/.s
e0e80 74 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 tatic char *unic
e0e90 6f 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 odeToMbcs(const
e0ea0 57 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 WCHAR *zWideFile
e0eb0 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 name){. int nBy
e0ec0 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c te;. char *zFil
e0ed0 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 ename;. int cod
e0ee0 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 epage = AreFileA
e0ef0 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 pisANSI() ? CP_A
e0f00 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a CP : CP_OEMCP;..
e0f10 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 nByte = WideCh
e0f20 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f arToMultiByte(co
e0f30 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 depage, 0, zWide
e0f40 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c Filename, -1, 0,
e0f50 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 0, 0, 0);. zFi
e0f60 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 lename = malloc(
e0f70 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 nByte );. if(
e0f80 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a zFilename==0 ){.
e0f90 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
e0fa0 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 }. nByte = Wide
e0fb0 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 CharToMultiByte(
e0fc0 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 codepage, 0, zWi
e0fd0 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 deFilename, -1,
e0fe0 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 zFilename, nByte
e0ff0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
e1000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1010 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 0, 0);. if( nBy
e1020 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 te == 0 ){. f
e1030 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ree(zFilename);.
e1040 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 zFilename =
e1050 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
e1060 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a zFilename;.}../*
e1070 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 .** Convert mult
e1080 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 ibyte character
e1090 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e string to UTF-8.
e10a0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
e10b0 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 the.** returned
e10c0 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e string is obtain
e10d0 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ed from malloc()
e10e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
e10f0 20 2a 6d 62 63 73 54 6f 55 74 66 38 28 63 6f 6e *mbcsToUtf8(con
e1100 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
e1110 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 me){. char *zFi
e1120 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 lenameUtf8;. WC
e1130 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a HAR *zTmpWide;..
e1140 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 zTmpWide = mbc
e1150 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 sToUnicode(zFile
e1160 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d name);. if( zTm
e1170 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 pWide==0 ){.
e1180 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
e1190 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 zFilenameUtf8 =
e11a0 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 unicodeToUtf8(zT
e11b0 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 mpWide);. free(
e11c0 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 zTmpWide);. ret
e11d0 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 urn zFilenameUtf
e11e0 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 8;.}../*.** Conv
e11f0 65 72 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c ert UTF-8 to mul
e1200 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 tibyte character
e1210 20 73 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 string. Space
e1220 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 to hold the .**
e1230 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
e1240 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d is obtained from
e1250 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 malloc()..*/.st
e1260 61 74 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 atic char *utf8T
e1270 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 oMbcs(const char
e1280 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 *zFilename){.
e1290 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d char *zFilenameM
e12a0 62 63 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 bcs;. WCHAR *zT
e12b0 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 mpWide;.. zTmpW
e12c0 69 64 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 ide = utf8ToUnic
e12d0 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ode(zFilename);.
e12e0 20 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d if( zTmpWide==
e12f0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
e1300 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 0;. }. zFilena
e1310 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 meMbcs = unicode
e1320 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 ToMbcs(zTmpWide)
e1330 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 ;. free(zTmpWid
e1340 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 e);. return zFi
e1350 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 lenameMbcs;.}..#
e1360 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
e1370 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a CE./************
e1380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e13a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e13b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
e13c0 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f This section co
e13d0 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 ntains code for
e13e0 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f WinCE only..*/./
e13f0 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 *.** WindowsCE d
e1400 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c oes not have a l
e1410 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 ocaltime() funct
e1420 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 ion. So create
e1430 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e a.** substitute.
e1440 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f .*/.struct tm *_
e1450 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 _cdecl localtime
e1460 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 (const time_t *t
e1470 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 ).{. static str
e1480 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 uct tm y;. FILE
e1490 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 TIME uTm, lTm;.
e14a0 20 53 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b SYSTEMTIME pTm;
e14b0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
e14c0 20 74 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 t64;. t64 = *t
e14d0 3b 0a 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b ;. t64 = (t64 +
e14e0 20 31 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 11644473600)*10
e14f0 30 30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 000000;. uTm.dw
e1500 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d 20 74 36 LowDateTime = t6
e1510 34 20 26 20 30 78 46 46 46 46 46 46 46 46 3b 0a 4 & 0xFFFFFFFF;.
e1520 20 20 75 54 6d 2e 64 77 48 69 67 68 44 61 74 65 uTm.dwHighDate
e1530 54 69 6d 65 3d 20 74 36 34 20 3e 3e 20 33 32 3b Time= t64 >> 32;
e1540 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 . FileTimeToLoc
e1550 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c alFileTime(&uTm,
e1560 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d &lTm);. FileTim
e1570 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c eToSystemTime(&l
e1580 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d Tm,&pTm);. y.tm
e1590 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 _year = pTm.wYea
e15a0 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d r - 1900;. y.tm
e15b0 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 _mon = pTm.wMont
e15c0 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 h - 1;. y.tm_wd
e15d0 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 ay = pTm.wDayOfW
e15e0 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 eek;. y.tm_mday
e15f0 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 = pTm.wDay;. y
e1600 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 .tm_hour = pTm.w
e1610 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e Hour;. y.tm_min
e1620 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a = pTm.wMinute;.
e1630 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d y.tm_sec = pTm
e1640 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 .wSecond;. retu
e1650 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 rn &y;.}../* Thi
e1660 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 s will never be
e1670 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 69 called, but defi
e1680 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 ned to make the
e1690 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a code compile */.
e16a0 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 50 #define GetTempP
e16b0 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 69 athA(a,b)..#defi
e16c0 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c ne LockFile(a,b,
e16d0 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 6e c,d,e) win
e16e0 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 ceLockFile(&a, b
e16f0 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 , c, d, e).#defi
e1700 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c ne UnlockFile(a,
e1710 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 6e b,c,d,e) win
e1720 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c ceUnlockFile(&a,
e1730 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 b, c, d, e).#de
e1740 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 fine LockFileEx(
e1750 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 a,b,c,d,e,f) w
e1760 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 inceLockFileEx(&
e1770 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 a, b, c, d, e, f
e1780 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 4c )..#define HANDL
e1790 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20 E_TO_WINFILE(a)
e17a0 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 61 (winFile*)&((cha
e17b0 72 2a 29 61 29 5b 2d 6f 66 66 73 65 74 6f 66 28 r*)a)[-offsetof(
e17c0 77 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a winFile,h)]../*.
e17d0 2a 2a 20 41 63 71 75 69 72 65 20 61 20 6c 6f 63 ** Acquire a loc
e17e0 6b 20 6f 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 k on the handle
e17f0 68 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 h.*/.static void
e1800 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 winceMutexAcqui
e1810 72 65 28 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 re(HANDLE h){.
e1820 20 44 57 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 DWORD dwErr;.
e1830 20 64 6f 20 7b 0a 20 20 20 20 20 64 77 45 72 72 do {. dwErr
e1840 20 3d 20 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 = WaitForSingle
e1850 4f 62 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 Object(h, INFINI
e1860 54 45 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 TE);. } while
e1870 28 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f (dwErr != WAIT_O
e1880 42 4a 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 BJECT_0 && dwErr
e1890 20 21 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e != WAIT_ABANDON
e18a0 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c ED);.}./*.** Rel
e18b0 65 61 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 ease a lock acqu
e18c0 69 72 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 ired by winceMut
e18d0 65 78 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 exAcquire().*/.#
e18e0 64 65 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 define winceMute
e18f0 78 52 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 xRelease(h) Rele
e1900 61 73 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a aseMutex(h)../*.
e1910 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 ** Create the mu
e1920 74 65 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d tex and shared m
e1930 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c emory used for l
e1940 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 ocking in the fi
e1950 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 le.** descriptor
e1960 20 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 pFile.*/.static
e1970 20 42 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 BOOL winceCreat
e1980 65 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 eLock(const char
e1990 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e *zFilename, win
e19a0 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 File *pFile){.
e19b0 57 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 WCHAR *zTok;. W
e19c0 43 48 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 CHAR *zName = ut
e19d0 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c f8ToUnicode(zFil
e19e0 65 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 ename);. BOOL b
e19f0 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 Init = TRUE;..
e1a00 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 /* Initialize th
e1a10 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 e local lockdata
e1a20 20 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 */. ZeroMemory
e1a30 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 (&pFile->local,
e1a40 73 69 7a 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f sizeof(pFile->lo
e1a50 63 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 cal));.. /* Rep
e1a60 6c 61 63 65 20 74 68 65 20 62 61 63 6b 73 6c 61 lace the backsla
e1a70 73 68 65 73 20 66 72 6f 6d 20 74 68 65 20 66 69 shes from the fi
e1a80 6c 65 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 lename and lower
e1a90 63 61 73 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 case it. ** to
e1aa0 64 65 72 69 76 65 20 61 20 6d 75 74 65 78 20 6e derive a mutex n
e1ab0 61 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d ame. */. zTok =
e1ac0 20 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d CharLowerW(zNam
e1ad0 65 29 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f e);. for (;*zTo
e1ae0 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 k;zTok++){. i
e1af0 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 f (*zTok == '\\'
e1b00 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 ) *zTok = '_';.
e1b10 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f }.. /* Create/
e1b20 6f 70 65 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d open the named m
e1b30 75 74 65 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d utex */. pFile-
e1b40 3e 68 4d 75 74 65 78 20 3d 20 43 72 65 61 74 65 >hMutex = Create
e1b50 4d 75 74 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c MutexW(NULL, FAL
e1b60 53 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 SE, zName);. if
e1b70 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 (!pFile->hMutex
e1b80 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 4e 61 6d ){. free(zNam
e1b90 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 46 e);. return F
e1ba0 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ALSE;. }.. /*
e1bb0 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74 65 Acquire the mute
e1bc0 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 x before continu
e1bd0 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 ing */. winceMu
e1be0 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 texAcquire(pFile
e1bf0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a 20 20 ->hMutex);. .
e1c00 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e 61 6d /* Since the nam
e1c10 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75 74 65 es of named mute
e1c20 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65 73 2c xes, semaphores,
e1c30 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73 20 65 file mappings e
e1c40 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63 61 73 tc are . ** cas
e1c50 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74 61 6b e-sensitive, tak
e1c60 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74 e advantage of t
e1c70 68 61 74 20 62 79 20 75 70 70 65 72 63 61 73 69 hat by uppercasi
e1c80 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e 61 6d ng the mutex nam
e1c90 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69 6e 67 e. ** and using
e1ca0 20 74 68 61 74 20 61 73 20 74 68 65 20 73 68 61 that as the sha
e1cb0 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e 67 20 red filemapping
e1cc0 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 name.. */. Cha
e1cd0 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a rUpperW(zName);.
e1ce0 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 pFile->hShared
e1cf0 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d 61 70 = CreateFileMap
e1d00 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 pingW(INVALID_HA
e1d10 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c NDLE_VALUE, NULL
e1d20 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
e1d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d40 20 20 20 20 20 20 20 20 20 50 41 47 45 5f 52 45 PAGE_RE
e1d50 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a 65 ADWRITE, 0, size
e1d60 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 of(winceLock),.
e1d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d90 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 20 0a zName); .
e1da0 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c 61 67 . /* Set a flag
e1db0 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 that indicates
e1dc0 77 65 27 72 65 20 74 68 65 20 66 69 72 73 74 20 we're the first
e1dd0 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6d 65 to create the me
e1de0 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 2a 2a mory so it . **
e1df0 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 69 6e must be zero-in
e1e00 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 itialized */. i
e1e10 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f 72 28 f (GetLastError(
e1e20 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 45 41 ) == ERROR_ALREA
e1e30 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20 20 20 DY_EXISTS){.
e1e40 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b 0a 20 bInit = FALSE;.
e1e50 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61 6d 65 }.. free(zName
e1e60 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 73 );.. /* If we s
e1e70 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61 6b 69 ucceeded in maki
e1e80 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 ng the shared me
e1e90 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d 61 70 mory handle, map
e1ea0 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 it. */. if (pF
e1eb0 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b 0a 20 ile->hShared){.
e1ec0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
e1ed0 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d = (winceLock*)M
e1ee0 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 apViewOfFile(pFi
e1ef0 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a 20 20 le->hShared, .
e1f00 20 20 20 20 20 20 20 20 20 20 20 46 49 4c 45 5f FILE_
e1f10 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41 MAP_READ|FILE_MA
e1f20 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c 20 73 P_WRITE, 0, 0, s
e1f30 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 izeof(winceLock)
e1f40 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 61 70 );. /* If map
e1f50 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f ping failed, clo
e1f60 73 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 se the shared me
e1f70 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e 64 20 mory handle and
e1f80 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 erase it */.
e1f90 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68 61 72 if (!pFile->shar
e1fa0 65 64 29 7b 0a 20 20 20 20 20 20 43 6c 6f 73 65 ed){. Close
e1fb0 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 Handle(pFile->hS
e1fc0 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 46 hared);. pF
e1fd0 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e ile->hShared = N
e1fe0 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ULL;. }. }..
e1ff0 20 20 2f 2a 20 49 66 20 73 68 61 72 65 64 20 6d /* If shared m
e2000 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 emory could not
e2010 62 65 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e be created, then
e2020 20 63 6c 6f 73 65 20 74 68 65 20 6d 75 74 65 78 close the mutex
e2030 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 and fail */. i
e2040 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 f (pFile->hShare
e2050 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 d == NULL){.
e2060 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 winceMutexReleas
e2070 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 e(pFile->hMutex)
e2080 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c ;. CloseHandl
e2090 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 e(pFile->hMutex)
e20a0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 ;. pFile->hMu
e20b0 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 tex = NULL;.
e20c0 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 return FALSE;.
e20d0 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 }. . /* Initia
e20e0 6c 69 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 lize the shared
e20f0 6d 65 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 memory if we're
e2100 73 75 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 supposed to */.
e2110 20 69 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 if (bInit) {.
e2120 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 ZeroMemory(pFi
e2130 6c 65 2d 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 le->shared, size
e2140 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a of(winceLock));.
e2150 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 }.. winceMute
e2160 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e xRelease(pFile->
e2170 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 hMutex);. retur
e2180 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n TRUE;.}../*.**
e2190 20 44 65 73 74 72 6f 79 20 74 68 65 20 70 61 72 Destroy the par
e21a0 74 20 6f 66 20 77 69 6e 46 69 6c 65 20 74 68 61 t of winFile tha
e21b0 74 20 64 65 61 6c 73 20 77 69 74 68 20 77 69 6e t deals with win
e21c0 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 ce locks.*/.stat
e21d0 69 63 20 76 6f 69 64 20 77 69 6e 63 65 44 65 73 ic void winceDes
e21e0 74 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 troyLock(winFile
e21f0 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 *pFile){. if (
e2200 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a pFile->hMutex){.
e2210 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 /* Acquire t
e2220 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 he mutex */.
e2230 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 winceMutexAcquir
e2240 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 e(pFile->hMutex)
e2250 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f ;.. /* The fo
e2260 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 llowing blocks s
e2270 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 hould probably a
e2280 73 73 65 72 74 20 69 6e 20 64 65 62 75 67 20 6d ssert in debug m
e2290 6f 64 65 2c 20 62 75 74 20 74 68 65 79 0a 20 20 ode, but they.
e22a0 20 20 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61 are to clea
e22b0 6e 75 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20 nup in case any
e22c0 6c 6f 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f locks remained o
e22d0 70 65 6e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 pen */. if (p
e22e0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 File->local.nRea
e22f0 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 ders){. pFi
e2300 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 le->shared->nRea
e2310 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 ders --;. }.
e2320 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f if (pFile->lo
e2330 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 7b 0a cal.bReserved){.
e2340 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
e2350 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d red->bReserved =
e2360 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 FALSE;. }.
e2370 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
e2380 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 al.bPending){.
e2390 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
e23a0 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 d->bPending = FA
e23b0 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 LSE;. }. i
e23c0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
e23d0 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 bExclusive){.
e23e0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
e23f0 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 ->bExclusive = F
e2400 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ALSE;. }..
e2410 20 2f 2a 20 44 65 2d 72 65 66 65 72 65 6e 63 65 /* De-reference
e2420 20 61 6e 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 and close our c
e2430 6f 70 79 20 6f 66 20 74 68 65 20 73 68 61 72 65 opy of the share
e2440 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 d memory handle
e2450 2a 2f 0a 20 20 20 20 55 6e 6d 61 70 56 69 65 77 */. UnmapView
e2460 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 OfFile(pFile->sh
e2470 61 72 65 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 ared);. Close
e2480 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 Handle(pFile->hS
e2490 68 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 hared);.. /*
e24a0 44 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75 Done with the mu
e24b0 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 tex */. wince
e24c0 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 MutexRelease(pFi
e24d0 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 le->hMutex);
e24e0 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 . CloseHandle
e24f0 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
e2500 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 . pFile->hMut
e2510 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d ex = NULL;. }.}
e2520 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c ../* .** An impl
e2530 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
e2540 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 e LockFile() API
e2550 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 of windows for
e2560 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 wince.*/.static
e2570 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 BOOL winceLockFi
e2580 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 le(. HANDLE *ph
e2590 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 File,. DWORD dw
e25a0 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 FileOffsetLow,.
e25b0 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 DWORD dwFileOff
e25c0 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 setHigh,. DWORD
e25d0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
e25e0 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 oLockLow,. DWOR
e25f0 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 D nNumberOfBytes
e2600 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 ToLockHigh.){.
e2610 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
e2620 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 HANDLE_TO_WINFI
e2630 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f LE(phFile);. BO
e2640 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c OL bReturn = FAL
e2650 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c SE;.. if (!pFil
e2660 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 e->hMutex) retur
e2670 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d n TRUE;. winceM
e2680 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c utexAcquire(pFil
e2690 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f e->hMutex);.. /
e26a0 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 * Wanting an exc
e26b0 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a lusive lock? */.
e26c0 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 if (dwFileOffs
e26d0 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f etLow == SHARED_
e26e0 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 20 FIRST. &&
e26f0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
e2700 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 LockLow == SHARE
e2710 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 D_SIZE){. if
e2720 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e (pFile->shared->
e2730 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26 nReaders == 0 &&
e2740 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
e2750 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 bExclusive == 0)
e2760 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e {. pFile->
e2770 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 shared->bExclusi
e2780 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 ve = TRUE;.
e2790 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 pFile->local.b
e27a0 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45 Exclusive = TRUE
e27b0 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75 72 6e ;. bReturn
e27c0 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
e27d0 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 }.. /* Want a
e27e0 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 read-only lock?
e27f0 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 28 64 */. else if ((d
e2800 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e wFileOffsetLow >
e2810 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 = SHARED_FIRST &
e2820 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 77 &. dw
e2830 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3c 20 FileOffsetLow <
e2840 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 53 SHARED_FIRST + S
e2850 48 41 52 45 44 5f 53 49 5a 45 29 20 26 26 0a 20 HARED_SIZE) &&.
e2860 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62 nNumb
e2870 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
e2880 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 ow == 1){. if
e2890 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d (pFile->shared-
e28a0 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 >bExclusive == 0
e28b0 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
e28c0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b local.nReaders +
e28d0 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69 +;. if (pFi
e28e0 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 le->local.nReade
e28f0 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20 rs == 1){.
e2900 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
e2910 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 >nReaders ++;.
e2920 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 }. bRet
e2930 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
e2940 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 }. }.. /* Want
e2950 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f a pending lock?
e2960 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
e2970 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
e2980 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 = PENDING_BYTE &
e2990 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 & nNumberOfBytes
e29a0 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b ToLockLow == 1){
e29b0 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 . /* If no pe
e29c0 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 nding lock has b
e29d0 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68 een acquired, th
e29e0 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f en acquire it */
e29f0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
e2a00 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 shared->bPending
e2a10 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 == 0) {. p
e2a20 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 File->shared->bP
e2a30 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 ending = TRUE;.
e2a40 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
e2a50 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 l.bPending = TRU
e2a60 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
e2a70 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
e2a80 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 }. /* Want a r
e2a90 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f eserved lock? */
e2aa0 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 . else if (dwFi
e2ab0 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52 leOffsetLow == R
e2ac0 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 ESERVED_BYTE &&
e2ad0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
e2ae0 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 LockLow == 1){.
e2af0 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 if (pFile->sh
e2b00 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 ared->bReserved
e2b10 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 == 0) {. pF
e2b20 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 ile->shared->bRe
e2b30 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 served = TRUE;.
e2b40 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
e2b50 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 l.bReserved = TR
e2b60 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 UE;. bRetur
e2b70 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a n = TRUE;. }.
e2b80 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 }.. winceMute
e2b90 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e xRelease(pFile->
e2ba0 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 hMutex);. retur
e2bb0 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a n bReturn;.}../*
e2bc0 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 .** An implement
e2bd0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c ation of the Unl
e2be0 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77 ockFile API of w
e2bf0 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 indows for wince
e2c00 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 .*/.static BOOL
e2c10 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 winceUnlockFile(
e2c20 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c . HANDLE *phFil
e2c30 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c e,. DWORD dwFil
e2c40 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 eOffsetLow,. DW
e2c50 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 ORD dwFileOffset
e2c60 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e High,. DWORD nN
e2c70 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
e2c80 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 lockLow,. DWORD
e2c90 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
e2ca0 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 oUnlockHigh.){.
e2cb0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
e2cc0 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 = HANDLE_TO_WINF
e2cd0 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 ILE(phFile);. B
e2ce0 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 OOL bReturn = FA
e2cf0 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 LSE;.. if (!pFi
e2d00 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 le->hMutex) retu
e2d10 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 rn TRUE;. wince
e2d20 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 MutexAcquire(pFi
e2d30 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 le->hMutex);..
e2d40 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 /* Releasing a r
e2d50 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e eader lock or an
e2d60 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
e2d70 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f */. if (dwFileO
e2d80 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 48 41 52 ffsetLow >= SHAR
e2d90 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 ED_FIRST &&.
e2da0 20 20 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c dwFileOffsetL
e2db0 6f 77 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53 ow < SHARED_FIRS
e2dc0 54 20 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29 T + SHARED_SIZE)
e2dd0 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 {. /* Did we
e2de0 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 have an exclusiv
e2df0 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 e lock? */. i
e2e00 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
e2e10 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 bExclusive){.
e2e20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e pFile->local.
e2e30 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c bExclusive = FAL
e2e40 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d SE;. pFile-
e2e50 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 >shared->bExclus
e2e60 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ive = FALSE;.
e2e70 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 bReturn = TRU
e2e80 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a E;. }.. /*
e2e90 20 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 Did we just hav
e2ea0 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f e a reader lock?
e2eb0 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 */. else if
e2ec0 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 (pFile->local.nR
e2ed0 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70 eaders){. p
e2ee0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 File->local.nRea
e2ef0 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 ders --;. i
e2f00 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
e2f10 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 nReaders == 0).
e2f20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 {. p
e2f30 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 File->shared->nR
e2f40 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 eaders --;.
e2f50 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e }. bReturn
e2f60 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
e2f70 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 }.. /* Releasi
e2f80 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 ng a pending loc
e2f90 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 k */. else if (
e2fa0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
e2fb0 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 == PENDING_BYTE
e2fc0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 && nNumberOfByte
e2fd0 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 sToUnlockLow ==
e2fe0 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 1){. if (pFil
e2ff0 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e e->local.bPendin
e3000 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d g){. pFile-
e3010 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 >local.bPending
e3020 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 = FALSE;. p
e3030 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 File->shared->bP
e3040 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a ending = FALSE;.
e3050 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
e3060 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
e3070 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 /* Releasing a
e3080 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a reserved lock *
e3090 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 /. else if (dwF
e30a0 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 ileOffsetLow ==
e30b0 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 RESERVED_BYTE &&
e30c0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
e30d0 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 oUnlockLow == 1)
e30e0 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d {. if (pFile-
e30f0 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 >local.bReserved
e3100 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ) {. pFile-
e3110 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 >local.bReserved
e3120 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 = FALSE;.
e3130 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
e3140 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 Reserved = FALSE
e3150 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 ;. bReturn
e3160 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
e3170 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 }.. winceMutexR
e3180 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d elease(pFile->hM
e3190 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
e31a0 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a bReturn;.}../*.*
e31b0 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 * An implementat
e31c0 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 ion of the LockF
e31d0 69 6c 65 45 78 28 29 20 41 50 49 20 6f 66 20 77 ileEx() API of w
e31e0 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 indows for wince
e31f0 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 .*/.static BOOL
e3200 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 winceLockFileEx(
e3210 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c . HANDLE *phFil
e3220 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 e,. DWORD dwFla
e3230 67 73 2c 0a 20 20 44 57 4f 52 44 20 64 77 52 65 gs,. DWORD dwRe
e3240 73 65 72 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 served,. DWORD
e3250 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
e3260 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 LockLow,. DWORD
e3270 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
e3280 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f oLockHigh,. LPO
e3290 56 45 52 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 VERLAPPED lpOver
e32a0 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f 2a 20 49 lapped.){. /* I
e32b0 66 20 74 68 65 20 63 61 6c 6c 65 72 20 77 61 6e f the caller wan
e32c0 74 73 20 61 20 73 68 61 72 65 64 20 72 65 61 64 ts a shared read
e32d0 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 64 20 74 lock, forward t
e32e0 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f his call. ** to
e32f0 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a winceLockFile *
e3300 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 72 6c 61 /. if (lpOverla
e3310 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 pped->Offset ==
e3320 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a SHARED_FIRST &&.
e3330 20 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d dwFlags ==
e3340 20 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d 1 &&. nNum
e3350 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b berOfBytesToLock
e3360 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53 49 Low == SHARED_SI
e3370 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ZE){. return
e3380 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70 68 winceLockFile(ph
e3390 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 File, SHARED_FIR
e33a0 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 ST, 0, 1, 0);.
e33b0 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 }. return FALSE
e33c0 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f 66 ;.}./*.** End of
e33d0 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 64 the special cod
e33e0 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a 2a e for wince.****
e33f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 *********/.#endi
e3440 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 f /* SQLITE_OS_W
e3450 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a INCE */../******
e3460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e34a0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e *******.** The n
e34b0 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 ext group of rou
e34c0 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 tines implement
e34d0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 the I/O methods
e34e0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 specified.** by
e34f0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d the sqlite3_io_m
e3500 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a ethods object..*
e3510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
e3560 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
e3570 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 le..**.** It is
e3580 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 61 6e reported that an
e3590 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c 6f 73 attempt to clos
e35a0 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 68 74 e a handle might
e35b0 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 66 61 sometimes.** fa
e35c0 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 20 76 il. This is a v
e35d0 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 ery unreasonable
e35e0 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77 69 6e result, but win
e35f0 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f 75 dows is notoriou
e3600 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20 75 s.** for being u
e3610 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20 49 nreasonable so I
e3620 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74 68 do not doubt th
e3630 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 70 70 at it might happ
e3640 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 63 en. If.** the c
e3650 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20 70 lose fails, we p
e3660 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69 6c ause for 100 mil
e3670 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 liseconds and tr
e3680 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a 2a 20 y again. As.**
e3690 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53 45 many as MX_CLOSE
e36a0 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70 74 _ATTEMPT attempt
e36b0 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 68 s to close the h
e36c0 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20 62 andle are made b
e36d0 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e 67 20 efore.** giving
e36e0 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 up and returning
e36f0 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 an error..*/.#d
e3700 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f 41 efine MX_CLOSE_A
e3710 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 63 20 TTEMPT 3.static
e3720 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 71 6c int winClose(sql
e3730 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
e3740 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d 20 int rc, cnt =
e3750 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 0;. winFile *pF
e3760 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 ile = (winFile*)
e3770 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 id;. OSTRACE2("
e3780 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 CLOSE %d\n", pFi
e3790 6c 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b 0a 20 20 le->h);. do{.
e37a0 20 20 72 63 20 3d 20 43 6c 6f 73 65 48 61 6e 64 rc = CloseHand
e37b0 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 le(pFile->h);.
e37c0 7d 77 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 }while( rc==0 &&
e37d0 20 2b 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 ++cnt < MX_CLOS
e37e0 45 5f 41 54 54 45 4d 50 54 20 26 26 20 28 53 6c E_ATTEMPT && (Sl
e37f0 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a eep(100), 1) );.
e3800 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
e3810 4e 43 45 0a 23 64 65 66 69 6e 65 20 57 49 4e 43 NCE.#define WINC
e3820 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d E_DELETION_ATTEM
e3830 50 54 53 20 33 0a 20 20 77 69 6e 63 65 44 65 73 PTS 3. winceDes
e3840 74 72 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b troyLock(pFile);
e3850 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44 . if( pFile->zD
e3860 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a eleteOnClose ){.
e3870 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b int cnt = 0;
e3880 0a 20 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20 . while(.
e3890 20 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c DeleteFil
e38a0 65 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 eW(pFile->zDelet
e38b0 65 4f 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 eOnClose)==0.
e38c0 20 20 20 20 20 26 26 20 47 65 74 46 69 6c 65 41 && GetFileA
e38d0 74 74 72 69 62 75 74 65 73 57 28 70 46 69 6c 65 ttributesW(pFile
e38e0 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 ->zDeleteOnClose
e38f0 29 21 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20 )!=0xffffffff .
e3900 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 && cnt++
e3910 3c 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e < WINCE_DELETION
e3920 5f 41 54 54 45 4d 50 54 53 0a 20 20 20 20 29 7b _ATTEMPTS. ){
e3930 0a 20 20 20 20 20 20 20 53 6c 65 65 70 28 31 30 . Sleep(10
e3940 30 29 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 0); /* Wait a l
e3950 69 74 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79 ittle before try
e3960 69 6e 67 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 ing again */.
e3970 20 7d 0a 20 20 20 20 66 72 65 65 28 70 46 69 6c }. free(pFil
e3980 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 e->zDeleteOnClos
e3990 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 e);. }.#endif.
e39a0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 OpenCounter(-1)
e39b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3f 20 ;. return rc ?
e39c0 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 SQLITE_OK : SQLI
e39d0 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a TE_IOERR;.}../*.
e39e0 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 ** Some microsof
e39f0 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b t compilers lack
e3a00 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e this definition
e3a10 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 ..*/.#ifndef INV
e3a20 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f ALID_SET_FILE_PO
e3a30 49 4e 54 45 52 0a 23 20 64 65 66 69 6e 65 20 49 INTER.# define I
e3a40 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f NVALID_SET_FILE_
e3a50 50 4f 49 4e 54 45 52 20 28 28 44 57 4f 52 44 29 POINTER ((DWORD)
e3a60 2d 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a -1).#endif../*.*
e3a70 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d * Read data from
e3a80 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 a file into a b
e3a90 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 uffer. Return S
e3aa0 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a QLITE_OK if all.
e3ab0 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 ** bytes were re
e3ac0 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ad successfully
e3ad0 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 and SQLITE_IOERR
e3ae0 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 if anything goe
e3af0 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 s.** wrong..*/.s
e3b00 74 61 74 69 63 20 69 6e 74 20 77 69 6e 52 65 61 tatic int winRea
e3b10 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c d(. sqlite3_fil
e3b20 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 e *id,
e3b30 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 /* File to read
e3b40 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a from */. void *
e3b50 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 pBuf,
e3b60 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f /* Write co
e3b70 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 ntent into this
e3b80 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 buffer */. int
e3b90 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 amt,
e3ba0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
e3bb0 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
e3bc0 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 d */. sqlite3_i
e3bd0 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 nt64 offset
e3be0 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 /* Begin readi
e3bf0 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 ng at this offse
e3c00 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 t */.){. LONG u
e3c10 70 70 65 72 42 69 74 73 20 3d 20 28 6f 66 66 73 pperBits = (offs
e3c20 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 et>>32) & 0x7fff
e3c30 66 66 66 66 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 ffff;. LONG low
e3c40 65 72 42 69 74 73 20 3d 20 6f 66 66 73 65 74 20 erBits = offset
e3c50 26 20 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 & 0xffffffff;.
e3c60 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 DWORD rc;. DWOR
e3c70 44 20 67 6f 74 3b 0a 20 20 77 69 6e 46 69 6c 65 D got;. winFile
e3c80 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
e3c90 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 le*)id;. assert
e3ca0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d ( id!=0 );. Sim
e3cb0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 ulateIOError(ret
e3cc0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
e3cd0 5f 52 45 41 44 29 3b 0a 20 20 4f 53 54 52 41 43 _READ);. OSTRAC
e3ce0 45 33 28 22 52 45 41 44 20 25 64 20 6c 6f 63 6b E3("READ %d lock
e3cf0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
e3d00 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
e3d10 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 e);. rc = SetFi
e3d20 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d lePointer(pFile-
e3d30 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 >h, lowerBits, &
e3d40 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f upperBits, FILE_
e3d50 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 BEGIN);. if( rc
e3d60 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 ==INVALID_SET_FI
e3d70 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 47 65 LE_POINTER && Ge
e3d80 74 4c 61 73 74 45 72 72 6f 72 28 29 21 3d 4e 4f tLastError()!=NO
e3d90 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 _ERROR ){. re
e3da0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
e3db0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 65 61 ;. }. if( !Rea
e3dc0 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 dFile(pFile->h,
e3dd0 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c pBuf, amt, &got,
e3de0 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 0) ){. retur
e3df0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 n SQLITE_IOERR_R
e3e00 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 EAD;. }. if( g
e3e10 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74 20 29 ot==(DWORD)amt )
e3e20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
e3e30 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
e3e40 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 . /* Unread p
e3e50 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66 66 arts of the buff
e3e60 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d er must be zero-
e3e70 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 filled */. me
e3e80 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 mset(&((char*)pB
e3e90 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 uf)[got], 0, amt
e3ea0 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 -got);. retur
e3eb0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 n SQLITE_IOERR_S
e3ec0 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d HORT_READ;. }.}
e3ed0 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
e3ee0 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 ta from a buffer
e3ef0 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 into a file. R
e3f00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
e3f10 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 on success.** or
e3f20 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f some other erro
e3f30 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 r code on failur
e3f40 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
e3f50 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 71 6c winWrite(. sql
e3f60 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 ite3_file *id,
e3f70 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 /* File t
e3f80 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a o write into */.
e3f90 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 const void *pB
e3fa0 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 uf, /* T
e3fb0 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 20 77 he bytes to be w
e3fc0 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 ritten */. int
e3fd0 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 amt,
e3fe0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
e3ff0 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 of bytes to writ
e4000 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 e */. sqlite3_i
e4010 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 nt64 offset
e4020 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
e4030 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69 the file to begi
e4040 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a n writing at */.
e4050 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 ){. LONG upperB
e4060 69 74 73 20 3d 20 28 6f 66 66 73 65 74 3e 3e 33 its = (offset>>3
e4070 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 2) & 0x7fffffff;
e4080 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 . LONG lowerBit
e4090 73 20 3d 20 6f 66 66 73 65 74 20 26 20 30 78 66 s = offset & 0xf
e40a0 66 66 66 66 66 66 66 3b 0a 20 20 44 57 4f 52 44 fffffff;. DWORD
e40b0 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 77 72 6f rc;. DWORD wro
e40c0 74 65 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 te;. winFile *p
e40d0 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
e40e0 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 )id;. assert( i
e40f0 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 d!=0 );. Simula
e4100 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e teIOError(return
e4110 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 SQLITE_IOERR_WR
e4120 49 54 45 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 ITE);. Simulate
e4130 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 72 65 DiskfullError(re
e4140 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
e4150 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 57 );. OSTRACE3("W
e4160 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c RITE %d lock=%d\
e4170 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 n", pFile->h, pF
e4180 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a ile->locktype);.
e4190 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f rc = SetFilePo
e41a0 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 inter(pFile->h,
e41b0 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 lowerBits, &uppe
e41c0 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 rBits, FILE_BEGI
e41d0 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e N);. if( rc==IN
e41e0 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 VALID_SET_FILE_P
e41f0 4f 49 4e 54 45 52 20 26 26 20 47 65 74 4c 61 73 OINTER && GetLas
e4200 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f 45 52 52 tError()!=NO_ERR
e4210 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OR ){. return
e4220 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 SQLITE_FULL;.
e4230 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e }. assert( amt>
e4240 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 0 );. while(.
e4250 20 20 20 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 amt>0. &&
e4260 20 28 72 63 20 3d 20 57 72 69 74 65 46 69 6c 65 (rc = WriteFile
e4270 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c (pFile->h, pBuf,
e4280 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 amt, &wrote, 0)
e4290 29 21 3d 30 0a 20 20 20 20 20 26 26 20 77 72 6f )!=0. && wro
e42a0 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d te>0. ){. am
e42b0 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 t -= wrote;.
e42c0 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 pBuf = &((char*)
e42d0 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 pBuf)[wrote];.
e42e0 7d 0a 20 20 69 66 28 20 21 72 63 20 7c 7c 20 61 }. if( !rc || a
e42f0 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65 20 29 7b mt>(int)wrote ){
e4300 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
e4310 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 72 TE_FULL;. }. r
e4320 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e4330 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 .}../*.** Trunca
e4340 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 te an open file
e4350 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 73 to a specified s
e4360 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ize.*/.static in
e4370 74 20 77 69 6e 54 72 75 6e 63 61 74 65 28 73 71 t winTruncate(sq
e4380 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
e4390 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 42 sqlite3_int64 nB
e43a0 79 74 65 29 7b 0a 20 20 44 57 4f 52 44 20 72 63 yte){. DWORD rc
e43b0 3b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 ;. LONG upperBi
e43c0 74 73 20 3d 20 28 6e 42 79 74 65 3e 3e 33 32 29 ts = (nByte>>32)
e43d0 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20 & 0x7fffffff;.
e43e0 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 LONG lowerBits
e43f0 3d 20 6e 42 79 74 65 20 26 20 30 78 66 66 66 66 = nByte & 0xffff
e4400 66 66 66 66 3b 0a 20 20 77 69 6e 46 69 6c 65 20 ffff;. winFile
e4410 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
e4420 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 e*)id;. OSTRACE
e4430 33 28 22 54 52 55 4e 43 41 54 45 20 25 64 20 25 3("TRUNCATE %d %
e4440 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 lld\n", pFile->h
e4450 2c 20 6e 42 79 74 65 29 3b 0a 20 20 53 69 6d 75 , nByte);. Simu
e4460 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 lateIOError(retu
e4470 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
e4480 54 52 55 4e 43 41 54 45 29 3b 0a 20 20 72 63 20 TRUNCATE);. rc
e4490 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 = SetFilePointer
e44a0 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 (pFile->h, lower
e44b0 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 Bits, &upperBits
e44c0 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 , FILE_BEGIN);.
e44d0 20 69 66 28 20 49 4e 56 41 4c 49 44 5f 53 45 54 if( INVALID_SET
e44e0 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 21 3d _FILE_POINTER !=
e44f0 20 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 rc ){. /* Se
e4500 74 45 6e 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20 tEndOfFile will
e4510 66 61 69 6c 20 69 66 20 6e 42 79 74 65 20 69 73 fail if nByte is
e4520 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 20 negative */.
e4530 20 69 66 28 20 53 65 74 45 6e 64 4f 66 46 69 6c if( SetEndOfFil
e4540 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 e(pFile->h) ){.
e4550 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
e4560 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
e4570 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
e4580 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b _IOERR_TRUNCATE;
e4590 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .}..#ifdef SQLIT
e45a0 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 E_TEST./*.** Cou
e45b0 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 nt the number of
e45c0 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e fullsyncs and n
e45d0 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 ormal syncs. Th
e45e0 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 is is used to te
e45f0 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 st.** that syncs
e4600 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 and fullsyncs a
e4610 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74 re occuring at t
e4620 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a he right times..
e4630 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
e4640 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 t sqlite3_sync_c
e4650 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 ount = 0;.SQLITE
e4660 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
e4670 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 _fullsync_count
e4680 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
e4690 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c ** Make sure all
e46a0 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 writes to a par
e46b0 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 ticular file are
e46c0 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 committed to di
e46d0 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e sk..*/.static in
e46e0 74 20 77 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 t winSync(sqlite
e46f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
e4700 66 6c 61 67 73 29 7b 0a 20 20 77 69 6e 46 69 6c flags){. winFil
e4710 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 e *pFile = (winF
e4720 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 ile*)id;. OSTRA
e4730 43 45 33 28 22 53 59 4e 43 20 25 64 20 6c 6f 63 CE3("SYNC %d loc
e4740 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e k=%d\n", pFile->
e4750 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
e4760 70 65 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 pe);.#ifdef SQLI
e4770 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c TE_TEST. if( fl
e4780 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e ags & SQLITE_SYN
e4790 43 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 C_FULL ){. sq
e47a0 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 lite3_fullsync_c
e47b0 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 ount++;. }. sq
e47c0 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 lite3_sync_count
e47d0 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 ++;.#endif. if(
e47e0 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 FlushFileBuffer
e47f0 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 s(pFile->h) ){.
e4800 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e4810 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
e4820 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e4830 49 4f 45 52 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a IOERR;. }.}../*
e4840 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 .** Determine th
e4850 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
e4860 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 f a file in byte
e4870 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 s.*/.static int
e4880 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 winFileSize(sqli
e4890 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 te3_file *id, sq
e48a0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 lite3_int64 *pSi
e48b0 7a 65 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a ze){. winFile *
e48c0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
e48d0 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 75 70 *)id;. DWORD up
e48e0 70 65 72 42 69 74 73 2c 20 6c 6f 77 65 72 42 69 perBits, lowerBi
e48f0 74 73 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f ts;. SimulateIO
e4900 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c Error(return SQL
e4910 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 ITE_IOERR_FSTAT)
e4920 3b 0a 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 ;. lowerBits =
e4930 47 65 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c GetFileSize(pFil
e4940 65 2d 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 e->h, &upperBits
e4950 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 );. *pSize = ((
e4960 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 (sqlite3_int64)u
e4970 70 70 65 72 42 69 74 73 29 3c 3c 33 32 29 20 2b pperBits)<<32) +
e4980 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 72 65 lowerBits;. re
e4990 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e49a0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c }../*.** LOCKFIL
e49b0 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 E_FAIL_IMMEDIATE
e49c0 4c 59 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 LY is undefined
e49d0 6f 6e 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20 on some Windows
e49e0 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e systems..*/.#ifn
e49f0 64 65 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 def LOCKFILE_FAI
e4a00 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20 L_IMMEDIATELY.#
e4a10 64 65 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f define LOCKFILE_
e4a20 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 FAIL_IMMEDIATELY
e4a30 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1.#endif../*.**
e4a40 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 65 Acquire a reade
e4a50 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65 r lock..** Diffe
e4a60 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69 6e 65 rent API routine
e4a70 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64 65 70 s are called dep
e4a80 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
e4a90 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a r or not this.**
e4aa0 20 69 73 20 57 69 6e 39 35 20 6f 72 20 57 69 6e is Win95 or Win
e4ab0 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e NT..*/.static in
e4ac0 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 77 69 t getReadLock(wi
e4ad0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 nFile *pFile){.
e4ae0 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 20 int res;. if(
e4af0 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f 56 isNT() ){. OV
e4b00 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20 ERLAPPED ovlp;.
e4b10 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d ovlp.Offset =
e4b20 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 SHARED_FIRST;.
e4b30 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 ovlp.OffsetHi
e4b40 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70 gh = 0;. ovlp
e4b50 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 .hEvent = 0;.
e4b60 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45 res = LockFileE
e4b70 78 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b x(pFile->h, LOCK
e4b80 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 FILE_FAIL_IMMEDI
e4b90 41 54 45 4c 59 2c 0a 20 20 20 20 20 20 20 20 20 ATELY,.
e4ba0 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 0, S
e4bb0 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26 HARED_SIZE, 0, &
e4bc0 6f 76 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 ovlp);./* isNT()
e4bd0 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
e4be0 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
e4bf0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
e4c00 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f er executed. .*/
e4c10 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
e4c20 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b INCE==0. }else{
e4c30 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20 . int lk;.
e4c40 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
e4c50 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20 ess(sizeof(lk),
e4c60 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d &lk);. pFile-
e4c70 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 >sharedLockByte
e4c80 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66 = (lk & 0x7fffff
e4c90 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45 ff)%(SHARED_SIZE
e4ca0 20 2d 20 31 29 3b 0a 20 20 20 20 72 65 73 20 3d - 1);. res =
e4cb0 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d LockFile(pFile-
e4cc0 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 >h, SHARED_FIRST
e4cd0 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f +pFile->sharedLo
e4ce0 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 ckByte, 0, 1, 0)
e4cf0 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 ;.#endif. }. r
e4d00 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a eturn res;.}../*
e4d10 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c .** Undo a readl
e4d20 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ock.*/.static in
e4d30 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b t unlockReadLock
e4d40 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 (winFile *pFile)
e4d50 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 {. int res;. i
e4d60 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
e4d70 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c res = UnlockFil
e4d80 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 e(pFile->h, SHAR
e4d90 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 ED_FIRST, 0, SHA
e4da0 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a RED_SIZE, 0);./*
e4db0 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
e4dc0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
e4dd0 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
e4de0 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
e4df0 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 ed. .*/.#if SQLI
e4e00 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 TE_OS_WINCE==0.
e4e10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 }else{. res
e4e20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 = UnlockFile(pFi
e4e30 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 le->h, SHARED_FI
e4e40 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 RST + pFile->sha
e4e50 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 redLockByte, 0,
e4e60 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 1, 0);.#endif.
e4e70 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a }. return res;.
e4e80 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
e4e90 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
e4ea0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
e4eb0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
e4ec0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
e4ed0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
e4ee0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
e4ef0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
e4f00 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
e4f10 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
e4f20 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
e4f30 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
e4f40 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
e4f50 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
e4f60 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
e4f70 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
e4f80 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
e4f90 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
e4fa0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
e4fb0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
e4fc0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
e4fd0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
e4fe0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
e4ff0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
e5000 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
e5010 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
e5020 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
e5030 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
e5040 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
e5050 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
e5060 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
e5070 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
e5080 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
e5090 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
e50a0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
e50b0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
e50c0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
e50d0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
e50e0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
e50f0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
e5100 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
e5110 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
e5120 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
e5130 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
e5140 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
e5150 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
e5160 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63 k. The winUnloc
e5170 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 k() routine.** e
e5180 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 rases all locks
e5190 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 at once and retu
e51a0 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 rns us immediate
e51b0 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 ly to locking le
e51c0 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 vel 0..** It is
e51d0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
e51e0 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e lower the lockin
e51f0 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 g level one step
e5200 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 at a time. You
e5210 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 .** must go stra
e5220 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 ight to locking
e5230 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 level 0..*/.stat
e5240 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 ic int winLock(s
e5250 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
e5260 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a int locktype){.
e5270 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
e5280 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75 E_OK; /* Retu
e5290 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 rn code from sub
e52a0 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e routines */. in
e52b0 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20 t res = 1;
e52c0 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f /* Result o
e52d0 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b f a windows lock
e52e0 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e call */. int n
e52f0 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 ewLocktype;
e5300 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e /* Set pFile->
e5310 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73 locktype to this
e5320 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78 value before ex
e5330 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 iting */. int g
e5340 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 otPendingLock =
e5350 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 0;/* True if we
e5360 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49 acquired a PENDI
e5370 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d NG lock this tim
e5380 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a e */. winFile *
e5390 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
e53a0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 *)id;.. assert(
e53b0 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f pFile!=0 );. O
e53c0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 25 64 STRACE5("LOCK %d
e53d0 20 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e %d was %d(%d)\n
e53e0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ",. pFi
e53f0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
e5400 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
e5410 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c , pFile->sharedL
e5420 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f 2a 20 ockByte);.. /*
e5430 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 If there is alre
e5440 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 ady a lock of th
e5450 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 is type or more
e5460 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 restrictive on t
e5470 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 he. ** OsFile,
e5480 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 do nothing. Don'
e5490 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f t use the end_lo
e54a0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 ck: exit path, a
e54b0 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73 s. ** sqlite3Os
e54c0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 EnterMutex() has
e54d0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 n't been called
e54e0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 yet.. */. if(
e54f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
e5500 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
e5510 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
e5520 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b K;. }.. /* Mak
e5530 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 e sure the locki
e5540 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 ng sequence is c
e5550 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 orrect. */. as
e5560 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 sert( pFile->loc
e5570 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c ktype!=NO_LOCK |
e5580 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 | locktype==SHAR
e5590 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
e55a0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 ert( locktype!=P
e55b0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 ENDING_LOCK );.
e55c0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
e55d0 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e!=RESERVED_LOCK
e55e0 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 || pFile->lockt
e55f0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
e5600 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 );.. /* Lock t
e5610 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 he PENDING_LOCK
e5620 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 byte if we need
e5630 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e to acquire a PEN
e5640 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a DING lock or. *
e5650 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e * a SHARED lock.
e5660 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75 If we are acqu
e5670 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c iring a SHARED l
e5680 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 ock, the acquisi
e5690 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 tion of. ** the
e56a0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 PENDING_LOCK by
e56b0 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e te is temporary.
e56c0 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 . */. newLockt
e56d0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 ype = pFile->loc
e56e0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 ktype;. if( pFi
e56f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f le->locktype==NO
e5700 5f 4c 4f 43 4b 0a 20 20 20 7c 7c 20 28 6c 6f 63 _LOCK. || (loc
e5710 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
e5720 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e _LOCK && pFile->
e5730 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 locktype==RESERV
e5740 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 ED_LOCK). ){.
e5750 20 20 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 int cnt = 3;.
e5760 20 20 20 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e while( cnt-->
e5770 30 20 26 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 0 && (res = Lock
e5780 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 File(pFile->h, P
e5790 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 ENDING_BYTE, 0,
e57a0 31 2c 20 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 1, 0))==0 ){.
e57b0 20 20 20 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 /* Try 3 time
e57c0 73 20 74 6f 20 67 65 74 20 74 68 65 20 70 65 6e s to get the pen
e57d0 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 ding lock. The
e57e0 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 pending lock mig
e57f0 68 74 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 ht be. ** h
e5800 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 72 eld by another r
e5810 65 61 64 65 72 20 70 72 6f 63 65 73 73 20 77 68 eader process wh
e5820 6f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 o will release i
e5830 74 20 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 t momentarily..
e5840 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 */. OS
e5850 54 52 41 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f TRACE2("could no
e5860 74 20 67 65 74 20 61 20 50 45 4e 44 49 4e 47 20 t get a PENDING
e5870 6c 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c lock. cnt=%d\n",
e5880 20 63 6e 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 cnt);. Sle
e5890 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ep(1);. }.
e58a0 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 gotPendingLock
e58b0 3d 20 72 65 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a = res;. }.. /*
e58c0 20 41 63 71 75 69 72 65 20 61 20 73 68 61 72 65 Acquire a share
e58d0 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 d lock. */. if
e58e0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
e58f0 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 ED_LOCK && res )
e5900 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 {. assert( pF
e5910 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e ile->locktype==N
e5920 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 O_LOCK );. re
e5930 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 s = getReadLock(
e5940 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 pFile);. if(
e5950 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 res ){. new
e5960 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 Locktype = SHARE
e5970 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 D_LOCK;. }.
e5980 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
e5990 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a a RESERVED lock.
e59a0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
e59b0 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype==RESERVED_LO
e59c0 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 CK && res ){.
e59d0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
e59e0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
e59f0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 _LOCK );. res
e5a00 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c = LockFile(pFil
e5a10 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 e->h, RESERVED_B
e5a20 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 YTE, 0, 1, 0);.
e5a30 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 if( res ){.
e5a40 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 newLocktype
e5a50 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b = RESERVED_LOCK;
e5a60 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
e5a70 20 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 Acquire a PENDI
e5a80 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 NG lock. */. i
e5a90 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 f( locktype==EXC
e5aa0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 LUSIVE_LOCK && r
e5ab0 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 es ){. newLoc
e5ac0 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f ktype = PENDING_
e5ad0 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e LOCK;. gotPen
e5ae0 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 dingLock = 0;.
e5af0 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
e5b00 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
e5b10 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 k. */. if( loc
e5b20 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
e5b30 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a _LOCK && res ){.
e5b40 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
e5b50 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 e->locktype>=SHA
e5b60 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
e5b70 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 res = unlockRead
e5b80 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
e5b90 20 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 OSTRACE2("unrea
e5ba0 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 dlock = %d\n", r
e5bb0 65 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c es);. res = L
e5bc0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 ockFile(pFile->h
e5bd0 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 , SHARED_FIRST,
e5be0 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 0, SHARED_SIZE,
e5bf0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 0);. if( res
e5c00 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
e5c10 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 type = EXCLUSIVE
e5c20 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 _LOCK;. }else
e5c30 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 {. OSTRACE2
e5c40 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 ("error-code = %
e5c50 64 5c 6e 22 2c 20 47 65 74 4c 61 73 74 45 72 72 d\n", GetLastErr
e5c60 6f 72 28 29 29 3b 0a 20 20 20 20 20 20 67 65 74 or());. get
e5c70 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b ReadLock(pFile);
e5c80 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
e5c90 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69 If we are holdi
e5ca0 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 ng a PENDING loc
e5cb0 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20 k that ought to
e5cc0 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 be released, the
e5cd0 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69 n. ** release i
e5ce0 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 t now.. */. if
e5cf0 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b ( gotPendingLock
e5d00 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 && locktype==SH
e5d10 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
e5d20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
e5d30 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 e->h, PENDING_BY
e5d40 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 TE, 0, 1, 0);.
e5d50 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 }.. /* Update t
e5d60 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 he state of the
e5d70 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69 6e lock has held in
e5d80 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
e5d90 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 ptor then. ** r
e5da0 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70 eturn the approp
e5db0 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f 64 riate result cod
e5dc0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 e.. */. if( re
e5dd0 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 s ){. rc = SQ
e5de0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
e5df0 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 {. OSTRACE4("
e5e00 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 LOCK FAILED %d t
e5e10 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 rying for %d but
e5e20 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c got %d\n", pFil
e5e30 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 e->h,.
e5e40 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f locktype, newLo
e5e50 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 72 63 20 cktype);. rc
e5e60 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
e5e70 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b }. pFile->lock
e5e80 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79 type = newLockty
e5e90 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b pe;. return rc;
e5ea0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
e5eb0 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 outine checks if
e5ec0 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 there is a RESE
e5ed0 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f RVED lock held o
e5ee0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a n the specified.
e5ef0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 ** file by this
e5f00 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f or any other pro
e5f10 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 cess. If such a
e5f20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 lock is held, re
e5f30 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f turn.** non-zero
e5f40 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f , otherwise zero
e5f50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e5f60 77 69 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 winCheckReserved
e5f70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
e5f80 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
e5f90 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Out){. int rc;.
e5fa0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
e5fb0 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
e5fc0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
e5fd0 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 !=0 );. if( pFi
e5fe0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 le->locktype>=RE
e5ff0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SERVED_LOCK ){.
e6000 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 4f rc = 1;. O
e6010 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57 52 STRACE3("TEST WR
e6020 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 -LOCK %d %d (loc
e6030 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 al)\n", pFile->h
e6040 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a , rc);. }else{.
e6050 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c rc = LockFil
e6060 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 e(pFile->h, RESE
e6070 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c RVED_BYTE, 0, 1,
e6080 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 0);. if( rc
e6090 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 46 ){. UnlockF
e60a0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 ile(pFile->h, RE
e60b0 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 SERVED_BYTE, 0,
e60c0 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 1, 0);. }.
e60d0 20 72 63 20 3d 20 21 72 63 3b 0a 20 20 20 20 4f rc = !rc;. O
e60e0 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57 52 STRACE3("TEST WR
e60f0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d -LOCK %d %d (rem
e6100 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e ote)\n", pFile->
e6110 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 2a 70 h, rc);. }. *p
e6120 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 ResOut = rc;. r
e6130 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e6140 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 .}../*.** Lower
e6150 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
e6160 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
e6170 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 ptor id to lockt
e6180 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a ype. locktype.*
e6190 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 * must be either
e61a0 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 NO_LOCK or SHAR
e61b0 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 ED_LOCK..**.** I
e61c0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 f the locking le
e61d0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 vel of the file
e61e0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c descriptor is al
e61f0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f ready at or belo
e6200 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 w.** the request
e6210 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c ed locking level
e6220 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
e6230 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
e6240 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 It is not possi
e6250 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 ble for this rou
e6260 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 tine to fail if
e6270 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
e6280 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 ent.** is NO_LOC
e6290 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e K. If the secon
e62a0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 d argument is SH
e62b0 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 ARED_LOCK then t
e62c0 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d his routine.** m
e62d0 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 ight return SQLI
e62e0 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 TE_IOERR;.*/.sta
e62f0 74 69 63 20 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 tic int winUnloc
e6300 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
e6310 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
e6320 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 ){. int type;.
e6330 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
e6340 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
e6350 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
e6360 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
e6370 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 pFile!=0 );. as
e6380 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d sert( locktype<=
e6390 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
e63a0 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 OSTRACE5("UNLOC
e63b0 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20 25 K %d to %d was %
e63c0 64 28 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d d(%d)\n", pFile-
e63d0 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 >h, locktype,.
e63e0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
e63f0 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e ocktype, pFile->
e6400 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b sharedLockByte);
e6410 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d . type = pFile-
e6420 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 >locktype;. if(
e6430 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 type>=EXCLUSIVE
e6440 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c _LOCK ){. Unl
e6450 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 ockFile(pFile->h
e6460 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 , SHARED_FIRST,
e6470 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 0, SHARED_SIZE,
e6480 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 0);. if( lock
e6490 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
e64a0 4b 20 26 26 20 21 67 65 74 52 65 61 64 4c 6f 63 K && !getReadLoc
e64b0 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 k(pFile) ){.
e64c0 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 /* This should
e64d0 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e 20 20 never happen.
e64e0 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 We should always
e64f0 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 be able to.
e6500 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65 20 74 ** reacquire t
e6510 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a he read lock */.
e6520 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
e6530 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a E_IOERR_UNLOCK;.
e6540 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
e6550 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c type>=RESERVED_L
e6560 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 OCK ){. Unloc
e6570 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
e6580 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 RESERVED_BYTE, 0
e6590 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 , 1, 0);. }. i
e65a0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f f( locktype==NO_
e65b0 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 LOCK && type>=SH
e65c0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
e65d0 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 unlockReadLock(
e65e0 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 pFile);. }. if
e65f0 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f ( type>=PENDING_
e6600 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f LOCK ){. Unlo
e6610 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
e6620 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 PENDING_BYTE, 0
e6630 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 , 1, 0);. }. p
e6640 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
e6650 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 locktype;. ret
e6660 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
e6670 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65 Control and que
e6680 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 ry of the open f
e6690 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 ile handle..*/.s
e66a0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c tatic int winFil
e66b0 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
e66c0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f _file *id, int o
e66d0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a p, void *pArg){.
e66e0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
e66f0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
e6700 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a FCNTL_LOCKSTATE:
e6710 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 {. *(int*)
e6720 70 41 72 67 20 3d 20 28 28 77 69 6e 46 69 6c 65 pArg = ((winFile
e6730 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b *)id)->locktype;
e6740 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
e6750 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
e6760 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
e6770 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a TE_ERROR;.}../*.
e6780 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 ** Return the se
e6790 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 ctor size in byt
e67a0 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c es of the underl
e67b0 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 ying block devic
e67c0 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 e for.** the spe
e67d0 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 cified file. Thi
e67e0 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 s is almost alwa
e67f0 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 ys 512 bytes, bu
e6800 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 t may be.** larg
e6810 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 er for some devi
e6820 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ces..**.** SQLit
e6830 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 e code assumes t
e6840 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e his function can
e6850 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 not fail. It als
e6860 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a o assumes that.*
e6870 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 * if two files a
e6880 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 re created in th
e6890 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 e same file-syst
e68a0 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e em directory (i.
e68b0 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 e..** a database
e68c0 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c and its journal
e68d0 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 file) that the
e68e0 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c sector size will
e68f0 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 be the.** same
e6900 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 for both..*/.sta
e6910 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63 74 6f tic int winSecto
e6920 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 rSize(sqlite3_fi
e6930 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 le *id){. retur
e6940 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 n SQLITE_DEFAULT
e6950 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a _SECTOR_SIZE;.}.
e6960 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
e6970 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65 vector of device
e6980 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 characteristics
e6990 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e69a0 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 winDeviceCharact
e69b0 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 eristics(sqlite3
e69c0 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 _file *id){. re
e69d0 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
e69e0 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 This vector def
e69f0 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 ines all the met
e6a00 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 hods that can op
e6a10 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 erate on an.** s
e6a20 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 qlite3_file for
e6a30 77 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 win32..*/.static
e6a40 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e6a50 6f 5f 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d o_methods winIoM
e6a60 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 ethod = {. 1,
e6a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6a80 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
e6a90 6e 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c n */. winClose,
e6aa0 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69 . winRead,. wi
e6ab0 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75 nWrite,. winTru
e6ac0 6e 63 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63 ncate,. winSync
e6ad0 2c 0a 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c ,. winFileSize,
e6ae0 0a 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 . winLock,. wi
e6af0 6e 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68 nUnlock,. winCh
e6b00 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c eckReservedLock,
e6b10 0a 20 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f . winFileContro
e6b20 6c 2c 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69 l,. winSectorSi
e6b30 7a 65 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43 ze,. winDeviceC
e6b40 68 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d haracteristics.}
e6b50 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;../************
e6b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
e6ba0 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 ** Here ends the
e6bb0 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 I/O methods tha
e6bc0 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 t form the sqlit
e6bd0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
e6be0 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ject..**.** The
e6bf0 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f next block of co
e6c00 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 de implements th
e6c10 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a e VFS methods..*
e6c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
e6c70 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 .** Convert a UT
e6c80 46 2d 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 F-8 filename int
e6c90 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20 o whatever form
e6ca0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a the underlying.*
e6cb0 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 * operating syst
e6cc0 65 6d 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d em wants filenam
e6cd0 65 73 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f es in. Space to
e6ce0 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 hold the result
e6cf0 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 .** is obtained
e6d00 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 from malloc and
e6d10 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 must be freed by
e6d20 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 the calling.**
e6d30 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
e6d40 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 tic void *conver
e6d50 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f tUtf8Filename(co
e6d60 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
e6d70 61 6d 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43 ame){. void *zC
e6d80 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20 onverted = 0;.
e6d90 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
e6da0 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 zConverted = u
e6db0 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 tf8ToUnicode(zFi
e6dc0 6c 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 4e 54 lename);./* isNT
e6dd0 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 () is 1 if SQLIT
e6de0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 E_OS_WINCE==1, s
e6df0 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e o this else is n
e6e00 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a ever executed. .
e6e10 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
e6e20 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 _WINCE==0. }els
e6e30 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 e{. zConverte
e6e40 64 20 3d 20 75 74 66 38 54 6f 4d 62 63 73 28 7a d = utf8ToMbcs(z
e6e50 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 Filename);.#endi
e6e60 66 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65 f. }. /* calle
e6e70 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75 r will handle ou
e6e80 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 t of memory */.
e6e90 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74 return zConvert
e6ea0 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 ed;.}../*.** Cre
e6eb0 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ate a temporary
e6ec0 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 file name in zBu
e6ed0 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 f. zBuf must be
e6ee0 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a big enough to.*
e6ef0 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e * hold at pVfs->
e6f00 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 mxPathname chara
e6f10 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 cters..*/.static
e6f20 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 int getTempname
e6f30 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 (int nBuf, char
e6f40 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 *zBuf){. static
e6f50 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d char zChars[] =
e6f60 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a . "abcdefghij
e6f70 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a klmnopqrstuvwxyz
e6f80 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49 ". "ABCDEFGHI
e6f90 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 JKLMNOPQRSTUVWXY
e6fa0 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 Z". "01234567
e6fb0 38 39 22 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 89";. size_t i,
e6fc0 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 j;. char zTemp
e6fd0 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d Path[MAX_PATH+1]
e6fe0 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f ;. if( sqlite3_
e6ff0 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 temp_directory )
e7000 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
e7010 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d printf(MAX_PATH-
e7020 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 30, zTempPath, "
e7030 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d %s", sqlite3_tem
e7040 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 p_directory);.
e7050 7d 65 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29 }else if( isNT()
e7060 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d ){. char *zM
e7070 75 6c 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20 ulti;. WCHAR
e7080 7a 57 69 64 65 50 61 74 68 5b 4d 41 58 5f 50 41 zWidePath[MAX_PA
e7090 54 48 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 TH];. GetTemp
e70a0 50 61 74 68 57 28 4d 41 58 5f 50 41 54 48 2d 33 PathW(MAX_PATH-3
e70b0 30 2c 20 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 0, zWidePath);.
e70c0 20 20 20 7a 4d 75 6c 74 69 20 3d 20 75 6e 69 63 zMulti = unic
e70d0 6f 64 65 54 6f 55 74 66 38 28 7a 57 69 64 65 50 odeToUtf8(zWideP
e70e0 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 4d ath);. if( zM
e70f0 75 6c 74 69 20 29 7b 0a 20 20 20 20 20 20 73 71 ulti ){. sq
e7100 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d lite3_snprintf(M
e7110 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d AX_PATH-30, zTem
e7120 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75 pPath, "%s", zMu
e7130 6c 74 69 29 3b 0a 20 20 20 20 20 20 66 72 65 65 lti);. free
e7140 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d 65 (zMulti);. }e
e7150 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
e7160 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
e7170 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20 }./* isNT()
e7180 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f is 1 if SQLITE_O
e7190 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 S_WINCE==1, so t
e71a0 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 his else is neve
e71b0 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 r executed. .**
e71c0 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 Since the ASCII
e71d0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 version of these
e71e0 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 Windows API do
e71f0 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 not exist for WI
e7200 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 NCE,.** it's imp
e7210 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 ortant to not re
e7220 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 ference them for
e7230 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a WINCE builds..*
e7240 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
e7250 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
e7260 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 55 74 66 {. char *zUtf
e7270 38 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d 62 63 8;. char zMbc
e7280 73 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b sPath[MAX_PATH];
e7290 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 . GetTempPath
e72a0 41 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a A(MAX_PATH-30, z
e72b0 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a MbcsPath);. z
e72c0 55 74 66 38 20 3d 20 6d 62 63 73 54 6f 55 74 66 Utf8 = mbcsToUtf
e72d0 38 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 8(zMbcsPath);.
e72e0 20 20 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20 if( zUtf8 ){.
e72f0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
e7300 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 rintf(MAX_PATH-3
e7310 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 0, zTempPath, "%
e7320 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20 20 20 s", zUtf8);.
e7330 20 20 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 free(zUtf8);.
e7340 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
e7350 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
e7360 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 MEM;. }.#endi
e7370 66 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 74 f. }. for(i=st
e7380 72 6c 65 6e 28 7a 54 65 6d 70 50 61 74 68 29 3b rlen(zTempPath);
e7390 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50 61 74 i>0 && zTempPat
e73a0 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20 69 2d h[i-1]=='\\'; i-
e73b0 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 -){}. zTempPath
e73c0 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 [i] = 0;. sqlit
e73d0 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
e73e0 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 -30, zBuf,.
e73f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 "%
e7400 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f s\\"SQLITE_TEMP_
e7410 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 FILE_PREFIX, zTe
e7420 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d 20 73 mpPath);. j = s
e7430 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 73 trlen(zBuf);. s
e7440 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
e7450 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b s(20, &zBuf[j]);
e7460 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30 . for(i=0; i<20
e7470 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 ; i++, j++){.
e7480 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 zBuf[j] = (char
e7490 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 )zChars[ ((unsig
e74a0 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d ned char)zBuf[j]
e74b0 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 )%(sizeof(zChars
e74c0 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 )-1) ];. }. zB
e74d0 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 uf[j] = 0;. OST
e74e0 52 41 43 45 32 28 22 54 45 4d 50 20 46 49 4c 45 RACE2("TEMP FILE
e74f0 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 NAME: %s\n", zBu
e7500 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c f);. return SQL
e7510 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a ITE_OK; .}../*.*
e7520 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c * The return val
e7530 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45 72 72 ue of getLastErr
e7540 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f orMsg.** is zero
e7550 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 if the error me
e7560 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 68 ssage fits in th
e7570 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e e buffer, or non
e7580 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 -zero.** otherwi
e7590 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61 se (if the messa
e75a0 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 ge was truncated
e75b0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
e75c0 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 getLastErrorMsg
e75d0 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 (int nBuf, char
e75e0 2a 7a 42 75 66 29 7b 0a 20 20 44 57 4f 52 44 20 *zBuf){. DWORD
e75f0 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 error = GetLastE
e7600 72 72 6f 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c rror();..#if SQL
e7610 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73 ITE_OS_WINCE. s
e7620 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
e7630 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 nBuf, zBuf, "OsE
e7640 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22 2c rror 0x%x (%u)",
e7650 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a error, error);.
e7660 23 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61 #else. /* Forma
e7670 74 4d 65 73 73 61 67 65 20 72 65 74 75 72 6e 73 tMessage returns
e7680 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 0 on failure.
e7690 4f 74 68 65 72 77 69 73 65 20 69 74 0a 20 20 2a Otherwise it. *
e76a0 2a 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 * returns the nu
e76b0 6d 62 65 72 20 6f 66 20 54 43 48 41 52 73 20 77 mber of TCHARs w
e76c0 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 ritten to the ou
e76d0 74 70 75 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 tput. ** buffer
e76e0 2c 20 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20 , excluding the
e76f0 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c terminating null
e7700 20 63 68 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 char.. */. if
e7710 20 28 21 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 (!FormatMessage
e7720 41 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 A(FORMAT_MESSAGE
e7730 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 _FROM_SYSTEM,.
e7740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7750 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 NULL,.
e7760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7770 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 error,.
e7780 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a 0,.
e7790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e77a0 20 20 20 20 20 20 7a 42 75 66 2c 0a 20 20 20 20 zBuf,.
e77b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e77c0 20 20 6e 42 75 66 2d 31 2c 0a 20 20 20 20 20 20 nBuf-1,.
e77d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e77e0 30 29 29 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 0)). {. sqli
e77f0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
e7800 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f f, zBuf, "OsErro
e7810 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 r 0x%x (%u)", er
e7820 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d ror, error);. }
e7830 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 .#endif.. retur
e7840 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f n 0;.}.../*.** O
e7850 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 pen a file..*/.s
e7860 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65 tatic int winOpe
e7870 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
e7880 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f *pVfs, /
e7890 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 * Not used */.
e78a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
e78b0 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d e, /* Nam
e78c0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55 e of the file (U
e78d0 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 TF-8) */. sqlit
e78e0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 e3_file *id,
e78f0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
e7900 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 e SQLite file ha
e7910 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 ndle here */. i
e7920 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
e7930 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e /* Open
e7940 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 mode flags */.
e7950 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 int *pOutFlags
e7960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
e7970 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 atus return flag
e7980 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 s */.){. HANDLE
e7990 20 68 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65 h;. DWORD dwDe
e79a0 73 69 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44 siredAccess;. D
e79b0 57 4f 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 WORD dwShareMode
e79c0 3b 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 ;. DWORD dwCrea
e79d0 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b tionDisposition;
e79e0 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 . DWORD dwFlags
e79f0 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 AndAttributes =
e7a00 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 0;.#if SQLITE_OS
e7a10 5f 57 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 54 _WINCE. int isT
e7a20 65 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a emp = 0;.#endif.
e7a30 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
e7a40 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
e7a50 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 . void *zConver
e7a60 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 ted;
e7a70 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 /* Filename
e7a80 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 20 in OS encoding
e7a90 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
e7aa0 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 *zUtf8Name = zNa
e7ab0 6d 65 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 me; /* Filena
e7ac0 6d 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f me in UTF-8 enco
e7ad0 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a ding */. char z
e7ae0 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 Tmpname[MAX_PATH
e7af0 2b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 +1]; /* B
e7b00 75 66 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 uffer used to cr
e7b10 65 61 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 eate temp filena
e7b20 6d 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 me */.. /* If t
e7b30 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
e7b40 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 nt to this funct
e7b50 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e ion is NULL, gen
e7b60 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 erate a . ** te
e7b70 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d mporary file nam
e7b80 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 e to use . */.
e7b90 20 69 66 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 if( !zUtf8Name
e7ba0 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 ){. int rc =
e7bb0 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f getTempname(MAX_
e7bc0 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 PATH+1, zTmpname
e7bd0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
e7be0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e7bf0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
e7c00 20 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 }. zUtf8Name
e7c10 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d = zTmpname;. }
e7c20 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 .. /* Convert t
e7c30 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 he filename to t
e7c40 68 65 20 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 he system encodi
e7c50 6e 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 ng. */. zConver
e7c60 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 ted = convertUtf
e7c70 38 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 8Filename(zUtf8N
e7c80 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e ame);. if( zCon
e7c90 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 verted==0 ){.
e7ca0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
e7cb0 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 OMEM;. }.. if(
e7cc0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
e7cd0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 OPEN_READWRITE )
e7ce0 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 {. dwDesiredA
e7cf0 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f ccess = GENERIC_
e7d00 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 READ | GENERIC_W
e7d10 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 RITE;. }else{.
e7d20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 dwDesiredAcce
e7d30 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 ss = GENERIC_REA
e7d40 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 D;. }. if( fla
e7d50 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
e7d60 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 64 _CREATE ){. d
e7d70 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 wCreationDisposi
e7d80 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 41 tion = OPEN_ALWA
e7d90 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 YS;. }else{.
e7da0 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f dwCreationDispo
e7db0 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 sition = OPEN_EX
e7dc0 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 69 66 ISTING;. }. if
e7dd0 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
e7de0 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b _OPEN_MAIN_DB ){
e7df0 0a 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 . dwShareMode
e7e00 20 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52 45 = FILE_SHARE_RE
e7e10 41 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45 5f AD | FILE_SHARE_
e7e20 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a WRITE;. }else{.
e7e30 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 20 dwShareMode
e7e40 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 = 0;. }. if( f
e7e50 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
e7e60 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
e7e70 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f ){.#if SQLITE_O
e7e80 53 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 6c S_WINCE. dwFl
e7e90 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 agsAndAttributes
e7ea0 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 = FILE_ATTRIBUT
e7eb0 45 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 73 E_HIDDEN;. is
e7ec0 54 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a Temp = 1;.#else.
e7ed0 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 dwFlagsAndAt
e7ee0 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f tributes = FILE_
e7ef0 41 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 ATTRIBUTE_TEMPOR
e7f00 41 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 ARY.
e7f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7f20 20 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 | FILE_ATTRIB
e7f30 55 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 20 UTE_HIDDEN.
e7f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7f50 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45 | FILE
e7f60 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f _FLAG_DELETE_ON_
e7f70 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20 CLOSE;.#endif.
e7f80 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c 61 }else{. dwFla
e7f90 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 gsAndAttributes
e7fa0 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 = FILE_ATTRIBUTE
e7fb0 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f _NORMAL;. }. /
e7fc0 2a 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 74 * Reports from t
e7fd0 68 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 20 he internet are
e7fe0 74 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 that performance
e7ff0 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 is always. **
e8000 62 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f 46 better if FILE_F
e8010 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 LAG_RANDOM_ACCES
e8020 53 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 6b S is used. Tick
e8030 65 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66 et #2699. */.#if
e8040 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
e8050 0a 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 . dwFlagsAndAtt
e8060 72 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f ributes |= FILE_
e8070 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 FLAG_RANDOM_ACCE
e8080 53 53 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 SS;.#endif. if(
e8090 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 isNT() ){. h
e80a0 20 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28 28 = CreateFileW((
e80b0 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 WCHAR*)zConverte
e80c0 64 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 d,. dwDesi
e80d0 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 redAccess,.
e80e0 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 dwShareMode,.
e80f0 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 NULL,.
e8100 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 dwCreationDis
e8110 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 position,.
e8120 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 dwFlagsAndAttri
e8130 62 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 butes,. NU
e8140 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 69 73 4e LL. );./* isN
e8150 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 T() is 1 if SQLI
e8160 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 TE_OS_WINCE==1,
e8170 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 so this else is
e8180 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 never executed.
e8190 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 .** Since the AS
e81a0 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 CII version of t
e81b0 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 hese Windows API
e81c0 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f do not exist fo
e81d0 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 r WINCE,.** it's
e81e0 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f important to no
e81f0 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d t reference them
e8200 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 for WINCE build
e8210 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s..*/.#if SQLITE
e8220 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d _OS_WINCE==0. }
e8230 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 43 72 else{. h = Cr
e8240 65 61 74 65 46 69 6c 65 41 28 28 63 68 61 72 2a eateFileA((char*
e8250 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 )zConverted,.
e8260 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 dwDesiredAcc
e8270 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 ess,. dwSh
e8280 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 areMode,.
e8290 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 NULL,. dwC
e82a0 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 reationDispositi
e82b0 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 on,. dwFla
e82c0 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c gsAndAttributes,
e82d0 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 . NULL.
e82e0 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 );.#endif. }.
e82f0 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f if( h==INVALID_
e8300 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a HANDLE_VALUE ){.
e8310 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
e8320 74 65 64 29 3b 0a 20 20 20 20 69 66 28 20 66 6c ted);. if( fl
e8330 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
e8340 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 N_READWRITE ){.
e8350 20 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 4f return winO
e8360 70 65 6e 28 30 2c 20 7a 4e 61 6d 65 2c 20 69 64 pen(0, zName, id
e8370 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
e8380 28 28 66 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f ((flags|SQLITE_O
e8390 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 53 PEN_READONLY)&~S
e83a0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
e83b0 52 49 54 45 29 2c 20 70 4f 75 74 46 6c 61 67 73 RITE), pOutFlags
e83c0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
e83d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e83e0 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 E_CANTOPEN;.
e83f0 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 }. }. if( pOut
e8400 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 66 28 Flags ){. if(
e8410 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
e8420 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 OPEN_READWRITE )
e8430 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 {. *pOutFla
e8440 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs = SQLITE_OPEN
e8450 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 _READWRITE;.
e8460 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 4f }else{. *pO
e8470 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 utFlags = SQLITE
e8480 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a _OPEN_READONLY;.
e8490 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 }. }. mems
e84a0 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a et(pFile, 0, siz
e84b0 65 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a 20 20 eof(*pFile));.
e84c0 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d pFile->pMethod =
e84d0 20 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 &winIoMethod;.
e84e0 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 23 pFile->h = h;.#
e84f0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
e8500 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 CE. if( (flags
e8510 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & (SQLITE_OPEN_R
e8520 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f EADWRITE|SQLITE_
e8530 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d OPEN_MAIN_DB)) =
e8540 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 =.
e8550 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 (SQLITE_OPEN_RE
e8560 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f ADWRITE|SQLITE_O
e8570 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20 PEN_MAIN_DB).
e8580 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 72 65 && !winceCre
e8590 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 ateLock(zName, p
e85a0 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 43 File). ){. C
e85b0 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 loseHandle(h);.
e85c0 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 free(zConvert
e85d0 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ed);. return
e85e0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
e85f0 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65 6d . }. if( isTem
e8600 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e p ){. pFile->
e8610 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d zDeleteOnClose =
e8620 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7d zConverted;. }
e8630 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a else.#endif. {.
e8640 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
e8650 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e ted);. }. Open
e8660 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 72 Counter(+1);. r
e8670 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e8680 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
e8690 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e the named file.
e86a0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
e86b0 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f windows does no
e86c0 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74 t allow a file t
e86d0 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 66 20 o be deleted if
e86e0 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 some other.** pr
e86f0 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f 70 65 ocess has it ope
e8700 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 20 n. Sometimes a
e8710 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f 72 virus scanner or
e8720 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 61 indexing progra
e8730 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61 m.** will open a
e8740 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 journal file sh
e8750 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 20 69 ortly after it i
e8760 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72 64 s created in ord
e8770 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 er to do.** what
e8780 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20 57 ever it does. W
e8790 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72 20 hile this other
e87a0 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 process is holdi
e87b0 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f ng the.** file o
e87c0 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 pen, we will be
e87d0 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 unable to delete
e87e0 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 it. To work ar
e87f0 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f ound this.** pro
e8800 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 blem, we delay 1
e8810 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 00 milliseconds
e8820 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65 74 and try to delet
e8830 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 e again. Up.**
e8840 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 to MX_DELETION_A
e8850 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e TTEMPTs deletion
e8860 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 72 75 attempts are ru
e8870 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67 0a n before giving.
e8880 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e ** up and return
e8890 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f ing an error..*/
e88a0 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45 .#define MX_DELE
e88b0 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 0a TION_ATTEMPTS 5.
e88c0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 static int winDe
e88d0 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f lete(. sqlite3_
e88e0 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
e88f0 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 /* Not used
e8900 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f on win32 */. co
e8910 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
e8920 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d ame, /* Nam
e8930 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c e of file to del
e8940 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e ete */. int syn
e8950 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20 20 cDir
e8960 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 /* Not used
e8970 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a on win32 */.){.
e8980 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 int cnt = 0;.
e8990 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f DWORD rc;. DWO
e89a0 52 44 20 65 72 72 6f 72 3b 0a 20 20 76 6f 69 64 RD error;. void
e89b0 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 *zConverted = c
e89c0 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 onvertUtf8Filena
e89d0 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 me(zFilename);.
e89e0 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d if( zConverted=
e89f0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
e8a00 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
e8a10 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 }. SimulateIOE
e8a20 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 rror(return SQLI
e8a30 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 TE_IOERR_DELETE)
e8a40 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 ;. if( isNT() )
e8a50 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 {. do{.
e8a60 44 65 6c 65 74 65 46 69 6c 65 57 28 7a 43 6f 6e DeleteFileW(zCon
e8a70 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 verted);. }wh
e8a80 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 63 20 ile( ( ((rc
e8a90 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 = GetFileAttribu
e8aa0 74 65 73 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 tesW(zConverted)
e8ab0 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c ) != INVALID_FIL
e8ac0 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a 20 20 E_ATTRIBUTES).
e8ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
e8ae0 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 ((error = GetLas
e8af0 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 52 52 tError()) == ERR
e8b00 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44 OR_ACCESS_DENIED
e8b10 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 )). &&
e8b20 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c (++cnt < MX_DEL
e8b30 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a ETION_ATTEMPTS).
e8b40 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 53 && (S
e8b50 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b leep(100), 1) );
e8b60 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
e8b70 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
e8b80 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
e8b90 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
e8ba0 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 cuted. .** Since
e8bb0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 the ASCII versi
e8bc0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 on of these Wind
e8bd0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 ows API do not e
e8be0 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a xist for WINCE,.
e8bf0 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e ** it's importan
e8c00 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e t to not referen
e8c10 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 ce them for WINC
e8c20 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 E builds..*/.#if
e8c30 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
e8c40 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
e8c50 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 do{. Delet
e8c60 65 46 69 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65 eFileA(zConverte
e8c70 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 d);. }while(
e8c80 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 ( ((rc = Get
e8c90 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 28 FileAttributesA(
e8ca0 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 zConverted)) !=
e8cb0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
e8cc0 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 RIBUTES).
e8cd0 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 || ((err
e8ce0 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f or = GetLastErro
e8cf0 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 r()) == ERROR_AC
e8d00 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 CESS_DENIED)).
e8d10 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 && (++c
e8d20 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e nt < MX_DELETION
e8d30 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 _ATTEMPTS).
e8d40 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 && (Sleep(
e8d50 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 65 6e 64 100), 1) );.#end
e8d60 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 if. }. free(zC
e8d70 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f 53 54 onverted);. OST
e8d80 52 41 43 45 32 28 22 44 45 4c 45 54 45 20 5c 22 RACE2("DELETE \"
e8d90 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 %s\"\n", zFilena
e8da0 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 20 me);. return (
e8db0 20 20 28 72 63 20 3d 3d 20 49 4e 56 41 4c 49 44 (rc == INVALID
e8dc0 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 _FILE_ATTRIBUTES
e8dd0 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 ) . &&
e8de0 28 65 72 72 6f 72 20 3d 3d 20 45 52 52 4f 52 5f (error == ERROR_
e8df0 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29 FILE_NOT_FOUND))
e8e00 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
e8e10 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
e8e20 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 TE;.}../*.** Che
e8e30 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 ck the existance
e8e40 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 and status of a
e8e50 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
e8e60 20 69 6e 74 20 77 69 6e 41 63 63 65 73 73 28 0a int winAccess(.
e8e70 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
e8e80 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 Vfs, /*
e8e90 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 Not used on win3
e8ea0 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 2 */. const cha
e8eb0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 r *zFilename,
e8ec0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c /* Name of fil
e8ed0 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 e to check */.
e8ee0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
e8ef0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 /* Ty
e8f00 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 pe of test to ma
e8f10 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 ke on this file
e8f20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 */. int *pResOu
e8f30 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
e8f40 2f 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 20 2a /* OUT: Result *
e8f50 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 61 74 74 /.){. DWORD att
e8f60 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 76 r;. int rc;. v
e8f70 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 oid *zConverted
e8f80 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
e8f90 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 ename(zFilename)
e8fa0 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 ;. if( zConvert
e8fb0 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ed==0 ){. ret
e8fc0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
e8fd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 ;. }. if( isNT
e8fe0 28 29 20 29 7b 0a 20 20 20 20 61 74 74 72 20 3d () ){. attr =
e8ff0 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 GetFileAttribut
e9000 65 73 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e esW((WCHAR*)zCon
e9010 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 verted);./* isNT
e9020 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 () is 1 if SQLIT
e9030 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 E_OS_WINCE==1, s
e9040 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e o this else is n
e9050 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a ever executed. .
e9060 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 ** Since the ASC
e9070 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 II version of th
e9080 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 ese Windows API
e9090 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 do not exist for
e90a0 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 WINCE,.** it's
e90b0 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 important to not
e90c0 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 reference them
e90d0 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 for WINCE builds
e90e0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
e90f0 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 OS_WINCE==0. }e
e9100 6c 73 65 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 lse{. attr =
e9110 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 GetFileAttribute
e9120 73 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 sA((char*)zConve
e9130 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 rted);.#endif.
e9140 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 }. free(zConver
e9150 74 65 64 29 3b 0a 20 20 73 77 69 74 63 68 28 20 ted);. switch(
e9160 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 flags ){. cas
e9170 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
e9180 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 READ:. case S
e9190 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 QLITE_ACCESS_EXI
e91a0 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 STS:. rc =
e91b0 61 74 74 72 21 3d 49 4e 56 41 4c 49 44 5f 46 49 attr!=INVALID_FI
e91c0 4c 45 5f 41 54 54 52 49 42 55 54 45 53 3b 0a 20 LE_ATTRIBUTES;.
e91d0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
e91e0 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 case SQLITE_ACCE
e91f0 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 SS_READWRITE:.
e9200 20 20 20 20 72 63 20 3d 20 28 61 74 74 72 20 26 rc = (attr &
e9210 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f FILE_ATTRIBUTE_
e9220 52 45 41 44 4f 4e 4c 59 29 3d 3d 30 3b 0a 20 20 READONLY)==0;.
e9230 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 break;. d
e9240 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 efault:. as
e9250 73 65 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 sert(!"Invalid f
e9260 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b lags argument");
e9270 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 . }. *pResOut
e9280 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 = rc;. return S
e9290 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a QLITE_OK;.}.../*
e92a0 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 .** Turn a relat
e92b0 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 ive pathname int
e92c0 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d o a full pathnam
e92d0 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75 e. Write the fu
e92e0 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69 ll.** pathname i
e92f0 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f 75 nto zOut[]. zOu
e9300 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c t[] will be at l
e9310 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 east pVfs->mxPat
e9320 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69 hname.** bytes i
e9330 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 n size..*/.stati
e9340 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61 74 c int winFullPat
e9350 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 hname(. sqlite3
e9360 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
e9370 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
e9380 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 r to vfs object
e9390 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
e93a0 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20 *zRelative,
e93b0 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 /* Possibly r
e93c0 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 elative input pa
e93d0 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c th */. int nFul
e93e0 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l,
e93f0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
e9400 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 output buffer i
e9410 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 n bytes */. cha
e9420 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 r *zFull
e9430 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 /* Ou
e9440 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 tput buffer */.)
e9450 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f {..#if defined(_
e9460 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 63 79 67 _CYGWIN__). cyg
e9470 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c 6c win_conv_to_full
e9480 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 65 6c _win32_path(zRel
e9490 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a 20 ative, zFull);.
e94a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
e94b0 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 K;.#endif..#if S
e94c0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
e94d0 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e 6f /* WinCE has no
e94e0 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 65 concept of a re
e94f0 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2c lative pathname,
e9500 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 or so I am told
e9510 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 . */. sqlite3_s
e9520 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 nprintf(pVfs->mx
e9530 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c Pathname, zFull,
e9540 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 "%s", zRelative
e9550 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
e9560 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 TE_OK;.#endif..#
e9570 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if !SQLITE_OS_WI
e9580 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 NCE && !defined(
e9590 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e __CYGWIN__). in
e95a0 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 t nByte;. void
e95b0 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63 *zConverted;. c
e95c0 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 7a 43 6f har *zOut;. zCo
e95d0 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 nverted = conver
e95e0 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 tUtf8Filename(zR
e95f0 65 6c 61 74 69 76 65 29 3b 0a 20 20 69 66 28 20 elative);. if(
e9600 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 43 isNT() ){. WC
e9610 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 HAR *zTemp;.
e9620 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 nByte = GetFullP
e9630 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a athNameW((WCHAR*
e9640 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 )zConverted, 0,
e9650 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 0, 0) + 3;. z
e9660 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e Temp = malloc( n
e9670 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d Byte*sizeof(zTem
e9680 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 p[0]) );. if(
e9690 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 zTemp==0 ){.
e96a0 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 free(zConvert
e96b0 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ed);. retur
e96c0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
e96d0 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c }. GetFul
e96e0 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 lPathNameW((WCHA
e96f0 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e R*)zConverted, n
e9700 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b Byte, zTemp, 0);
e9710 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 . free(zConve
e9720 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 rted);. zOut
e9730 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 = unicodeToUtf8(
e9740 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 zTemp);. free
e9750 28 7a 54 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 (zTemp);./* isNT
e9760 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 () is 1 if SQLIT
e9770 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 E_OS_WINCE==1, s
e9780 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e o this else is n
e9790 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a ever executed. .
e97a0 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 ** Since the ASC
e97b0 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 II version of th
e97c0 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 ese Windows API
e97d0 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 do not exist for
e97e0 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 WINCE,.** it's
e97f0 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 important to not
e9800 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 reference them
e9810 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 for WINCE builds
e9820 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
e9830 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 OS_WINCE==0. }e
e9840 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a lse{. char *z
e9850 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 Temp;. nByte
e9860 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d = GetFullPathNam
e9870 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 eA((char*)zConve
e9880 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b rted, 0, 0, 0) +
e9890 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 3;. zTemp =
e98a0 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 malloc( nByte*si
e98b0 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 zeof(zTemp[0]) )
e98c0 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d ;. if( zTemp=
e98d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 =0 ){. free
e98e0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
e98f0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e9900 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
e9910 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 GetFullPathNa
e9920 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 meA((char*)zConv
e9930 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 erted, nByte, zT
e9940 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 emp, 0);. fre
e9950 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
e9960 20 20 20 7a 4f 75 74 20 3d 20 6d 62 63 73 54 6f zOut = mbcsTo
e9970 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 Utf8(zTemp);.
e9980 20 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 23 65 free(zTemp);.#e
e9990 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a ndif. }. if( z
e99a0 4f 75 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Out ){. sqlit
e99b0 65 33 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 e3_snprintf(pVfs
e99c0 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 ->mxPathname, zF
e99d0 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 ull, "%s", zOut)
e99e0 3b 0a 20 20 20 20 66 72 65 65 28 7a 4f 75 74 29 ;. free(zOut)
e99f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
e9a00 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
e9a10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
e9a20 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 TE_NOMEM;. }.#e
e9a30 6e 64 69 66 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 ndif.}..#ifndef
e9a40 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 SQLITE_OMIT_LOAD
e9a50 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a _EXTENSION./*.**
e9a60 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 Interfaces for
e9a70 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 opening a shared
e9a80 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e library, findin
e9a90 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a g entry points.*
e9aa0 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 * within the sha
e9ab0 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 red library, and
e9ac0 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 closing the sha
e9ad0 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a red library..*/.
e9ae0 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 /*.** Interfaces
e9af0 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 for opening a s
e9b00 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 hared library, f
e9b10 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 inding entry poi
e9b20 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 nts.** within th
e9b30 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
e9b40 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 , and closing th
e9b50 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
e9b60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
e9b70 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73 71 6c 69 *winDlOpen(sqli
e9b80 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 te3_vfs *pVfs, c
e9b90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
e9ba0 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44 4c 45 20 name){. HANDLE
e9bb0 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 h;. void *zConv
e9bc0 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 erted = convertU
e9bd0 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c tf8Filename(zFil
e9be0 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 ename);. if( zC
e9bf0 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 onverted==0 ){.
e9c00 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
e9c10 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b . if( isNT() ){
e9c20 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 . h = LoadLib
e9c30 72 61 72 79 57 28 28 57 43 48 41 52 2a 29 7a 43 raryW((WCHAR*)zC
e9c40 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 onverted);./* is
e9c50 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c NT() is 1 if SQL
e9c60 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c ITE_OS_WINCE==1,
e9c70 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 so this else is
e9c80 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e never executed.
e9c90 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 .** Since the A
e9ca0 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 SCII version of
e9cb0 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 these Windows AP
e9cc0 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 I do not exist f
e9cd0 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 or WINCE,.** it'
e9ce0 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e s important to n
e9cf0 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 ot reference the
e9d00 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c m for WINCE buil
e9d10 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ds..*/.#if SQLIT
e9d20 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 E_OS_WINCE==0.
e9d30 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c }else{. h = L
e9d40 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63 68 61 oadLibraryA((cha
e9d50 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a r*)zConverted);.
e9d60 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 #endif. }. fre
e9d70 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
e9d80 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 return (void*)h
e9d90 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
e9da0 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 winDlError(sqlit
e9db0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
e9dc0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
e9dd0 75 66 4f 75 74 29 7b 0a 20 20 67 65 74 4c 61 73 ufOut){. getLas
e9de0 74 45 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 tErrorMsg(nBuf,
e9df0 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 zBufOut);.}.void
e9e00 20 2a 77 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74 *winDlSym(sqlit
e9e10 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f e3_vfs *pVfs, vo
e9e20 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e id *pHandle, con
e9e30 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c st char *zSymbol
e9e40 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 ){.#if SQLITE_OS
e9e50 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 54 68 65 20 _WINCE. /* The
e9e60 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 GetProcAddressA(
e9e70 29 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c ) routine is onl
e9e80 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 77 y available on w
e9e90 69 6e 63 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 ince. */. retur
e9ea0 6e 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 n GetProcAddress
e9eb0 41 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c A((HANDLE)pHandl
e9ec0 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c e, zSymbol);.#el
e9ed0 73 65 0a 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 se. /* All othe
e9ee0 72 20 77 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f r windows platfo
e9ef0 72 6d 73 20 65 78 70 65 63 74 20 47 65 74 50 72 rms expect GetPr
e9f00 6f 63 41 64 64 72 65 73 73 28 29 20 74 6f 20 74 ocAddress() to t
e9f10 61 6b 65 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 69 ake. ** an Ansi
e9f20 20 73 74 72 69 6e 67 20 72 65 67 61 72 64 6c 65 string regardle
e9f30 73 73 20 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f ss of the _UNICO
e9f40 44 45 20 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20 DE setting */.
e9f50 72 65 74 75 72 6e 20 47 65 74 50 72 6f 63 41 64 return GetProcAd
e9f60 64 72 65 73 73 28 28 48 41 4e 44 4c 45 29 70 48 dress((HANDLE)pH
e9f70 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b andle, zSymbol);
e9f80 0a 23 65 6e 64 69 66 0a 7d 0a 76 6f 69 64 20 77 .#endif.}.void w
e9f90 69 6e 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 inDlClose(sqlite
e9fa0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
e9fb0 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 46 d *pHandle){. F
e9fc0 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e 44 reeLibrary((HAND
e9fd0 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 LE)pHandle);.}.#
e9fe0 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 else /* if SQLIT
e9ff0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
ea000 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 NSION is defined
ea010 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 77 : */. #define w
ea020 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 inDlOpen 0. #d
ea030 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f 72 efine winDlError
ea040 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 0. #define win
ea050 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 DlSym 0. #def
ea060 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20 30 ine winDlClose 0
ea070 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
ea080 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 Write up to nBuf
ea090 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d bytes of random
ea0a0 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a ness into zBuf..
ea0b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
ea0c0 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 nRandomness(sqli
ea0d0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
ea0e0 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
ea0f0 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 Buf){. int n =
ea100 30 3b 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 0;. if( sizeof(
ea110 53 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 SYSTEMTIME)<=nBu
ea120 66 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 f-n ){. SYSTE
ea130 4d 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 MTIME x;. Get
ea140 53 79 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a SystemTime(&x);.
ea150 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
ea160 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 [n], &x, sizeof(
ea170 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 x));. n += si
ea180 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 zeof(x);. }. i
ea190 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 f( sizeof(DWORD)
ea1a0 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 <=nBuf-n ){.
ea1b0 44 57 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 DWORD pid = GetC
ea1c0 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 urrentProcessId(
ea1d0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
ea1e0 42 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 Buf[n], &pid, si
ea1f0 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 zeof(pid));.
ea200 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 n += sizeof(pid)
ea210 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 ;. }. if( size
ea220 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d of(DWORD)<=nBuf-
ea230 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 n ){. DWORD c
ea240 6e 74 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e nt = GetTickCoun
ea250 74 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 t();. memcpy(
ea260 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 &zBuf[n], &cnt,
ea270 73 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 sizeof(cnt));.
ea280 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e n += sizeof(cn
ea290 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 t);. }. if( si
ea2a0 7a 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 zeof(LARGE_INTEG
ea2b0 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 ER)<=nBuf-n ){.
ea2c0 20 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 LARGE_INTEGER
ea2d0 20 69 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 i;. QueryPer
ea2e0 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 formanceCounter(
ea2f0 26 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 &i);. memcpy(
ea300 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 &zBuf[n], &i, si
ea310 7a 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 zeof(i));. n
ea320 2b 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 += sizeof(i);.
ea330 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a }. return n;.}.
ea340 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f ../*.** Sleep fo
ea350 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 r a little while
ea360 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d . Return the am
ea370 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 ount of time sle
ea380 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e pt..*/.static in
ea390 74 20 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 74 t winSleep(sqlit
ea3a0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
ea3b0 74 20 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 53 t microsec){. S
ea3c0 6c 65 65 70 28 28 6d 69 63 72 6f 73 65 63 2b 39 leep((microsec+9
ea3d0 39 39 29 2f 31 30 30 30 29 3b 0a 20 20 72 65 74 99)/1000);. ret
ea3e0 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 urn ((microsec+9
ea3f0 39 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 99)/1000)*1000;.
ea400 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
ea410 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c lowing variable,
ea420 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e if set to a non
ea430 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 -zero value, bec
ea440 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a omes the result.
ea450 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d ** returned from
ea460 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
ea470 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 tTime(). This i
ea480 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
ea490 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ng..*/.#ifdef SQ
ea4a0 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
ea4b0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
ea4c0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 _current_time =
ea4d0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
ea4e0 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e Find the curren
ea4f0 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 t time (in Unive
ea500 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 rsal Coordinated
ea510 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 Time). Write t
ea520 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 he.** current ti
ea530 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 me and date as a
ea540 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 Julian Day numb
ea550 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 er into *prNow a
ea560 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 nd.** return 0.
ea570 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
ea580 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 time and date c
ea590 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a annot be found..
ea5a0 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e */.int winCurren
ea5b0 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 tTime(sqlite3_vf
ea5c0 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 s *pVfs, double
ea5d0 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 *prNow){. FILET
ea5e0 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c IME ft;. /* FIL
ea5f0 45 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 ETIME structure
ea600 69 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 is a 64-bit valu
ea610 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 e representing t
ea620 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 he number of .
ea630 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 100-nanosecon
ea640 64 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 d intervals sinc
ea650 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 e January 1, 160
ea660 31 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 1 (= JD 2305813.
ea670 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 64 6f 75 62 5). . */. doub
ea680 6c 65 20 6e 6f 77 3b 0a 23 69 66 20 53 51 4c 49 le now;.#if SQLI
ea690 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 TE_OS_WINCE. SY
ea6a0 53 54 45 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 STEMTIME time;.
ea6b0 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 GetSystemTime(&
ea6c0 74 69 6d 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 time);. /* if S
ea6d0 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 ystemTimeToFileT
ea6e0 69 6d 65 28 29 20 66 61 69 6c 73 2c 20 69 74 20 ime() fails, it
ea6f0 72 65 74 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f returns zero. */
ea700 0a 20 20 69 66 20 28 21 53 79 73 74 65 6d 54 69 . if (!SystemTi
ea710 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 meToFileTime(&ti
ea720 6d 65 2c 26 66 74 29 29 7b 0a 20 20 20 20 72 65 me,&ft)){. re
ea730 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6c 73 turn 1;. }.#els
ea740 65 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d e. GetSystemTim
ea750 65 41 73 46 69 6c 65 54 69 6d 65 28 20 26 66 74 eAsFileTime( &ft
ea760 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 6f 77 );.#endif. now
ea770 20 3d 20 28 28 64 6f 75 62 6c 65 29 66 74 2e 64 = ((double)ft.d
ea780 77 48 69 67 68 44 61 74 65 54 69 6d 65 29 20 2a wHighDateTime) *
ea790 20 34 32 39 34 39 36 37 32 39 36 2e 30 3b 20 0a 4294967296.0; .
ea7a0 20 20 2a 70 72 4e 6f 77 20 3d 20 28 6e 6f 77 20 *prNow = (now
ea7b0 2b 20 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69 + ft.dwLowDateTi
ea7c0 6d 65 29 2f 38 36 34 30 30 30 30 30 30 30 30 30 me)/864000000000
ea7d0 2e 30 20 2b 20 32 33 30 35 38 31 33 2e 35 3b 0a .0 + 2305813.5;.
ea7e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
ea7f0 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ST. if( sqlite3
ea800 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b _current_time ){
ea810 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 . *prNow = sq
ea820 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
ea830 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 me/86400.0 + 244
ea840 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 0587.5;. }.#end
ea850 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d if. return 0;.}
ea860 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 65 61 ../*.** The idea
ea870 20 69 73 20 74 68 61 74 20 74 68 69 73 20 66 75 is that this fu
ea880 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b nction works lik
ea890 65 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 e a combination
ea8a0 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 74 45 72 72 of.** GetLastErr
ea8b0 6f 72 28 29 20 61 6e 64 20 46 6f 72 6d 61 74 4d or() and FormatM
ea8c0 65 73 73 61 67 65 28 29 20 6f 6e 20 77 69 6e 64 essage() on wind
ea8d0 6f 77 73 20 28 6f 72 20 65 72 72 6e 6f 20 61 6e ows (or errno an
ea8e0 64 0a 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 d.** strerror_r(
ea8f0 29 20 6f 6e 20 75 6e 69 78 29 2e 20 41 66 74 65 ) on unix). Afte
ea900 72 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 r an error is re
ea910 74 75 72 6e 65 64 20 62 79 20 61 6e 20 4f 53 0a turned by an OS.
ea920 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c ** function, SQL
ea930 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66 ite calls this f
ea940 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 7a 42 75 unction with zBu
ea950 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a f pointing to.**
ea960 20 61 20 62 75 66 66 65 72 20 6f 66 20 6e 42 75 a buffer of nBu
ea970 66 20 62 79 74 65 73 2e 20 54 68 65 20 4f 53 20 f bytes. The OS
ea980 6c 61 79 65 72 20 73 68 6f 75 6c 64 20 70 6f 70 layer should pop
ea990 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 62 75 66 ulate the.** buf
ea9a0 66 65 72 20 77 69 74 68 20 61 20 6e 75 6c 2d 74 fer with a nul-t
ea9b0 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 erminated UTF-8
ea9c0 65 6e 63 6f 64 65 64 20 65 72 72 6f 72 20 6d 65 encoded error me
ea9d0 73 73 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 62 ssage.** describ
ea9e0 69 6e 67 20 74 68 65 20 6c 61 73 74 20 49 4f 20 ing the last IO
ea9f0 65 72 72 6f 72 20 74 6f 20 68 61 76 65 20 6f 63 error to have oc
eaa00 63 75 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 cured within the
eaa10 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 72 65 calling.** thre
eaa20 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ad..**.** If the
eaa30 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
eaa40 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 s too large for
eaa50 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66 the supplied buf
eaa60 66 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f 75 6c fer,.** it shoul
eaa70 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e 20 d be truncated.
eaa80 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
eaa90 20 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72 6f of xGetLastErro
eaaa0 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 r.** is zero if
eaab0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
eaac0 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 e fits in the bu
eaad0 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 ffer, or non-zer
eaae0 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 o.** otherwise (
eaaf0 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 if the message w
eab00 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20 49 as truncated). I
eab10 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 f non-zero is re
eab20 74 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 turned,.** then
eab30 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 it is not necess
eab40 61 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 ary to include t
eab50 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f he nul-terminato
eab60 72 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69 r character.** i
eab70 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 n the output buf
eab80 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 fer..**.** Not s
eab90 75 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 72 6f upplying an erro
eaba0 72 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 68 r message will h
eabb0 61 76 65 20 6e 6f 20 61 64 76 65 72 73 65 20 65 ave no adverse e
eabc0 66 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 4c 69 ffect.** on SQLi
eabd0 74 65 2e 20 49 74 20 69 73 20 66 69 6e 65 20 74 te. It is fine t
eabe0 6f 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 65 6d o have an implem
eabf0 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6e 65 entation that ne
eac00 76 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 ver.** returns a
eac10 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 3a n error message:
eac20 0a 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 47 65 .**.** int xGe
eac30 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 tLastError(sqlit
eac40 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
eac50 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
eac60 75 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 73 65 uf){.** asse
eac70 72 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c 30 27 rt(zBuf[0]=='\0'
eac80 29 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 72 6e );.** return
eac90 20 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 0;.** }.**.**
eaca0 20 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20 65 However if an e
eacb0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 rror message is
eacc0 73 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69 6c supplied, it wil
eacd0 6c 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 65 l be incorporate
eace0 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20 69 d.** by sqlite i
eacf0 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 nto the error me
ead00 73 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65 20 ssage available
ead10 74 6f 20 74 68 65 20 75 73 65 72 20 75 73 69 6e to the user usin
ead20 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 g.** sqlite3_err
ead30 6d 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79 20 msg(), possibly
ead40 6d 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 making IO errors
ead50 20 65 61 73 69 65 72 20 74 6f 20 64 65 62 75 67 easier to debug
ead60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
ead70 77 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72 28 winGetLastError(
ead80 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
ead90 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 s, int nBuf, cha
eada0 72 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 r *zBuf){. retu
eadb0 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d rn getLastErrorM
eadc0 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a sg(nBuf, zBuf);.
eadd0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
eade0 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 ize and deinitia
eadf0 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 lize the operati
eae00 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
eae10 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ace..*/.SQLITE_A
eae20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
eae30 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 s_init(void){.
eae40 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 static sqlite3_v
eae50 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 fs winVfs = {.
eae60 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
eae70 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
eae80 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 */. sizeof(w
eae90 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a inFile), /* sz
eaea0 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 OsFile */. MA
eaeb0 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 X_PATH,
eaec0 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a /* mxPathname *
eaed0 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
eaee0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 /* pNex
eaef0 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 t */. "win32"
eaf00 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a , /* z
eaf10 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 Name */. 0,
eaf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
eaf30 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a * pAppData */. .
eaf40 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 winOpen,
eaf50 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 /* xOpen
eaf60 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 */. winDelete
eaf70 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 , /* xDe
eaf80 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 lete */. winA
eaf90 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f ccess, /
eafa0 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 * xAccess */.
eafb0 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 winFullPathname
eafc0 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 , /* xFullPath
eafd0 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 name */. winD
eafe0 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f lOpen, /
eaff0 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 * xDlOpen */.
eb000 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 winDlError,
eb010 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 /* xDlError
eb020 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c */. winDlSym,
eb030 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
eb040 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c Sym */. winDl
eb050 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Close, /*
eb060 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 xDlClose */.
eb070 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 winRandomness,
eb080 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 /* xRandomne
eb090 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 ss */. winSle
eb0a0 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ep, /*
eb0b0 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 xSleep */. wi
eb0c0 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 nCurrentTime,
eb0d0 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 /* xCurrentTime
eb0e0 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 */. winGetLa
eb0f0 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 stError /* xG
eb100 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 etLastError */.
eb110 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 };. sqlite3_vf
eb120 73 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56 s_register(&winV
eb130 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e fs, 1);. return
eb140 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 SQLITE_OK; .}.S
eb150 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
eb160 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 lite3_os_end(voi
eb170 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51 d){ . return SQ
eb180 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 LITE_OK;.}..#end
eb190 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f if /* SQLITE_OS_
eb1a0 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a WIN */../*******
eb1b0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f ******* End of o
eb1c0 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_win.c ********
eb1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb1f0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
eb200 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
eb210 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a le bitvec.c ****
eb220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb240 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
eb250 30 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 2a 08 February 16.*
eb260 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
eb270 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
eb280 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
eb290 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
eb2a0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
eb2b0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
eb2c0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
eb2d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
eb2e0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
eb2f0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
eb300 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
eb310 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
eb320 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
eb330 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
eb340 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
eb350 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
eb360 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
eb370 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
eb380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
eb3c0 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
eb3d0 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 ments an object
eb3e0 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 that represents
eb3f0 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a 2a a fixed-length.*
eb400 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20 * bitmap. Bits
eb410 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 are numbered sta
eb420 72 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a 2a rting with 1..**
eb430 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20 .** A bitmap is
eb440 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 used to record w
eb450 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20 hich pages of a
eb460 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
eb470 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e ve been.** journ
eb480 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 74 alled during a t
eb490 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 77 ransaction, or w
eb4a0 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65 20 hich pages have
eb4b0 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 22 the "dont-write"
eb4c0 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 55 .** property. U
eb4d0 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 65 sually only a fe
eb4e0 77 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 74 w pages are meet
eb4f0 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 6f either conditio
eb500 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 74 n..** So the bit
eb510 6d 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 73 map is usually s
eb520 70 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c 6f parse and has lo
eb530 77 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a 2a w cardinality..*
eb540 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 * But sometimes
eb550 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65 (for example whe
eb560 6e 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 20 n during a DROP
eb570 6f 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c 65 of a large table
eb580 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c ) most.** or all
eb590 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e of the pages in
eb5a0 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 a database can
eb5b0 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 get journalled.
eb5c0 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c In those cases,
eb5d0 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 20 .** the bitmap
eb5e0 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 69 becomes dense wi
eb5f0 74 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 6c th high cardinal
eb600 69 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 69 ity. The algori
eb610 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 6f thm needs .** to
eb620 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 73 handle both cas
eb630 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 es well..**.** T
eb640 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 he size of the b
eb650 69 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 77 itmap is fixed w
eb660 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 69 hen the object i
eb670 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a s created..**.**
eb680 20 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 6c All bits are cl
eb690 65 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 74 ear when the bit
eb6a0 6d 61 70 20 69 73 20 63 72 65 61 74 65 64 2e 20 map is created.
eb6b0 20 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 Individual bits
eb6c0 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 6f .** may be set o
eb6d0 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 74 r cleared one at
eb6e0 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 a time..**.** T
eb6f0 65 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 est operations a
eb700 72 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d re about 100 tim
eb710 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 es more common t
eb720 68 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f hat set operatio
eb730 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65 ns..** Clear ope
eb740 72 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65 rations are exce
eb750 65 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54 edingly rare. T
eb760 68 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79 here are usually
eb770 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e between.** 5 an
eb780 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74 d 500 set operat
eb790 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20 ions per Bitvec
eb7a0 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74 object, though t
eb7b0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74 he number of set
eb7c0 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d s can.** sometim
eb7d0 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e es grow into ten
eb7e0 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 6f s of thousands o
eb7f0 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 73 r larger. The s
eb800 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69 ize of the.** Bi
eb810 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 74 tvec object is t
eb820 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
eb830 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 es in the databa
eb840 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a se file at the.*
eb850 2a 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 61 * start of a tra
eb860 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73 nsaction, and is
eb870 20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c 65 thus usually le
eb880 73 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 68 ss than a few th
eb890 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 ousand,.** but c
eb8a0 61 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 61 an be as large a
eb8b0 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 s 2 billion for
eb8c0 61 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 74 a really big dat
eb8d0 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 abase..**.** @(#
eb8e0 29 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63 2c ) $Id: bitvec.c,
eb8f0 76 20 31 2e 37 20 32 30 30 38 2f 31 31 2f 30 33 v 1.7 2008/11/03
eb900 20 32 30 3a 35 35 3a 30 37 20 64 72 68 20 45 78 20:55:07 drh Ex
eb910 70 20 24 0a 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 p $.*/..#define
eb920 42 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 BITVEC_SZ
eb930 20 35 31 32 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 512./* Round th
eb940 65 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 e union size dow
eb950 6e 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 n to the nearest
eb960 20 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 pointer boundar
eb970 79 2c 20 73 69 6e 63 65 20 74 68 61 74 27 73 20 y, since that's
eb980 68 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 how .** it will
eb990 62 65 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69 be aligned withi
eb9a0 6e 20 74 68 65 20 42 69 74 76 65 63 20 73 74 72 n the Bitvec str
eb9b0 75 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 uct. */.#define
eb9c0 42 49 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20 BITVEC_USIZE
eb9d0 20 28 28 28 42 49 54 56 45 43 5f 53 5a 2d 31 32 (((BITVEC_SZ-12
eb9e0 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a )/sizeof(Bitvec*
eb9f0 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 65 63 ))*sizeof(Bitvec
eba00 2a 29 29 0a 23 64 65 66 69 6e 65 20 42 49 54 56 *)).#define BITV
eba10 45 43 5f 4e 43 48 41 52 20 20 20 20 20 42 49 54 EC_NCHAR BIT
eba20 56 45 43 5f 55 53 49 5a 45 0a 23 64 65 66 69 6e VEC_USIZE.#defin
eba30 65 20 42 49 54 56 45 43 5f 4e 42 49 54 20 20 20 e BITVEC_NBIT
eba40 20 20 20 28 42 49 54 56 45 43 5f 4e 43 48 41 52 (BITVEC_NCHAR
eba50 2a 38 29 0a 23 64 65 66 69 6e 65 20 42 49 54 56 *8).#define BITV
eba60 45 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 EC_NINT (BI
eba70 54 56 45 43 5f 55 53 49 5a 45 2f 34 29 0a 23 64 TVEC_USIZE/4).#d
eba80 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 efine BITVEC_MXH
eba90 41 53 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e ASH (BITVEC_N
ebaa0 49 4e 54 2f 32 29 0a 23 64 65 66 69 6e 65 20 42 INT/2).#define B
ebab0 49 54 56 45 43 5f 4e 50 54 52 20 20 20 20 20 20 ITVEC_NPTR
ebac0 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 (BITVEC_USIZE/si
ebad0 7a 65 6f 66 28 42 69 74 76 65 63 20 2a 29 29 0a zeof(Bitvec *)).
ebae0 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f .#define BITVEC_
ebaf0 48 41 53 48 28 58 29 20 20 20 28 28 28 58 29 2a HASH(X) (((X)*
ebb00 33 37 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 37)%BITVEC_NINT)
ebb10 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 ../*.** A bitmap
ebb20 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
ebb30 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
ebb40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
ebb50 2a 20 54 68 69 73 20 62 69 74 6d 61 70 20 72 65 * This bitmap re
ebb60 63 6f 72 64 73 20 74 68 65 20 65 78 69 73 74 61 cords the exista
ebb70 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d nce of zero or m
ebb80 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 ore bits.** with
ebb90 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 values between
ebba0 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 1 and iSize, inc
ebbb0 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 lusive..**.** Th
ebbc0 65 72 65 20 61 72 65 20 74 68 72 65 65 20 70 6f ere are three po
ebbd0 73 73 69 62 6c 65 20 72 65 70 72 65 73 65 6e 74 ssible represent
ebbe0 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 ations of the bi
ebbf0 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a tmap..** If iSiz
ebc00 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 e<=BITVEC_NBIT,
ebc10 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 then Bitvec.u.aB
ebc20 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 73 74 72 itmap[] is a str
ebc30 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e aight.** bitmap.
ebc40 20 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e The least sign
ebc50 69 66 69 63 61 6e 74 20 62 69 74 20 69 73 20 62 ificant bit is b
ebc60 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 it 1..**.** If i
ebc70 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 Size>BITVEC_NBIT
ebc80 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 and iDivisor==0
ebc90 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 then Bitvec.u.a
ebca0 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 Hash[] is.** a h
ebcb0 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 77 ash table that w
ebcc0 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 ill hold up to B
ebcd0 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64 69 73 ITVEC_MXHASH dis
ebce0 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a tinct values..**
ebcf0 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 .** Otherwise, t
ebd00 68 65 20 76 61 6c 75 65 20 69 20 69 73 20 72 65 he value i is re
ebd10 64 69 72 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e directed into on
ebd20 65 20 6f 66 20 42 49 54 56 45 43 5f 4e 50 54 52 e of BITVEC_NPTR
ebd30 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 70 73 20 .** sub-bitmaps
ebd40 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 42 69 pointed to by Bi
ebd50 74 76 65 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 tvec.u.apSub[].
ebd60 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 0a Each subbitmap.
ebd70 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 20 74 6f ** handles up to
ebd80 20 69 44 69 76 69 73 6f 72 20 73 65 70 61 72 61 iDivisor separa
ebd90 74 65 20 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 te values of i.
ebda0 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a apSub[0] holds.
ebdb0 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 ** values betwee
ebdc0 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 n 1 and iDivisor
ebdd0 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 . apSub[1] hold
ebde0 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e s values between
ebdf0 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b 31 20 61 .** iDivisor+1 a
ebe00 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 nd 2*iDivisor.
ebe10 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 apSub[N] holds v
ebe20 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a alues between.**
ebe30 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 20 61 6e N*iDivisor+1 an
ebe40 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 d (N+1)*iDivisor
ebe50 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 . Each subbitma
ebe60 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a p is normalized.
ebe70 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 ** to hold deal
ebe80 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 with values betw
ebe90 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 een 1 and iDivis
ebea0 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 or..*/.struct Bi
ebeb0 74 76 65 63 20 7b 0a 20 20 75 33 32 20 69 53 69 tvec {. u32 iSi
ebec0 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 ze; /* Maxi
ebed0 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 20 2a 2f mum bit index */
ebee0 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 20 20 . u32 nSet;
ebef0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
ebf00 62 69 74 73 20 74 68 61 74 20 61 72 65 20 73 65 bits that are se
ebf10 74 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 69 t */. u32 iDivi
ebf20 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 sor; /* Number
ebf30 20 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 64 of bits handled
ebf40 20 62 79 20 65 61 63 68 20 61 70 53 75 62 5b 5d by each apSub[]
ebf50 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 6e 69 6f entry */. unio
ebf60 6e 20 7b 0a 20 20 20 20 75 38 20 61 42 69 74 6d n {. u8 aBitm
ebf70 61 70 5b 42 49 54 56 45 43 5f 4e 43 48 41 52 5d ap[BITVEC_NCHAR]
ebf80 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 ; /* Bitmap r
ebf90 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f epresentation */
ebfa0 0a 20 20 20 20 75 33 32 20 61 48 61 73 68 5b 42 . u32 aHash[B
ebfb0 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 ITVEC_NINT];
ebfc0 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 /* Hash table
ebfd0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a representation *
ebfe0 2f 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70 /. Bitvec *ap
ebff0 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d Sub[BITVEC_NPTR]
ec000 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20 ; /* Recursive
ec010 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a representation *
ec020 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a /. } u;.};../*.
ec030 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
ec040 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 62 bitmap object ab
ec050 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 74 le to handle bit
ec060 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 s between 0 and
ec070 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73 iSize,.** inclus
ec080 69 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 ive. Return a p
ec090 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
ec0a0 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 72 w object. Retur
ec0b0 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61 n NULL if .** ma
ec0c0 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 lloc fails..*/.S
ec0d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 QLITE_PRIVATE Bi
ec0e0 74 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 tvec *sqlite3Bit
ec0f0 76 65 63 43 72 65 61 74 65 28 75 33 32 20 69 53 vecCreate(u32 iS
ec100 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 2a ize){. Bitvec *
ec110 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a p;. assert( siz
ec120 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f eof(*p)==BITVEC_
ec130 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 SZ );. p = sqli
ec140 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
ec150 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 izeof(*p) );. i
ec160 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69 f( p ){. p->i
ec170 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20 Size = iSize;.
ec180 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
ec190 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
ec1a0 73 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 20 see if the i-th
ec1b0 62 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 74 bit is set. Ret
ec1c0 75 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c 73 urn true or fals
ec1d0 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e 55 e..** If p is NU
ec1e0 4c 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d 61 LL (if the bitma
ec1f0 70 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 63 p has not been c
ec200 72 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a reated) or if.**
ec210 20 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e i is out of ran
ec220 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 ge, then return
ec230 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 false..*/.SQLITE
ec240 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ec250 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 ite3BitvecTest(B
ec260 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 itvec *p, u32 i)
ec270 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 {. if( p==0 ) r
ec280 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 eturn 0;. if( i
ec290 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d >p->iSize || i==
ec2a0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
ec2b0 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 if( p->iSize<=BI
ec2c0 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 TVEC_NBIT ){.
ec2d0 20 69 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e i--;. return
ec2e0 20 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 (p->u.aBitmap[i
ec2f0 2f 38 5d 20 26 20 28 31 3c 3c 28 69 26 37 29 29 /8] & (1<<(i&7))
ec300 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 )!=0;. }. if(
ec310 70 2d 3e 69 44 69 76 69 73 6f 72 3e 30 20 29 7b p->iDivisor>0 ){
ec320 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 28 . u32 bin = (
ec330 69 2d 31 29 2f 70 2d 3e 69 44 69 76 69 73 6f 72 i-1)/p->iDivisor
ec340 3b 0a 20 20 20 20 69 20 3d 20 28 69 2d 31 29 25 ;. i = (i-1)%
ec350 70 2d 3e 69 44 69 76 69 73 6f 72 20 2b 20 31 3b p->iDivisor + 1;
ec360 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
ec370 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2d te3BitvecTest(p-
ec380 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 2c 20 69 >u.apSub[bin], i
ec390 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
ec3a0 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 u32 h = BITVEC_H
ec3b0 41 53 48 28 69 29 3b 0a 20 20 20 20 77 68 69 6c ASH(i);. whil
ec3c0 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d e( p->u.aHash[h]
ec3d0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d ){. if( p-
ec3e0 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 >u.aHash[h]==i )
ec3f0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
ec400 20 68 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 h++;. if(
ec410 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 h>=BITVEC_NINT )
ec420 20 68 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 h = 0;. }.
ec430 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
ec440 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
ec450 20 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 i-th bit. Retu
ec460 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 rn 0 on success
ec470 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
ec480 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 e if.** anything
ec490 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a goes wrong..**.
ec4a0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
ec4b0 6d 69 67 68 74 20 63 61 75 73 65 20 73 75 62 2d might cause sub-
ec4c0 62 69 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c bitmaps to be al
ec4d0 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e located. Failin
ec4e0 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 g.** to get the
ec4f0 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f memory needed to
ec500 20 68 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 hold the sub-bi
ec510 74 6d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 tmap is the only
ec520 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 .** that can go
ec530 77 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e wrong with an in
ec540 73 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 sert, assuming p
ec550 20 61 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 and i are valid
ec560 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
ec570 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 ing function mus
ec580 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 t ensure that p
ec590 69 73 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 is a valid Bitve
ec5a0 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 c object.** and
ec5b0 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 that the value f
ec5c0 6f 72 20 22 69 22 20 69 73 20 77 69 74 68 69 6e or "i" is within
ec5d0 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 range of the Bi
ec5e0 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 tvec object..**
ec5f0 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 62 65 Otherwise the be
ec600 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
ec610 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
ec620 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ec630 65 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76 e3BitvecSet(Bitv
ec640 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 ec *p, u32 i){.
ec650 20 75 33 32 20 68 3b 0a 20 20 61 73 73 65 72 74 u32 h;. assert
ec660 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( p!=0 );. asse
ec670 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 61 73 73 rt( i>0 );. ass
ec680 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 ert( i<=p->iSize
ec690 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 );. if( p->iSi
ec6a0 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 ze<=BITVEC_NBIT
ec6b0 29 7b 0a 20 20 20 20 69 2d 2d 3b 0a 20 20 20 20 ){. i--;.
ec6c0 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 38 p->u.aBitmap[i/8
ec6d0 5d 20 7c 3d 20 31 20 3c 3c 20 28 69 26 37 29 3b ] |= 1 << (i&7);
ec6e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
ec6f0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 TE_OK;. }. if(
ec700 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a p->iDivisor ){.
ec710 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 28 69 u32 bin = (i
ec720 2d 31 29 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b -1)/p->iDivisor;
ec730 0a 20 20 20 20 69 20 3d 20 28 69 2d 31 29 25 70 . i = (i-1)%p
ec740 2d 3e 69 44 69 76 69 73 6f 72 20 2b 20 31 3b 0a ->iDivisor + 1;.
ec750 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 if( p->u.apS
ec760 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20 ub[bin]==0 ){.
ec770 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e sqlite3Begin
ec780 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
ec790 20 20 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62 p->u.apSub
ec7a0 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42 [bin] = sqlite3B
ec7b0 69 74 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e itvecCreate( p->
ec7c0 69 44 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20 iDivisor );.
ec7d0 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
ec7e0 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
ec7f0 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 if( p->u.apSub
ec800 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 [bin]==0 ) retur
ec810 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
ec820 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
ec830 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 sqlite3BitvecSe
ec840 74 28 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e t(p->u.apSub[bin
ec850 5d 2c 20 69 29 3b 0a 20 20 7d 0a 20 20 68 20 3d ], i);. }. h =
ec860 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 29 3b BITVEC_HASH(i);
ec870 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 . while( p->u.a
ec880 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 Hash[h] ){. i
ec890 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d f( p->u.aHash[h]
ec8a0 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==i ) return SQL
ec8b0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b ITE_OK;. h++;
ec8c0 0a 20 20 20 20 69 66 28 20 68 3d 3d 42 49 54 56 . if( h==BITV
ec8d0 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b EC_NINT ) h = 0;
ec8e0 0a 20 20 7d 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b . }. p->nSet++
ec8f0 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74 3e ;. if( p->nSet>
ec900 3d 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 29 =BITVEC_MXHASH )
ec910 7b 0a 20 20 20 20 69 6e 74 20 6a 2c 20 72 63 3b {. int j, rc;
ec920 0a 20 20 20 20 75 33 32 20 61 69 56 61 6c 75 65 . u32 aiValue
ec930 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a s[BITVEC_NINT];.
ec940 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c memcpy(aiVal
ec950 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c ues, p->u.aHash,
ec960 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 sizeof(aiValues
ec970 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 ));. memset(p
ec980 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69 ->u.apSub, 0, si
ec990 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53 75 62 5b zeof(p->u.apSub[
ec9a0 30 5d 29 2a 42 49 54 56 45 43 5f 4e 50 54 52 29 0])*BITVEC_NPTR)
ec9b0 3b 0a 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f ;. p->iDiviso
ec9c0 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20 r = (p->iSize +
ec9d0 42 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29 BITVEC_NPTR - 1)
ec9e0 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20 /BITVEC_NPTR;.
ec9f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 rc = sqlite3Bi
eca00 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a 20 tvecSet(p, i);.
eca10 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 for(j=0; j<BI
eca20 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b TVEC_NINT; j++){
eca30 0a 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c . if( aiVal
eca40 75 65 73 5b 6a 5d 20 29 20 72 63 20 7c 3d 20 73 ues[j] ) rc |= s
eca50 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
eca60 70 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29 3b p, aiValues[j]);
eca70 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
eca80 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 2d 3e 75 n rc;. }. p->u
eca90 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a 20 .aHash[h] = i;.
ecaa0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
ecab0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 K;.}../*.** Clea
ecac0 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e 20 r the i-th bit.
ecad0 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 Return 0 on suc
ecae0 63 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f cess and an erro
ecaf0 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 r code if.** any
ecb00 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
ecb10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
ecb20 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
ecb30 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 BitvecClear(Bitv
ecb40 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 ec *p, u32 i){.
ecb50 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
ecb60 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 . assert( i>0 )
ecb70 3b 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 ;. if( p->iSize
ecb80 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b <=BITVEC_NBIT ){
ecb90 0a 20 20 20 20 69 2d 2d 3b 0a 20 20 20 20 70 2d . i--;. p-
ecba0 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 38 5d 20 >u.aBitmap[i/8]
ecbb0 26 3d 20 7e 28 31 20 3c 3c 20 28 69 26 37 29 29 &= ~(1 << (i&7))
ecbc0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d ;. }else if( p-
ecbd0 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 >iDivisor ){.
ecbe0 20 75 33 32 20 62 69 6e 20 3d 20 28 69 2d 31 29 u32 bin = (i-1)
ecbf0 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 /p->iDivisor;.
ecc00 20 20 69 20 3d 20 28 69 2d 31 29 25 70 2d 3e 69 i = (i-1)%p->i
ecc10 44 69 76 69 73 6f 72 20 2b 20 31 3b 0a 20 20 20 Divisor + 1;.
ecc20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b if( p->u.apSub[
ecc30 62 69 6e 5d 20 29 7b 0a 20 20 20 20 20 20 73 71 bin] ){. sq
ecc40 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 lite3BitvecClear
ecc50 28 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d (p->u.apSub[bin]
ecc60 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 , i);. }. }e
ecc70 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a lse{. int j;.
ecc80 20 20 20 20 75 33 32 20 61 69 56 61 6c 75 65 73 u32 aiValues
ecc90 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a 20 [BITVEC_NINT];.
ecca0 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 memcpy(aiValu
eccb0 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 es, p->u.aHash,
eccc0 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29 sizeof(aiValues)
eccd0 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d );. memset(p-
ecce0 3e 75 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a >u.aHash, 0, siz
eccf0 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 5b 30 eof(p->u.aHash[0
ecd00 5d 29 2a 42 49 54 56 45 43 5f 4e 49 4e 54 29 3b ])*BITVEC_NINT);
ecd10 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d 20 30 . p->nSet = 0
ecd20 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a ;. for(j=0; j
ecd30 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b <BITVEC_NINT; j+
ecd40 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69 +){. if( ai
ecd50 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20 61 69 56 Values[j] && aiV
ecd60 61 6c 75 65 73 5b 6a 5d 21 3d 69 20 29 7b 0a 20 alues[j]!=i ){.
ecd70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 sqlite3Bi
ecd80 74 76 65 63 53 65 74 28 70 2c 20 61 69 56 61 6c tvecSet(p, aiVal
ecd90 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d ues[j]);. }
ecda0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
ecdb0 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69 .** Destroy a bi
ecdc0 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65 tmap object. Re
ecdd0 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 claim all memory
ecde0 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 used..*/.SQLITE
ecdf0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
ece00 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
ece10 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 oy(Bitvec *p){.
ece20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
ece30 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 44 69 rn;. if( p->iDi
ece40 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 69 6e 74 visor ){. int
ece50 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b i;. for(i=0;
ece60 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 52 3b 20 i<BITVEC_NPTR;
ece70 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
ece80 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
ece90 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d 29 3b (p->u.apSub[i]);
ecea0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
eceb0 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a ite3_free(p);.}.
ecec0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
eced0 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
ecee0 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 T./*.** Let V[]
ecef0 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75 be an array of u
ecf00 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 nsigned characte
ecf10 72 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f rs sufficient to
ecf20 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 6f 20 4e hold.** up to N
ecf30 20 62 69 74 73 2e 20 20 4c 65 74 20 49 20 62 65 bits. Let I be
ecf40 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 an integer betw
ecf50 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c een 0 and N. 0<
ecf60 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 =I<N..** Then th
ecf70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
ecf80 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 os can be used t
ecf90 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c 20 6f 72 o set, clear, or
ecfa0 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 76 69 64 test.** individ
ecfb0 75 61 6c 20 62 69 74 73 20 77 69 74 68 69 6e 20 ual bits within
ecfc0 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 45 V..*/.#define SE
ecfd0 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 20 56 TBIT(V,I) V
ecfe0 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 [I>>3] |= (1<<(I
ecff0 26 37 29 29 0a 23 64 65 66 69 6e 65 20 43 4c 45 &7)).#define CLE
ed000 41 52 42 49 54 28 56 2c 49 29 20 20 20 20 56 5b ARBIT(V,I) V[
ed010 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 I>>3] &= ~(1<<(I
ed020 26 37 29 29 0a 23 64 65 66 69 6e 65 20 54 45 53 &7)).#define TES
ed030 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 28 56 TBIT(V,I) (V
ed040 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 26 37 29 [I>>3]&(1<<(I&7)
ed050 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ))!=0../*.** Thi
ed060 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 61 s routine runs a
ed070 6e 20 65 78 74 65 6e 73 69 76 65 20 74 65 73 74 n extensive test
ed080 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 63 of the Bitvec c
ed090 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 ode..**.** The i
ed0a0 6e 70 75 74 20 69 73 20 61 6e 20 61 72 72 61 79 nput is an array
ed0b0 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74 68 61 of integers tha
ed0c0 74 20 61 63 74 73 20 61 73 20 61 20 70 72 6f 67 t acts as a prog
ed0d0 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74 ram.** to test t
ed0e0 68 65 20 42 69 74 76 65 63 2e 20 20 54 68 65 20 he Bitvec. The
ed0f0 69 6e 74 65 67 65 72 73 20 61 72 65 20 6f 70 63 integers are opc
ed100 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a odes followed.**
ed110 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f by 0, 1, or 3 o
ed120 70 65 72 61 6e 64 73 2c 20 64 65 70 65 6e 64 69 perands, dependi
ed130 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65 ng on the opcode
ed140 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 . Another.** op
ed150 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d code follows imm
ed160 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 ediately after t
ed170 68 65 20 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e he last operand.
ed180 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
ed190 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65 6 opcodes numbe
ed1a0 72 65 64 20 66 72 6f 6d 20 30 20 74 68 72 6f 75 red from 0 throu
ed1b0 67 68 20 35 2e 20 20 30 20 69 73 20 74 68 65 0a gh 5. 0 is the.
ed1c0 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 6f 64 65 ** "halt" opcode
ed1d0 20 61 6e 64 20 63 61 75 73 65 73 20 74 68 65 20 and causes the
ed1e0 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a test to end..**.
ed1f0 2a 2a 20 20 20 20 30 20 20 20 20 20 20 20 20 20 ** 0
ed200 20 48 61 6c 74 20 61 6e 64 20 72 65 74 75 72 6e Halt and return
ed210 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
ed220 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 20 4e 20 rrors.** 1 N
ed230 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 S X Set N bit
ed240 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 s beginning with
ed250 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 S and increment
ed260 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 32 ing by X.** 2
ed270 20 4e 20 53 20 58 20 20 20 20 43 6c 65 61 72 20 N S X Clear
ed280 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 N bits beginning
ed290 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 with S and incr
ed2a0 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a ementing by X.**
ed2b0 20 20 20 20 33 20 4e 20 20 20 20 20 20 20 20 53 3 N S
ed2c0 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 et N randomly ch
ed2d0 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 osen bits.**
ed2e0 34 20 4e 20 20 20 20 20 20 20 20 43 6c 65 61 72 4 N Clear
ed2f0 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 N randomly chos
ed300 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 35 20 en bits.** 5
ed310 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 N S X Set N b
ed320 69 74 73 20 66 72 6f 6d 20 53 20 69 6e 63 72 65 its from S incre
ed330 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 61 79 20 ment X in array
ed340 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 62 69 74 only, not in bit
ed350 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 vec.**.** The op
ed360 63 6f 64 65 73 20 31 20 74 68 72 6f 75 67 68 20 codes 1 through
ed370 34 20 70 65 72 66 6f 72 6d 20 73 65 74 20 61 6e 4 perform set an
ed380 64 20 63 6c 65 61 72 20 6f 70 65 72 61 74 69 6f d clear operatio
ed390 6e 73 20 61 72 65 20 70 65 72 66 6f 72 6d 65 64 ns are performed
ed3a0 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 20 42 69 .** on both a Bi
ed3b0 74 76 65 63 20 6f 62 6a 65 63 74 20 61 6e 64 20 tvec object and
ed3c0 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 on a linear arra
ed3d0 79 20 6f 66 20 62 69 74 73 20 6f 62 74 61 69 6e y of bits obtain
ed3e0 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a ed from malloc..
ed3f0 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 6f 72 6b ** Opcode 5 work
ed400 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 61 72 20 s on the linear
ed410 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 array only, not
ed420 6f 6e 20 74 68 65 20 42 69 74 76 65 63 2e 0a 2a on the Bitvec..*
ed430 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 20 75 73 * Opcode 5 is us
ed440 65 64 20 74 6f 20 64 65 6c 69 62 65 72 61 74 65 ed to deliberate
ed450 6c 79 20 69 6e 64 75 63 65 20 61 20 66 61 75 6c ly induce a faul
ed460 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a t in order to.**
ed470 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 20 65 72 confirm that er
ed480 72 6f 72 20 64 65 74 65 63 74 69 6f 6e 20 77 6f ror detection wo
ed490 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 rks..**.** At th
ed4a0 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 e conclusion of
ed4b0 74 68 65 20 74 65 73 74 20 74 68 65 20 6c 69 6e the test the lin
ed4c0 65 61 72 20 61 72 72 61 79 20 69 73 20 63 6f 6d ear array is com
ed4d0 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 6e 73 74 pared.** against
ed4e0 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a 65 the Bitvec obje
ed4f0 63 74 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 ct. If there ar
ed500 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65 e any difference
ed510 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 s,.** an error i
ed520 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 s returned. If
ed530 74 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d they are the sam
ed540 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72 e, zero is retur
ed550 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ned..**.** If a
ed560 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
ed570 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
ed580 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 return -1..*/.SQ
ed590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ed5a0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 sqlite3BitvecBu
ed5b0 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 20 73 7a iltinTest(int sz
ed5c0 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a 20 20 42 , int *aOp){. B
ed5d0 69 74 76 65 63 20 2a 70 42 69 74 76 65 63 20 3d itvec *pBitvec =
ed5e0 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 0;. unsigned c
ed5f0 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a 20 20 69 har *pV = 0;. i
ed600 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 20 69 6e nt rc = -1;. in
ed610 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 6f 70 3b t i, nx, pc, op;
ed620 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
ed630 74 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65 the Bitvec to be
ed640 20 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69 tested and a li
ed650 6e 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 20 near array of.
ed660 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 61 ** bits to act a
ed670 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 s the reference
ed680 2a 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 73 */. pBitvec = s
ed690 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
ed6a0 74 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 3d te( sz );. pV =
ed6b0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
ed6c0 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 3b (sz+7)/8 + 1 );
ed6d0 0a 20 20 69 66 28 20 70 42 69 74 76 65 63 3d 3d . if( pBitvec==
ed6e0 30 20 7c 7c 20 70 56 3d 3d 30 20 29 20 67 6f 74 0 || pV==0 ) got
ed6f0 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 o bitvec_end;.
ed700 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c 20 28 73 memset(pV, 0, (s
ed710 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0a 0a 20 20 z+7)/8 + 1);..
ed720 2f 2a 20 52 75 6e 20 74 68 65 20 70 72 6f 67 72 /* Run the progr
ed730 61 6d 20 2a 2f 0a 20 20 70 63 20 3d 20 30 3b 0a am */. pc = 0;.
ed740 20 20 77 68 69 6c 65 28 20 28 6f 70 20 3d 20 61 while( (op = a
ed750 4f 70 5b 70 63 5d 29 21 3d 30 20 29 7b 0a 20 20 Op[pc])!=0 ){.
ed760 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
ed770 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20 20 case 1:.
ed780 20 20 20 20 63 61 73 65 20 32 3a 0a 20 20 20 20 case 2:.
ed790 20 20 63 61 73 65 20 35 3a 20 7b 0a 20 20 20 20 case 5: {.
ed7a0 20 20 20 20 6e 78 20 3d 20 34 3b 0a 20 20 20 20 nx = 4;.
ed7b0 20 20 20 20 69 20 3d 20 61 4f 70 5b 70 63 2b 32 i = aOp[pc+2
ed7c0 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 61 ] - 1;. a
ed7d0 4f 70 5b 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b Op[pc+2] += aOp[
ed7e0 70 63 2b 33 5d 3b 0a 20 20 20 20 20 20 20 20 62 pc+3];. b
ed7f0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
ed800 20 20 20 20 63 61 73 65 20 33 3a 0a 20 20 20 20 case 3:.
ed810 20 20 63 61 73 65 20 34 3a 20 0a 20 20 20 20 20 case 4: .
ed820 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
ed830 20 20 20 20 6e 78 20 3d 20 32 3b 0a 20 20 20 20 nx = 2;.
ed840 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 sqlite3_rand
ed850 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 29 omness(sizeof(i)
ed860 2c 20 26 69 29 3b 0a 20 20 20 20 20 20 20 20 62 , &i);. b
ed870 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
ed880 20 20 7d 0a 20 20 20 20 69 66 28 20 28 2d 2d 61 }. if( (--a
ed890 4f 70 5b 70 63 2b 31 5d 29 20 3e 20 30 20 29 20 Op[pc+1]) > 0 )
ed8a0 6e 78 20 3d 20 30 3b 0a 20 20 20 20 70 63 20 2b nx = 0;. pc +
ed8b0 3d 20 6e 78 3b 0a 20 20 20 20 69 20 3d 20 28 69 = nx;. i = (i
ed8c0 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25 73 & 0x7fffffff)%s
ed8d0 7a 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 20 26 z;. if( (op &
ed8e0 20 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 1)!=0 ){.
ed8f0 53 45 54 42 49 54 28 70 56 2c 20 28 69 2b 31 29 SETBIT(pV, (i+1)
ed900 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 21 );. if( op!
ed910 3d 35 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 =5 ){. if
ed920 28 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 ( sqlite3BitvecS
ed930 65 74 28 70 42 69 74 76 65 63 2c 20 69 2b 31 29 et(pBitvec, i+1)
ed940 20 29 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 ) goto bitvec_e
ed950 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 nd;. }.
ed960 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 43 4c 45 }else{. CLE
ed970 41 52 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29 ARBIT(pV, (i+1))
ed980 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ;. sqlite3B
ed990 69 74 76 65 63 43 6c 65 61 72 28 70 42 69 74 76 itvecClear(pBitv
ed9a0 65 63 2c 20 69 2b 31 29 3b 0a 20 20 20 20 7d 0a ec, i+1);. }.
ed9b0 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74 }.. /* Test t
ed9c0 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 o make sure the
ed9d0 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65 78 61 linear array exa
ed9e0 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65 ctly matches the
ed9f0 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f 62 6a . ** Bitvec obj
eda00 65 63 74 2e 20 20 53 74 61 72 74 20 77 69 74 68 ect. Start with
eda10 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 the assumption
eda20 74 68 61 74 20 74 68 65 79 20 64 6f 0a 20 20 2a that they do. *
eda30 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30 29 2e * match (rc==0).
eda40 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f 20 6e Change rc to n
eda50 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64 69 73 on-zero if a dis
eda60 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a 20 69 73 crepancy. ** is
eda70 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 72 found.. */. r
eda80 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 c = sqlite3Bitve
eda90 63 54 65 73 74 28 30 2c 30 29 20 2b 20 73 71 6c cTest(0,0) + sql
edaa0 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
edab0 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 0a 20 20 Bitvec, sz+1).
edac0 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74 65 + sqlite
edad0 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 74 3BitvecTest(pBit
edae0 76 65 63 2c 20 30 29 3b 0a 20 20 66 6f 72 28 69 vec, 0);. for(i
edaf0 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 7b =1; i<=sz; i++){
edb00 0a 20 20 20 20 69 66 28 20 20 28 54 45 53 54 42 . if( (TESTB
edb10 49 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 74 IT(pV,i))!=sqlit
edb20 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 e3BitvecTest(pBi
edb30 74 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 20 tvec,i) ){.
edb40 20 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 62 rc = i;. b
edb50 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
edb60 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63 . /* Free alloc
edb70 61 74 65 64 20 73 74 72 75 63 74 75 72 65 20 2a ated structure *
edb80 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 20 /.bitvec_end:.
edb90 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56 29 sqlite3_free(pV)
edba0 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 ;. sqlite3Bitve
edbb0 63 44 65 73 74 72 6f 79 28 70 42 69 74 76 65 63 cDestroy(pBitvec
edbc0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
edbd0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
edbe0 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
edbf0 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a TEST */../******
edc00 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
edc10 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a bitvec.c *******
edc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc40 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
edc50 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
edc60 69 6c 65 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a ile pcache.c ***
edc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edc90 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
edca0 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 008 August 05.**
edcb0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
edcc0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
edcd0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
edce0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
edcf0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
edd00 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
edd10 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
edd20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
edd30 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
edd40 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
edd50 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
edd60 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
edd70 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
edd80 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
edd90 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
edda0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
eddb0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
eddc0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
eddd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eddf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ede00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
ede10 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d This file implem
ede20 65 6e 74 73 20 74 68 61 74 20 70 61 67 65 20 63 ents that page c
ede30 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 ache..**.** @(#)
ede40 20 24 49 64 3a 20 70 63 61 63 68 65 2e 63 2c 76 $Id: pcache.c,v
ede50 20 31 2e 33 34 20 32 30 30 38 2f 31 30 2f 31 37 1.34 2008/10/17
ede60 20 31 38 3a 35 31 3a 35 33 20 64 61 6e 69 65 6c 18:51:53 daniel
ede70 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
ede80 2f 2a 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 65 /*.** A complete
ede90 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61 page cache is a
edea0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
edeb0 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a is structure..**
edec0 0a 2a 2a 20 41 20 63 61 63 68 65 20 6d 61 79 20 .** A cache may
eded0 6f 6e 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 only be deleted
edee0 62 79 20 69 74 73 20 6f 77 6e 65 72 20 61 6e 64 by its owner and
edef0 20 77 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 while holding t
edf00 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 he.** SQLITE_MUT
edf10 45 58 5f 53 54 41 54 55 53 5f 4c 52 55 20 6d 75 EX_STATUS_LRU mu
edf20 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 tex..*/.struct P
edf30 43 61 63 68 65 20 7b 0a 20 20 2f 2a 2a 2a 2a 2a Cache {. /*****
edf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edf50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edf60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edf70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
edf80 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 . ** The first
edf90 67 72 6f 75 70 20 6f 66 20 65 6c 65 6d 65 6e 74 group of element
edfa0 73 20 6d 61 79 20 62 65 20 72 65 61 64 20 6f 72 s may be read or
edfb0 20 77 72 69 74 74 65 6e 20 61 74 20 61 6e 79 20 written at any
edfc0 74 69 6d 65 20 62 79 0a 20 20 2a 2a 20 74 68 65 time by. ** the
edfd0 20 63 61 63 68 65 20 6f 77 6e 65 72 20 77 69 74 cache owner wit
edfe0 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 74 68 65 hout holding the
edff0 20 6d 75 74 65 78 2e 20 20 4e 6f 20 74 68 72 65 mutex. No thre
ee000 61 64 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 ad other than th
ee010 65 0a 20 20 2a 2a 20 63 61 63 68 65 20 6f 77 6e e. ** cache own
ee020 65 72 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 er is permitted
ee030 74 6f 20 61 63 63 65 73 73 20 74 68 65 73 65 20 to access these
ee040 65 6c 65 6d 65 6e 74 73 20 61 74 20 61 6e 79 20 elements at any
ee050 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 50 67 48 time.. */. PgH
ee060 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 69 dr *pDirty, *pDi
ee070 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 20 rtyTail;
ee080 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74 /* List of dirt
ee090 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 6f y pages in LRU o
ee0a0 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 20 rder */. PgHdr
ee0b0 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20 *pSynced;
ee0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ee0d0 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 67 Last synced pag
ee0e0 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 20 e in dirty page
ee0f0 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 list */. int nR
ee100 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
ee110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ee120 20 4e 75 6d 62 65 72 20 6f 66 20 70 69 6e 6e 65 Number of pinne
ee130 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 d pages */. int
ee140 20 6e 50 69 6e 6e 65 64 3b 20 20 20 20 20 20 20 nPinned;
ee150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee160 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 69 /* Number of pi
ee170 6e 6e 65 64 20 61 6e 64 2f 6f 72 20 64 69 72 74 nned and/or dirt
ee180 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 y pages */. int
ee190 20 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 nMax;
ee1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee1b0 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 63 /* Configured c
ee1c0 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 ache size */. i
ee1d0 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 nt nMin;
ee1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee1f0 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 /* Configured
ee200 20 6d 69 6e 69 6d 75 6d 20 63 61 63 68 65 20 73 minimum cache s
ee210 69 7a 65 20 2a 2f 0a 20 20 2f 2a 2a 2a 2a 2a 2a ize */. /******
ee220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee260 0a 20 20 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 . ** The next g
ee270 72 6f 75 70 20 6f 66 20 65 6c 65 6d 65 6e 74 73 roup of elements
ee280 20 61 72 65 20 66 69 78 65 64 20 77 68 65 6e 20 are fixed when
ee290 74 68 65 20 63 61 63 68 65 20 69 73 20 63 72 65 the cache is cre
ee2a0 61 74 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6d 61 ated and. ** ma
ee2b0 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 y not be changed
ee2c0 20 61 66 74 65 72 77 61 72 64 73 2e 20 20 54 68 afterwards. Th
ee2d0 65 73 65 20 65 6c 65 6d 65 6e 74 73 20 63 61 6e ese elements can
ee2e0 20 72 65 61 64 20 61 74 20 61 6e 79 20 74 69 6d read at any tim
ee2f0 65 20 62 79 0a 20 20 2a 2a 20 74 68 65 20 63 61 e by. ** the ca
ee300 63 68 65 20 6f 77 6e 65 72 20 6f 72 20 62 79 20 che owner or by
ee310 61 6e 79 20 74 68 72 65 61 64 20 68 6f 6c 64 69 any thread holdi
ee320 6e 67 20 74 68 65 20 74 68 65 20 6d 75 74 65 78 ng the the mutex
ee330 2e 20 20 4e 6f 6e 2d 6f 77 6e 65 72 0a 20 20 2a . Non-owner. *
ee340 2a 20 74 68 72 65 61 64 73 20 6d 75 73 74 20 68 * threads must h
ee350 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 20 77 68 old the mutex wh
ee360 65 6e 20 72 65 61 64 69 6e 67 20 74 68 65 73 65 en reading these
ee370 20 65 6c 65 6d 65 6e 74 73 20 74 6f 20 70 72 65 elements to pre
ee380 76 65 6e 74 0a 20 20 2a 2a 20 74 68 65 20 65 6e vent. ** the en
ee390 74 69 72 65 20 50 43 61 63 68 65 20 6f 62 6a 65 tire PCache obje
ee3a0 63 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 64 65 ct from being de
ee3b0 6c 65 74 65 64 20 64 75 72 69 6e 67 20 74 68 65 leted during the
ee3c0 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e read.. */. in
ee3d0 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 t szPage;
ee3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee3f0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 /* Size of eve
ee400 72 79 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 ry page in this
ee410 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73 cache */. int s
ee420 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 zExtra;
ee430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ee440 2a 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20 * Size of extra
ee450 73 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70 space for each p
ee460 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 age */. int bPu
ee470 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 rgeable;
ee480 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ee490 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
ee4a0 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
ee4b0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
ee4c0 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 tress)(void*,PgH
ee4d0 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 dr*); /* C
ee4e0 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20 all to try make
ee4f0 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a a page clean */.
ee500 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b void *pStress;
ee510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee520 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
ee530 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a t to xStress */.
ee540 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /*************
ee550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee580 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 54 *********. ** T
ee590 68 65 20 66 69 6e 61 6c 20 67 72 6f 75 70 20 6f he final group o
ee5a0 66 20 65 6c 65 6d 65 6e 74 73 20 63 61 6e 20 6f f elements can o
ee5b0 6e 6c 79 20 62 65 20 61 63 63 65 73 73 65 64 20 nly be accessed
ee5c0 77 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 while holding th
ee5d0 65 0a 20 20 2a 2a 20 6d 75 74 65 78 2e 20 20 42 e. ** mutex. B
ee5e0 6f 74 68 20 74 68 65 20 63 61 63 68 65 20 6f 77 oth the cache ow
ee5f0 6e 65 72 20 61 6e 64 20 61 6e 79 20 6f 74 68 65 ner and any othe
ee600 72 20 74 68 72 65 61 64 20 6d 75 73 74 20 68 6f r thread must ho
ee610 6c 64 20 74 68 65 20 6d 75 74 65 78 0a 20 20 2a ld the mutex. *
ee620 2a 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 * to read or wri
ee630 74 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 te any of these
ee640 65 6c 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 elements.. */.
ee650 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 int nPage;
ee660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee670 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
ee680 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
ee690 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 69 6e 74 apHash */. int
ee6a0 20 6e 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 nHash;
ee6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee6c0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c /* Number of sl
ee6d0 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d 20 ots in apHash[]
ee6e0 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 61 70 48 */. PgHdr **apH
ee6f0 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 ash;
ee700 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 /* Hash
ee710 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74 20 table for fast
ee720 6c 6f 6f 6b 75 70 20 62 79 20 70 67 6e 6f 20 2a lookup by pgno *
ee730 2f 0a 20 20 50 67 48 64 72 20 2a 70 43 6c 65 61 /. PgHdr *pClea
ee740 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
ee750 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
ee760 6f 66 20 63 6c 65 61 6e 20 70 61 67 65 73 20 69 of clean pages i
ee770 6e 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a n use */.};../*.
ee780 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69 6e ** Free slots in
ee790 20 74 68 65 20 70 61 67 65 20 62 6c 6f 63 6b 20 the page block
ee7a0 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2f 0a 74 79 70 allocator.*/.typ
ee7b0 65 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72 edef struct PgFr
ee7c0 65 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f eeslot PgFreeslo
ee7d0 74 3b 0a 73 74 72 75 63 74 20 50 67 46 72 65 65 t;.struct PgFree
ee7e0 73 6c 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73 slot {. PgFrees
ee7f0 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 lot *pNext; /*
ee800 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a Next free slot *
ee810 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 /.};../*.** Glob
ee820 61 6c 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 al data for the
ee830 70 61 67 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 page cache..*/.s
ee840 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
ee850 20 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c struct PCacheGl
ee860 6f 62 61 6c 20 7b 0a 20 20 69 6e 74 20 69 73 49 obal {. int isI
ee870 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 nit;
ee880 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ee890 54 72 75 65 20 77 68 65 6e 20 69 6e 69 74 69 61 True when initia
ee8a0 6c 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 lized */. sqlit
ee8b0 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b e3_mutex *mutex;
ee8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ee8d0 2a 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 4d * static mutex M
ee8e0 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 UTEX_STATIC_LRU
ee8f0 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d 61 78 50 61 */.. int nMaxPa
ee900 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
ee910 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 6d /* Sum
ee920 20 6f 66 20 6e 4d 61 78 50 61 67 65 20 66 6f 72 of nMaxPage for
ee930 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 purgeable cache
ee940 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e 50 s */. int nMinP
ee950 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
ee960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 /* Su
ee970 6d 20 6f 66 20 6e 4d 69 6e 50 61 67 65 20 66 6f m of nMinPage fo
ee980 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 r purgeable cach
ee990 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 es */. int nCur
ee9a0 72 65 6e 74 50 61 67 65 3b 20 20 20 20 20 20 20 rentPage;
ee9b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ee9c0 75 6d 62 65 72 20 6f 66 20 70 75 72 67 65 61 62 umber of purgeab
ee9d0 6c 65 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 le pages allocat
ee9e0 65 64 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 ed */. PgHdr *p
ee9f0 4c 72 75 48 65 61 64 2c 20 2a 70 4c 72 75 54 61 LruHead, *pLruTa
eea00 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c il; /* L
eea10 52 55 20 6c 69 73 74 20 6f 66 20 75 6e 75 73 65 RU list of unuse
eea20 64 20 63 6c 65 61 6e 20 70 67 73 20 2a 2f 0a 0a d clean pgs */..
eea30 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20 72 /* Variables r
eea40 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49 54 45 elated to SQLITE
eea50 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
eea60 45 20 73 65 74 74 69 6e 67 73 2e 20 2a 2f 0a 20 E settings. */.
eea70 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 20 20 20 int szSlot;
eea80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eea90 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
eeaa0 65 61 63 68 20 66 72 65 65 20 73 6c 6f 74 20 2a each free slot *
eeab0 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74 /. void *pStart
eeac0 2c 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 , *pEnd;
eead0 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e 64 /* Bound
eeae0 73 20 6f 66 20 70 61 67 65 63 61 63 68 65 20 6d s of pagecache m
eeaf0 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f 0a 20 alloc range */.
eeb00 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 46 72 PgFreeslot *pFr
eeb10 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ee;
eeb20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70 61 67 /* Free pag
eeb30 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 7d 20 70 63 e blocks */.} pc
eeb40 61 63 68 65 20 3d 20 7b 30 7d 3b 0a 0a 2f 2a 0a ache = {0};../*.
eeb50 2a 2a 20 41 6c 6c 20 63 6f 64 65 20 69 6e 20 74 ** All code in t
eeb60 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 his file should
eeb70 61 63 63 65 73 73 20 74 68 65 20 67 6c 6f 62 61 access the globa
eeb80 6c 20 70 63 61 63 68 65 20 73 74 72 75 63 74 75 l pcache structu
eeb90 72 65 20 76 69 61 20 74 68 65 0a 2a 2a 20 61 6c re via the.** al
eeba0 69 61 73 20 22 70 63 61 63 68 65 5f 67 22 2e 20 ias "pcache_g".
eebb0 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 This ensures tha
eebc0 74 20 74 68 65 20 57 53 44 20 65 6d 75 6c 61 74 t the WSD emulat
eebd0 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68 65 6e ion is used when
eebe0 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f .** compiling fo
eebf0 72 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 64 r systems that d
eec00 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 65 o not support re
eec10 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 65 66 69 al WSD..*/.#defi
eec20 6e 65 20 70 63 61 63 68 65 5f 67 20 28 47 4c 4f ne pcache_g (GLO
eec30 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 63 68 BAL(struct PCach
eec40 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 65 29 eGlobal, pcache)
eec50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 67 6c 6f )../*.** All glo
eec60 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 75 73 bal variables us
eec70 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c ed by this modul
eec80 65 20 28 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 e (all of which
eec90 61 72 65 20 67 72 6f 75 70 65 64 20 0a 2a 2a 20 are grouped .**
eeca0 74 6f 67 65 74 68 65 72 20 69 6e 20 67 6c 6f 62 together in glob
eecb0 61 6c 20 73 74 72 75 63 74 75 72 65 20 22 70 63 al structure "pc
eecc0 61 63 68 65 22 20 61 62 6f 76 65 29 20 61 72 65 ache" above) are
eecd0 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 protected by th
eece0 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 53 51 4c e static .** SQL
eecf0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
eed00 5f 4c 52 55 20 6d 75 74 65 78 2e 20 41 20 70 6f _LRU mutex. A po
eed10 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 6d 75 inter to this mu
eed20 74 65 78 20 69 73 20 73 74 6f 72 65 64 20 69 6e tex is stored in
eed30 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 22 70 63 .** variable "pc
eed40 61 63 68 65 2e 6d 75 74 65 78 22 2e 0a 2a 2a 0a ache.mutex"..**.
eed50 2a 2a 20 53 6f 6d 65 20 65 6c 65 6d 65 6e 74 73 ** Some elements
eed60 20 6f 66 20 74 68 65 20 50 43 61 63 68 65 20 61 of the PCache a
eed70 6e 64 20 50 67 48 64 72 20 73 74 72 75 63 74 75 nd PgHdr structu
eed80 72 65 73 20 61 72 65 20 70 72 6f 74 65 63 74 65 res are protecte
eed90 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 53 51 4c d by the .** SQL
eeda0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 55 53 ITE_MUTEX_STATUS
eedb0 5f 4c 52 55 20 6d 75 74 65 78 20 61 6e 64 20 6f _LRU mutex and o
eedc0 74 68 65 72 20 61 72 65 20 6e 6f 74 2e 20 20 54 ther are not. T
eedd0 68 65 20 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 he protected.**
eede0 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 67 72 6f elements are gro
eedf0 75 70 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 uped at the end
eee00 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65 of the structure
eee10 73 20 61 6e 64 20 61 72 65 20 63 6c 65 61 72 6c s and are clearl
eee20 79 0a 2a 2a 20 6d 61 72 6b 65 64 2e 0a 2a 2a 0a y.** marked..**.
eee30 2a 2a 20 55 73 65 20 74 68 65 20 66 6f 6c 6c 6f ** Use the follo
eee40 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74 wing macros must
eee50 20 73 75 72 72 6f 75 6e 64 20 61 6c 6c 20 61 63 surround all ac
eee60 63 65 73 73 20 28 72 65 61 64 20 6f 72 20 77 72 cess (read or wr
eee70 69 74 65 29 0a 2a 2a 20 6f 66 20 70 72 6f 74 65 ite).** of prote
eee80 63 74 65 64 20 65 6c 65 6d 65 6e 74 73 2e 20 20 cted elements.
eee90 54 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 The mutex is not
eeea0 20 72 65 63 75 72 73 69 76 65 20 61 6e 64 20 6d recursive and m
eeeb0 61 79 20 6e 6f 74 20 62 65 0a 2a 2a 20 65 6e 74 ay not be.** ent
eeec0 65 72 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f ered more than o
eeed0 6e 63 65 2e 20 20 54 68 65 20 70 63 61 63 68 65 nce. The pcache
eeee0 4d 75 74 65 78 48 65 6c 64 28 29 20 6d 61 63 72 MutexHeld() macr
eeef0 6f 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 o should only be
eef00 0a 2a 2a 20 75 73 65 64 20 77 69 74 68 69 6e 20 .** used within
eef10 61 6e 20 61 73 73 65 72 74 28 29 20 74 6f 20 76 an assert() to v
eef20 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6d erify that the m
eef30 75 74 65 78 20 69 73 20 62 65 69 6e 67 20 68 65 utex is being he
eef40 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 ld..*/.#define p
eef50 63 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 28 cacheEnterMutex(
eef60 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ) sqlite3_mutex_
eef70 65 6e 74 65 72 28 70 63 61 63 68 65 5f 67 2e 6d enter(pcache_g.m
eef80 75 74 65 78 29 0a 23 64 65 66 69 6e 65 20 70 63 utex).#define pc
eef90 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 20 acheExitMutex()
eefa0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
eefb0 65 61 76 65 28 70 63 61 63 68 65 5f 67 2e 6d 75 eave(pcache_g.mu
eefc0 74 65 78 29 0a 23 64 65 66 69 6e 65 20 70 63 61 tex).#define pca
eefd0 63 68 65 4d 75 74 65 78 48 65 6c 64 28 29 20 20 cheMutexHeld()
eefe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
eeff0 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 74 65 ld(pcache_g.mute
ef000 78 29 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f x)../*.** Some o
ef010 66 20 74 68 65 20 61 73 73 65 72 74 28 29 20 6d f the assert() m
ef020 61 63 72 6f 73 20 69 6e 20 74 68 69 73 20 63 6f acros in this co
ef030 64 65 20 61 72 65 20 74 6f 6f 20 65 78 70 65 6e de are too expen
ef040 73 69 76 65 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 sive to run.** e
ef050 76 65 6e 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 ven during norma
ef060 6c 20 64 65 62 75 67 67 69 6e 67 2e 20 20 55 73 l debugging. Us
ef070 65 20 74 68 65 6d 20 6f 6e 6c 79 20 72 61 72 65 e them only rare
ef080 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 ly on long-runni
ef090 6e 67 0a 2a 2a 20 74 65 73 74 73 2e 20 20 45 6e ng.** tests. En
ef0a0 61 62 6c 65 20 74 68 65 20 65 78 70 65 6e 73 69 able the expensi
ef0b0 76 65 20 61 73 73 65 72 74 73 20 75 73 69 6e 67 ve asserts using
ef0c0 20 74 68 65 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 the.** -DSQLITE
ef0d0 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 _ENABLE_EXPENSIV
ef0e0 45 5f 41 53 53 45 52 54 3d 31 20 63 6f 6d 70 69 E_ASSERT=1 compi
ef0f0 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a le-time option..
ef100 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
ef110 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 _ENABLE_EXPENSIV
ef120 45 5f 41 53 53 45 52 54 0a 23 20 64 65 66 69 6e E_ASSERT.# defin
ef130 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 e expensive_asse
ef140 72 74 28 58 29 20 20 61 73 73 65 72 74 28 58 29 rt(X) assert(X)
ef150 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
ef160 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 expensive_assert
ef170 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a (X).#endif../***
ef180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
ef1a0 4c 69 6e 6b 65 64 20 4c 69 73 74 20 4d 61 6e 61 Linked List Mana
ef1b0 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a gement *********
ef1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 ***********/..#i
ef1d0 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 f !defined(NDEBU
ef1e0 47 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 G) && defined(SQ
ef1f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 LITE_ENABLE_EXPE
ef200 4e 53 49 56 45 5f 41 53 53 45 52 54 29 0a 2f 2a NSIVE_ASSERT)./*
ef210 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
ef220 20 76 65 72 69 66 69 65 73 20 74 68 61 74 20 74 verifies that t
ef230 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 he number of ent
ef240 72 69 65 73 20 69 6e 20 74 68 65 20 68 61 73 68 ries in the hash
ef250 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 70 43 61 table.** is pCa
ef260 63 68 65 2d 3e 6e 50 61 67 65 2e 20 20 54 68 69 che->nPage. Thi
ef270 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
ef280 64 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 d within assert(
ef290 29 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 ) statements.**
ef2a0 6f 6e 6c 79 20 61 6e 64 20 69 73 20 74 68 65 72 only and is ther
ef2b0 65 66 6f 72 65 20 64 69 73 61 62 6c 65 64 20 64 efore disabled d
ef2c0 75 72 69 6e 67 20 70 72 6f 64 75 63 74 69 6f 6e uring production
ef2d0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 73 74 61 74 builds..*/.stat
ef2e0 69 63 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 ic int pcacheChe
ef2f0 63 6b 48 61 73 68 43 6f 75 6e 74 28 50 43 61 63 ckHashCount(PCac
ef300 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 he *pCache){. i
ef310 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 50 61 67 nt i;. int nPag
ef320 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 e = 0;. for(i=0
ef330 3b 20 69 3c 70 43 61 63 68 65 2d 3e 6e 48 61 73 ; i<pCache->nHas
ef340 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 48 h; i++){. PgH
ef350 64 72 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 dr *p;. for(p
ef360 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b =pCache->apHash[
ef370 69 5d 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 i]; p; p=p->pNex
ef380 74 48 61 73 68 29 7b 0a 20 20 20 20 20 20 6e 50 tHash){. nP
ef390 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d age++;. }. }
ef3a0 0a 20 20 61 73 73 65 72 74 28 20 6e 50 61 67 65 . assert( nPage
ef3b0 3d 3d 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 ==pCache->nPage
ef3c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d );. return 1;.}
ef3d0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 .#endif /* !NDEB
ef3e0 55 47 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 UG && SQLITE_ENA
ef3f0 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
ef400 53 45 52 54 20 2a 2f 0a 0a 0a 23 69 66 20 21 64 SERT */...#if !d
ef410 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 efined(NDEBUG) &
ef420 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
ef430 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 _ENABLE_EXPENSIV
ef440 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 E_ASSERT)./*.**
ef450 42 61 73 65 64 20 6f 6e 20 74 68 65 20 63 75 72 Based on the cur
ef460 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 43 rent value of PC
ef470 61 63 68 65 2e 6e 52 65 66 20 61 6e 64 20 74 68 ache.nRef and th
ef480 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
ef490 65 0a 2a 2a 20 50 43 61 63 68 65 2e 70 44 69 72 e.** PCache.pDir
ef4a0 74 79 20 6c 69 73 74 2c 20 72 65 74 75 72 6e 20 ty list, return
ef4b0 74 68 65 20 65 78 70 65 63 74 65 64 20 76 61 6c the expected val
ef4c0 75 65 20 6f 66 20 74 68 65 20 50 43 61 63 68 65 ue of the PCache
ef4d0 2e 6e 50 69 6e 6e 65 64 0a 2a 2a 20 63 6f 75 6e .nPinned.** coun
ef4e0 74 65 72 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c ter. This is onl
ef4f0 79 20 75 73 65 64 20 69 6e 20 64 65 62 75 67 67 y used in debugg
ef500 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73 20 66 ing builds, as f
ef510 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
ef520 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 expensive_assert
ef530 28 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 ( pCache->nPinne
ef540 64 3d 3d 70 63 61 63 68 65 50 69 6e 6e 65 64 43 d==pcachePinnedC
ef550 6f 75 6e 74 28 70 43 61 63 68 65 29 20 29 3b 0a ount(pCache) );.
ef560 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 */.static int pc
ef570 61 63 68 65 50 69 6e 6e 65 64 43 6f 75 6e 74 28 achePinnedCount(
ef580 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
ef590 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 69 . PgHdr *p;. i
ef5a0 6e 74 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 nt nPinned = pCa
ef5b0 63 68 65 2d 3e 6e 52 65 66 3b 0a 20 20 66 6f 72 che->nRef;. for
ef5c0 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 (p=pCache->pDirt
ef5d0 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 y; p; p=p->pNext
ef5e0 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 ){. if( p->nR
ef5f0 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e ef==0 ){. n
ef600 50 69 6e 6e 65 64 2b 2b 3b 0a 20 20 20 20 7d 0a Pinned++;. }.
ef610 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 50 69 }. return nPi
ef620 6e 6e 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f nned;.}.#endif /
ef630 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c * !NDEBUG && SQL
ef640 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
ef650 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a SIVE_ASSERT */..
ef660 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
ef670 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 EBUG) && defined
ef680 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 (SQLITE_ENABLE_E
ef690 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 29 XPENSIVE_ASSERT)
ef6a0 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 ./*.** Check tha
ef6b0 74 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 53 t the pCache->pS
ef6c0 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69 ynced variable i
ef6d0 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e s set correctly.
ef6e0 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 If it.** is not
ef6f0 2c 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 6e , either fail an
ef700 20 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 72 assert or retur
ef710 6e 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 73 n zero. Otherwis
ef720 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e e, return.** non
ef730 2d 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 6f -zero. This is o
ef740 6e 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 75 nly used in debu
ef750 67 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73 gging builds, as
ef760 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
ef770 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
ef780 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 rt( pcacheCheckS
ef790 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b ynced(pCache) );
ef7a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
ef7b0 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 cacheCheckSynced
ef7c0 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
ef7d0 7b 0a 20 20 50 67 48 64 72 20 2a 70 20 3d 20 70 {. PgHdr *p = p
ef7e0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 Cache->pDirtyTai
ef7f0 6c 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 l;. for(p=pCach
ef800 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 e->pDirtyTail; p
ef810 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 !=pCache->pSynce
ef820 64 3b 20 70 3d 70 2d 3e 70 50 72 65 76 29 7b 0a d; p=p->pPrev){.
ef830 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e assert( p->n
ef840 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 Ref || (p->flags
ef850 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
ef860 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ) );. }. retur
ef870 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 n (p==0 || p->nR
ef880 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26 ef || (p->flags&
ef890 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
ef8a0 3d 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f ==0);.}.#endif /
ef8b0 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c * !NDEBUG && SQL
ef8c0 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
ef8d0 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a SIVE_ASSERT */..
ef8e0 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 61 ../*.** Remove a
ef8f0 20 70 61 67 65 20 66 72 6f 6d 20 69 74 73 20 68 page from its h
ef900 61 73 68 20 74 61 62 6c 65 20 28 50 43 61 63 68 ash table (PCach
ef910 65 2e 61 70 48 61 73 68 5b 5d 29 2e 0a 2a 2f 0a e.apHash[])..*/.
ef920 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
ef930 68 65 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 heRemoveFromHash
ef940 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a (PgHdr *pPage){.
ef950 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 assert( pcache
ef960 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 MutexHeld() );.
ef970 20 69 66 28 20 70 50 61 67 65 2d 3e 70 50 72 65 if( pPage->pPre
ef980 76 48 61 73 68 20 29 7b 0a 20 20 20 20 70 50 61 vHash ){. pPa
ef990 67 65 2d 3e 70 50 72 65 76 48 61 73 68 2d 3e 70 ge->pPrevHash->p
ef9a0 4e 65 78 74 48 61 73 68 20 3d 20 70 50 61 67 65 NextHash = pPage
ef9b0 2d 3e 70 4e 65 78 74 48 61 73 68 3b 0a 20 20 7d ->pNextHash;. }
ef9c0 65 6c 73 65 7b 0a 20 20 20 20 50 43 61 63 68 65 else{. PCache
ef9d0 20 2a 70 43 61 63 68 65 20 3d 20 70 50 61 67 65 *pCache = pPage
ef9e0 2d 3e 70 43 61 63 68 65 3b 0a 20 20 20 20 75 33 ->pCache;. u3
ef9f0 32 20 68 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 2 h = pPage->pgn
efa00 6f 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 o % pCache->nHas
efa10 68 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 h;. assert( p
efa20 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d Cache->apHash[h]
efa30 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70 ==pPage );. p
efa40 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d Cache->apHash[h]
efa50 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 48 = pPage->pNextH
efa60 61 73 68 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ash;. }. if( p
efa70 50 61 67 65 2d 3e 70 4e 65 78 74 48 61 73 68 20 Page->pNextHash
efa80 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e ){. pPage->pN
efa90 65 78 74 48 61 73 68 2d 3e 70 50 72 65 76 48 61 extHash->pPrevHa
efaa0 73 68 20 3d 20 70 50 61 67 65 2d 3e 70 50 72 65 sh = pPage->pPre
efab0 76 48 61 73 68 3b 0a 20 20 7d 0a 20 20 70 50 61 vHash;. }. pPa
efac0 67 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 50 61 67 ge->pCache->nPag
efad0 65 2d 2d 3b 0a 20 20 65 78 70 65 6e 73 69 76 65 e--;. expensive
efae0 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 _assert( pcacheC
efaf0 68 65 63 6b 48 61 73 68 43 6f 75 6e 74 28 70 50 heckHashCount(pP
efb00 61 67 65 2d 3e 70 43 61 63 68 65 29 20 29 3b 0a age->pCache) );.
efb10 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 }../*.** Insert
efb20 61 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 a page into the
efb30 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 0a 2a 2a hash table.**.**
efb40 20 54 68 65 20 6d 75 74 65 78 20 6d 75 73 74 20 The mutex must
efb50 62 65 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 be held by the c
efb60 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 aller..*/.static
efb70 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 void pcacheAddT
efb80 6f 48 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 oHash(PgHdr *pPa
efb90 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 ge){. PCache *p
efba0 43 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 Cache = pPage->p
efbb0 43 61 63 68 65 3b 0a 20 20 75 33 32 20 68 20 3d Cache;. u32 h =
efbc0 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 25 20 70 pPage->pgno % p
efbd0 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 Cache->nHash;.
efbe0 61 73 73 65 72 74 28 20 70 63 61 63 68 65 4d 75 assert( pcacheMu
efbf0 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 70 texHeld() );. p
efc00 50 61 67 65 2d 3e 70 4e 65 78 74 48 61 73 68 20 Page->pNextHash
efc10 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 = pCache->apHash
efc20 5b 68 5d 3b 0a 20 20 70 50 61 67 65 2d 3e 70 50 [h];. pPage->pP
efc30 72 65 76 48 61 73 68 20 3d 20 30 3b 0a 20 20 69 revHash = 0;. i
efc40 66 28 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 f( pCache->apHas
efc50 68 5b 68 5d 20 29 7b 0a 20 20 20 20 70 43 61 63 h[h] ){. pCac
efc60 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 2d 3e 70 he->apHash[h]->p
efc70 50 72 65 76 48 61 73 68 20 3d 20 70 50 61 67 65 PrevHash = pPage
efc80 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 2d 3e ;. }. pCache->
efc90 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 apHash[h] = pPag
efca0 65 3b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 e;. pCache->nPa
efcb0 67 65 2b 2b 3b 0a 20 20 65 78 70 65 6e 73 69 76 ge++;. expensiv
efcc0 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 e_assert( pcache
efcd0 43 68 65 63 6b 48 61 73 68 43 6f 75 6e 74 28 70 CheckHashCount(p
efce0 43 61 63 68 65 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a Cache) );.}../*.
efcf0 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 69 6e ** Attempt to in
efd00 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 crease the size
efd10 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 the hash table t
efd20 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 74 20 o contain.** at
efd30 6c 65 61 73 74 20 6e 48 61 73 68 20 62 75 63 6b least nHash buck
efd40 65 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ets..*/.static i
efd50 6e 74 20 70 63 61 63 68 65 52 65 73 69 7a 65 48 nt pcacheResizeH
efd60 61 73 68 28 50 43 61 63 68 65 20 2a 70 43 61 63 ash(PCache *pCac
efd70 68 65 2c 20 69 6e 74 20 6e 48 61 73 68 29 7b 0a he, int nHash){.
efd80 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 50 67 PgHdr *p;. Pg
efd90 48 64 72 20 2a 2a 70 4e 65 77 3b 0a 20 20 61 73 Hdr **pNew;. as
efda0 73 65 72 74 28 20 70 63 61 63 68 65 4d 75 74 65 sert( pcacheMute
efdb0 78 48 65 6c 64 28 29 20 29 3b 0a 23 69 66 64 65 xHeld() );.#ifde
efdc0 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f f SQLITE_MALLOC_
efdd0 53 4f 46 54 5f 4c 49 4d 49 54 0a 20 20 69 66 28 SOFT_LIMIT. if(
efde0 20 6e 48 61 73 68 2a 73 69 7a 65 6f 66 28 50 67 nHash*sizeof(Pg
efdf0 48 64 72 2a 29 3e 53 51 4c 49 54 45 5f 4d 41 4c Hdr*)>SQLITE_MAL
efe00 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 LOC_SOFT_LIMIT )
efe10 7b 0a 20 20 20 20 6e 48 61 73 68 20 3d 20 53 51 {. nHash = SQ
efe20 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 LITE_MALLOC_SOFT
efe30 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 50 67 _LIMIT/sizeof(Pg
efe40 48 64 72 20 2a 29 3b 0a 20 20 7d 0a 23 65 6e 64 Hdr *);. }.#end
efe50 69 66 0a 20 20 70 63 61 63 68 65 45 78 69 74 4d if. pcacheExitM
efe60 75 74 65 78 28 29 3b 0a 20 20 70 4e 65 77 20 3d utex();. pNew =
efe70 20 28 50 67 48 64 72 20 2a 2a 29 73 71 6c 69 74 (PgHdr **)sqlit
efe80 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 e3Malloc(sizeof(
efe90 50 67 48 64 72 2a 29 2a 6e 48 61 73 68 29 3b 0a PgHdr*)*nHash);.
efea0 20 20 70 63 61 63 68 65 45 6e 74 65 72 4d 75 74 pcacheEnterMut
efeb0 65 78 28 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 ex();. if( !pNe
efec0 77 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 w ){. return
efed0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
efee0 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c }. memset(pNew,
efef0 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 48 64 72 0, sizeof(PgHdr
eff00 20 2a 29 2a 6e 48 61 73 68 29 3b 0a 20 20 73 71 *)*nHash);. sq
eff10 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 lite3_free(pCach
eff20 65 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 70 43 e->apHash);. pC
eff30 61 63 68 65 2d 3e 61 70 48 61 73 68 20 3d 20 70 ache->apHash = p
eff40 4e 65 77 3b 0a 20 20 70 43 61 63 68 65 2d 3e 6e New;. pCache->n
eff50 48 61 73 68 20 3d 20 6e 48 61 73 68 3b 0a 20 20 Hash = nHash;.
eff60 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 3d 20 pCache->nPage =
eff70 30 3b 0a 20 0a 20 20 66 6f 72 28 70 3d 70 43 61 0;. . for(p=pCa
eff80 63 68 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20 che->pClean; p;
eff90 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
effa0 20 70 63 61 63 68 65 41 64 64 54 6f 48 61 73 68 pcacheAddToHash
effb0 28 70 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 (p);. }. for(p
effc0 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b =pCache->pDirty;
effd0 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
effe0 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f . pcacheAddTo
efff0 48 61 73 68 28 70 29 3b 0a 20 20 7d 0a 20 20 72 Hash(p);. }. r
f0000 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
f0010 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 .}../*.** Remove
f0020 20 61 20 70 61 67 65 20 66 72 6f 6d 20 61 20 6c a page from a l
f0030 69 6e 6b 65 64 20 6c 69 73 74 20 74 68 61 74 20 inked list that
f0040 69 73 20 68 65 61 64 65 64 20 62 79 20 2a 70 70 is headed by *pp
f0050 48 65 61 64 2e 0a 2a 2a 20 2a 70 70 48 65 61 64 Head..** *ppHead
f0060 20 69 73 20 65 69 74 68 65 72 20 50 43 61 63 68 is either PCach
f0070 65 2e 70 43 6c 65 61 6e 20 6f 72 20 50 43 61 63 e.pClean or PCac
f0080 68 65 2e 70 44 69 72 74 79 2e 0a 2a 2f 0a 73 74 he.pDirty..*/.st
f0090 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
f00a0 52 65 6d 6f 76 65 46 72 6f 6d 4c 69 73 74 28 50 RemoveFromList(P
f00b0 67 48 64 72 20 2a 2a 70 70 48 65 61 64 2c 20 50 gHdr **ppHead, P
f00c0 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 gHdr *pPage){.
f00d0 69 6e 74 20 69 73 44 69 72 74 79 4c 69 73 74 20 int isDirtyList
f00e0 3d 20 28 70 70 48 65 61 64 3d 3d 26 70 50 61 67 = (ppHead==&pPag
f00f0 65 2d 3e 70 43 61 63 68 65 2d 3e 70 44 69 72 74 e->pCache->pDirt
f0100 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 70 y);. assert( pp
f0110 48 65 61 64 3d 3d 26 70 50 61 67 65 2d 3e 70 43 Head==&pPage->pC
f0120 61 63 68 65 2d 3e 70 43 6c 65 61 6e 20 7c 7c 20 ache->pClean ||
f0130 70 70 48 65 61 64 3d 3d 26 70 50 61 67 65 2d 3e ppHead==&pPage->
f0140 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 20 29 pCache->pDirty )
f0150 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 ;. assert( pcac
f0160 68 65 4d 75 74 65 78 48 65 6c 64 28 29 20 7c 7c heMutexHeld() ||
f0170 20 70 70 48 65 61 64 21 3d 26 70 50 61 67 65 2d ppHead!=&pPage-
f0180 3e 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 6e 20 >pCache->pClean
f0190 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d );.. if( pPage-
f01a0 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 50 >pPrev ){. pP
f01b0 61 67 65 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 age->pPrev->pNex
f01c0 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 t = pPage->pNext
f01d0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
f01e0 73 73 65 72 74 28 20 2a 70 70 48 65 61 64 3d 3d ssert( *ppHead==
f01f0 70 50 61 67 65 20 29 3b 0a 20 20 20 20 2a 70 70 pPage );. *pp
f0200 48 65 61 64 20 3d 20 70 50 61 67 65 2d 3e 70 4e Head = pPage->pN
f0210 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ext;. }. if( p
f0220 50 61 67 65 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 Page->pNext ){.
f0230 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 2d pPage->pNext-
f0240 3e 70 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e >pPrev = pPage->
f0250 70 50 72 65 76 3b 0a 20 20 7d 0a 0a 20 20 69 66 pPrev;. }.. if
f0260 28 20 69 73 44 69 72 74 79 4c 69 73 74 20 29 7b ( isDirtyList ){
f0270 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 . PCache *pCa
f0280 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 che = pPage->pCa
f0290 63 68 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 che;. assert(
f02a0 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 7c 7c pPage->pNext ||
f02b0 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 pCache->pDirtyT
f02c0 61 69 6c 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 ail==pPage );.
f02d0 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70 4e if( !pPage->pN
f02e0 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 43 61 ext ){. pCa
f02f0 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 che->pDirtyTail
f0300 3d 20 70 50 61 67 65 2d 3e 70 50 72 65 76 3b 0a = pPage->pPrev;.
f0310 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 }. if( pC
f0320 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 ache->pSynced==p
f0330 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 50 67 Page ){. Pg
f0340 48 64 72 20 2a 70 53 79 6e 63 65 64 20 3d 20 70 Hdr *pSynced = p
f0350 50 61 67 65 2d 3e 70 50 72 65 76 3b 0a 20 20 20 Page->pPrev;.
f0360 20 20 20 77 68 69 6c 65 28 20 70 53 79 6e 63 65 while( pSynce
f0370 64 20 26 26 20 28 70 53 79 6e 63 65 64 2d 3e 66 d && (pSynced->f
f0380 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
f0390 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 20 SYNC) ){.
f03a0 20 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 pSynced = pSync
f03b0 65 64 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 ed->pPrev;.
f03c0 20 7d 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d }. pCache-
f03d0 3e 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 >pSynced = pSync
f03e0 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a ed;. }. }.}.
f03f0 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 70 61 67 ./*.** Add a pag
f0400 65 20 66 72 6f 6d 20 61 20 6c 69 6e 6b 65 64 20 e from a linked
f0410 6c 69 73 74 20 74 68 61 74 20 69 73 20 68 65 61 list that is hea
f0420 64 65 64 20 62 79 20 2a 70 70 48 65 61 64 2e 0a ded by *ppHead..
f0430 2a 2a 20 2a 70 70 48 65 61 64 20 69 73 20 65 69 ** *ppHead is ei
f0440 74 68 65 72 20 50 43 61 63 68 65 2e 70 43 6c 65 ther PCache.pCle
f0450 61 6e 20 6f 72 20 50 43 61 63 68 65 2e 70 44 69 an or PCache.pDi
f0460 72 74 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 rty..*/.static v
f0470 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 4c oid pcacheAddToL
f0480 69 73 74 28 50 67 48 64 72 20 2a 2a 70 70 48 65 ist(PgHdr **ppHe
f0490 61 64 2c 20 50 67 48 64 72 20 2a 70 50 61 67 65 ad, PgHdr *pPage
f04a0 29 7b 0a 20 20 69 6e 74 20 69 73 44 69 72 74 79 ){. int isDirty
f04b0 4c 69 73 74 20 3d 20 28 70 70 48 65 61 64 3d 3d List = (ppHead==
f04c0 26 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e &pPage->pCache->
f04d0 70 44 69 72 74 79 29 3b 0a 20 20 61 73 73 65 72 pDirty);. asser
f04e0 74 28 20 70 70 48 65 61 64 3d 3d 26 70 50 61 67 t( ppHead==&pPag
f04f0 65 2d 3e 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 e->pCache->pClea
f0500 6e 20 7c 7c 20 70 70 48 65 61 64 3d 3d 26 70 50 n || ppHead==&pP
f0510 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 70 44 69 age->pCache->pDi
f0520 72 74 79 20 29 3b 0a 0a 20 20 69 66 28 20 28 2a rty );.. if( (*
f0530 70 70 48 65 61 64 29 20 29 7b 0a 20 20 20 20 28 ppHead) ){. (
f0540 2a 70 70 48 65 61 64 29 2d 3e 70 50 72 65 76 20 *ppHead)->pPrev
f0550 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 70 = pPage;. }. p
f0560 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 Page->pNext = *p
f0570 70 48 65 61 64 3b 0a 20 20 70 50 61 67 65 2d 3e pHead;. pPage->
f0580 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 2a 70 70 pPrev = 0;. *pp
f0590 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 0a 20 Head = pPage;..
f05a0 20 69 66 28 20 69 73 44 69 72 74 79 4c 69 73 74 if( isDirtyList
f05b0 20 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a ){. PCache *
f05c0 70 43 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e pCache = pPage->
f05d0 70 43 61 63 68 65 3b 0a 20 20 20 20 69 66 28 20 pCache;. if(
f05e0 21 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 !pCache->pDirtyT
f05f0 61 69 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ail ){. ass
f0600 65 72 74 28 20 70 50 61 67 65 2d 3e 70 4e 65 78 ert( pPage->pNex
f0610 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 43 t==0 );. pC
f0620 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c ache->pDirtyTail
f0630 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a = pPage;. }.
f0640 20 20 20 20 69 66 28 20 21 70 43 61 63 68 65 2d if( !pCache-
f0650 3e 70 53 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 >pSynced && 0==(
f0660 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 pPage->flags&PGH
f0670 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b DR_NEED_SYNC) ){
f0680 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70 . pCache->p
f0690 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a Synced = pPage;.
f06a0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
f06b0 2a 2a 20 52 65 6d 6f 76 65 20 61 20 70 61 67 65 ** Remove a page
f06c0 20 66 72 6f 6d 20 74 68 65 20 67 6c 6f 62 61 6c from the global
f06d0 20 4c 52 55 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 LRU list.*/.sta
f06e0 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 52 tic void pcacheR
f06f0 65 6d 6f 76 65 46 72 6f 6d 4c 72 75 4c 69 73 74 emoveFromLruList
f0700 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a (PgHdr *pPage){.
f0710 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
f0720 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 3_mutex_held(pca
f0730 63 68 65 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a che_g.mutex) );.
f0740 20 20 61 73 73 65 72 74 28 20 28 70 50 61 67 65 assert( (pPage
f0750 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
f0760 52 54 59 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 RTY)==0 );. if(
f0770 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e pPage->pCache->
f0780 62 50 75 72 67 65 61 62 6c 65 3d 3d 30 20 29 20 bPurgeable==0 )
f0790 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 return;. if( pP
f07a0 61 67 65 2d 3e 70 4e 65 78 74 4c 72 75 20 29 7b age->pNextLru ){
f07b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 61 . assert( pca
f07c0 63 68 65 5f 67 2e 70 4c 72 75 54 61 69 6c 21 3d che_g.pLruTail!=
f07d0 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70 50 61 pPage );. pPa
f07e0 67 65 2d 3e 70 4e 65 78 74 4c 72 75 2d 3e 70 50 ge->pNextLru->pP
f07f0 72 65 76 4c 72 75 20 3d 20 70 50 61 67 65 2d 3e revLru = pPage->
f0800 70 50 72 65 76 4c 72 75 3b 0a 20 20 7d 65 6c 73 pPrevLru;. }els
f0810 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
f0820 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 61 69 6c cache_g.pLruTail
f0830 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70 ==pPage );. p
f0840 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 61 69 6c cache_g.pLruTail
f0850 20 3d 20 70 50 61 67 65 2d 3e 70 50 72 65 76 4c = pPage->pPrevL
f0860 72 75 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 ru;. }. if( pP
f0870 61 67 65 2d 3e 70 50 72 65 76 4c 72 75 20 29 7b age->pPrevLru ){
f0880 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 61 . assert( pca
f0890 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 64 21 3d che_g.pLruHead!=
f08a0 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70 50 61 pPage );. pPa
f08b0 67 65 2d 3e 70 50 72 65 76 4c 72 75 2d 3e 70 4e ge->pPrevLru->pN
f08c0 65 78 74 4c 72 75 20 3d 20 70 50 61 67 65 2d 3e extLru = pPage->
f08d0 70 4e 65 78 74 4c 72 75 3b 0a 20 20 7d 65 6c 73 pNextLru;. }els
f08e0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
f08f0 63 61 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 64 cache_g.pLruHead
f0900 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70 ==pPage );. p
f0910 63 61 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 64 cache_g.pLruHead
f0920 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 4c = pPage->pNextL
f0930 72 75 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ru;. }.}../*.**
f0940 20 41 64 64 20 61 20 70 61 67 65 20 74 6f 20 74 Add a page to t
f0950 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 he global LRU li
f0960 73 74 2e 20 20 54 68 65 20 70 61 67 65 20 69 73 st. The page is
f0970 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 64 65 64 0a normally added.
f0980 2a 2a 20 74 6f 20 74 68 65 20 66 72 6f 6e 74 20 ** to the front
f0990 6f 66 20 74 68 65 20 6c 69 73 74 20 73 6f 20 74 of the list so t
f09a0 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 74 hat it will be t
f09b0 68 65 20 6c 61 73 74 20 70 61 67 65 20 72 65 63 he last page rec
f09c0 79 63 6c 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 ycled..** Howeve
f09d0 72 2c 20 69 66 20 74 68 65 20 50 47 48 44 52 5f r, if the PGHDR_
f09e0 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 62 REUSE_UNLIKELY b
f09f0 69 74 20 69 73 20 73 65 74 2c 20 74 68 65 20 70 it is set, the p
f0a00 61 67 65 20 69 73 20 61 64 64 65 64 0a 2a 2a 20 age is added.**
f0a10 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 to the end of th
f0a20 65 20 4c 52 55 20 6c 69 73 74 20 73 6f 20 74 68 e LRU list so th
f0a30 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 74 68 at it will be th
f0a40 65 20 6e 65 78 74 20 74 6f 20 62 65 20 72 65 63 e next to be rec
f0a50 79 63 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 ycled..*/.static
f0a60 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 void pcacheAddT
f0a70 6f 4c 72 75 4c 69 73 74 28 50 67 48 64 72 20 2a oLruList(PgHdr *
f0a80 70 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 pPage){. assert
f0a90 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
f0aa0 68 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 held(pcache_g.mu
f0ab0 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
f0ac0 28 20 28 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 ( (pPage->flags&
f0ad0 50 47 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 PGHDR_DIRTY)==0
f0ae0 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e );. if( pPage->
f0af0 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
f0b00 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a le==0 ) return;.
f0b10 20 20 69 66 28 20 70 63 61 63 68 65 5f 67 2e 70 if( pcache_g.p
f0b20 4c 72 75 54 61 69 6c 20 26 26 20 28 70 50 61 67 LruTail && (pPag
f0b30 65 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 e->flags & PGHDR
f0b40 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 29 _REUSE_UNLIKELY)
f0b50 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 !=0 ){. /* If
f0b60 20 72 65 75 73 65 20 69 73 20 75 6e 6c 69 6b 65 reuse is unlike
f0b70 6c 79 2e 20 20 50 75 74 20 74 68 65 20 70 61 67 ly. Put the pag
f0b80 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 e at the end of
f0b90 74 68 65 20 4c 52 55 20 6c 69 73 74 0a 20 20 20 the LRU list.
f0ba0 20 2a 2a 20 77 68 65 72 65 20 69 74 20 77 69 6c ** where it wil
f0bb0 6c 20 62 65 20 72 65 63 79 63 6c 65 64 20 73 6f l be recycled so
f0bc0 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e oner rather than
f0bd0 20 6c 61 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a later. . */.
f0be0 20 20 20 20 61 73 73 65 72 74 28 20 70 63 61 63 assert( pcac
f0bf0 68 65 5f 67 2e 70 4c 72 75 48 65 61 64 20 29 3b he_g.pLruHead );
f0c00 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 . pPage->pNex
f0c10 74 4c 72 75 20 3d 20 30 3b 0a 20 20 20 20 70 50 tLru = 0;. pP
f0c20 61 67 65 2d 3e 70 50 72 65 76 4c 72 75 20 3d 20 age->pPrevLru =
f0c30 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 61 69 pcache_g.pLruTai
f0c40 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 5f 67 2e l;. pcache_g.
f0c50 70 4c 72 75 54 61 69 6c 2d 3e 70 4e 65 78 74 4c pLruTail->pNextL
f0c60 72 75 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 ru = pPage;.
f0c70 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 61 69 pcache_g.pLruTai
f0c80 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 70 l = pPage;. p
f0c90 50 61 67 65 2d 3e 66 6c 61 67 73 20 26 3d 20 7e Page->flags &= ~
f0ca0 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 PGHDR_REUSE_UNLI
f0cb0 4b 45 4c 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 KELY;. }else{.
f0cc0 20 20 20 2f 2a 20 49 66 20 72 65 75 73 65 20 69 /* If reuse i
f0cd0 73 20 70 6f 73 73 69 62 6c 65 2e 20 74 68 65 20 s possible. the
f0ce0 70 61 67 65 20 67 6f 65 73 20 61 74 20 74 68 65 page goes at the
f0cf0 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
f0d00 65 20 4c 52 55 0a 20 20 20 20 2a 2a 20 6c 69 73 e LRU. ** lis
f0d10 74 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c t so that it wil
f0d20 6c 20 62 65 20 74 68 65 20 6c 61 73 74 20 74 6f l be the last to
f0d30 20 62 65 20 72 65 63 79 63 6c 65 64 2e 0a 20 20 be recycled..
f0d40 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 */. if( pca
f0d50 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 64 20 29 che_g.pLruHead )
f0d60 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 5f 67 {. pcache_g
f0d70 2e 70 4c 72 75 48 65 61 64 2d 3e 70 50 72 65 76 .pLruHead->pPrev
f0d80 4c 72 75 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 Lru = pPage;.
f0d90 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e }. pPage->pN
f0da0 65 78 74 4c 72 75 20 3d 20 70 63 61 63 68 65 5f extLru = pcache_
f0db0 67 2e 70 4c 72 75 48 65 61 64 3b 0a 20 20 20 20 g.pLruHead;.
f0dc0 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 pcache_g.pLruHea
f0dd0 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 70 d = pPage;. p
f0de0 50 61 67 65 2d 3e 70 50 72 65 76 4c 72 75 20 3d Page->pPrevLru =
f0df0 20 30 3b 0a 20 20 20 20 69 66 28 20 70 63 61 63 0;. if( pcac
f0e00 68 65 5f 67 2e 70 4c 72 75 54 61 69 6c 3d 3d 30 he_g.pLruTail==0
f0e10 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ){. pcache
f0e20 5f 67 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 _g.pLruTail = pP
f0e30 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d age;. }. }.}
f0e40 0a 0a 2f 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 2a 2a 2a 2a ****************
f0e70 2a 2a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 ** Memory Alloca
f0e80 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tion ***********
f0e90 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a .**.** Initializ
f0ea0 65 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 e the page cache
f0eb0 20 6d 65 6d 6f 72 79 20 70 6f 6f 6c 2e 0a 2a 2a memory pool..**
f0ec0 0a 2a 2a 20 54 68 69 73 20 6d 75 73 74 20 62 65 .** This must be
f0ed0 20 63 61 6c 6c 65 64 20 61 74 20 73 74 61 72 74 called at start
f0ee0 2d 74 69 6d 65 20 77 68 65 6e 20 6e 6f 20 70 61 -time when no pa
f0ef0 67 65 20 63 61 63 68 65 20 6c 69 6e 65 73 20 61 ge cache lines a
f0f00 72 65 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 re.** checked ou
f0f10 74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e t. This function
f0f20 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 is not threadsa
f0f30 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 fe..*/.SQLITE_PR
f0f40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
f0f50 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 e3PCacheBufferSe
f0f60 74 75 70 28 76 6f 69 64 20 2a 70 42 75 66 2c 20 tup(void *pBuf,
f0f70 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a int sz, int n){.
f0f80 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b PgFreeslot *p;
f0f90 0a 20 20 73 7a 20 26 3d 20 7e 37 3b 0a 20 20 70 . sz &= ~7;. p
f0fa0 63 61 63 68 65 5f 67 2e 73 7a 53 6c 6f 74 20 3d cache_g.szSlot =
f0fb0 20 73 7a 3b 0a 20 20 70 63 61 63 68 65 5f 67 2e sz;. pcache_g.
f0fc0 70 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20 pStart = pBuf;.
f0fd0 20 70 63 61 63 68 65 5f 67 2e 70 46 72 65 65 20 pcache_g.pFree
f0fe0 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d = 0;. while( n-
f0ff0 2d 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 50 67 - ){. p = (Pg
f1000 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a Freeslot*)pBuf;.
f1010 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
f1020 63 61 63 68 65 5f 67 2e 70 46 72 65 65 3b 0a 20 cache_g.pFree;.
f1030 20 20 20 70 63 61 63 68 65 5f 67 2e 70 46 72 65 pcache_g.pFre
f1040 65 20 3d 20 70 3b 0a 20 20 20 20 70 42 75 66 20 e = p;. pBuf
f1050 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 = (void*)&((char
f1060 2a 29 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 7d *)pBuf)[sz];. }
f1070 0a 20 20 70 63 61 63 68 65 5f 67 2e 70 45 6e 64 . pcache_g.pEnd
f1080 20 3d 20 70 42 75 66 3b 0a 7d 0a 0a 2f 2a 0a 2a = pBuf;.}../*.*
f1090 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 70 61 67 * Allocate a pag
f10a0 65 20 63 61 63 68 65 20 6c 69 6e 65 2e 20 20 4c e cache line. L
f10b0 6f 6f 6b 20 69 6e 20 74 68 65 20 70 61 67 65 20 ook in the page
f10c0 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 70 6f 6f cache memory poo
f10d0 6c 20 66 69 72 73 74 0a 2a 2a 20 61 6e 64 20 75 l first.** and u
f10e0 73 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 66 72 se an element fr
f10f0 6f 6d 20 69 74 20 66 69 72 73 74 20 69 66 20 61 om it first if a
f1100 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 6e 6f vailable. If no
f1110 74 68 69 6e 67 20 69 73 20 61 76 61 69 6c 61 62 thing is availab
f1120 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 le.** in the pag
f1130 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 70 e cache memory p
f1140 6f 6f 6c 2c 20 67 6f 20 74 6f 20 74 68 65 20 67 ool, go to the g
f1150 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 6d eneral purpose m
f1160 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e emory allocator.
f1170 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
f1180 2a 70 63 61 63 68 65 4d 61 6c 6c 6f 63 28 69 6e *pcacheMalloc(in
f1190 74 20 73 7a 2c 20 50 43 61 63 68 65 20 2a 70 43 t sz, PCache *pC
f11a0 61 63 68 65 29 7b 0a 20 20 61 73 73 65 72 74 28 ache){. assert(
f11b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
f11c0 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 74 eld(pcache_g.mut
f11d0 65 78 29 20 29 3b 0a 20 20 69 66 28 20 73 7a 3c ex) );. if( sz<
f11e0 3d 70 63 61 63 68 65 5f 67 2e 73 7a 53 6c 6f 74 =pcache_g.szSlot
f11f0 20 26 26 20 70 63 61 63 68 65 5f 67 2e 70 46 72 && pcache_g.pFr
f1200 65 65 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 ee ){. PgFree
f1210 73 6c 6f 74 20 2a 70 20 3d 20 70 63 61 63 68 65 slot *p = pcache
f1220 5f 67 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 _g.pFree;. pc
f1230 61 63 68 65 5f 67 2e 70 46 72 65 65 20 3d 20 70 ache_g.pFree = p
f1240 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c ->pNext;. sql
f1250 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 ite3StatusSet(SQ
f1260 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
f1270 43 41 43 48 45 5f 53 49 5a 45 2c 20 73 7a 29 3b CACHE_SIZE, sz);
f1280 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 . sqlite3Stat
f1290 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
f12a0 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 TUS_PAGECACHE_US
f12b0 45 44 2c 20 31 29 3b 0a 20 20 20 20 72 65 74 75 ED, 1);. retu
f12c0 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 20 20 7d rn (void*)p;. }
f12d0 65 6c 73 65 7b 0a 20 20 20 20 76 6f 69 64 20 2a else{. void *
f12e0 70 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 p;.. /* Alloc
f12f0 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 ate a new buffer
f1300 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 using sqlite3Ma
f1310 6c 6c 6f 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 lloc. Before doi
f1320 6e 67 20 73 6f 2c 20 65 78 69 74 20 74 68 65 0a ng so, exit the.
f1330 20 20 20 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 ** global pc
f1340 61 63 68 65 20 6d 75 74 65 78 20 61 6e 64 20 75 ache mutex and u
f1350 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2d nlock the pager-
f1360 63 61 63 68 65 20 6f 62 6a 65 63 74 20 70 43 61 cache object pCa
f1370 63 68 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20 che. This is .
f1380 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 69 66 20 ** so that if
f1390 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61 the attempt to a
f13a0 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 62 75 llocate a new bu
f13b0 66 66 65 72 20 63 61 75 73 65 73 20 74 68 65 20 ffer causes the
f13c0 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 the . ** conf
f13d0 69 67 75 72 65 64 20 73 6f 66 74 2d 68 65 61 70 igured soft-heap
f13e0 2d 6c 69 6d 69 74 20 74 6f 20 62 65 20 62 72 65 -limit to be bre
f13f0 61 63 68 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 ached, it will b
f1400 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 e possible to.
f1410 20 20 2a 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d ** reclaim mem
f1420 6f 72 79 20 66 72 6f 6d 20 74 68 69 73 20 70 61 ory from this pa
f1430 67 65 72 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a ger-cache.. *
f1440 2f 0a 20 20 20 20 70 63 61 63 68 65 45 78 69 74 /. pcacheExit
f1450 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d Mutex();. p =
f1460 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 sqlite3Malloc(s
f1470 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 65 45 6e z);. pcacheEn
f1480 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 20 terMutex();..
f1490 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 if( p ){.
f14a0 73 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c sz = sqlite3Mall
f14b0 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 ocSize(p);.
f14c0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
f14d0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
f14e0 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c PAGECACHE_OVERFL
f14f0 4f 57 2c 20 73 7a 29 3b 0a 20 20 20 20 7d 0a 20 OW, sz);. }.
f1500 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d return p;. }
f1510 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
f1520 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 E void *sqlite3P
f1530 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a ageMalloc(int sz
f1540 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 ){. void *p;.
f1550 70 63 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 pcacheEnterMutex
f1560 28 29 3b 0a 20 20 70 20 3d 20 70 63 61 63 68 65 ();. p = pcache
f1570 4d 61 6c 6c 6f 63 28 73 7a 2c 20 30 29 3b 0a 20 Malloc(sz, 0);.
f1580 20 70 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 pcacheExitMutex
f1590 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a ();. return p;.
f15a0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 }../*.** Release
f15b0 20 61 20 70 61 67 65 72 20 6d 65 6d 6f 72 79 20 a pager memory
f15c0 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 allocation.*/.st
f15d0 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
f15e0 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 Free(void *p){.
f15f0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
f1600 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 _mutex_held(pcac
f1610 68 65 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a 20 he_g.mutex) );.
f1620 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
f1630 72 6e 3b 0a 20 20 69 66 28 20 70 3e 3d 70 63 61 rn;. if( p>=pca
f1640 63 68 65 5f 67 2e 70 53 74 61 72 74 20 26 26 20 che_g.pStart &&
f1650 70 3c 70 63 61 63 68 65 5f 67 2e 70 45 6e 64 20 p<pcache_g.pEnd
f1660 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c 6f ){. PgFreeslo
f1670 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73 71 t *pSlot;. sq
f1680 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
f1690 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
f16a0 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d 31 29 ECACHE_USED, -1)
f16b0 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20 28 50 ;. pSlot = (P
f16c0 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a 20 20 gFreeslot*)p;.
f16d0 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 3d pSlot->pNext =
f16e0 20 70 63 61 63 68 65 5f 67 2e 70 46 72 65 65 3b pcache_g.pFree;
f16f0 0a 20 20 20 20 70 63 61 63 68 65 5f 67 2e 70 46 . pcache_g.pF
f1700 72 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d ree = pSlot;. }
f1710 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 53 else{. int iS
f1720 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c ize = sqlite3Mal
f1730 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 locSize(p);.
f1740 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
f1750 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 (SQLITE_STATUS_P
f1760 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f AGECACHE_OVERFLO
f1770 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 W, -iSize);.
f1780 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
f1790 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 . }.}.SQLITE_PR
f17a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
f17b0 65 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 e3PageFree(void
f17c0 2a 70 29 7b 0a 20 20 70 63 61 63 68 65 45 6e 74 *p){. pcacheEnt
f17d0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 erMutex();. pca
f17e0 63 68 65 46 72 65 65 28 70 29 3b 0a 20 20 70 63 cheFree(p);. pc
f17f0 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 3b acheExitMutex();
f1800 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
f1810 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 0a 2a te a new page..*
f1820 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a /.static PgHdr *
f1830 70 63 61 63 68 65 50 61 67 65 41 6c 6c 6f 63 28 pcachePageAlloc(
f1840 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
f1850 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 69 . PgHdr *p;. i
f1860 6e 74 20 73 7a 20 3d 20 73 69 7a 65 6f 66 28 2a nt sz = sizeof(*
f1870 70 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 50 p) + pCache->szP
f1880 61 67 65 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a age + pCache->sz
f1890 45 78 74 72 61 3b 0a 20 20 61 73 73 65 72 74 28 Extra;. assert(
f18a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
f18b0 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 74 eld(pcache_g.mut
f18c0 65 78 29 20 29 3b 0a 20 20 70 20 3d 20 70 63 61 ex) );. p = pca
f18d0 63 68 65 4d 61 6c 6c 6f 63 28 73 7a 2c 20 70 43 cheMalloc(sz, pC
f18e0 61 63 68 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d ache);. if( p==
f18f0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
f1900 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a memset(p, 0, siz
f1910 65 6f 66 28 50 67 48 64 72 29 29 3b 0a 20 20 70 eof(PgHdr));. p
f1920 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 2a ->pData = (void*
f1930 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e 70 45 78 )&p[1];. p->pEx
f1940 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 tra = (void*)&((
f1950 63 68 61 72 2a 29 70 2d 3e 70 44 61 74 61 29 5b char*)p->pData)[
f1960 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 5d 3b pCache->szPage];
f1970 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 . if( pCache->b
f1980 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 Purgeable ){.
f1990 20 70 63 61 63 68 65 5f 67 2e 6e 43 75 72 72 65 pcache_g.nCurre
f19a0 6e 74 50 61 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 ntPage++;. }.
f19b0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
f19c0 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 20 ** Deallocate a
f19d0 70 61 67 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 page.*/.static v
f19e0 6f 69 64 20 70 63 61 63 68 65 50 61 67 65 46 72 oid pcachePageFr
f19f0 65 65 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 ee(PgHdr *p){.
f1a00 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
f1a10 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 mutex_held(pcach
f1a20 65 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 e_g.mutex) );.
f1a30 69 66 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 62 if( p->pCache->b
f1a40 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 Purgeable ){.
f1a50 20 70 63 61 63 68 65 5f 67 2e 6e 43 75 72 72 65 pcache_g.nCurre
f1a60 6e 74 50 61 67 65 2d 2d 3b 0a 20 20 7d 0a 20 20 ntPage--;. }.
f1a70 70 63 61 63 68 65 46 72 65 65 28 70 29 3b 0a 7d pcacheFree(p);.}
f1a80 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
f1a90 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
f1aa0 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 52 NAGEMENT./*.** R
f1ab0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
f1ac0 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 77 of bytes that w
f1ad0 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 ill be returned
f1ae0 74 6f 20 74 68 65 20 68 65 61 70 20 77 68 65 6e to the heap when
f1af0 0a 2a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e 74 .** the argument
f1b00 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 70 63 is passed to pc
f1b10 61 63 68 65 50 61 67 65 46 72 65 65 28 29 2e 0a achePageFree()..
f1b20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 */.static int pc
f1b30 61 63 68 65 50 61 67 65 53 69 7a 65 28 50 67 48 achePageSize(PgH
f1b40 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 dr *p){. assert
f1b50 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
f1b60 68 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 held(pcache_g.mu
f1b70 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
f1b80 28 20 21 70 63 61 63 68 65 5f 67 2e 70 53 74 61 ( !pcache_g.pSta
f1b90 72 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 rt );. assert(
f1ba0 70 2d 3e 61 70 53 61 76 65 5b 30 5d 3d 3d 30 20 p->apSave[0]==0
f1bb0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
f1bc0 61 70 53 61 76 65 5b 31 5d 3d 3d 30 20 29 3b 0a apSave[1]==0 );.
f1bd0 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 assert( p && p
f1be0 2d 3e 70 43 61 63 68 65 20 29 3b 0a 20 20 72 65 ->pCache );. re
f1bf0 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c turn sqlite3Mall
f1c00 6f 63 53 69 7a 65 28 70 29 3b 0a 7d 0a 23 65 6e ocSize(p);.}.#en
f1c10 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d dif../*.** Attem
f1c20 70 74 20 74 6f 20 27 72 65 63 79 63 6c 65 27 20 pt to 'recycle'
f1c30 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
f1c40 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e global LRU list.
f1c50 20 4f 6e 6c 79 20 63 6c 65 61 6e 2c 0a 2a 2a 20 Only clean,.**
f1c60 75 6e 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 unreferenced pag
f1c70 65 73 20 66 72 6f 6d 20 70 75 72 67 65 61 62 6c es from purgeabl
f1c80 65 20 63 61 63 68 65 73 20 61 72 65 20 65 6c 69 e caches are eli
f1c90 67 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c gible for recycl
f1ca0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ing..**.** This
f1cb0 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65 73 function removes
f1cc0 20 70 61 67 65 20 70 63 61 63 68 65 2e 70 4c 72 page pcache.pLr
f1cd0 75 54 61 69 6c 20 66 72 6f 6d 20 74 68 65 20 67 uTail from the g
f1ce0 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 0a lobal LRU list,.
f1cf0 2a 2a 20 61 6e 64 20 66 72 6f 6d 20 74 68 65 20 ** and from the
f1d00 68 61 73 68 2d 74 61 62 6c 65 20 61 6e 64 20 50 hash-table and P
f1d10 43 61 63 68 65 2e 70 43 6c 65 61 6e 20 6c 69 73 Cache.pClean lis
f1d20 74 20 6f 66 20 74 68 65 20 6f 77 6e 65 72 20 70 t of the owner p
f1d30 63 61 63 68 65 2e 0a 2a 2a 20 54 68 65 72 65 20 cache..** There
f1d40 73 68 6f 75 6c 64 20 62 65 20 6e 6f 20 6f 74 68 should be no oth
f1d50 65 72 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f er references to
f1d60 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
f1d70 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 A pointer to th
f1d80 65 20 72 65 63 79 63 6c 65 64 20 70 61 67 65 20 e recycled page
f1d90 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 is returned, or
f1da0 4e 55 4c 4c 20 69 66 20 6e 6f 20 70 61 67 65 20 NULL if no page
f1db0 69 73 0a 2a 2a 20 65 6c 69 67 69 62 6c 65 20 66 is.** eligible f
f1dc0 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e 0a 2a 2f or recycling..*/
f1dd0 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 .static PgHdr *p
f1de0 63 61 63 68 65 52 65 63 79 63 6c 65 50 61 67 65 cacheRecyclePage
f1df0 28 76 6f 69 64 29 7b 0a 20 20 50 67 48 64 72 20 (void){. PgHdr
f1e00 2a 70 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 *p = 0;. assert
f1e10 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
f1e20 68 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 held(pcache_g.mu
f1e30 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 28 20 28 tex) );.. if( (
f1e40 70 3d 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 p=pcache_g.pLruT
f1e50 61 69 6c 29 20 29 7b 0a 20 20 20 20 61 73 73 65 ail) ){. asse
f1e60 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 rt( (p->flags&PG
f1e70 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 29 3b HDR_DIRTY)==0 );
f1e80 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 . pcacheRemov
f1e90 65 46 72 6f 6d 4c 72 75 4c 69 73 74 28 70 29 3b eFromLruList(p);
f1ea0 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 . pcacheRemov
f1eb0 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20 eFromHash(p);.
f1ec0 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 pcacheRemoveFr
f1ed0 6f 6d 4c 69 73 74 28 26 70 2d 3e 70 43 61 63 68 omList(&p->pCach
f1ee0 65 2d 3e 70 43 6c 65 61 6e 2c 20 70 29 3b 0a 20 e->pClean, p);.
f1ef0 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }.. return p;.
f1f00 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 }../*.** Obtain
f1f10 73 70 61 63 65 20 66 6f 72 20 61 20 70 61 67 65 space for a page
f1f20 2e 20 54 72 79 20 74 6f 20 72 65 63 79 63 6c 65 . Try to recycle
f1f30 20 61 6e 20 6f 6c 64 20 70 61 67 65 20 69 66 20 an old page if
f1f40 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 the limit on the
f1f50 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 .** number of p
f1f60 61 67 65 73 20 68 61 73 20 62 65 65 6e 20 72 65 ages has been re
f1f70 61 63 68 65 64 2e 20 49 66 20 74 68 65 20 6c 69 ached. If the li
f1f80 6d 69 74 20 68 61 73 20 6e 6f 74 20 62 65 65 6e mit has not been
f1f90 20 72 65 61 63 68 65 64 20 6f 72 0a 2a 2a 20 74 reached or.** t
f1fa0 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67 65 here are no page
f1fb0 73 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 72 s eligible for r
f1fc0 65 63 79 63 6c 69 6e 67 2c 20 61 6c 6c 6f 63 61 ecycling, alloca
f1fd0 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 0a 2a te a new page..*
f1fe0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
f1ff0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 inter to the new
f2000 20 70 61 67 65 2c 20 6f 72 20 4e 55 4c 4c 20 69 page, or NULL i
f2010 66 20 61 6e 20 4f 4f 4d 20 63 6f 6e 64 69 74 69 f an OOM conditi
f2020 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 on occurs..*/.st
f2030 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 52 atic int pcacheR
f2040 65 63 79 63 6c 65 4f 72 41 6c 6c 6f 63 28 50 43 ecycleOrAlloc(PC
f2050 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67 ache *pCache, Pg
f2060 48 64 72 20 2a 2a 70 70 50 61 67 65 29 7b 0a 20 Hdr **ppPage){.
f2070 20 50 67 48 64 72 20 2a 70 20 3d 20 30 3b 0a 0a PgHdr *p = 0;..
f2080 20 20 69 6e 74 20 73 7a 50 61 67 65 20 3d 20 70 int szPage = p
f2090 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 Cache->szPage;.
f20a0 20 69 6e 74 20 73 7a 45 78 74 72 61 20 3d 20 70 int szExtra = p
f20b0 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 3b 0a Cache->szExtra;.
f20c0 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68 . assert( pcach
f20d0 65 5f 67 2e 69 73 49 6e 69 74 20 29 3b 0a 20 20 e_g.isInit );.
f20e0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
f20f0 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 mutex_held(pcach
f2100 65 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 e_g.mutex) );..
f2110 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 *ppPage = 0;..
f2120 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 72 /* If we have r
f2130 65 61 63 68 65 64 20 65 69 74 68 65 72 20 74 68 eached either th
f2140 65 20 67 6c 6f 62 61 6c 20 6f 72 20 74 68 65 20 e global or the
f2150 6c 6f 63 61 6c 20 6c 69 6d 69 74 20 66 6f 72 20 local limit for
f2160 0a 20 20 2a 2a 20 70 69 6e 6e 65 64 2b 64 69 72 . ** pinned+dir
f2170 74 79 20 70 61 67 65 73 2c 20 61 6e 64 20 74 68 ty pages, and th
f2180 65 72 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 ere is at least
f2190 6f 6e 65 20 64 69 72 74 79 20 70 61 67 65 2c 0a one dirty page,.
f21a0 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 ** invoke the
f21b0 78 53 74 72 65 73 73 20 63 61 6c 6c 62 61 63 6b xStress callback
f21c0 20 74 6f 20 63 61 75 73 65 20 61 20 70 61 67 65 to cause a page
f21d0 20 74 6f 20 62 65 63 6f 6d 65 20 63 6c 65 61 6e to become clean
f21e0 2e 0a 20 20 2a 2f 0a 20 20 65 78 70 65 6e 73 69 .. */. expensi
f21f0 76 65 5f 61 73 73 65 72 74 28 20 70 43 61 63 68 ve_assert( pCach
f2200 65 2d 3e 6e 50 69 6e 6e 65 64 3d 3d 70 63 61 63 e->nPinned==pcac
f2210 68 65 50 69 6e 6e 65 64 43 6f 75 6e 74 28 70 43 hePinnedCount(pC
f2220 61 63 68 65 29 20 29 3b 0a 20 20 65 78 70 65 6e ache) );. expen
f2230 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 sive_assert( pca
f2240 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 cheCheckSynced(p
f2250 43 61 63 68 65 29 20 29 3b 0a 20 20 69 66 28 20 Cache) );. if(
f2260 70 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73 0a pCache->xStress.
f2270 20 20 20 26 26 20 70 43 61 63 68 65 2d 3e 70 44 && pCache->pD
f2280 69 72 74 79 0a 20 20 20 26 26 20 28 70 43 61 63 irty. && (pCac
f2290 68 65 2d 3e 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 he->nPinned>=(pc
f22a0 61 63 68 65 5f 67 2e 6e 4d 61 78 50 61 67 65 2b ache_g.nMaxPage+
f22b0 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 pCache->nMin-pca
f22c0 63 68 65 5f 67 2e 6e 4d 69 6e 50 61 67 65 29 0a che_g.nMinPage).
f22d0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43 || pC
f22e0 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 3e 3d 70 ache->nPinned>=p
f22f0 43 61 63 68 65 2d 3e 6e 4d 61 78 29 0a 20 20 29 Cache->nMax). )
f2300 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 {. PgHdr *pPg
f2310 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 43 61 ;. assert(pCa
f2320 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 29 che->pDirtyTail)
f2330 3b 0a 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 ;.. for(pPg=p
f2340 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 Cache->pSynced;
f2350 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20 . pPg &&
f2360 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 (pPg->nRef || (p
f2370 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
f2380 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 NEED_SYNC)); .
f2390 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 pPg=pPg->p
f23a0 50 72 65 76 0a 20 20 20 20 29 3b 0a 20 20 20 20 Prev. );.
f23b0 69 66 28 20 21 70 50 67 20 29 7b 0a 20 20 20 20 if( !pPg ){.
f23c0 20 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 for(pPg=pCache
f23d0 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 50 ->pDirtyTail; pP
f23e0 67 20 26 26 20 70 50 67 2d 3e 6e 52 65 66 3b 20 g && pPg->nRef;
f23f0 70 50 67 3d 70 50 67 2d 3e 70 50 72 65 76 29 3b pPg=pPg->pPrev);
f2400 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
f2410 50 67 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 Pg ){. int
f2420 72 63 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 rc;. pcache
f2430 45 78 69 74 4d 75 74 65 78 28 29 3b 0a 20 20 20 ExitMutex();.
f2440 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e rc = pCache->
f2450 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e xStress(pCache->
f2460 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20 pStress, pPg);.
f2470 20 20 20 20 20 70 63 61 63 68 65 45 6e 74 65 72 pcacheEnter
f2480 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 69 Mutex();. i
f2490 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
f24a0 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 && rc!=SQLITE_B
f24b0 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 72 USY ){. r
f24c0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
f24d0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f }. }. }.. /
f24e0 2a 20 49 66 20 65 69 74 68 65 72 20 74 68 65 20 * If either the
f24f0 6c 6f 63 61 6c 20 6f 72 20 74 68 65 20 67 6c 6f local or the glo
f2500 62 61 6c 20 70 61 67 65 20 6c 69 6d 69 74 20 68 bal page limit h
f2510 61 73 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c as been reached,
f2520 20 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20 72 65 . ** try to re
f2530 63 79 63 6c 65 20 61 20 70 61 67 65 2e 20 0a 20 cycle a page. .
f2540 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65 */. if( pCache
f2550 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 26 20 ->bPurgeable &&
f2560 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3e 3d (pCache->nPage>=
f2570 70 43 61 63 68 65 2d 3e 6e 4d 61 78 2d 31 20 7c pCache->nMax-1 |
f2580 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |.
f2590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
f25a0 63 61 63 68 65 5f 67 2e 6e 43 75 72 72 65 6e 74 cache_g.nCurrent
f25b0 50 61 67 65 3e 3d 70 63 61 63 68 65 5f 67 2e 6e Page>=pcache_g.n
f25c0 4d 61 78 50 61 67 65 29 20 29 7b 0a 20 20 20 20 MaxPage) ){.
f25d0 70 20 3d 20 70 63 61 63 68 65 52 65 63 79 63 6c p = pcacheRecycl
f25e0 65 50 61 67 65 28 29 3b 0a 20 20 7d 0a 0a 20 20 ePage();. }..
f25f0 2f 2a 20 49 66 20 61 20 70 61 67 65 20 68 61 73 /* If a page has
f2600 20 62 65 65 6e 20 72 65 63 79 63 6c 65 64 20 62 been recycled b
f2610 75 74 20 69 74 20 69 73 20 74 68 65 20 77 72 6f ut it is the wro
f2620 6e 67 20 73 69 7a 65 2c 20 66 72 65 65 20 69 74 ng size, free it
f2630 2e 20 2a 2f 0a 20 20 69 66 28 20 70 20 26 26 20 . */. if( p &&
f2640 28 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 (p->pCache->szPa
f2650 67 65 21 3d 73 7a 50 61 67 65 20 7c 7c 20 70 2d ge!=szPage || p-
f2660 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 >pCache->szPage!
f2670 3d 73 7a 45 78 74 72 61 29 20 29 7b 0a 20 20 20 =szExtra) ){.
f2680 20 70 63 61 63 68 65 50 61 67 65 46 72 65 65 28 pcachePageFree(
f2690 70 29 3b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 p);. p = 0;.
f26a0 20 7d 0a 0a 20 20 69 66 28 20 21 70 20 29 7b 0a }.. if( !p ){.
f26b0 20 20 20 20 70 20 3d 20 70 63 61 63 68 65 50 61 p = pcachePa
f26c0 67 65 41 6c 6c 6f 63 28 70 43 61 63 68 65 29 3b geAlloc(pCache);
f26d0 0a 20 20 7d 0a 0a 20 20 2a 70 70 50 61 67 65 20 . }.. *ppPage
f26e0 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 = p;. return (p
f26f0 3f 53 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 ?SQLITE_OK:SQLIT
f2700 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 2a E_NOMEM);.}../**
f2710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2740 2a 20 47 65 6e 65 72 61 6c 20 49 6e 74 65 72 66 * General Interf
f2750 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a aces ******.**.*
f2760 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 * Initialize and
f2770 20 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 shutdown the pa
f2780 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 ge cache subsyst
f2790 65 6d 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74 em. Neither of t
f27a0 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f hese .** functio
f27b0 6e 73 20 61 72 65 20 74 68 72 65 61 64 73 61 66 ns are threadsaf
f27c0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
f27d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
f27e0 50 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 PcacheInitialize
f27f0 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 74 (void){. assert
f2800 28 20 70 63 61 63 68 65 5f 67 2e 69 73 49 6e 69 ( pcache_g.isIni
f2810 74 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 t==0 );. memset
f2820 28 26 70 63 61 63 68 65 5f 67 2c 20 30 2c 20 73 (&pcache_g, 0, s
f2830 69 7a 65 6f 66 28 70 63 61 63 68 65 29 29 3b 0a izeof(pcache));.
f2840 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
f2850 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d balConfig.bCoreM
f2860 75 74 65 78 20 29 7b 0a 20 20 20 20 2f 2a 20 4e utex ){. /* N
f2870 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 o need to check
f2880 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
f2890 20 6f 66 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 of sqlite3_mute
f28a0 78 5f 61 6c 6c 6f 63 28 29 2e 20 0a 20 20 20 20 x_alloc(). .
f28b0 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 20 61 20 ** Allocating a
f28c0 73 74 61 74 69 63 20 6d 75 74 65 78 20 63 61 6e static mutex can
f28d0 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2f not fail.. */
f28e0 0a 20 20 20 20 70 63 61 63 68 65 5f 67 2e 6d 75 . pcache_g.mu
f28f0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 tex = sqlite3_mu
f2900 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 tex_alloc(SQLITE
f2910 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
f2920 55 29 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 U);. }. pcache
f2930 5f 67 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 _g.isInit = 1;.
f2940 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f2950 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 K;.}.SQLITE_PRIV
f2960 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
f2970 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 PcacheShutdown(v
f2980 6f 69 64 29 7b 0a 20 20 6d 65 6d 73 65 74 28 26 oid){. memset(&
f2990 70 63 61 63 68 65 5f 67 2c 20 30 2c 20 73 69 7a pcache_g, 0, siz
f29a0 65 6f 66 28 70 63 61 63 68 65 29 29 3b 0a 7d 0a eof(pcache));.}.
f29b0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
f29c0 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 e size in bytes
f29d0 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 of a PCache obje
f29e0 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ct..*/.SQLITE_PR
f29f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
f2a00 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 3PcacheSize(void
f2a10 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 ){ return sizeof
f2a20 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a (PCache); }../*.
f2a30 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
f2a40 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 PCache object.
f2a50 53 74 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f Storage space to
f2a60 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 hold the object
f2a70 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 .** has already
f2a80 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 been allocated a
f2a90 6e 64 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 nd is passed in
f2aa0 61 73 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 as the p pointer
f2ab0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
f2ac0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
f2ad0 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e PcacheOpen(. in
f2ae0 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20 t szPage,
f2af0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
f2b00 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 ze of every page
f2b10 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 */. int szExtr
f2b20 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a,
f2b30 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 /* Extra spac
f2b40 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
f2b50 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 h each page */.
f2b60 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c int bPurgeable,
f2b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f2b80 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 True if pages a
f2b90 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 re on backing st
f2ba0 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 ore */. int (*x
f2bb0 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 Stress)(void*,Pg
f2bc0 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f Hdr*),/* Call to
f2bd0 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 try to make pag
f2be0 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f es clean */. vo
f2bf0 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 id *pStress,
f2c00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
f2c10 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 gument to xStres
f2c20 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 s */. PCache *p
f2c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2c40 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 /* Prealloca
f2c50 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 ted space for th
f2c60 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20 e PCache */.){.
f2c70 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 5f assert( pcache_
f2c80 67 2e 69 73 49 6e 69 74 20 29 3b 0a 20 20 6d 65 g.isInit );. me
f2c90 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f mset(p, 0, sizeo
f2ca0 66 28 50 43 61 63 68 65 29 29 3b 0a 20 20 70 2d f(PCache));. p-
f2cb0 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 >szPage = szPage
f2cc0 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72 61 20 3d ;. p->szExtra =
f2cd0 20 73 7a 45 78 74 72 61 3b 0a 20 20 70 2d 3e 62 szExtra;. p->b
f2ce0 50 75 72 67 65 61 62 6c 65 20 3d 20 62 50 75 72 Purgeable = bPur
f2cf0 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 geable;. p->xSt
f2d00 72 65 73 73 20 3d 20 78 53 74 72 65 73 73 3b 0a ress = xStress;.
f2d10 20 20 70 2d 3e 70 53 74 72 65 73 73 20 3d 20 70 p->pStress = p
f2d20 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 Stress;. p->nMa
f2d30 78 20 3d 20 31 30 30 3b 0a 20 20 70 2d 3e 6e 4d x = 100;. p->nM
f2d40 69 6e 20 3d 20 31 30 3b 0a 0a 20 20 70 63 61 63 in = 10;.. pcac
f2d50 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a heEnterMutex();.
f2d60 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c 65 if( bPurgeable
f2d70 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 5f 67 ){. pcache_g
f2d80 2e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 70 2d 3e .nMaxPage += p->
f2d90 6e 4d 61 78 3b 0a 20 20 20 20 70 63 61 63 68 65 nMax;. pcache
f2da0 5f 67 2e 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 70 _g.nMinPage += p
f2db0 2d 3e 6e 4d 69 6e 3b 0a 20 20 7d 0a 0a 20 20 70 ->nMin;. }.. p
f2dc0 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 cacheExitMutex()
f2dd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
f2de0 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 e the page size
f2df0 66 6f 72 20 50 43 61 63 68 65 20 6f 62 6a 65 63 for PCache objec
f2e00 74 2e 20 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c t. This can onl
f2e10 79 20 68 61 70 70 65 6e 0a 2a 2a 20 77 68 65 6e y happen.** when
f2e20 20 74 68 65 20 63 61 63 68 65 20 69 73 20 65 6d the cache is em
f2e30 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 pty..*/.SQLITE_P
f2e40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
f2e50 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 te3PcacheSetPage
f2e60 53 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 Size(PCache *pCa
f2e70 63 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 che, int szPage)
f2e80 7b 0a 20 20 61 73 73 65 72 74 28 70 43 61 63 68 {. assert(pCach
f2e90 65 2d 3e 6e 50 61 67 65 3d 3d 30 29 3b 0a 20 20 e->nPage==0);.
f2ea0 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 3d pCache->szPage =
f2eb0 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a szPage;.}../*.*
f2ec0 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 * Try to obtain
f2ed0 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
f2ee0 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cache..*/.SQLITE
f2ef0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
f2f00 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
f2f10 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 . PCache *pCach
f2f20 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 e, /* Obta
f2f30 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d in the page from
f2f40 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 this cache */.
f2f50 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 Pgno pgno,
f2f60 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
f2f70 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 umber to obtain
f2f80 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65 46 */. int createF
f2f90 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49 66 lag, /* If
f2fa0 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70 61 true, create pa
f2fb0 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f ge if it does no
f2fc0 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79 20 t exist already
f2fd0 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70 50 */. PgHdr **ppP
f2fe0 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57 72 age /* Wr
f2ff0 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65 72 ite the page her
f3000 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
f3010 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
f3020 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 30 PgHdr *pPage = 0
f3030 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 ;.. assert( pca
f3040 63 68 65 5f 67 2e 69 73 49 6e 69 74 20 29 3b 0a che_g.isInit );.
f3050 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 assert( pCache
f3060 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
f3070 20 70 67 6e 6f 3e 30 20 29 3b 0a 20 20 65 78 70 pgno>0 );. exp
f3080 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 ensive_assert( p
f3090 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 3d 3d Cache->nPinned==
f30a0 70 63 61 63 68 65 50 69 6e 6e 65 64 43 6f 75 6e pcachePinnedCoun
f30b0 74 28 70 43 61 63 68 65 29 20 29 3b 0a 0a 20 20 t(pCache) );..
f30c0 70 63 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 pcacheEnterMutex
f30d0 28 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 ();.. /* Search
f30e0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
f30f0 66 6f 72 20 74 68 65 20 72 65 71 75 65 73 74 65 for the requeste
f3100 64 20 70 61 67 65 2e 20 45 78 69 74 20 65 61 72 d page. Exit ear
f3110 6c 79 20 69 66 20 69 74 20 69 73 20 66 6f 75 6e ly if it is foun
f3120 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 d. */. if( pCac
f3130 68 65 2d 3e 61 70 48 61 73 68 20 29 7b 0a 20 20 he->apHash ){.
f3140 20 20 75 33 32 20 68 20 3d 20 70 67 6e 6f 20 25 u32 h = pgno %
f3150 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a pCache->nHash;.
f3160 20 20 20 20 66 6f 72 28 70 50 61 67 65 3d 70 43 for(pPage=pC
f3170 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b ache->apHash[h];
f3180 20 70 50 61 67 65 3b 20 70 50 61 67 65 3d 70 50 pPage; pPage=pP
f3190 61 67 65 2d 3e 70 4e 65 78 74 48 61 73 68 29 7b age->pNextHash){
f31a0 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 . if( pPage
f31b0 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 7b 0a ->pgno==pgno ){.
f31c0 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 if( pPag
f31d0 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 e->nRef==0 ){.
f31e0 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 if( 0==(
f31f0 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 pPage->flags&PGH
f3200 44 52 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 20 DR_DIRTY) ){.
f3210 20 20 20 20 20 20 20 20 20 70 63 61 63 68 65 52 pcacheR
f3220 65 6d 6f 76 65 46 72 6f 6d 4c 72 75 4c 69 73 74 emoveFromLruList
f3230 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
f3240 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 69 pCache->nPi
f3250 6e 6e 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nned++;.
f3260 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 43 }. pC
f3270 61 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 ache->nRef++;.
f3280 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
f3290 70 50 61 67 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 pPage->nRef++;.
f32a0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
f32b0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
f32c0 0a 20 20 69 66 28 20 21 70 50 61 67 65 20 26 26 . if( !pPage &&
f32d0 20 63 72 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 createFlag ){.
f32e0 20 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e if( pCache->n
f32f0 48 61 73 68 3c 3d 70 43 61 63 68 65 2d 3e 6e 50 Hash<=pCache->nP
f3300 61 67 65 20 29 7b 0a 20 20 20 20 20 20 72 63 20 age ){. rc
f3310 3d 20 70 63 61 63 68 65 52 65 73 69 7a 65 48 61 = pcacheResizeHa
f3320 73 68 28 70 43 61 63 68 65 2c 20 70 43 61 63 68 sh(pCache, pCach
f3330 65 2d 3e 6e 48 61 73 68 3c 32 35 36 20 3f 20 32 e->nHash<256 ? 2
f3340 35 36 20 3a 20 70 43 61 63 68 65 2d 3e 6e 48 61 56 : pCache->nHa
f3350 73 68 2a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 sh*2);. }.
f3360 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
f3370 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
f3380 20 70 63 61 63 68 65 52 65 63 79 63 6c 65 4f 72 pcacheRecycleOr
f3390 41 6c 6c 6f 63 28 70 43 61 63 68 65 2c 20 26 70 Alloc(pCache, &p
f33a0 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Page);. }.
f33b0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
f33c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 OK ){. pPag
f33d0 65 2d 3e 70 50 61 67 65 72 20 3d 20 30 3b 0a 20 e->pPager = 0;.
f33e0 20 20 20 20 20 70 50 61 67 65 2d 3e 66 6c 61 67 pPage->flag
f33f0 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 s = 0;. pPa
f3400 67 65 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a ge->pDirty = 0;.
f3410 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e pPage->pgn
f3420 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 o = pgno;.
f3430 70 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 pPage->pCache =
f3440 70 43 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50 pCache;. pP
f3450 61 67 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 age->nRef = 1;.
f3460 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 pCache->nRe
f3470 66 2b 2b 3b 0a 20 20 20 20 20 20 70 43 61 63 68 f++;. pCach
f3480 65 2d 3e 6e 50 69 6e 6e 65 64 2b 2b 3b 0a 20 20 e->nPinned++;.
f3490 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c pcacheAddToL
f34a0 69 73 74 28 26 70 43 61 63 68 65 2d 3e 70 43 6c ist(&pCache->pCl
f34b0 65 61 6e 2c 20 70 50 61 67 65 29 3b 0a 20 20 20 ean, pPage);.
f34c0 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 48 61 pcacheAddToHa
f34d0 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d sh(pPage);. }
f34e0 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 45 78 . }.. pcacheEx
f34f0 69 74 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2a 70 itMutex();.. *p
f3500 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 pPage = pPage;.
f3510 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 expensive_asser
f3520 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e 6e t( pCache->nPinn
f3530 65 64 3d 3d 70 63 61 63 68 65 50 69 6e 6e 65 64 ed==pcachePinned
f3540 43 6f 75 6e 74 28 70 43 61 63 68 65 29 20 29 3b Count(pCache) );
f3550 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
f3560 20 7c 7c 20 21 63 72 65 61 74 65 46 6c 61 67 20 || !createFlag
f3570 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b || rc!=SQLITE_OK
f3580 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
f3590 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 72 65 66 65 .}../*.** Derefe
f35a0 72 65 6e 63 65 20 61 20 70 61 67 65 2e 20 20 57 rence a page. W
f35b0 68 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 hen the referenc
f35c0 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 e count reaches
f35d0 7a 65 72 6f 2c 0a 2a 2a 20 6d 6f 76 65 20 74 68 zero,.** move th
f35e0 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 4c 52 e page to the LR
f35f0 55 20 6c 69 73 74 20 69 66 20 69 74 20 69 73 20 U list if it is
f3600 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 clean..*/.SQLITE
f3610 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
f3620 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 lite3PcacheRelea
f3630 73 65 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 se(PgHdr *p){.
f3640 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
f3650 30 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 0 );. p->nRef--
f3660 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d ;. if( p->nRef=
f3670 3d 30 20 29 7b 0a 20 20 20 20 50 43 61 63 68 65 =0 ){. PCache
f3680 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 *pCache = p->pC
f3690 61 63 68 65 3b 0a 20 20 20 20 70 43 61 63 68 65 ache;. pCache
f36a0 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 ->nRef--;. if
f36b0 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 ( (p->flags&PGHD
f36c0 52 5f 44 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 R_DIRTY)==0 ){.
f36d0 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 69 pCache->nPi
f36e0 6e 6e 65 64 2d 2d 3b 0a 20 20 20 20 20 20 70 63 nned--;. pc
f36f0 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29 acheEnterMutex()
f3700 3b 0a 20 20 20 20 20 20 69 66 28 20 70 63 61 63 ;. if( pcac
f3710 68 65 5f 67 2e 6e 43 75 72 72 65 6e 74 50 61 67 he_g.nCurrentPag
f3720 65 3e 70 63 61 63 68 65 5f 67 2e 6e 4d 61 78 50 e>pcache_g.nMaxP
f3730 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 age ){. p
f3740 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 4c cacheRemoveFromL
f3750 69 73 74 28 26 70 43 61 63 68 65 2d 3e 70 43 6c ist(&pCache->pCl
f3760 65 61 6e 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 ean, p);.
f3770 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f pcacheRemoveFro
f3780 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20 mHash(p);.
f3790 20 20 70 63 61 63 68 65 50 61 67 65 46 72 65 65 pcachePageFree
f37a0 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 (p);. }else
f37b0 7b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 {. pcache
f37c0 41 64 64 54 6f 4c 72 75 4c 69 73 74 28 70 29 3b AddToLruList(p);
f37d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
f37e0 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 cacheExitMutex()
f37f0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
f3800 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 /* Move the p
f3810 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 age to the head
f3820 6f 66 20 74 68 65 20 63 61 63 68 65 73 20 64 69 of the caches di
f3830 72 74 79 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 rty list. */.
f3840 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 pcacheRemoveF
f3850 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 68 65 2d romList(&pCache-
f3860 3e 70 44 69 72 74 79 2c 20 70 29 3b 0a 20 20 20 >pDirty, p);.
f3870 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c 69 pcacheAddToLi
f3880 73 74 28 26 70 43 61 63 68 65 2d 3e 70 44 69 72 st(&pCache->pDir
f3890 74 79 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 ty, p);. }.
f38a0 7d 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 }.}..SQLITE_PRIV
f38b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
f38c0 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20 PcacheRef(PgHdr
f38d0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d *p){. assert(p-
f38e0 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e >nRef>0);. p->n
f38f0 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Ref++;.}../*.**
f3900 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d Drop a page from
f3910 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72 the cache. Ther
f3920 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c e must be exactl
f3930 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 y one reference
f3940 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 to the.** page.
f3950 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 This function de
f3960 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72 letes that refer
f3970 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69 ence, so after i
f3980 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a t returns the.**
f3990 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f page pointed to
f39a0 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64 by p is invalid
f39b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
f39c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
f39d0 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 PcacheDrop(PgHdr
f39e0 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a *p){. PCache *
f39f0 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 pCache;. assert
f3a00 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a ( p->nRef==1 );.
f3a10 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 2d assert( 0==(p-
f3a20 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
f3a30 54 59 29 20 29 3b 0a 20 20 70 43 61 63 68 65 20 TY) );. pCache
f3a40 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 = p->pCache;. p
f3a50 43 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 Cache->nRef--;.
f3a60 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 pCache->nPinned
f3a70 2d 2d 3b 0a 20 20 70 63 61 63 68 65 45 6e 74 65 --;. pcacheEnte
f3a80 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 rMutex();. pcac
f3a90 68 65 52 65 6d 6f 76 65 46 72 6f 6d 4c 69 73 74 heRemoveFromList
f3aa0 28 26 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 6e (&pCache->pClean
f3ab0 2c 20 70 29 3b 0a 20 20 70 63 61 63 68 65 52 65 , p);. pcacheRe
f3ac0 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b moveFromHash(p);
f3ad0 0a 20 20 70 63 61 63 68 65 50 61 67 65 46 72 65 . pcachePageFre
f3ae0 65 28 70 29 3b 0a 20 20 70 63 61 63 68 65 45 78 e(p);. pcacheEx
f3af0 69 74 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a itMutex();.}../*
f3b00 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 .** Make sure th
f3b10 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 e page is marked
f3b20 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 69 as dirty. If i
f3b30 74 20 69 73 6e 27 74 20 64 69 72 74 79 20 61 6c t isn't dirty al
f3b40 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20 69 ready,.** make i
f3b50 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f t so..*/.SQLITE_
f3b60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
f3b70 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 ite3PcacheMakeDi
f3b80 72 74 79 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 rty(PgHdr *p){.
f3b90 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b PCache *pCache;
f3ba0 0a 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e . p->flags &= ~
f3bb0 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 PGHDR_DONT_WRITE
f3bc0 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 ;. if( p->flags
f3bd0 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 20 29 & PGHDR_DIRTY )
f3be0 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 return;. asser
f3bf0 74 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 50 t( (p->flags & P
f3c00 47 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 29 GHDR_DIRTY)==0 )
f3c10 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
f3c20 52 65 66 3e 30 20 29 3b 0a 20 20 70 43 61 63 68 Ref>0 );. pCach
f3c30 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 e = p->pCache;.
f3c40 20 70 63 61 63 68 65 45 6e 74 65 72 4d 75 74 65 pcacheEnterMute
f3c50 78 28 29 3b 0a 20 20 70 63 61 63 68 65 52 65 6d x();. pcacheRem
f3c60 6f 76 65 46 72 6f 6d 4c 69 73 74 28 26 70 43 61 oveFromList(&pCa
f3c70 63 68 65 2d 3e 70 43 6c 65 61 6e 2c 20 70 29 3b che->pClean, p);
f3c80 0a 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c 69 . pcacheAddToLi
f3c90 73 74 28 26 70 43 61 63 68 65 2d 3e 70 44 69 72 st(&pCache->pDir
f3ca0 74 79 2c 20 70 29 3b 0a 20 20 70 63 61 63 68 65 ty, p);. pcache
f3cb0 45 78 69 74 4d 75 74 65 78 28 29 3b 0a 20 20 70 ExitMutex();. p
f3cc0 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
f3cd0 5f 44 49 52 54 59 3b 0a 7d 0a 0a 73 74 61 74 69 _DIRTY;.}..stati
f3ce0 63 20 76 6f 69 64 20 70 63 61 63 68 65 4d 61 6b c void pcacheMak
f3cf0 65 43 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 29 eClean(PgHdr *p)
f3d00 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 {. PCache *pCac
f3d10 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a he = p->pCache;.
f3d20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61 assert( p->fla
f3d30 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 gs & PGHDR_DIRTY
f3d40 20 29 3b 0a 20 20 70 63 61 63 68 65 52 65 6d 6f );. pcacheRemo
f3d50 76 65 46 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 veFromList(&pCac
f3d60 68 65 2d 3e 70 44 69 72 74 79 2c 20 70 29 3b 0a he->pDirty, p);.
f3d70 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c 69 73 pcacheAddToLis
f3d80 74 28 26 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 t(&pCache->pClea
f3d90 6e 2c 20 70 29 3b 0a 20 20 70 2d 3e 66 6c 61 67 n, p);. p->flag
f3da0 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 49 52 54 s &= ~PGHDR_DIRT
f3db0 59 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 Y;. if( p->nRef
f3dc0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63 68 ==0 ){. pcach
f3dd0 65 41 64 64 54 6f 4c 72 75 4c 69 73 74 28 70 29 eAddToLruList(p)
f3de0 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 ;. pCache->nP
f3df0 69 6e 6e 65 64 2d 2d 3b 0a 20 20 7d 0a 20 20 65 inned--;. }. e
f3e00 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 xpensive_assert(
f3e10 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 pCache->nPinned
f3e20 3d 3d 70 63 61 63 68 65 50 69 6e 6e 65 64 43 6f ==pcachePinnedCo
f3e30 75 6e 74 28 70 43 61 63 68 65 29 20 29 3b 0a 7d unt(pCache) );.}
f3e40 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
f3e50 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 e the page is ma
f3e60 72 6b 65 64 20 61 73 20 63 6c 65 61 6e 2e 20 20 rked as clean.
f3e70 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c 65 61 If it isn't clea
f3e80 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 n already,.** ma
f3e90 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c ke it so..*/.SQL
f3ea0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
f3eb0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 sqlite3PcacheMa
f3ec0 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 keClean(PgHdr *p
f3ed0 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 ){. if( (p->fla
f3ee0 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 gs & PGHDR_DIRTY
f3ef0 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 45 ) ){. pcacheE
f3f00 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
f3f10 20 70 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e pcacheMakeClean
f3f20 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 45 (p);. pcacheE
f3f30 78 69 74 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a xitMutex();. }.
f3f40 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 }../*.** Make ev
f3f50 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
f3f60 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a cache clean..*/.
f3f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
f3f80 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
f3f90 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 eCleanAll(PCache
f3fa0 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
f3fb0 64 72 20 2a 70 3b 0a 20 20 70 63 61 63 68 65 45 dr *p;. pcacheE
f3fc0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 77 nterMutex();. w
f3fd0 68 69 6c 65 28 20 28 70 20 3d 20 70 43 61 63 68 hile( (p = pCach
f3fe0 65 2d 3e 70 44 69 72 74 79 29 21 3d 30 20 29 7b e->pDirty)!=0 ){
f3ff0 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 . pcacheRemov
f4000 65 46 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 68 eFromList(&pCach
f4010 65 2d 3e 70 44 69 72 74 79 2c 20 70 29 3b 0a 20 e->pDirty, p);.
f4020 20 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e p->flags &= ~
f4030 50 47 48 44 52 5f 44 49 52 54 59 3b 0a 20 20 20 PGHDR_DIRTY;.
f4040 20 70 63 61 63 68 65 41 64 64 54 6f 4c 69 73 74 pcacheAddToList
f4050 28 26 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 6e (&pCache->pClean
f4060 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d , p);. if( p-
f4070 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 >nRef==0 ){.
f4080 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c 72 75 pcacheAddToLru
f4090 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70 List(p);. p
f40a0 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 2d 2d Cache->nPinned--
f40b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
f40c0 6c 69 74 65 33 50 63 61 63 68 65 41 73 73 65 72 lite3PcacheAsser
f40d0 74 46 6c 61 67 73 28 70 43 61 63 68 65 2c 20 30 tFlags(pCache, 0
f40e0 2c 20 50 47 48 44 52 5f 44 49 52 54 59 29 3b 0a , PGHDR_DIRTY);.
f40f0 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
f4100 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e rt( pCache->nPin
f4110 6e 65 64 3d 3d 70 63 61 63 68 65 50 69 6e 6e 65 ned==pcachePinne
f4120 64 43 6f 75 6e 74 28 70 43 61 63 68 65 29 20 29 dCount(pCache) )
f4130 3b 0a 20 20 70 63 61 63 68 65 45 78 69 74 4d 75 ;. pcacheExitMu
f4140 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tex();.}../*.**
f4150 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 Change the page
f4160 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 number of page p
f4170 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 49 66 20 to newPgno. If
f4180 6e 65 77 50 67 6e 6f 20 69 73 20 30 2c 20 74 68 newPgno is 0, th
f4190 65 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6f en the.** page o
f41a0 62 6a 65 63 74 20 69 73 20 61 64 64 65 64 20 74 bject is added t
f41b0 6f 20 74 68 65 20 63 6c 65 61 6e 2d 6c 69 73 74 o the clean-list
f41c0 20 61 6e 64 20 74 68 65 20 50 47 48 44 52 5f 52 and the PGHDR_R
f41d0 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 0a 2a EUSE_UNLIKELY .*
f41e0 2a 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 53 * flag set..*/.S
f41f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
f4200 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
f4210 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 50 Move(PgHdr *p, P
f4220 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 gno newPgno){.
f4230 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
f4240 30 20 29 3b 0a 20 20 70 63 61 63 68 65 45 6e 74 0 );. pcacheEnt
f4250 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 erMutex();. pca
f4260 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 cheRemoveFromHas
f4270 68 28 70 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 h(p);. p->pgno
f4280 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 = newPgno;. if(
f4290 20 6e 65 77 50 67 6e 6f 3d 3d 30 20 29 7b 0a 20 newPgno==0 ){.
f42a0 20 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 if( (p->flags
f42b0 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 & PGHDR_DIRTY)
f42c0 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 4d ){. pcacheM
f42d0 61 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 20 akeClean(p);.
f42e0 20 7d 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 }. p->flags
f42f0 3d 20 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e = PGHDR_REUSE_UN
f4300 4c 49 4b 45 4c 59 3b 0a 20 20 7d 0a 20 20 70 63 LIKELY;. }. pc
f4310 61 63 68 65 41 64 64 54 6f 48 61 73 68 28 70 29 acheAddToHash(p)
f4320 3b 0a 20 20 70 63 61 63 68 65 45 78 69 74 4d 75 ;. pcacheExitMu
f4330 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tex();.}../*.**
f4340 52 65 6d 6f 76 65 20 61 6c 6c 20 63 6f 6e 74 65 Remove all conte
f4350 6e 74 20 66 72 6f 6d 20 61 20 70 61 67 65 20 63 nt from a page c
f4360 61 63 68 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ache.*/.static v
f4370 6f 69 64 20 70 63 61 63 68 65 43 6c 65 61 72 28 oid pcacheClear(
f4380 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
f4390 0a 20 20 50 67 48 64 72 20 2a 70 2c 20 2a 70 4e . PgHdr *p, *pN
f43a0 65 78 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ext;. assert( s
f43b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
f43c0 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 74 65 78 d(pcache_g.mutex
f43d0 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 ) );. for(p=pCa
f43e0 63 68 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20 che->pClean; p;
f43f0 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e p=pNext){. pN
f4400 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a ext = p->pNext;.
f4410 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 pcacheRemove
f4420 46 72 6f 6d 4c 72 75 4c 69 73 74 28 70 29 3b 0a FromLruList(p);.
f4430 20 20 20 20 70 63 61 63 68 65 50 61 67 65 46 72 pcachePageFr
f4440 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 ee(p);. }. for
f4450 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 (p=pCache->pDirt
f4460 79 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a y; p; p=pNext){.
f4470 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 pNext = p->p
f4480 4e 65 78 74 3b 0a 20 20 20 20 70 63 61 63 68 65 Next;. pcache
f4490 50 61 67 65 46 72 65 65 28 70 29 3b 0a 20 20 7d PageFree(p);. }
f44a0 0a 20 20 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 . pCache->pClea
f44b0 6e 20 3d 20 30 3b 0a 20 20 70 43 61 63 68 65 2d n = 0;. pCache-
f44c0 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 70 >pDirty = 0;. p
f44d0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 Cache->pDirtyTai
f44e0 6c 20 3d 20 30 3b 0a 20 20 70 43 61 63 68 65 2d l = 0;. pCache-
f44f0 3e 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 70 43 >nPage = 0;. pC
f4500 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 20 3d 20 ache->nPinned =
f4510 30 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43 61 63 0;. memset(pCac
f4520 68 65 2d 3e 61 70 48 61 73 68 2c 20 30 2c 20 70 he->apHash, 0, p
f4530 43 61 63 68 65 2d 3e 6e 48 61 73 68 2a 73 69 7a Cache->nHash*siz
f4540 65 6f 66 28 70 43 61 63 68 65 2d 3e 61 70 48 61 eof(pCache->apHa
f4550 73 68 5b 30 5d 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a sh[0]));.}.../*.
f4560 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 61 ** Drop every ca
f4570 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 20 che entry whose
f4580 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 page number is g
f4590 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 6e reater than "pgn
f45a0 6f 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 o"..*/.SQLITE_PR
f45b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
f45c0 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 e3PcacheTruncate
f45d0 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c (PCache *pCache,
f45e0 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 Pgno pgno){. P
f45f0 67 48 64 72 20 2a 70 2c 20 2a 70 4e 65 78 74 3b gHdr *p, *pNext;
f4600 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 . PgHdr *pDirty
f4610 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 = pCache->pDirt
f4620 79 3b 0a 20 20 70 63 61 63 68 65 45 6e 74 65 72 y;. pcacheEnter
f4630 4d 75 74 65 78 28 29 3b 0a 20 20 66 6f 72 28 70 Mutex();. for(p
f4640 3d 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 6e 3b =pCache->pClean;
f4650 20 70 7c 7c 70 44 69 72 74 79 3b 20 70 3d 70 4e p||pDirty; p=pN
f4660 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 21 70 ext){. if( !p
f4670 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 44 ){. p = pD
f4680 69 72 74 79 3b 0a 20 20 20 20 20 20 70 44 69 72 irty;. pDir
f4690 74 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 ty = 0;. }.
f46a0 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 pNext = p->pNe
f46b0 78 74 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 xt;. if( p->p
f46c0 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 gno>pgno ){.
f46d0 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 if( p->nRef==0
f46e0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63 61 63 ){. pcac
f46f0 68 65 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 heRemoveFromHash
f4700 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 (p);. if(
f4710 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f p->flags&PGHDR_
f4720 44 49 52 54 59 20 29 7b 0a 20 20 20 20 20 20 20 DIRTY ){.
f4730 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 pcacheRemoveF
f4740 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 68 65 2d romList(&pCache-
f4750 3e 70 44 69 72 74 79 2c 20 70 29 3b 0a 20 20 20 >pDirty, p);.
f4760 20 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e pCache->n
f4770 50 69 6e 6e 65 64 2d 2d 3b 0a 20 20 20 20 20 20 Pinned--;.
f4780 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
f4790 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 pcacheRemoveF
f47a0 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 68 65 2d romList(&pCache-
f47b0 3e 70 43 6c 65 61 6e 2c 20 70 29 3b 0a 20 20 20 >pClean, p);.
f47c0 20 20 20 20 20 20 20 70 63 61 63 68 65 52 65 6d pcacheRem
f47d0 6f 76 65 46 72 6f 6d 4c 72 75 4c 69 73 74 28 70 oveFromLruList(p
f47e0 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
f47f0 20 20 20 20 20 70 63 61 63 68 65 50 61 67 65 46 pcachePageF
f4800 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 ree(p);. }e
f4810 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
f4820 49 66 20 74 68 65 72 65 20 61 72 65 20 72 65 66 If there are ref
f4830 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 erences to the p
f4840 61 67 65 2c 20 69 74 20 63 61 6e 6e 6f 74 20 62 age, it cannot b
f4850 65 20 66 72 65 65 64 2e 20 49 6e 20 74 68 69 73 e freed. In this
f4860 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 . ** case
f4870 2c 20 7a 65 72 6f 20 74 68 65 20 70 61 67 65 20 , zero the page
f4880 63 6f 6e 74 65 6e 74 20 69 6e 73 74 65 61 64 2e content instead.
f4890 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
f48a0 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 70 44 memset(p->pD
f48b0 61 74 61 2c 20 30 2c 20 70 43 61 63 68 65 2d 3e ata, 0, pCache->
f48c0 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d szPage);. }
f48d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 63 61 . }. }. pca
f48e0 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 3b 0a cheExitMutex();.
f48f0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 }../*.** If ther
f4900 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 e are currently
f4910 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 65 more than pcache
f4920 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20 .nMaxPage pages
f4930 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a allocated, try.*
f4940 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67 * to recycle pag
f4950 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 es to reduce the
f4960 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65 number allocate
f4970 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 78 d to pcache.nMax
f4980 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Page..*/.static
f4990 76 6f 69 64 20 70 63 61 63 68 65 45 6e 66 6f 72 void pcacheEnfor
f49a0 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 29 7b ceMaxPage(void){
f49b0 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 61 . PgHdr *p;. a
f49c0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
f49d0 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 utex_held(pcache
f49e0 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 77 _g.mutex) );. w
f49f0 68 69 6c 65 28 20 70 63 61 63 68 65 5f 67 2e 6e hile( pcache_g.n
f4a00 43 75 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63 CurrentPage>pcac
f4a10 68 65 5f 67 2e 6e 4d 61 78 50 61 67 65 20 26 26 he_g.nMaxPage &&
f4a20 20 28 70 20 3d 20 70 63 61 63 68 65 52 65 63 79 (p = pcacheRecy
f4a30 63 6c 65 50 61 67 65 28 29 29 20 29 7b 0a 20 20 clePage()) ){.
f4a40 20 20 70 63 61 63 68 65 50 61 67 65 46 72 65 65 pcachePageFree
f4a50 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
f4a60 2a 20 43 6c 6f 73 65 20 61 20 63 61 63 68 65 2e * Close a cache.
f4a70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
f4a80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
f4a90 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 cacheClose(PCach
f4aa0 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 70 63 e *pCache){. pc
f4ab0 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29 acheEnterMutex()
f4ac0 3b 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c ;.. /* Free all
f4ad0 20 74 68 65 20 70 61 67 65 73 20 75 73 65 64 20 the pages used
f4ae0 62 79 20 74 68 69 73 20 70 61 67 65 72 20 61 6e by this pager an
f4af0 64 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72 d remove them fr
f4b00 6f 6d 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e om the LRU list.
f4b10 20 2a 2f 0a 20 20 70 63 61 63 68 65 43 6c 65 61 */. pcacheClea
f4b20 72 28 70 43 61 63 68 65 29 3b 0a 20 20 69 66 28 r(pCache);. if(
f4b30 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 pCache->bPurgea
f4b40 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 63 68 ble ){. pcach
f4b50 65 5f 67 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20 e_g.nMaxPage -=
f4b60 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 pCache->nMax;.
f4b70 20 20 70 63 61 63 68 65 5f 67 2e 6e 4d 69 6e 50 pcache_g.nMinP
f4b80 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e age -= pCache->n
f4b90 4d 69 6e 3b 0a 20 20 20 20 70 63 61 63 68 65 45 Min;. pcacheE
f4ba0 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b nforceMaxPage();
f4bb0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 . }. sqlite3_f
f4bc0 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70 48 61 ree(pCache->apHa
f4bd0 73 68 29 3b 0a 20 20 70 63 61 63 68 65 45 78 69 sh);. pcacheExi
f4be0 74 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 0a 23 69 tMutex();.}...#i
f4bf0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 fndef NDEBUG./*
f4c00 0a 2a 2a 20 41 73 73 65 72 74 20 66 6c 61 67 73 .** Assert flags
f4c10 20 73 65 74 74 69 6e 67 73 20 6f 6e 20 61 6c 6c settings on all
f4c20 20 70 61 67 65 73 2e 20 20 44 65 62 75 67 67 69 pages. Debuggi
f4c30 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 ng only..*/.SQLI
f4c40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
f4c50 73 71 6c 69 74 65 33 50 63 61 63 68 65 41 73 73 sqlite3PcacheAss
f4c60 65 72 74 46 6c 61 67 73 28 50 43 61 63 68 65 20 ertFlags(PCache
f4c70 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 74 72 75 *pCache, int tru
f4c80 65 4d 61 73 6b 2c 20 69 6e 74 20 66 61 6c 73 65 eMask, int false
f4c90 4d 61 73 6b 29 7b 0a 20 20 50 67 48 64 72 20 2a Mask){. PgHdr *
f4ca0 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 p;. for(p=pCach
f4cb0 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d e->pDirty; p; p=
f4cc0 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 p->pNext){. a
f4cd0 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 ssert( (p->flags
f4ce0 26 74 72 75 65 4d 61 73 6b 29 3d 3d 74 72 75 65 &trueMask)==true
f4cf0 4d 61 73 6b 20 29 3b 0a 20 20 20 20 61 73 73 65 Mask );. asse
f4d00 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 66 61 rt( (p->flags&fa
f4d10 6c 73 65 4d 61 73 6b 29 3d 3d 30 20 29 3b 0a 20 lseMask)==0 );.
f4d20 20 7d 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 }. for(p=pCach
f4d30 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20 70 3d e->pClean; p; p=
f4d40 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 p->pNext){. a
f4d50 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 ssert( (p->flags
f4d60 26 74 72 75 65 4d 61 73 6b 29 3d 3d 74 72 75 65 &trueMask)==true
f4d70 4d 61 73 6b 20 29 3b 0a 20 20 20 20 61 73 73 65 Mask );. asse
f4d80 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 66 61 rt( (p->flags&fa
f4d90 6c 73 65 4d 61 73 6b 29 3d 3d 30 20 29 3b 0a 20 lseMask)==0 );.
f4da0 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 }.}.#endif../*
f4db0 0a 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 .** Discard the
f4dc0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
f4dd0 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cache..*/.SQLITE
f4de0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
f4df0 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 ite3PcacheClear(
f4e00 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
f4e10 0a 20 20 61 73 73 65 72 74 28 70 43 61 63 68 65 . assert(pCache
f4e20 2d 3e 6e 52 65 66 3d 3d 30 29 3b 0a 20 20 70 63 ->nRef==0);. pc
f4e30 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29 acheEnterMutex()
f4e40 3b 0a 20 20 70 63 61 63 68 65 43 6c 65 61 72 28 ;. pcacheClear(
f4e50 70 43 61 63 68 65 29 3b 0a 20 20 70 63 61 63 68 pCache);. pcach
f4e60 65 45 78 69 74 4d 75 74 65 78 28 29 3b 0a 20 20 eExitMutex();.
f4e70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f4e80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 ;.}../*.** Merge
f4e90 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 two lists of pa
f4ea0 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 ges connected by
f4eb0 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 pDirty and in p
f4ec0 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f gno order..** Do
f4ed0 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 not both fixing
f4ee0 20 74 68 65 20 70 50 72 65 76 44 69 72 74 79 20 the pPrevDirty
f4ef0 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 pointers..*/.sta
f4f00 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 tic PgHdr *pcach
f4f10 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 eMergeDirtyList(
f4f20 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 PgHdr *pA, PgHdr
f4f30 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 *pB){. PgHdr r
f4f40 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 esult, *pTail;.
f4f50 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 pTail = &result
f4f60 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 ;. while( pA &&
f4f70 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 pB ){. if( p
f4f80 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f A->pgno<pB->pgno
f4f90 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d ){. pTail-
f4fa0 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 >pDirty = pA;.
f4fb0 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a pTail = pA;.
f4fc0 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 pA = pA->p
f4fd0 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 Dirty;. }else
f4fe0 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 {. pTail->p
f4ff0 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 Dirty = pB;.
f5000 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 pTail = pB;.
f5010 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 pB = pB->pDi
f5020 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 rty;. }. }.
f5030 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 if( pA ){. p
f5040 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 Tail->pDirty = p
f5050 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 A;. }else if( p
f5060 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e B ){. pTail->
f5070 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d pDirty = pB;. }
f5080 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d else{. pTail-
f5090 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d >pDirty = 0;. }
f50a0 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 . return result
f50b0 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a .pDirty;.}../*.*
f50c0 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 * Sort the list
f50d0 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 of pages in acce
f50e0 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 nding order by p
f50f0 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a gno. Pages are.
f5100 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 ** connected by
f5110 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e pDirty pointers.
f5120 20 20 54 68 65 20 70 50 72 65 76 44 69 72 74 79 The pPrevDirty
f5130 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a pointers are.**
f5140 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 corrupted by th
f5150 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 65 66 is sort..*/.#def
f5160 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 ine N_SORT_BUCKE
f5170 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65 66 69 T_ALLOC 25.#defi
f5180 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 ne N_SORT_BUCKET
f5190 20 20 20 20 20 20 20 32 35 0a 23 69 66 64 65 66 25.#ifdef
f51a0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 SQLITE_TEST. i
f51b0 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 nt sqlite3_pager
f51c0 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 20 3d _n_sort_bucket =
f51d0 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e 5f 53 0;. #undef N_S
f51e0 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 23 64 65 ORT_BUCKET. #de
f51f0 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b fine N_SORT_BUCK
f5200 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 74 65 33 ET \. (sqlite3
f5210 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 _pager_n_sort_bu
f5220 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70 61 67 cket?sqlite3_pag
f5230 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 er_n_sort_bucket
f5240 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41 :N_SORT_BUCKET_A
f5250 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73 74 61 LLOC).#endif.sta
f5260 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 tic PgHdr *pcach
f5270 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 50 eSortDirtyList(P
f5280 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50 67 gHdr *pIn){. Pg
f5290 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55 Hdr *a[N_SORT_BU
f52a0 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a 70 3b CKET_ALLOC], *p;
f52b0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 . int i;. mems
f52c0 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 et(a, 0, sizeof(
f52d0 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 a));. while( pI
f52e0 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e n ){. p = pIn
f52f0 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 ;. pIn = p->p
f5300 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 Dirty;. p->pD
f5310 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f irty = 0;. fo
f5320 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52 54 5f r(i=0; i<N_SORT_
f5330 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b 29 7b 0a BUCKET-1; i++){.
f5340 20 20 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d if( a[i]==
f5350 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69 0 ){. a[i
f5360 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 ] = p;. b
f5370 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 reak;. }els
f5380 65 7b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 e{. p = p
f5390 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c cacheMergeDirtyL
f53a0 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 ist(a[i], p);.
f53b0 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a a[i] = 0;.
f53c0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
f53d0 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52 54 5f if( i==N_SORT_
f53e0 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20 20 20 BUCKET-1 ){.
f53f0 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a 20 54 /* Coverage: T
f5400 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 72 o get here, ther
f5410 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28 e need to be 2^(
f5420 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 0a N_SORT_BUCKET) .
f5430 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 ** element
f5440 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 6c s in the input l
f5450 69 73 74 2e 20 54 68 69 73 20 69 73 20 70 6f 73 ist. This is pos
f5460 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70 72 61 sible, but impra
f5470 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20 2a 2a ctical.. **
f5480 20 54 65 73 74 69 6e 67 20 74 68 69 73 20 6c 69 Testing this li
f5490 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e 74 20 ne is the point
f54a0 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 of global variab
f54b0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 le. ** sqli
f54c0 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 te3_pager_n_sort
f54d0 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20 20 2a _bucket.. *
f54e0 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 /. a[i] = p
f54f0 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c cacheMergeDirtyL
f5500 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 ist(a[i], p);.
f5510 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b }. }. p = a[
f5520 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 0];. for(i=1; i
f5530 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 <N_SORT_BUCKET;
f5540 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63 i++){. p = pc
f5550 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 acheMergeDirtyLi
f5560 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d st(p, a[i]);. }
f5570 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
f5580 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c /*.** Return a l
f5590 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 ist of all dirty
f55a0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 pages in the ca
f55b0 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 che, sorted by p
f55c0 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 age number..*/.S
f55d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 QLITE_PRIVATE Pg
f55e0 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 Hdr *sqlite3Pcac
f55f0 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 heDirtyList(PCac
f5600 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 he *pCache){. P
f5610 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 gHdr *p;. for(p
f5620 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b =pCache->pDirty;
f5630 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
f5640 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 3d . p->pDirty =
f5650 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 p->pNext;. }.
f5660 20 72 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f return pcacheSo
f5670 72 74 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 rtDirtyList(pCac
f5680 68 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a he->pDirty);.}..
f5690 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 /* .** Return th
f56a0 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
f56b0 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 f outstanding pa
f56c0 67 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a ge references..*
f56d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
f56e0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
f56f0 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 68 heRefCount(PCach
f5700 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 e *pCache){. re
f5710 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 65 turn pCache->nRe
f5720 66 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 f;.}..SQLITE_PRI
f5730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
f5740 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 PcachePageRefcou
f5750 6e 74 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 nt(PgHdr *p){.
f5760 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a return p->nRef;.
f5770 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e }../* .** Return
f5780 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
f5790 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
f57a0 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 e cache..*/.SQLI
f57b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
f57c0 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
f57d0 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 count(PCache *pC
f57e0 61 63 68 65 29 7b 0a 20 20 61 73 73 65 72 74 28 ache){. assert(
f57f0 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3e 3d pCache->nPage>=
f5800 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 0 );. return pC
f5810 61 63 68 65 2d 3e 6e 50 61 67 65 3b 0a 7d 0a 0a ache->nPage;.}..
f5820 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 #ifdef SQLITE_CH
f5830 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 ECK_PAGES./*.**
f5840 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
f5850 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 67 used by the pag
f5860 65 72 2e 63 20 6d 6f 64 75 6c 65 20 74 6f 20 69 er.c module to i
f5870 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 terate through a
f5880 6c 6c 20 0a 2a 2a 20 70 61 67 65 73 20 69 6e 20 ll .** pages in
f5890 74 68 65 20 63 61 63 68 65 2e 20 41 74 20 70 72 the cache. At pr
f58a0 65 73 65 6e 74 2c 20 74 68 69 73 20 69 73 20 6f esent, this is o
f58b0 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 20 nly required if
f58c0 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 48 the.** SQLITE_CH
f58d0 45 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 ECK_PAGES macro
f58e0 28 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 (used for debugg
f58f0 69 6e 67 29 20 69 73 20 73 70 65 63 69 66 69 65 ing) is specifie
f5900 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
f5910 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
f5920 33 50 63 61 63 68 65 49 74 65 72 61 74 65 28 50 3PcacheIterate(P
f5930 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 Cache *pCache, v
f5940 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 oid (*xIter)(PgH
f5950 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 dr *)){. PgHdr
f5960 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 *p;. for(p=pCac
f5970 68 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20 70 he->pClean; p; p
f5980 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
f5990 78 49 74 65 72 28 70 29 3b 0a 20 20 7d 0a 20 20 xIter(p);. }.
f59a0 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 for(p=pCache->pD
f59b0 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e irty; p; p=p->pN
f59c0 65 78 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28 ext){. xIter(
f59d0 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 p);. }.}.#endif
f59e0 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 66 6c 61 ../* .** Set fla
f59f0 67 73 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 20 gs on all pages
f5a00 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 in the page cach
f5a10 65 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e .*/.SQLITE_PRI
f5a20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
f5a30 33 50 63 61 63 68 65 43 6c 65 61 72 46 6c 61 67 3PcacheClearFlag
f5a40 73 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 s(PCache *pCache
f5a50 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 50 , int mask){. P
f5a60 67 48 64 72 20 2a 70 3b 0a 0a 20 20 2f 2a 20 4f gHdr *p;.. /* O
f5a70 62 74 61 69 6e 20 74 68 65 20 67 6c 6f 62 61 6c btain the global
f5a80 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 6d 6f mutex before mo
f5a90 64 69 66 79 69 6e 67 20 61 6e 79 20 50 67 48 64 difying any PgHd
f5aa0 72 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c 65 r.flags variable
f5ab0 73 20 0a 20 20 2a 2a 20 6f 72 20 74 72 61 76 65 s . ** or trave
f5ac0 72 73 69 6e 67 20 74 68 65 20 4c 52 55 20 6c 69 rsing the LRU li
f5ad0 73 74 2e 0a 20 20 2a 2f 20 0a 20 20 70 63 61 63 st.. */ . pcac
f5ae0 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a heEnterMutex();.
f5af0 0a 20 20 6d 61 73 6b 20 3d 20 7e 6d 61 73 6b 3b . mask = ~mask;
f5b00 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d . for(p=pCache-
f5b10 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d >pDirty; p; p=p-
f5b20 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e >pNext){. p->
f5b30 66 6c 61 67 73 20 26 3d 20 6d 61 73 6b 3b 0a 20 flags &= mask;.
f5b40 20 7d 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 }. for(p=pCach
f5b50 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20 70 3d e->pClean; p; p=
f5b60 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 p->pNext){. p
f5b70 2d 3e 66 6c 61 67 73 20 26 3d 20 6d 61 73 6b 3b ->flags &= mask;
f5b80 0a 20 20 7d 0a 0a 20 20 69 66 28 20 30 3d 3d 28 . }.. if( 0==(
f5b90 6d 61 73 6b 26 50 47 48 44 52 5f 4e 45 45 44 5f mask&PGHDR_NEED_
f5ba0 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 43 61 SYNC) ){. pCa
f5bb0 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 che->pSynced = p
f5bc0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 Cache->pDirtyTai
f5bd0 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 l;. assert( !
f5be0 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 pCache->pSynced
f5bf0 7c 7c 20 28 70 43 61 63 68 65 2d 3e 70 53 79 6e || (pCache->pSyn
f5c00 63 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 ced->flags&PGHDR
f5c10 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d 30 20 29 _NEED_SYNC)==0 )
f5c20 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 45 ;. }.. pcacheE
f5c30 78 69 74 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f xitMutex();.}../
f5c40 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67 *.** Set the sug
f5c50 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a gested cache-siz
f5c60 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 e value..*/.SQLI
f5c70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
f5c80 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 qlite3PcacheGetC
f5c90 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 achesize(PCache
f5ca0 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 75 *pCache){. retu
f5cb0 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b rn pCache->nMax;
f5cc0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
f5cd0 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 e suggested cach
f5ce0 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f e-size value..*/
f5cf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
f5d00 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
f5d10 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 heSetCachesize(P
f5d20 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 69 Cache *pCache, i
f5d30 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66 nt mxPage){. if
f5d40 28 20 6d 78 50 61 67 65 3c 31 30 20 29 7b 0a 20 ( mxPage<10 ){.
f5d50 20 20 20 6d 78 50 61 67 65 20 3d 20 31 30 3b 0a mxPage = 10;.
f5d60 20 20 7d 0a 20 20 69 66 28 20 70 43 61 63 68 65 }. if( pCache
f5d70 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a ->bPurgeable ){.
f5d80 20 20 20 20 70 63 61 63 68 65 45 6e 74 65 72 4d pcacheEnterM
f5d90 75 74 65 78 28 29 3b 0a 20 20 20 20 70 63 61 63 utex();. pcac
f5da0 68 65 5f 67 2e 6e 4d 61 78 50 61 67 65 20 2d 3d he_g.nMaxPage -=
f5db0 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 pCache->nMax;.
f5dc0 20 20 20 70 63 61 63 68 65 5f 67 2e 6e 4d 61 78 pcache_g.nMax
f5dd0 50 61 67 65 20 2b 3d 20 6d 78 50 61 67 65 3b 0a Page += mxPage;.
f5de0 20 20 20 20 70 63 61 63 68 65 45 6e 66 6f 72 63 pcacheEnforc
f5df0 65 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 20 20 eMaxPage();.
f5e00 70 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 pcacheExitMutex(
f5e10 29 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 2d );. }. pCache-
f5e20 3e 6e 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a >nMax = mxPage;.
f5e30 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
f5e40 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
f5e50 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 ANAGEMENT./*.**
f5e60 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
f5e70 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 called to free
f5e80 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 superfluous dyna
f5e90 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
f5ea0 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 d memory.** held
f5eb0 20 62 79 20 74 68 65 20 70 61 67 65 72 20 73 79 by the pager sy
f5ec0 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 stem. Memory in
f5ed0 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74 use by any SQLit
f5ee0 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 e pager allocate
f5ef0 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72 d.** by the curr
f5f00 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62 ent thread may b
f5f10 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 e sqlite3_free()
f5f20 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 ed..**.** nReq i
f5f30 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
f5f40 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
f5f50 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20 74 required. Once t
f5f60 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20 his much has.**
f5f70 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c 20 74 been released, t
f5f80 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
f5f90 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 6e 20 rns. The return
f5fa0 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74 value is the tot
f5fb0 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 al number .** of
f5fc0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
f5fd0 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 released..*/.SQ
f5fe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
f5ff0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
f6000 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 leaseMemory(int
f6010 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72 nReq){. int nFr
f6020 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 63 ee = 0;. if( pc
f6030 61 63 68 65 5f 67 2e 70 53 74 61 72 74 3d 3d 30 ache_g.pStart==0
f6040 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 ){. PgHdr *p
f6050 3b 0a 20 20 20 20 70 63 61 63 68 65 45 6e 74 65 ;. pcacheEnte
f6060 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 77 68 rMutex();. wh
f6070 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c 20 ile( (nReq<0 ||
f6080 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20 28 nFree<nReq) && (
f6090 70 3d 70 63 61 63 68 65 52 65 63 79 63 6c 65 50 p=pcacheRecycleP
f60a0 61 67 65 28 29 29 20 29 7b 0a 20 20 20 20 20 20 age()) ){.
f60b0 6e 46 72 65 65 20 2b 3d 20 70 63 61 63 68 65 50 nFree += pcacheP
f60c0 61 67 65 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 ageSize(p);.
f60d0 20 20 70 63 61 63 68 65 50 61 67 65 46 72 65 65 pcachePageFree
f60e0 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 (p);. }. p
f60f0 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 cacheExitMutex()
f6100 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e ;. }. return n
f6110 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f Free;.}.#endif /
f6120 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
f6130 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e MEMORY_MANAGEMEN
f6140 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c T */..#ifdef SQL
f6150 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
f6160 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
f6170 69 74 65 33 50 63 61 63 68 65 53 74 61 74 73 28 ite3PcacheStats(
f6180 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72 65 6e . int *pnCurren
f6190 74 2c 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c t,. int *pnMax,
f61a0 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c 0a 20 . int *pnMin,.
f61b0 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 int *pnRecyclab
f61c0 6c 65 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 le.){. PgHdr *p
f61d0 3b 0a 20 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 ;. int nRecycla
f61e0 62 6c 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 ble = 0;. for(p
f61f0 3d 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 48 65 =pcache_g.pLruHe
f6200 61 64 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 ad; p; p=p->pNex
f6210 74 4c 72 75 29 7b 0a 20 20 20 20 6e 52 65 63 79 tLru){. nRecy
f6220 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 clable++;. }..
f6230 20 2a 70 6e 43 75 72 72 65 6e 74 20 3d 20 70 63 *pnCurrent = pc
f6240 61 63 68 65 5f 67 2e 6e 43 75 72 72 65 6e 74 50 ache_g.nCurrentP
f6250 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 78 20 3d 20 age;. *pnMax =
f6260 70 63 61 63 68 65 5f 67 2e 6e 4d 61 78 50 61 67 pcache_g.nMaxPag
f6270 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d 20 70 63 e;. *pnMin = pc
f6280 61 63 68 65 5f 67 2e 6e 4d 69 6e 50 61 67 65 3b ache_g.nMinPage;
f6290 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 . *pnRecyclable
f62a0 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a = nRecyclable;.
f62b0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a }.#endif../*****
f62c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
f62d0 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a pcache.c ******
f62e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f62f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6300 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
f6310 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
f6320 66 69 6c 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a file pager.c ***
f6330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6350 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
f6360 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
f6370 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
f6380 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
f6390 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
f63a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
f63b0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
f63c0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
f63d0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
f63e0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
f63f0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
f6400 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
f6410 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
f6420 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
f6430 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
f6440 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
f6450 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
f6460 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
f6470 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
f6480 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
f6490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f64a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f64b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f64c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f64d0 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
f64e0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
f64f0 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 f the page cache
f6500 20 73 75 62 73 79 73 74 65 6d 20 6f 72 20 22 70 subsystem or "p
f6510 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 ager"..** .** Th
f6520 65 20 70 61 67 65 72 20 69 73 20 75 73 65 64 20 e pager is used
f6530 74 6f 20 61 63 63 65 73 73 20 61 20 64 61 74 61 to access a data
f6540 62 61 73 65 20 64 69 73 6b 20 66 69 6c 65 2e 20 base disk file.
f6550 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a It implements.*
f6560 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 * atomic commit
f6570 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72 and rollback thr
f6580 6f 75 67 68 20 74 68 65 20 75 73 65 20 6f 66 20 ough the use of
f6590 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 a journal file t
f65a0 68 61 74 0a 2a 2a 20 69 73 20 73 65 70 61 72 61 hat.** is separa
f65b0 74 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 te from the data
f65c0 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 base file. The
f65d0 70 61 67 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65 pager also imple
f65e0 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a 20 6c 6f ments file.** lo
f65f0 63 6b 69 6e 67 20 74 6f 20 70 72 65 76 65 6e 74 cking to prevent
f6600 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 66 two processes f
f6610 72 6f 6d 20 77 72 69 74 69 6e 67 20 74 68 65 20 rom writing the
f6620 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a same database.**
f6630 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e 65 6f file simultaneo
f6640 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 70 72 6f usly, or one pro
f6650 63 65 73 73 20 66 72 6f 6d 20 72 65 61 64 69 6e cess from readin
f6660 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 g the database w
f6670 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 hile.** another
f6680 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a is writing..**.*
f6690 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 * @(#) $Id: page
f66a0 72 2e 63 2c 76 20 31 2e 35 30 32 20 32 30 30 38 r.c,v 1.502 2008
f66b0 2f 31 31 2f 30 37 20 30 30 3a 32 34 3a 35 34 20 /11/07 00:24:54
f66c0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 drh Exp $.*/.#if
f66d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
f66e0 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d _DISKIO../*.** M
f66f0 61 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c acros for troubl
f6700 65 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d eshooting. Norm
f6710 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a ally turned off.
f6720 2a 2f 0a 23 69 66 20 30 0a 23 64 65 66 69 6e 65 */.#if 0.#define
f6730 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
f6740 6e 74 66 20 70 72 69 6e 74 66 0a 23 64 65 66 69 ntf printf.#defi
f6750 6e 65 20 50 41 47 45 52 54 52 41 43 45 31 28 58 ne PAGERTRACE1(X
f6760 29 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 ) sqlite3D
f6770 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 ebugPrintf(X).#d
f6780 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 efine PAGERTRACE
f6790 32 28 58 2c 59 29 20 20 20 20 20 73 71 6c 69 74 2(X,Y) sqlit
f67a0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
f67b0 59 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 Y).#define PAGER
f67c0 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 TRACE3(X,Y,Z)
f67d0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
f67e0 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e tf(X,Y,Z).#defin
f67f0 65 20 50 41 47 45 52 54 52 41 43 45 34 28 58 2c e PAGERTRACE4(X,
f6800 59 2c 5a 2c 57 29 20 73 71 6c 69 74 65 33 44 65 Y,Z,W) sqlite3De
f6810 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
f6820 57 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 W).#define PAGER
f6830 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 57 2c 56 TRACE5(X,Y,Z,W,V
f6840 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
f6850 69 6e 74 66 28 58 2c 59 2c 5a 2c 57 2c 56 29 0a intf(X,Y,Z,W,V).
f6860 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50 41 #else.#define PA
f6870 47 45 52 54 52 41 43 45 31 28 58 29 0a 23 64 65 GERTRACE1(X).#de
f6880 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 32 fine PAGERTRACE2
f6890 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 50 41 (X,Y).#define PA
f68a0 47 45 52 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 GERTRACE3(X,Y,Z)
f68b0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 .#define PAGERTR
f68c0 41 43 45 34 28 58 2c 59 2c 5a 2c 57 29 0a 23 64 ACE4(X,Y,Z,W).#d
f68d0 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 efine PAGERTRACE
f68e0 35 28 58 2c 59 2c 5a 2c 57 2c 56 29 0a 23 65 6e 5(X,Y,Z,W,V).#en
f68f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
f6900 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 ollowing two mac
f6910 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 74 ros are used wit
f6920 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 41 hin the PAGERTRA
f6930 43 45 58 28 29 20 6d 61 63 72 6f 73 20 61 62 6f CEX() macros abo
f6940 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f ve.** to print o
f6950 75 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 ut file-descript
f6960 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 ors. .**.** PAGE
f6970 52 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f RID() takes a po
f6980 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 inter to a Pager
f6990 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 struct as its a
f69a0 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 rgument. The.**
f69b0 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d associated file-
f69c0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 descriptor is re
f69d0 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 turned. FILEHAND
f69e0 4c 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 LEID() takes an
f69f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 sqlite3_file.**
f6a00 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 struct as its ar
f6a10 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 gument..*/.#defi
f6a20 6e 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28 ne PAGERID(p) ((
f6a30 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 int)(p->fd)).#de
f6a40 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 fine FILEHANDLEI
f6a50 44 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a D(fd) ((int)fd).
f6a60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 ./*.** The page
f6a70 63 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 cache as a whole
f6a80 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e is always in on
f6a90 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
f6aa0 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a ng.** states:.**
f6ab0 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f .** PAGER_UNLO
f6ac0 43 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61 CK The pa
f6ad0 67 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 ge cache is not
f6ae0 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e currently readin
f6af0 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 g or .**
f6b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 w
f6b10 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 riting the datab
f6b20 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 ase file. There
f6b30 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 is no.**
f6b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6b50 64 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d data held in mem
f6b60 6f 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 ory. This is th
f6b70 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 e initial.**
f6b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6b90 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 state..**.**
f6ba0 20 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 PAGER_SHARED
f6bb0 20 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 The page c
f6bc0 61 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 ache is reading
f6bd0 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a the database..**
f6be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6bf0 20 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 Writing i
f6c00 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e s not permitted.
f6c10 20 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a There can be.*
f6c20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
f6c30 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 multiple
f6c40 20 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69 readers accessi
f6c50 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 ng the same data
f6c60 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 base.**
f6c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 fi
f6c80 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 le at the same t
f6c90 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 ime..**.** PAG
f6ca0 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 ER_RESERVED
f6cb0 20 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 This process ha
f6cc0 73 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64 s reserved the d
f6cd0 61 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 atabase for writ
f6ce0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
f6cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74 but
f6d00 20 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 has not yet mad
f6d10 65 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 e any changes.
f6d20 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 Only one process
f6d30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
f6d40 20 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74 at a t
f6d50 69 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 ime can reserve
f6d60 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
f6d70 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 he original.**
f6d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6d90 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 database fi
f6da0 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 le has not been
f6db0 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 modified so othe
f6dc0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 r.**
f6dd0 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 proce
f6de0 73 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 sses may still b
f6df0 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e e reading the on
f6e00 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 -disk.**
f6e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
f6e20 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
f6e30 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c .** PAGER_EXCL
f6e40 55 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61 USIVE The pa
f6e50 67 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74 ge cache is writ
f6e60 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
f6e70 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 ..**
f6e80 20 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73 Acces
f6e90 73 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 s is exclusive.
f6ea0 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 No other proces
f6eb0 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 ses or.**
f6ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6ed0 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 threads can be r
f6ee0 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e eading or writin
f6ef0 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 g while one.**
f6f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6f10 20 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 process is
f6f20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 writing..**.**
f6f30 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 PAGER_SYNCED
f6f40 20 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d The pager m
f6f50 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 oves to this sta
f6f60 74 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 te from PAGER_EX
f6f70 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 CLUSIVE.**
f6f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6f90 20 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 after all dirty
f6fa0 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e pages have been
f6fb0 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a written to the.
f6fc0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
f6fd0 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 databas
f6fe0 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 e file and the f
f6ff0 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e ile has been syn
f7000 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 ced to.**
f7010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7020 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 disk. All that r
f7030 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 emains to do is
f7040 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 to remove or.**
f7050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7060 20 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 truncate t
f7070 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
f7080 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 and the transact
f7090 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ion .**
f70a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 wi
f70b0 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e ll be committed.
f70c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 .**.** The page
f70d0 63 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 cache comes up i
f70e0 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 n PAGER_UNLOCK.
f70f0 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 The first time
f7100 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 a.** sqlite3Page
f7110 72 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 rGet() occurs, t
f7120 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 he state transit
f7130 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 ions to PAGER_SH
f7140 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 ARED..** After a
f7150 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65 ll pages have be
f7160 65 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e en released usin
f7170 67 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e g sqlite_page_un
f7180 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 ref(),.** the st
f7190 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 ate transitions
f71a0 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e back to PAGER_UN
f71b0 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 LOCK. The first
f71c0 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 time.** that sq
f71d0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
f71e0 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 ) is called, the
f71f0 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f state transitio
f7200 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 ns to.** PAGER_R
f7210 45 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 ESERVED. (Note
f7220 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 that sqlite3Page
f7230 72 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c rWrite() can onl
f7240 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f y be.** called o
f7250 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 n an outstanding
f7260 20 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e page which mean
f7270 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 s that the pager
f7280 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 must.** be in P
f7290 41 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f AGER_SHARED befo
f72a0 72 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e re it transition
f72b0 73 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 s to PAGER_RESER
f72c0 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 VED.).** PAGER_R
f72d0 45 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 ESERVED means th
f72e0 61 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f at there is an o
f72f0 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 pen rollback jou
f7300 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 rnal..** The tra
f7310 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 nsition to PAGER
f7320 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 _EXCLUSIVE occur
f7330 73 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 s before any cha
f7340 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 nges.** are made
f7350 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
f7360 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 file, though wr
f7370 69 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c ites to the roll
f7380 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 back.** journal
f7390 6f 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 occurs with just
f73a0 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e PAGER_RESERVED.
f73b0 20 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 After an sqlit
f73c0 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
f73d0 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 ).** or sqlite3P
f73e0 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 agerCommitPhaseT
f73f0 77 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 wo(), the state
f7400 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 can go back to P
f7410 41 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 AGER_SHARED,.**
f7420 6f 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 or it can stay a
f7430 74 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 t PAGER_EXCLUSIV
f7440 45 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 E if we are in e
f7450 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 xclusive access
f7460 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 mode..*/.#define
f7470 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 PAGER_UNLOCK
f7480 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 0.#define PAG
f7490 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 ER_SHARED 1
f74a0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 /* same as SH
f74b0 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 ARED_LOCK */.#de
f74c0 66 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 fine PAGER_RESER
f74d0 56 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 VED 2 /* sa
f74e0 6d 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c me as RESERVED_L
f74f0 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 OCK */.#define P
f7500 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 AGER_EXCLUSIVE
f7510 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 4 /* same as
f7520 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a EXCLUSIVE_LOCK *
f7530 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
f7540 53 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f SYNCED 5../
f7550 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 *.** If the SQLI
f7560 54 45 5f 42 55 53 59 5f 52 45 53 45 52 56 45 44 TE_BUSY_RESERVED
f7570 5f 4c 4f 43 4b 20 6d 61 63 72 6f 20 69 73 20 73 _LOCK macro is s
f7580 65 74 20 74 6f 20 74 72 75 65 20 61 74 20 63 6f et to true at co
f7590 6d 70 69 6c 65 2d 74 69 6d 65 2c 0a 2a 2a 20 74 mpile-time,.** t
f75a0 68 65 6e 20 66 61 69 6c 65 64 20 61 74 74 65 6d hen failed attem
f75b0 70 74 73 20 74 6f 20 67 65 74 20 61 20 72 65 73 pts to get a res
f75c0 65 72 76 65 64 20 6c 6f 63 6b 20 77 69 6c 6c 20 erved lock will
f75d0 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 invoke the busy
f75e0 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 callback..** Thi
f75f0 73 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 s is off by defa
f7600 75 6c 74 2e 20 20 54 6f 20 73 65 65 20 77 68 79 ult. To see why
f7610 2c 20 63 6f 6e 73 69 64 65 72 20 74 68 65 20 66 , consider the f
f7620 6f 6c 6c 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 ollowing scenari
f7630 6f 3a 0a 2a 2a 20 0a 2a 2a 20 53 75 70 70 6f 73 o:.** .** Suppos
f7640 65 20 74 68 72 65 61 64 20 41 20 61 6c 72 65 61 e thread A alrea
f7650 64 79 20 68 61 73 20 61 20 73 68 61 72 65 64 20 dy has a shared
f7660 6c 6f 63 6b 20 61 6e 64 20 77 61 6e 74 73 20 61 lock and wants a
f7670 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 0a reserved lock..
f7680 2a 2a 20 54 68 72 65 61 64 20 42 20 61 6c 72 65 ** Thread B alre
f7690 61 64 79 20 68 61 73 20 61 20 72 65 73 65 72 76 ady has a reserv
f76a0 65 64 20 6c 6f 63 6b 20 61 6e 64 20 77 61 6e 74 ed lock and want
f76b0 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c s an exclusive l
f76c0 6f 63 6b 2e 20 20 49 66 0a 2a 2a 20 62 6f 74 68 ock. If.** both
f76d0 20 74 68 72 65 61 64 73 20 61 72 65 20 75 73 69 threads are usi
f76e0 6e 67 20 74 68 65 69 72 20 62 75 73 79 20 63 61 ng their busy ca
f76f0 6c 6c 62 61 63 6b 73 2c 20 69 74 20 6d 69 67 68 llbacks, it migh
f7700 74 20 62 65 20 61 20 6c 6f 6e 67 20 74 69 6d 65 t be a long time
f7710 0a 2a 2a 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f .** be for one o
f7720 66 20 74 68 65 20 74 68 72 65 61 64 73 20 67 69 f the threads gi
f7730 76 65 20 75 70 20 61 6e 64 20 61 6c 6c 6f 77 73 ve up and allows
f7740 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 70 72 the other to pr
f7750 6f 63 65 65 64 2e 0a 2a 2a 20 42 75 74 20 69 66 oceed..** But if
f7760 20 74 68 65 20 74 68 72 65 61 64 20 74 72 79 69 the thread tryi
f7770 6e 67 20 74 6f 20 67 65 74 20 74 68 65 20 72 65 ng to get the re
f7780 73 65 72 76 65 64 20 6c 6f 63 6b 20 67 69 76 65 served lock give
f7790 73 20 75 70 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 s up quickly.**
f77a0 28 69 66 20 69 74 20 6e 65 76 65 72 20 69 6e 76 (if it never inv
f77b0 6f 6b 65 73 20 69 74 73 20 62 75 73 79 20 63 61 okes its busy ca
f77c0 6c 6c 62 61 63 6b 29 20 74 68 65 6e 20 74 68 65 llback) then the
f77d0 20 63 6f 6e 74 65 6e 74 69 6f 6e 20 77 69 6c 6c contention will
f77e0 20 62 65 0a 2a 2a 20 72 65 73 6f 6c 76 65 64 20 be.** resolved
f77f0 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 23 69 66 6e quickly..*/.#ifn
f7800 64 65 66 20 53 51 4c 49 54 45 5f 42 55 53 59 5f def SQLITE_BUSY_
f7810 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 23 20 RESERVED_LOCK.#
f7820 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 define SQLITE_BU
f7830 53 59 5f 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b SY_RESERVED_LOCK
f7840 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
f7850 20 54 68 69 73 20 6d 61 63 72 6f 20 72 6f 75 6e This macro roun
f7860 64 73 20 76 61 6c 75 65 73 20 75 70 20 73 6f 20 ds values up so
f7870 74 68 61 74 20 69 66 20 74 68 65 20 76 61 6c 75 that if the valu
f7880 65 20 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 e is an address
f7890 69 74 0a 2a 2a 20 69 73 20 67 75 61 72 61 6e 74 it.** is guarant
f78a0 65 65 64 20 74 6f 20 62 65 20 61 6e 20 61 64 64 eed to be an add
f78b0 72 65 73 73 20 74 68 61 74 20 69 73 20 61 6c 69 ress that is ali
f78c0 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 gned to an 8-byt
f78d0 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 23 e boundary..*/.#
f78e0 64 65 66 69 6e 65 20 46 4f 52 43 45 5f 41 4c 49 define FORCE_ALI
f78f0 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 28 58 GNMENT(X) (((X
f7900 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 )+7)&~7)../*.**
f7910 41 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 A macro used for
f7920 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f invoking the co
f7930 64 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20 dec if there is
f7940 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 one.*/.#ifdef SQ
f7950 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 LITE_HAS_CODEC.#
f7960 20 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 define CODEC1(P
f7970 2c 44 2c 4e 2c 58 29 20 69 66 28 20 50 2d 3e 78 ,D,N,X) if( P->x
f7980 43 6f 64 65 63 21 3d 30 20 29 7b 20 50 2d 3e 78 Codec!=0 ){ P->x
f7990 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 Codec(P->pCodecA
f79a0 72 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23 20 64 rg,D,N,X); }.# d
f79b0 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 efine CODEC2(P,D
f79c0 2c 4e 2c 58 29 20 28 28 63 68 61 72 2a 29 28 50 ,N,X) ((char*)(P
f79d0 2d 3e 78 43 6f 64 65 63 21 3d 30 3f 50 2d 3e 78 ->xCodec!=0?P->x
f79e0 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 Codec(P->pCodecA
f79f0 72 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a 23 65 rg,D,N,X):D)).#e
f7a00 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 lse.# define COD
f7a10 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20 2f 2a 20 EC1(P,D,N,X) /*
f7a20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e NO-OP */.# defin
f7a30 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 e CODEC2(P,D,N,X
f7a40 29 20 28 28 63 68 61 72 2a 29 44 29 0a 23 65 6e ) ((char*)D).#en
f7a50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 dif../*.** A ope
f7a60 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 n page cache is
f7a70 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
f7a80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
f7a90 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 ucture..**.** Pa
f7aa0 67 65 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 ger.errCode may
f7ab0 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 be set to SQLITE
f7ac0 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 _IOERR, SQLITE_C
f7ad0 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 6f 72 ORRUPT, or.** or
f7ae0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e SQLITE_FULL. On
f7af0 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 ce one of the fi
f7b00 72 73 74 20 74 68 72 65 65 20 65 72 72 6f 72 73 rst three errors
f7b10 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 72 73 occurs, it pers
f7b20 69 73 74 73 0a 2a 2a 20 61 6e 64 20 69 73 20 72 ists.** and is r
f7b30 65 74 75 72 6e 65 64 20 61 73 20 74 68 65 20 72 eturned as the r
f7b40 65 73 75 6c 74 20 6f 66 20 65 76 65 72 79 20 6d esult of every m
f7b50 61 6a 6f 72 20 70 61 67 65 72 20 41 50 49 20 63 ajor pager API c
f7b60 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c all. The.** SQL
f7b70 49 54 45 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20 ITE_FULL return
f7b80 63 6f 64 65 20 69 73 20 73 6c 69 67 68 74 6c 79 code is slightly
f7b90 20 64 69 66 66 65 72 65 6e 74 2e 20 49 74 20 70 different. It p
f7ba0 65 72 73 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74 ersists only unt
f7bb0 69 6c 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 73 il the.** next s
f7bc0 75 63 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 uccessful rollba
f7bd0 63 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 ck is performed
f7be0 6f 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 on the pager cac
f7bf0 68 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 53 51 4c he. Also,.** SQL
f7c00 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f ITE_FULL does no
f7c10 74 20 61 66 66 65 63 74 20 74 68 65 20 73 71 6c t affect the sql
f7c20 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 61 ite3PagerGet() a
f7c30 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c nd sqlite3PagerL
f7c40 6f 6f 6b 75 70 28 29 0a 2a 2a 20 41 50 49 73 2c ookup().** APIs,
f7c50 20 74 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20 they may still
f7c60 62 65 20 75 73 65 64 20 73 75 63 63 65 73 73 66 be used successf
f7c70 75 6c 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ully..*/.struct
f7c80 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 Pager {. sqlite
f7c90 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 3_vfs *pVfs;
f7ca0 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63 /* OS func
f7cb0 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72 tions to use for
f7cc0 20 49 4f 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 IO */. u8 jour
f7cd0 6e 61 6c 4f 70 65 6e 3b 20 20 20 20 20 20 20 20 nalOpen;
f7ce0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
f7cf0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 journal file des
f7d00 63 72 69 70 74 6f 72 73 20 69 73 20 76 61 6c 69 criptors is vali
f7d10 64 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 d */. u8 journa
f7d20 6c 53 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 lStarted;
f7d30 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 /* True if he
f7d40 61 64 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 ader of journal
f7d50 69 73 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 is synced */. u
f7d60 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 8 useJournal;
f7d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
f7d80 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 e a rollback jou
f7d90 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c rnal on this fil
f7da0 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64 e */. u8 noRead
f7db0 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 lock;
f7dc0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 /* Do not bot
f7dd0 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65 her to obtain re
f7de0 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20 adlocks */. u8
f7df0 73 74 6d 74 4f 70 65 6e 3b 20 20 20 20 20 20 20 stmtOpen;
f7e00 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
f7e10 20 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e if the statemen
f7e20 74 20 73 75 62 6a 6f 75 72 6e 61 6c 20 69 73 20 t subjournal is
f7e30 6f 70 65 6e 20 2a 2f 0a 20 20 75 38 20 73 74 6d open */. u8 stm
f7e40 74 49 6e 55 73 65 3b 20 20 20 20 20 20 20 20 20 tInUse;
f7e50 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 65 /* True we
f7e60 20 61 72 65 20 69 6e 20 61 20 73 74 61 74 65 6d are in a statem
f7e70 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 ent subtransacti
f7e80 6f 6e 20 2a 2f 0a 20 20 75 38 20 73 74 6d 74 41 on */. u8 stmtA
f7e90 75 74 6f 6f 70 65 6e 3b 20 20 20 20 20 20 20 20 utoopen;
f7ea0 20 20 20 20 2f 2a 20 4f 70 65 6e 20 73 74 6d 74 /* Open stmt
f7eb0 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 20 6d 61 journal when ma
f7ec0 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 in journal is op
f7ed0 65 6e 65 64 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 ened*/. u8 noSy
f7ee0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nc;
f7ef0 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 /* Do not s
f7f00 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ync the journal
f7f10 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 if true */. u8
f7f20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 fullSync;
f7f30 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 /* Do e
f7f40 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68 xtra syncs of th
f7f50 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f e journal for ro
f7f60 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 bustness */. u8
f7f70 20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20 sync_flags;
f7f80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
f7f90 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 of SYNC_NORMAL
f7fa0 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a or SYNC_FULL */.
f7fb0 20 20 75 38 20 73 74 61 74 65 3b 20 20 20 20 20 u8 state;
f7fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f7fd0 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f PAGER_UNLOCK, _
f7fe0 53 48 41 52 45 44 2c 20 5f 52 45 53 45 52 56 45 SHARED, _RESERVE
f7ff0 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 D, etc. */. u8
f8000 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 20 tempFile;
f8010 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c /* zFil
f8020 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70 6f ename is a tempo
f8030 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75 rary file */. u
f8040 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 8 readOnly;
f8050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
f8060 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f 6e ue for a read-on
f8070 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 ly database */.
f8080 20 75 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20 u8 needSync;
f8090 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f80a0 54 72 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63 True if an fsync
f80b0 28 29 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20 () is needed on
f80c0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 the journal */.
f80d0 20 75 38 20 64 69 72 74 79 43 61 63 68 65 3b 20 u8 dirtyCache;
f80e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f80f0 54 72 75 65 20 69 66 20 63 61 63 68 65 64 20 70 True if cached p
f8100 61 67 65 73 20 68 61 76 65 20 63 68 61 6e 67 65 ages have change
f8110 64 20 2a 2f 0a 20 20 75 38 20 61 6c 77 61 79 73 d */. u8 always
f8120 52 6f 6c 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 Rollback;
f8130 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 44 6f /* Disable Do
f8140 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 66 6f 72 ntRollback() for
f8150 20 61 6c 6c 20 70 61 67 65 73 20 2a 2f 0a 20 20 all pages */.
f8160 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 20 20 u8 memDb;
f8170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
f8180 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74 20 61 rue to inhibit a
f8190 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 ll file I/O */.
f81a0 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b 20 20 u8 setMaster;
f81b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f81c0 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e 61 True if a m-j na
f81d0 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 me has been writ
f81e0 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 ten to jrnl */.
f81f0 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20 20 u8 doNotSync;
f8200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f8210 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20 74 Boolean. While t
f8220 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69 6c rue, do not spil
f8230 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 l the cache */.
f8240 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64 u8 exclusiveMod
f8250 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
f8260 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 Boolean. True if
f8270 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 locking_mode==E
f8280 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38 XCLUSIVE */. u8
f8290 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 journalMode;
f82a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 20 /* On
f82b0 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55 of the PAGER_JOU
f82c0 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 65 RNALMODE_* value
f82d0 73 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f 64 69 s */. u8 dbModi
f82e0 66 69 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 fied;
f82f0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
f8300 65 72 65 20 61 72 65 20 61 6e 79 20 63 68 61 6e ere are any chan
f8310 67 65 73 20 74 6f 20 74 68 65 20 44 62 20 2a 2f ges to the Db */
f8320 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f 75 6e . u8 changeCoun
f8330 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 2f tDone; /
f8340 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e 63 72 * Set after incr
f8350 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 68 61 ementing the cha
f8360 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 nge-counter */.
f8370 20 75 33 32 20 76 66 73 46 6c 61 67 73 3b 20 20 u32 vfsFlags;
f8380 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f8390 46 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74 65 Flags for sqlite
f83a0 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
f83b0 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 . int errCode;
f83c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f83d0 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c * One of several
f83e0 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 kinds of errors
f83f0 20 2a 2f 0a 20 20 69 6e 74 20 64 62 53 69 7a 65 */. int dbSize
f8400 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
f8410 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
f8420 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 ages in the file
f8430 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69 67 44 62 */. int origDb
f8440 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
f8450 20 20 2f 2a 20 64 62 53 69 7a 65 20 62 65 66 6f /* dbSize befo
f8460 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 re the current c
f8470 68 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 hange */. int s
f8480 74 6d 74 53 69 7a 65 3b 20 20 20 20 20 20 20 20 tmtSize;
f8490 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
f84a0 66 20 64 61 74 61 62 61 73 65 20 28 69 6e 20 70 f database (in p
f84b0 61 67 65 73 29 20 61 74 20 73 74 6d 74 5f 62 65 ages) at stmt_be
f84c0 67 69 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e gin() */. int n
f84d0 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
f84e0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
f84f0 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 65 of pages writte
f8500 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
f8510 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 */. u32 cksumI
f8520 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 nit;
f8530 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f /* Quasi-rando
f8540 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f m value added to
f8550 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20 every checksum
f8560 2a 2f 0a 20 20 69 6e 74 20 73 74 6d 74 4e 52 65 */. int stmtNRe
f8570 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
f8580 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
f8590 63 6f 72 64 73 20 69 6e 20 73 74 6d 74 20 73 75 cords in stmt su
f85a0 62 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e bjournal */. in
f85b0 74 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 t nExtra;
f85c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
f85d0 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 this many bytes
f85e0 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f to each in-memo
f85f0 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ry page */. int
f8600 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 pageSize;
f8610 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
f8620 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 er of bytes in a
f8630 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e page */. int n
f8640 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
f8650 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
f8660 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d number of in-mem
f8670 6f 72 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 ory pages */. i
f8680 6e 74 20 6d 78 50 61 67 65 3b 20 20 20 20 20 20 nt mxPage;
f8690 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
f86a0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
f86b0 70 61 67 65 73 20 74 6f 20 68 6f 6c 64 20 69 6e pages to hold in
f86c0 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f cache */. Pgno
f86d0 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 mxPgno;
f86e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
f86f0 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 um allowed size
f8700 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
f8710 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e */. Bitvec *pIn
f8720 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 Journal;
f8730 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 /* One bit for
f8740 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 each page in the
f8750 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
f8760 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53 /. Bitvec *pInS
f8770 74 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 tmt;
f8780 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 /* One bit for e
f8790 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 ach page in the
f87a0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 69 database */. Bi
f87b0 74 76 65 63 20 2a 70 41 6c 77 61 79 73 52 6f 6c tvec *pAlwaysRol
f87c0 6c 62 61 63 6b 3b 20 20 20 20 2f 2a 20 4f 6e 65 lback; /* One
f87d0 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 70 61 bit for each pa
f87e0 67 65 20 6d 61 72 6b 65 64 20 61 6c 77 61 79 73 ge marked always
f87f0 2d 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 -rollback */. c
f8800 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 20 har *zFilename;
f8810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
f8820 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
f8830 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 se file */. cha
f8840 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 r *zJournal;
f8850 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
f8860 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
f8870 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a file */. char *
f8880 7a 44 69 72 65 63 74 6f 72 79 3b 20 20 20 20 20 zDirectory;
f8890 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f /* Directo
f88a0 72 79 20 68 6f 6c 64 20 64 61 74 61 62 61 73 65 ry hold database
f88b0 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c and journal fil
f88c0 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f es */. sqlite3_
f88d0 66 69 6c 65 20 2a 66 64 2c 20 2a 6a 66 64 3b 20 file *fd, *jfd;
f88e0 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 /* File desc
f88f0 72 69 70 74 6f 72 73 20 66 6f 72 20 64 61 74 61 riptors for data
f8900 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e 61 6c base and journal
f8910 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
f8920 6c 65 20 2a 73 74 66 64 3b 20 20 20 20 20 20 20 le *stfd;
f8930 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 /* File descri
f8940 70 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74 61 ptor for the sta
f8950 74 65 6d 65 6e 74 20 73 75 62 6a 6f 75 72 6e 61 tement subjourna
f8960 6c 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c 65 l*/. BusyHandle
f8970 72 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 3b r *pBusyHandler;
f8980 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
f8990 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c sqlite.busyHandl
f89a0 65 72 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 er */. i64 jour
f89b0 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20 nalOff;
f89c0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 62 /* Current b
f89d0 79 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74 68 yte offset in th
f89e0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a e journal file *
f89f0 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 /. i64 journalH
f8a00 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dr;
f8a10 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 74 /* Byte offset t
f8a20 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72 6e o previous journ
f8a30 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 al header */. i
f8a40 36 34 20 73 74 6d 74 48 64 72 4f 66 66 3b 20 20 64 stmtHdrOff;
f8a50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
f8a60 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 rst journal head
f8a70 65 72 20 77 72 69 74 74 65 6e 20 74 68 69 73 20 er written this
f8a80 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 statement */. i
f8a90 36 34 20 73 74 6d 74 43 6b 73 75 6d 3b 20 20 20 64 stmtCksum;
f8aa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6b /* ck
f8ab0 73 75 6d 49 6e 69 74 20 77 68 65 6e 20 73 74 61 sumInit when sta
f8ac0 74 65 6d 65 6e 74 20 77 61 73 20 73 74 61 72 74 tement was start
f8ad0 65 64 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74 ed */. i64 stmt
f8ae0 4a 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 JSize;
f8af0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a /* Size of j
f8b00 6f 75 72 6e 61 6c 20 61 74 20 73 74 6d 74 5f 62 ournal at stmt_b
f8b10 65 67 69 6e 28 29 20 2a 2f 0a 20 20 75 33 32 20 egin() */. u32
f8b20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 sectorSize;
f8b30 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d /* Assum
f8b40 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 64 ed sector size d
f8b50 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 2a uring rollback *
f8b60 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
f8b70 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c TEST. int nHit,
f8b80 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20 nMiss;
f8b90 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73 /* Cache hits
f8ba0 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a and missing */.
f8bb0 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 int nRead, nWr
f8bc0 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ite; /*
f8bd0 20 44 61 74 61 62 61 73 65 20 70 61 67 65 73 20 Database pages
f8be0 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a read/written */.
f8bf0 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a #endif. void (*
f8c00 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 67 xReiniter)(DbPag
f8c10 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 e*); /* Call thi
f8c20 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 s routine when r
f8c30 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a eloading pages *
f8c40 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
f8c50 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 HAS_CODEC. void
f8c60 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 *(*xCodec)(void
f8c70 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 *,void*,Pgno,int
f8c80 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f ); /* Routine fo
f8c90 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 r en/decoding da
f8ca0 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 ta */. void *pC
f8cb0 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20 20 20 odecArg;
f8cc0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 /* First arg
f8cd0 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 28 ument to xCodec(
f8ce0 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 ) */.#endif. ch
f8cf0 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 ar *pTmpSpace;
f8d00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
f8d10 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 er.pageSize byte
f8d20 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 s of space for t
f8d30 6d 70 20 75 73 65 20 2a 2f 0a 20 20 63 68 61 72 mp use */. char
f8d40 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b dbFileVers[16];
f8d50 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 /* Chang
f8d60 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 es whenever data
f8d70 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 base file change
f8d80 73 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e s */. i64 journ
f8d90 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 alSizeLimit;
f8da0 20 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 /* Size limit
f8db0 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 for persistent
f8dc0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f journal files */
f8dd0 0a 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 . PCache *pPCac
f8de0 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f he; /
f8df0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 * Pointer to pag
f8e00 65 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a e cache object *
f8e10 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 /.};../*.** The
f8e20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c following global
f8e30 20 76 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 variables hold
f8e40 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20 66 6f counters used fo
f8e50 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 72 r.** testing pur
f8e60 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 poses only. The
f8e70 73 65 20 76 61 72 69 61 62 6c 65 73 20 64 6f 20 se variables do
f8e80 6e 6f 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 not exist in.**
f8e90 61 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 a non-testing bu
f8ea0 69 6c 64 2e 20 20 54 68 65 73 65 20 76 61 72 69 ild. These vari
f8eb0 61 62 6c 65 73 20 61 72 65 20 6e 6f 74 20 74 68 ables are not th
f8ec0 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 read-safe..*/.#i
f8ed0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
f8ee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f8ef0 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 sqlite3_pager_re
f8f00 61 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 addb_count = 0;
f8f10 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
f8f20 66 75 6c 6c 20 70 61 67 65 73 20 72 65 61 64 20 full pages read
f8f30 66 72 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 from DB */.SQLIT
f8f40 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
f8f50 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 3_pager_writedb_
f8f60 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 count = 0; /*
f8f70 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 Number of full p
f8f80 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 ages written to
f8f90 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 DB */.SQLITE_API
f8fa0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 int sqlite3_pag
f8fb0 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 er_writej_count
f8fc0 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 = 0; /* Numbe
f8fd0 72 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 r of pages writt
f8fe0 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f en to journal */
f8ff0 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f .# define PAGER_
f9000 49 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c INCR(v) v++.#el
f9010 73 65 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 se.# define PAGE
f9020 52 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 R_INCR(v).#endif
f9030 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 ..../*.** Journa
f9040 6c 20 66 69 6c 65 73 20 62 65 67 69 6e 20 77 69 l files begin wi
f9050 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 th the following
f9060 20 6d 61 67 69 63 20 73 74 72 69 6e 67 2e 20 20 magic string.
f9070 54 68 65 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 The data.** was
f9080 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 obtained from /d
f9090 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 ev/random. It i
f90a0 73 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 s used only as a
f90b0 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a sanity check..*
f90c0 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 69 *.** Since versi
f90d0 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f on 2.8.0, the jo
f90e0 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e urnal format con
f90f0 74 61 69 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c tains additional
f9100 20 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b sanity.** check
f9110 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e ing information.
f9120 20 20 49 66 20 74 68 65 20 70 6f 77 65 72 20 66 If the power f
f9130 61 69 6c 73 20 77 68 69 6c 65 20 74 68 65 20 6a ails while the j
f9140 6f 75 72 6e 61 6c 20 69 73 20 62 65 67 69 6e 0a ournal is begin.
f9150 2a 2a 20 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 ** written, semi
f9160 2d 72 61 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 -random garbage
f9170 64 61 74 61 20 6d 69 67 68 74 20 61 70 70 65 61 data might appea
f9180 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c r in the journal
f9190 0a 2a 2a 20 66 69 6c 65 20 61 66 74 65 72 20 70 .** file after p
f91a0 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 ower is restored
f91b0 2e 20 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 . If an attempt
f91c0 20 69 73 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a is then made.**
f91d0 20 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 to roll the jou
f91e0 72 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 rnal back, the d
f91f0 61 74 61 62 61 73 65 20 63 6f 75 6c 64 20 62 65 atabase could be
f9200 20 63 6f 72 72 75 70 74 65 64 2e 20 20 54 68 65 corrupted. The
f9210 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 additional.** s
f9220 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 64 anity checking d
f9230 61 74 61 20 69 73 20 61 6e 20 61 74 74 65 6d 70 ata is an attemp
f9240 74 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 t to discover th
f9250 65 20 67 61 72 62 61 67 65 20 69 6e 20 74 68 65 e garbage in the
f9260 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 .** journal and
f9270 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a ignore it..**.**
f9280 20 54 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 The sanity chec
f9290 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e king information
f92a0 20 66 6f 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 for the new jou
f92b0 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 rnal format cons
f92c0 69 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d ists.** of a 32-
f92d0 62 69 74 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 bit checksum on
f92e0 65 61 63 68 20 70 61 67 65 20 6f 66 20 64 61 74 each page of dat
f92f0 61 2e 20 20 54 68 65 20 63 68 65 63 6b 73 75 6d a. The checksum
f9300 20 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 covers both.**
f9310 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
f9320 61 6e 64 20 74 68 65 20 70 50 61 67 65 72 2d 3e and the pPager->
f9330 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f pageSize bytes o
f9340 66 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 70 f data for the p
f9350 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 age..** This cks
f9360 75 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 um is initialize
f9370 64 20 74 6f 20 61 20 33 32 2d 62 69 74 20 72 61 d to a 32-bit ra
f9380 6e 64 6f 6d 20 76 61 6c 75 65 20 74 68 61 74 20 ndom value that
f9390 61 70 70 65 61 72 73 20 69 6e 20 74 68 65 0a 2a appears in the.*
f93a0 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 * journal file r
f93b0 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20 68 ight after the h
f93c0 65 61 64 65 72 2e 20 20 54 68 65 20 72 61 6e 64 eader. The rand
f93d0 6f 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 om initializer i
f93e0 73 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 s important,.**
f93f0 62 65 63 61 75 73 65 20 67 61 72 62 61 67 65 20 because garbage
f9400 64 61 74 61 20 74 68 61 74 20 61 70 70 65 61 72 data that appear
f9410 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 s at the end of
f9420 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b a journal is lik
f9430 65 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 ely.** data that
f9440 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 was once in oth
f9450 65 72 20 66 69 6c 65 73 20 74 68 61 74 20 68 61 er files that ha
f9460 76 65 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 ve now been dele
f9470 74 65 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 ted. If the.**
f9480 67 61 72 62 61 67 65 20 64 61 74 61 20 63 61 6d garbage data cam
f9490 65 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 e from an obsole
f94a0 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c te journal file,
f94b0 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 6d the checksums m
f94c0 69 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 ight.** be corre
f94d0 63 74 2e 20 20 42 75 74 20 62 79 20 69 6e 69 74 ct. But by init
f94e0 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 63 68 65 ializing the che
f94f0 63 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 cksum to random
f9500 76 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 value which.** i
f9510 73 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 20 s different for
f9520 65 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 every journal, w
f9530 65 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 e minimize that
f9540 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 risk..*/.static
f9550 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
f9560 68 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 har aJournalMagi
f9570 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 c[] = {. 0xd9,
f9580 30 78 64 35 2c 20 30 78 30 35 2c 20 30 78 66 39 0xd5, 0x05, 0xf9
f9590 2c 20 30 78 32 30 2c 20 30 78 61 31 2c 20 30 78 , 0x20, 0xa1, 0x
f95a0 36 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 63, 0xd7,.};../*
f95b0 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 .** The size of
f95c0 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 6f the header and o
f95d0 66 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 f each page in t
f95e0 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65 he journal is de
f95f0 74 65 72 6d 69 6e 65 64 0a 2a 2a 20 62 79 20 74 termined.** by t
f9600 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
f9610 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ros..*/.#define
f9620 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
f9630 61 67 65 72 29 20 20 28 28 70 50 61 67 65 72 2d ager) ((pPager-
f9640 3e 70 61 67 65 53 69 7a 65 29 20 2b 20 38 29 0a >pageSize) + 8).
f9650 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e ./*.** The journ
f9660 61 6c 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 al header size f
f9670 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 49 or this pager. I
f9680 6e 20 74 68 65 20 66 75 74 75 72 65 2c 20 74 68 n the future, th
f9690 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 73 is could be.** s
f96a0 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c 75 65 et to some value
f96b0 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 read from the d
f96c0 69 73 6b 20 63 6f 6e 74 72 6f 6c 6c 65 72 2e 20 isk controller.
f96d0 54 68 65 20 69 6d 70 6f 72 74 61 6e 74 0a 2a 2a The important.**
f96e0 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 20 characteristic
f96f0 69 73 20 74 68 61 74 20 69 74 20 69 73 20 74 68 is that it is th
f9700 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20 61 e same size as a
f9710 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 0a 2a 2f disk sector..*/
f9720 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c .#define JOURNAL
f9730 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
f9740 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 (pPager->sectorS
f9750 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ize)../*.** The
f9760 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 74 macro MEMDB is t
f9770 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64 65 rue if we are de
f9780 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69 6e aling with an in
f9790 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
f97a0 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 20 ..** We do this
f97b0 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 as a macro so th
f97c0 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 at if the SQLITE
f97d0 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d _OMIT_MEMORYDB m
f97e0 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20 acro is set,.**
f97f0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d the value of MEM
f9800 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e DB will be a con
f9810 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63 6f stant and the co
f9820 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69 mpiler will opti
f9830 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65 mize.** out code
f9840 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 65 that would neve
f9850 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69 r execute..*/.#i
f9860 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
f9870 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 69 _MEMORYDB.# defi
f9880 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65 ne MEMDB 0.#else
f9890 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 .# define MEMDB
f98a0 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65 pPager->memDb.#e
f98b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 ndif../*.** Page
f98c0 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d 4a number PAGER_MJ
f98d0 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20 75 _PGNO is never u
f98e0 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 sed in an SQLite
f98f0 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69 73 database (it is
f9900 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f 72 .** reserved for
f9910 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64 20 working around
f9920 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78 20 a windows/posix
f9930 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 29 incompatibility)
f9940 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 . It is.** used
f9950 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 in the journal t
f9960 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20 74 o signify that t
f9970 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 he remainder of
f9980 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
f9990 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64 20 .** is devoted
f99a0 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61 73 to storing a mas
f99b0 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
f99c0 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 - there are no
f99d0 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a 2a more pages to.**
f99e0 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65 20 roll back. See
f99f0 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75 6e comments for fun
f9a00 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74 65 ction writeMaste
f9a10 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 64 rJournal() for d
f9a20 65 74 61 69 6c 73 2e 0a 2a 2f 0a 2f 2a 20 23 64 etails..*/./* #d
f9a30 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 efine PAGER_MJ_P
f9a40 47 4e 4f 28 78 29 20 28 50 45 4e 44 49 4e 47 5f GNO(x) (PENDING_
f9a50 42 59 54 45 2f 28 28 78 29 2d 3e 70 61 67 65 53 BYTE/((x)->pageS
f9a60 69 7a 65 29 29 20 2a 2f 0a 23 64 65 66 69 6e 65 ize)) */.#define
f9a70 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 78 PAGER_MJ_PGNO(x
f9a80 29 20 28 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 ) ((PENDING_BYTE
f9a90 2f 28 28 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 /((x)->pageSize)
f9aa0 29 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 )+1)../*.** The
f9ab0 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 maximum legal pa
f9ac0 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e ge number is (2^
f9ad0 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 31 - 1)..*/.#def
f9ae0 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 ine PAGER_MAX_PG
f9af0 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a 2f NO 2147483647../
f9b00 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
f9b10 20 69 66 20 70 61 67 65 20 2a 70 50 67 20 68 61 if page *pPg ha
f9b20 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 s already been w
f9b30 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74 ritten to the st
f9b40 61 74 65 6d 65 6e 74 0a 2a 2a 20 6a 6f 75 72 6e atement.** journ
f9b50 61 6c 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 al (or statement
f9b60 20 73 6e 61 70 73 68 6f 74 20 68 61 73 20 62 65 snapshot has be
f9b70 65 6e 20 63 72 65 61 74 65 64 2c 20 69 66 20 2a en created, if *
f9b80 70 50 67 20 69 73 20 70 61 72 74 0a 2a 2a 20 6f pPg is part.** o
f9b90 66 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 f an in-memory d
f9ba0 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 atabase)..*/.sta
f9bb0 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 53 74 tic int pageInSt
f9bc0 61 74 65 6d 65 6e 74 28 50 67 48 64 72 20 2a 70 atement(PgHdr *p
f9bd0 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 Pg){. Pager *pP
f9be0 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
f9bf0 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c er;. return sql
f9c00 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
f9c10 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20 Pager->pInStmt,
f9c20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f pPg->pgno);.}../
f9c30 2a 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 *.** Read a 32-b
f9c40 69 74 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 it integer from
f9c50 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 the given file d
f9c60 65 73 63 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 escriptor. Stor
f9c70 65 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a e the integer.**
f9c80 20 74 68 61 74 20 69 73 20 72 65 61 64 20 69 6e that is read in
f9c90 20 2a 70 52 65 73 2e 20 20 52 65 74 75 72 6e 20 *pRes. Return
f9ca0 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 SQLITE_OK if eve
f9cb0 72 79 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 rything worked,
f9cc0 6f 72 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 or an.** error c
f9cd0 6f 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 ode is something
f9ce0 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a goes wrong..**.
f9cf0 2a 2a 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 ** All values ar
f9d00 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b e stored on disk
f9d10 20 61 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a as big-endian..
f9d20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
f9d30 61 64 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 ad32bits(sqlite3
f9d40 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f _file *fd, i64 o
f9d50 66 66 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73 ffset, u32 *pRes
f9d60 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ){. unsigned ch
f9d70 61 72 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 ar ac[4];. int
f9d80 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
f9d90 61 64 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f ad(fd, ac, sizeo
f9da0 66 28 61 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a f(ac), offset);.
f9db0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
f9dc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 _OK ){. *pRes
f9dd0 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 = sqlite3Get4by
f9de0 74 65 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 te(ac);. }. re
f9df0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
f9e00 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 * Write a 32-bit
f9e10 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 20 integer into a
f9e20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 69 6e string buffer in
f9e30 20 62 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 big-endian byte
f9e40 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 order..*/.#defi
f9e50 6e 65 20 70 75 74 33 32 62 69 74 73 28 41 2c 42 ne put32bits(A,B
f9e60 29 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 ) sqlite3Put4by
f9e70 74 65 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a te((u8*)A,B)../*
f9e80 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 .** Write a 32-b
f9e90 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 it integer into
f9ea0 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 the given file d
f9eb0 65 73 63 72 69 70 74 6f 72 2e 20 20 52 65 74 75 escriptor. Retu
f9ec0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 rn SQLITE_OK.**
f9ed0 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e on success or an
f9ee0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 error code is s
f9ef0 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 omething goes wr
f9f00 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ong..*/.static i
f9f10 6e 74 20 77 72 69 74 65 33 32 62 69 74 73 28 73 nt write32bits(s
f9f20 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c qlite3_file *fd,
f9f30 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 i64 offset, u32
f9f40 20 76 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 val){. char ac
f9f50 5b 34 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 73 [4];. put32bits
f9f60 28 61 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 (ac, val);. ret
f9f70 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 69 urn sqlite3OsWri
f9f80 74 65 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 te(fd, ac, 4, of
f9f90 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 fset);.}../*.**
f9fa0 49 66 20 66 69 6c 65 20 70 46 64 20 69 73 20 6f If file pFd is o
f9fb0 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 pen, call sqlite
f9fc0 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20 69 3OsUnlock() on i
f9fd0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
f9fe0 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 osUnlock(sqlite
f9ff0 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e 74 3_file *pFd, int
fa000 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 21 eLock){. if( !
fa010 70 46 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b pFd->pMethods ){
fa020 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
fa030 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 TE_OK;. }. ret
fa040 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c urn sqlite3OsUnl
fa050 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b ock(pFd, eLock);
fa060 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
fa070 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e unction determin
fa080 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f es whether or no
fa090 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 t the atomic-wri
fa0a0 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a te optimization.
fa0b0 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 ** can be used w
fa0c0 69 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 ith this pager.
fa0d0 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e The optimization
fa0e0 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a can be used if:
fa0f0 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 .**.** (a) the
fa100 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
fa110 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 y OsDeviceCharac
fa120 74 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 teristics() indi
fa130 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 cates that.**
fa140 20 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61 a database pa
fa150 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 ge may be writte
fa160 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e n atomically, an
fa170 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 d.** (b) the va
fa180 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
fa190 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 OsSectorSize() i
fa1a0 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
fa1b0 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 qual.** to
fa1c0 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a the page size..*
fa1d0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 *.** If the opti
fa1e0 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 mization cannot
fa1f0 62 65 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 be used, 0 is re
fa200 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 turned. If it ca
fa210 6e 20 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 n be used,.** th
fa220 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 en the value ret
fa230 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a urned is the siz
fa240 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
fa250 20 66 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a file when it.**
fa260 20 63 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 contains rollba
fa270 63 6b 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 ck data for exac
fa280 74 6c 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f tly one page..*/
fa290 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
fa2a0 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 NABLE_ATOMIC_WRI
fa2b0 54 45 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 TE.static int jr
fa2c0 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 nlBufferSize(Pag
fa2d0 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
fa2e0 6e 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 nt dc;
fa2f0 20 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 /* Device chara
fa300 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 cteristics */.
fa310 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 int nSector;
fa320 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 /* Sector size
fa330 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 */. int szPage
fa340 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 ; /* Page
fa350 20 73 69 7a 65 20 2a 2f 0a 20 20 73 71 6c 69 74 size */. sqlit
fa360 65 33 5f 66 69 6c 65 20 2a 66 64 20 3d 20 70 50 e3_file *fd = pP
fa370 61 67 65 72 2d 3e 66 64 3b 0a 0a 20 20 69 66 28 ager->fd;.. if(
fa380 20 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b fd->pMethods ){
fa390 0a 20 20 20 20 64 63 20 3d 20 73 71 6c 69 74 65 . dc = sqlite
fa3a0 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 3OsDeviceCharact
fa3b0 65 72 69 73 74 69 63 73 28 66 64 29 3b 0a 20 20 eristics(fd);.
fa3c0 20 20 6e 53 65 63 74 6f 72 20 3d 20 73 71 6c 69 nSector = sqli
fa3d0 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 te3OsSectorSize(
fa3e0 66 64 29 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 fd);. szPage
fa3f0 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
fa400 7a 65 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 ze;. }.. asser
fa410 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 t(SQLITE_IOCAP_A
fa420 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e TOMIC512==(512>>
fa430 38 29 29 3b 0a 20 20 61 73 73 65 72 74 28 53 51 8));. assert(SQ
fa440 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
fa450 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 C64K==(65536>>8)
fa460 29 3b 0a 0a 20 20 69 66 28 20 21 66 64 2d 3e 70 );.. if( !fd->p
fa470 4d 65 74 68 6f 64 73 20 7c 7c 20 0a 20 20 20 20 Methods || .
fa480 20 20 20 28 64 63 20 26 20 28 53 51 4c 49 54 45 (dc & (SQLITE
fa490 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 73 _IOCAP_ATOMIC|(s
fa4a0 7a 50 61 67 65 3e 3e 38 29 29 20 26 26 20 6e 53 zPage>>8)) && nS
fa4b0 65 63 74 6f 72 3c 3d 73 7a 50 61 67 65 29 20 29 ector<=szPage) )
fa4c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 4a 4f 55 {. return JOU
fa4d0 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
fa4e0 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 er) + JOURNAL_PG
fa4f0 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d _SZ(pPager);. }
fa500 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 . return 0;.}.#
fa510 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 endif../*.** Thi
fa520 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c s function shoul
fa530 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e d be called when
fa540 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
fa550 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 within the page
fa560 72 0a 2a 2a 20 63 6f 64 65 2e 20 54 68 65 20 66 r.** code. The f
fa570 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 irst argument is
fa580 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
fa590 65 20 70 61 67 65 72 20 73 74 72 75 63 74 75 72 e pager structur
fa5a0 65 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 e, the.** second
fa5b0 20 74 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 the error-code
fa5c0 61 62 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75 about to be retu
fa5d0 72 6e 65 64 20 62 79 20 61 20 70 61 67 65 72 20 rned by a pager
fa5e0 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a API function. .*
fa5f0 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 * The value retu
fa600 72 6e 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f rned is a copy o
fa610 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
fa620 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 ument to this fu
fa630 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 nction. .**.** I
fa640 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
fa650 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f ument is SQLITE_
fa660 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f IOERR, SQLITE_CO
fa670 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 45 RRUPT, or SQLITE
fa680 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72 _FULL.** the err
fa690 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 73 69 or becomes persi
fa6a0 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68 65 stent. Until the
fa6b0 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 6f 72 persisten error
fa6c0 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a 20 is cleared,.**
fa6d0 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 20 63 subsequent API c
fa6e0 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 67 alls on this Pag
fa6f0 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 er will immediat
fa700 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 73 ely return the s
fa710 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63 6f ame .** error co
fa720 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 73 de..**.** A pers
fa730 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 6e 64 istent error ind
fa740 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65 20 icates that the
fa750 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
fa760 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 pager-cache .**
fa770 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 cannot be truste
fa780 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 63 61 d. This state ca
fa790 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 n be cleared by
fa7a0 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 61 completely disca
fa7b0 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f rding .** the co
fa7c0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
fa7d0 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 61 20 ger-cache. If a
fa7e0 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
fa7f0 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 active when.** t
fa800 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 he persistent er
fa810 72 6f 72 20 6f 63 63 75 72 65 64 2c 20 74 68 65 ror occured, the
fa820 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a n the rollback j
fa830 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a ournal may need.
fa840 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 ** to be replaye
fa850 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
fa860 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 d pager_unlock(P
fa870 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 73 ager *pPager);.s
fa880 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f tatic int pager_
fa890 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 61 error(Pager *pPa
fa8a0 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 ger, int rc){.
fa8b0 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 30 int rc2 = rc & 0
fa8c0 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 0a 20 xff;. assert(.
fa8d0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 pPager->er
fa8e0 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55 rCode==SQLITE_FU
fa8f0 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 50 61 LL ||. pPa
fa900 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 ger->errCode==SQ
fa910 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20 LITE_OK ||.
fa920 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f (pPager->errCo
fa930 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c 49 de & 0xff)==SQLI
fa940 54 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20 20 TE_IOERR. );.
fa950 69 66 28 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c if(. rc2==SQL
fa960 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 ITE_FULL ||.
fa970 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 rc2==SQLITE_IOER
fa980 52 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53 51 R ||. rc2==SQ
fa990 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 20 20 29 LITE_CORRUPT. )
fa9a0 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 {. pPager->er
fa9b0 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 rCode = rc;.
fa9c0 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
fa9d0 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 e==PAGER_UNLOCK
fa9e0 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 . && sqlite3
fa9f0 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 PcacheRefCount(p
faa00 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d Pager->pPCache)=
faa10 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 =0 . ){.
faa20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 /* If the pager
faa30 20 69 73 20 61 6c 72 65 61 64 79 20 75 6e 6c 6f is already unlo
faa40 63 6b 65 64 2c 20 63 61 6c 6c 20 70 61 67 65 72 cked, call pager
faa50 5f 75 6e 6c 6f 63 6b 28 29 20 6e 6f 77 20 74 6f _unlock() now to
faa60 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 72 20 . ** clear
faa70 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 the error state
faa80 61 6e 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 and ensure that
faa90 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 the pager-cache
faaa0 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d is . ** com
faab0 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 0a 20 pletely empty..
faac0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 61 */. pa
faad0 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 ger_unlock(pPage
faae0 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 r);. }. }.
faaf0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
fab00 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 48 .** If SQLITE_CH
fab10 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66 ECK_PAGES is def
fab20 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 6f 20 ined then we do
fab30 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 65 63 some sanity chec
fab40 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 king.** on the c
fab50 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 61 73 ache using a has
fab60 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 h function. Thi
fab70 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 s is used for te
fab80 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62 sting.** and deb
fab90 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a ugging only..*/.
faba0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 #ifdef SQLITE_CH
fabb0 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 ECK_PAGES./*.**
fabc0 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 74 20 Return a 32-bit
fabd0 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 67 65 hash of the page
fabe0 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 2e data for pPage.
fabf0 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 .*/.static u32 p
fac00 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 69 6e ager_datahash(in
fac10 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 6e 65 t nByte, unsigne
fac20 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 7b 0a d char *pData){.
fac30 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 3b 0a u32 hash = 0;.
fac40 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
fac50 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b 2b =0; i<nByte; i++
fac60 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 28 68 ){. hash = (h
fac70 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61 74 ash*1039) + pDat
fac80 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 a[i];. }. retu
fac90 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 74 69 rn hash;.}.stati
faca0 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 67 65 c u32 pager_page
facb0 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 67 hash(PgHdr *pPag
facc0 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 67 e){. return pag
facd0 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67 er_datahash(pPag
face0 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 65 53 e->pPager->pageS
facf0 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 ize, (unsigned c
fad00 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 44 61 har *)pPage->pDa
fad10 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 ta);.}.static u3
fad20 32 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65 2 pager_set_page
fad30 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 67 hash(PgHdr *pPag
fad40 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e 70 61 67 e){. pPage->pag
fad50 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 eHash = pager_pa
fad60 67 65 68 61 73 68 28 70 50 61 67 65 29 3b 0a 7d gehash(pPage);.}
fad70 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 ../*.** The CHEC
fad80 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 61 6b K_PAGE macro tak
fad90 65 73 20 61 20 50 67 48 64 72 2a 20 61 73 20 61 es a PgHdr* as a
fada0 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 53 n argument. If S
fadb0 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
fadc0 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2c S.** is defined,
fadd0 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73 20 6e and NDEBUG is n
fade0 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 ot defined, an a
fadf0 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
fae00 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 61 74 t checks.** that
fae10 20 74 68 65 20 70 61 67 65 20 69 73 20 65 69 74 the page is eit
fae20 68 65 72 20 64 69 72 74 79 20 6f 72 20 73 74 69 her dirty or sti
fae30 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65 20 63 ll matches the c
fae40 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65 2d 68 alculated page-h
fae50 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ash..*/.#define
fae60 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63 68 CHECK_PAGE(x) ch
fae70 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61 74 69 eckPage(x).stati
fae80 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67 65 c void checkPage
fae90 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 (PgHdr *pPg){.
faea0 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
faeb0 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 61 pPg->pPager;. a
faec0 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70 61 67 ssert( !pPg->pag
faed0 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65 72 2d eHash || pPager-
faee0 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 20 20 7c >errCode. |
faef0 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 | (pPg->flags&PG
faf00 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20 70 50 HDR_DIRTY) || pP
faf10 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70 61 67 g->pageHash==pag
faf20 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 er_pagehash(pPg)
faf30 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23 64 65 );.}..#else.#de
faf40 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74 61 68 fine pager_datah
faf50 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64 65 66 ash(X,Y) 0.#def
faf60 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65 68 61 ine pager_pageha
faf70 73 68 28 58 29 20 20 30 0a 23 64 65 66 69 6e 65 sh(X) 0.#define
faf80 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 0a 23 CHECK_PAGE(x).#
faf90 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 endif /* SQLITE
fafa0 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 2a 2f 0a _CHECK_PAGES */.
fafb0 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 ./*.** When this
fafc0 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 6a is called the j
fafd0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 ournal file for
fafe0 70 61 67 65 72 20 70 50 61 67 65 72 20 6d 75 73 pager pPager mus
faff0 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 54 68 t be open..** Th
fb000 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
fb010 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 72 65 file name is re
fb020 61 64 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 ad from the end
fb030 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 of the file and
fb040 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e 74 6f .** written into
fb050 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 memory supplied
fb060 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 by the caller.
fb070 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d .**.** zMaster m
fb080 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 ust point to a b
fb090 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73 uffer of at leas
fb0a0 74 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20 t nMaster bytes
fb0b0 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 allocated by.**
fb0c0 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 68 69 73 the caller. This
fb0d0 20 73 68 6f 75 6c 64 20 62 65 20 73 71 6c 69 74 should be sqlit
fb0e0 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d e3_vfs.mxPathnam
fb0f0 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 65 20 74 e+1 (to ensure t
fb100 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 here is.** enoug
fb110 68 20 73 70 61 63 65 20 74 6f 20 77 72 69 74 65 h space to write
fb120 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
fb130 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 20 74 68 nal name). If th
fb140 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
fb150 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 .** name in the
fb160 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 journal is longe
fb170 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 72 20 62 r than nMaster b
fb180 79 74 65 73 20 28 69 6e 63 6c 75 64 69 6e 67 20 ytes (including
fb190 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 a.** nul-termina
fb1a0 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 69 73 20 tor), then this
fb1b0 69 73 20 68 61 6e 64 6c 65 64 20 61 73 20 69 66 is handled as if
fb1c0 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e no master journ
fb1d0 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 72 65 20 al name.** were
fb1e0 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 6a present in the j
fb1f0 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 ournal..**.** If
fb200 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e no master journ
fb210 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 al file name is
fb220 70 72 65 73 65 6e 74 20 7a 4d 61 73 74 65 72 5b present zMaster[
fb230 30 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 61 0] is set to 0 a
fb240 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 nd.** SQLITE_OK
fb250 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 returned..*/.sta
fb260 74 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 74 tic int readMast
fb270 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 erJournal(sqlite
fb280 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63 3_file *pJrnl, c
fb290 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 69 6e har *zMaster, in
fb2a0 74 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e t nMaster){. in
fb2b0 74 20 72 63 3b 0a 20 20 75 33 32 20 6c 65 6e 3b t rc;. u32 len;
fb2c0 0a 20 20 69 36 34 20 73 7a 4a 3b 0a 20 20 75 33 . i64 szJ;. u3
fb2d0 32 20 63 6b 73 75 6d 3b 0a 20 20 75 33 32 20 75 2 cksum;. u32 u
fb2e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
fb2f0 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 /* Unsigned
fb300 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a loop counter */.
fb310 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
fb320 61 4d 61 67 69 63 5b 38 5d 3b 20 2f 2a 20 41 20 aMagic[8]; /* A
fb330 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 buffer to hold t
fb340 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 20 he magic header
fb350 2a 2f 0a 0a 20 20 7a 4d 61 73 74 65 72 5b 30 5d */.. zMaster[0]
fb360 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 72 63 20 3d = '\0';.. rc =
fb370 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
fb380 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 3b ze(pJrnl, &szJ);
fb390 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
fb3a0 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3c 31 36 20 29 E_OK || szJ<16 )
fb3b0 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 return rc;.. r
fb3c0 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 c = read32bits(p
fb3d0 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c Jrnl, szJ-16, &l
fb3e0 65 6e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 en);. if( rc!=S
fb3f0 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
fb400 6e 20 72 63 3b 0a 0a 20 20 69 66 28 20 6c 65 6e n rc;.. if( len
fb410 3e 3d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 >=nMaster ){.
fb420 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
fb430 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72 K;. }.. rc = r
fb440 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c ead32bits(pJrnl,
fb450 20 73 7a 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 szJ-12, &cksum)
fb460 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
fb470 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
fb480 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 c;.. rc = sqlit
fb490 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 e3OsRead(pJrnl,
fb4a0 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a 4a 2d 38 aMagic, 8, szJ-8
fb4b0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
fb4c0 49 54 45 5f 4f 4b 20 7c 7c 20 6d 65 6d 63 6d 70 ITE_OK || memcmp
fb4d0 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 (aMagic, aJourna
fb4e0 6c 4d 61 67 69 63 2c 20 38 29 20 29 20 72 65 74 lMagic, 8) ) ret
fb4f0 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 urn rc;.. rc =
fb500 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a sqlite3OsRead(pJ
fb510 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c 20 6c 65 rnl, zMaster, le
fb520 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e 29 3b 0a n, szJ-16-len);.
fb530 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
fb540 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
fb550 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 n rc;. }. zMas
fb560 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b ter[len] = '\0';
fb570 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74 68 .. /* See if th
fb580 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 68 e checksum match
fb590 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f es the master jo
fb5a0 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 urnal name */.
fb5b0 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b 20 for(u=0; u<len;
fb5c0 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 u++){. cksum
fb5d0 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a 20 -= zMaster[u];.
fb5e0 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20 }. if( cksum
fb5f0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ){. /* If the
fb600 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27 checksum doesn'
fb610 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f t add up, then o
fb620 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
fb630 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20 e disk sectors.
fb640 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 ** containing
fb650 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
fb660 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 nal filename is
fb670 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20 corrupted. This
fb680 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66 means. ** def
fb690 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63 initely roll bac
fb6a0 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72 k, so just retur
fb6b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 n SQLITE_OK and
fb6c0 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20 report a (nul).
fb6d0 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 ** master-jou
fb6e0 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20 rnal filename..
fb6f0 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 */. zMaste
fb700 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 7d r[0] = '\0';. }
fb710 0a 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 . . return SQ
fb720 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
fb730 2a 20 53 65 65 6b 20 74 68 65 20 6a 6f 75 72 6e * Seek the journ
fb740 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 al file descript
fb750 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 or to the next s
fb760 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 77 ector boundary w
fb770 68 65 72 65 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 here a.** journa
fb780 6c 20 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 l header may be
fb790 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e read or written.
fb7a0 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 Pager.journalOf
fb7b0 66 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 f is updated wit
fb7c0 68 0a 2a 2a 20 74 68 65 20 6e 65 77 20 73 65 65 h.** the new see
fb7d0 6b 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 k offset..**.**
fb7e0 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 6f 72 i.e for a sector
fb7f0 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a size of 512:.**
fb800 0a 2a 2a 20 49 6e 70 75 74 20 4f 66 66 73 65 74 .** Input Offset
fb810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 75 Ou
fb820 74 70 75 74 20 4f 66 66 73 65 74 0a 2a 2a 20 2d tput Offset.** -
fb830 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
fb840 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
fb850 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 30 20 20 20 20 20 ------.** 0
fb860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fb870 20 20 20 20 30 0a 2a 2a 20 35 31 32 20 20 20 20 0.** 512
fb880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fb890 20 20 20 35 31 32 0a 2a 2a 20 31 30 30 20 20 20 512.** 100
fb8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fb8b0 20 20 20 20 35 31 32 0a 2a 2a 20 32 30 30 30 20 512.** 2000
fb8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fb8d0 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 2048.** .*/
fb8e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 65 .static void see
fb8f0 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 kJournalHdr(Page
fb900 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36 r *pPager){. i6
fb910 34 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 4 offset = 0;.
fb920 69 36 34 20 63 20 3d 20 70 50 61 67 65 72 2d 3e i64 c = pPager->
fb930 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 journalOff;. if
fb940 28 20 63 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 ( c ){. offse
fb950 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e t = ((c-1)/JOURN
fb960 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
fb970 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c ) + 1) * JOURNAL
fb980 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b _HDR_SZ(pPager);
fb990 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f . }. assert( o
fb9a0 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 ffset%JOURNAL_HD
fb9b0 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 30 20 R_SZ(pPager)==0
fb9c0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 );. assert( off
fb9d0 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65 set>=c );. asse
fb9e0 72 74 28 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a rt( (offset-c)<J
fb9f0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
fba00 61 67 65 72 29 20 29 3b 0a 20 20 70 50 61 67 65 ager) );. pPage
fba10 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
fba20 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a offset;.}../*.**
fba30 20 57 72 69 74 65 20 7a 65 72 6f 73 20 6f 76 65 Write zeros ove
fba40 72 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20 r the header of
fba50 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
fba60 2e 20 20 54 68 69 73 20 68 61 73 20 74 68 65 0a . This has the.
fba70 2a 2a 20 65 66 66 65 63 74 20 6f 66 20 69 6e 76 ** effect of inv
fba80 61 6c 69 64 61 74 69 6e 67 20 74 68 65 20 6a 6f alidating the jo
fba90 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 63 urnal file and c
fbaa0 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a ommitting the.**
fbab0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f transaction..*/
fbac0 0a 73 74 61 74 69 63 20 69 6e 74 20 7a 65 72 6f .static int zero
fbad0 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 JournalHdr(Pager
fbae0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 64 6f *pPager, int do
fbaf0 54 72 75 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 Truncate){. int
fbb00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
fbb10 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
fbb20 63 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d char zeroHdr[28]
fbb30 20 3d 20 7b 30 7d 3b 0a 0a 20 20 69 66 28 20 70 = {0};.. if( p
fbb40 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
fbb50 66 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4c 69 f ){. i64 iLi
fbb60 6d 69 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f mit = pPager->jo
fbb70 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a urnalSizeLimit;.
fbb80 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a . IOTRACE(("J
fbb90 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 ZEROHDR %p\n", p
fbba0 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 Pager)). if(
fbbb0 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c doTruncate || iL
fbbc0 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 imit==0 ){.
fbbd0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
fbbe0 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
fbbf0 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c jfd, 0);. }el
fbc00 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 se{. rc = s
fbc10 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
fbc20 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48 ager->jfd, zeroH
fbc30 64 72 2c 20 73 69 7a 65 6f 66 28 7a 65 72 6f 48 dr, sizeof(zeroH
fbc40 64 72 29 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 dr), 0);. }.
fbc50 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
fbc60 45 5f 4f 4b 20 26 26 20 21 70 50 61 67 65 72 2d E_OK && !pPager-
fbc70 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 >noSync ){.
fbc80 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 rc = sqlite3OsS
fbc90 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ync(pPager->jfd,
fbca0 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 SQLITE_SYNC_DAT
fbcb0 41 4f 4e 4c 59 7c 70 50 61 67 65 72 2d 3e 73 79 AONLY|pPager->sy
fbcc0 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d nc_flags);. }
fbcd0 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 .. /* At this
fbce0 20 70 6f 69 6e 74 20 74 68 65 20 74 72 61 6e 73 point the trans
fbcf0 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
fbd00 74 65 64 20 62 75 74 20 74 68 65 20 77 72 69 74 ted but the writ
fbd10 65 20 6c 6f 63 6b 20 0a 20 20 20 20 2a 2a 20 69 e lock . ** i
fbd20 73 20 73 74 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 s still held on
fbd30 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 the file. If the
fbd40 72 65 20 69 73 20 61 20 73 69 7a 65 20 6c 69 6d re is a size lim
fbd50 69 74 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f it configured fo
fbd60 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 65 r . ** the pe
fbd70 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c rsistent journal
fbd80 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c and the journal
fbd90 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 file currently
fbda0 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 65 0a 20 20 consumes more.
fbdb0 20 20 2a 2a 20 73 70 61 63 65 20 74 68 61 6e 20 ** space than
fbdc0 74 68 61 74 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 that limit allow
fbdd0 73 20 66 6f 72 2c 20 74 72 75 6e 63 61 74 65 20 s for, truncate
fbde0 69 74 20 6e 6f 77 2e 20 54 68 65 72 65 20 69 73 it now. There is
fbdf0 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 no need. **
fbe00 74 6f 20 73 79 6e 63 20 74 68 65 20 66 69 6c 65 to sync the file
fbe10 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 following this
fbe20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a operation.. *
fbe30 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
fbe40 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 LITE_OK && iLimi
fbe50 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 36 34 t>0 ){. i64
fbe60 20 73 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 sz;. rc =
fbe70 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
fbe80 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 e(pPager->jfd, &
fbe90 73 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 sz);. if( r
fbea0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
fbeb0 73 7a 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 sz>iLimit ){.
fbec0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
fbed0 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 3OsTruncate(pPag
fbee0 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 6d 69 74 29 er->jfd, iLimit)
fbef0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
fbf00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
fbf10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f .}../*.** The jo
fbf20 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 urnal file must
fbf30 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 be open when thi
fbf40 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
fbf50 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a led. A journal.*
fbf60 2a 20 68 65 61 64 65 72 20 28 4a 4f 55 52 4e 41 * header (JOURNA
fbf70 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 L_HDR_SZ bytes)
fbf80 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
fbf90 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
fbfa0 20 61 74 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 at the.** curre
fbfb0 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a nt location..**.
fbfc0 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 6f ** The format fo
fbfd0 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 r the journal he
fbfe0 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f ader is as follo
fbff0 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 79 74 65 73 ws:.** - 8 bytes
fc000 3a 20 4d 61 67 69 63 20 69 64 65 6e 74 69 66 79 : Magic identify
fc010 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d ing journal form
fc020 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 at..** - 4 bytes
fc030 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f : Number of reco
fc040 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 rds in journal,
fc050 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f or -1 no-sync mo
fc060 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 de is on..** - 4
fc070 20 62 79 74 65 73 3a 20 52 61 6e 64 6f 6d 20 6e bytes: Random n
fc080 75 6d 62 65 72 20 75 73 65 64 20 66 6f 72 20 70 umber used for p
fc090 61 67 65 20 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 age hash..** - 4
fc0a0 20 62 79 74 65 73 3a 20 49 6e 69 74 69 61 6c 20 bytes: Initial
fc0b0 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63 6f database page co
fc0c0 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 unt..** - 4 byte
fc0d0 73 3a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 75 s: Sector size u
fc0e0 73 65 64 20 62 79 20 74 68 65 20 70 72 6f 63 65 sed by the proce
fc0f0 73 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 ss that wrote th
fc100 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d is journal..** -
fc110 20 34 20 62 79 74 65 73 3a 20 44 61 74 61 62 61 4 bytes: Databa
fc120 73 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a se page size..**
fc130 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64 20 62 79 .** Followed by
fc140 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a (JOURNAL_HDR_SZ
fc150 20 2d 20 32 38 29 20 62 79 74 65 73 20 6f 66 20 - 28) bytes of
fc160 75 6e 75 73 65 64 20 73 70 61 63 65 2e 0a 2a 2f unused space..*/
fc170 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 .static int writ
fc180 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 eJournalHdr(Page
fc190 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
fc1a0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
fc1b0 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65 ;. char *zHeade
fc1c0 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 r = pPager->pTmp
fc1d0 53 70 61 63 65 3b 0a 20 20 69 6e 74 20 6e 48 65 Space;. int nHe
fc1e0 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 ader = pPager->p
fc1f0 61 67 65 53 69 7a 65 3b 0a 20 20 69 6e 74 20 6e ageSize;. int n
fc200 57 72 69 74 65 3b 0a 0a 20 20 69 66 28 20 6e 48 Write;.. if( nH
fc210 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 eader>JOURNAL_HD
fc220 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a R_SZ(pPager) ){.
fc230 20 20 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f nHeader = JO
fc240 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
fc250 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 ger);. }.. if(
fc260 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 48 64 72 pPager->stmtHdr
fc270 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50 Off==0 ){. pP
fc280 61 67 65 72 2d 3e 73 74 6d 74 48 64 72 4f 66 66 ager->stmtHdrOff
fc290 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e = pPager->journ
fc2a0 61 6c 4f 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 65 alOff;. }.. se
fc2b0 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 ekJournalHdr(pPa
fc2c0 67 65 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e ger);. pPager->
fc2d0 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 50 61 journalHdr = pPa
fc2e0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
fc2f0 0a 0a 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 .. memcpy(zHead
fc300 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 er, aJournalMagi
fc310 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e c, sizeof(aJourn
fc320 61 6c 4d 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a alMagic));.. /*
fc330 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 . ** Write the
fc340 20 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 nRec Field - th
fc350 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
fc360 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f records that fo
fc370 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a llow this. ** j
fc380 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e ournal header. N
fc390 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 ormally, zero is
fc3a0 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 written to this
fc3b0 20 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 value at this t
fc3c0 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 ime.. ** After
fc3d0 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 the records are
fc3e0 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 added to the jou
fc3f0 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f rnal (and the jo
fc400 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 urnal synced, .
fc410 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 ** if in full-s
fc420 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a ync mode), the z
fc430 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 ero is overwritt
fc440 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 65 en with the true
fc450 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 number. ** of
fc460 72 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e records (see syn
fc470 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a cJournal()).. *
fc480 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 *. ** A faster
fc490 61 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 alternative is t
fc4a0 6f 20 77 72 69 74 65 20 30 78 46 46 46 46 46 46 o write 0xFFFFFF
fc4b0 46 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 FF to the nRec f
fc4c0 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 ield. When. **
fc4d0 72 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 reading the jour
fc4e0 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 nal this value t
fc4f0 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 ells SQLite to a
fc500 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 ssume that the.
fc510 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 ** rest of the
fc520 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e journal file con
fc530 74 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 tains valid page
fc540 20 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 records. This a
fc550 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 ssumption. ** i
fc560 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 s dangerous, as
fc570 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 if a failure occ
fc580 75 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 ured whilst writ
fc590 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ing to the journ
fc5a0 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 al. ** file it
fc5b0 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 may contain some
fc5c0 20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 garbage data. T
fc5d0 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 here are two sce
fc5e0 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 narios. ** wher
fc5f0 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 e this risk can
fc600 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a be ignored:. **
fc610 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 . ** * When t
fc620 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e he pager is in n
fc630 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 o-sync mode. Cor
fc640 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c ruption can foll
fc650 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f ow a. ** po
fc660 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 wer failure in t
fc670 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e his case anyway.
fc680 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 . **. ** * W
fc690 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 hen the SQLITE_I
fc6a0 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
fc6b0 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 flag is set. Th
fc6c0 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 is guarantees.
fc6d0 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62 ** that garb
fc6e0 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 age data is neve
fc6f0 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 r appended to th
fc700 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
fc710 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 70 50 */. assert(pP
fc720 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f ager->fd->pMetho
fc730 64 73 7c 7c 70 50 61 67 65 72 2d 3e 6e 6f 53 79 ds||pPager->noSy
fc740 6e 63 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 nc);. if( (pPag
fc750 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28 er->noSync) || (
fc760 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
fc770 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
fc780 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20 ALMODE_MEMORY).
fc790 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44 || (sqlite3OsD
fc7a0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
fc7b0 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
fc7c0 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 &SQLITE_IOCAP_SA
fc7d0 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b FE_APPEND) . ){
fc7e0 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 . put32bits(&
fc7f0 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
fc800 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 JournalMagic)],
fc810 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 7d 0xffffffff);. }
fc820 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32 62 else{. put32b
fc830 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
fc840 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
fc850 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 c)], 0);. }..
fc860 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68 /* The random ch
fc870 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c eck-hash initial
fc880 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74 iser */ . sqlit
fc890 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
fc8a0 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73 zeof(pPager->cks
fc8b0 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72 umInit), &pPager
fc8c0 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 ->cksumInit);.
fc8d0 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
fc8e0 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
fc8f0 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61 alMagic)+4], pPa
fc900 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b ger->cksumInit);
fc910 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 . /* The initia
fc920 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 l database size
fc930 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 */. put32bits(&
fc940 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
fc950 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d JournalMagic)+8]
fc960 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 , pPager->dbSize
fc970 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75 );. /* The assu
fc980 6d 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 med sector size
fc990 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 for this process
fc9a0 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 */. put32bits(
fc9b0 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
fc9c0 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 aJournalMagic)+1
fc9d0 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 2], pPager->sect
fc9e0 6f 72 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 orSize);. if( p
fc9f0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
fca00 72 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 r==0 ){. /* T
fca10 68 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a he page size */.
fca20 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 7a put32bits(&z
fca30 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
fca40 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d ournalMagic)+16]
fca50 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
fca60 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 ze);. }.. for(
fca70 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51 nWrite=0; rc==SQ
fca80 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c LITE_OK&&nWrite<
fca90 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
fcaa0 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d Pager); nWrite+=
fcab0 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f nHeader){. IO
fcac0 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 TRACE(("JHDR %p
fcad0 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 %lld %d\n", pPag
fcae0 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 er, pPager->jour
fcaf0 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29 nalHdr, nHeader)
fcb00 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ). rc = sqlit
fcb10 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
fcb20 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 ->jfd, zHeader,
fcb30 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d nHeader, pPager-
fcb40 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 >journalOff);.
fcb50 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
fcb60 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b lOff += nHeader;
fcb70 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
fcb80 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 c;.}../*.** The
fcb90 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 journal file mus
fcba0 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 t be open when t
fcbb0 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 his is called. A
fcbc0 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
fcbd0 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c file.** (JOURNAL
fcbe0 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 _HDR_SZ bytes) i
fcbf0 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 s read from the
fcc00 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e current location
fcc10 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a in the journal.
fcc20 2a 2a 20 66 69 6c 65 2e 20 53 65 65 20 63 6f 6d ** file. See com
fcc30 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 ments above func
fcc40 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 tion writeJourna
fcc50 6c 48 64 72 28 29 20 66 6f 72 20 61 20 64 65 73 lHdr() for a des
fcc60 63 72 69 70 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 cription of.** t
fcc70 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
fcc80 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 r format..**.**
fcc90 49 66 20 74 68 65 20 68 65 61 64 65 72 20 69 73 If the header is
fcca0 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c read successful
fccb0 6c 79 2c 20 2a 6e 52 65 63 20 69 73 20 73 65 74 ly, *nRec is set
fccc0 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
fccd0 66 0a 2a 2a 20 70 61 67 65 20 72 65 63 6f 72 64 f.** page record
fcce0 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 s following this
fccf0 20 68 65 61 64 65 72 20 61 6e 64 20 2a 64 62 53 header and *dbS
fcd00 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 ize is set to th
fcd10 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a e size of the.**
fcd20 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 database before
fcd30 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
fcd40 20 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 began, in pages
fcd50 2e 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e . Also, pPager->
fcd60 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 cksumInit.** is
fcd70 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 set to the value
fcd80 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a read from the j
fcd90 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 ournal header. S
fcda0 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
fcdb0 72 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 rned.** in this
fcdc0 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 case..**.** If t
fcdd0 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
fcde0 72 20 66 69 6c 65 20 61 70 70 65 61 72 73 20 74 r file appears t
fcdf0 6f 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c 20 o be corrupted,
fce00 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a SQLITE_DONE is.*
fce10 2a 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a * returned and *
fce20 6e 52 65 63 20 61 6e 64 20 2a 64 62 53 69 7a 65 nRec and *dbSize
fce30 20 61 72 65 20 6e 6f 74 20 73 65 74 2e 20 20 49 are not set. I
fce40 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a f JOURNAL_HDR_SZ
fce50 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f 74 bytes.** cannot
fce60 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 be read from th
fce70 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 e journal file a
fce80 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 n error code is
fce90 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 returned..*/.sta
fcea0 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75 72 tic int readJour
fceb0 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72 20 nalHdr(. Pager
fcec0 2a 70 50 61 67 65 72 2c 20 0a 20 20 69 36 34 20 *pPager, . i64
fced0 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 0a 20 20 75 journalSize,. u
fcee0 33 32 20 2a 70 4e 52 65 63 2c 20 0a 20 20 75 33 32 *pNRec, . u3
fcef0 32 20 2a 70 44 62 53 69 7a 65 0a 29 7b 0a 20 20 2 *pDbSize.){.
fcf00 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e int rc;. unsign
fcf10 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 ed char aMagic[8
fcf20 5d 3b 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 ]; /* A buffer t
fcf30 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 o hold the magic
fcf40 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 header */. i64
fcf50 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20 69 6e 74 20 jrnlOff;. int
fcf60 69 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 73 65 iPageSize;.. se
fcf70 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 ekJournalHdr(pPa
fcf80 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 ger);. if( pPag
fcf90 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 4a er->journalOff+J
fcfa0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
fcfb0 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61 6c 53 ager) > journalS
fcfc0 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ize ){. retur
fcfd0 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 n SQLITE_DONE;.
fcfe0 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66 20 3d 20 70 }. jrnlOff = p
fcff0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
fd000 66 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 f;.. rc = sqlit
fd010 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
fd020 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 73 69 >jfd, aMagic, si
fd030 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c 20 6a 72 zeof(aMagic), jr
fd040 6e 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 72 63 nlOff);. if( rc
fd050 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
fd060 6a 72 6e 6c 4f 66 66 20 2b 3d 20 73 69 7a 65 6f jrnlOff += sizeo
fd070 66 28 61 4d 61 67 69 63 29 3b 0a 0a 20 20 69 66 f(aMagic);.. if
fd080 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c ( memcmp(aMagic,
fd090 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
fd0a0 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 sizeof(aMagic))!
fd0b0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
fd0c0 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
fd0d0 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 }.. rc = read32
fd0e0 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
fd0f0 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70 4e 52 65 63 , jrnlOff, pNRec
fd100 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
fd110 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d turn rc;.. rc =
fd120 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 read32bits(pPag
fd130 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 er->jfd, jrnlOff
fd140 2b 34 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 +4, &pPager->cks
fd150 75 6d 49 6e 69 74 29 3b 0a 20 20 69 66 28 20 72 umInit);. if( r
fd160 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a c ) return rc;..
fd170 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 rc = read32bit
fd180 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a s(pPager->jfd, j
fd190 72 6e 6c 4f 66 66 2b 38 2c 20 70 44 62 53 69 7a rnlOff+8, pDbSiz
fd1a0 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 e);. if( rc ) r
fd1b0 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 eturn rc;.. rc
fd1c0 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 = read32bits(pPa
fd1d0 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 ger->jfd, jrnlOf
fd1e0 66 2b 31 36 2c 20 28 75 33 32 20 2a 29 26 69 50 f+16, (u32 *)&iP
fd1f0 61 67 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 ageSize);. if(
fd200 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 0a 20 rc==SQLITE_OK .
fd210 20 20 26 26 20 69 50 61 67 65 53 69 7a 65 3e 3d && iPageSize>=
fd220 35 31 32 20 0a 20 20 20 26 26 20 69 50 61 67 65 512 . && iPage
fd230 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 Size<=SQLITE_MAX
fd240 5f 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 20 26 _PAGE_SIZE . &
fd250 26 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 & ((iPageSize-1)
fd260 26 69 50 61 67 65 53 69 7a 65 29 3d 3d 30 20 0a &iPageSize)==0 .
fd270 20 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 ){. u16 pag
fd280 65 73 69 7a 65 20 3d 20 69 50 61 67 65 53 69 7a esize = iPageSiz
fd290 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 e;. rc = sqli
fd2a0 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 te3PagerSetPages
fd2b0 69 7a 65 28 70 50 61 67 65 72 2c 20 26 70 61 67 ize(pPager, &pag
fd2c0 65 73 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 69 66 esize);. }. if
fd2d0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
fd2e0 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 ;.. /* Update t
fd2f0 68 65 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f he assumed secto
fd300 72 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 r-size to match
fd310 74 68 65 20 76 61 6c 75 65 20 75 73 65 64 20 62 the value used b
fd320 79 20 0a 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 y . ** the proc
fd330 65 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64 ess that created
fd340 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 this journal. I
fd350 66 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 f this journal w
fd360 61 73 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 as. ** created
fd370 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74 68 by a process oth
fd380 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 er than this one
fd390 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
fd3a0 69 6e 65 0a 20 20 2a 2a 20 69 73 20 62 65 69 6e ine. ** is bein
fd3b0 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 g called from wi
fd3c0 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 62 thin pager_playb
fd3d0 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c ack(). The local
fd3e0 20 76 61 6c 75 65 0a 20 20 2a 2a 20 6f 66 20 50 value. ** of P
fd3f0 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 ager.sectorSize
fd400 69 73 20 72 65 73 74 6f 72 65 64 20 61 74 20 74 is restored at t
fd410 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74 20 72 he end of that r
fd420 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 72 outine.. */. r
fd430 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 c = read32bits(p
fd440 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c Pager->jfd, jrnl
fd450 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 72 2d Off+12, &pPager-
fd460 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 20 20 >sectorSize);.
fd470 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
fd480 72 63 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 rc;. if( (pPage
fd490 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 26 20 r->sectorSize &
fd4a0 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 (pPager->sectorS
fd4b0 69 7a 65 2d 31 29 29 21 3d 30 0a 20 20 20 20 20 ize-1))!=0.
fd4c0 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 || pPager->s
fd4d0 65 63 74 6f 72 53 69 7a 65 3e 30 78 31 30 30 30 ectorSize>0x1000
fd4e0 30 30 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 000 ){. retur
fd4f0 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 n SQLITE_DONE;.
fd500 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f }.. pPager->jo
fd510 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52 urnalOff += JOUR
fd520 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
fd530 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c r);. return SQL
fd540 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ITE_OK;.}.../*.*
fd550 2a 20 57 72 69 74 65 20 74 68 65 20 73 75 70 70 * Write the supp
fd560 6c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 lied master jour
fd570 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 nal name into th
fd580 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 e journal file f
fd590 6f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 61 67 or pager.** pPag
fd5a0 65 72 20 61 74 20 74 68 65 20 63 75 72 72 65 6e er at the curren
fd5b0 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 t location. The
fd5c0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
fd5d0 61 6d 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 ame must be the
fd5e0 6c 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72 last.** thing wr
fd5f0 69 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e itten to a journ
fd600 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 al file. If the
fd610 70 61 67 65 72 20 69 73 20 69 6e 20 66 75 6c 6c pager is in full
fd620 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a -sync mode, the.
fd630 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
fd640 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 64 descriptor is ad
fd650 76 61 6e 63 65 64 20 74 6f 20 74 68 65 20 6e 65 vanced to the ne
fd660 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 xt sector bounda
fd670 72 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 ry before.** any
fd680 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e thing is written
fd690 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 69 73 3a . The format is:
fd6a0 0a 2a 2a 0a 2a 2a 20 2b 20 34 20 62 79 74 65 73 .**.** + 4 bytes
fd6b0 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e : PAGER_MJ_PGNO.
fd6c0 0a 2a 2a 20 2b 20 4e 20 62 79 74 65 73 3a 20 6c .** + N bytes: l
fd6d0 65 6e 67 74 68 20 6f 66 20 6d 61 73 74 65 72 20 ength of master
fd6e0 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 0a 2a 2a journal name..**
fd6f0 20 2b 20 34 20 62 79 74 65 73 3a 20 4e 0a 2a 2a + 4 bytes: N.**
fd700 20 2b 20 34 20 62 79 74 65 73 3a 20 4d 61 73 74 + 4 bytes: Mast
fd710 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er journal name
fd720 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 2b 20 38 checksum..** + 8
fd730 20 62 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c bytes: aJournal
fd740 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 Magic[]..**.** T
fd750 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
fd760 6c 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 l page checksum
fd770 69 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 is the sum of th
fd780 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d e bytes in the m
fd790 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c aster.** journal
fd7a0 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 name..**.** If
fd7b0 7a 4d 61 73 74 65 72 20 69 73 20 61 20 4e 55 4c zMaster is a NUL
fd7c0 4c 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 L pointer (occur
fd7d0 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 s for a single d
fd7e0 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 atabase transact
fd7f0 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 ion), .** this c
fd800 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a all is a no-op..
fd810 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 */.static int wr
fd820 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c iteMasterJournal
fd830 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
fd840 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
fd850 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ter){. int rc;.
fd860 20 20 69 6e 74 20 6c 65 6e 3b 20 0a 20 20 69 6e int len; . in
fd870 74 20 69 3b 20 0a 20 20 69 36 34 20 6a 72 6e 6c t i; . i64 jrnl
fd880 4f 66 66 3b 0a 20 20 69 36 34 20 6a 72 6e 6c 53 Off;. i64 jrnlS
fd890 69 7a 65 3b 0a 20 20 75 33 32 20 63 6b 73 75 6d ize;. u32 cksum
fd8a0 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a 42 75 = 0;. char zBu
fd8b0 66 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 f[sizeof(aJourna
fd8c0 6c 4d 61 67 69 63 29 2b 32 2a 34 5d 3b 0a 0a 20 lMagic)+2*4];..
fd8d0 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 7c 7c if( !zMaster ||
fd8e0 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
fd8f0 65 72 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 er ) return SQLI
fd900 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 TE_OK;. if( pPa
fd910 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
fd920 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
fd930 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 20 72 65 74 ODE_MEMORY ) ret
fd940 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
fd950 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
fd960 65 72 20 3d 20 31 3b 0a 0a 20 20 6c 65 6e 20 3d er = 1;.. len =
fd970 20 73 74 72 6c 65 6e 28 7a 4d 61 73 74 65 72 29 strlen(zMaster)
fd980 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6c ;. for(i=0; i<l
fd990 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6b en; i++){. ck
fd9a0 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 69 sum += zMaster[i
fd9b0 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 ];. }.. /* If
fd9c0 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 in full-sync mod
fd9d0 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20 74 68 e, advance to th
fd9e0 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65 63 74 e next disk sect
fd9f0 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e or before writin
fda00 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73 74 65 g. ** the maste
fda10 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 r journal name.
fda20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73 65 20 This is in case
fda30 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 67 the previous pag
fda40 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 2a e written to. *
fda50 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 * the journal ha
fda60 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 s already been s
fda70 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 ynced.. */. if
fda80 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 ( pPager->fullSy
fda90 6e 63 20 29 7b 0a 20 20 20 20 73 65 65 6b 4a 6f nc ){. seekJo
fdaa0 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 urnalHdr(pPager)
fdab0 3b 0a 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66 20 ;. }. jrnlOff
fdac0 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
fdad0 6c 4f 66 66 3b 0a 20 20 70 50 61 67 65 72 2d 3e lOff;. pPager->
fdae0 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 6c journalOff += (l
fdaf0 65 6e 2b 32 30 29 3b 0a 0a 20 20 72 63 20 3d 20 en+20);.. rc =
fdb00 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
fdb10 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 er->jfd, jrnlOff
fdb20 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 , PAGER_MJ_PGNO(
fdb30 70 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20 pPager));. if(
fdb40 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
fdb50 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e return rc;. jrn
fdb60 6c 4f 66 66 20 2b 3d 20 34 3b 0a 0a 20 20 72 63 lOff += 4;.. rc
fdb70 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
fdb80 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a e(pPager->jfd, z
fdb90 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 6a 72 6e Master, len, jrn
fdba0 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 72 63 21 lOff);. if( rc!
fdbb0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
fdbc0 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c 4f 66 urn rc;. jrnlOf
fdbd0 66 20 2b 3d 20 6c 65 6e 3b 0a 0a 20 20 70 75 74 f += len;.. put
fdbe0 33 32 62 69 74 73 28 7a 42 75 66 2c 20 6c 65 6e 32bits(zBuf, len
fdbf0 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 );. put32bits(&
fdc00 7a 42 75 66 5b 34 5d 2c 20 63 6b 73 75 6d 29 3b zBuf[4], cksum);
fdc10 0a 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b . memcpy(&zBuf[
fdc20 38 5d 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 8], aJournalMagi
fdc30 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e c, sizeof(aJourn
fdc40 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 72 63 20 alMagic));. rc
fdc50 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
fdc60 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 42 (pPager->jfd, zB
fdc70 75 66 2c 20 38 2b 73 69 7a 65 6f 66 28 61 4a 6f uf, 8+sizeof(aJo
fdc80 75 72 6e 61 6c 4d 61 67 69 63 29 2c 20 6a 72 6e urnalMagic), jrn
fdc90 6c 4f 66 66 29 3b 0a 20 20 6a 72 6e 6c 4f 66 66 lOff);. jrnlOff
fdca0 20 2b 3d 20 38 2b 73 69 7a 65 6f 66 28 61 4a 6f += 8+sizeof(aJo
fdcb0 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20 20 70 urnalMagic);. p
fdcc0 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
fdcd0 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e = !pPager->noSyn
fdce0 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 c;.. /* If the
fdcf0 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 72 69 pager is in peri
fdd00 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f stent-journal mo
fdd10 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 79 de, then the phy
fdd20 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72 sical . ** jour
fdd30 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78 74 nal-file may ext
fdd40 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e 64 end past the end
fdd50 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a of the master-j
fdd60 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a ournal name. **
fdd70 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66 20 and 8 bytes of
fdd80 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 74 20 magic data just
fdd90 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 written to the f
fdda0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20 ile. This is .
fddb0 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65 63 ** dangerous bec
fddc0 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 74 6f ause the code to
fddd0 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d rollback a hot-
fdde0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a journal file. *
fddf0 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 * will not be ab
fde00 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d le to find the m
fde10 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 aster-journal na
fde20 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 me to determine
fde30 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 . ** whether or
fde40 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c not the journal
fde50 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20 is hot. . **.
fde60 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69 6e ** Easiest thin
fde70 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73 20 g to do in this
fde80 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20 74 scenario is to t
fde90 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 runcate the jour
fdea0 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74 nal . ** file t
fdeb0 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 20 73 o the required s
fdec0 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 ize.. */ . if(
fded0 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 (rc==SQLITE_OK)
fdee0 0a 20 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c . && (rc = sql
fdef0 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
fdf00 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e Pager->jfd, &jrn
fdf10 6c 53 69 7a 65 29 29 3d 3d 53 51 4c 49 54 45 5f lSize))==SQLITE_
fdf20 4f 4b 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a OK. && jrnlSiz
fdf30 65 3e 6a 72 6e 6c 4f 66 66 0a 20 20 29 7b 0a 20 e>jrnlOff. ){.
fdf40 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
fdf50 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 sTruncate(pPager
fdf60 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 29 3b ->jfd, jrnlOff);
fdf70 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
fdf80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 ;.}../*.** Find
fdf90 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 68 61 a page in the ha
fdfa0 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 69 sh table given i
fdfb0 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 ts page number.
fdfc0 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 Return.** a poi
fdfd0 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 nter to the page
fdfe0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 or NULL if not
fdff0 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 found..*/.static
fe000 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f PgHdr *pager_lo
fe010 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 okup(Pager *pPag
fe020 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
fe030 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 73 71 PgHdr *p;. sq
fe040 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
fe050 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
fe060 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a , pgno, 0, &p);.
fe070 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
fe080 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69 *.** Clear the i
fe090 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 n-memory cache.
fe0a0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a This routine.**
fe0b0 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 sets the state
fe0c0 6f 66 20 74 68 65 20 70 61 67 65 72 20 62 61 63 of the pager bac
fe0d0 6b 20 74 6f 20 77 68 61 74 20 69 74 20 77 61 73 k to what it was
fe0e0 20 77 68 65 6e 20 69 74 20 77 61 73 20 66 69 72 when it was fir
fe0f0 73 74 0a 2a 2a 20 6f 70 65 6e 65 64 2e 20 20 41 st.** opened. A
fe100 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 ny outstanding p
fe110 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 ages are invalid
fe120 61 74 65 64 20 61 6e 64 20 73 75 62 73 65 71 75 ated and subsequ
fe130 65 6e 74 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 ent attempts.**
fe140 74 6f 20 61 63 63 65 73 73 20 74 68 6f 73 65 20 to access those
fe150 70 61 67 65 73 20 77 69 6c 6c 20 6c 69 6b 65 6c pages will likel
fe160 79 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f y result in a co
fe170 72 65 64 75 6d 70 2e 0a 2a 2f 0a 73 74 61 74 69 redump..*/.stati
fe180 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65 73 c void pager_res
fe190 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 et(Pager *pPager
fe1a0 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d ){. if( pPager-
fe1b0 3e 65 72 72 43 6f 64 65 20 29 20 72 65 74 75 72 >errCode ) retur
fe1c0 6e 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 n;. sqlite3Pcac
fe1d0 68 65 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e heClear(pPager->
fe1e0 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a pPCache);.}../*.
fe1f0 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 ** Unlock the da
fe200 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a tabase file. .**
fe210 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 .** If the pager
fe220 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e is currently in
fe230 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69 error state, di
fe240 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e scard the conten
fe250 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61 ts of .** the ca
fe260 63 68 65 20 61 6e 64 20 72 65 73 65 74 20 74 68 che and reset th
fe270 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 e Pager structur
fe280 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 e internal state
fe290 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a . If there is.**
fe2a0 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c an open journal
fe2b0 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 -file, then the
fe2c0 6e 65 78 74 20 74 69 6d 65 20 61 20 73 68 61 72 next time a shar
fe2d0 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 ed-lock is obtai
fe2e0 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61 ned.** on the pa
fe2f0 67 65 72 20 66 69 6c 65 20 28 62 79 20 74 68 69 ger file (by thi
fe300 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
fe310 72 6f 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c rocess), it will
fe320 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 be.** treated a
fe330 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 s a hot-journal
fe340 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e and rolled back.
fe350 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
fe360 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 pager_unlock(Pag
fe370 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
fe380 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c f( !pPager->excl
fe390 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 usiveMode ){.
fe3a0 20 69 6e 74 20 72 63 20 3d 20 6f 73 55 6e 6c 6f int rc = osUnlo
fe3b0 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 4e ck(pPager->fd, N
fe3c0 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 O_LOCK);. if(
fe3d0 20 72 63 20 29 20 70 50 61 67 65 72 2d 3e 65 72 rc ) pPager->er
fe3e0 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 rCode = rc;.
fe3f0 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
fe400 20 2d 31 3b 0a 20 20 20 20 49 4f 54 52 41 43 45 -1;. IOTRACE
fe410 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c (("UNLOCK %p\n",
fe420 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f pPager)).. /
fe430 2a 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 * Always close t
fe440 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
fe450 77 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 when dropping th
fe460 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e e database lock.
fe470 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 . ** Otherwis
fe480 65 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 e, another conne
fe490 63 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e ction with journ
fe4a0 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d al_mode=delete m
fe4b0 69 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 ight. ** dele
fe4c0 74 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20 te the file out
fe4d0 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 from under us..
fe4e0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 */. if( pP
fe4f0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
fe500 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 n ){. sqlit
fe510 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 e3OsClose(pPager
fe520 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50 ->jfd);. pP
fe530 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
fe540 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c n = 0;. sql
fe550 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
fe560 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 y(pPager->pInJou
fe570 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 70 50 61 rnal);. pPa
fe580 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 ger->pInJournal
fe590 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 = 0;. sqlit
fe5a0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
fe5b0 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 pPager->pAlwaysR
fe5c0 6f 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20 20 20 ollback);.
fe5d0 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 pPager->pAlwaysR
fe5e0 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 20 ollback = 0;.
fe5f0 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 50 61 }.. /* If Pa
fe600 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20 73 ger.errCode is s
fe610 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 et, the contents
fe620 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63 61 of the pager ca
fe630 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a 20 20 che cannot be.
fe640 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f ** trusted. No
fe650 77 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 w that the pager
fe660 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 file is unlocke
fe670 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 d, the contents
fe680 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 of the. ** ca
fe690 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 63 61 che can be disca
fe6a0 72 64 65 64 20 61 6e 64 20 74 68 65 20 65 72 72 rded and the err
fe6b0 6f 72 20 63 6f 64 65 20 73 61 66 65 6c 79 20 63 or code safely c
fe6c0 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 leared.. */.
fe6d0 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 if( pPager->e
fe6e0 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 rrCode ){.
fe6f0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
fe700 4b 20 29 20 70 50 61 67 65 72 2d 3e 65 72 72 43 K ) pPager->errC
fe710 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b ode = SQLITE_OK;
fe720 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 . pager_res
fe730 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 et(pPager);.
fe740 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
fe750 6d 74 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 mtOpen ){.
fe760 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
fe770 28 70 50 61 67 65 72 2d 3e 73 74 66 64 29 3b 0a (pPager->stfd);.
fe780 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
fe790 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
fe7a0 67 65 72 2d 3e 70 49 6e 53 74 6d 74 29 3b 0a 20 ger->pInStmt);.
fe7b0 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 pPager->p
fe7c0 49 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 InStmt = 0;.
fe7d0 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 }. pPager
fe7e0 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 0a ->stmtOpen = 0;.
fe7f0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 pPager->st
fe800 6d 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20 20 20 mtInUse = 0;.
fe810 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
fe820 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 alOff = 0;.
fe830 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
fe840 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
fe850 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 pPager->stmtA
fe860 75 74 6f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 utoopen = 0;.
fe870 20 20 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 pPager->origD
fe880 62 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d bSize = 0;. }
fe890 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 .. pPager->st
fe8a0 61 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f ate = PAGER_UNLO
fe8b0 43 4b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e CK;. pPager->
fe8c0 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 changeCountDone
fe8d0 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a = 0;. }.}../*.*
fe8e0 2a 20 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c * Execute a roll
fe8f0 62 61 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 back if a transa
fe900 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 ction is active
fe910 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a and unlock the .
fe920 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
fe930 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 68 . If the pager h
fe940 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 as already enter
fe950 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 ed the error sta
fe960 74 65 2c 20 0a 2a 2a 20 64 6f 20 6e 6f 74 20 61 te, .** do not a
fe970 74 74 65 6d 70 74 20 74 68 65 20 72 6f 6c 6c 62 ttempt the rollb
fe980 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ack..*/.static v
fe990 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 oid pagerUnlockA
fe9a0 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 ndRollback(Pager
fe9b0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 65 *p){. if( p->e
fe9c0 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f rrCode==SQLITE_O
fe9d0 4b 20 26 26 20 70 2d 3e 73 74 61 74 65 3e 3d 50 K && p->state>=P
fe9e0 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b AGER_RESERVED ){
fe9f0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 . sqlite3Begi
fea00 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
fea10 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
fea20 72 52 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 rRollback(p);.
fea30 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
fea40 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a gnMalloc();. }.
fea50 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 pager_unlock(p
fea60 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 );.}../*.** This
fea70 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 routine ends a
fea80 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 41 20 transaction. A
fea90 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 transaction is e
feaa0 6e 64 65 64 20 62 79 20 65 69 74 68 65 72 0a 2a nded by either.*
feab0 2a 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20 * a COMMIT or a
feac0 52 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 20 ROLLBACK..**.**
fead0 57 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e When this routin
feae0 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 e is called, the
feaf0 20 70 61 67 65 72 20 68 61 73 20 74 68 65 20 6a pager has the j
feb00 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 70 65 6e ournal file open
feb10 20 61 6e 64 0a 2a 2a 20 61 20 52 45 53 45 52 56 and.** a RESERV
feb20 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 ED or EXCLUSIVE
feb30 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
feb40 62 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 base. This rout
feb50 69 6e 65 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 ine will release
feb60 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
feb70 20 6c 6f 63 6b 20 61 6e 64 20 61 63 71 75 69 72 lock and acquir
feb80 65 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b es a SHARED lock
feb90 20 69 6e 20 69 74 73 20 70 6c 61 63 65 20 69 66 in its place if
feba0 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 68 65 20 that is.** the
febb0 61 70 70 72 6f 70 72 69 61 74 65 20 74 68 69 6e appropriate thin
febc0 67 20 74 6f 20 64 6f 2e 20 20 52 65 6c 65 61 73 g to do. Releas
febd0 65 20 6c 6f 63 6b 73 20 75 73 75 61 6c 6c 79 20 e locks usually
febe0 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 2c 0a is appropriate,.
febf0 2a 2a 20 75 6e 6c 65 73 73 20 77 65 20 61 72 65 ** unless we are
fec00 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 61 63 in exclusive ac
fec10 63 65 73 73 20 6d 6f 64 65 20 6f 72 20 75 6e 6c cess mode or unl
fec20 65 73 73 20 74 68 69 73 20 69 73 20 61 20 0a 2a ess this is a .*
fec30 2a 20 43 4f 4d 4d 49 54 20 41 4e 44 20 42 45 47 * COMMIT AND BEG
fec40 49 4e 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 41 IN or ROLLBACK A
fec50 4e 44 20 42 45 47 49 4e 20 6f 70 65 72 61 74 69 ND BEGIN operati
fec60 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f on..**.** The jo
fec70 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 65 69 urnal file is ei
fec80 74 68 65 72 20 64 65 6c 65 74 65 64 20 6f 72 20 ther deleted or
fec90 74 72 75 6e 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a truncated..**.**
feca0 20 54 4f 44 4f 3a 20 43 6f 6e 73 69 64 65 72 20 TODO: Consider
fecb0 6b 65 65 70 69 6e 67 20 74 68 65 20 6a 6f 75 72 keeping the jour
fecc0 6e 61 6c 20 66 69 6c 65 20 6f 70 65 6e 20 66 6f nal file open fo
fecd0 72 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 r temporary data
fece0 62 61 73 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6d bases..** This m
fecf0 69 67 68 74 20 67 69 76 65 20 61 20 70 65 72 66 ight give a perf
fed00 6f 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d ormance improvem
fed10 65 6e 74 20 6f 6e 20 77 69 6e 64 6f 77 73 20 77 ent on windows w
fed20 68 65 72 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 here opening.**
fed30 61 20 66 69 6c 65 20 69 73 20 61 6e 20 65 78 70 a file is an exp
fed40 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e ensive operation
fed50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
fed60 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
fed70 63 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 ction(Pager *pPa
fed80 67 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74 ger, int hasMast
fed90 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
feda0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
fedb0 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc2 = SQLITE_OK
fedc0 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
fedd0 73 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 state<PAGER_RESE
fede0 52 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 RVED ){. retu
fedf0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
fee00 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 }. sqlite3Pager
fee10 53 74 6d 74 43 6f 6d 6d 69 74 28 70 50 61 67 65 StmtCommit(pPage
fee20 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 r);. if( pPager
fee30 2d 3e 73 74 6d 74 4f 70 65 6e 20 26 26 20 21 70 ->stmtOpen && !p
fee40 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
fee50 4d 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 Mode ){. sqli
fee60 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 te3OsClose(pPage
fee70 72 2d 3e 73 74 66 64 29 3b 0a 20 20 20 20 70 50 r->stfd);. pP
fee80 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d ager->stmtOpen =
fee90 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 0;. }. if( pP
feea0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
feeb0 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 n ){. if( pPa
feec0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
feed0 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
feee0 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 ODE_MEMORY ){.
feef0 20 20 20 20 69 6e 74 20 69 73 4d 65 6d 6f 72 79 int isMemory
fef00 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 Journal = sqlite
fef10 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 3IsMemJournal(pP
fef20 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 ager->jfd);.
fef30 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
fef40 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
fef50 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
fef60 72 6e 61 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 rnalOpen = 0;.
fef70 20 20 20 20 69 66 28 20 21 69 73 4d 65 6d 6f 72 if( !isMemor
fef80 79 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 yJournal ){.
fef90 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
fefa0 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d OsDelete(pPager-
fefb0 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a >pVfs, pPager->z
fefc0 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 Journal, 0);.
fefd0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 }. }else i
fefe0 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
feff0 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
ff000 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
ff010 54 45 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 TE. && (
ff020 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 rc = sqlite3OsTr
ff030 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a uncate(pPager->j
ff040 66 64 2c 20 30 29 29 3d 3d 53 51 4c 49 54 45 5f fd, 0))==SQLITE_
ff050 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 OK ){. pPag
ff060 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
ff070 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
ff080 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
ff090 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
ff0a0 69 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c if( pPager->excl
ff0b0 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 usiveMode .
ff0c0 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e || pPager->journ
ff0d0 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
ff0e0 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 URNALMODE_PERSIS
ff0f0 54 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 T. ){. r
ff100 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 c = zeroJournalH
ff110 64 72 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 dr(pPager, hasMa
ff120 73 74 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67 ster);. pag
ff130 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
ff140 20 72 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67 rc);. pPag
ff150 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
ff160 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
ff170 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
ff180 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
ff190 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
ff1a0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
ff1b0 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
ff1c0 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 7c 7c 20 LMODE_DELETE ||
ff1d0 72 63 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 rc );. sqli
ff1e0 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 te3OsClose(pPage
ff1f0 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 r->jfd);. p
ff200 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 Pager->journalOp
ff210 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 en = 0;. if
ff220 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
ff230 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 && !pPager->temp
ff240 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 File ){.
ff250 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 rc = sqlite3OsDe
ff260 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 lete(pPager->pVf
ff270 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
ff280 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d nal, 0);. }
ff290 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
ff2a0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
ff2b0 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
ff2c0 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d al);. pPager-
ff2d0 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b >pInJournal = 0;
ff2e0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 . sqlite3Bitv
ff2f0 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 ecDestroy(pPager
ff300 2d 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 ->pAlwaysRollbac
ff310 6b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e k);. pPager->
ff320 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 pAlwaysRollback
ff330 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 0;. sqlite3
ff340 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 PcacheCleanAll(p
ff350 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b Pager->pPCache);
ff360 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
ff370 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 73 HECK_PAGES. s
ff380 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 qlite3PcacheIter
ff390 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ate(pPager->pPCa
ff3a0 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74 5f 70 che, pager_set_p
ff3b0 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64 69 66 agehash);.#endif
ff3c0 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
ff3d0 68 65 43 6c 65 61 72 46 6c 61 67 73 28 70 50 61 heClearFlags(pPa
ff3e0 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 0a 20 20 ger->pPCache,.
ff3f0 20 20 20 20 20 50 47 48 44 52 5f 49 4e 5f 4a 4f PGHDR_IN_JO
ff400 55 52 4e 41 4c 20 7c 20 50 47 48 44 52 5f 4e 45 URNAL | PGHDR_NE
ff410 45 44 5f 53 59 4e 43 0a 20 20 20 20 29 3b 0a 20 ED_SYNC. );.
ff420 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 pPager->dirty
ff430 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 20 20 70 Cache = 0;. p
ff440 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b Pager->nRec = 0;
ff450 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
ff460 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 sert( pPager->pI
ff470 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 nJournal==0 );.
ff480 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 }.. if( !pPage
ff490 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
ff4a0 20 29 7b 0a 20 20 20 20 72 63 32 20 3d 20 6f 73 ){. rc2 = os
ff4b0 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 Unlock(pPager->f
ff4c0 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b d, SHARED_LOCK);
ff4d0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 . pPager->sta
ff4e0 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 te = PAGER_SHARE
ff4f0 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 D;. }else if( p
ff500 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 Pager->state==PA
ff510 47 45 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20 20 GER_SYNCED ){.
ff520 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
ff530 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 = PAGER_EXCLUSIV
ff540 45 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d E;. }. pPager-
ff550 3e 6f 72 69 67 44 62 53 69 7a 65 20 3d 20 30 3b >origDbSize = 0;
ff560 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 . pPager->setMa
ff570 73 74 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 ster = 0;. pPag
ff580 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 er->needSync = 0
ff590 3b 0a 20 20 2f 2a 20 6c 72 75 4c 69 73 74 53 65 ;. /* lruListSe
ff5a0 74 46 69 72 73 74 53 79 6e 63 65 64 28 70 50 61 tFirstSynced(pPa
ff5b0 67 65 72 29 3b 20 2a 2f 0a 20 20 69 66 28 20 21 ger); */. if( !
ff5c0 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61 MEMDB ){. pPa
ff5d0 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 2d 31 ger->dbSize = -1
ff5e0 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e ;. }. pPager->
ff5f0 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 3b 0a dbModified = 0;.
ff600 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 . return (rc==S
ff610 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 QLITE_OK?rc2:rc)
ff620 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 ;.}../*.** Compu
ff630 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 te and return a
ff640 63 68 65 63 6b 73 75 6d 20 66 6f 72 20 74 68 65 checksum for the
ff650 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 0a 2a page of data..*
ff660 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 *.** This is not
ff670 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 6d a real checksum
ff680 2e 20 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20 . It is really
ff690 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20 just the sum of
ff6a0 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 the .** random i
ff6b0 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 61 6e 64 nitial value and
ff6c0 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
ff6d0 2e 20 20 57 65 20 65 78 70 65 72 69 6d 65 6e 74 . We experiment
ff6e0 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 63 68 65 ed with.** a che
ff6f0 63 6b 73 75 6d 20 6f 66 20 74 68 65 20 65 6e 74 cksum of the ent
ff700 69 72 65 20 64 61 74 61 2c 20 62 75 74 20 74 68 ire data, but th
ff710 61 74 20 77 61 73 20 66 6f 75 6e 64 20 74 6f 20 at was found to
ff720 62 65 20 74 6f 6f 20 73 6c 6f 77 2e 0a 2a 2a 0a be too slow..**.
ff730 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
ff740 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
ff750 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 62 65 stored at the be
ff760 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61 74 61 20 ginning of data
ff770 61 6e 64 0a 2a 2a 20 74 68 65 20 63 68 65 63 6b and.** the check
ff780 73 75 6d 20 69 73 20 73 74 6f 72 65 64 20 61 74 sum is stored at
ff790 20 74 68 65 20 65 6e 64 2e 20 20 54 68 69 73 20 the end. This
ff7a0 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 49 is important. I
ff7b0 66 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 63 6f 72 f journal.** cor
ff7c0 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20 64 ruption occurs d
ff7d0 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66 61 ue to a power fa
ff7e0 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74 20 ilure, the most
ff7f0 6c 69 6b 65 6c 79 20 73 63 65 6e 61 72 69 6f 0a likely scenario.
ff800 2a 2a 20 69 73 20 74 68 61 74 20 6f 6e 65 20 65 ** is that one e
ff810 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 nd or the other
ff820 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 77 69 of the record wi
ff830 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e 20 20 ll be changed.
ff840 49 74 20 69 73 0a 2a 2a 20 6d 75 63 68 20 6c 65 It is.** much le
ff850 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74 20 74 ss likely that t
ff860 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66 20 74 he two ends of t
ff870 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 he journal recor
ff880 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63 6f 72 d will be.** cor
ff890 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d 69 64 rect and the mid
ff8a0 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74 2e 20 dle be corrupt.
ff8b0 20 54 68 75 73 2c 20 74 68 69 73 20 22 63 68 65 Thus, this "che
ff8c0 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c 0a 2a cksum" scheme,.*
ff8d0 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20 61 6e * though fast an
ff8e0 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63 68 65 d simple, catche
ff8f0 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c 69 6b s the mostly lik
ff900 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f 72 72 ely kind of corr
ff910 75 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 49 uption..**.** FI
ff920 58 20 4d 45 3a 20 20 43 6f 6e 73 69 64 65 72 20 X ME: Consider
ff930 61 64 64 69 6e 67 20 65 76 65 72 79 20 32 30 30 adding every 200
ff940 74 68 20 28 6f 72 20 73 6f 29 20 62 79 74 65 20 th (or so) byte
ff950 6f 66 20 74 68 65 20 64 61 74 61 20 74 6f 20 74 of the data to t
ff960 68 65 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 2e 20 he.** checksum.
ff970 20 54 68 61 74 20 77 61 79 20 69 66 20 61 20 73 That way if a s
ff980 69 6e 67 6c 65 20 70 61 67 65 20 73 70 61 6e 73 ingle page spans
ff990 20 33 20 6f 72 20 6d 6f 72 65 20 64 69 73 6b 20 3 or more disk
ff9a0 73 65 63 74 6f 72 73 20 61 6e 64 0a 2a 2a 20 6f sectors and.** o
ff9b0 6e 6c 79 20 74 68 65 20 6d 69 64 64 6c 65 20 73 nly the middle s
ff9c0 65 63 74 6f 72 20 69 73 20 63 6f 72 72 75 70 74 ector is corrupt
ff9d0 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 6c 6c 20 , we will still
ff9e0 68 61 76 65 20 61 20 72 65 61 73 6f 6e 61 62 6c have a reasonabl
ff9f0 65 0a 2a 2a 20 63 68 61 6e 63 65 20 6f 66 20 66 e.** chance of f
ffa00 61 69 6c 69 6e 67 20 74 68 65 20 63 68 65 63 6b ailing the check
ffa10 73 75 6d 20 61 6e 64 20 74 68 75 73 20 64 65 74 sum and thus det
ffa20 65 63 74 69 6e 67 20 74 68 65 20 70 72 6f 62 6c ecting the probl
ffa30 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 em..*/.static u3
ffa40 32 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61 2 pager_cksum(Pa
ffa50 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e ger *pPager, con
ffa60 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 st u8 *aData){.
ffa70 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61 u32 cksum = pPa
ffa80 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 0a ger->cksumInit;.
ffa90 20 20 69 6e 74 20 69 20 3d 20 70 50 61 67 65 72 int i = pPager
ffaa0 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 30 3b 0a ->pageSize-200;.
ffab0 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a while( i>0 ){.
ffac0 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 cksum += aDa
ffad0 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 ta[i];. i -=
ffae0 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 200;. }. retur
ffaf0 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 20 46 n cksum;.}../* F
ffb00 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
ffb10 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 on */.static voi
ffb20 64 20 6d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 d makeClean(PgHd
ffb30 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 r*);../*.** Read
ffb40 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 66 a single page f
ffb50 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 rom the journal
ffb60 66 69 6c 65 20 6f 70 65 6e 65 64 20 6f 6e 20 66 file opened on f
ffb70 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a ile descriptor.*
ffb80 2a 20 6a 66 64 2e 20 20 50 6c 61 79 62 61 63 6b * jfd. Playback
ffb90 20 74 68 69 73 20 6f 6e 65 20 70 61 67 65 2e 0a this one page..
ffba0 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e **.** The isMain
ffbb0 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75 Jrnl flag is tru
ffbc0 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 e if this is the
ffbd0 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a main rollback j
ffbe0 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 ournal and.** fa
ffbf0 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74 lse for the stat
ffc00 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 ement journal.
ffc10 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 The main rollbac
ffc20 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a k journal uses.*
ffc30 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68 * checksums - th
ffc40 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
ffc50 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f nal does not..*/
ffc60 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
ffc70 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
ffc80 61 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 age(. Pager *pP
ffc90 61 67 65 72 2c 20 20 20 20 20 20 20 2f 2a 20 54 ager, /* T
ffca0 68 65 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 he pager being p
ffcb0 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 layed back */.
ffcc0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 sqlite3_file *jf
ffcd0 64 2c 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 d, /* The file
ffce0 20 74 68 61 74 20 69 73 20 74 68 65 20 6a 6f 75 that is the jou
ffcf0 72 6e 61 6c 20 62 65 69 6e 67 20 72 6f 6c 6c 65 rnal being rolle
ffd00 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 36 34 20 d back */. i64
ffd10 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 offset,
ffd20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 74 68 /* Offset of th
ffd30 65 20 70 61 67 65 20 77 69 74 68 69 6e 20 74 68 e page within th
ffd40 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 e journal */. i
ffd50 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 20 20 nt isMainJrnl
ffd60 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 /* True for
ffd70 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f main rollback jo
ffd80 75 72 6e 61 6c 2e 20 46 61 6c 73 65 20 66 6f 72 urnal. False for
ffd90 20 53 74 6d 74 20 6a 72 6e 6c 20 2a 2f 0a 29 7b Stmt jrnl */.){
ffda0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 . int rc;. PgH
ffdb0 64 72 20 2a 70 50 67 3b 20 20 20 20 20 20 20 20 dr *pPg;
ffdc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e /* An
ffdd0 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 69 existing page i
ffde0 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 n the cache */.
ffdf0 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 Pgno pgno;
ffe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ffe10 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 * The page numbe
ffe20 72 20 6f 66 20 61 20 70 61 67 65 20 69 6e 20 6a r of a page in j
ffe30 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 ournal */. u32
ffe40 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 cksum;
ffe50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 /* Che
ffe60 63 6b 73 75 6d 20 75 73 65 64 20 66 6f 72 20 73 cksum used for s
ffe70 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
ffe80 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 20 3d 20 /. u8 *aData =
ffe90 28 75 38 20 2a 29 70 50 61 67 65 72 2d 3e 70 54 (u8 *)pPager->pT
ffea0 6d 70 53 70 61 63 65 3b 20 20 20 2f 2a 20 54 65 mpSpace; /* Te
ffeb0 6d 70 20 73 74 6f 72 61 67 65 20 66 6f 72 20 61 mp storage for a
ffec0 20 70 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 69 page */.. /* i
ffed0 73 4d 61 69 6e 4a 72 6e 6c 20 73 68 6f 75 6c 64 sMainJrnl should
ffee0 20 62 65 20 74 72 75 65 20 66 6f 72 20 74 68 65 be true for the
ffef0 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 61 6e main journal an
fff00 64 20 66 61 6c 73 65 20 66 6f 72 0a 20 20 2a 2a d false for. **
fff10 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
fff20 61 6c 73 2e 20 20 56 65 72 69 66 79 20 74 68 61 als. Verify tha
fff30 74 20 74 68 69 73 20 69 73 20 61 6c 77 61 79 73 t this is always
fff40 20 74 68 65 20 63 61 73 65 0a 20 20 2a 2f 0a 20 the case. */.
fff50 20 61 73 73 65 72 74 28 20 6a 66 64 20 3d 3d 20 assert( jfd ==
fff60 28 69 73 4d 61 69 6e 4a 72 6e 6c 20 3f 20 70 50 (isMainJrnl ? pP
fff70 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70 50 61 67 ager->jfd : pPag
fff80 65 72 2d 3e 73 74 66 64 29 20 29 3b 0a 20 20 61 er->stfd) );. a
fff90 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b 0a ssert( aData );.
fffa0 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 . rc = read32bi
fffb0 74 73 28 6a 66 64 2c 20 6f 66 66 73 65 74 2c 20 ts(jfd, offset,
fffc0 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 &pgno);. if( rc
fffd0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
fffe0 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 turn rc;. rc =
ffff0 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 6a 66 sqlite3OsRead(jf
10000 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 d, aData, pPager
10001 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 ->pageSize, offs
10002 65 74 2b 34 29 3b 0a 20 20 69 66 28 20 72 63 21 et+4);. if( rc!
10003 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
10004 75 72 6e 20 72 63 3b 0a 20 20 70 50 61 67 65 72 urn rc;. pPager
10005 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 ->journalOff +=
10006 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
10007 20 2b 20 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 + 4;.. /* Sani
10008 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 ty checking on t
10009 68 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 69 he page. This i
1000a 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 s more important
1000b 20 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61 6c that I original
1000c 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e ly. ** thought.
1000d 20 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 If a power fai
1000e 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c lure occurs whil
1000f 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 e the journal is
10010 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a being written,.
10011 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 ** it could ca
10012 75 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74 61 use invalid data
10013 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 to be written i
10014 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e nto the journal.
10015 20 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a We need to. *
10016 2a 20 64 65 74 65 63 74 20 74 68 69 73 20 69 6e * detect this in
10017 76 61 6c 69 64 20 64 61 74 61 20 28 77 69 74 68 valid data (with
10018 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 high probabilit
10019 79 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 y) and ignore it
1001a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e .. */. if( pgn
1001b 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 o==0 || pgno==PA
1001c 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
1001d 65 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 er) ){. retur
1001e 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 n SQLITE_DONE;.
1001f 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 75 }. if( pgno>(u
10020 6e 73 69 67 6e 65 64 29 70 50 61 67 65 72 2d 3e nsigned)pPager->
10021 64 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 dbSize ){. re
10022 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
10023 20 20 7d 0a 20 20 69 66 28 20 69 73 4d 61 69 6e }. if( isMain
10024 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d Jrnl ){. rc =
10025 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 64 2c read32bits(jfd,
10026 20 6f 66 66 73 65 74 2b 70 50 61 67 65 72 2d 3e offset+pPager->
10027 70 61 67 65 53 69 7a 65 2b 34 2c 20 26 63 6b 73 pageSize+4, &cks
10028 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 um);. if( rc
10029 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
1002a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1002b 4f 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 69 66 Off += 4;. if
1002c 28 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 ( pager_cksum(pP
1002d 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63 6b ager, aData)!=ck
1002e 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 sum ){. ret
1002f 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b urn SQLITE_DONE;
10030 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 . }. }.. as
10031 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
10032 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 ate==PAGER_RESER
10033 56 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 VED || pPager->s
10034 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c tate>=PAGER_EXCL
10035 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 USIVE );.. /* I
10036 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 f the pager is i
10037 6e 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65 n RESERVED state
10038 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 , then there mus
10039 74 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 t be a copy of t
1003a 68 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e his. ** page in
1003b 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 the pager cache
1003c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a . In this case j
1003d 75 73 74 20 75 70 64 61 74 65 20 74 68 65 20 70 ust update the p
1003e 61 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a ager cache,. **
1003f 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 not the databas
10040 65 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65 e file. The page
10041 20 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 is left marked
10042 64 69 72 74 79 20 69 6e 20 74 68 69 73 20 63 61 dirty in this ca
10043 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e se.. **. ** An
10044 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 exception to th
10045 65 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 e above rule: If
10046 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
10047 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 in no-sync mode
10048 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 . ** and a page
10049 20 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 is moved during
1004a 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 an incremental
1004b 76 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65 20 vacuum then the
1004c 70 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f page may. ** no
1004d 74 20 62 65 20 69 6e 20 74 68 65 20 70 61 67 65 t be in the page
1004e 72 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 r cache. Later:
1004f 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 if a malloc() or
10050 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
10051 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d . ** during a M
10052 6f 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 ovepage() call,
10053 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 then the page ma
10054 79 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20 y not be in the
10055 63 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 cache. ** eithe
10056 72 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 r. So the condit
10057 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69 6e ion described in
10058 20 74 68 65 20 61 62 6f 76 65 20 70 61 72 61 67 the above parag
10059 72 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a raph is not. **
1005a 20 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 assert()able..
1005b 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 **. ** If in E
1005c 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 XCLUSIVE state,
1005d 74 68 65 6e 20 77 65 20 75 70 64 61 74 65 20 74 then we update t
1005e 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 69 he pager cache i
1005f 66 20 69 74 20 65 78 69 73 74 73 0a 20 20 2a 2a f it exists. **
10060 20 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 66 69 and the main fi
10061 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 le. The page is
10062 74 68 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 then marked not
10063 64 69 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a dirty.. **. **
10064 20 54 69 63 6b 65 74 20 23 31 31 37 31 3a 20 20 Ticket #1171:
10065 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f The statement jo
10066 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 urnal might cont
10067 61 69 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 ain page content
10068 20 74 68 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 that is. ** di
10069 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 fferent from the
1006a 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 page content at
1006b 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
1006c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 e transaction..
1006d 20 2a 2a 20 54 68 69 73 20 6f 63 63 75 72 73 20 ** This occurs
1006e 77 68 65 6e 20 61 20 70 61 67 65 20 69 73 20 63 when a page is c
1006f 68 61 6e 67 65 64 20 70 72 69 6f 72 20 74 6f 20 hanged prior to
10070 74 68 65 20 73 74 61 72 74 20 6f 66 20 61 20 73 the start of a s
10071 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 tatement. ** th
10072 65 6e 20 63 68 61 6e 67 65 64 20 61 67 61 69 6e en changed again
10073 20 77 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 within the stat
10074 65 6d 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c ement. When rol
10075 6c 69 6e 67 20 62 61 63 6b 20 73 75 63 68 20 61 ling back such a
10076 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 . ** statement
10077 77 65 20 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 we must not writ
10078 65 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 e to the origina
10079 6c 20 64 61 74 61 62 61 73 65 20 75 6e 6c 65 73 l database unles
1007a 73 20 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 s we know. ** f
1007b 6f 72 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 or certain that
1007c 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f original page co
1007d 6e 74 65 6e 74 73 20 61 72 65 20 73 79 6e 63 65 ntents are synce
1007e 64 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 d into the main
1007f 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f rollback. ** jo
10080 75 72 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 urnal. Otherwis
10081 65 2c 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 e, a power loss
10082 6d 69 67 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 might leave modi
10083 66 69 65 64 20 64 61 74 61 20 69 6e 20 74 68 65 fied data in the
10084 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 . ** database f
10085 69 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 20 65 ile without an e
10086 6e 74 72 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c ntry in the roll
10087 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 back journal tha
10088 74 20 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f t can. ** resto
10089 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 re the database
1008a 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 to its original
1008b 66 6f 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 form. Two condi
1008c 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 tions must be.
1008d 2a 2a 20 6d 65 74 20 62 65 66 6f 72 65 20 77 72 ** met before wr
1008e 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 iting to the dat
1008f 61 62 61 73 65 20 66 69 6c 65 73 2e 20 28 31 29 abase files. (1)
10090 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 the database mu
10091 73 74 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 st be. ** locke
10092 64 2e 20 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 d. (2) we know
10093 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 that the origina
10094 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 l page content i
10095 73 20 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 s fully synced.
10096 20 2a 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 ** in the main
10097 6a 6f 75 72 6e 61 6c 20 65 69 74 68 65 72 20 62 journal either b
10098 65 63 61 75 73 65 20 74 68 65 20 70 61 67 65 20 ecause the page
10099 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 is not in cache
1009a 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 or else. ** the
1009b 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 page is marked
1009c 61 73 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a as needSync==0..
1009d 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 **. ** 2008-0
1009e 34 2d 31 34 3a 20 20 57 68 65 6e 20 61 74 74 65 4-14: When atte
1009f 6d 70 74 69 6e 67 20 74 6f 20 76 61 63 75 75 6d mpting to vacuum
100a0 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 a corrupt datab
100a1 61 73 65 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a ase file, it. *
100a2 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f * is possible to
100a3 20 66 61 69 6c 20 61 20 73 74 61 74 65 6d 65 6e fail a statemen
100a4 74 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 t on a database
100a5 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 that does not ye
100a6 74 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f t exist.. ** Do
100a7 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 not attempt to
100a8 77 72 69 74 65 20 69 66 20 64 61 74 61 62 61 73 write if databas
100a9 65 20 66 69 6c 65 20 68 61 73 20 6e 65 76 65 72 e file has never
100aa 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 been opened..
100ab 2a 2f 0a 20 20 70 50 67 20 3d 20 70 61 67 65 72 */. pPg = pager
100ac 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 _lookup(pPager,
100ad 70 67 6e 6f 29 3b 0a 20 20 50 41 47 45 52 54 52 pgno);. PAGERTR
100ae 41 43 45 34 28 22 50 4c 41 59 42 41 43 4b 20 25 ACE4("PLAYBACK %
100af 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 d page %d hash(%
100b0 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 08x)\n",.
100b1 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 PAGERID(
100b2 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 pPager), pgno, p
100b3 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 ager_datahash(pP
100b4 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
100b5 61 44 61 74 61 29 29 3b 0a 20 20 69 66 28 20 28 aData));. if( (
100b6 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
100b7 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 29 0a AGER_EXCLUSIVE).
100b8 20 20 20 26 26 20 28 70 50 67 3d 3d 30 20 7c 7c && (pPg==0 ||
100b9 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 61 67 73 26 0==(pPg->flags&
100ba 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
100bb 29 0a 20 20 20 26 26 20 28 70 50 61 67 65 72 2d ). && (pPager-
100bc 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 20 >fd->pMethods).
100bd 20 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 73 74 ){. i64 ofst
100be 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 = (pgno-1)*(i64
100bf 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a )pPager->pageSiz
100c0 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 e;. rc = sqli
100c1 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
100c2 72 2d 3e 66 64 2c 20 61 44 61 74 61 2c 20 70 50 r->fd, aData, pP
100c3 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
100c4 6f 66 73 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 ofst);. }. if(
100c5 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e pPg ){. /* N
100c6 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76 o page should ev
100c7 65 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79 er be explicitly
100c8 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 rolled back tha
100c9 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63 t is in use, exc
100ca 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 ept. ** for p
100cb 61 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68 age 1 which is h
100cc 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 eld in use in or
100cd 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 der to keep the
100ce 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 lock on the.
100cf 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69 ** database acti
100d0 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68 ve. However such
100d1 20 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72 a page may be r
100d2 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20 olled back as a
100d3 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 result. ** of
100d4 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 an internal err
100d5 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 or resulting in
100d6 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c an automatic cal
100d7 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 l to. ** sqli
100d8 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
100d9 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 ().. */. v
100da 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 oid *pData;.
100db 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 pData = pPg->pDa
100dc 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 ta;. memcpy(p
100dd 44 61 74 61 2c 20 61 44 61 74 61 2c 20 70 50 61 Data, aData, pPa
100de 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
100df 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
100e0 78 52 65 69 6e 69 74 65 72 20 29 7b 0a 20 20 20 xReiniter ){.
100e1 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e pPager->xRein
100e2 69 74 65 72 28 70 50 67 29 3b 0a 20 20 20 20 7d iter(pPg);. }
100e3 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a . if( isMainJ
100e4 72 6e 6c 20 29 20 6d 61 6b 65 43 6c 65 61 6e 28 rnl ) makeClean(
100e5 70 50 67 29 3b 0a 23 69 66 64 65 66 20 53 51 4c pPg);.#ifdef SQL
100e6 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
100e7 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 pPg->pageHas
100e8 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 h = pager_pageha
100e9 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a sh(pPg);.#endif.
100ea 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 /* If this w
100eb 61 73 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 as page 1, then
100ec 72 65 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 restore the valu
100ed 65 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c e of Pager.dbFil
100ee 65 56 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f eVers.. ** Do
100ef 20 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 this before any
100f0 20 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 decoding. */.
100f1 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b if( pgno==1 ){
100f2 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 . memcpy(&p
100f3 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
100f4 73 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 s, &((u8*)pData)
100f5 5b 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 [24],sizeof(pPag
100f6 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 er->dbFileVers))
100f7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
100f8 44 65 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 Decode the page
100f9 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 just read from d
100fa 69 73 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 isk */. CODEC
100fb 31 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 1(pPager, pData,
100fc 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a pPg->pgno, 3);.
100fd 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
100fe 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 eRelease(pPg);.
100ff 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
10100 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 }../*.** Paramet
10101 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74 68 er zMaster is th
10102 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 e name of a mast
10103 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
10104 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 A single journa
10105 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72 l.** file that r
10106 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d eferred to the m
10107 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
10108 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e le has just been
10109 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a rolled back..**
1010a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
1010b 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f ecks if it is po
1010c 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 ssible to delete
1010d 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1010e 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 nal file,.** and
1010f 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69 does so if it i
10110 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e s..**.** Argumen
10111 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 6f t zMaster may po
10112 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54 6d int to Pager.pTm
10113 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74 20 pSpace. So that
10114 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a buffer is not .*
10115 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 * available for
10116 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 use within this
10117 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 0a function..**.**.
10118 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f ** The master jo
10119 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 urnal file conta
1011a 69 6e 73 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 ins the names of
1011b 20 61 6c 6c 20 63 68 69 6c 64 20 6a 6f 75 72 6e all child journ
1011c 61 6c 73 2e 0a 2a 2a 20 54 6f 20 74 65 6c 6c 20 als..** To tell
1011d 69 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 if a master jour
1011e 6e 61 6c 20 63 61 6e 20 62 65 20 64 65 6c 65 74 nal can be delet
1011f 65 64 2c 20 63 68 65 63 6b 20 74 6f 20 65 61 63 ed, check to eac
10120 68 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69 6c h of the.** chil
10121 64 72 65 6e 2e 20 20 49 66 20 61 6c 6c 20 63 68 dren. If all ch
10122 69 6c 64 72 65 6e 20 61 72 65 20 65 69 74 68 65 ildren are eithe
10123 72 20 6d 69 73 73 69 6e 67 20 6f 72 20 64 6f 20 r missing or do
10124 6e 6f 74 20 72 65 66 65 72 20 74 6f 0a 2a 2a 20 not refer to.**
10125 61 20 64 69 66 66 65 72 65 6e 74 20 6d 61 73 74 a different mast
10126 65 72 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e er journal, then
10127 20 74 68 69 73 20 6d 61 73 74 65 72 20 6a 6f 75 this master jou
10128 72 6e 61 6c 20 63 61 6e 20 62 65 20 64 65 6c 65 rnal can be dele
10129 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ted..*/.static i
1012a 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 nt pager_delmast
1012b 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 er(Pager *pPager
1012c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d , const char *zM
1012d 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 aster){. sqlite
1012e 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 3_vfs *pVfs = pP
1012f 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e ager->pVfs;. in
10130 74 20 72 63 3b 0a 20 20 69 6e 74 20 6d 61 73 74 t rc;. int mast
10131 65 72 5f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 73 er_open = 0;. s
10132 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 qlite3_file *pMa
10133 73 74 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f ster;. sqlite3_
10134 66 69 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b 0a file *pJournal;.
10135 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a char *zMasterJ
10136 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43 ournal = 0; /* C
10137 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 73 74 65 ontents of maste
10138 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a r journal file *
10139 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 65 72 4a /. i64 nMasterJ
1013a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a ournal; /*
1013b 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 65 72 20 Size of master
1013c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
1013d 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6d . /* Open the m
1013e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1013f 6c 65 20 65 78 63 6c 75 73 69 76 65 6c 79 20 69 le exclusively i
10140 6e 20 63 61 73 65 20 73 6f 6d 65 20 6f 74 68 65 n case some othe
10141 72 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 69 r process. ** i
10142 73 20 72 75 6e 6e 69 6e 67 20 74 68 69 73 20 72 s running this r
10143 6f 75 74 69 6e 65 20 61 6c 73 6f 2e 20 4e 6f 74 outine also. Not
10144 20 74 68 61 74 20 69 74 20 6d 61 6b 65 73 20 74 that it makes t
10145 6f 6f 20 6d 75 63 68 20 64 69 66 66 65 72 65 6e oo much differen
10146 63 65 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74 ce.. */. pMast
10147 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 er = (sqlite3_fi
10148 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c le *)sqlite3Mall
10149 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c oc(pVfs->szOsFil
1014a 65 20 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e e * 2);. pJourn
1014b 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 al = (sqlite3_fi
1014c 6c 65 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61 le *)(((u8 *)pMa
1014d 73 74 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a ster) + pVfs->sz
1014e 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 OsFile);. if( !
1014f 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72 pMaster ){. r
10150 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
10151 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
10152 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 nt flags = (SQLI
10153 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
10154 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 |SQLITE_OPEN_MAS
10155 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 TER_JOURNAL);.
10156 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
10157 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74 Open(pVfs, zMast
10158 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61 er, pMaster, fla
10159 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 gs, 0);. }. if
1015a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1015b 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 ) goto delmaster
1015c 5f 6f 75 74 3b 0a 20 20 6d 61 73 74 65 72 5f 6f _out;. master_o
1015d 70 65 6e 20 3d 20 31 3b 0a 0a 20 20 72 63 20 3d pen = 1;.. rc =
1015e 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
1015f 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 ze(pMaster, &nMa
10160 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 sterJournal);.
10161 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
10162 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 K ) goto delmast
10163 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e er_out;.. if( n
10164 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 MasterJournal>0
10165 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f ){. char *zJo
10166 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 urnal;. char
10167 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30 3b *zMasterPtr = 0;
10168 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 . int nMaster
10169 50 74 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 Ptr = pPager->pV
1016a 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 fs->mxPathname+1
1016b 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 ;.. /* Load t
1016c 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 he entire master
1016d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e journal file in
1016e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 to space obtaine
1016f 64 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 71 d from. ** sq
10170 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 lite3_malloc() a
10171 6e 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 nd pointed to by
10172 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2e zMasterJournal.
10173 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61 . */. zMa
10174 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 28 63 sterJournal = (c
10175 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c har *)sqlite3Mal
10176 6c 6f 63 28 6e 4d 61 73 74 65 72 4a 6f 75 72 6e loc(nMasterJourn
10177 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 72 29 al + nMasterPtr)
10178 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 ;. if( !zMast
10179 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 erJournal ){.
1017a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
1017b 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f OMEM;. goto
1017c 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
1017d 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65 }. zMaste
1017e 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a rPtr = &zMasterJ
1017f 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f ournal[nMasterJo
10180 75 72 6e 61 6c 5d 3b 0a 20 20 20 20 72 63 20 3d urnal];. rc =
10181 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
10182 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 72 4a Master, zMasterJ
10183 6f 75 72 6e 61 6c 2c 20 6e 4d 61 73 74 65 72 4a ournal, nMasterJ
10184 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 ournal, 0);.
10185 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
10186 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 K ) goto delmast
10187 65 72 5f 6f 75 74 3b 0a 0a 20 20 20 20 7a 4a 6f er_out;.. zJo
10188 75 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a urnal = zMasterJ
10189 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c ournal;. whil
1018a 65 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 e( (zJournal-zMa
1018b 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61 sterJournal)<nMa
1018c 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 sterJournal ){.
1018d 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b int exists;
1018e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1018f 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 te3OsAccess(pVfs
10190 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 , zJournal, SQLI
10191 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
10192 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20 , &exists);.
10193 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
10194 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
10195 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 oto delmaster_ou
10196 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
10197 20 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 if( exists ){.
10198 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
10199 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f the journals po
1019a 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 inted to by the
1019b 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 master journal e
1019c 78 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a xists.. *
1019d 2a 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68 * Open it and ch
1019e 65 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73 eck if it points
1019f 20 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a at the master j
101a0 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 ournal. If.
101a1 20 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e ** so, return
101a2 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e without deletin
101a3 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 g the master jou
101a4 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 rnal file..
101a5 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e */. in
101a6 74 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 t c;. int
101a7 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 flags = (SQLITE
101a8 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 _OPEN_READONLY|S
101a9 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
101aa 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 JOURNAL);.
101ab 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
101ac 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 Open(pVfs, zJour
101ad 6e 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 nal, pJournal, f
101ae 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 lags, 0);.
101af 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
101b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
101b1 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
101b2 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a out;. }..
101b3 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 rc = rea
101b4 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 dMasterJournal(p
101b5 4a 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 Journal, zMaster
101b6 50 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 Ptr, nMasterPtr)
101b7 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
101b8 33 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 3OsClose(pJourna
101b9 6c 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 l);. if(
101ba 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
101bb 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
101bc 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 delmaster_out;.
101bd 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
101be 20 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 c = zMasterPtr
101bf 5b 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70 [0]!=0 && strcmp
101c0 28 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 (zMasterPtr, zMa
101c1 73 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 ster)==0;.
101c2 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20 if( c ){.
101c3 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 /* We have
101c4 61 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 a match. Do not
101c5 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 delete the maste
101c6 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 r journal file.
101c7 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 */. got
101c8 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b o delmaster_out;
101c9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
101ca 20 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 }. zJourna
101cb 6c 20 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 4a 6f l += (strlen(zJo
101cc 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d urnal)+1);. }
101cd 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 . }. . rc = s
101ce 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 qlite3OsDelete(p
101cf 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 Vfs, zMaster, 0)
101d0 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 ;..delmaster_out
101d1 3a 0a 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a :. if( zMasterJ
101d2 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 ournal ){. sq
101d3 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 lite3_free(zMast
101d4 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 erJournal);. }
101d5 20 0a 20 20 69 66 28 20 6d 61 73 74 65 72 5f 6f . if( master_o
101d6 70 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 pen ){. sqlit
101d7 65 33 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 e3OsClose(pMaste
101d8 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 r);. }. sqlite
101d9 33 5f 66 72 65 65 28 70 4d 61 73 74 65 72 29 3b 3_free(pMaster);
101da 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
101db 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 ..static void pa
101dc 67 65 72 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 ger_truncate_cac
101dd 68 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 he(Pager *pPager
101de 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 );../*.** Trunca
101df 74 65 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 te the main file
101e0 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 70 61 of the given pa
101e1 67 65 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 ger to the numbe
101e2 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 69 6e r of pages.** in
101e3 64 69 63 61 74 65 64 2e 20 41 6c 73 6f 20 74 72 dicated. Also tr
101e4 75 6e 63 61 74 65 20 74 68 65 20 63 61 63 68 65 uncate the cache
101e5 64 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e d representation
101e6 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a of the file..**
101e7 0a 2a 2a 20 4d 69 67 68 74 20 6d 69 67 68 74 20 .** Might might
101e8 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
101e9 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
101ea 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 k is smaller tha
101eb 6e 20 6e 50 61 67 65 2e 0a 2a 2a 20 54 68 69 73 n nPage..** This
101ec 20 63 61 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 can happen, for
101ed 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 77 65 20 example, if we
101ee 61 72 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c are in the middl
101ef 65 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 e of a transacti
101f0 6f 6e 0a 2a 2a 20 77 68 69 63 68 20 68 61 73 20 on.** which has
101f1 65 78 74 65 6e 64 65 64 20 74 68 65 20 66 69 6c extended the fil
101f2 65 20 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e e size and the n
101f3 65 77 20 70 61 67 65 73 20 61 72 65 20 73 74 69 ew pages are sti
101f4 6c 6c 20 61 6c 6c 20 68 65 6c 64 0a 2a 2a 20 69 ll all held.** i
101f5 6e 20 63 61 63 68 65 2c 20 74 68 65 6e 20 61 6e n cache, then an
101f6 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 INSERT or UPDAT
101f7 45 20 64 6f 65 73 20 61 20 73 74 61 74 65 6d 65 E does a stateme
101f8 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 53 6f nt rollback. So
101f9 6d 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 me.** operating
101fa 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 system implement
101fb 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 20 63 ations can get c
101fc 6f 6e 66 75 73 65 64 20 69 66 20 79 6f 75 20 74 onfused if you t
101fd 72 79 20 74 6f 0a 2a 2a 20 74 72 75 6e 63 61 74 ry to.** truncat
101fe 65 20 61 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 e a file to some
101ff 20 73 69 7a 65 20 74 68 61 74 20 69 73 20 6c 61 size that is la
10200 72 67 65 72 20 74 68 61 6e 20 69 74 20 63 75 72 rger than it cur
10201 72 65 6e 74 6c 79 20 69 73 2c 0a 2a 2a 20 73 6f rently is,.** so
10202 20 64 65 74 65 63 74 20 74 68 69 73 20 63 61 73 detect this cas
10203 65 20 61 6e 64 20 77 72 69 74 65 20 61 20 73 69 e and write a si
10204 6e 67 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74 ngle zero byte t
10205 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
10206 20 6e 65 77 0a 2a 2a 20 66 69 6c 65 20 69 6e 73 new.** file ins
10207 74 65 61 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tead..*/.static
10208 69 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63 61 int pager_trunca
10209 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 te(Pager *pPager
1020a 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 , int nPage){.
1020b 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1020c 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 OK;. if( pPager
1020d 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 ->state>=PAGER_E
1020e 58 43 4c 55 53 49 56 45 20 26 26 20 70 50 61 67 XCLUSIVE && pPag
1020f 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 er->fd->pMethods
10210 20 29 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72 ){. i64 curr
10211 65 6e 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 entSize, newSize
10212 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
10213 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
10214 67 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e ger->fd, ¤
10215 74 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53 tSize);. newS
10216 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 ize = pPager->pa
10217 67 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67 geSize*(i64)nPag
10218 65 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 e;. if( rc==S
10219 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 QLITE_OK && curr
1021a 65 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 entSize!=newSize
1021b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 ){. if( cu
1021c 72 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a rrentSize>newSiz
1021d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 e ){. rc
1021e 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
1021f 61 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ate(pPager->fd,
10220 6e 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 newSize);.
10221 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
10222 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
10223 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22 te(pPager->fd, "
10224 22 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29 ", 1, newSize-1)
10225 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
10226 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
10227 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
10228 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
10229 6e 50 61 67 65 3b 0a 20 20 20 20 70 61 67 65 72 nPage;. pager
1022a 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65 28 _truncate_cache(
1022b 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 pPager);. }. r
1022c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1022d 2a 2a 20 53 65 74 20 74 68 65 20 73 65 63 74 6f ** Set the secto
1022e 72 53 69 7a 65 20 66 6f 72 20 74 68 65 20 67 69 rSize for the gi
1022f 76 65 6e 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a ven pager..**.**
10230 20 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 The sector size
10231 20 69 73 20 61 74 20 6c 65 61 73 74 20 61 73 20 is at least as
10232 62 69 67 20 61 73 20 74 68 65 20 73 65 63 74 6f big as the secto
10233 72 20 73 69 7a 65 20 72 65 70 6f 72 74 65 64 0a r size reported.
10234 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 4f 73 53 ** by sqlite3OsS
10235 65 63 74 6f 72 53 69 7a 65 28 29 2e 20 20 54 68 ectorSize(). Th
10236 65 20 6d 69 6e 69 6d 75 6d 20 73 65 63 74 6f 72 e minimum sector
10237 20 73 69 7a 65 20 69 73 20 35 31 32 2e 0a 2a 2f size is 512..*/
10238 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
10239 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67 65 72 SectorSize(Pager
1023a 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 *pPager){. ass
1023b 65 72 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e ert(pPager->fd->
1023c 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67 65 72 pMethods||pPager
1023d 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 69 ->tempFile);. i
1023e 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 f( !pPager->temp
1023f 46 69 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 File ){. /* S
10240 65 63 74 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e ector size doesn
10241 27 74 20 6d 61 74 74 65 72 20 66 6f 72 20 74 65 't matter for te
10242 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 41 mporary files. A
10243 6c 73 6f 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 lso, the file.
10244 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 ** may not hav
10245 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 e been opened ye
10246 74 2c 20 69 6e 20 77 68 63 69 68 20 63 61 73 65 t, in whcih case
10247 20 74 68 65 20 4f 73 53 65 63 74 6f 72 53 69 7a the OsSectorSiz
10248 65 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 e(). ** call
10249 77 69 6c 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20 will segfault..
1024a 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 */. pPager
1024b 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 ->sectorSize = s
1024c 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 qlite3OsSectorSi
1024d 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a ze(pPager->fd);.
1024e 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 }. if( pPager
1024f 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3c 35 31 32 ->sectorSize<512
10250 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
10251 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 35 31 32 sectorSize = 512
10252 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ;. }.}../*.** P
10253 6c 61 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 layback the jour
10254 6e 61 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73 nal and thus res
10255 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 tore the databas
10256 65 20 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 e file to.** the
10257 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e state it was in
10258 20 62 65 66 6f 72 65 20 77 65 20 73 74 61 72 74 before we start
10259 65 64 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 ed making change
1025a 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a s. .**.** The j
1025b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d ournal file form
1025c 61 74 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 at is as follows
1025d 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 : .**.** (1) 8
1025e 20 62 79 74 65 20 70 72 65 66 69 78 2e 20 20 41 byte prefix. A
1025f 20 63 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 copy of aJourna
10260 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 lMagic[]..** (2
10261 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e ) 4 byte big-en
10262 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 dian integer whi
10263 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ch is the number
10264 20 6f 66 20 76 61 6c 69 64 20 70 61 67 65 20 72 of valid page r
10265 65 63 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 ecords.**
10266 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 in the journal.
10267 20 49 66 20 74 68 69 73 20 76 61 6c 75 65 20 69 If this value i
10268 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 s 0xffffffff, th
10269 65 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a en compute the.*
1026a 2a 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f * number o
1026b 66 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 f page records f
1026c 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 rom the journal
1026d 73 69 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 size..** (3) 4
1026e 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e byte big-endian
1026f 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 integer which i
10270 73 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61 s the initial va
10271 6c 75 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 lue for the .**
10272 20 20 20 20 20 20 73 61 6e 69 74 79 20 63 68 65 sanity che
10273 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 cksum..** (4)
10274 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 4 byte integer w
10275 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 hich is the numb
10276 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74 er of pages to t
10277 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 runcate the.**
10278 20 20 20 20 20 64 61 74 61 62 61 73 65 20 74 6f database to
10279 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 during a rollba
1027a 63 6b 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 ck..** (5) 4 b
1027b 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
1027c 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
1027d 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e the sector size.
1027e 20 20 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 The header.**
1027f 20 20 20 20 20 20 69 73 20 74 68 69 73 20 6d 61 is this ma
10280 6e 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ny bytes in size
10281 2e 0a 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74 ..** (6) 4 byt
10282 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 e big-endian int
10283 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
10284 65 20 70 61 67 65 20 63 61 73 65 2e 0a 2a 2a 20 e page case..**
10285 20 28 37 29 20 20 34 20 62 79 74 65 20 69 6e 74 (7) 4 byte int
10286 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
10287 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
10288 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 s in the master
10289 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 journal.**
1028a 20 6e 61 6d 65 2e 20 20 54 68 65 20 76 61 6c 75 name. The valu
1028b 65 20 6d 61 79 20 62 65 20 7a 65 72 6f 20 28 69 e may be zero (i
1028c 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 ndicate that the
1028d 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 0a re is no master.
1028e 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c ** journal
1028f 2e 29 0a 2a 2a 20 20 28 38 29 20 20 4e 20 62 79 .).** (8) N by
10290 74 65 73 20 6f 66 20 74 68 65 20 6d 61 73 74 65 tes of the maste
10291 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 r journal name.
10292 20 54 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 62 The name will b
10293 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 e nul-terminated
10294 0a 2a 2a 20 20 20 20 20 20 20 61 6e 64 20 6d 69 .** and mi
10295 67 68 74 20 62 65 20 73 68 6f 72 74 65 72 20 74 ght be shorter t
10296 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 han the value re
10297 61 64 20 66 72 6f 6d 20 28 35 29 2e 20 20 49 66 ad from (5). If
10298 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a the first byte.
10299 2a 2a 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 ** of the
1029a 6e 61 6d 65 20 69 73 20 5c 30 30 30 20 74 68 65 name is \000 the
1029b 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 n there is no ma
1029c 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 ster journal. T
1029d 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 he master.**
1029e 20 20 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 journal name
1029f 69 73 20 73 74 6f 72 65 64 20 69 6e 20 55 54 46 is stored in UTF
102a0 2d 38 2e 0a 2a 2a 20 20 28 39 29 20 20 5a 65 72 -8..** (9) Zer
102a1 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 o or more pages
102a2 69 6e 73 74 61 6e 63 65 73 2c 20 65 61 63 68 20 instances, each
102a3 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 as follows:.**
102a4 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 + 4 byte
102a5 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 page number..**
102a6 20 20 20 20 20 20 20 2b 20 20 70 50 61 67 65 72 + pPager
102a7 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 ->pageSize bytes
102a8 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20 of data..**
102a9 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 63 68 + 4 byte ch
102aa 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 ecksum.**.** Whe
102ab 6e 20 77 65 20 73 70 65 61 6b 20 6f 66 20 74 68 n we speak of th
102ac 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
102ad 2c 20 77 65 20 6d 65 61 6e 20 74 68 65 20 66 69 , we mean the fi
102ae 72 73 74 20 38 20 69 74 65 6d 73 20 61 62 6f 76 rst 8 items abov
102af 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 e..** Each entry
102b0 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
102b1 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
102b2 66 20 74 68 65 20 39 74 68 20 69 74 65 6d 2e 0a f the 9th item..
102b3 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76 **.** Call the v
102b4 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 73 65 alue from the se
102b5 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22 6e 52 65 cond bullet "nRe
102b6 63 22 2e 20 20 6e 52 65 63 20 69 73 20 74 68 65 c". nRec is the
102b7 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61 number of.** va
102b8 6c 69 64 20 70 61 67 65 20 65 6e 74 72 69 65 73 lid page entries
102b9 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e in the journal.
102ba 20 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c In most cases,
102bb 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70 75 74 65 you can compute
102bc 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 the.** value of
102bd 20 6e 52 65 63 20 66 72 6f 6d 20 74 68 65 20 73 nRec from the s
102be 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ize of the journ
102bf 61 6c 20 66 69 6c 65 2e 20 20 42 75 74 20 69 66 al file. But if
102c0 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c a power.** fail
102c1 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69 ure occurred whi
102c2 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 le the journal w
102c3 61 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e as being written
102c4 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 , it could be th
102c5 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 74 e.** case that t
102c6 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a he size of the j
102c7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 64 20 ournal file had
102c8 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63 already been inc
102c9 72 65 61 73 65 64 20 62 75 74 0a 2a 2a 20 74 68 reased but.** th
102ca 65 20 65 78 74 72 61 20 65 6e 74 72 69 65 73 20 e extra entries
102cb 68 61 64 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 had not yet made
102cc 20 69 74 20 73 61 66 65 6c 79 20 74 6f 20 64 69 it safely to di
102cd 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 61 20 63 sk. In such a c
102ce 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 ase,.** the valu
102cf 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d 70 75 74 e of nRec comput
102d0 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 ed from the file
102d1 20 73 69 7a 65 20 77 6f 75 6c 64 20 62 65 20 74 size would be t
102d2 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a oo large. For.*
102d3 2a 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 * that reason, w
102d4 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 65 e always use the
102d5 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 20 74 nRec value in t
102d6 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a he header..**.**
102d7 20 49 66 20 74 68 65 20 6e 52 65 63 20 76 61 6c If the nRec val
102d8 75 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66 ue is 0xffffffff
102d9 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 6e it means that n
102da 52 65 63 20 73 68 6f 75 6c 64 20 62 65 20 63 6f Rec should be co
102db 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 mputed.** from t
102dc 68 65 20 66 69 6c 65 20 73 69 7a 65 2e 20 20 54 he file size. T
102dd 68 69 73 20 76 61 6c 75 65 20 69 73 20 75 73 65 his value is use
102de 64 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 d when the user
102df 73 65 6c 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e selects the.** n
102e0 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f o-sync option fo
102e1 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 r the journal.
102e2 41 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 A power failure
102e3 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f could lead to co
102e4 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 rruption.** in t
102e5 68 69 73 20 63 61 73 65 2e 20 20 42 75 74 20 66 his case. But f
102e6 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 or things like t
102e7 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28 emporary table (
102e8 77 68 69 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a which will be.**
102e9 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 deleted when th
102ea 65 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f e power is resto
102eb 72 65 64 29 20 77 65 20 64 6f 6e 27 74 20 63 61 red) we don't ca
102ec 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 re. .**.** If t
102ed 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 he file opened a
102ee 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 s the journal fi
102ef 6c 65 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c le is not a well
102f0 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e -formed.** journ
102f1 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c al file then all
102f2 20 70 61 67 65 73 20 75 70 20 74 6f 20 74 68 65 pages up to the
102f3 20 66 69 72 73 74 20 63 6f 72 72 75 70 74 65 64 first corrupted
102f4 20 70 61 67 65 20 61 72 65 20 72 6f 6c 6c 65 64 page are rolled
102f5 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20 .** back (or no
102f6 70 61 67 65 73 20 69 66 20 74 68 65 20 6a 6f 75 pages if the jou
102f7 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 63 rnal header is c
102f8 6f 72 72 75 70 74 65 64 29 2e 20 54 68 65 20 6a orrupted). The j
102f9 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 ournal file.** i
102fa 73 20 74 68 65 6e 20 64 65 6c 65 74 65 64 20 61 s then deleted a
102fb 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
102fc 75 72 6e 65 64 2c 20 6a 75 73 74 20 61 73 20 69 urned, just as i
102fd 66 20 6e 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20 f no corruption
102fe 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f had.** been enco
102ff 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 untered..**.** I
10300 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c f an I/O or mall
10301 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 oc() error occur
10302 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 s, the journal-f
10303 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c 65 74 ile is not delet
10304 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 ed.** and an err
10305 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
10306 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
10307 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 nt pager_playbac
10308 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c k(Pager *pPager,
10309 20 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20 73 int isHot){. s
1030a 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1030b 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b = pPager->pVfs;
1030c 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 . i64 szJ;
1030d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1030e 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ize of the journ
1030f 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 al file in bytes
10310 20 2a 2f 0a 20 20 75 33 32 20 6e 52 65 63 3b 20 */. u32 nRec;
10311 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10312 2a 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f * Number of Reco
10313 72 64 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e rds in the journ
10314 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20 al */. u32 u;
10315 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10316 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f /* Unsigned loo
10317 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 50 p counter */. P
10318 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20 20 gno mxPg = 0;
10319 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
1031a 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 of the original
1031b 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 2a 2f file in pages */
1031c 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
1031d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1031e 65 73 75 6c 74 20 63 6f 64 65 20 6f 66 20 61 20 esult code of a
1031f 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 subroutine */.
10320 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 int res = 1;
10321 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
10322 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 e returned by sq
10323 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 lite3OsAccess()
10324 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 */. char *zMast
10325 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a er = 0; /*
10326 20 4e 61 6d 65 20 6f 66 20 6d 61 73 74 65 72 20 Name of master
10327 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 journal file if
10328 61 6e 79 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 any */.. /* Fig
10329 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 ure out how many
1032a 20 72 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20 records are in
1032b 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 the journal. Ab
1032c 6f 72 74 20 65 61 72 6c 79 20 69 66 0a 20 20 2a ort early if. *
1032d 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 * the journal is
1032e 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 empty.. */. a
1032f 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a ssert( pPager->j
10330 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 ournalOpen );.
10331 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 rc = sqlite3OsFi
10332 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a leSize(pPager->j
10333 66 64 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 fd, &szJ);. if(
10334 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
10335 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 | szJ==0 ){.
10336 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 goto end_playbac
10337 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 k;. }.. /* Rea
10338 64 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 d the master jou
10339 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 rnal name from t
1033a 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 he journal, if i
1033b 74 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 t is present..
1033c 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a ** If a master j
1033d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
1033e 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 62 is specified, b
1033f 75 74 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e ut the file is n
10340 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 ot. ** present
10341 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 on disk, then th
10342 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 e journal is not
10343 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f hot and does no
10344 74 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 2a t need to be. *
10345 2a 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 * played back..
10346 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 */. zMaster =
10347 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
10348 65 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 e;. rc = readMa
10349 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 sterJournal(pPag
1034a 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 er->jfd, zMaster
1034b 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e , pPager->pVfs->
1034c 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 mxPathname+1);.
1034d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1034e 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d OK && zMaster[0]
1034f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
10350 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 ite3OsAccess(pVf
10351 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 s, zMaster, SQLI
10352 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
10353 2c 20 26 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a , &res);. }. z
10354 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 Master = 0;. if
10355 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
10356 7c 7c 20 21 72 65 73 20 29 7b 0a 20 20 20 20 67 || !res ){. g
10357 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b oto end_playback
10358 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e ;. }. pPager->
10359 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
1035a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 . /* This loop
1035b 74 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 terminates eithe
1035c 72 20 77 68 65 6e 20 74 68 65 20 72 65 61 64 4a r when the readJ
1035d 6f 75 72 6e 61 6c 48 64 72 28 29 20 63 61 6c 6c ournalHdr() call
1035e 20 72 65 74 75 72 6e 73 0a 20 20 2a 2a 20 53 51 returns. ** SQ
1035f 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 LITE_DONE or an
10360 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e IO error occurs.
10361 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29 */. while( 1 )
10362 7b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 {.. /* Read t
10363 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 he next journal
10364 68 65 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20 header from the
10365 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 journal file. I
10366 66 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 f there are.
10367 2a 2a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 ** not enough by
10368 74 65 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 tes left in the
10369 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
1036a 20 61 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 a complete head
1036b 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 er, or. ** it
1036c 20 69 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 is corrupted, t
1036d 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 hen a process mu
1036e 73 74 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69 st of failed whi
1036f 6c 65 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 le writing it..
10370 20 20 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 ** This indic
10371 61 74 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 ates nothing mor
10372 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f e needs to be ro
10373 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a lled back.. *
10374 2f 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a /. rc = readJ
10375 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 ournalHdr(pPager
10376 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d , szJ, &nRec, &m
10377 78 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 xPg);. if( rc
10378 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a !=SQLITE_OK ){ .
10379 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1037a 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
1037b 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1037c 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 _OK;. }.
1037d 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 goto end_play
1037e 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 back;. }..
1037f 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 /* If nRec is 0
10380 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 xffffffff, then
10381 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 this journal was
10382 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 created by a pr
10383 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 ocess. ** wor
10384 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 king in no-sync
10385 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 mode. This means
10386 20 74 68 61 74 20 74 68 65 20 72 65 73 74 20 6f that the rest o
10387 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 f the journal.
10388 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 ** file consis
10389 74 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 ts of pages, the
1038a 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a re are no more j
1038b 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 ournal headers.
1038c 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 Compute. ** t
1038d 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 he value of nRec
1038e 20 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 based on this a
1038f 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a ssumption.. *
10390 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d /. if( nRec==
10391 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 0xffffffff ){.
10392 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
10393 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d er->journalOff==
10394 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
10395 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 Pager) );.
10396 6e 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 4a 4f nRec = (szJ - JO
10397 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
10398 67 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 ger))/JOURNAL_PG
10399 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 _SZ(pPager);.
1039a 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 }.. /* If nR
1039b 65 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73 ec is 0 and this
1039c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20 rollback is of
1039d 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72 a transaction cr
1039e 65 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20 eated by this.
1039f 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e 64 ** process and
103a0 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 if this is the
103a1 66 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 final header in
103a2 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 the journal, the
103a3 6e 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a n it means. *
103a4 2a 20 74 68 61 74 20 74 68 69 73 20 70 61 72 74 * that this part
103a5 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
103a6 77 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 64 was being filled
103a7 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 but has not yet
103a8 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 6e been. ** syn
103a9 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 6f ced to disk. Co
103aa 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 mpute the number
103ab 20 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 20 of pages based
103ac 6f 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 on the remaining
103ad 0a 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 . ** size of
103ae 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a the file.. **
103af 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 72 . ** The thir
103b0 64 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 65 d term of the te
103b1 73 74 20 77 61 73 20 61 64 64 65 64 20 74 6f 20 st was added to
103b2 66 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 35 fix ticket #2565
103b3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
103b4 20 6e 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 nRec==0 && !isH
103b5 6f 74 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 ot &&. pP
103b6 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
103b7 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 +JOURNAL_HDR_SZ(
103b8 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d pPager)==pPager-
103b9 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 >journalOff ){.
103ba 20 20 20 20 20 6e 52 65 63 20 3d 20 28 73 7a 4a nRec = (szJ
103bb 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e - pPager->journ
103bc 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c alOff) / JOURNAL
103bd 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a _PG_SZ(pPager);.
103be 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
103bf 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 this is the fir
103c0 73 74 20 68 65 61 64 65 72 20 72 65 61 64 20 66 st header read f
103c1 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c rom the journal,
103c2 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20 truncate the.
103c3 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 ** database fi
103c4 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 6f le back to its o
103c5 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 riginal size..
103c6 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 */. if( pPa
103c7 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
103c8 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 =JOURNAL_HDR_SZ(
103c9 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 pPager) ){.
103ca 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e rc = pager_trun
103cb 63 61 74 65 28 70 50 61 67 65 72 2c 20 6d 78 50 cate(pPager, mxP
103cc 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 g);. if( rc
103cd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
103ce 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
103cf 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 playback;.
103d0 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
103d1 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 Copy original pa
103d2 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a ges out of the j
103d3 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 ournal and back
103d4 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
103d5 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 e file.. */.
103d6 20 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 for(u=0; u<nR
103d7 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 ec; u++){.
103d8 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 rc = pager_playb
103d9 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 ack_one_page(pPa
103da 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 ger, pPager->jfd
103db 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 , pPager->journa
103dc 6c 4f 66 66 2c 20 31 29 3b 0a 20 20 20 20 20 20 lOff, 1);.
103dd 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
103de 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 K ){. if(
103df 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc==SQLITE_DONE
103e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
103e1 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
103e2 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
103e3 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a journalOff = szJ
103e4 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
103e5 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 k;. }else
103e6 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 {. /* I
103e7 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 f we are unable
103e8 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 to rollback, the
103e9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 n the database i
103ea 73 20 70 72 6f 62 61 62 6c 79 0a 20 20 20 20 20 s probably.
103eb 20 20 20 20 20 2a 2a 20 67 6f 69 6e 67 20 74 6f ** going to
103ec 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 63 6f end up being co
103ed 72 72 75 70 74 2e 20 20 49 74 20 69 73 20 63 6f rrupt. It is co
103ee 72 72 75 70 74 20 74 6f 20 75 73 2c 20 61 6e 79 rrupt to us, any
103ef 68 6f 77 2e 0a 20 20 20 20 20 20 20 20 20 20 2a how.. *
103f0 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 65 * Perhaps the ne
103f1 78 74 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f xt process to co
103f2 6d 65 20 61 6c 6f 6e 67 20 63 61 6e 20 66 69 78 me along can fix
103f3 20 69 74 2e 2e 2e 2e 0a 20 20 20 20 20 20 20 20 it.....
103f4 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 */. r
103f5 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
103f6 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
103f7 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 goto end_play
103f8 62 61 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a back;. }.
103f9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
103fa 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 }. /*NOTREACHED
103fb 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 */. assert( 0 )
103fc 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3a ;..end_playback:
103fd 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
103fe 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73 E_OK ){. zMas
103ff 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 ter = pPager->pT
10400 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 63 20 mpSpace;. rc
10401 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
10402 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c nal(pPager->jfd,
10403 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 zMaster, pPager
10404 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 ->pVfs->mxPathna
10405 6d 65 2b 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 me+1);. }. if(
10406 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
10407 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 {. rc = pager
10408 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
10409 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 (pPager, zMaster
1040a 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 7d 0a [0]!='\0');. }.
1040b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1040c 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 _OK && zMaster[0
1040d 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 ] && res ){.
1040e 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20 /* If there was
1040f 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
10410 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e and this routin
10411 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 e will return su
10412 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 ccess,. ** se
10413 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 e if it is possi
10414 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 ble to delete th
10415 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
10416 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
10417 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 = pager_delmaste
10418 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 r(pPager, zMaste
10419 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 r);. }.. /* Th
1041a 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 e Pager.sectorSi
1041b 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 ze variable may
1041c 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65 have been update
1041d 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a d while rolling.
1041e 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 ** back a jour
1041f 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 20 61 nal created by a
10420 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 61 20 process with a
10421 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f 72 different sector
10422 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 size. ** value
10423 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 74 68 . Reset it to th
10424 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 20 e correct value
10425 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 for this process
10426 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 .. */. setSect
10427 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a orSize(pPager);.
10428 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
10429 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 74 /*.** Playback t
1042a 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 he statement jou
1042b 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rnal..**.** This
1042c 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 70 is similar to p
1042d 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20 laying back the
1042e 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 transaction jour
1042f 6e 61 6c 20 62 75 74 20 77 69 74 68 0a 2a 2a 20 nal but with.**
10430 61 20 66 65 77 20 65 78 74 72 61 20 74 77 69 73 a few extra twis
10431 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 ts..**.** (1)
10432 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 The number of
10433 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
10434 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 abase file at th
10435 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 20 20 e start of.**
10436 20 20 20 20 20 20 74 68 65 20 73 74 61 74 65 6d the statem
10437 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e ent is stored in
10438 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a pPager->stmtSiz
10439 65 2c 20 6e 6f 74 20 69 6e 20 74 68 65 0a 2a 2a e, not in the.**
1043a 20 20 20 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c journal
1043b 20 66 69 6c 65 20 69 74 73 65 6c 66 2e 0a 2a 2a file itself..**
1043c 0a 2a 2a 20 20 20 20 28 32 29 20 20 49 6e 20 61 .** (2) In a
1043d 64 64 69 74 69 6f 6e 20 74 6f 20 70 6c 61 79 69 ddition to playi
1043e 6e 67 20 62 61 63 6b 20 74 68 65 20 73 74 61 74 ng back the stat
1043f 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2c 20 61 ement journal, a
10440 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 lso.** p
10441 6c 61 79 62 61 63 6b 20 61 6c 6c 20 70 61 67 65 layback all page
10442 73 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 s of the transac
10443 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 62 65 67 tion journal beg
10444 69 6e 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 inning.**
10445 20 20 61 74 20 6f 66 66 73 65 74 20 70 50 61 67 at offset pPag
10446 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 2e 0a 2a er->stmtJSize..*
10447 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
10448 65 72 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b er_stmt_playback
10449 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
1044a 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 . i64 szJ;
1044b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1044c 69 7a 65 20 6f 66 20 74 68 65 20 66 75 6c 6c 20 ize of the full
1044d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 journal */. i64
1044e 20 68 64 72 4f 66 66 3b 0a 20 20 69 6e 74 20 6e hdrOff;. int n
1044f 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
10450 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
10451 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69 6e Records */. in
10452 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
10453 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
10454 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
10455 72 63 3b 0a 0a 20 20 73 7a 4a 20 3d 20 70 50 61 rc;.. szJ = pPa
10456 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
10457 0a 0a 20 20 2f 2a 20 53 65 74 20 68 64 72 4f 66 .. /* Set hdrOf
10458 66 20 74 6f 20 62 65 20 74 68 65 20 6f 66 66 73 f to be the offs
10459 65 74 20 6a 75 73 74 20 61 66 74 65 72 20 74 68 et just after th
1045a 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 e end of the las
1045b 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 70 t journal. ** p
1045c 61 67 65 20 77 72 69 74 74 65 6e 20 62 65 66 6f age written befo
1045d 72 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 re the first jou
1045e 72 6e 61 6c 2d 68 65 61 64 65 72 20 66 6f 72 20 rnal-header for
1045f 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 0a 20 this statement.
10460 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
10461 77 61 73 20 77 72 69 74 74 65 6e 2c 20 6f 72 20 was written, or
10462 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 the end of the f
10463 69 6c 65 20 69 66 20 6e 6f 20 6a 6f 75 72 6e 61 ile if no journa
10464 6c 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77 61 l. ** header wa
10465 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a s written.. */.
10466 20 20 68 64 72 4f 66 66 20 3d 20 70 50 61 67 65 hdrOff = pPage
10467 72 2d 3e 73 74 6d 74 48 64 72 4f 66 66 3b 0a 20 r->stmtHdrOff;.
10468 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
10469 3e 66 75 6c 6c 53 79 6e 63 20 7c 7c 20 21 68 64 >fullSync || !hd
1046a 72 4f 66 66 20 29 3b 0a 20 20 69 66 28 20 21 68 rOff );. if( !h
1046b 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 68 64 72 drOff ){. hdr
1046c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a 20 Off = szJ;. }.
1046d 20 0a 20 20 2f 2a 20 54 72 75 6e 63 61 74 65 20 . /* Truncate
1046e 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 the database bac
1046f 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 k to its origina
10470 6c 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72 l size.. */. r
10471 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 c = pager_trunca
10472 74 65 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 te(pPager, pPage
10473 72 2d 3e 73 74 6d 74 53 69 7a 65 29 3b 0a 20 20 r->stmtSize);.
10474 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
10475 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 state>=PAGER_SHA
10476 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 RED );.. /* Fig
10477 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 ure out how many
10478 20 72 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20 records are in
10479 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f the statement jo
1047a 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 61 73 urnal.. */. as
1047b 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
1047c 6d 74 49 6e 55 73 65 20 26 26 20 70 50 61 67 65 mtInUse && pPage
1047d 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 r->journalOpen )
1047e 3b 0a 20 20 6e 52 65 63 20 3d 20 70 50 61 67 65 ;. nRec = pPage
1047f 72 2d 3e 73 74 6d 74 4e 52 65 63 3b 0a 20 20 0a r->stmtNRec;. .
10480 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e /* Copy origin
10481 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 al pages out of
10482 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f the statement jo
10483 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 69 urnal and back i
10484 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 nto the. ** dat
10485 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f 74 abase file. Not
10486 65 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 e that the state
10487 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6f 6d 69 ment journal omi
10488 74 73 20 63 68 65 63 6b 73 75 6d 73 20 66 72 6f ts checksums fro
10489 6d 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63 6f m. ** each reco
1048a 72 64 20 73 69 6e 63 65 20 70 6f 77 65 72 2d 66 rd since power-f
1048b 61 69 6c 75 72 65 20 72 65 63 6f 76 65 72 79 20 ailure recovery
1048c 69 73 20 6e 6f 74 20 69 6d 70 6f 72 74 61 6e 74 is not important
1048d 20 74 6f 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 to statement.
1048e 2a 2a 20 6a 6f 75 72 6e 61 6c 73 2e 0a 20 20 2a ** journals.. *
1048f 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e /. for(i=0; i<n
10490 52 65 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 Rec; i++){. i
10491 36 34 20 6f 66 66 73 65 74 20 3d 20 69 2a 28 34 64 offset = i*(4
10492 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a +pPager->pageSiz
10493 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 e);. rc = pag
10494 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f er_playback_one_
10495 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 61 page(pPager, pPa
10496 67 65 72 2d 3e 73 74 66 64 2c 20 6f 66 66 73 65 ger->stfd, offse
10497 74 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 t, 0);. asser
10498 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f t( rc!=SQLITE_DO
10499 4e 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 NE );. if( rc
1049a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
1049b 74 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 to end_stmt_play
1049c 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 back;. }.. /*
1049d 4e 6f 77 20 72 6f 6c 6c 20 73 6f 6d 65 20 70 61 Now roll some pa
1049e 67 65 73 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 ges back from th
1049f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f e transaction jo
104a0 75 72 6e 61 6c 2e 20 50 61 67 65 72 2e 73 74 6d urnal. Pager.stm
104a1 74 4a 53 69 7a 65 0a 20 20 2a 2a 20 77 61 73 20 tJSize. ** was
104a2 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
104a3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 journal file whe
104a4 6e 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 n this statement
104a5 20 77 61 73 20 73 74 61 72 74 65 64 2c 20 73 6f was started, so
104a6 0a 20 20 2a 2a 20 65 76 65 72 79 74 68 69 6e 67 . ** everything
104a7 20 61 66 74 65 72 20 74 68 61 74 20 6e 65 65 64 after that need
104a8 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 s to be rolled b
104a9 61 63 6b 2c 20 65 69 74 68 65 72 20 69 6e 74 6f ack, either into
104aa 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 the. ** databa
104ab 73 65 2c 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 se, the memory c
104ac 61 63 68 65 2c 20 6f 72 20 62 6f 74 68 2e 0a 20 ache, or both..
104ad 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 74 20 69 **. ** If it i
104ae 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e s not zero, then
104af 20 50 61 67 65 72 2e 73 74 6d 74 48 64 72 4f 66 Pager.stmtHdrOf
104b0 66 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 f is the offset
104b1 74 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 2a to the start. *
104b2 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 6a * of the first j
104b3 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 72 ournal header wr
104b4 69 74 74 65 6e 20 64 75 72 69 6e 67 20 74 68 69 itten during thi
104b5 73 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e s statement tran
104b6 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 saction.. */.
104b7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
104b8 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 6d ff = pPager->stm
104b9 74 4a 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 72 tJSize;. pPager
104ba 2d 3e 63 6b 73 75 6d 49 6e 69 74 20 3d 20 70 50 ->cksumInit = pP
104bb 61 67 65 72 2d 3e 73 74 6d 74 43 6b 73 75 6d 3b ager->stmtCksum;
104bc 0a 20 20 77 68 69 6c 65 28 20 70 50 61 67 65 72 . while( pPager
104bd 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3c 20 68 ->journalOff < h
104be 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 72 63 20 drOff ){. rc
104bf 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
104c0 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 _one_page(pPager
104c1 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 , pPager->jfd, p
104c2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
104c3 66 2c 20 31 29 3b 0a 20 20 20 20 61 73 73 65 72 f, 1);. asser
104c4 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f t( rc!=SQLITE_DO
104c5 4e 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 NE );. if( rc
104c6 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
104c7 74 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 to end_stmt_play
104c8 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 back;. }.. whi
104c9 6c 65 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 le( pPager->jour
104ca 6e 61 6c 4f 66 66 20 3c 20 73 7a 4a 20 29 7b 0a nalOff < szJ ){.
104cb 20 20 20 20 75 33 32 20 6e 4a 52 65 63 3b 20 20 u32 nJRec;
104cc 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
104cd 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f of Journal Reco
104ce 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64 rds */. u32 d
104cf 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72 ummy;. rc = r
104d0 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 eadJournalHdr(pP
104d1 61 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 ager, szJ, &nJRe
104d2 63 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 c, &dummy);.
104d3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
104d4 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 K ){. asser
104d5 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f t( rc!=SQLITE_DO
104d6 4e 45 20 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f NE );. goto
104d7 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 end_stmt_playba
104d8 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ck;. }. if
104d9 28 20 6e 4a 52 65 63 3d 3d 30 20 29 7b 0a 20 20 ( nJRec==0 ){.
104da 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 73 7a 4a nJRec = (szJ
104db 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e - pPager->journ
104dc 61 6c 4f 66 66 29 20 2f 20 28 70 50 61 67 65 72 alOff) / (pPager
104dd 2d 3e 70 61 67 65 53 69 7a 65 2b 38 29 3b 0a 20 ->pageSize+8);.
104de 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 6e }. for(i=n
104df 4a 52 65 63 2d 31 3b 20 69 3e 3d 30 20 26 26 20 JRec-1; i>=0 &&
104e0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
104e1 66 66 20 3c 20 73 7a 4a 3b 20 69 2d 2d 29 7b 0a ff < szJ; i--){.
104e2 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
104e3 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 _playback_one_pa
104e4 67 65 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 ge(pPager, pPage
104e5 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e r->jfd, pPager->
104e6 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 29 3b 0a journalOff, 1);.
104e7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
104e8 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
104e9 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
104ea 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
104eb 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 end_stmt_playbac
104ec 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 k;. }. }..
104ed 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
104ee 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 0a 65 6e 64 ff = szJ;. .end
104ef 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3a 0a _stmt_playback:.
104f0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
104f1 5f 4f 4b 29 20 7b 0a 20 20 20 20 70 50 61 67 65 _OK) {. pPage
104f2 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
104f3 73 7a 4a 3b 0a 20 20 20 20 2f 2a 20 70 61 67 65 szJ;. /* page
104f4 72 5f 72 65 6c 6f 61 64 5f 63 61 63 68 65 28 70 r_reload_cache(p
104f5 50 61 67 65 72 29 3b 20 2a 2f 0a 20 20 7d 0a 20 Pager); */. }.
104f6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
104f7 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
104f8 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
104f9 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 f in-memory page
104fa 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f 77 s that are allow
104fb 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
104fc 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
104fd 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 e3PagerSetCaches
104fe 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ize(Pager *pPage
104ff 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a r, int mxPage){.
10500 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 sqlite3PcacheS
10501 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 etCachesize(pPag
10502 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d 78 50 er->pPCache, mxP
10503 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 age);.}../*.** A
10504 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73 74 djust the robust
10505 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61 74 61 ness of the data
10506 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20 64 base to damage d
10507 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 ue to OS crashes
10508 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 .** or power fai
10509 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67 69 6e lures by changin
1050a 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 g the number of
1050b 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77 72 syncs()s when wr
1050c 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f 6c iting.** the rol
1050d 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 lback journal.
1050e 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 There are three
1050f 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 levels:.**.**
10510 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69 74 OFF sqlit
10511 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e 65 e3OsSync() is ne
10512 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 ver called. Thi
10513 73 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 s is the default
10514 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
10515 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 61 for temporary a
10516 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c nd transient fil
10517 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f 52 es..**.** NOR
10518 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72 6e MAL The journ
10519 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e 63 al is synced onc
1051a 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 e before writes
1051b 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 begin on the.**
1051c 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 dat
1051d 61 62 61 73 65 2e 20 20 54 68 69 73 20 69 73 20 abase. This is
1051e 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61 74 normally adequat
1051f 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62 75 e protection, bu
10520 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
10521 20 20 69 74 20 69 73 20 74 68 65 6f 72 65 74 69 it is theoreti
10522 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c 20 cally possible,
10523 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c 69 though very unli
10524 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20 20 kely,.**
10525 20 20 20 20 20 20 74 68 61 74 20 61 6e 20 69 6e that an in
10526 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20 66 opertune power f
10527 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 61 ailure could lea
10528 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a ve the journal.*
10529 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 * i
1052a 6e 20 61 20 73 74 61 74 65 20 77 68 69 63 68 20 n a state which
1052b 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d 61 would cause dama
1052c 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ge to the databa
1052d 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 se.**
1052e 20 20 20 77 68 65 6e 20 69 74 20 69 73 20 72 6f when it is ro
1052f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a lled back..**.**
10530 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54 68 FULL Th
10531 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e e journal is syn
10532 63 65 64 20 74 77 69 63 65 20 62 65 66 6f 72 65 ced twice before
10533 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e writes begin on
10534 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
10535 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28 77 database (w
10536 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69 6f ith some additio
10537 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 nal information
10538 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 - the nRec field
10539 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1053a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
1053b 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20 77 header - being w
1053c 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65 65 ritten in betwee
1053d 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20 20 n the two.**
1053e 20 20 20 20 20 20 20 20 20 20 73 79 6e 63 73 29 syncs)
1053f 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d 65 20 . If we assume
10540 74 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a 2a that writing a.*
10541 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 * s
10542 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f ingle disk secto
10543 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68 65 r is atomic, the
10544 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f 76 n this mode prov
10545 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ides.**
10546 20 20 20 20 20 61 73 73 75 72 61 6e 63 65 20 74 assurance t
10547 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
10548 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72 72 will not be corr
10549 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 upted to the.**
1054a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f 69 poi
1054b 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64 61 nt of causing da
1054c 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 mage to the data
1054d 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c base during roll
1054e 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 back..**.** Nume
1054f 72 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f 63 ric values assoc
10550 69 61 74 65 64 20 77 69 74 68 20 74 68 65 73 65 iated with these
10551 20 73 74 61 74 65 73 20 61 72 65 20 4f 46 46 3d states are OFF=
10552 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a =1, NORMAL=2,.**
10553 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a and FULL=3..*/.
10554 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
10555 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 MIT_PAGER_PRAGMA
10556 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 S.SQLITE_PRIVATE
10557 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
10558 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c erSetSafetyLevel
10559 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
1055a 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 62 int level, int b
1055b 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 70 50 FullFsync){. pP
1055c 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 20 ager->noSync =
1055d 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 50 61 67 level==1 || pPag
1055e 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 er->tempFile;.
1055f 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 pPager->fullSync
10560 20 3d 20 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 = level==3 && !
10561 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
10562 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 ;. pPager->sync
10563 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46 _flags = (bFullF
10564 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 sync?SQLITE_SYNC
10565 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e _FULL:SQLITE_SYN
10566 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 C_NORMAL);. if(
10567 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
10568 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 ) pPager->needSy
10569 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 nc = 0;.}.#endif
1056a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
1056b 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 owing global var
1056c 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 iable is increme
1056d 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 nted whenever th
1056e 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 e library.** att
1056f 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 empts to open a
10570 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 temporary file.
10571 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f This informatio
10572 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a n is used for.**
10573 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 testing and ana
10574 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f lysis only. .*/
10575 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
10576 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
10577 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 nt sqlite3_opent
10578 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 emp_count = 0;.#
10579 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 endif../*.** Ope
1057a 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 n a temporary fi
1057b 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 le. .**.** Write
1057c 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
1057d 70 74 6f 72 20 69 6e 74 6f 20 2a 66 64 2e 20 20 ptor into *fd.
1057e 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
1057f 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 73 on success or s
10580 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 65 72 72 ome.** other err
10581 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 61 or code if we fa
10582 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c 20 il. The OS will
10583 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 automatically de
10584 6c 65 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 lete the tempora
10585 72 79 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 ry.** file when
10586 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f it is closed..*/
10587 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
10588 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d 70 te3PagerOpentemp
10589 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 (. Pager *pPage
1058a 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 r, /* The
1058b 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f pager object */
1058c 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
1058d 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 *pFile, /* Writ
1058e 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 e the file descr
1058f 69 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 iptor here */.
10590 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 int vfsFlags
10591 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 /* Flags p
10592 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f assed through to
10593 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 the VFS */.){.
10594 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 64 65 66 int rc;..#ifdef
10595 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 SQLITE_TEST. s
10596 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f qlite3_opentemp_
10597 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 count++; /* Use
10598 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e d for testing an
10599 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 d analysis only
1059a 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 */.#endif.. vfs
1059b 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 Flags |= SQLITE
1059c 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
1059d 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 | SQLITE_OPEN_CR
1059e 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 EATE |.
1059f 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 SQLITE_OPEN_E
105a0 58 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 54 XCLUSIVE | SQLIT
105a1 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
105a2 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c LOSE;. rc = sql
105a3 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 ite3OsOpen(pPage
105a4 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c r->pVfs, 0, pFil
105a5 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b e, vfsFlags, 0);
105a6 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 . assert( rc!=S
105a7 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c QLITE_OK || pFil
105a8 65 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a 20 e->pMethods );.
105a9 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 return rc;.}..s
105aa 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 tatic int pagerS
105ab 74 72 65 73 73 28 76 6f 69 64 20 2a 2c 50 67 48 tress(void *,PgH
105ac 64 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 dr *);../*.** Cr
105ad 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 eate a new page
105ae 63 61 63 68 65 20 61 6e 64 20 70 75 74 20 61 20 cache and put a
105af 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
105b0 61 67 65 20 63 61 63 68 65 20 69 6e 20 2a 70 70 age cache in *pp
105b1 50 61 67 65 72 2e 0a 2a 2a 20 54 68 65 20 66 69 Pager..** The fi
105b2 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64 20 le to be cached
105b3 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 74 2e 20 need not exist.
105b4 20 54 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 The file is not
105b5 20 6c 6f 63 6b 65 64 20 75 6e 74 69 6c 0a 2a 2a locked until.**
105b6 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 the first call
105b7 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 to sqlite3PagerG
105b8 65 74 28 29 20 61 6e 64 20 69 73 20 6f 6e 6c 79 et() and is only
105b9 20 68 65 6c 64 20 6f 70 65 6e 20 75 6e 74 69 6c held open until
105ba 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61 67 the.** last pag
105bb 65 20 69 73 20 72 65 6c 65 61 73 65 64 20 75 73 e is released us
105bc 69 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65 72 ing sqlite3Pager
105bd 55 6e 72 65 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 Unref()..**.** I
105be 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e f zFilename is N
105bf 55 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f ULL then a rando
105c0 6d 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 mly-named tempor
105c1 61 72 79 20 66 69 6c 65 20 69 73 20 63 72 65 61 ary file is crea
105c2 74 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 ted.** and used
105c3 61 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 as the file to b
105c4 65 20 63 61 63 68 65 64 2e 20 20 54 68 65 20 66 e cached. The f
105c5 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 ile will be dele
105c6 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 ted.** automatic
105c7 61 6c 6c 79 20 77 68 65 6e 20 69 74 20 69 73 20 ally when it is
105c8 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 closed..**.** If
105c9 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a zFilename is ":
105ca 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6c memory:" then al
105cb 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 l information is
105cc 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 0a held in cache..
105cd 2a 2a 20 49 74 20 69 73 20 6e 65 76 65 72 20 77 ** It is never w
105ce 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 ritten to disk.
105cf 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
105d0 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 d to implement a
105d1 6e 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 n.** in-memory d
105d2 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 atabase..*/.SQLI
105d3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
105d4 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 qlite3PagerOpen(
105d5 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
105d6 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 pVfs, /* T
105d7 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 he virtual file
105d8 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f system to use */
105d9 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 . Pager **ppPag
105da 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 er, /* R
105db 65 74 75 72 6e 20 74 68 65 20 50 61 67 65 72 20 eturn the Pager
105dc 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a structure here *
105dd 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
105de 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 zFilename, /*
105df 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
105e0 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 base file to ope
105e1 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 n */. int nExtr
105e2 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a,
105e3 2f 2a 20 45 78 74 72 61 20 62 79 74 65 73 20 61 /* Extra bytes a
105e4 70 70 65 6e 64 20 74 6f 20 65 61 63 68 20 69 6e ppend to each in
105e5 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a -memory page */.
105e6 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 int flags,
105e7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c /* fl
105e8 61 67 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 ags controlling
105e9 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 this file */. i
105ea 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 nt vfsFlags
105eb 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 /* flags
105ec 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 passed through
105ed 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 to sqlite3_vfs.x
105ee 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 75 Open() */.){. u
105ef 38 20 2a 70 50 74 72 3b 0a 20 20 50 61 67 65 72 8 *pPtr;. Pager
105f0 20 2a 70 50 61 67 65 72 20 3d 20 30 3b 0a 20 20 *pPager = 0;.
105f1 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
105f2 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 OK;. int i;. i
105f3 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 3b nt tempFile = 0;
105f4 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 30 . int memDb = 0
105f5 3b 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 ;. int readOnly
105f6 20 3d 20 30 3b 0a 20 20 69 6e 74 20 75 73 65 4a = 0;. int useJ
105f7 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 ournal = (flags
105f8 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 & PAGER_OMIT_JOU
105f9 52 4e 41 4c 29 3d 3d 30 3b 0a 20 20 69 6e 74 20 RNAL)==0;. int
105fa 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c noReadlock = (fl
105fb 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 ags & PAGER_NO_R
105fc 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 0a 20 20 69 EADLOCK)!=0;. i
105fd 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 nt journalFileSi
105fe 7a 65 3b 0a 20 20 69 6e 74 20 70 63 61 63 68 65 ze;. int pcache
105ff 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63 Size = sqlite3Pc
10600 61 63 68 65 53 69 7a 65 28 29 3b 0a 20 20 69 6e acheSize();. in
10601 74 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 t szPageDflt = S
10602 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
10603 47 45 5f 53 49 5a 45 3b 0a 20 20 63 68 61 72 20 GE_SIZE;. char
10604 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 0a *zPathname = 0;.
10605 20 20 69 6e 74 20 6e 50 61 74 68 6e 61 6d 65 20 int nPathname
10606 3d 20 30 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 = 0;.. if( sqli
10607 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 te3JournalSize(p
10608 56 66 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a Vfs)>sqlite3MemJ
10609 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a ournalSize() ){.
1060a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 journalFileS
1060b 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 ize = sqlite3Jou
1060c 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3b 0a rnalSize(pVfs);.
1060d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 }else{. jou
1060e 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 73 rnalFileSize = s
1060f 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
10610 53 69 7a 65 28 29 3b 0a 20 20 7d 0a 0a 20 20 2f Size();. }.. /
10611 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 72 65 * The default re
10612 74 75 72 6e 20 69 73 20 61 20 4e 55 4c 4c 20 70 turn is a NULL p
10613 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 2a 70 70 50 ointer */. *ppP
10614 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 ager = 0;.. /*
10615 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f 72 Compute and stor
10616 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e e the full pathn
10617 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 61 ame in an alloca
10618 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e 74 ted buffer point
10619 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a 50 ed. ** to by zP
1061a 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 athname, length
1061b 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 69 nPathname. Or, i
1061c 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 f this is a temp
1061d 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a 2a orary file,. **
1061e 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 74 leave both nPat
1061f 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 6e hname and zPathn
10620 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 ame set to 0..
10621 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 */. if( zFilena
10622 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b me && zFilename[
10623 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68 6e 0] ){. nPathn
10624 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 ame = pVfs->mxPa
10625 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a 50 thname+1;. zP
10626 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 athname = sqlite
10627 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 6d 3Malloc(nPathnam
10628 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a 50 e*2);. if( zP
10629 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 athname==0 ){.
1062a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1062b 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 E_NOMEM;. }.#
1062c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1062d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 IT_MEMORYDB.
1062e 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c 65 if( strcmp(zFile
1062f 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22 29 name,":memory:")
10630 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ==0 ){. mem
10631 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a 50 Db = 1;. zP
10632 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 0a athname[0] = 0;.
10633 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 }else.#endif
10634 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 72 63 20 . {. rc
10635 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 = sqlite3OsFullP
10636 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 athname(pVfs, zF
10637 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 ilename, nPathna
10638 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a me, zPathname);.
10639 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
1063a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1063b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
1063c 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 e(zPathname);.
1063d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1063e 20 20 20 7d 0a 20 20 20 20 6e 50 61 74 68 6e 61 }. nPathna
1063f 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 50 61 74 me = strlen(zPat
10640 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f hname);. }.. /
10641 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
10642 79 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 y for the pager
10643 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 70 structure */. p
10644 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 4d Pager = sqlite3M
10645 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 73 allocZero(. s
10646 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 20 2b izeof(*pPager) +
10647 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
10648 67 65 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f ger structure */
10649 0a 20 20 20 20 70 63 61 63 68 65 53 69 7a 65 20 . pcacheSize
1064a 20 20 20 20 20 2b 20 20 20 20 20 20 20 20 20 20 +
1064b 20 2f 2a 20 50 43 61 63 68 65 20 6f 62 6a 65 63 /* PCache objec
1064c 74 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c t */. journal
1064d 46 69 6c 65 53 69 7a 65 20 2b 20 20 20 20 20 20 FileSize +
1064e 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 /* The jour
1064f 6e 61 6c 20 66 69 6c 65 20 73 74 72 75 63 74 75 nal file structu
10650 72 65 20 2a 2f 20 0a 20 20 20 20 70 56 66 73 2d re */ . pVfs-
10651 3e 73 7a 4f 73 46 69 6c 65 20 20 2b 20 20 20 20 >szOsFile +
10652 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 /* The ma
10653 69 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a 20 20 in db file */.
10654 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a journalFileSiz
10655 65 20 2a 20 32 20 2b 20 20 20 20 20 20 20 2f 2a e * 2 + /*
10656 20 54 68 65 20 74 77 6f 20 6a 6f 75 72 6e 61 6c The two journal
10657 20 66 69 6c 65 73 20 2a 2f 20 0a 20 20 20 20 33 files */ . 3
10658 2a 6e 50 61 74 68 6e 61 6d 65 20 2b 20 34 30 20 *nPathname + 40
10659 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 /* zF
1065a 69 6c 65 6e 61 6d 65 2c 20 7a 44 69 72 65 63 74 ilename, zDirect
1065b 6f 72 79 2c 20 7a 4a 6f 75 72 6e 61 6c 20 2a 2f ory, zJournal */
1065c 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 . );. if( !pPa
1065d 67 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ger ){. sqlit
1065e 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d e3_free(zPathnam
1065f 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 e);. return S
10660 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
10661 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 . pPager->pPCac
10662 68 65 20 3d 20 28 50 43 61 63 68 65 20 2a 29 26 he = (PCache *)&
10663 70 50 61 67 65 72 5b 31 5d 3b 0a 20 20 70 50 74 pPager[1];. pPt
10664 72 20 3d 20 28 28 75 38 20 2a 29 26 70 50 61 67 r = ((u8 *)&pPag
10665 65 72 5b 31 5d 29 20 2b 20 70 63 61 63 68 65 53 er[1]) + pcacheS
10666 69 7a 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 ize;. pPager->v
10667 66 73 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 fsFlags = vfsFla
10668 67 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64 gs;. pPager->fd
10669 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 = (sqlite3_file
1066a 2a 29 26 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a *)&pPtr[pVfs->sz
1066b 4f 73 46 69 6c 65 2a 30 5d 3b 0a 20 20 70 50 61 OsFile*0];. pPa
1066c 67 65 72 2d 3e 73 74 66 64 20 3d 20 28 73 71 6c ger->stfd = (sql
1066d 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 74 72 ite3_file*)&pPtr
1066e 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 5d [pVfs->szOsFile]
1066f 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 ;. pPager->jfd
10670 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a = (sqlite3_file*
10671 29 26 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f )&pPtr[pVfs->szO
10672 73 46 69 6c 65 2b 6a 6f 75 72 6e 61 6c 46 69 6c sFile+journalFil
10673 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65 72 eSize];. pPager
10674 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 28 63 ->zFilename = (c
10675 68 61 72 2a 29 26 70 50 74 72 5b 70 56 66 73 2d har*)&pPtr[pVfs-
10676 3e 73 7a 4f 73 46 69 6c 65 2b 32 2a 6a 6f 75 72 >szOsFile+2*jour
10677 6e 61 6c 46 69 6c 65 53 69 7a 65 5d 3b 0a 20 20 nalFileSize];.
10678 70 50 61 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f pPager->zDirecto
10679 72 79 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a 46 ry = &pPager->zF
1067a 69 6c 65 6e 61 6d 65 5b 6e 50 61 74 68 6e 61 6d ilename[nPathnam
1067b 65 2b 31 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e e+1];. pPager->
1067c 7a 4a 6f 75 72 6e 61 6c 20 3d 20 26 70 50 61 67 zJournal = &pPag
1067d 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 6e er->zDirectory[n
1067e 50 61 74 68 6e 61 6d 65 2b 31 5d 3b 0a 20 20 70 Pathname+1];. p
1067f 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56 Pager->pVfs = pV
10680 66 73 3b 0a 20 20 69 66 28 20 7a 50 61 74 68 6e fs;. if( zPathn
10681 61 6d 65 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 ame ){. memcp
10682 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e y(pPager->zFilen
10683 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 ame, zPathname,
10684 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 nPathname+1);.
10685 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
10686 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 0a Pathname);. }..
10687 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70 61 /* Open the pa
10688 67 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 ger file.. */.
10689 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 if( zFilename &
1068a 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 & zFilename[0] &
1068b 26 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 & !memDb ){.
1068c 69 66 28 20 6e 50 61 74 68 6e 61 6d 65 3e 28 70 if( nPathname>(p
1068d 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 Vfs->mxPathname
1068e 2d 20 73 69 7a 65 6f 66 28 22 2d 6a 6f 75 72 6e - sizeof("-journ
1068f 61 6c 22 29 29 20 29 7b 0a 20 20 20 20 20 20 72 al")) ){. r
10690 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f c = SQLITE_CANTO
10691 50 45 4e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a PEN;. }else{.
10692 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d int fout =
10693 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0;. rc = s
10694 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
10695 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 s, pPager->zFile
10696 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 name, pPager->fd
10697 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
10698 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 pPage
10699 72 2d 3e 76 66 73 46 6c 61 67 73 2c 20 26 66 6f r->vfsFlags, &fo
1069a 75 74 29 3b 0a 20 20 20 20 20 20 72 65 61 64 4f ut);. readO
1069b 6e 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 nly = (fout&SQLI
1069c 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
1069d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 );.. /* If
1069e 74 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63 the file was suc
1069f 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 cessfully opened
106a0 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 for read/write
106a1 61 63 63 65 73 73 2c 0a 20 20 20 20 20 20 2a 2a access,. **
106a2 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c choose a defaul
106a3 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63 t page size in c
106a4 61 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63 ase we have to c
106a5 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 20 20 reate the.
106a6 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
106a7 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 . The default pa
106a8 67 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d ge size is the m
106a9 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 20 aximum of:.
106aa 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 **. **
106ab 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 + SQLITE_DEFAULT
106ac 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 _PAGE_SIZE,.
106ad 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 ** + The va
106ae 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
106af 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 sqlite3OsSectorS
106b0 69 7a 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 20 ize(). **
106b1 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 + The largest
106b2 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 page size that c
106b3 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 an be written at
106b4 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 20 20 omically..
106b5 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
106b6 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 =SQLITE_OK && !r
106b7 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 eadOnly ){.
106b8 20 20 20 69 6e 74 20 69 53 65 63 74 6f 72 53 69 int iSectorSi
106b9 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 ze = sqlite3OsSe
106ba 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d ctorSize(pPager-
106bb 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 >fd);. if
106bc 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 69 53 65 ( szPageDflt<iSe
106bd 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 20 ctorSize ){.
106be 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 szPageDflt
106bf 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a = iSectorSize;.
106c0 20 20 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 }.#ifdef
106c1 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 SQLITE_ENABLE_A
106c2 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 TOMIC_WRITE.
106c3 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 {.
106c4 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 int iDc = sqlite
106c5 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 3OsDeviceCharact
106c6 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d eristics(pPager-
106c7 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 >fd);.
106c8 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 int ii;.
106c9 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f assert(SQLITE_
106ca 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d IOCAP_ATOMIC512=
106cb 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 =(512>>8));.
106cc 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c assert(SQL
106cd 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
106ce 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 64K==(65536>>8))
106cf 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 ;. asse
106d0 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 rt(SQLITE_MAX_DE
106d1 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c FAULT_PAGE_SIZE<
106d2 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20 =65536);.
106d3 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 for(ii=szPage
106d4 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 Dflt; ii<=SQLITE
106d5 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
106d6 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 E_SIZE; ii=ii*2)
106d7 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 {. if
106d8 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f ( iDc&(SQLITE_IO
106d9 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e CAP_ATOMIC|(ii>>
106da 38 29 29 20 29 20 73 7a 50 61 67 65 44 66 6c 74 8)) ) szPageDflt
106db 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20 = ii;.
106dc 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e }. }.#en
106dd 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 28 20 dif. if(
106de 73 7a 50 61 67 65 44 66 6c 74 3e 53 51 4c 49 54 szPageDflt>SQLIT
106df 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
106e0 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 GE_SIZE ){.
106e1 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 szPageDflt
106e2 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 = SQLITE_MAX_DEF
106e3 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 0a AULT_PAGE_SIZE;.
106e4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
106e5 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b }. }. }else{
106e6 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 6d . /* If a tem
106e7 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 72 porary file is r
106e8 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 equested, it is
106e9 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 not opened immed
106ea 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 iately.. ** I
106eb 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 61 n this case we a
106ec 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75 6c ccept the defaul
106ed 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 t page size and
106ee 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 delay actually.
106ef 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 ** opening th
106f0 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 e file until the
106f1 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f first call to O
106f2 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a sWrite().. **
106f3 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 61 . ** This bra
106f4 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 nch is also run
106f5 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
106f6 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 6e database. An in
106f7 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 -memory. ** d
106f8 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 73 atabase is the s
106f9 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 69 ame as a temp-fi
106fa 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 72 le that is never
106fb 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a written out to.
106fc 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 ** disk and
106fd 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 uses an in-memor
106fe 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e y rollback journ
106ff 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 al.. */ .
10700 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 tempFile = 1;.
10701 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
10702 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 = PAGER_EXCLUSIV
10703 45 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 E;. }.. if( pP
10704 61 67 65 72 20 26 26 20 72 63 3d 3d 53 51 4c 49 ager && rc==SQLI
10705 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 TE_OK ){. pPa
10706 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d ger->pTmpSpace =
10707 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c sqlite3PageMall
10708 6f 63 28 73 7a 50 61 67 65 44 66 6c 74 29 3b 0a oc(szPageDflt);.
10709 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 }.. /* If an
1070a 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69 6e error occured in
1070b 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 62 either of the b
1070c 6c 6f 63 6b 73 20 61 62 6f 76 65 2e 0a 20 20 2a locks above.. *
1070d 2a 20 46 72 65 65 20 74 68 65 20 50 61 67 65 72 * Free the Pager
1070e 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 structure and c
1070f 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 lose the file..
10710 20 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 70 61 ** Since the pa
10711 67 65 72 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 63 ger is not alloc
10712 61 74 65 64 20 74 68 65 72 65 20 69 73 20 6e 6f ated there is no
10713 20 6e 65 65 64 20 74 6f 20 73 65 74 20 0a 20 20 need to set .
10714 2a 2a 20 61 6e 79 20 50 61 67 65 72 2e 65 72 72 ** any Pager.err
10715 4d 61 73 6b 20 76 61 72 69 61 62 6c 65 73 2e 0a Mask variables..
10716 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67 */. if( !pPag
10717 65 72 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 70 er || !pPager->p
10718 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 TmpSpace ){.
10719 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
1071a 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 Pager->fd);.
1071b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 sqlite3_free(pPa
1071c 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ger);. return
1071d 20 28 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b ((rc==SQLITE_OK
1071e 29 3f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3a 72 )?SQLITE_NOMEM:r
1071f 63 29 3b 0a 20 20 7d 0a 20 20 6e 45 78 74 72 61 c);. }. nExtra
10720 20 3d 20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d 45 = FORCE_ALIGNME
10721 4e 54 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 NT(nExtra);. sq
10722 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 lite3PcacheOpen(
10723 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 szPageDflt, nExt
10724 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 ra, !memDb,.
10725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10726 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65 !memDb?pagerStre
10727 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 ss:0, (void *)pP
10728 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 ager, pPager->pP
10729 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 Cache);.. PAGER
1072a 54 52 41 43 45 33 28 22 4f 50 45 4e 20 25 64 20 TRACE3("OPEN %d
1072b 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c %s\n", FILEHANDL
1072c 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c EID(pPager->fd),
1072d 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
1072e 6d 65 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 me);. IOTRACE((
1072f 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 "OPEN %p %s\n",
10730 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
10731 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 2f zFilename)).. /
10732 2a 20 46 69 6c 6c 20 69 6e 20 50 61 67 65 72 2e * Fill in Pager.
10733 7a 44 69 72 65 63 74 6f 72 79 5b 5d 20 2a 2f 0a zDirectory[] */.
10734 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d memcpy(pPager-
10735 3e 7a 44 69 72 65 63 74 6f 72 79 2c 20 70 50 61 >zDirectory, pPa
10736 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 ger->zFilename,
10737 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 nPathname+1);.
10738 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28 70 50 61 for(i=strlen(pPa
10739 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 29 ger->zDirectory)
1073a 3b 20 69 3e 30 20 26 26 20 70 50 61 67 65 72 2d ; i>0 && pPager-
1073b 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31 5d >zDirectory[i-1]
1073c 21 3d 27 2f 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 !='/'; i--){}.
1073d 69 66 28 20 69 3e 30 20 29 20 70 50 61 67 65 72 if( i>0 ) pPager
1073e 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31 ->zDirectory[i-1
1073f 5d 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 46 69 6c ] = 0;.. /* Fil
10740 6c 20 69 6e 20 50 61 67 65 72 2e 7a 4a 6f 75 72 l in Pager.zJour
10741 6e 61 6c 5b 5d 20 2a 2f 0a 20 20 69 66 28 20 7a nal[] */. if( z
10742 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 Pathname ){.
10743 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a memcpy(pPager->z
10744 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d Journal, pPager-
10745 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 >zFilename, nPat
10746 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 hname);. memc
10747 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 py(&pPager->zJou
10748 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c rnal[nPathname],
10749 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 39 29 3b "-journal", 9);
1074a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 . }else{. pP
1074b 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d ager->zJournal =
1074c 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 70 50 0;. }.. /* pP
1074d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
1074e 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 n = 0; */. pPag
1074f 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d er->useJournal =
10750 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 useJournal;. p
10751 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 Pager->noReadloc
10752 6b 20 3d 20 6e 6f 52 65 61 64 6c 6f 63 6b 20 26 k = noReadlock &
10753 26 20 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a & readOnly;. /*
10754 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 pPager->stmtOpe
10755 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 n = 0; */. /* p
10756 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 Pager->stmtInUse
10757 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
10758 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 ager->nRef = 0;
10759 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 */. pPager->dbS
1075a 69 7a 65 20 3d 20 6d 65 6d 44 62 2d 31 3b 0a 20 ize = memDb-1;.
1075b 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
1075c 65 20 3d 20 73 7a 50 61 67 65 44 66 6c 74 3b 0a e = szPageDflt;.
1075d 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d /* pPager->stm
1075e 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 tSize = 0; */.
1075f 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a /* pPager->stmtJ
10760 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f Size = 0; */. /
10761 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 67 65 20 * pPager->nPage
10762 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 = 0; */. pPager
10763 2d 3e 6d 78 50 61 67 65 20 3d 20 31 30 30 3b 0a ->mxPage = 100;.
10764 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f pPager->mxPgno
10765 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 = SQLITE_MAX_PA
10766 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 GE_COUNT;. /* p
10767 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
10768 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a AGER_UNLOCK; */.
10769 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
1076a 2d 3e 73 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 ->state == (temp
1076b 46 69 6c 65 20 3f 20 50 41 47 45 52 5f 45 58 43 File ? PAGER_EXC
1076c 4c 55 53 49 56 45 20 3a 20 50 41 47 45 52 5f 55 LUSIVE : PAGER_U
1076d 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70 NLOCK) );. /* p
1076e 50 61 67 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d Pager->errMask =
1076f 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 0; */. pPager-
10770 3e 74 65 6d 70 46 69 6c 65 20 3d 20 74 65 6d 70 >tempFile = temp
10771 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 File;. assert(
10772 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f tempFile==PAGER_
10773 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d LOCKINGMODE_NORM
10774 41 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c AL . ||
10775 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 tempFile==PAGER
10776 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 _LOCKINGMODE_EXC
10777 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 LUSIVE );. asse
10778 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e rt( PAGER_LOCKIN
10779 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d GMODE_EXCLUSIVE=
1077a 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e =1 );. pPager->
1077b 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 exclusiveMode =
1077c 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 70 50 61 tempFile; . pPa
1077d 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 6d 65 6d ger->memDb = mem
1077e 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 Db;. pPager->re
1077f 61 64 4f 6e 6c 79 20 3d 20 72 65 61 64 4f 6e 6c adOnly = readOnl
10780 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e y;. /* pPager->
10781 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 2a 2f needSync = 0; */
10782 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e . pPager->noSyn
10783 63 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 c = pPager->temp
10784 46 69 6c 65 20 7c 7c 20 21 75 73 65 4a 6f 75 72 File || !useJour
10785 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 nal;. pPager->f
10786 75 6c 6c 53 79 6e 63 20 3d 20 28 70 50 61 67 65 ullSync = (pPage
10787 72 2d 3e 6e 6f 53 79 6e 63 3f 30 3a 31 29 3b 0a r->noSync?0:1);.
10788 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 pPager->sync_f
10789 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 lags = SQLITE_SY
1078a 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 NC_NORMAL;. /*
1078b 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d pPager->pFirst =
1078c 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
1078d 65 72 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 er->pFirstSynced
1078e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
1078f 61 67 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b ager->pLast = 0;
10790 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 */. pPager->nE
10791 78 74 72 61 20 3d 20 6e 45 78 74 72 61 3b 0a 20 xtra = nExtra;.
10792 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
10793 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 SizeLimit = SQLI
10794 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e TE_DEFAULT_JOURN
10795 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 AL_SIZE_LIMIT;.
10796 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e assert(pPager->
10797 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 74 65 fd->pMethods||te
10798 6d 70 46 69 6c 65 29 3b 0a 20 20 73 65 74 53 65 mpFile);. setSe
10799 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 ctorSize(pPager)
1079a 3b 0a 20 20 69 66 28 20 6d 65 6d 44 62 20 29 7b ;. if( memDb ){
1079b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
1079c 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 rnalMode = PAGER
1079d 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
1079e 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 ORY;. }. /* pP
1079f 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c ager->pBusyHandl
107a0 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 er = 0; */. /*
107a1 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 memset(pPager->a
107a2 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Hash, 0, sizeof(
107a3 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b pPager->aHash));
107a4 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d */. *ppPager =
107a5 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 pPager;. retur
107a6 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
107a7 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 /*.** Set the bu
107a8 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 sy handler funct
107a9 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
107aa 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
107ab 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 te3PagerSetBusyh
107ac 61 6e 64 6c 65 72 28 50 61 67 65 72 20 2a 70 50 andler(Pager *pP
107ad 61 67 65 72 2c 20 42 75 73 79 48 61 6e 64 6c 65 ager, BusyHandle
107ae 72 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 29 r *pBusyHandler)
107af 7b 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75 73 {. pPager->pBus
107b0 79 48 61 6e 64 6c 65 72 20 3d 20 70 42 75 73 79 yHandler = pBusy
107b1 48 61 6e 64 6c 65 72 3b 0a 7d 0a 0a 2f 2a 0a 2a Handler;.}../*.*
107b2 2a 20 53 65 74 20 74 68 65 20 72 65 69 6e 69 74 * Set the reinit
107b3 69 61 6c 69 7a 65 72 20 66 6f 72 20 74 68 69 73 ializer for this
107b4 20 70 61 67 65 72 2e 20 20 49 66 20 6e 6f 74 20 pager. If not
107b5 4e 55 4c 4c 2c 20 74 68 65 20 72 65 69 6e 69 74 NULL, the reinit
107b6 69 61 6c 69 7a 65 72 0a 2a 2a 20 69 73 20 63 61 ializer.** is ca
107b7 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f lled when the co
107b8 6e 74 65 6e 74 20 6f 66 20 61 20 70 61 67 65 20 ntent of a page
107b9 69 6e 20 63 61 63 68 65 20 69 73 20 72 65 73 74 in cache is rest
107ba 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 ored to its orig
107bb 69 6e 61 6c 0a 2a 2a 20 76 61 6c 75 65 20 61 73 inal.** value as
107bc 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20 72 a result of a r
107bd 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 63 61 ollback. The ca
107be 6c 6c 62 61 63 6b 20 67 69 76 65 73 20 68 69 67 llback gives hig
107bf 68 65 72 2d 6c 65 76 65 6c 20 63 6f 64 65 0a 2a her-level code.*
107c0 2a 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 * an opportunity
107c1 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 to restore the
107c2 45 58 54 52 41 20 73 65 63 74 69 6f 6e 20 74 6f EXTRA section to
107c3 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65 20 agree with the
107c4 72 65 73 74 6f 72 65 64 0a 2a 2a 20 70 61 67 65 restored.** page
107c5 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 data..*/.SQLITE
107c6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
107c7 6c 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69 lite3PagerSetRei
107c8 6e 69 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 niter(Pager *pPa
107c9 67 65 72 2c 20 76 6f 69 64 20 28 2a 78 52 65 69 ger, void (*xRei
107ca 6e 69 74 29 28 44 62 50 61 67 65 2a 29 29 7b 0a nit)(DbPage*)){.
107cb 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 pPager->xReini
107cc 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a 7d ter = xReinit;.}
107cd 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
107ce 70 61 67 65 20 73 69 7a 65 20 74 6f 20 2a 70 50 page size to *pP
107cf 61 67 65 53 69 7a 65 2e 20 49 66 20 74 68 65 20 ageSize. If the
107d0 73 75 67 67 65 73 74 20 6e 65 77 20 70 61 67 65 suggest new page
107d1 20 73 69 7a 65 20 69 73 0a 2a 2a 20 69 6e 61 70 size is.** inap
107d2 70 72 6f 70 72 69 61 74 65 2c 20 74 68 65 6e 20 propriate, then
107d3 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 an alternative p
107d4 61 67 65 20 73 69 7a 65 20 69 73 20 73 65 74 20 age size is set
107d5 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 to that.** value
107d6 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
107d7 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
107d8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
107d9 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
107da 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
107db 75 31 36 20 2a 70 50 61 67 65 53 69 7a 65 29 7b u16 *pPageSize){
107dc 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 . int rc = pPag
107dd 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 69 er->errCode;. i
107de 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
107df 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65 ){. u16 page
107e0 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a Size = *pPageSiz
107e1 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e;. assert( p
107e2 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 ageSize==0 || (p
107e3 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 ageSize>=512 &&
107e4 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 pageSize<=SQLITE
107e5 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20 _MAX_PAGE_SIZE)
107e6 29 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 );. if( pageS
107e7 69 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 ize && pageSize!
107e8 3d 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a =pPager->pageSiz
107e9 65 20 0a 20 20 20 20 20 26 26 20 28 70 50 61 67 e . && (pPag
107ea 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 er->memDb==0 ||
107eb 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d pPager->dbSize==
107ec 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 0). && sqlit
107ed 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
107ee 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
107ef 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 )==0 . ){.
107f0 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20 char *pNew =
107f1 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 (char *)sqlite3P
107f2 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 ageMalloc(pageSi
107f3 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ze);. if( !
107f4 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
107f5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
107f6 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a M;. }else{.
107f7 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 pager_re
107f8 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 set(pPager);.
107f9 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67 pPager->pag
107fa 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 eSize = pageSize
107fb 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 ;. if( !p
107fc 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 20 73 Pager->memDb ) s
107fd 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 etSectorSize(pPa
107fe 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ger);. sq
107ff 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50 lite3PageFree(pP
10800 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 ager->pTmpSpace)
10801 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 ;. pPager
10802 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70 4e ->pTmpSpace = pN
10803 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ew;. sqli
10804 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 te3PcacheSetPage
10805 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 Size(pPager->pPC
10806 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b ache, pageSize);
10807 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
10808 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20 *pPageSize =
10809 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
1080a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1080b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
1080c 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
1080d 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79 20 70 the "temporary p
1080e 61 67 65 22 20 62 75 66 66 65 72 20 68 65 6c 64 age" buffer held
1080f 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 internally.** b
10810 79 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 y the pager. Th
10811 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 74 is is a buffer t
10812 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67 hat is big enoug
10813 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a h to hold the.**
10814 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 entire content
10815 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 of a database pa
10816 67 65 2e 20 20 54 68 69 73 20 62 75 66 66 65 72 ge. This buffer
10817 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 is used interna
10818 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f lly.** during ro
10819 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 llback and will
1081a 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 be overwritten w
1081b 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 henever a rollba
1081c 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 ck.** occurs. B
1081d 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 ut other modules
1081e 20 61 72 65 20 66 72 65 65 20 74 6f 20 75 73 65 are free to use
1081f 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 it too, as long
10820 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61 as.** no rollba
10821 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e cks are happenin
10822 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
10823 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
10824 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 e3PagerTempSpace
10825 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
10826 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 . return pPager
10827 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a ->pTmpSpace;.}..
10828 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f /*.** Attempt to
10829 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d set the maximum
1082a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63 database page c
1082b 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69 ount if mxPage i
1082c 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20 s positive. .**
1082d 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 Make no changes
1082e 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72 if mxPage is zer
1082f 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20 o or negative.
10830 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75 63 65 And never reduce
10831 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 the.** maximum
10832 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77 page count below
10833 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a the current siz
10834 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
10835 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c e..**.** Regardl
10836 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 ess of mxPage, r
10837 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e eturn the curren
10838 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 t maximum page c
10839 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ount..*/.SQLITE_
1083a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1083b 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 te3PagerMaxPageC
1083c 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 ount(Pager *pPag
1083d 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b er, int mxPage){
1083e 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20 . if( mxPage>0
1083f 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d ){. pPager->m
10840 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a xPgno = mxPage;.
10841 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 }. sqlite3Pag
10842 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
10843 65 72 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e er, 0);. return
10844 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b pPager->mxPgno;
10845 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f .}../*.** The fo
10846 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 llowing set of r
10847 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
10848 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20 to disable the
10849 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f simulated.** I/O
1084a 20 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d error mechanism
1084b 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
1084c 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 76 s are used to av
1084d 6f 69 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a oid simulated.**
1084e 20 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 errors in place
1084f 73 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f s where we do no
10850 74 20 63 61 72 65 20 61 62 6f 75 74 20 65 72 72 t care about err
10851 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 ors..**.** Unles
10852 73 20 2d 44 53 51 4c 49 54 45 5f 54 45 53 54 3d s -DSQLITE_TEST=
10853 31 20 69 73 20 75 73 65 64 2c 20 74 68 65 73 65 1 is used, these
10854 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c routines are al
10855 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 l no-ops.** and
10856 67 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 generate no code
10857 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
10858 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
10859 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 PI extern int sq
1085a 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
1085b 65 6e 64 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 ending;.SQLITE_A
1085c 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 PI extern int sq
1085d 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
1085e 69 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 it;.static int s
1085f 61 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 aved_cnt;.void d
10860 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 isable_simulated
10861 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 _io_errors(void)
10862 7b 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 {. saved_cnt =
10863 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
10864 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 _pending;. sqli
10865 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
10866 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 ding = -1;.}.voi
10867 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 d enable_simulat
10868 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 ed_io_errors(voi
10869 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f d){. sqlite3_io
1086a 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d _error_pending =
1086b 20 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 saved_cnt;.}.#e
1086c 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 lse.# define dis
1086d 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
1086e 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 o_errors().# def
1086f 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c ine enable_simul
10870 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
10871 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
10872 65 61 64 20 74 68 65 20 66 69 72 73 74 20 4e 20 ead the first N
10873 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 bytes from the b
10874 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
10875 66 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 file into memory
10876 0a 2a 2a 20 74 68 61 74 20 70 44 65 73 74 20 70 .** that pDest p
10877 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a oints to. .**.**
10878 20 4e 6f 20 65 72 72 6f 72 20 63 68 65 63 6b 69 No error checki
10879 6e 67 20 69 73 20 64 6f 6e 65 2e 20 54 68 65 20 ng is done. The
1087a 72 61 74 69 6f 6e 61 6c 20 66 6f 72 20 74 68 69 rational for thi
1087b 73 20 69 73 20 74 68 61 74 20 74 68 69 73 20 66 s is that this f
1087c 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 6d 61 79 20 unction .** may
1087d 62 65 20 63 61 6c 6c 65 64 20 65 76 65 6e 20 69 be called even i
1087e 66 20 74 68 65 20 66 69 6c 65 20 64 6f 65 73 20 f the file does
1087f 6e 6f 74 20 65 78 69 73 74 20 6f 72 20 63 6f 6e not exist or con
10880 74 61 69 6e 20 61 20 68 65 61 64 65 72 2e 20 49 tain a header. I
10881 6e 20 0a 2a 2a 20 74 68 65 73 65 20 63 61 73 65 n .** these case
10882 73 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 s sqlite3OsRead(
10883 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 6e ) will return an
10884 20 65 72 72 6f 72 2c 20 74 6f 20 77 68 69 63 68 error, to which
10885 20 74 68 65 20 63 6f 72 72 65 63 74 20 0a 2a 2a the correct .**
10886 20 72 65 73 70 6f 6e 73 65 20 69 73 20 74 6f 20 response is to
10887 7a 65 72 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 zero the memory
10888 61 74 20 70 44 65 73 74 20 61 6e 64 20 63 6f 6e at pDest and con
10889 74 69 6e 75 65 2e 20 20 41 20 72 65 61 6c 20 49 tinue. A real I
1088a 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 77 69 6c 6c O error .** will
1088b 20 70 72 65 73 75 6d 61 62 6c 79 20 72 65 63 75 presumably recu
1088c 72 20 61 6e 64 20 62 65 20 70 69 63 6b 65 64 20 r and be picked
1088d 75 70 20 6c 61 74 65 72 20 28 54 6f 64 6f 3a 20 up later (Todo:
1088e 54 68 69 6e 6b 20 61 62 6f 75 74 20 74 68 69 73 Think about this
1088f 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
10890 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
10891 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 PagerReadFilehea
10892 64 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 der(Pager *pPage
10893 72 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e r, int N, unsign
10894 65 64 20 63 68 61 72 20 2a 70 44 65 73 74 29 7b ed char *pDest){
10895 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
10896 54 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 TE_OK;. memset(
10897 70 44 65 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 pDest, 0, N);.
10898 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66 assert(pPager->f
10899 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 d->pMethods||pPa
1089a 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a ger->tempFile);.
1089b 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 64 if( pPager->fd
1089c 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 ->pMethods ){.
1089d 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 48 44 IOTRACE(("DBHD
1089e 52 20 25 70 20 30 20 25 64 5c 6e 22 2c 20 70 50 R %p 0 %d\n", pP
1089f 61 67 65 72 2c 20 4e 29 29 0a 20 20 20 20 72 63 ager, N)). rc
108a0 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
108a1 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 65 (pPager->fd, pDe
108a2 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 20 69 st, N, 0);. i
108a3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f f( rc==SQLITE_IO
108a4 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 ERR_SHORT_READ )
108a5 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
108a6 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
108a7 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
108a8 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
108a9 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
108aa 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
108ab 64 69 73 6b 20 66 69 6c 65 20 61 73 73 6f 63 69 disk file associ
108ac 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 70 50 61 ated with.** pPa
108ad 67 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ger. .**.** If t
108ae 68 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 he PENDING_BYTE
108af 6c 69 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 lies on the page
108b0 20 64 69 72 65 63 74 6c 79 20 61 66 74 65 72 20 directly after
108b1 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a the end of the.*
108b2 2a 20 66 69 6c 65 2c 20 74 68 65 6e 20 63 6f 6e * file, then con
108b3 73 69 64 65 72 20 74 68 69 73 20 70 61 67 65 20 sider this page
108b4 70 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 part of the file
108b5 20 74 6f 6f 2e 20 46 6f 72 20 65 78 61 6d 70 6c too. For exampl
108b6 65 2c 20 69 66 0a 2a 2a 20 50 45 4e 44 49 4e 47 e, if.** PENDING
108b7 5f 42 59 54 45 20 69 73 20 62 79 74 65 20 34 30 _BYTE is byte 40
108b8 39 36 20 28 74 68 65 20 66 69 72 73 74 20 62 79 96 (the first by
108b9 74 65 20 6f 66 20 70 61 67 65 20 35 29 20 61 6e te of page 5) an
108ba 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 d the size of th
108bb 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 34 30 39 e.** file is 409
108bc 36 20 62 79 74 65 73 2c 20 35 20 69 73 20 72 65 6 bytes, 5 is re
108bd 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 20 6f turned instead o
108be 66 20 34 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 f 4..*/.SQLITE_P
108bf 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
108c0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
108c1 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
108c2 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 int *pnPage){.
108c3 69 36 34 20 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 i64 n = 0;. int
108c4 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 rc;. assert( p
108c5 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 69 66 Pager!=0 );. if
108c6 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
108c7 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 50 e ){. rc = pP
108c8 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
108c9 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
108ca 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e }. if( pPager->
108cb 64 62 53 69 7a 65 3e 3d 30 20 29 7b 0a 20 20 20 dbSize>=0 ){.
108cc 20 6e 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 n = pPager->dbS
108cd 69 7a 65 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a ize;. } else {.
108ce 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 assert(pPage
108cf 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c r->fd->pMethods|
108d0 7c 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c |pPager->tempFil
108d1 65 29 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 e);. if( (pPa
108d2 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 ger->fd->pMethod
108d3 73 29 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d s). && (rc =
108d4 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
108d5 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 ze(pPager->fd, &
108d6 6e 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 n))!=SQLITE_OK )
108d7 7b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 {. pager_er
108d8 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b ror(pPager, rc);
108d9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
108da 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
108db 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d n>0 && n<pPager-
108dc 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 >pageSize ){.
108dd 20 20 20 6e 20 3d 20 31 3b 0a 20 20 20 20 7d 65 n = 1;. }e
108de 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20 2f 3d 20 lse{. n /=
108df 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
108e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
108e1 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 pPager->state!=P
108e2 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 AGER_UNLOCK ){.
108e3 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 pPager->dbS
108e4 69 7a 65 20 3d 20 6e 3b 0a 20 20 20 20 7d 0a 20 ize = n;. }.
108e5 20 7d 0a 20 20 69 66 28 20 6e 3d 3d 28 50 45 4e }. if( n==(PEN
108e6 44 49 4e 47 5f 42 59 54 45 2f 70 50 61 67 65 72 DING_BYTE/pPager
108e7 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 7b 0a 20 ->pageSize) ){.
108e8 20 20 20 6e 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 n++;. }. if
108e9 28 20 6e 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 ( n>pPager->mxPg
108ea 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 no ){. pPager
108eb 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6e 3b 0a 20 20 ->mxPgno = n;.
108ec 7d 0a 20 20 69 66 28 20 70 6e 50 61 67 65 20 29 }. if( pnPage )
108ed 7b 0a 20 20 20 20 2a 70 6e 50 61 67 65 20 3d 20 {. *pnPage =
108ee 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 n;. }. return
108ef 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
108f0 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c .** Forward decl
108f1 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 aration.*/.stati
108f2 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 c int syncJourna
108f3 6c 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 0a 2a l(Pager*);../*.*
108f4 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
108f5 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61 s used to trunca
108f6 74 65 20 74 68 65 20 63 61 63 68 65 20 77 68 65 te the cache whe
108f7 6e 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 n a database.**
108f8 69 73 20 74 72 75 6e 63 61 74 65 64 2e 20 20 44 is truncated. D
108f9 72 6f 70 20 66 72 6f 6d 20 74 68 65 20 63 61 63 rop from the cac
108fa 68 65 20 61 6c 6c 20 70 61 67 65 73 20 77 68 6f he all pages who
108fb 73 65 20 70 67 6e 6f 20 69 73 0a 2a 2a 20 6c 61 se pgno is.** la
108fc 72 67 65 72 20 74 68 61 6e 20 70 50 61 67 65 72 rger than pPager
108fd 2d 3e 64 62 53 69 7a 65 20 61 6e 64 20 69 73 20 ->dbSize and is
108fe 75 6e 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a 2a unreferenced..**
108ff 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 64 20 70 .** Referenced p
10900 61 67 65 73 20 6c 61 72 67 65 72 20 74 68 61 6e ages larger than
10901 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 pPager->dbSize
10902 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a are zeroed..**.*
10903 2a 20 41 63 74 75 61 6c 6c 79 2c 20 61 74 20 74 * Actually, at t
10904 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f he point this ro
10905 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
10906 20 69 74 20 77 6f 75 6c 64 20 62 65 0a 2a 2a 20 it would be.**
10907 61 6e 20 65 72 72 6f 72 20 74 6f 20 68 61 76 65 an error to have
10908 20 61 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 a referenced pa
10909 67 65 2e 20 20 42 75 74 20 72 61 74 68 65 72 20 ge. But rather
1090a 74 68 61 6e 20 64 65 6c 65 74 65 0a 2a 2a 20 74 than delete.** t
1090b 68 61 74 20 70 61 67 65 20 61 6e 64 20 67 75 61 hat page and gua
1090c 72 61 6e 74 65 65 20 61 20 73 75 62 73 65 71 75 rantee a subsequ
1090d 65 6e 74 20 73 65 67 66 61 75 6c 74 2c 20 69 74 ent segfault, it
1090e 20 73 65 65 6d 73 20 62 65 74 74 65 72 0a 2a 2a seems better.**
1090f 20 74 6f 20 7a 65 72 6f 20 69 74 20 61 6e 64 20 to zero it and
10910 68 6f 70 65 20 74 68 61 74 20 77 65 20 65 72 72 hope that we err
10911 6f 72 20 6f 75 74 20 73 61 6e 65 6c 79 2e 0a 2a or out sanely..*
10912 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 /.static void pa
10913 67 65 72 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 ger_truncate_cac
10914 68 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 he(Pager *pPager
10915 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 ){. sqlite3Pcac
10916 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 heTruncate(pPage
10917 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61 67 r->pPCache, pPag
10918 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 7d 0a 0a er->dbSize);.}..
10919 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 /*.** Try to obt
1091a 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 ain a lock on a
1091b 66 69 6c 65 2e 20 20 49 6e 76 6f 6b 65 20 74 68 file. Invoke th
1091c 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 e busy callback
1091d 69 66 20 74 68 65 20 6c 6f 63 6b 0a 2a 2a 20 69 if the lock.** i
1091e 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 s currently not
1091f 61 76 61 69 6c 61 62 6c 65 2e 20 20 52 65 70 65 available. Repe
10920 61 74 20 75 6e 74 69 6c 20 74 68 65 20 62 75 73 at until the bus
10921 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 y callback retur
10922 6e 73 0a 2a 2a 20 66 61 6c 73 65 20 6f 72 20 75 ns.** false or u
10923 6e 74 69 6c 20 74 68 65 20 6c 6f 63 6b 20 73 75 ntil the lock su
10924 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 cceeds..**.** Re
10925 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
10926 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e n success and an
10927 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 error code if w
10928 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a e cannot obtain.
10929 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2f 0a ** the lock..*/.
1092a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
1092b 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 _wait_on_lock(Pa
1092c 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
1092d 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e locktype){. in
1092e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 t rc;.. /* The
1092f 4f 53 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 6d OS lock values m
10930 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 ust be the same
10931 61 73 20 74 68 65 20 50 61 67 65 72 20 6c 6f 63 as the Pager loc
10932 6b 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 61 73 k values */. as
10933 73 65 72 74 28 20 50 41 47 45 52 5f 53 48 41 52 sert( PAGER_SHAR
10934 45 44 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ED==SHARED_LOCK
10935 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
10936 45 52 5f 52 45 53 45 52 56 45 44 3d 3d 52 45 53 ER_RESERVED==RES
10937 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 ERVED_LOCK );.
10938 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 45 58 assert( PAGER_EX
10939 43 4c 55 53 49 56 45 3d 3d 45 58 43 4c 55 53 49 CLUSIVE==EXCLUSI
1093a 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a VE_LOCK );.. /*
1093b 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 If the file is
1093c 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b currently unlock
1093d 65 64 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 ed then the size
1093e 20 6d 75 73 74 20 62 65 20 75 6e 6b 6e 6f 77 6e must be unknown
1093f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
10940 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
10941 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 61 ER_SHARED || pPa
10942 67 65 72 2d 3e 64 62 53 69 7a 65 3c 30 20 29 3b ger->dbSize<0 );
10943 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e .. if( pPager->
10944 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 state>=locktype
10945 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
10946 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
10947 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
10948 70 42 75 73 79 48 61 6e 64 6c 65 72 20 29 20 70 pBusyHandler ) p
10949 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 Pager->pBusyHand
1094a 6c 65 72 2d 3e 6e 42 75 73 79 20 3d 20 30 3b 0a ler->nBusy = 0;.
1094b 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 do {. r
1094c 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 c = sqlite3OsLoc
1094d 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f k(pPager->fd, lo
1094e 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 cktype);. }wh
1094f 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
10950 42 55 53 59 20 26 26 20 73 71 6c 69 74 65 33 49 BUSY && sqlite3I
10951 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
10952 28 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 (pPager->pBusyHa
10953 6e 64 6c 65 72 29 20 29 3b 0a 20 20 20 20 69 66 ndler) );. if
10954 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
10955 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
10956 3e 73 74 61 74 65 20 3d 20 6c 6f 63 6b 74 79 70 >state = locktyp
10957 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 e;. IOTRACE
10958 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 (("LOCK %p %d\n"
10959 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 , pPager, lockty
1095a 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 pe)). }. }.
1095b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1095c 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 *.** Truncate th
1095d 65 20 66 69 6c 65 20 74 6f 20 74 68 65 20 6e 75 e file to the nu
1095e 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 70 mber of pages sp
1095f 65 63 69 66 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ecified..*/.SQLI
10960 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
10961 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 qlite3PagerTrunc
10962 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ate(Pager *pPage
10963 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a r, Pgno nPage){.
10964 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
10965 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
10966 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
10967 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a AGER_SHARED );..
10968 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 sqlite3PagerPa
10969 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 gecount(pPager,
1096a 30 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 0);. if( pPager
1096b 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 ->errCode ){.
1096c 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 rc = pPager->er
1096d 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 20 69 rCode;. }else i
1096e 66 28 20 6e 50 61 67 65 3c 28 75 6e 73 69 67 6e f( nPage<(unsign
1096f 65 64 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a ed)pPager->dbSiz
10970 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 79 e ){. rc = sy
10971 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 ncJournal(pPager
10972 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
10973 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
10974 20 20 2f 2a 20 47 65 74 20 61 6e 20 65 78 63 6c /* Get an excl
10975 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 usive lock on th
10976 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 e database befor
10977 65 20 74 72 75 6e 63 61 74 69 6e 67 2e 20 2a 2f e truncating. */
10978 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
10979 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 r_wait_on_lock(p
1097a 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 Pager, EXCLUSIVE
1097b 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 _LOCK);. }.
1097c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1097d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
1097e 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 = pager_truncate
1097f 28 70 50 61 67 65 72 2c 20 6e 50 61 67 65 29 3b (pPager, nPage);
10980 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 . }. }.. re
10981 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
10982 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70 * Shutdown the p
10983 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 65 age cache. Free
10984 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20 all memory and
10985 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e close all files.
10986 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e .**.** If a tran
10987 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70 saction was in p
10988 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 69 rogress when thi
10989 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
1098a 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61 led, that.** tra
1098b 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c nsaction is roll
1098c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75 ed back. All ou
1098d 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 tstanding pages
1098e 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a are invalidated.
1098f 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d ** and their mem
10990 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 41 ory is freed. A
10991 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ny attempt to us
10992 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 61 e a page associa
10993 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73 ted.** with this
10994 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74 65 page cache afte
10995 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 r this function
10996 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b returns will lik
10997 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e ely.** result in
10998 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a a coredump..**.
10999 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1099a 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 always succeeds
1099b 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 . If a transacti
1099c 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e 20 on is active an
1099d 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 attempt.** is ma
1099e 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 de to roll it ba
1099f 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 ck. If an error
109a0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 occurs during th
109a1 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 e rollback .** a
109a2 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 hot journal may
109a3 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20 be left in the
109a4 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e filesystem but n
109a5 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 o error is retur
109a6 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 ned.** to the ca
109a7 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ller..*/.SQLITE_
109a8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
109a9 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 te3PagerClose(Pa
109aa 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 ger *pPager){..
109ab 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 disable_simulat
109ac 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a ed_io_errors();.
109ad 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
109ae 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
109af 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
109b0 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 = 0;. pPager->e
109b1 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 30 xclusiveMode = 0
109b2 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 ;. pager_reset(
109b3 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 pPager);. if( !
109b4 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 61 67 MEMDB ){. pag
109b5 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 erUnlockAndRollb
109b6 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ack(pPager);. }
109b7 0a 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 . enable_simula
109b8 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b ted_io_errors();
109b9 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e . sqlite3EndBen
109ba 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 50 ignMalloc();. P
109bb 41 47 45 52 54 52 41 43 45 32 28 22 43 4c 4f 53 AGERTRACE2("CLOS
109bc 45 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 E %d\n", PAGERID
109bd 28 70 50 61 67 65 72 29 29 3b 0a 20 20 49 4f 54 (pPager));. IOT
109be 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c RACE(("CLOSE %p\
109bf 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 69 n", pPager)). i
109c0 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
109c1 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 73 71 alOpen ){. sq
109c2 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
109c3 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 7d 0a 20 ger->jfd);. }.
109c4 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
109c5 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 stroy(pPager->pI
109c6 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 73 71 6c nJournal);. sql
109c7 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
109c8 79 28 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 y(pPager->pAlway
109c9 73 52 6f 6c 6c 62 61 63 6b 29 3b 0a 20 20 69 66 sRollback);. if
109ca 28 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 ( pPager->stmtOp
109cb 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 en ){. sqlite
109cc 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
109cd 3e 73 74 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 >stfd);. }. sq
109ce 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
109cf 67 65 72 2d 3e 66 64 29 3b 0a 20 20 2f 2a 20 54 ger->fd);. /* T
109d0 65 6d 70 20 66 69 6c 65 73 20 61 72 65 20 61 75 emp files are au
109d1 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 tomatically dele
109d2 74 65 64 20 62 79 20 74 68 65 20 4f 53 0a 20 20 ted by the OS.
109d3 2a 2a 20 69 66 28 20 70 50 61 67 65 72 2d 3e 74 ** if( pPager->t
109d4 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 2a 2a 20 empFile ){. **
109d5 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 sqlite3OsDelet
109d6 65 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e e(pPager->zFilen
109d7 61 6d 65 29 3b 0a 20 20 2a 2a 20 7d 0a 20 20 2a ame);. ** }. *
109d8 2f 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 /.. sqlite3Page
109d9 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d Free(pPager->pTm
109da 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 pSpace);. sqlit
109db 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 70 50 e3PcacheClose(pP
109dc 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
109dd 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
109de 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e Pager);. return
109df 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 SQLITE_OK;.}..#
109e0 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
109e1 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 UG) || defined(S
109e2 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a QLITE_TEST)./*.*
109e3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 * Return the pag
109e4 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 e number for the
109e5 20 67 69 76 65 6e 20 70 61 67 65 20 64 61 74 61 given page data
109e6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
109e7 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 ATE Pgno sqlite3
109e8 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 PagerPagenumber(
109e9 44 62 50 61 67 65 20 2a 70 29 7b 0a 20 20 72 65 DbPage *p){. re
109ea 74 75 72 6e 20 70 2d 3e 70 67 6e 6f 3b 0a 7d 0a turn p->pgno;.}.
109eb 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
109ec 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 crement the refe
109ed 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 rence count for
109ee 61 20 70 61 67 65 2e 20 20 54 68 65 20 69 6e 70 a page. The inp
109ef 75 74 20 70 6f 69 6e 74 65 72 20 69 73 0a 2a 2a ut pointer is.**
109f0 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
109f1 74 68 65 20 70 61 67 65 20 64 61 74 61 2e 0a 2a the page data..*
109f2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
109f3 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
109f4 72 52 65 66 28 44 62 50 61 67 65 20 2a 70 50 67 rRef(DbPage *pPg
109f5 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 ){. sqlite3Pcac
109f6 68 65 52 65 66 28 70 50 67 29 3b 0a 20 20 72 65 heRef(pPg);. re
109f7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
109f8 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 }../*.** Sync th
109f9 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6f e journal. In o
109fa 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 ther words, make
109fb 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 70 61 sure all the pa
109fc 67 65 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a ges that have.**
109fd 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
109fe 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76 the journal hav
109ff 65 20 61 63 74 75 61 6c 6c 79 20 72 65 61 63 68 e actually reach
10a00 65 64 20 74 68 65 20 73 75 72 66 61 63 65 20 6f ed the surface o
10a01 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b 2e 20 20 f the.** disk.
10a02 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 It is not safe t
10a03 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 6f 72 69 o modify the ori
10a04 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 ginal database f
10a05 69 6c 65 20 75 6e 74 69 6c 20 61 66 74 65 72 0a ile until after.
10a06 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 ** the journal h
10a07 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 20 as been synced.
10a08 20 49 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c If the original
10a09 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 database is mod
10a0a 69 66 69 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 ified before.**
10a0b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 the journal is s
10a0c 79 6e 63 65 64 20 61 6e 64 20 61 20 70 6f 77 65 ynced and a powe
10a0d 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 r failure occurs
10a0e 2c 20 74 68 65 20 75 6e 73 79 6e 63 65 64 20 6a , the unsynced j
10a0f 6f 75 72 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 77 ournal.** data w
10a10 6f 75 6c 64 20 62 65 20 6c 6f 73 74 20 61 6e 64 ould be lost and
10a11 20 77 65 20 77 6f 75 6c 64 20 62 65 20 75 6e 61 we would be una
10a12 62 6c 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c ble to completel
10a13 79 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 0a 2a y rollback the.*
10a14 2a 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67 * database chang
10a15 65 73 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f es. Database co
10a16 72 72 75 70 74 69 6f 6e 20 77 6f 75 6c 64 20 6f rruption would o
10a17 63 63 75 72 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69 ccur..** .** Thi
10a18 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 75 s routine also u
10a19 70 64 61 74 65 73 20 74 68 65 20 6e 52 65 63 20 pdates the nRec
10a1a 66 69 65 6c 64 20 69 6e 20 74 68 65 20 68 65 61 field in the hea
10a1b 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e der of the journ
10a1c 61 6c 2e 0a 2a 2a 20 28 53 65 65 20 63 6f 6d 6d al..** (See comm
10a1d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 61 67 65 ents on the page
10a1e 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 72 6f 75 r_playback() rou
10a1f 74 69 6e 65 20 66 6f 72 20 61 64 64 69 74 69 6f tine for additio
10a20 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
10a21 29 0a 2a 2a 20 49 66 20 74 68 65 20 73 79 6e 63 ).** If the sync
10a22 20 6d 6f 64 65 20 69 73 20 46 55 4c 4c 2c 20 74 mode is FULL, t
10a23 77 6f 20 73 79 6e 63 73 20 77 69 6c 6c 20 6f 63 wo syncs will oc
10a24 63 75 72 2e 20 20 46 69 72 73 74 20 74 68 65 20 cur. First the
10a25 77 68 6f 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a whole journal.**
10a26 20 69 73 20 73 79 6e 63 65 64 2c 20 74 68 65 6e is synced, then
10a27 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 the nRec field
10a28 69 73 20 75 70 64 61 74 65 64 2c 20 74 68 65 6e is updated, then
10a29 20 61 20 73 65 63 6f 6e 64 20 73 79 6e 63 20 6f a second sync o
10a2a 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ccurs..**.** For
10a2b 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 temporary datab
10a2c 61 73 65 73 2c 20 77 65 20 64 6f 20 6e 6f 74 20 ases, we do not
10a2d 63 61 72 65 20 69 66 20 77 65 20 61 72 65 20 61 care if we are a
10a2e 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 0a ble to rollback.
10a2f 2a 2a 20 61 66 74 65 72 20 61 20 70 6f 77 65 72 ** after a power
10a30 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 6e 6f 20 failure, so no
10a31 73 79 6e 63 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a sync occurs..**.
10a32 2a 2a 20 49 66 20 74 68 65 20 49 4f 43 41 50 5f ** If the IOCAP_
10a33 53 45 51 55 45 4e 54 49 41 4c 20 66 6c 61 67 20 SEQUENTIAL flag
10a34 69 73 20 73 65 74 20 66 6f 72 20 74 68 65 20 70 is set for the p
10a35 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69 61 20 ersistent media
10a36 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 on which.** the
10a37 64 61 74 61 62 61 73 65 20 69 73 20 73 74 6f 72 database is stor
10a38 65 64 2c 20 74 68 65 6e 20 4f 73 53 79 6e 63 28 ed, then OsSync(
10a39 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 ) is never calle
10a3a 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c d on the journal
10a3b 0a 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69 .** file. In thi
10a3c 73 20 63 61 73 65 20 61 6c 6c 20 74 68 61 74 20 s case all that
10a3d 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74 is required is t
10a3e 6f 20 75 70 64 61 74 65 20 74 68 65 20 6e 52 65 o update the nRe
10a3f 63 20 66 69 65 6c 64 20 69 6e 0a 2a 2a 20 74 68 c field in.** th
10a40 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
10a41 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
10a42 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65 20 tine clears the
10a43 6e 65 65 64 53 79 6e 63 20 66 69 65 6c 64 20 6f needSync field o
10a44 66 20 65 76 65 72 79 20 70 61 67 65 20 63 75 72 f every page cur
10a45 72 65 6e 74 20 68 65 6c 64 20 69 6e 0a 2a 2a 20 rent held in.**
10a46 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 memory..*/.stati
10a47 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 c int syncJourna
10a48 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 l(Pager *pPager)
10a49 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
10a4a 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 53 79 ITE_OK;.. /* Sy
10a4b 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 nc the journal b
10a4c 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20 efore modifying
10a4d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
10a4e 65 0a 20 20 2a 2a 20 28 61 73 73 75 6d 69 6e 67 e. ** (assuming
10a4f 20 74 68 65 72 65 20 69 73 20 61 20 6a 6f 75 72 there is a jour
10a50 6e 61 6c 20 61 6e 64 20 69 74 20 6e 65 65 64 73 nal and it needs
10a51 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 29 0a to be synced.).
10a52 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 */. if( pPage
10a53 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 r->needSync ){.
10a54 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 assert( !pPag
10a55 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a er->tempFile );.
10a56 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
10a57 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 journalMode!=PAG
10a58 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
10a59 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 69 EMORY ){. i
10a5a 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 nt iDc = sqlite3
10a5b 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
10a5c 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e ristics(pPager->
10a5d 66 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 fd);. asser
10a5e 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e t( pPager->journ
10a5f 61 6c 4f 70 65 6e 20 29 3b 0a 0a 20 20 20 20 20 alOpen );..
10a60 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c if( 0==(iDc&SQL
10a61 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
10a62 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 PPEND) ){.
10a63 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e /* Write the n
10a64 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 Rec value into t
10a65 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
10a66 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a 20 20 header. If in.
10a67 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 ** full-sy
10a68 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 nchronous mode,
10a69 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c sync the journal
10a6a 20 66 69 72 73 74 2e 20 54 68 69 73 20 65 6e 73 first. This ens
10a6b 75 72 65 73 20 74 68 61 74 0a 20 20 20 20 20 20 ures that.
10a6c 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 ** all data ha
10a6d 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74 68 65 s really hit the
10a6e 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 disk before nRe
10a6f 63 20 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 c is updated to
10a70 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20 mark. **
10a71 69 74 20 61 73 20 61 20 63 61 6e 64 69 64 61 74 it as a candidat
10a72 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a e for rollback..
10a73 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
10a74 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f ** This is no
10a75 74 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68 t required if th
10a76 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d 65 64 e persistent med
10a77 69 61 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a ia supports the.
10a78 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46 45 5f ** SAFE_
10a79 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2e APPEND property.
10a7a 20 42 65 63 61 75 73 65 20 69 6e 20 74 68 69 73 Because in this
10a7b 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 case it is not
10a7c 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20 20 20 possible .
10a7d 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61 67 65 ** for garbage
10a7e 20 64 61 74 61 20 74 6f 20 62 65 20 61 70 70 65 data to be appe
10a7f 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69 6c 65 nded to the file
10a80 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 , the nRec field
10a81 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 70 . ** is p
10a82 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 30 78 opulated with 0x
10a83 46 46 46 46 46 46 46 46 20 77 68 65 6e 20 74 68 FFFFFFFF when th
10a84 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
10a85 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20 20 20 is written.
10a86 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72 ** and never
10a87 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 needs to be upd
10a88 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f ated.. */
10a89 0a 20 20 20 20 20 20 20 20 69 36 34 20 6a 72 6e . i64 jrn
10a8a 6c 4f 66 66 3b 0a 20 20 20 20 20 20 20 20 69 66 lOff;. if
10a8b 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 ( pPager->fullSy
10a8c 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26 53 51 nc && 0==(iDc&SQ
10a8d 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 LITE_IOCAP_SEQUE
10a8e 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 NTIAL) ){.
10a8f 20 20 20 20 50 41 47 45 52 54 52 41 43 45 32 28 PAGERTRACE2(
10a90 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 "SYNC journal of
10a91 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
10a92 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 pPager));.
10a93 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 IOTRACE(("JS
10a94 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 YNC %p\n", pPage
10a95 72 29 29 0a 20 20 20 20 20 20 20 20 20 20 72 63 r)). rc
10a96 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
10a97 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
10a98 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 ager->sync_flags
10a99 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
10a9a 20 72 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 rc!=0 ) return
10a9b 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 rc;. }..
10a9c 20 20 20 20 20 20 20 6a 72 6e 6c 4f 66 66 20 3d jrnlOff =
10a9d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
10a9e 48 64 72 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f Hdr + sizeof(aJo
10a9f 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20 20 20 urnalMagic);.
10aa0 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a IOTRACE(("J
10aa1 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e HDR %p %lld %d\n
10aa2 22 2c 20 70 50 61 67 65 72 2c 20 6a 72 6e 6c 4f ", pPager, jrnlO
10aa3 66 66 2c 20 34 29 29 3b 0a 20 20 20 20 20 20 20 ff, 4));.
10aa4 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 rc = write32bit
10aa5 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a s(pPager->jfd, j
10aa6 72 6e 6c 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e rnlOff, pPager->
10aa7 6e 52 65 63 29 3b 0a 20 20 20 20 20 20 20 20 69 nRec);. i
10aa8 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
10aa9 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
10aaa 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c if( 0==(iDc&SQL
10aab 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e ITE_IOCAP_SEQUEN
10aac 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 TIAL) ){.
10aad 20 50 41 47 45 52 54 52 41 43 45 32 28 22 53 59 PAGERTRACE2("SY
10aae 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 NC journal of %d
10aaf 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
10ab0 67 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20 49 ger));. I
10ab1 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 OTRACE(("JSYNC %
10ab2 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 p\n", pPager)).
10ab3 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
10ab4 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 te3OsSync(pPager
10ab5 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 ->jfd, pPager->s
10ab6 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20 20 20 20 ync_flags| .
10ab7 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 73 (pPager->s
10ab8 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 54 ync_flags==SQLIT
10ab9 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c 49 E_SYNC_FULL?SQLI
10aba 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
10abb 3a 30 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 :0). );.
10abc 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 30 if( rc!=0
10abd 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
10abe 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 }. pPag
10abf 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 er->journalStart
10ac0 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ed = 1;. }.
10ac1 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 pPager->needSy
10ac2 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 nc = 0;.. /*
10ac3 45 72 61 73 65 20 74 68 65 20 6e 65 65 64 53 79 Erase the needSy
10ac4 6e 63 20 66 6c 61 67 20 66 72 6f 6d 20 65 76 65 nc flag from eve
10ac5 72 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a ry page.. */.
10ac6 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
10ac7 65 43 6c 65 61 72 46 6c 61 67 73 28 70 50 61 67 eClearFlags(pPag
10ac8 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 50 47 48 er->pPCache, PGH
10ac9 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 DR_NEED_SYNC);.
10aca 20 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 }..#ifndef NDEB
10acb 55 47 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 50 UG. /* If the P
10acc 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c ager.needSync fl
10acd 61 67 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e ag is clear then
10ace 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 the PgHdr.needS
10acf 79 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20 6d 75 ync. ** flag mu
10ad0 73 74 20 61 6c 73 6f 20 62 65 20 63 6c 65 61 72 st also be clear
10ad1 20 66 6f 72 20 61 6c 6c 20 70 61 67 65 73 2e 20 for all pages.
10ad2 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 69 Verify that thi
10ad3 73 0a 20 20 2a 2a 20 69 6e 76 61 72 69 61 6e 74 s. ** invariant
10ad4 20 69 73 20 74 72 75 65 2e 0a 20 20 2a 2f 0a 20 is true.. */.
10ad5 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 else{. sqlit
10ad6 65 33 50 63 61 63 68 65 41 73 73 65 72 74 46 6c e3PcacheAssertFl
10ad7 61 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ags(pPager->pPCa
10ad8 63 68 65 2c 20 30 2c 20 50 47 48 44 52 5f 4e 45 che, 0, PGHDR_NE
10ad9 45 44 5f 53 59 4e 43 29 3b 0a 20 20 7d 0a 23 65 ED_SYNC);. }.#e
10ada 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 ndif.. return r
10adb 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 c;.}../*.** Give
10adc 6e 20 61 20 6c 69 73 74 20 6f 66 20 70 61 67 65 n a list of page
10add 73 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 s (connected by
10ade 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79 the PgHdr.pDirty
10adf 20 70 6f 69 6e 74 65 72 29 20 77 72 69 74 65 0a pointer) write.
10ae0 2a 2a 20 65 76 65 72 79 20 6f 6e 65 20 6f 66 20 ** every one of
10ae1 74 68 6f 73 65 20 70 61 67 65 73 20 6f 75 74 20 those pages out
10ae2 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
10ae3 66 69 6c 65 2e 20 4e 6f 20 63 61 6c 6c 73 20 61 file. No calls a
10ae4 72 65 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 re made.** to th
10ae5 65 20 70 61 67 65 2d 63 61 63 68 65 20 74 6f 20 e page-cache to
10ae6 6d 61 72 6b 20 74 68 65 20 70 61 67 65 73 20 61 mark the pages a
10ae7 73 20 63 6c 65 61 6e 2e 20 49 74 20 69 73 20 74 s clean. It is t
10ae8 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 he responsibilit
10ae9 79 0a 2a 2a 20 6f 66 20 74 68 65 20 63 61 6c 6c y.** of the call
10aea 65 72 20 74 6f 20 75 73 65 20 50 63 61 63 68 65 er to use Pcache
10aeb 43 6c 65 61 6e 41 6c 6c 28 29 20 6f 72 20 50 63 CleanAll() or Pc
10aec 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 acheMakeClean()
10aed 74 6f 20 6d 61 72 6b 0a 2a 2a 20 74 68 65 20 70 to mark.** the p
10aee 61 67 65 73 20 61 73 20 63 6c 65 61 6e 2e 0a 2a ages as clean..*
10aef 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
10af0 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 er_write_pagelis
10af1 74 28 50 67 48 64 72 20 2a 70 4c 69 73 74 29 7b t(PgHdr *pList){
10af2 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
10af3 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 ;. int rc;.. i
10af4 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 f( pList==0 ) re
10af5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
10af6 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73 74 pPager = pList
10af7 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a 20 ->pPager;.. /*
10af8 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 At this point th
10af9 65 72 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 ere may be eithe
10afa 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 r a RESERVED or
10afb 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
10afc 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 n the. ** datab
10afd 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 ase file. If the
10afe 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e re is already an
10aff 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c EXCLUSIVE lock,
10b00 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 the following.
10b01 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ** calls to sql
10b02 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 61 72 65 ite3OsLock() are
10b03 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2a 0a 20 20 no-ops.. **.
10b04 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f ** Moving the lo
10b05 63 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44 ck from RESERVED
10b06 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63 to EXCLUSIVE ac
10b07 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 tually involves
10b08 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 going. ** throu
10b09 67 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 gh an intermedia
10b0a 74 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47 te state PENDING
10b0b 2e 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f . A PENDING lo
10b0c 63 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a ck prevents new.
10b0d 20 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f ** readers fro
10b0e 6d 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74 m attaching to t
10b0f 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 he database but
10b10 69 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20 is unsufficient
10b11 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 for us to. ** w
10b12 72 69 74 65 2e 20 20 54 68 65 20 69 64 65 61 20 rite. The idea
10b13 6f 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 of a PENDING loc
10b14 6b 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 k is to prevent
10b15 6e 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d new readers from
10b16 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 . ** coming in
10b17 77 68 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f while we wait fo
10b18 72 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65 r existing reade
10b19 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a rs to clear.. *
10b1a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 *. ** While the
10b1b 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 pager is in the
10b1c 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c RESERVED state,
10b1d 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 the original da
10b1e 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a tabase file. **
10b1f 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e is unchanged an
10b20 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 d we can rollbac
10b21 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 k without having
10b22 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65 to playback the
10b23 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e . ** journal in
10b24 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 to the original
10b25 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
10b26 4f 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74 69 Once we transiti
10b27 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 on to. ** EXCLU
10b28 53 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 SIVE, it means t
10b29 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
10b2a 20 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65 has been change
10b2b 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 d and any rollba
10b2c 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 ck. ** will req
10b2d 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 uire a journal p
10b2e 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 layback.. */.
10b2f 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f rc = pager_wait_
10b30 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 on_lock(pPager,
10b31 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
10b32 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
10b33 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
10b34 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 77 68 rn rc;. }.. wh
10b35 69 6c 65 28 20 70 4c 69 73 74 20 29 7b 0a 0a 20 ile( pList ){..
10b36 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c /* If the fil
10b37 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 e has not yet be
10b38 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 en opened, open
10b39 69 74 20 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20 69 it now. */. i
10b3a 66 28 20 21 70 50 61 67 65 72 2d 3e 66 64 2d 3e f( !pPager->fd->
10b3b 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 pMethods ){.
10b3c 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d assert(pPager-
10b3d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 20 20 >tempFile);.
10b3e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
10b3f 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 gerOpentemp(pPag
10b40 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 er, pPager->fd,
10b41 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 pPager->vfsFlags
10b42 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
10b43 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
10b44 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 }.. /* If th
10b45 65 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61 ere are dirty pa
10b46 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 ges in the page
10b47 63 61 63 68 65 20 77 69 74 68 20 70 61 67 65 20 cache with page
10b48 6e 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a numbers greater.
10b49 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 ** than Page
10b4a 72 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d r.dbSize, this m
10b4b 65 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 eans sqlite3Page
10b4c 72 54 72 75 6e 63 61 74 65 28 29 20 77 61 73 20 rTruncate() was
10b4d 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a called to. **
10b4e 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73 make the file s
10b4f 6d 61 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62 maller (presumab
10b50 6c 79 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 ly by auto-vacuu
10b51 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 m code). Do not
10b52 77 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79 write. ** any
10b53 20 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74 such pages to t
10b54 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a he file.. */.
10b55 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 if( pList->p
10b56 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53 gno<=pPager->dbS
10b57 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c 69 73 74 ize && 0==(pList
10b58 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 4f ->flags&PGHDR_DO
10b59 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a 20 20 20 NT_WRITE) ){.
10b5a 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 i64 offset =
10b5b 28 70 4c 69 73 74 2d 3e 70 67 6e 6f 2d 31 29 2a (pList->pgno-1)*
10b5c 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 (i64)pPager->pag
10b5d 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 63 68 61 eSize;. cha
10b5e 72 20 2a 70 44 61 74 61 20 3d 20 43 4f 44 45 43 r *pData = CODEC
10b5f 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74 2d 2(pPager, pList-
10b60 3e 70 44 61 74 61 2c 20 70 4c 69 73 74 2d 3e 70 >pData, pList->p
10b61 67 6e 6f 2c 20 36 29 3b 0a 20 20 20 20 20 20 50 gno, 6);. P
10b62 41 47 45 52 54 52 41 43 45 34 28 22 53 54 4f 52 AGERTRACE4("STOR
10b63 45 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 E %d page %d has
10b64 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 h(%08x)\n",.
10b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
10b66 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
10b67 70 4c 69 73 74 2d 3e 70 67 6e 6f 2c 20 70 61 67 pList->pgno, pag
10b68 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73 er_pagehash(pLis
10b69 74 29 29 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 t));. IOTRA
10b6a 43 45 28 28 22 50 47 4f 55 54 20 25 70 20 25 64 CE(("PGOUT %p %d
10b6b 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 4c 69 \n", pPager, pLi
10b6c 73 74 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 st->pgno));.
10b6d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
10b6e 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 Write(pPager->fd
10b6f 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d , pData, pPager-
10b70 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 >pageSize, offse
10b71 74 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f t);. PAGER_
10b72 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 INCR(sqlite3_pag
10b73 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 er_writedb_count
10b74 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 );. PAGER_I
10b75 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69 NCR(pPager->nWri
10b76 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 te);. if( p
10b77 4c 69 73 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b List->pgno==1 ){
10b78 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
10b79 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 &pPager->dbFileV
10b7a 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d 2c ers, &pData[24],
10b7b 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e sizeof(pPager->
10b7c 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
10b7d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e }. }.#ifn
10b7e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 65 def NDEBUG. e
10b7f 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 lse{. PAGER
10b80 54 52 41 43 45 33 28 22 4e 4f 53 54 4f 52 45 20 TRACE3("NOSTORE
10b81 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 %d page %d\n", P
10b82 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
10b83 70 4c 69 73 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 pList->pgno);.
10b84 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 }.#endif. i
10b85 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
10b86 63 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 c;.#ifdef SQLITE
10b87 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 _CHECK_PAGES.
10b88 20 70 4c 69 73 74 2d 3e 70 61 67 65 48 61 73 68 pList->pageHash
10b89 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
10b8a 68 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66 h(pList);.#endif
10b8b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 4c 69 . pList = pLi
10b8c 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a st->pDirty;. }.
10b8d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
10b8e 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 _OK;.}../*.** Th
10b8f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
10b90 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 63 61 alled by the pca
10b91 63 68 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69 che layer when i
10b92 74 20 68 61 73 20 72 65 61 63 68 65 64 20 73 6f t has reached so
10b93 6d 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 me.** soft memor
10b94 79 20 6c 69 6d 69 74 2e 20 54 68 65 20 61 72 67 y limit. The arg
10b95 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 ument is a point
10b96 65 72 20 74 6f 20 61 20 70 75 72 67 65 61 62 6c er to a purgeabl
10b97 65 20 50 61 67 65 72 20 0a 2a 2a 20 6f 62 6a 65 e Pager .** obje
10b98 63 74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f ct. This functio
10b99 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6d 61 n attempts to ma
10b9a 6b 65 20 61 20 73 69 6e 67 6c 65 20 64 69 72 74 ke a single dirt
10b9b 79 20 70 61 67 65 20 74 68 61 74 20 68 61 73 20 y page that has
10b9c 6e 6f 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e no.** outstandin
10b9d 67 20 72 65 66 65 72 65 6e 63 65 73 20 28 69 66 g references (if
10b9e 20 6f 6e 65 20 65 78 69 73 74 73 29 20 63 6c 65 one exists) cle
10b9f 61 6e 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 an so that it ca
10ba0 6e 20 62 65 20 72 65 63 79 63 6c 65 64 20 0a 2a n be recycled .*
10ba1 2a 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20 * by the pcache
10ba2 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 layer..*/.static
10ba3 20 69 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 int pagerStress
10ba4 28 76 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20 (void *p, PgHdr
10ba5 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a *pPg){. Pager *
10ba6 70 50 61 67 65 72 20 3d 20 28 50 61 67 65 72 20 pPager = (Pager
10ba7 2a 29 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 *)p;. int rc =
10ba8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 SQLITE_OK;.. if
10ba9 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 ( pPager->doNotS
10baa 79 6e 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ync ){. retur
10bab 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
10bac 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d .. assert( pPg-
10bad 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
10bae 54 59 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 TY );. if( pPag
10baf 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
10bb0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
10bb1 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 ( pPg->flags&PGH
10bb2 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a DR_NEED_SYNC ){.
10bb3 20 20 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a rc = syncJ
10bb4 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a ournal(pPager);.
10bb5 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
10bb6 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 LITE_OK && pPage
10bb7 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a r->fullSync && .
10bb8 20 20 20 20 20 20 20 20 21 28 70 50 61 67 65 72 !(pPager
10bb9 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
10bba 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
10bbb 5f 4d 45 4d 4f 52 59 29 20 26 26 0a 20 20 20 20 _MEMORY) &&.
10bbc 20 20 20 20 21 28 73 71 6c 69 74 65 33 4f 73 44 !(sqlite3OsD
10bbd 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
10bbe 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
10bbf 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 &SQLITE_IOCAP_SA
10bc0 46 45 5f 41 50 50 45 4e 44 29 0a 20 20 20 20 20 FE_APPEND).
10bc1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 ){. pPag
10bc2 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 er->nRec = 0;.
10bc3 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 rc = write
10bc4 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
10bc5 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 r);. }.
10bc6 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
10bc7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
10bc8 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d 20 30 pPg->pDirty = 0
10bc9 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 ;. rc = pag
10bca 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 er_write_pagelis
10bcb 74 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 t(pPg);. }.
10bcc 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
10bcd 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 61 67 _OK ){. pag
10bce 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
10bcf 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rc);. }. }.
10bd0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
10bd1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
10bd2 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
10bd3 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 an(pPg);. }. r
10bd4 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
10bd5 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 .** Return 1 if
10bd6 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a there is a hot j
10bd7 6f 75 72 6e 61 6c 20 6f 6e 20 74 68 65 20 67 69 ournal on the gi
10bd8 76 65 6e 20 70 61 67 65 72 2e 0a 2a 2a 20 41 20 ven pager..** A
10bd9 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f hot journal is o
10bda 6e 65 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f ne that needs to
10bdb 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e be played back.
10bdc 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 .**.** If the cu
10bdd 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 rrent size of th
10bde 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
10bdf 69 73 20 30 20 62 75 74 20 61 20 6a 6f 75 72 6e is 0 but a journ
10be0 61 6c 20 66 69 6c 65 0a 2a 2a 20 65 78 69 73 74 al file.** exist
10be1 73 2c 20 74 68 61 74 20 69 73 20 70 72 6f 62 61 s, that is proba
10be2 62 6c 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e bly an old journ
10be3 61 6c 20 6c 65 66 74 20 6f 76 65 72 20 66 72 6f al left over fro
10be4 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 64 61 74 m a prior.** dat
10be5 61 62 61 73 65 20 77 69 74 68 20 74 68 65 20 73 abase with the s
10be6 61 6d 65 20 6e 61 6d 65 2e 20 20 4a 75 73 74 20 ame name. Just
10be7 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e delete the journ
10be8 61 6c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e al..**.** Return
10be9 20 6e 65 67 61 74 69 76 65 20 69 66 20 75 6e 61 negative if una
10bea 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ble to determine
10beb 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 74 the status of t
10bec 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a he journal..**.*
10bed 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
10bee 6f 65 73 20 6e 6f 74 20 6f 70 65 6e 20 74 68 65 oes not open the
10bef 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f journal file to
10bf0 20 65 78 61 6d 69 6e 65 20 69 74 73 0a 2a 2a 20 examine its.**
10bf1 63 6f 6e 74 65 6e 74 2e 20 20 48 65 6e 63 65 2c content. Hence,
10bf2 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 the journal mig
10bf3 68 74 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e ht contain the n
10bf4 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 0a ame of a master.
10bf5 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
10bf6 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 64 65 that has been de
10bf7 6c 65 74 65 64 2c 20 61 6e 64 20 68 65 6e 63 65 leted, and hence
10bf8 20 6e 6f 74 20 62 65 20 68 6f 74 2e 20 20 4f 72 not be hot. Or
10bf9 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f .** the header o
10bfa 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 f the journal mi
10bfb 67 68 74 20 62 65 20 7a 65 72 6f 65 64 20 6f 75 ght be zeroed ou
10bfc 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 t. This routine
10bfd 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 64 69 73 .** does not dis
10bfe 63 6f 76 65 72 20 74 68 65 73 65 20 63 61 73 65 cover these case
10bff 73 20 6f 66 20 61 20 6e 6f 6e 2d 68 6f 74 20 6a s of a non-hot j
10c00 6f 75 72 6e 61 6c 20 2d 20 69 66 20 74 68 65 0a ournal - if the.
10c01 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
10c02 65 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f exists and is no
10c03 74 20 65 6d 70 74 79 20 74 68 69 73 20 72 6f 75 t empty this rou
10c04 74 69 6e 65 20 61 73 73 75 6d 65 73 20 69 74 0a tine assumes it.
10c05 2a 2a 20 69 73 20 68 6f 74 2e 20 20 54 68 65 20 ** is hot. The
10c06 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 pager_playback()
10c07 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 routine will di
10c08 73 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 0a scover that the.
10c09 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
10c0a 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f is not really ho
10c0b 74 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 2d 6f 70 t and will no-op
10c0c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
10c0d 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 hasHotJournal(Pa
10c0e 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
10c0f 20 2a 70 45 78 69 73 74 73 29 7b 0a 20 20 73 71 *pExists){. sq
10c10 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 lite3_vfs *pVfs
10c11 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a = pPager->pVfs;.
10c12 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
10c13 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 65 78 69 73 E_OK;. int exis
10c14 74 73 3b 0a 20 20 69 6e 74 20 6c 6f 63 6b 65 64 ts;. int locked
10c15 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
10c16 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 er!=0 );. asser
10c17 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f t( pPager->useJo
10c18 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 urnal );. asser
10c19 74 28 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 t( pPager->fd->p
10c1a 4d 65 74 68 6f 64 73 20 29 3b 0a 20 20 2a 70 45 Methods );. *pE
10c1b 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 72 63 20 xists = 0;. rc
10c1c 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 = sqlite3OsAcces
10c1d 73 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e s(pVfs, pPager->
10c1e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 zJournal, SQLITE
10c1f 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 _ACCESS_EXISTS,
10c20 26 65 78 69 73 74 73 29 3b 0a 20 20 69 66 28 20 &exists);. if(
10c21 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
10c22 20 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 72 exists ){. r
10c23 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 68 65 c = sqlite3OsChe
10c24 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 ckReservedLock(p
10c25 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b Pager->fd, &lock
10c26 65 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 ed);. }. if( r
10c27 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
10c28 65 78 69 73 74 73 20 26 26 20 21 6c 6f 63 6b 65 exists && !locke
10c29 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 d ){. int nPa
10c2a 67 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ge;. rc = sql
10c2b 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
10c2c 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 nt(pPager, &nPag
10c2d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d e);. if( rc==
10c2e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10c2f 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 if( nPage==0 )
10c30 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
10c31 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 3OsDelete(pVfs,
10c32 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
10c33 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 , 0);. }els
10c34 65 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 78 69 e{. *pExi
10c35 73 74 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d sts = 1;. }
10c36 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
10c37 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
10c38 20 52 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e Read the conten
10c39 74 20 6f 66 20 70 61 67 65 20 70 50 67 20 6f 75 t of page pPg ou
10c3a 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 t of the databas
10c3b 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 e file..*/.stati
10c3c 63 20 69 6e 74 20 72 65 61 64 44 62 50 61 67 65 c int readDbPage
10c3d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
10c3e 50 67 48 64 72 20 2a 70 50 67 2c 20 50 67 6e 6f PgHdr *pPg, Pgno
10c3f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 pgno){. int rc
10c40 3b 0a 20 20 69 36 34 20 6f 66 66 73 65 74 3b 0a ;. i64 offset;.
10c41 20 20 61 73 73 65 72 74 28 20 4d 45 4d 44 42 3d assert( MEMDB=
10c42 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 =0 );. assert(p
10c43 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 Pager->fd->pMeth
10c44 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e 74 65 6d ods||pPager->tem
10c45 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 pFile);. if( !p
10c46 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 Pager->fd->pMeth
10c47 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ods ){. retur
10c48 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 n SQLITE_IOERR_S
10c49 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 HORT_READ;. }.
10c4a 20 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d offset = (pgno-
10c4b 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 1)*(i64)pPager->
10c4c 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 20 3d pageSize;. rc =
10c4d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
10c4e 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d 3e Pager->fd, pPg->
10c4f 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 pData, pPager->p
10c50 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 29 ageSize, offset)
10c51 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 ;. PAGER_INCR(s
10c52 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 qlite3_pager_rea
10c53 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41 ddb_count);. PA
10c54 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d GER_INCR(pPager-
10c55 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54 52 41 >nRead);. IOTRA
10c56 43 45 28 28 22 50 47 49 4e 20 25 70 20 25 64 5c CE(("PGIN %p %d\
10c57 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f n", pPager, pgno
10c58 29 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d ));. if( pgno==
10c59 31 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 1 ){. memcpy(
10c5a 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 &pPager->dbFileV
10c5b 65 72 73 2c 20 26 28 28 75 38 2a 29 70 50 67 2d ers, &((u8*)pPg-
10c5c 3e 70 44 61 74 61 29 5b 32 34 5d 2c 0a 20 20 20 >pData)[24],.
10c5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10c5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10c5f 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f sizeo
10c60 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 f(pPager->dbFile
10c61 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f Vers));. }. CO
10c62 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67 DEC1(pPager, pPg
10c63 2d 3e 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 ->pData, pPg->pg
10c64 6e 6f 2c 20 33 29 3b 0a 20 20 50 41 47 45 52 54 no, 3);. PAGERT
10c65 52 41 43 45 34 28 22 46 45 54 43 48 20 25 64 20 RACE4("FETCH %d
10c66 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 page %d hash(%08
10c67 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 x)\n",.
10c68 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 PAGERID(pP
10c69 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f ager), pPg->pgno
10c6a 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 , pager_pagehash
10c6b 28 70 50 67 29 29 3b 0a 20 20 72 65 74 75 72 6e (pPg));. return
10c6c 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 rc;.}.../*.** T
10c6d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
10c6e 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e called to obtain
10c6f 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b the shared lock
10c70 20 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 required before
10c71 0a 2a 2a 20 64 61 74 61 20 6d 61 79 20 62 65 20 .** data may be
10c72 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 70 61 read from the pa
10c73 67 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 ger cache. If th
10c74 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 68 61 e shared lock ha
10c75 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 s already.** bee
10c76 6e 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 69 73 n obtained, this
10c77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
10c78 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d 65 o-op..**.** Imme
10c79 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f 62 diately after ob
10c7a 74 61 69 6e 69 6e 67 20 74 68 65 20 73 68 61 72 taining the shar
10c7b 65 64 20 6c 6f 63 6b 20 28 69 66 20 72 65 71 75 ed lock (if requ
10c7c 69 72 65 64 29 2c 20 74 68 69 73 20 66 75 6e 63 ired), this func
10c7d 74 69 6f 6e 0a 2a 2a 20 63 68 65 63 6b 73 20 66 tion.** checks f
10c7e 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c or a hot-journal
10c7f 20 66 69 6c 65 2e 20 49 66 20 6f 6e 65 20 69 73 file. If one is
10c80 20 66 6f 75 6e 64 2c 20 61 6e 20 65 6d 65 72 67 found, an emerg
10c81 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a ency rollback.**
10c82 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 69 6d is performed im
10c83 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 mediately..*/.st
10c84 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 68 atic int pagerSh
10c85 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a aredLock(Pager *
10c86 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
10c87 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
10c88 20 69 6e 74 20 69 73 45 72 72 6f 72 52 65 73 65 int isErrorRese
10c89 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 t = 0;.. /* If
10c8a 74 68 69 73 20 64 61 74 61 62 61 73 65 20 69 73 this database is
10c8b 20 6f 70 65 6e 65 64 20 66 6f 72 20 65 78 63 6c opened for excl
10c8c 75 73 69 76 65 20 61 63 63 65 73 73 2c 20 68 61 usive access, ha
10c8d 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 s no outstanding
10c8e 20 0a 20 20 2a 2a 20 70 61 67 65 20 72 65 66 65 . ** page refe
10c8f 72 65 6e 63 65 73 20 61 6e 64 20 69 73 20 69 6e rences and is in
10c90 20 61 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c an error-state,
10c91 20 6e 6f 77 20 69 73 20 74 68 65 20 63 68 61 6e now is the chan
10c92 63 65 20 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a ce to clear. **
10c93 20 74 68 65 20 65 72 72 6f 72 2e 20 44 69 73 63 the error. Disc
10c94 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ard the contents
10c95 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 of the pager-ca
10c96 63 68 65 20 61 6e 64 20 74 72 65 61 74 20 61 6e che and treat an
10c97 79 0a 20 20 2a 2a 20 6f 70 65 6e 20 6a 6f 75 72 y. ** open jour
10c98 6e 61 6c 20 66 69 6c 65 20 61 73 20 61 20 68 6f nal file as a ho
10c99 74 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a t-journal.. */.
10c9a 20 20 69 66 28 20 21 4d 45 4d 44 42 20 26 26 20 if( !MEMDB &&
10c9b 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
10c9c 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 73 71 6c eMode . && sql
10c9d 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 ite3PcacheRefCou
10c9e 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
10c9f 68 65 29 3d 3d 30 20 26 26 20 70 50 61 67 65 72 he)==0 && pPager
10ca0 2d 3e 65 72 72 43 6f 64 65 20 0a 20 20 29 7b 0a ->errCode . ){.
10ca1 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
10ca2 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 journalOpen ){.
10ca3 20 20 20 20 20 69 73 45 72 72 6f 72 52 65 73 65 isErrorRese
10ca4 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 t = 1;. }.
10ca5 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
10ca6 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
10ca7 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 pager_reset(pP
10ca8 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a ager);. }.. /*
10ca9 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
10caa 20 73 74 69 6c 6c 20 69 6e 20 61 6e 20 65 72 72 still in an err
10cab 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 or state, do not
10cac 20 70 72 6f 63 65 65 64 2e 20 54 68 65 20 65 72 proceed. The er
10cad 72 6f 72 20 0a 20 20 2a 2a 20 73 74 61 74 65 20 ror . ** state
10cae 77 69 6c 6c 20 62 65 20 63 6c 65 61 72 65 64 20 will be cleared
10caf 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e at some point in
10cb0 20 74 68 65 20 66 75 74 75 72 65 20 77 68 65 6e the future when
10cb1 20 61 6c 6c 20 70 61 67 65 20 0a 20 20 2a 2a 20 all page . **
10cb2 72 65 66 65 72 65 6e 63 65 73 20 61 72 65 20 64 references are d
10cb3 72 6f 70 70 65 64 20 61 6e 64 20 74 68 65 20 63 ropped and the c
10cb4 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 63 ache can be disc
10cb5 61 72 64 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 arded.. */. if
10cb6 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
10cb7 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 e && pPager->err
10cb8 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c Code!=SQLITE_FUL
10cb9 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 L ){. return
10cba 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
10cbb 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 . }.. if( pPag
10cbc 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
10cbd 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45 72 72 _UNLOCK || isErr
10cbe 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 73 orReset ){. s
10cbf 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
10cc0 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b = pPager->pVfs;
10cc1 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74 4a 6f . int isHotJo
10cc2 75 72 6e 61 6c 3b 0a 20 20 20 20 61 73 73 65 72 urnal;. asser
10cc3 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 t( !MEMDB );.
10cc4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
10cc5 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 PcacheRefCount(p
10cc6 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d Pager->pPCache)=
10cc7 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 =0 );. if( !p
10cc8 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 Pager->noReadloc
10cc9 6b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 k ){. rc =
10cca 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f pager_wait_on_lo
10ccb 63 6b 28 70 50 61 67 65 72 2c 20 53 48 41 52 45 ck(pPager, SHARE
10ccc 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 D_LOCK);. i
10ccd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
10cce 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
10ccf 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
10cd0 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 e==PAGER_UNLOCK
10cd1 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
10cd2 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 n pager_error(pP
10cd3 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 ager, rc);.
10cd4 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
10cd5 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
10cd6 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
10cd7 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
10cd8 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 a journal file e
10cd9 78 69 73 74 73 2c 20 61 6e 64 20 74 68 65 72 65 xists, and there
10cda 20 69 73 20 6e 6f 20 52 45 53 45 52 56 45 44 20 is no RESERVED
10cdb 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 lock on the.
10cdc 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
10cdd 2c 20 74 68 65 6e 20 69 74 20 65 69 74 68 65 72 , then it either
10cde 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61 needs to be pla
10cdf 79 65 64 20 62 61 63 6b 20 6f 72 20 64 65 6c 65 yed back or dele
10ce0 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
10ce1 69 66 28 20 21 69 73 45 72 72 6f 72 52 65 73 65 if( !isErrorRese
10ce2 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 t ){. rc =
10ce3 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50 hasHotJournal(pP
10ce4 61 67 65 72 2c 20 26 69 73 48 6f 74 4a 6f 75 72 ager, &isHotJour
10ce5 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 nal);. if(
10ce6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
10ce7 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 . goto fa
10ce8 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 iled;. }.
10ce9 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 45 72 }. if( isEr
10cea 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 73 48 6f rorReset || isHo
10ceb 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 tJournal ){.
10cec 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c /* Get an EXCL
10ced 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 USIVE lock on th
10cee 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
10cef 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 At this point i
10cf0 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 6d t is. ** im
10cf1 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 20 52 portant that a R
10cf2 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 ESERVED lock is
10cf3 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 not obtained on
10cf4 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 0a 20 the way to the.
10cf5 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 ** EXCLUSIV
10cf6 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 77 65 E lock. If it we
10cf7 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 re, another proc
10cf8 65 73 73 20 6d 69 67 68 74 20 6f 70 65 6e 20 74 ess might open t
10cf9 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 he. ** data
10cfa 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74 65 63 base file, detec
10cfb 74 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c t the RESERVED l
10cfc 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c 75 64 ock, and conclud
10cfd 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 20 e that the.
10cfe 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 ** database is
10cff 73 61 66 65 20 74 6f 20 72 65 61 64 20 77 68 69 safe to read whi
10d00 6c 65 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 le this process
10d01 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c 69 6e 67 is still rolling
10d02 20 69 74 20 0a 20 20 20 20 20 20 2a 2a 20 62 61 it . ** ba
10d03 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 ck.. ** .
10d04 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74 ** Because t
10d05 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 he intermediate
10d06 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 RESERVED lock is
10d07 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20 not requested,
10d08 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 the. ** sec
10d09 6f 6e 64 20 70 72 6f 63 65 73 73 20 77 69 6c 6c ond process will
10d0a 20 67 65 74 20 74 6f 20 74 68 69 73 20 70 6f 69 get to this poi
10d0b 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61 nt in the code a
10d0c 6e 64 20 66 61 69 6c 20 74 6f 0a 20 20 20 20 20 nd fail to.
10d0d 20 2a 2a 20 6f 62 74 61 69 6e 20 69 74 73 20 6f ** obtain its o
10d0e 77 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 wn EXCLUSIVE loc
10d0f 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
10d10 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a 2f e file.. */
10d11 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 . if( pPage
10d12 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 49 r->state<EXCLUSI
10d13 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 VE_LOCK ){.
10d14 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
10d15 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 sLock(pPager->fd
10d16 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b , EXCLUSIVE_LOCK
10d17 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
10d18 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
10d19 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 rc = p
10d1a 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
10d1b 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 r, rc);.
10d1c 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 goto failed;.
10d1d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
10d1e 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d pPager->state =
10d1f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
10d20 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20 ;. }. .
10d21 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f /* Open the jo
10d22 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f 77 urnal for read/w
10d23 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 68 69 rite access. Thi
10d24 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 s is because in
10d25 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 . ** exclus
10d26 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 ive-access mode
10d27 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
10d28 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74 tor will be kept
10d29 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20 open and.
10d2a 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73 65 64 ** possibly used
10d2b 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 for a transacti
10d2c 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e 20 on later on. On
10d2d 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 68 some systems, th
10d2e 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 75 e. ** OsTru
10d2f 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 75 73 65 ncate() call use
10d30 64 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 d in exclusive-a
10d31 63 63 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f 20 ccess mode also
10d32 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a requires. *
10d33 2a 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 66 * a read/write f
10d34 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 ile handle..
10d35 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 */. if( !
10d36 69 73 45 72 72 6f 72 52 65 73 65 74 20 26 26 20 isErrorReset &&
10d37 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
10d38 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pen==0 ){.
10d39 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 int res;.
10d3a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
10d3b 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 50 61 sAccess(pVfs,pPa
10d3c 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 ger->zJournal,SQ
10d3d 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
10d3e 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20 TS,&res);.
10d3f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
10d40 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
10d41 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 if( res ){.
10d42 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 int fout
10d43 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
10d44 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 int f = SQLITE
10d45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c _OPEN_READWRITE|
10d46 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
10d47 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 _JOURNAL;.
10d48 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
10d49 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
10d4a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 );. r
10d4b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
10d4c 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e n(pVfs, pPager->
10d4d 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 zJournal, pPager
10d4e 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 ->jfd, f, &fout)
10d4f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 ;. as
10d50 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
10d51 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a _OK || pPager->j
10d52 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a fd->pMethods );.
10d53 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
10d54 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
10d55 20 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 fout&SQLITE_OPE
10d56 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 N_READONLY ){.
10d57 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
10d58 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
10d59 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
10d5a 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
10d5b 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 Pager->jfd);.
10d5c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
10d5d 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
10d5e 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
10d5f 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e e journal does n
10d60 6f 74 20 65 78 69 73 74 2c 20 74 68 61 74 20 6d ot exist, that m
10d61 65 61 6e 73 20 73 6f 6d 65 20 6f 74 68 65 72 20 eans some other
10d62 70 72 6f 63 65 73 73 0a 20 20 20 20 20 20 20 20 process.
10d63 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 ** has alrea
10d64 64 79 20 72 6f 6c 6c 65 64 20 69 74 20 62 61 63 dy rolled it bac
10d65 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 k */.
10d66 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
10d67 59 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 Y;. }.
10d68 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
10d69 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
10d6a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
10d6b 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b goto failed;
10d6c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
10d6d 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 Pager->journalOp
10d6e 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 50 en = 1;. pP
10d6f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 ager->journalSta
10d70 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 rted = 0;.
10d71 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
10d72 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 ff = 0;. pP
10d73 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 ager->setMaster
10d74 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 = 0;. pPage
10d75 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 r->journalHdr =
10d76 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 0;. . /* Pl
10d77 61 79 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 ayback and delet
10d78 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 e the journal.
10d79 44 72 6f 70 20 74 68 65 20 64 61 74 61 62 61 73 Drop the databas
10d7a 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a e write. **
10d7b 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75 lock and reacqu
10d7c 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 ire the read loc
10d7d 6b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 k.. */.
10d7e 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
10d7f 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29 yback(pPager, 1)
10d80 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
10d81 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10d82 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
10d83 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 error(pPager, rc
10d84 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 );. goto
10d85 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a failed;. }.
10d86 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 assert(pPa
10d87 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
10d88 52 5f 53 48 41 52 45 44 20 7c 7c 20 0a 20 20 20 R_SHARED || .
10d89 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e (pPager->
10d8a 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 exclusiveMode &&
10d8b 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 pPager->state>P
10d8c 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 AGER_SHARED).
10d8d 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 );. }..
10d8e 20 69 66 28 20 73 71 6c 69 74 65 33 50 63 61 63 if( sqlite3Pcac
10d8f 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 hePagecount(pPag
10d90 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 er->pPCache)>0 )
10d91 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 {. /* The s
10d92 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a hared-lock has j
10d93 75 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 ust been acquire
10d94 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 d on the databas
10d95 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 e file. **
10d96 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c and there are al
10d97 72 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 ready pages in t
10d98 68 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 he cache (from a
10d99 20 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 previous.
10d9a 2a 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 ** read or write
10d9b 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 transaction).
10d9c 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
10d9d 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 the database.
10d9e 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d ** has been m
10d9f 6f 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 odified. If the
10da0 20 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 database has ch
10da1 61 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 anged, flush the
10da2 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e . ** cache.
10da3 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
10da4 2a 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e ** Database chan
10da5 67 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 ges is detected
10da6 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 by looking at 15
10da7 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 bytes beginning
10da8 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 . ** at off
10da9 73 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 set 24 into the
10daa 66 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 file. The first
10dab 20 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 4 of these 16 b
10dac 79 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a ytes are. *
10dad 2a 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 * a 32-bit count
10dae 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 er that is incre
10daf 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 mented with each
10db0 20 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 change. The.
10db1 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 ** other byt
10db2 65 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d es change random
10db3 6c 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c ly with each fil
10db4 65 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 e change when.
10db5 20 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 ** a codec i
10db6 73 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 s in use..
10db7 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 ** . ** The
10db8 72 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e re is a vanishin
10db9 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 gly small chance
10dba 20 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 that a change w
10dbb 69 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 ill not be .
10dbc 20 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 ** detected.
10dbd 54 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e The chance of an
10dbe 20 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e undetected chan
10dbf 67 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 ge is so small t
10dc0 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 hat. ** it
10dc1 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 can be neglected
10dc2 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
10dc3 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 char dbFileVers
10dc4 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e [sizeof(pPager->
10dc5 64 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 dbFileVers)];.
10dc6 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
10dc7 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
10dc8 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 , 0);.. if(
10dc9 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
10dca 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
10dcb 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
10dcc 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 ;. goto f
10dcd 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a ailed;. }..
10dce 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 if( pPager
10dcf 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20 20 ->dbSize>0 ){.
10dd0 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
10dd1 43 4b 56 45 52 53 20 25 70 20 25 64 5c 6e 22 2c CKVERS %p %d\n",
10dd2 20 70 50 61 67 65 72 2c 20 73 69 7a 65 6f 66 28 pPager, sizeof(
10dd3 64 62 46 69 6c 65 56 65 72 73 29 29 29 3b 0a 20 dbFileVers)));.
10dd4 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
10dd5 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 te3OsRead(pPager
10dd6 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56 65 72 ->fd, &dbFileVer
10dd7 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 s, sizeof(dbFile
10dd8 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 20 20 20 Vers), 24);.
10dd9 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
10dda 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
10ddb 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
10ddc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
10ddd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d }else{. m
10dde 65 6d 73 65 74 28 64 62 46 69 6c 65 56 65 72 73 emset(dbFileVers
10ddf 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 , 0, sizeof(dbFi
10de0 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 leVers));.
10de1 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d }.. if( mem
10de2 63 6d 70 28 70 50 61 67 65 72 2d 3e 64 62 46 69 cmp(pPager->dbFi
10de3 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 65 leVers, dbFileVe
10de4 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c rs, sizeof(dbFil
10de5 65 56 65 72 73 29 29 21 3d 30 20 29 7b 0a 20 20 eVers))!=0 ){.
10de6 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 pager_rese
10de7 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 t(pPager);.
10de8 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 }. }. ass
10de9 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 78 63 ert( pPager->exc
10dea 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 lusiveMode || pP
10deb 61 67 65 72 2d 3e 73 74 61 74 65 3c 3d 50 41 47 ager->state<=PAG
10dec 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 20 ER_SHARED );.
10ded 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
10dee 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te==PAGER_UNLOCK
10def 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
10df0 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
10df1 53 48 41 52 45 44 3b 0a 20 20 20 20 7d 0a 20 20 SHARED;. }.
10df2 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 20 69 66 }.. failed:. if
10df3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
10df4 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f ){. /* pager_
10df5 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f unlock() is a no
10df6 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 73 69 76 -op for exclusiv
10df7 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e 2d 6d 65 e mode and in-me
10df8 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 2e 20 mory databases.
10df9 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c */. pager_unl
10dfa 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ock(pPager);. }
10dfb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
10dfc 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
10dfd 20 77 65 20 68 61 76 65 20 74 68 65 20 63 6f 6e we have the con
10dfe 74 65 6e 74 20 66 6f 72 20 61 20 70 61 67 65 2e tent for a page.
10dff 20 20 49 66 20 74 68 65 20 70 61 67 65 20 77 61 If the page wa
10e00 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
10e01 61 63 71 75 69 72 65 64 20 77 69 74 68 20 6e 6f acquired with no
10e02 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74 68 65 6e Content==1, then
10e03 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 61 73 the content was
10e04 0a 2a 2a 20 6a 75 73 74 20 69 6e 69 74 69 61 6c .** just initial
10e05 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 69 6e ized to zeros in
10e06 73 74 65 61 64 20 6f 66 20 62 65 69 6e 67 20 72 stead of being r
10e07 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a ead from disk..*
10e08 2a 20 42 75 74 20 6e 6f 77 20 77 65 20 6e 65 65 * But now we nee
10e09 64 20 74 68 65 20 72 65 61 6c 20 64 61 74 61 20 d the real data
10e0a 6f 66 66 20 6f 66 20 64 69 73 6b 2e 20 20 53 6f off of disk. So
10e0b 20 6d 61 6b 65 20 73 75 72 65 20 77 65 0a 2a 2a make sure we.**
10e0c 20 68 61 76 65 20 69 74 2e 20 20 52 65 61 64 20 have it. Read
10e0d 69 74 20 69 6e 20 69 66 20 77 65 20 64 6f 20 6e it in if we do n
10e0e 6f 74 20 68 61 76 65 20 69 74 20 61 6c 72 65 61 ot have it alrea
10e0f 64 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e dy..*/.static in
10e10 74 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74 t pager_get_cont
10e11 65 6e 74 28 50 67 48 64 72 20 2a 70 50 67 29 7b ent(PgHdr *pPg){
10e12 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 . if( pPg->flag
10e13 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 s&PGHDR_NEED_REA
10e14 44 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 D ){. int rc
10e15 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50 67 = readDbPage(pPg
10e16 2d 3e 70 50 61 67 65 72 2c 20 70 50 67 2c 20 70 ->pPager, pPg, p
10e17 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 Pg->pgno);. i
10e18 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
10e19 20 29 7b 0a 20 20 20 20 20 20 70 50 67 2d 3e 66 ){. pPg->f
10e1a 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e lags &= ~PGHDR_N
10e1b 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 7d 65 EED_READ;. }e
10e1c 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
10e1d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n rc;. }. }.
10e1e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10e1f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 OK;.}../*.** If
10e20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
10e21 75 6e 74 20 68 61 73 20 72 65 61 63 68 65 64 20 unt has reached
10e22 7a 65 72 6f 2c 20 61 6e 64 20 74 68 65 20 70 61 zero, and the pa
10e23 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 ger is not in th
10e24 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 61 e.** middle of a
10e25 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
10e26 6f 6e 20 6f 72 20 6f 70 65 6e 65 64 20 69 6e 20 on or opened in
10e27 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
10e28 75 6e 6c 6f 63 6b 20 69 74 2e 0a 2a 2f 20 0a 73 unlock it..*/ .s
10e29 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 tatic void pager
10e2a 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 50 UnlockIfUnused(P
10e2b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
10e2c 20 69 66 28 20 28 73 71 6c 69 74 65 33 50 63 61 if( (sqlite3Pca
10e2d 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
10e2e 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 29 er->pPCache)==0)
10e2f 0a 20 20 20 20 26 26 20 28 21 70 50 61 67 65 72 . && (!pPager
10e30 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 ->exclusiveMode
10e31 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e || pPager->journ
10e32 61 6c 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a 20 alOff>0) . ){.
10e33 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e pagerUnlockAn
10e34 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 dRollback(pPager
10e35 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
10e36 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d Drop a page from
10e37 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e 67 the cache using
10e38 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 sqlite3PcacheDr
10e39 6f 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 op()..**.** If t
10e3a 68 69 73 20 6d 65 61 6e 73 20 74 68 65 72 65 20 his means there
10e3b 61 72 65 20 6e 6f 77 20 6e 6f 20 70 61 67 65 73 are now no pages
10e3c 20 77 69 74 68 20 72 65 66 65 72 65 6e 63 65 73 with references
10e3d 20 74 6f 20 74 68 65 6d 2c 20 61 20 72 6f 6c 6c to them, a roll
10e3e 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 20 61 back.** occurs a
10e3f 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 nd the lock on t
10e40 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 72 he database is r
10e41 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 emoved..*/.stati
10e42 63 20 76 6f 69 64 20 70 61 67 65 72 44 72 6f 70 c void pagerDrop
10e43 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 50 67 Page(DbPage *pPg
10e44 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
10e45 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
10e46 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 ;. sqlite3Pcach
10e47 65 44 72 6f 70 28 70 50 67 29 3b 0a 20 20 70 61 eDrop(pPg);. pa
10e48 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 gerUnlockIfUnuse
10e49 64 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a d(pPager);.}../*
10e4a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 70 61 .** Acquire a pa
10e4b 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 61 64 ge..**.** A read
10e4c 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 69 73 lock on the dis
10e4d 6b 20 66 69 6c 65 20 69 73 20 6f 62 74 61 69 6e k file is obtain
10e4e 65 64 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 ed when the firs
10e4f 74 20 70 61 67 65 20 69 73 20 61 63 71 75 69 72 t page is acquir
10e50 65 64 2e 20 0a 2a 2a 20 54 68 69 73 20 72 65 61 ed. .** This rea
10e51 64 20 6c 6f 63 6b 20 69 73 20 64 72 6f 70 70 65 d lock is droppe
10e52 64 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 d when the last
10e53 70 61 67 65 20 69 73 20 72 65 6c 65 61 73 65 64 page is released
10e54 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
10e55 74 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20 61 tine works for a
10e56 6e 79 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 ny page number g
10e57 72 65 61 74 65 72 20 74 68 61 6e 20 30 2e 20 20 reater than 0.
10e58 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a If the database.
10e59 2a 2a 20 66 69 6c 65 20 69 73 20 73 6d 61 6c 6c ** file is small
10e5a 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 er than the requ
10e5b 65 73 74 65 64 20 70 61 67 65 2c 20 74 68 65 6e ested page, then
10e5c 20 6e 6f 20 61 63 74 75 61 6c 20 64 69 73 6b 0a no actual disk.
10e5d 2a 2a 20 72 65 61 64 20 6f 63 63 75 72 73 20 61 ** read occurs a
10e5e 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 6d nd the memory im
10e5f 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 age of the page
10e60 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 is initialized t
10e61 6f 0a 2a 2a 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 o.** all zeros.
10e62 20 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20 The extra data
10e63 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 appended to a pa
10e64 67 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 ge is always ini
10e65 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 7a tialized.** to z
10e66 65 72 6f 73 20 74 68 65 20 66 69 72 73 74 20 74 eros the first t
10e67 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f ime a page is lo
10e68 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 aded into memory
10e69 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 ..**.** The acqu
10e6a 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 isition might fa
10e6b 69 6c 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 il for several r
10e6c 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 easons. In all
10e6d 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 cases,.** an app
10e6e 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 ropriate error c
10e6f 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
10e70 61 6e 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 and *ppPage is s
10e71 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a et to NULL..**.*
10e72 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 * See also sqlit
10e73 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e e3PagerLookup().
10e74 20 20 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 Both this rout
10e75 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 ine and Lookup()
10e76 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 attempt.** to f
10e77 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 ind a page in th
10e78 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 e in-memory cach
10e79 65 20 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 e first. If the
10e7a 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
10e7b 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 eady.** in memor
10e7c 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 y, this routine
10e7d 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 goes to disk to
10e7e 72 65 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 read it in where
10e7f 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a as Lookup().** j
10e80 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 ust returns 0.
10e81 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 This routine acq
10e82 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 uires a read-loc
10e83 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 k the first time
10e84 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f it.** has to go
10e85 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f to disk, and co
10e86 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 uld also playbac
10e87 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c k an old journal
10e88 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a if necessary..*
10e89 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 * Since Lookup()
10e8a 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 never goes to d
10e8b 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 isk, it never ha
10e8c 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c s to deal with l
10e8d 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e ocks.** or journ
10e8e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 al files..**.**
10e8f 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 If noContent is
10e90 66 61 6c 73 65 2c 20 74 68 65 20 70 61 67 65 20 false, the page
10e91 63 6f 6e 74 65 6e 74 73 20 61 72 65 20 61 63 74 contents are act
10e92 75 61 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 ually read from
10e93 64 69 73 6b 2e 0a 2a 2a 20 49 66 20 6e 6f 43 6f disk..** If noCo
10e94 6e 74 65 6e 74 20 69 73 20 74 72 75 65 2c 20 69 ntent is true, i
10e95 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 t means that we
10e96 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 do not care abou
10e97 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a t the contents.*
10e98 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 * of the page at
10e99 20 74 68 69 73 20 74 69 6d 65 2c 20 73 6f 20 64 this time, so d
10e9a 6f 20 6e 6f 74 20 64 6f 20 61 20 64 69 73 6b 20 o not do a disk
10e9b 72 65 61 64 2e 20 20 4a 75 73 74 20 66 69 6c 6c read. Just fill
10e9c 20 69 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 in the.** page
10e9d 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 content with zer
10e9e 6f 73 2e 20 20 42 75 74 20 6d 61 72 6b 20 74 68 os. But mark th
10e9f 65 20 66 61 63 74 20 74 68 61 74 20 77 65 20 68 e fact that we h
10ea0 61 76 65 20 6e 6f 74 20 72 65 61 64 20 74 68 65 ave not read the
10ea1 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 62 79 20 73 .** content by s
10ea2 65 74 74 69 6e 67 20 74 68 65 20 50 67 48 64 72 etting the PgHdr
10ea3 2e 6e 65 65 64 52 65 61 64 20 66 6c 61 67 2e 20 .needRead flag.
10ea4 20 4c 61 74 65 72 20 6f 6e 2c 20 69 66 20 0a 2a Later on, if .*
10ea5 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 * sqlite3PagerWr
10ea6 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 ite() is called
10ea7 6f 6e 20 74 68 69 73 20 70 61 67 65 20 6f 72 20 on this page or
10ea8 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 if this routine
10ea9 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 67 61 is.** called aga
10eaa 69 6e 20 77 69 74 68 20 6e 6f 43 6f 6e 74 65 6e in with noConten
10eab 74 3d 3d 30 2c 20 74 68 61 74 20 6d 65 61 6e 73 t==0, that means
10eac 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e that the conten
10ead 74 20 69 73 20 6e 65 65 64 65 64 0a 2a 2a 20 61 t is needed.** a
10eae 6e 64 20 74 68 65 20 64 69 73 6b 20 72 65 61 64 nd the disk read
10eaf 20 73 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 should occur at
10eb0 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a that point..*/.
10eb1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
10eb2 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 nt sqlite3PagerA
10eb3 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20 cquire(. Pager
10eb4 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a *pPager, /*
10eb5 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20 The pager open
10eb6 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
10eb7 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 file */. Pgno p
10eb8 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a gno, /*
10eb9 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 Page number to
10eba 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 fetch */. DbPag
10ebb 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f e **ppPage, /
10ebc 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 * Write a pointe
10ebd 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 r to the page he
10ebe 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f re */. int noCo
10ebf 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 ntent /* D
10ec0 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61 o not bother rea
10ec1 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f ding content fro
10ec2 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a m disk if true *
10ec3 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 /.){. PgHdr *pP
10ec4 67 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b g = 0;. int rc;
10ec5 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
10ec6 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
10ec7 5f 55 4e 4c 4f 43 4b 20 0a 20 20 20 20 20 20 20 _UNLOCK .
10ec8 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 || sqlite3Pcache
10ec9 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
10eca 3e 70 50 43 61 63 68 65 29 3e 30 20 0a 20 20 20 >pPCache)>0 .
10ecb 20 20 20 20 7c 7c 20 70 67 6e 6f 3d 3d 31 0a 20 || pgno==1.
10ecc 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 );.. /* The ma
10ecd 78 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 ximum page numbe
10ece 72 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 r is 2^31. Retur
10ecf 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
10ed0 20 69 66 20 61 20 70 61 67 65 0a 20 20 2a 2a 20 if a page. **
10ed1 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 number greater t
10ed2 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 7a 65 72 han this, or zer
10ed3 6f 2c 20 69 73 20 72 65 71 75 65 73 74 65 64 2e o, is requested.
10ed4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f . */. if( pgno
10ed5 3e 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 >PAGER_MAX_PGNO
10ed6 7c 7c 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 || pgno==0 || pg
10ed7 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e no==PAGER_MJ_PGN
10ed8 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 O(pPager) ){.
10ed9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
10eda 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
10edb 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
10edc 20 77 65 20 68 61 76 65 20 6e 6f 74 20 68 69 74 we have not hit
10edd 20 61 6e 79 20 63 72 69 74 69 63 61 6c 20 65 72 any critical er
10ede 72 6f 72 73 2e 0a 20 20 2a 2f 20 0a 20 20 61 73 rors.. */ . as
10edf 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 sert( pPager!=0
10ee0 29 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30 );. *ppPage = 0
10ee1 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 ;.. /* If this
10ee2 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 is the first pag
10ee3 65 20 61 63 63 65 73 73 65 64 2c 20 74 68 65 6e e accessed, then
10ee4 20 67 65 74 20 61 20 53 48 41 52 45 44 20 6c 6f get a SHARED lo
10ee5 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 ck. ** on the d
10ee6 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 70 61 atabase file. pa
10ee7 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29 20 gerSharedLock()
10ee8 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 0a 20 is a no-op if .
10ee9 20 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 6c ** a database l
10eea 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 ock is already h
10eeb 65 6c 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d eld.. */. rc =
10eec 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b pagerSharedLock
10eed 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 (pPager);. if(
10eee 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
10eef 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
10ef0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 }. assert( pP
10ef1 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
10ef2 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 ER_UNLOCK );..
10ef3 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 rc = sqlite3Pcac
10ef4 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e heFetch(pPager->
10ef5 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31 pPCache, pgno, 1
10ef6 2c 20 26 70 50 67 29 3b 0a 20 20 69 66 28 20 72 , &pPg);. if( r
10ef7 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
10ef8 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
10ef9 20 7d 0a 20 20 69 66 28 20 70 50 67 2d 3e 70 50 }. if( pPg->pP
10efa 61 67 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 2f ager==0 ){. /
10efb 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 63 68 * The pager cach
10efc 65 20 68 61 73 20 63 72 65 61 74 65 64 20 61 20 e has created a
10efd 6e 65 77 20 70 61 67 65 2e 20 49 74 73 20 63 6f new page. Its co
10efe 6e 74 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 0a ntent needs to .
10eff 20 20 20 20 2a 2a 20 62 65 20 69 6e 69 74 69 61 ** be initia
10f00 6c 69 7a 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 lized.. */.
10f01 20 20 69 6e 74 20 6e 4d 61 78 3b 0a 20 20 20 20 int nMax;.
10f02 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
10f03 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70 r->nMiss);. p
10f04 50 67 2d 3e 70 50 61 67 65 72 20 3d 20 70 50 61 Pg->pPager = pPa
10f05 67 65 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c ger;. if( sql
10f06 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
10f07 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
10f08 6c 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 l, pgno) ){.
10f09 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPg->flags |=
10f0a 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c PGHDR_IN_JOURNAL
10f0b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 ;. }. mems
10f0c 65 74 28 70 50 67 2d 3e 70 45 78 74 72 61 2c 20 et(pPg->pExtra,
10f0d 30 2c 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 0, pPager->nExtr
10f0e 61 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 a);.. rc = sq
10f0f 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
10f10 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 4d 61 unt(pPager, &nMa
10f11 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d x);. if( rc!=
10f12 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10f13 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
10f14 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 20 nref(pPg);.
10f15 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
10f16 7d 0a 0a 20 20 20 20 69 66 28 20 6e 4d 61 78 3c }.. if( nMax<
10f17 28 69 6e 74 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d (int)pgno || MEM
10f18 44 42 20 7c 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 DB || noContent
10f19 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e ){. if( pgn
10f1a 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f o>pPager->mxPgno
10f1b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
10f1c 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
10f1d 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 g);. retu
10f1e 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a rn SQLITE_FULL;.
10f1f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 }. me
10f20 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c mset(pPg->pData,
10f21 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 0, pPager->page
10f22 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Size);. if(
10f23 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 noContent ){.
10f24 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 pPg->flags
10f25 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 52 |= PGHDR_NEED_R
10f26 45 41 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 EAD;. }.
10f27 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52 IOTRACE(("ZER
10f28 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 O %p %d\n", pPag
10f29 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 er, pgno));.
10f2a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
10f2b 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50 61 = readDbPage(pPa
10f2c 67 65 72 2c 20 70 50 67 2c 20 70 67 6e 6f 29 3b ger, pPg, pgno);
10f2d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
10f2e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d QLITE_OK && rc!=
10f2f 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
10f30 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 RT_READ ){.
10f31 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 50 61 67 /* sqlite3Pag
10f32 65 72 55 6e 72 65 66 28 70 50 67 29 3b 20 2a 2f erUnref(pPg); */
10f33 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 44 72 . pagerDr
10f34 6f 70 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 opPage(pPg);.
10f35 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
10f36 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 }. }.#i
10f37 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 fdef SQLITE_CHEC
10f38 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d K_PAGES. pPg-
10f39 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 >pageHash = page
10f3a 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b r_pagehash(pPg);
10f3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b .#endif. }else{
10f3c 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 . /* The requ
10f3d 65 73 74 65 64 20 70 61 67 65 20 69 73 20 69 6e ested page is in
10f3e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e the page cache.
10f3f 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 73 */. assert(s
10f40 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
10f41 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
10f42 61 63 68 65 29 3e 30 20 7c 7c 20 70 67 6e 6f 3d ache)>0 || pgno=
10f43 3d 31 29 3b 0a 20 20 20 20 50 41 47 45 52 5f 49 =1);. PAGER_I
10f44 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 48 69 74 NCR(pPager->nHit
10f45 29 3b 0a 20 20 20 20 69 66 28 20 21 6e 6f 43 6f );. if( !noCo
10f46 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 ntent ){. r
10f47 63 20 3d 20 70 61 67 65 72 5f 67 65 74 5f 63 6f c = pager_get_co
10f48 6e 74 65 6e 74 28 70 50 67 29 3b 0a 20 20 20 20 ntent(pPg);.
10f49 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
10f4a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
10f4b 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 Unref(pPg);.
10f4c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
10f4d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
10f4e 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 .. *ppPage = pP
10f4f 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 g;. return SQLI
10f50 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
10f51 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69 Acquire a page i
10f52 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 f it is already
10f53 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 in the in-memory
10f54 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e cache. Do.** n
10f55 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65 ot read the page
10f56 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 from disk. Ret
10f57 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
10f58 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 the page,.** or
10f59 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69 0 if the page i
10f5a 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 0a s not in cache..
10f5b 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 **.** See also s
10f5c 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
10f5d 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 . The differenc
10f5e 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 e between this r
10f5f 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 outine.** and sq
10f60 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 lite3PagerGet()
10f61 69 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 77 is that _get() w
10f62 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 69 ill go to the di
10f63 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 69 sk and read.** i
10f64 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68 n the page if th
10f65 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c e page is not al
10f66 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20 ready in cache.
10f67 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a This routine.**
10f68 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 returns NULL if
10f69 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
10f6a 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20 in cache or if
10f6b 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 a disk I/O error
10f6c 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61 .** has ever ha
10f6d 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 ppened..*/.SQLIT
10f6e 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65 E_PRIVATE DbPage
10f6f 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f *sqlite3PagerLo
10f70 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 okup(Pager *pPag
10f71 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
10f72 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30 PgHdr *pPg = 0
10f73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
10f74 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 er!=0 );. asser
10f75 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 0a 20 t( pgno!=0 );..
10f76 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 73 74 if( (pPager->st
10f77 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 ate!=PAGER_UNLOC
10f78 4b 29 0a 20 20 20 26 26 20 28 70 50 61 67 65 72 K). && (pPager
10f79 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 ->errCode==SQLIT
10f7a 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e E_OK || pPager->
10f7b 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f errCode==SQLITE_
10f7c 46 55 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20 73 FULL). ){. s
10f7d 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 qlite3PcacheFetc
10f7e 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 h(pPager->pPCach
10f7f 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50 67 e, pgno, 0, &pPg
10f80 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e );. }.. return
10f81 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 pPg;.}../*.** R
10f82 65 6c 65 61 73 65 20 61 20 70 61 67 65 2e 0a 2a elease a page..*
10f83 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 *.** If the numb
10f84 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
10f85 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f to the page dro
10f86 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 p to zero, then
10f87 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 the.** page is a
10f88 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 dded to the LRU
10f89 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 list. When all
10f8a 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c references to al
10f8b 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 l pages.** are r
10f8c 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 eleased, a rollb
10f8d 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 ack occurs and t
10f8e 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 he lock on the d
10f8f 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 atabase is.** re
10f90 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 moved..*/.SQLITE
10f91 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
10f92 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 ite3PagerUnref(D
10f93 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 69 bPage *pPg){. i
10f94 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 61 f( pPg ){. Pa
10f95 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
10f96 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 73 g->pPager;. s
10f97 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 qlite3PcacheRele
10f98 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 61 ase(pPg);. pa
10f99 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 gerUnlockIfUnuse
10f9a 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 d(pPager);. }.
10f9b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
10f9c 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 K;.}../*.** Crea
10f9d 74 65 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c te a journal fil
10f9e 65 20 66 6f 72 20 70 50 61 67 65 72 2e 20 20 54 e for pPager. T
10f9f 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 here should alre
10fa0 61 64 79 20 62 65 20 61 20 52 45 53 45 52 56 45 ady be a RESERVE
10fa1 44 0a 2a 2a 20 6f 72 20 45 58 43 4c 55 53 49 56 D.** or EXCLUSIV
10fa2 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 E lock on the da
10fa3 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e tabase file when
10fa4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
10fa5 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 called..**.** R
10fa6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
10fa7 69 66 20 65 76 65 72 79 74 68 69 6e 67 2e 20 20 if everything.
10fa8 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
10fa9 63 6f 64 65 20 61 6e 64 20 72 65 6c 65 61 73 65 code and release
10faa 20 74 68 65 0a 2a 2a 20 77 72 69 74 65 20 6c 6f the.** write lo
10fab 63 6b 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 ck if anything g
10fac 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 oes wrong..*/.st
10fad 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 6f atic int pager_o
10fae 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61 67 65 pen_journal(Page
10faf 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 71 r *pPager){. sq
10fb0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 lite3_vfs *pVfs
10fb1 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a = pPager->pVfs;.
10fb2 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 int flags = (S
10fb3 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
10fb4 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
10fb5 5f 45 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 _EXCLUSIVE|SQLIT
10fb6 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a E_OPEN_CREATE);.
10fb7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 . int rc;. ass
10fb8 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
10fb9 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te>=PAGER_RESERV
10fba 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ED );. assert(
10fbb 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e pPager->useJourn
10fbc 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 al );. assert(
10fbd 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
10fbe 61 6c 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 al==0 );. sqlit
10fbf 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
10fc0 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 70 (pPager, 0);. p
10fc1 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
10fc2 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 l = sqlite3Bitve
10fc3 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e cCreate(pPager->
10fc4 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 dbSize);. if( p
10fc5 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
10fc6 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d l==0 ){. rc =
10fc7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
10fc8 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f 74 goto failed_t
10fc9 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a o_open_journal;.
10fca 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 }.. if( pPage
10fcb 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d r->journalOpen==
10fcc 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 0 ){. if( pPa
10fcd 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b ger->tempFile ){
10fce 0a 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 . flags |=
10fcf 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c (SQLITE_OPEN_DEL
10fd0 45 54 45 4f 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 ETEONCLOSE|SQLIT
10fd1 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 E_OPEN_TEMP_JOUR
10fd2 4e 41 4c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b NAL);. }else{
10fd3 0a 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 . flags |=
10fd4 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 (SQLITE_OPEN_MAI
10fd5 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 N_JOURNAL);.
10fd6 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 }. if( pPager
10fd7 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
10fd8 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
10fd9 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 _MEMORY ){.
10fda 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e sqlite3MemJourn
10fdb 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a alOpen(pPager->j
10fdc 66 64 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 fd);. rc =
10fdd 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
10fde 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51 4c else{.#ifdef SQL
10fdf 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 ITE_ENABLE_ATOMI
10fe0 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 72 63 C_WRITE. rc
10fe1 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 = sqlite3Journa
10fe2 6c 4f 70 65 6e 28 0a 20 20 20 20 20 20 20 20 20 lOpen(.
10fe3 20 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a pVfs, pPager->z
10fe4 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d Journal, pPager-
10fe5 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 6a 72 6e >jfd, flags, jrn
10fe6 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 lBufferSize(pPag
10fe7 65 72 29 0a 20 20 20 20 20 20 29 3b 0a 23 65 6c er). );.#el
10fe8 73 65 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 se. rc = sq
10fe9 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 lite3OsOpen(pVfs
10fea 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e , pPager->zJourn
10feb 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c al, pPager->jfd,
10fec 20 66 6c 61 67 73 2c 20 30 29 3b 0a 23 65 6e 64 flags, 0);.#end
10fed 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 if. }. ass
10fee 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
10fef 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 66 OK || pPager->jf
10ff0 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a 20 d->pMethods );.
10ff1 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
10ff2 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 alOff = 0;. p
10ff3 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
10ff4 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
10ff5 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 ->journalHdr = 0
10ff6 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
10ff7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
10ff8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
10ff9 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 20 NOMEM ){.
10ffa 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
10ffb 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
10ffc 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 Journal, 0);.
10ffd 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
10ffe 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a failed_to_open_j
10fff 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 ournal;. }.
11000 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 }. pPager->jour
11001 6e 61 6c 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 70 nalOpen = 1;. p
11002 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 Pager->journalSt
11003 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 70 50 61 arted = 0;. pPa
11004 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 ger->needSync =
11005 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 0;. pPager->nRe
11006 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 c = 0;. if( pPa
11007 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a ger->errCode ){.
11008 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d rc = pPager-
11009 3e 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 67 6f >errCode;. go
1100a 74 6f 20 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65 to failed_to_ope
1100b 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a 20 20 7d 0a 20 n_journal;. }.
1100c 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 pPager->origDbS
1100d 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 ize = pPager->db
1100e 53 69 7a 65 3b 0a 0a 20 20 72 63 20 3d 20 77 72 Size;.. rc = wr
1100f 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 iteJournalHdr(pP
11010 61 67 65 72 29 3b 0a 0a 20 20 69 66 28 20 70 50 ager);.. if( pP
11011 61 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 ager->stmtAutoop
11012 65 6e 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 en && rc==SQLITE
11013 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 _OK ){. rc =
11014 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 sqlite3PagerStmt
11015 42 65 67 69 6e 28 70 50 61 67 65 72 29 3b 0a 20 Begin(pPager);.
11016 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c }. if( rc!=SQL
11017 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 ITE_OK && rc!=SQ
11018 4c 49 54 45 5f 4e 4f 4d 45 4d 20 26 26 20 72 63 LITE_NOMEM && rc
11019 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e !=SQLITE_IOERR_N
1101a 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 72 63 20 3d OMEM ){. rc =
1101b 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 pager_end_trans
1101c 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 action(pPager, 0
1101d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1101e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1101f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 rc = SQLITE_FU
11020 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 LL;. }. }.
11021 72 65 74 75 72 6e 20 72 63 3b 0a 0a 66 61 69 6c return rc;..fail
11022 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e ed_to_open_journ
11023 61 6c 3a 0a 20 20 73 71 6c 69 74 65 33 42 69 74 al:. sqlite3Bit
11024 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
11025 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a r->pInJournal);.
11026 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 pPager->pInJou
11027 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 rnal = 0;. retu
11028 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
11029 41 63 71 75 69 72 65 20 61 20 77 72 69 74 65 2d Acquire a write-
1102a 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
1102b 62 61 73 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20 base. The lock
1102c 69 73 20 72 65 6d 6f 76 65 64 20 77 68 65 6e 0a is removed when.
1102d 2a 2a 20 74 68 65 20 61 6e 79 20 6f 66 20 74 68 ** the any of th
1102e 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 68 61 70 70 e following happ
1102f 65 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 73 en:.**.** * s
11030 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 qlite3PagerCommi
11031 74 50 68 61 73 65 54 77 6f 28 29 20 69 73 20 63 tPhaseTwo() is c
11032 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 alled..** * s
11033 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 qlite3PagerRollb
11034 61 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e ack() is called.
11035 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 .** * sqlite3
11036 50 61 67 65 72 43 6c 6f 73 65 28 29 20 69 73 20 PagerClose() is
11037 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 called..** *
11038 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
11039 66 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 6f f() is called to
1103a 20 6f 6e 20 65 76 65 72 79 20 6f 75 74 73 74 61 on every outsta
1103b 6e 64 69 6e 67 20 70 61 67 65 2e 0a 2a 2a 0a 2a nding page..**.*
1103c 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 * The first para
1103d 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 72 6f meter to this ro
1103e 75 74 69 6e 65 20 69 73 20 61 20 70 6f 69 6e 74 utine is a point
1103f 65 72 20 74 6f 20 61 6e 79 20 6f 70 65 6e 20 70 er to any open p
11040 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 age of the.** da
11041 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f tabase file. No
11042 74 68 69 6e 67 20 63 68 61 6e 67 65 73 20 61 62 thing changes ab
11043 6f 75 74 20 74 68 65 20 70 61 67 65 20 2d 20 69 out the page - i
11044 74 20 69 73 20 75 73 65 64 20 6d 65 72 65 6c 79 t is used merely
11045 20 74 6f 0a 2a 2a 20 61 63 71 75 69 72 65 20 61 to.** acquire a
11046 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
11047 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 Pager structure
11048 61 6e 64 20 61 73 20 70 72 6f 6f 66 20 74 68 61 and as proof tha
11049 74 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6c t there is.** al
1104a 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 ready a read-loc
1104b 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
1104c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 e..**.** The sec
1104d 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 6e ond parameter in
1104e 64 69 63 61 74 65 73 20 68 6f 77 20 6d 75 63 68 dicates how much
1104f 20 73 70 61 63 65 20 69 6e 20 62 79 74 65 73 20 space in bytes
11050 74 6f 20 72 65 73 65 72 76 65 20 66 6f 72 20 61 to reserve for a
11051 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e .** master journ
11052 61 6c 20 66 69 6c 65 2d 6e 61 6d 65 20 61 74 20 al file-name at
11053 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
11054 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 20 69 74 journal when it
11055 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a is created..**.
11056 2a 2a 20 41 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ** A journal fil
11057 65 20 69 73 20 6f 70 65 6e 65 64 20 69 66 20 74 e is opened if t
11058 68 69 73 20 69 73 20 6e 6f 74 20 61 20 74 65 6d his is not a tem
11059 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 46 6f porary file. Fo
1105a 72 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 66 r temporary.** f
1105b 69 6c 65 73 2c 20 74 68 65 20 6f 70 65 6e 69 6e iles, the openin
1105c 67 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c g of the journal
1105d 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 65 file is deferre
1105e 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 73 d until there is
1105f 20 61 6e 0a 2a 2a 20 61 63 74 75 61 6c 20 6e 65 an.** actual ne
11060 65 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 ed to write to t
11061 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a he journal..**.*
11062 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
11063 65 20 69 73 20 61 6c 72 65 61 64 79 20 72 65 73 e is already res
11064 65 72 76 65 64 20 66 6f 72 20 77 72 69 74 69 6e erved for writin
11065 67 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 g, this routine
11066 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
11067 2a 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 * If exFlag is t
11068 72 75 65 2c 20 67 6f 20 61 68 65 61 64 20 61 6e rue, go ahead an
11069 64 20 67 65 74 20 61 6e 20 45 58 43 4c 55 53 49 d get an EXCLUSI
1106a 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 VE lock on the f
1106b 69 6c 65 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 ile.** immediate
1106c 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 20 77 61 ly instead of wa
1106d 69 74 69 6e 67 20 75 6e 74 69 6c 20 77 65 20 74 iting until we t
1106e 72 79 20 74 6f 20 66 6c 75 73 68 20 74 68 65 20 ry to flush the
1106f 63 61 63 68 65 2e 20 20 54 68 65 0a 2a 2a 20 65 cache. The.** e
11070 78 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 xFlag is ignored
11071 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f if a transactio
11072 6e 20 69 73 20 61 6c 72 65 61 64 79 20 61 63 74 n is already act
11073 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ive..*/.SQLITE_P
11074 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
11075 65 33 50 61 67 65 72 42 65 67 69 6e 28 44 62 50 e3PagerBegin(DbP
11076 61 67 65 20 2a 70 50 67 2c 20 69 6e 74 20 65 78 age *pPg, int ex
11077 46 6c 61 67 29 7b 0a 20 20 50 61 67 65 72 20 2a Flag){. Pager *
11078 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
11079 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d ager;. int rc =
1107a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 SQLITE_OK;. as
1107b 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e sert( pPg->nRef>
1107c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1107d 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 Pager->state!=PA
1107e 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 GER_UNLOCK );.
1107f 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
11080 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 e==PAGER_SHARED
11081 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
11082 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
11083 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 l==0 );. asse
11084 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 rt( !MEMDB );.
11085 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 41 sqlite3PcacheA
11086 73 73 65 72 74 46 6c 61 67 73 28 70 50 61 67 65 ssertFlags(pPage
11087 72 2d 3e 70 50 43 61 63 68 65 2c 20 30 2c 20 50 r->pPCache, 0, P
11088 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c 29 GHDR_IN_JOURNAL)
11089 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1108a 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d e3OsLock(pPager-
1108b 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f >fd, RESERVED_LO
1108c 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d CK);. if( rc=
1108d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1108e 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
1108f 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56 e = PAGER_RESERV
11090 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20 65 78 ED;. if( ex
11091 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 Flag ){.
11092 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f rc = pager_wait_
11093 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 on_lock(pPager,
11094 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
11095 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
11096 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
11097 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
11098 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
11099 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 pPager->dirty
1109a 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 20 20 50 Cache = 0;. P
1109b 41 47 45 52 54 52 41 43 45 32 28 22 54 52 41 4e AGERTRACE2("TRAN
1109c 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50 SACTION %d\n", P
1109d 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b AGERID(pPager));
1109e 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
1109f 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 26 26 20 21 >useJournal && !
110a0 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
110a1 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 . && p
110a2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
110a3 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de!=PAGER_JOURNA
110a4 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 LMODE_OFF ){.
110a5 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 rc = pager_op
110a6 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 en_journal(pPage
110a7 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 r);. }. }els
110a8 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f e if( pPager->jo
110a9 75 72 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50 61 urnalOpen && pPa
110aa 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
110ab 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 =0 ){. /* Thi
110ac 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74 s happens when t
110ad 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 he pager was in
110ae 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 exclusive-access
110af 20 6d 6f 64 65 20 74 68 65 20 6c 61 73 74 0a 20 mode the last.
110b0 20 20 20 2a 2a 20 74 69 6d 65 20 61 20 28 72 65 ** time a (re
110b1 61 64 20 6f 72 20 77 72 69 74 65 29 20 74 72 61 ad or write) tra
110b2 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 73 75 63 nsaction was suc
110b3 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c 75 cessfully conclu
110b4 64 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 ded. ** by th
110b5 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 is connection. I
110b6 6e 73 74 65 61 64 20 6f 66 20 64 65 6c 65 74 69 nstead of deleti
110b7 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ng the journal f
110b8 69 6c 65 20 69 74 20 77 61 73 20 0a 20 20 20 20 ile it was .
110b9 2a 2a 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 ** kept open and
110ba 20 65 69 74 68 65 72 20 77 61 73 20 74 72 75 6e either was trun
110bb 63 61 74 65 64 20 74 6f 20 30 20 62 79 74 65 73 cated to 0 bytes
110bc 20 6f 72 20 69 74 73 20 68 65 61 64 65 72 20 77 or its header w
110bd 61 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77 72 as. ** overwr
110be 69 74 74 65 6e 20 77 69 74 68 20 7a 65 72 6f 73 itten with zeros
110bf 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
110c0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52 65 ert( pPager->nRe
110c1 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 c==0 );. asse
110c2 72 74 28 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 rt( pPager->orig
110c3 44 62 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 20 DbSize==0 );.
110c4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
110c5 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 >pInJournal==0 )
110c6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 ;. sqlite3Pag
110c7 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
110c8 65 72 2c 20 30 29 3b 0a 20 20 20 20 70 50 61 67 er, 0);. pPag
110c9 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d er->pInJournal =
110ca 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 sqlite3BitvecCr
110cb 65 61 74 65 28 20 70 50 61 67 65 72 2d 3e 64 62 eate( pPager->db
110cc 53 69 7a 65 20 29 3b 0a 20 20 20 20 69 66 28 20 Size );. if(
110cd 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 !pPager->pInJour
110ce 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 nal ){. rc
110cf 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
110d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
110d1 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 pPager->origDbS
110d2 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 ize = pPager->db
110d3 53 69 7a 65 3b 0a 20 20 20 20 20 20 72 63 20 3d Size;. rc =
110d4 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 writeJournalHdr
110d5 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a (pPager);. }.
110d6 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 }. assert( !p
110d7 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 Pager->journalOp
110d8 65 6e 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f en || pPager->jo
110d9 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 63 urnalOff>0 || rc
110da 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 !=SQLITE_OK );.
110db 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
110dc 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 70 61 67 65 *.** Make a page
110dd 20 64 69 72 74 79 2e 20 20 53 65 74 20 69 74 73 dirty. Set its
110de 20 64 69 72 74 79 20 66 6c 61 67 20 61 6e 64 20 dirty flag and
110df 61 64 64 20 69 74 20 74 6f 20 74 68 65 20 64 69 add it to the di
110e0 72 74 79 0a 2a 2a 20 70 61 67 65 20 6c 69 73 74 rty.** page list
110e1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
110e2 20 6d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 makeDirty(PgHdr
110e3 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 *pPg){. sqlite
110e4 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
110e5 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (pPg);.}../*.**
110e6 4d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 61 Make a page clea
110e7 6e 2e 20 20 43 6c 65 61 72 20 69 74 73 20 64 69 n. Clear its di
110e8 72 74 79 20 62 69 74 20 61 6e 64 20 72 65 6d 6f rty bit and remo
110e9 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a ve it from the.*
110ea 2a 20 64 69 72 74 79 20 70 61 67 65 20 6c 69 73 * dirty page lis
110eb 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
110ec 64 20 6d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 d makeClean(PgHd
110ed 72 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 r *pPg){. sqlit
110ee 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
110ef 6e 28 70 50 67 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a n(pPg);.}.../*.*
110f0 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20 70 61 * Mark a data pa
110f1 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e ge as writeable.
110f2 20 20 54 68 65 20 70 61 67 65 20 69 73 20 77 72 The page is wr
110f3 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
110f4 6f 75 72 6e 61 6c 20 0a 2a 2a 20 69 66 20 69 74 ournal .** if it
110f5 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c is not there al
110f6 72 65 61 64 79 2e 20 20 54 68 69 73 20 72 6f 75 ready. This rou
110f7 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c tine must be cal
110f8 6c 65 64 20 62 65 66 6f 72 65 20 6d 61 6b 69 6e led before makin
110f9 67 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 g.** changes to
110fa 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 a page..**.** Th
110fb 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 e first time thi
110fc 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
110fd 6c 65 64 2c 20 74 68 65 20 70 61 67 65 72 20 63 led, the pager c
110fe 72 65 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 reates a new.**
110ff 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 61 63 71 75 journal and acqu
11100 69 72 65 73 20 61 20 52 45 53 45 52 56 45 44 20 ires a RESERVED
11101 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
11102 62 61 73 65 2e 20 20 49 66 20 74 68 65 20 52 45 base. If the RE
11103 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 63 SERVED.** lock c
11104 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 71 75 ould not be acqu
11105 69 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69 ired, this routi
11106 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ne returns SQLIT
11107 45 5f 42 55 53 59 2e 20 20 54 68 65 0a 2a 2a 20 E_BUSY. The.**
11108 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 calling routine
11109 6d 75 73 74 20 63 68 65 63 6b 20 66 6f 72 20 74 must check for t
1110a 68 61 74 20 72 65 74 75 72 6e 20 76 61 6c 75 65 hat return value
1110b 20 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c 20 and be careful
1110c 6e 6f 74 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 not to.** change
1110d 20 61 6e 79 20 70 61 67 65 20 64 61 74 61 20 75 any page data u
1110e 6e 74 69 6c 20 74 68 69 73 20 72 6f 75 74 69 6e ntil this routin
1110f 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
11110 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 _OK..**.** If th
11111 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 e journal file c
11112 6f 75 6c 64 20 6e 6f 74 20 62 65 20 77 72 69 74 ould not be writ
11113 74 65 6e 20 62 65 63 61 75 73 65 20 74 68 65 20 ten because the
11114 64 69 73 6b 20 69 73 20 66 75 6c 6c 2c 0a 2a 2a disk is full,.**
11115 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
11116 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ne returns SQLIT
11117 45 5f 46 55 4c 4c 20 61 6e 64 20 64 6f 65 73 20 E_FULL and does
11118 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 72 6f 6c an immediate rol
11119 6c 62 61 63 6b 2e 0a 2a 2a 20 41 6c 6c 20 73 75 lback..** All su
1111a 62 73 65 71 75 65 6e 74 20 77 72 69 74 65 20 61 bsequent write a
1111b 74 74 65 6d 70 74 73 20 61 6c 73 6f 20 72 65 74 ttempts also ret
1111c 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 urn SQLITE_FULL
1111d 75 6e 74 69 6c 20 74 68 65 72 65 0a 2a 2a 20 69 until there.** i
1111e 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 s a call to sqli
1111f 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 28 29 te3PagerCommit()
11120 20 6f 72 20 73 71 6c 69 74 65 33 50 61 67 65 72 or sqlite3Pager
11121 52 6f 6c 6c 62 61 63 6b 28 29 20 74 6f 0a 2a 2a Rollback() to.**
11122 20 72 65 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 reset..*/.stati
11123 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 c int pager_writ
11124 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 e(PgHdr *pPg){.
11125 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70 void *pData = p
11126 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 50 61 67 Pg->pData;. Pag
11127 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 er *pPager = pPg
11128 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 ->pPager;. int
11129 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1112a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 . /* Check for
1112b 65 72 72 6f 72 73 0a 20 20 2a 2f 0a 20 20 69 66 errors. */. if
1112c 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
1112d 65 20 29 7b 20 0a 20 20 20 20 72 65 74 75 72 6e e ){ . return
1112e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
1112f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 ;. }. if( pPag
11130 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a er->readOnly ){.
11131 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
11132 45 5f 50 45 52 4d 3b 0a 20 20 7d 0a 0a 20 20 61 E_PERM;. }.. a
11133 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e ssert( !pPager->
11134 73 65 74 4d 61 73 74 65 72 20 29 3b 0a 0a 20 20 setMaster );..
11135 43 48 45 43 4b 5f 50 41 47 45 28 70 50 67 29 3b CHECK_PAGE(pPg);
11136 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 .. /* If this p
11137 61 67 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 age was previous
11138 6c 79 20 61 63 71 75 69 72 65 64 20 77 69 74 68 ly acquired with
11139 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74 noContent==1, t
1113a 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 77 hat means. ** w
1113b 65 20 64 69 64 6e 27 74 20 72 65 61 6c 6c 79 20 e didn't really
1113c 72 65 61 64 20 69 6e 20 74 68 65 20 63 6f 6e 74 read in the cont
1113d 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e ent of the page.
1113e 20 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 This can happe
1113f 6e 0a 20 20 2a 2a 20 28 66 6f 72 20 65 78 61 6d n. ** (for exam
11140 70 6c 65 29 20 77 68 65 6e 20 74 68 65 20 70 61 ple) when the pa
11141 67 65 20 69 73 20 62 65 69 6e 67 20 6d 6f 76 65 ge is being move
11142 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 d to the freelis
11143 74 2e 20 20 42 75 74 0a 20 20 2a 2a 20 6e 6f 77 t. But. ** now
11144 20 77 65 20 61 72 65 20 28 70 65 72 68 61 70 73 we are (perhaps
11145 29 20 6d 6f 76 69 6e 67 20 74 68 65 20 70 61 67 ) moving the pag
11146 65 20 6f 66 66 20 6f 66 20 74 68 65 20 66 72 65 e off of the fre
11147 65 6c 69 73 74 20 66 6f 72 0a 20 20 2a 2a 20 72 elist for. ** r
11148 65 75 73 65 20 61 6e 64 20 77 65 20 6e 65 65 64 euse and we need
11149 20 74 6f 20 6b 6e 6f 77 20 69 74 73 20 6f 72 69 to know its ori
1114a 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 73 6f ginal content so
1114b 20 74 68 61 74 20 63 6f 6e 74 65 6e 74 0a 20 20 that content.
1114c 2a 2a 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 ** can be stored
1114d 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b in the rollback
1114e 20 6a 6f 75 72 6e 61 6c 2e 20 20 53 6f 20 64 6f journal. So do
1114f 20 74 68 65 20 72 65 61 64 20 61 74 20 74 68 69 the read at thi
11150 73 0a 20 20 2a 2a 20 74 69 6d 65 2e 0a 20 20 2a s. ** time.. *
11151 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 67 /. rc = pager_g
11152 65 74 5f 63 6f 6e 74 65 6e 74 28 70 50 67 29 3b et_content(pPg);
11153 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
11154 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
11155 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 . /* Mark the p
11156 61 67 65 20 61 73 20 64 69 72 74 79 2e 20 20 49 age as dirty. I
11157 66 20 74 68 65 20 70 61 67 65 20 68 61 73 20 61 f the page has a
11158 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 lready been writ
11159 74 65 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 ten. ** to the
1115a 6a 6f 75 72 6e 61 6c 20 74 68 65 6e 20 77 65 20 journal then we
1115b 63 61 6e 20 72 65 74 75 72 6e 20 72 69 67 68 74 can return right
1115c 20 61 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 6d 61 away.. */. ma
1115d 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 keDirty(pPg);.
1115e 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 if( (pPg->flags&
1115f 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c PGHDR_IN_JOURNAL
11160 29 0a 20 20 20 26 26 20 28 70 61 67 65 49 6e 53 ). && (pageInS
11161 74 61 74 65 6d 65 6e 74 28 70 50 67 29 20 7c 7c tatement(pPg) ||
11162 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 pPager->stmtInU
11163 73 65 3d 3d 30 29 20 0a 20 20 29 7b 0a 20 20 20 se==0) . ){.
11164 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 pPager->dirtyCa
11165 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 che = 1;. pPa
11166 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 ger->dbModified
11167 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 = 1;. }else{..
11168 20 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 20 /* If we get
11169 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 61 this far, it mea
1116a 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ns that the page
1116b 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 needs to be.
1116c 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ** written to t
1116d 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a he transaction j
1116e 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b ournal or the ck
1116f 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c eckpoint journal
11170 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e . ** or both.
11171 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 . **. ** F
11172 69 72 73 74 20 63 68 65 63 6b 20 74 6f 20 73 65 irst check to se
11173 65 20 74 68 61 74 20 74 68 65 20 74 72 61 6e 73 e that the trans
11174 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 65 action journal e
11175 78 69 73 74 73 20 61 6e 64 0a 20 20 20 20 2a 2a xists and. **
11176 20 63 72 65 61 74 65 20 69 74 20 69 66 20 69 74 create it if it
11177 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20 20 20 2a does not.. *
11178 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 /. assert( pP
11179 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
1117a 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 ER_UNLOCK );.
1117b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1117c 65 72 42 65 67 69 6e 28 70 50 67 2c 20 30 29 3b erBegin(pPg, 0);
1117d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1117e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1117f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
11180 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
11181 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
11182 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 R_RESERVED );.
11183 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a if( !pPager->j
11184 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50 ournalOpen && pP
11185 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c ager->useJournal
11186 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 70 50 . && pP
11187 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
11188 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e!=PAGER_JOURNAL
11189 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 MODE_OFF ){.
1118a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 rc = pager_ope
1118b 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 n_journal(pPager
1118c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1118d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
1118e 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1118f 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 pPager->dirtyC
11190 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 ache = 1;. pP
11191 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
11192 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 = 1;. . /*
11193 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 The transaction
11194 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73 journal now exis
11195 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 61 ts and we have a
11196 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e 0a RESERVED or an.
11197 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 ** EXCLUSIVE
11198 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69 lock on the mai
11199 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
1119a 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 Write the curr
1119b 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 20 ent page to.
1119c 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ** the transacti
1119d 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74 on journal if it
1119e 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c is not there al
1119f 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 ready.. */.
111a0 20 20 69 66 28 20 21 28 70 50 67 2d 3e 66 6c 61 if( !(pPg->fla
111a1 67 73 26 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 gs&PGHDR_IN_JOUR
111a2 4e 41 4c 29 20 26 26 20 70 50 61 67 65 72 2d 3e NAL) && pPager->
111a3 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 journalOpen ){.
111a4 20 20 20 20 20 69 66 28 20 28 69 6e 74 29 70 50 if( (int)pP
111a5 67 2d 3e 70 67 6e 6f 20 3c 3d 20 70 50 61 67 65 g->pgno <= pPage
111a6 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 7b r->origDbSize ){
111a7 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 . u32 cks
111a8 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 um;. char
111a9 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 *pData2;..
111aa 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 /* We should
111ab 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 never write to t
111ac 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
111ad 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 the page that.
111ae 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e ** contain
111af 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c s the database l
111b0 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f ocks. The follo
111b1 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 wing assert veri
111b2 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 fies. **
111b3 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 that we do not.
111b4 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
111b5 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 t( pPg->pgno!=PA
111b6 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
111b7 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 er) );. p
111b8 44 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 Data2 = CODEC2(p
111b9 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 Pager, pData, pP
111ba 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20 g->pgno, 7);.
111bb 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 cksum = pag
111bc 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c er_cksum(pPager,
111bd 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 (u8*)pData2);.
111be 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 rc = writ
111bf 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
111c0 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 jfd, pPager->jou
111c1 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 rnalOff, pPg->pg
111c2 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 no);. if(
111c3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
111c4 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
111c5 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
111c6 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 pPager->jfd, pDa
111c7 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 ta2, pPager->pag
111c8 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 eSize,.
111c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
111ca 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
111cb 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20 rnalOff + 4);.
111cc 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
111cd 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 journalOff += pP
111ce 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 ager->pageSize+4
111cf 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
111d0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
111d1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
111d2 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 rc = write32b
111d3 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
111d4 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
111d5 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 Off, cksum);.
111d6 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
111d7 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a ournalOff += 4;.
111d8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
111d9 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54 IOTRACE(("JOUT
111da 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e %p %d %lld %d\n
111db 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e ", pPager, pPg->
111dc 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 pgno, .
111dd 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
111de 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 journalOff, pPag
111df 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a er->pageSize));.
111e0 20 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e PAGER_IN
111e1 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 CR(sqlite3_pager
111e2 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a _writej_count);.
111e3 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 PAGERTRA
111e4 43 45 35 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 CE5("JOURNAL %d
111e5 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 page %d needSync
111e6 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e =%d hash(%08x)\n
111e7 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
111e8 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
111e9 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 pPg->pgno, .
111ea 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d ((pPg-
111eb 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
111ec 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61 D_SYNC)?1:0), pa
111ed 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
111ee 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 ));.. /*
111ef 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 An error has occ
111f0 75 72 65 64 20 77 72 69 74 69 6e 67 20 74 6f 20 ured writing to
111f1 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
111f2 2e 20 54 68 65 20 0a 20 20 20 20 20 20 20 20 2a . The . *
111f3 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 * transaction wi
111f4 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 ll be rolled bac
111f5 6b 20 62 79 20 74 68 65 20 6c 61 79 65 72 20 61 k by the layer a
111f6 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f bove.. */
111f7 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
111f8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
111f9 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
111fa 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 c;. }..
111fb 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 pPager->nR
111fc 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 ec++;. as
111fd 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 sert( pPager->pI
111fe 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 nJournal!=0 );.
111ff 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 sqlite3Bi
11200 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e tvecSet(pPager->
11201 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d pInJournal, pPg-
11202 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 >pgno);.
11203 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 if( !pPager->noS
11204 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ync ){.
11205 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 pPg->flags |= P
11206 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a GHDR_NEED_SYNC;.
11207 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
11208 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
11209 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 20 mtInUse ){.
1120a 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
1120b 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49 ecSet(pPager->pI
1120c 6e 53 74 6d 74 2c 20 70 50 67 2d 3e 70 67 6e 6f nStmt, pPg->pgno
1120d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1120e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1120f 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a if( !pPager->j
11210 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 26 ournalStarted &&
11211 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 !pPager->noSync
11212 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 ){. pP
11213 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 g->flags |= PGHD
11214 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 R_NEED_SYNC;.
11215 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 50 }. P
11216 41 47 45 52 54 52 41 43 45 34 28 22 41 50 50 45 AGERTRACE4("APPE
11217 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 ND %d page %d ne
11218 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 edSync=%d\n",.
11219 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 PA
1121a 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
1121b 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 Pg->pgno,.
1121c 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e ((pPg->
1121d 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
1121e 5f 53 59 4e 43 29 3f 31 3a 30 29 29 3b 0a 20 20 _SYNC)?1:0));.
1121f 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
11220 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
11221 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 _NEED_SYNC ){.
11222 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 pPager->ne
11223 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
11224 20 20 7d 0a 20 20 20 20 20 20 70 50 67 2d 3e 66 }. pPg->f
11225 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 49 4e lags |= PGHDR_IN
11226 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 7d 0a _JOURNAL;. }.
11227 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
11228 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
11229 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 al is open and t
1122a 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 he page is not i
1122b 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 n it,. ** the
1122c 6e 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 n write the curr
1122d 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 ent page to the
1122e 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 statement journa
1122f 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 l. Note that.
11230 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 ** the stateme
11231 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 nt journal forma
11232 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 t differs from t
11233 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 he standard jour
11234 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a nal format. *
11235 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 * in that it omi
11236 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 ts the checksums
11237 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e and the header.
11238 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
11239 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 pPager->stmtInUs
1123a 65 20 0a 20 20 20 20 20 26 26 20 21 70 61 67 65 e . && !page
1123b 49 6e 53 74 61 74 65 6d 65 6e 74 28 70 50 67 29 InStatement(pPg)
1123c 20 0a 20 20 20 20 20 26 26 20 28 69 6e 74 29 70 . && (int)p
1123d 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 Pg->pgno<=pPager
1123e 2d 3e 73 74 6d 74 53 69 7a 65 20 0a 20 20 20 20 ->stmtSize .
1123f 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 ){. i64 off
11240 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 set = pPager->st
11241 6d 74 4e 52 65 63 2a 28 34 2b 70 50 61 67 65 72 mtNRec*(4+pPager
11242 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
11243 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 20 char *pData2
11244 3d 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c = CODEC2(pPager,
11245 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e pData, pPg->pgn
11246 6f 2c 20 37 29 3b 0a 20 20 20 20 20 20 61 73 73 o, 7);. ass
11247 65 72 74 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 ert( (pPg->flags
11248 26 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 &PGHDR_IN_JOURNA
11249 4c 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 L) .
1124a 20 20 20 20 20 7c 7c 20 28 69 6e 74 29 70 50 67 || (int)pPg
1124b 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f ->pgno>pPager->o
1124c 72 69 67 44 62 53 69 7a 65 20 29 3b 0a 20 20 20 rigDbSize );.
1124d 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 rc = write32b
1124e 69 74 73 28 70 50 61 67 65 72 2d 3e 73 74 66 64 its(pPager->stfd
1124f 2c 20 6f 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 , offset, pPg->p
11250 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 gno);. if(
11251 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
11252 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
11253 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
11254 67 65 72 2d 3e 73 74 66 64 2c 20 70 44 61 74 61 ger->stfd, pData
11255 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 2, pPager->pageS
11256 69 7a 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b 0a ize, offset+4);.
11257 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 50 41 }. PA
11258 47 45 52 54 52 41 43 45 33 28 22 53 54 4d 54 2d GERTRACE3("STMT-
11259 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 JOURNAL %d page
1125a 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 %d\n", PAGERID(p
1125b 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e Pager), pPg->pgn
1125c 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 o);. if( rc
1125d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1125e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1125f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
11260 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 pPager->stmtNRec
11261 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ++;. assert
11262 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d ( pPager->pInStm
11263 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 t!=0 );. sq
11264 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 lite3BitvecSet(p
11265 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20 Pager->pInStmt,
11266 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 pPg->pgno);.
11267 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 }. }.. /* Upda
11268 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 te the database
11269 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72 6e 2e size and return.
1126a 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1126b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
1126c 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 AGER_SHARED );.
1126d 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 if( pPager->dbS
1126e 69 7a 65 3c 28 69 6e 74 29 70 50 67 2d 3e 70 67 ize<(int)pPg->pg
1126f 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 no ){. pPager
11270 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e ->dbSize = pPg->
11271 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20 70 50 pgno;. if( pP
11272 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 45 ager->dbSize==PE
11273 4e 44 49 4e 47 5f 42 59 54 45 2f 70 50 61 67 65 NDING_BYTE/pPage
11274 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 r->pageSize ){.
11275 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 pPager->dbS
11276 69 7a 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d ize++;. }. }
11277 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11278 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
11279 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
1127a 6d 61 72 6b 20 61 20 64 61 74 61 2d 70 61 67 65 mark a data-page
1127b 20 61 73 20 77 72 69 74 61 62 6c 65 2e 20 49 74 as writable. It
1127c 20 75 73 65 73 20 0a 2a 2a 20 70 61 67 65 72 5f uses .** pager_
1127d 77 72 69 74 65 28 29 20 74 6f 20 6f 70 65 6e 20 write() to open
1127e 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 a journal file (
1127f 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 if it is not alr
11280 65 61 64 79 20 6f 70 65 6e 29 0a 2a 2a 20 61 6e eady open).** an
11281 64 20 77 72 69 74 65 20 74 68 65 20 70 61 67 65 d write the page
11282 20 2a 70 44 61 74 61 20 74 6f 20 74 68 65 20 6a *pData to the j
11283 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 ournal..**.** Th
11284 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 e difference bet
11285 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 ween this functi
11286 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 on and pager_wri
11287 74 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69 te() is that thi
11288 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c s.** function al
11289 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 so deals with th
1128a 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 e special case w
1128b 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 here 2 or more p
1128c 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 ages.** fit on a
1128d 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 single disk sec
1128e 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73 tor. In this cas
1128f 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e e all co-residen
11290 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 t pages.** must
11291 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 have been writte
11292 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
11293 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74 file before ret
11294 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 urning..*/.SQLIT
11295 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
11296 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
11297 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 DbPage *pDbPage)
11298 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
11299 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 ITE_OK;.. PgHdr
1129a 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b *pPg = pDbPage;
1129b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
1129c 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
1129d 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 Pgno nPagePerS
1129e 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d ector = (pPager-
1129f 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 >sectorSize/pPag
112a0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a er->pageSize);..
112a1 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 53 65 if( nPagePerSe
112a2 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 ctor>1 ){. Pg
112a3 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 no nPageCount;
112a4 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
112a5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
112a6 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c in database fil
112a7 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 e */. Pgno pg
112a8 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
112a9 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 /* First page
112aa 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 of the sector p
112ab 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e Pg is located on
112ac 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 . */. int nPa
112ad 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
112ae 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
112af 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 20 61 pages starting a
112b0 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c t pg1 to journal
112b1 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a */. int ii;.
112b2 20 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 int needSync
112b3 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 = 0;.. /* Se
112b4 74 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 t the doNotSync
112b5 66 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 flag to 1. This
112b6 69 73 20 62 65 63 61 75 73 65 20 77 65 20 63 61 is because we ca
112b7 6e 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 nnot allow a jou
112b8 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 rnal. ** head
112b9 65 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e er to be written
112ba 20 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 between the pag
112bb 65 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 es journaled by
112bc 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 this function..
112bd 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
112be 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 ( !MEMDB );.
112bf 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
112c0 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a doNotSync==0 );.
112c1 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f pPager->doNo
112c2 74 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 tSync = 1;..
112c3 2f 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 /* This trick as
112c4 73 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 sumes that both
112c5 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e the page-size an
112c6 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 d sector-size ar
112c7 65 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 e. ** an inte
112c8 67 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 ger power of 2.
112c9 49 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 It sets variable
112ca 20 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e pg1 to the iden
112cb 74 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 tifier. ** of
112cc 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
112cd 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 of the sector pP
112ce 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e g is located on.
112cf 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 . */. pg1
112d0 3d 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 = ((pPg->pgno-1)
112d1 20 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 & ~(nPagePerSec
112d2 74 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 tor-1)) + 1;..
112d3 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 sqlite3PagerPa
112d4 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 gecount(pPager,
112d5 28 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 (int *)&nPageCou
112d6 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 nt);. if( pPg
112d7 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e ->pgno>nPageCoun
112d8 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 t ){. nPage
112d9 20 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 = (pPg->pgno -
112da 70 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 pg1)+1;. }els
112db 65 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 e if( (pg1+nPage
112dc 50 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 PerSector-1)>nPa
112dd 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 geCount ){.
112de 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f nPage = nPageCo
112df 75 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d unt+1-pg1;. }
112e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 else{. nPag
112e1 65 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 e = nPagePerSect
112e2 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 or;. }. as
112e3 73 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 sert(nPage>0);.
112e4 20 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 assert(pg1<=p
112e5 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 Pg->pgno);. a
112e6 73 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 ssert((pg1+nPage
112e7 29 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 )>pPg->pgno);..
112e8 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c for(ii=0; ii<
112e9 6e 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c nPage && rc==SQL
112ea 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 ITE_OK; ii++){.
112eb 20 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 Pgno pg = p
112ec 67 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 g1+ii;. PgH
112ed 64 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 dr *pPage;.
112ee 20 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 if( pg==pPg->pg
112ef 6e 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 no || !sqlite3Bi
112f0 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d tvecTest(pPager-
112f1 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 >pInJournal, pg)
112f2 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
112f3 70 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e pg!=PAGER_MJ_PGN
112f4 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 O(pPager) ){.
112f5 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
112f6 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 te3PagerGet(pPag
112f7 65 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b er, pg, &pPage);
112f8 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
112f9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
112fa 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
112fb 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 pager_write(pPa
112fc 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ge);.
112fd 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 if( pPage->flag
112fe 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
112ff 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 C ){.
11300 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b needSync = 1;
11301 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
11302 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
11303 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
11304 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ge);. }
11305 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
11306 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 61 67 }else if( (pPag
11307 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 e = pager_lookup
11308 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30 (pPager, pg))!=0
11309 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1130a 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 pPage->flags&PGH
1130b 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a DR_NEED_SYNC ){.
1130c 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 needSy
1130d 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 nc = 1;.
1130e 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1130f 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 3PagerUnref(pPag
11310 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
11311 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
11312 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 PgHdr.needSync
11313 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20 flag is set for
11314 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 65 any of the nPage
11315 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 pages . ** s
11316 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c 20 tarting at pg1,
11317 74 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 6f then it needs to
11318 20 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 be set for all
11319 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73 65 of them. Because
1131a 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 . ** writing
1131b 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 to any of these
1131c 6e 50 61 67 65 20 70 61 67 65 73 20 6d 61 79 20 nPage pages may
1131d 64 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65 72 damage the other
1131e 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f s, the. ** jo
1131f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 urnal file must
11320 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 64 contain sync()ed
11321 20 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f copies of all o
11322 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65 f them. ** be
11323 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65 6d fore any of them
11324 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 can be written
11325 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 out to the datab
11326 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f ase file.. */
11327 0a 20 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e . if( needSyn
11328 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 c ){. asser
11329 74 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 t( !MEMDB && pPa
1132a 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29 ger->noSync==0 )
1132b 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 ;. for(ii=0
1132c 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 20 6e 65 ; ii<nPage && ne
1132d 65 64 53 79 6e 63 3b 20 69 69 2b 2b 29 7b 0a 20 edSync; ii++){.
1132e 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 PgHdr *pP
1132f 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b age = pager_look
11330 75 70 28 70 50 61 67 65 72 2c 20 70 67 31 2b 69 up(pPager, pg1+i
11331 69 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 i);. if(
11332 70 50 61 67 65 20 29 20 70 50 61 67 65 2d 3e 66 pPage ) pPage->f
11333 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 lags |= PGHDR_NE
11334 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 ED_SYNC;.
11335 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
11336 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
11337 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
11338 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
11339 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 );. }.. as
1133a 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f sert( pPager->do
1133b 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 NotSync==1 );.
1133c 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 pPager->doNotS
1133d 79 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 ync = 0;. }else
1133e 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 {. rc = pager
1133f 5f 77 72 69 74 65 28 70 44 62 50 61 67 65 29 3b _write(pDbPage);
11340 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
11341 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
11342 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 70 61 n TRUE if the pa
11343 67 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 ge given in the
11344 61 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 argument was pre
11345 76 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a viously passed.*
11346 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 * to sqlite3Page
11347 72 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 rWrite(). In ot
11348 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75 72 her words, retur
11349 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73 20 n TRUE if it is
1134a 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 ok.** to change
1134b 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 the content of t
1134c 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e he page..*/.#ifn
1134d 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 def NDEBUG.SQLIT
1134e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1134f 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
11350 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70 50 eable(DbPage *pP
11351 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 g){. return pPg
11352 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
11353 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f RTY;.}.#endif../
11354 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 *.** A call to t
11355 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c his routine tell
11356 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74 s the pager that
11357 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 it is not neces
11358 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 sary to.** write
11359 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e the information
1135a 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61 63 on page pPg bac
1135b 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 k to the disk, e
1135c 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 ven though.** th
1135d 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 at page might be
1135e 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 marked as dirty
1135f 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c . This happens,
11360 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 for example, wh
11361 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 68 en.** the page h
11362 61 73 20 62 65 65 6e 20 61 64 64 65 64 20 61 73 as been added as
11363 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20 66 a leaf of the f
11364 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 reelist and so i
11365 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f ts.** content no
11366 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 2e longer matters.
11367 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c .**.** The overl
11368 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c 61 ying software la
11369 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 yer calls this r
1136a 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 outine when all
1136b 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f of the data.** o
1136c 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 n the given page
1136d 20 69 73 20 75 6e 75 73 65 64 2e 20 20 54 68 65 is unused. The
1136e 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 pager marks the
1136f 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 page as clean s
11370 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 o.** that it doe
11371 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 s not get writte
11372 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a n to disk..**.**
11373 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 Tests show that
11374 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 this optimizati
11375 6f 6e 2c 20 74 6f 67 65 74 68 65 72 20 77 69 74 on, together wit
11376 68 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 h the.** sqlite3
11377 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 PagerDontRollbac
11378 6b 28 29 20 62 65 6c 6f 77 2c 20 6d 6f 72 65 20 k() below, more
11379 74 68 61 6e 20 64 6f 75 62 6c 65 20 74 68 65 20 than double the
1137a 73 70 65 65 64 0a 2a 2a 20 6f 66 20 6c 61 72 67 speed.** of larg
1137b 65 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 e INSERT operati
1137c 6f 6e 73 20 61 6e 64 20 71 75 61 64 72 75 70 6c ons and quadrupl
1137d 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c e the speed of l
1137e 61 72 67 65 20 44 45 4c 45 54 45 73 2e 0a 2a 2a arge DELETEs..**
1137f 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f .** When this ro
11380 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
11381 20 73 65 74 20 74 68 65 20 61 6c 77 61 79 73 52 set the alwaysR
11382 6f 6c 6c 62 61 63 6b 20 66 6c 61 67 20 74 6f 20 ollback flag to
11383 74 72 75 65 2e 0a 2a 2a 20 53 75 62 73 65 71 75 true..** Subsequ
11384 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ent calls to sql
11385 69 74 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c ite3PagerDontRol
11386 6c 62 61 63 6b 28 29 20 66 6f 72 20 74 68 65 20 lback() for the
11387 73 61 6d 65 20 70 61 67 65 0a 2a 2a 20 77 69 6c same page.** wil
11388 6c 20 74 68 65 72 65 61 66 74 65 72 20 62 65 20 l thereafter be
11389 69 67 6e 6f 72 65 64 2e 20 20 54 68 69 73 20 69 ignored. This i
1138a 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 61 s necessary to a
1138b 76 6f 69 64 20 61 20 70 72 6f 62 6c 65 6d 0a 2a void a problem.*
1138c 2a 20 77 68 65 72 65 20 61 20 70 61 67 65 20 77 * where a page w
1138d 69 74 68 20 64 61 74 61 20 69 73 20 61 64 64 65 ith data is adde
1138e 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 d to the freelis
1138f 74 20 64 75 72 69 6e 67 20 6f 6e 65 20 70 61 72 t during one par
11390 74 20 6f 66 0a 2a 2a 20 61 20 74 72 61 6e 73 61 t of.** a transa
11391 63 74 69 6f 6e 20 74 68 65 6e 20 72 65 6d 6f 76 ction then remov
11392 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 ed from the free
11393 6c 69 73 74 20 64 75 72 69 6e 67 20 61 20 6c 61 list during a la
11394 74 65 72 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 ter part.** of t
11395 68 65 20 73 61 6d 65 20 74 72 61 6e 73 61 63 74 he same transact
11396 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 64 20 66 ion and reused f
11397 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 75 or some other pu
11398 72 70 6f 73 65 2e 20 20 57 68 65 6e 20 69 74 0a rpose. When it.
11399 2a 2a 20 69 73 20 66 69 72 73 74 20 61 64 64 65 ** is first adde
1139a 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 d to the freelis
1139b 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 t, this routine
1139c 69 73 20 63 61 6c 6c 65 64 2e 20 20 57 68 65 6e is called. When
1139d 20 72 65 75 73 65 64 2c 0a 2a 2a 20 74 68 65 20 reused,.** the
1139e 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
1139f 52 6f 6c 6c 62 61 63 6b 28 29 20 72 6f 75 74 69 Rollback() routi
113a0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 42 ne is called. B
113a1 75 74 20 62 65 63 61 75 73 65 20 74 68 65 0a 2a ut because the.*
113a2 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 * page contains
113a3 63 72 69 74 69 63 61 6c 20 64 61 74 61 2c 20 77 critical data, w
113a4 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 e still need to
113a5 62 65 20 73 75 72 65 20 69 74 20 67 65 74 73 0a be sure it gets.
113a6 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 ** rolled back i
113a7 6e 20 73 70 69 74 65 20 6f 66 20 74 68 65 20 73 n spite of the s
113a8 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 52 qlite3PagerDontR
113a9 6f 6c 6c 62 61 63 6b 28 29 20 63 61 6c 6c 2e 0a ollback() call..
113aa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
113ab 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
113ac 65 72 44 6f 6e 74 57 72 69 74 65 28 44 62 50 61 erDontWrite(DbPa
113ad 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 ge *pDbPage){.
113ae 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70 44 62 PgHdr *pPg = pDb
113af 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 Page;. Pager *p
113b0 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
113b1 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a ger;. int rc;..
113b2 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e if( pPg->pgno>
113b3 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 pPager->origDbSi
113b4 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ze ){. return
113b5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
113b6 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 41 if( pPager->pA
113b7 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 3d 3d 30 lwaysRollback==0
113b8 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
113b9 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
113ba 61 6c 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 al );. pPager
113bb 2d 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 ->pAlwaysRollbac
113bc 6b 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 k = sqlite3Bitve
113bd 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e cCreate(pPager->
113be 6f 72 69 67 44 62 53 69 7a 65 29 3b 0a 20 20 20 origDbSize);.
113bf 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 70 41 if( !pPager->pA
113c0 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 29 7b lwaysRollback ){
113c1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
113c2 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
113c3 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c }. }. rc = sql
113c4 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50 ite3BitvecSet(pP
113c5 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 6f 6c ager->pAlwaysRol
113c6 6c 62 61 63 6b 2c 20 70 50 67 2d 3e 70 67 6e 6f lback, pPg->pgno
113c7 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 );.. if( rc==SQ
113c8 4c 49 54 45 5f 4f 4b 20 26 26 20 28 70 50 67 2d LITE_OK && (pPg-
113c9 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
113ca 54 59 29 20 26 26 20 21 70 50 61 67 65 72 2d 3e TY) && !pPager->
113cb 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 stmtInUse ){.
113cc 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
113cd 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 >state>=PAGER_SH
113ce 41 52 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20 ARED );. if(
113cf 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d pPager->dbSize==
113d0 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20 26 (int)pPg->pgno &
113d1 26 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 & pPager->origDb
113d2 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 53 Size<pPager->dbS
113d3 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ize ){. /*
113d4 49 66 20 74 68 69 73 20 70 61 67 65 73 20 69 73 If this pages is
113d5 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 the last page i
113d6 6e 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 74 n the file and t
113d7 68 65 20 66 69 6c 65 20 68 61 73 20 67 72 6f 77 he file has grow
113d8 6e 0a 20 20 20 20 20 20 2a 2a 20 64 75 72 69 6e n. ** durin
113d9 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 g the current tr
113da 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 ansaction, then
113db 64 6f 20 4e 4f 54 20 6d 61 72 6b 20 74 68 65 20 do NOT mark the
113dc 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 0a 20 page as clean..
113dd 20 20 20 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 ** When the
113de 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 67 database file g
113df 72 6f 77 73 2c 20 77 65 20 6d 75 73 74 20 6d 61 rows, we must ma
113e0 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ke sure that the
113e1 20 6c 61 73 74 20 70 61 67 65 0a 20 20 20 20 20 last page.
113e2 20 2a 2a 20 67 65 74 73 20 77 72 69 74 74 65 6e ** gets written
113e3 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 20 73 at least once s
113e4 6f 20 74 68 61 74 20 74 68 65 20 64 69 73 6b 20 o that the disk
113e5 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 74 68 65 file will be the
113e6 20 63 6f 72 72 65 63 74 0a 20 20 20 20 20 20 2a correct. *
113e7 2a 20 73 69 7a 65 2e 20 49 66 20 79 6f 75 20 64 * size. If you d
113e8 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 68 69 73 o not write this
113e9 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 73 69 page and the si
113ea 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 ze of the file.
113eb 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 ** on the d
113ec 69 73 6b 20 65 6e 64 73 20 75 70 20 62 65 69 6e isk ends up bein
113ed 67 20 74 6f 6f 20 73 6d 61 6c 6c 2c 20 74 68 61 g too small, tha
113ee 74 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 t can lead to da
113ef 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 tabase. **
113f0 63 6f 72 72 75 70 74 69 6f 6e 20 64 75 72 69 6e corruption durin
113f1 67 20 74 68 65 20 6e 65 78 74 20 74 72 61 6e 73 g the next trans
113f2 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f action.. */
113f3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
113f4 20 20 50 41 47 45 52 54 52 41 43 45 33 28 22 44 PAGERTRACE3("D
113f5 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 25 ONT_WRITE page %
113f6 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d d of %d\n", pPg-
113f7 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 >pgno, PAGERID(p
113f8 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 49 Pager));. I
113f9 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25 OTRACE(("CLEAN %
113fa 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
113fb 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 pPg->pgno)).
113fc 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
113fd 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 PGHDR_DONT_WRIT
113fe 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 E;.#ifdef SQLITE
113ff 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 _CHECK_PAGES.
11400 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 pPg->pageHash
11401 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
11402 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 h(pPg);.#endif.
11403 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
11404 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 n rc;.}../*.** A
11405 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f call to this ro
11406 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 68 65 20 utine tells the
11407 70 61 67 65 72 20 74 68 61 74 20 69 66 20 61 20 pager that if a
11408 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2c rollback occurs,
11409 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 .** it is not ne
1140a 63 65 73 73 61 72 79 20 74 6f 20 72 65 73 74 6f cessary to resto
1140b 72 65 20 74 68 65 20 64 61 74 61 20 6f 6e 20 74 re the data on t
1140c 68 65 20 67 69 76 65 6e 20 70 61 67 65 2e 20 20 he given page.
1140d 54 68 69 73 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 This.** means th
1140e 61 74 20 74 68 65 20 70 61 67 65 72 20 64 6f 65 at the pager doe
1140f 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 72 65 s not have to re
11410 63 6f 72 64 20 74 68 65 20 67 69 76 65 6e 20 70 cord the given p
11411 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a 20 72 6f age in the.** ro
11412 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a llback journal..
11413 2a 2a 0a 2a 2a 20 49 66 20 77 65 20 68 61 76 65 **.** If we have
11414 20 6e 6f 74 20 79 65 74 20 61 63 74 75 61 6c 6c not yet actuall
11415 79 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 y read the conte
11416 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 nt of this page
11417 28 69 66 0a 2a 2a 20 74 68 65 20 50 67 48 64 72 (if.** the PgHdr
11418 2e 6e 65 65 64 52 65 61 64 20 66 6c 61 67 20 69 .needRead flag i
11419 73 20 73 65 74 29 20 74 68 65 6e 20 74 68 69 73 s set) then this
1141a 20 72 6f 75 74 69 6e 65 20 61 63 74 73 20 61 73 routine acts as
1141b 20 61 20 70 72 6f 6d 69 73 65 0a 2a 2a 20 74 68 a promise.** th
1141c 61 74 20 77 65 20 77 69 6c 6c 20 6e 65 76 65 72 at we will never
1141d 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 need to read th
1141e 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 e page content i
1141f 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 2a 2a n the future..**
11420 20 73 6f 20 74 68 65 20 6e 65 65 64 52 65 61 64 so the needRead
11421 20 66 6c 61 67 20 63 61 6e 20 62 65 20 63 6c 65 flag can be cle
11422 61 72 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 ared at this poi
11423 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
11424 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
11425 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 e3PagerDontRollb
11426 61 63 6b 28 44 62 50 61 67 65 20 2a 70 50 67 29 ack(DbPage *pPg)
11427 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 {. Pager *pPage
11428 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b r = pPg->pPager;
11429 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
1142a 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
1142b 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 0a 20 20 _RESERVED );..
1142c 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 /* If the journa
1142d 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 l file is not op
1142e 65 6e 2c 20 6f 72 20 44 6f 6e 74 57 72 69 74 65 en, or DontWrite
1142f 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c () has been call
11430 65 64 20 6f 6e 0a 20 20 2a 2a 20 74 68 69 73 20 ed on. ** this
11431 70 61 67 65 20 28 44 6f 6e 74 57 72 69 74 65 28 page (DontWrite(
11432 29 20 73 65 74 73 20 74 68 65 20 61 6c 77 61 79 ) sets the alway
11433 73 52 6f 6c 6c 62 61 63 6b 20 66 6c 61 67 29 2c sRollback flag),
11434 20 74 68 65 6e 20 74 68 69 73 0a 20 20 2a 2a 20 then this. **
11435 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
11436 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 -op.. */. if(
11437 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11438 70 65 6e 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71 pen==0 . || sq
11439 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
1143a 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 pPager->pAlwaysR
1143b 6f 6c 6c 62 61 63 6b 2c 20 70 50 67 2d 3e 70 67 ollback, pPg->pg
1143c 6e 6f 29 0a 20 20 20 7c 7c 20 70 50 67 2d 3e 70 no). || pPg->p
1143d 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f 72 69 67 gno>pPager->orig
1143e 44 62 53 69 7a 65 0a 20 20 29 7b 0a 20 20 20 20 DbSize. ){.
1143f 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 23 69 66 return;. }..#if
11440 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 def SQLITE_SECUR
11441 45 5f 44 45 4c 45 54 45 0a 20 20 69 66 28 20 28 E_DELETE. if( (
11442 70 50 67 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 pPg->flags & PGH
11443 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c 29 21 3d DR_IN_JOURNAL)!=
11444 30 20 7c 7c 20 28 69 6e 74 29 70 50 67 2d 3e 70 0 || (int)pPg->p
11445 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f 72 69 67 gno>pPager->orig
11446 44 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 DbSize ){. re
11447 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 turn;. }.#endif
11448 0a 0a 20 20 2f 2a 20 49 66 20 53 45 43 55 52 45 .. /* If SECURE
11449 5f 44 45 4c 45 54 45 20 69 73 20 64 69 73 61 62 _DELETE is disab
1144a 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 led, then there
1144b 69 73 20 6e 6f 20 77 61 79 20 74 68 61 74 20 74 is no way that t
1144c 68 69 73 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 his. ** routine
1144d 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 6f can be called o
1144e 6e 20 61 20 70 61 67 65 20 66 6f 72 20 77 68 69 n a page for whi
1144f 63 68 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 ch sqlite3PagerD
11450 6f 6e 74 57 72 69 74 65 28 29 0a 20 20 2a 2a 20 ontWrite(). **
11451 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65 has not been pre
11452 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 20 64 viously called d
11453 75 72 69 6e 67 20 74 68 65 20 73 61 6d 65 20 74 uring the same t
11454 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a ransaction.. **
11455 20 41 6e 64 20 69 66 20 44 6f 6e 74 57 72 69 74 And if DontWrit
11456 65 28 29 20 68 61 73 20 70 72 65 76 69 6f 75 73 e() has previous
11457 6c 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 ly been called,
11458 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
11459 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 ** conditions mu
1145a 73 74 20 62 65 20 6d 65 74 2e 0a 20 20 2a 2a 0a st be met.. **.
1145b 20 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20 20 4e ** (Later:) N
1145c 6f 74 20 74 72 75 65 2e 20 20 49 66 20 74 68 65 ot true. If the
1145d 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 database is cor
1145e 72 75 70 74 65 64 20 62 79 20 68 61 76 69 6e 67 rupted by having
1145f 20 64 75 70 6c 69 63 61 74 65 0a 20 20 2a 2a 20 duplicate. **
11460 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 pages on the fre
11461 65 6c 69 73 74 20 28 65 78 3a 20 63 6f 72 72 75 elist (ex: corru
11462 70 74 39 2e 74 65 73 74 29 20 74 68 65 6e 20 74 pt9.test) then t
11463 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 he following is
11464 6e 6f 74 0a 20 20 2a 2a 20 6e 65 63 65 73 73 61 not. ** necessa
11465 72 69 6c 79 20 74 72 75 65 3a 0a 20 20 2a 2f 0a rily true:. */.
11466 20 20 2f 2a 20 61 73 73 65 72 74 28 20 21 70 50 /* assert( !pP
11467 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 26 26 20 g->inJournal &&
11468 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20 3c (int)pPg->pgno <
11469 3d 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 = pPager->origDb
1146a 53 69 7a 65 20 29 3b 20 2a 2f 0a 0a 20 20 61 73 Size ); */.. as
1146b 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 sert( pPager->pI
1146c 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 nJournal!=0 );.
1146d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 sqlite3BitvecSe
1146e 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 t(pPager->pInJou
1146f 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 rnal, pPg->pgno)
11470 3b 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c ;. pPg->flags |
11471 3d 20 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e = PGHDR_IN_JOURN
11472 41 4c 3b 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 AL;. pPg->flags
11473 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f &= ~PGHDR_NEED_
11474 52 45 41 44 3b 0a 20 20 69 66 28 20 70 50 61 67 READ;. if( pPag
11475 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b er->stmtInUse ){
11476 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
11477 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3e 3d ger->stmtSize >=
11478 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 pPager->origDbS
11479 69 7a 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ize );. sqlit
1147a 65 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67 e3BitvecSet(pPag
1147b 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20 70 50 67 er->pInStmt, pPg
1147c 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 50 ->pgno);. }. P
1147d 41 47 45 52 54 52 41 43 45 33 28 22 44 4f 4e 54 AGERTRACE3("DONT
1147e 5f 52 4f 4c 4c 42 41 43 4b 20 70 61 67 65 20 25 _ROLLBACK page %
1147f 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d d of %d\n", pPg-
11480 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 >pgno, PAGERID(p
11481 50 61 67 65 72 29 29 3b 0a 20 20 49 4f 54 52 41 Pager));. IOTRA
11482 43 45 28 28 22 47 41 52 42 41 47 45 20 25 70 20 CE(("GARBAGE %p
11483 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
11484 50 67 2d 3e 70 67 6e 6f 29 29 0a 7d 0a 0a 0a 2f Pg->pgno)).}.../
11485 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
11486 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 e is called to i
11487 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 64 61 74 ncrement the dat
11488 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 abase file chang
11489 65 2d 63 6f 75 6e 74 65 72 2c 0a 2a 2a 20 73 74 e-counter,.** st
1148a 6f 72 65 64 20 61 74 20 62 79 74 65 20 32 34 20 ored at byte 24
1148b 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c of the pager fil
1148c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1148d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
1148e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65 72 20 gecounter(Pager
1148f 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 44 *pPager, int isD
11490 69 72 65 63 74 29 7b 0a 20 20 50 67 48 64 72 20 irect){. PgHdr
11491 2a 70 50 67 48 64 72 3b 0a 20 20 75 33 32 20 63 *pPgHdr;. u32 c
11492 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 0a 20 hange_counter;.
11493 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
11494 5f 4f 4b 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 _OK;..#ifndef SQ
11495 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
11496 49 43 5f 57 52 49 54 45 0a 20 20 61 73 73 65 72 IC_WRITE. asser
11497 74 28 20 69 73 44 69 72 65 63 74 3d 3d 30 20 29 t( isDirect==0 )
11498 3b 20 20 2f 2a 20 69 73 44 69 72 65 63 74 20 69 ; /* isDirect i
11499 73 20 6f 6e 6c 79 20 74 72 75 65 20 66 6f 72 20 s only true for
1149a 61 74 6f 6d 69 63 20 77 72 69 74 65 73 20 2a 2f atomic writes */
1149b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 70 .#endif. if( !p
1149c 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
1149d 6e 74 44 6f 6e 65 20 29 7b 0a 20 20 20 20 2f 2a ntDone ){. /*
1149e 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20 Open page 1 of
1149f 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69 the file for wri
114a0 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 ting. */. rc
114a1 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
114a2 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50 t(pPager, 1, &pP
114a3 67 48 64 72 29 3b 0a 20 20 20 20 69 66 28 20 72 gHdr);. if( r
114a4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
114a5 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 69 eturn rc;.. i
114a6 66 28 20 21 69 73 44 69 72 65 63 74 20 29 7b 0a f( !isDirect ){.
114a7 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
114a8 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 e3PagerWrite(pPg
114a9 48 64 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 Hdr);. if(
114aa 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
114ab 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
114ac 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 PagerUnref(pPgHd
114ad 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 r);. retu
114ae 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
114af 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 63 }.. /* Inc
114b0 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 rement the value
114b1 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 just read and w
114b2 72 69 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 rite it back to
114b3 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20 byte 24. */.
114b4 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d change_counter =
114b5 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 sqlite3Get4byte
114b6 28 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 ((u8*)pPager->db
114b7 46 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 63 FileVers);. c
114b8 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b hange_counter++;
114b9 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 28 . put32bits((
114ba 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d 3e 70 (char*)pPgHdr->p
114bb 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 65 Data)+24, change
114bc 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 23 69 66 64 _counter);..#ifd
114bd 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
114be 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 _ATOMIC_WRITE.
114bf 20 20 69 66 28 20 69 73 44 69 72 65 63 74 20 26 if( isDirect &
114c0 26 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d & pPager->fd->pM
114c1 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 ethods ){.
114c2 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 const void *zBuf
114c3 20 3d 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61 = pPgHdr->pData
114c4 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
114c5 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
114c6 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 50 er->fd, zBuf, pP
114c7 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
114c8 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0);. }.#endif
114c9 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 .. /* Release
114ca 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65 the page refere
114cb 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 nce. */. sqli
114cc 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
114cd 67 48 64 72 29 3b 0a 20 20 20 20 70 50 61 67 65 gHdr);. pPage
114ce 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
114cf 6e 65 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 ne = 1;. }. re
114d0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
114d1 2a 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 72 * Sync the pager
114d2 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 0a 2a file to disk..*
114d3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
114d4 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
114d5 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 rSync(Pager *pPa
114d6 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ger){. int rc;.
114d7 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 if( MEMDB ){.
114d8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
114d9 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
114da 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 rc = sqlite3OsSy
114db 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 nc(pPager->fd, p
114dc 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 Pager->sync_flag
114dd 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e s);. }. return
114de 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 rc;.}../*.** Sy
114df 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nc the database
114e0 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70 61 67 file for the pag
114e1 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61 73 74 er pPager. zMast
114e2 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 er points to the
114e3 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 6d 61 name.** of a ma
114e4 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
114e5 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 e that should be
114e6 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
114e7 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 e individual.**
114e8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 7a 4d journal file. zM
114e9 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e 55 4c aster may be NUL
114ea 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e 74 65 L, which is inte
114eb 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20 6d 61 rpreted as no ma
114ec 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 ster.** journal
114ed 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 (a single databa
114ee 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e se transaction).
114ef 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
114f0 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 74 ine ensures that
114f1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
114f2 73 79 6e 63 65 64 2c 20 61 6c 6c 20 64 69 72 74 synced, all dirt
114f3 79 20 70 61 67 65 73 20 77 72 69 74 74 65 6e 0a y pages written.
114f4 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ** to the databa
114f5 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 se file and the
114f6 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79 database file sy
114f7 6e 63 65 64 2e 20 54 68 65 20 6f 6e 6c 79 20 74 nced. The only t
114f8 68 69 6e 67 20 74 68 61 74 0a 2a 2a 20 72 65 6d hing that.** rem
114f9 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 ains to commit t
114fa 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
114fb 73 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 s to delete the
114fc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 journal file (or
114fd 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e .** master journ
114fe 61 6c 20 66 69 6c 65 20 69 66 20 73 70 65 63 69 al file if speci
114ff 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 fied)..**.** Not
11500 65 20 74 68 61 74 20 69 66 20 7a 4d 61 73 74 65 e that if zMaste
11501 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 64 6f r==NULL, this do
11502 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 65 es not overwrite
11503 20 61 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 a previous valu
11504 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 61 e.** passed to a
11505 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f n sqlite3PagerCo
11506 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 63 mmitPhaseOne() c
11507 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 all..**.** If pa
11508 72 61 6d 65 74 65 72 20 6e 54 72 75 6e 63 20 69 rameter nTrunc i
11509 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1150a 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 the pager file
1150b 69 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 0a is truncated to.
1150c 2a 2a 20 6e 54 72 75 6e 63 20 70 61 67 65 73 20 ** nTrunc pages
1150d 28 74 68 69 73 20 69 73 20 75 73 65 64 20 62 79 (this is used by
1150e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 auto-vacuum dat
1150f 61 62 61 73 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 49 abases)..**.** I
11510 66 20 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 f the final para
11511 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d meter - noSync -
11512 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
11513 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11514 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f itself.** is no
11515 74 20 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 t synced. The ca
11516 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 ller must call s
11517 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 qlite3PagerSync(
11518 29 20 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a ) directly to.**
11519 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 sync the databa
1151a 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63 se file before c
1151b 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61 alling CommitPha
1151c 73 65 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74 seTwo() to delet
1151d 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c e the.** journal
1151e 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 file in this ca
1151f 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
11520 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
11521 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
11522 65 4f 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70 eOne(. Pager *p
11523 50 61 67 65 72 2c 20 0a 20 20 63 6f 6e 73 74 20 Pager, . const
11524 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 0a char *zMaster, .
11525 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63 2c 0a 20 Pgno nTrunc,.
11526 20 69 6e 74 20 6e 6f 53 79 6e 63 0a 29 7b 0a 20 int noSync.){.
11527 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
11528 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 _OK;.. if( pPag
11529 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 er->errCode ){.
1152a 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 return pPager
1152b 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a ->errCode;. }..
1152c 20 20 2f 2a 20 49 66 20 6e 6f 20 63 68 61 6e 67 /* If no chang
1152d 65 73 20 68 61 76 65 20 62 65 65 6e 20 6d 61 64 es have been mad
1152e 65 2c 20 77 65 20 63 61 6e 20 6c 65 61 76 65 20 e, we can leave
1152f 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
11530 65 61 72 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 early.. */. if
11531 28 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 ( pPager->dbModi
11532 66 69 65 64 3d 3d 30 20 26 26 0a 20 20 20 20 20 fied==0 &&.
11533 20 20 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 (pPager->jour
11534 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a nalMode!=PAGER_J
11535 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 OURNALMODE_DELET
11536 45 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 E ||. p
11537 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
11538 4d 6f 64 65 21 3d 30 29 20 29 7b 0a 20 20 20 20 Mode!=0) ){.
11539 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
1153a 64 69 72 74 79 43 61 63 68 65 3d 3d 30 20 7c 7c dirtyCache==0 ||
1153b 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1153c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 72 Open==0 );. r
1153d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1153e 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 . }.. PAGERTRA
1153f 43 45 34 28 22 44 41 54 41 42 41 53 45 20 53 59 CE4("DATABASE SY
11540 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73 NC: File=%s zMas
11541 74 65 72 3d 25 73 20 6e 54 72 75 6e 63 3d 25 64 ter=%s nTrunc=%d
11542 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 61 67 \n", . pPag
11543 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a er->zFilename, z
11544 4d 61 73 74 65 72 2c 20 6e 54 72 75 6e 63 29 3b Master, nTrunc);
11545 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
11546 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 s an in-memory d
11547 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 b, or no pages h
11548 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e ave been written
11549 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a to, or this. *
1154a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 * function has a
1154b 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c lready been call
1154c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f ed, it is a no-o
1154d 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 p.. */. if( pP
1154e 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
1154f 45 52 5f 53 59 4e 43 45 44 20 26 26 20 21 4d 45 ER_SYNCED && !ME
11550 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 MDB && pPager->d
11551 69 72 74 79 43 61 63 68 65 20 29 7b 0a 20 20 20 irtyCache ){.
11552 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 23 69 PgHdr *pPg;..#i
11553 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
11554 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a LE_ATOMIC_WRITE.
11555 20 20 20 20 2f 2a 20 54 68 65 20 61 74 6f 6d 69 /* The atomi
11556 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
11557 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 tion can be used
11558 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 if all of the.
11559 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 ** following
1155a 61 72 65 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a are true:. **
1155b 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 . ** + The
1155c 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 file-system sup
1155d 70 6f 72 74 73 20 74 68 65 20 61 74 6f 6d 69 63 ports the atomic
1155e 2d 77 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 -write property
1155f 66 6f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 for. **
11560 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 blocks of size p
11561 61 67 65 2d 73 69 7a 65 2c 20 61 6e 64 0a 20 20 age-size, and.
11562 20 20 2a 2a 20 20 20 20 2b 20 54 68 69 73 20 63 ** + This c
11563 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 ommit is not par
11564 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c t of a multi-fil
11565 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 e transaction, a
11566 6e 64 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 45 nd. ** + E
11567 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 xactly one page
11568 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 has been modifie
11569 64 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 d and store in t
1156a 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
1156b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
1156c 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 f the optimizati
1156d 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 on can be used,
1156e 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
1156f 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 file will never
11570 0a 20 20 20 20 2a 2a 20 62 65 20 63 72 65 61 74 . ** be creat
11571 65 64 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e ed for this tran
11572 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a saction.. */.
11573 20 20 20 20 69 6e 74 20 75 73 65 41 74 6f 6d 69 int useAtomi
11574 63 57 72 69 74 65 3b 0a 20 20 20 20 70 50 67 20 cWrite;. pPg
11575 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 = sqlite3PcacheD
11576 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d irtyList(pPager-
11577 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 75 >pPCache);. u
11578 73 65 41 74 6f 6d 69 63 57 72 69 74 65 20 3d 20 seAtomicWrite =
11579 28 0a 20 20 20 20 20 20 20 20 21 7a 4d 61 73 74 (. !zMast
1157a 65 72 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 er && . p
1157b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 Pager->journalOp
1157c 65 6e 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 en &&. pP
1157d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
1157e 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 ==jrnlBufferSize
1157f 28 70 50 61 67 65 72 29 20 26 26 20 0a 20 20 20 (pPager) && .
11580 20 20 20 20 20 6e 54 72 75 6e 63 3d 3d 30 20 26 nTrunc==0 &
11581 26 20 0a 20 20 20 20 20 20 20 20 28 70 50 67 3d & . (pPg=
11582 3d 30 20 7c 7c 20 70 50 67 2d 3e 70 44 69 72 74 =0 || pPg->pDirt
11583 79 3d 3d 30 29 0a 20 20 20 20 29 3b 0a 20 20 20 y==0). );.
11584 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
11585 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c 20 >journalOpen ||
11586 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
11587 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
11588 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 ALMODE_OFF );.
11589 20 20 69 66 28 20 75 73 65 41 74 6f 6d 69 63 57 if( useAtomicW
1158a 72 69 74 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a rite ){. /*
1158b 20 55 70 64 61 74 65 20 74 68 65 20 6e 52 65 63 Update the nRec
1158c 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 6a 6f field in the jo
1158d 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 urnal file. */.
1158e 20 20 20 20 20 69 6e 74 20 6f 66 66 73 65 74 20 int offset
1158f 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
11590 6c 48 64 72 20 2b 20 73 69 7a 65 6f 66 28 61 4a lHdr + sizeof(aJ
11591 6f 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20 20 ournalMagic);.
11592 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 assert(pPage
11593 72 2d 3e 6e 52 65 63 3d 3d 31 29 3b 0a 20 20 20 r->nRec==1);.
11594 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 rc = write32b
11595 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
11596 20 6f 66 66 73 65 74 2c 20 70 50 61 67 65 72 2d offset, pPager-
11597 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20 20 2f >nRec);.. /
11598 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 62 20 * Update the db
11599 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e file change coun
1159a 74 65 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ter. The followi
1159b 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64 ng call will mod
1159c 69 66 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 ify. ** the
1159d 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 in-memory repre
1159e 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67 sentation of pag
1159f 65 20 31 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 e 1 to include t
115a0 68 65 20 75 70 64 61 74 65 64 0a 20 20 20 20 20 he updated.
115a1 20 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 ** change count
115a2 65 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 74 er and then writ
115a3 65 20 70 61 67 65 20 31 20 64 69 72 65 63 74 6c e page 1 directl
115a4 79 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 y to the databas
115a5 65 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e e. ** file.
115a6 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 Because of the
115a7 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 6f atomic-write pro
115a8 70 65 72 74 79 20 6f 66 20 74 68 65 20 68 6f 73 perty of the hos
115a9 74 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 0a t file-system, .
115aa 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 ** this is
115ab 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a 2f 0a safe.. */.
115ac 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
115ad 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
115ae 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e rc = pager_in
115af 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 cr_changecounter
115b0 28 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 (pPager, 1);.
115b1 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
115b2 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
115b3 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 e3JournalCreate(
115b4 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 pPager->jfd);.
115b5 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 75 73 }.. if( !us
115b6 65 41 74 6f 6d 69 63 57 72 69 74 65 20 26 26 20 eAtomicWrite &&
115b7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 0a rc==SQLITE_OK ).
115b8 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 #endif.. /* I
115b9 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e f a master journ
115ba 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 61 73 al file name has
115bb 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 already been wr
115bc 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 20 20 20 itten to the.
115bd 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ** journal file
115be 2c 20 74 68 65 6e 20 6e 6f 20 73 79 6e 63 20 69 , then no sync i
115bf 73 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 s required. This
115c0 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 69 74 happens when it
115c1 20 69 73 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 is. ** writt
115c2 65 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f en, then the pro
115c3 63 65 73 73 20 66 61 69 6c 73 20 74 6f 20 75 70 cess fails to up
115c4 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45 53 grade from a RES
115c5 45 52 56 45 44 20 74 6f 20 61 6e 0a 20 20 20 20 ERVED to an.
115c6 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 ** EXCLUSIVE loc
115c7 6b 2e 20 54 68 65 20 6e 65 78 74 20 74 69 6d 65 k. The next time
115c8 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 72 69 the process tri
115c9 65 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 es to commit the
115ca 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 . ** transact
115cb 69 6f 6e 20 74 68 65 20 6d 2d 6a 20 6e 61 6d 65 ion the m-j name
115cc 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 will have alrea
115cd 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 2e dy been written.
115ce 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
115cf 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 !pPager->setMast
115d0 65 72 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d er ){. rc =
115d1 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
115d2 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 gecounter(pPager
115d3 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
115d4 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
115d5 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a goto sync_exit;.
115d6 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 if( pPager
115d7 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 ->journalMode!=P
115d8 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
115d9 5f 4f 46 46 20 29 7b 0a 23 69 66 6e 64 65 66 20 _OFF ){.#ifndef
115da 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
115db 56 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 69 VACUUM. i
115dc 66 28 20 6e 54 72 75 6e 63 21 3d 30 20 29 7b 0a f( nTrunc!=0 ){.
115dd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 /* If
115de 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
115df 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 has made the da
115e0 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 tabase smaller,
115e1 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 then all pages.
115e2 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 69 6e ** bein
115e3 67 20 64 69 73 63 61 72 64 65 64 20 62 79 20 74 g discarded by t
115e4 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d 75 he truncation mu
115e5 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f st be written to
115e6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 the journal.
115e7 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 0a ** file..
115e8 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
115e9 20 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 0a 20 Pgno i;.
115ea 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 6b int iSk
115eb 69 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 ip = PAGER_MJ_PG
115ec 4e 4f 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 NO(pPager);.
115ed 20 20 20 20 20 20 66 6f 72 28 20 69 3d 6e 54 72 for( i=nTr
115ee 75 6e 63 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 unc+1; i<=pPager
115ef 2d 3e 6f 72 69 67 44 62 53 69 7a 65 3b 20 69 2b ->origDbSize; i+
115f0 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 + ){.
115f1 20 69 66 28 20 21 73 71 6c 69 74 65 33 42 69 74 if( !sqlite3Bit
115f2 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e vecTest(pPager->
115f3 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 29 20 26 pInJournal, i) &
115f4 26 20 69 21 3d 69 53 6b 69 70 20 29 7b 0a 20 20 & i!=iSkip ){.
115f5 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
115f6 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
115f7 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 67 (pPager, i, &pPg
115f8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
115f9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
115fa 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 OK ) goto sync_e
115fb 78 69 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 xit;.
115fc 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
115fd 61 67 65 72 57 72 69 74 65 28 70 50 67 29 3b 0a agerWrite(pPg);.
115fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
115ff 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
11600 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 pPg);.
11601 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
11602 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e TE_OK ) goto syn
11603 63 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 c_exit;.
11604 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
11605 7d 20 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e } . }.#en
11606 64 69 66 0a 20 20 20 20 20 20 20 20 72 63 20 3d dif. rc =
11607 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 writeMasterJour
11608 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 nal(pPager, zMas
11609 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 ter);. if
1160a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1160b 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 ) goto sync_exit
1160c 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
1160d 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 yncJournal(pPage
1160e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 r);. }.
1160f 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 }. if( rc!=SQ
11610 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 LITE_OK ) goto s
11611 79 6e 63 5f 65 78 69 74 3b 0a 0a 23 69 66 6e 64 ync_exit;..#ifnd
11612 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
11613 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
11614 28 20 6e 54 72 75 6e 63 21 3d 30 20 29 7b 0a 20 ( nTrunc!=0 ){.
11615 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
11616 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 70 3PagerTruncate(p
11617 50 61 67 65 72 2c 20 6e 54 72 75 6e 63 29 3b 0a Pager, nTrunc);.
11618 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
11619 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 LITE_OK ) goto s
1161a 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a ync_exit;. }.
1161b 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 #endif.. /* W
1161c 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70 rite all dirty p
1161d 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 ages to the data
1161e 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 base file */.
1161f 20 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63 pPg = sqlite3Pc
11620 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 acheDirtyList(pP
11621 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
11622 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 rc = pager_w
11623 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50 rite_pagelist(pP
11624 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d g);. if( rc!=
11625 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
11626 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
11627 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 QLITE_IOERR_BLOC
11628 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 KED );. /*
11629 54 68 65 20 65 72 72 6f 72 20 6d 69 67 68 74 20 The error might
1162a 68 61 76 65 20 6c 65 66 74 20 74 68 65 20 64 69 have left the di
1162b 72 74 79 20 6c 69 73 74 20 61 6c 6c 20 66 6f 75 rty list all fou
1162c 6c 65 64 20 75 70 20 68 65 72 65 2c 0a 20 20 20 led up here,.
1162d 20 20 20 2a 2a 20 62 75 74 20 74 68 61 74 20 64 ** but that d
1162e 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 20 62 oes not matter b
1162f 65 63 61 75 73 65 20 69 66 20 74 68 65 20 69 66 ecause if the if
11630 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 the dirty list
11631 64 69 64 0a 20 20 20 20 20 20 2a 2a 20 67 65 74 did. ** get
11632 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e corrupted, then
11633 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
11634 20 77 69 6c 6c 20 72 6f 6c 6c 20 62 61 63 6b 20 will roll back
11635 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 64 69 73 and. ** dis
11636 63 61 72 64 20 74 68 65 20 64 69 72 74 79 20 6c card the dirty l
11637 69 73 74 2e 20 20 54 68 65 72 65 20 69 73 20 61 ist. There is a
11638 6e 20 61 73 73 65 72 74 20 69 6e 0a 20 20 20 20 n assert in.
11639 20 20 2a 2a 20 70 61 67 65 72 5f 67 65 74 5f 61 ** pager_get_a
1163a 6c 6c 5f 64 69 72 74 79 5f 70 61 67 65 73 28 29 ll_dirty_pages()
1163b 20 74 68 61 74 20 76 65 72 69 66 69 65 73 20 74 that verifies t
1163c 68 61 74 20 6e 6f 20 61 74 74 65 6d 70 74 0a 20 hat no attempt.
1163d 20 20 20 20 20 2a 2a 20 69 73 20 6d 61 64 65 20 ** is made
1163e 74 6f 20 75 73 65 20 61 6e 20 69 6e 76 61 6c 69 to use an invali
1163f 64 20 64 69 72 74 79 20 6c 69 73 74 2e 0a 20 20 d dirty list..
11640 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74 */. got
11641 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 o sync_exit;.
11642 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 }. sqlite3Pc
11643 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 acheCleanAll(pPa
11644 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a ger->pPCache);..
11645 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 /* Sync the
11646 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a database file. *
11647 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 /. if( !pPage
11648 72 2d 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f r->noSync && !no
11649 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 Sync ){. rc
1164a 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
1164b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 (pPager->fd, pPa
1164c 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 ger->sync_flags)
1164d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 ;. }. IOTR
1164e 41 43 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c ACE(("DBSYNC %p\
1164f 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 n", pPager))..
11650 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
11651 3d 20 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a = PAGER_SYNCED;.
11652 20 20 7d 65 6c 73 65 20 69 66 28 20 4d 45 4d 44 }else if( MEMD
11653 42 20 26 26 20 6e 54 72 75 6e 63 21 3d 30 20 29 B && nTrunc!=0 )
11654 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
11655 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 e3PagerTruncate(
11656 70 50 61 67 65 72 2c 20 6e 54 72 75 6e 63 29 3b pPager, nTrunc);
11657 0a 20 20 7d 0a 0a 73 79 6e 63 5f 65 78 69 74 3a . }..sync_exit:
11658 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
11659 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 E_IOERR_BLOCKED
1165a 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f ){. /* pager_
1165b 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
1165c 65 72 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74 er() may attempt
1165d 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78 to obtain an ex
1165e 63 6c 75 73 69 76 65 0a 20 20 20 20 20 2a 20 6c clusive. * l
1165f 6f 63 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65 ock to spill the
11660 20 63 61 63 68 65 20 61 6e 64 20 72 65 74 75 72 cache and retur
11661 6e 20 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e n IOERR_BLOCKED.
11662 20 42 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20 But since .
11663 20 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 * there is no c
11664 68 61 6e 63 65 20 74 68 65 20 63 61 63 68 65 20 hance the cache
11665 69 73 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c is inconsistent,
11666 20 69 74 20 69 73 0a 20 20 20 20 20 2a 20 62 65 it is. * be
11667 74 74 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53 tter to return S
11668 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 QLITE_BUSY..
11669 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c */. rc = SQL
1166a 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 ITE_BUSY;. }.
1166b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
1166c 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 6c 6c 20 *.** Commit all
1166d 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 changes to the d
1166e 61 74 61 62 61 73 65 20 61 6e 64 20 72 65 6c 65 atabase and rele
1166f 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f ase the write lo
11670 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ck..**.** If the
11671 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20 66 6f commit fails fo
11672 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 61 20 r any reason, a
11673 72 6f 6c 6c 62 61 63 6b 20 61 74 74 65 6d 70 74 rollback attempt
11674 20 69 73 20 6d 61 64 65 0a 2a 2a 20 61 6e 64 20 is made.** and
11675 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 an error code is
11676 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 returned. If t
11677 68 65 20 63 6f 6d 6d 69 74 20 77 6f 72 6b 65 64 he commit worked
11678 2c 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 , SQLITE_OK.** i
11679 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 s returned..*/.S
1167a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1167b 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
1167c 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 mmitPhaseTwo(Pag
1167d 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
1167e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1167f 4b 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 K;.. if( pPager
11680 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 ->errCode ){.
11681 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
11682 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 20 20 69 errCode;. }. i
11683 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
11684 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 <PAGER_RESERVED
11685 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
11686 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
11687 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 if( pPager->db
11688 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 0a 20 Modified==0 &&.
11689 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e (pPager->
1168a 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 journalMode!=PAG
1168b 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 ER_JOURNALMODE_D
1168c 45 4c 45 54 45 20 7c 7c 0a 20 20 20 20 20 20 20 ELETE ||.
1168d 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 pPager->exclu
1168e 73 69 76 65 4d 6f 64 65 21 3d 30 29 20 29 7b 0a siveMode!=0) ){.
1168f 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
11690 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 3d 3d er->dirtyCache==
11691 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 0 || pPager->jou
11692 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20 rnalOpen==0 );.
11693 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
11694 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 50 41 47 45 52 _OK;. }. PAGER
11695 54 52 41 43 45 32 28 22 43 4f 4d 4d 49 54 20 25 TRACE2("COMMIT %
11696 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
11697 61 67 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74 ager));. assert
11698 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d ( pPager->state=
11699 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c =PAGER_SYNCED ||
1169a 20 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 MEMDB || !pPage
1169b 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 29 3b r->dirtyCache );
1169c 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e . rc = pager_en
1169d 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 d_transaction(pP
1169e 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 ager, pPager->se
1169f 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 63 20 3d tMaster);. rc =
116a0 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 pager_error(pPa
116a1 67 65 72 2c 20 72 63 29 3b 0a 20 20 72 65 74 75 ger, rc);. retu
116a2 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
116a3 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 Rollback all cha
116a4 6e 67 65 73 2e 20 20 54 68 65 20 64 61 74 61 62 nges. The datab
116a5 61 73 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 ase falls back t
116a6 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d o PAGER_SHARED m
116a7 6f 64 65 2e 0a 2a 2a 20 41 6c 6c 20 69 6e 2d 6d ode..** All in-m
116a8 65 6d 6f 72 79 20 63 61 63 68 65 20 70 61 67 65 emory cache page
116a9 73 20 72 65 76 65 72 74 20 74 6f 20 74 68 65 69 s revert to thei
116aa 72 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 20 r original data
116ab 63 6f 6e 74 65 6e 74 73 2e 0a 2a 2a 20 54 68 65 contents..** The
116ac 20 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65 6c 65 journal is dele
116ad 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ted..**.** This
116ae 72 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20 66 routine cannot f
116af 61 69 6c 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20 ail unless some
116b0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 other process is
116b1 20 6e 6f 74 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a not following.*
116b2 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f * the correct lo
116b3 63 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c 20 6f cking protocol o
116b4 72 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20 6f 74 r unless some ot
116b5 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 her.** process i
116b6 73 20 77 72 69 74 69 6e 67 20 74 72 61 73 68 20 s writing trash
116b7 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
116b8 20 66 69 6c 65 20 28 53 51 4c 49 54 45 5f 43 4f file (SQLITE_CO
116b9 52 52 55 50 54 29 20 6f 72 0a 2a 2a 20 75 6e 6c RRUPT) or.** unl
116ba 65 73 73 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c ess a prior mall
116bb 6f 63 28 29 20 66 61 69 6c 65 64 20 28 53 51 4c oc() failed (SQL
116bc 49 54 45 5f 4e 4f 4d 45 4d 29 2e 20 20 41 70 70 ITE_NOMEM). App
116bd 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 0a 2a ropriate error.*
116be 2a 20 63 6f 64 65 73 20 61 72 65 20 72 65 74 75 * codes are retu
116bf 72 6e 65 64 20 66 6f 72 20 61 6c 6c 20 74 68 65 rned for all the
116c0 73 65 20 6f 63 63 61 73 69 6f 6e 73 2e 20 20 4f se occasions. O
116c1 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 53 51 4c therwise,.** SQL
116c2 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
116c3 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
116c4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
116c5 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 3PagerRollback(P
116c6 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
116c7 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
116c8 5f 4f 4b 3b 0a 20 20 50 41 47 45 52 54 52 41 43 _OK;. PAGERTRAC
116c9 45 32 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 5c E2("ROLLBACK %d\
116ca 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
116cb 65 72 29 29 3b 0a 20 20 69 66 28 20 21 70 50 61 er));. if( !pPa
116cc 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 ger->dirtyCache
116cd 7c 7c 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || !pPager->jour
116ce 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 72 nalOpen ){. r
116cf 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 c = pager_end_tr
116d0 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 ansaction(pPager
116d1 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 , pPager->setMas
116d2 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ter);. }else if
116d3 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
116d4 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 e && pPager->err
116d5 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c Code!=SQLITE_FUL
116d6 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 L ){. if( pPa
116d7 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
116d8 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 R_EXCLUSIVE ){.
116d9 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61 79 62 pager_playb
116da 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ack(pPager, 0);.
116db 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 }. rc = p
116dc 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
116dd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 }else{. if(
116de 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
116df 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 PAGER_RESERVED )
116e0 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b {. int rc2;
116e1 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
116e2 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 r_playback(pPage
116e3 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32 r, 0);. rc2
116e4 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 = pager_end_tra
116e5 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
116e6 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
116e7 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 er);. if( r
116e8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
116e9 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 rc = rc2
116ea 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
116eb 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
116ec 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 pager_playback(p
116ed 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d Pager, 0);. }
116ee 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42 .. if( !MEMDB
116ef 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
116f0 2d 3e 64 62 53 69 7a 65 20 3d 20 2d 31 3b 0a 20 ->dbSize = -1;.
116f1 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
116f2 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
116f3 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 during a ROLLBAC
116f4 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e K, we can no lon
116f5 67 65 72 20 74 72 75 73 74 20 74 68 65 20 70 61 ger trust the pa
116f6 67 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 ger. ** cache
116f7 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f . So call pager_
116f8 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 77 error() on the w
116f9 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61 ay out to make a
116fa 6e 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a 2a ny error . **
116fb 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 20 persistent..
116fc 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 */. rc = pag
116fd 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
116fe 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rc);. }. retu
116ff 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
11700 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 Return TRUE if t
11701 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11702 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d is opened read-
11703 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41 only. Return FA
11704 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 LSE.** if the da
11705 74 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 68 tabase is (in th
11706 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a eory) writable..
11707 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
11708 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
11709 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 erIsreadonly(Pag
1170a 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
1170b 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65 eturn pPager->re
1170c 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a adOnly;.}../*.**
1170d 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1170e 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
1170f 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a to the pager..*
11710 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
11711 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
11712 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20 rRefcount(Pager
11713 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 *pPager){. retu
11714 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 rn sqlite3Pcache
11715 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
11716 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a >pPCache);.}../*
11717 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
11718 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e umber of referen
11719 63 65 73 20 74 6f 20 74 68 65 20 73 70 65 63 69 ces to the speci
1171a 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 fied page..*/.SQ
1171b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1171c 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
1171d 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 eRefcount(DbPage
1171e 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 *pPage){. retu
1171f 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 rn sqlite3Pcache
11720 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61 PageRefcount(pPa
11721 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 ge);.}..#ifdef S
11722 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a QLITE_TEST./*.**
11723 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
11724 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
11725 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f g and analysis o
11726 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nly..*/.SQLITE_P
11727 52 49 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 RIVATE int *sqli
11728 74 65 33 50 61 67 65 72 53 74 61 74 73 28 50 61 te3PagerStats(Pa
11729 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
1172a 73 74 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d static int a[11]
1172b 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74 ;. a[0] = sqlit
1172c 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
1172d 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
1172e 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69 );. a[1] = sqli
1172f 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 te3PcachePagecou
11730 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
11731 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71 he);. a[2] = sq
11732 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61 lite3PcacheGetCa
11733 63 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e chesize(pPager->
11734 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d pPCache);. a[3]
11735 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a = pPager->dbSiz
11736 65 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67 e;. a[4] = pPag
11737 65 72 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35 er->state;. a[5
11738 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 ] = pPager->errC
11739 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 ode;. a[6] = pP
1173a 61 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b ager->nHit;. a[
1173b 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69 7] = pPager->nMi
1173c 73 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 ss;. a[8] = 0;
1173d 20 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 /* Used to be p
1173e 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a Pager->nOvfl */.
1173f 20 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d a[9] = pPager-
11740 3e 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 >nRead;. a[10]
11741 3d 20 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 = pPager->nWrite
11742 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a ;. return a;.}.
11743 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
11744 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 nt sqlite3PagerI
11745 73 4d 65 6d 64 62 28 50 61 67 65 72 20 2a 70 50 sMemdb(Pager *pP
11746 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
11747 4d 45 4d 44 42 3b 0a 7d 0a 23 65 6e 64 69 66 0a MEMDB;.}.#endif.
11748 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 ./*.** Set the s
11749 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 63 tatement rollbac
1174a 6b 20 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 k point..**.** T
1174b 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 his routine shou
1174c 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 ld be called wit
1174d 68 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f h the transactio
1174e 6e 20 6a 6f 75 72 6e 61 6c 20 61 6c 72 65 61 64 n journal alread
1174f 79 0a 2a 2a 20 6f 70 65 6e 2e 20 20 41 20 6e 65 y.** open. A ne
11750 77 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 w statement jour
11751 6e 61 6c 20 69 73 20 63 72 65 61 74 65 64 20 74 nal is created t
11752 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 hat can be used
11753 74 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 63 to rollback.** c
11754 68 61 6e 67 65 73 20 6f 66 20 61 20 73 69 6e 67 hanges of a sing
11755 6c 65 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 77 le SQL command w
11756 69 74 68 69 6e 20 61 20 6c 61 72 67 65 72 20 74 ithin a larger t
11757 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ransaction..*/.s
11758 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 tatic int pagerS
11759 74 6d 74 42 65 67 69 6e 28 50 61 67 65 72 20 2a tmtBegin(Pager *
1175a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
1175b 63 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 c;. assert( !pP
1175c 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 ager->stmtInUse
1175d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
1175e 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
1175f 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 61 73 R_SHARED );. as
11760 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 sert( pPager->db
11761 53 69 7a 65 3e 3d 30 20 29 3b 0a 20 20 50 41 47 Size>=0 );. PAG
11762 45 52 54 52 41 43 45 32 28 22 53 54 4d 54 2d 42 ERTRACE2("STMT-B
11763 45 47 49 4e 20 25 64 5c 6e 22 2c 20 50 41 47 45 EGIN %d\n", PAGE
11764 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 RID(pPager));.
11765 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 if( !pPager->jou
11766 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 rnalOpen ){.
11767 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f pPager->stmtAuto
11768 6f 70 65 6e 20 3d 20 31 3b 0a 20 20 20 20 72 65 open = 1;. re
11769 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1176a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 }. assert( pP
1176b 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
1176c 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 n );. assert( p
1176d 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 3d 3d Pager->pInStmt==
1176e 30 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 0 );. pPager->p
1176f 49 6e 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 InStmt = sqlite3
11770 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50 61 BitvecCreate(pPa
11771 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 ger->dbSize);.
11772 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 if( pPager->pInS
11773 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a tmt==0 ){. /*
11774 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 sqlite3OsLock(p
11775 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 Pager->fd, SHARE
11776 44 5f 4c 4f 43 4b 29 3b 20 2a 2f 0a 20 20 20 20 D_LOCK); */.
11777 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
11778 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 MEM;. }. pPage
11779 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 70 r->stmtJSize = p
1177a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
1177b 66 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d f;. pPager->stm
1177c 74 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e tSize = pPager->
1177d 64 62 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 72 dbSize;. pPager
1177e 2d 3e 73 74 6d 74 48 64 72 4f 66 66 20 3d 20 30 ->stmtHdrOff = 0
1177f 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 ;. pPager->stmt
11780 43 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e Cksum = pPager->
11781 63 6b 73 75 6d 49 6e 69 74 3b 0a 20 20 69 66 28 cksumInit;. if(
11782 20 21 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 !pPager->stmtOp
11783 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 en ){. if( pP
11784 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
11785 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
11786 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 MODE_MEMORY ){.
11787 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a sqlite3MemJ
11788 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 ournalOpen(pPage
11789 72 2d 3e 73 74 66 64 29 3b 0a 20 20 20 20 7d 65 r->stfd);. }e
1178a 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1178b 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
1178c 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61 temp(pPager, pPa
1178d 67 65 72 2d 3e 73 74 66 64 2c 20 53 51 4c 49 54 ger->stfd, SQLIT
1178e 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 E_OPEN_SUBJOURNA
1178f 4c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 L);. if( rc
11790 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
11791 20 73 74 6d 74 5f 62 65 67 69 6e 5f 66 61 69 6c stmt_begin_fail
11792 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ed;. }.
11793 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 }. pPager->st
11794 6d 74 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 20 20 mtOpen = 1;.
11795 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 pPager->stmtNRec
11796 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 67 = 0;. }. pPag
11797 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 er->stmtInUse =
11798 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 1;. return SQLI
11799 54 45 5f 4f 4b 3b 0a 20 0a 73 74 6d 74 5f 62 65 TE_OK;. .stmt_be
1179a 67 69 6e 5f 66 61 69 6c 65 64 3a 0a 20 20 69 66 gin_failed:. if
1179b 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d ( pPager->pInStm
1179c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
1179d 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 BitvecDestroy(pP
1179e 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 29 3b 0a ager->pInStmt);.
1179f 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 pPager->pInS
117a0 74 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 tmt = 0;. }. r
117a1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 eturn rc;.}.SQLI
117a2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
117a3 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 42 qlite3PagerStmtB
117a4 65 67 69 6e 28 50 61 67 65 72 20 2a 70 50 61 67 egin(Pager *pPag
117a5 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 er){. int rc;.
117a6 20 72 63 20 3d 20 70 61 67 65 72 53 74 6d 74 42 rc = pagerStmtB
117a7 65 67 69 6e 28 70 50 61 67 65 72 29 3b 0a 20 20 egin(pPager);.
117a8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
117a9 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 73 74 61 .** Commit a sta
117aa 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 tement..*/.SQLIT
117ab 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
117ac 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 43 6f lite3PagerStmtCo
117ad 6d 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 mmit(Pager *pPag
117ae 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 er){. if( pPage
117af 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a r->stmtInUse ){.
117b0 20 20 20 20 50 41 47 45 52 54 52 41 43 45 32 28 PAGERTRACE2(
117b1 22 53 54 4d 54 2d 43 4f 4d 4d 49 54 20 25 64 5c "STMT-COMMIT %d\
117b2 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
117b3 65 72 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 er));. sqlite
117b4 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
117b5 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 29 3b Pager->pInStmt);
117b6 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e . pPager->pIn
117b7 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 70 50 Stmt = 0;. pP
117b8 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 20 3d ager->stmtNRec =
117b9 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
117ba 73 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20 stmtInUse = 0;.
117bb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 if( sqlite3Is
117bc 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 MemJournal(pPage
117bd 72 2d 3e 73 74 66 64 29 20 29 7b 0a 20 20 20 20 r->stfd) ){.
117be 20 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 sqlite3OsTrunc
117bf 61 74 65 28 70 50 61 67 65 72 2d 3e 73 74 66 64 ate(pPager->stfd
117c0 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a , 0);. }. }.
117c1 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75 pPager->stmtAu
117c2 74 6f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 72 65 toopen = 0;. re
117c3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
117c4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 }../*.** Rollbac
117c5 6b 20 61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a k a statement..*
117c6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
117c7 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
117c8 72 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 50 61 rStmtRollback(Pa
117c9 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
117ca 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 50 int rc;. if( pP
117cb 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 ager->stmtInUse
117cc 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 ){. PAGERTRAC
117cd 45 32 28 22 53 54 4d 54 2d 52 4f 4c 4c 42 41 43 E2("STMT-ROLLBAC
117ce 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 K %d\n", PAGERID
117cf 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 72 (pPager));. r
117d0 63 20 3d 20 70 61 67 65 72 5f 73 74 6d 74 5f 70 c = pager_stmt_p
117d1 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 29 3b layback(pPager);
117d2 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
117d3 72 53 74 6d 74 43 6f 6d 6d 69 74 28 70 50 61 67 rStmtCommit(pPag
117d4 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 er);. }else{.
117d5 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
117d6 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e ;. }. pPager->
117d7 73 74 6d 74 41 75 74 6f 6f 70 65 6e 20 3d 20 30 stmtAutoopen = 0
117d8 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
117d9 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
117da 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 he full pathname
117db 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
117dc 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 file..*/.SQLITE
117dd 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
117de 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 har *sqlite3Page
117df 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 20 rFilename(Pager
117e0 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 *pPager){. retu
117e1 72 6e 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 rn pPager->zFile
117e2 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 name;.}../*.** R
117e3 65 74 75 72 6e 20 74 68 65 20 56 46 53 20 73 74 eturn the VFS st
117e4 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 ructure for the
117e5 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pager..*/.SQLITE
117e6 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
117e7 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 qlite3_vfs *sqli
117e8 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65 te3PagerVfs(Page
117e9 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
117ea 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 56 66 turn pPager->pVf
117eb 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 s;.}../*.** Retu
117ec 72 6e 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 rn the file hand
117ed 6c 65 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 le for the datab
117ee 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61 ase file associa
117ef 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 ted.** with the
117f0 70 61 67 65 72 2e 20 20 54 68 69 73 20 6d 69 67 pager. This mig
117f1 68 74 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 ht return NULL i
117f2 66 20 74 68 65 20 66 69 6c 65 20 68 61 73 0a 2a f the file has.*
117f3 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f * not yet been o
117f4 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pened..*/.SQLITE
117f5 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
117f6 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 50 61 _file *sqlite3Pa
117f7 67 65 72 46 69 6c 65 28 50 61 67 65 72 20 2a 70 gerFile(Pager *p
117f8 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
117f9 20 70 50 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a pPager->fd;.}..
117fa 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
117fb 20 64 69 72 65 63 74 6f 72 79 20 6f 66 20 74 68 directory of th
117fc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
117fd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
117fe 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
117ff 71 6c 69 74 65 33 50 61 67 65 72 44 69 72 6e 61 qlite3PagerDirna
11800 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 me(Pager *pPager
11801 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
11802 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 3b 0a er->zDirectory;.
11803 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
11804 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d the full pathnam
11805 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
11806 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 file..*/.SQLITE
11807 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
11808 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 har *sqlite3Page
11809 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 rJournalname(Pag
1180a 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
1180b 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a 4a eturn pPager->zJ
1180c 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ournal;.}../*.**
1180d 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
1180e 66 73 79 6e 63 28 29 20 63 61 6c 6c 73 20 61 72 fsync() calls ar
1180f 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 74 e disabled for t
11810 68 69 73 20 70 61 67 65 72 2e 20 20 52 65 74 75 his pager. Retu
11811 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 66 rn FALSE.** if f
11812 73 79 6e 63 28 29 73 20 61 72 65 20 65 78 65 63 sync()s are exec
11813 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a uted normally..*
11814 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
11815 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
11816 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 20 2a 70 rNosync(Pager *p
11817 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
11818 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b pPager->noSync;
11819 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .}..#ifdef SQLIT
1181a 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a 2a E_HAS_CODEC./*.*
1181b 2a 20 53 65 74 20 74 68 65 20 63 6f 64 65 63 20 * Set the codec
1181c 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 0a 2a for this pager.*
1181d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1181e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
1181f 65 72 53 65 74 43 6f 64 65 63 28 0a 20 20 50 61 erSetCodec(. Pa
11820 67 65 72 20 2a 70 50 61 67 65 72 2c 0a 20 20 76 ger *pPager,. v
11821 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 oid *(*xCodec)(v
11822 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c oid*,void*,Pgno,
11823 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43 int),. void *pC
11824 6f 64 65 63 41 72 67 0a 29 7b 0a 20 20 70 50 61 odecArg.){. pPa
11825 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d 20 78 43 ger->xCodec = xC
11826 6f 64 65 63 3b 0a 20 20 70 50 61 67 65 72 2d 3e odec;. pPager->
11827 70 43 6f 64 65 63 41 72 67 20 3d 20 70 43 6f 64 pCodecArg = pCod
11828 65 63 41 72 67 3b 0a 7d 0a 23 65 6e 64 69 66 0a ecArg;.}.#endif.
11829 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1182a 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1182b 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70 /*.** Move the p
1182c 61 67 65 20 70 50 67 20 74 6f 20 6c 6f 63 61 74 age pPg to locat
1182d 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 ion pgno in the
1182e 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 file..**.** Ther
1182f 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 72 65 66 e must be no ref
11830 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 erences to the p
11831 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c age previously l
11832 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 70 67 6e ocated at.** pgn
11833 6f 20 28 77 68 69 63 68 20 77 65 20 63 61 6c 6c o (which we call
11834 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 67 68 20 pPgOld) though
11835 74 68 61 74 20 70 61 67 65 20 69 73 20 61 6c 6c that page is all
11836 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a 20 69 6e owed to be.** in
11837 20 63 61 63 68 65 2e 20 20 49 66 20 74 68 65 20 cache. If the
11838 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 page previously
11839 6c 6f 63 61 74 65 64 20 61 74 20 70 67 6e 6f 20 located at pgno
1183a 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a is not already.*
1183b 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 * in the rollbac
1183c 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 69 73 k journal, it is
1183d 20 6e 6f 74 20 70 75 74 20 74 68 65 72 65 20 62 not put there b
1183e 79 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e y by this routin
1183f 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e e..**.** Referen
11840 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 ces to the page
11841 70 50 67 20 72 65 6d 61 69 6e 20 76 61 6c 69 64 pPg remain valid
11842 2e 20 55 70 64 61 74 69 6e 67 20 61 6e 79 0a 2a . Updating any.*
11843 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73 73 6f * meta-data asso
11844 63 69 61 74 65 64 20 77 69 74 68 20 70 50 67 20 ciated with pPg
11845 28 69 2e 65 2e 20 64 61 74 61 20 73 74 6f 72 65 (i.e. data store
11846 64 20 69 6e 20 74 68 65 20 6e 45 78 74 72 61 20 d in the nExtra
11847 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 bytes.** allocat
11848 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 ed along with th
11849 65 20 70 61 67 65 29 20 69 73 20 74 68 65 20 72 e page) is the r
1184a 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 esponsibility of
1184b 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a the caller..**.
1184c 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e ** A transaction
1184d 20 6d 75 73 74 20 62 65 20 61 63 74 69 76 65 20 must be active
1184e 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
1184f 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 e is called. It
11850 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a 20 72 65 used to be.** re
11851 71 75 69 72 65 64 20 74 68 61 74 20 61 20 73 74 quired that a st
11852 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
11853 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61 63 74 69 ion was not acti
11854 76 65 2c 20 62 75 74 20 74 68 69 73 20 72 65 73 ve, but this res
11855 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 triction.** has
11856 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 28 43 52 been removed (CR
11857 45 41 54 45 20 49 4e 44 45 58 20 6e 65 65 64 73 EATE INDEX needs
11858 20 74 6f 20 6d 6f 76 65 20 61 20 70 61 67 65 20 to move a page
11859 77 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 when a statement
1185a 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
1185b 69 73 20 61 63 74 69 76 65 29 2e 0a 2a 2a 0a 2a is active)..**.*
1185c 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 * If the fourth
1185d 61 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f 6d 6d argument, isComm
1185e 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c it, is non-zero,
1185f 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 then this page
11860 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d 6f 76 65 is being.** move
11861 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 64 d as part of a d
11862 61 74 61 62 61 73 65 20 72 65 6f 72 67 61 6e 69 atabase reorgani
11863 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 65 66 6f zation just befo
11864 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 re the transacti
11865 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 on .** is being
11866 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20 74 68 committed. In th
11867 69 73 20 63 61 73 65 2c 20 69 74 20 69 73 20 67 is case, it is g
11868 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 74 uaranteed that t
11869 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
1186a 20 0a 2a 2a 20 70 50 67 20 72 65 66 65 72 73 20 .** pPg refers
1186b 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77 to will not be w
1186c 72 69 74 74 65 6e 20 74 6f 20 61 67 61 69 6e 20 ritten to again
1186d 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e within this tran
1186e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 saction..*/.SQLI
1186f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
11870 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 qlite3PagerMovep
11871 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 age(Pager *pPage
11872 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c 20 r, DbPage *pPg,
11873 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69 Pgno pgno, int i
11874 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48 64 sCommit){. PgHd
11875 72 20 2a 70 50 67 4f 6c 64 3b 20 20 2f 2a 20 54 r *pPgOld; /* T
11876 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 he page being ov
11877 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 erwritten. */.
11878 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e Pgno needSyncPgn
11879 6f 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 o = 0;.. assert
1187a 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b ( pPg->nRef>0 );
1187b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 35 28 .. PAGERTRACE5(
1187c 22 4d 4f 56 45 20 25 64 20 70 61 67 65 20 25 64 "MOVE %d page %d
1187d 20 28 6e 65 65 64 53 79 6e 63 3d 25 64 29 20 6d (needSync=%d) m
1187e 6f 76 65 73 20 74 6f 20 25 64 5c 6e 22 2c 20 0a oves to %d\n", .
1187f 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 PAGERID(pP
11880 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f ager), pPg->pgno
11881 2c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 , (pPg->flags&PG
11882 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 HDR_NEED_SYNC)?1
11883 3a 30 2c 20 70 67 6e 6f 29 3b 0a 20 20 49 4f 54 :0, pgno);. IOT
11884 52 41 43 45 28 28 22 4d 4f 56 45 20 25 70 20 25 RACE(("MOVE %p %
11885 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c d %d\n", pPager,
11886 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f pPg->pgno, pgno
11887 29 29 0a 0a 20 20 70 61 67 65 72 5f 67 65 74 5f )).. pager_get_
11888 63 6f 6e 74 65 6e 74 28 70 50 67 29 3b 0a 0a 20 content(pPg);..
11889 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e /* If the journ
1188a 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 al needs to be s
1188b 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 70 ync()ed before p
1188c 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63 61 age pPg->pgno ca
1188d 6e 0a 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 n. ** be writte
1188e 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50 67 2d n to, store pPg-
1188f 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20 76 >pgno in local v
11890 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79 6e 63 ariable needSync
11891 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 Pgno.. **. **
11892 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69 74 20 If the isCommit
11893 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
11894 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f re is no need to
11895 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 0a 20 remember that.
11896 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ** the journal
11897 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 needs to be sync
11898 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 61 ()ed before data
11899 62 61 73 65 20 70 61 67 65 20 70 50 67 2d 3e 70 base page pPg->p
1189a 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 62 65 gno . ** can be
1189b 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 68 65 written to. The
1189c 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 72 65 caller has alre
1189d 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f 74 ady promised not
1189e 20 74 6f 20 77 72 69 74 65 20 74 6f 20 69 74 2e to write to it.
1189f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 67 . */. if( (pPg
118a0 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
118a1 45 44 5f 53 59 4e 43 29 20 26 26 20 21 69 73 43 ED_SYNC) && !isC
118a2 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 6e 65 65 ommit ){. nee
118a3 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50 67 2d dSyncPgno = pPg-
118a4 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73 65 72 >pgno;. asser
118a5 74 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 t( (pPg->flags&P
118a6 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c 29 GHDR_IN_JOURNAL)
118a7 20 7c 7c 20 28 69 6e 74 29 70 67 6e 6f 3e 70 50 || (int)pgno>pP
118a8 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 ager->origDbSize
118a9 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
118aa 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
118ab 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 _DIRTY );. as
118ac 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 sert( pPager->ne
118ad 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 edSync );. }..
118ae 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 /* If the cache
118af 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 contains a page
118b0 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 with page-numbe
118b1 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 r pgno, remove i
118b2 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 t. ** from its
118b3 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f hash chain. Also
118b4 2c 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e , if the PgHdr.n
118b5 65 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 eedSync was set
118b6 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 for . ** page p
118b7 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 gno before the '
118b8 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c move' operation,
118b9 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 it needs to be
118ba 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 retained . ** f
118bb 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 or the page move
118bc 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 d there.. */.
118bd 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 pPg->flags &= ~(
118be 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 7c PGHDR_NEED_SYNC|
118bf 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c PGHDR_IN_JOURNAL
118c0 29 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 );. pPgOld = pa
118c1 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 ger_lookup(pPage
118c2 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 r, pgno);. asse
118c3 72 74 28 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70 rt( !pPgOld || p
118c4 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 PgOld->nRef==1 )
118c5 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 20 29 ;. if( pPgOld )
118c6 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 {. pPg->flags
118c7 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 |= (pPgOld->fla
118c8 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
118c9 4e 43 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 NC);. }. if( s
118ca 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
118cb 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
118cc 6e 61 6c 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 nal, pgno) ){.
118cd 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPg->flags |=
118ce 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c PGHDR_IN_JOURNAL
118cf 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 ;. }.. sqlite3
118d0 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 2c 20 PcacheMove(pPg,
118d1 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 50 67 pgno);. if( pPg
118d2 4f 6c 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Old ){. sqlit
118d3 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 e3PcacheMove(pPg
118d4 4f 6c 64 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c Old, 0);. sql
118d5 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
118d6 65 28 70 50 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a e(pPgOld);. }..
118d7 20 20 6d 61 6b 65 44 69 72 74 79 28 70 50 67 29 makeDirty(pPg)
118d8 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 ;. pPager->dirt
118d9 79 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 70 50 yCache = 1;. pP
118da 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
118db 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65 = 1;.. if( nee
118dc 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20 dSyncPgno ){.
118dd 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50 /* If needSyncP
118de 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c gno is non-zero,
118df 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 then the journa
118e0 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 l file needs to
118e1 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28 be . ** sync(
118e2 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 )ed before any d
118e3 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 ata is written t
118e4 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 o database file
118e5 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e page needSyncPgn
118e6 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e o.. ** Curren
118e7 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67 tly, no such pag
118e8 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 e exists in the
118e9 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74 page-cache and t
118ea 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a he . ** "is j
118eb 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63 ournaled" bitvec
118ec 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73 flag has been s
118ed 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74 et. This needs t
118ee 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79 o be remedied by
118ef 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20 . ** loading
118f0 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 the page into th
118f1 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e e pager-cache an
118f2 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67 d setting the Pg
118f3 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 Hdr.needSync .
118f4 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a ** flag.. *
118f5 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 *. ** If the
118f6 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 attempt to load
118f7 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 the page into th
118f8 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69 e page-cache fai
118f9 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20 ls, (due. **
118fa 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 to a malloc() or
118fb 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c IO failure), cl
118fc 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74 ear the bit in t
118fd 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a he pInJournal[].
118fe 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74 ** array. Ot
118ff 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 herwise, if the
11900 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61 page is loaded a
11901 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e nd written again
11902 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 in. ** this
11903 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 transaction, it
11904 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74 may be written t
11905 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
11906 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a ile before. *
11907 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69 * it is synced i
11908 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
11909 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20 file. This way,
1190a 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e it may end up in
1190b 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 . ** the jour
1190c 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20 nal file twice,
1190d 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20 but that is not
1190e 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a a problem.. *
1190f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c *. ** The sql
11910 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63 ite3PagerGet() c
11911 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68 all may cause th
11912 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e e journal to syn
11913 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a c. So make. *
11914 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72 * sure the Pager
11915 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 .needSync flag i
11916 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a s set too.. *
11917 2f 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 /. int rc;.
11918 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72 3b PgHdr *pPgHdr;
11919 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
1191a 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b ger->needSync );
1191b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1191c 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
1191d 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 , needSyncPgno,
1191e 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 69 66 &pPgHdr);. if
1191f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
11920 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 ){. if( pPa
11921 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 ger->pInJournal
11922 26 26 20 28 69 6e 74 29 6e 65 65 64 53 79 6e 63 && (int)needSync
11923 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 6f 72 Pgno<=pPager->or
11924 69 67 44 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 igDbSize ){.
11925 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
11926 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 cClear(pPager->p
11927 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53 InJournal, needS
11928 79 6e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 yncPgno);.
11929 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 }. return r
1192a 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 c;. }. pPa
1192b 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 ger->needSync =
1192c 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 1;. assert( p
1192d 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 Pager->noSync==0
1192e 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 && !MEMDB );.
1192f 20 20 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20 pPgHdr->flags
11930 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 |= PGHDR_NEED_SY
11931 4e 43 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e NC;. pPgHdr->
11932 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 49 flags |= PGHDR_I
11933 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 6d N_JOURNAL;. m
11934 61 6b 65 44 69 72 74 79 28 70 50 67 48 64 72 29 akeDirty(pPgHdr)
11935 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 ;. sqlite3Pag
11936 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b erUnref(pPgHdr);
11937 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 . }.. return S
11938 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 QLITE_OK;.}.#end
11939 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
1193a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1193b 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 73 e data for the s
1193c 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a pecified page..*
1193d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1193e 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
1193f 67 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67 gerGetData(DbPag
11940 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72 e *pPg){. asser
11941 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 7c t( pPg->nRef>0 |
11942 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6d | pPg->pPager->m
11943 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75 72 6e emDb );. return
11944 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d 0a 0a pPg->pData;.}..
11945 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 /*.** Return a p
11946 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 ointer to the Pa
11947 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74 65 73 ger.nExtra bytes
11948 20 6f 66 20 22 65 78 74 72 61 22 20 73 70 61 63 of "extra" spac
11949 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 e .** allocated
1194a 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 73 along with the s
1194b 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a pecified page..*
1194c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1194d 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
1194e 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61 gerGetExtra(DbPa
1194f 67 65 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 ge *pPg){. Page
11950 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
11951 3e 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 >pPager;. retur
11952 6e 20 28 70 50 61 67 65 72 3f 70 50 67 2d 3e 70 n (pPager?pPg->p
11953 45 78 74 72 61 3a 30 29 3b 0a 7d 0a 0a 2f 2a 0a Extra:0);.}../*.
11954 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 6c ** Get/set the l
11955 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 ocking-mode for
11956 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 61 this pager. Para
11957 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 meter eMode must
11958 20 62 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 be one.** of PA
11959 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
1195a 51 55 45 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43 QUERY, PAGER_LOC
1195b 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 KINGMODE_NORMAL
1195c 6f 72 20 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 or .** PAGER_LOC
1195d 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
1195e 56 45 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d VE. If the param
1195f 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 eter is not _QUE
11960 52 59 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 RY, then.** the
11961 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20 locking-mode is
11962 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 set to the value
11963 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a specified..**.*
11964 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 * The returned v
11965 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 50 alue is either P
11966 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
11967 5f 4e 4f 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 _NORMAL or.** PA
11968 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
11969 45 58 43 4c 55 53 49 56 45 2c 20 69 6e 64 69 63 EXCLUSIVE, indic
1196a 61 74 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e ating the curren
1196b 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 t (possibly upda
1196c 74 65 64 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d ted).** locking-
1196d 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mode..*/.SQLITE_
1196e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1196f 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d te3PagerLockingM
11970 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ode(Pager *pPage
11971 72 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 r, int eMode){.
11972 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d assert( eMode==
11973 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
11974 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 E_QUERY.
11975 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 || eMode==PA
11976 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
11977 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20 20 20 20 NORMAL.
11978 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
11979 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 ER_LOCKINGMODE_E
1197a 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 XCLUSIVE );. as
1197b 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b sert( PAGER_LOCK
1197c 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 INGMODE_QUERY<0
1197d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
1197e 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
1197f 4f 52 4d 41 4c 3e 3d 30 20 26 26 20 50 41 47 45 ORMAL>=0 && PAGE
11980 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
11981 43 4c 55 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20 CLUSIVE>=0 );.
11982 69 66 28 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 if( eMode>=0 &&
11983 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
11984 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d e ){. pPager-
11985 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d >exclusiveMode =
11986 20 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 eMode;. }. re
11987 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72 turn (int)pPager
11988 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b ->exclusiveMode;
11989 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 .}../*.** Get/se
1198a 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f t the journal-mo
1198b 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 de for this page
1198c 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f r. Parameter eMo
1198d 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f de must be one o
1198e 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45 f:.**.** PAGE
1198f 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
11990 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f ERY.** PAGER_
11991 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 JOURNALMODE_DELE
11992 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a TE.** PAGER_J
11993 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
11994 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f ATE.** PAGER_
11995 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 JOURNALMODE_PERS
11996 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f IST.** PAGER_
11997 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a JOURNALMODE_OFF.
11998 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 **.** If the par
11999 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 ameter is not _Q
1199a 55 45 52 59 2c 20 74 68 65 6e 20 74 68 65 20 6a UERY, then the j
1199b 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 69 73 20 73 ournal-mode is s
1199c 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c et to the.** val
1199d 75 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a ue specified..**
1199e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 .** The returned
1199f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 63 75 indicate the cu
119a0 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c 79 20 rrent (possibly
119a1 75 70 64 61 74 65 64 29 0a 2a 2a 20 6a 6f 75 72 updated).** jour
119a2 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 4c nal-mode..*/.SQL
119a3 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
119a4 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 sqlite3PagerJour
119a5 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a 70 nalMode(Pager *p
119a6 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64 65 Pager, int eMode
119a7 29 7b 0a 20 20 69 66 28 20 21 4d 45 4d 44 42 20 ){. if( !MEMDB
119a8 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 ){. assert( e
119a9 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
119aa 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 20 20 NALMODE_QUERY.
119ab 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
119ac 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
119ad 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 NALMODE_DELETE.
119ae 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
119af 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
119b0 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 RNALMODE_TRUNCAT
119b1 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E.
119b2 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
119b3 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 JOURNALMODE_PERS
119b4 49 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 IST.
119b5 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 || eMode==PAGE
119b6 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
119b7 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 F .
119b8 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 || eMode==PAGER
119b9 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
119ba 4f 52 59 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ORY );. asser
119bb 74 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c t( PAGER_JOURNAL
119bc 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a MODE_QUERY<0 );.
119bd 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d 30 if( eMode>=0
119be 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
119bf 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 ->journalMode =
119c0 65 4d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 eMode;. }else
119c1 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
119c2 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
119c3 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 29 RNALMODE_QUERY )
119c4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
119c5 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72 turn (int)pPager
119c6 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a 7d ->journalMode;.}
119c7 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 ../*.** Get/set
119c8 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 74 20 75 the size-limit u
119c9 73 65 64 20 66 6f 72 20 70 65 72 73 69 73 74 65 sed for persiste
119ca 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 nt journal files
119cb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
119cc 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 ATE i64 sqlite3P
119cd 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c agerJournalSizeL
119ce 69 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 imit(Pager *pPag
119cf 65 72 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b er, i64 iLimit){
119d0 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d . if( iLimit>=-
119d1 31 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 1 ){. pPager-
119d2 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 >journalSizeLimi
119d3 74 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a t = iLimit;. }.
119d4 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
119d5 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 >journalSizeLimi
119d6 74 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 t;.}..#endif /*
119d7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
119d8 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a IO */../********
119d9 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 ****** End of pa
119da 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ger.c **********
119db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
119dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
119dd 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
119de 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
119df 65 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a e btmutex.c ****
119e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
119e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
119e2 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
119e3 37 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 7 August 27.**.*
119e4 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
119e5 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
119e6 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
119e7 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
119e8 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
119e9 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
119ea 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
119eb 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
119ec 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
119ed 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
119ee 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
119ef 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
119f0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
119f1 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
119f2 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
119f3 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
119f4 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
119f5 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
119f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
119f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
119f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
119f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
119fa 20 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c $Id: btmutex.c,
119fb 76 20 31 2e 31 31 20 32 30 30 38 2f 31 30 2f 30 v 1.11 2008/10/0
119fc 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45 7 15:25:48 drh E
119fd 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 xp $.**.** This
119fe 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
119ff 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 de used to imple
11a00 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e 20 ment mutexes on
11a01 42 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a Btree objects..*
11a02 2a 20 54 68 69 73 20 63 6f 64 65 20 72 65 61 6c * This code real
11a03 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74 ly belongs in bt
11a04 72 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72 65 ree.c. But btre
11a05 65 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20 74 e.c is getting t
11a06 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 oo.** big and we
11a07 20 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 want to break i
11a08 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 t down some. Th
11a09 69 73 20 70 61 63 6b 61 67 65 64 20 73 65 65 6d is packaged seem
11a0a 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f ed like.** a goo
11a0b 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f d breakout..*/./
11a0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
11a0d 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 2e nclude btreeInt.
11a0e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
11a0f 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a of btmutex.c ***
11a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
11a11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
11a12 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 49 egin file btreeI
11a13 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
11a14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11a15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
11a16 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 *.** 2004 April
11a17 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
11a18 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
11a19 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
11a1a 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
11a1b 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
11a1c 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
11a1d 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
11a1e 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
11a1f 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
11a20 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
11a21 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
11a22 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
11a23 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
11a24 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
11a25 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
11a26 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
11a27 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
11a28 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
11a29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11a2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11a2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11a2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11a2d 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49 6e .** $Id: btreeIn
11a2e 74 2e 68 2c 76 20 31 2e 33 34 20 32 30 30 38 2f t.h,v 1.34 2008/
11a2f 30 39 2f 33 30 20 31 37 3a 31 38 3a 31 37 20 64 09/30 17:18:17 d
11a30 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 rh Exp $.**.** T
11a31 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 his file impleme
11a32 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 nts a external (
11a33 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 disk-based) data
11a34 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 base using BTree
11a35 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 s..** For a deta
11a36 69 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 iled discussion
11a37 6f 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 of BTrees, refer
11a38 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f to.**.** Do
11a39 6e 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 nald E. Knuth, T
11a3a 48 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 HE ART OF COMPUT
11a3b 45 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 ER PROGRAMMING,
11a3c 56 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 Volume 3:.**
11a3d 20 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 "Sorting And Se
11a3e 61 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 arching", pages
11a3f 34 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 473-480. Addison
11a40 2d 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 -Wesley.** P
11a41 75 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e ublishing Compan
11a42 79 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 y, Reading, Mass
11a43 61 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a achusetts..**.**
11a44 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20 The basic idea
11a45 69 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67 is that each pag
11a46 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f e of the file co
11a47 6e 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 ntains N databas
11a48 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 e.** entries and
11a49 20 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f N+1 pointers to
11a4a 20 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a subpages..**.**
11a4b 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d -------------
11a4c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a4d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a4e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a4f 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 ---.** | Ptr(
11a50 30 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 0) | Key(0) | Pt
11a51 72 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 r(1) | Key(1) |
11a52 2e 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c ... | Key(N-1) |
11a53 20 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d Ptr(N) |.** -
11a54 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a55 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a56 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a57 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
11a58 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 **.** All of the
11a59 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 keys on the pag
11a5a 65 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f e that Ptr(0) po
11a5b 69 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c ints to have val
11a5c 75 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e ues less.** than
11a5d 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 Key(0). All of
11a5e 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 the keys on pag
11a5f 65 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 e Ptr(1) and its
11a60 20 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a subpages have.*
11a61 2a 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 * values greater
11a62 20 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 than Key(0) and
11a63 20 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 less than Key(1
11a64 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b ). All of the k
11a65 65 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 eys.** on Ptr(N)
11a66 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 and its subpage
11a67 73 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 s have values gr
11a68 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e eater than Key(N
11a69 2d 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 -1). And.** so
11a6a 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e forth..**.** Fin
11a6b 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 ding a particula
11a6c 72 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 r key requires r
11a6d 65 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 eading O(log(M))
11a6e 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 pages from the
11a6f 0a 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d .** disk where M
11a70 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
11a71 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 f entries in the
11a72 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 tree..**.** In
11a73 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 this implementat
11a74 69 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 ion, a single fi
11a75 6c 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 le can hold one
11a76 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 or more separate
11a77 20 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 .** BTrees. Ea
11a78 63 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e ch BTree is iden
11a79 74 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e tified by the in
11a7a 64 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 dex of its root
11a7b 70 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 page. The.** ke
11a7c 79 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 y and data for a
11a7d 6e 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d ny entry are com
11a7e 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 bined to form th
11a7f 65 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a e "payload". A.
11a80 2a 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 ** fixed amount
11a81 6f 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 of payload can b
11a82 65 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74 e carried direct
11a83 6c 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ly on the databa
11a84 73 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 se.** page. If
11a85 74 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c the payload is l
11a86 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 arger than the p
11a87 72 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 reset amount the
11a88 6e 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 n surplus.** byt
11a89 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e es are stored on
11a8a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
11a8b 20 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f The payload fo
11a8c 72 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e r an entry.** an
11a8d 64 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 d the preceding
11a8e 70 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 pointer are comb
11a8f 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 ined to form a "
11a90 43 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a Cell". Each .**
11a91 20 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c page has a smal
11a92 6c 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63 l header which c
11a93 6f 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 ontains the Ptr(
11a94 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f N) pointer and o
11a95 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 ther.** informat
11a96 69 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 ion such as the
11a97 73 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 size of key and
11a98 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d data..**.** FORM
11a99 41 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a AT DETAILS.**.**
11a9a 20 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 The file is div
11a9b 69 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e ided into pages.
11a9c 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 The first page
11a9d 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 is called page
11a9e 31 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 1,.** the second
11a9f 20 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 is page 2, and
11aa0 73 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 so forth. A pag
11aa1 65 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f e number of zero
11aa2 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e indicates.** "n
11aa3 6f 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 o such page". T
11aa4 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e he page size can
11aa5 20 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 be anything bet
11aa6 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 ween 512 and 655
11aa7 33 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 36..** Each page
11aa8 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 can be either a
11aa9 20 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 btree page, a f
11aaa 72 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 reelist page or
11aab 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 an overflow.** p
11aac 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 age..**.** The f
11aad 69 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 irst page is alw
11aae 61 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65 ays a btree page
11aaf 2e 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30 . The first 100
11ab0 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 bytes of the fi
11ab1 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 rst.** page cont
11ab2 61 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 ain a special he
11ab3 61 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 ader (the "file
11ab4 68 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65 header") that de
11ab5 73 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 scribes the file
11ab6 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 ..** The format
11ab7 6f 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 of the file head
11ab8 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 er is as follows
11ab9 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 :.**.** OFFSET
11aba 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
11abb 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 IPTION.** 0
11abc 20 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61 16 Hea
11abd 64 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c der string: "SQL
11abe 69 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 ite format 3\000
11abf 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 ".** 16
11ac0 20 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 2 Page siz
11ac1 65 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a e in bytes. .**
11ac2 20 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20 18 1
11ac3 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 File format
11ac4 77 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a write version.**
11ac5 20 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20 19 1
11ac6 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 File format
11ac7 72 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 read version.**
11ac8 20 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20 1
11ac9 20 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 Bytes of unus
11aca 65 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20 ed space at the
11acb 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 end of each page
11acc 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 .** 21
11acd 20 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 1 Max embed
11ace 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 ded payload frac
11acf 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 tion.** 22
11ad0 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 1 Min e
11ad1 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 mbedded payload
11ad2 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 fraction.**
11ad3 32 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 23 1 M
11ad4 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 in leaf payload
11ad5 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 fraction.**
11ad6 32 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46 24 4 F
11ad7 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 ile change count
11ad8 65 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 er.** 28
11ad9 20 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65 4 Reserve
11ada 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 d for future use
11adb 0a 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 .** 32
11adc 20 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65 4 First fre
11add 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 elist page.**
11ade 20 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20 36 4
11adf 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c Number of freel
11ae0 69 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 ist pages in the
11ae1 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 file.** 40
11ae2 20 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34 60 15 4
11ae3 2d 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 -byte meta value
11ae4 73 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 s passed to high
11ae5 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 er layers.**.**
11ae6 41 6c 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67 All of the integ
11ae7 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 62 69 er values are bi
11ae8 67 2d 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73 g-endian (most s
11ae9 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 ignificant byte
11aea 66 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 first)..**.** Th
11aeb 65 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f e file change co
11aec 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 unter is increme
11aed 6e 74 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 nted when the da
11aee 74 61 62 61 73 65 20 69 73 20 63 68 61 6e 67 65 tabase is change
11aef 64 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65 d.** This counte
11af0 72 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 r allows other p
11af1 72 6f 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77 rocesses to know
11af2 20 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68 when the file h
11af3 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e as changed.** an
11af4 64 20 74 68 75 73 20 77 68 65 6e 20 74 68 65 79 d thus when they
11af5 20 6e 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74 need to flush t
11af6 68 65 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a heir cache..**.*
11af7 2a 20 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64 * The max embedd
11af8 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ed payload fract
11af9 69 6f 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e ion is the amoun
11afa 74 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75 t of the total u
11afb 73 61 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69 sable.** space i
11afc 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 63 61 n a page that ca
11afd 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79 n be consumed by
11afe 20 61 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66 a single cell f
11aff 6f 72 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42 or standard.** B
11b00 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 -tree (non-LEAFD
11b01 41 54 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20 ATA) tables. A
11b02 76 61 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61 value of 255 mea
11b03 6e 73 20 31 30 30 25 2e 20 20 54 68 65 20 64 65 ns 100%. The de
11b04 66 61 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c fault.** is to l
11b05 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d imit the maximum
11b06 20 63 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68 cell size so th
11b07 61 74 20 61 74 20 6c 65 61 73 74 20 34 20 63 65 at at least 4 ce
11b08 6c 6c 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 lls will fit.**
11b09 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68 on one page. Th
11b0a 75 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d us the default m
11b0b 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c ax embedded payl
11b0c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 oad fraction is
11b0d 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 64..**.** If the
11b0e 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63 payload for a c
11b0f 65 6c 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68 ell is larger th
11b10 61 6e 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f an the max paylo
11b11 61 64 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a ad, then extra.*
11b12 2a 20 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69 * payload is spi
11b13 6c 6c 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 lled to overflow
11b14 20 70 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e pages. Once an
11b15 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
11b16 73 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 s allocated,.**
11b17 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73 as many bytes as
11b18 20 70 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f possible are mo
11b19 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65 ved into the ove
11b1a 72 66 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68 rflow pages with
11b1b 6f 75 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 out letting.** t
11b1c 68 65 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f he cell size dro
11b1d 70 20 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20 p below the min
11b1e 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
11b1f 20 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a fraction..**.**
11b20 20 54 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61 The min leaf pa
11b21 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 yload fraction i
11b22 73 20 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65 s like the min e
11b23 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 mbedded payload
11b24 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 fraction.** exce
11b25 70 74 20 74 68 61 74 20 69 74 20 61 70 70 6c 69 pt that it appli
11b26 65 73 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73 es to leaf nodes
11b27 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 in a LEAFDATA t
11b28 72 65 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75 ree. The maximu
11b29 6d 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61 m.** payload fra
11b2a 63 74 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46 ction for a LEAF
11b2b 44 41 54 41 20 74 72 65 65 20 69 73 20 61 6c 77 DATA tree is alw
11b2c 61 79 73 20 31 30 30 25 20 28 6f 72 20 32 35 35 ays 100% (or 255
11b2d 29 20 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 ) and it.** not
11b2e 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 specified in the
11b2f 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 header..**.** E
11b30 61 63 68 20 62 74 72 65 65 20 70 61 67 65 73 20 ach btree pages
11b31 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 is divided into
11b32 74 68 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20 three sections:
11b33 20 54 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 The header, the
11b34 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 .** cell pointer
11b35 20 61 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20 array, and the
11b36 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
11b37 61 2e 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20 a. Page 1 also
11b38 68 61 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a has a 100-byte.*
11b39 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 74 68 * file header th
11b3a 61 74 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 at occurs before
11b3b 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
11b3c 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d ..**.** |--
11b3d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a --------------|.
11b3e 2a 2a 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68 ** | file h
11b3f 65 61 64 65 72 20 20 20 20 7c 20 20 20 31 30 30 eader | 100
11b40 20 62 79 74 65 73 2e 20 20 50 61 67 65 20 31 20 bytes. Page 1
11b41 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d only..** |-
11b42 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c ---------------|
11b43 0a 2a 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20 .** | page
11b44 68 65 61 64 65 72 20 20 20 20 7c 20 20 20 38 20 header | 8
11b45 62 79 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73 bytes for leaves
11b46 2e 20 20 31 32 20 62 79 74 65 73 20 66 6f 72 20 . 12 bytes for
11b47 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a interior nodes.*
11b48 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
11b49 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
11b4a 20 20 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 | cell pointer
11b4b 20 20 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65 | | 2 byte
11b4c 73 20 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72 s per cell. Sor
11b4d 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 ted order..**
11b4e 20 20 20 7c 20 61 72 72 61 79 20 20 20 20 20 20 | array
11b4f 20 20 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73 | | Grows
11b50 20 64 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20 downward.**
11b51 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 |
11b52 20 20 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20 | v.**
11b53 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
11b54 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e --|.** | un
11b55 61 6c 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a allocated |.*
11b56 2a 20 20 20 20 20 20 7c 20 73 70 61 63 65 20 20 * | space
11b57 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 |.**
11b58 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-------------
11b59 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20 ---| ^ Grows
11b5a 75 70 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20 upwards.**
11b5b 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 | cell content
11b5c 20 7c 20 20 20 7c 20 20 41 72 62 69 74 72 61 72 | | Arbitrar
11b5d 79 20 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65 y order interspe
11b5e 72 73 65 64 20 77 69 74 68 20 66 72 65 65 62 6c rsed with freebl
11b5f 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 ocks..** |
11b60 61 72 65 61 20 20 20 20 20 20 20 20 20 20 20 7c area |
11b61 20 20 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73 | and free s
11b62 70 61 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a pace fragments..
11b63 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d ** |-------
11b64 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a ---------|.**.**
11b65 20 54 68 65 20 70 61 67 65 20 68 65 61 64 65 72 The page header
11b66 73 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 s looks like thi
11b67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 s:.**.** OFFSE
11b68 54 20 20 20 53 49 5a 45 20 20 20 20 20 44 45 53 T SIZE DES
11b69 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
11b6a 20 30 20 20 20 20 20 20 20 31 20 20 20 20 20 20 0 1
11b6b 46 6c 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79 Flags. 1: intkey
11b6c 2c 20 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34 , 2: zerodata, 4
11b6d 3a 20 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c : leafdata, 8: l
11b6e 65 61 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 eaf.** 1
11b6f 20 20 20 20 32 20 20 20 20 20 20 62 79 74 65 20 2 byte
11b70 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69 offset to the fi
11b71 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a rst freeblock.**
11b72 20 20 20 20 20 20 33 20 20 20 20 20 20 20 32 20 3 2
11b73 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63 number of c
11b74 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 ells on this pag
11b75 65 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20 e.** 5
11b76 20 20 32 20 20 20 20 20 20 66 69 72 73 74 20 62 2 first b
11b77 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 yte of the cell
11b78 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20 content area.**
11b79 20 20 20 20 20 37 20 20 20 20 20 20 20 31 20 20 7 1
11b7a 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 number of fr
11b7b 61 67 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79 agmented free by
11b7c 74 65 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 tes.** 8
11b7d 20 20 20 20 34 20 20 20 20 20 20 52 69 67 68 74 4 Right
11b7e 20 63 68 69 6c 64 20 28 74 68 65 20 50 74 72 28 child (the Ptr(
11b7f 4e 29 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74 N) value). Omit
11b80 74 65 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a ted on leaves..*
11b81 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64 *.** The flags d
11b82 65 66 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74 efine the format
11b83 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 70 of this btree p
11b84 61 67 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66 age. The leaf f
11b85 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a lag means that.*
11b86 2a 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20 * this page has
11b87 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68 no children. Th
11b88 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 e zerodata flag
11b89 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 means that this
11b8a 70 61 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20 page carries.**
11b8b 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f only keys and no
11b8c 20 64 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b data. The intk
11b8d 65 79 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 ey flag means th
11b8e 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 61 20 at the key is a
11b8f 69 6e 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68 integer.** which
11b90 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 is stored in th
11b91 65 20 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79 e key size entry
11b92 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61 of the cell hea
11b93 64 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 der rather than
11b94 69 6e 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61 in.** the payloa
11b95 64 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 d area..**.** Th
11b96 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
11b97 72 72 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74 rray begins on t
11b98 68 65 20 66 69 72 73 74 20 62 79 74 65 20 61 66 he first byte af
11b99 74 65 72 20 74 68 65 20 70 61 67 65 20 68 65 61 ter the page hea
11b9a 64 65 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c der..** The cell
11b9b 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63 pointer array c
11b9c 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20 ontains zero or
11b9d 6d 6f 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62 more 2-byte numb
11b9e 65 72 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a ers which are.**
11b9f 20 6f 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68 offsets from th
11ba0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
11ba1 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 he page to the c
11ba2 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 ell content in t
11ba3 68 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 he cell.** conte
11ba4 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 63 65 nt area. The ce
11ba5 6c 6c 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75 ll pointers occu
11ba6 72 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 r in sorted orde
11ba7 72 2e 20 20 54 68 65 20 73 79 73 74 65 6d 20 73 r. The system s
11ba8 74 72 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 trives.** to kee
11ba9 70 20 66 72 65 65 20 73 70 61 63 65 20 61 66 74 p free space aft
11baa 65 72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c er the last cell
11bab 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 pointer so that
11bac 20 6e 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a new cells can.*
11bad 2a 20 62 65 20 65 61 73 69 6c 79 20 61 64 64 65 * be easily adde
11bae 64 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 d without having
11baf 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74 to defragment t
11bb0 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 he page..**.** C
11bb1 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 ell content is s
11bb2 74 6f 72 65 64 20 61 74 20 74 68 65 20 76 65 72 tored at the ver
11bb3 79 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 y end of the pag
11bb4 65 20 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61 e and grows towa
11bb5 72 64 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e rd the.** beginn
11bb6 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e ing of the page.
11bb7 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70 .**.** Unused sp
11bb8 61 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 63 ace within the c
11bb9 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
11bba 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e is collected in
11bbb 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 to a linked list
11bbc 20 6f 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b of.** freeblock
11bbd 73 2e 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f s. Each freeblo
11bbe 63 6b 20 69 73 20 61 74 20 6c 65 61 73 74 20 34 ck is at least 4
11bbf 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 bytes in size.
11bc0 20 54 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 The byte offset
11bc1 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 .** to the first
11bc2 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69 freeblock is gi
11bc3 76 65 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65 ven in the heade
11bc4 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f r. Freeblocks o
11bc5 63 63 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 ccur in.** incre
11bc6 61 73 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65 asing order. Be
11bc7 63 61 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63 cause a freebloc
11bc8 6b 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 k must be at lea
11bc9 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 st 4 bytes in si
11bca 7a 65 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70 ze,.** any group
11bcb 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 75 of 3 or fewer u
11bcc 6e 75 73 65 64 20 62 79 74 65 73 20 69 6e 20 74 nused bytes in t
11bcd 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
11bce 61 72 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 area cannot.** e
11bcf 78 69 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65 xist on the free
11bd0 62 6c 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20 block chain. A
11bd1 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 group of 3 or fe
11bd2 77 65 72 20 66 72 65 65 20 62 79 74 65 73 20 69 wer free bytes i
11bd3 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 s called.** a fr
11bd4 61 67 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74 agment. The tot
11bd5 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
11bd6 65 73 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65 es in all fragme
11bd7 6e 74 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e nts is recorded.
11bd8 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 .** in the page
11bd9 68 65 61 64 65 72 20 61 74 20 6f 66 66 73 65 74 header at offset
11bda 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 7..**.** SIZ
11bdb 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e E DESCRIPTION
11bdc 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 .** 2 B
11bdd 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 yte offset of th
11bde 65 20 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b e next freeblock
11bdf 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 .** 2 B
11be0 79 74 65 73 20 69 6e 20 74 68 69 73 20 66 72 65 ytes in this fre
11be1 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c eblock.**.** Cel
11be2 6c 73 20 61 72 65 20 6f 66 20 76 61 72 69 61 62 ls are of variab
11be3 6c 65 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c le length. Cell
11be4 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 s are stored in
11be5 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
11be6 20 61 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20 area at.** the
11be7 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e end of the page.
11be8 20 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 Pointers to th
11be9 65 20 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74 e cells are in t
11bea 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
11beb 61 72 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d array.** that im
11bec 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 mediately follow
11bed 73 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 s the page heade
11bee 72 2e 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74 r. Cells is not
11bef 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 necessarily.**
11bf0 63 6f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e contiguous or in
11bf1 20 6f 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c order, but cell
11bf2 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f pointers are co
11bf3 6e 74 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20 ntiguous and in
11bf4 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c order..**.** Cel
11bf5 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20 l content makes
11bf6 75 73 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 use of variable
11bf7 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e length integers.
11bf8 20 20 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 A variable.**
11bf9 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69 length integer i
11bfa 73 20 31 20 74 6f 20 39 20 62 79 74 65 73 20 77 s 1 to 9 bytes w
11bfb 68 65 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37 here the lower 7
11bfc 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a bits of each .*
11bfd 2a 20 62 79 74 65 20 61 72 65 20 75 73 65 64 2e * byte are used.
11bfe 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f The integer co
11bff 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79 nsists of all by
11c00 74 65 73 20 74 68 61 74 20 68 61 76 65 20 62 69 tes that have bi
11c01 74 20 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74 t 8 set and.** t
11c02 68 65 20 66 69 72 73 74 20 62 79 74 65 20 77 69 he first byte wi
11c03 74 68 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20 th bit 8 clear.
11c04 20 54 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66 The most signif
11c05 69 63 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68 icant byte of th
11c06 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70 e integer.** app
11c07 65 61 72 73 20 66 69 72 73 74 2e 20 20 41 20 76 ears first. A v
11c08 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
11c09 6e 74 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62 nteger may not b
11c0a 65 20 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79 e more than 9 by
11c0b 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 tes long..** As
11c0c 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20 a special case,
11c0d 61 6c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74 all 8 bytes of t
11c0e 68 65 20 39 74 68 20 62 79 74 65 20 61 72 65 20 he 9th byte are
11c0f 75 73 65 64 20 61 73 20 64 61 74 61 2e 20 20 54 used as data. T
11c10 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 his.** allows a
11c11 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74 64-bit integer t
11c12 6f 20 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20 o be encoded in
11c13 39 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 9 bytes..**.**
11c14 20 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20 0x00
11c15 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
11c16 6d 65 73 20 20 30 78 30 30 30 30 30 30 30 30 0a mes 0x00000000.
11c17 2a 2a 20 20 20 20 30 78 37 66 20 20 20 20 20 20 ** 0x7f
11c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c19 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 becomes 0x00000
11c1a 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 07f.** 0x81 0
11c1b 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 x00
11c1c 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 becomes 0x0
11c1d 30 30 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78 0000080.** 0x
11c1e 38 32 20 30 78 30 30 20 20 20 20 20 20 20 20 20 82 0x00
11c1f 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 becomes
11c20 20 30 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20 0x00000100.**
11c21 20 20 30 78 38 30 20 30 78 37 66 20 20 20 20 20 0x80 0x7f
11c22 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
11c23 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a mes 0x0000007f.
11c24 2a 2a 20 20 20 20 30 78 38 61 20 30 78 39 31 20 ** 0x8a 0x91
11c25 30 78 64 31 20 30 78 61 63 20 30 78 37 38 20 20 0xd1 0xac 0x78
11c26 62 65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35 becomes 0x12345
11c27 36 37 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 678.** 0x81 0
11c28 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78 x81 0x81 0x81 0x
11c29 30 31 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 01 becomes 0x1
11c2a 30 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 0204081.**.** Va
11c2b 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e riable length in
11c2c 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64 20 tegers are used
11c2d 66 6f 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74 for rowids and t
11c2e 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65 o hold the numbe
11c2f 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66 r of.** bytes of
11c30 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e key and data in
11c31 20 61 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a a btree cell..*
11c32 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 *.** The content
11c33 20 6f 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 of a cell looks
11c34 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
11c35 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 * SIZE DES
11c36 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
11c37 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 4 Page numb
11c38 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63 er of the left c
11c39 68 69 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66 hild. Omitted if
11c3a 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 leaf flag is se
11c3b 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20 t..** var
11c3c 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
11c3d 20 6f 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65 of data. Omitte
11c3e 64 20 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74 d if the zerodat
11c3f 61 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a a flag is set..*
11c40 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d * var Num
11c41 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
11c42 6b 65 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20 key. Or the key
11c43 69 74 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79 itself if intkey
11c44 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a flag is set..**
11c45 20 20 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c * Payl
11c46 6f 61 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 oad.** 4
11c47 20 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 First page of
11c48 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 the overflow cha
11c49 69 6e 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20 in. Omitted if
11c4a 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a no overflow.**.*
11c4b 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 * Overflow pages
11c4c 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c form a linked l
11c4d 69 73 74 2e 20 20 45 61 63 68 20 70 61 67 65 20 ist. Each page
11c4e 65 78 63 65 70 74 20 74 68 65 20 6c 61 73 74 20 except the last
11c4f 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a is completely.**
11c50 20 66 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74 filled with dat
11c51 61 20 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20 a (pagesize - 4
11c52 62 79 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73 bytes). The las
11c53 74 20 70 61 67 65 20 63 61 6e 20 68 61 76 65 20 t page can have
11c54 61 73 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20 as little.** as
11c55 31 20 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a 1 byte of data..
11c56 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 **.** SIZE
11c57 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 DESCRIPTION.**
11c58 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 4 Page
11c59 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f number of next o
11c5a 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 verflow page.**
11c5b 20 20 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a * Data.
11c5c 2a 2a 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70 **.** Freelist p
11c5d 61 67 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f ages come in two
11c5e 20 73 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b subtypes: trunk
11c5f 20 70 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20 pages and leaf
11c60 70 61 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 pages. The.** f
11c61 69 6c 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74 ile header point
11c62 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 s to the first i
11c63 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 n a linked list
11c64 6f 66 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20 of trunk page.
11c65 45 61 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 Each trunk.** pa
11c66 67 65 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c ge points to mul
11c67 74 69 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73 tiple leaf pages
11c68 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f . The content o
11c69 66 20 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 f a leaf page is
11c6a 0a 2a 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e .** unspecified.
11c6b 20 20 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c A trunk page l
11c6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a ooks like this:.
11c6d 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 **.** SIZE
11c6e 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 DESCRIPTION.**
11c6f 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 4 Page
11c70 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74 number of next t
11c71 72 75 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20 runk page.**
11c72 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 4 Number o
11c73 66 20 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20 f leaf pointers
11c74 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 on this page.**
11c75 20 20 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20 * zero
11c76 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75 or more pages nu
11c77 6d 62 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a mbers of leaves.
11c78 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e 64 20 75 70 20 */../* Round up
11c79 61 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 a number to the
11c7a 6e 65 78 74 20 6c 61 72 67 65 72 20 6d 75 6c 74 next larger mult
11c7b 69 70 6c 65 20 6f 66 20 38 2e 20 20 54 68 69 73 iple of 8. This
11c7c 20 69 73 20 75 73 65 64 0a 2a 2a 20 74 6f 20 66 is used.** to f
11c7d 6f 72 63 65 20 38 2d 62 79 74 65 20 61 6c 69 67 orce 8-byte alig
11c7e 6e 6d 65 6e 74 20 6f 6e 20 36 34 2d 62 69 74 20 nment on 64-bit
11c7f 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a architectures..*
11c80 2f 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 38 /.#define ROUND8
11c81 28 78 29 20 20 20 28 28 78 2b 37 29 26 7e 37 29 (x) ((x+7)&~7)
11c82 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 .../* The follow
11c83 69 6e 67 20 76 61 6c 75 65 20 69 73 20 74 68 65 ing value is the
11c84 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 maximum cell si
11c85 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61 ze assuming a ma
11c86 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69 ximum page.** si
11c87 7a 65 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a ze give above..*
11c88 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c /.#define MX_CEL
11c89 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 28 70 42 L_SIZE(pBt) (pB
11c8a 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 0a 0a t->pageSize-8)..
11c8b 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e /* The maximum n
11c8c 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
11c8d 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 n a single page
11c8e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e of the database.
11c8f 20 20 54 68 69 73 0a 2a 2a 20 61 73 73 75 6d 65 This.** assume
11c90 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c s a minimum cell
11c91 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74 65 73 size of 6 bytes
11c92 20 20 28 34 20 62 79 74 65 73 20 66 6f 72 20 74 (4 bytes for t
11c93 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 0a 2a he cell itself.*
11c94 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73 20 66 * plus 2 bytes f
11c95 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 or the index to
11c96 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 the cell in the
11c97 70 61 67 65 20 68 65 61 64 65 72 29 2e 20 20 53 page header). S
11c98 75 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c uch.** small cel
11c99 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72 65 2c ls will be rare,
11c9a 20 62 75 74 20 74 68 65 79 20 61 72 65 20 70 6f but they are po
11c9b 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 ssible..*/.#defi
11c9c 6e 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 ne MX_CELL(pBt)
11c9d 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d ((pBt->pageSize-
11c9e 38 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 8)/6)../* Forwar
11c9f 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a d declarations *
11ca0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
11ca1 20 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61 67 65 MemPage MemPage
11ca2 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
11ca3 20 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a BtLock BtLock;.
11ca4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 ./*.** This is a
11ca5 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 74 68 magic string th
11ca6 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 at appears at th
11ca7 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 e beginning of e
11ca8 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 very.** SQLite d
11ca9 61 74 61 62 61 73 65 20 69 6e 20 6f 72 64 65 72 atabase in order
11caa 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 to identify the
11cab 20 66 69 6c 65 20 61 73 20 61 20 72 65 61 6c 20 file as a real
11cac 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
11cad 59 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74 You can change t
11cae 68 69 73 20 76 61 6c 75 65 20 61 74 20 63 6f 6d his value at com
11caf 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 73 70 65 pile-time by spe
11cb0 63 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 cifying a.** -DS
11cb1 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 QLITE_FILE_HEADE
11cb2 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 20 63 R="..." on the c
11cb3 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d ompiler command-
11cb4 6c 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 68 65 line. The.** he
11cb5 61 64 65 72 20 6d 75 73 74 20 62 65 20 65 78 61 ader must be exa
11cb6 63 74 6c 79 20 31 36 20 62 79 74 65 73 20 69 6e ctly 16 bytes in
11cb7 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65 72 6f cluding the zero
11cb8 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a -terminator so.*
11cb9 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 74 73 * the string its
11cba 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20 31 35 elf should be 15
11cbb 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 characters long
11cbc 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e 67 65 . If you change
11cbd 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20 .** the header,
11cbe 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74 6f 6d then your custom
11cbf 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 6e 6f library will no
11cc0 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61 t be able to rea
11cc1 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 d .** databases
11cc2 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 generated by the
11cc3 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 20 standard tools
11cc4 61 6e 64 20 74 68 65 20 73 74 61 6e 64 61 72 64 and the standard
11cc5 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e tools.** will n
11cc6 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 ot be able to re
11cc7 61 64 20 64 61 74 61 62 61 73 65 73 20 63 72 65 ad databases cre
11cc8 61 74 65 64 20 62 79 20 79 6f 75 72 20 63 75 73 ated by your cus
11cc9 74 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a tom library..*/.
11cca 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 46 #ifndef SQLITE_F
11ccb 49 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20 31 32 ILE_HEADER /* 12
11ccc 33 34 35 36 37 38 39 20 31 32 33 34 35 36 20 2a 3456789 123456 *
11ccd 2f 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 /.# define SQLI
11cce 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 22 TE_FILE_HEADER "
11ccf 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 22 SQLite format 3"
11cd0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 .#endif../*.** P
11cd1 61 67 65 20 74 79 70 65 20 66 6c 61 67 73 2e 20 age type flags.
11cd2 20 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69 6e 61 An ORed combina
11cd3 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 66 6c tion of these fl
11cd4 61 67 73 20 61 70 70 65 61 72 20 61 73 20 74 68 ags appear as th
11cd5 65 0a 2a 2a 20 66 69 72 73 74 20 62 79 74 65 20 e.** first byte
11cd6 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 67 65 of on-disk image
11cd7 20 6f 66 20 65 76 65 72 79 20 42 54 72 65 65 20 of every BTree
11cd8 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 page..*/.#define
11cd9 20 50 54 46 5f 49 4e 54 4b 45 59 20 20 20 20 30 PTF_INTKEY 0
11cda 78 30 31 0a 23 64 65 66 69 6e 65 20 50 54 46 5f x01.#define PTF_
11cdb 5a 45 52 4f 44 41 54 41 20 20 30 78 30 32 0a 23 ZERODATA 0x02.#
11cdc 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 44 define PTF_LEAFD
11cdd 41 54 41 20 20 30 78 30 34 0a 23 64 65 66 69 6e ATA 0x04.#defin
11cde 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20 20 20 e PTF_LEAF
11cdf 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 0x08../*.** As e
11ce0 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 ach page of the
11ce1 66 69 6c 65 20 69 73 20 6c 6f 61 64 65 64 20 69 file is loaded i
11ce2 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 nto memory, an i
11ce3 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
11ce4 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 ollowing.** stru
11ce5 63 74 75 72 65 20 69 73 20 61 70 70 65 6e 64 65 cture is appende
11ce6 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 d and initialize
11ce7 64 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 69 73 d to zero. This
11ce8 20 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 structure store
11ce9 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e s.** information
11cea 20 61 62 6f 75 74 20 74 68 65 20 70 61 67 65 20 about the page
11ceb 74 68 61 74 20 69 73 20 64 65 63 6f 64 65 64 20 that is decoded
11cec 66 72 6f 6d 20 74 68 65 20 72 61 77 20 66 69 6c from the raw fil
11ced 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e page..**.** Th
11cee 65 20 70 50 61 72 65 6e 74 20 66 69 65 6c 64 20 e pParent field
11cef 70 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f 20 74 points back to t
11cf0 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 he parent page.
11cf1 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 75 73 20 This allows us
11cf2 74 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 74 68 to.** walk up th
11cf3 65 20 42 54 72 65 65 20 66 72 6f 6d 20 61 6e 79 e BTree from any
11cf4 20 6c 65 61 66 20 74 6f 20 74 68 65 20 72 6f 6f leaf to the roo
11cf5 74 2e 20 20 43 61 72 65 20 6d 75 73 74 20 62 65 t. Care must be
11cf6 20 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 taken to.** unr
11cf7 65 66 28 29 20 74 68 65 20 70 61 72 65 6e 74 20 ef() the parent
11cf8 70 61 67 65 20 70 6f 69 6e 74 65 72 20 77 68 65 page pointer whe
11cf9 6e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 6e n this page is n
11cfa 6f 20 6c 6f 6e 67 65 72 20 72 65 66 65 72 65 6e o longer referen
11cfb 63 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 ced..** The page
11cfc 44 65 73 74 72 75 63 74 6f 72 28 29 20 72 6f 75 Destructor() rou
11cfd 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 74 68 61 tine handles tha
11cfe 74 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 t chore..**.** A
11cff 63 63 65 73 73 20 74 6f 20 61 6c 6c 20 66 69 65 ccess to all fie
11d00 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 lds of this stru
11d01 63 74 75 72 65 20 69 73 20 63 6f 6e 74 72 6f 6c cture is control
11d02 6c 65 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 led by the mutex
11d03 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 .** stored in Me
11d04 6d 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74 65 78 mPage.pBt->mutex
11d05 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 50 ..*/.struct MemP
11d06 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49 6e 69 age {. u8 isIni
11d07 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
11d08 54 72 75 65 20 69 66 20 70 72 65 76 69 6f 75 73 True if previous
11d09 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 ly initialized.
11d0a 4d 55 53 54 20 42 45 20 46 49 52 53 54 21 20 2a MUST BE FIRST! *
11d0b 2f 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c 6f 77 /. u8 nOverflow
11d0c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
11d0d 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 63 er of overflow c
11d0e 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20 61 43 ell bodies in aC
11d0f 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 69 6e ell[] */. u8 in
11d10 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 tKey;
11d11 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 6b 65 /* True if intke
11d12 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f y flag is set */
11d13 0a 20 20 75 38 20 6c 65 61 66 3b 20 20 20 20 20 . u8 leaf;
11d14 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
11d15 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 if leaf flag is
11d16 73 65 74 20 2a 2f 0a 20 20 75 38 20 68 61 73 44 set */. u8 hasD
11d17 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ata; /*
11d18 20 54 72 75 65 20 69 66 20 74 68 69 73 20 70 61 True if this pa
11d19 67 65 20 73 74 6f 72 65 73 20 64 61 74 61 20 2a ge stores data *
11d1a 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73 65 74 /. u8 hdrOffset
11d1b 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30 30 20 ; /* 100
11d1c 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30 20 6f for page 1. 0 o
11d1d 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 75 38 therwise */. u8
11d1e 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 20 20 childPtrSize;
11d1f 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61 66 3d /* 0 if leaf=
11d20 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66 3d 3d =1. 4 if leaf==
11d21 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 0 */. u16 maxLo
11d22 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 cal; /* C
11d23 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e opy of BtShared.
11d24 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 maxLocal or BtSh
11d25 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a ared.maxLeaf */.
11d26 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 u16 minLocal;
11d27 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
11d28 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 6f f BtShared.minLo
11d29 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e cal or BtShared.
11d2a 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 minLeaf */. u16
11d2b 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 cellOffset;
11d2c 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 44 /* Index in aD
11d2d 61 74 61 20 6f 66 20 66 69 72 73 74 20 63 65 6c ata of first cel
11d2e 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 l pointer */. u
11d2f 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 16 nFree;
11d30 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
11d31 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e 20 74 free bytes on t
11d32 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 he page */. u16
11d33 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 nCell;
11d34 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
11d35 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 ells on this pag
11d36 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f 76 66 e, local and ovf
11d37 6c 20 2a 2f 0a 20 20 75 31 36 20 6d 61 73 6b 50 l */. u16 maskP
11d38 61 67 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d age; /* M
11d39 61 73 6b 20 66 6f 72 20 70 61 67 65 20 6f 66 66 ask for page off
11d3a 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 set */. struct
11d3b 5f 4f 76 66 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a _OvflCell { /*
11d3c 20 43 65 6c 6c 73 20 74 68 61 74 20 77 69 6c 6c Cells that will
11d3d 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 44 61 74 not fit on aDat
11d3e 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70 a[] */. u8 *p
11d3f 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f Cell; /
11d40 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 * Pointers to th
11d41 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 6f 76 e body of the ov
11d42 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 erflow cell */.
11d43 20 20 20 75 31 36 20 69 64 78 3b 20 20 20 20 20 u16 idx;
11d44 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 /* Insert
11d45 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66 6f 72 this cell befor
11d46 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 e idx-th non-ove
11d47 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 rflow cell */.
11d48 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 } aOvfl[5];. Bt
11d49 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 Shared *pBt;
11d4a 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
11d4b 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 74 BtShared that t
11d4c 68 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74 his page is part
11d4d 20 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 of */. u8 *aDa
11d4e 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ta; /*
11d4f 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 73 6b Pointer to disk
11d50 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 70 61 image of the pa
11d51 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 44 62 50 ge data */. DbP
11d52 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 age *pDbPage;
11d53 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67 65 20 /* Pager page
11d54 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f handle */. Pgno
11d55 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
11d56 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
11d57 66 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f for this page */
11d58 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 .};../*.** The i
11d59 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f n-memory image o
11d5a 66 20 61 20 64 69 73 6b 20 70 61 67 65 20 68 61 f a disk page ha
11d5b 73 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 s the auxiliary
11d5c 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 65 information appe
11d5d 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 65 nded.** to the e
11d5e 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a 45 20 nd. EXTRA_SIZE
11d5f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
11d60 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
11d61 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 0a 2a needed to hold.*
11d62 2a 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66 * that extra inf
11d63 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 ormation..*/.#de
11d64 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a 45 20 fine EXTRA_SIZE
11d65 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 0a sizeof(MemPage).
11d66 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 6e 64 ./* A Btree hand
11d67 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 le.**.** A datab
11d68 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
11d69 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 ontains a pointe
11d6a 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
11d6b 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 of.** this obje
11d6c 63 74 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 ct for every dat
11d6d 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20 abase file that
11d6e 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 it has open. Th
11d6f 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 is structure.**
11d70 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74 68 65 is opaque to the
11d71 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
11d72 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74 61 62 tion. The datab
11d73 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
11d74 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 annot.** see the
11d75 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 internals of th
11d76 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 is structure and
11d77 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68 only deals with
11d78 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 pointers to.**
11d79 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a this structure..
11d7a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 **.** For some d
11d7b 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c 20 74 atabase files, t
11d7c 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 he same underlyi
11d7d 6e 67 20 64 61 74 61 62 61 73 65 20 63 61 63 68 ng database cach
11d7e 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 e might be .** s
11d7f 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 6d 75 hared between mu
11d80 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f ltiple connectio
11d81 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 ns. In that cas
11d82 65 2c 20 65 61 63 68 20 63 6f 6e 74 65 63 74 69 e, each contecti
11d83 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e on.** has it own
11d84 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 pointer to this
11d85 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61 object. But ea
11d86 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ch instance of t
11d87 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f his object.** po
11d88 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 ints to the same
11d89 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
11d8a 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 . The database
11d8b 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a cache and the.**
11d8c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 schema associat
11d8d 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 ed with the data
11d8e 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c base file are al
11d8f 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 l contained with
11d90 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72 in.** the BtShar
11d91 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a ed object..**.**
11d92 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74 All fields in t
11d93 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 his structure ar
11d94 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 e accessed under
11d95 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a sqlite3.mutex..
11d96 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74 ** The pBt point
11d97 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f er itself may no
11d98 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 t be changed whi
11d99 6c 65 20 74 68 65 72 65 20 65 78 69 73 74 73 20 le there exists
11d9a 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74 cursors .** in t
11d9b 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42 74 he referenced Bt
11d9c 53 68 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e Shared that poin
11d9d 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42 t back to this B
11d9e 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65 tree since those
11d9f 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65 .** cursors have
11da0 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75 67 to do go throug
11da1 68 20 74 68 69 73 20 42 74 72 65 65 20 74 6f 20 h this Btree to
11da2 66 69 6e 64 20 74 68 65 69 72 20 42 74 53 68 61 find their BtSha
11da3 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 20 red and.** they
11da4 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 68 often do so with
11da5 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c 69 out holding sqli
11da6 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 te3.mutex..*/.st
11da7 72 75 63 74 20 42 74 72 65 65 20 7b 0a 20 20 73 ruct Btree {. s
11da8 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
11da9 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
11daa 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c e connection hol
11dab 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 65 20 ding this btree
11dac 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 */. BtShared *p
11dad 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 61 Bt; /* Shara
11dae 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 ble content of t
11daf 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75 his btree */. u
11db0 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 8 inTrans;
11db1 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 2c /* TRANS_NONE,
11db2 20 54 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 54 TRANS_READ or T
11db3 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 20 RANS_WRITE */.
11db4 75 38 20 73 68 61 72 61 62 6c 65 3b 20 20 20 20 u8 sharable;
11db5 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 /* True if we
11db6 20 63 61 6e 20 73 68 61 72 65 20 70 42 74 20 77 can share pBt w
11db7 69 74 68 20 61 6e 6f 74 68 65 72 20 64 62 20 2a ith another db *
11db8 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20 /. u8 locked;
11db9 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
11dba 66 20 64 62 20 63 75 72 72 65 6e 74 6c 79 20 68 f db currently h
11dbb 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a 2f as pBt locked */
11dbc 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f 63 . int wantToLoc
11dbd 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 k; /* Number
11dbe 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 of nested calls
11dbf 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 to sqlite3BtreeE
11dc0 6e 74 65 72 28 29 20 2a 2f 0a 20 20 42 74 72 65 nter() */. Btre
11dc1 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f e *pNext; /
11dc2 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65 72 20 * List of other
11dc3 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20 sharable Btrees
11dc4 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 64 62 from the same db
11dc5 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 50 72 */. Btree *pPr
11dc6 65 76 3b 20 20 20 20 20 20 2f 2a 20 42 61 63 6b ev; /* Back
11dc7 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 pointer of the
11dc8 73 61 6d 65 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a same list */.};.
11dc9 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54 ./*.** Btree.inT
11dca 72 61 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e rans may take on
11dcb 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
11dcc 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a ng values..**.**
11dcd 20 49 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 If the shared-d
11dce 61 74 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 ata extension is
11dcf 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20 enabled, there
11dd0 6d 61 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 may be multiple
11dd1 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20 users.** of the
11dd2 42 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e Btree structure.
11dd3 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 At most one of
11dd4 74 68 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61 these may open a
11dd5 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
11dd6 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e on,.** but any n
11dd7 75 6d 62 65 72 20 6d 61 79 20 68 61 76 65 20 61 umber may have a
11dd8 63 74 69 76 65 20 72 65 61 64 20 74 72 61 6e 73 ctive read trans
11dd9 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 actions..*/.#def
11dda 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 ine TRANS_NONE
11ddb 30 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 0.#define TRANS_
11ddc 52 45 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20 READ 1.#define
11ddd 54 52 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f TRANS_WRITE 2../
11dde 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
11ddf 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 of this object
11de0 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e represents a sin
11de1 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c gle database fil
11de2 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 e..** .** A sing
11de3 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 le database file
11de4 20 63 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61 can be in use a
11de5 73 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 s the same time
11de6 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 by two.** or mor
11de7 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
11de8 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77 ctions. When tw
11de9 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 o or more connec
11dea 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61 tions are.** sha
11deb 72 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 ring the same da
11dec 74 61 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63 tabase file, eac
11ded 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 h connection has
11dee 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 it own.** priva
11def 74 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 te Btree object
11df0 66 6f 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64 for the file and
11df1 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42 each of those B
11df2 74 72 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 trees points.**
11df3 74 6f 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68 to this one BtSh
11df4 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74 ared object. Bt
11df5 53 68 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74 Shared.nRef is t
11df6 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
11df7 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 connections curr
11df8 65 6e 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68 ently sharing th
11df9 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 is database file
11dfa 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 ..**.** Fields i
11dfb 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
11dfc 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e are accessed un
11dfd 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 der the BtShared
11dfe 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c .mutex.** mutex,
11dff 20 65 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66 except for nRef
11e00 20 61 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68 and pNext which
11e01 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e are accessed un
11e02 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 der the.** globa
11e03 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 l SQLITE_MUTEX_S
11e04 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 TATIC_MASTER mut
11e05 65 78 2e 20 20 54 68 65 20 70 50 61 67 65 72 20 ex. The pPager
11e06 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 field.** may not
11e07 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63 be modified onc
11e08 65 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c e it is initiall
11e09 79 20 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73 y set as long as
11e0a 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 nRef>0..** The
11e0b 70 53 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61 pSchema field ma
11e0c 79 20 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e y be set once un
11e0d 64 65 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74 der BtShared.mut
11e0e 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61 ex and.** therea
11e0f 66 74 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65 fter is unchange
11e10 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 d as long as nRe
11e11 66 3e 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 f>0..*/.struct B
11e12 74 53 68 61 72 65 64 20 7b 0a 20 20 50 61 67 65 tShared {. Page
11e13 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 r *pPager;
11e14 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 63 61 /* The page ca
11e15 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 che */. sqlite3
11e16 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f *db; /
11e17 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * Database conne
11e18 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 ction currently
11e19 75 73 69 6e 67 20 74 68 69 73 20 42 74 72 65 65 using this Btree
11e1a 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a */. BtCursor *
11e1b 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41 pCursor; /* A
11e1c 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65 list of all ope
11e1d 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d n cursors */. M
11e1e 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 20 emPage *pPage1;
11e1f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 /* First pa
11e20 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ge of the databa
11e21 73 65 20 2a 2f 0a 20 20 75 38 20 69 6e 53 74 6d se */. u8 inStm
11e22 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
11e23 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 True if we are
11e24 69 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 73 in a statement s
11e25 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f ubtransaction */
11e26 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 . u8 readOnly;
11e27 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
11e28 20 69 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 if the underlyi
11e29 6e 67 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f ng file is reado
11e2a 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 nly */. u8 page
11e2b 53 69 7a 65 46 69 78 65 64 3b 20 20 20 20 20 2f SizeFixed; /
11e2c 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 70 61 * True if the pa
11e2d 67 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c ge size can no l
11e2e 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 onger be changed
11e2f 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
11e30 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
11e31 55 4d 0a 20 20 75 38 20 61 75 74 6f 56 61 63 75 UM. u8 autoVacu
11e32 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 um; /* Tr
11e33 75 65 20 69 66 20 61 75 74 6f 2d 76 61 63 75 75 ue if auto-vacuu
11e34 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a m is enabled */.
11e35 20 20 75 38 20 69 6e 63 72 56 61 63 75 75 6d 3b u8 incrVacuum;
11e36 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
11e37 69 66 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 if incr-vacuum i
11e38 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 50 s enabled */. P
11e39 67 6e 6f 20 6e 54 72 75 6e 63 3b 20 20 20 20 20 gno nTrunc;
11e3a 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f /* Non-zero
11e3b 20 69 66 20 74 68 65 20 64 62 20 77 69 6c 6c 20 if the db will
11e3c 62 65 20 74 72 75 6e 63 61 74 65 64 20 28 69 6e be truncated (in
11e3d 63 72 20 76 61 63 75 75 6d 29 20 2a 2f 0a 23 65 cr vacuum) */.#e
11e3e 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67 65 53 ndif. u16 pageS
11e3f 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ize; /*
11e40 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
11e41 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 bytes on a page
11e42 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c 65 53 */. u16 usableS
11e43 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 ize; /* Nu
11e44 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62 mber of usable b
11e45 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 ytes on each pag
11e46 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 78 4c 6f e */. int maxLo
11e47 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 cal; /*
11e48 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 Maximum local pa
11e49 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 yload in non-LEA
11e4a 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a FDATA tables */.
11e4b 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 int minLocal;
11e4c 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
11e4d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 um local payload
11e4e 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 in non-LEAFDATA
11e4f 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 tables */. int
11e50 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20 20 20 maxLeaf;
11e51 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f /* Maximum lo
11e52 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 cal payload in a
11e53 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20 LEAFDATA table
11e54 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 4c 65 61 66 */. int minLeaf
11e55 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 ; /* Mi
11e56 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c nimum local payl
11e57 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54 oad in a LEAFDAT
11e58 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 A table */. u8
11e59 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 inTransaction;
11e5a 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f /* Transactio
11e5b 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 n state */. int
11e5c 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 nTransaction;
11e5d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
11e5e 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e open transaction
11e5f 73 20 28 72 65 61 64 20 2b 20 77 72 69 74 65 29 s (read + write)
11e60 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 */. void *pSch
11e61 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 ema; /* P
11e62 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 65 20 ointer to space
11e63 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c allocated by sql
11e64 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 ite3BtreeSchema(
11e65 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 ) */. void (*xF
11e66 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 64 2a reeSchema)(void*
11e67 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f ); /* Destructo
11e68 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 2e 70 r for BtShared.p
11e69 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 Schema */. sqli
11e6a 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
11e6b 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 ; /* Non-recursi
11e6c 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 72 65 ve mutex require
11e6d 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 d to access this
11e6e 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 75 73 struct */. Bus
11e6f 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48 64 72 yHandler busyHdr
11e70 3b 20 20 2f 2a 20 54 68 65 20 62 75 73 79 20 68 ; /* The busy h
11e71 61 6e 64 6c 65 72 20 66 6f 72 20 74 68 69 73 20 andler for this
11e72 62 74 72 65 65 20 2a 2f 0a 23 69 66 6e 64 65 66 btree */.#ifndef
11e73 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
11e74 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 RED_CACHE. int
11e75 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
11e76 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
11e77 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 eferences to thi
11e78 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 s structure */.
11e79 20 42 74 53 68 61 72 65 64 20 2a 70 4e 65 78 74 BtShared *pNext
11e7a 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f ; /* Next o
11e7b 6e 20 61 20 6c 69 73 74 20 6f 66 20 73 68 61 72 n a list of shar
11e7c 61 62 6c 65 20 42 74 53 68 61 72 65 64 20 73 74 able BtShared st
11e7d 72 75 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 ructs */. BtLoc
11e7e 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 k *pLock;
11e7f 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b /* List of lock
11e80 73 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 s held on this s
11e81 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 hared-btree stru
11e82 63 74 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 ct */. Btree *p
11e83 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a Exclusive; /*
11e84 20 42 74 72 65 65 20 77 69 74 68 20 61 6e 20 45 Btree with an E
11e85 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
11e86 20 74 68 65 20 77 68 6f 6c 65 20 64 62 20 2a 2f the whole db */
11e87 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 2a 70 54 .#endif. u8 *pT
11e88 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 mpSpace;
11e89 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 61 67 65 /* BtShared.page
11e8a 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 Size bytes of sp
11e8b 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 ace for tmp use
11e8c 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
11e8d 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
11e8e 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
11e8f 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 ure is used to h
11e90 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a old information.
11e91 2a 2a 20 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e ** about a cell.
11e92 20 20 54 68 65 20 70 61 72 73 65 43 65 6c 6c 50 The parseCellP
11e93 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 tr() function fi
11e94 6c 6c 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 lls in this stru
11e95 63 74 75 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f cture.** based o
11e96 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78 n information ex
11e97 74 72 61 63 74 20 66 72 6f 6d 20 74 68 65 20 72 tract from the r
11e98 61 77 20 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f aw disk page..*/
11e99 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
11e9a 43 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 CellInfo CellInf
11e9b 6f 3b 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e o;.struct CellIn
11e9c 66 6f 20 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c fo {. u8 *pCell
11e9d 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 ; /* Pointer
11e9e 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
11e9f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f cell content */
11ea0 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 . i64 nKey;
11ea1 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 66 6f 72 /* The key for
11ea2 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 INTKEY tables,
11ea3 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 or number of byt
11ea4 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 es in key */. u
11ea5 33 32 20 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a 32 nData; /*
11ea6 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
11ea7 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 75 33 of data */. u3
11ea8 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 2 nPayload; /*
11ea9 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 Total amount of
11eaa 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36 payload */. u16
11eab 20 6e 48 65 61 64 65 72 3b 20 20 20 2f 2a 20 53 nHeader; /* S
11eac 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 ize of the cell
11ead 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 69 content header i
11eae 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 31 36 n bytes */. u16
11eaf 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41 nLocal; /* A
11eb0 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 mount of payload
11eb1 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f held locally */
11eb2 0a 20 20 75 31 36 20 69 4f 76 65 72 66 6c 6f 77 . u16 iOverflow
11eb3 3b 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f ; /* Offset to o
11eb4 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d verflow page num
11eb5 62 65 72 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f ber. Zero if no
11eb6 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 overflow */. u
11eb7 31 36 20 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a 16 nSize; /*
11eb8 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c Size of the cel
11eb9 6c 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65 l content on the
11eba 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 main b-tree pag
11ebb 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d e */.};../*.** M
11ebc 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 aximum depth of
11ebd 61 6e 20 53 51 4c 69 74 65 20 42 2d 54 72 65 65 an SQLite B-Tree
11ebe 20 73 74 72 75 63 74 75 72 65 2e 20 41 6e 79 20 structure. Any
11ebf 42 2d 54 72 65 65 20 64 65 65 70 65 72 20 74 68 B-Tree deeper th
11ec0 61 6e 0a 2a 2a 20 74 68 69 73 20 77 69 6c 6c 20 an.** this will
11ec1 62 65 20 64 65 63 6c 61 72 65 64 20 63 6f 72 72 be declared corr
11ec2 75 70 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 upt. This value
11ec3 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 62 61 is calculated ba
11ec4 73 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 sed on a.** maxi
11ec5 6d 75 6d 20 64 61 74 61 62 61 73 65 20 73 69 7a mum database siz
11ec6 65 20 6f 66 20 32 5e 33 31 20 70 61 67 65 73 20 e of 2^31 pages
11ec7 61 20 6d 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 a minimum fanout
11ec8 20 6f 66 20 32 20 66 6f 72 20 61 0a 2a 2a 20 72 of 2 for a.** r
11ec9 6f 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66 oot-node and 3 f
11eca 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74 or all other int
11ecb 65 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a ernal nodes..**.
11ecc 2a 2a 20 49 66 20 61 20 74 72 65 65 20 74 68 61 ** If a tree tha
11ecd 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 t appears to be
11ece 74 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73 taller than this
11ecf 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c is encountered,
11ed0 20 69 74 20 69 73 0a 2a 2a 20 61 73 73 75 6d 65 it is.** assume
11ed1 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 d that the datab
11ed2 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a ase is corrupt..
11ed3 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 55 52 */.#define BTCUR
11ed4 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 32 30 SOR_MAX_DEPTH 20
11ed5 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 ../*.** A cursor
11ed6 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
11ed7 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 65 6e a particular en
11ed8 74 72 79 20 77 69 74 68 69 6e 20 61 20 70 61 72 try within a par
11ed9 74 69 63 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 ticular.** b-tre
11eda 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62 e within a datab
11edb 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
11edc 54 68 65 20 65 6e 74 72 79 20 69 73 20 69 64 65 The entry is ide
11edd 6e 74 69 66 69 65 64 20 62 79 20 69 74 73 20 4d ntified by its M
11ede 65 6d 50 61 67 65 20 61 6e 64 20 74 68 65 20 69 emPage and the i
11edf 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 ndex in.** MemPa
11ee0 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 ge.aCell[] of th
11ee1 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 e entry..**.** W
11ee2 68 65 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 74 hen a single dat
11ee3 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 73 abase file can s
11ee4 68 61 72 65 64 20 62 79 20 74 77 6f 20 6d 6f 72 hared by two mor
11ee5 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
11ee6 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 ctions,.** but c
11ee7 75 72 73 6f 72 73 20 63 61 6e 6e 6f 74 20 62 65 ursors cannot be
11ee8 20 73 68 61 72 65 64 2e 20 20 45 61 63 68 20 63 shared. Each c
11ee9 75 72 73 6f 72 20 69 73 20 61 73 73 6f 63 69 61 ursor is associa
11eea 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 70 61 ted with a.** pa
11eeb 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 rticular databas
11eec 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 64 65 e connection ide
11eed 6e 74 69 66 69 65 64 20 42 74 43 75 72 73 6f 72 ntified BtCursor
11eee 2e 70 42 74 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a .pBtree.db..**.*
11eef 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 * Fields in this
11ef0 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61 structure are a
11ef1 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 ccessed under th
11ef2 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 e BtShared.mutex
11ef3 0a 2a 2a 20 66 6f 75 6e 64 20 61 74 20 73 65 6c .** found at sel
11ef4 66 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0a f->pBt->mutex. .
11ef5 2a 2f 0a 73 74 72 75 63 74 20 42 74 43 75 72 73 */.struct BtCurs
11ef6 6f 72 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 or {. Btree *pB
11ef7 74 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 tree;
11ef8 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20 74 6f /* The Btree to
11ef9 20 77 68 69 63 68 20 74 68 69 73 20 63 75 72 73 which this curs
11efa 6f 72 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 or belongs */.
11efb 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
11efc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
11efd 20 42 74 53 68 61 72 65 64 20 74 68 69 73 20 63 BtShared this c
11efe 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 ursor points to
11eff 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 */. BtCursor *p
11f00 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f Next, *pPrev; /
11f01 2a 20 46 6f 72 6d 73 20 61 20 6c 69 6e 6b 65 64 * Forms a linked
11f02 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 75 72 list of all cur
11f03 73 6f 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 sors */. struct
11f04 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
11f05 66 6f 3b 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 fo; /* Argument
11f06 70 61 73 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 passed to compar
11f07 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ison function */
11f08 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 . Pgno pgnoRoot
11f09 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
11f0a 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 The root page of
11f0b 20 74 68 69 73 20 74 72 65 65 20 2a 2f 0a 20 20 this tree */.
11f0c 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 CellInfo info;
11f0d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 /* A p
11f0e 61 72 73 65 20 6f 66 20 74 68 65 20 63 65 6c 6c arse of the cell
11f0f 20 77 65 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 we are pointing
11f10 20 61 74 20 2a 2f 0a 20 20 75 38 20 77 72 46 6c at */. u8 wrFl
11f11 61 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ag;
11f12 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 72 /* True if wr
11f13 69 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 61 itable */. u8 a
11f14 74 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 tLast;
11f15 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
11f16 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
11f17 6c 61 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 last entry */.
11f18 75 38 20 76 61 6c 69 64 4e 4b 65 79 3b 20 20 20 u8 validNKey;
11f19 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
11f1a 65 20 69 66 20 69 6e 66 6f 2e 6e 4b 65 79 20 69 e if info.nKey i
11f1b 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 75 38 20 s valid */. u8
11f1c 65 53 74 61 74 65 3b 20 20 20 20 20 20 20 20 20 eState;
11f1d 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
11f1e 20 74 68 65 20 43 55 52 53 4f 52 5f 58 58 58 20 the CURSOR_XXX
11f1f 63 6f 6e 73 74 61 6e 74 73 20 28 73 65 65 20 62 constants (see b
11f20 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 6f 69 64 20 elow) */. void
11f21 2a 70 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 53 *pKey; /* S
11f22 61 76 65 64 20 6b 65 79 20 74 68 61 74 20 77 61 aved key that wa
11f23 73 20 63 75 72 73 6f 72 27 73 20 6c 61 73 74 20 s cursor's last
11f24 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 6f 6e 20 2a known position *
11f25 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 /. i64 nKey;
11f26 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
11f27 70 4b 65 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e pKey, or last in
11f28 74 65 67 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69 teger key */. i
11f29 6e 74 20 73 6b 69 70 3b 20 20 20 20 20 20 20 20 nt skip;
11f2a 2f 2a 20 28 73 6b 69 70 3c 30 29 20 2d 3e 20 50 /* (skip<0) -> P
11f2b 72 65 76 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 rev() is a no-op
11f2c 2e 20 28 73 6b 69 70 3e 30 29 20 2d 3e 20 4e 65 . (skip>0) -> Ne
11f2d 78 74 28 29 20 69 73 20 2a 2f 0a 23 69 66 6e 64 xt() is */.#ifnd
11f2e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
11f2f 4e 43 52 42 4c 4f 42 0a 20 20 75 38 20 69 73 49 NCRBLOB. u8 isI
11f30 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 ncrblobHandle;
11f31 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
11f32 68 69 73 20 63 75 72 73 6f 72 20 69 73 20 61 6e his cursor is an
11f33 20 69 6e 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65 incr. io handle
11f34 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 */. Pgno *aOve
11f35 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 rflow;
11f36 2f 2a 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 /* Cache of over
11f37 66 6c 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 flow page locati
11f38 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ons */.#endif.#i
11f39 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 75 fndef NDEBUG. u
11f3a 38 20 70 61 67 65 73 53 68 75 66 66 6c 65 64 3b 8 pagesShuffled;
11f3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
11f3c 20 69 66 20 42 74 72 65 65 20 70 61 67 65 73 20 if Btree pages
11f3d 61 72 65 20 72 65 61 72 72 61 6e 67 65 64 20 62 are rearranged b
11f3e 79 20 62 61 6c 61 6e 63 65 28 29 2a 2f 0a 23 65 y balance()*/.#e
11f3f 6e 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 ndif. i16 iPage
11f40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11f41 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11f42 49 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 Index of current
11f43 20 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 page in apPage
11f44 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 */. MemPage *ap
11f45 50 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 Page[BTCURSOR_MA
11f46 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 X_DEPTH]; /* Pa
11f47 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f ges from root to
11f48 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f current page */
11f49 0a 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 . u16 aiIdx[BTC
11f4a 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d URSOR_MAX_DEPTH]
11f4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 ; /* Curr
11f4c 65 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 ent index in apP
11f4d 61 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a age[i] */.};../*
11f4e 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 .** Potential va
11f4f 6c 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f lues for BtCurso
11f50 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 r.eState..**.**
11f51 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a CURSOR_VALID:.**
11f52 20 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 Cursor points
11f53 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 to a valid entr
11f54 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 y. getPayload()
11f55 65 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c etc. may be call
11f56 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 ed..**.** CURSOR
11f57 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 _INVALID:.** C
11f58 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 ursor does not p
11f59 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 oint to a valid
11f5a 65 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 entry. This can
11f5b 68 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d happen (for exam
11f5c 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 ple) .** becau
11f5d 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 se the table is
11f5e 65 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 empty or because
11f5f 20 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 BtreeCursorFirs
11f60 74 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e t() has not been
11f61 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a .** called..**
11f62 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 .** CURSOR_REQUI
11f63 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 RESEEK:.** The
11f64 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 table that this
11f65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e cursor was open
11f66 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 ed on still exis
11f67 74 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e ts, but has been
11f68 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 .** modified
11f69 73 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 since the cursor
11f6a 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 was last used.
11f6b 54 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 The cursor posit
11f6c 69 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 ion is saved.**
11f6d 20 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 in variables B
11f6e 74 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 tCursor.pKey and
11f6f 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 BtCursor.nKey.
11f70 57 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 When a cursor is
11f71 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 in .** this s
11f72 74 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 tate, restoreCur
11f73 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 sorPosition() ca
11f74 6e 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 n be called to a
11f75 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 ttempt to.** s
11f76 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 eek the cursor t
11f77 6f 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 o the saved posi
11f78 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 tion..**.** CURS
11f79 4f 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 OR_FAULT:.** A
11f7a 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 unrecoverable e
11f7b 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 rror (an I/O err
11f7c 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 or or a malloc f
11f7d 61 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 ailure) has occu
11f7e 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 rred.** on a d
11f7f 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 ifferent connect
11f80 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 ion that shares
11f81 74 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63 the BtShared cac
11f82 68 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 he with this.**
11f83 20 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 cursor. The e
11f84 72 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 rror has left th
11f85 65 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e e cache in an in
11f86 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 consistent state
11f87 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e ..** Do nothin
11f88 67 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 g else with this
11f89 20 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 cursor. Any at
11f8a 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 tempt to use the
11f8b 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f cursor.** sho
11f8c 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 uld return the e
11f8d 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 rror code stored
11f8e 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 in BtCursor.ski
11f8f 70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 p.*/.#define CUR
11f90 53 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 SOR_INVALID
11f91 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
11f92 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 CURSOR_VALID
11f93 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 1.#defi
11f94 6e 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 ne CURSOR_REQUIR
11f95 45 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 ESEEK 2.#d
11f96 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 efine CURSOR_FAU
11f97 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 33 LT 3
11f98 0a 0a 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 ../* The databas
11f99 65 20 70 61 67 65 20 74 68 65 20 50 45 4e 44 49 e page the PENDI
11f9a 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65 73 NG_BYTE occupies
11f9b 2e 20 54 68 69 73 20 70 61 67 65 20 69 73 20 6e . This page is n
11f9c 65 76 65 72 20 75 73 65 64 2e 0a 2a 2a 20 54 4f ever used..** TO
11f9d 44 4f 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 DO: This macro i
11f9e 73 20 76 65 72 79 20 73 69 6d 69 6c 61 72 79 20 s very similary
11f9f 74 6f 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f to PAGER_MJ_PGNO
11fa0 28 29 20 69 6e 20 70 61 67 65 72 2e 63 2e 20 54 () in pager.c. T
11fa1 68 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 70 6f hey.** should po
11fa2 73 73 69 62 6c 79 20 62 65 20 63 6f 6e 73 6f 6c ssibly be consol
11fa3 69 64 61 74 65 64 20 28 70 72 65 73 75 6d 61 62 idated (presumab
11fa4 6c 79 20 69 6e 20 70 61 67 65 72 2e 68 29 2e 0a ly in pager.h)..
11fa5 2a 2a 0a 2a 2a 20 49 66 20 64 69 73 6b 20 49 2f **.** If disk I/
11fa6 4f 20 69 73 20 6f 6d 69 74 74 65 64 20 28 6d 65 O is omitted (me
11fa7 61 6e 69 6e 67 20 74 68 61 74 20 74 68 65 20 64 aning that the d
11fa8 61 74 61 62 61 73 65 20 69 73 20 73 74 6f 72 65 atabase is store
11fa9 64 20 70 75 72 65 6c 79 0a 2a 2a 20 69 6e 20 6d d purely.** in m
11faa 65 6d 6f 72 79 29 20 74 68 65 6e 20 74 68 65 72 emory) then ther
11fab 65 20 69 73 20 6e 6f 20 70 65 6e 64 69 6e 67 20 e is no pending
11fac 62 79 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 byte..*/.#ifdef
11fad 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
11fae 49 4f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 IO.# define PEND
11faf 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
11fb0 74 29 20 20 30 78 37 66 66 66 66 66 66 66 0a 23 t) 0x7fffffff.#
11fb1 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 45 else.# define PE
11fb2 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
11fb3 70 42 74 29 20 28 28 50 45 4e 44 49 4e 47 5f 42 pBt) ((PENDING_B
11fb4 59 54 45 2f 28 70 42 74 29 2d 3e 70 61 67 65 53 YTE/(pBt)->pageS
11fb5 69 7a 65 29 2b 31 29 0a 23 65 6e 64 69 66 0a 0a ize)+1).#endif..
11fb6 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c /*.** A linked l
11fb7 69 73 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f ist of the follo
11fb8 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 73 20 wing structures
11fb9 69 73 20 73 74 6f 72 65 64 20 61 74 20 42 74 53 is stored at BtS
11fba 68 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 hared.pLock..**
11fbb 4c 6f 63 6b 73 20 61 72 65 20 61 64 64 65 64 20 Locks are added
11fbc 28 6f 72 20 75 70 67 72 61 64 65 64 20 66 72 6f (or upgraded fro
11fbd 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 m READ_LOCK to W
11fbe 52 49 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 RITE_LOCK) when
11fbf 61 20 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 a cursor .** is
11fc0 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 opened on the ta
11fc1 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 ble with root pa
11fc2 67 65 20 42 74 53 68 61 72 65 64 2e 69 54 61 62 ge BtShared.iTab
11fc3 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 le. Locks are re
11fc4 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 moved.** from th
11fc5 69 73 20 6c 69 73 74 20 77 68 65 6e 20 61 20 74 is list when a t
11fc6 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
11fc7 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 mmitted or rolle
11fc8 64 20 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a d back, or when.
11fc9 2a 2a 20 61 20 62 74 72 65 65 20 68 61 6e 64 6c ** a btree handl
11fca 65 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a e is closed..*/.
11fcb 73 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a struct BtLock {.
11fcc 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b Btree *pBtree;
11fcd 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 /* Btree
11fce 20 68 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 handle holding
11fcf 74 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 this lock */. P
11fd0 67 6e 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 gno iTable;
11fd1 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
11fd2 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 e of table */.
11fd3 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 u8 eLock;
11fd4 20 20 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f /* READ_LO
11fd5 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b CK or WRITE_LOCK
11fd6 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e */. BtLock *pN
11fd7 65 78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ext; /* N
11fd8 65 78 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e ext in BtShared.
11fd9 70 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b pLock list */.};
11fda 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 76 ../* Candidate v
11fdb 61 6c 75 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b alues for BtLock
11fdc 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e .eLock */.#defin
11fdd 65 20 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 e READ_LOCK
11fde 31 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 1.#define WRITE_
11fdf 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a LOCK 2../*.**
11fe0 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 These macros de
11fe1 66 69 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f fine the locatio
11fe2 6e 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 n of the pointer
11fe3 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61 -map entry for a
11fe4 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 .** database pa
11fe5 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 ge. The first ar
11fe6 67 75 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69 gument to each i
11fe7 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
11fe8 75 73 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20 usable.** bytes
11fe9 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 on each page of
11fea 74 68 65 20 64 61 74 61 62 61 73 65 20 28 6f 66 the database (of
11feb 74 65 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73 ten 1024). The s
11fec 65 63 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20 econd is the.**
11fed 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c page number to l
11fee 6f 6f 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f ook up in the po
11fef 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a inter map..**.**
11ff0 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72 PTRMAP_PAGENO r
11ff1 65 74 75 72 6e 73 20 74 68 65 20 64 61 74 61 62 eturns the datab
11ff2 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ase page number
11ff3 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d of the pointer-m
11ff4 61 70 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20 ap.** page that
11ff5 73 74 6f 72 65 73 20 74 68 65 20 72 65 71 75 69 stores the requi
11ff6 72 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52 red pointer. PTR
11ff7 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65 MAP_PTROFFSET re
11ff8 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66 turns.** the off
11ff9 73 65 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 set of the reque
11ffa 73 74 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a sted map entry..
11ffb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e **.** If the pgn
11ffc 6f 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 o argument passe
11ffd 64 20 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45 d to PTRMAP_PAGE
11ffe 4e 4f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d NO is a pointer-
11fff 6d 61 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 map page,.** the
12000 6e 20 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e n pgno is return
12001 65 64 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 ed. So (pgno==PT
12002 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a RMAP_PAGENO(pgsz
12003 2c 20 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a , pgno)) can be.
12004 2a 2a 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 ** used to test
12005 69 66 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69 if pgno is a poi
12006 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50 nter-map page. P
12007 54 52 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70 TRMAP_ISPAGE imp
12008 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20 lements.** this
12009 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 test..*/.#define
1200a 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 PTRMAP_PAGENO(p
1200b 42 74 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70 Bt, pgno) ptrmap
1200c 50 61 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f Pageno(pBt, pgno
1200d 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 ).#define PTRMAP
1200e 5f 50 54 52 4f 46 46 53 45 54 28 70 67 70 74 72 _PTROFFSET(pgptr
1200f 6d 61 70 2c 20 70 67 6e 6f 29 20 28 35 2a 28 70 map, pgno) (5*(p
12010 67 6e 6f 2d 70 67 70 74 72 6d 61 70 2d 31 29 29 gno-pgptrmap-1))
12011 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f .#define PTRMAP_
12012 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f ISPAGE(pBt, pgno
12013 29 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f ) (PTRMAP_PAGENO
12014 28 28 70 42 74 29 2c 28 70 67 6e 6f 29 29 3d 3d ((pBt),(pgno))==
12015 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 (pgno))../*.** T
12016 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 he pointer map i
12017 73 20 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 s a lookup table
12018 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 that identifies
12019 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
1201a 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 for.** each chi
1201b 6c 64 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ld page in the d
1201c 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
1201d 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 he parent page i
1201e 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a s the page that.
1201f 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f ** contains a po
12020 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 68 69 inter to the chi
12021 6c 64 2e 20 20 45 76 65 72 79 20 70 61 67 65 20 ld. Every page
12022 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
12023 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 6f 72 contains.** 0 or
12024 20 31 20 70 61 72 65 6e 74 20 70 61 67 65 73 2e 1 parent pages.
12025 20 20 28 49 6e 20 74 68 69 73 20 63 6f 6e 74 65 (In this conte
12026 78 74 20 27 64 61 74 61 62 61 73 65 20 70 61 67 xt 'database pag
12027 65 27 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 20 e' refers.** to
12028 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 69 73 any page that is
12029 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 not part of the
1202a 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 74 73 pointer map its
1202b 65 6c 66 2e 29 20 20 45 61 63 68 20 70 6f 69 6e elf.) Each poin
1202c 74 65 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 72 79 ter map.** entry
1202d 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 consists of a s
1202e 69 6e 67 6c 65 20 62 79 74 65 20 27 74 79 70 65 ingle byte 'type
1202f 27 20 61 6e 64 20 61 20 34 20 62 79 74 65 20 70 ' and a 4 byte p
12030 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 arent page numbe
12031 72 2e 0a 2a 2a 20 54 68 65 20 50 54 52 4d 41 50 r..** The PTRMAP
12032 5f 58 58 58 20 69 64 65 6e 74 69 66 69 65 72 73 _XXX identifiers
12033 20 62 65 6c 6f 77 20 61 72 65 20 74 68 65 20 76 below are the v
12034 61 6c 69 64 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a alid types..**.*
12035 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 * The purpose of
12036 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
12037 20 69 73 20 74 6f 20 66 61 63 69 6c 69 74 79 20 is to facility
12038 6d 6f 76 69 6e 67 20 70 61 67 65 73 20 66 72 6f moving pages fro
12039 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 69 6f m one.** positio
1203a 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 20 74 6f n in the file to
1203b 20 61 6e 6f 74 68 65 72 20 61 73 20 70 61 72 74 another as part
1203c 20 6f 66 20 61 75 74 6f 76 61 63 75 75 6d 2e 20 of autovacuum.
1203d 20 57 68 65 6e 20 61 20 70 61 67 65 0a 2a 2a 20 When a page.**
1203e 69 73 20 6d 6f 76 65 64 2c 20 74 68 65 20 70 6f is moved, the po
1203f 69 6e 74 65 72 20 69 6e 20 69 74 73 20 70 61 72 inter in its par
12040 65 6e 74 20 6d 75 73 74 20 62 65 20 75 70 64 61 ent must be upda
12041 74 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ted to point to
12042 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 61 74 the.** new locat
12043 69 6f 6e 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 ion. The pointe
12044 72 20 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f r map is used to
12045 20 6c 6f 63 61 74 65 20 74 68 65 20 70 61 72 65 locate the pare
12046 6e 74 20 70 61 67 65 20 71 75 69 63 6b 6c 79 2e nt page quickly.
12047 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f .**.** PTRMAP_RO
12048 4f 54 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 OTPAGE: The data
12049 62 61 73 65 20 70 61 67 65 20 69 73 20 61 20 72 base page is a r
1204a 6f 6f 74 2d 70 61 67 65 2e 20 54 68 65 20 70 61 oot-page. The pa
1204b 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 ge-number is not
1204c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1204d 20 20 20 20 20 75 73 65 64 20 69 6e 20 74 68 69 used in thi
1204e 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 s case..**.** PT
1204f 52 4d 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54 RMAP_FREEPAGE: T
12050 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
12051 20 69 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66 is an unused (f
12052 72 65 65 29 20 70 61 67 65 2e 20 54 68 65 20 70 ree) page. The p
12053 61 67 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a 20 20 age-number .**
12054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12055 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74 is not used in t
12056 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 his case..**.**
12057 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 PTRMAP_OVERFLOW1
12058 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
12059 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74 age is the first
1205a 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 page in a list
1205b 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of .**
1205c 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f overflo
1205d 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 w pages. The pag
1205e 65 20 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 e number identif
1205f 69 65 73 20 74 68 65 20 70 61 67 65 20 74 68 61 ies the page tha
12060 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
12061 20 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 contains
12062 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 61 20 the cell with a
12063 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 pointer to this
12064 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a overflow page..*
12065 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 *.** PTRMAP_OVER
12066 46 4c 4f 57 32 3a 20 54 68 65 20 64 61 74 61 62 FLOW2: The datab
12067 61 73 65 20 70 61 67 65 20 69 73 20 74 68 65 20 ase page is the
12068 73 65 63 6f 6e 64 20 6f 72 20 6c 61 74 65 72 20 second or later
12069 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f page in a list o
1206a 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 f.**
1206b 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 overflow
1206c 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 65 2d pages. The page-
1206d 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 number identifie
1206e 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a s the previous.*
1206f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
12070 20 20 20 20 70 61 67 65 20 69 6e 20 74 68 65 20 page in the
12071 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 overflow page li
12072 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 st..**.** PTRMAP
12073 5f 42 54 52 45 45 3a 20 54 68 65 20 64 61 74 61 _BTREE: The data
12074 62 61 73 65 20 70 61 67 65 20 69 73 20 61 20 6e base page is a n
12075 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 65 20 70 61 on-root btree pa
12076 67 65 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d ge. The page num
12077 62 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ber.**
12078 20 20 20 20 20 69 64 65 6e 74 69 66 69 65 73 20 identifies
12079 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
1207a 69 6e 20 74 68 65 20 62 74 72 65 65 2e 0a 2a 2f in the btree..*/
1207b 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f .#define PTRMAP_
1207c 52 4f 4f 54 50 41 47 45 20 31 0a 23 64 65 66 69 ROOTPAGE 1.#defi
1207d 6e 65 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 ne PTRMAP_FREEPA
1207e 47 45 20 32 0a 23 64 65 66 69 6e 65 20 50 54 52 GE 2.#define PTR
1207f 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0a MAP_OVERFLOW1 3.
12080 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f #define PTRMAP_O
12081 56 45 52 46 4c 4f 57 32 20 34 0a 23 64 65 66 69 VERFLOW2 4.#defi
12082 6e 65 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20 ne PTRMAP_BTREE
12083 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 68 20 6f 66 5../* A bunch of
12084 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
12085 65 6e 74 73 20 74 6f 20 63 68 65 63 6b 20 74 68 ents to check th
12086 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 e transaction st
12087 61 74 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a ate variables.**
12088 20 6f 66 20 68 61 6e 64 6c 65 20 70 20 28 74 79 of handle p (ty
12089 70 65 20 42 74 72 65 65 2a 29 20 61 72 65 20 69 pe Btree*) are i
1208a 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 nternally consis
1208b 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tent..*/.#define
1208c 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 btreeIntegrity(
1208d 70 29 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 p) \. assert( p
1208e 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ->pBt->inTransac
1208f 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 tion!=TRANS_NONE
12090 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61 || p->pBt->nTra
12091 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c nsaction==0 ); \
12092 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 . assert( p->pB
12093 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
12094 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 20 29 3b 20 >=p->inTrans );
12095 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 53 41 .../*.** The ISA
12096 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 20 UTOVACUUM macro
12097 69 73 20 75 73 65 64 20 77 69 74 68 69 6e 20 62 is used within b
12098 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
12099 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a to determine.**
1209a 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 if the database
1209b 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 supports auto-v
1209c 61 63 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 acuum or not. Be
1209d 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64 cause it is used
1209e 0a 2a 2a 20 77 69 74 68 69 6e 20 61 6e 20 65 78 .** within an ex
1209f 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 pression that is
120a0 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 an argument to
120a1 61 6e 6f 74 68 65 72 20 6d 61 63 72 6f 20 0a 2a another macro .*
120a2 2a 20 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 * (sqliteMallocR
120a3 61 77 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 aw), it is not p
120a4 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63 ossible to use c
120a5 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 onditional compi
120a6 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 lation..** So, t
120a7 68 69 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66 his macro is def
120a8 69 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f ined instead..*/
120a9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
120aa 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
120ab 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41 #define ISAUTOVA
120ac 43 55 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56 CUUM (pBt->autoV
120ad 61 63 75 75 6d 29 0a 23 65 6c 73 65 0a 23 64 65 acuum).#else.#de
120ae 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 fine ISAUTOVACUU
120af 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a M 0.#endif.../*.
120b0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
120b1 65 20 69 73 20 70 61 73 73 65 64 20 61 72 6f 75 e is passed arou
120b2 6e 64 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 nd through all t
120b3 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 he sanity checki
120b4 6e 67 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 ng routines.** i
120b5 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
120b6 74 72 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c track of some gl
120b7 6f 62 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72 obal state infor
120b8 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 mation..*/.typed
120b9 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 67 72 ef struct Integr
120ba 69 74 79 43 6b 20 49 6e 74 65 67 72 69 74 79 43 ityCk IntegrityC
120bb 6b 3b 0a 73 74 72 75 63 74 20 49 6e 74 65 67 72 k;.struct Integr
120bc 69 74 79 43 6b 20 7b 0a 20 20 42 74 53 68 61 72 ityCk {. BtShar
120bd 65 64 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54 ed *pBt; /* T
120be 68 65 20 74 72 65 65 20 62 65 69 6e 67 20 63 68 he tree being ch
120bf 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 50 ecked out */. P
120c0 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 ager *pPager;
120c1 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 /* The associat
120c2 65 64 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20 ed pager. Also
120c3 61 63 63 65 73 73 69 62 6c 65 20 62 79 20 70 42 accessible by pB
120c4 74 2d 3e 70 50 61 67 65 72 20 2a 2f 0a 20 20 69 t->pPager */. i
120c5 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 nt nPage;
120c6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
120c7 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
120c8 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e ase */. int *an
120c9 52 65 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 Ref; /* Nu
120ca 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 65 61 mber of times ea
120cb 63 68 20 70 61 67 65 20 69 73 20 72 65 66 65 72 ch page is refer
120cc 65 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d enced */. int m
120cd 78 45 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 xErr; /*
120ce 53 74 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e Stop accumulatin
120cf 67 20 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68 g errors when th
120d0 69 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 is reaches zero
120d1 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 */. int nErr;
120d2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
120d3 20 6f 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 of messages wri
120d4 74 74 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 tten to zErrMsg
120d5 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
120d6 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a mallocFailed; /*
120d7 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 A memory alloca
120d8 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f tion error has o
120d9 63 63 75 72 72 65 64 20 2a 2f 0a 20 20 53 74 72 ccurred */. Str
120da 41 63 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f Accum errMsg; /
120db 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 * Accumulate the
120dc 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
120dd 65 78 74 20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a ext here */.};..
120de 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 /*.** Read or wr
120df 69 74 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66 ite a two- and f
120e0 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 our-byte big-end
120e1 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 ian integer valu
120e2 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 es..*/.#define g
120e3 65 74 32 62 79 74 65 28 78 29 20 20 20 28 28 78 et2byte(x) ((x
120e4 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d )[0]<<8 | (x)[1]
120e5 29 0a 23 64 65 66 69 6e 65 20 70 75 74 32 62 79 ).#define put2by
120e6 74 65 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20 te(p,v) ((p)[0]
120e7 3d 20 28 76 29 3e 3e 38 2c 20 28 70 29 5b 31 5d = (v)>>8, (p)[1]
120e8 20 3d 20 28 76 29 29 0a 23 64 65 66 69 6e 65 20 = (v)).#define
120e9 67 65 74 34 62 79 74 65 20 73 71 6c 69 74 65 33 get4byte sqlite3
120ea 47 65 74 34 62 79 74 65 0a 23 64 65 66 69 6e 65 Get4byte.#define
120eb 20 70 75 74 34 62 79 74 65 20 73 71 6c 69 74 65 put4byte sqlite
120ec 33 50 75 74 34 62 79 74 65 0a 0a 2f 2a 0a 2a 2a 3Put4byte../*.**
120ed 20 49 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e Internal routin
120ee 65 73 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 es that should b
120ef 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 e accessed by th
120f0 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6f 6e e btree layer on
120f1 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
120f2 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
120f3 33 42 74 72 65 65 47 65 74 50 61 67 65 28 42 74 3BtreeGetPage(Bt
120f4 53 68 61 72 65 64 2a 2c 20 50 67 6e 6f 2c 20 4d Shared*, Pgno, M
120f5 65 6d 50 61 67 65 2a 2a 2c 20 69 6e 74 29 3b 0a emPage**, int);.
120f6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
120f7 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
120f8 6e 69 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 nitPage(MemPage
120f9 2a 70 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f *pPage);.SQLITE_
120fa 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
120fb 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
120fc 6c 6c 50 74 72 28 4d 65 6d 50 61 67 65 2a 2c 20 llPtr(MemPage*,
120fd 75 38 2a 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b u8*, CellInfo*);
120fe 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
120ff 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
12100 65 50 61 72 73 65 43 65 6c 6c 28 4d 65 6d 50 61 eParseCell(MemPa
12101 67 65 2a 2c 20 69 6e 74 2c 20 43 65 6c 6c 49 6e ge*, int, CellIn
12102 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 fo*);.SQLITE_PRI
12103 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
12104 42 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 BtreeRestoreCurs
12105 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 orPosition(BtCur
12106 73 6f 72 20 2a 70 43 75 72 29 3b 0a 53 51 4c 49 sor *pCur);.SQLI
12107 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12108 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 sqlite3BtreeGetT
12109 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 empCursor(BtCurs
1210a 6f 72 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 or *pCur, BtCurs
1210b 6f 72 20 2a 70 54 65 6d 70 43 75 72 29 3b 0a 53 or *pTempCur);.S
1210c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1210d 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 id sqlite3BtreeR
1210e 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 6f 72 eleaseTempCursor
1210f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
12110 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
12111 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
12112 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 eeMoveToParent(B
12113 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a tCursor *pCur);.
12114 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
12115 20 45 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 End of btreeInt
12116 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
12117 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12118 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
12119 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1211a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
1211b 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
1211c 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
1211d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1211e 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
1211f 41 44 53 41 46 45 20 26 26 20 21 64 65 66 69 6e ADSAFE && !defin
12120 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
12121 48 41 52 45 44 5f 43 41 43 48 45 29 0a 0a 0a 2f HARED_CACHE).../
12122 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 *.** Enter a mut
12123 65 78 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 ex on the given
12124 42 54 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a BTree object..**
12125 0a 2a 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 .** If the objec
12126 74 20 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c t is not sharabl
12127 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 e, then no mutex
12128 20 69 73 20 65 76 65 72 20 72 65 71 75 69 72 65 is ever require
12129 64 0a 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f d.** and this ro
1212a 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
1212b 2e 20 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e . The underlyin
1212c 67 20 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 g mutex is non-r
1212d 65 63 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 ecursive..** But
1212e 20 77 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 we keep a refer
1212f 65 6e 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 ence count in Bt
12130 72 65 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 ree.wantToLock s
12131 6f 20 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a o the behavior.*
12132 2a 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 * of this interf
12133 61 63 65 20 69 73 20 72 65 63 75 72 73 69 76 65 ace is recursive
12134 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 ..**.** To avoid
12135 20 64 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 deadlocks, mult
12136 69 70 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 iple Btrees are
12137 6c 6f 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 locked in the sa
12138 6d 65 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 me order.** by a
12139 6c 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ll database conn
1213a 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d ections. The p-
1213b 3e 70 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 >pNext is a list
1213c 20 6f 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 of other.** Btr
1213d 65 65 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f ees belonging to
1213e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
1213f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 se connection as
12140 20 74 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 the p Btree.**
12141 77 68 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 which need to be
12142 20 6c 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e locked after p.
12143 20 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 If we cannot g
12144 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 et a lock on.**
12145 70 2c 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e p, then first un
12146 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 lock all of the
12147 6f 74 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 others on p->pNe
12148 78 74 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a xt, then wait.**
12149 20 66 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f for the lock to
1214a 20 62 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c become availabl
1214b 65 20 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c e on p, then rel
1214c 6f 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a ock all of the.*
1214d 2a 20 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 * subsequent Btr
1214e 65 65 73 20 74 68 61 74 20 64 65 73 69 72 65 20 ees that desire
1214f 61 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 a lock..*/.SQLIT
12150 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
12151 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
12152 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 (Btree *p){. Bt
12153 72 65 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 ree *pLater;..
12154 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 /* Some basic sa
12155 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e nity checking on
12156 20 74 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 the Btree. The
12157 20 6c 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a list of Btrees.
12158 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 ** connected b
12159 79 20 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 y pNext and pPre
1215a 76 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 v should be in s
1215b 6f 72 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 orted order by.
1215c 20 2a 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 ** Btree.pBt va
1215d 6c 75 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 lue. All element
1215e 73 20 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 s of the list sh
1215f 6f 75 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 ould belong to.
12160 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e ** the same con
12161 6e 65 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 nection. Only sh
12162 61 72 65 64 20 42 74 72 65 65 73 20 61 72 65 20 ared Btrees are
12163 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a on the list. */.
12164 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 assert( p->pNe
12165 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 xt==0 || p->pNex
12166 74 2d 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b t->pBt>p->pBt );
12167 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 . assert( p->pP
12168 72 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 rev==0 || p->pPr
12169 65 76 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 ev->pBt<p->pBt )
1216a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 ;. assert( p->p
1216b 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e Next==0 || p->pN
1216c 65 78 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 ext->db==p->db )
1216d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 ;. assert( p->p
1216e 50 72 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 Prev==0 || p->pP
1216f 72 65 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 rev->db==p->db )
12170 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 ;. assert( p->s
12171 68 61 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 harable || (p->p
12172 4e 65 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 Next==0 && p->pP
12173 72 65 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a rev==0) );.. /*
12174 20 43 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 Check for locki
12175 6e 67 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a ng consistency *
12176 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e /. assert( !p->
12177 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e locked || p->wan
12178 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 tToLock>0 );. a
12179 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
1217a 6c 65 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c le || p->wantToL
1217b 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 ock==0 );.. /*
1217c 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 We should alread
1217d 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e y hold a lock on
1217e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
1217f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 nnection */. as
12180 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
12181 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
12182 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 28 mutex) );.. if(
12183 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 20 !p->sharable )
12184 72 65 74 75 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e return;. p->wan
12185 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28 tToLock++;. if(
12186 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 20 72 65 74 p->locked ) ret
12187 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6d 6f urn;.. /* In mo
12188 73 74 20 63 61 73 65 73 2c 20 77 65 20 73 68 6f st cases, we sho
12189 75 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 61 uld be able to a
1218a 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 cquire the lock
1218b 77 65 0a 20 20 2a 2a 20 77 61 6e 74 20 77 69 74 we. ** want wit
1218c 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 67 hout having to g
1218d 6f 20 74 68 72 6f 75 67 68 74 20 74 68 65 20 61 o throught the a
1218e 73 63 65 6e 64 69 6e 67 20 6c 6f 63 6b 0a 20 20 scending lock.
1218f 2a 2a 20 70 72 6f 63 65 64 75 72 65 20 74 68 61 ** procedure tha
12190 74 20 66 6f 6c 6c 6f 77 73 2e 20 20 4a 75 73 74 t follows. Just
12191 20 62 65 20 73 75 72 65 20 6e 6f 74 20 74 6f 20 be sure not to
12192 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 block.. */. if
12193 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
12194 74 72 79 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 try(p->pBt->mute
12195 78 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b x)==SQLITE_OK ){
12196 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d . p->locked =
12197 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 1;. return;.
12198 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 6f }.. /* To avo
12199 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 72 id deadlock, fir
1219a 73 74 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6c st release all l
1219b 6f 63 6b 73 20 77 69 74 68 20 61 20 6c 61 72 67 ocks with a larg
1219c 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 64 er. ** BtShared
1219d 20 61 64 64 72 65 73 73 2e 20 20 54 68 65 6e 20 address. Then
1219e 61 63 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 6b acquire our lock
1219f 2e 20 20 54 68 65 6e 20 72 65 61 63 71 75 69 72 . Then reacquir
121a0 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 e. ** the other
121a1 20 42 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 BtShared locks
121a2 74 68 61 74 20 77 65 20 75 73 65 64 20 74 6f 20 that we used to
121a3 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 6e hold in ascendin
121a4 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 g. ** order..
121a5 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d */. for(pLater=
121a6 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 p->pNext; pLater
121a7 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d ; pLater=pLater-
121a8 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 >pNext){. ass
121a9 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61 ert( pLater->sha
121aa 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 rable );. ass
121ab 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65 ert( pLater->pNe
121ac 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d xt==0 || pLater-
121ad 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74 >pNext->pBt>pLat
121ae 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 er->pBt );. a
121af 73 73 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e ssert( !pLater->
121b0 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72 locked || pLater
121b1 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 ->wantToLock>0 )
121b2 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 ;. if( pLater
121b3 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 ->locked ){.
121b4 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
121b5 6c 65 61 76 65 28 70 4c 61 74 65 72 2d 3e 70 42 leave(pLater->pB
121b6 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 t->mutex);.
121b7 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 pLater->locked
121b8 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 0;. }. }.
121b9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
121ba 6e 74 65 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 nter(p->pBt->mut
121bb 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 ex);. p->locked
121bc 20 3d 20 31 3b 0a 20 20 66 6f 72 28 70 4c 61 74 = 1;. for(pLat
121bd 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 er=p->pNext; pLa
121be 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 ter; pLater=pLat
121bf 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 er->pNext){.
121c0 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 if( pLater->want
121c1 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 ToLock ){.
121c2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
121c3 74 65 72 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d ter(pLater->pBt-
121c4 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 >mutex);. p
121c5 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20 Later->locked =
121c6 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 1;. }. }.}..
121c7 2f 2a 0a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 /*.** Exit the r
121c8 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 6f ecursive mutex o
121c9 6e 20 61 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 n a Btree..*/.SQ
121ca 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
121cb 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 d sqlite3BtreeLe
121cc 61 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ave(Btree *p){.
121cd 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 if( p->sharable
121ce 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
121cf 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 p->wantToLock>0
121d0 29 3b 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f );. p->wantTo
121d1 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 Lock--;. if(
121d2 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 p->wantToLock==0
121d3 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
121d4 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 ( p->locked );.
121d5 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
121d6 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d ex_leave(p->pBt-
121d7 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 >mutex);. p
121d8 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 ->locked = 0;.
121d9 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 }. }.}..#ifnd
121da 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ef NDEBUG./*.**
121db 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
121dc 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 he BtShared mute
121dd 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 x is held on the
121de 20 62 74 72 65 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 btree. .**.**
121df 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b This routine mak
121e0 65 73 20 6e 6f 20 64 65 74 65 72 6d 69 6e 61 74 es no determinat
121e1 69 6f 6e 20 6f 6e 65 20 77 68 79 20 6f 72 20 61 ion one why or a
121e2 6e 6f 74 68 65 72 20 69 66 20 74 68 65 0a 2a 2a nother if the.**
121e3 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
121e4 74 69 6f 6e 20 6d 75 74 65 78 20 69 73 20 68 65 tion mutex is he
121e5 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ld..**.** This r
121e6 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f outine is used o
121e7 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 nly from within
121e8 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
121e9 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts..*/.SQLITE_P
121ea 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
121eb 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
121ec 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 x(Btree *p){. r
121ed 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 62 eturn (p->sharab
121ee 6c 65 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20 le==0 ||.
121ef 20 20 20 20 20 20 28 70 2d 3e 6c 6f 63 6b 65 64 (p->locked
121f0 20 26 26 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 && p->wantToLoc
121f1 6b 20 26 26 20 73 71 6c 69 74 65 33 5f 6d 75 74 k && sqlite3_mut
121f2 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e ex_held(p->pBt->
121f3 6d 75 74 65 78 29 29 29 3b 0a 7d 0a 23 65 6e 64 mutex)));.}.#end
121f4 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c if...#ifndef SQL
121f5 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
121f6 42 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e B./*.** Enter an
121f7 64 20 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20 d leave a mutex
121f8 6f 6e 20 61 20 42 74 72 65 65 20 67 69 76 65 6e on a Btree given
121f9 20 61 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 a cursor owned
121fa 62 79 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 by that.** Btree
121fb 2e 20 20 54 68 65 73 65 20 65 6e 74 72 79 20 70 . These entry p
121fc 6f 69 6e 74 73 20 61 72 65 20 75 73 65 64 20 62 oints are used b
121fd 79 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f y incremental I/
121fe 4f 20 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 O and can be.**
121ff 6f 6d 69 74 74 65 64 20 69 66 20 74 68 61 74 20 omitted if that
12200 6d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 module is not us
12201 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
12202 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
12203 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 e3BtreeEnterCurs
12204 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
12205 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 r){. sqlite3Btr
12206 65 65 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 eeEnter(pCur->pB
12207 74 72 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f tree);.}.SQLITE_
12208 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
12209 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 ite3BtreeLeaveCu
1220a 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 rsor(BtCursor *p
1220b 43 75 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 Cur){. sqlite3B
1220c 74 72 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e treeLeave(pCur->
1220d 70 42 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 pBtree);.}.#endi
1220e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1220f 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f _INCRBLOB */.../
12210 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d *.** Enter the m
12211 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74 utex on every Bt
12212 72 65 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 ree associated w
12213 69 74 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a ith a database.*
12214 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 * connection. T
12215 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 28 66 his is needed (f
12216 6f 72 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f or example) prio
12217 72 20 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 r to parsing.**
12218 61 20 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 a statement sinc
12219 65 20 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d e we will be com
1221a 70 61 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 paring table and
1221b 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a column names.**
1221c 20 61 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 against all sch
1221d 65 6d 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e emas and we do n
1221e 6f 74 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63 ot want those sc
1221f 68 65 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 hemas being.** r
12220 65 73 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e eset out from un
12221 64 65 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 der us..**.** Th
12222 65 72 65 20 69 73 20 61 20 63 6f 72 72 65 73 70 ere is a corresp
12223 6f 6e 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c onding leave-all
12224 20 70 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a procedures..**.
12225 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 ** Enter the mut
12226 65 78 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e exes in accendin
12227 67 20 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 g order by BtSha
12228 72 65 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 red pointer addr
12229 65 73 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 ess.** to avoid
1222a 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 the possibility
1222b 6f 66 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e of deadlock when
1222c 20 74 77 6f 20 74 68 72 65 61 64 73 20 77 69 74 two threads wit
1222d 68 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 h.** two or more
1222e 20 62 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f btrees in commo
1222f 6e 20 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f n both try to lo
12230 63 6b 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72 ck all their btr
12231 65 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 ees.** at the sa
12232 6d 65 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 me instant..*/.S
12233 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
12234 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
12235 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 nterAll(sqlite3
12236 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *db){. int i;.
12237 20 42 74 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74 Btree *p, *pLat
12238 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 er;. assert( sq
12239 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1223a 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
1223b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
1223c 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 nDb; i++){. p
1223d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
1223e 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 t;. if( p &&
1223f 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 p->sharable ){.
12240 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f p->wantToLo
12241 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 ck++;. if(
12242 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 !p->locked ){.
12243 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
12244 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 >wantToLock==1 )
12245 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 ;. while(
12246 20 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 p->pPrev ) p =
12247 70 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 p->pPrev;.
12248 20 20 77 68 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b while( p->lock
12249 65 64 20 26 26 20 70 2d 3e 70 4e 65 78 74 20 29 ed && p->pNext )
1224a 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 p = p->pNext;.
1224b 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61 74 65 for(pLate
1224c 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 70 4c r = p->pNext; pL
1224d 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 ater; pLater=pLa
1224e 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
1224f 20 20 20 20 20 20 20 69 66 28 20 70 4c 61 74 65 if( pLate
12250 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 r->locked ){.
12251 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
12252 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4c 61 _mutex_leave(pLa
12253 74 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 ter->pBt->mutex)
12254 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4c ;. pL
12255 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 ater->locked = 0
12256 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
12257 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
12258 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 while( p ){.
12259 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
1225a 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 tex_enter(p->pBt
1225b 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
1225c 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 2b 2b 3b p->locked++;
1225d 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 . p = p
1225e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 ->pNext;.
1225f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
12260 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 . }.}.SQLITE_PR
12261 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
12262 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
12263 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
12264 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a int i;. Btree *
12265 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c p;. assert( sql
12266 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
12267 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
12268 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e for(i=0; i<db->n
12269 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 Db; i++){. p
1226a 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
1226b 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 ;. if( p && p
1226c 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
1226d 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 assert( p->w
1226e 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 antToLock>0 );.
1226f 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f p->wantToLo
12270 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 ck--;. if(
12271 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 p->wantToLock==0
12272 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
12273 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b rt( p->locked );
12274 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
12275 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
12276 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 pBt->mutex);.
12277 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d p->locked =
12278 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
12279 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 }. }.}..#ifndef
1227a 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 NDEBUG./*.** Re
1227b 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
1227c 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 current thread
1227d 68 6f 6c 64 73 20 74 68 65 20 64 61 74 61 62 61 holds the databa
1227e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a se connection.**
1227f 20 6d 75 74 65 78 20 61 6e 64 20 61 6c 6c 20 72 mutex and all r
12280 65 71 75 69 72 65 64 20 42 74 53 68 61 72 65 64 equired BtShared
12281 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 mutexes..**.**
12282 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
12283 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 used inside asse
12284 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
12285 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f only..*/.SQLITE_
12286 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
12287 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c te3BtreeHoldsAll
12288 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 20 Mutexes(sqlite3
12289 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *db){. int i;.
1228a 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 6d 75 if( !sqlite3_mu
1228b 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
1228c 65 78 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ex) ){. retur
1228d 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 n 0;. }. for(i
1228e 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1228f 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a ++){. Btree *
12290 70 3b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 p;. p = db->a
12291 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 Db[i].pBt;. i
12292 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 f( p && p->shara
12293 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 ble &&.
12294 28 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d (p->wantToLock==
12295 30 20 7c 7c 20 21 73 71 6c 69 74 65 33 5f 6d 75 0 || !sqlite3_mu
12296 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d tex_held(p->pBt-
12297 3e 6d 75 74 65 78 29 29 20 29 7b 0a 20 20 20 20 >mutex)) ){.
12298 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
12299 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 }. }. return 1
1229a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 ;.}.#endif /* ND
1229b 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 EBUG */../*.** A
1229c 64 64 20 61 20 6e 65 77 20 42 74 72 65 65 20 70 dd a new Btree p
1229d 6f 69 6e 74 65 72 20 74 6f 20 61 20 42 74 72 65 ointer to a Btre
1229e 65 4d 75 74 65 78 41 72 72 61 79 2e 20 0a 2a 2a eMutexArray. .**
1229f 20 69 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 if the pointer
122a0 63 61 6e 20 70 6f 73 73 69 62 6c 79 20 62 65 20 can possibly be
122a1 73 68 61 72 65 64 20 77 69 74 68 0a 2a 2a 20 61 shared with.** a
122a2 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 nother database
122a3 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a connection..**.*
122a4 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 20 61 * The pointers a
122a5 72 65 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 65 re kept in sorte
122a6 64 20 6f 72 64 65 72 20 62 79 20 70 42 74 72 65 d order by pBtre
122a7 65 2d 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a 2a e->pBt. That.**
122a8 20 77 61 79 20 77 68 65 6e 20 77 65 20 67 6f 20 way when we go
122a9 74 6f 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 65 to enter all the
122aa 20 6d 75 74 65 78 65 73 2c 20 77 65 20 63 61 6e mutexes, we can
122ab 20 65 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 69 enter them.** i
122ac 6e 20 6f 72 64 65 72 20 77 69 74 68 6f 75 74 20 n order without
122ad 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 every having to
122ae 62 61 63 6b 75 70 20 61 6e 64 20 72 65 74 72 79 backup and retry
122af 20 61 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20 and without.**
122b0 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 20 64 worrying about d
122b1 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 eadlock..**.** T
122b2 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 61 he number of sha
122b3 72 65 64 20 62 74 72 65 65 73 20 77 69 6c 6c 20 red btrees will
122b4 61 6c 77 61 79 73 20 62 65 20 73 6d 61 6c 6c 20 always be small
122b5 28 75 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 29 (usually 0 or 1)
122b6 0a 2a 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 74 .** so an insert
122b7 69 6f 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 61 ion sort is an a
122b8 64 65 71 75 61 74 65 20 61 6c 67 6f 72 69 74 68 dequate algorith
122b9 6d 20 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 m here..*/.SQLIT
122ba 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
122bb 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
122bc 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 72 65 ArrayInsert(Btre
122bd 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 72 eMutexArray *pAr
122be 72 61 79 2c 20 42 74 72 65 65 20 2a 70 42 74 72 ray, Btree *pBtr
122bf 65 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b ee){. int i, j;
122c0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
122c1 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 3d 3d ;. if( pBtree==
122c2 30 20 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 61 0 || pBtree->sha
122c3 72 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 rable==0 ) retur
122c4 6e 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 n;.#ifndef NDEBU
122c5 47 0a 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 3d G. {. for(i=
122c6 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 0; i<pArray->nMu
122c7 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 tex; i++){.
122c8 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d assert( pArray-
122c9 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 >aBtree[i]!=pBtr
122ca 65 65 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ee );. }. }.
122cb 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 #endif. assert(
122cc 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3e pArray->nMutex>
122cd 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
122ce 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c 73 pArray->nMutex<s
122cf 69 7a 65 6f 66 28 70 41 72 72 61 79 2d 3e 61 42 izeof(pArray->aB
122d0 74 72 65 65 29 2f 73 69 7a 65 6f 66 28 70 41 72 tree)/sizeof(pAr
122d1 72 61 79 2d 3e 61 42 74 72 65 65 5b 30 5d 29 2d ray->aBtree[0])-
122d2 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 74 1 );. pBt = pBt
122d3 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 28 ree->pBt;. for(
122d4 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e i=0; i<pArray->n
122d5 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 Mutex; i++){.
122d6 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d assert( pArray-
122d7 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 >aBtree[i]!=pBtr
122d8 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 41 ee );. if( pA
122d9 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 2d rray->aBtree[i]-
122da 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 20 >pBt>pBt ){.
122db 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d 3e for(j=pArray->
122dc 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d 2d nMutex; j>i; j--
122dd 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 61 ){. pArra
122de 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 70 y->aBtree[j] = p
122df 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 2d Array->aBtree[j-
122e0 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1];. }.
122e1 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 pArray->aBtree
122e2 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 20 [i] = pBtree;.
122e3 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 pArray->nMut
122e4 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 ex++;. retu
122e5 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rn;. }. }.
122e6 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 70 pArray->aBtree[p
122e7 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 5d Array->nMutex++]
122e8 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f 2a = pBtree;.}../*
122e9 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 .** Enter the mu
122ea 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72 tex of every btr
122eb 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e ee in the array.
122ec 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 This routine i
122ed 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 74 s.** called at t
122ee 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
122ef 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
122f0 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 ). The mutexes
122f1 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 74 are.** exited at
122f2 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
122f3 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a same function..*
122f4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
122f5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
122f6 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 eeMutexArrayEnte
122f7 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 r(BtreeMutexArra
122f8 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e y *pArray){. in
122f9 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
122fa 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 i<pArray->nMutex
122fb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 ; i++){. Btre
122fc 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 e *p = pArray->a
122fd 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a Btree[i];. /*
122fe 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 Some basic sani
122ff 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 ty checking */.
12300 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 assert( i==0
12301 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 || pArray->aBtre
12302 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 e[i-1]->pBt<p->p
12303 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Bt );. assert
12304 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 ( !p->locked ||
12305 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 p->wantToLock>0
12306 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 );.. /* We sh
12307 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c ould already hol
12308 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 d a lock on the
12309 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1230a 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 ion */. asser
1230b 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1230c 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
1230d 65 78 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 ex) );.. p->w
1230e 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 antToLock++;.
1230f 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 if( !p->locked
12310 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 && p->sharable )
12311 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
12312 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 mutex_enter(p->p
12313 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 Bt->mutex);.
12314 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b p->locked = 1;
12315 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
12316 0a 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d 75 .** Leave the mu
12317 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72 tex of every btr
12318 65 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 2e ee in the group.
12319 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1231a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
1231b 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 treeMutexArrayLe
1231c 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 ave(BtreeMutexAr
1231d 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 ray *pArray){.
1231e 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
1231f 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 ; i<pArray->nMut
12320 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 ex; i++){. Bt
12321 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d ree *p = pArray-
12322 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 >aBtree[i];.
12323 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 /* Some basic sa
12324 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f nity checking */
12325 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d . assert( i==
12326 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 0 || pArray->aBt
12327 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d ree[i-1]->pBt<p-
12328 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 >pBt );. asse
12329 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c rt( p->locked ||
1232a 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b !p->sharable );
1232b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
1232c 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a wantToLock>0 );.
1232d 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c . /* We shoul
1232e 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 d already hold a
1232f 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
12330 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
12331 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
12332 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
12333 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
12334 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 );.. p->want
12335 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 ToLock--;. if
12336 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
12337 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20 =0 && p->locked
12338 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
12339 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
1233a 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 pBt->mutex);.
1233b 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 p->locked = 0
1233c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a ;. }. }.}...
1233d 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 #endif /* SQLIT
1233e 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
1233f 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 !SQLITE_OMIT_SHA
12340 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a RED_CACHE */../*
12341 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
12342 64 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a d of btmutex.c *
12343 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12344 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12345 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
12346 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
12347 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 63 gin file btree.c
12348 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
12349 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1234a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1234b 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36 .** 2004 April 6
1234c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1234d 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1234e 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1234f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
12350 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
12351 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
12352 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
12353 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
12354 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
12355 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
12356 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
12357 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
12358 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
12359 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1235a 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1235b 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1235c 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1235d 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1235e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1235f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
12362 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 2e 63 2c ** $Id: btree.c,
12363 76 20 31 2e 35 32 37 20 32 30 30 38 2f 31 31 2f v 1.527 2008/11/
12364 30 33 20 32 30 3a 35 35 3a 30 37 20 64 72 68 20 03 20:55:07 drh
12365 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 Exp $.**.** This
12366 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 file implements
12367 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73 a external (dis
12368 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 73 k-based) databas
12369 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a e using BTrees..
1236a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 65 ** See the heade
1236b 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 74 r comment on "bt
1236c 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 64 reeInt.h" for ad
1236d 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
1236e 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 69 tion..** Includi
1236f 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e ng a description
12370 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 of file format
12371 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 20 and an overview
12372 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f of operation..*/
12373 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 ../*.** The head
12374 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 er string that a
12375 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 ppears at the be
12376 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 ginning of every
12377 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 .** SQLite datab
12378 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 ase..*/.static c
12379 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 onst char zMagic
1237a 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 Header[] = SQLIT
1237b 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a E_FILE_HEADER;..
1237c 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 /*.** Set this g
1237d 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 lobal variable t
1237e 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 o 1 to enable tr
1237f 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 acing using the
12380 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a TRACE.** macro..
12381 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c */.#if 0.int sql
12382 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d 30 ite3BtreeTrace=0
12383 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e ; /* True to en
12384 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a able tracing */.
12385 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 # define TRACE(X
12386 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 ) if(sqlite3Btr
12387 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 eeTrace){printf
12388 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 X;fflush(stdout)
12389 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e ;}.#else.# defin
1238a 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 e TRACE(X).#endi
1238b 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d f../*.** Sometim
1238c 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d 61 es we need a sma
1238d 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f 64 ll amount of cod
1238e 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72 69 e such as a vari
1238f 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 able initializat
12390 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70 20 ion.** to setup
12391 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73 65 for a later asse
12392 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e 20 rt() statement.
12393 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 We do not want
12394 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 this code to.**
12395 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 73 65 appear when asse
12396 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 64 rt() is disabled
12397 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
12398 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65 66 macro is theref
12399 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 ore.** used to c
1239a 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74 75 ontain that setu
1239b 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56 56 p code. The "VV
1239c 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e 64 A" acronym stand
1239d 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66 69 s for.** "Verifi
1239e 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74 69 cation, Validati
1239f 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69 74 on, and Accredit
123a0 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68 65 ation". In othe
123a1 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a 20 r words, the.**
123a2 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41 5f code within VVA_
123a3 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c 79 ONLY() will only
123a4 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72 69 run during veri
123a5 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 fication process
123a6 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e es..*/.#ifndef N
123a7 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 56 DEBUG.# define V
123a8 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23 65 VA_ONLY(X) X.#e
123a9 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56 41 lse.# define VVA
123aa 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 0a _ONLY(X).#endif.
123ab 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
123ac 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
123ad 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 CHE./*.** A list
123ae 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 6a of BtShared obj
123af 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 6c ects that are el
123b0 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 69 igible for parti
123b1 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 cipation.** in s
123b2 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 68 hared cache. Th
123b3 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 is variable has
123b4 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 6e file scope durin
123b5 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2c g normal builds,
123b6 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 74 .** but the test
123b7 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 74 harness needs t
123b8 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 77 o access it so w
123b9 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 6c e make it global
123ba 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 75 for .** test bu
123bb 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ilds..*/.#ifdef
123bc 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
123bd 54 45 5f 50 52 49 56 41 54 45 20 42 74 53 68 61 TE_PRIVATE BtSha
123be 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 red *SQLITE_WSD
123bf 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 sqlite3SharedCac
123c0 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6c 73 heList = 0;.#els
123c1 65 0a 73 74 61 74 69 63 20 42 74 53 68 61 72 65 e.static BtShare
123c2 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 d *SQLITE_WSD sq
123c3 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 lite3SharedCache
123c4 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 List = 0;.#endif
123c5 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
123c6 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
123c7 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 CHE */..#ifndef
123c8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
123c9 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45 ED_CACHE./*.** E
123ca 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 nable or disable
123cb 20 74 68 65 20 73 68 61 72 65 64 20 70 61 67 65 the shared page
123cc 72 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65 61 r and schema fea
123cd 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 tures..**.** Thi
123ce 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e 6f s routine has no
123cf 20 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73 74 effect on exist
123d0 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e ing database con
123d1 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 nections..** The
123d2 20 73 68 61 72 65 64 20 63 61 63 68 65 20 73 65 shared cache se
123d3 74 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f 6e tting effects on
123d4 6c 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73 20 ly future calls
123d5 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 to.** sqlite3_op
123d6 65 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f 70 en(), sqlite3_op
123d7 65 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69 74 en16(), or sqlit
123d8 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2f e3_open_v2()..*/
123d9 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
123da 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 sqlite3_enable_s
123db 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 20 hared_cache(int
123dc 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69 74 enable){. sqlit
123dd 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
123de 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 haredCacheEnable
123df 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 65 d = enable;. re
123e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
123e1 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a }.#endif.../*.**
123e2 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 Forward declara
123e3 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 tion.*/.static i
123e4 6e 74 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b nt checkReadLock
123e5 73 28 42 74 72 65 65 2a 2c 20 50 67 6e 6f 2c 20 s(Btree*, Pgno,
123e6 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 29 3b BtCursor*, i64);
123e7 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ...#ifdef SQLITE
123e8 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
123e9 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 HE. /*. ** The
123ea 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 functions query
123eb 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 6c 6f 63 TableLock(), loc
123ec 6b 54 61 62 6c 65 28 29 20 61 6e 64 20 75 6e 6c kTable() and unl
123ed 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 29 0a 20 ockAllTables().
123ee 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 65 ** manipulate e
123ef 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 74 ntries in the Bt
123f0 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e Shared.pLock lin
123f1 6b 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 6f ked list used to
123f2 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 72 store. ** shar
123f3 65 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 6c ed-cache table l
123f4 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 74 evel locks. If t
123f5 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f he library is co
123f6 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a mpiled with the.
123f7 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 ** shared-cach
123f8 65 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c e feature disabl
123f9 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 ed, then there i
123fa 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 20 s only ever one
123fb 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 63 user. ** of eac
123fc 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 h BtShared struc
123fd 74 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 73 ture and so this
123fe 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 20 locking is not
123ff 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a 2a necessary. . **
12400 20 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6c So define the l
12401 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e 63 ock related func
12402 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e tions as no-ops.
12403 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 . */. #define
12404 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 61 queryTableLock(a
12405 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a ,b,c) SQLITE_OK.
12406 20 20 23 64 65 66 69 6e 65 20 6c 6f 63 6b 54 61 #define lockTa
12407 62 6c 65 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 ble(a,b,c) SQLIT
12408 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 75 E_OK. #define u
12409 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 61 nlockAllTables(a
1240a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 ).#endif..#ifnde
1240b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
1240c 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
1240d 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 66 Query to see if
1240e 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20 btree handle p
1240f 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 may obtain a loc
12410 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 k of type eLock
12411 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f .** (READ_LOCK o
12412 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e r WRITE_LOCK) on
12413 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 the table with
12414 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20 root-page iTab.
12415 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 Return.** SQLITE
12416 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 _OK if the lock
12417 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 may be obtained
12418 28 62 79 20 63 61 6c 6c 69 6e 67 20 6c 6f 63 6b (by calling lock
12419 54 61 62 6c 65 28 29 29 2c 20 6f 72 0a 2a 2a 20 Table()), or.**
1241a 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 SQLITE_LOCKED if
1241b 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 not..*/.static
1241c 69 6e 74 20 71 75 65 72 79 54 61 62 6c 65 4c 6f int queryTableLo
1241d 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e ck(Btree *p, Pgn
1241e 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b o iTab, u8 eLock
1241f 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
12420 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
12421 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 tLock *pIter;..
12422 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
12423 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
12424 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
12425 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
12426 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 || eLock==WRITE
12427 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
12428 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 t( p->db!=0 );.
12429 20 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 . /* This is a
1242a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 no-op if the sh
1242b 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f ared-cache is no
1242c 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 t enabled */. i
1242d 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 f( !p->sharable
1242e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
1242f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
12430 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 /* If some other
12431 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 connection is h
12432 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 olding an exclus
12433 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 ive lock, the.
12434 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 ** requested loc
12435 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 k may not be obt
12436 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 ained.. */. if
12437 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 ( pBt->pExclusiv
12438 65 20 26 26 20 70 42 74 2d 3e 70 45 78 63 6c 75 e && pBt->pExclu
12439 73 69 76 65 21 3d 70 20 29 7b 0a 20 20 20 20 72 sive!=p ){. r
1243a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 eturn SQLITE_LOC
1243b 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 KED;. }.. /* T
1243c 68 69 73 20 28 61 6c 6f 6e 67 20 77 69 74 68 20 his (along with
1243d 6c 6f 63 6b 54 61 62 6c 65 28 29 29 20 69 73 20 lockTable()) is
1243e 77 68 65 72 65 20 74 68 65 20 52 65 61 64 55 6e where the ReadUn
1243f 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 committed flag i
12440 73 0a 20 20 2a 2a 20 64 65 61 6c 74 20 77 69 74 s. ** dealt wit
12441 68 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 h. If the caller
12442 20 69 73 20 71 75 65 72 79 69 6e 67 20 66 6f 72 is querying for
12443 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 61 6e 64 a read-lock and
12444 20 74 68 65 20 66 6c 61 67 20 69 73 0a 20 20 2a the flag is. *
12445 2a 20 73 65 74 2c 20 69 74 20 69 73 20 75 6e 63 * set, it is unc
12446 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 67 72 61 onditionally gra
12447 6e 74 65 64 20 2d 20 65 76 65 6e 20 69 66 20 74 nted - even if t
12448 68 65 72 65 20 61 72 65 20 77 72 69 74 65 2d 6c here are write-l
12449 6f 63 6b 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 ocks. ** on the
1244a 20 74 61 62 6c 65 2e 20 49 66 20 61 20 77 72 69 table. If a wri
1244b 74 65 2d 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 te-lock is reque
1244c 73 74 65 64 2c 20 74 68 65 20 52 65 61 64 55 6e sted, the ReadUn
1244d 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 0a 20 committed flag.
1244e 20 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 ** is not consi
1244f 64 65 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a dered.. **. **
12450 20 49 6e 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 63 In function loc
12451 6b 54 61 62 6c 65 28 29 2c 20 69 66 20 61 20 72 kTable(), if a r
12452 65 61 64 2d 6c 6f 63 6b 20 69 73 20 64 65 6d 61 ead-lock is dema
12453 6e 64 65 64 20 61 6e 64 20 74 68 65 20 0a 20 20 nded and the .
12454 2a 2a 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 ** ReadUncommitt
12455 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 ed flag is set,
12456 6e 6f 20 65 6e 74 72 79 20 69 73 20 61 64 64 65 no entry is adde
12457 64 20 74 6f 20 74 68 65 20 6c 6f 63 6b 73 20 6c d to the locks l
12458 69 73 74 20 0a 20 20 2a 2a 20 28 42 74 53 68 61 ist . ** (BtSha
12459 72 65 64 2e 70 4c 6f 63 6b 29 2e 0a 20 20 2a 2a red.pLock).. **
1245a 0a 20 20 2a 2a 20 54 6f 20 73 75 6d 6d 61 72 69 . ** To summari
1245b 7a 65 3a 20 49 66 20 74 68 65 20 52 65 61 64 55 ze: If the ReadU
1245c 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 ncommitted flag
1245d 69 73 20 73 65 74 2c 20 74 68 65 6e 20 72 65 61 is set, then rea
1245e 64 20 63 75 72 73 6f 72 73 20 64 6f 0a 20 20 2a d cursors do. *
1245f 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 6f 72 20 * not create or
12460 72 65 73 70 65 63 74 20 74 61 62 6c 65 20 6c 6f respect table lo
12461 63 6b 73 2e 20 54 68 65 20 6c 6f 63 6b 69 6e 67 cks. The locking
12462 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20 61 procedure for a
12463 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d 63 75 72 . ** write-cur
12464 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 sor does not cha
12465 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 nge.. */. if(
12466 0a 20 20 20 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e . 0==(p->db->
12467 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 flags&SQLITE_Rea
12468 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c dUncommitted) ||
12469 20 0a 20 20 20 20 65 4c 6f 63 6b 3d 3d 57 52 49 . eLock==WRI
1246a 54 45 5f 4c 4f 43 4b 20 7c 7c 0a 20 20 20 20 69 TE_LOCK ||. i
1246b 54 61 62 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 Tab==MASTER_ROOT
1246c 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 . ){. for(pI
1246d 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 ter=pBt->pLock;
1246e 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 pIter; pIter=pIt
1246f 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 er->pNext){.
12470 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 if( pIter->pBt
12471 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d ree!=p && pIter-
12472 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 >iTable==iTab &&
12473 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 49 74 . (pIt
12474 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b er->eLock!=eLock
12475 20 7c 7c 20 65 4c 6f 63 6b 21 3d 52 45 41 44 5f || eLock!=READ_
12476 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 20 20 20 LOCK) ){.
12477 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c return SQLITE_L
12478 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20 OCKED;. }.
12479 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1247a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 n SQLITE_OK;.}.#
1247b 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 endif /* !SQLITE
1247c 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1247d 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 HE */..#ifndef S
1247e 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1247f 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 D_CACHE./*.** Ad
12480 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 d a lock on the
12481 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d table with root-
12482 70 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 page iTable to t
12483 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 he shared-btree
12484 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 used.** by Btree
12485 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d handle p. Param
12486 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 eter eLock must
12487 62 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c be either READ_L
12488 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 OCK or .** WRITE
12489 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c _LOCK..**.** SQL
1248a 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
1248b 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 ed if the lock i
1248c 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73 66 s added successf
1248d 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 42 55 53 ully. SQLITE_BUS
1248e 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f Y and.** SQLITE_
1248f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73 6f 20 62 NOMEM may also b
12490 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 e returned..*/.s
12491 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 61 tatic int lockTa
12492 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 ble(Btree *p, Pg
12493 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 4c no iTable, u8 eL
12494 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 ock){. BtShared
12495 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
12496 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 BtLock *pLock
12497 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 = 0;. BtLock *p
12498 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 Iter;.. assert(
12499 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
1249a 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 dsMutex(p) );.
1249b 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 assert( eLock==R
1249c 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 EAD_LOCK || eLoc
1249d 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b k==WRITE_LOCK );
1249e 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 . assert( p->db
1249f 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 !=0 );.. /* Thi
124a0 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 s is a no-op if
124a1 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 the shared-cache
124a2 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20 is not enabled
124a3 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 */. if( !p->sha
124a4 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 74 rable ){. ret
124a5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
124a6 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 53 51 }.. assert( SQ
124a7 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 54 61 LITE_OK==queryTa
124a8 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c bleLock(p, iTabl
124a9 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 e, eLock) );..
124aa 2f 2a 20 49 66 20 74 68 65 20 72 65 61 64 2d 75 /* If the read-u
124ab 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 ncommitted flag
124ac 69 73 20 73 65 74 20 61 6e 64 20 61 20 72 65 61 is set and a rea
124ad 64 2d 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 d-lock is reques
124ae 74 65 64 2c 0a 20 20 2a 2a 20 72 65 74 75 72 6e ted,. ** return
124af 20 65 61 72 6c 79 20 77 69 74 68 6f 75 74 20 61 early without a
124b0 64 64 69 6e 67 20 61 6e 20 65 6e 74 72 79 20 74 dding an entry t
124b1 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 o the BtShared.p
124b2 4c 6f 63 6b 20 6c 69 73 74 2e 20 53 65 65 0a 20 Lock list. See.
124b3 20 2a 2a 20 63 6f 6d 6d 65 6e 74 20 69 6e 20 66 ** comment in f
124b4 75 6e 63 74 69 6f 6e 20 71 75 65 72 79 54 61 62 unction queryTab
124b5 6c 65 4c 6f 63 6b 28 29 20 66 6f 72 20 6d 6f 72 leLock() for mor
124b6 65 20 69 6e 66 6f 20 6f 6e 20 68 61 6e 64 6c 69 e info on handli
124b7 6e 67 20 0a 20 20 2a 2a 20 74 68 65 20 52 65 61 ng . ** the Rea
124b8 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 dUncommitted fla
124b9 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20 g.. */. if( .
124ba 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 (p->db->flags
124bb 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f &SQLITE_ReadUnco
124bc 6d 6d 69 74 74 65 64 29 20 26 26 20 0a 20 20 20 mmitted) && .
124bd 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f (eLock==READ_LO
124be 43 4b 29 20 26 26 0a 20 20 20 20 69 54 61 62 6c CK) &&. iTabl
124bf 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a 20 e!=MASTER_ROOT.
124c0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
124c1 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
124c2 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 /* First search
124c3 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 6e the list for an
124c4 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 6f existing lock o
124c5 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f n this table. */
124c6 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 . for(pIter=pBt
124c7 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 ->pLock; pIter;
124c8 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 pIter=pIter->pNe
124c9 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 xt){. if( pIt
124ca 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 er->iTable==iTab
124cb 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 74 le && pIter->pBt
124cc 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree==p ){.
124cd 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a 20 pLock = pIter;.
124ce 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
124cf 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
124d0 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 20 he above search
124d1 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 42 did not find a B
124d2 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 73 tLock struct ass
124d3 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 70 ociating Btree p
124d4 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c 65 . ** with table
124d5 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 iTable, allocat
124d6 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 e one and link i
124d7 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e t into the list.
124d8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c 6f . */. if( !pLo
124d9 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 ck ){. pLock
124da 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 = (BtLock *)sqli
124db 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 te3MallocZero(si
124dc 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 zeof(BtLock));.
124dd 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b if( !pLock ){
124de 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
124df 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
124e0 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 }. pLock->iTa
124e1 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 ble = iTable;.
124e2 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 pLock->pBtree
124e3 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e = p;. pLock->
124e4 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f pNext = pBt->pLo
124e5 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c 6f ck;. pBt->pLo
124e6 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a ck = pLock;. }.
124e7 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 74 . /* Set the Bt
124e8 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 61 Lock.eLock varia
124e9 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 6d ble to the maxim
124ea 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e um of the curren
124eb 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 t lock. ** and
124ec 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f the requested lo
124ed 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 ck. This means i
124ee 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 f a write-lock w
124ef 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 0a as already held.
124f0 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 2d ** and a read-
124f1 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c 20 lock requested,
124f2 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 65 we don't incorre
124f3 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 74 ctly downgrade t
124f4 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 he lock.. */.
124f5 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f assert( WRITE_LO
124f6 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a CK>READ_LOCK );.
124f7 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 if( eLock>pLoc
124f8 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 k->eLock ){.
124f9 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 pLock->eLock = e
124fa 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 Lock;. }.. ret
124fb 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
124fc 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 .#endif /* !SQLI
124fd 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
124fe 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 ACHE */..#ifndef
124ff 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
12500 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 RED_CACHE./*.**
12501 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 Release all the
12502 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 table locks (loc
12503 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 20 ks obtained via
12504 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 6c 6f 63 calls to the loc
12505 6b 54 61 62 6c 65 28 29 0a 2a 2a 20 70 72 6f 63 kTable().** proc
12506 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 42 edure) held by B
12507 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a 2a tree handle p..*
12508 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e /.static void un
12509 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 42 74 lockAllTables(Bt
1250a 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 ree *p){. BtSha
1250b 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1250c 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 t;. BtLock **pp
1250d 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f Iter = &pBt->pLo
1250e 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ck;.. assert( s
1250f 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
12510 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 Mutex(p) );. as
12511 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
12512 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 e || 0==*ppIter
12513 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 70 70 );.. while( *pp
12514 49 74 65 72 20 29 7b 0a 20 20 20 20 42 74 4c 6f Iter ){. BtLo
12515 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 ck *pLock = *ppI
12516 74 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ter;. assert(
12517 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 65 pBt->pExclusive
12518 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 45 78 63 ==0 || pBt->pExc
12519 6c 75 73 69 76 65 3d 3d 70 4c 6f 63 6b 2d 3e 70 lusive==pLock->p
1251a 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 Btree );. if(
1251b 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d pLock->pBtree==
1251c 70 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 49 74 p ){. *ppIt
1251d 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 er = pLock->pNex
1251e 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 t;. sqlite3
1251f 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 _free(pLock);.
12520 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
12521 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e pIter = &pLock->
12522 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d pNext;. }. }
12523 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 45 78 .. if( pBt->pEx
12524 63 6c 75 73 69 76 65 3d 3d 70 20 29 7b 0a 20 20 clusive==p ){.
12525 20 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 pBt->pExclusiv
12526 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e e = 0;. }.}.#en
12527 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
12528 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 IT_SHARED_CACHE
12529 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */..static void
1252a 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 releasePage(MemP
1252b 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 2f 2a age *pPage); /*
1252c 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
1252d 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 ce */../*.** Ver
1252e 69 66 79 20 74 68 61 74 20 74 68 65 20 63 75 72 ify that the cur
1252f 73 6f 72 20 68 6f 6c 64 73 20 61 20 6d 75 74 65 sor holds a mute
12530 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 x on the BtShare
12531 64 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 d.*/.#ifndef NDE
12532 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20 63 BUG.static int c
12533 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
12534 42 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 BtCursor *p){.
12535 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d return sqlite3_m
12536 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 utex_held(p->pBt
12537 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 ->mutex);.}.#end
12538 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c if...#ifndef SQL
12539 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
1253a 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 B./*.** Invalida
1253b 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 te the overflow
1253c 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 page-list cache
1253d 66 6f 72 20 63 75 72 73 6f 72 20 70 43 75 72 2c for cursor pCur,
1253e 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 61 74 if any..*/.stat
1253f 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 ic void invalida
12540 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 teOverflowCache(
12541 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
12542 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
12543 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
12544 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ) );. sqlite3_f
12545 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 65 72 66 ree(pCur->aOverf
12546 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 61 4f low);. pCur->aO
12547 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 7d 0a 0a verflow = 0;.}..
12548 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 /*.** Invalidate
12549 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
1254a 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f ge-list cache fo
1254b 72 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 r all cursors op
1254c 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 ened.** on the s
1254d 68 61 72 65 64 20 62 74 72 65 65 20 73 74 72 75 hared btree stru
1254e 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 cture pBt..*/.st
1254f 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 atic void invali
12550 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 dateAllOverflowC
12551 61 63 68 65 28 42 74 53 68 61 72 65 64 20 2a 70 ache(BtShared *p
12552 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 Bt){. BtCursor
12553 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 *p;. assert( sq
12554 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
12555 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
12556 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 for(p=pBt->pCu
12557 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e rsor; p; p=p->pN
12558 65 78 74 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 ext){. invali
12559 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 dateOverflowCach
1255a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 e(p);. }.}.#els
1255b 65 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 e. #define inva
1255c 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 lidateOverflowCa
1255d 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 che(x). #define
1255e 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 invalidateAllOv
1255f 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 23 erflowCache(x).#
12560 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 endif../*.** Sav
12561 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 e the current cu
12562 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e rsor position in
12563 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 42 the variables B
12564 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a tCursor.nKey .**
12565 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b and BtCursor.pK
12566 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73 ey. The cursor's
12567 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f state is set to
12568 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
12569 45 45 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 EEK..*/.static i
1256a 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 nt saveCursorPos
1256b 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a ition(BtCursor *
1256c 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b pCur){. int rc;
1256d 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 53 .. assert( CURS
1256e 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e OR_VALID==pCur->
1256f 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73 65 eState );. asse
12570 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b 65 rt( 0==pCur->pKe
12571 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 y );. assert( c
12572 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
12573 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 20 3d pCur) );.. rc =
12574 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
12575 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 75 72 Size(pCur, &pCur
12576 2d 3e 6e 4b 65 79 29 3b 0a 0a 20 20 2f 2a 20 49 ->nKey);.. /* I
12577 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 74 f this is an int
12578 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 Key table, then
12579 74 68 65 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 the above call t
1257a 6f 20 42 74 72 65 65 4b 65 79 53 69 7a 65 28 29 o BtreeKeySize()
1257b 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 . ** stores the
1257c 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69 6e 20 integer key in
1257d 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 pCur->nKey. In t
1257e 68 69 73 20 63 61 73 65 20 74 68 69 73 20 76 61 his case this va
1257f 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 lue is. ** all
12580 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 that is required
12581 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 . Otherwise, if
12582 70 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e pCur is not open
12583 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 on an intKey.
12584 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d ** table, then m
12585 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f 72 20 alloc space for
12586 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 70 43 and store the pC
12587 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f ur->nKey bytes o
12588 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 f key . ** data
12589 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d .. */. if( rc=
1258a 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d =SQLITE_OK && 0=
1258b 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d =pCur->apPage[0]
1258c 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20 20 20 76 ->intKey){. v
1258d 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 oid *pKey = sqli
1258e 74 65 33 4d 61 6c 6c 6f 63 28 70 43 75 72 2d 3e te3Malloc(pCur->
1258f 6e 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 70 nKey);. if( p
12590 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Key ){. rc
12591 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 = sqlite3BtreeKe
12592 79 28 70 43 75 72 2c 20 30 2c 20 70 43 75 72 2d y(pCur, 0, pCur-
12593 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20 >nKey, pKey);.
12594 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12595 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
12596 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b pCur->pKey = pK
12597 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ey;. }else{
12598 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
12599 5f 66 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 _free(pKey);.
1259a 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1259b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1259c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
1259d 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 43 }. assert( !pC
1259e 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
1259f 6e 74 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d 3e ntKey || !pCur->
125a0 70 4b 65 79 20 29 3b 0a 0a 20 20 69 66 28 20 72 pKey );.. if( r
125a1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
125a2 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 int i;. f
125a3 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d or(i=0; i<=pCur-
125a4 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 >iPage; i++){.
125a5 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
125a6 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 pCur->apPage[i])
125a7 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 70 ;. pCur->ap
125a8 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 Page[i] = 0;.
125a9 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 }. pCur->iPa
125aa 67 65 20 3d 20 2d 31 3b 0a 20 20 20 20 70 43 75 ge = -1;. pCu
125ab 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
125ac 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a OR_REQUIRESEEK;.
125ad 20 20 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 }.. invalidat
125ae 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 eOverflowCache(p
125af 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 Cur);. return r
125b0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 c;.}../*.** Save
125b1 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f the positions o
125b2 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 f all cursors ex
125b3 63 65 70 74 20 70 45 78 63 65 70 74 20 6f 70 65 cept pExcept ope
125b4 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a n on the table .
125b5 2a 2a 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 ** with root-pag
125b6 65 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79 e iRoot. Usually
125b7 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 , this is called
125b8 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75 72 just before cur
125b9 73 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 69 sor.** pExcept i
125ba 73 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 s used to modify
125bb 20 74 68 65 20 74 61 62 6c 65 20 28 42 74 72 65 the table (Btre
125bc 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72 eDelete() or Btr
125bd 65 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a eeInsert())..*/.
125be 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41 static int saveA
125bf 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 llCursors(BtShar
125c0 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 ed *pBt, Pgno iR
125c1 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 oot, BtCursor *p
125c2 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72 Except){. BtCur
125c3 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 sor *p;. assert
125c4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
125c5 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
125c6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
125c7 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 xcept==0 || pExc
125c8 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b ept->pBt==pBt );
125c9 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 . for(p=pBt->pC
125ca 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
125cb 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
125cc 21 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d !=pExcept && (0=
125cd 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e =iRoot || p->pgn
125ce 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26 oRoot==iRoot) &&
125cf 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 . p->eSt
125d0 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
125d1 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 D ){. int r
125d2 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f c = saveCursorPo
125d3 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 sition(p);.
125d4 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
125d5 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 rc ){. re
125d6 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
125d7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
125d8 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
125d9 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
125da 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 e current cursor
125db 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 position..*/.SQ
125dc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
125dd 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c d sqlite3BtreeCl
125de 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 earCursor(BtCurs
125df 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
125e0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
125e1 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
125e2 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
125e3 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75 ur->pKey);. pCu
125e4 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70 r->pKey = 0;. p
125e5 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 Cur->eState = CU
125e6 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a RSOR_INVALID;.}.
125e7 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 ./*.** Restore t
125e8 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 he cursor to the
125e9 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 position it was
125ea 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 in (or as close
125eb 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 to as possible)
125ec 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 .** when saveCur
125ed 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 sorPosition() wa
125ee 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 s called. Note t
125ef 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 hat this call de
125f0 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 letes the .** sa
125f1 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 ved position inf
125f2 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 o stored by save
125f3 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
125f4 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 , so there can b
125f5 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 e.** at most one
125f6 20 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f effective resto
125f7 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
125f8 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 () call after ea
125f9 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f ch .** saveCurso
125fa 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a rPosition()..*/.
125fb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
125fc 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 nt sqlite3BtreeR
125fd 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
125fe 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 tion(BtCursor *p
125ff 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Cur){. int rc;.
12600 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
12601 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
12602 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
12603 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 ur->eState>=CURS
12604 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
12605 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
12606 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 tate==CURSOR_FAU
12607 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e LT ){. return
12608 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d pCur->skip;. }
12609 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 . pCur->eState
1260a 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
1260b 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1260c 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 BtreeMoveto(pCur
1260d 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 , pCur->pKey, pC
1260e 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 ur->nKey, 0, &pC
1260f 75 72 2d 3e 73 6b 69 70 29 3b 0a 20 20 69 66 28 ur->skip);. if(
12610 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
12611 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
12612 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a ee(pCur->pKey);.
12613 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d pCur->pKey =
12614 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 0;. assert(
12615 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
12616 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 RSOR_VALID || pC
12617 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
12618 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 OR_INVALID );.
12619 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1261a 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72 ..#define restor
1261b 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
1261c 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 p) \. (p->eStat
1261d 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 e>=CURSOR_REQUIR
1261e 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 ESEEK ? \.
1261f 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 sqlite3BtreeR
12620 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
12621 74 69 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 tion(p) : \.
12622 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a SQLITE_OK).
12623 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 ./*.** Determine
12624 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
12625 61 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 a cursor has mov
12626 65 64 20 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 ed from the posi
12627 74 69 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c tion it.** was l
12628 61 73 74 20 70 6c 61 63 65 64 20 61 74 2e 20 20 ast placed at.
12629 43 75 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 Cursors can move
1262a 20 77 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68 when the row th
1262b 65 79 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a ey are pointing.
1262c 2a 2a 20 61 74 20 69 73 20 64 65 6c 65 74 65 64 ** at is deleted
1262d 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 out from under
1262e 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 them..**.** This
1262f 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
12630 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
12631 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 f something goes
12632 20 77 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 wrong. The.**
12633 69 6e 74 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 integer *pHasMov
12634 65 64 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 ed is set to one
12635 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 68 if the cursor h
12636 61 73 20 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 as moved and 0 i
12637 66 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 f not..*/.SQLITE
12638 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
12639 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 ite3BtreeCursorH
1263a 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 asMoved(BtCursor
1263b 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 *pCur, int *pHa
1263c 73 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 sMoved){. int r
1263d 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 6f c;.. rc = resto
1263e 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
1263f 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
12640 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 ){. *pHasMov
12641 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 ed = 1;. retu
12642 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 rn rc;. }. if(
12643 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCur->eState!=C
12644 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 URSOR_VALID || p
12645 43 75 72 2d 3e 73 6b 69 70 21 3d 30 20 29 7b 0a Cur->skip!=0 ){.
12646 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d *pHasMoved =
12647 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1;. }else{.
12648 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b *pHasMoved = 0;
12649 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
1264a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e LITE_OK;.}..#ifn
1264b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1264c 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a AUTOVACUUM./*.**
1264d 20 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 Given a page nu
1264e 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 mber of a regula
1264f 72 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c r database page,
12650 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 return the page
12651 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 .** number for t
12652 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 he pointer-map p
12653 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e age that contain
12654 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 s the entry for
12655 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 the.** input pag
12656 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
12657 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 tic Pgno ptrmapP
12658 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a ageno(BtShared *
12659 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b pBt, Pgno pgno){
1265a 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 . int nPagesPer
1265b 4d 61 70 50 61 67 65 2c 20 69 50 74 72 4d 61 70 MapPage, iPtrMap
1265c 2c 20 72 65 74 3b 0a 20 20 61 73 73 65 72 74 28 , ret;. assert(
1265d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1265e 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
1265f 29 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d 61 );. nPagesPerMa
12660 70 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75 73 pPage = (pBt->us
12661 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20 ableSize/5)+1;.
12662 20 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f iPtrMap = (pgno
12663 2d 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61 70 -2)/nPagesPerMap
12664 50 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28 69 Page;. ret = (i
12665 50 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65 72 PtrMap*nPagesPer
12666 4d 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a 20 MapPage) + 2; .
12667 20 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e if( ret==PENDIN
12668 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
12669 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20 ){. ret++;.
1266a 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b }. return ret;
1266b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 .}../*.** Write
1266c 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 an entry into th
1266d 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a e pointer map..*
1266e 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1266f 65 20 75 70 64 61 74 65 73 20 74 68 65 20 70 6f e updates the po
12670 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 inter map entry
12671 66 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72 20 for page number
12672 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74 'key'.** so that
12673 20 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70 65 it maps to type
12674 20 27 65 54 79 70 65 27 20 61 6e 64 20 70 61 72 'eType' and par
12675 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ent page number
12676 27 70 67 6e 6f 27 2e 0a 2a 2a 20 41 6e 20 65 72 'pgno'..** An er
12677 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
12678 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e rned if somethin
12679 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 g goes wrong, ot
1267a 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f herwise SQLITE_O
1267b 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 K..*/.static int
1267c 20 70 74 72 6d 61 70 50 75 74 28 42 74 53 68 61 ptrmapPut(BtSha
1267d 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b red *pBt, Pgno k
1267e 65 79 2c 20 75 38 20 65 54 79 70 65 2c 20 50 67 ey, u8 eType, Pg
1267f 6e 6f 20 70 61 72 65 6e 74 29 7b 0a 20 20 44 62 no parent){. Db
12680 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 Page *pDbPage;
12681 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d /* The pointer m
12682 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 ap page */. u8
12683 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f *pPtrmap; /
12684 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
12685 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f p data */. Pgno
12686 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a iPtrmap; /*
12687 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
12688 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
12689 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 int offset;
1268a 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
1268b 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 pointer map pag
1268c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a e */. int rc;..
1268d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1268e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
1268f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a ->mutex) );. /*
12690 20 54 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 The master-jour
12691 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 nal page number
12692 6d 75 73 74 20 6e 65 76 65 72 20 62 65 20 75 73 must never be us
12693 65 64 20 61 73 20 61 20 70 6f 69 6e 74 65 72 20 ed as a pointer
12694 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 61 73 map page */. as
12695 73 65 72 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f sert( 0==PTRMAP_
12696 49 53 50 41 47 45 28 70 42 74 2c 20 50 45 4e 44 ISPAGE(pBt, PEND
12697 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
12698 74 29 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 t)) );.. assert
12699 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
1269a 6d 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d m );. if( key==
1269b 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1269c 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1269d 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d KPT;. }. iPtrm
1269e 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 ap = PTRMAP_PAGE
1269f 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 NO(pBt, key);.
126a0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
126a1 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 rGet(pBt->pPager
126a2 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 , iPtrmap, &pDbP
126a3 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d age);. if( rc!=
126a4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
126a5 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
126a6 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 offset = PTRMA
126a7 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 P_PTROFFSET(iPtr
126a8 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 70 50 74 map, key);. pPt
126a9 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c rmap = (u8 *)sql
126aa 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
126ab 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 69 66 (pDbPage);.. if
126ac 28 20 65 54 79 70 65 21 3d 70 50 74 72 6d 61 70 ( eType!=pPtrmap
126ad 5b 6f 66 66 73 65 74 5d 20 7c 7c 20 67 65 74 34 [offset] || get4
126ae 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 byte(&pPtrmap[of
126af 66 73 65 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74 fset+1])!=parent
126b0 20 29 7b 0a 20 20 20 20 54 52 41 43 45 28 28 22 ){. TRACE(("
126b1 50 54 52 4d 41 50 5f 55 50 44 41 54 45 3a 20 25 PTRMAP_UPDATE: %
126b2 64 2d 3e 28 25 64 2c 25 64 29 5c 6e 22 2c 20 6b d->(%d,%d)\n", k
126b3 65 79 2c 20 65 54 79 70 65 2c 20 70 61 72 65 6e ey, eType, paren
126b4 74 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 t));. rc = sq
126b5 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
126b6 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
126b7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
126b8 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 70 ){. pPtrmap
126b9 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 65 [offset] = eType
126ba 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
126bb 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 (&pPtrmap[offset
126bc 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 +1], parent);.
126bd 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 }. }.. sqlit
126be 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 e3PagerUnref(pDb
126bf 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 Page);. return
126c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 rc;.}../*.** Rea
126c1 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 d an entry from
126c2 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e the pointer map.
126c3 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
126c4 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 68 ine retrieves th
126c5 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e e pointer map en
126c6 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b 65 try for page 'ke
126c7 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 y', writing.** t
126c8 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 65 he type and pare
126c9 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 nt page number t
126ca 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a 70 o *pEType and *p
126cb 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 6c Pgno respectivel
126cc 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 y..** An error c
126cd 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
126ce 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 if something goe
126cf 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 s wrong, otherwi
126d0 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f se SQLITE_OK..*/
126d1 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d .static int ptrm
126d2 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20 2a apGet(BtShared *
126d3 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 pBt, Pgno key, u
126d4 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 8 *pEType, Pgno
126d5 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 *pPgno){. DbPag
126d6 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f 2a e *pDbPage; /*
126d7 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
126d8 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 page */. int i
126d9 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a Ptrmap; /*
126da 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 Pointer map pag
126db 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 e index */. u8
126dc 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 *pPtrmap;
126dd 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 /* Pointer map p
126de 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e age data */. in
126df 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 t offset;
126e0 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e /* Offset of en
126e1 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d try in pointer m
126e2 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a ap */. int rc;.
126e3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
126e4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
126e5 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 t->mutex) );..
126e6 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 iPtrmap = PTRMAP
126e7 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 _PAGENO(pBt, key
126e8 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
126e9 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 3PagerGet(pBt->p
126ea 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 Pager, iPtrmap,
126eb 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 &pDbPage);. if(
126ec 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 rc!=0 ){. re
126ed 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 turn rc;. }. p
126ee 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 Ptrmap = (u8 *)s
126ef 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
126f0 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 ta(pDbPage);..
126f1 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f offset = PTRMAP_
126f2 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 PTROFFSET(iPtrma
126f3 70 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65 72 p, key);. asser
126f4 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b 0a t( pEType!=0 );.
126f5 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 72 *pEType = pPtr
126f6 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 map[offset];. i
126f7 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e f( pPgno ) *pPgn
126f8 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 o = get4byte(&pP
126f9 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 trmap[offset+1])
126fa 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 ;.. sqlite3Page
126fb 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b rUnref(pDbPage);
126fc 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c 31 . if( *pEType<1
126fd 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 20 || *pEType>5 )
126fe 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
126ff 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65 RRUPT_BKPT;. re
12700 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
12701 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 }..#else /* if d
12702 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d efined SQLITE_OM
12703 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f IT_AUTOVACUUM */
12704 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 . #define ptrma
12705 70 50 75 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 pPut(w,x,y,z) SQ
12706 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e LITE_OK. #defin
12707 65 20 70 74 72 6d 61 70 47 65 74 28 77 2c 78 2c e ptrmapGet(w,x,
12708 79 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 y,z) SQLITE_OK.
12709 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50 #define ptrmapP
1270a 75 74 4f 76 66 6c 28 79 2c 7a 29 20 53 51 4c 49 utOvfl(y,z) SQLI
1270b 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a TE_OK.#endif../*
1270c 0a 2a 2a 20 47 69 76 65 6e 20 61 20 62 74 72 65 .** Given a btre
1270d 65 20 70 61 67 65 20 61 6e 64 20 61 20 63 65 6c e page and a cel
1270e 6c 20 69 6e 64 65 78 20 28 30 20 6d 65 61 6e 73 l index (0 means
1270f 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 the first cell
12710 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 2c 20 on.** the page,
12711 31 20 6d 65 61 6e 73 20 74 68 65 20 73 65 63 6f 1 means the seco
12712 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73 6f 20 nd cell, and so
12713 66 6f 72 74 68 29 20 72 65 74 75 72 6e 20 61 20 forth) return a
12714 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 pointer.** to th
12715 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0a e cell content..
12716 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
12717 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20 66 6f ne works only fo
12718 72 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 r pages that do
12719 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 not contain over
1271a 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 flow cells..*/.#
1271b 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c 6c 28 define findCell(
1271c 50 2c 49 29 20 5c 0a 20 20 28 28 50 29 2d 3e 61 P,I) \. ((P)->a
1271d 44 61 74 61 20 2b 20 28 28 50 29 2d 3e 6d 61 73 Data + ((P)->mas
1271e 6b 50 61 67 65 20 26 20 67 65 74 32 62 79 74 65 kPage & get2byte
1271f 28 26 28 50 29 2d 3e 61 44 61 74 61 5b 28 50 29 (&(P)->aData[(P)
12720 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 28 ->cellOffset+2*(
12721 49 29 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 I)])))../*.** Th
12722 69 73 20 61 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 is a more comple
12723 78 20 76 65 72 73 69 6f 6e 20 6f 66 20 66 69 6e x version of fin
12724 64 43 65 6c 6c 28 29 20 74 68 61 74 20 77 6f 72 dCell() that wor
12725 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 67 65 73 20 ks for.** pages
12726 74 68 61 74 20 64 6f 20 63 6f 6e 74 61 69 6e 20 that do contain
12727 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 20 overflow cells.
12728 20 53 65 65 20 69 6e 73 65 72 74 0a 2a 2f 0a 73 See insert.*/.s
12729 74 61 74 69 63 20 75 38 20 2a 66 69 6e 64 4f 76 tatic u8 *findOv
1272a 65 72 66 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 erflowCell(MemPa
1272b 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 ge *pPage, int i
1272c 43 65 6c 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a Cell){. int i;.
1272d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1272e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
1272f 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
12730 29 3b 0a 20 20 66 6f 72 28 69 3d 70 50 61 67 65 );. for(i=pPage
12731 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 ->nOverflow-1; i
12732 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 >=0; i--){. i
12733 6e 74 20 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 nt k;. struct
12734 20 5f 4f 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 _OvflCell *pOvf
12735 6c 3b 0a 20 20 20 20 70 4f 76 66 6c 20 3d 20 26 l;. pOvfl = &
12736 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b pPage->aOvfl[i];
12737 0a 20 20 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e . k = pOvfl->
12738 69 64 78 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d idx;. if( k<=
12739 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 iCell ){. i
1273a 66 28 20 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 f( k==iCell ){.
1273b 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 4f return pO
1273c 76 66 6c 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 vfl->pCell;.
1273d 20 20 7d 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d }. iCell-
1273e 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 -;. }. }. r
1273f 65 74 75 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70 eturn findCell(p
12740 50 61 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a Page, iCell);.}.
12741 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 ./*.** Parse a c
12742 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 ell content bloc
12743 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 k and fill in th
12744 65 20 43 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 e CellInfo struc
12745 74 75 72 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20 ture. There.**
12746 61 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73 are two versions
12747 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
12748 6e 2e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 n. sqlite3Btree
12749 50 61 72 73 65 43 65 6c 6c 28 29 20 74 61 6b 65 ParseCell() take
1274a 73 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64 s a .** cell ind
1274b 65 78 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ex as the second
1274c 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 73 71 argument and sq
1274d 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 lite3BtreeParseC
1274e 65 6c 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b ellPtr() .** tak
1274f 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 es a pointer to
12750 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 the body of the
12751 63 65 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f cell as its seco
12752 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a nd argument..**.
12753 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 ** Within this f
12754 69 6c 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 ile, the parseCe
12755 6c 6c 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 ll() macro can b
12756 65 20 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 e called instead
12757 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 of.** sqlite3Bt
12758 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
12759 29 2e 20 55 73 69 6e 67 20 73 6f 6d 65 20 63 6f ). Using some co
1275a 6d 70 69 6c 65 72 73 2c 20 74 68 69 73 20 77 69 mpilers, this wi
1275b 6c 6c 20 62 65 20 66 61 73 74 65 72 2e 0a 2a 2f ll be faster..*/
1275c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1275d 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
1275e 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 0a 20 eParseCellPtr(.
1275f 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c MemPage *pPage,
12760 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
12761 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
12762 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 43 cell */. u8 *pC
12763 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ell,
12764 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
12765 74 68 65 20 63 65 6c 6c 20 74 65 78 74 2e 20 2a the cell text. *
12766 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 /. CellInfo *pI
12767 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 nfo /* F
12768 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 ill in this stru
12769 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e cture */.){. in
1276a 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 t n;
1276b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1276c 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 20 63 6f bytes in cell co
1276d 6e 74 65 6e 74 20 68 65 61 64 65 72 20 2a 2f 0a ntent header */.
1276e 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 u32 nPayload;
1276f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
12770 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
12771 63 65 6c 6c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a cell payload */.
12772 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
12773 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
12774 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
12775 20 29 3b 0a 0a 20 20 70 49 6e 66 6f 2d 3e 70 43 );.. pInfo->pC
12776 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 61 ell = pCell;. a
12777 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 ssert( pPage->le
12778 61 66 3d 3d 30 20 7c 7c 20 70 50 61 67 65 2d 3e af==0 || pPage->
12779 6c 65 61 66 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d leaf==1 );. n =
1277a 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
1277b 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Size;. assert(
1277c 6e 3d 3d 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 n==4-4*pPage->le
1277d 61 66 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 af );. if( pPag
1277e 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 e->intKey ){.
1277f 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 if( pPage->hasD
12780 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b ata ){. n +
12781 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 70 = getVarint32(&p
12782 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 Cell[n], nPayloa
12783 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 d);. }else{.
12784 20 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 nPayload =
12785 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 2b 0;. }. n +
12786 3d 20 67 65 74 56 61 72 69 6e 74 28 26 70 43 65 = getVarint(&pCe
12787 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a 29 26 70 49 ll[n], (u64*)&pI
12788 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 nfo->nKey);.
12789 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 6e pInfo->nData = n
1278a 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 65 6c 73 65 Payload;. }else
1278b 7b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 {. pInfo->nDa
1278c 74 61 20 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d ta = 0;. n +=
1278d 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 70 43 getVarint32(&pC
1278e 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 ell[n], nPayload
1278f 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b );. pInfo->nK
12790 65 79 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 ey = nPayload;.
12791 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 }. pInfo->nPay
12792 6c 6f 61 64 20 3d 20 6e 50 61 79 6c 6f 61 64 3b load = nPayload;
12793 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48 65 61 64 65 . pInfo->nHeade
12794 72 20 3d 20 6e 3b 0a 20 20 69 66 28 20 6c 69 6b r = n;. if( lik
12795 65 6c 79 28 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 ely(nPayload<=pP
12796 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 age->maxLocal) )
12797 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 {. /* This is
12798 20 74 68 65 20 28 65 61 73 79 29 20 63 6f 6d 6d the (easy) comm
12799 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 74 68 on case where th
1279a 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 e entire payload
1279b 20 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 fits. ** on
1279c 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2e 20 the local page.
1279d 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69 73 20 No overflow is
1279e 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f required.. */
1279f 0a 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 3b 20 . int nSize;
127a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
127a1 6c 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 20 63 l size of cell c
127a2 6f 6e 74 65 6e 74 20 69 6e 20 62 79 74 65 73 20 ontent in bytes
127a3 2a 2f 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6e */. nSize = n
127a4 50 61 79 6c 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 Payload + n;.
127a5 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d pInfo->nLocal =
127a6 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 nPayload;. p
127a7 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 Info->iOverflow
127a8 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e 53 = 0;. if( (nS
127a9 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b 0a ize & ~3)==0 ){.
127aa 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b nSize = 4;
127ab 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
127ac 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 um cell size is
127ad 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 70 4 */. }. p
127ae 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 6e 53 Info->nSize = nS
127af 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ize;. }else{.
127b0 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c /* If the payl
127b1 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 oad will not fit
127b2 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74 completely on t
127b3 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77 he local page, w
127b4 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f e have. ** to
127b5 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68 decide how much
127b6 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c to store locall
127b7 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74 y and how much t
127b8 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20 o spill onto.
127b9 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ** overflow pag
127ba 65 73 2e 20 20 54 68 65 20 73 74 72 61 74 65 67 es. The strateg
127bb 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 y is to minimize
127bc 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75 the amount of u
127bd 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61 nused. ** spa
127be 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ce on overflow p
127bf 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69 ages while keepi
127c0 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 ng the amount of
127c1 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20 local storage.
127c2 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e ** in between
127c3 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61 minLocal and ma
127c4 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20 xLocal.. **.
127c5 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20 ** Warning:
127c6 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79 changing the way
127c7 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 overflow payloa
127c8 64 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64 d is distributed
127c9 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77 in any. ** w
127ca 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 ay will result i
127cb 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c n an incompatibl
127cc 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20 e file format..
127cd 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69 */. int mi
127ce 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69 nLocal; /* Mini
127cf 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 mum amount of pa
127d0 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c yload held local
127d1 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 ly */. int ma
127d2 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69 xLocal; /* Maxi
127d3 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 mum amount of pa
127d4 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c yload held local
127d5 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75 ly */. int su
127d6 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72 rplus; /* Over
127d7 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61 flow payload ava
127d8 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c ilable for local
127d9 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20 storage */..
127da 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 minLocal = pPag
127db 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 e->minLocal;.
127dc 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67 maxLocal = pPag
127dd 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 e->maxLocal;.
127de 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f surplus = minLo
127df 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20 cal + (nPayload
127e0 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61 - minLocal)%(pPa
127e1 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
127e2 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 69 66 ize - 4);. if
127e3 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 ( surplus <= max
127e4 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 Local ){. p
127e5 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 73 Info->nLocal = s
127e6 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 65 6c 73 urplus;. }els
127e7 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e e{. pInfo->
127e8 6e 4c 6f 63 61 6c 20 3d 20 6d 69 6e 4c 6f 63 61 nLocal = minLoca
127e9 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e l;. }. pIn
127ea 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 fo->iOverflow =
127eb 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 pInfo->nLocal +
127ec 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 n;. pInfo->nS
127ed 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 ize = pInfo->iOv
127ee 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a erflow + 4;. }.
127ef 7d 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 }.#define parseC
127f0 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c ell(pPage, iCell
127f1 2c 20 70 49 6e 66 6f 29 20 5c 0a 20 20 73 71 6c , pInfo) \. sql
127f2 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
127f3 6c 6c 50 74 72 28 28 70 50 61 67 65 29 2c 20 66 llPtr((pPage), f
127f4 69 6e 64 43 65 6c 6c 28 28 70 50 61 67 65 29 2c indCell((pPage),
127f5 20 28 69 43 65 6c 6c 29 29 2c 20 28 70 49 6e 66 (iCell)), (pInf
127f6 6f 29 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 o)).SQLITE_PRIVA
127f7 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
127f8 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 0a 20 treeParseCell(.
127f9 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c MemPage *pPage,
127fa 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
127fb 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
127fc 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 cell */. int iC
127fd 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ell,
127fe 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69 6e /* The cell in
127ff 64 65 78 2e 20 20 46 69 72 73 74 20 63 65 6c 6c dex. First cell
12800 20 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c 6c 49 is 0 */. CellI
12801 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 nfo *pInfo
12802 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 /* Fill in th
12803 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a is structure */.
12804 29 7b 0a 20 20 70 61 72 73 65 43 65 6c 6c 28 70 ){. parseCell(p
12805 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e Page, iCell, pIn
12806 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f fo);.}../*.** Co
12807 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20 mpute the total
12808 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
12809 74 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65 64 that a Cell need
1280a 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a s in the cell.**
1280b 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 74 68 data area of th
1280c 65 20 62 74 72 65 65 2d 70 61 67 65 2e 20 20 54 e btree-page. T
1280d 68 65 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 72 he return number
1280e 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 63 65 includes the ce
1280f 6c 6c 0a 2a 2a 20 64 61 74 61 20 68 65 61 64 65 ll.** data heade
12810 72 20 61 6e 64 20 74 68 65 20 6c 6f 63 61 6c 20 r and the local
12811 70 61 79 6c 6f 61 64 2c 20 62 75 74 20 6e 6f 74 payload, but not
12812 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 any overflow pa
12813 67 65 20 6f 72 0a 2a 2a 20 74 68 65 20 73 70 61 ge or.** the spa
12814 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 ce used by the c
12815 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a ell pointer..*/.
12816 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 #ifndef NDEBUG.s
12817 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 tatic u16 cellSi
12818 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ze(MemPage *pPag
12819 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 e, int iCell){.
1281a 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
1281b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 sqlite3BtreePa
1281c 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 rseCell(pPage, i
1281d 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 Cell, &info);.
1281e 72 65 74 75 72 6e 20 69 6e 66 6f 2e 6e 53 69 7a return info.nSiz
1281f 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 73 74 61 74 e;.}.#endif.stat
12820 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 ic u16 cellSizeP
12821 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 tr(MemPage *pPag
12822 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 e, u8 *pCell){.
12823 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
12824 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 sqlite3BtreePa
12825 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 rseCellPtr(pPage
12826 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b , pCell, &info);
12827 0a 20 20 72 65 74 75 72 6e 20 69 6e 66 6f 2e 6e . return info.n
12828 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 Size;.}..#ifndef
12829 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1282a 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 OVACUUM./*.** If
1282b 20 74 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c the cell pCell,
1282c 20 70 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 part of page pP
1282d 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 age contains a p
1282e 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 ointer.** to an
1282f 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 overflow page, i
12830 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 nsert an entry i
12831 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d nto the pointer-
12832 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f map.** for the o
12833 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f verflow page..*/
12834 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d .static int ptrm
12835 61 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d apPutOvflPtr(Mem
12836 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 Page *pPage, u8
12837 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 *pCell){. CellI
12838 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 61 73 73 65 nfo info;. asse
12839 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a rt( pCell!=0 );.
1283a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 sqlite3BtreePa
1283b 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 rseCellPtr(pPage
1283c 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b , pCell, &info);
1283d 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f . assert( (info
1283e 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 .nData+(pPage->i
1283f 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 ntKey?0:info.nKe
12840 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f y))==info.nPaylo
12841 61 64 20 29 3b 0a 20 20 69 66 28 20 28 69 6e 66 ad );. if( (inf
12842 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e o.nData+(pPage->
12843 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b intKey?0:info.nK
12844 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c ey))>info.nLocal
12845 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 66 ){. Pgno ovf
12846 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 l = get4byte(&pC
12847 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c ell[info.iOverfl
12848 6f 77 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ow]);. return
12849 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 ptrmapPut(pPage
1284a 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 ->pBt, ovfl, PTR
1284b 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 MAP_OVERFLOW1, p
1284c 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d Page->pgno);. }
1284d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1284e 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 49 66 20 _OK;.}./*.** If
1284f 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69 6e the cell with in
12850 64 65 78 20 69 43 65 6c 6c 20 6f 6e 20 70 61 67 dex iCell on pag
12851 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 e pPage contains
12852 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f a pointer.** to
12853 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
12854 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 e, insert an ent
12855 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e ry into the poin
12856 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 ter-map.** for t
12857 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
12858 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
12859 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 4d 65 ptrmapPutOvfl(Me
1285a 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e mPage *pPage, in
1285b 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a t iCell){. u8 *
1285c 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 pCell;. assert(
1285d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1285e 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
1285f 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 65 6c mutex) );. pCel
12860 6c 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f 77 l = findOverflow
12861 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c Cell(pPage, iCel
12862 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 74 72 l);. return ptr
12863 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 mapPutOvflPtr(pP
12864 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 7d 0a 23 age, pCell);.}.#
12865 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 endif.../*.** De
12866 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 fragment the pag
12867 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65 e given. All Ce
12868 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f lls are moved to
12869 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 the.** end of t
1286a 68 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 he page and all
1286b 66 72 65 65 20 73 70 61 63 65 20 69 73 20 63 6f free space is co
1286c 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 llected into one
1286d 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b 20 .** big FreeBlk
1286e 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 62 that occurs in b
1286f 65 74 77 65 65 6e 20 74 68 65 20 68 65 61 64 65 etween the heade
12870 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f r and cell.** po
12871 69 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64 20 inter array and
12872 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
12873 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 area..*/.static
12874 20 76 6f 69 64 20 64 65 66 72 61 67 6d 65 6e 74 void defragment
12875 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Page(MemPage *pP
12876 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 age){. int i;
12877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12878 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
12879 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 er */. int pc;
1287a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1287b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
1287c 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a a i-th cell */.
1287d 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 int addr;
1287e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1287f 4f 66 66 73 65 74 20 6f 66 20 66 69 72 73 74 20 Offset of first
12880 62 79 74 65 20 61 66 74 65 72 20 63 65 6c 6c 20 byte after cell
12881 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f pointer array */
12882 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 . int hdr;
12883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12884 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 70 Offset to the p
12885 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 age header */.
12886 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 int size;
12887 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
12888 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a ze of a cell */.
12889 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 int usableSize
1288a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1288b 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 Number of usable
1288c 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 bytes on a page
1288d 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 */. int cellOf
1288e 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 fset;
1288f 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 /* Offset to th
12890 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
12891 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 rray */. int cb
12892 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rk;
12893 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
12894 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 o the cell conte
12895 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 nt area */. int
12896 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 nCell;
12897 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
12898 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 r of cells on th
12899 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 e page */. unsi
1289a 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b gned char *data;
1289b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
1289c 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 ge data */. uns
1289d 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 igned char *temp
1289e 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 ; /* Temp
1289f 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f area for cell co
128a0 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 ntent */.. asse
128a1 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
128a2 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
128a3 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
128a4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
128a5 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 pBt!=0 );. asse
128a6 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e rt( pPage->pBt->
128a7 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51 usableSize <= SQ
128a8 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
128a9 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ZE );. assert(
128aa 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
128ab 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
128ac 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
128ad 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
128ae 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70 mutex) );. temp
128af 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 = sqlite3PagerT
128b0 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d 3e empSpace(pPage->
128b1 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
128b2 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
128b3 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 ata;. hdr = pPa
128b4 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 ge->hdrOffset;.
128b5 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 cellOffset = pP
128b6 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b age->cellOffset;
128b7 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 . nCell = pPage
128b8 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 ->nCell;. asser
128b9 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79 t( nCell==get2by
128ba 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 te(&data[hdr+3])
128bb 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 );. usableSize
128bc 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 = pPage->pBt->u
128bd 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 72 sableSize;. cbr
128be 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 k = get2byte(&da
128bf 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 ta[hdr+5]);. me
128c0 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b 5d mcpy(&temp[cbrk]
128c1 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75 , &data[cbrk], u
128c2 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 6b sableSize - cbrk
128c3 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62 );. cbrk = usab
128c4 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 3d leSize;. for(i=
128c5 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 0; i<nCell; i++)
128c6 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b {. u8 *pAddr;
128c7 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 /* The i-th
128c8 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f cell pointer */
128c9 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 . pAddr = &da
128ca 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 ta[cellOffset +
128cb 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 i*2];. pc = g
128cc 65 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a et2byte(pAddr);.
128cd 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3c 70 assert( pc<p
128ce 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
128cf 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 73 69 7a eSize );. siz
128d0 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 e = cellSizePtr(
128d1 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d pPage, &temp[pc]
128d2 29 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 73 );. cbrk -= s
128d3 69 7a 65 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 ize;. memcpy(
128d4 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65 &data[cbrk], &te
128d5 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 mp[pc], size);.
128d6 20 20 20 70 75 74 32 62 79 74 65 28 70 41 64 64 put2byte(pAdd
128d7 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 r, cbrk);. }.
128d8 61 73 73 65 72 74 28 20 63 62 72 6b 3e 3d 63 65 assert( cbrk>=ce
128d9 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c llOffset+2*nCell
128da 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 );. put2byte(&
128db 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 data[hdr+5], cbr
128dc 6b 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 k);. data[hdr+1
128dd 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 ] = 0;. data[hd
128de 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 r+2] = 0;. data
128df 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 61 [hdr+7] = 0;. a
128e0 64 64 72 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 ddr = cellOffset
128e1 2b 32 2a 6e 43 65 6c 6c 3b 0a 20 20 6d 65 6d 73 +2*nCell;. mems
128e2 65 74 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 et(&data[addr],
128e3 30 2c 20 63 62 72 6b 2d 61 64 64 72 29 3b 0a 7d 0, cbrk-addr);.}
128e4 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
128e5 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 nByte bytes of
128e6 73 70 61 63 65 20 6f 6e 20 61 20 70 61 67 65 2e space on a page.
128e7 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
128e8 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 e index into pPa
128e9 67 65 2d 3e 61 44 61 74 61 5b 5d 20 6f 66 20 74 ge->aData[] of t
128ea 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 he first byte of
128eb 0a 2a 2a 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f .** the new allo
128ec 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 61 6c cation. The cal
128ed 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 ler guarantees t
128ee 68 61 74 20 74 68 65 72 65 20 69 73 20 65 6e 6f hat there is eno
128ef 75 67 68 0a 2a 2a 20 73 70 61 63 65 2e 20 20 54 ugh.** space. T
128f0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
128f1 20 6e 65 76 65 72 20 66 61 69 6c 2e 0a 2a 2a 0a never fail..**.
128f2 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 63 ** If the page c
128f3 6f 6e 74 61 69 6e 73 20 6e 42 79 74 65 73 20 6f ontains nBytes o
128f4 66 20 66 72 65 65 20 73 70 61 63 65 20 62 75 74 f free space but
128f5 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 does not contai
128f6 6e 0a 2a 2a 20 6e 42 79 74 65 73 20 6f 66 20 63 n.** nBytes of c
128f7 6f 6e 74 69 67 75 6f 75 73 20 66 72 65 65 20 73 ontiguous free s
128f8 70 61 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 pace, then this
128f9 72 6f 75 74 69 6e 65 20 61 75 74 6f 6d 61 74 69 routine automati
128fa 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 73 20 64 cally.** calls d
128fb 65 66 72 61 67 65 6d 65 6e 74 50 61 67 65 28 29 efragementPage()
128fc 20 74 6f 20 63 6f 6e 73 6f 6c 69 64 61 74 65 20 to consolidate
128fd 61 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 62 all free space b
128fe 65 66 6f 72 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 efore .** alloca
128ff 74 69 6e 67 20 74 68 65 20 6e 65 77 20 63 68 75 ting the new chu
12900 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nk..*/.static in
12901 74 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 t allocateSpace(
12902 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
12903 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e int nByte){. in
12904 74 20 61 64 64 72 2c 20 70 63 2c 20 68 64 72 3b t addr, pc, hdr;
12905 0a 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 69 . int size;. i
12906 6e 74 20 6e 46 72 61 67 3b 0a 20 20 69 6e 74 20 nt nFrag;. int
12907 74 6f 70 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c top;. int nCell
12908 3b 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 ;. int cellOffs
12909 65 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 et;. unsigned c
1290a 68 61 72 20 2a 64 61 74 61 3b 0a 20 20 0a 20 20 har *data;. .
1290b 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
1290c 61 74 61 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ata;. assert( s
1290d 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1290e 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
1290f 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
12910 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 rt( pPage->pBt )
12911 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
12912 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
12913 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
12914 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e ) );. assert( n
12915 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d Byte>=0 ); /* M
12916 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 inimum cell size
12917 20 69 73 20 34 20 2a 2f 0a 20 20 61 73 73 65 72 is 4 */. asser
12918 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e t( pPage->nFree>
12919 3d 6e 42 79 74 65 20 29 3b 0a 20 20 61 73 73 65 =nByte );. asse
1291a 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 rt( pPage->nOver
1291b 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 50 61 flow==0 );. pPa
1291c 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 6e 42 79 ge->nFree -= nBy
1291d 74 65 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 te;. hdr = pPag
1291e 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 0a 20 e->hdrOffset;..
1291f 20 6e 46 72 61 67 20 3d 20 64 61 74 61 5b 68 64 nFrag = data[hd
12920 72 2b 37 5d 3b 0a 20 20 69 66 28 20 6e 46 72 61 r+7];. if( nFra
12921 67 3c 36 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53 g<60 ){. /* S
12922 65 61 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 earch the freeli
12923 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 st looking for a
12924 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 slot big enough
12925 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a to satisfy the.
12926 20 20 20 20 2a 2a 20 73 70 61 63 65 20 72 65 71 ** space req
12927 75 65 73 74 2e 20 2a 2f 0a 20 20 20 20 61 64 64 uest. */. add
12928 72 20 3d 20 68 64 72 2b 31 3b 0a 20 20 20 20 77 r = hdr+1;. w
12929 68 69 6c 65 28 20 28 70 63 20 3d 20 67 65 74 32 hile( (pc = get2
1292a 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d byte(&data[addr]
1292b 29 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 69 ))>0 ){. si
1292c 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 ze = get2byte(&d
1292d 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 ata[pc+2]);.
1292e 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 if( size>=nByt
1292f 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 e ){. if(
12930 20 73 69 7a 65 3c 6e 42 79 74 65 2b 34 20 29 7b size<nByte+4 ){
12931 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
12932 79 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 y(&data[addr], &
12933 64 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 data[pc], 2);.
12934 20 20 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 data[hdr
12935 2b 37 5d 20 3d 20 6e 46 72 61 67 20 2b 20 73 69 +7] = nFrag + si
12936 7a 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20 ze - nByte;.
12937 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 63 3b return pc;
12938 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
12939 20 20 20 20 20 20 20 20 20 20 70 75 74 32 62 79 put2by
1293a 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c 20 te(&data[pc+2],
1293b 73 69 7a 65 2d 6e 42 79 74 65 29 3b 0a 20 20 20 size-nByte);.
1293c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 63 return pc
1293d 20 2b 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b + size - nByte;
1293e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1293f 20 7d 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 }. addr =
12940 70 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 pc;. }. }..
12941 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d /* Allocate mem
12942 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 ory from the gap
12943 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 in between the
12944 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 cell pointer arr
12945 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 ay. ** and the
12946 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
12947 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 3d 20 a.. */. top =
12948 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
12949 64 72 2b 35 5d 29 3b 0a 20 20 6e 43 65 6c 6c 20 dr+5]);. nCell
1294a 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
1294b 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 63 65 6c 6c [hdr+3]);. cell
1294c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e Offset = pPage->
1294d 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 69 66 cellOffset;. if
1294e 28 20 6e 46 72 61 67 3e 3d 36 30 20 7c 7c 20 63 ( nFrag>=60 || c
1294f 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 ellOffset + 2*nC
12950 65 6c 6c 20 3e 20 74 6f 70 20 2d 20 6e 42 79 74 ell > top - nByt
12951 65 20 29 7b 0a 20 20 20 20 64 65 66 72 61 67 6d e ){. defragm
12952 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a entPage(pPage);.
12953 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 top = get2by
12954 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
12955 3b 0a 20 20 7d 0a 20 20 74 6f 70 20 2d 3d 20 6e ;. }. top -= n
12956 42 79 74 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Byte;. assert(
12957 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e cellOffset + 2*n
12958 43 65 6c 6c 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 Cell <= top );.
12959 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
1295a 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 hdr+5], top);.
1295b 72 65 74 75 72 6e 20 74 6f 70 3b 0a 7d 0a 0a 2f return top;.}../
1295c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 65 *.** Return a se
1295d 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 50 61 ction of the pPa
1295e 67 65 2d 3e 61 44 61 74 61 20 74 6f 20 74 68 65 ge->aData to the
1295f 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 20 54 68 freelist..** Th
12960 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
12961 74 68 65 20 6e 65 77 20 66 72 65 65 20 62 6c 6f the new free blo
12962 63 6b 20 69 73 20 70 50 61 67 65 2d 3e 61 44 69 ck is pPage->aDi
12963 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20 61 6e 64 sk[start].** and
12964 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
12965 20 62 6c 6f 63 6b 20 69 73 20 22 73 69 7a 65 22 block is "size"
12966 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4d 6f bytes..**.** Mo
12967 73 74 20 6f 66 20 74 68 65 20 65 66 66 6f 72 74 st of the effort
12968 20 68 65 72 65 20 69 73 20 69 6e 76 6f 6c 76 65 here is involve
12969 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e 67 20 61 d in coalesing a
1296a 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72 65 65 20 djacent.** free
1296b 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61 20 73 69 blocks into a si
1296c 6e 67 6c 65 20 62 69 67 20 66 72 65 65 20 62 6c ngle big free bl
1296d 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ock..*/.static v
1296e 6f 69 64 20 66 72 65 65 53 70 61 63 65 28 4d 65 oid freeSpace(Me
1296f 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e mPage *pPage, in
12970 74 20 73 74 61 72 74 2c 20 69 6e 74 20 73 69 7a t start, int siz
12971 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c 20 e){. int addr,
12972 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a 20 20 75 pbegin, hdr;. u
12973 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 nsigned char *da
12974 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
12975 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 a;.. assert( pP
12976 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 age->pBt!=0 );.
12977 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
12978 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
12979 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
1297a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 );. assert( st
1297b 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f art>=pPage->hdrO
1297c 66 66 73 65 74 2b 36 2b 28 70 50 61 67 65 2d 3e ffset+6+(pPage->
1297d 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a 20 20 61 leaf?0:4) );. a
1297e 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b 20 ssert( (start +
1297f 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70 42 size)<=pPage->pB
12980 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b t->usableSize );
12981 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
12982 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
12983 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
12984 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 );. assert( si
12985 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d 69 ze>=0 ); /* Mi
12986 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 nimum cell size
12987 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66 20 is 4 */..#ifdef
12988 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 SQLITE_SECURE_DE
12989 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77 72 LETE. /* Overwr
1298a 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f ite deleted info
1298b 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 rmation with zer
1298c 6f 73 20 77 68 65 6e 20 74 68 65 20 53 45 43 55 os when the SECU
1298d 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a 20 RE_DELETE . **
1298e 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 option is enable
1298f 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d d at compile-tim
12990 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 64 e */. memset(&d
12991 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20 73 ata[start], 0, s
12992 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 ize);.#endif..
12993 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 65 /* Add the space
12994 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 6c back into the l
12995 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 72 inked list of fr
12996 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 68 64 eeblocks */. hd
12997 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
12998 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 68 fset;. addr = h
12999 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 28 dr + 1;. while(
1299a 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 (pbegin = get2b
1299b 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 yte(&data[addr])
1299c 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 69 )<start && pbegi
1299d 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 n>0 ){. asser
1299e 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 t( pbegin<=pPage
1299f 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
129a0 65 2d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 e-4 );. asser
129a1 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 29 t( pbegin>addr )
129a2 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 ;. addr = pbe
129a3 67 69 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 gin;. }. asser
129a4 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 t( pbegin<=pPage
129a5 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
129a6 65 2d 34 20 29 3b 0a 20 20 61 73 73 65 72 74 28 e-4 );. assert(
129a7 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20 pbegin>addr ||
129a8 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 pbegin==0 );. p
129a9 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 ut2byte(&data[ad
129aa 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20 20 70 dr], start);. p
129ab 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 ut2byte(&data[st
129ac 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20 art], pbegin);.
129ad 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
129ae 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b start+2], size);
129af 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 . pPage->nFree
129b0 2b 3d 20 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 += size;.. /* C
129b1 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 oalesce adjacent
129b2 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a free blocks */.
129b3 20 20 61 64 64 72 20 3d 20 70 50 61 67 65 2d 3e addr = pPage->
129b4 68 64 72 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20 hdrOffset + 1;.
129b5 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 while( (pbegin
129b6 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
129b7 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 [addr]))>0 ){.
129b8 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 69 int pnext, psi
129b9 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ze;. assert(
129ba 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 pbegin>addr );.
129bb 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 assert( pbegi
129bc 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 n<=pPage->pBt->u
129bd 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 sableSize-4 );.
129be 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62 pnext = get2b
129bf 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
129c0 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 3d 20 ]);. psize =
129c1 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
129c2 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69 begin+2]);. i
129c3 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73 69 7a f( pbegin + psiz
129c4 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 20 26 e + 3 >= pnext &
129c5 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20 & pnext>0 ){.
129c6 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20 70 6e int frag = pn
129c7 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b 70 73 ext - (pbegin+ps
129c8 69 7a 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ize);. asse
129c9 72 74 28 20 66 72 61 67 3c 3d 64 61 74 61 5b 70 rt( frag<=data[p
129ca 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
129cb 37 5d 20 29 3b 0a 20 20 20 20 20 20 64 61 74 61 7] );. data
129cc 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
129cd 74 2b 37 5d 20 2d 3d 20 66 72 61 67 3b 0a 20 20 t+7] -= frag;.
129ce 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
129cf 74 61 5b 70 62 65 67 69 6e 5d 2c 20 67 65 74 32 ta[pbegin], get2
129d0 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 byte(&data[pnext
129d1 5d 29 29 3b 0a 20 20 20 20 20 20 70 75 74 32 62 ]));. put2b
129d2 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
129d3 2b 32 5d 2c 20 70 6e 65 78 74 2b 67 65 74 32 62 +2], pnext+get2b
129d4 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 2b yte(&data[pnext+
129d5 32 5d 29 2d 70 62 65 67 69 6e 29 3b 0a 20 20 20 2])-pbegin);.
129d6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 }else{. ad
129d7 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 dr = pbegin;.
129d8 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
129d9 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
129da 20 61 72 65 61 20 62 65 67 69 6e 73 20 77 69 74 area begins wit
129db 68 20 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 h a freeblock, r
129dc 65 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 emove it. */. i
129dd 66 28 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d f( data[hdr+1]==
129de 64 61 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 data[hdr+5] && d
129df 61 74 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 ata[hdr+2]==data
129e0 5b 68 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 [hdr+6] ){. i
129e1 6e 74 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 nt top;. pbeg
129e2 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 in = get2byte(&d
129e3 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 ata[hdr+1]);.
129e4 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 68 64 memcpy(&data[hd
129e5 72 2b 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 r+1], &data[pbeg
129e6 69 6e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 in], 2);. top
129e7 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
129e8 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 a[hdr+5]);. p
129e9 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
129ea 72 2b 35 5d 2c 20 74 6f 70 20 2b 20 67 65 74 32 r+5], top + get2
129eb 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 byte(&data[pbegi
129ec 6e 2b 32 5d 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f n+2]));. }.}../
129ed 2a 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 20 *.** Decode the
129ee 66 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 20 flags byte (the
129ef 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 first byte of th
129f0 65 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 20 e header) for a
129f1 70 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 page.** and init
129f2 69 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f 66 ialize fields of
129f3 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 72 the MemPage str
129f4 75 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e 67 ucture according
129f5 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 ly..**.** Only t
129f6 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d he following com
129f7 62 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 75 binations are su
129f8 70 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 69 pported. Anythi
129f9 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 ng different.**
129fa 69 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 72 indicates a corr
129fb 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c upt database fil
129fc 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 es:.**.**
129fd 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a 2a PTF_ZERODATA.*
129fe 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 * PTF_ZE
129ff 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41 RODATA | PTF_LEA
12a00 46 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 F.** PTF
12a01 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f _LEAFDATA | PTF_
12a02 49 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 20 INTKEY.**
12a03 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c PTF_LEAFDATA |
12a04 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 54 PTF_INTKEY | PT
12a05 46 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 63 F_LEAF.*/.static
12a06 20 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 73 int decodeFlags
12a07 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
12a08 20 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b 0a int flagByte){.
12a09 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b BtShared *pBt;
12a0a 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f /* A copy o
12a0b 66 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f 0a f pPage->pBt */.
12a0c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
12a0d 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 50 ->hdrOffset==(pP
12a0e 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 age->pgno==1 ? 1
12a0f 30 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 73 00 : 0) );. ass
12a10 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
12a11 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
12a12 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
12a13 70 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 66 6c pPage->leaf = fl
12a14 61 67 42 79 74 65 3e 3e 33 3b 20 20 61 73 73 65 agByte>>3; asse
12a15 72 74 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 rt( PTF_LEAF ==
12a16 31 3c 3c 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 1<<3 );. flagBy
12a17 74 65 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b te &= ~PTF_LEAF;
12a18 0a 20 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 . pPage->childP
12a19 74 72 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 trSize = 4-4*pPa
12a1a 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 ge->leaf;. pBt
12a1b 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
12a1c 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 if( flagByte==(P
12a1d 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 TF_LEAFDATA | PT
12a1e 46 5f 49 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 F_INTKEY) ){.
12a1f 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d pPage->intKey =
12a20 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 1;. pPage->h
12a21 61 73 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e asData = pPage->
12a22 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d leaf;. pPage-
12a23 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d >maxLocal = pBt-
12a24 3e 6d 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 >maxLeaf;. pP
12a25 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 age->minLocal =
12a26 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 pBt->minLeaf;.
12a27 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 42 79 }else if( flagBy
12a28 74 65 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 te==PTF_ZERODATA
12a29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 ){. pPage->i
12a2a 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 ntKey = 0;. p
12a2b 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 Page->hasData =
12a2c 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 0;. pPage->ma
12a2d 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 xLocal = pBt->ma
12a2e 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 xLocal;. pPag
12a2f 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 e->minLocal = pB
12a30 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d t->minLocal;. }
12a31 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
12a32 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
12a33 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 BKPT;. }. retu
12a34 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
12a35 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
12a36 65 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 e the auxiliary
12a37 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
12a38 61 20 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a a disk block..**
12a39 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
12a3a 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e E_OK on success.
12a3b 20 20 49 66 20 77 65 20 73 65 65 20 74 68 61 74 If we see that
12a3c 20 74 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a the page does.*
12a3d 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 * not contain a
12a3e 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 well-formed data
12a3f 62 61 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20 base page, then
12a40 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 return .** SQLIT
12a41 45 5f 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 E_CORRUPT. Note
12a42 20 74 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f that a return o
12a43 66 20 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 f SQLITE_OK does
12a44 20 6e 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 not.** guarante
12a45 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
12a46 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 is well-formed.
12a47 20 49 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 It only shows t
12a48 68 61 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 hat.** we failed
12a49 20 74 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63 to detect any c
12a4a 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 orruption..*/.SQ
12a4b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
12a4c 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 sqlite3BtreeIni
12a4d 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 tPage(MemPage *p
12a4e 50 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 Page){.. assert
12a4f 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 ( pPage->pBt!=0
12a50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
12a51 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
12a52 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
12a53 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
12a54 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c pPage->pgno==sql
12a55 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d ite3PagerPagenum
12a56 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ber(pPage->pDbPa
12a57 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
12a58 20 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 pPage == sqlite
12a59 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 3PagerGetExtra(p
12a5a 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
12a5b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
12a5c 65 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 e->aData == sqli
12a5d 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
12a5e 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
12a5f 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 );.. if( !pPage
12a60 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 ->isInit ){.
12a61 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 int pc;
12a62 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
12a63 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 a freeblock wit
12a64 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 hin pPage->aData
12a65 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 68 64 [] */. int hd
12a66 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
12a67 4f 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e Offset to beginn
12a68 69 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61 64 ing of page head
12a69 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 er */. u8 *da
12a6a 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ta; /*
12a6b 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e Equal to pPage->
12a6c 61 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 aData */. BtS
12a6d 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 hared *pBt;
12a6e 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 /* The main b
12a6f 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a tree structure *
12a70 2f 0a 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 /. int usable
12a71 53 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 Size; /* Amou
12a72 6e 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 nt of usable spa
12a73 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 ce on each page
12a74 2a 2f 0a 20 20 20 20 69 6e 74 20 63 65 6c 6c 4f */. int cellO
12a75 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 ffset; /* Off
12a76 73 65 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f set from start o
12a77 66 20 70 61 67 65 20 74 6f 20 66 69 72 73 74 20 f page to first
12a78 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a cell pointer */.
12a79 20 20 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 int nFree;
12a7a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
12a7b 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 of unused bytes
12a7c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a on the page */.
12a7d 20 20 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 int top;
12a7e 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
12a7f 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c byte of the cell
12a80 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f content area */
12a81 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 61 67 .. pBt = pPag
12a82 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 64 72 e->pBt;.. hdr
12a83 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
12a84 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 set;. data =
12a85 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 pPage->aData;.
12a86 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61 67 if( decodeFlag
12a87 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68 64 s(pPage, data[hd
12a88 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c r]) ) return SQL
12a89 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
12a8a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
12a8b 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 t->pageSize>=512
12a8c 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a && pBt->pageSiz
12a8d 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 20 20 e<=32768 );.
12a8e 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 pPage->maskPage
12a8f 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 = pBt->pageSize
12a90 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e - 1;. pPage->
12a91 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 nOverflow = 0;.
12a92 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 usableSize =
12a93 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
12a94 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c . pPage->cell
12a95 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 Offset = cellOff
12a96 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d set = hdr + 12 -
12a97 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 4*pPage->leaf;.
12a98 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 top = get2by
12a99 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
12a9a 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 ;. pPage->nCe
12a9b 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 ll = get2byte(&d
12a9c 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 ata[hdr+3]);.
12a9d 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c if( pPage->nCel
12a9e 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 l>MX_CELL(pBt) )
12a9f 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61 {. /* To ma
12aa0 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 ny cells for a s
12aa1 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 68 65 ingle page. The
12aa2 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f page must be co
12aa3 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 20 72 rrupt */. r
12aa4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
12aa5 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
12aa6 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 . . /* Compu
12aa7 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 65 te the total fre
12aa8 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 e space on the p
12aa9 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 age */. pc =
12aaa 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
12aab 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 dr+1]);. nFre
12aac 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 e = data[hdr+7]
12aad 2b 20 74 6f 70 20 2d 20 28 63 65 6c 6c 4f 66 66 + top - (cellOff
12aae 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e set + 2*pPage->n
12aaf 43 65 6c 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65 Cell);. while
12ab0 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 ( pc>0 ){.
12ab1 69 6e 74 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a int next, size;.
12ab2 20 20 20 20 20 20 69 66 28 20 70 63 3e 75 73 61 if( pc>usa
12ab3 62 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 bleSize-4 ){.
12ab4 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f /* Free blo
12ab5 63 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70 61 ck is off the pa
12ab6 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 ge */. re
12ab7 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
12ab8 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 UPT_BKPT; .
12ab9 20 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 }. next =
12aba 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
12abb 63 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 c]);. size
12abc 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
12abd 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 [pc+2]);. i
12abe 66 28 20 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 f( next>0 && nex
12abf 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a t<=pc+size+3 ){.
12ac0 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 /* Free
12ac1 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69 blocks must be i
12ac2 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 n accending orde
12ac3 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 r */. ret
12ac4 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
12ac5 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 PT_BKPT; .
12ac6 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d }. nFree +=
12ac7 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 size;. pc
12ac8 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 = next;. }.
12ac9 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d pPage->nFree =
12aca 20 6e 46 72 65 65 3b 0a 20 20 20 20 69 66 28 20 nFree;. if(
12acb 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65 53 69 7a nFree>=usableSiz
12acc 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 72 e ){. /* Fr
12acd 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f 74 20 ee space cannot
12ace 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70 61 67 exceed total pag
12acf 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 e size */.
12ad0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
12ad1 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 RRUPT_BKPT; .
12ad2 20 7d 0a 0a 23 69 66 20 30 0a 20 20 2f 2a 20 43 }..#if 0. /* C
12ad3 68 65 63 6b 20 74 68 61 74 20 61 6c 6c 20 74 68 heck that all th
12ad4 65 20 6f 66 66 73 65 74 73 20 69 6e 20 74 68 65 e offsets in the
12ad5 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61 72 72 cell offset arr
12ad6 61 79 20 61 72 65 20 77 69 74 68 69 6e 20 72 61 ay are within ra
12ad7 6e 67 65 2e 20 0a 20 20 2a 2a 20 0a 20 20 2a 2a nge. . ** . **
12ad8 20 4f 6d 69 74 74 69 6e 67 20 74 68 69 73 20 63 Omitting this c
12ad9 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b onsistency check
12ada 20 61 6e 64 20 75 73 69 6e 67 20 74 68 65 20 70 and using the p
12adb 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 6d Page->maskPage m
12adc 61 73 6b 0a 20 20 2a 2a 20 74 6f 20 70 72 65 76 ask. ** to prev
12add 65 6e 74 20 6f 76 65 72 72 75 6e 6e 69 6e 67 20 ent overrunning
12ade 74 68 65 20 70 61 67 65 20 62 75 66 66 65 72 20 the page buffer
12adf 69 6e 20 66 69 6e 64 43 65 6c 6c 28 29 20 72 65 in findCell() re
12ae0 73 75 6c 74 73 20 69 6e 20 61 0a 20 20 2a 2a 20 sults in a. **
12ae1 32 2e 35 25 20 70 65 72 66 6f 72 6d 61 6e 63 65 2.5% performance
12ae2 20 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20 7b 0a gain.. */. {.
12ae3 20 20 20 20 75 38 20 2a 70 4f 66 66 3b 20 20 20 u8 *pOff;
12ae4 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
12ae5 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 61 used to check a
12ae6 6c 6c 20 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 ll cell offsets
12ae7 61 72 65 20 69 6e 20 72 61 6e 67 65 20 2a 2f 0a are in range */.
12ae8 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 20 20 20 u8 *pEnd;
12ae9 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
12aea 74 6f 20 65 6e 64 20 6f 66 20 63 65 6c 6c 20 6f to end of cell o
12aeb 66 66 73 65 74 20 61 72 72 61 79 20 2a 2f 0a 20 ffset array */.
12aec 20 20 20 75 38 20 6d 61 73 6b 3b 20 20 20 20 20 u8 mask;
12aed 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 62 /* Mask of b
12aee 69 74 73 20 74 68 61 74 20 6d 75 73 74 20 62 65 its that must be
12aef 20 7a 65 72 6f 20 69 6e 20 4d 53 42 20 6f 66 20 zero in MSB of
12af0 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 2a 2f 0a cell offsets */.
12af1 20 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28 28 75 mask = ~(((u
12af2 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 8)(pBt->pageSize
12af3 3e 3e 38 29 29 2d 31 29 3b 0a 20 20 20 20 70 45 >>8))-1);. pE
12af4 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f nd = &data[cellO
12af5 66 66 73 65 74 20 2b 20 70 50 61 67 65 2d 3e 6e ffset + pPage->n
12af6 43 65 6c 6c 2a 32 5d 3b 0a 20 20 20 20 66 6f 72 Cell*2];. for
12af7 28 70 4f 66 66 3d 26 64 61 74 61 5b 63 65 6c 6c (pOff=&data[cell
12af8 4f 66 66 73 65 74 5d 3b 20 70 4f 66 66 21 3d 70 Offset]; pOff!=p
12af9 45 6e 64 20 26 26 20 21 28 28 2a 70 4f 66 66 29 End && !((*pOff)
12afa 26 6d 61 73 6b 29 3b 20 70 4f 66 66 2b 3d 32 29 &mask); pOff+=2)
12afb 3b 0a 20 20 20 20 69 66 28 20 70 4f 66 66 21 3d ;. if( pOff!=
12afc 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 72 65 pEnd ){. re
12afd 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
12afe 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
12aff 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
12b00 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
12b01 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
12b02 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
12b03 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 .** Set up a raw
12b04 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 page so that it
12b05 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 looks like a da
12b06 74 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 tabase page hold
12b07 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 ing.** no entrie
12b08 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
12b09 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 d zeroPage(MemPa
12b0a 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 ge *pPage, int f
12b0b 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 lags){. unsigne
12b0c 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 d char *data = p
12b0d 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 Page->aData;. B
12b0e 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
12b0f 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 Page->pBt;. int
12b10 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 hdr = pPage->hd
12b11 72 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 66 rOffset;. int f
12b12 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 irst;.. assert(
12b13 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12b14 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 enumber(pPage->p
12b15 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e DbPage)==pPage->
12b16 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 pgno );. assert
12b17 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 ( sqlite3PagerGe
12b18 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 tExtra(pPage->pD
12b19 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a bPage) == (void*
12b1a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 )pPage );. asse
12b1b 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
12b1c 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 GetData(pPage->p
12b1d 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 DbPage) == data
12b1e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
12b1f 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
12b20 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
12b21 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 age) );. assert
12b22 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
12b23 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
12b24 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 );. /*memset(&
12b25 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 data[hdr], 0, pB
12b26 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
12b27 68 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 hdr);*/. data[h
12b28 64 72 5d 20 3d 20 66 6c 61 67 73 3b 0a 20 20 66 dr] = flags;. f
12b29 69 72 73 74 20 3d 20 68 64 72 20 2b 20 38 20 2b irst = hdr + 8 +
12b2a 20 34 2a 28 28 66 6c 61 67 73 26 50 54 46 5f 4c 4*((flags&PTF_L
12b2b 45 41 46 29 3d 3d 30 29 3b 0a 20 20 6d 65 6d 73 EAF)==0);. mems
12b2c 65 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c et(&data[hdr+1],
12b2d 20 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 0, 4);. data[h
12b2e 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 dr+7] = 0;. put
12b2f 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
12b30 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 5], pBt->usableS
12b31 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e ize);. pPage->n
12b32 46 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Free = pBt->usab
12b33 6c 65 53 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a leSize - first;.
12b34 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 decodeFlags(pP
12b35 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 age, flags);. p
12b36 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 Page->hdrOffset
12b37 3d 20 68 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e = hdr;. pPage->
12b38 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 cellOffset = fir
12b39 73 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 st;. pPage->nOv
12b3a 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 erflow = 0;. as
12b3b 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 sert( pBt->pageS
12b3c 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d ize>=512 && pBt-
12b3d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 >pageSize<=32768
12b3e 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 );. pPage->mas
12b3f 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 kPage = pBt->pag
12b40 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 eSize - 1;. pPa
12b41 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 ge->nCell = 0;.
12b42 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d pPage->isInit =
12b43 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 1;.}.../*.** Co
12b44 6e 76 65 72 74 20 61 20 44 62 50 61 67 65 20 6f nvert a DbPage o
12b45 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 btained from the
12b46 20 70 61 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 pager into a Me
12b47 6d 50 61 67 65 20 75 73 65 64 20 62 79 0a 2a 2a mPage used by.**
12b48 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 the btree layer
12b49 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 ..*/.static MemP
12b4a 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 46 72 age *btreePageFr
12b4b 6f 6d 44 62 50 61 67 65 28 44 62 50 61 67 65 20 omDbPage(DbPage
12b4c 2a 70 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 *pDbPage, Pgno p
12b4d 67 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70 gno, BtShared *p
12b4e 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a Bt){. MemPage *
12b4f 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 pPage = (MemPage
12b50 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
12b51 74 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b tExtra(pDbPage);
12b52 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 . pPage->aData
12b53 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
12b54 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a tData(pDbPage);.
12b55 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 pPage->pDbPage
12b56 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 70 50 = pDbPage;. pP
12b57 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a age->pBt = pBt;.
12b58 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 pPage->pgno =
12b59 70 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 pgno;. pPage->h
12b5a 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 drOffset = pPage
12b5b 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 ->pgno==1 ? 100
12b5c 3a 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 : 0;. return pP
12b5d 61 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 age; .}../*.** G
12b5e 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 et a page from t
12b5f 68 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69 he pager. Initi
12b60 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 alize the MemPag
12b61 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d e.pBt and.** Mem
12b62 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 Page.aData eleme
12b63 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a nts if needed..*
12b64 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f *.** If the noCo
12b65 6e 74 65 6e 74 20 66 6c 61 67 20 69 73 20 73 65 ntent flag is se
12b66 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 t, it means that
12b67 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 we do not care
12b68 61 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e about.** the con
12b69 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 tent of the page
12b6a 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 20 at this time.
12b6b 53 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 So do not go to
12b6c 74 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 the disk.** to f
12b6d 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 etch the content
12b6e 2e 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 . Just fill in
12b6f 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 the content with
12b70 20 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a zeros for now..
12b71 2a 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74 ** If in the fut
12b72 75 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 ure we call sqli
12b73 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
12b74 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68 on this page, th
12b75 61 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 at.** means we h
12b76 61 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62 ave started to b
12b77 65 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 e concerned abou
12b78 74 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 t content and th
12b79 65 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 e disk.** read s
12b7a 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20 74 hould occur at t
12b7b 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 hat point..*/.SQ
12b7c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
12b7d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
12b7e 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
12b7f 20 2a 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 *pBt, /*
12b80 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 The btree */. P
12b81 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 gno pgno,
12b82 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
12b83 20 74 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 the page to fet
12b84 63 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ch */. MemPage
12b85 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 **ppPage, /*
12b86 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 Return the page
12b87 69 6e 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 in this paramete
12b88 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e r */. int noCon
12b89 74 65 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 tent /* D
12b8a 6f 20 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 o not load page
12b8b 63 6f 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 content if true
12b8c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
12b8d 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
12b8e 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 e;.. assert( sq
12b8f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
12b90 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
12b91 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
12b92 67 65 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e gerAcquire(pBt->
12b93 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 pPager, pgno, (D
12b94 62 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 bPage**)&pDbPage
12b95 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 , noContent);.
12b96 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
12b97 72 63 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 rc;. *ppPage =
12b98 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 btreePageFromDbP
12b99 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e age(pDbPage, pgn
12b9a 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 o, pBt);. retur
12b9b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
12b9c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
12b9d 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
12b9e 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 abase file in pa
12b9f 67 65 73 2e 20 20 4f 72 20 72 65 74 75 72 6e 20 ges. Or return
12ba0 2d 31 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 69 -1 if.** there i
12ba1 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 s any kind of er
12ba2 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ror..*/.static i
12ba3 6e 74 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e nt pagerPagecoun
12ba4 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 t(Pager *pPager)
12ba5 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e {. int rc;. in
12ba6 74 20 6e 50 61 67 65 3b 0a 20 20 72 63 20 3d 20 t nPage;. rc =
12ba7 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
12ba8 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e count(pPager, &n
12ba9 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 Page);. return
12baa 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 6e (rc==SQLITE_OK?n
12bab 50 61 67 65 3a 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a Page:-1);.}../*.
12bac 2a 2a 20 47 65 74 20 61 20 70 61 67 65 20 66 72 ** Get a page fr
12bad 6f 6d 20 74 68 65 20 70 61 67 65 72 20 61 6e 64 om the pager and
12bae 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2e 20 initialize it.
12baf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a This routine.**
12bb0 20 69 73 20 6a 75 73 74 20 61 20 63 6f 6e 76 65 is just a conve
12bb1 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 20 61 nience wrapper a
12bb2 72 6f 75 6e 64 20 73 65 70 61 72 61 74 65 20 63 round separate c
12bb3 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 alls to.** sqlit
12bb4 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 29 e3BtreeGetPage()
12bb5 20 61 6e 64 20 73 71 6c 69 74 65 33 42 74 72 65 and sqlite3Btre
12bb6 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a 2f 0a eInitPage()..*/.
12bb7 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 41 6e static int getAn
12bb8 64 49 6e 69 74 50 61 67 65 28 0a 20 20 42 74 53 dInitPage(. BtS
12bb9 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 hared *pBt,
12bba 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
12bbb 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 base file */. P
12bbc 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 gno pgno,
12bbd 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
12bbe 20 74 68 65 20 70 61 67 65 20 74 6f 20 67 65 74 the page to get
12bbf 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
12bc0 70 70 50 61 67 65 20 20 20 20 20 2f 2a 20 57 72 ppPage /* Wr
12bc1 69 74 65 20 74 68 65 20 70 61 67 65 20 70 6f 69 ite the page poi
12bc2 6e 74 65 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a nter here */.){.
12bc3 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 int rc;. DbPa
12bc4 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 4d ge *pDbPage;. M
12bc5 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a emPage *pPage;..
12bc6 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
12bc7 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
12bc8 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
12bc9 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20 ( pgno==0 ){.
12bca 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
12bcb 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 ORRUPT_BKPT; .
12bcc 7d 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 6f 66 }.. /* It is of
12bcd 74 65 6e 20 74 68 65 20 63 61 73 65 20 74 68 61 ten the case tha
12bce 74 20 74 68 65 20 70 61 67 65 20 77 65 20 77 61 t the page we wa
12bcf 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e nt is already in
12bd0 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20 49 66 20 cache.. ** If
12bd1 73 6f 2c 20 67 65 74 20 69 74 20 64 69 72 65 63 so, get it direc
12bd2 74 6c 79 2e 20 20 54 68 69 73 20 73 61 76 65 73 tly. This saves
12bd3 20 75 73 20 66 72 6f 6d 20 68 61 76 69 6e 67 20 us from having
12bd4 74 6f 20 63 61 6c 6c 0a 20 20 2a 2a 20 70 61 67 to call. ** pag
12bd5 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 74 6f erPagecount() to
12bd6 20 6d 61 6b 65 20 73 75 72 65 20 70 67 6e 6f 20 make sure pgno
12bd7 69 73 20 77 69 74 68 69 6e 20 6c 69 6d 69 74 73 is within limits
12bd8 2c 20 77 68 69 63 68 20 72 65 73 75 6c 74 73 0a , which results.
12bd9 20 20 2a 2a 20 69 6e 20 61 20 6d 65 61 73 75 72 ** in a measur
12bda 65 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 eable performanc
12bdb 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 73 2e 0a e improvements..
12bdc 20 20 2a 2f 0a 20 20 70 44 62 50 61 67 65 20 3d */. pDbPage =
12bdd 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f sqlite3PagerLoo
12bde 6b 75 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c kup(pBt->pPager,
12bdf 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 pgno);. if( pD
12be0 62 50 61 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20 bPage ){. /*
12be1 50 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 Page is already
12be2 69 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20 in cache */.
12be3 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 20 *ppPage = pPage
12be4 3d 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 = btreePageFromD
12be5 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 bPage(pDbPage, p
12be6 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 20 20 72 gno, pBt);. r
12be7 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
12be8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 50 }else{. /* P
12be9 61 67 65 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 age not in cache
12bea 2e 20 20 41 63 71 75 69 72 65 20 69 74 2e 20 2a . Acquire it. *
12beb 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 /. if( pgno>p
12bec 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
12bed 74 2d 3e 70 50 61 67 65 72 29 20 29 7b 0a 20 20 t->pPager) ){.
12bee 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
12bef 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 E_CORRUPT_BKPT;
12bf0 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
12bf1 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
12bf2 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70 age(pBt, pgno, p
12bf3 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 pPage, 0);. i
12bf4 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
12bf5 63 3b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 2a c;. pPage = *
12bf6 70 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 ppPage;. }. if
12bf7 28 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 ( !pPage->isInit
12bf8 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
12bf9 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
12bfa 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 e(pPage);. }.
12bfb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12bfc 4b 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 K ){. release
12bfd 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
12bfe 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 *ppPage = 0;.
12bff 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
12c00 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
12c01 61 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 a MemPage. This
12c02 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 should be calle
12c03 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 d once for each
12c04 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f prior.** call to
12c05 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
12c06 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Page..*/.static
12c07 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 void releasePage
12c08 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
12c09 7b 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b {. if( pPage ){
12c0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
12c0b 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 ge->aData );.
12c0c 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
12c0d 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
12c0e 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
12c0f 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 etExtra(pPage->p
12c10 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 DbPage) == (void
12c11 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 20 20 61 *)pPage );. a
12c12 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
12c13 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 gerGetData(pPage
12c14 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 ->pDbPage)==pPag
12c15 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 e->aData );.
12c16 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
12c17 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
12c18 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
12c19 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
12c1a 72 55 6e 72 65 66 28 70 50 61 67 65 2d 3e 70 44 rUnref(pPage->pD
12c1b 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f bPage);. }.}../
12c1c 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20 72 6f *.** During a ro
12c1d 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 llback, when the
12c1e 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73 20 69 pager reloads i
12c1f 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 nformation into
12c20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 the cache.** so
12c21 74 68 61 74 20 74 68 65 20 63 61 63 68 65 20 69 that the cache i
12c22 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 s restored to it
12c23 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 s original state
12c24 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
12c25 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 .** the transact
12c26 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20 70 61 ion, for each pa
12c27 67 65 20 72 65 73 74 6f 72 65 64 20 74 68 69 73 ge restored this
12c28 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
12c29 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
12c2a 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 outine needs to
12c2b 72 65 73 65 74 20 74 68 65 20 65 78 74 72 61 20 reset the extra
12c2c 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61 74 20 data section at
12c2d 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a the end of the.*
12c2e 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65 65 20 * page to agree
12c2f 77 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65 with the restore
12c30 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 d data..*/.stati
12c31 63 20 76 6f 69 64 20 70 61 67 65 52 65 69 6e 69 c void pageReini
12c32 74 28 44 62 50 61 67 65 20 2a 70 44 61 74 61 29 t(DbPage *pData)
12c33 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
12c34 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d ge;. pPage = (M
12c35 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 emPage *)sqlite3
12c36 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 PagerGetExtra(pD
12c37 61 74 61 29 3b 0a 20 20 69 66 28 20 70 50 61 67 ata);. if( pPag
12c38 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 e->isInit ){.
12c39 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
12c3a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
12c3b 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
12c3c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 ;. pPage->isI
12c3d 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 nit = 0;. if(
12c3e 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12c3f 65 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 eRefcount(pData)
12c40 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 >0 ){. sqli
12c41 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 te3BtreeInitPage
12c42 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 (pPage);. }.
12c43 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f }.}../*.** Invo
12c44 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 ke the busy hand
12c45 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65 2e ler for a btree.
12c46 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
12c47 71 6c 69 74 65 33 42 74 72 65 65 49 6e 76 6f 6b qlite3BtreeInvok
12c48 65 42 75 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 eBusyHandler(voi
12c49 64 20 2a 70 41 72 67 2c 20 69 6e 74 20 6e 29 7b d *pArg, int n){
12c4a 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
12c4b 20 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41 = (BtShared*)pA
12c4c 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 rg;. assert( pB
12c4d 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 t->db );. asser
12c4e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
12c4f 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d _held(pBt->db->m
12c50 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 utex) );. retur
12c51 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 n sqlite3InvokeB
12c52 75 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d usyHandler(&pBt-
12c53 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 >db->busyHandler
12c54 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e );.}../*.** Open
12c55 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
12c56 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 ..** .** zFilena
12c57 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f me is the name o
12c58 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
12c59 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 ile. If zFilena
12c5a 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 me is NULL.** a
12c5b 6e 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74 new database wit
12c5c 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 h a random name
12c5d 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 is created. Thi
12c5e 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 s randomly named
12c5f 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c .** database fil
12c60 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 e will be delete
12c61 64 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74 d when sqlite3Bt
12c62 72 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61 reeClose() is ca
12c63 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c lled..** If zFil
12c64 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 ename is ":memor
12c65 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d y:" then an in-m
12c66 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 emory database i
12c67 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 s created.** tha
12c68 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c t is automatical
12c69 6c 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 ly destroyed whe
12c6a 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a n it is closed..
12c6b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12c6c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
12c6d 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 eeOpen(. const
12c6e 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
12c6f 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
12c70 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 file containing
12c71 20 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62 the BTree datab
12c72 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ase */. sqlite3
12c73 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
12c74 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 /* Associated d
12c75 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
12c76 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 /. Btree **ppBt
12c77 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 ree, /* P
12c78 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 ointer to new Bt
12c79 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 ree object writt
12c7a 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 en here */. int
12c7b 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
12c7c 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 /* Options
12c7d 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 */. int vfsFlag
12c7e 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s /*
12c7f 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 Flags passed thr
12c80 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f ough to sqlite3_
12c81 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 vfs.xOpen() */.)
12c82 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
12c83 2a 70 56 66 73 3b 20 20 20 20 20 20 2f 2a 20 54 *pVfs; /* T
12c84 68 65 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f he VFS to use fo
12c85 72 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a r this btree */.
12c86 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
12c87 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 53 68 61 = 0; /* Sha
12c88 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 red part of btre
12c89 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
12c8a 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 Btree *p;
12c8b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
12c8c 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a le to return */.
12c8d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
12c8e 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 52 65 73 E_OK;. int nRes
12c8f 65 72 76 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 erve;. unsigned
12c90 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b char zDbHeader[
12c91 31 30 30 5d 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 100];.. /* Set
12c92 74 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d the variable isM
12c93 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 emdb to true for
12c94 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
12c95 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a tabase, or . **
12c96 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c false for a fil
12c97 65 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65 e-based database
12c98 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 . This symbol is
12c99 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 only required i
12c9a 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 f. ** either of
12c9b 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 the shared-data
12c9c 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 or autovacuum f
12c9d 65 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 eatures are comp
12c9e 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 iled . ** into
12c9f 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a the library.. *
12ca0 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
12ca1 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
12ca2 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 D_CACHE) || !def
12ca3 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
12ca4 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 _AUTOVACUUM). #
12ca5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
12ca6 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 T_MEMORYDB. c
12ca7 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 onst int isMemdb
12ca8 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 = 0;. #else.
12ca9 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 const int isMe
12caa 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 mdb = zFilename
12cab 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 && !strcmp(zFile
12cac 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 name, ":memory:"
12cad 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 );. #endif.#end
12cae 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 if.. assert( db
12caf 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
12cb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
12cb1 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
12cb2 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e ;.. pVfs = db->
12cb3 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 pVfs;. p = sqli
12cb4 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 te3MallocZero(si
12cb5 7a 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 zeof(Btree));.
12cb6 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 if( !p ){. re
12cb7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
12cb8 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 M;. }. p->inTr
12cb9 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 ans = TRANS_NONE
12cba 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a ;. p->db = db;.
12cbb 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
12cbc 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
12cbd 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 _CACHE) && !defi
12cbe 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
12cbf 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a DISKIO). /*. *
12cc0 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 65 20 * If this Btree
12cc1 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 is a candidate f
12cc2 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 65 2c or shared cache,
12cc3 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a try to find an.
12cc4 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 ** existing Bt
12cc5 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 Shared object th
12cc6 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 at we can share
12cc7 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 with. */. if(
12cc8 69 73 4d 65 6d 64 62 3d 3d 30 0a 20 20 20 26 26 isMemdb==0. &&
12cc9 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 (db->flags & SQ
12cca 4c 49 54 45 5f 56 74 61 62 29 3d 3d 30 0a 20 20 LITE_Vtab)==0.
12ccb 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 && zFilename &&
12ccc 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 0a 20 20 zFilename[0].
12ccd 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
12cce 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
12ccf 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 haredCacheEnable
12cd0 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e d ){. int n
12cd1 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 70 FullPathname = p
12cd2 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
12cd3 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 1;. char *z
12cd4 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 73 FullPathname = s
12cd5 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 46 75 qlite3Malloc(nFu
12cd6 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 llPathname);.
12cd7 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
12cd8 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20 *mutexShared;.
12cd9 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 p->sharable
12cda 20 3d 20 31 3b 0a 20 20 20 20 20 20 64 62 2d 3e = 1;. db->
12cdb 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f flags |= SQLITE_
12cdc 53 68 61 72 65 64 43 61 63 68 65 3b 0a 20 20 20 SharedCache;.
12cdd 20 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 if( !zFullPat
12cde 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 hname ){.
12cdf 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
12ce0 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
12ce1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
12ce2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
12ce3 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 ite3OsFullPathna
12ce4 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 me(pVfs, zFilena
12ce5 6d 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d me, nFullPathnam
12ce6 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e, zFullPathname
12ce7 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 );. mutexSh
12ce8 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 ared = sqlite3Mu
12ce9 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
12cea 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
12ceb 54 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 TER);. sqli
12cec 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
12ced 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 mutexShared);.
12cee 20 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 for(pBt=GLOB
12cef 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
12cf0 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
12cf1 69 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 ist); pBt; pBt=p
12cf2 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 Bt->pNext){.
12cf3 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d assert( pBt-
12cf4 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 >nRef>0 );.
12cf5 20 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 if( 0==strcmp
12cf6 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 (zFullPathname,
12cf7 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
12cf8 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 name(pBt->pPager
12cf9 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 )).
12cfa 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 && sqlite3Pa
12cfb 67 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67 gerVfs(pBt->pPag
12cfc 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 er)==pVfs ){.
12cfd 20 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 p->pBt =
12cfe 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 pBt;. p
12cff 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 Bt->nRef++;.
12d00 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
12d01 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
12d02 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
12d03 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 ex_leave(mutexSh
12d04 61 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c ared);. sql
12d05 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 ite3_free(zFullP
12d06 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a athname);. }.
12d07 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
12d08 42 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 BUG. else{.
12d09 20 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 /* In debug
12d0a 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c mode, we mark al
12d0b 6c 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 l persistent dat
12d0c 61 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62 abases as sharab
12d0d 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e le. ** even
12d0e 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e when they are n
12d0f 6f 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 ot. This exerci
12d10 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ses the locking
12d11 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a code and. *
12d12 2a 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 * gives more opp
12d13 6f 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 ortunity for ass
12d14 65 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 erts(sqlite3_mut
12d15 65 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 ex_held()).
12d16 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 ** statements t
12d17 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 o find locking p
12d18 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a roblems.. *
12d19 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 /. p->shara
12d1a 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 ble = 1;. }.#
12d1b 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 endif. }.#endif
12d1c 0a 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b . if( pBt==0 ){
12d1d 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 . /*. ** T
12d1e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 he following ass
12d1f 65 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 erts make sure t
12d20 68 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75 hat structures u
12d21 73 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 sed by the btree
12d22 20 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 are. ** the
12d23 72 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 right size. Thi
12d24 73 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 s is to guard ag
12d25 61 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 ainst size chang
12d26 65 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 es that result.
12d27 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 ** when compi
12d28 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 ling on a differ
12d29 65 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65 ent architecture
12d2a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
12d2b 65 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 ert( sizeof(i64)
12d2c 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 ==8 || sizeof(i6
12d2d 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 4)==4 );. ass
12d2e 65 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 ert( sizeof(u64)
12d2f 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 ==8 || sizeof(u6
12d30 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 4)==4 );. ass
12d31 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 ert( sizeof(u32)
12d32 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==4 );. asser
12d33 74 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d t( sizeof(u16)==
12d34 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 2 );. assert(
12d35 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 sizeof(Pgno)==4
12d36 20 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d );. . pBt =
12d37 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 sqlite3MallocZe
12d38 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 ro( sizeof(*pBt)
12d39 20 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d );. if( pBt=
12d3a 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d =0 ){. rc =
12d3b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
12d3c 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f goto btree_
12d3d 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a open_out;. }.
12d3e 20 20 20 20 70 42 74 2d 3e 62 75 73 79 48 64 72 pBt->busyHdr
12d3f 2e 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 .xFunc = sqlite3
12d40 42 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 BtreeInvokeBusyH
12d41 61 6e 64 6c 65 72 3b 0a 20 20 20 20 70 42 74 2d andler;. pBt-
12d42 3e 62 75 73 79 48 64 72 2e 70 41 72 67 20 3d 20 >busyHdr.pArg =
12d43 70 42 74 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 pBt;. rc = sq
12d44 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 lite3PagerOpen(p
12d45 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 Vfs, &pBt->pPage
12d46 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 r, zFilename,.
12d47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12d48 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 EXTRA_SI
12d49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c ZE, flags, vfsFl
12d4a 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ags);. if( rc
12d4b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
12d4c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
12d4d 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 3PagerReadFilehe
12d4e 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 ader(pBt->pPager
12d4f 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 65 ,sizeof(zDbHeade
12d50 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a 20 r),zDbHeader);.
12d51 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
12d52 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12d53 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f goto btree_o
12d54 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
12d55 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 sqlite3PagerS
12d56 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42 etBusyhandler(pB
12d57 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d t->pPager, &pBt-
12d58 3e 62 75 73 79 48 64 72 29 3b 0a 20 20 20 20 70 >busyHdr);. p
12d59 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a ->pBt = pBt;. .
12d5a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
12d5b 53 65 74 52 65 69 6e 69 74 65 72 28 70 42 74 2d SetReiniter(pBt-
12d5c 3e 70 50 61 67 65 72 2c 20 70 61 67 65 52 65 69 >pPager, pageRei
12d5d 6e 69 74 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 nit);. pBt->p
12d5e 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 Cursor = 0;.
12d5f 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b pBt->pPage1 = 0;
12d60 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e . pBt->readOn
12d61 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ly = sqlite3Page
12d62 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d rIsreadonly(pBt-
12d63 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 >pPager);. pB
12d64 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 t->pageSize = ge
12d65 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 t2byte(&zDbHeade
12d66 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 r[16]);. if(
12d67 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 pBt->pageSize<51
12d68 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 2 || pBt->pageSi
12d69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ze>SQLITE_MAX_PA
12d6a 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 GE_SIZE.
12d6b 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 || ((pBt->pageS
12d6c 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 ize-1)&pBt->page
12d6d 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Size)!=0 ){.
12d6e 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
12d6f 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 = 0;. sqlit
12d70 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 e3PagerSetPagesi
12d71 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ze(pBt->pPager,
12d72 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b &pBt->pageSize);
12d73 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
12d74 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
12d75 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
12d76 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d magic name ":mem
12d77 6f 72 79 3a 22 20 77 69 6c 6c 20 63 72 65 61 74 ory:" will creat
12d78 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 e an in-memory d
12d79 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 20 20 atabase, then.
12d7a 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 74 68 65 ** leave the
12d7b 20 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65 autoVacuum mode
12d7c 20 61 74 20 30 20 28 64 6f 20 6e 6f 74 20 61 75 at 0 (do not au
12d7d 74 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76 65 6e to-vacuum), even
12d7e 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c if. ** SQL
12d7f 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
12d80 56 41 43 55 55 4d 20 69 73 20 74 72 75 65 2e 20 VACUUM is true.
12d81 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
12d82 64 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 d, if. ** S
12d83 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 QLITE_OMIT_MEMOR
12d84 59 44 42 20 68 61 73 20 62 65 65 6e 20 64 65 66 YDB has been def
12d85 69 6e 65 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d ined, then ":mem
12d86 6f 72 79 3a 22 20 69 73 20 6a 75 73 74 20 61 0a ory:" is just a.
12d87 20 20 20 20 20 20 2a 2a 20 72 65 67 75 6c 61 72 ** regular
12d88 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74 file-name. In t
12d89 68 69 73 20 63 61 73 65 20 74 68 65 20 61 75 74 his case the aut
12d8a 6f 2d 76 61 63 75 75 6d 20 61 70 70 6c 69 65 73 o-vacuum applies
12d8b 20 61 73 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a as per normal..
12d8c 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
12d8d 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 f( zFilename &&
12d8e 21 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 !isMemdb ){.
12d8f 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 pBt->autoVac
12d90 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 uum = (SQLITE_DE
12d91 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d FAULT_AUTOVACUUM
12d92 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 ? 1 : 0);.
12d93 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 pBt->incrVacu
12d94 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 um = (SQLITE_DEF
12d95 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d AULT_AUTOVACUUM=
12d96 3d 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 =2 ? 1 : 0);.
12d97 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
12d98 20 20 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0a nReserve = 0;.
12d99 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
12d9a 20 6e 52 65 73 65 72 76 65 20 3d 20 7a 44 62 48 nReserve = zDbH
12d9b 65 61 64 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20 eader[20];.
12d9c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 pBt->pageSizeFi
12d9d 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 xed = 1;.#ifndef
12d9e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
12d9f 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 70 42 OVACUUM. pB
12da0 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 t->autoVacuum =
12da1 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 (get4byte(&zDbHe
12da2 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 5d 29 3f ader[36 + 4*4])?
12da3 31 3a 30 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 1:0);. pBt-
12da4 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 >incrVacuum = (g
12da5 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 et4byte(&zDbHead
12da6 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a er[36 + 7*4])?1:
12da7 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0);.#endif. }
12da8 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 . pBt->usable
12da9 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 Size = pBt->page
12daa 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b Size - nReserve;
12dab 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42 . assert( (pB
12dac 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 t->pageSize & 7)
12dad 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 ==0 ); /* 8-byt
12dae 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 e alignment of p
12daf 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 20 73 ageSize */. s
12db0 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
12db1 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 gesize(pBt->pPag
12db2 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 er, &pBt->pageSi
12db3 7a 65 29 3b 0a 20 20 20 0a 23 69 66 20 21 64 65 ze);. .#if !de
12db4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
12db5 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 T_SHARED_CACHE)
12db6 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
12db7 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a TE_OMIT_DISKIO).
12db8 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e /* Add the n
12db9 65 77 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 ew BtShared obje
12dba 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 ct to the linked
12dbb 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65 20 42 list sharable B
12dbc 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f tShareds.. */
12dbd 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 . if( p->shar
12dbe 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 able ){. sq
12dbf 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
12dc0 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 exShared;.
12dc1 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 pBt->nRef = 1;.
12dc2 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 mutexShared
12dc3 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
12dc4 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
12dc5 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
12dc6 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 ;. if( SQLI
12dc7 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
12dc8 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
12dc9 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 nfig.bCoreMutex
12dca 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e ){. pBt->
12dcb 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
12dcc 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
12dcd 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 _MUTEX_FAST);.
12dce 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d if( pBt->m
12dcf 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 utex==0 ){.
12dd0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
12dd1 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
12dd2 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
12dd3 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ed = 0;.
12dd4 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 goto btree_ope
12dd5 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d n_out;. }
12dd6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
12dd7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
12dd8 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b er(mutexShared);
12dd9 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78 . pBt->pNex
12dda 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 t = GLOBAL(BtSha
12ddb 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
12ddc 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 edCacheList);.
12ddd 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 GLOBAL(BtSha
12dde 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
12ddf 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 edCacheList) = p
12de0 42 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 Bt;. sqlite
12de1 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
12de2 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 texShared);.
12de3 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 }.#endif. }..#i
12de4 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
12de5 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
12de6 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 CHE) && !defined
12de7 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 (SQLITE_OMIT_DIS
12de8 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 KIO). /* If the
12de9 20 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20 new Btree uses
12dea 61 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 a sharable pBtSh
12deb 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 ared, then link
12dec 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 the new. ** Btr
12ded 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 ee into the list
12dee 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 of all sharable
12def 20 42 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 Btrees for the
12df0 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e same connection.
12df1 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 . ** The list i
12df2 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 s kept in ascend
12df3 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 ing order by pBt
12df4 20 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 address.. */.
12df5 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 if( p->sharable
12df6 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
12df7 20 20 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a Btree *pSib;.
12df8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 for(i=0; i<d
12df9 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
12dfa 20 20 20 20 69 66 28 20 28 70 53 69 62 20 3d 20 if( (pSib =
12dfb 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 db->aDb[i].pBt)!
12dfc 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72 =0 && pSib->shar
12dfd 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 able ){.
12dfe 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72 while( pSib->pPr
12dff 65 76 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69 ev ){ pSib = pSi
12e00 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20 b->pPrev; }.
12e01 20 20 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70 if( p->pBt<p
12e02 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 Sib->pBt ){.
12e03 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d p->pNext =
12e04 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 pSib;.
12e05 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 p->pPrev = 0;.
12e06 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 pSib->p
12e07 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 Prev = p;.
12e08 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
12e09 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e while( pSib->
12e0a 70 4e 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 pNext && pSib->p
12e0b 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 Next->pBt<p->pBt
12e0c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
12e0d 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 pSib = pSib->pNe
12e0e 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a xt;. }.
12e0f 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 p->pNe
12e10 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 xt = pSib->pNext
12e11 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 ;. p->p
12e12 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 Prev = pSib;.
12e13 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e if( p->pN
12e14 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ext ){.
12e15 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 p->pNext->pPr
12e16 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 ev = p;.
12e17 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 }. pS
12e18 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 ib->pNext = p;.
12e19 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12e1a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
12e1b 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
12e1c 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b . *ppBtree = p;
12e1d 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 ..btree_open_out
12e1e 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 :. if( rc!=SQLI
12e1f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
12e20 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 pBt && pBt->pPa
12e21 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ger ){. sql
12e22 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 ite3PagerClose(p
12e23 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
12e24 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 }. sqlite3_f
12e25 72 65 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 ree(pBt);. sq
12e26 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
12e27 20 20 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b *ppBtree = 0;
12e28 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
12e29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 ;.}../*.** Decre
12e2a 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 65 ment the BtShare
12e2b 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 d.nRef counter.
12e2c 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 When it reaches
12e2d 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 zero,.** remove
12e2e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 the BtShared st
12e2f 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65 ructure from the
12e30 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 sharing list.
12e31 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 Return.** true i
12e32 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e f the BtShared.n
12e33 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63 Ref counter reac
12e34 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 hes zero and ret
12e35 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 urn.** false if
12e36 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 it is still posi
12e37 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tive..*/.static
12e38 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 int removeFromSh
12e39 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72 aringList(BtShar
12e3a 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 ed *pBt){.#ifnde
12e3b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
12e3c 41 52 45 44 5f 43 41 43 48 45 0a 20 20 73 71 6c ARED_CACHE. sql
12e3d 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 ite3_mutex *pMas
12e3e 74 65 72 3b 0a 20 20 42 74 53 68 61 72 65 64 20 ter;. BtShared
12e3f 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 *pList;. int re
12e40 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 moved = 0;.. as
12e41 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
12e42 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d tex_notheld(pBt-
12e43 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 61 >mutex) );. pMa
12e44 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 ster = sqlite3Mu
12e45 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
12e46 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
12e47 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f TER);. sqlite3_
12e48 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 mutex_enter(pMas
12e49 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 ter);. pBt->nRe
12e4a 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e f--;. if( pBt->
12e4b 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 nRef<=0 ){. i
12e4c 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 f( GLOBAL(BtShar
12e4d 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 ed*,sqlite3Share
12e4e 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 dCacheList)==pBt
12e4f 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c ){. GLOBAL
12e50 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 (BtShared*,sqlit
12e51 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
12e52 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b t) = pBt->pNext;
12e53 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
12e54 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c pList = GLOBAL
12e55 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 (BtShared*,sqlit
12e56 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
12e57 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 t);. while(
12e58 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 26 ALWAYS(pList) &
12e59 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d & pList->pNext!=
12e5a 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 pBt ){. p
12e5b 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 List=pList->pNex
12e5c 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
12e5d 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c 69 73 if( ALWAYS(pLis
12e5e 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c t) ){. pL
12e5f 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 ist->pNext = pBt
12e60 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
12e61 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53 . }. if( S
12e62 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
12e63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
12e64 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 74 3_mutex_free(pBt
12e65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a ->mutex);. }.
12e66 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b removed = 1;
12e67 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
12e68 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 utex_leave(pMast
12e69 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 er);. return re
12e6a 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 moved;.#else. r
12e6b 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a eturn 1;.#endif.
12e6c 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 }../*.** Make su
12e6d 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 re pBt->pTmpSpac
12e6e 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 e points to an a
12e6f 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a llocation of .**
12e70 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 MX_CELL_SIZE(pB
12e71 74 29 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 t) bytes..*/.sta
12e72 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 tic void allocat
12e73 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 eTempSpace(BtSha
12e74 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 red *pBt){. if(
12e75 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 !pBt->pTmpSpace
12e76 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d ){. pBt->pTm
12e77 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 pSpace = sqlite3
12e78 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d PageMalloc( pBt-
12e79 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d >pageSize );. }
12e7a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 .}../*.** Free t
12e7b 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 he pBt->pTmpSpac
12e7c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a e allocation.*/.
12e7d 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 static void free
12e7e 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 TempSpace(BtShar
12e7f 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 ed *pBt){. sqli
12e80 74 65 33 50 61 67 65 46 72 65 65 28 20 70 42 74 te3PageFree( pBt
12e81 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
12e82 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d pBt->pTmpSpace =
12e83 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 0;.}../*.** Clo
12e84 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 se an open datab
12e85 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 ase and invalida
12e86 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a te all cursors..
12e87 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12e88 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
12e89 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 eeClose(Btree *p
12e8a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
12e8b 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
12e8c 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a tCursor *pCur;..
12e8d 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 /* Close all c
12e8e 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 ursors opened vi
12e8f 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 a this handle.
12e90 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c */. assert( sql
12e91 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
12e92 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
12e93 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
12e94 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e nter(p);. pBt->
12e95 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 70 43 db = p->db;. pC
12e96 75 72 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f ur = pBt->pCurso
12e97 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 r;. while( pCur
12e98 20 29 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 ){. BtCursor
12e99 20 2a 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 *pTmp = pCur;.
12e9a 20 20 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e pCur = pCur->
12e9b 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 pNext;. if( p
12e9c 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 Tmp->pBtree==p )
12e9d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 {. sqlite3B
12e9e 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 treeCloseCursor(
12e9f 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d pTmp);. }. }
12ea0 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 .. /* Rollback
12ea1 61 6e 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 any active trans
12ea2 61 63 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 action and free
12ea3 74 68 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 the handle struc
12ea4 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 ture.. ** The c
12ea5 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 all to sqlite3Bt
12ea6 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 reeRollback() dr
12ea7 6f 70 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f ops any table-lo
12ea8 63 6b 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a cks held by. **
12ea9 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 this handle..
12eaa 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 */. sqlite3Btre
12eab 65 52 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 eRollback(p);.
12eac 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
12ead 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 e(p);.. /* If t
12eae 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f here are still o
12eaf 74 68 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 ther outstanding
12eb0 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
12eb1 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a he shared-btree.
12eb2 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 ** structure,
12eb3 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 return now. The
12eb4 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 remainder of thi
12eb5 73 20 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61 s procedure clea
12eb6 6e 73 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 ns . ** up the
12eb7 73 68 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 shared-btree..
12eb8 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e */. assert( p->
12eb9 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 wantToLock==0 &&
12eba 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b p->locked==0 );
12ebb 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 . if( !p->shara
12ebc 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f ble || removeFro
12ebd 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74 mSharingList(pBt
12ebe 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 ) ){. /* The
12ebf 70 42 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 pBt is no longer
12ec0 20 6f 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20 on the sharing
12ec1 6c 69 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 list, so we can
12ec2 61 63 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 access. ** it
12ec3 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
12ec4 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 to hold the mute
12ec5 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a x.. **. **
12ec6 20 43 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 Clean out and d
12ec7 65 6c 65 74 65 20 74 68 65 20 42 74 53 68 61 72 elete the BtShar
12ec8 65 64 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a ed object.. *
12ec9 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 /. assert( !p
12eca 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 Bt->pCursor );.
12ecb 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 sqlite3PagerC
12ecc 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 lose(pBt->pPager
12ecd 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e );. if( pBt->
12ece 78 46 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 xFreeSchema && p
12ecf 42 74 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 Bt->pSchema ){.
12ed0 20 20 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 pBt->xFreeS
12ed1 63 68 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 chema(pBt->pSche
12ed2 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ma);. }. s
12ed3 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d qlite3_free(pBt-
12ed4 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 >pSchema);. f
12ed5 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 reeTempSpace(pBt
12ed6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
12ed7 72 65 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 ree(pBt);. }..#
12ed8 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
12ed9 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
12eda 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e assert( p->wan
12edb 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 tToLock==0 );.
12edc 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 assert( p->locke
12edd 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d d==0 );. if( p-
12ede 3e 70 50 72 65 76 20 29 20 70 2d 3e 70 50 72 65 >pPrev ) p->pPre
12edf 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e v->pNext = p->pN
12ee0 65 78 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e ext;. if( p->pN
12ee1 65 78 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e ext ) p->pNext->
12ee2 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 pPrev = p->pPrev
12ee3 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 ;.#endif.. sqli
12ee4 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 te3_free(p);. r
12ee5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
12ee6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
12ee7 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 the limit on th
12ee8 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
12ee9 73 20 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 s allowed in the
12eea 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 cache..**.** Th
12eeb 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
12eec 20 6f 66 20 63 61 63 68 65 20 70 61 67 65 73 20 of cache pages
12eed 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 61 62 is set to the ab
12eee 73 6f 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 solute.** value
12eef 6f 66 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d of mxPage. If m
12ef0 78 50 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 xPage is negativ
12ef1 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 69 6c e, the pager wil
12ef2 6c 0a 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79 l.** operate asy
12ef3 6e 63 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 nchronously - it
12ef4 20 77 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 will not stop t
12ef5 6f 20 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a o do fsync()s.**
12ef6 20 74 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20 to insure data
12ef7 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 is written to th
12ef8 65 20 64 69 73 6b 20 73 75 72 66 61 63 65 20 62 e disk surface b
12ef9 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 efore.** continu
12efa 69 6e 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f ing. Transactio
12efb 6e 73 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 ns still work if
12efc 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 synchronous is
12efd 6f 66 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 off,.** and the
12efe 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 database cannot
12eff 62 65 20 63 6f 72 72 75 70 74 65 64 20 69 66 20 be corrupted if
12f00 74 68 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 this program.**
12f01 63 72 61 73 68 65 73 2e 20 20 42 75 74 20 69 66 crashes. But if
12f02 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
12f03 79 73 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72 ystem crashes or
12f04 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 there is.** an
12f05 61 62 72 75 70 74 20 70 6f 77 65 72 20 66 61 69 abrupt power fai
12f06 6c 75 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72 lure when synchr
12f07 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68 onous is off, th
12f08 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
12f09 75 6c 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61 uld be left in a
12f0a 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 n inconsistent a
12f0b 6e 64 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 nd unrecoverable
12f0c 20 73 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 state..** Synch
12f0d 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 ronous is on by
12f0e 64 65 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62 default so datab
12f0f 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 ase corruption i
12f10 73 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c s not.** normall
12f11 79 20 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 y a worry..*/.SQ
12f12 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
12f13 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
12f14 43 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 20 CacheSize(Btree
12f15 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b *p, int mxPage){
12f16 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
12f17 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 = p->pBt;. ass
12f18 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
12f19 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
12f1a 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
12f1b 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
12f1c 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 . sqlite3PagerS
12f1d 65 74 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d etCachesize(pBt-
12f1e 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 >pPager, mxPage)
12f1f 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
12f20 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
12f21 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
12f22 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
12f23 65 20 77 61 79 20 64 61 74 61 20 69 73 20 73 79 e way data is sy
12f24 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 nced to disk in
12f25 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73 order to increas
12f26 65 20 6f 72 20 64 65 63 72 65 61 73 65 0a 2a 2a e or decrease.**
12f27 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61 how well the da
12f28 74 61 62 61 73 65 20 72 65 73 69 73 74 73 20 64 tabase resists d
12f29 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 amage due to OS
12f2a 63 72 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65 crashes and powe
12f2b 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 r.** failures.
12f2c 4c 65 76 65 6c 20 31 20 69 73 20 74 68 65 20 73 Level 1 is the s
12f2d 61 6d 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e ame as asynchron
12f2e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 ous (no syncs()
12f2f 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 occur and.** the
12f30 72 65 20 69 73 20 61 20 68 69 67 68 20 70 72 6f re is a high pro
12f31 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 bability of dama
12f32 67 65 29 20 20 4c 65 76 65 6c 20 32 20 69 73 20 ge) Level 2 is
12f33 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 the default. Th
12f34 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 ere.** is a very
12f35 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 low but non-zer
12f36 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 o probability of
12f37 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 damage. Level
12f38 33 20 72 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 3 reduces the.**
12f39 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 probability of
12f3a 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 7a damage to near z
12f3b 65 72 6f 20 62 75 74 20 77 69 74 68 20 61 20 77 ero but with a w
12f3c 72 69 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 rite performance
12f3d 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 reduction..*/.#
12f3e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
12f3f 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 IT_PAGER_PRAGMAS
12f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12f41 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
12f42 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 SetSafetyLevel(B
12f43 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 tree *p, int lev
12f44 65 6c 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 el, int fullSync
12f45 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
12f46 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 Bt = p->pBt;. a
12f47 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
12f48 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
12f49 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
12f4a 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
12f4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 );. sqlite3Page
12f4c 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 rSetSafetyLevel(
12f4d 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 pBt->pPager, lev
12f4e 65 6c 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 el, fullSync);.
12f4f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
12f50 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
12f51 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e SQLITE_OK;.}.#en
12f52 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 dif../*.** Retur
12f53 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 n TRUE if the gi
12f54 76 65 6e 20 62 74 72 65 65 20 69 73 20 73 65 74 ven btree is set
12f55 20 74 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c to safety level
12f56 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 1. In other.**
12f57 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 words, return T
12f58 52 55 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 RUE if no sync()
12f59 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64 occurs on the d
12f5a 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 isk files..*/.SQ
12f5b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
12f5c 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e sqlite3BtreeSyn
12f5d 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 20 cDisabled(Btree
12f5e 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 *p){. BtShared
12f5f 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
12f60 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
12f61 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
12f62 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
12f63 65 78 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 ex) ); . sqlit
12f64 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
12f65 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 20 26 . assert( pBt &
12f66 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b & pBt->pPager );
12f67 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
12f68 61 67 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e agerNosync(pBt->
12f69 70 50 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 pPager);. sqlit
12f6a 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
12f6b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
12f6c 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
12f6d 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f LITE_OMIT_PAGER_
12f6e 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 PRAGMAS) || !def
12f6f 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
12f70 5f 56 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 _VACUUM)./*.** C
12f71 68 61 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c hange the defaul
12f72 74 20 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 t pages size and
12f73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
12f74 65 73 65 72 76 65 64 20 62 79 74 65 73 20 70 65 eserved bytes pe
12f75 72 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 r page..**.** Th
12f76 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 e page size must
12f77 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 be a power of 2
12f78 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 between 512 and
12f79 20 36 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 65536. If the
12f7a 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 page.** size sup
12f7b 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d plied does not m
12f7c 65 65 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 eet this constra
12f7d 69 6e 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 int then the pag
12f7e 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a e size is not.**
12f7f 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 changed..**.**
12f80 50 61 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 Page sizes are c
12f81 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 onstrained to be
12f82 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 a power of two
12f83 73 6f 20 74 68 61 74 20 74 68 65 20 72 65 67 69 so that the regi
12f84 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 on.** of the dat
12f85 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 abase file used
12f86 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 for locking (beg
12f87 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e inning at PENDIN
12f88 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 G_BYTE,.** the f
12f89 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 irst byte past t
12f8a 68 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c he 1GB boundary,
12f8b 20 30 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 0x40000000) nee
12f8c 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 ds to occur.** a
12f8d 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
12f8e 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a of a page..**.**
12f8f 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 If parameter nR
12f90 65 73 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 eserve is less t
12f91 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 han zero, then t
12f92 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 he number of res
12f93 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 erved.** bytes p
12f94 65 72 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 er page is left
12f95 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 unchanged..*/.SQ
12f96 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
12f97 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
12f98 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a PageSize(Btree *
12f99 70 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c p, int pageSize,
12f9a 20 69 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a int nReserve){.
12f9b 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
12f9c 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 E_OK;. BtShared
12f9d 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
12f9e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
12f9f 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42 ter(p);. if( pB
12fa0 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 t->pageSizeFixed
12fa1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
12fa2 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
12fa3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12fa4 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 READONLY;. }.
12fa5 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29 if( nReserve<0 )
12fa6 7b 0a 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d {. nReserve =
12fa7 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d pBt->pageSize -
12fa8 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
12fa9 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 61 67 65 ;. }. if( page
12faa 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 Size>=512 && pag
12fab 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 eSize<=SQLITE_MA
12fac 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26 0a 20 X_PAGE_SIZE &&.
12fad 20 20 20 20 20 20 20 28 28 70 61 67 65 53 69 7a ((pageSiz
12fae 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 3d 3d e-1)&pageSize)==
12faf 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
12fb0 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d (pageSize & 7)=
12fb1 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
12fb2 28 20 21 70 42 74 2d 3e 70 50 61 67 65 31 20 26 ( !pBt->pPage1 &
12fb3 26 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 & !pBt->pCursor
12fb4 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 );. pBt->page
12fb5 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b Size = pageSize;
12fb6 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 . freeTempSpa
12fb7 63 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20 ce(pBt);. rc
12fb8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
12fb9 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 tPagesize(pBt->p
12fba 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 Pager, &pBt->pag
12fbb 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 70 42 eSize);. }. pB
12fbc 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 t->usableSize =
12fbd 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
12fbe 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 nReserve;. sqli
12fbf 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
12fc0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
12fc1 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
12fc2 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 he currently def
12fc3 69 6e 65 64 20 70 61 67 65 20 73 69 7a 65 0a 2a ined page size.*
12fc4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
12fc5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
12fc6 65 47 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 eGetPageSize(Btr
12fc7 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ee *p){. return
12fc8 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a p->pBt->pageSiz
12fc9 65 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 e;.}.SQLITE_PRIV
12fca 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
12fcb 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 treeGetReserve(B
12fcc 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 tree *p){. int
12fcd 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 n;. sqlite3Btre
12fce 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d eEnter(p);. n =
12fcf 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a p->pBt->pageSiz
12fd0 65 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 e - p->pBt->usab
12fd1 6c 65 53 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 leSize;. sqlite
12fd2 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
12fd3 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f return n;.}../
12fd4 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 61 78 *.** Set the max
12fd5 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 imum page count
12fd6 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 69 for a database i
12fd7 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 f mxPage is posi
12fd8 74 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e tive..** No chan
12fd9 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 66 20 ges are made if
12fda 6d 78 50 61 67 65 20 69 73 20 30 20 6f 72 20 6e mxPage is 0 or n
12fdb 65 67 61 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 egative..** Rega
12fdc 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 61 rdless of the va
12fdd 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 lue of mxPage, r
12fde 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 eturn the maximu
12fdf 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f m page count..*/
12fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12fe1 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
12fe2 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 MaxPageCount(Btr
12fe3 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 ee *p, int mxPag
12fe4 65 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 e){. int n;. s
12fe5 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
12fe6 28 70 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 (p);. n = sqlit
12fe7 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f e3PagerMaxPageCo
12fe8 75 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 unt(p->pBt->pPag
12fe9 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 er, mxPage);. s
12fea 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
12feb 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b (p);. return n;
12fec 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 .}.#endif /* !de
12fed 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
12fee 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 T_PAGER_PRAGMAS)
12fef 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
12ff0 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 ITE_OMIT_VACUUM)
12ff1 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 */../*.** Chang
12ff2 65 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 e the 'auto-vacu
12ff3 75 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66 20 um' property of
12ff4 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 the database. If
12ff5 20 74 68 65 20 27 61 75 74 6f 56 61 63 75 75 6d the 'autoVacuum
12ff6 27 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 '.** parameter i
12ff7 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
12ff8 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 auto-vacuum mod
12ff9 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 e is enabled. If
12ffa 20 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 zero, it.** is
12ffb 64 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 65 disabled. The de
12ffc 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 fault value for
12ffd 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 the auto-vacuum
12ffe 70 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 property is .**
12fff 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 determined by th
13000 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
13001 5f 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 _AUTOVACUUM macr
13002 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 o..*/.SQLITE_PRI
13003 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
13004 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 BtreeSetAutoVacu
13005 75 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 um(Btree *p, int
13006 20 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 autoVacuum){.#i
13007 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
13008 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 _AUTOVACUUM. re
13009 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 turn SQLITE_READ
1300a 4f 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 ONLY;.#else. Bt
1300b 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
1300c 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d >pBt;. int rc =
1300d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
1300e 74 20 61 76 20 3d 20 28 61 75 74 6f 56 61 63 75 t av = (autoVacu
1300f 75 6d 3f 31 3a 30 29 3b 0a 0a 20 20 73 71 6c 69 um?1:0);.. sqli
13010 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
13011 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 ;. if( pBt->pag
13012 65 53 69 7a 65 46 69 78 65 64 20 26 26 20 61 76 eSizeFixed && av
13013 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 !=pBt->autoVacuu
13014 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 m ){. rc = SQ
13015 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 LITE_READONLY;.
13016 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d }else{. pBt-
13017 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 >autoVacuum = av
13018 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 ;. }. sqlite3B
13019 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
1301a 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 return rc;.#endi
1301b 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 f.}../*.** Retur
1301c 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 n the value of t
1301d 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 he 'auto-vacuum'
1301e 20 70 72 6f 70 65 72 74 79 2e 20 49 66 20 61 75 property. If au
1301f 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 0a 2a 2a to-vacuum is .**
13020 20 65 6e 61 62 6c 65 64 20 31 20 69 73 20 72 65 enabled 1 is re
13021 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 turned. Otherwis
13022 65 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 e 0..*/.SQLITE_P
13023 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13024 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 e3BtreeGetAutoVa
13025 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b 0a cuum(Btree *p){.
13026 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
13027 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
13028 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54 return BTREE_AUT
13029 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 23 65 OVACUUM_NONE;.#e
1302a 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 lse. int rc;.
1302b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1302c 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 0a 20 r(p);. rc = (.
1302d 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 61 75 74 (!p->pBt->aut
1302e 6f 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f 41 oVacuum)?BTREE_A
1302f 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3a 0a UTOVACUUM_NONE:.
13030 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 69 6e (!p->pBt->in
13031 63 72 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f crVacuum)?BTREE_
13032 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3a AUTOVACUUM_FULL:
13033 0a 20 20 20 20 42 54 52 45 45 5f 41 55 54 4f 56 . BTREE_AUTOV
13034 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20 29 3b 0a ACUUM_INCR. );.
13035 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
13036 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
13037 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a rc;.#endif.}...
13038 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72 65 66 65 /*.** Get a refe
13039 72 65 6e 63 65 20 74 6f 20 70 50 61 67 65 31 20 rence to pPage1
1303a 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1303b 66 69 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c file. This will
1303c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75 69 72 65 .** also acquire
1303d 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f 6e 20 74 a readlock on t
1303e 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 hat file..**.**
1303f 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
13040 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 urned on success
13041 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 . If the file i
13042 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65 6c 6c 2d s not a.** well-
13043 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 formed database
13044 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 file, then SQLIT
13045 45 5f 43 4f 52 52 55 50 54 20 69 73 20 72 65 74 E_CORRUPT is ret
13046 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c 49 54 45 urned..** SQLITE
13047 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 _BUSY is returne
13048 64 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 d if the databas
13049 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 53 51 e is locked. SQ
1304a 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 73 LITE_NOMEM.** is
1304b 20 72 65 74 75 72 6e 65 64 20 69 66 20 77 65 20 returned if we
1304c 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 run out of memor
1304d 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e y. .*/.static in
1304e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42 74 53 68 t lockBtree(BtSh
1304f 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e ared *pBt){. in
13050 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 t rc;. MemPage
13051 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 6e *pPage1;. int n
13052 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
13053 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
13054 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
13055 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50 );. if( pBt->pP
13056 61 67 65 31 20 29 20 72 65 74 75 72 6e 20 53 51 age1 ) return SQ
13057 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d 20 LITE_OK;. rc =
13058 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
13059 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 age(pBt, 1, &pPa
1305a 67 65 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 ge1, 0);. if( r
1305b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
1305c 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 eturn rc;.. /*
1305d 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 Do some checking
1305e 20 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 to help insure
1305f 74 68 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e the file we open
13060 65 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a ed really is. *
13061 2a 20 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 * a valid databa
13062 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 se file. . */.
13063 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
13064 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
13065 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 >pPager, &nPage)
13066 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
13067 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 TE_OK ){. got
13068 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 o page1_init_fai
13069 6c 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 led;. }else if(
1306a 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 nPage>0 ){.
1306b 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 int pageSize;.
1306c 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 int usableSize
1306d 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 65 31 20 ;. u8 *page1
1306e 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b = pPage1->aData;
1306f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
13070 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 69 66 28 _NOTADB;. if(
13071 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a memcmp(page1, z
13072 4d 61 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 MagicHeader, 16)
13073 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 !=0 ){. got
13074 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 o page1_init_fai
13075 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 led;. }. i
13076 66 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 f( page1[18]>1 )
13077 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61 {. pBt->rea
13078 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d dOnly = 1;. }
13079 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 . if( page1[1
1307a 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 9]>1 ){. go
1307b 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 to page1_init_fa
1307c 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 iled;. }..
1307d 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 /* The maximum
1307e 65 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f embedded fractio
1307f 6e 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c n must be exactl
13080 79 20 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20 y 25%. And the
13081 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 minimum. ** e
13082 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e mbedded fraction
13083 20 6d 75 73 74 20 62 65 20 31 32 2e 35 25 20 66 must be 12.5% f
13084 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 or both leaf-dat
13085 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 a and non-leaf-d
13086 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 ata.. ** The
13087 6f 72 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 original design
13088 61 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d allowed these am
13089 6f 75 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62 ounts to vary, b
1308a 75 74 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 ut as of. **
1308b 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 version 3.6.0, w
1308c 65 20 72 65 71 75 69 72 65 20 74 68 65 6d 20 74 e require them t
1308d 6f 20 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20 o be fixed..
1308e 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d */. if( memcm
1308f 70 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c p(&page1[21], "\
13090 31 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 100\040\040",3)!
13091 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f =0 ){. goto
13092 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
13093 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 ed;. }. pa
13094 67 65 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 geSize = get2byt
13095 65 28 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 e(&page1[16]);.
13096 20 20 20 69 66 28 20 28 28 70 61 67 65 53 69 7a if( ((pageSiz
13097 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d e-1)&pageSize)!=
13098 30 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 0 || pageSize<51
13099 32 20 7c 7c 0a 20 20 20 20 20 20 20 20 28 53 51 2 ||. (SQ
1309a 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
1309b 5a 45 3c 33 32 37 36 38 20 26 26 20 70 61 67 65 ZE<32768 && page
1309c 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f Size>SQLITE_MAX_
1309d 50 41 47 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 PAGE_SIZE). )
1309e 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 {. goto pag
1309f 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a e1_init_failed;.
130a0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
130a1 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 ( (pageSize & 7)
130a2 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73 61 62 6c ==0 );. usabl
130a3 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 eSize = pageSize
130a4 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 - page1[20];.
130a5 20 20 69 66 28 20 70 61 67 65 53 69 7a 65 21 3d if( pageSize!=
130a6 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b pBt->pageSize ){
130a7 0a 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 . /* After
130a8 72 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 reading the firs
130a9 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 t page of the da
130aa 74 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 tabase assuming
130ab 61 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 a page size.
130ac 20 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 ** of BtShared
130ad 2e 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 .pageSize, we ha
130ae 76 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 ve discovered th
130af 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 at the page-size
130b0 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 is. ** act
130b1 75 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 ually pageSize.
130b2 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 Unlock the datab
130b3 61 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e ase, leave pBt->
130b4 70 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 pPage1 at.
130b5 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 ** zero and retu
130b6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 rn SQLITE_OK. Th
130b7 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 e caller will ca
130b8 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ll this function
130b9 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 . ** again
130ba 77 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 with the correct
130bb 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 page-size..
130bc 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 */. relea
130bd 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a sePage(pPage1);.
130be 20 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c pBt->usabl
130bf 65 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 53 69 eSize = usableSi
130c0 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 ze;. pBt->p
130c1 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 ageSize = pageSi
130c2 7a 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 65 ze;. freeTe
130c3 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 mpSpace(pBt);.
130c4 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
130c5 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d SetPagesize(pBt-
130c6 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 >pPager, &pBt->p
130c7 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 ageSize);.
130c8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
130c9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
130ca 75 73 61 62 6c 65 53 69 7a 65 3c 35 30 30 20 29 usableSize<500 )
130cb 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 {. goto pag
130cc 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a e1_init_failed;.
130cd 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 }. pBt->p
130ce 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 ageSize = pageSi
130cf 7a 65 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 ze;. pBt->usa
130d0 62 6c 65 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 bleSize = usable
130d1 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 Size;.#ifndef SQ
130d2 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
130d3 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 CUUM. pBt->au
130d4 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 toVacuum = (get4
130d5 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b byte(&page1[36 +
130d6 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 4*4])?1:0);.
130d7 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d pBt->incrVacuum
130d8 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 = (get4byte(&pa
130d9 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 ge1[36 + 7*4])?1
130da 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a :0);.#endif. }.
130db 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 . /* maxLocal i
130dc 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d s the maximum am
130dd 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 ount of payload
130de 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 to store locally
130df 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c for. ** a cell
130e0 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 . Make sure it
130e1 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 is small enough
130e2 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 so that at least
130e3 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 minFanout. **
130e4 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 cells can will f
130e5 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 it on one page.
130e6 20 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d We assume a 10-
130e7 62 79 74 65 20 70 61 67 65 20 68 65 61 64 65 72 byte page header
130e8 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74 .. ** Besides t
130e9 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 he payload, the
130ea 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a cell must store:
130eb 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 . ** 2-byte
130ec 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
130ed 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d cell. ** 4-
130ee 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 byte child point
130ef 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 er. ** 9-by
130f0 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 te nKey value.
130f1 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44 ** 4-byte nD
130f2 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 ata value. **
130f3 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 4-byte overfl
130f4 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a ow page pointer.
130f5 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 ** So a cell c
130f6 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62 onsists of a 2-b
130f7 79 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 yte poiner, a he
130f8 61 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73 ader which is as
130f9 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 much as. ** 17
130fa 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 bytes long, 0 t
130fb 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79 o N bytes of pay
130fc 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 load, and an opt
130fd 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65 ional 4 byte ove
130fe 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 rflow. ** page
130ff 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 pointer.. */.
13100 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 pBt->maxLocal =
13101 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
13102 2d 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 -12)*64/255 - 23
13103 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 ;. pBt->minLoca
13104 6c 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 l = (pBt->usable
13105 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 Size-12)*32/255
13106 2d 20 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 - 23;. pBt->max
13107 4c 65 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 Leaf = pBt->usab
13108 6c 65 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 leSize - 35;. p
13109 42 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 Bt->minLeaf = (p
1310a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 Bt->usableSize-1
1310b 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 2)*32/255 - 23;.
1310c 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d assert( pBt->m
1310d 61 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d axLeaf + 23 <= M
1310e 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 X_CELL_SIZE(pBt)
1310f 20 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 );. pBt->pPage
13110 31 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65 1 = pPage1;. re
13111 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
13112 0a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c .page1_init_fail
13113 65 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 ed:. releasePag
13114 65 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74 e(pPage1);. pBt
13115 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 ->pPage1 = 0;.
13116 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
13117 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
13118 20 77 6f 72 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b works like lock
13119 42 74 72 65 65 28 29 20 65 78 63 65 70 74 20 74 Btree() except t
1311a 68 61 74 20 69 74 20 61 6c 73 6f 20 69 6e 76 6f hat it also invo
1311b 6b 65 73 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 kes the.** busy
1311c 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 callback if ther
1311d 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e e is lock conten
1311e 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
1311f 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 int lockBtreeWit
13120 68 52 65 74 72 79 28 42 74 72 65 65 20 2a 70 52 hRetry(Btree *pR
13121 65 66 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ef){. int rc =
13122 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 SQLITE_OK;.. as
13123 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
13124 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 52 65 eeHoldsMutex(pRe
13125 66 29 20 29 3b 0a 20 20 69 66 28 20 70 52 65 66 f) );. if( pRef
13126 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
13127 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 75 38 20 _NONE ){. u8
13128 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
13129 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 pRef->pBt->inTra
1312a 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 62 74 nsaction;. bt
1312b 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 52 65 reeIntegrity(pRe
1312c 66 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c f);. rc = sql
1312d 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 ite3BtreeBeginTr
1312e 61 6e 73 28 70 52 65 66 2c 20 30 29 3b 0a 20 20 ans(pRef, 0);.
1312f 20 20 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 pRef->pBt->inT
13130 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 69 6e 54 ransaction = inT
13131 72 61 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 ransaction;.
13132 70 52 65 66 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 pRef->inTrans =
13133 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 TRANS_NONE;.
13134 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
13135 4b 20 29 7b 0a 20 20 20 20 20 20 70 52 65 66 2d K ){. pRef-
13136 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 >pBt->nTransacti
13137 6f 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 on--;. }.
13138 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
13139 52 65 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Ref);. }. retu
1313a 72 6e 20 72 63 3b 0a 7d 0a 20 20 20 20 20 20 20 rn rc;.}.
1313b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ../*.** If there
1313c 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 are no outstand
1313d 69 6e 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 ing cursors and
1313e 77 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 we are not in th
1313f 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 e middle.** of a
13140 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 transaction but
13141 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64 there is a read
13142 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
13143 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 abase, then.** t
13144 68 69 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 his routine unre
13145 66 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 fs the first pag
13146 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
13147 65 20 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a e file which .**
13148 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 has the effect
13149 6f 66 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 of releasing the
1314a 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a read lock..**.*
1314b 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 * If there are a
1314c 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 ny outstanding c
1314d 75 72 73 6f 72 73 2c 20 74 68 69 73 20 72 6f 75 ursors, this rou
1314e 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tine is a no-op.
1314f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
13150 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e is a transaction
13151 20 69 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 in progress, th
13152 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
13153 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
13154 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 void unlockBtre
13155 65 49 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 eIfUnused(BtShar
13156 65 64 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 ed *pBt){. asse
13157 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
13158 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
13159 78 29 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d x) );. if( pBt-
1315a 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
1315b 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 TRANS_NONE && pB
1315c 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 26 26 t->pCursor==0 &&
1315d 20 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 pBt->pPage1!=0
1315e 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
1315f 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 e3PagerRefcount(
13160 70 42 74 2d 3e 70 50 61 67 65 72 29 3e 3d 31 20 pBt->pPager)>=1
13161 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
13162 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 pBt->pPage1->aD
13163 61 74 61 20 29 3b 0a 23 69 66 20 30 0a 20 20 20 ata );.#if 0.
13164 20 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 if( pBt->pPag
13165 65 31 2d 3e 61 44 61 74 61 3d 3d 30 20 29 7b 0a e1->aData==0 ){.
13166 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 MemPage
13167 2a 70 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 50 *pPage = pBt->pP
13168 61 67 65 31 3b 0a 20 20 20 20 20 20 20 20 70 50 age1;. pP
13169 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c age->aData = sql
1316a 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
1316b 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
1316c 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 2d ;. pPage-
1316d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 >pBt = pBt;.
1316e 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 pPage->pgno
1316f 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e = 1;. }.#en
13170 64 69 66 0a 20 20 20 20 20 20 72 65 6c 65 61 73 dif. releas
13171 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 ePage(pBt->pPage
13172 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 1);. }. pB
13173 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 t->pPage1 = 0;.
13174 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d pBt->inStmt =
13175 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0;. }.}../*.**
13176 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 64 61 Create a new da
13177 74 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 61 tabase by initia
13178 6c 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 74 lizing the first
13179 20 70 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 page of the.**
1317a 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1317b 69 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28 int newDatabase(
1317c 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
1317d 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a MemPage *pP1;.
1317e 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
1317f 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b *data;. int rc;
13180 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 . int nPage;..
13181 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
13182 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
13183 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 >mutex) );. rc
13184 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 = sqlite3PagerPa
13185 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 gecount(pBt->pPa
13186 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 ger, &nPage);.
13187 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
13188 4b 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a K || nPage>0 ){.
13189 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1318a 20 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e }. pP1 = pBt->
1318b 70 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 pPage1;. assert
1318c 28 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 ( pP1!=0 );. da
1318d 74 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b ta = pP1->aData;
1318e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
1318f 61 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 agerWrite(pP1->p
13190 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
13191 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
13192 20 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d memcpy(data, zM
13193 61 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 agicHeader, size
13194 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 of(zMagicHeader)
13195 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a );. assert( siz
13196 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 eof(zMagicHeader
13197 29 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 )==16 );. put2b
13198 79 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 yte(&data[16], p
13199 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 Bt->pageSize);.
1319a 20 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 data[18] = 1;.
1319b 20 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 data[19] = 1;.
1319c 20 64 61 74 61 5b 32 30 5d 20 3d 20 70 42 74 2d data[20] = pBt-
1319d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d >pageSize - pBt-
1319e 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 64 >usableSize;. d
1319f 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20 ata[21] = 64;.
131a0 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20 data[22] = 32;.
131a1 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a data[23] = 32;.
131a2 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32 memset(&data[2
131a3 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a 4], 0, 100-24);.
131a4 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20 zeroPage(pP1,
131a5 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c PTF_INTKEY|PTF_L
131a6 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 EAF|PTF_LEAFDATA
131a7 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67 65 53 );. pBt->pageS
131a8 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 69 izeFixed = 1;.#i
131a9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
131aa 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 61 T_AUTOVACUUM. a
131ab 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f ssert( pBt->auto
131ac 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 Vacuum==1 || pBt
131ad 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 30 20 ->autoVacuum==0
131ae 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
131af 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 31 20 ->incrVacuum==1
131b0 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 || pBt->incrVacu
131b1 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 34 62 um==0 );. put4b
131b2 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 34 yte(&data[36 + 4
131b3 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 56 61 *4], pBt->autoVa
131b4 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 79 74 cuum);. put4byt
131b5 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37 2a 34 e(&data[36 + 7*4
131b6 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 ], pBt->incrVacu
131b7 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 um);.#endif. re
131b8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
131b9 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 }../*.** Attempt
131ba 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20 to start a new
131bb 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 77 transaction. A w
131bc 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
131bd 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64 20 69 .** is started i
131be 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
131bf 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65 72 6f ument is nonzero
131c0 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20 72 65 , otherwise a re
131c1 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 ad-.** transacti
131c2 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f on. If the seco
131c3 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 32 nd argument is 2
131c4 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65 78 63 or more and exc
131c5 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 lusive.** transa
131c6 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 ction is started
131c7 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e , meaning that n
131c8 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 o other process
131c9 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 6f is allowed.** to
131ca 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 access the data
131cb 62 61 73 65 2e 20 20 41 20 70 72 65 65 78 69 73 base. A preexis
131cc 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f 6e ting transaction
131cd 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a 20 75 may not be.** u
131ce 70 67 72 61 64 65 64 20 74 6f 20 65 78 63 6c 75 pgraded to exclu
131cf 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 sive by calling
131d0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 20 73 this routine a s
131d1 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74 68 65 econd time - the
131d2 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74 79 20 .** exclusivity
131d3 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 flag only works
131d4 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e 73 61 for a new transa
131d5 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 77 ction..**.** A w
131d6 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
131d7 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 64 must be started
131d8 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 before attempti
131d9 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61 6e 67 ng any .** chang
131da 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 es to the databa
131db 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 se. None of the
131dc 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
131dd 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77 6f 72 nes .** will wor
131de 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e 73 k unless a trans
131df 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 action is starte
131e0 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 d first:.**.**
131e1 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
131e2 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a 2a 2a CreateTable().**
131e3 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
131e4 65 65 43 72 65 61 74 65 49 6e 64 65 78 28 29 0a eeCreateIndex().
131e5 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
131e6 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 29 treeClearTable()
131e7 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
131e8 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 29 BtreeDropTable()
131e9 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
131ea 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a 2a 2a BtreeInsert().**
131eb 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
131ec 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20 20 20 eeDelete().**
131ed 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 sqlite3BtreeU
131ee 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a 0a 2a pdateMeta().**.*
131ef 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61 6c 20 * If an initial
131f0 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71 75 69 attempt to acqui
131f1 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61 69 6c re the lock fail
131f2 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c 6f 63 s because of loc
131f3 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a 2a 20 k contention.**
131f4 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 and the database
131f5 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 was previously
131f6 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 69 unlocked, then i
131f7 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 nvoke the busy h
131f8 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74 68 65 andler.** if the
131f9 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75 74 20 re is one. But
131fa 69 66 20 74 68 65 72 65 20 77 61 73 20 70 72 65 if there was pre
131fb 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64 2d 6c viously a read-l
131fc 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 69 ock, do not.** i
131fd 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 nvoke the busy h
131fe 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20 72 65 andler - just re
131ff 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
13200 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 . SQLITE_BUSY i
13201 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 77 s .** returned w
13202 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6c 72 hen there is alr
13203 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b eady a read-lock
13204 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f in order to avo
13205 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a id a deadlock..*
13206 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 *.** Suppose the
13207 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f 63 65 re are two proce
13208 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20 20 41 sses A and B. A
13209 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b has a read lock
1320a 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20 61 20 and B has.** a
1320b 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 20 reserved lock.
1320c 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f B tries to promo
1320d 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 te to exclusive
1320e 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 but is blocked b
1320f 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41 27 73 ecause.** of A's
13210 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41 20 74 read lock. A t
13211 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 ries to promote
13212 74 6f 20 72 65 73 65 72 76 65 64 20 62 75 74 20 to reserved but
13213 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 42 2e is blocked by B.
13214 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 20 6f .** One or the o
13215 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20 ther of the two
13216 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74 20 67 processes must g
13217 69 76 65 20 77 61 79 20 6f 72 20 74 68 65 72 65 ive way or there
13218 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20 70 72 can be.** no pr
13219 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 74 75 ogress. By retu
1321a 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 55 53 rning SQLITE_BUS
1321b 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f 6b 69 Y and not invoki
1321c 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c ng the busy call
1321d 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41 20 61 back.** when A a
1321e 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65 61 lready has a rea
1321f 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 6f 75 d lock, we encou
13220 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 20 75 rage A to give u
13221 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a 20 70 p and let B.** p
13222 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 roceed..*/.SQLIT
13223 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13224 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
13225 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c 20 69 rans(Btree *p, i
13226 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20 42 74 nt wrflag){. Bt
13227 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
13228 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d >pBt;. int rc =
13229 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 SQLITE_OK;.. s
1322a 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1322b 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d (p);. pBt->db =
1322c 20 70 2d 3e 64 62 3b 0a 20 20 62 74 72 65 65 49 p->db;. btreeI
1322d 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 ntegrity(p);..
1322e 2f 2a 20 49 66 20 74 68 65 20 62 74 72 65 65 20 /* If the btree
1322f 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20 is already in a
13230 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
13231 6e 2c 20 6f 72 20 69 74 0a 20 20 2a 2a 20 69 73 n, or it. ** is
13232 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20 72 65 already in a re
13233 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 ad-transaction a
13234 6e 64 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 nd a read-transa
13235 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 72 65 ction. ** is re
13236 71 75 65 73 74 65 64 2c 20 74 68 69 73 20 69 73 quested, this is
13237 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 a no-op.. */.
13238 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d if( p->inTrans=
13239 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 =TRANS_WRITE ||
1323a 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 (p->inTrans==TRA
1323b 4e 53 5f 52 45 41 44 20 26 26 20 21 77 72 66 6c NS_READ && !wrfl
1323c 61 67 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ag) ){. goto
1323d 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d trans_begun;. }
1323e 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 72 61 .. /* Write tra
1323f 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f nsactions are no
13240 74 20 70 6f 73 73 69 62 6c 65 20 6f 6e 20 61 20 t possible on a
13241 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 read-only databa
13242 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d se */. if( pBt-
13243 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 77 72 66 >readOnly && wrf
13244 6c 61 67 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 lag ){. rc =
13245 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
13246 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f . goto trans_
13247 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a begun;. }.. /*
13248 20 49 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61 If another data
13249 62 61 73 65 20 68 61 6e 64 6c 65 20 68 61 73 20 base handle has
1324a 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 61 already opened a
1324b 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1324c 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73 on . ** on this
1324d 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 shared-btree st
1324e 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 73 65 ructure and a se
1324f 63 6f 6e 64 20 77 72 69 74 65 20 74 72 61 6e 73 cond write trans
13250 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 action is. ** r
13251 65 71 75 65 73 74 65 64 2c 20 72 65 74 75 72 6e equested, return
13252 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 20 SQLITE_BUSY..
13253 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e */. if( pBt->in
13254 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
13255 4e 53 5f 57 52 49 54 45 20 26 26 20 77 72 66 6c NS_WRITE && wrfl
13256 61 67 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 ag ){. rc = S
13257 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
13258 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e goto trans_begun
13259 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ;. }..#ifndef S
1325a 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1325b 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20 77 72 D_CACHE. if( wr
1325c 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 flag>1 ){. Bt
1325d 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 Lock *pIter;.
1325e 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e for(pIter=pBt->
1325f 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 pLock; pIter; pI
13260 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 ter=pIter->pNext
13261 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 ){. if( pIt
13262 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b er->pBtree!=p ){
13263 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
13264 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 LITE_BUSY;.
13265 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 goto trans_be
13266 67 75 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 gun;. }.
13267 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
13268 20 64 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 42 do {. if( pB
13269 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29 7b 0a t->pPage1==0 ){.
1326a 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 do{.
1326b 20 20 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 rc = lockBtree
1326c 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 77 68 (pBt);. }wh
1326d 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ile( pBt->pPage1
1326e 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 ==0 && rc==SQLIT
1326f 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 E_OK );. }..
13270 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13271 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 E_OK && wrflag )
13272 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d {. if( pBt-
13273 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 >readOnly ){.
13274 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
13275 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 _READONLY;.
13276 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
13277 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
13278 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 rBegin(pBt->pPag
13279 65 31 2d 3e 70 44 62 50 61 67 65 2c 20 77 72 66 e1->pDbPage, wrf
1327a 6c 61 67 3e 31 29 3b 0a 20 20 20 20 20 20 20 20 lag>1);.
1327b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1327c 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
1327d 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 c = newDatabase(
1327e 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a pBt);. }.
1327f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
13280 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
13281 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13282 69 66 28 20 77 72 66 6c 61 67 20 29 20 70 42 74 if( wrflag ) pBt
13283 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 ->inStmt = 0;.
13284 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 }else{. u
13285 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
13286 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 ed(pBt);. }.
13287 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c }while( rc==SQL
13288 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74 2d ITE_BUSY && pBt-
13289 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
1328a 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 TRANS_NONE &&.
1328b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
1328c 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 treeInvokeBusyHa
1328d 6e 64 6c 65 72 28 70 42 74 2c 20 30 29 20 29 3b ndler(pBt, 0) );
1328e 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
1328f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
13290 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
13291 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 NS_NONE ){.
13292 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 pBt->nTransacti
13293 6f 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 on++;. }.
13294 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 28 77 72 p->inTrans = (wr
13295 66 6c 61 67 3f 54 52 41 4e 53 5f 57 52 49 54 45 flag?TRANS_WRITE
13296 3a 54 52 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20 :TRANS_READ);.
13297 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
13298 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
13299 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ion ){. pBt
1329a 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
1329b 3d 20 70 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 = p->inTrans;.
1329c 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
1329d 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
1329e 41 43 48 45 0a 20 20 20 20 69 66 28 20 77 72 66 ACHE. if( wrf
1329f 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 20 20 61 lag>1 ){. a
132a0 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 45 78 ssert( !pBt->pEx
132a1 63 6c 75 73 69 76 65 20 29 3b 0a 20 20 20 20 20 clusive );.
132a2 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 65 pBt->pExclusive
132a3 20 3d 20 70 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 = p;. }.#end
132a4 69 66 0a 20 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 if. }...trans_b
132a5 65 67 75 6e 3a 0a 20 20 62 74 72 65 65 49 6e 74 egun:. btreeInt
132a6 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c egrity(p);. sql
132a7 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
132a8 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
132a9 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }...#ifndef SQLI
132aa 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
132ab 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 UM../*.** Set th
132ac 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e e pointer-map en
132ad 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 tries for all ch
132ae 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 ildren of page p
132af 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a Page. Also, if.*
132b0 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 * pPage contains
132b1 20 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e cells that poin
132b2 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 t to overflow pa
132b3 67 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 ges, set the poi
132b4 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 nter.** map entr
132b5 69 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 ies for the over
132b6 66 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 flow pages as we
132b7 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ll..*/.static in
132b8 74 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 t setChildPtrmap
132b9 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 s(MemPage *pPage
132ba 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
132bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132bc 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 /* Count
132bd 65 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 er variable */.
132be 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 int nCell;
132bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
132c1 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 cells in page p
132c2 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 Page */. int rc
132c3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
132c4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
132c5 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
132c6 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
132c7 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 pPage->pBt;. i
132c8 6e 74 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 nt isInitOrig =
132c9 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 pPage->isInit;.
132ca 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 Pgno pgno = pPa
132cb 67 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 ge->pgno;.. ass
132cc 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
132cd 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
132ce 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
132cf 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
132d0 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 eInitPage(pPage)
132d1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
132d2 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 TE_OK ){. got
132d3 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d o set_child_ptrm
132d4 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e aps_out;. }. n
132d5 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
132d6 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b ell;.. for(i=0;
132d7 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
132d8 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 u8 *pCell =
132d9 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
132da 69 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 i);.. rc = pt
132db 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 rmapPutOvflPtr(p
132dc 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 Page, pCell);.
132dd 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
132de 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 _OK ){. got
132df 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d o set_child_ptrm
132e0 61 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a aps_out;. }..
132e1 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e if( !pPage->
132e2 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 leaf ){. Pg
132e3 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 no childPgno = g
132e4 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a et4byte(pCell);.
132e5 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
132e6 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 pPut(pBt, childP
132e7 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
132e8 45 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 E, pgno);.
132e9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
132ea 4b 20 29 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 K ) goto set_chi
132eb 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a ld_ptrmaps_out;.
132ec 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
132ed 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
132ee 0a 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 . Pgno childP
132ef 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 gno = get4byte(&
132f0 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
132f1 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
132f2 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d );. rc = ptrm
132f3 61 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 apPut(pBt, child
132f4 50 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 Pgno, PTRMAP_BTR
132f5 45 45 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a EE, pgno);. }..
132f6 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 set_child_ptrmap
132f7 73 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e s_out:. pPage->
132f8 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f isInit = isInitO
132f9 72 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 rig;. return rc
132fa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 ;.}../*.** Somew
132fb 68 65 72 65 20 6f 6e 20 70 50 61 67 65 2c 20 77 here on pPage, w
132fc 68 69 63 68 20 69 73 20 67 75 61 72 65 6e 74 65 hich is guarente
132fd 65 64 20 74 6f 20 62 65 20 61 20 62 74 72 65 65 ed to be a btree
132fe 20 70 61 67 65 2c 20 6e 6f 74 20 61 6e 20 6f 76 page, not an ov
132ff 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 erflow.** page,
13300 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
13301 70 61 67 65 20 69 46 72 6f 6d 2e 20 4d 6f 64 69 page iFrom. Modi
13302 66 79 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 fy this pointer
13303 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 so that it point
13304 73 20 74 6f 0a 2a 2a 20 69 54 6f 2e 20 50 61 72 s to.** iTo. Par
13305 61 6d 65 74 65 72 20 65 54 79 70 65 20 64 65 73 ameter eType des
13306 63 72 69 62 65 73 20 74 68 65 20 74 79 70 65 20 cribes the type
13307 6f 66 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 of pointer to be
13308 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 0a 2a modified, as .*
13309 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a * follows:.**.**
1330a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 PTRMAP_BTREE:
1330b 20 20 20 70 50 61 67 65 20 69 73 20 61 20 62 74 pPage is a bt
1330c 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f ree-page. The po
1330d 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 inter points at
1330e 61 20 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 a child .**
1330f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
13310 67 65 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a ge of pPage..**.
13311 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c ** PTRMAP_OVERFL
13312 4f 57 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 OW1: pPage is a
13313 62 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 btree-page. The
13314 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 pointer points a
13315 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a t an overflow.**
13316 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13317 20 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 page pointed
13318 74 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 to by one of the
13319 20 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e cells on pPage.
1331a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 .**.** PTRMAP_OV
1331b 45 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 ERFLOW2: pPage i
1331c 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 s an overflow-pa
1331d 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 ge. The pointer
1331e 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 points at the ne
1331f 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 xt.**
13320 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 overflow
13321 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 page in the lis
13322 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
13323 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 modifyPagePoint
13324 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 er(MemPage *pPag
13325 65 2c 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 e, Pgno iFrom, P
13326 67 6e 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 gno iTo, u8 eTyp
13327 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 e){. assert( sq
13328 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
13329 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
1332a 65 78 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79 ex) );. if( eTy
1332b 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 pe==PTRMAP_OVERF
1332c 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 LOW2 ){. /* T
1332d 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c he pointer is al
1332e 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34 ways the first 4
1332f 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 bytes of the pa
13330 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ge in this case.
13331 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74 */. if( get
13332 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 4byte(pPage->aDa
13333 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 ta)!=iFrom ){.
13334 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
13335 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
13336 20 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79 }. put4by
13337 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c te(pPage->aData,
13338 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a iTo);. }else{.
13339 20 20 20 20 69 6e 74 20 69 73 49 6e 69 74 4f 72 int isInitOr
1333a 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e ig = pPage->isIn
1333b 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 it;. int i;.
1333c 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 int nCell;..
1333d 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 sqlite3BtreeI
1333e 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a nitPage(pPage);.
1333f 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 nCell = pPag
13340 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 e->nCell;.. f
13341 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b or(i=0; i<nCell;
13342 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 i++){. u8
13343 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c *pCell = findCel
13344 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 l(pPage, i);.
13345 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 if( eType==PT
13346 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 RMAP_OVERFLOW1 )
13347 7b 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e {. CellIn
13348 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 fo info;.
13349 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
1334a 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
1334b 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
1334c 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f if( info
1334d 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 .iOverflow ){.
1334e 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f if( iFro
1334f 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 m==get4byte(&pCe
13350 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
13351 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 w]) ){.
13352 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65 put4byte(&pCe
13353 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
13354 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 w], iTo);.
13355 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
13356 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
13357 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
13358 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 34 if( get4
13359 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 byte(pCell)==iFr
1335a 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 om ){.
1335b 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 put4byte(pCell,
1335c 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 iTo);.
1335d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
1335e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1335f 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 . if( i==nCe
13360 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
13361 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 eType!=PTRMAP_BT
13362 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 REE || .
13363 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 get4byte(&pPag
13364 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
13365 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 hdrOffset+8])!=i
13366 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 From ){.
13367 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
13368 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
13369 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 }. put4by
1336a 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
1336b 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
1336c 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 t+8], iTo);.
1336d 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 }.. pPage->is
1336e 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 Init = isInitOri
1336f 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 g;. }. return
13370 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
13371 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 *.** Move the op
13372 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 en database page
13373 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 pDbPage to loca
13374 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 tion iFreePage i
13375 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 n the .** databa
13376 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 se. The pDbPage
13377 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e reference remain
13378 73 20 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 s valid..*/.stat
13379 69 63 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 ic int relocateP
1337a 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 age(. BtShared
1337b 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 *pBt,
1337c 2f 2a 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 /* Btree */. Me
1337d 6d 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20 mPage *pDbPage,
1337e 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 /* Open p
1337f 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 age to move */.
13380 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 u8 eType,
13381 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
13382 6e 74 65 72 20 6d 61 70 20 27 74 79 70 65 27 20 nter map 'type'
13383 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 entry for pDbPag
13384 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 e */. Pgno iPtr
13385 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
13386 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 /* Pointer map '
13387 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 page-no' entry f
13388 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 or pDbPage */.
13389 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65 2c 20 Pgno iFreePage,
1338a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1338b 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 location to move
1338c 20 70 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 pDbPage to */.
1338d 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 0a 29 7b int isCommit.){
1338e 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 . MemPage *pPtr
1338f 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 Page; /* The p
13390 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e age that contain
13391 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 s a pointer to p
13392 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f DbPage */. Pgno
13393 20 69 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 iDbPage = pDbPa
13394 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 ge->pgno;. Page
13395 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d r *pPager = pBt-
13396 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 >pPager;. int r
13397 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 54 c;.. assert( eT
13398 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
13399 46 4c 4f 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d FLOW2 || eType==
1339a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 PTRMAP_OVERFLOW1
1339b 20 7c 7c 20 0a 20 20 20 20 20 20 65 54 79 70 65 || . eType
1339c 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c ==PTRMAP_BTREE |
1339d 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f | eType==PTRMAP_
1339e 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 61 73 ROOTPAGE );. as
1339f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
133a0 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
133a1 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
133a2 28 20 70 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d ( pDbPage->pBt==
133a3 70 42 74 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 pBt );.. /* Mov
133a4 65 20 70 61 67 65 20 69 44 62 50 61 67 65 20 66 e page iDbPage f
133a5 72 6f 6d 20 69 74 73 20 63 75 72 72 65 6e 74 20 rom its current
133a6 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 location to page
133a7 20 6e 75 6d 62 65 72 20 69 46 72 65 65 50 61 67 number iFreePag
133a8 65 20 2a 2f 0a 20 20 54 52 41 43 45 28 28 22 41 e */. TRACE(("A
133a9 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e UTOVACUUM: Movin
133aa 67 20 25 64 20 74 6f 20 66 72 65 65 20 70 61 67 g %d to free pag
133ab 65 20 25 64 20 28 70 74 72 20 70 61 67 65 20 25 e %d (ptr page %
133ac 64 20 74 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a d type %d)\n", .
133ad 20 20 20 20 20 20 69 44 62 50 61 67 65 2c 20 69 iDbPage, i
133ae 46 72 65 65 50 61 67 65 2c 20 69 50 74 72 50 61 FreePage, iPtrPa
133af 67 65 2c 20 65 54 79 70 65 29 29 3b 0a 20 20 72 ge, eType));. r
133b0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
133b1 4d 6f 76 65 70 61 67 65 28 70 50 61 67 65 72 2c Movepage(pPager,
133b2 20 70 44 62 50 61 67 65 2d 3e 70 44 62 50 61 67 pDbPage->pDbPag
133b3 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 73 e, iFreePage, is
133b4 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66 28 20 72 Commit);. if( r
133b5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
133b6 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
133b7 20 7d 0a 20 20 70 44 62 50 61 67 65 2d 3e 70 67 }. pDbPage->pg
133b8 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 65 3b 0a no = iFreePage;.
133b9 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 61 67 65 . /* If pDbPage
133ba 20 77 61 73 20 61 20 62 74 72 65 65 2d 70 61 67 was a btree-pag
133bb 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 68 e, then it may h
133bc 61 76 65 20 63 68 69 6c 64 20 70 61 67 65 73 20 ave child pages
133bd 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a and/or cells. *
133be 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 * that point to
133bf 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
133c0 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
133c1 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 entries for all
133c2 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 67 65 73 these. ** pages
133c3 20 6e 65 65 64 20 74 6f 20 62 65 20 63 68 61 6e need to be chan
133c4 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 ged.. **. ** I
133c5 66 20 70 44 62 50 61 67 65 20 69 73 20 61 6e 20 f pDbPage is an
133c6 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 overflow page, t
133c7 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 34 20 hen the first 4
133c8 62 79 74 65 73 20 6d 61 79 20 73 74 6f 72 65 20 bytes may store
133c9 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 a. ** pointer t
133ca 6f 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f o a subsequent o
133cb 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 verflow page. If
133cc 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 this is the cas
133cd 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 e, then. ** the
133ce 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 pointer map nee
133cf 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 ds to be updated
133d0 20 66 6f 72 20 74 68 65 20 73 75 62 73 65 71 75 for the subsequ
133d1 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ent overflow pag
133d2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 e.. */. if( eT
133d3 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 ype==PTRMAP_BTRE
133d4 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d E || eType==PTRM
133d5 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 AP_ROOTPAGE ){.
133d6 20 20 20 72 63 20 3d 20 73 65 74 43 68 69 6c 64 rc = setChild
133d7 50 74 72 6d 61 70 73 28 70 44 62 50 61 67 65 29 Ptrmaps(pDbPage)
133d8 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
133d9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
133da 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
133db 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 }. }else{. P
133dc 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67 gno nextOvfl = g
133dd 65 74 34 62 79 74 65 28 70 44 62 50 61 67 65 2d et4byte(pDbPage-
133de 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28 >aData);. if(
133df 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a nextOvfl!=0 ){.
133e0 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
133e1 70 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 pPut(pBt, nextOv
133e2 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 fl, PTRMAP_OVERF
133e3 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 29 LOW2, iFreePage)
133e4 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
133e5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
133e6 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
133e7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
133e8 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 68 65 20 }.. /* Fix the
133e9 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72 database pointer
133ea 20 6f 6e 20 70 61 67 65 20 69 50 74 72 50 61 67 on page iPtrPag
133eb 65 20 74 68 61 74 20 70 6f 69 6e 74 65 64 20 61 e that pointed a
133ec 74 20 69 44 62 50 61 67 65 20 73 6f 0a 20 20 2a t iDbPage so. *
133ed 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 * that it points
133ee 20 61 74 20 69 46 72 65 65 50 61 67 65 2e 20 41 at iFreePage. A
133ef 6c 73 6f 20 66 69 78 20 74 68 65 20 70 6f 69 6e lso fix the poin
133f0 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f ter map entry fo
133f1 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 67 65 2e r. ** iPtrPage.
133f2 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 . */. if( eTyp
133f3 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 e!=PTRMAP_ROOTPA
133f4 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 GE ){. rc = s
133f5 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
133f6 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 67 65 ge(pBt, iPtrPage
133f7 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b , &pPtrPage, 0);
133f8 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
133f9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
133fa 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
133fb 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
133fc 33 50 61 67 65 72 57 72 69 74 65 28 70 50 74 72 3PagerWrite(pPtr
133fd 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
133fe 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
133ff 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
13400 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 eleasePage(pPtrP
13401 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 age);. retu
13402 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
13403 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 rc = modifyPage
13404 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67 65 Pointer(pPtrPage
13405 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 , iDbPage, iFree
13406 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 Page, eType);.
13407 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
13408 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 trPage);. if(
13409 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1340a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 {. rc = ptr
1340b 6d 61 70 50 75 74 28 70 42 74 2c 20 69 46 72 65 mapPut(pBt, iFre
1340c 65 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50 ePage, eType, iP
1340d 74 72 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 trPage);. }.
1340e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1340f 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 }../* Forward de
13410 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72 claration requir
13411 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d ed by incrVacuum
13412 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 Step(). */.stati
13413 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 c int allocateBt
13414 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65 64 reePage(BtShared
13415 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 *, MemPage **,
13416 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 Pgno *, Pgno, u8
13417 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 );../*.** Perfor
13418 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 m a single step
13419 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 of an incrementa
1341a 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75 63 l-vacuum. If suc
1341b 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 cessful,.** retu
1341c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 rn SQLITE_OK. If
1341d 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 there is no wor
1341e 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 k to do (and the
1341f 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 refore no.** poi
13420 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 nt in calling th
13421 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 is function agai
13422 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 n), return SQLIT
13423 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f E_DONE..**.** Mo
13424 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74 re specificly, t
13425 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 his function att
13426 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 empts to re-orga
13427 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 nize the .** dat
13428 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 74 68 abase so that th
13429 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74 e last page of t
1342a 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c he file currentl
1342b 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e y in use.** is n
1342c 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e o longer in use.
1342d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 .**.** If the nF
1342e 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 in parameter is
1342f 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 69 6d non-zero, the im
13430 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 plementation ass
13431 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 umes.** that the
13432 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 caller will kee
13433 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 p calling incrVa
13434 63 75 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c cuumStep() until
13435 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20 53 .** it returns S
13436 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e QLITE_DONE or an
13437 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 error, and that
13438 20 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 nFin is the.**
13439 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
1343a 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1343b 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 e will contain a
1343c 66 74 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 fter this .** pr
1343d 6f 63 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 ocess is complet
1343e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1343f 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 incrVacuumStep(
13440 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
13441 67 6e 6f 20 6e 46 69 6e 29 7b 0a 20 20 50 67 6e gno nFin){. Pgn
13442 6f 20 69 4c 61 73 74 50 67 3b 20 20 20 20 20 20 o iLastPg;
13443 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 /* Last p
13444 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
13445 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 46 ase */. Pgno nF
13446 72 65 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 reeList;
13447 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
13448 70 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 pages still on t
13449 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a he free-list */.
1344a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1344b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
1344c 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 t->mutex) );. i
1344d 4c 61 73 74 50 67 20 3d 20 70 42 74 2d 3e 6e 54 LastPg = pBt->nT
1344e 72 75 6e 63 3b 0a 20 20 69 66 28 20 69 4c 61 73 runc;. if( iLas
1344f 74 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c tPg==0 ){. iL
13450 61 73 74 50 67 20 3d 20 70 61 67 65 72 50 61 67 astPg = pagerPag
13451 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 ecount(pBt->pPag
13452 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 er);. }.. if(
13453 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 !PTRMAP_ISPAGE(p
13454 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26 20 Bt, iLastPg) &&
13455 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e 47 iLastPg!=PENDING
13456 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
13457 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 ){. int rc;.
13458 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 u8 eType;.
13459 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a Pgno iPtrPage;.
1345a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 74 20 3d . nFreeList =
1345b 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e get4byte(&pBt->
1345c 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
1345d 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 72 65 ]);. if( nFre
1345e 65 4c 69 73 74 3d 3d 30 20 7c 7c 20 6e 46 69 6e eList==0 || nFin
1345f 3d 3d 69 4c 61 73 74 50 67 20 29 7b 0a 20 20 20 ==iLastPg ){.
13460 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
13461 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 _DONE;. }..
13462 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 rc = ptrmapGet
13463 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 (pBt, iLastPg, &
13464 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 67 65 eType, &iPtrPage
13465 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
13466 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13467 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
13468 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79 70 65 }. if( eType
13469 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 ==PTRMAP_ROOTPAG
1346a 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 E ){. retur
1346b 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1346c 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 _BKPT;. }..
1346d 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
1346e 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a MAP_FREEPAGE ){.
1346f 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e 3d 3d if( nFin==
13470 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 0 ){. /*
13471 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 Remove the page
13472 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 20 66 from the files f
13473 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 ree-list. This i
13474 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 0a 20 s not required.
13475 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e 46 69 ** if nFi
13476 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 n is non-zero. I
13477 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 n that case, the
13478 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20 free-list will
13479 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 be. ** tr
1347a 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 uncated to zero
1347b 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
1347c 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 6f 20 ion returns, so
1347d 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 20 20 it doesn't .
1347e 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 69 66 ** matter if
1347f 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 it still contai
13480 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20 ns some garbage
13481 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20 entries..
13482 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f */. Pgno
13483 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 iFreePg;.
13484 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 MemPage *pFree
13485 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d Pg;. rc =
13486 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
13487 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 ge(pBt, &pFreePg
13488 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c 61 73 , &iFreePg, iLas
13489 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 tPg, 1);.
1348a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1348b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1348c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1348d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 }. ass
1348e 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d 69 4c ert( iFreePg==iL
1348f 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 20 astPg );.
13490 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 releasePage(pFr
13491 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 eePg);. }.
13492 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
13493 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 20 Pgno iFreePg;
13494 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
13495 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 61 67 ndex of free pag
13496 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50 e to move pLastP
13497 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 g to */. Me
13498 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a mPage *pLastPg;.
13499 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1349a 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
1349b 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 pBt, iLastPg, &p
1349c 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 LastPg, 0);.
1349d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1349e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
1349f 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
134a0 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e }.. /* If n
134a1 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 Fin is zero, thi
134a2 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 s loop runs exac
134a3 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 tly once and pag
134a4 65 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 e pLastPg.
134a5 2a 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 ** is swapped wi
134a6 74 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 th the first fre
134a7 65 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 e page pulled of
134a8 66 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e f the free list.
134a9 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
134aa 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 ** On the other
134ab 68 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 hand, if nFin is
134ac 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 greater than ze
134ad 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 ro, then keep.
134ae 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 ** looping u
134af 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65 ntil a free-page
134b0 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 located within
134b1 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70 the first nFin p
134b2 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 ages. ** of
134b3 20 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 the file is fou
134b4 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 nd.. */.
134b5 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 do {.
134b6 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 MemPage *pFreePg
134b7 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 ;. rc = a
134b8 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
134b9 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 (pBt, &pFreePg,
134ba 26 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b &iFreePg, 0, 0);
134bb 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
134bc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
134bd 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
134be 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 age(pLastPg);.
134bf 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
134c0 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
134c1 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
134c2 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 (pFreePg);.
134c3 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 }while( nFin!=0
134c4 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e && iFreePg>nFin
134c5 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
134c6 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50 ( iFreePg<iLastP
134c7 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 g );. .
134c8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
134c9 67 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67 gerWrite(pLastPg
134ca 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
134cb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
134cc 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
134cd 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 c = relocatePage
134ce 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 (pBt, pLastPg, e
134cf 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 Type, iPtrPage,
134d0 69 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 iFreePg, nFin!=0
134d1 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
134d2 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 releasePage(pLa
134d3 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 stPg);. if(
134d4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
134d5 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
134d6 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
134d7 20 7d 0a 20 20 7d 0a 0a 20 20 70 42 74 2d 3e 6e }. }.. pBt->n
134d8 54 72 75 6e 63 20 3d 20 69 4c 61 73 74 50 67 20 Trunc = iLastPg
134d9 2d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 70 42 - 1;. while( pB
134da 74 2d 3e 6e 54 72 75 6e 63 3d 3d 50 45 4e 44 49 t->nTrunc==PENDI
134db 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
134dc 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45 )||PTRMAP_ISPAGE
134dd 28 70 42 74 2c 20 70 42 74 2d 3e 6e 54 72 75 6e (pBt, pBt->nTrun
134de 63 29 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 6e c) ){. pBt->n
134df 54 72 75 6e 63 2d 2d 3b 0a 20 20 7d 0a 20 20 72 Trunc--;. }. r
134e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
134e1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 .}../*.** A writ
134e2 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 e-transaction mu
134e3 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 st be opened bef
134e4 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 ore calling this
134e5 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 function..** It
134e6 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 performs a sing
134e7 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 le unit of work
134e8 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 towards an incre
134e9 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a mental vacuum..*
134ea 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 *.** If the incr
134eb 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 emental vacuum i
134ec 73 20 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 s finished after
134ed 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 this function h
134ee 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 as run,.** SQLIT
134ef 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e E_DONE is return
134f0 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 ed. If it is not
134f1 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e finished, but n
134f2 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 2c o error occured,
134f3 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
134f4 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
134f5 77 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 wise an SQLite e
134f6 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 rror code. .*/.S
134f7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
134f8 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e t sqlite3BtreeIn
134f9 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a crVacuum(Btree *
134fa 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 p){. int rc;.
134fb 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
134fc 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 p->pBt;.. sqlit
134fd 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
134fe 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e . pBt->db = p->
134ff 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 db;. assert( pB
13500 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
13501 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26 ==TRANS_WRITE &&
13502 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
13503 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 NS_WRITE );. if
13504 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 ( !pBt->autoVacu
13505 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 um ){. rc = S
13506 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 QLITE_DONE;. }e
13507 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 lse{. invalid
13508 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 ateAllOverflowCa
13509 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 che(pBt);. rc
1350a 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 = incrVacuumSte
1350b 70 28 70 42 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 p(pBt, 0);. }.
1350c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1350d 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
1350e 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
1350f 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
13510 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c led prior to sql
13511 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20 ite3PagerCommit
13512 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 when a transacti
13513 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 65 on.** is commite
13514 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61 d for an auto-va
13515 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a 2a cuum database..*
13516 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f *.** If SQLITE_O
13517 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 K is returned, t
13518 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 20 hen *pnTrunc is
13519 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
1351a 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 68 r of pages.** th
1351b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1351c 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 should be trunca
1351d 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 68 ted to during th
1351e 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 e commit process
1351f 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 64 . .** i.e. the d
13520 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e atabase has been
13521 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f 20 reorganized so
13522 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 69 that only the fi
13523 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 rst *pnTrunc.**
13524 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 65 pages are in use
13525 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
13526 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 autoVacuumCommit
13527 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
13528 50 67 6e 6f 20 2a 70 6e 54 72 75 6e 63 29 7b 0a Pgno *pnTrunc){.
13529 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1352a 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 E_OK;. Pager *p
1352b 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 Pager = pBt->pPa
1352c 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 ger;. VVA_ONLY(
1352d 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 6c 69 int nRef = sqli
1352e 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
1352f 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 61 (pPager) );.. a
13530 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
13531 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
13532 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 61 6c utex) );. inval
13533 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 idateAllOverflow
13534 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 61 73 Cache(pBt);. as
13535 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f 56 61 sert(pBt->autoVa
13536 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 70 42 cuum);. if( !pB
13537 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 29 7b t->incrVacuum ){
13538 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e 20 3d . Pgno nFin =
13539 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 70 42 74 0;.. if( pBt
1353a 2d 3e 6e 54 72 75 6e 63 3d 3d 30 20 29 7b 0a 20 ->nTrunc==0 ){.
1353b 20 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b Pgno nFree;
1353c 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 50 74 72 . Pgno nPtr
1353d 6d 61 70 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 map;. const
1353e 20 69 6e 74 20 70 67 73 7a 20 3d 20 70 42 74 2d int pgsz = pBt-
1353f 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 >pageSize;.
13540 20 69 6e 74 20 6e 4f 72 69 67 20 3d 20 70 61 67 int nOrig = pag
13541 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
13542 3e 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 20 >pPager);..
13543 20 69 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41 if( PTRMAP_ISPA
13544 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 29 GE(pBt, nOrig) )
13545 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
13546 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
13547 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 BKPT;. }.
13548 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3d 3d 50 if( nOrig==P
13549 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
1354a 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 (pBt) ){.
1354b 20 6e 4f 72 69 67 2d 2d 3b 0a 20 20 20 20 20 20 nOrig--;.
1354c 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 }. nFree =
1354d 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 get4byte(&pBt->p
1354e 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
1354f 29 3b 0a 20 20 20 20 20 20 6e 50 74 72 6d 61 70 );. nPtrmap
13550 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 2b = (nFree-nOrig+
13551 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 PTRMAP_PAGENO(pB
13552 74 2c 20 6e 4f 72 69 67 29 2b 70 67 73 7a 2f 35 t, nOrig)+pgsz/5
13553 29 2f 28 70 67 73 7a 2f 35 29 3b 0a 20 20 20 20 )/(pgsz/5);.
13554 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d nFin = nOrig -
13555 20 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 nFree - nPtrmap
13556 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 72 69 ;. if( nOri
13557 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 g>PENDING_BYTE_P
13558 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69 6e AGE(pBt) && nFin
13559 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 <=PENDING_BYTE_P
1355a 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
1355b 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 nFin--;.
1355c 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 }. while(
1355d 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 PTRMAP_ISPAGE(p
1355e 42 74 2c 20 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 Bt, nFin) || nFi
1355f 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f n==PENDING_BYTE_
13560 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
13561 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 nFin--;.
13562 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
13563 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
13564 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 E_OK ){. rc
13565 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 = incrVacuumSte
13566 70 28 70 42 74 2c 20 6e 46 69 6e 29 3b 0a 20 20 p(pBt, nFin);.
13567 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
13568 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 SQLITE_DONE ){.
13569 20 20 20 20 20 61 73 73 65 72 74 28 6e 46 69 6e assert(nFin
1356a 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 6e 54 72 75 ==0 || pBt->nTru
1356b 6e 63 3d 3d 30 20 7c 7c 20 6e 46 69 6e 3c 3d 70 nc==0 || nFin<=p
1356c 42 74 2d 3e 6e 54 72 75 6e 63 29 3b 0a 20 20 20 Bt->nTrunc);.
1356d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1356e 4b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 K;. if( pBt
1356f 2d 3e 6e 54 72 75 6e 63 20 26 26 20 6e 46 69 6e ->nTrunc && nFin
13570 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
13571 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
13572 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e te(pBt->pPage1->
13573 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
13574 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d put4byte(&pBt-
13575 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
13576 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 2], 0);.
13577 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 put4byte(&pBt->p
13578 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
13579 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 , 0);. pB
1357a 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 6e 46 69 6e t->nTrunc = nFin
1357b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1357c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1357d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
1357e 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 qlite3PagerRollb
1357f 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 ack(pPager);.
13580 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 }. }.. if( rc
13581 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13582 20 20 20 2a 70 6e 54 72 75 6e 63 20 3d 20 70 42 *pnTrunc = pB
13583 74 2d 3e 6e 54 72 75 6e 63 3b 0a 20 20 20 20 70 t->nTrunc;. p
13584 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 30 3b 0a Bt->nTrunc = 0;.
13585 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 52 }. assert( nR
13586 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 ef==sqlite3Pager
13587 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 Refcount(pPager)
13588 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
13589 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a .}..#endif../*.*
1358a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
1358b 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 68 oes the first ph
1358c 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 61 ase of a two-pha
1358d 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 73 se commit. This
1358e 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 73 routine.** caus
1358f 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f es a rollback jo
13590 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 61 urnal to be crea
13591 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 20 ted (if it does
13592 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 not already exis
13593 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c 61 t).** and popula
13594 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 20 ted with enough
13595 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 74 information so t
13596 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 6c hat if a power l
13597 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 oss occurs.** th
13598 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 e database can b
13599 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 e restored to it
1359a 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 s original state
1359b 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 6b by playing back
1359c 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e .** the journal.
1359d 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 Then the conte
1359e 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e nts of the journ
1359f 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 6f al are flushed o
135a0 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 73 ut to.** the dis
135a1 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a 6f k. After the jo
135a2 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 20 urnal is safely
135a3 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 68 on oxide, the ch
135a4 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 anges to the.**
135a5 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72 69 database are wri
135a6 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 tten into the da
135a7 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
135a8 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 65 flushed to oxide
135a9 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 ..** At the end
135aa 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 68 of this call, th
135ab 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
135ac 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 al still exists
135ad 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61 on the.** disk a
135ae 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c 20 nd we are still
135af 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 6b holding all lock
135b0 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 61 s, so the transa
135b1 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a 2a ction has not.**
135b2 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 65 committed. See
135b3 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
135b4 6d 69 74 28 29 20 66 6f 72 20 74 68 65 20 73 65 mit() for the se
135b5 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 74 68 cond phase of th
135b6 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 e.** commit proc
135b7 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ess..**.** This
135b8 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 call is a no-op
135b9 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e if no write-tran
135ba 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 saction is curre
135bb 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e 20 70 ntly active on p
135bc 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 Bt..**.** Otherw
135bd 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20 64 61 ise, sync the da
135be 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 tabase file for
135bf 74 68 65 20 62 74 72 65 65 20 70 42 74 2e 20 7a the btree pBt. z
135c0 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f Master points to
135c1 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 .** the name of
135c2 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
135c3 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c file that shoul
135c4 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 d be written int
135c5 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 o the.** individ
135c6 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ual journal file
135c7 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e , or is NULL, in
135c8 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 dicating no mast
135c9 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
135ca 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61 74 61 .** (single data
135cb 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e base transaction
135cc 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 )..**.** When th
135cd 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 is is called, th
135ce 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
135cf 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 should already
135d0 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 have been.** cre
135d1 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 ated, populated
135d2 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 with this journa
135d3 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 l pointer and sy
135d4 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a nced to disk..**
135d5 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20 69 73 .** Once this is
135d6 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72 65 74 routine has ret
135d7 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 urned, the only
135d8 74 68 69 6e 67 20 72 65 71 75 69 72 65 64 20 74 thing required t
135d9 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 o commit.** the
135da 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
135db 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 n for this datab
135dc 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f 20 64 ase file is to d
135dd 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 elete the journa
135de 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 l..*/.SQLITE_PRI
135df 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
135e0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
135e1 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20 63 6f One(Btree *p, co
135e2 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 nst char *zMaste
135e3 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
135e4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
135e5 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
135e6 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42 S_WRITE ){. B
135e7 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
135e8 2d 3e 70 42 74 3b 0a 20 20 20 20 50 67 6e 6f 20 ->pBt;. Pgno
135e9 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 20 20 20 20 nTrunc = 0;.
135ea 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
135eb 72 28 70 29 3b 0a 20 20 20 20 70 42 74 2d 3e 64 r(p);. pBt->d
135ec 62 20 3d 20 70 2d 3e 64 62 3b 0a 23 69 66 6e 64 b = p->db;.#ifnd
135ed 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
135ee 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
135ef 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
135f0 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 m ){. rc =
135f1 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 autoVacuumCommit
135f2 28 70 42 74 2c 20 26 6e 54 72 75 6e 63 29 3b 20 (pBt, &nTrunc);
135f3 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
135f4 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
135f5 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
135f6 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20 Leave(p);.
135f7 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
135f8 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
135f9 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 f. rc = sqlit
135fa 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 e3PagerCommitPha
135fb 73 65 4f 6e 65 28 70 42 74 2d 3e 70 50 61 67 65 seOne(pBt->pPage
135fc 72 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 54 72 75 r, zMaster, nTru
135fd 6e 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 nc, 0);. sqli
135fe 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
135ff 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
13600 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d c;.}../*.** Comm
13601 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 it the transacti
13602 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 on currently in
13603 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 progress..**.**
13604 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 This routine imp
13605 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f lements the seco
13606 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d nd phase of a 2-
13607 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 phase commit. T
13608 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 he.** sqlite3Btr
13609 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 eeSync() routine
1360a 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 does the first
1360b 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64 phase and should
1360c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 70 be invoked.** p
1360d 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 rior to calling
1360e 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 this routine. T
1360f 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 he sqlite3BtreeS
13610 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 20 64 69 ync() routine di
13611 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 77 6f 72 d.** all the wor
13612 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69 6e 66 k of writing inf
13613 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 ormation out to
13614 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e disk and flushin
13615 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 g the.** content
13616 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 61 s so that they a
13617 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 re written onto
13618 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74 65 72 the disk platter
13619 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 . All this.** r
1361a 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 64 6f outine has to do
1361b 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 74 72 is delete or tr
1361c 75 6e 63 61 74 65 20 74 68 65 20 72 6f 6c 6c 62 uncate the rollb
1361d 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 28 ack journal.** (
1361e 77 68 69 63 68 20 63 61 75 73 65 73 20 74 68 65 which causes the
1361f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 transaction to
13620 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 72 6f 70 commit) and drop
13621 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 locks..**.** Th
13622 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 is will release
13623 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f the write lock o
13624 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13625 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a ile. If there.*
13626 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 * are no active
13627 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f cursors, it also
13628 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 releases the re
13629 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 ad lock..*/.SQLI
1362a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1362b 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1362c 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 20 tPhaseTwo(Btree
1362d 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 *p){. BtShared
1362e 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a *pBt = p->pBt;..
1362f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
13630 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 ter(p);. pBt->d
13631 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 62 74 72 b = p->db;. btr
13632 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a eeIntegrity(p);.
13633 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e . /* If the han
13634 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d dle has a write-
13635 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e transaction open
13636 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61 , commit the sha
13637 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a red-btrees . **
13638 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
13639 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20 set the shared
1363a 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52 state to TRANS_R
1363b 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 EAD.. */. if(
1363c 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
1363d 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 S_WRITE ){. i
1363e 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 nt rc;. asser
1363f 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 t( pBt->inTransa
13640 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
13641 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 TE );. assert
13642 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 ( pBt->nTransact
13643 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 ion>0 );. rc
13644 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f = sqlite3PagerCo
13645 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 mmitPhaseTwo(pBt
13646 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 ->pPager);. i
13647 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13648 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
13649 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1364a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1364b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e . }. pBt->
1364c 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
1364d 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 20 20 TRANS_READ;.
1364e 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b pBt->inStmt = 0;
1364f 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b 41 6c 6c . }. unlockAll
13650 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20 20 2f 2a Tables(p);.. /*
13651 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 If the handle h
13652 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 as any kind of t
13653 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c ransaction open,
13654 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 74 decrement the t
13655 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 ransaction. **
13656 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 61 count of the sha
13657 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 68 red btree. If th
13658 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
13659 75 6e 74 20 72 65 61 63 68 65 73 20 30 2c 20 73 unt reaches 0, s
1365a 65 74 0a 20 20 2a 2a 20 74 68 65 20 73 68 61 72 et. ** the shar
1365b 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e ed state to TRAN
1365c 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e 6c 6f S_NONE. The unlo
1365d 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 ckBtreeIfUnused(
1365e 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a 20 20 2a ) call below. *
1365f 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 68 * will unlock th
13660 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 e pager.. */.
13661 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d if( p->inTrans!=
13662 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 TRANS_NONE ){.
13663 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 pBt->nTransact
13664 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 30 ion--;. if( 0
13665 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 ==pBt->nTransact
13666 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ion ){. pBt
13667 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
13668 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 = TRANS_NONE;.
13669 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 }. }.. /* Se
1366a 74 20 74 68 65 20 68 61 6e 64 6c 65 73 20 63 75 t the handles cu
1366b 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
1366c 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 n state to TRANS
1366d 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 6b _NONE and unlock
1366e 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72 20 . ** the pager
1366f 69 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f if this call clo
13670 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 sed the only rea
13671 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 d or write trans
13672 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 70 action.. */. p
13673 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e ->inTrans = TRAN
13674 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b S_NONE;. unlock
13675 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 BtreeIfUnused(pB
13676 74 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 t);.. btreeInte
13677 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 grity(p);. sqli
13678 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
13679 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1367a 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 E_OK;.}../*.** D
1367b 6f 20 62 6f 74 68 20 70 68 61 73 65 73 20 6f 66 o both phases of
1367c 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 a commit..*/.SQ
1367d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1367e 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1367f 6d 69 74 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 mit(Btree *p){.
13680 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
13681 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
13682 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
13683 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
13684 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 ne(p, 0);. if(
13685 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
13686 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
13687 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
13688 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a 20 20 73 eTwo(p);. }. s
13689 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
1368a 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
1368b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 ;.}..#ifndef NDE
1368c 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e BUG./*.** Return
1368d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 the number of w
1368e 72 69 74 65 2d 63 75 72 73 6f 72 73 20 6f 70 65 rite-cursors ope
1368f 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c 65 n on this handle
13690 2e 20 54 68 69 73 20 69 73 20 66 6f 72 20 75 73 . This is for us
13691 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 29 e.** in assert()
13692 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 6f expressions, so
13693 20 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 it is only comp
13694 69 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20 69 iled if NDEBUG i
13695 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 64 s not.** defined
13696 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 ..**.** For the
13697 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 purposes of this
13698 20 72 6f 75 74 69 6e 65 2c 20 61 20 77 72 69 74 routine, a writ
13699 65 2d 63 75 72 73 6f 72 20 69 73 20 61 6e 79 20 e-cursor is any
1369a 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 69 cursor that.** i
1369b 73 20 63 61 70 61 62 6c 65 20 6f 66 20 77 72 69 s capable of wri
1369c 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 ting to the data
1369d 62 73 65 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 bse. That means
1369e 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 0a the cursor was.
1369f 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f 70 ** originally op
136a0 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 ened for writing
136a1 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 and the cursor
136a2 68 61 73 20 6e 6f 74 20 62 65 20 64 69 73 61 62 has not be disab
136a3 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e 67 led.** by having
136a4 20 69 74 73 20 73 74 61 74 65 20 63 68 61 6e 67 its state chang
136a5 65 64 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 ed to CURSOR_FAU
136a6 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e LT..*/.static in
136a7 74 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 t countWriteCurs
136a8 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 ors(BtShared *pB
136a9 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a t){. BtCursor *
136aa 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20 pCur;. int r =
136ab 30 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42 0;. for(pCur=pB
136ac 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72 t->pCursor; pCur
136ad 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65 ; pCur=pCur->pNe
136ae 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 xt){. if( pCu
136af 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 43 75 r->wrFlag && pCu
136b0 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f r->eState!=CURSO
136b1 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 0a R_FAULT ) r++; .
136b2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a }. return r;.
136b3 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
136b4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 This routine set
136b5 73 20 74 68 65 20 73 74 61 74 65 20 74 6f 20 43 s the state to C
136b6 55 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 20 URSOR_FAULT and
136b7 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 the error.** cod
136b8 65 20 74 6f 20 65 72 72 43 6f 64 65 20 66 6f 72 e to errCode for
136b9 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 6f 6e every cursor on
136ba 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70 BtShared that p
136bb 42 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 65 6e Btree.** referen
136bc 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 ces..**.** Every
136bd 20 63 75 72 73 6f 72 20 69 73 20 74 72 69 70 70 cursor is tripp
136be 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 ed, including cu
136bf 72 73 6f 72 73 20 74 68 61 74 20 62 65 6c 6f 6e rsors that belon
136c0 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64 61 g.** to other da
136c1 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
136c2 6e 73 20 74 68 61 74 20 68 61 70 70 65 6e 20 74 ns that happen t
136c3 6f 20 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 o be sharing.**
136c4 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20 70 the cache with p
136c5 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 Btree..**.** Thi
136c6 73 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63 s routine gets c
136c7 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 6c alled when a rol
136c8 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a lback occurs..**
136c9 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 73 69 All cursors usi
136ca 6e 67 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 ng the same cach
136cb 65 20 6d 75 73 74 20 62 65 20 74 72 69 70 70 65 e must be trippe
136cc 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 d.** to prevent
136cd 74 68 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e 67 them from trying
136ce 20 74 6f 20 75 73 65 20 74 68 65 20 62 74 72 65 to use the btre
136cf 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 72 e after.** the r
136d0 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72 6f ollback. The ro
136d1 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20 llback may have
136d2 64 65 6c 65 74 65 64 20 74 61 62 6c 65 73 0a 2a deleted tables.*
136d3 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74 20 * or moved root
136d4 70 61 67 65 73 2c 20 73 6f 20 69 74 20 69 73 20 pages, so it is
136d5 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 74 not sufficient t
136d6 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65 20 73 74 o.** save the st
136d7 61 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f ate of the curso
136d8 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d r. The cursor m
136d9 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 ust be.** invali
136da 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 dated..*/.SQLITE
136db 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
136dc 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c lite3BtreeTripAl
136dd 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 20 2a lCursors(Btree *
136de 70 42 74 72 65 65 2c 20 69 6e 74 20 65 72 72 43 pBtree, int errC
136df 6f 64 65 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 ode){. BtCursor
136e0 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 *p;. sqlite3Bt
136e1 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 reeEnter(pBtree)
136e2 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65 65 ;. for(p=pBtree
136e3 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 ->pBt->pCursor;
136e4 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a p; p=p->pNext){.
136e5 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
136e6 43 6c 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a ClearCursor(p);.
136e7 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 p->eState =
136e8 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 CURSOR_FAULT;.
136e9 20 20 70 2d 3e 73 6b 69 70 20 3d 20 65 72 72 43 p->skip = errC
136ea 6f 64 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ode;. }. sqlit
136eb 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 e3BtreeLeave(pBt
136ec 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ree);.}../*.** R
136ed 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e ollback the tran
136ee 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 saction in progr
136ef 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72 ess. All cursor
136f0 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 s will be.** inv
136f1 61 6c 69 64 65 64 20 62 79 20 74 68 69 73 20 6f alided by this o
136f2 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 peration. Any a
136f3 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 ttempt to use a
136f4 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77 cursor.** that w
136f5 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20 62 as open at the b
136f6 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 eginning of this
136f7 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 operation will
136f8 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 result.** in an
136f9 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 error..**.** Thi
136fa 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 s will release t
136fb 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e he write lock on
136fc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
136fd 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a le. If there.**
136fe 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 are no active c
136ff 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 ursors, it also
13700 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 releases the rea
13701 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 d lock..*/.SQLIT
13702 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13703 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
13704 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 ck(Btree *p){.
13705 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 int rc;. BtShar
13706 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
13707 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
13708 67 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 ge1;.. sqlite3B
13709 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
1370a 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b pBt->db = p->db;
1370b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 . rc = saveAllC
1370c 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 ursors(pBt, 0, 0
1370d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
1370e 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
1370f 43 48 45 0a 20 20 69 66 28 20 72 63 21 3d 53 51 CHE. if( rc!=SQ
13710 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f LITE_OK ){. /
13711 2a 20 54 68 69 73 20 69 73 20 61 20 68 6f 72 72 * This is a horr
13712 69 62 6c 65 20 73 69 74 75 61 74 69 6f 6e 2e 20 ible situation.
13713 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 An IO or malloc(
13714 29 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 ) error occured
13715 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72 whilst. ** tr
13716 79 69 6e 67 20 74 6f 20 73 61 76 65 20 63 75 72 ying to save cur
13717 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49 sor positions. I
13718 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 f this is an aut
13719 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 omatic rollback
1371a 28 61 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 (as. ** the r
1371b 65 73 75 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74 esult of a const
1371c 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 raint, malloc()
1371d 66 61 69 6c 75 72 65 20 6f 72 20 49 4f 20 65 72 failure or IO er
1371e 72 6f 72 29 20 74 68 65 6e 20 0a 20 20 20 20 2a ror) then . *
1371f 2a 20 74 68 65 20 63 61 63 68 65 20 6d 61 79 20 * the cache may
13720 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e be internally in
13721 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e 6f 74 20 consistent (not
13722 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64 20 74 72 contain valid tr
13723 65 65 73 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77 ees) so. ** w
13724 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 e cannot simply
13725 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
13726 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 to the caller.
13727 49 6e 73 74 65 61 64 2c 20 61 62 6f 72 74 20 0a Instead, abort .
13728 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69 ** all queri
13729 65 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 es that may be u
1372a 73 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20 sing any of the
1372b 63 75 72 73 6f 72 73 20 74 68 61 74 20 66 61 69 cursors that fai
1372c 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a 20 20 20 led to save..
1372d 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 */. sqlite3B
1372e 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f treeTripAllCurso
1372f 72 73 28 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 rs(p, rc);. }.#
13730 65 6e 64 69 66 0a 20 20 62 74 72 65 65 49 6e 74 endif. btreeInt
13731 65 67 72 69 74 79 28 70 29 3b 0a 20 20 75 6e 6c egrity(p);. unl
13732 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 70 29 3b ockAllTables(p);
13733 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 .. if( p->inTra
13734 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
13735 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a ){. int rc2;.
13736 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
13737 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
13738 20 20 20 20 70 42 74 2d 3e 6e 54 72 75 6e 63 20 pBt->nTrunc
13739 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 = 0;.#endif..
1373a 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57 assert( TRANS_W
1373b 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 RITE==pBt->inTra
1373c 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20 nsaction );.
1373d 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc2 = sqlite3Pag
1373e 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e erRollback(pBt->
1373f 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 pPager);. if(
13740 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc2!=SQLITE_OK
13741 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 ){. rc = rc
13742 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 2;. }.. /*
13743 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 The rollback ma
13744 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 65 64 y have destroyed
13745 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61 the pPage1->aDa
13746 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20 ta value. So.
13747 20 20 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74 65 ** call sqlite
13748 33 42 74 72 65 65 47 65 74 50 61 67 65 28 29 20 3BtreeGetPage()
13749 6f 6e 20 70 61 67 65 20 31 20 61 67 61 69 6e 20 on page 1 again
1374a 74 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 to make. ** s
1374b 75 72 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 ure pPage1->aDat
1374c 61 20 69 73 20 73 65 74 20 63 6f 72 72 65 63 74 a is set correct
1374d 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 ly. */. if( s
1374e 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
1374f 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 ge(pBt, 1, &pPag
13750 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f e1, 0)==SQLITE_O
13751 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 K ){. relea
13752 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a sePage(pPage1);.
13753 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
13754 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 ( countWriteCurs
13755 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20 ors(pBt)==0 );.
13756 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 pBt->inTransa
13757 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 ction = TRANS_RE
13758 41 44 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 AD;. }.. if( p
13759 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 ->inTrans!=TRANS
1375a 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73 _NONE ){. ass
1375b 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 ert( pBt->nTrans
1375c 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 action>0 );.
1375d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
1375e 6e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d n--;. if( 0==
1375f 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
13760 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e n ){. pBt->
13761 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
13762 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 TRANS_NONE;.
13763 7d 0a 20 20 7d 0a 0a 20 20 70 2d 3e 69 6e 54 72 }. }.. p->inTr
13764 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 ans = TRANS_NONE
13765 3b 0a 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 ;. pBt->inStmt
13766 3d 20 30 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 = 0;. unlockBtr
13767 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b eeIfUnused(pBt);
13768 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 .. btreeIntegri
13769 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 ty(p);. sqlite3
1376a 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1376b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1376c 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 *.** Start a sta
1376d 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 tement subtransa
1376e 63 74 69 6f 6e 2e 20 20 54 68 65 20 73 75 62 74 ction. The subt
1376f 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 0a 2a ransaction can.*
13770 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 * can be rolled
13771 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 back independent
13772 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 ly of the main t
13773 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 59 ransaction..** Y
13774 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 ou must start a
13775 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f transaction befo
13776 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 re starting a su
13777 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a btransaction..**
13778 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 The subtransact
13779 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74 ion is ended aut
1377a 6f 6d 61 74 69 63 61 6c 6c 79 20 69 66 20 74 68 omatically if th
1377b 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 e main transacti
1377c 6f 6e 0a 2a 2a 20 63 6f 6d 6d 69 74 73 20 6f 72 on.** commits or
1377d 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a rolls back..**.
1377e 2a 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 75 62 74 ** Only one subt
1377f 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 62 ransaction may b
13780 65 20 61 63 74 69 76 65 20 61 74 20 61 20 74 69 e active at a ti
13781 6d 65 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72 me. It is an er
13782 72 6f 72 20 74 6f 20 74 72 79 0a 2a 2a 20 74 6f ror to try.** to
13783 20 73 74 61 72 74 20 61 20 6e 65 77 20 73 75 62 start a new sub
13784 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 61 transaction if a
13785 6e 6f 74 68 65 72 20 73 75 62 74 72 61 6e 73 61 nother subtransa
13786 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 ction is already
13787 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 53 active..**.** S
13788 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e tatement subtran
13789 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 sactions are use
1378a 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 64 d around individ
1378b 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ual SQL statemen
1378c 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 63 ts.** that are c
1378d 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 ontained within
1378e 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 a BEGIN...COMMIT
1378f 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 6f block. If a co
13790 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f nstraint.** erro
13791 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 r occurs within
13792 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 the statement, t
13793 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68 61 he effect of tha
13794 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 0a t one statement.
13795 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 ** can be rolled
13796 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 back without ha
13797 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b ving to rollback
13798 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e the entire tran
13799 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 saction..*/.SQLI
1379a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1379b 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e qlite3BtreeBegin
1379c 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b 0a Stmt(Btree *p){.
1379d 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 int rc;. BtSh
1379e 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
1379f 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 Bt;. sqlite3Btr
137a0 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 eeEnter(p);. pB
137a1 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 t->db = p->db;.
137a2 20 69 66 28 20 28 70 2d 3e 69 6e 54 72 61 6e 73 if( (p->inTrans
137a3 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c !=TRANS_WRITE) |
137a4 7c 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29 7b | pBt->inStmt ){
137a5 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 . rc = pBt->r
137a6 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 eadOnly ? SQLITE
137a7 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 _READONLY : SQLI
137a8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 TE_ERROR;. }els
137a9 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
137aa 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
137ab 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n==TRANS_WRITE )
137ac 3b 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e ;. rc = pBt->
137ad 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 readOnly ? SQLIT
137ae 45 5f 4f 4b 20 3a 20 73 71 6c 69 74 65 33 50 61 E_OK : sqlite3Pa
137af 67 65 72 53 74 6d 74 42 65 67 69 6e 28 70 42 74 gerStmtBegin(pBt
137b0 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 ->pPager);. p
137b1 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 31 3b 0a Bt->inStmt = 1;.
137b2 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
137b3 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
137b4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
137b5 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 73 74 ** Commit the st
137b6 61 74 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 atment subtransa
137b7 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 ction currently
137b8 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 49 66 in progress. If
137b9 20 6e 6f 0a 2a 2a 20 73 75 62 74 72 61 6e 73 61 no.** subtransa
137ba 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c ction is active,
137bb 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 this is a no-op
137bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
137bd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
137be 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 28 42 treeCommitStmt(B
137bf 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 tree *p){. int
137c0 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a rc;. BtShared *
137c1 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
137c2 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
137c3 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 r(p);. pBt->db
137c4 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 = p->db;. if( p
137c5 42 74 2d 3e 69 6e 53 74 6d 74 20 26 26 20 21 70 Bt->inStmt && !p
137c6 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a Bt->readOnly ){.
137c7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
137c8 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28 PagerStmtCommit(
137c9 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
137ca 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
137cb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
137cc 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 pBt->inStmt = 0
137cd 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
137ce 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
137cf 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
137d0 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 61 63 74 Rollback the act
137d1 69 76 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 ive statement su
137d2 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 btransaction. I
137d3 66 20 6e 6f 20 73 75 62 74 72 61 6e 73 61 63 74 f no subtransact
137d4 69 6f 6e 0a 2a 2a 20 69 73 20 61 63 74 69 76 65 ion.** is active
137d5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
137d6 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
137d7 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c All cursors will
137d8 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 be invalidated
137d9 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f by this operatio
137da 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 0a n. Any attempt.
137db 2a 2a 20 74 6f 20 75 73 65 20 61 20 63 75 72 73 ** to use a curs
137dc 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e or that was open
137dd 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
137de 67 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 g of this operat
137df 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75 ion.** will resu
137e0 6c 74 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a lt in an error..
137e1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
137e2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
137e3 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 28 42 eeRollbackStmt(B
137e4 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 tree *p){. int
137e5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
137e6 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
137e7 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 = p->pBt;. sqli
137e8 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
137e9 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d ;. pBt->db = p-
137ea 3e 64 62 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e >db;. if( pBt->
137eb 69 6e 53 74 6d 74 20 26 26 20 21 70 42 74 2d 3e inStmt && !pBt->
137ec 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 readOnly ){.
137ed 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
137ee 72 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 70 42 rStmtRollback(pB
137ef 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 t->pPager);.
137f0 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b pBt->inStmt = 0;
137f1 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
137f2 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
137f3 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
137f4 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
137f5 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42 cursor for the B
137f6 54 72 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 Tree whose root
137f7 69 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a is on the page.*
137f8 2a 20 69 54 61 62 6c 65 2e 20 20 54 68 65 20 61 * iTable. The a
137f9 63 74 20 6f 66 20 61 63 71 75 69 72 69 6e 67 20 ct of acquiring
137fa 61 20 63 75 72 73 6f 72 20 67 65 74 73 20 61 20 a cursor gets a
137fb 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a read lock on .**
137fc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
137fd 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 le..**.** If wrF
137fe 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 lag==0, then the
137ff 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 cursor can only
13800 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 be used for rea
13801 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c ding..** If wrFl
13802 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 ag==1, then the
13803 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73 cursor can be us
13804 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f ed for reading o
13805 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 r for.** writing
13806 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 if other condit
13807 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67 ions for writing
13808 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 are also met.
13809 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 These.** are the
1380a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 conditions that
1380b 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20 must be met in
1380c 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e order for writin
1380d 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 g to.** be allow
1380e 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 ed:.**.** 1: Th
1380f 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 e cursor must ha
13810 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 ve been opened w
13811 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a ith wrFlag==1.**
13812 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61 .** 2: Other da
13813 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
13814 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74 68 ns that share th
13815 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61 63 e same pager cac
13816 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68 he.** but wh
13817 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 ich are not in t
13818 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 he READ_UNCOMMIT
13819 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f TED state may no
1381a 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 t have.** cu
1381b 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20 rsors open with
1381c 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 wrFlag==0 on the
1381d 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 same table. Ot
1381e 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 herwise.** t
1381f 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 he changes made
13820 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63 75 by this write cu
13821 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69 rsor would be vi
13822 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 sible to.**
13823 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72 73 the read cursors
13824 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61 in the other da
13825 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
13826 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 n..**.** 3: The
13827 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 database must b
13828 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 e writable (not
13829 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 on read-only med
1382a 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 ia).**.** 4: Th
1382b 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61 ere must be an a
1382c 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f ctive transactio
1382d 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 n..**.** No chec
1382e 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 king is done to
1382f 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 70 make sure that p
13830 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c age iTable reall
13831 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 y is the.** root
13832 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65 page of a b-tre
13833 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 e. If it is not
13834 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f , then the curso
13835 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69 r acquired.** wi
13836 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 ll not work corr
13837 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 ectly..**.** It
13838 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 is assumed that
13839 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 the sqlite3Btree
1383a 43 75 72 73 6f 72 53 69 7a 65 28 29 20 62 79 74 CursorSize() byt
1383b 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a es of memory .**
1383c 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
1383d 43 75 72 20 68 61 76 65 20 62 65 65 6e 20 7a 65 Cur have been ze
1383e 72 6f 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c roed by the call
1383f 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e er..*/.static in
13840 74 20 62 74 72 65 65 43 75 72 73 6f 72 28 0a 20 t btreeCursor(.
13841 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 Btree *p,
13842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13843 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 /* The b
13844 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 tree */. int iT
13845 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 able,
13846 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13847 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 /* Root page of
13848 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a table to open *
13849 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 /. int wrFlag,
1384a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1384b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 /* 1
1384c 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 to write. 0 read
1384d 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 -only */. struc
1384e 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 t KeyInfo *pKeyI
1384f 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 nfo,
13850 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 /* First arg t
13851 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e o comparison fun
13852 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 ction */. BtCur
13853 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 sor *pCur
13854 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13855 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6e /* Space for n
13856 65 77 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a ew cursor */.){.
13857 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 int rc;. BtSh
13858 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
13859 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 Bt;.. assert( s
1385a 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1385b 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 69 66 Mutex(p) );. if
1385c 28 20 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 ( wrFlag ){.
1385d 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c if( pBt->readOnl
1385e 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 y ){. retur
1385f 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c n SQLITE_READONL
13860 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 Y;. }. if(
13861 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 checkReadLocks(
13862 70 2c 20 69 54 61 62 6c 65 2c 20 30 2c 20 30 29 p, iTable, 0, 0)
13863 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
13864 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a SQLITE_LOCKED;.
13865 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
13866 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 pBt->pPage1==0
13867 29 7b 0a 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b ){. rc = lock
13868 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28 70 BtreeWithRetry(p
13869 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
1386a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1386b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1386c 20 7d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e }. if( pBt->
1386d 72 65 61 64 4f 6e 6c 79 20 26 26 20 77 72 46 6c readOnly && wrFl
1386e 61 67 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ag ){. retu
1386f 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e rn SQLITE_READON
13870 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 LY;. }. }.
13871 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d pCur->pgnoRoot =
13872 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 (Pgno)iTable;.
13873 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 if( iTable==1 &
13874 26 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 & pagerPagecount
13875 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 30 (pBt->pPager)==0
13876 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
13877 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20 20 20 67 ITE_EMPTY;. g
13878 6f 74 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f oto create_curso
13879 72 5f 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d r_exception;. }
1387a 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e . rc = getAndIn
1387b 69 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 itPage(pBt, pCur
1387c 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 ->pgnoRoot, &pCu
1387d 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 r->apPage[0]);.
1387e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1387f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 63 OK ){. goto c
13880 72 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 reate_cursor_exc
13881 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f eption;. }.. /
13882 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 * Now that no ot
13883 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f her errors can o
13884 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c ccur, finish fil
13885 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 ling in the BtCu
13886 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 rsor. ** variab
13887 6c 65 73 2c 20 6c 69 6e 6b 20 74 68 65 20 63 75 les, link the cu
13888 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74 rsor into the Bt
13889 53 68 61 72 65 64 20 6c 69 73 74 20 61 6e 64 20 Shared list and
1388a 73 65 74 20 2a 70 70 43 75 72 20 28 74 68 65 0a set *ppCur (the.
1388b 20 20 2a 2a 20 6f 75 74 70 75 74 20 61 72 67 75 ** output argu
1388c 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e ment to this fun
1388d 63 74 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20 20 70 ction).. */. p
1388e 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 Cur->pKeyInfo =
1388f 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 pKeyInfo;. pCur
13890 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 ->pBtree = p;.
13891 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b pCur->pBt = pBt;
13892 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 . pCur->wrFlag
13893 3d 20 77 72 46 6c 61 67 3b 0a 20 20 70 43 75 72 = wrFlag;. pCur
13894 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 ->pNext = pBt->p
13895 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 Cursor;. if( pC
13896 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 ur->pNext ){.
13897 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 pCur->pNext->pP
13898 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a rev = pCur;. }.
13899 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d pBt->pCursor =
1389a 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65 pCur;. pCur->e
1389b 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 State = CURSOR_I
1389c 4e 56 41 4c 49 44 3b 0a 0a 20 20 72 65 74 75 72 NVALID;.. retur
1389d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72 n SQLITE_OK;..cr
1389e 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 eate_cursor_exce
1389f 70 74 69 6f 6e 3a 0a 20 20 72 65 6c 65 61 73 65 ption:. release
138a0 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 Page(pCur->apPag
138a1 65 5b 30 5d 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42 e[0]);. unlockB
138a2 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 treeIfUnused(pBt
138a3 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
138a4 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
138a5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
138a6 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 eCursor(. Btree
138a7 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
138a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
138a9 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 /* The bt
138aa 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 ree */. int iTa
138ab 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ble,
138ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
138ad 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
138ae 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 e of table to op
138af 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c en */. int wrFl
138b0 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ag,
138b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
138b2 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 /* 1 to writ
138b3 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a e. 0 read-only *
138b4 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e /. struct KeyIn
138b5 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 fo *pKeyInfo,
138b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
138b7 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 /* First arg to
138b8 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 xCompare() */.
138b9 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 BtCursor *pCur
138ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
138bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
138bc 72 69 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20 rite new cursor
138bd 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
138be 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
138bf 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 reeEnter(p);. p
138c0 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 ->pBt->db = p->d
138c1 62 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 b;. rc = btreeC
138c2 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c ursor(p, iTable,
138c3 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 wrFlag, pKeyInf
138c4 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 o, pCur);. sqli
138c5 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
138c6 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
138c7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
138c8 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
138c9 43 75 72 73 6f 72 53 69 7a 65 28 29 7b 0a 20 20 CursorSize(){.
138ca 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 42 74 return sizeof(Bt
138cb 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 0a 0a 2f 2a Cursor);.}..../*
138cc 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 .** Close a curs
138cd 6f 72 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f or. The read lo
138ce 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
138cf 73 65 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 se file is relea
138d0 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 sed.** when the
138d1 6c 61 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 last cursor is c
138d2 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 losed..*/.SQLITE
138d3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
138d4 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 ite3BtreeCloseCu
138d5 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 rsor(BtCursor *p
138d6 43 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 Cur){. Btree *p
138d7 42 74 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 Btree = pCur->pB
138d8 74 72 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 tree;. if( pBtr
138d9 65 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b ee ){. int i;
138da 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 . BtShared *p
138db 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a Bt = pCur->pBt;.
138dc 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
138dd 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 Enter(pBtree);.
138de 20 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 42 74 pBt->db = pBt
138df 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c ree->db;. sql
138e0 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 ite3BtreeClearCu
138e1 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 20 20 rsor(pCur);.
138e2 69 66 28 20 70 43 75 72 2d 3e 70 50 72 65 76 20 if( pCur->pPrev
138e3 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 ){. pCur->p
138e4 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43 Prev->pNext = pC
138e5 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d ur->pNext;. }
138e6 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 42 74 2d else{. pBt-
138e7 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 2d >pCursor = pCur-
138e8 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
138e9 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 if( pCur->pNex
138ea 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d t ){. pCur-
138eb 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 >pNext->pPrev =
138ec 70 43 75 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20 pCur->pPrev;.
138ed 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 }. for(i=0;
138ee 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 i<=pCur->iPage;
138ef 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 i++){. rele
138f0 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 asePage(pCur->ap
138f1 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a Page[i]);. }.
138f2 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 unlockBtreeI
138f3 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 fUnused(pBt);.
138f4 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 invalidateOver
138f5 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b flowCache(pCur);
138f6 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f . /* sqlite3_
138f7 66 72 65 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20 free(pCur); */.
138f8 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
138f9 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 20 20 eave(pBtree);.
138fa 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
138fb 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d E_OK;.}../*.** M
138fc 61 6b 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ake a temporary
138fd 63 75 72 73 6f 72 20 62 79 20 66 69 6c 6c 69 6e cursor by fillin
138fe 67 20 69 6e 20 74 68 65 20 66 69 65 6c 64 73 20 g in the fields
138ff 6f 66 20 70 54 65 6d 70 43 75 72 2e 0a 2a 2a 20 of pTempCur..**
13900 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 63 75 The temporary cu
13901 72 73 6f 72 20 69 73 20 6e 6f 74 20 6f 6e 20 74 rsor is not on t
13902 68 65 20 63 75 72 73 6f 72 20 6c 69 73 74 20 66 he cursor list f
13903 6f 72 20 74 68 65 20 42 74 72 65 65 2e 0a 2a 2f or the Btree..*/
13904 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13905 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
13906 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 eGetTempCursor(B
13907 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 tCursor *pCur, B
13908 74 43 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 tCursor *pTempCu
13909 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 r){. int i;. a
1390a 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
1390b 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
1390c 0a 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 43 . memcpy(pTempC
1390d 75 72 2c 20 70 43 75 72 2c 20 73 69 7a 65 6f 66 ur, pCur, sizeof
1390e 28 42 74 43 75 72 73 6f 72 29 29 3b 0a 20 20 70 (BtCursor));. p
1390f 54 65 6d 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d TempCur->pNext =
13910 20 30 3b 0a 20 20 70 54 65 6d 70 43 75 72 2d 3e 0;. pTempCur->
13911 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 66 6f 72 pPrev = 0;. for
13912 28 69 3d 30 3b 20 69 3c 3d 70 54 65 6d 70 43 75 (i=0; i<=pTempCu
13913 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a r->iPage; i++){.
13914 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
13915 52 65 66 28 70 54 65 6d 70 43 75 72 2d 3e 61 70 Ref(pTempCur->ap
13916 50 61 67 65 5b 69 5d 2d 3e 70 44 62 50 61 67 65 Page[i]->pDbPage
13917 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
13918 44 65 6c 65 74 65 20 61 20 74 65 6d 70 6f 72 61 Delete a tempora
13919 72 79 20 63 75 72 73 6f 72 20 73 75 63 68 20 61 ry cursor such a
1391a 73 20 77 61 73 20 6d 61 64 65 20 62 79 20 74 68 s was made by th
1391b 65 20 43 72 65 61 74 65 54 65 6d 70 6f 72 61 72 e CreateTemporar
1391c 79 43 75 72 73 6f 72 28 29 0a 2a 2a 20 66 75 6e yCursor().** fun
1391d 63 74 69 6f 6e 20 61 62 6f 76 65 2e 0a 2a 2f 0a ction above..*/.
1391e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1391f 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
13920 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 6f ReleaseTempCurso
13921 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 r(BtCursor *pCur
13922 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 ){. int i;. as
13923 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
13924 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
13925 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 for(i=0; i<=pC
13926 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b ur->iPage; i++){
13927 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
13928 72 55 6e 72 65 66 28 70 43 75 72 2d 3e 61 70 50 rUnref(pCur->apP
13929 61 67 65 5b 69 5d 2d 3e 70 44 62 50 61 67 65 29 age[i]->pDbPage)
1392a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d ;. }.}../*.** M
1392b 61 6b 65 20 73 75 72 65 20 74 68 65 20 42 74 43 ake sure the BtC
1392c 75 72 73 6f 72 2a 20 67 69 76 65 6e 20 69 6e 20 ursor* given in
1392d 74 68 65 20 61 72 67 75 6d 65 6e 74 20 68 61 73 the argument has
1392e 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42 74 43 75 a valid.** BtCu
1392f 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72 75 63 74 rsor.info struct
13930 75 72 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e ure. If it is n
13931 6f 74 20 61 6c 72 65 61 64 79 20 76 61 6c 69 64 ot already valid
13932 2c 20 63 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 , call.** sqlite
13933 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 3BtreeParseCell(
13934 29 20 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e ) to fill it in.
13935 0a 2a 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e .**.** BtCursor.
13936 69 6e 66 6f 20 69 73 20 61 20 63 61 63 68 65 20 info is a cache
13937 6f 66 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 of the informati
13938 6f 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e on in the curren
13939 74 20 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 t cell..** Using
1393a 20 74 68 69 73 20 63 61 63 68 65 20 72 65 64 75 this cache redu
1393b 63 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ces the number o
1393c 66 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 f calls to sqlit
1393d 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
1393e 28 29 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 37 2d 30 ()..**.** 2007-0
1393f 36 2d 32 35 3a 20 20 54 68 65 72 65 20 69 73 20 6-25: There is
13940 61 20 62 75 67 20 69 6e 20 73 6f 6d 65 20 76 65 a bug in some ve
13941 72 73 69 6f 6e 73 20 6f 66 20 4d 53 56 43 20 74 rsions of MSVC t
13942 68 61 74 20 63 61 75 73 65 20 74 68 65 0a 2a 2a hat cause the.**
13943 20 63 6f 6d 70 69 6c 65 72 20 74 6f 20 63 72 61 compiler to cra
13944 73 68 20 77 68 65 6e 20 67 65 74 43 65 6c 6c 49 sh when getCellI
13945 6e 66 6f 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 nfo() is impleme
13946 6e 74 65 64 20 61 73 20 61 20 6d 61 63 72 6f 2e nted as a macro.
13947 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 .** But there is
13948 20 61 20 6d 65 61 73 75 72 65 61 62 6c 65 20 73 a measureable s
13949 70 65 65 64 20 61 64 76 61 6e 74 61 67 65 20 74 peed advantage t
1394a 6f 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 72 o using the macr
1394b 6f 20 6f 6e 20 67 63 63 0a 2a 2a 20 28 77 68 65 o on gcc.** (whe
1394c 6e 20 6c 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 n less compiler
1394d 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 6c 69 optimizations li
1394e 6b 65 20 2d 4f 73 20 6f 72 20 2d 4f 30 20 61 72 ke -Os or -O0 ar
1394f 65 20 75 73 65 64 20 61 6e 64 20 74 68 65 0a 2a e used and the.*
13950 2a 20 63 6f 6d 70 69 6c 65 72 20 69 73 20 6e 6f * compiler is no
13951 74 20 64 6f 69 6e 67 20 61 67 72 65 73 73 69 76 t doing agressiv
13952 65 20 69 6e 6c 69 6e 69 6e 67 2e 29 20 20 53 6f e inlining.) So
13953 20 77 65 20 75 73 65 20 61 20 72 65 61 6c 20 66 we use a real f
13954 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 4d unction.** for M
13955 53 56 43 20 61 6e 64 20 61 20 6d 61 63 72 6f 20 SVC and a macro
13956 66 6f 72 20 65 76 65 72 79 74 68 69 6e 67 20 65 for everything e
13957 6c 73 65 2e 20 20 54 69 63 6b 65 74 20 23 32 34 lse. Ticket #24
13958 35 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 57..*/.#ifndef N
13959 44 45 42 55 47 0a 20 20 73 74 61 74 69 63 20 76 DEBUG. static v
1395a 6f 69 64 20 61 73 73 65 72 74 43 65 6c 6c 49 6e oid assertCellIn
1395b 66 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 fo(BtCursor *pCu
1395c 72 29 7b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f r){. CellInfo
1395d 20 69 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 69 info;. int i
1395e 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 Page = pCur->iPa
1395f 67 65 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 ge;. memset(&
13960 69 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 info, 0, sizeof(
13961 69 6e 66 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 info));. sqli
13962 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
13963 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 l(pCur->apPage[i
13964 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 Page], pCur->aiI
13965 64 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f dx[iPage], &info
13966 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d );. assert( m
13967 65 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 emcmp(&info, &pC
13968 75 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 ur->info, sizeof
13969 28 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 (info))==0 );.
1396a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e }.#else. #defin
1396b 65 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f e assertCellInfo
1396c 28 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 (x).#endif.#ifde
1396d 66 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 f _MSC_VER. /*
1396e 55 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 Use a real funct
1396f 69 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 ion in MSVC to w
13970 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 ork around bugs
13971 69 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 in that compiler
13972 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f . */. static vo
13973 69 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 id getCellInfo(B
13974 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
13975 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e if( pCur->in
13976 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 fo.nSize==0 ){.
13977 20 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d int iPage =
13978 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 pCur->iPage;.
13979 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1397a 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e ParseCell(pCur->
1397b 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 apPage[iPage],pC
1397c 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d ur->aiIdx[iPage]
1397d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 ,&pCur->info);.
1397e 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 pCur->valid
1397f 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 7d 65 NKey = 1;. }e
13980 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
13981 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b tCellInfo(pCur);
13982 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 . }. }.#else
13983 20 2f 2a 20 69 66 20 6e 6f 74 20 5f 4d 53 43 5f /* if not _MSC_
13984 56 45 52 20 2a 2f 0a 20 20 2f 2a 20 55 73 65 20 VER */. /* Use
13985 61 20 6d 61 63 72 6f 20 69 6e 20 61 6c 6c 20 6f a macro in all o
13986 74 68 65 72 20 63 6f 6d 70 69 6c 65 72 73 20 73 ther compilers s
13987 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 o that the funct
13988 69 6f 6e 20 69 73 20 69 6e 6c 69 6e 65 64 20 2a ion is inlined *
13989 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 43 65 6c /.#define getCel
1398a 6c 49 6e 66 6f 28 70 43 75 72 29 20 20 20 20 20 lInfo(pCur)
1398b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1398c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1398d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1398e 20 5c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 \. if( pCur->i
1398f 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 20 nfo.nSize==0 ){
13990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13992 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13993 20 20 5c 0a 20 20 20 20 69 6e 74 20 69 50 61 67 \. int iPag
13994 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b e = pCur->iPage;
13995 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13996 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13998 20 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 65 33 \. sqlite3
13999 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 BtreeParseCell(p
1399a 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 Cur->apPage[iPag
1399b 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 e],pCur->aiIdx[i
1399c 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 Page],&pCur->inf
1399d 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d 3e o); \. pCur->
1399e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 20 validNKey = 1;
1399f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139a2 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 \. }else{
139a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139a7 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73 65 \. asse
139a8 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 rtCellInfo(pCur)
139a9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
139aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139ac 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e \. }.#en
139ad 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20 dif /* _MSC_VER
139ae 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 */../*.** Set *p
139af 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65 Size to the size
139b0 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6e of the buffer n
139b1 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 eeded to hold th
139b2 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68 e value of.** th
139b3 65 20 6b 65 79 20 66 6f 72 20 74 68 65 20 63 75 e key for the cu
139b4 72 72 65 6e 74 20 65 6e 74 72 79 2e 20 20 49 66 rrent entry. If
139b5 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e the cursor is n
139b6 6f 74 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 ot pointing.** t
139b7 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2c o a valid entry,
139b8 20 2a 70 53 69 7a 65 20 69 73 20 73 65 74 20 74 *pSize is set t
139b9 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 o 0. .**.** For
139ba 61 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 a table with the
139bb 20 49 4e 54 4b 45 59 20 66 6c 61 67 20 73 65 74 INTKEY flag set
139bc 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 , this routine r
139bd 65 74 75 72 6e 73 20 74 68 65 20 6b 65 79 0a 2a eturns the key.*
139be 2a 20 69 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68 * itself, not th
139bf 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
139c0 73 20 69 6e 20 74 68 65 20 6b 65 79 2e 0a 2a 2f s in the key..*/
139c1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
139c2 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
139c3 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 KeySize(BtCursor
139c4 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69 *pCur, i64 *pSi
139c5 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a ze){. int rc;..
139c6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
139c7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
139c8 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f );. rc = resto
139c9 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
139ca 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
139cb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
139cc 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
139cd 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
139ce 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d INVALID || pCur-
139cf 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
139d0 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28 VALID );. if(
139d1 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
139d2 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
139d3 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 . *pSize =
139d4 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
139d5 20 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 getCellInfo(
139d6 70 43 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 pCur);. *pS
139d7 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f ize = pCur->info
139d8 2e 6e 4b 65 79 3b 0a 20 20 20 20 7d 0a 20 20 7d .nKey;. }. }
139d9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
139da 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a ./*.** Set *pSiz
139db 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 e to the number
139dc 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 of bytes of data
139dd 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74 68 in the entry th
139de 65 0a 2a 2a 20 63 75 72 73 6f 72 20 63 75 72 72 e.** cursor curr
139df 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e ently points to.
139e0 20 20 41 6c 77 61 79 73 20 72 65 74 75 72 6e 20 Always return
139e1 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 46 61 SQLITE_OK..** Fa
139e2 69 6c 75 72 65 20 69 73 20 6e 6f 74 20 70 6f 73 ilure is not pos
139e3 73 69 62 6c 65 2e 20 20 49 66 20 74 68 65 20 63 sible. If the c
139e4 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 63 75 72 ursor is not cur
139e5 72 65 6e 74 6c 79 0a 2a 2a 20 70 6f 69 6e 74 69 rently.** pointi
139e6 6e 67 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 28 ng to an entry (
139e7 77 68 69 63 68 20 63 61 6e 20 68 61 70 70 65 6e which can happen
139e8 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 , for example, i
139e9 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 f.** the databas
139ea 65 20 69 73 20 65 6d 70 74 79 29 20 74 68 65 6e e is empty) then
139eb 20 2a 70 53 69 7a 65 20 69 73 20 73 65 74 20 74 *pSize is set t
139ec 6f 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 o 0..*/.SQLITE_P
139ed 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
139ee 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 e3BtreeDataSize(
139ef 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
139f0 75 33 32 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 u32 *pSize){. i
139f1 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
139f2 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
139f3 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
139f4 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
139f5 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
139f6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
139f7 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
139f8 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
139f9 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
139fa 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d || pCur->eState=
139fb 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
139fc 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 . if( pCur->e
139fd 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
139fe 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2f VALID ){. /
139ff 2a 20 4e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 * Not pointing a
13a00 74 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 20 t a valid entry
13a01 2d 20 73 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 - set *pSize to
13a02 30 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70 53 69 0. */. *pSi
13a03 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ze = 0;. }els
13a04 65 7b 0a 20 20 20 20 20 20 67 65 74 43 65 6c 6c e{. getCell
13a05 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 Info(pCur);.
13a06 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d *pSize = pCur-
13a07 3e 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 >info.nData;.
13a08 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
13a09 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 rc;.}../*.** Giv
13a0a 65 6e 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 en the page numb
13a0b 65 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f er of an overflo
13a0c 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 w page in the da
13a0d 74 61 62 61 73 65 20 28 70 61 72 61 6d 65 74 65 tabase (paramete
13a0e 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74 68 69 73 r.** ovfl), this
13a0f 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 64 73 20 function finds
13a10 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
13a11 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 of the next page
13a12 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b in the .** link
13a13 65 64 20 6c 69 73 74 20 6f 66 20 6f 76 65 72 66 ed list of overf
13a14 6c 6f 77 20 70 61 67 65 73 2e 20 49 66 20 70 6f low pages. If po
13a15 73 73 69 62 6c 65 2c 20 69 74 20 75 73 65 73 20 ssible, it uses
13a16 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a the auto-vacuum.
13a17 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 64 ** pointer-map d
13a18 61 74 61 20 69 6e 73 74 65 61 64 20 6f 66 20 72 ata instead of r
13a19 65 61 64 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 eading the conte
13a1a 6e 74 20 6f 66 20 70 61 67 65 20 6f 76 66 6c 20 nt of page ovfl
13a1b 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a to do so. .**.**
13a1c 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
13a1d 75 72 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72 urs an SQLite er
13a1e 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
13a1f 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 3a rned. Otherwise:
13a20 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 70 50 .**.** Unless pP
13a21 67 6e 6f 4e 65 78 74 20 69 73 20 4e 55 4c 4c 2c gnoNext is NULL,
13a22 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
13a23 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76 65 of the next ove
13a24 72 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 20 69 rflow .** page i
13a25 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 n the linked lis
13a26 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 t is written to
13a27 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 70 *pPgnoNext. If p
13a28 61 67 65 20 6f 76 66 6c 0a 2a 2a 20 69 73 20 74 age ovfl.** is t
13a29 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 he last page in
13a2a 69 74 73 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c its linked list,
13a2b 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 *pPgnoNext is s
13a2c 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a et to zero. .**.
13a2d 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69 73 20 ** If ppPage is
13a2e 6e 6f 74 20 4e 55 4c 4c 2c 20 2a 70 70 50 61 67 not NULL, *ppPag
13a2f 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 e is set to the
13a30 4d 65 6d 50 61 67 65 2a 20 68 61 6e 64 6c 65 0a MemPage* handle.
13a31 2a 2a 20 66 6f 72 20 70 61 67 65 20 6f 76 66 6c ** for page ovfl
13a32 2e 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 . The underlying
13a33 20 70 61 67 65 72 20 70 61 67 65 20 6d 61 79 20 pager page may
13a34 68 61 76 65 20 62 65 65 6e 20 72 65 71 75 65 73 have been reques
13a35 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 ted.** with the
13a36 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61 67 20 73 noContent flag s
13a37 65 74 2c 20 73 6f 20 74 68 65 20 70 61 67 65 20 et, so the page
13a38 64 61 74 61 20 61 63 63 65 73 73 61 62 6c 65 20 data accessable
13a39 76 69 61 0a 2a 2a 20 74 68 69 73 20 68 61 6e 64 via.** this hand
13a3a 6c 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 72 le may not be tr
13a3b 75 73 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 usted..*/.static
13a3c 20 69 6e 74 20 67 65 74 4f 76 65 72 66 6c 6f 77 int getOverflow
13a3d 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
13a3e 20 2a 70 42 74 2c 20 0a 20 20 50 67 6e 6f 20 6f *pBt, . Pgno o
13a3f 76 66 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 vfl,
13a40 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 66 6c /* Overfl
13a41 6f 77 20 70 61 67 65 20 2a 2f 0a 20 20 4d 65 6d ow page */. Mem
13a42 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 Page **ppPage,
13a43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
13a44 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 6c 65 : MemPage handle
13a45 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e */. Pgno *pPgn
13a46 6f 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20 oNext
13a47 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20 /* OUT: Next
13a48 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 overflow page nu
13a49 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e mber */.){. Pgn
13a4a 6f 20 6e 65 78 74 20 3d 20 30 3b 0a 20 20 69 6e o next = 0;. in
13a4b 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
13a4c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
13a4d 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
13a4e 29 3b 0a 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 );. /* One of t
13a4f 68 65 73 65 20 6d 75 73 74 20 6e 6f 74 20 62 65 hese must not be
13a50 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 NULL. Otherwise
13a51 2c 20 77 68 79 20 63 61 6c 6c 20 74 68 69 73 20 , why call this
13a52 66 75 6e 63 74 69 6f 6e 3f 20 2a 2f 0a 20 20 61 function? */. a
13a53 73 73 65 72 74 28 70 70 50 61 67 65 20 7c 7c 20 ssert(ppPage ||
13a54 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a 20 20 2f pPgnoNext);.. /
13a55 2a 20 49 66 20 70 50 67 6e 6f 4e 65 78 74 20 69 * If pPgnoNext i
13a56 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 s NULL, then thi
13a57 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 s function is be
13a58 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 ing called to ob
13a59 74 61 69 6e 0a 20 20 2a 2a 20 61 20 4d 65 6d 50 tain. ** a MemP
13a5a 61 67 65 2a 20 72 65 66 65 72 65 6e 63 65 20 6f age* reference o
13a5b 6e 6c 79 2e 20 4e 6f 20 70 61 67 65 2d 64 61 74 nly. No page-dat
13a5c 61 20 69 73 20 72 65 71 75 69 72 65 64 20 69 6e a is required in
13a5d 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f this case.. */
13a5e 0a 20 20 69 66 28 20 21 70 50 67 6e 6f 4e 65 78 . if( !pPgnoNex
13a5f 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
13a60 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
13a61 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 70 age(pBt, ovfl, p
13a62 70 50 61 67 65 2c 20 31 29 3b 0a 20 20 7d 0a 0a pPage, 1);. }..
13a63 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
13a64 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
13a65 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 /* Try to find
13a66 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e the next page in
13a67 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 the overflow li
13a68 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a st using the. *
13a69 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69 * autovacuum poi
13a6a 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20 nter-map pages.
13a6b 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e Guess that the n
13a6c 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a ext page in . *
13a6d 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c * the overflow l
13a6e 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62 ist is page numb
13a6f 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20 er (ovfl+1). If
13a70 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73 that guess turns
13a71 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 . ** out to be
13a72 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63 wrong, fall bac
13a73 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 k to loading the
13a74 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20 data of page .
13a75 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20 ** number ovfl
13a76 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
13a77 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65 next page numbe
13a78 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 r.. */. if( pB
13a79 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
13a7a 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a . Pgno pgno;.
13a7b 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20 Pgno iGuess
13a7c 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38 = ovfl+1;. u8
13a7d 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69 eType;.. whi
13a7e 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 le( PTRMAP_ISPAG
13a7f 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c E(pBt, iGuess) |
13a80 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e | iGuess==PENDIN
13a81 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
13a82 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73 ){. iGuess
13a83 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 ++;. }.. i
13a84 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 72 f( iGuess<=pager
13a85 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 Pagecount(pBt->p
13a86 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 Pager) ){.
13a87 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 rc = ptrmapGet(p
13a88 42 74 2c 20 69 47 75 65 73 73 2c 20 26 65 54 79 Bt, iGuess, &eTy
13a89 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20 20 pe, &pgno);.
13a8a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13a8b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
13a8c 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
13a8d 7d 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 }. if( eTyp
13a8e 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
13a8f 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d 6f 76 66 OW2 && pgno==ovf
13a90 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 65 78 l ){. nex
13a91 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 20 20 20 t = iGuess;.
13a92 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 }. }. }.#e
13a93 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 65 78 74 ndif.. if( next
13a94 3d 3d 30 20 7c 7c 20 70 70 50 61 67 65 20 29 7b ==0 || ppPage ){
13a95 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 . MemPage *pP
13a96 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63 age = 0;.. rc
13a97 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
13a98 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c etPage(pBt, ovfl
13a99 2c 20 26 70 50 61 67 65 2c 20 6e 65 78 74 21 3d , &pPage, next!=
13a9a 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 72 0);. assert(r
13a9b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
13a9c 70 50 61 67 65 3d 3d 30 29 3b 0a 20 20 20 20 69 pPage==0);. i
13a9d 66 28 20 6e 65 78 74 3d 3d 30 20 26 26 20 72 63 f( next==0 && rc
13a9e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13a9f 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 34 next = get4
13aa0 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 byte(pPage->aDat
13aa1 61 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 a);. }.. i
13aa2 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20 f( ppPage ){.
13aa3 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 *ppPage = pPa
13aa4 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ge;. }else{.
13aa5 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
13aa6 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 (pPage);. }.
13aa7 20 7d 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 }. *pPgnoNext
13aa8 3d 20 6e 65 78 74 3b 0a 0a 20 20 72 65 74 75 72 = next;.. retur
13aa9 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
13aaa 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61 20 opy data from a
13aab 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67 65 buffer to a page
13aac 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 65 , or from a page
13aad 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a to a buffer..**
13aae 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 20 .** pPayload is
13aaf 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 a pointer to dat
13ab0 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 61 a stored on data
13ab1 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67 base page pDbPag
13ab2 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e e..** If argumen
13ab3 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c 20 t eOp is false,
13ab4 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65 73 then nByte bytes
13ab5 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 of data are cop
13ab6 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79 ied.** from pPay
13ab7 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66 66 load to the buff
13ab8 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62 79 er pointed at by
13ab9 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69 73 pBuf. If eOp is
13aba 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 true,.** then s
13abb 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
13abc 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 () is called on
13abd 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79 74 pDbPage and nByt
13abe 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 e bytes.** of da
13abf 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 ta are copied fr
13ac0 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 om the buffer pB
13ac1 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a uf to pPayload..
13ac2 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
13ac3 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 is returned on s
13ac4 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69 73 uccess, otherwis
13ac5 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e e an error code.
13ac6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
13ac7 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f opyPayload(. vo
13ac8 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20 id *pPayload,
13ac9 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
13aca 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61 20 er to page data
13acb 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c */. void *pBuf,
13acc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13acd 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 * Pointer to buf
13ace 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 fer */. int nBy
13acf 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
13ad0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
13ad1 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f bytes to copy */
13ad2 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 20 . int eOp,
13ad3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13ad4 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70 0 -> copy from p
13ad5 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74 age, 1 -> copy t
13ad6 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 61 o page */. DbPa
13ad7 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20 20 ge *pDbPage
13ad8 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f /* Page co
13ad9 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61 ntaining pPayloa
13ada 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f d */.){. if( eO
13adb 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 p ){. /* Copy
13adc 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65 data from buffe
13add 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72 69 r to page (a wri
13ade 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f te operation) */
13adf 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 . int rc = sq
13ae0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
13ae1 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
13ae2 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13ae3 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
13ae4 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 rc;. }. me
13ae5 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 mcpy(pPayload, p
13ae6 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d Buf, nByte);. }
13ae7 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 else{. /* Cop
13ae8 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67 65 y data from page
13ae9 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72 65 to buffer (a re
13aea 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f ad operation) */
13aeb 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66 . memcpy(pBuf
13aec 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74 , pPayload, nByt
13aed 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e e);. }. return
13aee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
13aef 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
13af0 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 on is used to re
13af1 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65 20 ad or overwrite
13af2 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 payload informat
13af3 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 ion.** for the e
13af4 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70 43 ntry that the pC
13af5 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 ur cursor is poi
13af6 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 68 65 nting to. If the
13af7 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74 65 eOp.** paramete
13af8 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 73 20 r is 0, this is
13af9 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e a read operation
13afa 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 69 6e (data copied in
13afb 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 to.** buffer pBu
13afc 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 6e f). If it is non
13afd 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 20 28 -zero, a write (
13afe 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d data copied from
13aff 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 .** buffer pBuf)
13b00 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 ..**.** A total
13b01 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 20 61 of "amt" bytes a
13b02 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 re read or writt
13b03 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 en beginning at
13b04 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74 "offset"..** Dat
13b05 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72 20 a is read to or
13b06 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 from the buffer
13b07 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 pBuf..**.** This
13b08 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f routine does no
13b09 74 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 t make a distinc
13b0a 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 6b 65 79 tion between key
13b0b 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 49 74 and data..** It
13b0c 20 6a 75 73 74 20 72 65 61 64 73 20 6f 72 20 77 just reads or w
13b0d 72 69 74 65 73 20 62 79 74 65 73 20 66 72 6f 6d rites bytes from
13b0e 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 the payload are
13b0f 61 2e 20 20 44 61 74 61 20 6d 69 67 68 74 20 0a a. Data might .
13b10 2a 2a 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 ** appear on the
13b11 20 6d 61 69 6e 20 70 61 67 65 20 6f 72 20 62 65 main page or be
13b12 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 20 6f scattered out o
13b13 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 n multiple overf
13b14 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 73 2e 0a 2a low .** pages..*
13b15 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 43 75 *.** If the BtCu
13b16 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62 48 rsor.isIncrblobH
13b17 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 andle flag is se
13b18 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 t, and the curre
13b19 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e 74 nt.** cursor ent
13b1a 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d ry uses one or m
13b1b 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ore overflow pag
13b1c 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f es, this functio
13b1d 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20 73 n.** allocates s
13b1e 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a pace for and laz
13b1f 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 74 68 ily popluates th
13b20 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d e overflow page-
13b21 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 20 61 list .** cache a
13b22 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e 61 rray (BtCursor.a
13b23 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 73 65 Overflow). Subse
13b24 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 20 quent calls use
13b25 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 74 6f this.** cache to
13b26 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f make seeking to
13b27 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f 66 the supplied of
13b28 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 63 69 fset more effici
13b29 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 ent..**.** Once
13b2a 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
13b2b 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20 -list cache has
13b2c 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 been allocated,
13b2d 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 76 it may be.** inv
13b2e 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f 6d 65 alidated if some
13b2f 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 77 72 other cursor wr
13b30 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d 65 ites to the same
13b31 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a table, or if.**
13b32 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d the cursor is m
13b33 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 oved to a differ
13b34 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 69 6f ent row. Additio
13b35 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d 76 nally, in auto-v
13b36 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74 acuum.** mode, t
13b37 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 65 he following eve
13b38 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 64 61 nts may invalida
13b39 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 te an overflow p
13b3a 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 0a age-list cache..
13b3b 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63 **.** * An inc
13b3c 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c remental vacuum,
13b3d 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74 .** * A commit
13b3e 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d in auto_vacuum=
13b3f 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20 "full" mode,.**
13b40 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20 74 * Creating a t
13b41 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69 72 able (may requir
13b42 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72 e moving an over
13b43 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a 73 flow page)..*/.s
13b44 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73 73 tatic int access
13b45 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 Payload(. BtCur
13b46 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 sor *pCur,
13b47 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 /* Cursor pointi
13b48 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 ng to entry to r
13b49 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e ead from */. in
13b4a 74 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20 t offset,
13b4b 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 /* Begin read
13b4c 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 6e 74 ing this far int
13b4d 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 69 o payload */. i
13b4e 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
13b4f 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 69 73 /* Read this
13b50 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a 20 many bytes */.
13b51 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
13b52 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 20 74 pBuf, /* Write t
13b53 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 74 68 he bytes into th
13b54 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a 20 20 is buffer */ .
13b55 69 6e 74 20 73 6b 69 70 4b 65 79 2c 20 20 20 20 int skipKey,
13b56 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 62 /* offset b
13b57 65 67 69 6e 73 20 61 74 20 64 61 74 61 20 69 66 egins at data if
13b58 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f this is true */
13b59 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 . int eOp
13b5a 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 /* zero
13b5b 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 to read. non-zer
13b5c 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 o to write. */.)
13b5d 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 {. unsigned cha
13b5e 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 r *aPayload;. i
13b5f 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
13b60 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 K;. u32 nKey;.
13b61 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 int iIdx = 0;.
13b62 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
13b63 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
13b64 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 Cur->iPage]; /*
13b65 42 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 Btree page of cu
13b66 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 rrent entry */.
13b67 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 BtShared *pBt;
13b68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13b69 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13b6a 42 74 72 65 65 20 74 68 69 73 20 63 75 72 73 6f Btree this curso
13b6b 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a r belongs to */.
13b6c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13b6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
13b6e 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
13b6f 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 OR_VALID );. as
13b70 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
13b71 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
13b72 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
13b73 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e assert( offset>
13b74 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
13b75 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
13b76 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 (pCur) );.. get
13b77 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a CellInfo(pCur);.
13b78 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 aPayload = pCu
13b79 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 r->info.pCell +
13b7a 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 pCur->info.nHead
13b7b 65 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 er;. nKey = (pP
13b7c 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 age->intKey ? 0
13b7d 3a 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 : pCur->info.nKe
13b7e 79 29 3b 0a 0a 20 20 69 66 28 20 73 6b 69 70 4b y);.. if( skipK
13b7f 65 79 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 ey ){. offset
13b80 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 += nKey;. }.
13b81 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 20 3e if( offset+amt >
13b82 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f nKey+pCur->info
13b83 2e 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 2f 2a .nData ){. /*
13b84 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 64 20 Trying to read
13b85 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 74 68 or write past th
13b86 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61 74 e end of the dat
13b87 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 2a 2f a is an error */
13b88 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
13b89 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
13b8a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b . }.. /* Check
13b8b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 62 65 if data must be
13b8c 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74 6f read/written to
13b8d 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 /from the btree
13b8e 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f 0a page itself. */.
13b8f 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43 75 if( offset<pCu
13b90 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 r->info.nLocal )
13b91 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d {. int a = am
13b92 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f 66 66 t;. if( a+off
13b93 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e set>pCur->info.n
13b94 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 61 Local ){. a
13b95 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c = pCur->info.nL
13b96 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b 0a 20 ocal - offset;.
13b97 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6f }. rc = co
13b98 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c pyPayload(&aPayl
13b99 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 42 75 oad[offset], pBu
13b9a 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 67 65 f, a, eOp, pPage
13b9b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
13b9c 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 offset = 0;.
13b9d 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 61 pBuf += a;. a
13b9e 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73 65 mt -= a;. }else
13b9f 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 {. offset -=
13ba0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
13ba1 6c 3b 0a 20 20 7d 0a 0a 20 20 70 42 74 20 3d 20 l;. }.. pBt =
13ba2 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 69 66 28 pCur->pBt;. if(
13ba3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
13ba4 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 & amt>0 ){. c
13ba5 6f 6e 73 74 20 69 6e 74 20 6f 76 66 6c 53 69 7a onst int ovflSiz
13ba6 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
13ba7 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 ize - 4; /* Byt
13ba8 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f es content per o
13ba9 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 vfl page */.
13baa 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a Pgno nextPage;..
13bab 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 nextPage = g
13bac 65 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 et4byte(&aPayloa
13bad 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f d[pCur->info.nLo
13bae 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 cal]);..#ifndef
13baf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
13bb0 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 BLOB. /* If t
13bb1 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e he isIncrblobHan
13bb2 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 dle flag is set
13bb3 61 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 and the BtCursor
13bb4 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 .aOverflow[].
13bb5 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ** has not been
13bb6 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f allocated, allo
13bb7 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 cate it now. The
13bb8 20 61 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 array is sized
13bb9 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e at. ** one en
13bba 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 try for each ove
13bbb 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 rflow page in th
13bbc 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e e overflow chain
13bbd 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 . The. ** pag
13bbe 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
13bbf 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 first overflow p
13bc0 61 67 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e age is stored in
13bc1 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 aOverflow[0],.
13bc2 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c ** etc. A val
13bc3 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 ue of 0 in the a
13bc4 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 Overflow[] array
13bc5 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 means "not yet
13bc6 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 known". ** (t
13bc7 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 he cache is lazi
13bc8 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 ly populated)..
13bc9 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 */. if( pC
13bca 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 ur->isIncrblobHa
13bcb 6e 64 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 ndle && !pCur->a
13bcc 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
13bcd 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 int nOvfl = (p
13bce 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f Cur->info.nPaylo
13bcf 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c ad-pCur->info.nL
13bd0 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 ocal+ovflSize-1)
13bd1 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 /ovflSize;.
13bd2 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
13bd3 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 = (Pgno *)sqlit
13bd4 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a e3MallocZero(siz
13bd5 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 eof(Pgno)*nOvfl)
13bd6 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66 ;. if( nOvf
13bd7 6c 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 l && !pCur->aOve
13bd8 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 rflow ){.
13bd9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
13bda 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 EM;. }.
13bdb 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
13bdc 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c overflow page-l
13bdd 69 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65 ist cache has be
13bde 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 en allocated and
13bdf 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 the. ** entr
13be0 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 y for the first
13be1 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f required overflo
13be2 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c w page is valid,
13be3 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 skip. ** dir
13be4 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 ectly to it..
13be5 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 */. if( pCur
13be6 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 ->aOverflow && p
13be7 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f Cur->aOverflow[o
13be8 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 ffset/ovflSize]
13be9 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20 ){. iIdx =
13bea 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 (offset/ovflSize
13beb 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67 );. nextPag
13bec 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 e = pCur->aOverf
13bed 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 low[iIdx];.
13bee 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65 offset = (offse
13bef 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 t%ovflSize);.
13bf0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66 }.#endif.. f
13bf1 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45 or( ; rc==SQLITE
13bf2 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20 _OK && amt>0 &&
13bf3 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b nextPage; iIdx++
13bf4 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ){..#ifndef SQLI
13bf5 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
13bf6 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71 . /* If req
13bf7 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 uired, populate
13bf8 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
13bf9 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f e-list cache. */
13bfa 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d . if( pCur-
13bfb 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 >aOverflow ){.
13bfc 20 20 20 20 20 20 61 73 73 65 72 74 28 21 70 43 assert(!pC
13bfd 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 ur->aOverflow[iI
13bfe 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 dx] || pCur->aOv
13bff 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 erflow[iIdx]==ne
13c00 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 xtPage);.
13c01 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
13c02 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 [iIdx] = nextPag
13c03 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 e;. }.#endi
13c04 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 f.. if( off
13c05 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b set>=ovflSize ){
13c06 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
13c07 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 only reason to r
13c08 65 61 64 20 74 68 69 73 20 70 61 67 65 20 69 73 ead this page is
13c09 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 to obtain the p
13c0a 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e age. ** n
13c0b 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 umber for the ne
13c0c 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f xt page in the o
13c0d 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 verflow chain. T
13c0e 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 he page.
13c0f 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 ** data is not r
13c10 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 equired. So firs
13c11 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 t try to lookup
13c12 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 the overflow.
13c13 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 ** page-lis
13c14 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c t cache, if any,
13c15 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 then fall back
13c16 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c to the getOverfl
13c17 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 owPage().
13c18 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 ** function..
13c19 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 */.#ifndef
13c1a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
13c1b 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66 RBLOB. if
13c1c 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f ( pCur->aOverflo
13c1d 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 w && pCur->aOver
13c1e 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a flow[iIdx+1] ){.
13c1f 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 nextPa
13c20 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 ge = pCur->aOver
13c21 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 flow[iIdx+1];.
13c22 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 } else .#e
13c23 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 72 ndif. r
13c24 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 c = getOverflowP
13c25 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 age(pBt, nextPag
13c26 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 e, 0, &nextPage)
13c27 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 ;. offset
13c28 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 -= ovflSize;.
13c29 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13c2a 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 /* Need to re
13c2b 61 64 20 74 68 69 73 20 70 61 67 65 20 70 72 6f ad this page pro
13c2c 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 perly. It contai
13c2d 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 ns some of the.
13c2e 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 ** range
13c2f 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 73 20 of data that is
13c30 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d being read (eOp=
13c31 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28 =0) or written (
13c32 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 eOp!=0)..
13c33 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61 */. DbPa
13c34 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 ge *pDbPage;.
13c35 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 int a = amt
13c36 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
13c37 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
13c38 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 Bt->pPager, next
13c39 50 61 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b Page, &pDbPage);
13c3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
13c3b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13c3c 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 aPayload
13c3d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
13c3e 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b etData(pDbPage);
13c3f 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 . nextP
13c40 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 age = get4byte(a
13c41 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 Payload);.
13c42 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 if( a + offs
13c43 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b et > ovflSize ){
13c44 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 20 3d . a =
13c45 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 ovflSize - offs
13c46 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a et;. }.
13c47 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 rc = c
13c48 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 opyPayload(&aPay
13c49 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 load[offset+4],
13c4a 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 pBuf, a, eOp, pD
13c4b 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
13c4c 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
13c4d 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ref(pDbPage);.
13c4e 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d offset =
13c4f 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d 0;. am
13c50 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 t -= a;.
13c51 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 pBuf += a;.
13c52 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
13c53 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
13c54 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
13c55 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 amt>0 ){. re
13c56 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
13c57 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 UPT_BKPT;. }.
13c58 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
13c59 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 .** Read part of
13c5a 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 the key associa
13c5b 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 ted with cursor
13c5c 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a pCur. Exactly.*
13c5d 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 * "amt" bytes wi
13c5e 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 ll be transfered
13c5f 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 into pBuf[]. T
13c60 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 he transfer.** b
13c61 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 egins at "offset
13c62 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 "..**.** Return
13c63 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
13c64 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 cess or an error
13c65 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e code if anythin
13c66 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e g goes.** wrong.
13c67 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 An error is re
13c68 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 turned if "offse
13c69 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 t+amt" is larger
13c6a 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 than.** the ava
13c6b 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a ilable payload..
13c6c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
13c6d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
13c6e 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a eeKey(BtCursor *
13c6f 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 pCur, u32 offset
13c70 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 , u32 amt, void
13c71 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 *pBuf){. int rc
13c72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
13c73 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
13c74 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 ur) );. rc = re
13c75 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
13c76 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 ion(pCur);. if(
13c77 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13c78 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 {. assert( pC
13c79 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
13c7a 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 OR_VALID );.
13c7b 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 assert( pCur->iP
13c7c 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e age>=0 && pCur->
13c7d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
13c7e 67 65 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ge] );. if( p
13c7f 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
13c80 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 intKey ){.
13c81 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
13c82 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
13c83 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
13c84 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
13c85 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 iPage]<pCur->apP
13c86 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
13c87 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 ->nCell );. r
13c88 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 c = accessPayloa
13c89 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 d(pCur, offset,
13c8a 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 amt, (unsigned c
13c8b 68 61 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29 har*)pBuf, 0, 0)
13c8c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
13c8d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 c;.}../*.** Read
13c8e 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 part of the dat
13c8f 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
13c90 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 h cursor pCur.
13c91 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 Exactly.** "amt"
13c92 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 bytes will be t
13c93 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 ransfered into p
13c94 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e Buf[]. The tran
13c95 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 sfer.** begins a
13c96 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a t "offset"..**.*
13c97 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
13c98 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 OK on success or
13c99 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
13c9a 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a f anything goes.
13c9b 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 ** wrong. An er
13c9c 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
13c9d 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 if "offset+amt"
13c9e 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a is larger than.*
13c9f 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 * the available
13ca0 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 payload..*/.SQLI
13ca1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
13ca2 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 qlite3BtreeData(
13ca3 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
13ca4 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
13ca5 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 amt, void *pBuf)
13ca6 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 {. int rc;..#if
13ca7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
13ca8 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 _INCRBLOB. if (
13ca9 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
13caa 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
13cab 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
13cac 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 TE_ABORT;. }.#e
13cad 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 ndif.. assert(
13cae 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
13caf 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d (pCur) );. rc =
13cb0 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
13cb1 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 sition(pCur);.
13cb2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
13cb3 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
13cb4 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
13cb5 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
13cb6 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
13cb7 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 >iPage>=0 && pCu
13cb8 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
13cb9 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 iPage] );. as
13cba 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
13cbb 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
13cbc 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
13cbd 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 ->iPage]->nCell
13cbe 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 );. rc = acce
13cbf 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 ssPayload(pCur,
13cc0 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 offset, amt, pBu
13cc1 66 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 f, 1, 0);. }.
13cc2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
13cc3 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
13cc4 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 nter to payload
13cc5 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d information from
13cc6 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 the entry that
13cc7 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 the .** pCur cur
13cc8 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 sor is pointing
13cc9 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 to. The pointer
13cca 20 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e is to the begin
13ccb 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b ning of.** the k
13ccc 65 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 ey if skipKey==0
13ccd 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 and it points t
13cce 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 o the beginning
13ccf 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b of data if.** sk
13cd0 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e ipKey==1. The n
13cd1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
13cd2 66 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f f available key/
13cd3 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a data is written.
13cd4 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 ** into *pAmt.
13cd5 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 If *pAmt==0, the
13cd6 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
13cd7 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 rned will not be
13cd8 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e .** a valid poin
13cd9 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ter..**.** This
13cda 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 routine is an op
13cdb 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 timization. It
13cdc 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 is common for th
13cdd 65 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 e entire key.**
13cde 61 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 and data to fit
13cdf 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 on the local pag
13ce0 65 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 e and for there
13ce1 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f to be no overflo
13ce2 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 w.** pages. Whe
13ce3 6e 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 n that is so, th
13ce4 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 is routine can b
13ce5 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 e used to access
13ce6 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 the.** key and
13ce7 64 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b data without mak
13ce8 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 ing a copy. If
13ce9 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 the key and/or d
13cea 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e ata spills.** on
13ceb 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
13cec 73 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 s, then accessPa
13ced 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 yload() must be
13cee 75 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 used to reassemb
13cef 6c 79 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 ly.** the key/da
13cf0 74 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 ta and copy it i
13cf1 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 nto a preallocat
13cf2 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a ed buffer..**.**
13cf3 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 The pointer ret
13cf4 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f urned by this ro
13cf5 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 utine looks dire
13cf6 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 ctly into the ca
13cf7 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 ched.** page of
13cf8 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
13cf9 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 he data might ch
13cfa 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 ange or move the
13cfb 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e next time.** an
13cfc 79 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 y btree routine
13cfd 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 is called..*/.st
13cfe 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
13cff 6e 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 ned char *fetchP
13d00 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 ayload(. BtCurs
13d01 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f or *pCur, /
13d02 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e * Cursor pointin
13d03 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 g to entry to re
13d04 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 ad from */. int
13d05 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 *pAmt,
13d06 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e /* Write the n
13d07 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 umber of availab
13d08 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f le bytes here */
13d09 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 . int skipKey
13d0a 20 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 /* read
13d0b 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 beginning at dat
13d0c 61 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 a if this is tru
13d0d 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e e */.){. unsign
13d0e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 ed char *aPayloa
13d0f 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 d;. MemPage *pP
13d10 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b age;. u32 nKey;
13d11 0a 20 20 69 6e 74 20 6e 4c 6f 63 61 6c 3b 0a 0a . int nLocal;..
13d12 20 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d assert( pCur!=
13d13 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 0 && pCur->iPage
13d14 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 >=0 && pCur->apP
13d15 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
13d16 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
13d17 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
13d18 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 R_VALID );. ass
13d19 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
13d1a 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
13d1b 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
13d1c 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
13d1d 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 e];. assert( pC
13d1e 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
13d1f 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 iPage]<pPage->nC
13d20 65 6c 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c ell );. getCell
13d21 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 Info(pCur);. aP
13d22 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 ayload = pCur->i
13d23 6e 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 nfo.pCell;. aPa
13d24 79 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 yload += pCur->i
13d25 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 nfo.nHeader;. i
13d26 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 f( pPage->intKey
13d27 20 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 ){. nKey = 0
13d28 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e ;. }else{. n
13d29 4b 65 79 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f Key = pCur->info
13d2a 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 .nKey;. }. if(
13d2b 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 skipKey ){.
13d2c 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 aPayload += nKey
13d2d 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 ;. nLocal = p
13d2e 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
13d2f 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 - nKey;. }else
13d30 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 {. nLocal = p
13d31 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
13d32 3b 0a 20 20 20 20 69 66 28 20 6e 4c 6f 63 61 6c ;. if( nLocal
13d33 3e 6e 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e >nKey ){. n
13d34 4c 6f 63 61 6c 20 3d 20 6e 4b 65 79 3b 0a 20 20 Local = nKey;.
13d35 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 41 6d 74 20 }. }. *pAmt
13d36 3d 20 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65 74 75 = nLocal;. retu
13d37 72 6e 20 61 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a rn aPayload;.}..
13d38 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 ./*.** For the e
13d39 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f 72 ntry that cursor
13d3a 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74 pCur is point t
13d3b 6f 2c 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 o, return as.**
13d3c 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68 many bytes of th
13d3d 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73 e key or data as
13d3e 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f are available o
13d3f 6e 20 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 n the local.** b
13d40 2d 74 72 65 65 20 70 61 67 65 2e 20 20 57 72 69 -tree page. Wri
13d41 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 te the number of
13d42 20 61 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 available bytes
13d43 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a into *pAmt..**.
13d44 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 ** The pointer r
13d45 65 74 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d eturned is ephem
13d46 65 72 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 eral. The key/d
13d47 61 74 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 ata may move.**
13d48 6f 72 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 or be destroyed
13d49 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c on the next call
13d4a 20 74 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f to any Btree ro
13d4b 75 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64 utine,.** includ
13d4c 69 6e 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f ing calls from o
13d4d 74 68 65 72 20 74 68 72 65 61 64 73 20 61 67 61 ther threads aga
13d4e 69 6e 73 74 20 74 68 65 20 73 61 6d 65 20 63 61 inst the same ca
13d4f 63 68 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 che..** Hence, a
13d50 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 mutex on the Bt
13d51 53 68 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 Shared should be
13d52 20 68 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 held prior to c
13d53 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 alling.** this r
13d54 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 outine..**.** Th
13d55 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20 ese routines is
13d56 75 73 65 64 20 74 6f 20 67 65 74 20 71 75 69 63 used to get quic
13d57 6b 20 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20 k access to key
13d58 61 6e 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74 and data.** in t
13d59 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 he common case w
13d5a 68 65 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 here no overflow
13d5b 20 70 61 67 65 73 20 61 72 65 20 75 73 65 64 2e pages are used.
13d5c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13d5d 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 TE const void *s
13d5e 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 qlite3BtreeKeyFe
13d5f 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 tch(BtCursor *pC
13d60 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a ur, int *pAmt){.
13d61 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
13d62 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
13d63 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e );. if( pCur->
13d64 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
13d65 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 ALID ){. retu
13d66 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 rn (const void*)
13d67 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 fetchPayload(pCu
13d68 72 2c 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d r, pAmt, 0);. }
13d69 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 . return 0;.}.S
13d6a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
13d6b 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
13d6c 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 3BtreeDataFetch(
13d6d 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
13d6e 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 61 73 int *pAmt){. as
13d6f 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
13d70 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
13d71 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 if( pCur->eSta
13d72 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
13d73 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 ){. return (
13d74 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 const void*)fetc
13d75 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 hPayload(pCur, p
13d76 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 Amt, 1);. }. r
13d77 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn 0;.}.../*.
13d78 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 ** Move the curs
13d79 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 or down to a new
13d7a 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 child page. Th
13d7b 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 e newPgno argume
13d7c 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 nt is the.** pag
13d7d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
13d7e 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f child page to mo
13d7f 76 65 20 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 ve to..*/.static
13d80 20 69 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 int moveToChild
13d81 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
13d82 20 75 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 u32 newPgno){.
13d83 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 int rc;. int i
13d84 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a = pCur->iPage;.
13d85 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 MemPage *pNewP
13d86 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 age;. BtShared
13d87 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 *pBt = pCur->pBt
13d88 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
13d89 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
13d8a 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
13d8b 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
13d8c 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
13d8d 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
13d8e 50 61 67 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 Page<BTCURSOR_MA
13d8f 58 5f 44 45 50 54 48 20 29 3b 0a 20 20 69 66 28 X_DEPTH );. if(
13d90 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 pCur->iPage>=(B
13d91 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 TCURSOR_MAX_DEPT
13d92 48 2d 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 H-1) ){. retu
13d93 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
13d94 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 T_BKPT;. }. rc
13d95 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 = getAndInitPag
13d96 65 28 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 e(pBt, newPgno,
13d97 26 70 4e 65 77 50 61 67 65 29 3b 0a 20 20 69 66 &pNewPage);. if
13d98 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
13d99 3b 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 ;. pCur->apPage
13d9a 5b 69 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 [i+1] = pNewPage
13d9b 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b ;. pCur->aiIdx[
13d9c 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 i+1] = 0;. pCur
13d9d 2d 3e 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 ->iPage++;.. pC
13d9e 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d ur->info.nSize =
13d9f 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 0;. pCur->vali
13da0 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 dNKey = 0;. if(
13da1 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c pNewPage->nCell
13da2 3c 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e <1 ){. return
13da3 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
13da4 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 BKPT;. }. retu
13da5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
13da6 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
13da7 2f 2a 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65 /*.** Page pPare
13da8 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 nt is an interna
13da9 6c 20 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65 l (non-leaf) tre
13daa 65 20 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e e page. This fun
13dab 63 74 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74 ction .** assert
13dac 73 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 s that page numb
13dad 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 er iChild is the
13dae 20 6c 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74 left-child if t
13daf 68 65 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65 he iIdx'th.** ce
13db0 6c 6c 20 69 6e 20 70 61 67 65 20 70 50 61 72 65 ll in page pPare
13db1 6e 74 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20 nt. Or, if iIdx
13db2 69 73 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 is equal to the
13db3 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a total number of.
13db4 2a 2a 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 ** cells in pPar
13db5 65 6e 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e ent, that page n
13db6 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 umber iChild is
13db7 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 the right-child
13db8 6f 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a of.** the page..
13db9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
13dba 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 ssertParentIndex
13dbb 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e (MemPage *pParen
13dbc 74 2c 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e t, int iIdx, Pgn
13dbd 6f 20 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73 o iChild){. ass
13dbe 65 72 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65 ert( iIdx<=pPare
13dbf 6e 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 nt->nCell );. i
13dc0 66 28 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 f( iIdx==pParent
13dc1 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 ->nCell ){. a
13dc2 73 73 65 72 74 28 20 67 65 74 34 62 79 74 65 28 ssert( get4byte(
13dc3 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b &pParent->aData[
13dc4 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 pParent->hdrOffs
13dc5 65 74 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 et+8])==iChild )
13dc6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
13dc7 73 73 65 72 74 28 20 67 65 74 34 62 79 74 65 28 ssert( get4byte(
13dc8 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 findCell(pParent
13dc9 2c 20 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 , iIdx))==iChild
13dca 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a );. }.}.#else.
13dcb 23 20 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 # define assert
13dcc 50 61 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c ParentIndex(x,y,
13dcd 7a 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a z) .#endif../*.*
13dce 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
13dcf 72 20 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 r up to the pare
13dd0 6e 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 nt page..**.** p
13dd1 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 Cur->idx is set
13dd2 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 to the cell inde
13dd3 78 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 x that contains
13dd4 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 the pointer.** t
13dd5 6f 20 74 68 65 20 70 61 67 65 20 77 65 20 61 72 o the page we ar
13dd6 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 e coming from.
13dd7 49 66 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 If we are coming
13dd8 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 from the.** rig
13dd9 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 ht-most child pa
13dda 67 65 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 ge then pCur->id
13ddb 78 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 x is set to one
13ddc 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 more than.** the
13ddd 20 6c 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e largest cell in
13dde 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 dex..*/.SQLITE_P
13ddf 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
13de0 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 te3BtreeMoveToPa
13de1 72 65 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 rent(BtCursor *p
13de2 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Cur){. assert(
13de3 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
13de4 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
13de5 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
13de6 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
13de7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
13de8 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 ->iPage>0 );. a
13de9 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
13dea 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
13deb 20 29 3b 0a 20 20 61 73 73 65 72 74 50 61 72 65 );. assertPare
13dec 6e 74 49 6e 64 65 78 28 0a 20 20 20 20 70 43 75 ntIndex(. pCu
13ded 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
13dee 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 iPage-1], . p
13def 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
13df0 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 >iPage-1], .
13df1 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
13df2 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f 0a r->iPage]->pgno.
13df3 20 20 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 );. releasePa
13df4 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ge(pCur->apPage[
13df5 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 pCur->iPage]);.
13df6 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a pCur->iPage--;.
13df7 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
13df8 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ze = 0;. pCur->
13df9 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 7d validNKey = 0;.}
13dfa 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 ../*.** Move the
13dfb 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 72 cursor to the r
13dfc 6f 6f 74 20 70 61 67 65 0a 2a 2f 0a 73 74 61 74 oot page.*/.stat
13dfd 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f ic int moveToRoo
13dfe 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 t(BtCursor *pCur
13dff 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 ){. MemPage *pR
13e00 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 oot;. int rc =
13e01 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72 SQLITE_OK;. Btr
13e02 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 ee *p = pCur->pB
13e03 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 tree;. BtShared
13e04 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
13e05 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
13e06 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
13e07 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 ) );. assert( C
13e08 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 URSOR_INVALID <
13e09 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
13e0a 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 EK );. assert(
13e0b 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c CURSOR_VALID <
13e0c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
13e0d 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 EEK );. assert(
13e0e 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 CURSOR_FAULT
13e0f 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 > CURSOR_REQUIRE
13e10 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 SEEK );. if( pC
13e11 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 ur->eState>=CURS
13e12 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
13e13 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e {. if( pCur->
13e14 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 eState==CURSOR_F
13e15 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 72 65 AULT ){. re
13e16 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b turn pCur->skip;
13e17 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
13e18 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
13e19 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 or(pCur);. }..
13e1a 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
13e1b 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 >=0 ){. int i
13e1c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 ;. for(i=1; i
13e1d 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 <=pCur->iPage; i
13e1e 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 ++){. relea
13e1f 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 sePage(pCur->apP
13e20 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 age[i]);. }.
13e21 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 }else{. if(
13e22 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b . SQLITE_OK
13e23 21 3d 28 72 63 20 3d 20 67 65 74 41 6e 64 49 6e !=(rc = getAndIn
13e24 69 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 itPage(pBt, pCur
13e25 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 ->pgnoRoot, &pCu
13e26 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 29 0a 20 r->apPage[0])).
13e27 20 20 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 ){. pCur
13e28 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
13e29 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 R_INVALID;.
13e2a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
13e2b 7d 0a 20 20 7d 0a 0a 20 20 70 52 6f 6f 74 20 3d }. }.. pRoot =
13e2c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
13e2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f ;. assert( pRoo
13e2e 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 t->pgno==pCur->p
13e2f 67 6e 6f 52 6f 6f 74 20 29 3b 0a 20 20 70 43 75 gnoRoot );. pCu
13e30 72 2d 3e 69 50 61 67 65 20 3d 20 30 3b 0a 20 20 r->iPage = 0;.
13e31 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d pCur->aiIdx[0] =
13e32 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 0;. pCur->info
13e33 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 .nSize = 0;. pC
13e34 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a ur->atLast = 0;.
13e35 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
13e36 79 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 y = 0;.. if( pR
13e37 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 oot->nCell==0 &&
13e38 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b !pRoot->leaf ){
13e39 0a 20 20 20 20 50 67 6e 6f 20 73 75 62 70 61 67 . Pgno subpag
13e3a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e;. assert( p
13e3b 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 3b Root->pgno==1 );
13e3c 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d 20 67 . subpage = g
13e3d 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e et4byte(&pRoot->
13e3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 aData[pRoot->hdr
13e3f 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
13e40 61 73 73 65 72 74 28 20 73 75 62 70 61 67 65 3e assert( subpage>
13e41 30 20 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 0 );. pCur->e
13e42 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 State = CURSOR_V
13e43 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 3d 20 6d ALID;. rc = m
13e44 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
13e45 20 73 75 62 70 61 67 65 29 3b 0a 20 20 7d 65 6c subpage);. }el
13e46 73 65 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 se{. pCur->eS
13e47 74 61 74 65 20 3d 20 28 28 70 52 6f 6f 74 2d 3e tate = ((pRoot->
13e48 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f nCell>0)?CURSOR_
13e49 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f 49 4e 56 VALID:CURSOR_INV
13e4a 41 4c 49 44 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ALID);. }. ret
13e4b 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
13e4c 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 Move the cursor
13e4d 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 down to the lef
13e4e 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 t-most leaf entr
13e4f 79 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a y beneath the.**
13e50 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 entry to which
13e51 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 it is currently
13e52 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 pointing..**.**
13e53 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 The left-most le
13e54 61 66 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 af is the one wi
13e55 74 68 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 th the smallest
13e56 6b 65 79 20 2d 20 74 68 65 20 66 69 72 73 74 0a key - the first.
13e57 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 ** in ascending
13e58 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 order..*/.static
13e59 20 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d int moveToLeftm
13e5a 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ost(BtCursor *pC
13e5b 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f ur){. Pgno pgno
13e5c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
13e5d 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 ITE_OK;. MemPag
13e5e 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 e *pPage;.. ass
13e5f 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
13e60 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
13e61 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
13e62 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
13e63 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 LID );. while(
13e64 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
13e65 20 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d !(pPage = pCur-
13e66 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
13e67 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 age])->leaf ){.
13e68 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
13e69 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
13e6a 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ge]<pPage->nCell
13e6b 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 );. pgno = g
13e6c 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c et4byte(findCell
13e6d 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 61 69 (pPage, pCur->ai
13e6e 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
13e6f 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 ));. rc = mov
13e70 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 eToChild(pCur, p
13e71 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 gno);. }. retu
13e72 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
13e73 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
13e74 64 6f 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68 down to the righ
13e75 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 t-most leaf entr
13e76 79 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a y beneath the.**
13e77 20 70 61 67 65 20 74 6f 20 77 68 69 63 68 20 69 page to which i
13e78 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 t is currently p
13e79 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 ointing. Notice
13e7a 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a the difference.
13e7b 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 ** between moveT
13e7c 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 oLeftmost() and
13e7d 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 moveToRightmost(
13e7e 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f ). moveToLeftmo
13e7f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 st().** finds th
13e80 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 e left-most entr
13e81 79 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65 y beneath the *e
13e82 6e 74 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f ntry* whereas mo
13e83 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a veToRightmost().
13e84 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 72 69 67 ** finds the rig
13e85 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 ht-most entry be
13e86 6e 65 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a neath the *page*
13e87 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 ..**.** The righ
13e88 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20 t-most entry is
13e89 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 the one with the
13e8a 20 6c 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74 largest key - t
13e8b 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 he last.** key i
13e8c 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
13e8d 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
13e8e 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
13e8f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
13e90 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 {. Pgno pgno;.
13e91 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
13e92 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a _OK;. MemPage *
13e93 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 pPage;.. assert
13e94 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
13e95 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
13e96 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
13e97 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
13e98 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d );. while( rc=
13e99 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 =SQLITE_OK && !(
13e9a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
13e9b 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
13e9c 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ])->leaf ){.
13e9d 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
13e9e 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
13e9f 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
13ea0 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 ]);. pCur->ai
13ea1 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
13ea2 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b = pPage->nCell;
13ea3 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
13ea4 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f Child(pCur, pgno
13ea5 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
13ea6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13ea7 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
13ea8 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 ur->iPage] = pPa
13ea9 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 ge->nCell-1;.
13eaa 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
13eab 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d e = 0;. pCur-
13eac 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
13ead 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13eae 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 .}../* Move the
13eaf 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 cursor to the fi
13eb0 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 rst entry in the
13eb1 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 table. Return
13eb2 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 SQLITE_OK.** on
13eb3 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 success. Set *p
13eb4 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 Res to 0 if the
13eb5 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 cursor actually
13eb6 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 points to someth
13eb7 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 ing.** or set *p
13eb8 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 Res to 1 if the
13eb9 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a table is empty..
13eba 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
13ebb 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
13ebc 65 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 eeFirst(BtCursor
13ebd 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 *pCur, int *pRe
13ebe 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 s){. int rc;..
13ebf 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
13ec0 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
13ec1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
13ec2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
13ec3 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
13ec4 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 ->mutex) );. rc
13ec5 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 = moveToRoot(pC
13ec6 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ur);. if( rc==S
13ec7 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13ec8 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
13ec9 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
13eca 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
13ecb 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ( pCur->apPage[p
13ecc 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
13ecd 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a ll==0 );. *
13ece 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 pRes = 1;.
13ecf 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
13ed0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13ed1 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
13ed2 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
13ed3 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 e]->nCell>0 );.
13ed4 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a *pRes = 0;.
13ed5 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 rc = moveT
13ed6 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b oLeftmost(pCur);
13ed7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
13ed8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f urn rc;.}../* Mo
13ed9 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ve the cursor to
13eda 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
13edb 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 in the table. R
13edc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a eturn SQLITE_OK.
13edd 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 ** on success.
13ede 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 Set *pRes to 0 i
13edf 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 f the cursor act
13ee0 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 ually points to
13ee1 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 something.** or
13ee2 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 set *pRes to 1 i
13ee3 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 f the table is e
13ee4 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mpty..*/.SQLITE_
13ee5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
13ee6 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 te3BtreeLast(BtC
13ee7 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 ursor *pCur, int
13ee8 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 *pRes){. int r
13ee9 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 c;. . assert( c
13eea 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
13eeb 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
13eec 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
13eed 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 _held(pCur->pBtr
13eee 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ee->db->mutex) )
13eef 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 ;. rc = moveToR
13ef0 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 oot(pCur);. if(
13ef1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13ef2 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 {. if( CURSOR
13ef3 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e _INVALID==pCur->
13ef4 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 eState ){.
13ef5 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
13ef6 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
13ef7 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 ]->nCell==0 );.
13ef8 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a *pRes = 1;.
13ef9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13efa 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
13efb 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
13efc 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 LID );. *pR
13efd 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 es = 0;. rc
13efe 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f = moveToRightmo
13eff 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 st(pCur);.
13f00 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 getCellInfo(pCur
13f01 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 );. pCur->a
13f02 74 4c 61 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49 tLast = rc==SQLI
13f03 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
13f04 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
13f05 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 ./* Move the cur
13f06 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 sor so that it p
13f07 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 oints to an entr
13f08 79 20 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a y near the key .
13f09 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ** specified by
13f0a 70 49 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 pIdxKey or intKe
13f0b 79 2e 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 y. Return a su
13f0c 63 63 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a ccess code..**.*
13f0d 2a 20 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 * For INTKEY tab
13f0e 6c 65 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 les, the intKey
13f0f 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 parameter is use
13f10 64 2e 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 d. pIdxKey .**
13f11 6d 75 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 must be NULL. F
13f12 6f 72 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c or index tables,
13f13 20 70 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 pIdxKey is used
13f14 20 61 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 and intKey.** i
13f15 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a s ignored..**.**
13f16 20 49 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 If an exact mat
13f17 63 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c ch is not found,
13f18 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 then the cursor
13f19 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 is always.** le
13f1a 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 ft pointing at a
13f1b 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 leaf page which
13f1c 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 would hold the
13f1d 65 6e 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 entry if it.** w
13f1e 65 72 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 ere present. Th
13f1f 65 20 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 e cursor might p
13f20 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 oint to an entry
13f21 20 74 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 that comes.** b
13f22 65 66 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 efore or after t
13f23 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 he key..**.** Th
13f24 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70 e result of comp
13f25 61 72 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69 aring the key wi
13f26 74 68 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 th the entry to
13f27 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 63 75 72 which the.** cur
13f28 73 6f 72 20 69 73 20 77 72 69 74 74 65 6e 20 74 sor is written t
13f29 6f 20 2a 70 52 65 73 20 69 66 20 70 52 65 73 21 o *pRes if pRes!
13f2a 3d 4e 55 4c 4c 2e 20 20 54 68 65 20 6d 65 61 6e =NULL. The mean
13f2b 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 69 73 20 76 ing of.** this v
13f2c 61 6c 75 65 20 69 73 20 61 73 20 66 6f 6c 6c 6f alue is as follo
13f2d 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 ws:.**.** *p
13f2e 52 65 73 3c 30 20 20 20 20 20 20 54 68 65 20 63 Res<0 The c
13f2f 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f ursor is left po
13f30 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 inting at an ent
13f31 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 ry that.**
13f32 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 73 is s
13f33 6d 61 6c 6c 65 72 20 74 68 61 6e 20 70 4b 65 79 maller than pKey
13f34 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 or if the table
13f35 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20 is empty.**
13f36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e an
13f37 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 d the cursor is
13f38 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70 therefore left p
13f39 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e oint to nothing.
13f3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 .**.** *pRes
13f3b 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 72 73 ==0 The curs
13f3c 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 or is left point
13f3d 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 ing at an entry
13f3e 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 that.**
13f3f 20 20 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 exactly
13f40 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2e 0a 2a matches pKey..*
13f41 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 *.** *pRes>0
13f42 20 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 The cursor
13f43 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
13f44 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 g at an entry th
13f45 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 at.**
13f46 20 20 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 is larger
13f47 20 74 68 61 6e 20 70 4b 65 79 2e 0a 2a 2a 0a 2a than pKey..**.*
13f48 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13f49 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
13f4a 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
13f4b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
13f4c 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 r, /* T
13f4d 68 65 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20 he cursor to be
13f4e 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 moved */. Unpac
13f4f 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b kedRecord *pIdxK
13f50 65 79 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 ey, /* Unpacked
13f51 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 index key */. i
13f52 36 34 20 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 64 intKey,
13f53 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
13f54 61 62 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e able key */. in
13f55 74 20 62 69 61 73 52 69 67 68 74 2c 20 20 20 20 t biasRight,
13f56 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 /* If tru
13f57 65 2c 20 62 69 61 73 20 74 68 65 20 73 65 61 72 e, bias the sear
13f58 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 ch to the high e
13f59 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 nd */. int *pRe
13f5a 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
13f5b 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 /* Write search
13f5c 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f results here */
13f5d 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 .){. int rc;..
13f5e 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
13f5f 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
13f60 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
13f61 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
13f62 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
13f63 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f ->mutex) );.. /
13f64 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 * If the cursor
13f65 69 73 20 61 6c 72 65 61 64 79 20 70 6f 73 69 74 is already posit
13f66 69 6f 6e 65 64 20 61 74 20 74 68 65 20 70 6f 69 ioned at the poi
13f67 6e 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 nt we are trying
13f68 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f . ** to move to
13f69 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 , then just retu
13f6a 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 rn without doing
13f6b 20 61 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 any work */. i
13f6c 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d f( pCur->eState=
13f6d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 =CURSOR_VALID &&
13f6e 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 pCur->validNKey
13f6f 20 0a 20 20 20 26 26 20 70 43 75 72 2d 3e 61 70 . && pCur->ap
13f70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 Page[0]->intKey
13f71 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 . ){. if( pC
13f72 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 ur->info.nKey==i
13f73 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a ntKey ){. *
13f74 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 pRes = 0;.
13f75 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
13f76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
13f77 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 26 26 20 pCur->atLast &&
13f78 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c pCur->info.nKey<
13f79 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 intKey ){.
13f7a 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 *pRes = -1;.
13f7b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
13f7c 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 OK;. }. }..
13f7d 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 rc = moveToRoot
13f7e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
13f7f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
13f80 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 c;. }. assert(
13f81 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
13f82 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 ur->iPage] );.
13f83 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
13f84 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
13f85 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 ]->isInit );. i
13f86 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d f( pCur->eState=
13f87 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
13f88 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d ){. *pRes = -
13f89 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 1;. assert( p
13f8a 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
13f8b 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d ->iPage]->nCell=
13f8c 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e =0 );. return
13f8d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
13f8e 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
13f8f 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 apPage[0]->intKe
13f90 79 20 7c 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a y || pIdxKey );.
13f91 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69 for(;;){. i
13f92 6e 74 20 6c 77 72 2c 20 75 70 72 3b 0a 20 20 20 nt lwr, upr;.
13f93 20 50 67 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 Pgno chldPg;.
13f94 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
13f95 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
13f96 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
13f97 20 20 69 6e 74 20 63 20 3d 20 2d 31 3b 20 20 2f int c = -1; /
13f98 2a 20 70 52 65 73 20 72 65 74 75 72 6e 20 69 66 * pRes return if
13f99 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20 table is empty
13f9a 6d 75 73 74 20 62 65 20 2d 31 20 2a 2f 0a 20 20 must be -1 */.
13f9b 20 20 6c 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 lwr = 0;. u
13f9c 70 72 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c pr = pPage->nCel
13f9d 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 21 70 50 l-1;. if( !pP
13f9e 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 70 age->intKey && p
13f9f 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 IdxKey==0 ){.
13fa0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
13fa1 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
13fa2 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 goto moveto_f
13fa3 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 inish;. }.
13fa4 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20 29 if( biasRight )
13fa5 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 {. pCur->ai
13fa6 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
13fa7 20 3d 20 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73 = upr;. }els
13fa8 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 e{. pCur->a
13fa9 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
13faa 5d 20 3d 20 28 75 70 72 2b 6c 77 72 29 2f 32 3b ] = (upr+lwr)/2;
13fab 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c . }. if( l
13fac 77 72 3c 3d 75 70 72 20 29 20 66 6f 72 28 3b 3b wr<=upr ) for(;;
13fad 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 ){. void *p
13fae 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 69 CellKey;. i
13faf 36 34 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 64 nCellKey;.
13fb0 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 int idx = pCu
13fb1 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
13fb2 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 70 43 75 Page];. pCu
13fb3 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 r->info.nSize =
13fb4 30 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 76 0;. pCur->v
13fb5 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 alidNKey = 1;.
13fb6 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 if( pPage->i
13fb7 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 ntKey ){.
13fb8 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 u8 *pCell;.
13fb9 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 pCell = find
13fba 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 Cell(pPage, idx)
13fbb 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 + pPage->childP
13fbc 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 trSize;.
13fbd 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
13fbe 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ta ){.
13fbf 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 u32 dummy;.
13fc0 20 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 pCell += ge
13fc1 74 56 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c tVarint32(pCell,
13fc2 20 64 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 dummy);.
13fc3 20 7d 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 }. getVa
13fc4 72 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 rint(pCell, (u64
13fc5 2a 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 *)&nCellKey);.
13fc6 20 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b if( nCellK
13fc7 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ey==intKey ){.
13fc8 20 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 c = 0;.
13fc9 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
13fca 20 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 nCellKey<intKey
13fcb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 ){. c
13fcc 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 = -1;. }e
13fcd 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 lse{. a
13fce 73 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e ssert( nCellKey>
13fcf 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 intKey );.
13fd0 20 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 c = +1;.
13fd1 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
13fd2 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 e{. int a
13fd3 76 61 69 6c 61 62 6c 65 3b 0a 20 20 20 20 20 20 vailable;.
13fd4 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 28 76 6f pCellKey = (vo
13fd5 69 64 20 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 id *)fetchPayloa
13fd6 64 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 d(pCur, &availab
13fd7 6c 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 le, 0);.
13fd8 6e 43 65 6c 6c 4b 65 79 20 3d 20 70 43 75 72 2d nCellKey = pCur-
13fd9 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 >info.nKey;.
13fda 20 20 20 20 69 66 28 20 61 76 61 69 6c 61 62 6c if( availabl
13fdb 65 3e 3d 6e 43 65 6c 6c 4b 65 79 20 29 7b 0a 20 e>=nCellKey ){.
13fdc 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c c = sql
13fdd 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f ite3VdbeRecordCo
13fde 6d 70 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c 20 mpare(nCellKey,
13fdf 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65 pCellKey, pIdxKe
13fe0 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 y);. }els
13fe1 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 e{. pCe
13fe2 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d llKey = sqlite3M
13fe3 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 4b 65 79 20 alloc( nCellKey
13fe4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
13fe5 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a pCellKey==0 ){.
13fe6 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
13fe7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
13fe8 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
13fe9 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
13fea 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
13feb 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13fec 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 3BtreeKey(pCur,
13fed 30 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 28 76 6f 0, nCellKey, (vo
13fee 69 64 20 2a 29 70 43 65 6c 6c 4b 65 79 29 3b 0a id *)pCellKey);.
13fef 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 c = sq
13ff0 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 lite3VdbeRecordC
13ff1 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c ompare(nCellKey,
13ff2 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b pCellKey, pIdxK
13ff3 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 ey);. s
13ff4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c qlite3_free(pCel
13ff5 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 lKey);.
13ff6 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d if( rc ) goto m
13ff7 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
13ff8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
13ff9 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 if( c==0 )
13ffa 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e {. pCur->
13ffb 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c info.nKey = nCel
13ffc 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 lKey;. if
13ffd 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
13ffe 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 && !pPage->leaf
13fff 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 ){. lwr
14000 20 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 = idx;.
14001 20 20 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b upr = lwr - 1;
14002 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
14003 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
14004 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
14005 52 65 73 20 29 20 2a 70 52 65 73 20 3d 20 30 3b Res ) *pRes = 0;
14006 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
14007 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
14008 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f goto moveto
14009 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 _finish;.
1400a 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1400b 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 if( c<0 ){.
1400c 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b lwr = idx+1;
1400d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1400e 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d upr = idx-
1400f 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
14010 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a if( lwr>upr ){.
14011 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e pCur->in
14012 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b fo.nKey = nCellK
14013 65 79 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 ey;. brea
14014 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
14015 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
14016 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 6c 77 72 r->iPage] = (lwr
14017 2b 75 70 72 29 2f 32 3b 0a 20 20 20 20 7d 0a 20 +upr)/2;. }.
14018 20 20 20 61 73 73 65 72 74 28 20 6c 77 72 3d 3d assert( lwr==
14019 75 70 72 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 upr+1 );. ass
1401a 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e ert( pPage->isIn
1401b 69 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 it );. if( pP
1401c 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
1401d 20 20 20 63 68 6c 64 50 67 20 3d 20 30 3b 0a 20 chldPg = 0;.
1401e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 77 72 }else if( lwr
1401f 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 >=pPage->nCell )
14020 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d {. chldPg =
14021 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
14022 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
14023 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
14024 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 }else{. c
14025 68 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 hldPg = get4byte
14026 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c (findCell(pPage,
14027 20 6c 77 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 lwr));. }.
14028 20 20 69 66 28 20 63 68 6c 64 50 67 3d 3d 30 20 if( chldPg==0
14029 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1402a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
1402b 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e r->iPage]<pCur->
1402c 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1402d 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 ge]->nCell );.
1402e 20 20 20 20 69 66 28 20 70 52 65 73 20 29 20 2a if( pRes ) *
1402f 70 52 65 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 pRes = c;.
14030 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
14031 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 goto movet
14032 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a o_finish;. }.
14033 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
14034 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 6c pCur->iPage] = l
14035 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e wr;. pCur->in
14036 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
14037 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
14038 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 y = 0;. rc =
14039 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
1403a 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 , chldPg);. i
1403b 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 f( rc ) goto mov
1403c 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a eto_finish;. }.
1403d 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 moveto_finish:.
1403e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1403f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 76 65 72 *.** In this ver
14040 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 4d 6f 76 sion of BtreeMov
14041 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 61 20 70 eto, pKey is a p
14042 61 63 6b 65 64 20 69 6e 64 65 78 20 72 65 63 6f acked index reco
14043 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 20 69 73 rd.** such as is
14044 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 generated by th
14045 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 e OP_MakeRecord
14046 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 63 6b 20 opcode. Unpack
14047 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 61 6e the.** record an
14048 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 74 72 65 d then call Btre
14049 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1404a 29 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b ) to do the work
1404b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1404c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1404d 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 treeMoveto(. Bt
1404e 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 Cursor *pCur,
1404f 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e /* Cursor open
14050 20 6f 6e 20 74 68 65 20 62 74 72 65 65 20 74 6f on the btree to
14051 20 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a be searched */.
14052 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b const void *pK
14053 65 79 2c 20 20 20 2f 2a 20 50 61 63 6b 65 64 20 ey, /* Packed
14054 6b 65 79 20 69 66 20 74 68 65 20 62 74 72 65 65 key if the btree
14055 20 69 73 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a is an index */.
14056 20 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 20 i64 nKey,
14057 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 /* Integer
14058 20 6b 65 79 20 66 6f 72 20 74 61 62 6c 65 73 2e key for tables.
14059 20 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 20 66 Size of pKey f
1405a 6f 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 or indices */.
1405b 69 6e 74 20 62 69 61 73 2c 20 20 20 20 20 20 20 int bias,
1405c 20 20 20 20 2f 2a 20 42 69 61 73 20 73 65 61 72 /* Bias sear
1405d 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 ch to the high e
1405e 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 nd */. int *pRe
1405f 73 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 s /* W
14060 72 69 74 65 20 73 65 61 72 63 68 20 72 65 73 75 rite search resu
14061 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 lts here */.){.
14062 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
14063 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
14064 74 61 74 75 73 20 63 6f 64 65 20 2a 2f 0a 20 20 tatus code */.
14065 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
14066 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e pIdxKey; /* Un
14067 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 packed index key
14068 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 */. UnpackedRe
14069 63 6f 72 64 20 61 53 70 61 63 65 5b 31 36 5d 3b cord aSpace[16];
1406a 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65 20 66 /* Temp space f
1406b 6f 72 20 70 49 64 78 4b 65 79 20 2d 20 74 6f 20 or pIdxKey - to
1406c 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f 63 20 2a avoid a malloc *
1406d 2f 0a 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b /.. if( pKey ){
1406e 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 . pIdxKey = s
1406f 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 qlite3VdbeRecord
14070 55 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 Unpack(pCur->pKe
14071 79 49 6e 66 6f 2c 20 6e 4b 65 79 2c 20 70 4b 65 yInfo, nKey, pKe
14072 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y,.
14073 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14074 20 20 20 20 20 20 20 20 20 61 53 70 61 63 65 2c aSpace,
14075 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 29 29 sizeof(aSpace))
14076 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 ;. if( pIdxKe
14077 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 y==0 ) return SQ
14078 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 LITE_NOMEM;. }e
14079 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79 lse{. pIdxKey
1407a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d = 0;. }. rc =
1407b 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1407c 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72 etoUnpacked(pCur
1407d 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c , pIdxKey, nKey,
1407e 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20 bias, pRes);.
1407f 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 if( pKey ){.
14080 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
14081 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 eUnpackedRecord(
14082 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 pIdxKey);. }.
14083 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
14084 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 *.** Return TRUE
14085 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 if the cursor i
14086 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 s not pointing a
14087 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 t an entry of th
14088 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 e table..**.** T
14089 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 RUE will be retu
1408a 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 6c rned after a cal
1408b 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 l to sqlite3Btre
1408c 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a eNext() moves.**
1408d 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 past the last e
1408e 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c ntry in the tabl
1408f 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 e or sqlite3Btre
14090 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 ePrev() moves pa
14091 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 st.** the first
14092 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 entry. TRUE is
14093 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 also returned if
14094 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
14095 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 pty..*/.SQLITE_P
14096 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
14097 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 e3BtreeEof(BtCur
14098 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a sor *pCur){. /*
14099 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 TODO: What if t
1409a 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 he cursor is in
1409b 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
1409c 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 EK but all table
1409d 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 entries. ** ha
1409e 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f ve been deleted?
1409f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e This API will n
140a0 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f eed to change to
140a1 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 return an error
140a2 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 code. ** as we
140a3 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 ll as the boolea
140a4 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a n result value..
140a5 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 */. return (C
140a6 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 URSOR_VALID!=pCu
140a7 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f r->eState);.}../
140a8 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
140a9 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
140aa 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72 20 61 ion handle for a
140ab 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 cursor..*/.SQLI
140ac 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
140ad 65 33 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 e3 *sqlite3Btree
140ae 43 75 72 73 6f 72 44 62 28 63 6f 6e 73 74 20 42 CursorDb(const B
140af 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
140b0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
140b1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
140b2 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
140b3 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 utex) );. retur
140b4 6e 20 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e n pCur->pBtree->
140b5 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 db;.}../*.** Adv
140b6 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 ance the cursor
140b7 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e 74 72 to the next entr
140b8 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
140b9 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 e. If.** succes
140ba 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 sful then set *p
140bb 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63 Res=0. If the c
140bc 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 ursor.** was alr
140bd 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f eady pointing to
140be 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
140bf 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
140c0 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 before.** this r
140c1 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 outine was calle
140c2 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 d, then set *pRe
140c3 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 s=1..*/.SQLITE_P
140c4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
140c5 65 33 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 e3BtreeNext(BtCu
140c6 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
140c7 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 *pRes){. int rc
140c8 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d ;. int idx;. M
140c9 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a emPage *pPage;..
140ca 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
140cb 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
140cc 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f );. rc = resto
140cd 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
140ce 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
140cf 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
140d0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
140d1 7d 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 73 }. assert( pRes
140d2 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 43 55 52 !=0 );. if( CUR
140d3 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 SOR_INVALID==pCu
140d4 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 r->eState ){.
140d5 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 *pRes = 1;.
140d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
140d7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 ;. }. if( pCur
140d8 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a 20 20 20 20 ->skip>0 ){.
140d9 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a pCur->skip = 0;.
140da 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
140db 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
140dc 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d _OK;. }. pCur-
140dd 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a 20 20 70 50 >skip = 0;.. pP
140de 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
140df 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
140e0 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d . idx = ++pCur-
140e1 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
140e2 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ge];. assert( p
140e3 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a Page->isInit );.
140e4 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 3d 70 assert( idx<=p
140e5 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a Page->nCell );..
140e6 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
140e7 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ze = 0;. pCur->
140e8 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 validNKey = 0;.
140e9 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67 65 2d if( idx>=pPage-
140ea 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 69 66 >nCell ){. if
140eb 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
140ec 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 {. rc = mov
140ed 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 eToChild(pCur, g
140ee 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
140ef 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
140f0 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 Offset+8]));.
140f1 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
140f2 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 rn rc;. rc
140f3 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 = moveToLeftmost
140f4 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 (pCur);. *p
140f5 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 Res = 0;. r
140f6 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
140f7 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 69 66 do{. if
140f8 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 ( pCur->iPage==0
140f9 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 ){. *pRe
140fa 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 s = 1;. p
140fb 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 Cur->eState = CU
140fc 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 RSOR_INVALID;.
140fd 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
140fe 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a ITE_OK;. }.
140ff 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
14100 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 eeMoveToParent(p
14101 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 Cur);. pPag
14102 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
14103 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
14104 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 72 2d }while( pCur-
14105 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
14106 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c ge]>=pPage->nCel
14107 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d l );. *pRes =
14108 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 0;. if( pPag
14109 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 e->intKey ){.
1410a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1410b 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20 70 treeNext(pCur, p
1410c 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Res);. }else{
1410d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1410e 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 TE_OK;. }.
1410f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
14110 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 *pRes = 0;. i
14111 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 f( pPage->leaf )
14112 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
14113 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 ITE_OK;. }. rc
14114 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 = moveToLeftmos
14115 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 t(pCur);. retur
14116 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
14117 53 74 65 70 20 74 68 65 20 63 75 72 73 6f 72 20 Step the cursor
14118 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f 20 74 to the back to t
14119 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 he previous entr
1411a 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
1411b 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 e. If.** succes
1411c 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 sful then set *p
1411d 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63 Res=0. If the c
1411e 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 ursor.** was alr
1411f 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f eady pointing to
14120 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
14121 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
14122 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 before.** this
14123 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c routine was call
14124 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 ed, then set *pR
14125 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f es=1..*/.SQLITE_
14126 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14127 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
14128 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
14129 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 int *pRes){. i
1412a 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 nt rc;. MemPage
1412b 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 *pPage;.. asse
1412c 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
1412d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
1412e 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 rc = restoreCurs
1412f 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 orPosition(pCur)
14130 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
14131 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 TE_OK ){. ret
14132 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 urn rc;. }. pC
14133 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a ur->atLast = 0;.
14134 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 if( CURSOR_INV
14135 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 ALID==pCur->eSta
14136 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 te ){. *pRes
14137 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
14138 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
14139 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 3c if( pCur->skip<
1413a 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 0 ){. pCur->s
1413b 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 kip = 0;. *pR
1413c 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 es = 0;. retu
1413d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1413e 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d }. pCur->skip =
1413f 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 0;.. pPage = p
14140 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
14141 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 ->iPage];. asse
14142 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
14143 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 t );. if( !pPag
14144 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 69 e->leaf ){. i
14145 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 nt idx = pCur->a
14146 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
14147 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 ];. rc = move
14148 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 ToChild(pCur, ge
14149 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
1414a 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b 0a 20 pPage, idx)));.
1414b 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
1414c 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1414d 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 }. rc = mov
1414e 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 43 75 eToRightmost(pCu
1414f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 r);. }else{.
14150 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 while( pCur->ai
14151 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
14152 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
14153 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 pCur->iPage==0
14154 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d ){. pCur-
14155 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
14156 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 _INVALID;.
14157 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 *pRes = 1;.
14158 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
14159 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 TE_OK;. }.
1415a 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1415b 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 eMoveToParent(pC
1415c 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ur);. }. p
1415d 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
1415e 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 = 0;. pCur->v
1415f 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 alidNKey = 0;..
14160 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
14161 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a 20 Cur->iPage]--;.
14162 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d pPage = pCur-
14163 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
14164 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 50 age];. if( pP
14165 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 age->intKey && !
14166 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
14167 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
14168 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 3BtreePrevious(p
14169 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 Cur, pRes);.
1416a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
1416b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
1416c 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d }. }. *pRes =
1416d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0;. return rc;
1416e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
1416f 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 66 72 te a new page fr
14170 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
14171 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 file..**.** The
14172 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 72 6b new page is mark
14173 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 28 49 ed as dirty. (I
14174 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 73 n other words, s
14175 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
14176 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 ().** has alread
14177 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e y been called on
14178 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 29 20 the new page.)
14179 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 68 61 The new page ha
1417a 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 72 s also.** been r
1417b 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 74 68 eferenced and th
1417c 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e e calling routin
1417d 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 e is responsible
1417e 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 for calling.**
1417f 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
14180 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 f() on the new p
14181 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 20 64 age when it is d
14182 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 one..**.** SQLIT
14183 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
14184 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 41 6e on success. An
14185 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e 20 76 y other return v
14186 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 0a 2a alue indicates.*
14187 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70 70 * an error. *pp
14188 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 Page and *pPgno
14189 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69 6e are undefined in
1418a 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 6e the event of an
1418b 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f error..** Do no
1418c 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 t invoke sqlite3
1418d 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 PagerUnref() on
1418e 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 65 72 *ppPage if an er
1418f 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ror is returned.
14190 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e .**.** If the "n
14191 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 65 72 earby" parameter
14192 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 is not 0, then
14193 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 6f 72 a (feeble) effor
14194 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a t is made to .**
14195 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20 63 locate a page c
14196 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67 65 lose to the page
14197 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22 number "nearby"
14198 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
14199 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 sed in an.** att
1419a 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c empt to keep rel
1419b 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65 ated pages close
1419c 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69 to each other i
1419d 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1419e 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e ile,.** which in
1419f 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64 turn can make d
141a0 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 66 atabase access f
141a1 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 aster..**.** If
141a2 74 68 65 20 22 65 78 61 63 74 22 20 70 61 72 61 the "exact" para
141a3 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 meter is not 0,
141a4 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e 75 6d and the page-num
141a5 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 73 74 ber nearby exist
141a6 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f s .** anywhere o
141a7 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c n the free-list,
141a8 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 72 then it is guar
141a9 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65 74 enteed to be ret
141aa 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 69 urned. This.** i
141ab 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 61 s only used by a
141ac 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
141ad 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63 61 ases when alloca
141ae 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 ting a new table
141af 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
141b0 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
141b1 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
141b2 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a Bt, . MemPage *
141b3 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 6e 6f *ppPage, . Pgno
141b4 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e 6f *pPgno, . Pgno
141b5 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 65 78 nearby,. u8 ex
141b6 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 act.){. MemPage
141b7 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 *pPage1;. int
141b8 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 rc;. int n;
141b9 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
141ba 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c ges on the freel
141bb 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6b 3b 20 ist */. int k;
141bc 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
141bd 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20 74 leaves on the t
141be 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 65 runk of the free
141bf 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 list */. MemPag
141c0 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 e *pTrunk = 0;.
141c1 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76 54 MemPage *pPrevT
141c2 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61 73 73 runk = 0;.. ass
141c3 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
141c4 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
141c5 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20 ex) );. pPage1
141c6 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 = pBt->pPage1;.
141c7 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 n = get4byte(&p
141c8 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
141c9 29 3b 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b 0a );. if( n>0 ){.
141ca 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 /* There are
141cb 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 pages on the fr
141cc 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 20 6f eelist. Reuse o
141cd 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 67 65 ne of those page
141ce 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 s. */. Pgno i
141cf 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 73 65 Trunk;. u8 se
141d0 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 2f 2a archList = 0; /*
141d1 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 If the free-lis
141d2 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 63 68 t must be search
141d3 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 27 20 ed for 'nearby'
141d4 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 49 */. . /* I
141d5 66 20 74 68 65 20 27 65 78 61 63 74 27 20 70 61 f the 'exact' pa
141d6 72 61 6d 65 74 65 72 20 77 61 73 20 74 72 75 65 rameter was true
141d7 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f 66 20 and a query of
141d8 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
141d9 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74 68 61 ** shows tha
141da 74 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 t the page 'near
141db 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65 72 65 by' is somewhere
141dc 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
141dd 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 t, then. ** t
141de 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 20 77 he entire-list w
141df 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65 64 20 ill be searched
141e0 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e 0a 20 for that page..
141e1 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 */.#ifndef SQ
141e2 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
141e3 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65 78 61 CUUM. if( exa
141e4 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d 70 61 ct && nearby<=pa
141e5 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
141e6 2d 3e 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 ->pPager) ){.
141e7 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 u8 eType;.
141e8 20 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 assert( nearb
141e9 79 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 y>0 );. ass
141ea 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 ert( pBt->autoVa
141eb 63 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 cuum );. rc
141ec 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 = ptrmapGet(pBt
141ed 2c 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 , nearby, &eType
141ee 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
141ef 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
141f0 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d if( eType=
141f1 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 =PTRMAP_FREEPAGE
141f2 20 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 ){. sear
141f3 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 chList = 1;.
141f4 20 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f }. *pPgno
141f5 20 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d = nearby;. }
141f6 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
141f7 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 Decrement the fr
141f8 65 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 ee-list count by
141f9 20 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 1. Set iTrunk t
141fa 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 o the index of t
141fb 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 he. ** first
141fc 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 free-list trunk
141fd 70 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b page. iPrevTrunk
141fe 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e is initially 1.
141ff 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
14200 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
14201 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 te(pPage1->pDbPa
14202 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ge);. if( rc
14203 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
14204 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
14205 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 1->aData[36], n-
14206 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 1);.. /* The
14207 63 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 code within this
14208 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c loop is run onl
14209 79 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 y once if the 's
1420a 65 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 earchList' varia
1420b 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f ble. ** is no
1420c 74 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 t true. Otherwis
1420d 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 e, it runs once
1420e 66 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 for each trunk-p
1420f 61 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a age on the. *
14210 2a 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 * free-list unti
14211 6c 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 l the page 'near
14212 62 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a by' is located..
14213 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a */. do {.
14214 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b pPrevTrunk
14215 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 = pTrunk;.
14216 20 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 if( pPrevTrunk
14217 29 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e ){. iTrun
14218 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 k = get4byte(&pP
14219 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b revTrunk->aData[
1421a 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0]);. }else
1421b 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b {. iTrunk
1421c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
1421d 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b ge1->aData[32]);
1421e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
1421f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
14220 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 GetPage(pBt, iTr
14221 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 unk, &pTrunk, 0)
14222 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
14223 7b 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b {. pTrunk
14224 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f = 0;. go
14225 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
14226 70 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 page;. }..
14227 20 20 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 k = get4byt
14228 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 e(&pTrunk->aData
14229 5b 34 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 [4]);. if(
1422a 6b 3d 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c k==0 && !searchL
1422b 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ist ){. /
1422c 2a 20 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 * The trunk has
1422d 6e 6f 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 no leaves and th
1422e 65 20 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 e list is not be
1422f 69 6e 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 ing searched. .
14230 20 20 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 ** So ext
14231 72 61 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 ract the trunk p
14232 61 67 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 age itself and u
14233 73 65 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 se it as the new
14234 6c 79 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 ly . ** a
14235 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f llocated page */
14236 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
14237 20 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 pPrevTrunk==0 )
14238 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
14239 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1423a 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 (pTrunk->pDbPage
1423b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1423c 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 c ){. g
1423d 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
1423e 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d _page;. }
1423f 0a 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 . *pPgno
14240 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 = iTrunk;.
14241 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 memcpy(&pPage1
14242 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 ->aData[32], &pT
14243 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 runk->aData[0],
14244 34 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 4);. *ppP
14245 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 age = pTrunk;.
14246 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 pTrunk = 0
14247 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 ;. TRACE(
14248 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 ("ALLOCATE: %d t
14249 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 runk - %d free p
1424a 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 ages left\n", *p
1424b 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 Pgno, n-1));.
1424c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 70 }else if( k>p
1424d 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 Bt->usableSize/4
1424e 20 2d 20 32 20 29 7b 0a 20 20 20 20 20 20 20 20 - 2 ){.
1424f 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b 20 69 73 /* Value of k is
14250 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 out of range.
14251 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 Database corrupt
14252 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 ion */. r
14253 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
14254 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
14255 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
14256 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e 64 65 66 te_page;.#ifndef
14257 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
14258 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 7d 65 OVACUUM. }e
14259 6c 73 65 20 69 66 28 20 73 65 61 72 63 68 4c 69 lse if( searchLi
1425a 73 74 20 26 26 20 6e 65 61 72 62 79 3d 3d 69 54 st && nearby==iT
1425b 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 runk ){.
1425c 2f 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 62 /* The list is b
1425d 65 69 6e 67 20 73 65 61 72 63 68 65 64 20 61 6e eing searched an
1425e 64 20 74 68 69 73 20 74 72 75 6e 6b 20 70 61 67 d this trunk pag
1425f 65 20 69 73 20 74 68 65 20 70 61 67 65 0a 20 20 e is the page.
14260 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f ** to allo
14261 63 61 74 65 2c 20 72 65 67 61 72 64 6c 65 73 73 cate, regardless
14262 20 6f 66 20 77 68 65 74 68 65 72 20 69 74 20 68 of whether it h
14263 61 73 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 as leaves..
14264 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 */. as
14265 73 65 72 74 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 sert( *pPgno==iT
14266 72 75 6e 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 runk );.
14267 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b *ppPage = pTrunk
14268 3b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 ;. search
14269 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 List = 0;.
1426a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1426b 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d gerWrite(pTrunk-
1426c 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
1426d 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
1426e 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
1426f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
14270 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
14271 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 if( k==0 ){.
14272 20 20 20 20 20 20 20 69 66 28 20 21 70 50 72 65 if( !pPre
14273 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 vTrunk ){.
14274 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 memcpy(&pP
14275 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c age1->aData[32],
14276 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b &pTrunk->aData[
14277 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 0], 4);.
14278 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
14279 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 72 memcpy(&pPr
1427a 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 evTrunk->aData[0
1427b 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
1427c 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[0], 4);.
1427d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
1427e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
1427f 2a 20 54 68 65 20 74 72 75 6e 6b 20 70 61 67 65 * The trunk page
14280 20 69 73 20 72 65 71 75 69 72 65 64 20 62 79 20 is required by
14281 74 68 65 20 63 61 6c 6c 65 72 20 62 75 74 20 69 the caller but i
14282 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20 20 20 20 t contains .
14283 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 ** pointer
14284 73 20 74 6f 20 66 72 65 65 2d 6c 69 73 74 20 6c s to free-list l
14285 65 61 76 65 73 2e 20 54 68 65 20 66 69 72 73 74 eaves. The first
14286 20 6c 65 61 66 20 62 65 63 6f 6d 65 73 20 61 20 leaf becomes a
14287 74 72 75 6e 6b 0a 20 20 20 20 20 20 20 20 20 20 trunk.
14288 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 ** page in this
14289 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 case..
1428a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 4d 65 6d */. Mem
1428b 50 61 67 65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b Page *pNewTrunk;
1428c 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 . Pgno
1428d 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67 65 74 34 iNewTrunk = get4
1428e 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
1428f 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 ata[8]);.
14290 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
14291 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
14292 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 iNewTrunk, &pNe
14293 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 wTrunk, 0);.
14294 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
14295 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
14296 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
14297 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
14298 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
14299 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1429a 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 3PagerWrite(pNew
1429b 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
1429c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
1429d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1429e 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 rele
1429f 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e asePage(pNewTrun
142a0 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 k);.
142a1 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
142a2 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 e_page;.
142a3 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 }. me
142a4 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d mcpy(&pNewTrunk-
142a5 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 >aData[0], &pTru
142a6 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 nk->aData[0], 4)
142a7 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 ;. put4
142a8 62 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d byte(&pNewTrunk-
142a9 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b >aData[4], k-1);
142aa 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
142ab 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 y(&pNewTrunk->aD
142ac 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d ata[8], &pTrunk-
142ad 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 >aData[12], (k-1
142ae 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 )*4);.
142af 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 releasePage(pNew
142b0 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 Trunk);.
142b1 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e if( !pPrevTrun
142b2 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 k ){.
142b3 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
142b4 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 1->aData[32], iN
142b5 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 ewTrunk);.
142b6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
142b7 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
142b8 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
142b9 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 revTrunk->pDbPag
142ba 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
142bb 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
142bc 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
142bd 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
142be 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
142bf 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 put4by
142c0 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e te(&pPrevTrunk->
142c1 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 aData[0], iNewTr
142c2 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 unk);.
142c3 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
142c4 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a pTrunk = 0;.
142c5 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 TRACE(("
142c6 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 ALLOCATE: %d tru
142c7 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 nk - %d free pag
142c8 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 es left\n", *pPg
142c9 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 no, n-1));.#endi
142ca 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 f. }else{.
142cb 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63 /* Extrac
142cc 74 20 61 20 6c 65 61 66 20 66 72 6f 6d 20 74 68 t a leaf from th
142cd 65 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 e trunk */.
142ce 20 20 20 69 6e 74 20 63 6c 6f 73 65 73 74 3b 0a int closest;.
142cf 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 50 61 Pgno iPa
142d0 67 65 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 69 ge;. unsi
142d1 67 6e 65 64 20 63 68 61 72 20 2a 61 44 61 74 61 gned char *aData
142d2 20 3d 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 = pTrunk->aData
142d3 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
142d4 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
142d5 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 (pTrunk->pDbPage
142d6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
142d7 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 c ){. g
142d8 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
142d9 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d _page;. }
142da 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 61 . if( nea
142db 72 62 79 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 rby>0 ){.
142dc 20 20 20 69 6e 74 20 69 2c 20 64 69 73 74 3b 0a int i, dist;.
142dd 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 closes
142de 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 t = 0;.
142df 20 64 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 dist = get4byte
142e0 28 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 6e 65 (&aData[8]) - ne
142e1 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20 arby;.
142e2 69 66 28 20 64 69 73 74 3c 30 20 29 20 64 69 73 if( dist<0 ) dis
142e3 74 20 3d 20 2d 64 69 73 74 3b 0a 20 20 20 20 20 t = -dist;.
142e4 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c for(i=1; i<
142e5 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 k; i++){.
142e6 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 67 65 int d2 = ge
142e7 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 2b t4byte(&aData[8+
142e8 69 2a 34 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a i*4]) - nearby;.
142e9 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
142ea 64 32 3c 30 20 29 20 64 32 20 3d 20 2d 64 32 3b d2<0 ) d2 = -d2;
142eb 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
142ec 20 64 32 3c 64 69 73 74 20 29 7b 0a 20 20 20 20 d2<dist ){.
142ed 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 closes
142ee 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 20 t = i;.
142ef 20 20 20 20 20 64 69 73 74 20 3d 20 64 32 3b 0a dist = d2;.
142f0 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
142f1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
142f2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
142f3 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a closest = 0;.
142f4 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }..
142f5 20 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62 iPage = get4b
142f6 79 74 65 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f yte(&aData[8+clo
142f7 73 65 73 74 2a 34 5d 29 3b 0a 20 20 20 20 20 20 sest*4]);.
142f8 20 20 69 66 28 20 21 73 65 61 72 63 68 4c 69 73 if( !searchLis
142f9 74 20 7c 7c 20 69 50 61 67 65 3d 3d 6e 65 61 72 t || iPage==near
142fa 62 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 by ){.
142fb 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 int nPage;.
142fc 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 *pPgno = iP
142fd 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 6e age;. n
142fe 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 Page = pagerPage
142ff 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 count(pBt->pPage
14300 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 r);. if
14301 28 20 2a 70 50 67 6e 6f 3e 6e 50 61 67 65 20 29 ( *pPgno>nPage )
14302 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a {. /*
14303 20 46 72 65 65 20 70 61 67 65 20 6f 66 66 20 74 Free page off t
14304 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 he end of the fi
14305 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 le */.
14306 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
14307 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
14308 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
14309 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
1430a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1430b 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c TRACE(("AL
1430c 4c 4f 43 41 54 45 3a 20 25 64 20 77 61 73 20 6c LOCATE: %d was l
1430d 65 61 66 20 25 64 20 6f 66 20 25 64 20 6f 6e 20 eaf %d of %d on
1430e 74 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 20 trunk %d".
1430f 20 20 20 20 20 20 20 20 20 20 20 22 3a 20 25 64 ": %d
14310 20 6d 6f 72 65 20 66 72 65 65 20 70 61 67 65 73 more free pages
14311 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
14312 20 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c *pPgno, cl
14313 6f 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75 osest+1, k, pTru
14314 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b nk->pgno, n-1));
14315 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 . if( c
14316 6c 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 losest<k-1 ){.
14317 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
14318 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 (&aData[8+closes
14319 74 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b t*4], &aData[4+k
1431a 2a 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 *4], 4);.
1431b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 }. p
1431c 75 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 ut4byte(&aData[4
1431d 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 ], k-1);.
1431e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1431f 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
14320 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c *pPgno, ppPage,
14321 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 1);. i
14322 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
14323 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
14324 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
14325 52 6f 6c 6c 62 61 63 6b 28 28 2a 70 70 50 61 67 Rollback((*ppPag
14326 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e)->pDbPage);.
14327 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
14328 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
14329 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 ((*ppPage)->pDbP
1432a 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
1432b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1432c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1432d 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
1432e 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 (*ppPage);.
1432f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
14330 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 }. s
14331 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 earchList = 0;.
14332 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
14333 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
14334 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a ge(pPrevTrunk);.
14335 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b pPrevTrunk
14336 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65 = 0;. }while
14337 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a ( searchList );.
14338 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
14339 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67 There are no pag
1433a 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 es on the freeli
1433b 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 20 st, so create a
1433c 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65 0a new page at the.
1433d 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68 ** end of th
1433e 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 6e e file */. in
1433f 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 t nPage = pagerP
14340 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 agecount(pBt->pP
14341 61 67 65 72 29 3b 0a 20 20 20 20 2a 70 50 67 6e ager);. *pPgn
14342 6f 20 3d 20 6e 50 61 67 65 20 2b 20 31 3b 0a 0a o = nPage + 1;..
14343 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
14344 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
14345 20 20 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 75 if( pBt->nTru
14346 6e 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 nc ){. /* A
14347 6e 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 68 61 n incr-vacuum ha
14348 73 20 61 6c 72 65 61 64 79 20 72 75 6e 20 77 69 s already run wi
14349 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 thin this transa
1434a 63 74 69 6f 6e 2e 20 53 6f 20 74 68 65 0a 20 20 ction. So the.
1434b 20 20 20 20 2a 2a 20 70 61 67 65 20 74 6f 20 61 ** page to a
1434c 6c 6c 6f 63 61 74 65 20 69 73 20 6e 6f 74 20 66 llocate is not f
1434d 72 6f 6d 20 74 68 65 20 70 68 79 73 69 63 61 6c rom the physical
1434e 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 end of the file
1434f 2c 20 62 75 74 0a 20 20 20 20 20 20 2a 2a 20 61 , but. ** a
14350 74 20 70 42 74 2d 3e 6e 54 72 75 6e 63 2e 20 0a t pBt->nTrunc. .
14351 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 2a */. *
14352 70 50 67 6e 6f 20 3d 20 70 42 74 2d 3e 6e 54 72 pPgno = pBt->nTr
14353 75 6e 63 2b 31 3b 0a 20 20 20 20 20 20 69 66 28 unc+1;. if(
14354 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 *pPgno==PENDING
14355 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
14356 29 7b 0a 20 20 20 20 20 20 20 20 28 2a 70 50 67 ){. (*pPg
14357 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 no)++;. }.
14358 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 42 74 }. if( pBt
14359 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 ->autoVacuum &&
1435a 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
1435b 74 2c 20 2a 70 50 67 6e 6f 29 20 29 7b 0a 20 20 t, *pPgno) ){.
1435c 20 20 20 20 2f 2a 20 49 66 20 2a 70 50 67 6e 6f /* If *pPgno
1435d 20 72 65 66 65 72 73 20 74 6f 20 61 20 70 6f 69 refers to a poi
1435e 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 61 nter-map page, a
1435f 6c 6c 6f 63 61 74 65 20 74 77 6f 20 6e 65 77 20 llocate two new
14360 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 pages. ** a
14361 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
14362 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 file instead of
14363 20 6f 6e 65 2e 20 54 68 65 20 66 69 72 73 74 20 one. The first
14364 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 0a 20 allocated page.
14365 20 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 73 20 ** becomes
14366 61 20 6e 65 77 20 70 6f 69 6e 74 65 72 2d 6d 61 a new pointer-ma
14367 70 20 70 61 67 65 2c 20 74 68 65 20 73 65 63 6f p page, the seco
14368 6e 64 20 69 73 20 75 73 65 64 20 62 79 20 74 68 nd is used by th
14369 65 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20 20 e caller..
1436a 2a 2f 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 */. TRACE((
1436b 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 "ALLOCATE: %d fr
1436c 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 om end of file (
1436d 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
1436e 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a )\n", *pPgno));.
1436f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 assert( *p
14370 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno!=PENDING_BY
14371 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a TE_PAGE(pBt) );.
14372 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29 2b 2b (*pPgno)++
14373 3b 0a 20 20 20 20 20 20 69 66 28 20 2a 70 50 67 ;. if( *pPg
14374 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 no==PENDING_BYTE
14375 5f 50 41 47 45 28 70 42 74 29 20 29 7b 20 28 2a _PAGE(pBt) ){ (*
14376 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a 20 20 20 20 pPgno)++; }.
14377 7d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6e }. if( pBt->n
14378 54 72 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 70 Trunc ){. p
14379 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 2a 70 50 Bt->nTrunc = *pP
1437a 67 6e 6f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 gno;. }.#endi
1437b 66 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a f.. assert( *
1437c 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 pPgno!=PENDING_B
1437d 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b YTE_PAGE(pBt) );
1437e 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1437f 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 3BtreeGetPage(pB
14380 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 t, *pPgno, ppPag
14381 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 e, 0);. if( r
14382 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
14383 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
14384 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 agerWrite((*ppPa
14385 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge)->pDbPage);.
14386 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
14387 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
14388 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 leasePage(*ppPag
14389 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 52 e);. }. TR
1438a 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 ACE(("ALLOCATE:
1438b 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 %d from end of f
1438c 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 ile\n", *pPgno))
1438d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
1438e 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 *pPgno!=PENDING
1438f 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
14390 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 );..end_allocate
14391 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73 65 _page:. release
14392 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 Page(pTrunk);.
14393 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 releasePage(pPre
14394 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 20 72 vTrunk);. if( r
14395 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
14396 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
14397 52 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 Refcount((*ppPag
14398 65 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 e)->pDbPage)>1 )
14399 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 {. releasePag
1439a 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 e(*ppPage);.
1439b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1439c 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a RRUPT_BKPT;. }.
1439d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1439e 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 70 61 67 65 /*.** Add a page
1439f 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
143a0 20 66 69 6c 65 20 74 6f 20 74 68 65 20 66 72 65 file to the fre
143a1 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c elist..**.** sql
143a2 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 ite3PagerUnref()
143a3 20 69 73 20 4e 4f 54 20 63 61 6c 6c 65 64 20 66 is NOT called f
143a4 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 or pPage..*/.sta
143a5 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65 tic int freePage
143a6 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
143a7 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
143a8 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
143a9 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
143aa 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
143ab 0a 20 20 69 6e 74 20 72 63 2c 20 6e 2c 20 6b 3b . int rc, n, k;
143ac 0a 0a 20 20 2f 2a 20 50 72 65 70 61 72 65 20 74 .. /* Prepare t
143ad 68 65 20 70 61 67 65 20 66 6f 72 20 66 72 65 65 he page for free
143ae 69 6e 67 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ing */. assert(
143af 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
143b0 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
143b1 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
143b2 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3e rt( pPage->pgno>
143b3 31 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 69 73 1 );. pPage->is
143b4 49 6e 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 Init = 0;.. /*
143b5 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 Increment the fr
143b6 65 65 20 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e ee page count on
143b7 20 70 50 61 67 65 31 20 2a 2f 0a 20 20 72 63 20 pPage1 */. rc
143b8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
143b9 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 ite(pPage1->pDbP
143ba 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 age);. if( rc )
143bb 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6e 20 return rc;. n
143bc 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
143bd 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a e1->aData[36]);.
143be 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 put4byte(&pPag
143bf 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e e1->aData[36], n
143c0 2b 31 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c +1);..#ifdef SQL
143c1 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 ITE_SECURE_DELET
143c2 45 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51 E. /* If the SQ
143c3 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 LITE_SECURE_DELE
143c4 54 45 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 TE compile-time
143c5 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 option is enable
143c6 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 d, then. ** alw
143c7 61 79 73 20 66 75 6c 6c 79 20 6f 76 65 72 77 72 ays fully overwr
143c8 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f ite deleted info
143c9 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 rmation with zer
143ca 6f 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 os.. */. rc =
143cb 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
143cc 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
143cd 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
143ce 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 73 65 turn rc;. memse
143cf 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 t(pPage->aData,
143d0 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 0, pPage->pBt->p
143d1 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66 ageSize);.#endif
143d2 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 .. /* If the da
143d3 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 tabase supports
143d4 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69 auto-vacuum, wri
143d5 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 te an entry in t
143d6 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 he pointer-map.
143d7 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20 ** to indicate
143d8 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 that the page is
143d9 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 free.. */. if
143da 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 ( ISAUTOVACUUM )
143db 7b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 {. rc = ptrma
143dc 70 50 75 74 28 70 42 74 2c 20 70 50 61 67 65 2d pPut(pBt, pPage-
143dd 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 46 52 >pgno, PTRMAP_FR
143de 45 45 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 EEPAGE, 0);.
143df 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
143e0 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e rc;. }.. if( n
143e1 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ==0 ){. /* Th
143e2 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 is is the first
143e3 66 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 free page */.
143e4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
143e5 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 erWrite(pPage->p
143e6 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 DbPage);. if(
143e7 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
143e8 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 . memset(pPag
143e9 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 38 29 3b e->aData, 0, 8);
143ea 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 . put4byte(&p
143eb 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
143ec 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a , pPage->pgno);.
143ed 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45 TRACE(("FREE
143ee 2d 50 41 47 45 3a 20 25 64 20 66 69 72 73 74 5c -PAGE: %d first\
143ef 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 n", pPage->pgno)
143f0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
143f1 2f 2a 20 4f 74 68 65 72 20 66 72 65 65 20 70 61 /* Other free pa
143f2 67 65 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 ges already exis
143f3 74 2e 20 20 52 65 74 72 69 76 65 20 74 68 65 20 t. Retrive the
143f4 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65 first trunk page
143f5 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 . ** of the f
143f6 72 65 65 6c 69 73 74 20 61 6e 64 20 66 69 6e 64 reelist and find
143f7 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6c 65 out how many le
143f8 61 76 65 73 20 69 74 20 68 61 73 2e 20 2a 2f 0a aves it has. */.
143f9 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 MemPage *pTr
143fa 75 6e 6b 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 unk;. rc = sq
143fb 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
143fc 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 e(pBt, get4byte(
143fd 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
143fe 32 5d 29 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 2]), &pTrunk, 0)
143ff 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
14400 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6b 20 eturn rc;. k
14401 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 = get4byte(&pTru
14402 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 nk->aData[4]);.
14403 20 20 20 69 66 28 20 6b 3e 3d 70 42 74 2d 3e 75 if( k>=pBt->u
14404 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 sableSize/4 - 8
14405 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ){. /* The
14406 74 72 75 6e 6b 20 69 73 20 66 75 6c 6c 2e 20 20 trunk is full.
14407 54 75 72 6e 20 74 68 65 20 70 61 67 65 20 62 65 Turn the page be
14408 69 6e 67 20 66 72 65 65 64 20 69 6e 74 6f 20 61 ing freed into a
14409 20 6e 65 77 0a 20 20 20 20 20 20 2a 2a 20 74 72 new. ** tr
1440a 75 6e 6b 20 70 61 67 65 20 77 69 74 68 20 6e 6f unk page with no
1440b 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 2a leaves.. *
1440c 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 *. ** Note
1440d 74 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70 that the trunk p
1440e 61 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c age is not reall
1440f 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 y full until it
14410 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a contains. *
14411 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d * usableSize/4 -
14412 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 2 entries, not
14413 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 usableSize/4 - 8
14414 20 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68 entries as we h
14415 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 ave. ** cod
14416 65 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20 ed. But due to
14417 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 a coding error i
14418 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 n versions of SQ
14419 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 Lite prior to.
1441a 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 ** 3.6.0, da
1441b 74 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65 tabases with fre
1441c 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 elist trunk page
1441d 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 s holding more t
1441e 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 han. ** usa
1441f 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e bleSize/4 - 8 en
14420 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65 tries will be re
14421 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70 ported as corrup
14422 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 t. In order.
14423 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 ** to maintai
14424 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 n backwards comp
14425 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f atibility with o
14426 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 lder versions of
14427 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a SQLite,. *
14428 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 * we will contai
14429 6e 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 n to restrict th
1442a 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
1442b 69 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a ies to usableSiz
1442c 65 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a e/4 - 8. **
1442d 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f for now. At so
1442e 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 me point in the
1442f 66 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 future (once eve
14430 72 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 ryone has upgrad
14431 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 ed. ** to 3
14432 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 .6.0 or later) w
14433 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 e should conside
14434 72 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e r fixing the con
14435 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 ditional above.
14436 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 ** to read
14437 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 "usableSize/4-2"
14438 20 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 instead of "usa
14439 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 bleSize/4-8"..
1443a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 */. rc
1443b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1443c 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
1443d 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
1443e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1443f 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
14440 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 70 (pPage->aData, p
14441 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 3b 0a 20 20 Trunk->pgno);.
14442 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
14443 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c pPage->aData[4],
14444 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 0);. put
14445 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
14446 44 61 74 61 5b 33 32 5d 2c 20 70 50 61 67 65 2d Data[32], pPage-
14447 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 >pgno);.
14448 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 TRACE(("FREE-PAG
14449 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 E: %d new trunk
1444a 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 page replacing %
1444b 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
1444c 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e pPage->pgn
1444d 6f 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 o, pTrunk->pgno)
1444e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1444f 65 6c 73 65 20 69 66 28 20 6b 3c 30 20 29 7b 0a else if( k<0 ){.
14450 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
14451 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 7d E_CORRUPT;. }
14452 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 else{. /* A
14453 64 64 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 dd the newly fre
14454 65 64 20 70 61 67 65 20 61 73 20 61 20 6c 65 61 ed page as a lea
14455 66 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e 74 f on the current
14456 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 trunk */.
14457 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
14458 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
14459 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 DbPage);. i
1445a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1445b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34 ){. put4
1445c 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
1445d 61 74 61 5b 34 5d 2c 20 6b 2b 31 29 3b 0a 20 20 ata[4], k+1);.
1445e 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
1445f 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b pTrunk->aData[8+
14460 6b 2a 34 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e k*4], pPage->pgn
14461 6f 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 o);.#ifndef SQLI
14462 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TE_SECURE_DELETE
14463 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
14464 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
14465 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
14466 67 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 ge);.#endif.
14467 20 20 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28 }. TRACE(
14468 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 ("FREE-PAGE: %d
14469 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 leaf on trunk pa
1446a 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e ge %d\n",pPage->
1446b 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e pgno,pTrunk->pgn
1446c 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 o));. }. r
1446d 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e eleasePage(pTrun
1446e 6b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e k);. }. return
1446f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 rc;.}../*.** Fr
14470 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 ee any overflow
14471 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 pages associated
14472 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 with the given
14473 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Cell..*/.static
14474 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65 int clearCell(Me
14475 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 6e mPage *pPage, un
14476 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 signed char *pCe
14477 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ll){. BtShared
14478 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 *pBt = pPage->pB
14479 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e t;. CellInfo in
1447a 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50 fo;. Pgno ovflP
1447b 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 gno;. int rc;.
1447c 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 69 6e int nOvfl;. in
1447d 74 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a t ovflPageSize;.
1447e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1447f 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
14480 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
14481 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
14482 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
14483 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
14484 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e fo);. if( info.
14485 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a iOverflow==0 ){.
14486 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
14487 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 E_OK; /* No ove
14488 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65 74 rflow pages. Ret
14489 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e urn without doin
1448a 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 g anything */.
1448b 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 }. ovflPgno = g
1448c 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 et4byte(&pCell[i
1448d 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b nfo.iOverflow]);
1448e 0a 20 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 . ovflPageSize
1448f 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
14490 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d e - 4;. nOvfl =
14491 20 28 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 (info.nPayload
14492 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 - info.nLocal +
14493 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 2d 20 31 ovflPageSize - 1
14494 29 2f 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a )/ovflPageSize;.
14495 20 20 61 73 73 65 72 74 28 20 6f 76 66 6c 50 67 assert( ovflPg
14496 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 no==0 || nOvfl>0
14497 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76 );. while( nOv
14498 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 4d 65 6d 50 fl-- ){. MemP
14499 61 67 65 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 age *pOvfl;.
1449a 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 if( ovflPgno==0
1449b 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65 || ovflPgno>page
1449c 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e rPagecount(pBt->
1449d 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 pPager) ){.
1449e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1449f 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
144a0 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 67 65 74 }.. rc = get
144a1 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 OverflowPage(pBt
144a2 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 , ovflPgno, &pOv
144a3 66 6c 2c 20 28 6e 4f 76 66 6c 3d 3d 30 29 3f 30 fl, (nOvfl==0)?0
144a4 3a 26 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 :&ovflPgno);.
144a5 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
144a6 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 66 72 rc;. rc = fr
144a7 65 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 eePage(pOvfl);.
144a8 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
144a9 6e 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 nref(pOvfl->pDbP
144aa 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
144ab 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
144ac 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
144ad 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
144ae 72 65 61 74 65 20 74 68 65 20 62 79 74 65 20 73 reate the byte s
144af 65 71 75 65 6e 63 65 20 75 73 65 64 20 74 6f 20 equence used to
144b0 72 65 70 72 65 73 65 6e 74 20 61 20 63 65 6c 6c represent a cell
144b1 20 6f 6e 20 70 61 67 65 20 70 50 61 67 65 0a 2a on page pPage.*
144b2 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68 61 74 * and write that
144b3 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 69 byte sequence i
144b4 6e 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 nto pCell[]. Ov
144b5 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 erflow pages are
144b6 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e .** allocated an
144b7 64 20 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e d filled in as n
144b8 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 20 63 ecessary. The c
144b9 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 alling procedure
144ba 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 .** is responsib
144bb 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 le for making su
144bc 72 65 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 re sufficient sp
144bd 61 63 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c ace has been all
144be 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43 ocated.** for pC
144bf 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 ell[]..**.** Not
144c0 65 20 74 68 61 74 20 70 43 65 6c 6c 20 64 6f 65 e that pCell doe
144c1 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 s not necessary
144c2 6e 65 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f need to point to
144c3 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 the pPage->aDat
144c4 61 0a 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 6c a.** area. pCel
144c5 6c 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f l might point to
144c6 20 73 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 20 some temporary
144c7 73 74 6f 72 61 67 65 2e 20 20 54 68 65 20 63 65 storage. The ce
144c8 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f ll will.** be co
144c9 6e 73 74 72 75 63 74 65 64 20 69 6e 20 74 68 69 nstructed in thi
144ca 73 20 74 65 6d 70 6f 72 61 72 79 20 61 72 65 61 s temporary area
144cb 20 74 68 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 then copied int
144cc 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a o pPage->aData.*
144cd 2a 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 * later..*/.stat
144ce 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c ic int fillInCel
144cf 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 l(. MemPage *pP
144d0 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 age,
144d1 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 /* The page
144d2 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
144d3 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 e cell */. unsi
144d4 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c gned char *pCell
144d5 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f , /* Co
144d6 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 mplete text of t
144d7 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e he cell */. con
144d8 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 st void *pKey, i
144d9 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 64 nKey, /* T
144da 68 65 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 he key */. cons
144db 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 6e t void *pData,in
144dc 74 20 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 68 t nData, /* Th
144dd 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 e data */. int
144de 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 nZero,
144df 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
144e0 74 72 61 20 7a 65 72 6f 20 62 79 74 65 73 20 74 tra zero bytes t
144e1 6f 20 61 70 70 65 6e 64 20 74 6f 20 70 44 61 74 o append to pDat
144e2 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 69 a */. int *pnSi
144e3 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ze
144e4 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 /* Write c
144e5 65 6c 6c 20 73 69 7a 65 20 68 65 72 65 20 2a 2f ell size here */
144e6 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c 6f .){. int nPaylo
144e7 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a ad;. const u8 *
144e8 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 63 pSrc;. int nSrc
144e9 2c 20 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 73 , n, rc;. int s
144ea 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 paceLeft;. MemP
144eb 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a age *pOvfl = 0;.
144ec 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 65 MemPage *pToRe
144ed 6c 65 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e 73 lease = 0;. uns
144ee 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69 igned char *pPri
144ef 6f 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 or;. unsigned c
144f0 68 61 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 har *pPayload;.
144f1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
144f2 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 50 pPage->pBt;. P
144f3 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 gno pgnoOvfl = 0
144f4 3b 0a 20 20 69 6e 74 20 6e 48 65 61 64 65 72 3b ;. int nHeader;
144f5 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f . CellInfo info
144f6 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
144f7 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
144f8 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
144f9 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c x) );.. /* Fill
144fa 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 in the header.
144fb 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 */. nHeader = 0
144fc 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e ;. if( !pPage->
144fd 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 leaf ){. nHea
144fe 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 der += 4;. }.
144ff 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
14500 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 ta ){. nHeade
14501 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 r += putVarint(&
14502 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 pCell[nHeader],
14503 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 nData+nZero);.
14504 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 }else{. nData
14505 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 = nZero = 0;.
14506 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 }. nHeader += p
14507 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b utVarint(&pCell[
14508 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a nHeader], *(u64*
14509 29 26 6e 4b 65 79 29 3b 0a 20 20 73 71 6c 69 74 )&nKey);. sqlit
1450a 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
1450b 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
1450c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 , &info);. asse
1450d 72 74 28 20 69 6e 66 6f 2e 6e 48 65 61 64 65 72 rt( info.nHeader
1450e 3d 3d 6e 48 65 61 64 65 72 20 29 3b 0a 20 20 61 ==nHeader );. a
1450f 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 ssert( info.nKey
14510 3d 3d 6e 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 ==nKey );. asse
14511 72 74 28 20 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d rt( info.nData==
14512 6e 44 61 74 61 2b 6e 5a 65 72 6f 20 29 3b 0a 20 nData+nZero );.
14513 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 . /* Fill in t
14514 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 he payload */.
14515 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 nPayload = nData
14516 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 + nZero;. if(
14517 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
14518 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 . pSrc = pDat
14519 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 a;. nSrc = nD
1451a 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d ata;. nData =
1451b 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
1451c 20 6e 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 nPayload += nKe
1451d 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 4b y;. pSrc = pK
1451e 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e ey;. nSrc = n
1451f 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 Key;. }. *pnSi
14520 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b ze = info.nSize;
14521 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 . spaceLeft = i
14522 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 nfo.nLocal;. pP
14523 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b ayload = &pCell[
14524 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 nHeader];. pPri
14525 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f or = &pCell[info
14526 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 .iOverflow];..
14527 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e while( nPayload>
14528 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 0 ){. if( spa
14529 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 ceLeft==0 ){.
1452a 20 20 20 69 6e 74 20 69 73 45 78 61 63 74 20 3d int isExact =
1452b 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 0;.#ifndef SQLI
1452c 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
1452d 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 UM. Pgno pg
1452e 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f noPtrmap = pgnoO
1452f 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 vfl; /* Overflow
14530 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 page pointer-ma
14531 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a p entry page */.
14532 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 if( pBt->a
14533 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
14534 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 do{.
14535 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 pgnoOvfl++;.
14536 20 20 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 } while(
14537 0a 20 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 . PTRMA
14538 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 P_ISPAGE(pBt, pg
14539 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f noOvfl) || pgnoO
1453a 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 vfl==PENDING_BYT
1453b 45 5f 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 E_PAGE(pBt) .
1453c 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 );.
1453d 69 66 28 20 70 67 6e 6f 4f 76 66 6c 3e 31 20 29 if( pgnoOvfl>1 )
1453e 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 {. /* i
1453f 73 45 78 61 63 74 20 3d 20 31 3b 20 2a 2f 0a 20 sExact = 1; */.
14540 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
14541 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 .#endif. rc
14542 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
14543 50 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66 6c Page(pBt, &pOvfl
14544 2c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e , &pgnoOvfl, pgn
14545 6f 4f 76 66 6c 2c 20 69 73 45 78 61 63 74 29 3b oOvfl, isExact);
14546 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14547 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
14548 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
14549 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
1454a 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 61 s auto-vacuum, a
1454b 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 nd the second or
1454c 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 20 subsequent.
1454d 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 ** overflow pa
1454e 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c 6f ge is being allo
1454f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 6e cated, add an en
14550 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 try to the point
14551 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a 20 er-map. **
14552 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e 6f for that page no
14553 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 w. . **.
14554 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 ** If this is
14555 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 the first overf
14556 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 77 low page, then w
14557 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 65 rite a partial e
14558 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 74 ntry . ** t
14559 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
1455a 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 6e p. If we write n
1455b 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 70 othing to this p
1455c 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 2c ointer-map slot,
1455d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 . ** then t
1455e 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f 76 he optimistic ov
1455f 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 6f erflow chain pro
14560 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 72 cessing in clear
14561 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a 20 Cell(). **
14562 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 74 may misinterpret
14563 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 73 the uninitialis
14564 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 65 ed values and de
14565 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a lete the. *
14566 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72 * wrong pages fr
14567 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e om the database.
14568 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
14569 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
1456a 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 uum && rc==SQLIT
1456b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1456c 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e 6f u8 eType = (pgno
1456d 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f 56 Ptrmap?PTRMAP_OV
1456e 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f 4f ERFLOW2:PTRMAP_O
1456f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 20 VERFLOW1);.
14570 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
14571 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c t(pBt, pgnoOvfl,
14572 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d eType, pgnoPtrm
14573 61 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ap);. if(
14574 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
14575 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 76 releasePage(pOv
14576 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 fl);. }.
14577 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
14578 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
14579 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
1457a 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 e(pToRelease);.
1457b 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1457c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1457d 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c put4byte(pPrior,
1457e 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 pgnoOvfl);.
1457f 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
14580 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 oRelease);.
14581 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f pToRelease = pO
14582 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f vfl;. pPrio
14583 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 r = pOvfl->aData
14584 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
14585 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 (pPrior, 0);.
14586 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 pPayload = &p
14587 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a Ovfl->aData[4];.
14588 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 spaceLeft
14589 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
1458a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 e - 4;. }.
1458b 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 n = nPayload;.
1458c 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 if( n>spaceLe
1458d 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 ft ) n = spaceLe
1458e 66 74 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63 ft;. if( nSrc
1458f 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 >0 ){. if(
14590 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 72 n>nSrc ) n = nSr
14591 63 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 c;. assert(
14592 20 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 6d pSrc );. m
14593 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 emcpy(pPayload,
14594 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 pSrc, n);. }e
14595 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 lse{. memse
14596 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 6e t(pPayload, 0, n
14597 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 );. }. nPa
14598 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 20 yload -= n;.
14599 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 pPayload += n;.
1459a 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 pSrc += n;.
1459b 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 nSrc -= n;.
1459c 20 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b spaceLeft -= n;
1459d 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d 30 . if( nSrc==0
1459e 20 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 3d ){. nSrc =
1459f 20 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 53 nData;. pS
145a0 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 rc = pData;.
145a1 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 }. }. releaseP
145a2 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b age(pToRelease);
145a3 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
145a4 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 _OK;.}../*.** Re
145a5 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 65 move the i-th ce
145a6 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 20 ll from pPage.
145a7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 66 This routine eff
145a8 65 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 2e ects pPage only.
145a9 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f 6e .** The cell con
145aa 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 65 tent is not free
145ab 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 d or deallocated
145ac 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 . It is assumed
145ad 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 6c that.** the cel
145ae 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 65 l content has be
145af 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 6c en copied somepl
145b0 61 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 20 ace else. This
145b1 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a 20 routine just.**
145b2 72 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 65 removes the refe
145b3 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 6c rence to the cel
145b4 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a l from pPage..**
145b5 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 65 .** "sz" must be
145b6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
145b7 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c ytes in the cell
145b8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
145b9 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67 dropCell(MemPag
145ba 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64 e *pPage, int id
145bb 78 2c 20 69 6e 74 20 73 7a 29 7b 0a 20 20 69 6e x, int sz){. in
145bc 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a t i; /*
145bd 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
145be 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 . int pc;
145bf 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 /* Offset to
145c0 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 cell content of
145c1 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 cell being delet
145c2 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 ed */. u8 *data
145c3 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 ; /* pPage
145c4 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 ->aData */. u8
145c5 2a 70 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 *ptr; /*
145c6 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 Used to move byt
145c7 65 73 20 61 72 6f 75 6e 64 20 77 69 74 68 69 6e es around within
145c8 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20 61 73 data[] */.. as
145c9 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 sert( idx>=0 &&
145ca 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c idx<pPage->nCell
145cb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a );. assert( sz
145cc 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 ==cellSize(pPage
145cd 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 , idx) );. asse
145ce 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
145cf 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
145d0 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
145d1 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
145d2 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
145d3 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
145d4 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 ;. data = pPage
145d5 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d ->aData;. ptr =
145d6 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 &data[pPage->ce
145d7 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 llOffset + 2*idx
145d8 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 ];. pc = get2by
145d9 74 65 28 70 74 72 29 3b 0a 20 20 61 73 73 65 72 te(ptr);. asser
145da 74 28 20 70 63 3e 31 30 20 26 26 20 70 63 2b 73 t( pc>10 && pc+s
145db 7a 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 z<=pPage->pBt->u
145dc 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 66 sableSize );. f
145dd 72 65 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 reeSpace(pPage,
145de 70 63 2c 20 73 7a 29 3b 0a 20 20 66 6f 72 28 69 pc, sz);. for(i
145df 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 2d =idx+1; i<pPage-
145e0 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 72 >nCell; i++, ptr
145e1 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 5d +=2){. ptr[0]
145e2 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 70 = ptr[2];. p
145e3 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b 0a tr[1] = ptr[3];.
145e4 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 }. pPage->nCe
145e5 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 65 ll--;. put2byte
145e6 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 (&data[pPage->hd
145e7 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67 rOffset+3], pPag
145e8 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 e->nCell);. pPa
145e9 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a ge->nFree += 2;.
145ea 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 }../*.** Insert
145eb 61 20 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 a new cell on pP
145ec 61 67 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 age at cell inde
145ed 78 20 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f x "i". pCell po
145ee 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 ints to the.** c
145ef 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 ontent of the ce
145f0 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ll..**.** If the
145f1 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 cell content wi
145f2 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 ll fit on the pa
145f3 67 65 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 ge, then put it
145f4 74 68 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a there. If it.**
145f5 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 will not fit, t
145f6 68 65 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 hen make a copy
145f7 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
145f8 65 6e 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 ent into pTemp i
145f9 66 0a 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f f.** pTemp is no
145fa 74 20 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c t null. Regardl
145fb 65 73 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c ess of pTemp, al
145fc 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 locate a new ent
145fd 72 79 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e ry.** in pPage->
145fe 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 aOvfl[] and make
145ff 20 69 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 it point to the
14600 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 cell content (e
14601 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d ither.** in pTem
14602 70 20 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 p or the origina
14603 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 l pCell) and als
14604 6f 20 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 o record its ind
14605 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 ex. .** Allocati
14606 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 ng a new entry i
14607 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d n pPage->aCell[]
14608 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a implies that .*
14609 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c * pPage->nOverfl
1460a 6f 77 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 ow is incremente
1460b 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 d..**.** If nSki
1460c 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 p is non-zero, t
1460d 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 hen do not copy
1460e 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 the first nSkip
1460f 62 79 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 bytes of the.**
14610 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 cell. The caller
14611 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 will overwrite
14612 74 68 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 them after this
14613 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
14614 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 . If.** nSkip is
14615 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
14616 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f pCell may not po
14617 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 int to an invali
14618 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f d memory locatio
14619 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c n .** (but pCell
1461a 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 +nSkip is always
1461b 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 valid)..*/.stat
1461c 69 63 20 69 6e 74 20 69 6e 73 65 72 74 43 65 6c ic int insertCel
1461d 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 l(. MemPage *pP
1461e 61 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 age, /* Page i
1461f 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 nto which we are
14620 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e copying */. in
14621 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 t i,
14622 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f /* New cell beco
14623 6d 65 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c mes the i-th cel
14624 6c 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f l of the page */
14625 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 . u8 *pCell,
14626 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 /* Content
14627 6f 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 of the new cell
14628 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20 */. int sz,
14629 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
1462a 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43 of content in pC
1462b 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65 ell */. u8 *pTe
1462c 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 mp, /* Te
1462d 6d 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 mp storage space
1462e 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e for pCell, if n
1462f 65 65 64 65 64 20 2a 2f 0a 20 20 75 38 20 6e 53 eeded */. u8 nS
14630 6b 69 70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 kip /*
14631 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 Do not write the
14632 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74 first nSkip byt
14633 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a es of the cell *
14634 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 /.){. int idx;
14635 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 /* Wher
14636 65 20 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63 e to write new c
14637 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64 ell content in d
14638 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a ata[] */. int j
14639 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1463a 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
1463b 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 int top;
1463c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
1463d 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 66 6f 72 e of content for
1463e 20 61 6e 79 20 63 65 6c 6c 20 69 6e 20 64 61 74 any cell in dat
1463f 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 a[] */. int end
14640 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 ; /* Fi
14641 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 rst byte past th
14642 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e e last cell poin
14643 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f ter in data[] */
14644 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 . int ins;
14645 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
14646 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 data[] where ne
14647 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 w cell pointer i
14648 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 s inserted */.
14649 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 int hdr;
1464a 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f /* Offset into
1464b 20 64 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 70 data[] of the p
1464c 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 age header */.
1464d 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 int cellOffset;
1464e 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
1464f 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 first cell point
14650 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a er in data[] */.
14651 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 u8 *data;
14652 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 /* The conte
14653 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 nt of the whole
14654 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74 page */. u8 *pt
14655 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 r; /* U
14656 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69 sed for moving i
14657 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e nformation aroun
14658 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a d in data[] */..
14659 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1465a 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c & i<=pPage->nCel
1465b 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c l+pPage->nOverfl
1465c 6f 77 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ow );. assert(
1465d 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 sz==cellSizePtr(
1465e 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b pPage, pCell) );
1465f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
14660 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
14661 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
14662 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d );. if( pPage-
14663 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a >nOverflow || sz
14664 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 +2>pPage->nFree
14665 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 ){. if( pTemp
14666 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 ){. memcpy
14667 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 (pTemp+nSkip, pC
14668 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 ell+nSkip, sz-nS
14669 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c kip);. pCel
1466a 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d l = pTemp;. }
1466b 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e . j = pPage->
1466c 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 nOverflow++;.
1466d 20 61 73 73 65 72 74 28 20 6a 3c 73 69 7a 65 6f assert( j<sizeo
1466e 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f f(pPage->aOvfl)/
1466f 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f sizeof(pPage->aO
14670 76 66 6c 5b 30 5d 29 20 29 3b 0a 20 20 20 20 70 vfl[0]) );. p
14671 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 Page->aOvfl[j].p
14672 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 Cell = pCell;.
14673 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a pPage->aOvfl[j
14674 5d 2e 69 64 78 20 3d 20 69 3b 0a 20 20 20 20 70 ].idx = i;. p
14675 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 30 3b Page->nFree = 0;
14676 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
14677 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 t rc = sqlite3Pa
14678 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e gerWrite(pPage->
14679 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
1467a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1467b 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1467c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 rc;. }. as
1467d 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
1467e 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
1467f 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
14680 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67 . data = pPag
14681 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 68 64 e->aData;. hd
14682 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
14683 66 73 65 74 3b 0a 20 20 20 20 74 6f 70 20 3d 20 fset;. top =
14684 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
14685 64 72 2b 35 5d 29 3b 0a 20 20 20 20 63 65 6c 6c dr+5]);. cell
14686 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e Offset = pPage->
14687 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 cellOffset;.
14688 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 end = cellOffset
14689 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c + 2*pPage->nCel
1468a 6c 20 2b 20 32 3b 0a 20 20 20 20 69 6e 73 20 3d l + 2;. ins =
1468b 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a cellOffset + 2*
1468c 69 3b 0a 20 20 20 20 69 66 28 20 65 6e 64 20 3e i;. if( end >
1468d 20 74 6f 70 20 2d 20 73 7a 20 29 7b 0a 20 20 20 top - sz ){.
1468e 20 20 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 defragmentPag
1468f 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
14690 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 top = get2byte(&
14691 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 data[hdr+5]);.
14692 20 20 20 20 61 73 73 65 72 74 28 20 65 6e 64 20 assert( end
14693 2b 20 73 7a 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 + sz <= top );.
14694 20 20 20 7d 0a 20 20 20 20 69 64 78 20 3d 20 61 }. idx = a
14695 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 llocateSpace(pPa
14696 67 65 2c 20 73 7a 29 3b 0a 20 20 20 20 61 73 73 ge, sz);. ass
14697 65 72 74 28 20 69 64 78 3e 30 20 29 3b 0a 20 20 ert( idx>0 );.
14698 20 20 61 73 73 65 72 74 28 20 65 6e 64 20 3c 3d assert( end <=
14699 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
1469a 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 20 20 70 hdr+5]) );. p
1469b 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 Page->nCell++;.
1469c 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 pPage->nFree
1469d 2d 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 63 70 79 -= 2;. memcpy
1469e 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 (&data[idx+nSkip
1469f 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 ], pCell+nSkip,
146a0 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 66 sz-nSkip);. f
146a1 6f 72 28 6a 3d 65 6e 64 2d 32 2c 20 70 74 72 3d or(j=end-2, ptr=
146a2 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b &data[j]; j>ins;
146a3 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d 32 29 7b 0a j-=2, ptr-=2){.
146a4 20 20 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 ptr[0] = p
146a5 74 72 5b 2d 32 5d 3b 0a 20 20 20 20 20 20 70 74 tr[-2];. pt
146a6 72 5b 31 5d 20 3d 20 70 74 72 5b 2d 31 5d 3b 0a r[1] = ptr[-1];.
146a7 20 20 20 20 7d 0a 20 20 20 20 70 75 74 32 62 79 }. put2by
146a8 74 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c 20 69 te(&data[ins], i
146a9 64 78 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 dx);. put2byt
146aa 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 e(&data[hdr+3],
146ab 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 23 pPage->nCell);.#
146ac 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
146ad 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
146ae 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 74 if( pPage->pBt
146af 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
146b0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c /* The cel
146b1 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20 l may contain a
146b2 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 76 pointer to an ov
146b3 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 erflow page. If
146b4 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20 20 so, write.
146b5 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 ** the entry for
146b6 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
146b7 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e ge into the poin
146b8 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20 2a ter map.. *
146b9 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f /. CellInfo
146ba 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 73 71 6c info;. sql
146bb 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
146bc 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 llPtr(pPage, pCe
146bd 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 ll, &info);.
146be 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f 2e assert( (info.
146bf 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e nData+(pPage->in
146c0 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 tKey?0:info.nKey
146c1 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 ))==info.nPayloa
146c2 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 d );. if( (
146c3 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 info.nData+(pPag
146c4 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f e->intKey?0:info
146c5 2e 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f .nKey))>info.nLo
146c6 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 50 cal ){. P
146c7 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 gno pgnoOvfl = g
146c8 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 et4byte(&pCell[i
146c9 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b nfo.iOverflow]);
146ca 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 . rc = pt
146cb 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d 3e 70 rmapPut(pPage->p
146cc 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 Bt, pgnoOvfl, PT
146cd 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 RMAP_OVERFLOW1,
146ce 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 pPage->pgno);.
146cf 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
146d0 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e LITE_OK ) return
146d1 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
146d2 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 }.#endif. }..
146d3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
146d4 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 K;.}../*.** Add
146d5 61 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 a list of cells
146d6 74 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65 20 to a page. The
146d7 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 69 page should be i
146d8 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a nitially empty..
146d9 2a 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72 65 ** The cells are
146da 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 guaranteed to f
146db 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a it on the page..
146dc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
146dd 73 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d ssemblePage(. M
146de 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 emPage *pPage,
146df 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 /* The page to
146e0 62 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f be assemblied */
146e1 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 . int nCell,
146e2 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 /* The numb
146e3 65 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 er of cells to a
146e4 64 64 20 74 6f 20 74 68 69 73 20 70 61 67 65 20 dd to this page
146e5 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c */. u8 **apCell
146e6 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 , /* Pointe
146e7 72 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 rs to cell bodie
146e8 73 20 2a 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a s */. u16 *aSiz
146e9 65 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 e /* Size
146ea 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a s of the cells *
146eb 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 /.){. int i;
146ec 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
146ed 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
146ee 74 20 74 6f 74 61 6c 53 69 7a 65 3b 20 20 20 20 t totalSize;
146ef 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 /* Total size of
146f0 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 all cells */.
146f1 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 int hdr;
146f2 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 70 61 /* Index of pa
146f3 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 ge header */. i
146f4 6e 74 20 63 65 6c 6c 70 74 72 3b 20 20 20 20 20 nt cellptr;
146f5 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e /* Address of n
146f6 65 78 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 ext cell pointer
146f7 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62 6f */. int cellbo
146f8 64 79 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 dy; /* Addre
146f9 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 ss of next cell
146fa 62 6f 64 79 20 2a 2f 0a 20 20 75 38 20 2a 64 61 body */. u8 *da
146fb 74 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 ta; /* D
146fc 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 65 ata for the page
146fd 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
146fe 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d Page->nOverflow=
146ff 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
14700 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14701 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
14702 75 74 65 78 29 20 29 3b 0a 20 20 74 6f 74 61 6c utex) );. total
14703 53 69 7a 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 Size = 0;. for(
14704 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b i=0; i<nCell; i+
14705 2b 29 7b 0a 20 20 20 20 74 6f 74 61 6c 53 69 7a +){. totalSiz
14706 65 20 2b 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 e += aSize[i];.
14707 20 7d 0a 20 20 61 73 73 65 72 74 28 20 74 6f 74 }. assert( tot
14708 61 6c 53 69 7a 65 2b 32 2a 6e 43 65 6c 6c 3c 3d alSize+2*nCell<=
14709 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 3b 0a pPage->nFree );.
1470a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1470b 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 63 >nCell==0 );. c
1470c 65 6c 6c 70 74 72 20 3d 20 70 50 61 67 65 2d 3e ellptr = pPage->
1470d 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 64 61 cellOffset;. da
1470e 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
1470f 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 a;. hdr = pPage
14710 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 70 ->hdrOffset;. p
14711 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
14712 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 r+3], nCell);.
14713 69 66 28 20 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 if( nCell ){.
14714 20 63 65 6c 6c 62 6f 64 79 20 3d 20 61 6c 6c 6f cellbody = allo
14715 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65 2c cateSpace(pPage,
14716 20 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 20 totalSize);.
14717 20 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 assert( cellbod
14718 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 y>0 );. asser
14719 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 t( pPage->nFree
1471a 3e 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20 >= 2*nCell );.
1471b 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d pPage->nFree -
1471c 3d 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 20 20 66 = 2*nCell;. f
1471d 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b or(i=0; i<nCell;
1471e 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 75 74 i++){. put
1471f 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 2byte(&data[cell
14720 70 74 72 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b ptr], cellbody);
14721 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 . memcpy(&d
14722 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 ata[cellbody], a
14723 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b pCell[i], aSize[
14724 69 5d 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 70 i]);. cellp
14725 74 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 63 tr += 2;. c
14726 65 6c 6c 62 6f 64 79 20 2b 3d 20 61 53 69 7a 65 ellbody += aSize
14727 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 [i];. }. a
14728 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79 3d ssert( cellbody=
14729 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 =pPage->pBt->usa
1472a 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 20 bleSize );. }.
1472b 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 pPage->nCell =
1472c 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 nCell;.}../*.**
1472d 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 The following pa
1472e 72 61 6d 65 74 65 72 73 20 64 65 74 65 72 6d 69 rameters determi
1472f 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 64 6a 61 ne how many adja
14730 63 65 6e 74 20 70 61 67 65 73 20 67 65 74 20 69 cent pages get i
14731 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 61 20 nvolved.** in a
14732 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 balancing operat
14733 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 68 65 20 ion. NN is the
14734 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 number of neighb
14735 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 ors on either si
14736 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 de.** of the pag
14737 65 20 74 68 61 74 20 70 61 72 74 69 63 69 70 61 e that participa
14738 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 te in the balanc
14739 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 ing operation.
1473a 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 74 6f 74 NB is the.** tot
1473b 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
1473c 65 73 20 74 68 61 74 20 70 61 72 74 69 63 69 70 es that particip
1473d 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 ate, including t
1473e 68 65 20 74 61 72 67 65 74 20 70 61 67 65 20 61 he target page a
1473f 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 62 6f nd.** NN neighbo
14740 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 rs on either sid
14741 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e e..**.** The min
14742 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e 4e imum value of NN
14743 20 69 73 20 31 20 28 6f 66 20 63 6f 75 72 73 65 is 1 (of course
14744 29 2e 20 20 49 6e 63 72 65 61 73 69 6e 67 20 4e ). Increasing N
14745 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 28 74 6f N above 1.** (to
14746 20 32 20 6f 72 20 33 29 20 67 69 76 65 73 20 61 2 or 3) gives a
14747 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f 76 65 6d modest improvem
14748 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 20 61 6e ent in SELECT an
14749 64 20 44 45 4c 45 54 45 20 70 65 72 66 6f 72 6d d DELETE perform
1474a 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 63 68 61 ance.** in excha
1474b 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 nge for a larger
1474c 20 64 65 67 72 61 64 61 74 69 6f 6e 20 69 6e 20 degradation in
1474d 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44 41 54 INSERT and UPDAT
1474e 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a 2a E performance..*
1474f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e * The value of N
14750 4e 20 61 70 70 65 61 72 73 20 74 6f 20 67 69 76 N appears to giv
14751 65 20 74 68 65 20 62 65 73 74 20 72 65 73 75 6c e the best resul
14752 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a 23 ts overall..*/.#
14753 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 20 20 20 define NN 1
14754 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
14755 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 6f r of neighbors o
14756 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 n either side of
14757 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 66 69 6e pPage */.#defin
14758 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 20 20 e NB (NN*2+1)
14759 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 61 67 65 /* Total page
1475a 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 s involved in th
1475b 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a 2f 2a e balance */../*
1475c 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
1475d 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 ce */.static int
1475e 20 62 61 6c 61 6e 63 65 28 42 74 43 75 72 73 6f balance(BtCurso
1475f 72 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 6e 64 r*, int);..#ifnd
14760 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 ef SQLITE_OMIT_Q
14761 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a 2a UICKBALANCE./*.*
14762 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
14763 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61 6e 64 f balance() hand
14764 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 les the common s
14765 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 pecial case wher
14766 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72 79 e.** a new entry
14767 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74 is being insert
14768 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72 65 6d ed on the extrem
14769 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66 20 74 e right-end of t
1476a 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 6f he.** tree, in o
1476b 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68 65 6e ther words, when
1476c 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77 the new entry w
1476d 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6c ill become the l
1476e 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79 20 argest.** entry
1476f 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a in the tree..**.
14770 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 74 72 ** Instead of tr
14771 79 69 6e 67 20 62 61 6c 61 6e 63 65 20 74 68 65 ying balance the
14772 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 3 right-most le
14773 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74 20 61 af pages, just a
14774 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67 65 dd.** a new page
14775 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 68 61 to the right-ha
14776 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75 74 20 nd side and put
14777 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72 the one new entr
14778 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67 y in.** that pag
14779 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 20 e. This leaves
1477a 74 68 65 20 72 69 67 68 74 20 73 69 64 65 20 6f the right side o
1477b 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d 65 77 f the tree somew
1477c 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65 hat.** unbalance
1477d 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61 72 65 d. But odds are
1477e 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65 that we will be
1477f 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 65 inserting new e
14780 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74 68 65 ntries.** at the
14781 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72 77 end soon afterw
14782 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65 61 72 ards so the near
14783 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20 77 69 ly empty page wi
14784 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69 ll quickly.** fi
14785 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65 72 61 ll up. On avera
14786 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20 ge..**.** pPage
14787 69 73 20 74 68 65 20 6c 65 61 66 20 70 61 67 65 is the leaf page
14788 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 69 which is the ri
14789 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e ght-most page in
1478a 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50 the tree..** pP
1478b 61 72 65 6e 74 20 69 73 20 69 74 73 20 70 61 72 arent is its par
1478c 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75 73 74 ent. pPage must
1478d 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f have a single o
1478e 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a verflow entry.**
1478f 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f 20 74 which is also t
14790 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e he right-most en
14791 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67 65 2e try on the page.
14792 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
14793 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 42 74 43 alance_quick(BtC
14794 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
14795 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 int rc;. MemPag
14796 65 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 50 e *pNew = 0;. P
14797 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 0a 20 20 75 gno pgnoNew;. u
14798 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 75 31 36 20 8 *pCell;. u16
14799 73 7a 43 65 6c 6c 3b 0a 20 20 43 65 6c 6c 49 6e szCell;. CellIn
1479a 66 6f 20 69 6e 66 6f 3b 0a 20 20 4d 65 6d 50 61 fo info;. MemPa
1479b 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 ge *pPage = pCur
1479c 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
1479d 50 61 67 65 5d 3b 0a 20 20 4d 65 6d 50 61 67 65 Page];. MemPage
1479e 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72 *pParent = pCur
1479f 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
147a0 50 61 67 65 2d 31 5d 3b 0a 20 20 42 74 53 68 61 Page-1];. BtSha
147a1 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
147a2 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 70 61 72 ->pBt;. int par
147a3 65 6e 74 49 64 78 20 3d 20 70 50 61 72 65 6e 74 entIdx = pParent
147a4 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f 2a 20 70 50 ->nCell; /* pP
147a5 61 72 65 6e 74 20 6e 65 77 20 64 69 76 69 64 65 arent new divide
147a6 72 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a r cell index */.
147a7 20 20 69 6e 74 20 70 61 72 65 6e 74 53 69 7a 65 int parentSize
147a8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
147a9 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6e /* Size of n
147aa 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 ew divider cell
147ab 2a 2f 0a 20 20 75 38 20 70 61 72 65 6e 74 43 65 */. u8 parentCe
147ac 6c 6c 5b 36 34 5d 3b 20 20 20 20 20 20 20 20 20 ll[64];
147ad 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
147ae 66 6f 72 20 74 68 65 20 6e 65 77 20 64 69 76 69 for the new divi
147af 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 61 der cell */.. a
147b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
147b1 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
147b2 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
147b3 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 . /* Allocate a
147b4 20 6e 65 77 20 70 61 67 65 2e 20 49 6e 73 65 72 new page. Inser
147b5 74 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 t the overflow c
147b6 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 0a 20 ell from pPage.
147b7 20 2a 2a 20 69 6e 74 6f 20 69 74 2e 20 54 68 65 ** into it. The
147b8 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 6f 76 65 n remove the ove
147b9 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f 6d 20 rflow cell from
147ba 70 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 pPage.. */. rc
147bb 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
147bc 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c Page(pBt, &pNew,
147bd 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29 &pgnoNew, 0, 0)
147be 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
147bf 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 65 TE_OK ){. pCe
147c0 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 66 ll = pPage->aOvf
147c1 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 20 l[0].pCell;.
147c2 73 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a szCell = cellSiz
147c3 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c ePtr(pPage, pCel
147c4 6c 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 l);. zeroPage
147c5 28 70 4e 65 77 2c 20 70 50 61 67 65 2d 3e 61 44 (pNew, pPage->aD
147c6 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 61 73 73 ata[0]);. ass
147c7 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 emblePage(pNew,
147c8 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65 1, &pCell, &szCe
147c9 6c 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e ll);. pPage->
147ca 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 nOverflow = 0;.
147cb 20 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 20 69 . /* pPage i
147cc 73 20 63 75 72 72 65 6e 74 6c 79 20 74 68 65 20 s currently the
147cd 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 70 right-child of p
147ce 50 61 72 65 6e 74 2e 20 43 68 61 6e 67 65 20 74 Parent. Change t
147cf 68 69 73 0a 20 20 20 20 2a 2a 20 73 6f 20 74 68 his. ** so th
147d0 61 74 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 at the right-chi
147d1 6c 64 20 69 73 20 74 68 65 20 6e 65 77 20 70 61 ld is the new pa
147d2 67 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 62 6f ge allocated abo
147d3 76 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 70 50 ve and. ** pP
147d4 61 67 65 20 69 73 20 74 68 65 20 6e 65 78 74 2d age is the next-
147d5 74 6f 2d 72 69 67 68 74 20 63 68 69 6c 64 2e 20 to-right child.
147d6 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
147d7 67 6e 6f 72 65 20 74 68 65 20 72 65 74 75 72 6e gnore the return
147d8 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 61 value of the ca
147d9 6c 6c 20 74 6f 20 66 69 6c 6c 49 6e 43 65 6c 6c ll to fillInCell
147da 28 29 2e 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 (). fillInCell()
147db 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6f 6e 6c 79 . ** may only
147dc 20 72 65 74 75 72 6e 20 6f 74 68 65 72 20 74 68 return other th
147dd 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 an SQLITE_OK if
147de 69 74 20 69 73 20 72 65 71 75 69 72 65 64 20 74 it is required t
147df 6f 20 61 6c 6c 6f 63 61 74 65 0a 20 20 20 20 2a o allocate. *
147e0 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 * one or more ov
147e1 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 53 69 erflow pages. Si
147e2 6e 63 65 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 nce an internal
147e3 74 61 62 6c 65 20 42 2d 54 72 65 65 20 63 65 6c table B-Tree cel
147e4 6c 20 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 65 l . ** may ne
147e5 76 65 72 20 73 70 69 6c 6c 20 6f 76 65 72 20 6f ver spill over o
147e6 6e 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 nto an overflow
147e7 70 61 67 65 20 28 69 74 20 69 73 20 61 20 6d 61 page (it is a ma
147e8 78 69 6d 75 6d 20 6f 66 20 0a 20 20 20 20 2a 2a ximum of . **
147e9 20 31 33 20 62 79 74 65 73 20 69 6e 20 73 69 7a 13 bytes in siz
147ea 65 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 e), it is not ne
147eb 63 63 65 73 73 61 72 79 20 74 6f 20 63 68 65 63 ccessary to chec
147ec 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 k the return cod
147ed 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a e.. **. **
147ee 20 53 69 6d 69 6c 61 72 6c 79 2c 20 74 68 65 20 Similarly, the
147ef 69 6e 73 65 72 74 43 65 6c 6c 28 29 20 66 75 6e insertCell() fun
147f0 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 ction cannot fai
147f1 6c 20 69 66 20 74 68 65 20 70 61 67 65 0a 20 20 l if the page.
147f2 20 20 2a 2a 20 62 65 69 6e 67 20 69 6e 73 65 72 ** being inser
147f3 74 65 64 20 69 6e 74 6f 20 69 73 20 61 6c 72 65 ted into is alre
147f4 61 64 79 20 77 72 69 74 61 62 6c 65 20 61 6e 64 ady writable and
147f5 20 74 68 65 20 63 65 6c 6c 20 64 6f 65 73 20 6e the cell does n
147f6 6f 74 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 ot . ** conta
147f7 69 6e 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 in an overflow p
147f8 6f 69 6e 74 65 72 2e 20 53 6f 20 69 67 6e 6f 72 ointer. So ignor
147f9 65 20 74 68 69 73 20 72 65 74 75 72 6e 20 63 6f e this return co
147fa 64 65 20 74 6f 6f 2e 0a 20 20 20 20 2a 2f 0a 20 de too.. */.
147fb 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
147fc 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 ->nCell>0 );.
147fd 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c pCell = findCel
147fe 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e l(pPage, pPage->
147ff 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 73 71 nCell-1);. sq
14800 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 lite3BtreeParseC
14801 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 ellPtr(pPage, pC
14802 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 ell, &info);.
14803 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 72 fillInCell(pPar
14804 65 6e 74 2c 20 70 61 72 65 6e 74 43 65 6c 6c 2c ent, parentCell,
14805 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 0, info.nKey, 0
14806 2c 20 30 2c 20 30 2c 20 26 70 61 72 65 6e 74 53 , 0, 0, &parentS
14807 69 7a 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ize);. assert
14808 28 20 70 61 72 65 6e 74 53 69 7a 65 3c 36 34 20 ( parentSize<64
14809 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
1480a 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1480b 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e teable(pParent->
1480c 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 pDbPage) );.
1480d 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 insertCell(pPare
1480e 6e 74 2c 20 70 61 72 65 6e 74 49 64 78 2c 20 70 nt, parentIdx, p
1480f 61 72 65 6e 74 43 65 6c 6c 2c 20 70 61 72 65 6e arentCell, paren
14810 74 53 69 7a 65 2c 20 30 2c 20 34 29 3b 0a 20 20 tSize, 0, 4);.
14811 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f put4byte(findO
14812 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72 verflowCell(pPar
14813 65 6e 74 2c 70 61 72 65 6e 74 49 64 78 29 2c 20 ent,parentIdx),
14814 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 pPage->pgno);.
14815 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 72 put4byte(&pPar
14816 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 ent->aData[pPare
14817 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d nt->hdrOffset+8]
14818 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 , pgnoNew);. .
14819 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 /* If this is
1481a 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 an auto-vacuum
1481b 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 database, update
1481c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
1481d 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 65 6e 74 . ** with ent
1481e 72 69 65 73 20 66 6f 72 20 74 68 65 20 6e 65 77 ries for the new
1481f 20 70 61 67 65 2c 20 61 6e 64 20 61 6e 79 20 70 page, and any p
14820 6f 69 6e 74 65 72 20 66 72 6f 6d 20 74 68 65 20 ointer from the
14821 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 . ** cell on
14822 74 68 65 20 70 61 67 65 20 74 6f 20 61 6e 20 6f the page to an o
14823 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 verflow page..
14824 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41 */. if( ISA
14825 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
14826 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
14827 74 28 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 t(pBt, pgnoNew,
14828 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 PTRMAP_BTREE, pP
14829 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 arent->pgno);.
1482a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1482b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1482c 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f rc = ptrmapPutO
1482d 76 66 6c 28 70 4e 65 77 2c 20 30 29 3b 0a 20 20 vfl(pNew, 0);.
1482e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1482f 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 /* Release the
14830 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 reference to the
14831 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 new page. */.
14832 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e releasePage(pN
14833 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 ew);. }.. /* A
14834 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 t this point the
14835 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 76 61 pPage->nFree va
14836 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 73 65 riable is not se
14837 74 20 63 6f 72 72 65 63 74 6c 79 20 77 69 74 68 t correctly with
14838 0a 20 20 2a 2a 20 72 65 73 70 65 63 74 20 74 6f . ** respect to
14839 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
1483a 74 68 65 20 70 61 67 65 20 28 62 65 63 61 75 73 the page (becaus
1483b 65 20 69 74 20 77 61 73 20 73 65 74 20 74 6f 20 e it was set to
1483c 30 20 62 79 20 0a 20 20 2a 2a 20 69 6e 73 65 72 0 by . ** inser
1483d 74 43 65 6c 6c 29 2e 20 53 6f 20 63 61 6c 6c 20 tCell). So call
1483e 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
1483f 50 61 67 65 28 29 20 74 6f 20 6d 61 6b 65 20 73 Page() to make s
14840 75 72 65 20 69 74 20 69 73 0a 20 20 2a 2a 20 63 ure it is. ** c
14841 6f 72 72 65 63 74 2e 0a 20 20 2a 2a 0a 20 20 2a orrect.. **. *
14842 2a 20 54 68 69 73 20 68 61 73 20 74 6f 20 62 65 * This has to be
14843 20 64 6f 6e 65 20 65 76 65 6e 20 69 66 20 61 6e done even if an
14844 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 error will be r
14845 65 74 75 72 6e 65 64 2e 20 4e 6f 72 6d 61 6c 6c eturned. Normall
14846 79 2c 20 69 66 0a 20 20 2a 2a 20 61 6e 20 65 72 y, if. ** an er
14847 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
14848 67 20 74 72 65 65 20 62 61 6c 61 6e 63 69 6e 67 g tree balancing
14849 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f , the contents o
1484a 66 20 4d 65 6d 50 61 67 65 20 61 72 65 0a 20 20 f MemPage are.
1484b 2a 2a 20 6e 6f 74 20 69 6d 70 6f 72 74 61 6e 74 ** not important
1484c 2c 20 61 73 20 74 68 65 79 20 77 69 6c 6c 20 62 , as they will b
1484d 65 20 72 65 63 61 6c 63 75 6c 61 74 65 64 20 77 e recalculated w
1484e 68 65 6e 20 74 68 65 20 70 61 67 65 20 69 73 20 hen the page is
1484f 72 6f 6c 6c 65 64 0a 20 20 2a 2a 20 62 61 63 6b rolled. ** back
14850 2e 20 42 75 74 20 68 65 72 65 2c 20 69 6e 20 62 . But here, in b
14851 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 2c 20 alance_quick(),
14852 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
14853 68 61 74 20 70 50 61 67 65 20 68 61 73 20 0a 20 hat pPage has .
14854 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e ** not yet been
14855 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 6f 72 marked dirty or
14856 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
14857 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
14858 54 68 65 72 65 66 6f 72 65 0a 20 20 2a 2a 20 69 Therefore. ** i
14859 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 6f t will not be ro
1485a 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 73 6f lled back and so
1485b 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 it is important
1485c 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
1485d 61 74 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 at. ** the page
1485e 20 64 61 74 61 20 61 6e 64 20 63 6f 6e 74 65 6e data and conten
1485f 74 73 20 6f 66 20 4d 65 6d 50 61 67 65 20 61 72 ts of MemPage ar
14860 65 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 20 20 e consistent..
14861 2a 2f 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e */. pPage->isIn
14862 69 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 it = 0;. sqlite
14863 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 3BtreeInitPage(p
14864 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 Page);.. /* If
14865 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 20 everything else
14866 73 75 63 63 65 65 64 65 64 2c 20 62 61 6c 61 6e succeeded, balan
14867 63 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 ce the parent pa
14868 67 65 2c 20 69 6e 20 0a 20 20 2a 2a 20 63 61 73 ge, in . ** cas
14869 65 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 e the divider ce
1486a 6c 6c 20 69 6e 73 65 72 74 65 64 20 63 61 75 73 ll inserted caus
1486b 65 64 20 69 74 20 74 6f 20 62 65 63 6f 6d 65 20 ed it to become
1486c 6f 76 65 72 66 75 6c 6c 2e 0a 20 20 2a 2f 0a 20 overfull.. */.
1486d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1486e 4f 4b 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 OK ){. releas
1486f 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
14870 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b pCur->iPage--;
14871 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 . rc = balanc
14872 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a e(pCur, 0);. }.
14873 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 return rc;.}.#
14874 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
14875 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 OMIT_QUICKBALANC
14876 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 E */../*.** This
14877 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73 74 72 routine redistr
14878 69 62 75 74 65 73 20 43 65 6c 6c 73 20 6f 6e 20 ibutes Cells on
14879 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 pPage and up to
1487a 4e 4e 2a 32 20 73 69 62 6c 69 6e 67 73 0a 2a 2a NN*2 siblings.**
1487b 20 6f 66 20 70 50 61 67 65 20 73 6f 20 74 68 61 of pPage so tha
1487c 74 20 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65 t all pages have
1487d 20 61 62 6f 75 74 20 74 68 65 20 73 61 6d 65 20 about the same
1487e 61 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 amount of free s
1487f 70 61 63 65 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 pace..** Usually
14880 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 NN siblings on
14881 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 either side of p
14882 50 61 67 65 20 69 73 20 75 73 65 64 20 69 6e 20 Page is used in
14883 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 0a 2a the balancing,.*
14884 2a 20 74 68 6f 75 67 68 20 6d 6f 72 65 20 73 69 * though more si
14885 62 6c 69 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d blings might com
14886 65 20 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65 20 e from one side
14887 69 66 20 70 50 61 67 65 20 69 73 20 74 68 65 20 if pPage is the
14888 66 69 72 73 74 0a 2a 2a 20 6f 72 20 6c 61 73 74 first.** or last
14889 20 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 child of its pa
1488a 72 65 6e 74 2e 20 20 49 66 20 70 50 61 67 65 20 rent. If pPage
1488b 68 61 73 20 66 65 77 65 72 20 74 68 61 6e 20 32 has fewer than 2
1488c 2a 4e 4e 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 *NN siblings.**
1488d 28 73 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 68 (something which
1488e 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e can only happen
1488f 20 69 66 20 70 50 61 67 65 20 69 73 20 74 68 65 if pPage is the
14890 20 72 6f 6f 74 20 70 61 67 65 20 6f 72 20 61 20 root page or a
14891 0a 2a 2a 20 63 68 69 6c 64 20 6f 66 20 72 6f 6f .** child of roo
14892 74 29 20 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 t) then all avai
14893 6c 61 62 6c 65 20 73 69 62 6c 69 6e 67 73 20 70 lable siblings p
14894 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 articipate in th
14895 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a e balancing..**.
14896 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** The number of
14897 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70 50 61 siblings of pPa
14898 67 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 ge might be incr
14899 65 61 73 65 64 20 6f 72 20 64 65 63 72 65 61 73 eased or decreas
1489a 65 64 20 62 79 20 6f 6e 65 20 6f 72 0a 2a 2a 20 ed by one or.**
1489b 74 77 6f 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 two in an effort
1489c 20 74 6f 20 6b 65 65 70 20 70 61 67 65 73 20 6e to keep pages n
1489d 65 61 72 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e early full but n
1489e 6f 74 20 6f 76 65 72 20 66 75 6c 6c 2e 20 54 68 ot over full. Th
1489f 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 e root page.** i
148a0 73 20 73 70 65 63 69 61 6c 20 61 6e 64 20 69 73 s special and is
148a1 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 6e allowed to be n
148a2 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 49 66 20 early empty. If
148a3 70 50 61 67 65 20 69 73 20 0a 2a 2a 20 74 68 65 pPage is .** the
148a4 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e root page, then
148a5 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 74 68 the depth of th
148a6 65 20 74 72 65 65 20 6d 69 67 68 74 20 62 65 20 e tree might be
148a7 69 6e 63 72 65 61 73 65 64 0a 2a 2a 20 6f 72 20 increased.** or
148a8 64 65 63 72 65 61 73 65 64 20 62 79 20 6f 6e 65 decreased by one
148a9 2c 20 61 73 20 6e 65 63 65 73 73 61 72 79 2c 20 , as necessary,
148aa 74 6f 20 6b 65 65 70 20 74 68 65 20 72 6f 6f 74 to keep the root
148ab 20 70 61 67 65 20 66 72 6f 6d 20 62 65 69 6e 67 page from being
148ac 0a 2a 2a 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 .** overfull or
148ad 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 completely empty
148ae 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
148af 74 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 t when this rout
148b0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 ine is called, s
148b1 6f 6d 65 20 6f 66 20 74 68 65 20 43 65 6c 6c 73 ome of the Cells
148b2 20 6f 6e 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 on pPage.** mig
148b3 68 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 ht not actually
148b4 62 65 20 73 74 6f 72 65 64 20 69 6e 20 70 50 61 be stored in pPa
148b5 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 20 20 54 68 ge->aData[]. Th
148b6 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a is can happen.**
148b7 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
148b8 6f 76 65 72 66 75 6c 6c 2e 20 20 50 61 72 74 20 overfull. Part
148b9 6f 66 20 74 68 65 20 6a 6f 62 20 6f 66 20 74 68 of the job of th
148ba 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 6f is routine is to
148bb 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20 61 6c .** make sure al
148bc 6c 20 43 65 6c 6c 73 20 66 6f 72 20 70 50 61 67 l Cells for pPag
148bd 65 20 6f 6e 63 65 20 61 67 61 69 6e 20 66 69 74 e once again fit
148be 20 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 in pPage->aData
148bf 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 []..**.** In the
148c0 20 63 6f 75 72 73 65 20 6f 66 20 62 61 6c 61 6e course of balan
148c1 63 69 6e 67 20 74 68 65 20 73 69 62 6c 69 6e 67 cing the sibling
148c2 73 20 6f 66 20 70 50 61 67 65 2c 20 74 68 65 20 s of pPage, the
148c3 70 61 72 65 6e 74 20 6f 66 20 70 50 61 67 65 0a parent of pPage.
148c4 2a 2a 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 ** might become
148c5 6f 76 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 overfull or unde
148c6 72 66 75 6c 6c 2e 20 20 49 66 20 74 68 61 74 20 rfull. If that
148c7 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 happens, then th
148c8 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 is routine.** is
148c9 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73 69 76 called recursiv
148ca 65 6c 79 20 6f 6e 20 74 68 65 20 70 61 72 65 6e ely on the paren
148cb 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 t..**.** If this
148cc 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66 routine fails f
148cd 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 69 or any reason, i
148ce 74 20 6d 69 67 68 74 20 6c 65 61 76 65 20 74 68 t might leave th
148cf 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 6e e database.** in
148d0 20 61 20 63 6f 72 72 75 70 74 65 64 20 73 74 61 a corrupted sta
148d1 74 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20 te. So if this
148d2 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c 20 74 routine fails, t
148d3 68 65 20 64 61 74 61 62 61 73 65 20 73 68 6f 75 he database shou
148d4 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20 ld.** be rolled
148d5 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 back..*/.static
148d6 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 int balance_nonr
148d7 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 oot(BtCursor *pC
148d8 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a ur){. MemPage *
148d9 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 pPage;
148da 20 20 20 20 2f 2a 20 54 68 65 20 6f 76 65 72 20 /* The over
148db 6f 72 20 75 6e 64 65 72 66 75 6c 6c 20 70 61 67 or underfull pag
148dc 65 20 74 6f 20 62 61 6c 61 6e 63 65 20 2a 2f 0a e to balance */.
148dd 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 MemPage *pPare
148de 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f nt; /
148df 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f 66 20 * The parent of
148e0 70 50 61 67 65 20 2a 2f 0a 20 20 42 74 53 68 61 pPage */. BtSha
148e1 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 red *pBt;
148e2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 /* The w
148e3 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f hole database */
148e4 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 . int nCell = 0
148e5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
148e6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c /* Number of cel
148e7 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a ls in apCell[] *
148e8 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c /. int nMaxCell
148e9 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 s = 0;
148ea 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 /* Allocated si
148eb 7a 65 20 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a ze of apCell, sz
148ec 43 65 6c 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a Cell, aFrom. */.
148ed 20 20 69 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 int nOld;
148ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
148ef 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
148f0 73 20 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a s in apOld[] */.
148f1 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 20 int nNew;
148f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
148f3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
148f4 73 20 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a s in apNew[] */.
148f5 20 20 69 6e 74 20 6e 44 69 76 3b 20 20 20 20 20 int nDiv;
148f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
148f7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c * Number of cell
148f8 73 20 69 6e 20 61 70 44 69 76 5b 5d 20 2a 2f 0a s in apDiv[] */.
148f9 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 int i, j, k;
148fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
148fb 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 * Loop counters
148fc 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20 */. int idx;
148fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
148fe 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 70 50 /* Index of pP
148ff 61 67 65 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e age in pParent->
14900 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 aCell[] */. int
14901 20 6e 78 44 69 76 3b 20 20 20 20 20 20 20 20 20 nxDiv;
14902 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 /* Nex
14903 74 20 64 69 76 69 64 65 72 20 73 6c 6f 74 20 69 t divider slot i
14904 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c n pParent->aCell
14905 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 [] */. int rc;
14906 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14907 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 /* The retu
14908 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 rn code */. int
14909 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b leafCorrection;
1490a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34 20 69 /* 4 i
1490b 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 f pPage is a lea
1490c 66 2e 20 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a f. 0 if not */.
1490d 20 20 69 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 int leafData;
1490e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1490f 2a 20 54 72 75 65 20 69 66 20 70 50 61 67 65 20 * True if pPage
14910 69 73 20 61 20 6c 65 61 66 20 6f 66 20 61 20 4c is a leaf of a L
14911 45 41 46 44 41 54 41 20 74 72 65 65 20 2a 2f 0a EAFDATA tree */.
14912 20 20 69 6e 74 20 75 73 61 62 6c 65 53 70 61 63 int usableSpac
14913 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
14914 2a 20 42 79 74 65 73 20 69 6e 20 70 50 61 67 65 * Bytes in pPage
14915 20 62 65 79 6f 6e 64 20 74 68 65 20 68 65 61 64 beyond the head
14916 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 er */. int page
14917 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 Flags;
14918 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 /* Value of
14919 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d pPage->aData[0]
1491a 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 */. int subtot
1491b 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 al;
1491c 20 20 20 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f /* Subtotal o
1491d 66 20 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 f bytes in cells
1491e 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a on one page */.
1491f 20 20 69 6e 74 20 69 53 70 61 63 65 31 20 3d 20 int iSpace1 =
14920 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
14921 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 62 * First unused b
14922 79 74 65 20 6f 66 20 61 53 70 61 63 65 31 5b 5d yte of aSpace1[]
14923 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 65 */. int iSpace
14924 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2 = 0;
14925 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 /* First unus
14926 65 64 20 62 79 74 65 20 6f 66 20 61 53 70 61 63 ed byte of aSpac
14927 65 32 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a e2[] */. int sz
14928 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 Scratch;
14929 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1492a 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 f scratch memory
1492b 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 requested */.
1492c 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c 64 5b 4e MemPage *apOld[N
1492d 42 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 B]; /*
1492e 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 pPage and up to
1492f 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a two siblings */.
14930 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 6c 64 5b 4e Pgno pgnoOld[N
14931 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f B]; /
14932 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 73 20 66 * Page numbers f
14933 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 or each page in
14934 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 4d 65 6d apOld[] */. Mem
14935 50 61 67 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d Page *apCopy[NB]
14936 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 ; /* Pri
14937 76 61 74 65 20 63 6f 70 69 65 73 20 6f 66 20 61 vate copies of a
14938 70 4f 6c 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a pOld[] pages */.
14939 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 MemPage *apNew
1493a 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f [NB+2]; /
1493b 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 * pPage and up t
1493c 6f 20 4e 42 20 73 69 62 6c 69 6e 67 73 20 61 66 o NB siblings af
1493d 74 65 72 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f ter balancing */
1493e 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 5b . Pgno pgnoNew[
1493f 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 NB+2];
14940 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 73 20 /* Page numbers
14941 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e for each page in
14942 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 75 38 apNew[] */. u8
14943 20 2a 61 70 44 69 76 5b 4e 42 5d 3b 20 20 20 20 *apDiv[NB];
14944 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 /* Di
14945 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 20 70 vider cells in p
14946 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 Parent */. int
14947 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 cntNew[NB+2];
14948 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
14949 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 20 x in aCell[] of
1494a 63 65 6c 6c 20 61 66 74 65 72 20 69 2d 74 68 20 cell after i-th
1494b 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a page */. int sz
1494c 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 New[NB+2];
1494d 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e /* Combin
1494e 65 64 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 73 ed size of cells
1494f 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20 70 place on i-th p
14950 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 age */. u8 **ap
14951 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 Cell = 0;
14952 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c /* All cel
14953 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 6e 63 65 ls begin balance
14954 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 d */. u16 *szCe
14955 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
14956 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a /* Local siz
14957 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 69 e of all cells i
14958 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 n apCell[] */.
14959 75 38 20 2a 61 43 6f 70 79 5b 4e 42 5d 3b 20 20 u8 *aCopy[NB];
1495a 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
1495b 66 6f 72 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 for holding data
1495c 20 6f 66 20 61 70 43 6f 70 79 5b 5d 20 2a 2f 0a of apCopy[] */.
1495d 20 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20 20 u8 *aSpace1;
1495e 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 /* Spac
1495f 65 20 66 6f 72 20 63 6f 70 69 65 73 20 6f 66 20 e for copies of
14960 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 62 dividers cells b
14961 65 66 6f 72 65 20 62 61 6c 61 6e 63 65 20 2a 2f efore balance */
14962 0a 20 20 75 38 20 2a 61 53 70 61 63 65 32 20 3d . u8 *aSpace2 =
14963 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 53 70 61 0; /* Spa
14964 63 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 ce for overflow
14965 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 61 dividers cells a
14966 66 74 65 72 20 62 61 6c 61 6e 63 65 20 2a 2f 0a fter balance */.
14967 20 20 75 38 20 2a 61 46 72 6f 6d 20 3d 20 30 3b u8 *aFrom = 0;
14968 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 .. pPage = pCur
14969 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
1496a 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 Page];. assert(
1496b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1496c 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
1496d 6d 75 74 65 78 29 20 29 3b 0a 20 20 56 56 41 5f mutex) );. VVA_
1496e 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 ONLY( pCur->page
1496f 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b sShuffled = 1 );
14970 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 46 69 6e .. /* . ** Fin
14971 64 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 d the parent pag
14972 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 e.. */. assert
14973 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 ( pCur->iPage>0
14974 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
14975 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 ge->isInit );.
14976 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
14977 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
14978 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
14979 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 || pPage->nOverf
1497a 6c 6f 77 3d 3d 31 20 29 3b 0a 20 20 70 42 74 20 low==1 );. pBt
1497b 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
1497c 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72 2d 3e pParent = pCur->
1497d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1497e 67 65 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ge-1];. assert(
1497f 20 70 50 61 72 65 6e 74 20 29 3b 0a 20 20 69 66 pParent );. if
14980 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
14981 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
14982 72 69 74 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 rite(pParent->pD
14983 62 50 61 67 65 29 29 20 29 7b 0a 20 20 20 20 72 bPage)) ){. r
14984 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 eturn rc;. }..
14985 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 TRACE(("BALANCE
14986 3a 20 62 65 67 69 6e 20 70 61 67 65 20 25 64 20 : begin page %d
14987 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c 20 child of %d\n",
14988 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 61 pPage->pgno, pPa
14989 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 0a 23 rent->pgno));..#
1498a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1498b 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a IT_QUICKBALANCE.
1498c 20 20 2f 2a 0a 20 20 2a 2a 20 41 20 73 70 65 63 /*. ** A spec
1498d 69 61 6c 20 63 61 73 65 3a 20 20 49 66 20 61 20 ial case: If a
1498e 6e 65 77 20 65 6e 74 72 79 20 68 61 73 20 6a 75 new entry has ju
1498f 73 74 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64 st been inserted
14990 20 69 6e 74 6f 20 61 0a 20 20 2a 2a 20 74 61 62 into a. ** tab
14991 6c 65 20 28 74 68 61 74 20 69 73 2c 20 61 20 62 le (that is, a b
14992 74 72 65 65 20 77 69 74 68 20 69 6e 74 65 67 65 tree with intege
14993 72 20 6b 65 79 73 20 61 6e 64 20 61 6c 6c 20 64 r keys and all d
14994 61 74 61 20 61 74 20 74 68 65 20 6c 65 61 76 65 ata at the leave
14995 73 29 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 s). ** and the
14996 6e 65 77 20 65 6e 74 72 79 20 69 73 20 74 68 65 new entry is the
14997 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 right-most entr
14998 79 20 69 6e 20 74 68 65 20 74 72 65 65 20 28 69 y in the tree (i
14999 74 20 68 61 73 20 74 68 65 0a 20 20 2a 2a 20 6c t has the. ** l
1499a 61 72 67 65 73 74 20 6b 65 79 29 20 74 68 65 6e argest key) then
1499b 20 75 73 65 20 74 68 65 20 73 70 65 63 69 61 6c use the special
1499c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 balance_quick()
1499d 20 72 6f 75 74 69 6e 65 20 66 6f 72 0a 20 20 2a routine for. *
1499e 2a 20 62 61 6c 61 6e 63 69 6e 67 2e 20 20 62 61 * balancing. ba
1499f 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 73 lance_quick() is
149a0 20 6d 75 63 68 20 66 61 73 74 65 72 20 61 6e 64 much faster and
149a1 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 74 69 results in a ti
149a2 67 68 74 65 72 0a 20 20 2a 2a 20 70 61 63 6b 69 ghter. ** packi
149a3 6e 67 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 ng of data in th
149a4 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 2e 0a 20 e common case..
149a5 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 2d */. if( pPage-
149a6 3e 6c 65 61 66 20 26 26 0a 20 20 20 20 20 20 70 >leaf &&. p
149a7 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 0a Page->intKey &&.
149a8 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 pPage->nOv
149a9 65 72 66 6c 6f 77 3d 3d 31 20 26 26 0a 20 20 20 erflow==1 &&.
149aa 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b pPage->aOvfl[
149ab 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 0].idx==pPage->n
149ac 43 65 6c 6c 20 26 26 0a 20 20 20 20 20 20 70 50 Cell &&. pP
149ad 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d 31 20 26 arent->pgno!=1 &
149ae 26 0a 20 20 20 20 20 20 67 65 74 34 62 79 74 65 &. get4byte
149af 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 (&pParent->aData
149b0 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 [pParent->hdrOff
149b1 73 65 74 2b 38 5d 29 3d 3d 70 50 61 67 65 2d 3e set+8])==pPage->
149b2 70 67 6e 6f 0a 20 20 29 7b 0a 20 20 20 20 61 73 pgno. ){. as
149b3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 sert( pPage->int
149b4 4b 65 79 20 29 3b 0a 20 20 20 20 2f 2a 0a 20 20 Key );. /*.
149b5 20 20 2a 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b ** TODO: Check
149b6 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 74 6f the siblings to
149b7 20 74 68 65 20 6c 65 66 74 20 6f 66 20 70 50 61 the left of pPa
149b8 67 65 2e 20 49 74 20 6d 61 79 20 62 65 20 74 68 ge. It may be th
149b9 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 79 20 61 at. ** they a
149ba 72 65 20 6e 6f 74 20 66 75 6c 6c 20 61 6e 64 20 re not full and
149bb 6e 6f 20 6e 65 77 20 70 61 67 65 20 69 73 20 72 no new page is r
149bc 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a equired.. */.
149bd 20 20 20 20 72 65 74 75 72 6e 20 62 61 6c 61 6e return balan
149be 63 65 5f 71 75 69 63 6b 28 70 43 75 72 29 3b 0a ce_quick(pCur);.
149bf 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 }.#endif.. if
149c0 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
149c1 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
149c2 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 rite(pPage->pDbP
149c3 61 67 65 29 29 20 29 7b 0a 20 20 20 20 72 65 74 age)) ){. ret
149c4 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f urn rc;. }.. /
149c5 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 *. ** Find the
149c6 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65 cell in the pare
149c7 6e 74 20 70 61 67 65 20 77 68 6f 73 65 20 6c 65 nt page whose le
149c8 66 74 20 63 68 69 6c 64 20 70 6f 69 6e 74 73 20 ft child points
149c9 62 61 63 6b 0a 20 20 2a 2a 20 74 6f 20 70 50 61 back. ** to pPa
149ca 67 65 2e 20 20 54 68 65 20 22 69 64 78 22 20 76 ge. The "idx" v
149cb 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 69 ariable is the i
149cc 6e 64 65 78 20 6f 66 20 74 68 61 74 20 63 65 6c ndex of that cel
149cd 6c 2e 20 20 49 66 20 70 50 61 67 65 0a 20 20 2a l. If pPage. *
149ce 2a 20 69 73 20 74 68 65 20 72 69 67 68 74 6d 6f * is the rightmo
149cf 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 st child of pPar
149d0 65 6e 74 20 74 68 65 6e 20 73 65 74 20 69 64 78 ent then set idx
149d1 20 74 6f 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 to pParent->nCe
149d2 6c 6c 20 0a 20 20 2a 2f 0a 20 20 69 64 78 20 3d ll . */. idx =
149d3 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
149d4 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 61 r->iPage-1];. a
149d5 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 ssertParentIndex
149d6 28 70 50 61 72 65 6e 74 2c 20 69 64 78 2c 20 70 (pParent, idx, p
149d7 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 Page->pgno);..
149d8 2f 2a 0a 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69 /*. ** Initiali
149d9 7a 65 20 76 61 72 69 61 62 6c 65 73 20 73 6f 20 ze variables so
149da 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 that it will be
149db 73 61 66 65 20 74 6f 20 6a 75 6d 70 0a 20 20 2a safe to jump. *
149dc 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 62 61 * directly to ba
149dd 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 20 61 74 lance_cleanup at
149de 20 61 6e 79 20 6d 6f 6d 65 6e 74 2e 0a 20 20 2a any moment.. *
149df 2f 0a 20 20 6e 4f 6c 64 20 3d 20 6e 4e 65 77 20 /. nOld = nNew
149e0 3d 20 30 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 = 0;.. /*. **
149e1 46 69 6e 64 20 73 69 62 6c 69 6e 67 20 70 61 67 Find sibling pag
149e2 65 73 20 74 6f 20 70 50 61 67 65 20 61 6e 64 20 es to pPage and
149e3 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 the cells in pPa
149e4 72 65 6e 74 20 74 68 61 74 20 64 69 76 69 64 65 rent that divide
149e5 0a 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e . ** the siblin
149e6 67 73 2e 20 20 41 6e 20 61 74 74 65 6d 70 74 20 gs. An attempt
149e7 69 73 20 6d 61 64 65 20 74 6f 20 66 69 6e 64 20 is made to find
149e8 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 65 NN siblings on e
149e9 69 74 68 65 72 0a 20 20 2a 2a 20 73 69 64 65 20 ither. ** side
149ea 6f 66 20 70 50 61 67 65 2e 20 20 4d 6f 72 65 20 of pPage. More
149eb 73 69 62 6c 69 6e 67 73 20 61 72 65 20 74 61 6b siblings are tak
149ec 65 6e 20 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65 en from one side
149ed 2c 20 68 6f 77 65 76 65 72 2c 20 69 66 0a 20 20 , however, if.
149ee 2a 2a 20 70 50 61 67 65 20 74 68 65 72 65 20 61 ** pPage there a
149ef 72 65 20 66 65 77 65 72 20 74 68 61 6e 20 4e 4e re fewer than NN
149f0 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74 68 65 siblings on the
149f1 20 6f 74 68 65 72 20 73 69 64 65 2e 20 20 49 66 other side. If
149f2 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20 68 61 pParent. ** ha
149f3 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20 63 68 s NB or fewer ch
149f4 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c 6c 20 ildren then all
149f5 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50 61 72 children of pPar
149f6 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e 0a 20 ent are taken..
149f7 20 2a 2f 0a 20 20 6e 78 44 69 76 20 3d 20 69 64 */. nxDiv = id
149f8 78 20 2d 20 4e 4e 3b 0a 20 20 69 66 28 20 6e 78 x - NN;. if( nx
149f9 44 69 76 20 2b 20 4e 42 20 3e 20 70 50 61 72 65 Div + NB > pPare
149fa 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 nt->nCell ){.
149fb 20 6e 78 44 69 76 20 3d 20 70 50 61 72 65 6e 74 nxDiv = pParent
149fc 2d 3e 6e 43 65 6c 6c 20 2d 20 4e 42 20 2b 20 31 ->nCell - NB + 1
149fd 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 78 44 69 ;. }. if( nxDi
149fe 76 3c 30 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 v<0 ){. nxDiv
149ff 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 44 69 76 = 0;. }. nDiv
14a00 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c = 0;. for(i=0,
14a01 20 6b 3d 6e 78 44 69 76 3b 20 69 3c 4e 42 3b 20 k=nxDiv; i<NB;
14a02 69 2b 2b 2c 20 6b 2b 2b 29 7b 0a 20 20 20 20 69 i++, k++){. i
14a03 66 28 20 6b 3c 70 50 61 72 65 6e 74 2d 3e 6e 43 f( k<pParent->nC
14a04 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 61 70 44 ell ){. apD
14a05 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c iv[i] = findCell
14a06 28 70 50 61 72 65 6e 74 2c 20 6b 29 3b 0a 20 20 (pParent, k);.
14a07 20 20 20 20 6e 44 69 76 2b 2b 3b 0a 20 20 20 20 nDiv++;.
14a08 20 20 61 73 73 65 72 74 28 20 21 70 50 61 72 65 assert( !pPare
14a09 6e 74 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20 20 nt->leaf );.
14a0a 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d 20 67 pgnoOld[i] = g
14a0b 65 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d et4byte(apDiv[i]
14a0c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
14a0d 20 6b 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 k==pParent->nCe
14a0e 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 67 6e 6f ll ){. pgno
14a0f 4f 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 74 Old[i] = get4byt
14a10 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 e(&pParent->aDat
14a11 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 a[pParent->hdrOf
14a12 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 7d 65 fset+8]);. }e
14a13 6c 73 65 7b 0a 20 20 20 20 20 20 62 72 65 61 6b lse{. break
14a14 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
14a15 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
14a16 70 42 74 2c 20 70 67 6e 6f 4f 6c 64 5b 69 5d 2c pBt, pgnoOld[i],
14a17 20 26 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 &apOld[i]);.
14a18 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 if( rc ) goto b
14a19 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a alance_cleanup;.
14a1a 20 20 20 20 2f 2a 20 61 70 4f 6c 64 5b 69 5d 2d /* apOld[i]-
14a1b 3e 69 64 78 50 61 72 65 6e 74 20 3d 20 6b 3b 20 >idxParent = k;
14a1c 2a 2f 0a 20 20 20 20 61 70 43 6f 70 79 5b 69 5d */. apCopy[i]
14a1d 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 = 0;. assert
14a1e 28 20 69 3d 3d 6e 4f 6c 64 20 29 3b 0a 20 20 20 ( i==nOld );.
14a1f 20 6e 4f 6c 64 2b 2b 3b 0a 20 20 20 20 6e 4d 61 nOld++;. nMa
14a20 78 43 65 6c 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c xCells += 1+apOl
14a21 64 5b 69 5d 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c d[i]->nCell+apOl
14a22 64 5b 69 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b d[i]->nOverflow;
14a23 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
14a24 6e 4d 61 78 43 65 6c 6c 73 20 61 20 6d 75 6c 74 nMaxCells a mult
14a25 69 70 6c 65 20 6f 66 20 34 20 69 6e 20 6f 72 64 iple of 4 in ord
14a26 65 72 20 74 6f 20 70 72 65 73 65 72 76 65 20 38 er to preserve 8
14a27 2d 62 79 74 65 0a 20 20 2a 2a 20 61 6c 69 67 6e -byte. ** align
14a28 6d 65 6e 74 20 2a 2f 0a 20 20 6e 4d 61 78 43 65 ment */. nMaxCe
14a29 6c 6c 73 20 3d 20 28 6e 4d 61 78 43 65 6c 6c 73 lls = (nMaxCells
14a2a 20 2b 20 33 29 26 7e 33 3b 0a 0a 20 20 2f 2a 0a + 3)&~3;.. /*.
14a2b 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 ** Allocate sp
14a2c 61 63 65 20 66 6f 72 20 6d 65 6d 6f 72 79 20 73 ace for memory s
14a2d 74 72 75 63 74 75 72 65 73 0a 20 20 2a 2f 0a 20 tructures. */.
14a2e 20 73 7a 53 63 72 61 74 63 68 20 3d 0a 20 20 20 szScratch =.
14a2f 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69 nMaxCells*si
14a30 7a 65 6f 66 28 75 38 2a 29 20 20 20 20 20 20 20 zeof(u8*)
14a31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a32 2f 2a 20 61 70 43 65 6c 6c 20 2a 2f 0a 20 20 20 /* apCell */.
14a33 20 20 2b 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69 + nMaxCells*si
14a34 7a 65 6f 66 28 75 31 36 29 20 20 20 20 20 20 20 zeof(u16)
14a35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a36 2f 2a 20 73 7a 43 65 6c 6c 20 2a 2f 0a 20 20 20 /* szCell */.
14a37 20 20 2b 20 28 52 4f 55 4e 44 38 28 73 69 7a 65 + (ROUND8(size
14a38 6f 66 28 4d 65 6d 50 61 67 65 29 29 2b 70 42 74 of(MemPage))+pBt
14a39 2d 3e 70 61 67 65 53 69 7a 65 29 2a 4e 42 20 20 ->pageSize)*NB
14a3a 2f 2a 20 61 43 6f 70 79 20 2a 2f 0a 20 20 20 20 /* aCopy */.
14a3b 20 2b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 + pBt->pageSize
14a3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14a3e 2a 20 61 53 70 61 63 65 31 20 2a 2f 0a 20 20 20 * aSpace1 */.
14a3f 20 20 2b 20 28 49 53 41 55 54 4f 56 41 43 55 55 + (ISAUTOVACUU
14a40 4d 20 3f 20 6e 4d 61 78 43 65 6c 6c 73 20 3a 20 M ? nMaxCells :
14a41 30 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0);
14a42 2f 2a 20 61 46 72 6f 6d 20 2a 2f 0a 20 20 61 70 /* aFrom */. ap
14a43 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 63 Cell = sqlite3Sc
14a44 72 61 74 63 68 4d 61 6c 6c 6f 63 28 20 73 7a 53 ratchMalloc( szS
14a45 63 72 61 74 63 68 20 29 3b 20 0a 20 20 69 66 28 cratch ); . if(
14a46 20 61 70 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 apCell==0 ){.
14a47 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
14a48 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 MEM;. goto ba
14a49 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
14a4a 20 7d 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28 75 }. szCell = (u
14a4b 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6e 4d 61 78 16*)&apCell[nMax
14a4c 43 65 6c 6c 73 5d 3b 0a 20 20 61 43 6f 70 79 5b Cells];. aCopy[
14a4d 30 5d 20 3d 20 28 75 38 2a 29 26 73 7a 43 65 6c 0] = (u8*)&szCel
14a4e 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 l[nMaxCells];.
14a4f 61 73 73 65 72 74 28 20 28 28 61 43 6f 70 79 5b assert( ((aCopy[
14a50 30 5d 20 2d 20 28 75 38 2a 29 61 70 43 65 6c 6c 0] - (u8*)apCell
14a51 29 20 26 20 37 29 3d 3d 30 20 29 3b 20 2f 2a 20 ) & 7)==0 ); /*
14a52 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 8-byte alignment
14a53 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 66 required */. f
14a54 6f 72 28 69 3d 31 3b 20 69 3c 4e 42 3b 20 69 2b or(i=1; i<NB; i+
14a55 2b 29 7b 0a 20 20 20 20 61 43 6f 70 79 5b 69 5d +){. aCopy[i]
14a56 20 3d 20 26 61 43 6f 70 79 5b 69 2d 31 5d 5b 70 = &aCopy[i-1][p
14a57 42 74 2d 3e 70 61 67 65 53 69 7a 65 2b 52 4f 55 Bt->pageSize+ROU
14a58 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 ND8(sizeof(MemPa
14a59 67 65 29 29 5d 3b 0a 20 20 20 20 61 73 73 65 72 ge))];. asser
14a5a 74 28 20 28 28 61 43 6f 70 79 5b 69 5d 20 2d 20 t( ((aCopy[i] -
14a5b 28 75 38 2a 29 61 70 43 65 6c 6c 29 20 26 20 37 (u8*)apCell) & 7
14a5c 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 )==0 ); /* 8-byt
14a5d 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 e alignment requ
14a5e 69 72 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53 ired */. }. aS
14a5f 70 61 63 65 31 20 3d 20 26 61 43 6f 70 79 5b 4e pace1 = &aCopy[N
14a60 42 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 B-1][pBt->pageSi
14a61 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 ze+ROUND8(sizeof
14a62 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 61 (MemPage))];. a
14a63 73 73 65 72 74 28 20 28 28 61 53 70 61 63 65 31 ssert( ((aSpace1
14a64 20 2d 20 28 75 38 2a 29 61 70 43 65 6c 6c 29 20 - (u8*)apCell)
14a65 26 20 37 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d & 7)==0 ); /* 8-
14a66 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 byte alignment r
14a67 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 66 28 equired */. if(
14a68 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
14a69 0a 20 20 20 20 61 46 72 6f 6d 20 3d 20 26 61 53 . aFrom = &aS
14a6a 70 61 63 65 31 5b 70 42 74 2d 3e 70 61 67 65 53 pace1[pBt->pageS
14a6b 69 7a 65 5d 3b 0a 20 20 7d 0a 20 20 61 53 70 61 ize];. }. aSpa
14a6c 63 65 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ce2 = sqlite3Pag
14a6d 65 4d 61 6c 6c 6f 63 28 70 42 74 2d 3e 70 61 67 eMalloc(pBt->pag
14a6e 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 61 53 eSize);. if( aS
14a6f 70 61 63 65 32 3d 3d 30 20 29 7b 0a 20 20 20 20 pace2==0 ){.
14a70 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
14a71 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 M;. goto bala
14a72 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d nce_cleanup;. }
14a73 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 . . /*. ** Ma
14a74 6b 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 ke copies of the
14a75 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 50 61 67 content of pPag
14a76 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e e and its siblin
14a77 67 73 20 69 6e 74 6f 20 61 4f 6c 64 5b 5d 2e 0a gs into aOld[]..
14a78 20 20 2a 2a 20 54 68 65 20 72 65 73 74 20 6f 66 ** The rest of
14a79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 this function w
14a7a 69 6c 6c 20 75 73 65 20 64 61 74 61 20 66 72 6f ill use data fro
14a7b 6d 20 74 68 65 20 63 6f 70 69 65 73 20 72 61 74 m the copies rat
14a7c 68 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 her. ** that th
14a7d 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 e original pages
14a7e 20 73 69 6e 63 65 20 74 68 65 20 6f 72 69 67 69 since the origi
14a7f 6e 61 6c 20 70 61 67 65 73 20 77 69 6c 6c 20 62 nal pages will b
14a80 65 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 70 72 e in the. ** pr
14a81 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 6f ocess of being o
14a82 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f verwritten.. */
14a83 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f . for(i=0; i<nO
14a84 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 ld; i++){. Me
14a85 6d 50 61 67 65 20 2a 70 20 3d 20 61 70 43 6f 70 mPage *p = apCop
14a86 79 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a y[i] = (MemPage*
14a87 29 61 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 6d )aCopy[i];. m
14a88 65 6d 63 70 79 28 70 2c 20 61 70 4f 6c 64 5b 69 emcpy(p, apOld[i
14a89 5d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 ], sizeof(MemPag
14a8a 65 29 29 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74 e));. p->aDat
14a8b 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 5b 31 5d a = (void*)&p[1]
14a8c 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e ;. memcpy(p->
14a8d 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d aData, apOld[i]-
14a8e 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 >aData, pBt->pag
14a8f 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f eSize);. }.. /
14a90 2a 0a 20 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e *. ** Load poin
14a91 74 65 72 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c ters to all cell
14a92 73 20 6f 6e 20 73 69 62 6c 69 6e 67 20 70 61 67 s on sibling pag
14a93 65 73 20 61 6e 64 20 74 68 65 20 64 69 76 69 64 es and the divid
14a94 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e er cells. ** in
14a95 74 6f 20 74 68 65 20 6c 6f 63 61 6c 20 61 70 43 to the local apC
14a96 65 6c 6c 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61 ell[] array. Ma
14a97 6b 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 ke copies of the
14a98 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 divider cells.
14a99 20 2a 2a 20 69 6e 74 6f 20 73 70 61 63 65 20 6f ** into space o
14a9a 62 74 61 69 6e 65 64 20 66 6f 72 6d 20 61 53 70 btained form aSp
14a9b 61 63 65 31 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 ace1[] and remov
14a9c 65 20 74 68 65 20 74 68 65 20 64 69 76 69 64 65 e the the divide
14a9d 72 20 43 65 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f r Cells. ** fro
14a9e 6d 20 70 50 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a m pParent.. **.
14a9f 20 20 2a 2a 20 49 66 20 74 68 65 20 73 69 62 6c ** If the sibl
14aa0 69 6e 67 73 20 61 72 65 20 6f 6e 20 6c 65 61 66 ings are on leaf
14aa1 20 70 61 67 65 73 2c 20 74 68 65 6e 20 74 68 65 pages, then the
14aa2 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 20 child pointers
14aa3 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64 69 76 69 of the. ** divi
14aa4 64 65 72 20 63 65 6c 6c 73 20 61 72 65 20 73 74 der cells are st
14aa5 72 69 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 ripped from the
14aa6 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 74 68 65 cells before the
14aa7 79 20 61 72 65 20 63 6f 70 69 65 64 0a 20 20 2a y are copied. *
14aa8 2a 20 69 6e 74 6f 20 61 53 70 61 63 65 31 5b 5d * into aSpace1[]
14aa9 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 . In this way,
14aaa 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 all cells in apC
14aab 65 6c 6c 5b 5d 20 61 72 65 20 77 69 74 68 6f 75 ell[] are withou
14aac 74 0a 20 20 2a 2a 20 63 68 69 6c 64 20 70 6f 69 t. ** child poi
14aad 6e 74 65 72 73 2e 20 20 49 66 20 73 69 62 6c 69 nters. If sibli
14aae 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 ngs are not leav
14aaf 65 73 2c 20 74 68 65 6e 20 61 6c 6c 20 63 65 6c es, then all cel
14ab0 6c 20 69 6e 0a 20 20 2a 2a 20 61 70 43 65 6c 6c l in. ** apCell
14ab1 5b 5d 20 69 6e 63 6c 75 64 65 20 63 68 69 6c 64 [] include child
14ab2 20 70 6f 69 6e 74 65 72 73 2e 20 20 45 69 74 68 pointers. Eith
14ab3 65 72 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c er way, all cell
14ab4 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 0a 20 20 s in apCell[].
14ab5 2a 2a 20 61 72 65 20 61 6c 69 6b 65 2e 0a 20 20 ** are alike..
14ab6 2a 2a 0a 20 20 2a 2a 20 6c 65 61 66 43 6f 72 72 **. ** leafCorr
14ab7 65 63 74 69 6f 6e 3a 20 20 34 20 69 66 20 70 50 ection: 4 if pP
14ab8 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20 20 age is a leaf.
14ab9 30 20 69 66 20 70 50 61 67 65 20 69 73 20 6e 6f 0 if pPage is no
14aba 74 20 61 20 6c 65 61 66 2e 0a 20 20 2a 2a 20 20 t a leaf.. **
14abb 20 20 20 20 20 6c 65 61 66 44 61 74 61 3a 20 20 leafData:
14abc 31 20 69 66 20 70 50 61 67 65 20 68 6f 6c 64 73 1 if pPage holds
14abd 20 6b 65 79 2b 64 61 74 61 20 61 6e 64 20 70 50 key+data and pP
14abe 61 72 65 6e 74 20 68 6f 6c 64 73 20 6f 6e 6c 79 arent holds only
14abf 20 6b 65 79 73 2e 0a 20 20 2a 2f 0a 20 20 6e 43 keys.. */. nC
14ac0 65 6c 6c 20 3d 20 30 3b 0a 20 20 6c 65 61 66 43 ell = 0;. leafC
14ac1 6f 72 72 65 63 74 69 6f 6e 20 3d 20 70 50 61 67 orrection = pPag
14ac2 65 2d 3e 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61 e->leaf*4;. lea
14ac3 66 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 68 fData = pPage->h
14ac4 61 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d asData;. for(i=
14ac5 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0; i<nOld; i++){
14ac6 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f . MemPage *pO
14ac7 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69 5d 3b 0a ld = apCopy[i];.
14ac8 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 20 3d 20 int limit =
14ac9 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 pOld->nCell+pOld
14aca 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 ->nOverflow;.
14acb 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 for(j=0; j<limi
14acc 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61 t; j++){. a
14acd 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 ssert( nCell<nMa
14ace 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 xCells );.
14acf 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 apCell[nCell] =
14ad0 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c findOverflowCell
14ad1 28 70 4f 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20 (pOld, j);.
14ad2 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d szCell[nCell] =
14ad3 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c cellSizePtr(pOl
14ad4 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d d, apCell[nCell]
14ad5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 41 );. if( ISA
14ad6 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
14ad7 20 20 20 20 20 69 6e 74 20 61 3b 0a 20 20 20 20 int a;.
14ad8 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d aFrom[nCell]
14ad9 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f = i;. fo
14ada 72 28 61 3d 30 3b 20 61 3c 70 4f 6c 64 2d 3e 6e r(a=0; a<pOld->n
14adb 4f 76 65 72 66 6c 6f 77 3b 20 61 2b 2b 29 7b 0a Overflow; a++){.
14adc 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4f if( pO
14add 6c 64 2d 3e 61 4f 76 66 6c 5b 61 5d 2e 70 43 65 ld->aOvfl[a].pCe
14ade 6c 6c 3d 3d 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c ll==apCell[nCell
14adf 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ] ){.
14ae0 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 aFrom[nCell] =
14ae1 30 78 46 46 3b 0a 20 20 20 20 20 20 20 20 20 20 0xFF;.
14ae2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
14ae3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
14ae4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 65 }. nCe
14ae5 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ll++;. }.
14ae6 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 29 7b 0a if( i<nOld-1 ){.
14ae7 20 20 20 20 20 20 75 31 36 20 73 7a 20 3d 20 63 u16 sz = c
14ae8 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 ellSizePtr(pPare
14ae9 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 20 nt, apDiv[i]);.
14aea 20 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 if( leafDat
14aeb 61 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 a ){. /*
14aec 57 69 74 68 20 74 68 65 20 4c 45 41 46 44 41 54 With the LEAFDAT
14aed 41 20 66 6c 61 67 2c 20 70 50 61 72 65 6e 74 20 A flag, pParent
14aee 63 65 6c 6c 73 20 68 6f 6c 64 20 6f 6e 6c 79 20 cells hold only
14aef 49 4e 54 4b 45 59 73 20 74 68 61 74 0a 20 20 20 INTKEYs that.
14af0 20 20 20 20 20 2a 2a 20 61 72 65 20 64 75 70 6c ** are dupl
14af1 69 63 61 74 65 73 20 6f 66 20 6b 65 79 73 20 6f icates of keys o
14af2 6e 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 n the child page
14af3 73 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72 s. We need to r
14af4 65 6d 6f 76 65 0a 20 20 20 20 20 20 20 20 2a 2a emove. **
14af5 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
14af6 6c 73 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 2c ls from pParent,
14af7 20 62 75 74 20 74 68 65 20 64 69 76 69 64 65 72 but the divider
14af8 73 20 63 65 6c 6c 73 20 61 72 65 20 6e 6f 74 0a s cells are not.
14af9 20 20 20 20 20 20 20 20 2a 2a 20 61 64 64 65 64 ** added
14afa 20 74 6f 20 61 70 43 65 6c 6c 5b 5d 20 62 65 63 to apCell[] bec
14afb 61 75 73 65 20 74 68 65 79 20 61 72 65 20 64 75 ause they are du
14afc 70 6c 69 63 61 74 65 73 20 6f 66 20 63 68 69 6c plicates of chil
14afd 64 20 63 65 6c 6c 73 2e 0a 20 20 20 20 20 20 20 d cells..
14afe 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 72 6f 70 */. drop
14aff 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 Cell(pParent, nx
14b00 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 Div, sz);.
14b01 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 }else{. u
14b02 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 8 *pTemp;.
14b03 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c assert( nCell<
14b04 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 nMaxCells );.
14b05 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c szCell[nCel
14b06 6c 5d 20 3d 20 73 7a 3b 0a 20 20 20 20 20 20 20 l] = sz;.
14b07 20 70 54 65 6d 70 20 3d 20 26 61 53 70 61 63 65 pTemp = &aSpace
14b08 31 5b 69 53 70 61 63 65 31 5d 3b 0a 20 20 20 20 1[iSpace1];.
14b09 20 20 20 20 69 53 70 61 63 65 31 20 2b 3d 20 73 iSpace1 += s
14b0a 7a 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 z;. asser
14b0b 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 t( sz<=pBt->page
14b0c 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 Size/4 );.
14b0d 20 20 61 73 73 65 72 74 28 20 69 53 70 61 63 65 assert( iSpace
14b0e 31 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 1<=pBt->pageSize
14b0f 20 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 );. memc
14b10 70 79 28 70 54 65 6d 70 2c 20 61 70 44 69 76 5b py(pTemp, apDiv[
14b11 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 20 i], sz);.
14b12 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d apCell[nCell] =
14b13 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65 pTemp+leafCorre
14b14 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 69 ction;. i
14b15 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
14b16 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 46 72 ){. aFr
14b17 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 om[nCell] = 0xFF
14b18 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
14b19 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 dropCell(pPa
14b1a 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a 29 rent, nxDiv, sz)
14b1b 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c ;. szCell
14b1c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 6c 65 61 66 43 [nCell] -= leafC
14b1d 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 orrection;.
14b1e 20 20 20 61 73 73 65 72 74 28 20 67 65 74 34 62 assert( get4b
14b1f 79 74 65 28 70 54 65 6d 70 29 3d 3d 70 67 6e 6f yte(pTemp)==pgno
14b20 4f 6c 64 5b 69 5d 20 29 3b 0a 20 20 20 20 20 20 Old[i] );.
14b21 20 20 69 66 28 20 21 70 4f 6c 64 2d 3e 6c 65 61 if( !pOld->lea
14b22 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 f ){. a
14b23 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 ssert( leafCorre
14b24 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 ction==0 );.
14b25 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 /* The rig
14b26 68 74 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 ht pointer of th
14b27 65 20 63 68 69 6c 64 20 70 61 67 65 20 70 4f 6c e child page pOl
14b28 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 d becomes the le
14b29 66 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ft. **
14b2a 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 64 pointer of the d
14b2b 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 ivider cell */.
14b2c 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
14b2d 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 26 apCell[nCell], &
14b2e 70 4f 6c 64 2d 3e 61 44 61 74 61 5b 70 4f 6c 64 pOld->aData[pOld
14b2f 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 ->hdrOffset+8],
14b30 34 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 4);. }els
14b31 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 e{. ass
14b32 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 ert( leafCorrect
14b33 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 ion==4 );.
14b34 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6e if( szCell[n
14b35 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20 20 20 Cell]<4 ){.
14b36 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 /* Do not
14b37 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c 73 allow any cells
14b38 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 20 smaller than 4
14b39 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 bytes. */.
14b3a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 szCell[nCe
14b3b 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 ll] = 4;.
14b3c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
14b3d 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a nCell++;.
14b3e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
14b3f 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 67 }.. /*. ** Fig
14b40 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d 62 ure out the numb
14b41 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65 64 er of pages need
14b42 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6e ed to hold all n
14b43 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a 2a Cell cells.. **
14b44 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d 62 Store this numb
14b45 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 6f er in "k". Also
14b46 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b 5d compute szNew[]
14b47 20 77 68 69 63 68 20 69 73 20 74 68 65 20 74 6f which is the to
14b48 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f 66 tal. ** size of
14b49 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 68 all cells on th
14b4a 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64 20 e i-th page and
14b4b 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 cntNew[] which i
14b4c 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a s the index. **
14b4d 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 20 in apCell[] of
14b4e 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64 69 the cell that di
14b4f 76 69 64 65 73 20 70 61 67 65 20 69 20 66 72 6f vides page i fro
14b50 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 20 m page i+1. .
14b51 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 6f ** cntNew[k] sho
14b52 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c 2e uld equal nCell.
14b53 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 65 . **. ** Value
14b54 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 s computed by th
14b55 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a 20 is block:. **.
14b56 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b 3a ** k:
14b57 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 The total numbe
14b58 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 67 r of sibling pag
14b59 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 77 es. ** szNew
14b5a 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65 64 [i]: Spaced used
14b5b 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 62 on the i-th sib
14b5c 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 ling page.. **
14b5d 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e 64 cntNew[i]: Ind
14b5e 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 ex in apCell[] a
14b5f 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20 nd szCell[] for
14b60 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 74 the first cell t
14b61 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 o. **
14b62 20 20 20 20 74 68 65 20 72 69 67 68 74 20 6f 66 the right of
14b63 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e the i-th siblin
14b64 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 61 g page.. ** usa
14b65 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 72 bleSpace: Number
14b66 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 of bytes of spa
14b67 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 ce available on
14b68 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 20 each sibling..
14b69 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 6c ** . */. usabl
14b6a 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 73 eSpace = pBt->us
14b6b 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b 20 ableSize - 12 +
14b6c 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a leafCorrection;.
14b6d 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d 6b for(subtotal=k
14b6e 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 =i=0; i<nCell; i
14b6f 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ++){. assert(
14b70 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a i<nMaxCells );.
14b71 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d 20 subtotal +=
14b72 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a 20 szCell[i] + 2;.
14b73 20 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c 20 if( subtotal
14b74 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20 29 7b > usableSpace ){
14b75 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d 20 . szNew[k]
14b76 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a 43 = subtotal - szC
14b77 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 6e ell[i];. cn
14b78 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 20 tNew[k] = i;.
14b79 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 61 20 if( leafData
14b7a 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 ){ i--; }.
14b7b 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 subtotal = 0;.
14b7c 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 20 k++;. }.
14b7d 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 }. szNew[k] =
14b7e 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e 74 4e subtotal;. cntN
14b7f 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b 0a 20 ew[k] = nCell;.
14b80 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a k++;.. /*. **
14b81 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63 6f 6d The packing com
14b82 70 75 74 65 64 20 62 79 20 74 68 65 20 70 72 65 puted by the pre
14b83 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73 20 62 vious block is b
14b84 69 61 73 65 64 20 74 6f 77 61 72 64 20 74 68 65 iased toward the
14b85 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a 20 6f siblings. ** o
14b86 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 2e n the left side.
14b87 20 20 54 68 65 20 6c 65 66 74 20 73 69 62 6c 69 The left sibli
14b88 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e ngs are always n
14b89 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68 69 6c early full, whil
14b8a 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67 68 74 e the. ** right
14b8b 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 6d 69 -most sibling mi
14b8c 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20 65 6d ght be nearly em
14b8d 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f 63 6b pty. This block
14b8e 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d 70 74 of code attempt
14b8f 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75 73 74 s. ** to adjust
14b90 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f 66 20 the packing of
14b91 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65 74 20 siblings to get
14b92 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e 63 65 a better balance
14b93 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 .. **. ** This
14b94 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d adjustment is m
14b95 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70 74 69 ore than an opti
14b96 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 mization. The p
14b97 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67 acking above mig
14b98 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75 ht. ** be so ou
14b99 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 73 20 t of balance as
14b9a 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20 to be illegal.
14b9b 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 For example, the
14b9c 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a right-most. **
14b9d 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 sibling might b
14b9e 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 e completely emp
14b9f 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75 73 74 ty. This adjust
14ba0 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 74 69 ment is not opti
14ba1 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 onal.. */. for
14ba2 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d (i=k-1; i>0; i--
14ba3 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52 69 67 ){. int szRig
14ba4 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b 20 20 ht = szNew[i];
14ba5 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 /* Size of sibli
14ba6 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 ng on the right
14ba7 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c 65 66 */. int szLef
14ba8 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d 3b 20 t = szNew[i-1];
14ba9 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 /* Size of sibli
14baa 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a ng on the left *
14bab 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20 20 20 /. int r;
14bac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
14bad 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 ex of right-most
14bae 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20 73 69 cell in left si
14baf 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 bling */. int
14bb0 20 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
14bb1 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72 /* Index of fir
14bb2 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65 20 6c st cell to the l
14bb3 65 66 74 20 6f 66 20 72 69 67 68 74 20 73 69 62 eft of right sib
14bb4 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72 20 3d ling */.. r =
14bb5 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 cntNew[i-1] - 1
14bb6 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 ;. d = r + 1
14bb7 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 - leafData;.
14bb8 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78 43 65 assert( d<nMaxCe
14bb9 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72 lls );. asser
14bba 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 t( r<nMaxCells )
14bbb 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 7a 52 ;. while( szR
14bbc 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52 69 67 ight==0 || szRig
14bbd 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d ht+szCell[d]+2<=
14bbe 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 szLeft-(szCell[r
14bbf 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 73 7a ]+2) ){. sz
14bc0 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b Right += szCell[
14bc1 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a d] + 2;. sz
14bc2 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72 Left -= szCell[r
14bc3 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 6e 74 ] + 2;. cnt
14bc4 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20 New[i-1]--;.
14bc5 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 r = cntNew[i-1
14bc6 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 20 3d ] - 1;. d =
14bc7 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 r + 1 - leafDat
14bc8 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e a;. }. szN
14bc9 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b ew[i] = szRight;
14bca 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20 . szNew[i-1]
14bcb 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20 = szLeft;. }..
14bcc 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20 66 6f /* Either we fo
14bcd 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 und one or more
14bce 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d cells (cntnew[0]
14bcf 29 3e 30 29 20 6f 72 20 77 65 20 61 72 65 20 74 )>0) or we are t
14bd0 68 65 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61 he. ** a virtua
14bd1 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20 l root page. A
14bd2 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 virtual root pag
14bd3 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72 65 e is when the re
14bd4 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67 al root. ** pag
14bd5 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64 20 e is page 1 and
14bd6 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 we are the only
14bd7 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61 child of that pa
14bd8 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ge.. */. asser
14bd9 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c t( cntNew[0]>0 |
14bda 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f | (pParent->pgno
14bdb 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e ==1 && pParent->
14bdc 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20 nCell==0) );..
14bdd 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 /*. ** Allocate
14bde 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20 20 52 k new pages. R
14bdf 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73 20 77 euse old pages w
14be0 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20 here possible..
14be1 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
14be2 61 67 65 2d 3e 70 67 6e 6f 3e 31 20 29 3b 0a 20 age->pgno>1 );.
14be3 20 70 61 67 65 46 6c 61 67 73 20 3d 20 70 50 61 pageFlags = pPa
14be4 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a 20 20 ge->aData[0];.
14be5 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b for(i=0; i<k; i+
14be6 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 +){. MemPage
14be7 2a 70 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 69 *pNew;. if( i
14be8 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 <nOld ){. p
14be9 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 20 3d New = apNew[i] =
14bea 20 61 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 apOld[i];.
14beb 20 70 67 6e 6f 4e 65 77 5b 69 5d 20 3d 20 70 67 pgnoNew[i] = pg
14bec 6e 6f 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 noOld[i];.
14bed 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 apOld[i] = 0;.
14bee 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
14bef 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 2d PagerWrite(pNew-
14bf0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
14bf1 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69 nNew++;. i
14bf2 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c f( rc ) goto bal
14bf3 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
14bf4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
14bf5 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 ssert( i>0 );.
14bf6 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 rc = allocat
14bf7 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
14bf8 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 5b &pNew, &pgnoNew[
14bf9 69 5d 2c 20 70 67 6e 6f 4e 65 77 5b 69 2d 31 5d i], pgnoNew[i-1]
14bfa 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
14bfb 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 rc ) goto balanc
14bfc 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 e_cleanup;.
14bfd 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65 77 apNew[i] = pNew
14bfe 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a ;. nNew++;.
14bff 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
14c00 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61 67 Free any old pag
14c01 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f 74 es that were not
14c02 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 20 70 reused as new p
14c03 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 ages.. */. whi
14c04 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 le( i<nOld ){.
14c05 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 rc = freePage(
14c06 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 69 apOld[i]);. i
14c07 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c f( rc ) goto bal
14c08 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
14c09 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 releasePage(ap
14c0a 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70 4f Old[i]);. apO
14c0b 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 69 ld[i] = 0;. i
14c0c 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 ++;. }.. /*.
14c0d 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20 70 ** Put the new p
14c0e 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e ages in accendin
14c0f 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20 68 g order. This h
14c10 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 elps to. ** kee
14c11 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 p entries in the
14c12 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f 72 disk file in or
14c13 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 73 63 der so that a sc
14c14 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 an. ** of the t
14c15 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 72 able is a linear
14c16 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 scan through th
14c17 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20 20 e file. That.
14c18 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 73 ** in turn helps
14c19 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
14c1a 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 72 ystem to deliver
14c1b 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f 6d pages. ** from
14c1c 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20 72 the disk more r
14c1d 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a apidly.. **. *
14c1e 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73 65 * An O(n^2) inse
14c1f 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f 72 rtion sort algor
14c20 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62 75 ithm is used, bu
14c21 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20 69 t since. ** n i
14c22 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 61 s never more tha
14c23 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63 6f n NB (a small co
14c24 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73 68 nstant), that sh
14c25 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 ould. ** not be
14c26 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a 2a a problem.. **
14c27 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d 33 . ** When NB==3
14c28 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69 6d , this one optim
14c29 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74 68 ization makes th
14c2a 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 e database. **
14c2b 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 65 72 about 25% faster
14c2c 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65 72 for large inser
14c2d 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74 69 tions and deleti
14c2e 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 ons.. */. for(
14c2f 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b 29 i=0; i<k-1; i++)
14c30 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20 3d {. int minV =
14c31 20 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 pgnoNew[i];.
14c32 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b 0a 20 int minI = i;.
14c33 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a 3c for(j=i+1; j<
14c34 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 k; j++){. i
14c35 66 28 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3c 28 75 f( pgnoNew[j]<(u
14c36 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b 0a nsigned)minV ){.
14c37 20 20 20 20 20 20 20 20 6d 69 6e 49 20 3d 20 6a minI = j
14c38 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e 56 20 3d ;. minV =
14c39 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3b 0a 20 20 20 pgnoNew[j];.
14c3a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
14c3b 66 28 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 20 f( minI>i ){.
14c3c 20 20 20 69 6e 74 20 74 3b 0a 20 20 20 20 20 20 int t;.
14c3d 4d 65 6d 50 61 67 65 20 2a 70 54 3b 0a 20 20 20 MemPage *pT;.
14c3e 20 20 20 74 20 3d 20 70 67 6e 6f 4e 65 77 5b 69 t = pgnoNew[i
14c3f 5d 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61 70 ];. pT = ap
14c40 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 70 67 New[i];. pg
14c41 6e 6f 4e 65 77 5b 69 5d 20 3d 20 70 67 6e 6f 4e noNew[i] = pgnoN
14c42 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 ew[minI];.
14c43 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 apNew[i] = apNew
14c44 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 70 67 [minI];. pg
14c45 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 74 3b noNew[minI] = t;
14c46 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 6d 69 6e . apNew[min
14c47 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20 7d 0a 20 I] = pT;. }.
14c48 20 7d 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c }. TRACE(("BAL
14c49 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25 64 ANCE: old: %d %d
14c4a 20 25 64 20 20 6e 65 77 3a 20 25 64 28 25 64 29 %d new: %d(%d)
14c4b 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 %d(%d) %d(%d) %
14c4c 64 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c d(%d) %d(%d)\n",
14c4d 0a 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 30 5d 2c . pgnoOld[0],
14c4e 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f 20 . nOld>=2 ?
14c4f 70 67 6e 6f 4f 6c 64 5b 31 5d 20 3a 20 30 2c 0a pgnoOld[1] : 0,.
14c50 20 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 70 67 nOld>=3 ? pg
14c51 6e 6f 4f 6c 64 5b 32 5d 20 3a 20 30 2c 0a 20 20 noOld[2] : 0,.
14c52 20 20 70 67 6e 6f 4e 65 77 5b 30 5d 2c 20 73 7a pgnoNew[0], sz
14c53 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e 4e 65 77 New[0],. nNew
14c54 3e 3d 32 20 3f 20 70 67 6e 6f 4e 65 77 5b 31 5d >=2 ? pgnoNew[1]
14c55 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f 20 : 0, nNew>=2 ?
14c56 73 7a 4e 65 77 5b 31 5d 20 3a 20 30 2c 0a 20 20 szNew[1] : 0,.
14c57 20 20 6e 4e 65 77 3e 3d 33 20 3f 20 70 67 6e 6f nNew>=3 ? pgno
14c58 4e 65 77 5b 32 5d 20 3a 20 30 2c 20 6e 4e 65 77 New[2] : 0, nNew
14c59 3e 3d 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a >=3 ? szNew[2] :
14c5a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 0,. nNew>=4
14c5b 3f 20 70 67 6e 6f 4e 65 77 5b 33 5d 20 3a 20 30 ? pgnoNew[3] : 0
14c5c 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 65 , nNew>=4 ? szNe
14c5d 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e w[3] : 0,. nN
14c5e 65 77 3e 3d 35 20 3f 20 70 67 6e 6f 4e 65 77 5b ew>=5 ? pgnoNew[
14c5f 34 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 35 20 4] : 0, nNew>=5
14c60 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 30 29 29 ? szNew[4] : 0))
14c61 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 45 76 65 ;.. /*. ** Eve
14c62 6e 6c 79 20 64 69 73 74 72 69 62 75 74 65 20 74 nly distribute t
14c63 68 65 20 64 61 74 61 20 69 6e 20 61 70 43 65 6c he data in apCel
14c64 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68 65 20 6e l[] across the n
14c65 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20 49 ew pages.. ** I
14c66 6e 73 65 72 74 20 64 69 76 69 64 65 72 20 63 65 nsert divider ce
14c67 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 lls into pParent
14c68 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 as necessary..
14c69 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a 20 20 66 */. j = 0;. f
14c6a 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 or(i=0; i<nNew;
14c6b 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 41 73 73 i++){. /* Ass
14c6c 65 6d 62 6c 65 20 74 68 65 20 6e 65 77 20 73 69 emble the new si
14c6d 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 bling page. */.
14c6e 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 MemPage *pNew
14c6f 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 = apNew[i];.
14c70 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 assert( j<nMaxC
14c71 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 65 ells );. asse
14c72 72 74 28 20 70 4e 65 77 2d 3e 70 67 6e 6f 3d 3d rt( pNew->pgno==
14c73 70 67 6e 6f 4e 65 77 5b 69 5d 20 29 3b 0a 20 20 pgnoNew[i] );.
14c74 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c zeroPage(pNew,
14c75 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20 pageFlags);.
14c76 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e assemblePage(pN
14c77 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c ew, cntNew[i]-j,
14c78 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a &apCell[j], &sz
14c79 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73 Cell[j]);. as
14c7a 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c sert( pNew->nCel
14c7b 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20 l>0 || (nNew==1
14c7c 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29 && cntNew[0]==0)
14c7d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
14c7e 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d pNew->nOverflow=
14c7f 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 =0 );.. /* If
14c80 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f this is an auto
14c81 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 -vacuum database
14c82 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 , update the poi
14c83 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 nter map entries
14c84 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 . ** that poi
14c85 6e 74 20 74 6f 20 74 68 65 20 73 69 62 6c 69 6e nt to the siblin
14c86 67 73 20 74 68 61 74 20 77 65 72 65 20 72 65 61 gs that were rea
14c87 72 72 61 6e 67 65 64 2e 20 54 68 65 73 65 20 63 rranged. These c
14c88 61 6e 20 62 65 3a 20 6c 65 66 74 0a 20 20 20 20 an be: left.
14c89 2a 2a 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 63 ** children of c
14c8a 65 6c 6c 73 2c 20 74 68 65 20 72 69 67 68 74 2d ells, the right-
14c8b 63 68 69 6c 64 20 6f 66 20 74 68 65 20 70 61 67 child of the pag
14c8c 65 2c 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 e, or overflow p
14c8d 61 67 65 73 0a 20 20 20 20 2a 2a 20 70 6f 69 6e ages. ** poin
14c8e 74 65 64 20 74 6f 20 62 79 20 63 65 6c 6c 73 2e ted to by cells.
14c8f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
14c90 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a ISAUTOVACUUM ){.
14c91 20 20 20 20 20 20 66 6f 72 28 6b 3d 6a 3b 20 6b for(k=j; k
14c92 3c 63 6e 74 4e 65 77 5b 69 5d 3b 20 6b 2b 2b 29 <cntNew[i]; k++)
14c93 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
14c94 28 20 6b 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( k<nMaxCells );
14c95 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 46 72 . if( aFr
14c96 6f 6d 5b 6b 5d 3d 3d 30 78 46 46 20 7c 7c 20 61 om[k]==0xFF || a
14c97 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6b 5d 5d 2d pCopy[aFrom[k]]-
14c98 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e >pgno!=pNew->pgn
14c99 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 o ){. r
14c9a 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 c = ptrmapPutOvf
14c9b 6c 28 70 4e 65 77 2c 20 6b 2d 6a 29 3b 0a 20 20 l(pNew, k-j);.
14c9c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
14c9d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 65 61 SQLITE_OK && lea
14c9e 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29 fCorrection==0 )
14c9f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
14ca0 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 = ptrmapPut(pBt
14ca1 2c 20 67 65 74 34 62 79 74 65 28 61 70 43 65 6c , get4byte(apCel
14ca2 6c 5b 6b 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 l[k]), PTRMAP_BT
14ca3 52 45 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 29 REE, pNew->pgno)
14ca4 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
14ca5 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
14ca6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
14ca7 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 goto ba
14ca8 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
14ca9 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
14caa 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
14cab 20 7d 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74 4e }.. j = cntN
14cac 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 49 ew[i];.. /* I
14cad 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 f the sibling pa
14cae 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f ge assembled abo
14caf 76 65 20 77 61 73 20 6e 6f 74 20 74 68 65 20 72 ve was not the r
14cb0 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e ight-most siblin
14cb1 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 g,. ** insert
14cb2 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 a divider cell
14cb3 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 into the parent
14cb4 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 page.. */.
14cb5 20 69 66 28 20 69 3c 6e 4e 65 77 2d 31 20 26 26 if( i<nNew-1 &&
14cb6 20 6a 3c 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 j<nCell ){.
14cb7 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 u8 *pCell;.
14cb8 20 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 u8 *pTemp;.
14cb9 20 20 20 20 69 6e 74 20 73 7a 3b 0a 0a 20 20 20 int sz;..
14cba 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 assert( j<nMa
14cbb 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 xCells );.
14cbc 70 43 65 6c 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a pCell = apCell[j
14cbd 5d 3b 0a 20 20 20 20 20 20 73 7a 20 3d 20 73 7a ];. sz = sz
14cbe 43 65 6c 6c 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f Cell[j] + leafCo
14cbf 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 rrection;.
14cc0 70 54 65 6d 70 20 3d 20 26 61 53 70 61 63 65 32 pTemp = &aSpace2
14cc1 5b 69 53 70 61 63 65 32 5d 3b 0a 20 20 20 20 20 [iSpace2];.
14cc2 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 66 if( !pNew->leaf
14cc3 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 ){. memc
14cc4 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 5b py(&pNew->aData[
14cc5 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 8], pCell, 4);.
14cc6 20 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54 if( ISAUT
14cc7 4f 56 41 43 55 55 4d 20 0a 20 20 20 20 20 20 20 OVACUUM .
14cc8 20 20 26 26 20 28 61 46 72 6f 6d 5b 6a 5d 3d 3d && (aFrom[j]==
14cc9 30 78 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 0xFF || apCopy[a
14cca 46 72 6f 6d 5b 6a 5d 5d 2d 3e 70 67 6e 6f 21 3d From[j]]->pgno!=
14ccb 70 4e 65 77 2d 3e 70 67 6e 6f 29 0a 20 20 20 20 pNew->pgno).
14ccc 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ){.
14ccd 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 rc = ptrmapPut(
14cce 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 pBt, get4byte(pC
14ccf 65 6c 6c 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 ell), PTRMAP_BTR
14cd0 45 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 29 3b EE, pNew->pgno);
14cd1 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
14cd2 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
14cd3 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
14cd4 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 balance_cleanup
14cd5 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
14cd6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
14cd7 6c 73 65 20 69 66 28 20 6c 65 61 66 44 61 74 61 lse if( leafData
14cd8 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 ){. /* I
14cd9 66 20 74 68 65 20 74 72 65 65 20 69 73 20 61 20 f the tree is a
14cda 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 2c 20 leaf-data tree,
14cdb 61 6e 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73 and the siblings
14cdc 20 61 72 65 20 6c 65 61 76 65 73 2c 20 0a 20 20 are leaves, .
14cdd 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 ** then th
14cde 65 72 65 20 69 73 20 6e 6f 20 64 69 76 69 64 65 ere is no divide
14cdf 72 20 63 65 6c 6c 20 69 6e 20 61 70 43 65 6c 6c r cell in apCell
14ce0 5b 5d 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 []. Instead, the
14ce1 20 64 69 76 69 64 65 72 20 0a 20 20 20 20 20 20 divider .
14ce2 20 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 73 69 73 ** cell consis
14ce3 74 73 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ts of the intege
14ce4 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72 69 r key for the ri
14ce5 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 ght-most cell of
14ce6 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 . ** the
14ce7 20 73 69 62 6c 69 6e 67 2d 70 61 67 65 20 61 73 sibling-page as
14ce8 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20 6f 6e sembled above on
14ce9 6c 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ly.. */.
14cea 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 CellInfo
14ceb 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 6a 2d info;. j-
14cec 2d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 -;. sqlit
14ced 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
14cee 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c Ptr(pNew, apCell
14cef 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 [j], &info);.
14cf0 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 pCell = pTe
14cf1 6d 70 3b 0a 20 20 20 20 20 20 20 20 66 69 6c 6c mp;. fill
14cf2 49 6e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 InCell(pParent,
14cf3 70 43 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e pCell, 0, info.n
14cf4 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26 73 Key, 0, 0, 0, &s
14cf5 7a 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d z);. pTem
14cf6 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c p = 0;. }el
14cf7 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65 6c se{. pCel
14cf8 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 l -= 4;.
14cf9 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 73 65 20 /* Obscure case
14cfa 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 for non-leaf-dat
14cfb 61 20 74 72 65 65 73 3a 20 49 66 20 74 68 65 20 a trees: If the
14cfc 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77 61 cell at pCell wa
14cfd 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 s. ** pre
14cfe 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 64 20 6f viously stored o
14cff 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 61 n a leaf node, a
14d00 6e 64 20 69 74 73 20 72 65 70 6f 72 74 65 64 20 nd its reported
14d01 73 69 7a 65 20 77 61 73 20 34 0a 20 20 20 20 20 size was 4.
14d02 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 65 ** bytes, the
14d03 6e 20 69 74 20 6d 61 79 20 61 63 74 75 61 6c 6c n it may actuall
14d04 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 y be smaller tha
14d05 6e 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 n this .
14d06 2a 2a 20 28 73 65 65 20 73 71 6c 69 74 65 33 42 ** (see sqlite3B
14d07 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
14d08 28 29 2c 20 34 20 62 79 74 65 73 20 69 73 20 74 (), 4 bytes is t
14d09 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 he minimum size
14d0a 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e of. ** an
14d0b 79 20 63 65 6c 6c 29 2e 20 42 75 74 20 69 74 20 y cell). But it
14d0c 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 is important to
14d0d 70 61 73 73 20 74 68 65 20 63 6f 72 72 65 63 74 pass the correct
14d0e 20 73 69 7a 65 20 74 6f 20 0a 20 20 20 20 20 20 size to .
14d0f 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 28 ** insertCell(
14d10 29 2c 20 73 6f 20 72 65 70 61 72 73 65 20 74 68 ), so reparse th
14d11 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 e cell now..
14d12 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
14d13 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 * Note that this
14d14 20 63 61 6e 20 6e 65 76 65 72 20 68 61 70 70 65 can never happe
14d15 6e 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 n in an SQLite d
14d16 61 74 61 20 66 69 6c 65 2c 20 61 73 20 61 6c 6c ata file, as all
14d17 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c . ** cell
14d18 73 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 34 s are at least 4
14d19 20 62 79 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20 bytes. It only
14d1a 68 61 70 70 65 6e 73 20 69 6e 20 62 2d 74 72 65 happens in b-tre
14d1b 65 73 20 75 73 65 64 0a 20 20 20 20 20 20 20 20 es used.
14d1c 2a 2a 20 74 6f 20 65 76 61 6c 75 61 74 65 20 22 ** to evaluate "
14d1d 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 IN (SELECT ...)"
14d1e 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 63 6c 61 and similar cla
14d1f 75 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f uses.. */
14d20 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 . if( szC
14d21 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 ell[j]==4 ){.
14d22 20 20 20 20 20 20 20 61 73 73 65 72 74 28 6c 65 assert(le
14d23 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 29 afCorrection==4)
14d24 3b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 20 3d ;. sz =
14d25 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
14d26 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 rent, pCell);.
14d27 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
14d28 20 20 20 20 20 20 69 53 70 61 63 65 32 20 2b 3d iSpace2 +=
14d29 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 sz;. asser
14d2a 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 t( sz<=pBt->page
14d2b 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 Size/4 );.
14d2c 61 73 73 65 72 74 28 20 69 53 70 61 63 65 32 3c assert( iSpace2<
14d2d 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 =pBt->pageSize )
14d2e 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e 73 ;. rc = ins
14d2f 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c ertCell(pParent,
14d30 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73 nxDiv, pCell, s
14d31 7a 2c 20 70 54 65 6d 70 2c 20 34 29 3b 0a 20 20 z, pTemp, 4);.
14d32 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
14d33 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61 6c TE_OK ) goto bal
14d34 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
14d35 20 20 20 20 70 75 74 34 62 79 74 65 28 66 69 6e put4byte(fin
14d36 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 dOverflowCell(pP
14d37 61 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e arent,nxDiv), pN
14d38 65 77 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 ew->pgno);..
14d39 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
14d3a 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 an auto-vacuum d
14d3b 61 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74 atabase, and not
14d3c 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65 a leaf-data tre
14d3d 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e e,. ** then
14d3e 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e update the poin
14d3f 74 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20 ter map with an
14d40 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 entry for the ov
14d41 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 erflow page.
14d42 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65 ** that the ce
14d43 6c 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 ll just inserted
14d44 20 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61 points to (if a
14d45 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ny).. */.
14d46 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
14d47 43 55 55 4d 20 26 26 20 21 6c 65 61 66 44 61 74 CUUM && !leafDat
14d48 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 a ){. rc
14d49 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 = ptrmapPutOvfl(
14d4a 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b pParent, nxDiv);
14d4b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
14d4c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14d4d 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c goto bal
14d4e 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
14d4f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
14d50 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 j++;.
14d51 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a nxDiv++;. }.
14d52 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 . /* Set the
14d53 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
14d54 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 y for the new si
14d55 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 bling page. */.
14d56 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
14d57 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 UUM ){. rc
14d58 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c = ptrmapPut(pBt,
14d59 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 pNew->pgno, PTR
14d5a 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 MAP_BTREE, pPare
14d5b 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 nt->pgno);.
14d5c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
14d5d 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
14d5e 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
14d5f 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 up;. }.
14d60 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 }. }. assert(
14d61 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 j==nCell );. as
14d62 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a sert( nOld>0 );.
14d63 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 assert( nNew>0
14d64 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 );. if( (pageF
14d65 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 lags & PTF_LEAF)
14d66 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a ==0 ){. u8 *z
14d67 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b Child = &apCopy[
14d68 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 nOld-1]->aData[8
14d69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 ];. memcpy(&a
14d6a 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 pNew[nNew-1]->aD
14d6b 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 ata[8], zChild,
14d6c 34 29 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55 4);. if( ISAU
14d6d 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
14d6e 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 rc = ptrmapPut
14d6f 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 7a (pBt, get4byte(z
14d70 43 68 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f 42 Child), PTRMAP_B
14d71 54 52 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 TREE, apNew[nNew
14d72 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 -1]->pgno);.
14d73 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
14d74 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
14d75 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 oto balance_clea
14d76 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nup;. }.
14d77 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 78 44 }. }. if( nxD
14d78 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 iv==pParent->nCe
14d79 6c 6c 2b 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 ll+pParent->nOve
14d7a 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 2f 2a 20 rflow ){. /*
14d7b 52 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 Right-most sibli
14d7c 6e 67 20 69 73 20 74 68 65 20 72 69 67 68 74 2d ng is the right-
14d7d 6d 6f 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50 most child of pP
14d7e 61 72 65 6e 74 20 2a 2f 0a 20 20 20 20 70 75 74 arent */. put
14d7f 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 4byte(&pParent->
14d80 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 aData[pParent->h
14d81 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e drOffset+8], pgn
14d82 6f 4e 65 77 5b 6e 4e 65 77 2d 31 5d 29 3b 0a 20 oNew[nNew-1]);.
14d83 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 52 }else{. /* R
14d84 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e ight-most siblin
14d85 67 20 69 73 20 74 68 65 20 6c 65 66 74 20 63 68 g is the left ch
14d86 69 6c 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 ild of the first
14d87 20 65 6e 74 72 79 20 69 6e 20 70 50 61 72 65 6e entry in pParen
14d88 74 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 t. ** past th
14d89 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 64 69 76 e right-most div
14d8a 69 64 65 72 20 65 6e 74 72 79 20 2a 2f 0a 20 20 ider entry */.
14d8b 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f put4byte(findO
14d8c 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72 verflowCell(pPar
14d8d 65 6e 74 2c 20 6e 78 44 69 76 29 2c 20 70 67 6e ent, nxDiv), pgn
14d8e 6f 4e 65 77 5b 6e 4e 65 77 2d 31 5d 29 3b 0a 20 oNew[nNew-1]);.
14d8f 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 42 61 }.. /*. ** Ba
14d90 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e 74 lance the parent
14d91 20 70 61 67 65 2e 20 20 4e 6f 74 65 20 74 68 61 page. Note tha
14d92 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 t the current pa
14d93 67 65 20 28 70 50 61 67 65 29 20 6d 69 67 68 74 ge (pPage) might
14d94 0a 20 20 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 . ** have been
14d95 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 added to the fre
14d96 65 6c 69 73 74 20 73 6f 20 69 74 20 6d 69 67 68 elist so it migh
14d97 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 69 t no longer be i
14d98 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a nitialized.. **
14d99 20 42 75 74 20 74 68 65 20 70 61 72 65 6e 74 20 But the parent
14d9a 70 61 67 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 page will always
14d9b 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e be initialized.
14d9c 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
14d9d 70 50 61 72 65 6e 74 2d 3e 69 73 49 6e 69 74 20 pParent->isInit
14d9e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 );. sqlite3Scra
14d9f 74 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b tchFree(apCell);
14da0 0a 20 20 61 70 43 65 6c 6c 20 3d 20 30 3b 0a 20 . apCell = 0;.
14da1 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
14da2 67 65 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 ge);. pCur->iPa
14da3 67 65 2d 2d 3b 0a 20 20 72 63 20 3d 20 62 61 6c ge--;. rc = bal
14da4 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 ance(pCur, 0);.
14da5 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 . /*. ** Clea
14da6 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74 75 72 nup before retur
14da7 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e ning.. */.balan
14da8 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 ce_cleanup:. sq
14da9 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 61 53 lite3PageFree(aS
14daa 70 61 63 65 32 29 3b 0a 20 20 73 71 6c 69 74 65 pace2);. sqlite
14dab 33 53 63 72 61 74 63 68 46 72 65 65 28 61 70 43 3ScratchFree(apC
14dac 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b ell);. for(i=0;
14dad 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 i<nOld; i++){.
14dae 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 releasePage(a
14daf 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 pOld[i]);. }.
14db0 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b for(i=0; i<nNew;
14db1 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 i++){. relea
14db2 73 65 50 61 67 65 28 61 70 4e 65 77 5b 69 5d 29 sePage(apNew[i])
14db3 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 72 65 6c 65 ;. }.. /* rele
14db4 61 73 65 50 61 67 65 28 70 50 61 72 65 6e 74 29 asePage(pParent)
14db5 3b 20 2a 2f 0a 20 20 54 52 41 43 45 28 28 22 42 ; */. TRACE(("B
14db6 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 64 ALANCE: finished
14db7 20 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d 25 64 with %d: old=%d
14db8 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 new=%d cells=%d
14db9 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 \n",. p
14dba 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c 64 Page->pgno, nOld
14dbb 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b , nNew, nCell));
14dbc 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
14dbd 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
14dbe 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 tine is called f
14dbf 6f 72 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 or the root page
14dc0 20 6f 66 20 61 20 62 74 72 65 65 20 77 68 65 6e of a btree when
14dc1 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 the root.** pag
14dc2 65 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 e contains no ce
14dc3 6c 6c 73 2e 20 20 54 68 69 73 20 69 73 20 61 6e lls. This is an
14dc4 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 opportunity to
14dc5 6d 61 6b 65 20 74 68 65 20 74 72 65 65 0a 2a 2a make the tree.**
14dc6 20 73 68 61 6c 6c 6f 77 65 72 20 62 79 20 6f 6e shallower by on
14dc7 65 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 e level..*/.stat
14dc8 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 73 ic int balance_s
14dc9 68 61 6c 6c 6f 77 65 72 28 42 74 43 75 72 73 6f hallower(BtCurso
14dca 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 r *pCur){. MemP
14dcb 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 age *pPage;
14dcc 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
14dcd 20 70 61 67 65 20 6f 66 20 42 2d 54 72 65 65 20 page of B-Tree
14dce 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 */. MemPage *pC
14dcf 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 hild;
14dd0 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 /* The only ch
14dd1 69 6c 64 20 70 61 67 65 20 6f 66 20 70 50 61 67 ild page of pPag
14dd2 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
14dd3 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20 20 Child;
14dd4 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
14dd5 65 72 20 66 6f 72 20 70 43 68 69 6c 64 20 2a 2f er for pChild */
14dd6 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
14dd7 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
14dd8 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 /* Return code f
14dd9 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65 rom subprocedure
14dda 73 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 s */. BtShared
14ddb 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 *pBt;
14ddc 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 /* The ma
14ddd 69 6e 20 42 54 72 65 65 20 73 74 72 75 63 74 75 in BTree structu
14dde 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 65 re */. int mxCe
14ddf 6c 6c 50 65 72 50 61 67 65 3b 20 20 20 20 20 20 llPerPage;
14de0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
14de1 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 number of cells
14de2 70 65 72 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 per page */. u8
14de3 20 2a 2a 61 70 43 65 6c 6c 3b 20 20 20 20 20 20 **apCell;
14de4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c /* Al
14de5 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 61 67 l cells from pag
14de6 65 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 es being balance
14de7 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 d */. u16 *szCe
14de8 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
14de9 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a /* Local siz
14dea 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 2a e of all cells *
14deb 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 /.. assert( pCu
14dec 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 3b 0a 20 r->iPage==0 );.
14ded 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
14dee 70 50 61 67 65 5b 30 5d 3b 0a 0a 20 20 61 73 73 pPage[0];.. ass
14def 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c ert( pPage->nCel
14df0 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 l==0 );. assert
14df1 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
14df2 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
14df3 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 42 74 >mutex) );. pBt
14df4 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 = pPage->pBt;.
14df5 20 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 20 3d mxCellPerPage =
14df6 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 3b 0a 20 MX_CELL(pBt);.
14df7 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 apCell = sqlite
14df8 33 4d 61 6c 6c 6f 63 28 20 6d 78 43 65 6c 6c 50 3Malloc( mxCellP
14df9 65 72 50 61 67 65 2a 28 73 69 7a 65 6f 66 28 75 erPage*(sizeof(u
14dfa 38 2a 29 2b 73 69 7a 65 6f 66 28 75 31 36 29 29 8*)+sizeof(u16))
14dfb 20 29 3b 0a 20 20 69 66 28 20 61 70 43 65 6c 6c );. if( apCell
14dfc 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==0 ) return SQL
14dfd 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 7a 43 ITE_NOMEM;. szC
14dfe 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43 ell = (u16*)&apC
14dff 65 6c 6c 5b 6d 78 43 65 6c 6c 50 65 72 50 61 67 ell[mxCellPerPag
14e00 65 5d 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d e];. if( pPage-
14e01 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 2f 2a 20 >leaf ){. /*
14e02 54 68 65 20 74 61 62 6c 65 20 69 73 20 63 6f 6d The table is com
14e03 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 20 2a 2f pletely empty */
14e04 0a 20 20 20 20 54 52 41 43 45 28 28 22 42 41 4c . TRACE(("BAL
14e05 41 4e 43 45 3a 20 65 6d 70 74 79 20 74 61 62 6c ANCE: empty tabl
14e06 65 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e e %d\n", pPage->
14e07 70 67 6e 6f 29 29 3b 0a 20 20 7d 65 6c 73 65 7b pgno));. }else{
14e08 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 . /* The root
14e09 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 20 62 page is empty b
14e0a 75 74 20 68 61 73 20 6f 6e 65 20 63 68 69 6c 64 ut has one child
14e0b 2e 20 20 54 72 61 6e 73 66 65 72 20 74 68 65 0a . Transfer the.
14e0c 20 20 20 20 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 ** informati
14e0d 6f 6e 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 on from that one
14e0e 20 63 68 69 6c 64 20 69 6e 74 6f 20 74 68 65 20 child into the
14e0f 72 6f 6f 74 20 70 61 67 65 20 69 66 20 69 74 20 root page if it
14e10 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 66 69 74 . ** will fit
14e11 2e 20 20 54 68 69 73 20 72 65 64 75 63 65 73 20 . This reduces
14e12 74 68 65 20 64 65 70 74 68 20 6f 66 20 74 68 65 the depth of the
14e13 20 74 72 65 65 20 62 79 20 6f 6e 65 2e 0a 20 20 tree by one..
14e14 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 **. ** If t
14e15 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 he root page is
14e16 70 61 67 65 20 31 2c 20 69 74 20 68 61 73 20 6c page 1, it has l
14e17 65 73 73 20 73 70 61 63 65 20 61 76 61 69 6c 61 ess space availa
14e18 62 6c 65 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 ble than. **
14e19 69 74 73 20 63 68 69 6c 64 20 28 64 75 65 20 74 its child (due t
14e1a 6f 20 74 68 65 20 31 30 30 20 62 79 74 65 20 68 o the 100 byte h
14e1b 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 75 72 eader that occur
14e1c 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 s at the beginni
14e1d 6e 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 ng. ** of the
14e1e 20 64 61 74 61 62 61 73 65 20 66 6c 65 29 2c 20 database fle),
14e1f 73 6f 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 so it might not
14e20 62 65 20 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 be able to hold
14e21 61 6c 6c 20 6f 66 20 74 68 65 20 0a 20 20 20 20 all of the .
14e22 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 ** information c
14e23 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e urrently contain
14e24 65 64 20 69 6e 20 74 68 65 20 63 68 69 6c 64 2e ed in the child.
14e25 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 If this is the
14e26 20 0a 20 20 20 20 2a 2a 20 63 61 73 65 2c 20 74 . ** case, t
14e27 68 65 6e 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 hen do not do th
14e28 65 20 74 72 61 6e 73 66 65 72 2e 20 20 4c 65 61 e transfer. Lea
14e29 76 65 20 70 61 67 65 20 31 20 65 6d 70 74 79 20 ve page 1 empty
14e2a 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f except. ** fo
14e2b 72 20 74 68 65 20 72 69 67 68 74 2d 70 6f 69 6e r the right-poin
14e2c 74 65 72 20 74 6f 20 74 68 65 20 63 68 69 6c 64 ter to the child
14e2d 20 70 61 67 65 2e 20 20 54 68 65 20 63 68 69 6c page. The chil
14e2e 64 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 20 d page becomes.
14e2f 20 20 20 2a 2a 20 74 68 65 20 76 69 72 74 75 61 ** the virtua
14e30 6c 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 72 l root of the tr
14e31 65 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 56 ee.. */. V
14e32 56 41 5f 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 VA_ONLY( pCur->p
14e33 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20 31 agesShuffled = 1
14e34 20 29 3b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c );. pgnoChil
14e35 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 d = get4byte(&pP
14e36 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
14e37 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
14e38 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e . assert( pgn
14e39 6f 43 68 69 6c 64 3e 30 20 29 3b 0a 20 20 20 20 oChild>0 );.
14e3a 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c assert( pgnoChil
14e3b 64 3c 3d 70 61 67 65 72 50 61 67 65 63 6f 75 6e d<=pagerPagecoun
14e3c 74 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 50 t(pPage->pBt->pP
14e3d 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72 63 20 ager) );. rc
14e3e 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
14e3f 74 50 61 67 65 28 70 50 61 67 65 2d 3e 70 42 74 tPage(pPage->pBt
14e40 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 26 70 43 , pgnoChild, &pC
14e41 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20 20 69 66 hild, 0);. if
14e42 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f ( rc ) goto end_
14e43 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b shallow_balance;
14e44 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
14e45 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 pgno==1 ){.
14e46 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
14e47 65 65 49 6e 69 74 50 61 67 65 28 70 43 68 69 6c eeInitPage(pChil
14e48 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 d);. if( rc
14e49 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73 68 61 6c ) goto end_shal
14e4a 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a 20 20 20 low_balance;.
14e4b 20 20 20 61 73 73 65 72 74 28 20 70 43 68 69 6c assert( pChil
14e4c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 d->nOverflow==0
14e4d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 );. if( pCh
14e4e 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d 31 30 30 20 ild->nFree>=100
14e4f 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
14e50 65 20 63 68 69 6c 64 20 69 6e 66 6f 72 6d 61 74 e child informat
14e51 69 6f 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 ion will fit on
14e52 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 73 the root page, s
14e53 6f 20 64 6f 20 74 68 65 0a 20 20 20 20 20 20 20 o do the.
14e54 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 ** copy */.
14e55 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
14e56 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 zeroPage(pPag
14e57 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 e, pChild->aData
14e58 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 6f [0]);. fo
14e59 72 28 69 3d 30 3b 20 69 3c 70 43 68 69 6c 64 2d r(i=0; i<pChild-
14e5a 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 >nCell; i++){.
14e5b 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 69 apCell[i
14e5c 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 43 68 ] = findCell(pCh
14e5d 69 6c 64 2c 69 29 3b 0a 20 20 20 20 20 20 20 20 ild,i);.
14e5e 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 3d 20 63 65 szCell[i] = ce
14e5f 6c 6c 53 69 7a 65 50 74 72 28 70 43 68 69 6c 64 llSizePtr(pChild
14e60 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20 , apCell[i]);.
14e61 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
14e62 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 50 61 assemblePage(pPa
14e63 67 65 2c 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c ge, pChild->nCel
14e64 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c l, apCell, szCel
14e65 6c 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 l);. /* C
14e66 6f 70 79 20 74 68 65 20 72 69 67 68 74 2d 70 6f opy the right-po
14e67 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 inter of the chi
14e68 6c 64 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 ld to the parent
14e69 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 74 . */. put
14e6a 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
14e6b 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
14e6c 66 73 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 fset+8], .
14e6d 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 get4byte(&
14e6e 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 pChild->aData[pC
14e6f 68 69 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b hild->hdrOffset+
14e70 38 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 66 72 8]));. fr
14e71 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a eePage(pChild);.
14e72 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 TRACE(("
14e73 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c 64 20 25 BALANCE: child %
14e74 64 20 74 72 61 6e 73 66 65 72 20 74 6f 20 70 61 d transfer to pa
14e75 67 65 20 31 5c 6e 22 2c 20 70 43 68 69 6c 64 2d ge 1\n", pChild-
14e76 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 7d >pgno));. }
14e77 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
14e78 20 54 68 65 20 63 68 69 6c 64 20 68 61 73 20 6d The child has m
14e79 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ore information
14e7a 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e that will fit on
14e7b 20 74 68 65 20 72 6f 6f 74 2e 0a 20 20 20 20 20 the root..
14e7c 20 20 20 2a 2a 20 54 68 65 20 74 72 65 65 20 69 ** The tree i
14e7d 73 20 61 6c 72 65 61 64 79 20 62 61 6c 61 6e 63 s already balanc
14e7e 65 64 2e 20 20 44 6f 20 6e 6f 74 68 69 6e 67 2e ed. Do nothing.
14e7f 20 2a 2f 0a 20 20 20 20 20 20 20 20 54 52 41 43 */. TRAC
14e80 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 E(("BALANCE: chi
14e81 6c 64 20 25 64 20 77 69 6c 6c 20 6e 6f 74 20 66 ld %d will not f
14e82 69 74 20 6f 6e 20 70 61 67 65 20 31 5c 6e 22 2c it on page 1\n",
14e83 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b pChild->pgno));
14e84 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
14e85 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 se{. memcpy
14e86 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 70 (pPage->aData, p
14e87 43 68 69 6c 64 2d 3e 61 44 61 74 61 2c 20 70 50 Child->aData, pP
14e88 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
14e89 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70 50 61 Size);. pPa
14e8a 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a ge->isInit = 0;.
14e8b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
14e8c 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 e3BtreeInitPage(
14e8d 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 61 73 pPage);. as
14e8e 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
14e8f 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 66 72 65 _OK );. fre
14e90 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 ePage(pChild);.
14e91 20 20 20 20 20 54 52 41 43 45 28 28 22 42 41 4c TRACE(("BAL
14e92 41 4e 43 45 3a 20 74 72 61 6e 73 66 65 72 20 63 ANCE: transfer c
14e93 68 69 6c 64 20 25 64 20 69 6e 74 6f 20 72 6f 6f hild %d into roo
14e94 74 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 t %d\n",.
14e95 20 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 pChild->p
14e96 67 6e 6f 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f gno, pPage->pgno
14e97 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 ));. }. as
14e98 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
14e99 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 20 erflow==0 );.
14e9a 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
14e9b 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 M ){. rc =
14e9c 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
14e9d 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 pPage);. }.
14e9e 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 releasePage(pC
14e9f 68 69 6c 64 29 3b 0a 20 20 7d 0a 65 6e 64 5f 73 hild);. }.end_s
14ea0 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3a 0a hallow_balance:.
14ea1 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 sqlite3_free(a
14ea2 70 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e pCell);. return
14ea3 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 rc;.}.../*.** T
14ea4 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 he root page is
14ea5 6f 76 65 72 66 75 6c 6c 0a 2a 2a 0a 2a 2a 20 57 overfull.**.** W
14ea6 68 65 6e 20 74 68 69 73 20 68 61 70 70 65 6e 73 hen this happens
14ea7 2c 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 , Create a new c
14ea8 68 69 6c 64 20 70 61 67 65 20 61 6e 64 20 63 6f hild page and co
14ea9 70 79 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e py the.** conten
14eaa 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 69 ts of the root i
14eab 6e 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 nto the child.
14eac 54 68 65 6e 20 6d 61 6b 65 20 74 68 65 20 72 6f Then make the ro
14ead 6f 74 0a 2a 2a 20 70 61 67 65 20 61 6e 20 65 6d ot.** page an em
14eae 70 74 79 20 70 61 67 65 20 77 69 74 68 20 72 69 pty page with ri
14eaf 67 68 74 43 68 69 6c 64 20 70 6f 69 6e 74 69 6e ghtChild pointin
14eb0 67 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20 g to the new.**
14eb1 63 68 69 6c 64 2e 20 20 20 46 69 6e 61 6c 6c 79 child. Finally
14eb2 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 69 , call balance_i
14eb3 6e 74 65 72 6e 61 6c 28 29 20 6f 6e 20 74 68 65 nternal() on the
14eb4 20 6e 65 77 20 63 68 69 6c 64 0a 2a 2a 20 74 6f new child.** to
14eb5 20 63 61 75 73 65 20 69 74 20 74 6f 20 73 70 6c cause it to spl
14eb6 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e it..*/.static in
14eb7 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 t balance_deeper
14eb8 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
14eb9 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
14eba 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
14ebb 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 75 62 n value from sub
14ebc 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20 20 procedures */.
14ebd 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 MemPage *pPage;
14ebe 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
14ebf 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 o the root page
14ec0 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 */. MemPage *pC
14ec1 68 69 6c 64 3b 20 20 20 20 2f 2a 20 50 6f 69 6e hild; /* Poin
14ec2 74 65 72 20 74 6f 20 61 20 6e 65 77 20 63 68 69 ter to a new chi
14ec3 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e ld page */. Pgn
14ec4 6f 20 70 67 6e 6f 43 68 69 6c 64 3b 20 20 20 20 o pgnoChild;
14ec5 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
14ec6 6f 66 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 of the new child
14ec7 20 70 61 67 65 20 2a 2f 0a 20 20 42 74 53 68 61 page */. BtSha
14ec8 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 red *pBt;
14ec9 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 2a /* The BTree *
14eca 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 /. int usableSi
14ecb 7a 65 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c ze; /* Total
14ecc 20 75 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 usable size of
14ecd 61 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a a page */. u8 *
14ece 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 data;
14ecf 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 /* Content of th
14ed0 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 2a 2f e parent page */
14ed1 0a 20 20 75 38 20 2a 63 64 61 74 61 3b 20 20 20 . u8 *cdata;
14ed2 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e /* Conten
14ed3 74 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 t of the child p
14ed4 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 age */. int hdr
14ed5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
14ed6 4f 66 66 73 65 74 20 74 6f 20 70 61 67 65 20 68 Offset to page h
14ed7 65 61 64 65 72 20 69 6e 20 70 61 72 65 6e 74 20 eader in parent
14ed8 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20 */. int cbrk;
14ed9 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
14eda 65 74 20 74 6f 20 63 6f 6e 74 65 6e 74 20 6f 66 et to content of
14edb 20 66 69 72 73 74 20 63 65 6c 6c 20 69 6e 20 70 first cell in p
14edc 61 72 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 arent */.. asse
14edd 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d rt( pCur->iPage=
14ede 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
14edf 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d pCur->apPage[0]-
14ee0 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a >nOverflow>0 );.
14ee1 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 43 75 . VVA_ONLY( pCu
14ee2 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 r->pagesShuffled
14ee3 20 3d 20 31 20 29 3b 0a 20 20 70 50 61 67 65 20 = 1 );. pPage
14ee4 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 = pCur->apPage[0
14ee5 5d 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 ];. pBt = pPage
14ee6 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
14ee7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14ee8 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
14ee9 29 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 );. rc = alloca
14eea 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
14eeb 20 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43 &pChild, &pgnoC
14eec 68 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e hild, pPage->pgn
14eed 6f 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 o, 0);. if( rc
14eee 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 ) return rc;. a
14eef 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
14ef0 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
14ef1 43 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 Child->pDbPage)
14ef2 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 );. usableSize
14ef3 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
14ef4 65 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 e;. data = pPag
14ef5 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 e->aData;. hdr
14ef6 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 = pPage->hdrOffs
14ef7 65 74 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 et;. cbrk = get
14ef8 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
14ef9 35 5d 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70 5]);. cdata = p
14efa 43 68 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20 Child->aData;.
14efb 6d 65 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64 memcpy(cdata, &d
14efc 61 74 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d ata[hdr], pPage-
14efd 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50 >cellOffset+2*pP
14efe 61 67 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b age->nCell-hdr);
14eff 0a 20 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61 . memcpy(&cdata
14f00 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 [cbrk], &data[cb
14f01 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d rk], usableSize-
14f02 63 62 72 6b 29 3b 0a 20 20 0a 20 20 72 63 20 3d cbrk);. . rc =
14f03 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 sqlite3BtreeIni
14f04 74 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 tPage(pChild);.
14f05 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
14f06 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 OK ){. int nC
14f07 6f 70 79 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 opy = pPage->nOv
14f08 65 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 50 erflow*sizeof(pP
14f09 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 3b 0a age->aOvfl[0]);.
14f0a 20 20 20 20 6d 65 6d 63 70 79 28 70 43 68 69 6c memcpy(pChil
14f0b 64 2d 3e 61 4f 76 66 6c 2c 20 70 50 61 67 65 2d d->aOvfl, pPage-
14f0c 3e 61 4f 76 66 6c 2c 20 6e 43 6f 70 79 29 3b 0a >aOvfl, nCopy);.
14f0d 20 20 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 pChild->nOve
14f0e 72 66 6c 6f 77 20 3d 20 70 50 61 67 65 2d 3e 6e rflow = pPage->n
14f0f 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 Overflow;. if
14f10 28 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 ( pChild->nOverf
14f11 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 43 68 low ){. pCh
14f12 69 6c 64 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a ild->nFree = 0;.
14f13 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
14f14 28 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d ( pChild->nCell=
14f15 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b =pPage->nCell );
14f16 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 . zeroPage(pP
14f17 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 age, pChild->aDa
14f18 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c 45 41 ta[0] & ~PTF_LEA
14f19 46 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65 F);. put4byte
14f1a 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
14f1b 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
14f1c 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a 8], pgnoChild);.
14f1d 20 20 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 TRACE(("BALA
14f1e 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25 NCE: copy root %
14f1f 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 50 d into %d\n", pP
14f20 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c age->pgno, pChil
14f21 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 69 d->pgno));. i
14f22 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
14f23 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 ){. rc = pt
14f24 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 43 68 rmapPut(pBt, pCh
14f25 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 ild->pgno, PTRMA
14f26 50 5f 42 54 52 45 45 2c 20 70 50 61 67 65 2d 3e P_BTREE, pPage->
14f27 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 pgno);. if(
14f28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
14f29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
14f2a 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 etChildPtrmaps(p
14f2b 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a Child);. }.
14f2c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
14f2d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
14f2e 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 {. pCur->iPag
14f2f 65 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 e++;. pCur->a
14f30 70 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 69 6c pPage[1] = pChil
14f31 64 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 d;. pCur->aiI
14f32 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 72 dx[0] = 0;. r
14f33 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 c = balance_nonr
14f34 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c oot(pCur);. }el
14f35 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 se{. releaseP
14f36 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 7d age(pChild);. }
14f37 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
14f38 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 ../*.** The page
14f39 20 74 68 61 74 20 70 43 75 72 20 63 75 72 72 65 that pCur curre
14f3a 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 68 ntly points to h
14f3b 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64 as just been mod
14f3c 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65 ified in.** some
14f3d 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74 way. This funct
14f3e 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20 ion figures out
14f3f 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61 if this modifica
14f40 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a 2a tion means the.*
14f41 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20 * tree needs to
14f42 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64 be balanced, and
14f43 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65 if so calls the
14f44 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c appropriate bal
14f45 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 69 ancing .** routi
14f46 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 61 6d ne..** .** Param
14f47 65 74 65 72 20 69 73 49 6e 73 65 72 74 20 69 73 eter isInsert is
14f48 20 74 72 75 65 20 69 66 20 61 20 6e 65 77 20 63 true if a new c
14f49 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73 ell was just ins
14f4a 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a erted into the.*
14f4b 2a 20 70 61 67 65 2c 20 6f 72 20 66 61 6c 73 65 * page, or false
14f4c 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 otherwise..*/.s
14f4d 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 tatic int balanc
14f4e 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 e(BtCursor *pCur
14f4f 2c 20 69 6e 74 20 69 73 49 6e 73 65 72 74 29 7b , int isInsert){
14f50 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
14f51 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 TE_OK;. MemPage
14f52 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e *pPage = pCur->
14f53 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
14f54 67 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ge];.. assert(
14f55 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14f56 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
14f57 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
14f58 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b Cur->iPage==0 ){
14f59 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
14f5a 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
14f5b 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
14f5c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
14f5d 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 OK && pPage->nOv
14f5e 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20 20 erflow>0 ){.
14f5f 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64 rc = balance_d
14f60 65 65 70 65 72 28 70 43 75 72 29 3b 0a 20 20 20 eeper(pCur);.
14f61 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
14f62 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 QLITE_OK && pPag
14f63 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 e->nCell==0 ){.
14f64 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 rc = balanc
14f65 65 5f 73 68 61 6c 6c 6f 77 65 72 28 70 43 75 72 e_shallower(pCur
14f66 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
14f67 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d {. if( pPage-
14f68 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 >nOverflow>0 ||
14f69 0a 20 20 20 20 20 20 20 20 28 21 69 73 49 6e 73 . (!isIns
14f6a 65 72 74 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 ert && pPage->nF
14f6b 72 65 65 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e ree>pPage->pBt->
14f6c 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 29 20 usableSize*2/3)
14f6d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 61 ){. rc = ba
14f6e 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 lance_nonroot(pC
14f6f 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ur);. }. }.
14f70 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
14f71 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
14f72 65 20 63 68 65 63 6b 73 20 61 6c 6c 20 63 75 72 e checks all cur
14f73 73 6f 72 73 20 74 68 61 74 20 70 6f 69 6e 74 20 sors that point
14f74 74 6f 20 74 61 62 6c 65 20 70 67 6e 6f 52 6f 6f to table pgnoRoo
14f75 74 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 t..** If any of
14f76 74 68 6f 73 65 20 63 75 72 73 6f 72 73 20 77 65 those cursors we
14f77 72 65 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77 re opened with w
14f78 72 46 6c 61 67 3d 3d 30 20 69 6e 20 61 20 64 69 rFlag==0 in a di
14f79 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 fferent.** datab
14f7a 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 ase connection (
14f7b 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 a database conne
14f7c 63 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 ction that share
14f7d 73 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 63 s the pager.** c
14f7e 61 63 68 65 20 77 69 74 68 20 74 68 65 20 63 75 ache with the cu
14f7f 72 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e rrent connection
14f80 29 20 61 6e 64 20 74 68 61 74 20 6f 74 68 65 72 ) and that other
14f81 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 connection .**
14f82 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 65 is not in the Re
14f83 61 64 55 6e 63 6f 6d 6d 6d 69 74 74 65 64 20 73 adUncommmitted s
14f84 74 61 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20 tate, then this
14f85 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
14f86 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 .** SQLITE_LOCKE
14f87 44 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c D..**.** As well
14f88 20 61 73 20 63 75 72 73 6f 72 73 20 77 69 74 68 as cursors with
14f89 20 77 72 46 6c 61 67 3d 3d 30 2c 20 63 75 72 73 wrFlag==0, curs
14f8a 6f 72 73 20 77 69 74 68 20 77 72 46 6c 61 67 3d ors with wrFlag=
14f8b 3d 31 20 61 6e 64 20 0a 2a 2a 20 69 73 49 6e 63 =1 and .** isInc
14f8c 72 62 6c 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 rblobHandle==1 a
14f8d 72 65 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 re also consider
14f8e 65 64 20 27 72 65 61 64 27 20 63 75 72 73 6f 72 ed 'read' cursor
14f8f 73 2e 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 0a s. Incremental .
14f90 2a 2a 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 ** blob cursors
14f91 61 72 65 20 75 73 65 64 20 66 6f 72 20 62 6f 74 are used for bot
14f92 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 h reading and wr
14f93 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 iting..**.** Whe
14f94 6e 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68 n pgnoRoot is th
14f95 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 e root page of a
14f96 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2c 20 n intkey table,
14f97 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
14f98 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 70 6f 6e 73 also.** respons
14f99 69 62 6c 65 20 66 6f 72 20 69 6e 76 61 6c 69 64 ible for invalid
14f9a 61 74 69 6e 67 20 69 6e 63 72 65 6d 65 6e 74 61 ating incrementa
14f9b 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 77 l blob cursors w
14f9c 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 72 6f hen the table ro
14f9d 77 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 w.** on which th
14f9e 65 79 20 61 72 65 20 6f 70 65 6e 65 64 20 69 73 ey are opened is
14f9f 20 64 65 6c 65 74 65 64 20 6f 72 20 6d 6f 64 69 deleted or modi
14fa0 66 69 65 64 2e 20 43 75 72 73 6f 72 73 20 61 72 fied. Cursors ar
14fa1 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a 2a 2a e invalidated.**
14fa2 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 according to th
14fa3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 75 6c 65 e following rule
14fa4 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 s:.**.** 1) Wh
14fa5 65 6e 20 42 74 72 65 65 43 6c 65 61 72 54 61 62 en BtreeClearTab
14fa6 6c 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 le() is called t
14fa7 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 65 6c o completely del
14fa8 65 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ete the contents
14fa9 0a 2a 2a 20 20 20 20 20 20 6f 66 20 61 20 42 2d .** of a B-
14faa 54 72 65 65 20 74 61 62 6c 65 2c 20 70 45 78 63 Tree table, pExc
14fab 6c 75 64 65 20 69 73 20 73 65 74 20 74 6f 20 7a lude is set to z
14fac 65 72 6f 20 61 6e 64 20 70 61 72 61 6d 65 74 65 ero and paramete
14fad 72 20 69 52 6f 77 20 69 73 20 0a 2a 2a 20 20 20 r iRow is .**
14fae 20 20 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 set to non-ze
14faf 72 6f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ro. In this case
14fb0 20 61 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 61 6c all incremental
14fb1 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 blob cursors op
14fb2 65 6e 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 en.** on th
14fb3 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 61 e table rooted a
14fb4 74 20 70 67 6e 6f 52 6f 6f 74 20 61 72 65 20 69 t pgnoRoot are i
14fb5 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a nvalidated..**.*
14fb6 2a 20 20 20 32 29 20 57 68 65 6e 20 42 74 72 65 * 2) When Btre
14fb7 65 49 6e 73 65 72 74 28 29 2c 20 42 74 72 65 65 eInsert(), Btree
14fb8 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65 Delete() or Btre
14fb9 65 50 75 74 44 61 74 61 28 29 20 69 73 20 63 61 ePutData() is ca
14fba 6c 6c 65 64 20 74 6f 20 0a 2a 2a 20 20 20 20 20 lled to .**
14fbb 20 6d 6f 64 69 66 79 20 61 20 74 61 62 6c 65 20 modify a table
14fbc 72 6f 77 20 76 69 61 20 61 6e 20 53 51 4c 20 73 row via an SQL s
14fbd 74 61 74 65 6d 65 6e 74 2c 20 70 45 78 63 6c 75 tatement, pExclu
14fbe 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 de is set to the
14fbf 20 0a 2a 2a 20 20 20 20 20 20 77 72 69 74 65 20 .** write
14fc0 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 64 cursor used to d
14fc1 6f 20 74 68 65 20 6d 6f 64 69 66 69 63 61 74 69 o the modificati
14fc2 6f 6e 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72 on and parameter
14fc3 20 69 52 6f 77 20 69 73 20 73 65 74 0a 2a 2a 20 iRow is set.**
14fc4 20 20 20 20 20 74 6f 20 74 68 65 20 69 6e 74 65 to the inte
14fc5 67 65 72 20 72 6f 77 20 69 64 20 6f 66 20 74 68 ger row id of th
14fc6 65 20 42 2d 54 72 65 65 20 65 6e 74 72 79 20 62 e B-Tree entry b
14fc7 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 55 eing modified. U
14fc8 6e 6c 65 73 73 0a 2a 2a 20 20 20 20 20 20 70 45 nless.** pE
14fc9 78 63 6c 75 64 65 20 69 73 20 69 74 73 65 6c 66 xclude is itself
14fca 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 an incremental
14fcb 62 6c 6f 62 20 63 75 72 73 6f 72 2c 20 74 68 65 blob cursor, the
14fcc 6e 20 61 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 61 n all incrementa
14fcd 6c 0a 2a 2a 20 20 20 20 20 20 62 6c 6f 62 20 63 l.** blob c
14fce 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 72 ursors open on r
14fcf 6f 77 20 69 52 6f 77 20 6f 66 20 74 68 65 20 42 ow iRow of the B
14fd0 2d 54 72 65 65 20 61 72 65 20 69 6e 76 61 6c 69 -Tree are invali
14fd1 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 dated..**.** 3
14fd2 29 20 49 66 20 62 6f 74 68 20 70 45 78 63 6c 75 ) If both pExclu
14fd3 64 65 20 61 6e 64 20 69 52 6f 77 20 61 72 65 20 de and iRow are
14fd4 73 65 74 20 74 6f 20 7a 65 72 6f 2c 20 6e 6f 20 set to zero, no
14fd5 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 incremental blob
14fd6 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 73 6f 72 .** cursor
14fd7 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 s are invalidate
14fd8 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
14fd9 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 checkReadLocks(
14fda 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 . Btree *pBtree
14fdb 2c 20 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f , . Pgno pgnoRo
14fdc 6f 74 2c 20 0a 20 20 42 74 43 75 72 73 6f 72 20 ot, . BtCursor
14fdd 2a 70 45 78 63 6c 75 64 65 2c 0a 20 20 69 36 34 *pExclude,. i64
14fde 20 69 52 6f 77 0a 29 7b 0a 20 20 42 74 43 75 72 iRow.){. BtCur
14fdf 73 6f 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 sor *p;. BtShar
14fe0 65 64 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 ed *pBt = pBtree
14fe1 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 ->pBt;. sqlite3
14fe2 20 2a 64 62 20 3d 20 70 42 74 72 65 65 2d 3e 64 *db = pBtree->d
14fe3 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c b;. assert( sql
14fe4 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
14fe5 74 65 78 28 70 42 74 72 65 65 29 20 29 3b 0a 20 tex(pBtree) );.
14fe6 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 for(p=pBt->pCur
14fe7 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 sor; p; p=p->pNe
14fe8 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d xt){. if( p==
14fe9 70 45 78 63 6c 75 64 65 20 29 20 63 6f 6e 74 69 pExclude ) conti
14fea 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e nue;. if( p->
14feb 70 67 6e 6f 52 6f 6f 74 21 3d 70 67 6e 6f 52 6f pgnoRoot!=pgnoRo
14fec 6f 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 23 ot ) continue;.#
14fed 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14fee 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 IT_INCRBLOB.
14fef 69 66 28 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f if( p->isIncrblo
14ff0 62 48 61 6e 64 6c 65 20 26 26 20 28 20 0a 20 20 bHandle && ( .
14ff1 20 20 20 20 20 20 20 28 21 70 45 78 63 6c 75 64 (!pExclud
14ff2 65 20 26 26 20 69 52 6f 77 29 0a 20 20 20 20 20 e && iRow).
14ff3 20 7c 7c 20 28 70 45 78 63 6c 75 64 65 20 26 26 || (pExclude &&
14ff4 20 21 70 45 78 63 6c 75 64 65 2d 3e 69 73 49 6e !pExclude->isIn
14ff5 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 crblobHandle &&
14ff6 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 p->info.nKey==iR
14ff7 6f 77 29 0a 20 20 20 20 29 29 7b 0a 20 20 20 20 ow). )){.
14ff8 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 p->eState = CU
14ff9 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 RSOR_INVALID;.
14ffa 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 }.#endif. i
14ffb 66 28 20 70 2d 3e 65 53 74 61 74 65 21 3d 43 55 f( p->eState!=CU
14ffc 52 53 4f 52 5f 56 41 4c 49 44 20 29 20 63 6f 6e RSOR_VALID ) con
14ffd 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 tinue;. if( p
14ffe 2d 3e 77 72 46 6c 61 67 3d 3d 30 20 0a 23 69 66 ->wrFlag==0 .#if
14fff 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15000 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 7c _INCRBLOB. |
15001 7c 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 | p->isIncrblobH
15002 61 6e 64 6c 65 0a 23 65 6e 64 69 66 0a 20 20 20 andle.#endif.
15003 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
15004 33 20 2a 64 62 4f 74 68 65 72 20 3d 20 70 2d 3e 3 *dbOther = p->
15005 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 pBtree->db;.
15006 20 20 69 66 28 20 64 62 4f 74 68 65 72 3d 3d 30 if( dbOther==0
15007 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 28 64 62 ||. (db
15008 4f 74 68 65 72 21 3d 64 62 20 26 26 20 28 64 62 Other!=db && (db
15009 4f 74 68 65 72 2d 3e 66 6c 61 67 73 20 26 20 53 Other->flags & S
1500a 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d QLITE_ReadUncomm
1500b 69 74 74 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20 itted)==0) ){.
1500c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1500d 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 ITE_LOCKED;.
1500e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
1500f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15010 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 ;.}../*.** Inser
15011 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 t a new record i
15012 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20 nto the BTree.
15013 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e The key is given
15014 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a by (pKey,nKey).
15015 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 ** and the data
15016 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 44 61 is given by (pDa
15017 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20 ta,nData). The
15018 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f cursor is used o
15019 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 nly to.** define
1501a 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 65 20 what table the
1501b 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 record should be
1501c 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 20 inserted into.
1501d 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 The cursor.** i
1501e 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
1501f 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 at a random loca
15020 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 tion..**.** For
15021 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c an INTKEY table,
15022 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 76 only the nKey v
15023 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20 alue of the key
15024 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20 69 is used. pKey i
15025 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46 s.** ignored. F
15026 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74 61 or a ZERODATA ta
15027 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20 61 ble, the pData a
15028 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f 74 nd nData are bot
15029 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 h ignored..*/.SQ
1502a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1502b 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 sqlite3BtreeIns
1502c 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72 20 ert(. BtCursor
1502d 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 *pCur,
1502e 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 /* Insert
1502f 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74 61 data into the ta
15030 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72 73 ble of this curs
15031 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f or */. const vo
15032 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b id *pKey, i64 nK
15033 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 ey, /* The ke
15034 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 y of the new rec
15035 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ord */. const v
15036 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 oid *pData, int
15037 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 64 nData, /* The d
15038 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20 72 ata of the new r
15039 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ecord */. int n
1503a 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 Zero,
1503b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1503c 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 20 62 ber of extra 0 b
1503d 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 ytes to append t
1503e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 o data */. int
1503f 61 70 70 65 6e 64 42 69 61 73 20 20 20 20 20 20 appendBias
15040 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
15041 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 ue if this is li
15042 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 2a kely an append *
15043 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
15044 20 69 6e 74 20 6c 6f 63 3b 0a 20 20 69 6e 74 20 int loc;. int
15045 73 7a 4e 65 77 3b 0a 20 20 69 6e 74 20 69 64 78 szNew;. int idx
15046 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
15047 67 65 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d ge;. Btree *p =
15048 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 pCur->pBtree;.
15049 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1504a 20 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 p->pBt;. unsig
1504b 6e 65 64 20 63 68 61 72 20 2a 6f 6c 64 43 65 6c ned char *oldCel
1504c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 l;. unsigned ch
1504d 61 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 3b ar *newCell = 0;
1504e 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
1504f 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
15050 72 29 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d r) );. if( pBt-
15051 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d >inTransaction!=
15052 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 TRANS_WRITE ){.
15053 20 20 20 2f 2a 20 4d 75 73 74 20 73 74 61 72 74 /* Must start
15054 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 a transaction b
15055 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 20 69 efore doing an i
15056 6e 73 65 72 74 20 2a 2f 0a 20 20 20 20 72 63 20 nsert */. rc
15057 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 = pBt->readOnly
15058 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c ? SQLITE_READONL
15059 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 Y : SQLITE_ERROR
1505a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
1505b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 . }. assert( !
1505c 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b pBt->readOnly );
1505d 0a 20 20 69 66 28 20 21 70 43 75 72 2d 3e 77 72 . if( !pCur->wr
1505e 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75 Flag ){. retu
1505f 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 20 rn SQLITE_PERM;
15060 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 6f 74 20 /* Cursor not
15061 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 open for writing
15062 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 63 68 */. }. if( ch
15063 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 75 eckReadLocks(pCu
15064 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72 2d r->pBtree, pCur-
15065 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2c >pgnoRoot, pCur,
15066 20 6e 4b 65 79 29 20 29 7b 0a 20 20 20 20 72 65 nKey) ){. re
15067 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b turn SQLITE_LOCK
15068 45 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 ED; /* The table
15069 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 pCur points to
1506a 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 has a read lock
1506b 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 */. }. if( pCu
1506c 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
1506d 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 R_FAULT ){. r
1506e 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 eturn pCur->skip
1506f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 ;. }.. /* Save
15070 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f the positions o
15071 66 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73 f any other curs
15072 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 ors open on this
15073 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 table */. sqli
15074 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 te3BtreeClearCur
15075 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 69 66 28 sor(pCur);. if(
15076 20 0a 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 . SQLITE_OK!
15077 3d 28 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 =(rc = saveAllCu
15078 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d rsors(pBt, pCur-
15079 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 >pgnoRoot, pCur)
1507a 29 20 7c 7c 0a 20 20 20 20 53 51 4c 49 54 45 5f ) ||. SQLITE_
1507b 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 OK!=(rc = sqlite
1507c 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 3BtreeMoveto(pCu
1507d 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 r, pKey, nKey, a
1507e 70 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 ppendBias, &loc)
1507f 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ). ){. retur
15080 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 n rc;. }.. pPa
15081 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
15082 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
15083 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15084 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e 4b 65 79 3e >intKey || nKey>
15085 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
15086 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 pPage->leaf || !
15087 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b pPage->intKey );
15088 0a 20 20 54 52 41 43 45 28 28 22 49 4e 53 45 52 . TRACE(("INSER
15089 54 3a 20 74 61 62 6c 65 3d 25 64 20 6e 6b 65 79 T: table=%d nkey
1508a 3d 25 6c 6c 64 20 6e 64 61 74 61 3d 25 64 20 70 =%lld ndata=%d p
1508b 61 67 65 3d 25 64 20 25 73 5c 6e 22 2c 0a 20 20 age=%d %s\n",.
1508c 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 pCur->pg
1508d 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 79 2c 20 6e 44 noRoot, nKey, nD
1508e 61 74 61 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f ata, pPage->pgno
1508f 2c 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 3d ,. loc=
15090 3d 30 20 3f 20 22 6f 76 65 72 77 72 69 74 65 22 =0 ? "overwrite"
15091 20 3a 20 22 6e 65 77 20 65 6e 74 72 79 22 29 29 : "new entry"))
15092 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
15093 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 e->isInit );. a
15094 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 llocateTempSpace
15095 28 70 42 74 29 3b 0a 20 20 6e 65 77 43 65 6c 6c (pBt);. newCell
15096 20 3d 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 = pBt->pTmpSpac
15097 65 3b 0a 20 20 69 66 28 20 6e 65 77 43 65 6c 6c e;. if( newCell
15098 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==0 ) return SQL
15099 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 63 20 ITE_NOMEM;. rc
1509a 3d 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 = fillInCell(pPa
1509b 67 65 2c 20 6e 65 77 43 65 6c 6c 2c 20 70 4b 65 ge, newCell, pKe
1509c 79 2c 20 6e 4b 65 79 2c 20 70 44 61 74 61 2c 20 y, nKey, pData,
1509d 6e 44 61 74 61 2c 20 6e 5a 65 72 6f 2c 20 26 73 nData, nZero, &s
1509e 7a 4e 65 77 29 3b 0a 20 20 69 66 28 20 72 63 20 zNew);. if( rc
1509f 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 ) goto end_inser
150a0 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 4e t;. assert( szN
150a1 65 77 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 ew==cellSizePtr(
150a2 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c 29 20 pPage, newCell)
150a3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 4e );. assert( szN
150a4 65 77 3c 3d 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 ew<=MX_CELL_SIZE
150a5 28 70 42 74 29 20 29 3b 0a 20 20 69 64 78 20 3d (pBt) );. idx =
150a6 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
150a7 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 66 28 r->iPage];. if(
150a8 20 6c 6f 63 3d 3d 30 20 26 26 20 43 55 52 53 4f loc==0 && CURSO
150a9 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 R_VALID==pCur->e
150aa 53 74 61 74 65 20 29 7b 0a 20 20 20 20 75 31 36 State ){. u16
150ab 20 73 7a 4f 6c 64 3b 0a 20 20 20 20 61 73 73 65 szOld;. asse
150ac 72 74 28 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e rt( idx<pPage->n
150ad 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d Cell );. rc =
150ae 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
150af 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 te(pPage->pDbPag
150b0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 e);. if( rc )
150b1 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 {. goto end
150b2 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 7d 0a 20 _insert;. }.
150b3 20 20 20 6f 6c 64 43 65 6c 6c 20 3d 20 66 69 6e oldCell = fin
150b4 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 dCell(pPage, idx
150b5 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 );. if( !pPag
150b6 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
150b7 20 6d 65 6d 63 70 79 28 6e 65 77 43 65 6c 6c 2c memcpy(newCell,
150b8 20 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 oldCell, 4);.
150b9 20 20 7d 0a 20 20 20 20 73 7a 4f 6c 64 20 3d 20 }. szOld =
150ba 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
150bb 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 e, oldCell);.
150bc 20 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 rc = clearCell(
150bd 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b pPage, oldCell);
150be 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
150bf 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 to end_insert;.
150c0 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 dropCell(pPag
150c1 65 2c 20 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a e, idx, szOld);.
150c2 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c }else if( loc<
150c3 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c 0 && pPage->nCel
150c4 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 l>0 ){. asser
150c5 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 t( pPage->leaf )
150c6 3b 0a 20 20 20 20 69 64 78 20 3d 20 2b 2b 70 43 ;. idx = ++pC
150c7 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
150c8 69 50 61 67 65 5d 3b 0a 20 20 20 20 70 43 75 72 iPage];. pCur
150c9 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
150ca 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 ;. pCur->vali
150cb 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 6c dNKey = 0;. }el
150cc 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
150cd 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 pPage->leaf );.
150ce 20 7d 0a 20 20 72 63 20 3d 20 69 6e 73 65 72 74 }. rc = insert
150cf 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c Cell(pPage, idx,
150d0 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e 65 77 2c newCell, szNew,
150d1 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 0, 0);. if( rc
150d2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
150d3 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 to end_insert;.
150d4 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 rc = balance(pC
150d5 75 72 2c 20 31 29 3b 0a 20 20 69 66 28 20 72 63 ur, 1);. if( rc
150d6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
150d7 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 moveToRoot(pC
150d8 75 72 29 3b 0a 20 20 7d 0a 65 6e 64 5f 69 6e 73 ur);. }.end_ins
150d9 65 72 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 ert:. return rc
150da 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
150db 65 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 e the entry that
150dc 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 70 the cursor is p
150dd 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 ointing to. The
150de 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 cursor.** is le
150df 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 ft pointing at a
150e0 20 61 72 62 69 74 72 61 72 79 20 6c 6f 63 61 74 arbitrary locat
150e1 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
150e2 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
150e3 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 e3BtreeDelete(Bt
150e4 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
150e5 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
150e6 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
150e7 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 Cur->iPage];. i
150e8 6e 74 20 69 64 78 3b 0a 20 20 75 6e 73 69 67 6e nt idx;. unsign
150e9 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a ed char *pCell;.
150ea 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e 6f int rc;. Pgno
150eb 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 0a pgnoChild = 0;.
150ec 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 Btree *p = pCu
150ed 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 r->pBtree;. BtS
150ee 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
150ef 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 pBt;.. assert(
150f0 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
150f1 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
150f2 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
150f3 74 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e t );. if( pBt->
150f4 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 inTransaction!=T
150f5 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 RANS_WRITE ){.
150f6 20 20 2f 2a 20 4d 75 73 74 20 73 74 61 72 74 20 /* Must start
150f7 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 a transaction be
150f8 66 6f 72 65 20 64 6f 69 6e 67 20 61 20 64 65 6c fore doing a del
150f9 65 74 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 ete */. rc =
150fa 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 pBt->readOnly ?
150fb 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 SQLITE_READONLY
150fc 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a : SQLITE_ERROR;.
150fd 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
150fe 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 }. assert( !pB
150ff 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 t->readOnly );.
15100 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
15101 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e==CURSOR_FAULT
15102 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43 ){. return pC
15103 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a 20 20 ur->skip;. }.
15104 69 66 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b if( pCur->aiIdx[
15105 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 pCur->iPage]>=pP
15106 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 age->nCell ){.
15107 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15108 45 52 52 4f 52 3b 20 20 2f 2a 20 54 68 65 20 63 ERROR; /* The c
15109 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 ursor is not poi
1510a 6e 74 69 6e 67 20 74 6f 20 61 6e 79 74 68 69 6e nting to anythin
1510b 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 21 g */. }. if( !
1510c 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a pCur->wrFlag ){.
1510d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1510e 45 5f 50 45 52 4d 3b 20 20 20 2f 2a 20 44 69 64 E_PERM; /* Did
1510f 20 6e 6f 74 20 6f 70 65 6e 20 74 68 69 73 20 63 not open this c
15110 75 72 73 6f 72 20 66 6f 72 20 77 72 69 74 69 6e ursor for writin
15111 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 63 g */. }. if( c
15112 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 heckReadLocks(pC
15113 75 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72 ur->pBtree, pCur
15114 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 ->pgnoRoot, pCur
15115 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 , pCur->info.nKe
15116 79 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e y) ){. return
15117 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 SQLITE_LOCKED;
15118 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 /* The table pCu
15119 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 r points to has
1511a 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 a read lock */.
1511b 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 }.. /* Restore
1511c 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 the current cur
1511d 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20 sor position (a
1511e 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72 no-op if the cur
1511f 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20 sor is not in .
15120 20 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 ** CURSOR_REQUI
15121 52 45 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e RESEEK state) an
15122 64 20 73 61 76 65 20 74 68 65 20 70 6f 73 69 74 d save the posit
15123 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 ions of any othe
15124 72 20 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20 r cursors . **
15125 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 open on the same
15126 20 74 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c table. Then cal
15127 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 l sqlite3PagerWr
15128 69 74 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67 ite() on the pag
15129 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 e. ** that the
1512a 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65 entry will be de
1512b 6c 65 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f leted from.. */
1512c 0a 20 20 69 66 28 20 0a 20 20 20 20 28 72 63 20 . if( . (rc
1512d 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 = restoreCursorP
1512e 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d osition(pCur))!=
1512f 30 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 0 ||. (rc = s
15130 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 aveAllCursors(pB
15131 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f t, pCur->pgnoRoo
15132 74 2c 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a t, pCur))!=0 ||.
15133 20 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 (rc = sqlite
15134 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
15135 65 2d 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a e->pDbPage))!=0.
15136 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ){. return
15137 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f rc;. }.. /* Lo
15138 63 61 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 cate the cell wi
15139 74 68 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e thin its page an
1513a 64 20 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f d leave pCell po
1513b 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 inting to the.
1513c 2a 2a 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 ** data. The cle
1513d 61 72 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 arCell() call fr
1513e 65 65 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 ees any overflow
1513f 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 pages associate
15140 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 d with the. **
15141 63 65 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 cell. The cell i
15142 74 73 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 tself is still i
15143 6e 74 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64 ntact.. */. id
15144 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b x = pCur->aiIdx[
15145 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
15146 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
15147 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 (pPage, idx);.
15148 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
15149 20 29 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c ){. pgnoChil
1514a 64 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 d = get4byte(pCe
1514b 6c 6c 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 ll);. }. rc =
1514c 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c clearCell(pPage,
1514d 20 70 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 pCell);. if( r
1514e 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 c ){. return
1514f 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 rc;. }.. if( !
15150 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
15151 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 /*. ** The
15152 20 65 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 entry we are ab
15153 6f 75 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 out to delete is
15154 20 6e 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 not a leaf so i
15155 66 20 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 f we do not.
15156 2a 2a 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 ** do something
15157 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 we will leave a
15158 68 6f 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 hole on an inter
15159 6e 61 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a nal page.. **
1515a 20 57 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c We have to fill
1515b 20 74 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 the hole by mov
1515c 69 6e 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 ing in a cell fr
1515d 6f 6d 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a om a leaf. The.
1515e 20 20 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c ** next Cell
1515f 20 61 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 after the one t
15160 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 o be deleted is
15161 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 guaranteed to ex
15162 69 73 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 ist and. ** t
15163 6f 20 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 o be a leaf so w
15164 65 20 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 e can use it..
15165 20 20 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f */. BtCurso
15166 72 20 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d r leafCur;. M
15167 65 6d 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67 emPage *pLeafPag
15168 65 3b 0a 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 e;.. unsigned
15169 20 63 68 61 72 20 2a 70 4e 65 78 74 3b 0a 20 20 char *pNext;.
1516a 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a 20 int notUsed;.
1516b 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 unsigned char
1516c 20 2a 74 65 6d 70 43 65 6c 6c 20 3d 20 30 3b 0a *tempCell = 0;.
1516d 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 assert( !pPa
1516e 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 ge->intKey );.
1516f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 sqlite3BtreeGe
15170 74 54 65 6d 70 43 75 72 73 6f 72 28 70 43 75 72 tTempCursor(pCur
15171 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 20 , &leafCur);.
15172 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
15173 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75 72 2c eeNext(&leafCur,
15174 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 20 20 ¬Used);.
15175 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
15176 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 K ){. asser
15177 74 28 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 t( leafCur.aiIdx
15178 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3d [leafCur.iPage]=
15179 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c 65 61 =0 );. pLea
1517a 66 50 61 67 65 20 3d 20 6c 65 61 66 43 75 72 2e fPage = leafCur.
1517b 61 70 50 61 67 65 5b 6c 65 61 66 43 75 72 2e 69 apPage[leafCur.i
1517c 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 72 63 20 Page];. rc
1517d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1517e 69 74 65 28 70 4c 65 61 66 50 61 67 65 2d 3e 70 ite(pLeafPage->p
1517f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 DbPage);. }.
15180 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
15181 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e E_OK ){. in
15182 74 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61 t leafCursorInva
15183 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 lid = 0;. u
15184 31 36 20 73 7a 4e 65 78 74 3b 0a 20 20 20 20 20 16 szNext;.
15185 20 54 52 41 43 45 28 28 22 44 45 4c 45 54 45 3a TRACE(("DELETE:
15186 20 74 61 62 6c 65 3d 25 64 20 64 65 6c 65 74 65 table=%d delete
15187 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f 6d 20 25 internal from %
15188 64 20 72 65 70 6c 61 63 65 20 66 72 6f 6d 20 6c d replace from l
15189 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 eaf %d\n",.
1518a 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f pCur->pgnoRo
1518b 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c ot, pPage->pgno,
1518c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 6e 6f pLeafPage->pgno
1518d 29 29 3b 0a 20 20 20 20 20 20 64 72 6f 70 43 65 ));. dropCe
1518e 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 63 ll(pPage, idx, c
1518f 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
15190 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20 , pCell));.
15191 20 70 4e 65 78 74 20 3d 20 66 69 6e 64 43 65 6c pNext = findCel
15192 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 30 29 3b l(pLeafPage, 0);
15193 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 20 3d 20 . szNext =
15194 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65 61 cellSizePtr(pLea
15195 66 50 61 67 65 2c 20 70 4e 65 78 74 29 3b 0a 20 fPage, pNext);.
15196 20 20 20 20 20 61 73 73 65 72 74 28 20 4d 58 5f assert( MX_
15197 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 3e 3d CELL_SIZE(pBt)>=
15198 73 7a 4e 65 78 74 2b 34 20 29 3b 0a 20 20 20 20 szNext+4 );.
15199 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 allocateTempSp
1519a 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 ace(pBt);.
1519b 74 65 6d 70 43 65 6c 6c 20 3d 20 70 42 74 2d 3e tempCell = pBt->
1519c 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 20 pTmpSpace;.
1519d 20 69 66 28 20 74 65 6d 70 43 65 6c 6c 3d 3d 30 if( tempCell==0
1519e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1519f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
151a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
151a1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
151a2 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 69 {. rc = i
151a3 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c nsertCell(pPage,
151a4 20 69 64 78 2c 20 70 4e 65 78 74 2d 34 2c 20 73 idx, pNext-4, s
151a5 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 70 43 65 6c zNext+4, tempCel
151a6 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a l, 0);. }..
151a7 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 69 . /* The "i
151a8 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 f" statement in
151a9 74 68 65 20 6e 65 78 74 20 63 6f 64 65 20 62 6c the next code bl
151aa 6f 63 6b 20 69 73 20 63 72 69 74 69 63 61 6c 2e ock is critical.
151ab 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 The. ** s
151ac 6c 69 67 68 74 65 73 74 20 65 72 72 6f 72 20 69 lightest error i
151ad 6e 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 n that statement
151ae 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 20 53 51 4c would allow SQL
151af 69 74 65 20 74 6f 20 6f 70 65 72 61 74 65 0a 20 ite to operate.
151b0 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 63 74 6c ** correctl
151b1 79 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 69 y most of the ti
151b2 6d 65 20 62 75 74 20 70 72 6f 64 75 63 65 20 76 me but produce v
151b3 65 72 79 20 72 61 72 65 20 66 61 69 6c 75 72 65 ery rare failure
151b4 73 2e 20 20 54 6f 0a 20 20 20 20 20 20 2a 2a 20 s. To. **
151b5 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 74 68 guard against th
151b6 69 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e is, the followin
151b7 67 20 6d 61 63 72 6f 73 20 68 65 6c 70 20 74 6f g macros help to
151b8 20 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20 verify that.
151b9 20 20 20 2a 2a 20 74 68 65 20 22 69 66 22 20 73 ** the "if" s
151ba 74 61 74 65 6d 65 6e 74 20 69 73 20 77 65 6c 6c tatement is well
151bb 20 74 65 73 74 65 64 2e 0a 20 20 20 20 20 20 2a tested.. *
151bc 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 /. testcase
151bd 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
151be 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e ow==0 && pPage->
151bf 6e 46 72 65 65 3c 70 42 74 2d 3e 75 73 61 62 6c nFree<pBt->usabl
151c0 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 20 20 20 eSize*2/3 .
151c1 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 && p
151c2 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b LeafPage->nFree+
151c3 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74 2d 3e 2+szNext > pBt->
151c4 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 29 usableSize*2/3 )
151c5 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
151c6 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
151c7 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e ow==0 && pPage->
151c8 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 73 61 62 nFree==pBt->usab
151c9 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 20 20 leSize*2/3 .
151ca 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
151cb 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 65 pLeafPage->nFree
151cc 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74 2d +2+szNext > pBt-
151cd 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 >usableSize*2/3
151ce 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
151cf 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 e( pPage->nOverf
151d0 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d low==0 && pPage-
151d1 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 73 61 >nFree==pBt->usa
151d2 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 0a 20 bleSize*2/3+1 .
151d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
151d4 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 && pLeafPage->nF
151d5 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 ree+2+szNext > p
151d6 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
151d7 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 /3 );. test
151d8 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 case( pPage->nOv
151d9 65 72 66 6c 6f 77 3e 30 20 26 26 20 70 50 61 67 erflow>0 && pPag
151da 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 74 2d 3e 75 e->nFree<=pBt->u
151db 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 0a 20 20 sableSize*2/3.
151dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
151dd 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 & pLeafPage->nFr
151de 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 ee+2+szNext > pB
151df 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f t->usableSize*2/
151e0 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 3 );. testc
151e1 61 73 65 28 20 28 70 50 61 67 65 2d 3e 6e 4f 76 ase( (pPage->nOv
151e2 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 70 50 61 erflow>0 || (pPa
151e3 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 42 74 2d ge->nFree > pBt-
151e4 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 29 >usableSize*2/3)
151e5 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
151e6 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d && pLeafPage-
151e7 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 >nFree+2+szNext
151e8 3d 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 == pBt->usableSi
151e9 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a 20 20 20 20 ze*2/3 );...
151ea 20 20 69 66 28 20 28 70 50 61 67 65 2d 3e 6e 4f if( (pPage->nO
151eb 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 70 50 verflow>0 || (pP
151ec 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 42 74 age->nFree > pBt
151ed 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 ->usableSize*2/3
151ee 29 29 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 )) &&.
151ef 28 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 (pLeafPage->nFre
151f0 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74 e+2+szNext > pBt
151f1 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 ->usableSize*2/3
151f2 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ). ){.
151f3 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 /* This branc
151f4 68 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 h is taken if th
151f5 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 e internal node
151f6 69 73 20 6e 6f 77 20 65 69 74 68 65 72 20 6f 76 is now either ov
151f7 65 72 66 6c 6f 77 69 6e 67 0a 20 20 20 20 20 20 erflowing.
151f8 20 20 2a 2a 20 6f 72 20 75 6e 64 65 72 66 75 6c ** or underful
151f9 6c 20 61 6e 64 20 74 68 65 20 6c 65 61 66 20 6e l and the leaf n
151fa 6f 64 65 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 ode will be unde
151fb 72 66 75 6c 6c 20 61 66 74 65 72 20 74 68 65 20 rfull after the
151fc 6a 75 73 74 20 63 65 6c 6c 20 0a 20 20 20 20 20 just cell .
151fd 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 74 6f 20 ** copied to
151fe 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 the internal nod
151ff 65 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f e is deleted fro
15200 6d 20 69 74 2e 20 54 68 69 73 20 69 73 20 61 20 m it. This is a
15201 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 20 special.
15202 2a 2a 20 63 61 73 65 20 62 65 63 61 75 73 65 20 ** case because
15203 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 the call to bala
15204 6e 63 65 28 29 20 74 6f 20 63 6f 72 72 65 63 74 nce() to correct
15205 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f the internal no
15206 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 de. ** ma
15207 79 20 63 68 61 6e 67 65 20 74 68 65 20 74 72 65 y change the tre
15208 65 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 e structure and
15209 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 invalidate the c
1520a 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 20 20 20 20 ontents of.
1520b 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 66 43 75 ** the leafCu
1520c 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 6c r.apPage[] and l
1520d 65 61 66 43 75 72 2e 61 69 49 64 78 5b 5d 20 61 eafCur.aiIdx[] a
1520e 72 72 61 79 73 2c 20 77 68 69 63 68 20 77 69 6c rrays, which wil
1520f 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 l be. **
15210 75 73 65 64 20 62 79 20 74 68 65 20 62 61 6c 61 used by the bala
15211 6e 63 65 28 29 20 72 65 71 75 69 72 65 64 20 74 nce() required t
15212 6f 20 63 6f 72 72 65 63 74 20 74 68 65 20 75 6e o correct the un
15213 64 65 72 66 75 6c 6c 20 6c 65 61 66 0a 20 20 20 derfull leaf.
15214 20 20 20 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20 ** node..
15215 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
15216 20 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c 61 20 ** The formula
15217 75 73 65 64 20 69 6e 20 74 68 65 20 65 78 70 72 used in the expr
15218 65 73 73 69 6f 6e 20 61 62 6f 76 65 20 61 72 65 ession above are
15219 20 62 61 73 65 64 20 6f 6e 20 66 61 63 65 74 73 based on facets
1521a 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 of. ** t
1521b 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 2d 66 he SQLite file-f
1521c 6f 72 6d 61 74 20 74 68 61 74 20 64 6f 20 6e 6f ormat that do no
1521d 74 20 63 68 61 6e 67 65 20 6f 76 65 72 20 74 69 t change over ti
1521e 6d 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 me.. */.
1521f 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
15220 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 pPage->nFree==p
15221 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
15222 2f 33 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 20 /3+1 );.
15223 74 65 73 74 63 61 73 65 28 20 70 4c 65 61 66 50 testcase( pLeafP
15224 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e age->nFree+2+szN
15225 65 78 74 3d 3d 70 42 74 2d 3e 75 73 61 62 6c 65 ext==pBt->usable
15226 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 Size*2/3+1 );.
15227 20 20 20 20 20 20 6c 65 61 66 43 75 72 73 6f 72 leafCursor
15228 49 6e 76 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 Invalid = 1;.
15229 20 20 20 7d 20 20 20 20 20 20 20 20 0a 0a 20 20 } ..
1522a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1522b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1522c 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76 put4byte(findOv
1522d 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 erflowCell(pPage
1522e 2c 20 69 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c , idx), pgnoChil
1522f 64 29 3b 0a 20 20 20 20 20 20 20 20 56 56 41 5f d);. VVA_
15230 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 ONLY( pCur->page
15231 73 53 68 75 66 66 6c 65 64 20 3d 20 30 20 29 3b sShuffled = 0 );
15232 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 . rc = ba
15233 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b 0a lance(pCur, 0);.
15234 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 }.. i
15235 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15236 20 26 26 20 6c 65 61 66 43 75 72 73 6f 72 49 6e && leafCursorIn
15237 76 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 valid ){.
15238 20 2f 2a 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 /* The leaf-nod
15239 65 20 69 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 e is now underfu
1523a 6c 6c 20 61 6e 64 20 73 6f 20 74 68 65 20 74 72 ll and so the tr
1523b 65 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0a ee needs to be .
1523c 20 20 20 20 20 20 20 20 2a 2a 20 72 65 62 61 6c ** rebal
1523d 61 6e 63 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 anced. However,
1523e 74 68 65 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 the balance() op
1523f 65 72 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 eration on the i
15240 6e 74 65 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 nternal.
15241 2a 2a 20 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 ** node above ma
15242 79 20 68 61 76 65 20 6d 6f 64 69 66 69 65 64 20 y have modified
15243 74 68 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 the structure of
15244 20 74 68 65 20 42 2d 54 72 65 65 20 61 6e 64 0a the B-Tree and.
15245 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 ** so th
15246 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e e current conten
15247 74 73 20 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 ts of leafCur.ap
15248 50 61 67 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 Page[] and leafC
15249 75 72 2e 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 ur.aiIdx[].
1524a 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 ** may not be
1524b 20 74 72 75 73 74 65 64 2e 0a 20 20 20 20 20 20 trusted..
1524c 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 **. **
1524d 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 It is not possib
1524e 6c 65 20 74 6f 20 63 6f 70 79 20 74 68 65 20 61 le to copy the a
1524f 6e 63 65 73 74 72 79 20 66 72 6f 6d 20 70 43 75 ncestry from pCu
15250 72 2c 20 61 73 20 74 68 65 20 73 61 6d 65 0a 20 r, as the same.
15251 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 ** balanc
15252 65 28 29 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 e() call has inv
15253 61 6c 69 64 61 74 65 64 20 74 68 65 20 70 43 75 alidated the pCu
15254 72 2d 3e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 r->apPage[] and
15255 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 aiIdx[].
15256 2a 2a 20 61 72 72 61 79 73 2e 20 0a 20 20 20 20 ** arrays. .
15257 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
15258 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 * The call to sa
15259 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e veCursorPosition
1525a 28 29 20 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 () below interna
1525b 6c 6c 79 20 73 61 76 65 73 20 74 68 65 20 0a 20 lly saves the .
1525c 20 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 ** key th
1525d 61 74 20 6c 65 61 66 43 75 72 20 69 73 20 63 75 at leafCur is cu
1525e 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
1525f 20 74 6f 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 to. Currently,
15260 74 68 65 72 65 0a 20 20 20 20 20 20 20 20 2a 2a there. **
15261 20 61 72 65 20 74 77 6f 20 63 6f 70 69 65 73 20 are two copies
15262 6f 66 20 74 68 61 74 20 6b 65 79 20 69 6e 20 74 of that key in t
15263 68 65 20 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 he tree - one he
15264 72 65 20 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 re on the leaf.
15265 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 61 ** page a
15266 6e 64 20 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 nd one on some i
15267 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 nternal node in
15268 74 68 65 20 74 72 65 65 2e 20 54 68 65 20 63 6f the tree. The co
15269 70 79 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a py on. **
1526a 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 the leaf node i
1526b 73 20 61 6c 77 61 79 73 20 74 68 65 20 6e 65 78 s always the nex
1526c 74 20 6b 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 t key in tree-or
1526d 64 65 72 20 61 66 74 65 72 20 74 68 65 20 0a 20 der after the .
1526e 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f ** copy o
1526f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e n the internal n
15270 6f 64 65 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c ode. So, the cal
15271 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 l to sqlite3Btre
15272 65 4e 65 78 74 28 29 0a 20 20 20 20 20 20 20 20 eNext().
15273 2a 2a 20 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 ** calls restore
15274 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
15275 20 74 6f 20 70 6f 69 6e 74 20 74 68 65 20 63 75 to point the cu
15276 72 73 6f 72 20 74 6f 20 74 68 65 20 63 6f 70 79 rsor to the copy
15277 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 . ** stor
15278 65 64 20 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e ed on the intern
15279 61 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 al node, then ad
1527a 76 61 6e 63 65 73 20 74 6f 20 74 68 65 20 6e 65 vances to the ne
1527b 78 74 20 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 xt entry,.
1527c 20 20 2a 2a 20 77 68 69 63 68 20 68 61 70 70 65 ** which happe
1527d 6e 73 20 74 6f 20 62 65 20 74 68 65 20 63 6f 70 ns to be the cop
1527e 79 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 y of the key on
1527f 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 the internal nod
15280 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 e.. ** Ne
15281 74 20 65 66 66 65 63 74 3a 20 6c 65 61 66 43 75 t effect: leafCu
15282 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 r is pointing ba
15283 63 6b 20 74 6f 20 74 68 65 20 64 75 70 6c 69 63 ck to the duplic
15284 61 74 65 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 ate cell.
15285 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 73 20 74 ** that needs t
15286 6f 20 62 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e o be removed, an
15287 64 20 74 68 65 20 6c 65 61 66 43 75 72 2e 61 70 d the leafCur.ap
15288 50 61 67 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 Page[] and.
15289 20 20 20 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 ** leafCur.ai
1528a 49 64 78 5b 5d 20 61 72 72 61 79 73 20 61 72 65 Idx[] arrays are
1528b 20 63 6f 72 72 65 63 74 2e 0a 20 20 20 20 20 20 correct..
1528c 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 56 56 41 */. VVA
1528d 5f 4f 4e 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 _ONLY( Pgno leaf
1528e 50 67 6e 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 Pgno = pLeafPage
1528f 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 ->pgno );.
15290 20 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f rc = saveCurso
15291 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 rPosition(&leafC
15292 75 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ur);. if(
15293 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
15294 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
15295 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
15296 74 28 26 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 t(&leafCur, ¬
15297 55 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d Used);. }
15298 0a 20 20 20 20 20 20 20 20 70 4c 65 61 66 50 61 . pLeafPa
15299 67 65 20 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 ge = leafCur.apP
1529a 61 67 65 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 age[leafCur.iPag
1529b 65 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 e];. asse
1529c 72 74 28 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 rt( pLeafPage->p
1529d 67 6e 6f 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b gno==leafPgno );
1529e 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1529f 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 6c leafCur.aiIdx[l
152a0 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3d 3d 30 eafCur.iPage]==0
152a1 20 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 );. }..
152a2 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
152a3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
152a4 64 72 6f 70 43 65 6c 6c 28 70 4c 65 61 66 50 61 dropCell(pLeafPa
152a5 67 65 2c 20 30 2c 20 73 7a 4e 65 78 74 29 3b 0a ge, 0, szNext);.
152a6 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59 VVA_ONLY
152a7 28 20 6c 65 61 66 43 75 72 2e 70 61 67 65 73 53 ( leafCur.pagesS
152a8 68 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a 20 huffled = 0 );.
152a9 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 rc = bala
152aa 6e 63 65 28 26 6c 65 61 66 43 75 72 2c 20 30 29 nce(&leafCur, 0)
152ab 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
152ac 28 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61 ( leafCursorInva
152ad 6c 69 64 20 7c 7c 20 21 6c 65 61 66 43 75 72 2e lid || !leafCur.
152ae 70 61 67 65 73 53 68 75 66 66 6c 65 64 0a 20 20 pagesShuffled.
152af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
152b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
152b1 20 7c 7c 20 21 70 43 75 72 2d 3e 70 61 67 65 73 || !pCur->pages
152b2 53 68 75 66 66 6c 65 64 20 29 3b 0a 20 20 20 20 Shuffled );.
152b3 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
152b4 6c 69 74 65 33 42 74 72 65 65 52 65 6c 65 61 73 lite3BtreeReleas
152b5 65 54 65 6d 70 43 75 72 73 6f 72 28 26 6c 65 61 eTempCursor(&lea
152b6 66 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a fCur);. }else{.
152b7 20 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45 TRACE(("DELE
152b8 54 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c TE: table=%d del
152b9 65 74 65 20 66 72 6f 6d 20 6c 65 61 66 20 25 64 ete from leaf %d
152ba 5c 6e 22 2c 0a 20 20 20 20 20 20 20 70 43 75 72 \n",. pCur
152bb 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 50 61 67 ->pgnoRoot, pPag
152bc 65 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 64 e->pgno));. d
152bd 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 ropCell(pPage, i
152be 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 dx, cellSizePtr(
152bf 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a pPage, pCell));.
152c0 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 rc = balance
152c1 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 (pCur, 0);. }.
152c2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
152c3 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f OK ){. moveTo
152c4 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a Root(pCur);. }.
152c5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
152c6 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
152c7 65 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 ew BTree table.
152c8 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 Write into *piT
152c9 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a able the page.**
152ca 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
152cb 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 root page of the
152cc 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a new table..**.*
152cd 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 * The type of ty
152ce 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 pe is determined
152cf 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 by the flags pa
152d0 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 rameter. Only t
152d1 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 he.** following
152d2 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 values of flags
152d3 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e are currently in
152d4 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c use. Other val
152d5 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 ues for.** flags
152d6 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a might not work:
152d7 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 .**.** BTREE
152d8 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 _INTKEY|BTREE_LE
152d9 41 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20 AFDATA Used
152da 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 for SQL tables w
152db 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a ith rowid keys.*
152dc 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f * BTREE_ZERO
152dd 44 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20 DATA
152de 20 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 Used for S
152df 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 QL indices.*/.st
152e0 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 atic int btreeCr
152e1 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 eateTable(Btree
152e2 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 *p, int *piTable
152e3 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
152e4 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
152e5 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 p->pBt;. MemPag
152e6 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f e *pRoot;. Pgno
152e7 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 pgnoRoot;. int
152e8 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
152e9 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
152ea 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 69 sMutex(p) );. i
152eb 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 f( pBt->inTransa
152ec 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 57 52 49 ction!=TRANS_WRI
152ed 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 75 73 TE ){. /* Mus
152ee 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 t start a transa
152ef 63 74 69 6f 6e 20 66 69 72 73 74 20 2a 2f 0a 20 ction first */.
152f0 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 rc = pBt->rea
152f1 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 dOnly ? SQLITE_R
152f2 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 EADONLY : SQLITE
152f3 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 _ERROR;. retu
152f4 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 rn rc;. }. ass
152f5 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f ert( !pBt->readO
152f6 6e 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 nly );..#ifdef S
152f7 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
152f8 41 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c ACUUM. rc = all
152f9 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 ocateBtreePage(p
152fa 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e Bt, &pRoot, &pgn
152fb 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 oRoot, 1, 0);.
152fc 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 if( rc ){. re
152fd 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c turn rc;. }.#el
152fe 73 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 se. if( pBt->au
152ff 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
15300 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 Pgno pgnoMove;
15301 20 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 /* Move a pa
15302 67 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 ge here to make
15303 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f room for the roo
15304 74 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 t-page */. Me
15305 6d 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 mPage *pPageMove
15306 3b 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f ; /* The page to
15307 20 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 move to. */..
15308 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 /* Creating a
15309 6e 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72 new table may pr
1530a 6f 62 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d obably require m
1530b 6f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e oving an existin
1530c 67 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a g database. *
1530d 2a 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 * to make room f
1530e 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 or the new table
1530f 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 s root page. In
15310 63 61 73 65 20 74 68 69 73 20 70 61 67 65 20 74 case this page t
15311 75 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 urns. ** out
15312 74 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f to be an overflo
15313 77 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61 w page, delete a
15314 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ll overflow page
15315 2d 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20 20 -map caches.
15316 2a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 ** held by open
15317 63 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a cursors.. */.
15318 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c invalidateAl
15319 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 lOverflowCache(p
1531a 42 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 Bt);.. /* Rea
1531b 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d d the value of m
1531c 65 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 eta[3] from the
1531d 64 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 65 database to dete
1531e 72 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a rmine where the.
1531f 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 ** root page
15320 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c of the new tabl
15321 65 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 e should go. met
15322 61 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67 a[3] is the larg
15323 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 est root-page.
15324 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 ** created so
15325 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 far, so the new
15326 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65 root-page is (me
15327 74 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f ta[3]+1).. */
15328 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
15329 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2c 3BtreeGetMeta(p,
1532a 20 34 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 4, &pgnoRoot);.
1532b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1532c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1532d 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1532e 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a pgnoRoot++;.
1532f 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 . /* The new
15330 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f root-page may no
15331 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f t be allocated o
15332 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 n a pointer-map
15333 70 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 page, or the.
15334 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 ** PENDING_BYTE
15335 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
15336 20 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f while( pgnoRoo
15337 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f t==PTRMAP_PAGENO
15338 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 (pBt, pgnoRoot)
15339 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 ||. pgnoR
1533a 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 oot==PENDING_BYT
1533b 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 E_PAGE(pBt) ){.
1533c 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b pgnoRoot++;
1533d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
1533e 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 t( pgnoRoot>=3 )
1533f 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 ;.. /* Alloca
15340 74 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 te a page. The p
15341 61 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 age that current
15342 6c 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67 ly resides at pg
15343 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 noRoot will.
15344 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 ** be moved to t
15345 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 he allocated pag
15346 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c e (unless the al
15347 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70 located page hap
15348 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 pens. ** to r
15349 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f eside at pgnoRoo
1534a 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 t).. */. r
1534b 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
1534c 65 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 ePage(pBt, &pPag
1534d 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 eMove, &pgnoMove
1534e 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a , pgnoRoot, 1);.
1534f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
15350 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
15351 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
15352 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 . if( pgnoMov
15353 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 e!=pgnoRoot ){.
15354 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 /* pgnoRoot
15355 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61 is the page tha
15356 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 t will be used f
15357 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 or the root-page
15358 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 of. ** the
15359 20 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 new table (assu
1535a 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 ming an error di
1535b 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 d not occur). Bu
1535c 74 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 t we were.
1535d 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e ** allocated pgn
1535e 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 oMove. If requir
1535f 65 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 ed (i.e. if it w
15360 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 as not allocated
15361 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 . ** by ext
15362 65 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 ending the file)
15363 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 , the current pa
15364 67 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 ge at position p
15365 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a gnoMove. **
15366 20 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 is already jour
15367 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a naled.. */.
15368 20 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a u8 eType;.
15369 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 Pgno iPtrP
1536a 61 67 65 3b 0a 0a 20 20 20 20 20 20 72 65 6c 65 age;.. rele
1536b 61 73 65 50 61 67 65 28 70 50 61 67 65 4d 6f 76 asePage(pPageMov
1536c 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 6f e);.. /* Mo
1536d 76 65 20 74 68 65 20 70 61 67 65 20 63 75 72 72 ve the page curr
1536e 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 6f 6f ently at pgnoRoo
1536f 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a t to pgnoMove. *
15370 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c /. rc = sql
15371 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
15372 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 (pBt, pgnoRoot,
15373 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 &pRoot, 0);.
15374 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15375 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
15376 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
15377 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 }. rc = ptr
15378 6d 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f mapGet(pBt, pgno
15379 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 Root, &eType, &i
1537a 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 PtrPage);.
1537b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1537c 4b 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d K || eType==PTRM
1537d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 AP_ROOTPAGE || e
1537e 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 Type==PTRMAP_FRE
1537f 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 EPAGE ){.
15380 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f releasePage(pRo
15381 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 ot);. ret
15382 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
15383 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 assert( eT
15384 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 ype!=PTRMAP_ROOT
15385 50 41 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73 PAGE );. as
15386 73 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 sert( eType!=PTR
15387 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a MAP_FREEPAGE );.
15388 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
15389 65 33 50 61 67 65 72 57 72 69 74 65 28 70 52 6f e3PagerWrite(pRo
1538a 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ot->pDbPage);.
1538b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1538c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1538d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f releasePage(pRo
1538e 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 ot);. ret
1538f 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
15390 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 rc = reloc
15391 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f atePage(pBt, pRo
15392 6f 74 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 ot, eType, iPtrP
15393 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 age, pgnoMove, 0
15394 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 );. release
15395 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 Page(pRoot);..
15396 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 /* Obtain th
15397 65 20 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f e page at pgnoRo
15398 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 ot */. if(
15399 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1539a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1539b 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
1539c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1539d 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1539e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 pgnoRoot, &pRoot
1539f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
153a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
153a1 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
153a2 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
153a3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
153a4 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e gerWrite(pRoot->
153a5 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
153a6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
153a7 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c K ){. rel
153a8 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
153a9 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
153aa 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
153ab 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f }else{. pRo
153ac 6f 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a ot = pPageMove;.
153ad 20 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 } .. /* U
153ae 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 pdate the pointe
153af 72 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 r-map and meta-d
153b0 61 74 61 20 77 69 74 68 20 74 68 65 20 6e 65 77 ata with the new
153b1 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 root-page numbe
153b2 72 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 r. */. rc = p
153b3 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 trmapPut(pBt, pg
153b4 6e 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52 noRoot, PTRMAP_R
153b5 4f 4f 54 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 OOTPAGE, 0);.
153b6 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
153b7 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f releasePage(pRo
153b8 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ot);. retur
153b9 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
153ba 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
153bb 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 eUpdateMeta(p, 4
153bc 2c 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 , pgnoRoot);.
153bd 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
153be 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f releasePage(pRo
153bf 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ot);. retur
153c0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d n rc;. }.. }
153c1 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 else{. rc = a
153c2 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
153c3 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 (pBt, &pRoot, &p
153c4 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a gnoRoot, 1, 0);.
153c5 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
153c6 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 urn rc;. }.#end
153c7 69 66 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c if. assert( sql
153c8 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
153c9 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 able(pRoot->pDbP
153ca 61 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 age) );. zeroPa
153cb 67 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 ge(pRoot, flags
153cc 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 | PTF_LEAF);. s
153cd 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
153ce 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 (pRoot->pDbPage)
153cf 3b 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 ;. *piTable = (
153d0 69 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 int)pgnoRoot;.
153d1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
153d2 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
153d3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
153d4 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 reeCreateTable(B
153d5 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 tree *p, int *pi
153d6 54 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 Table, int flags
153d7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
153d8 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
153d9 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 (p);. p->pBt->d
153da 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63 20 b = p->db;. rc
153db 3d 20 62 74 72 65 65 43 72 65 61 74 65 54 61 62 = btreeCreateTab
153dc 6c 65 28 70 2c 20 70 69 54 61 62 6c 65 2c 20 66 le(p, piTable, f
153dd 6c 61 67 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 lags);. sqlite3
153de 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
153df 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
153e0 2a 0a 2a 2a 20 45 72 61 73 65 20 74 68 65 20 67 *.** Erase the g
153e1 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 iven database pa
153e2 67 65 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 63 ge and all its c
153e3 68 69 6c 64 72 65 6e 2e 20 20 52 65 74 75 72 6e hildren. Return
153e4 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 74 6f 20 .** the page to
153e5 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2f the freelist..*/
153e6 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 .static int clea
153e7 72 44 61 74 61 62 61 73 65 50 61 67 65 28 0a 20 rDatabasePage(.
153e8 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
153e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
153ea 20 42 54 72 65 65 20 74 68 61 74 20 63 6f 6e 74 BTree that cont
153eb 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 20 2a ains the table *
153ec 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 /. Pgno pgno,
153ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
153ee 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 6c 65 61 e number to clea
153ef 72 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a r */. MemPage *
153f0 70 50 61 72 65 6e 74 2c 20 20 20 20 20 2f 2a 20 pParent, /*
153f1 50 61 72 65 6e 74 20 70 61 67 65 2e 20 20 4e 55 Parent page. NU
153f2 4c 4c 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 LL for the root
153f3 2a 2f 0a 20 20 69 6e 74 20 66 72 65 65 50 61 67 */. int freePag
153f4 65 46 6c 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 eFlag, /* De
153f5 61 6c 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 allocate page if
153f6 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a true */. int *
153f7 70 6e 43 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 pnChange.){. Me
153f8 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 mPage *pPage = 0
153f9 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e ;. int rc;. un
153fa 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 signed char *pCe
153fb 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 ll;. int i;..
153fc 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
153fd 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
153fe 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
153ff 70 67 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f pgno>pagerPageco
15400 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 unt(pBt->pPager)
15401 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
15402 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15403 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 PT;. }.. rc =
15404 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 getAndInitPage(p
15405 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 Bt, pgno, &pPage
15406 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f );. if( rc ) go
15407 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 to cleardatabase
15408 70 61 67 65 5f 6f 75 74 3b 0a 20 20 66 6f 72 28 page_out;. for(
15409 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 i=0; i<pPage->nC
1540a 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 ell; i++){. p
1540b 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
1540c 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 pPage, i);. i
1540d 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
1540e 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c ){. rc = cl
1540f 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 earDatabasePage(
15410 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 pBt, get4byte(pC
15411 65 6c 6c 29 2c 20 70 50 61 67 65 2c 20 31 2c 20 ell), pPage, 1,
15412 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 pnChange);.
15413 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 if( rc ) goto c
15414 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 leardatabasepage
15415 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
15416 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 rc = clearCell(p
15417 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 Page, pCell);.
15418 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
15419 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 cleardatabasepag
1541a 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 e_out;. }. if(
1541b 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
1541c 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 . rc = clearD
1541d 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c atabasePage(pBt,
1541e 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
1541f 2d 3e 61 44 61 74 61 5b 38 5d 29 2c 20 70 50 61 ->aData[8]), pPa
15420 67 65 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 ge, 1, pnChange)
15421 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
15422 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 oto cleardatabas
15423 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c epage_out;. }el
15424 73 65 20 69 66 28 20 70 6e 43 68 61 6e 67 65 20 se if( pnChange
15425 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
15426 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a Page->intKey );.
15427 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d *pnChange +=
15428 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 pPage->nCell;.
15429 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61 67 }. if( freePag
1542a 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 63 20 eFlag ){. rc
1542b 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67 65 = freePage(pPage
1542c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 );. }else if( (
1542d 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1542e 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 rWrite(pPage->pD
1542f 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 bPage))==0 ){.
15430 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 zeroPage(pPage
15431 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 , pPage->aData[0
15432 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 ] | PTF_LEAF);.
15433 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 73 }..cleardatabas
15434 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c epage_out:. rel
15435 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
15436 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15437 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c ./*.** Delete al
15438 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 l information fr
15439 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c om a single tabl
1543a 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
1543b 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a e. iTable is.**
1543c 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
1543d 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 of the root of
1543e 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74 65 the table. Afte
1543f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 r this routine r
15440 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 eturns,.** the r
15441 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70 74 oot page is empt
15442 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78 69 y, but still exi
15443 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 sts..**.** This
15444 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 routine will fai
15445 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f l with SQLITE_LO
15446 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 72 CKED if there ar
15447 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 e any open.** re
15448 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 ad cursors on th
15449 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 e table. Open w
1544a 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 65 rite cursors are
1544b 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a moved to the.**
1544c 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 root of the tab
1544d 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 le..**.** If pnC
1544e 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c hange is not NUL
1544f 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 69 54 L, then table iT
15450 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6e 20 able must be an
15451 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 intkey table. Th
15452 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c e.** integer val
15453 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ue pointed to by
15454 20 70 6e 43 68 61 6e 67 65 20 69 73 20 69 6e 63 pnChange is inc
15455 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 remented by the
15456 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 number of.** ent
15457 72 69 65 73 20 69 6e 20 74 68 65 20 74 61 62 6c ries in the tabl
15458 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
15459 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1545a 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 BtreeClearTable(
1545b 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 Btree *p, int iT
1545c 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 able, int *pnCha
1545d 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a nge){. int rc;.
1545e 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
1545f 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 = p->pBt;. sqli
15460 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
15461 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d ;. pBt->db = p-
15462 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e >db;. if( p->in
15463 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 Trans!=TRANS_WRI
15464 54 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 TE ){. rc = p
15465 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 53 Bt->readOnly ? S
15466 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 3a QLITE_READONLY :
15467 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
15468 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 3d }else if( (rc =
15469 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 checkReadLocks(
1546a 70 2c 20 69 54 61 62 6c 65 2c 20 30 2c 20 31 29 p, iTable, 0, 1)
1546b 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a )!=SQLITE_OK ){.
1546c 20 20 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 /* nothing t
1546d 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 o do */. }else
1546e 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 if( SQLITE_OK!=(
1546f 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 rc = saveAllCurs
15470 6f 72 73 28 70 42 74 2c 20 69 54 61 62 6c 65 2c ors(pBt, iTable,
15471 20 30 29 29 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 0)) ){. /* n
15472 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a othing to do */.
15473 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
15474 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 = clearDatabaseP
15475 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 age(pBt, (Pgno)i
15476 54 61 62 6c 65 2c 20 30 2c 20 30 2c 20 70 6e 43 Table, 0, 0, pnC
15477 68 61 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71 hange);. }. sq
15478 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15479 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
1547a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 .}../*.** Erase
1547b 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 all information
1547c 69 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 in a table and a
1547d 64 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 dd the root of t
1547e 68 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 he table to.** t
1547f 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78 he freelist. Ex
15480 63 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f cept, the root o
15481 66 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20 f the principle
15482 74 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f table (the one o
15483 6e 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20 n.** page 1) is
15484 6e 65 76 65 72 20 61 64 64 65 64 20 74 6f 20 74 never added to t
15485 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a he freelist..**.
15486 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
15487 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 will fail with S
15488 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 QLITE_LOCKED if
15489 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 there are any op
1548a 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e en.** cursors on
1548b 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a the table..**.*
1548c 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 * If AUTOVACUUM
1548d 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74 is enabled and t
1548e 68 65 20 70 61 67 65 20 61 74 20 69 54 61 62 6c he page at iTabl
1548f 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 e is not the las
15490 74 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69 t.** root page i
15491 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
15492 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 ile, then the la
15493 73 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a st root page .**
15494 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
15495 20 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69 file is moved i
15496 6e 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72 nto the slot for
15497 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 merly occupied b
15498 79 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20 y.** iTable and
15499 74 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66 that last slot f
1549a 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 ormerly occupied
1549b 20 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f by the last roo
1549c 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64 t page.** is add
1549d 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 ed to the freeli
1549e 73 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54 st instead of iT
1549f 61 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73 able. In this s
154a0 61 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 ay, all.** root
154a1 70 61 67 65 73 20 61 72 65 20 6b 65 70 74 20 61 pages are kept a
154a2 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
154a3 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
154a4 66 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69 file, which.** i
154a5 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 s necessary for
154a6 41 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f AUTOVACUUM to wo
154a7 72 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f rk right. *piMo
154a8 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 ved is set to th
154a9 65 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 e .** page numbe
154aa 72 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 62 r that used to b
154ab 65 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 e the last root
154ac 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 page in the file
154ad 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d before.** the m
154ae 6f 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 ove. If no page
154af 20 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69 gets moved, *pi
154b0 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 Moved is set to
154b1 30 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72 0..** The last r
154b2 6f 6f 74 20 70 61 67 65 20 69 73 20 72 65 63 6f oot page is reco
154b3 72 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20 rded in meta[3]
154b4 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 and the value of
154b5 0a 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75 .** meta[3] is u
154b6 70 64 61 74 65 64 20 62 79 20 74 68 69 73 20 70 pdated by this p
154b7 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 rocedure..*/.sta
154b8 74 69 63 20 69 6e 74 20 62 74 72 65 65 44 72 6f tic int btreeDro
154b9 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c pTable(Btree *p,
154ba 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 int iTable, int
154bb 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e *piMoved){. in
154bc 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 t rc;. MemPage
154bd 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42 74 *pPage = 0;. Bt
154be 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
154bf 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 >pBt;.. assert(
154c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
154c1 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 dsMutex(p) );.
154c2 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d if( p->inTrans!=
154c3 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 TRANS_WRITE ){.
154c4 20 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 72 return pBt->r
154c5 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 eadOnly ? SQLITE
154c6 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 _READONLY : SQLI
154c7 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 TE_ERROR;. }..
154c8 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 /* It is illega
154c9 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 6c l to drop a tabl
154ca 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 73 e if any cursors
154cb 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 are open on the
154cc 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 . ** database.
154cd 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 This is because
154ce 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d in auto-vacuum m
154cf 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 20 ode the backend
154d0 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f may. ** need to
154d1 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 6f move another ro
154d2 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c 20 ot-page to fill
154d3 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 a gap left by th
154d4 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72 e deleted. ** r
154d5 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e 20 oot page. If an
154d6 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 20 open cursor was
154d7 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 using this page
154d8 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 a problem would
154d9 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a . ** occur.. *
154da 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 43 75 /. if( pBt->pCu
154db 72 73 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 rsor ){. retu
154dc 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 rn SQLITE_LOCKED
154dd 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 ;. }.. rc = sq
154de 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
154df 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 e(pBt, (Pgno)iTa
154e0 62 6c 65 2c 20 26 70 50 61 67 65 2c 20 30 29 3b ble, &pPage, 0);
154e1 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
154e2 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 rn rc;. rc = sq
154e3 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 lite3BtreeClearT
154e4 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 able(p, iTable,
154e5 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 0);. if( rc ){.
154e6 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
154e7 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 pPage);. retu
154e8 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 rn rc;. }.. *p
154e9 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 69 iMoved = 0;.. i
154ea 66 28 20 69 54 61 62 6c 65 3e 31 20 29 7b 0a 23 f( iTable>1 ){.#
154eb 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
154ec 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
154ed 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 rc = freePage(p
154ee 50 61 67 65 29 3b 0a 20 20 20 20 72 65 6c 65 61 Page);. relea
154ef 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 23 sePage(pPage);.#
154f0 65 6c 73 65 0a 20 20 20 20 69 66 28 20 70 42 74 else. if( pBt
154f1 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
154f2 20 20 20 20 20 20 50 67 6e 6f 20 6d 61 78 52 6f Pgno maxRo
154f3 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 72 63 otPgno;. rc
154f4 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
154f5 65 74 4d 65 74 61 28 70 2c 20 34 2c 20 26 6d 61 etMeta(p, 4, &ma
154f6 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20 xRootPgno);.
154f7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
154f8 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
154f9 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
154fa 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
154fb 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 n rc;. }..
154fc 20 20 20 20 20 69 66 28 20 69 54 61 62 6c 65 3d if( iTable=
154fd 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a =maxRootPgno ){.
154fe 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
154ff 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 e table being dr
15500 6f 70 70 65 64 20 69 73 20 74 68 65 20 74 61 62 opped is the tab
15501 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 le with the larg
15502 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 est root-page.
15503 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 ** number
15504 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c in the database,
15505 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20 70 61 put the root pa
15506 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65 20 6c ge on the free l
15507 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20 2a 2f ist. . */
15508 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 72 . rc = fr
15509 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 eePage(pPage);.
1550a 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
1550b 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ge(pPage);.
1550c 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1550d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1550e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1550f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
15510 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 se{. /* T
15511 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 he table being d
15512 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f 74 20 ropped does not
15513 68 61 76 65 20 74 68 65 20 6c 61 72 67 65 73 74 have the largest
15514 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 root-page.
15515 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 ** number in
15516 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 53 6f the database. So
15517 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 74 move the page t
15518 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20 74 68 hat does into th
15519 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 61 e . ** ga
1551a 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 64 65 p left by the de
1551b 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67 65 2e leted root-page.
1551c 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1551d 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4d 6f MemPage *pMo
1551e 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 ve;. rele
1551f 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
15520 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
15521 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
15522 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e (pBt, maxRootPgn
15523 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 o, &pMove, 0);.
15524 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
15525 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
15526 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
15527 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
15528 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 rc = relocate
15529 50 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c Page(pBt, pMove,
1552a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 PTRMAP_ROOTPAGE
1552b 2c 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b , 0, iTable, 0);
1552c 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
1552d 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 Page(pMove);.
1552e 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1552f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
15530 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
15531 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15532 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
15533 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6d eeGetPage(pBt, m
15534 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f axRootPgno, &pMo
15535 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 ve, 0);.
15536 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15537 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
15538 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
15539 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d }. rc =
1553a 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76 65 29 freePage(pMove)
1553b 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 ;. releas
1553c 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 ePage(pMove);.
1553d 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1553e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1553f 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
15540 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15541 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78 *piMoved = max
15542 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 RootPgno;.
15543 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 }.. /* Set
15544 74 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f the new 'max-roo
15545 74 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e t-page' value in
15546 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65 the database he
15547 61 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20 ader. This.
15548 20 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76 ** is the old v
15549 61 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c alue less one, l
1554a 65 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20 ess one more if
1554b 74 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a that happens to.
1554c 20 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f ** be a ro
1554d 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 ot-page number,
1554e 6c 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69 less one again i
1554f 66 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20 f that is the.
15550 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 ** PENDING_B
15551 59 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20 YTE_PAGE..
15552 2a 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 */. maxRoot
15553 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 69 66 Pgno--;. if
15554 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 ( maxRootPgno==P
15555 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
15556 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 (pBt) ){.
15557 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a maxRootPgno--;.
15558 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
15559 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 ( maxRootPgno==P
1555a 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
1555b 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20 29 , maxRootPgno) )
1555c 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f 6f {. maxRoo
1555d 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d tPgno--;. }
1555e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d . assert( m
1555f 61 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e 44 axRootPgno!=PEND
15560 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
15561 74 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63 20 t) );.. rc
15562 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 = sqlite3BtreeUp
15563 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 6d dateMeta(p, 4, m
15564 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 axRootPgno);.
15565 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
15566 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67 = freePage(pPag
15567 65 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 e);. releas
15568 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
15569 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c }.#endif. }el
1556a 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 se{. /* If sq
1556b 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 lite3BtreeDropTa
1556c 62 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20 6f ble was called o
1556d 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20 20 20 n page 1. */.
1556e 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c zeroPage(pPage,
1556f 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f PTF_INTKEY|PTF_
15570 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65 6c 65 LEAF );. rele
15571 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
15572 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
15573 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 .}.SQLITE_PRIV
15574 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
15575 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 treeDropTable(Bt
15576 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 ree *p, int iTab
15577 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 le, int *piMoved
15578 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
15579 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1557a 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 (p);. p->pBt->d
1557b 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63 20 b = p->db;. rc
1557c 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 = btreeDropTable
1557d 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f (p, iTable, piMo
1557e 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 ved);. sqlite3B
1557f 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
15580 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
15581 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d 65 *.** Read the me
15582 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f ta-information o
15583 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 ut of a database
15584 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d 0a file. Meta[0].
15585 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ** is the number
15586 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 63 of free pages c
15587 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 urrently in the
15588 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b database. Meta[
15589 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d 65 1].** through me
1558a 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69 6c ta[15] are avail
1558b 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 20 able for use by
1558c 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20 20 higher layers.
1558d 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 65 Meta[0].** is re
1558e 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 68 ad-only, the oth
1558f 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 72 69 ers are read/wri
15590 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 73 te..** .** The s
15591 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d 62 chema layer numb
15592 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73 20 ers meta values
15593 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 74 differently. At
15594 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20 6c the schema.** l
15595 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 53 65 ayer (and the Se
15596 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61 64 tCookie and Read
15597 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29 20 Cookie opcodes)
15598 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
15599 20 66 72 65 65 20 70 61 67 65 73 20 69 73 20 6e free pages is n
1559a 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f 20 ot visible. So
1559b 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68 65 Cookie[0] is the
1559c 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31 5d same as Meta[1]
1559d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1559e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1559f 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 treeGetMeta(Btre
155a0 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 e *p, int idx, u
155a1 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20 44 62 32 *pMeta){. Db
155a2 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 Page *pDbPage;.
155a3 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 int rc;. unsig
155a4 6e 65 64 20 63 68 61 72 20 2a 70 50 31 3b 0a 20 ned char *pP1;.
155a5 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
155a6 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 p->pBt;.. sqli
155a7 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
155a8 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d ;. pBt->db = p-
155a9 3e 64 62 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 69 >db;.. /* Readi
155aa 6e 67 20 61 20 6d 65 74 61 2d 64 61 74 61 20 76 ng a meta-data v
155ab 61 6c 75 65 20 72 65 71 75 69 72 65 73 20 61 20 alue requires a
155ac 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70 61 67 read-lock on pag
155ad 65 20 31 20 28 61 6e 64 20 68 65 6e 63 65 0a 20 e 1 (and hence.
155ae 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 6d ** the sqlite_m
155af 61 73 74 65 72 20 74 61 62 6c 65 2e 20 57 65 20 aster table. We
155b0 67 72 61 62 20 74 68 69 73 20 6c 6f 63 6b 20 72 grab this lock r
155b1 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 egardless of whe
155b2 74 68 65 72 20 6f 72 0a 20 20 2a 2a 20 6e 6f 74 ther or. ** not
155b3 20 74 68 65 20 53 51 4c 49 54 45 5f 52 65 61 64 the SQLITE_Read
155b4 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 Uncommitted flag
155b5 20 69 73 20 73 65 74 20 28 74 68 65 20 74 61 62 is set (the tab
155b6 6c 65 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 le rooted at pag
155b7 65 0a 20 20 2a 2a 20 31 20 69 73 20 74 72 65 61 e. ** 1 is trea
155b8 74 65 64 20 61 73 20 61 20 73 70 65 63 69 61 6c ted as a special
155b9 20 63 61 73 65 20 62 79 20 71 75 65 72 79 54 61 case by queryTa
155ba 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64 20 6c 6f bleLock() and lo
155bb 63 6b 54 61 62 6c 65 28 29 29 2e 0a 20 20 2a 2f ckTable()).. */
155bc 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54 61 62 . rc = queryTab
155bd 6c 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41 leLock(p, 1, REA
155be 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 D_LOCK);. if( r
155bf 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
155c0 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
155c1 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 Leave(p);. re
155c2 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
155c3 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 assert( idx>=0 &
155c4 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 69 & idx<=15 );. i
155c5 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 f( pBt->pPage1 )
155c6 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74 {. /* The b-t
155c7 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 ree is already h
155c8 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e olding a referen
155c9 63 65 20 74 6f 20 70 61 67 65 20 31 20 6f 66 20 ce to page 1 of
155ca 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 the database.
155cb 20 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69 ** file. In thi
155cc 73 20 63 61 73 65 20 74 68 65 20 72 65 71 75 69 s case the requi
155cd 72 65 64 20 6d 65 74 61 2d 64 61 74 61 20 76 61 red meta-data va
155ce 6c 75 65 20 63 61 6e 20 62 65 20 72 65 61 64 20 lue can be read
155cf 64 69 72 65 63 74 6c 79 0a 20 20 20 20 2a 2a 20 directly. **
155d0 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 64 61 from the page da
155d1 74 61 20 6f 66 20 74 68 69 73 20 72 65 66 65 72 ta of this refer
155d2 65 6e 63 65 2e 20 54 68 69 73 20 69 73 20 73 6c ence. This is sl
155d3 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 ightly faster th
155d4 61 6e 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 73 an. ** reques
155d5 74 69 6e 67 20 61 20 6e 65 77 20 72 65 66 65 72 ting a new refer
155d6 65 6e 63 65 20 66 72 6f 6d 20 74 68 65 20 70 61 ence from the pa
155d7 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 2a ger layer.. *
155d8 2f 0a 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73 /. pP1 = (uns
155d9 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 42 74 igned char *)pBt
155da 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b ->pPage1->aData;
155db 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
155dc 20 54 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73 The b-tree does
155dd 20 6e 6f 74 20 68 61 76 65 20 61 20 72 65 66 65 not have a refe
155de 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
155df 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
155e0 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 20 4f 62 74 file.. ** Obt
155e1 61 69 6e 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 ain one from the
155e2 20 70 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 pager layer..
155e3 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
155e4 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 lite3PagerGet(pB
155e5 74 2d 3e 70 50 61 67 65 72 2c 20 31 2c 20 26 70 t->pPager, 1, &p
155e6 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 DbPage);. if(
155e7 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c rc ){. sql
155e8 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
155e9 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
155ea 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 rc;. }. pP
155eb 31 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 1 = (unsigned ch
155ec 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 ar *)sqlite3Page
155ed 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
155ee 29 3b 0a 20 20 7d 0a 20 20 2a 70 4d 65 74 61 20 );. }. *pMeta
155ef 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 31 5b = get4byte(&pP1[
155f0 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 36 + idx*4]);..
155f1 20 2f 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 /* If the b-tre
155f2 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69 6e 67 e is not holding
155f3 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
155f4 70 61 67 65 20 31 2c 20 74 68 65 6e 20 6f 6e 65 page 1, then one
155f5 20 77 61 73 20 0a 20 20 2a 2a 20 72 65 71 75 65 was . ** reque
155f6 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 sted from the pa
155f7 67 65 72 20 6c 61 79 65 72 20 69 6e 20 74 68 65 ger layer in the
155f8 20 61 62 6f 76 65 20 62 6c 6f 63 6b 2e 20 52 65 above block. Re
155f9 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 lease it now..
155fa 2a 2f 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 */. if( !pBt->p
155fb 50 61 67 65 31 20 29 7b 0a 20 20 20 20 73 71 6c Page1 ){. sql
155fc 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
155fd 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 DbPage);. }..
155fe 2f 2a 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d /* If autovacuum
155ff 65 64 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 ed is disabled i
15600 6e 20 74 68 69 73 20 62 75 69 6c 64 20 62 75 74 n this build but
15601 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 we are trying t
15602 6f 20 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61 o . ** access a
15603 6e 20 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64 n autovacuumed d
15604 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61 atabase, then ma
15605 6b 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ke the database
15606 72 65 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a readonly. . */.
15607 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
15608 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
15609 69 66 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70 if( idx==4 && *p
1560a 4d 65 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 Meta>0 ) pBt->re
1560b 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 adOnly = 1;.#end
1560c 69 66 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 if.. /* Grab th
1560d 65 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70 e read-lock on p
1560e 61 67 65 20 31 2e 20 2a 2f 0a 20 20 72 63 20 3d age 1. */. rc =
1560f 20 6c 6f 63 6b 54 61 62 6c 65 28 70 2c 20 31 2c lockTable(p, 1,
15610 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 73 READ_LOCK);. s
15611 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
15612 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
15613 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
15614 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f meta-informatio
15615 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 n back into the
15616 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b database. Meta[
15617 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 0] is.** read-on
15618 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 ly and may not b
15619 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 e written..*/.SQ
1561a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1561b 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
1561c 61 74 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70 ateMeta(Btree *p
1561d 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69 , int idx, u32 i
1561e 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65 Meta){. BtShare
1561f 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
15620 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
15621 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b *pP1;. int rc;
15622 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d . assert( idx>=
15623 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 1 && idx<=15 );.
15624 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15625 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 ter(p);. pBt->d
15626 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 b = p->db;. if(
15627 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 p->inTrans!=TRA
15628 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 NS_WRITE ){.
15629 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e rc = pBt->readOn
1562a 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 ly ? SQLITE_READ
1562b 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 ONLY : SQLITE_ER
1562c 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ROR;. }else{.
1562d 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 assert( pBt->p
1562e 50 61 67 65 31 21 3d 30 20 29 3b 0a 20 20 20 20 Page1!=0 );.
1562f 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 pP1 = pBt->pPage
15630 31 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 72 63 1->aData;. rc
15631 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
15632 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 rite(pBt->pPage1
15633 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
15634 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
15635 4b 20 29 7b 0a 20 20 20 20 20 20 70 75 74 34 62 K ){. put4b
15636 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20 69 64 yte(&pP1[36 + id
15637 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a 23 69 x*4], iMeta);.#i
15638 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15639 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
1563a 20 20 20 69 66 28 20 69 64 78 3d 3d 37 20 29 7b if( idx==7 ){
1563b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1563c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
1563d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a || iMeta==0 );.
1563e 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1563f 69 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 iMeta==0 || iMet
15640 61 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 a==1 );.
15641 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
15642 3d 20 69 4d 65 74 61 3b 0a 20 20 20 20 20 20 7d = iMeta;. }
15643 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 .#endif. }.
15644 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 }. sqlite3Btree
15645 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
15646 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
15647 52 65 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20 Return the flag
15648 62 79 74 65 20 61 74 20 74 68 65 20 62 65 67 69 byte at the begi
15649 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 nning of the pag
1564a 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f e that the curso
1564b 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c r.** is currentl
1564c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a y pointing to..*
1564d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1564e 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1564f 65 46 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20 eFlags(BtCursor
15650 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 *pCur){. /* TOD
15651 4f 3a 20 57 68 61 74 20 61 62 6f 75 74 20 43 55 O: What about CU
15652 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
15653 20 73 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79 state? Probably
15654 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20 need to call.
15655 2a 2a 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 ** restoreCursor
15656 50 6f 73 69 74 69 6f 6e 28 29 20 68 65 72 65 2e Position() here.
15657 0a 20 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 . */. MemPage
15658 2a 70 50 61 67 65 3b 0a 20 20 72 65 73 74 6f 72 *pPage;. restor
15659 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
1565a 70 43 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d pCur);. pPage =
1565b 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1565c 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 ur->iPage];. as
1565d 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
1565e 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
1565f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15660 3e 70 42 74 3d 3d 70 43 75 72 2d 3e 70 42 74 20 >pBt==pCur->pBt
15661 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 );. return pPag
15662 65 20 3f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 e ? pPage->aData
15663 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
15664 74 5d 20 3a 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a t] : 0;.}.../*.*
15665 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 * Return the pag
15666 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 er associated wi
15667 74 68 20 61 20 42 54 72 65 65 2e 20 20 54 68 69 th a BTree. Thi
15668 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
15669 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 d for.** testing
1566a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f and debugging o
1566b 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nly..*/.SQLITE_P
1566c 52 49 56 41 54 45 20 50 61 67 65 72 20 2a 73 71 RIVATE Pager *sq
1566d 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 lite3BtreePager(
1566e 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 Btree *p){. ret
1566f 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 urn p->pBt->pPag
15670 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 er;.}..#ifndef S
15671 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
15672 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a RITY_CHECK./*.**
15673 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73 61 67 Append a messag
15674 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d e to the error m
15675 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a essage string..*
15676 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 /.static void ch
15677 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a 20 20 eckAppendMsg(.
15678 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 IntegrityCk *pCh
15679 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a 4d 73 eck,. char *zMs
1567a 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 g1,. const char
1567b 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e 2e 2e *zFormat,. ...
1567c 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 .){. va_list ap
1567d 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63 6b 2d ;. if( !pCheck-
1567e 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72 6e 3b >mxErr ) return;
1567f 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 . pCheck->mxErr
15680 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6e 45 --;. pCheck->nE
15681 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 72 74 rr++;. va_start
15682 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
15683 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72 72 if( pCheck->err
15684 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20 20 20 Msg.nChar ){.
15685 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
15686 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e Append(&pCheck->
15687 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 31 29 errMsg, "\n", 1)
15688 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d 73 67 ;. }. if( zMsg
15689 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 1 ){. sqlite3
1568a 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 StrAccumAppend(&
1568b 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 pCheck->errMsg,
1568c 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20 7d 0a zMsg1, -1);. }.
1568d 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 sqlite3VXPrint
1568e 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 f(&pCheck->errMs
1568f 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 g, 1, zFormat, a
15690 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 p);. va_end(ap)
15691 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e ;. if( pCheck->
15692 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 61 69 errMsg.mallocFai
15693 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68 65 63 led ){. pChec
15694 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 k->mallocFailed
15695 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 = 1;. }.}.#endi
15696 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
15697 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
15698 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
15699 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 ITE_OMIT_INTEGRI
1569a 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 TY_CHECK./*.** A
1569b 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 66 65 dd 1 to the refe
1569c 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 rence count for
1569d 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 66 20 page iPage. If
1569e 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f this is the seco
1569f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 nd.** reference
156a0 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 64 64 to the page, add
156a1 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
156a2 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a 45 72 e to pCheck->zEr
156a3 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 rMsg..** Return
156a4 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20 32 1 if there are 2
156a5 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 72 65 ore more refere
156a6 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 nces to the page
156a7 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 66 20 and 0 if.** if
156a8 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 this is the firs
156a9 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 t reference to t
156aa 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 he page..**.** A
156ab 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74 lso check that t
156ac 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 he page number i
156ad 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a 2f 0a s in bounds..*/.
156ae 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b static int check
156af 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 6b 20 Ref(IntegrityCk
156b0 2a 70 43 68 65 63 6b 2c 20 69 6e 74 20 69 50 61 *pCheck, int iPa
156b1 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 ge, char *zConte
156b2 78 74 29 7b 0a 20 20 69 66 28 20 69 50 61 67 65 xt){. if( iPage
156b3 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a ==0 ) return 1;.
156b4 20 20 69 66 28 20 69 50 61 67 65 3e 70 43 68 65 if( iPage>pChe
156b5 63 6b 2d 3e 6e 50 61 67 65 20 7c 7c 20 69 50 61 ck->nPage || iPa
156b6 67 65 3c 30 20 29 7b 0a 20 20 20 20 63 68 65 63 ge<0 ){. chec
156b7 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
156b8 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 69 6e k, zContext, "in
156b9 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 65 valid page numbe
156ba 72 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 r %d", iPage);.
156bb 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
156bc 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 61 . if( pCheck->a
156bd 6e 52 65 66 5b 69 50 61 67 65 5d 3d 3d 31 20 29 nRef[iPage]==1 )
156be 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e {. checkAppen
156bf 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f dMsg(pCheck, zCo
156c0 6e 74 65 78 74 2c 20 22 32 6e 64 20 72 65 66 65 ntext, "2nd refe
156c1 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 25 64 rence to page %d
156c2 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 ", iPage);. r
156c3 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 eturn 1;. }. r
156c4 65 74 75 72 6e 20 20 28 70 43 68 65 63 6b 2d 3e eturn (pCheck->
156c5 61 6e 52 65 66 5b 69 50 61 67 65 5d 2b 2b 29 3e anRef[iPage]++)>
156c6 31 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 1;.}..#ifndef SQ
156c7 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
156c8 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b CUUM./*.** Check
156c9 20 74 68 61 74 20 74 68 65 20 65 6e 74 72 79 20 that the entry
156ca 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d in the pointer-m
156cb 61 70 20 66 6f 72 20 70 61 67 65 20 69 43 68 69 ap for page iChi
156cc 6c 64 20 6d 61 70 73 20 74 6f 20 0a 2a 2a 20 70 ld maps to .** p
156cd 61 67 65 20 69 50 61 72 65 6e 74 2c 20 70 6f 69 age iParent, poi
156ce 6e 74 65 72 20 74 79 70 65 20 70 74 72 54 79 70 nter type ptrTyp
156cf 65 2e 20 49 66 20 6e 6f 74 2c 20 61 70 70 65 6e e. If not, appen
156d0 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 d an error messa
156d1 67 65 0a 2a 2a 20 74 6f 20 70 43 68 65 63 6b 2e ge.** to pCheck.
156d2 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
156d3 63 68 65 63 6b 50 74 72 6d 61 70 28 0a 20 20 49 checkPtrmap(. I
156d4 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 ntegrityCk *pChe
156d5 63 6b 2c 20 20 20 2f 2a 20 49 6e 74 65 67 72 69 ck, /* Integri
156d6 74 79 20 63 68 65 63 6b 20 63 6f 6e 74 65 78 74 ty check context
156d7 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c */. Pgno iChil
156d8 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 d, /*
156d9 43 68 69 6c 64 20 70 61 67 65 20 6e 75 6d 62 65 Child page numbe
156da 72 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c r */. u8 eType,
156db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
156dc 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 65 Expected pointe
156dd 72 20 6d 61 70 20 74 79 70 65 20 2a 2f 0a 20 20 r map type */.
156de 50 67 6e 6f 20 69 50 61 72 65 6e 74 2c 20 20 20 Pgno iParent,
156df 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 /* Expect
156e0 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 ed pointer map p
156e1 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 arent page numbe
156e2 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f r */. char *zCo
156e3 6e 74 65 78 74 20 20 20 20 20 20 20 20 20 2f 2a ntext /*
156e4 20 43 6f 6e 74 65 78 74 20 64 65 73 63 72 69 70 Context descrip
156e5 74 69 6f 6e 20 28 75 73 65 64 20 66 6f 72 20 65 tion (used for e
156e6 72 72 6f 72 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a rror msg) */.){.
156e7 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 38 20 65 int rc;. u8 e
156e8 50 74 72 6d 61 70 54 79 70 65 3b 0a 20 20 50 67 PtrmapType;. Pg
156e9 6e 6f 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 no iPtrmapParent
156ea 3b 0a 0a 20 20 72 63 20 3d 20 70 74 72 6d 61 70 ;.. rc = ptrmap
156eb 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 42 74 2c Get(pCheck->pBt,
156ec 20 69 43 68 69 6c 64 2c 20 26 65 50 74 72 6d 61 iChild, &ePtrma
156ed 70 54 79 70 65 2c 20 26 69 50 74 72 6d 61 70 50 pType, &iPtrmapP
156ee 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 arent);. if( rc
156ef 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
156f0 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
156f1 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 g(pCheck, zConte
156f2 78 74 2c 20 22 46 61 69 6c 65 64 20 74 6f 20 72 xt, "Failed to r
156f3 65 61 64 20 70 74 72 6d 61 70 20 6b 65 79 3d 25 ead ptrmap key=%
156f4 64 22 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 d", iChild);.
156f5 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 return;. }..
156f6 69 66 28 20 65 50 74 72 6d 61 70 54 79 70 65 21 if( ePtrmapType!
156f7 3d 65 54 79 70 65 20 7c 7c 20 69 50 74 72 6d 61 =eType || iPtrma
156f8 70 50 61 72 65 6e 74 21 3d 69 50 61 72 65 6e 74 pParent!=iParent
156f9 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 ){. checkApp
156fa 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
156fb 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 Context, .
156fc 22 42 61 64 20 70 74 72 20 6d 61 70 20 65 6e 74 "Bad ptr map ent
156fd 72 79 20 6b 65 79 3d 25 64 20 65 78 70 65 63 74 ry key=%d expect
156fe 65 64 3d 28 25 64 2c 25 64 29 20 67 6f 74 3d 28 ed=(%d,%d) got=(
156ff 25 64 2c 25 64 29 22 2c 20 0a 20 20 20 20 20 20 %d,%d)", .
15700 69 43 68 69 6c 64 2c 20 65 54 79 70 65 2c 20 69 iChild, eType, i
15701 50 61 72 65 6e 74 2c 20 65 50 74 72 6d 61 70 54 Parent, ePtrmapT
15702 79 70 65 2c 20 69 50 74 72 6d 61 70 50 61 72 65 ype, iPtrmapPare
15703 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 nt);. }.}.#endi
15704 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 f../*.** Check t
15705 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 he integrity of
15706 74 68 65 20 66 72 65 65 6c 69 73 74 20 6f 72 20 the freelist or
15707 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 of an overflow p
15708 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 20 56 65 72 age list..** Ver
15709 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 6d ify that the num
1570a 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 ber of pages on
1570b 74 68 65 20 6c 69 73 74 20 69 73 20 4e 2e 0a 2a the list is N..*
1570c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 /.static void ch
1570d 65 63 6b 4c 69 73 74 28 0a 20 20 49 6e 74 65 67 eckList(. Integ
1570e 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 rityCk *pCheck,
1570f 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 /* Integrity ch
15710 65 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a ecking context *
15711 2f 0a 20 20 69 6e 74 20 69 73 46 72 65 65 4c 69 /. int isFreeLi
15712 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 st, /* Tru
15713 65 20 66 6f 72 20 61 20 66 72 65 65 6c 69 73 74 e for a freelist
15714 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 6f 76 65 . False for ove
15715 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 20 rflow page list
15716 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20 */. int iPage,
15717 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
15718 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 66 69 ge number for fi
15719 72 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 rst page in the
1571a 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c list */. int N,
1571b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1571c 2f 2a 20 45 78 70 65 63 74 65 64 20 6e 75 6d 62 /* Expected numb
1571d 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
1571e 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 63 68 61 he list */. cha
1571f 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 20 20 r *zContext
15720 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f /* Context fo
15721 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 r error messages
15722 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a */.){. int i;.
15723 20 20 69 6e 74 20 65 78 70 65 63 74 65 64 20 3d int expected =
15724 20 4e 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74 N;. int iFirst
15725 20 3d 20 69 50 61 67 65 3b 0a 20 20 77 68 69 6c = iPage;. whil
15726 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 20 70 43 e( N-- > 0 && pC
15727 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 7b 0a 20 heck->mxErr ){.
15728 20 20 20 44 62 50 61 67 65 20 2a 70 4f 76 66 6c DbPage *pOvfl
15729 50 61 67 65 3b 0a 20 20 20 20 75 6e 73 69 67 6e Page;. unsign
1572a 65 64 20 63 68 61 72 20 2a 70 4f 76 66 6c 44 61 ed char *pOvflDa
1572b 74 61 3b 0a 20 20 20 20 69 66 28 20 69 50 61 67 ta;. if( iPag
1572c 65 3c 31 20 29 7b 0a 20 20 20 20 20 20 63 68 65 e<1 ){. che
1572d 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
1572e 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 ck, zContext,.
1572f 20 20 20 20 20 20 20 22 25 64 20 6f 66 20 25 64 "%d of %d
15730 20 70 61 67 65 73 20 6d 69 73 73 69 6e 67 20 66 pages missing f
15731 72 6f 6d 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 rom overflow lis
15732 74 20 73 74 61 72 74 69 6e 67 20 61 74 20 25 64 t starting at %d
15733 22 2c 0a 20 20 20 20 20 20 20 20 20 20 4e 2b 31 ",. N+1
15734 2c 20 65 78 70 65 63 74 65 64 2c 20 69 46 69 72 , expected, iFir
15735 73 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b st);. break
15736 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
15737 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c checkRef(pCheck,
15738 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 iPage, zContext
15739 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 ) ) break;. i
1573a 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 f( sqlite3PagerG
1573b 65 74 28 70 43 68 65 63 6b 2d 3e 70 50 61 67 65 et(pCheck->pPage
1573c 72 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20 r, (Pgno)iPage,
1573d 26 70 4f 76 66 6c 50 61 67 65 29 20 29 7b 0a 20 &pOvflPage) ){.
1573e 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
1573f 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
15740 74 65 78 74 2c 20 22 66 61 69 6c 65 64 20 74 6f text, "failed to
15741 20 67 65 74 20 70 61 67 65 20 25 64 22 2c 20 69 get page %d", i
15742 50 61 67 65 29 3b 0a 20 20 20 20 20 20 62 72 65 Page);. bre
15743 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f ak;. }. pO
15744 76 66 6c 44 61 74 61 20 3d 20 28 75 6e 73 69 67 vflData = (unsig
15745 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 74 ned char *)sqlit
15746 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
15747 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 20 20 69 OvflPage);. i
15748 66 28 20 69 73 46 72 65 65 4c 69 73 74 20 29 7b f( isFreeList ){
15749 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 67 . int n = g
1574a 65 74 34 62 79 74 65 28 26 70 4f 76 66 6c 44 61 et4byte(&pOvflDa
1574b 74 61 5b 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20 ta[4]);.#ifndef
1574c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1574d 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 VACUUM. if(
1574e 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 pCheck->pBt->au
1574f 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
15750 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 checkPtrmap(
15751 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 50 pCheck, iPage, P
15752 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 TRMAP_FREEPAGE,
15753 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 0, zContext);.
15754 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
15755 20 20 20 69 66 28 20 6e 3e 70 43 68 65 63 6b 2d if( n>pCheck-
15756 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
15757 2f 34 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 /4-2 ){.
15758 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
15759 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
1575a 0a 20 20 20 20 20 20 20 20 20 20 20 22 66 72 65 . "fre
1575b 65 6c 69 73 74 20 6c 65 61 66 20 63 6f 75 6e 74 elist leaf count
1575c 20 74 6f 6f 20 62 69 67 20 6f 6e 20 70 61 67 65 too big on page
1575d 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 %d", iPage);.
1575e 20 20 20 20 20 20 4e 2d 2d 3b 0a 20 20 20 20 20 N--;.
1575f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
15760 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b for(i=0; i<n; i+
15761 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 67 +){. Pg
15762 6e 6f 20 69 46 72 65 65 50 61 67 65 20 3d 20 67 no iFreePage = g
15763 65 74 34 62 79 74 65 28 26 70 4f 76 66 6c 44 61 et4byte(&pOvflDa
15764 74 61 5b 38 2b 69 2a 34 5d 29 3b 0a 23 69 66 6e ta[8+i*4]);.#ifn
15765 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
15766 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
15767 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d if( pCheck-
15768 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d >pBt->autoVacuum
15769 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1576a 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 checkPtrmap(pChe
1576b 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 50 ck, iFreePage, P
1576c 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 TRMAP_FREEPAGE,
1576d 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 0, zContext);.
1576e 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }.#endif
1576f 0a 20 20 20 20 20 20 20 20 20 20 63 68 65 63 6b . check
15770 52 65 66 28 70 43 68 65 63 6b 2c 20 69 46 72 65 Ref(pCheck, iFre
15771 65 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 ePage, zContext)
15772 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
15773 20 20 20 20 4e 20 2d 3d 20 6e 3b 0a 20 20 20 20 N -= n;.
15774 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 }. }.#ifnde
15775 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
15776 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 65 6c 73 TOVACUUM. els
15777 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 e{. /* If t
15778 68 69 73 20 64 61 74 61 62 61 73 65 20 73 75 70 his database sup
15779 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 ports auto-vacuu
1577a 6d 20 61 6e 64 20 69 50 61 67 65 20 69 73 20 6e m and iPage is n
1577b 6f 74 20 74 68 65 20 6c 61 73 74 0a 20 20 20 20 ot the last.
1577c 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 69 ** page in thi
1577d 73 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 2c s overflow list,
1577e 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 check that the
1577f 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
15780 79 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 y for. ** t
15781 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 67 he following pag
15782 65 20 6d 61 74 63 68 65 73 20 69 50 61 67 65 2e e matches iPage.
15783 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
15784 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d if( pCheck->pBt-
15785 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 4e >autoVacuum && N
15786 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20 >0 ){. i
15787 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c = get4byte(pOvfl
15788 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 63 Data);. c
15789 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 heckPtrmap(pChec
1578a 6b 2c 20 69 2c 20 50 54 52 4d 41 50 5f 4f 56 45 k, i, PTRMAP_OVE
1578b 52 46 4c 4f 57 32 2c 20 69 50 61 67 65 2c 20 7a RFLOW2, iPage, z
1578c 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
1578d 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
1578e 20 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62 iPage = get4b
1578f 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a yte(pOvflData);.
15790 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
15791 55 6e 72 65 66 28 70 4f 76 66 6c 50 61 67 65 29 Unref(pOvflPage)
15792 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f ;. }.}.#endif /
15793 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e * SQLITE_OMIT_IN
15794 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f TEGRITY_CHECK */
15795 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
15796 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
15797 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 CHECK./*.** Do v
15798 61 72 69 6f 75 73 20 73 61 6e 69 74 79 20 63 68 arious sanity ch
15799 65 63 6b 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 ecks on a single
1579a 20 70 61 67 65 20 6f 66 20 61 20 74 72 65 65 2e page of a tree.
1579b 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 Return.** the
1579c 74 72 65 65 20 64 65 70 74 68 2e 20 20 52 6f 6f tree depth. Roo
1579d 74 20 70 61 67 65 73 20 72 65 74 75 72 6e 20 30 t pages return 0
1579e 2e 20 20 50 61 72 65 6e 74 73 20 6f 66 20 72 6f . Parents of ro
1579f 6f 74 20 70 61 67 65 73 0a 2a 2a 20 72 65 74 75 ot pages.** retu
157a0 72 6e 20 31 2c 20 61 6e 64 20 73 6f 20 66 6f 72 rn 1, and so for
157a1 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 73 65 th..** .** These
157a2 20 63 68 65 63 6b 73 20 61 72 65 20 64 6f 6e 65 checks are done
157a3 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 31 2e 20 :.**.** 1.
157a4 20 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 Make sure that
157a5 63 65 6c 6c 73 20 61 6e 64 20 66 72 65 65 62 6c cells and freebl
157a6 6f 63 6b 73 20 64 6f 20 6e 6f 74 20 6f 76 65 72 ocks do not over
157a7 6c 61 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 lap.**
157a8 62 75 74 20 63 6f 6d 62 69 6e 65 20 74 6f 20 63 but combine to c
157a9 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 76 65 72 20 ompletely cover
157aa 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 20 4e 4f the page..** NO
157ab 20 20 32 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 2. Make sure
157ac 63 65 6c 6c 20 6b 65 79 73 20 61 72 65 20 69 6e cell keys are in
157ad 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 4e 4f 20 20 order..** NO
157ae 33 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 3. Make sure no
157af 20 6b 65 79 20 69 73 20 6c 65 73 73 20 74 68 61 key is less tha
157b0 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 4c n or equal to zL
157b1 6f 77 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 4e owerBound..** N
157b2 4f 20 20 34 2e 20 20 4d 61 6b 65 20 73 75 72 65 O 4. Make sure
157b3 20 6e 6f 20 6b 65 79 20 69 73 20 67 72 65 61 74 no key is great
157b4 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
157b5 20 74 6f 20 7a 55 70 70 65 72 42 6f 75 6e 64 2e to zUpperBound.
157b6 0a 2a 2a 20 20 20 20 20 20 35 2e 20 20 43 68 65 .** 5. Che
157b7 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 ck the integrity
157b8 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 of overflow pag
157b9 65 73 2e 0a 2a 2a 20 20 20 20 20 20 36 2e 20 20 es..** 6.
157ba 52 65 63 75 72 73 69 76 65 6c 79 20 63 61 6c 6c Recursively call
157bb 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 20 6f checkTreePage o
157bc 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 2e 0a n all children..
157bd 2a 2a 20 20 20 20 20 20 37 2e 20 20 56 65 72 69 ** 7. Veri
157be 66 79 20 74 68 61 74 20 74 68 65 20 64 65 70 74 fy that the dept
157bf 68 20 6f 66 20 61 6c 6c 20 63 68 69 6c 64 72 65 h of all childre
157c0 6e 20 69 73 20 74 68 65 20 73 61 6d 65 2e 0a 2a n is the same..*
157c1 2a 20 20 20 20 20 20 38 2e 20 20 4d 61 6b 65 20 * 8. Make
157c2 73 75 72 65 20 74 68 69 73 20 70 61 67 65 20 69 sure this page i
157c3 73 20 61 74 20 6c 65 61 73 74 20 33 33 25 20 66 s at least 33% f
157c4 75 6c 6c 20 6f 72 20 65 6c 73 65 20 69 74 20 69 ull or else it i
157c5 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 s.** th
157c6 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 72 e root of the tr
157c7 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ee..*/.static in
157c8 74 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 t checkTreePage(
157c9 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a . IntegrityCk *
157ca 70 43 68 65 63 6b 2c 20 20 2f 2a 20 43 6f 6e 74 pCheck, /* Cont
157cb 65 78 74 20 66 6f 72 20 74 68 65 20 73 61 6e 69 ext for the sani
157cc 74 79 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e ty check */. in
157cd 74 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 t iPage,
157ce 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
157cf 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 er of the page t
157d0 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 4d 65 6d o check */. Mem
157d1 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 20 Page *pParent,
157d2 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 70 61 67 /* Parent pag
157d3 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 e */. char *zPa
157d4 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 rentContext /*
157d5 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a Parent context *
157d6 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a /.){. MemPage *
157d7 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 pPage;. int i,
157d8 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 70 rc, depth, d2, p
157d9 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 gno, cnt;. int
157da 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a hdr, cellStart;.
157db 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 int nCell;. u
157dc 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61 8 *data;. BtSha
157dd 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 red *pBt;. int
157de 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 usableSize;. ch
157df 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d ar zContext[100]
157e0 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 3b 0a 0a ;. char *hit;..
157e1 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
157e2 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 tf(sizeof(zConte
157e3 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 xt), zContext, "
157e4 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61 67 Page %d: ", iPag
157e5 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 e);.. /* Check
157e6 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65 78 that the page ex
157e7 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 20 ists. */. pBt
157e8 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a 20 = pCheck->pBt;.
157e9 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 usableSize = pB
157ea 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 t->usableSize;.
157eb 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20 if( iPage==0 )
157ec 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
157ed 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c checkRef(pCheck,
157ee 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 43 iPage, zParentC
157ef 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72 6e ontext) ) return
157f0 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 0;. if( (rc =
157f1 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
157f2 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 age(pBt, (Pgno)i
157f3 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 Page, &pPage, 0)
157f4 29 21 3d 30 20 29 7b 0a 20 20 20 20 63 68 65 63 )!=0 ){. chec
157f5 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
157f6 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 k, zContext,.
157f7 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 "unable to g
157f8 65 74 20 74 68 65 20 70 61 67 65 2e 20 65 72 72 et the page. err
157f9 6f 72 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 or code=%d", rc)
157fa 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
157fb 20 20 7d 0a 20 20 69 66 28 20 28 72 63 20 3d 20 }. if( (rc =
157fc 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
157fd 50 61 67 65 28 70 50 61 67 65 29 29 21 3d 30 20 Page(pPage))!=0
157fe 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 ){. checkAppe
157ff 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
15800 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 ontext, .
15801 20 20 20 20 20 20 20 20 20 20 20 20 22 73 71 6c "sql
15802 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
15803 65 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f e() returns erro
15804 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b r code %d", rc);
15805 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
15806 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 (pPage);. ret
15807 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 0;. }.. /*
15808 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 Check out all t
15809 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 he cells.. */.
1580a 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f depth = 0;. fo
1580b 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e r(i=0; i<pPage->
1580c 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d nCell && pCheck-
1580d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 >mxErr; i++){.
1580e 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 u8 *pCell;.
1580f 20 69 6e 74 20 73 7a 3b 0a 20 20 20 20 43 65 6c int sz;. Cel
15810 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 lInfo info;..
15811 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 /* Check payloa
15812 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 d overflow pages
15813 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
15814 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
15815 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a eof(zContext), z
15816 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 Context,.
15817 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 "On tree p
15818 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 age %d cell %d:
15819 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 ", iPage, i);.
1581a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
1581b 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 ll(pPage,i);.
1581c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
1581d 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
1581e 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
1581f 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 sz = info.nD
15820 61 74 61 3b 0a 20 20 20 20 69 66 28 20 21 70 50 ata;. if( !pP
15821 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a age->intKey ) sz
15822 20 2b 3d 20 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 += info.nKey;.
15823 20 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 assert( sz==i
15824 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a nfo.nPayload );.
15825 20 20 20 20 69 66 28 20 73 7a 3e 69 6e 66 6f 2e if( sz>info.
15826 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 nLocal ){.
15827 69 6e 74 20 6e 50 61 67 65 20 3d 20 28 73 7a 20 int nPage = (sz
15828 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 - info.nLocal +
15829 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 35 29 2f usableSize - 5)/
1582a 28 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 (usableSize - 4)
1582b 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e ;. Pgno pgn
1582c 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 oOvfl = get4byte
1582d 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
1582e 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66 6e 64 65 erflow]);.#ifnde
1582f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
15830 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 TOVACUUM. i
15831 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
15832 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 um ){. ch
15833 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b eckPtrmap(pCheck
15834 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d , pgnoOvfl, PTRM
15835 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 69 50 AP_OVERFLOW1, iP
15836 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a age, zContext);.
15837 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
15838 20 20 20 20 20 63 68 65 63 6b 4c 69 73 74 28 70 checkList(p
15839 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e 6f 4f 76 Check, 0, pgnoOv
1583a 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43 6f 6e 74 fl, nPage, zCont
1583b 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ext);. }..
1583c 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e 69 74 79 /* Check sanity
1583d 20 6f 66 20 6c 65 66 74 20 63 68 69 6c 64 20 70 of left child p
1583e 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 age.. */.
1583f 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
15840 20 29 7b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d ){. pgno =
15841 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 get4byte(pCell)
15842 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
15843 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
15844 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
15845 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 autoVacuum ){.
15846 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
15847 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 p(pCheck, pgno,
15848 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69 50 PTRMAP_BTREE, iP
15849 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a age, zContext);.
1584a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
1584b 20 20 20 20 20 64 32 20 3d 20 63 68 65 63 6b 54 d2 = checkT
1584c 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c 70 reePage(pCheck,p
1584d 67 6e 6f 2c 70 50 61 67 65 2c 7a 43 6f 6e 74 65 gno,pPage,zConte
1584e 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 xt);. if( i
1584f 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68 20 >0 && d2!=depth
15850 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
15851 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
15852 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 69 , zContext, "Chi
15853 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64 69 ld page depth di
15854 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20 7d ffers");. }
15855 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20 64 . depth = d
15856 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 2;. }. }. i
15857 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
15858 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 ){. pgno = ge
15859 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
1585a 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
1585b 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 73 ffset+8]);. s
1585c 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1585d 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 sizeof(zContext)
1585e 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 , zContext, .
1585f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15860 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 61 74 "On page %d at
15861 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 22 2c right child: ",
15862 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 iPage);.#ifndef
15863 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
15864 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 OVACUUM. if(
15865 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
15866 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 ){. checkPt
15867 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e rmap(pCheck, pgn
15868 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c o, PTRMAP_BTREE,
15869 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 iPage, 0);.
1586a 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 }.#endif. che
1586b 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 63 ckTreePage(pChec
1586c 6b 2c 20 70 67 6e 6f 2c 20 70 50 61 67 65 2c 20 k, pgno, pPage,
1586d 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 zContext);. }.
1586e 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 . /* Check for
1586f 63 6f 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67 complete coverag
15870 65 20 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20 e of the page.
15871 2a 2f 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 */. data = pPag
15872 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 e->aData;. hdr
15873 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 = pPage->hdrOffs
15874 65 74 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 et;. hit = sqli
15875 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 te3PageMalloc( p
15876 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a Bt->pageSize );.
15877 20 20 69 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a if( hit==0 ){.
15878 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c pCheck->mall
15879 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
1587a 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65 }else{. memse
1587b 74 28 68 69 74 2c 20 30 2c 20 75 73 61 62 6c 65 t(hit, 0, usable
1587c 53 69 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 Size );. mems
1587d 65 74 28 68 69 74 2c 20 31 2c 20 67 65 74 32 62 et(hit, 1, get2b
1587e 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
1587f 29 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 ));. nCell =
15880 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
15881 64 72 2b 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c dr+3]);. cell
15882 53 74 61 72 74 20 3d 20 68 64 72 20 2b 20 31 32 Start = hdr + 12
15883 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 - 4*pPage->leaf
15884 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
15885 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <nCell; i++){.
15886 20 20 20 20 69 6e 74 20 70 63 20 3d 20 67 65 74 int pc = get
15887 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 2byte(&data[cell
15888 53 74 61 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 Start+i*2]);.
15889 20 20 20 75 31 36 20 73 69 7a 65 20 3d 20 31 30 u16 size = 10
1588a 32 34 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 24;. int j;
1588b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 3d 75 . if( pc<=u
1588c 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 sableSize ){.
1588d 20 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c size = cell
1588e 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 SizePtr(pPage, &
1588f 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 data[pc]);.
15890 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70 63 }. if( (pc
15891 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 +size-1)>=usable
15892 53 69 7a 65 20 7c 7c 20 70 63 3c 30 20 29 7b 0a Size || pc<0 ){.
15893 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 checkApp
15894 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 endMsg(pCheck, 0
15895 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 , . "
15896 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 Corruption detec
15897 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f ted in cell %d o
15898 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 50 61 n page %d",i,iPa
15899 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c ge,0);. }el
1589a 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 se{. for(
1589b 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d j=pc+size-1; j>=
1589c 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b pc; j--) hit[j]+
1589d 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d +;. }. }
1589e 0a 20 20 20 20 66 6f 72 28 63 6e 74 3d 30 2c 20 . for(cnt=0,
1589f 69 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 i=get2byte(&data
158a0 5b 68 64 72 2b 31 5d 29 3b 20 69 3e 30 20 26 26 [hdr+1]); i>0 &&
158a1 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 20 26 26 i<usableSize &&
158a2 20 63 6e 74 3c 31 30 30 30 30 3b 20 0a 20 20 20 cnt<10000; .
158a3 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 29 7b 0a cnt++){.
158a4 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d int size =
158a5 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
158a6 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 i+2]);. int
158a7 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 j;. if( (i
158a8 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 +size-1)>=usable
158a9 53 69 7a 65 20 7c 7c 20 69 3c 30 20 29 7b 0a 20 Size || i<0 ){.
158aa 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 checkAppe
158ab 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c ndMsg(pCheck, 0,
158ac 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 . "
158ad 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 Corruption detec
158ae 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f ted in cell %d o
158af 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 50 61 n page %d",i,iPa
158b0 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c ge,0);. }el
158b1 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 se{. for(
158b2 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 69 j=i+size-1; j>=i
158b3 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b ; j--) hit[j]++;
158b4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
158b5 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
158b6 61 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 a[i]);. }.
158b7 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 69 3c for(i=cnt=0; i<
158b8 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b 2b 29 usableSize; i++)
158b9 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69 74 5b {. if( hit[
158ba 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 i]==0 ){.
158bb 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 cnt++;. }e
158bc 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d 3e 31 lse if( hit[i]>1
158bd 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
158be 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
158bf 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 k, 0,.
158c0 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73 20 66 "Multiple uses f
158c1 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20 70 61 or byte %d of pa
158c2 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61 67 65 ge %d", i, iPage
158c3 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b );. break
158c4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
158c5 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64 61 74 if( cnt!=dat
158c6 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20 20 20 a[hdr+7] ){.
158c7 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
158c8 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 (pCheck, 0, .
158c9 20 20 20 20 20 20 20 22 46 72 61 67 6d 65 6e 74 "Fragment
158ca 65 64 20 73 70 61 63 65 20 69 73 20 25 64 20 62 ed space is %d b
158cb 79 74 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 yte reported as
158cc 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a %d on page %d",.
158cd 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 64 cnt, d
158ce 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67 ata[hdr+7], iPag
158cf 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
158d0 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
158d1 68 69 74 29 3b 0a 0a 20 20 72 65 6c 65 61 73 65 hit);.. release
158d2 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 Page(pPage);. r
158d3 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d eturn depth+1;.}
158d4 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
158d5 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 E_OMIT_INTEGRITY
158d6 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 _CHECK */..#ifnd
158d7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
158d8 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f NTEGRITY_CHECK./
158d9 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
158da 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 e does a complet
158db 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67 e check of the g
158dc 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e iven BTree file.
158dd 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 aRoot[] is.**
158de 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65 an array of page
158df 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65 s numbers were e
158e0 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ach page number
158e1 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 is the root page
158e2 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 of.** a table.
158e3 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 nRoot is the nu
158e4 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
158e5 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 in aRoot..**.**
158e6 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 Write the number
158e7 20 6f 66 20 65 72 72 6f 72 20 73 65 65 6e 20 69 of error seen i
158e8 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 70 n *pnErr. Excep
158e9 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 t for some memor
158ea 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y.** allocation
158eb 65 72 72 6f 72 73 2c 20 20 6e 6e 20 65 72 72 6f errors, nn erro
158ec 72 20 6d 65 73 73 61 67 65 20 69 73 20 68 65 6c r message is hel
158ed 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 d in memory obta
158ee 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c ined from.** mal
158ef 6c 6f 63 20 69 73 20 72 65 74 75 72 6e 65 64 20 loc is returned
158f0 69 66 20 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e if *pnErr is non
158f1 2d 7a 65 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72 -zero. If *pnEr
158f2 72 3d 3d 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69 r==0 then NULL i
158f3 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a s.** returned..*
158f4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
158f5 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
158f6 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 reeIntegrityChec
158f7 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 k(. Btree *p,
158f8 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 /* The btree
158f9 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f to be checked */
158fa 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 . int *aRoot,
158fb 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 /* An array of
158fc 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65 root pages numbe
158fd 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61 rs for individua
158fe 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 l trees */. int
158ff 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 nRoot, /* Nu
15900 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
15901 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 in aRoot[] */.
15902 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a int mxErr, /*
15903 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 Stop reporting
15904 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69 errors after thi
15905 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 s many */. int
15906 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69 *pnErr /* Wri
15907 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 te number of err
15908 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73 ors seen to this
15909 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a variable */.){.
1590a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e int i;. int n
1590b 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 Ref;. Integrity
1590c 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 Ck sCheck;. BtS
1590d 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
1590e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 pBt;. char zErr
1590f 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 [100];.. sqlite
15910 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
15911 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 pBt->db = p->d
15912 62 3b 0a 20 20 6e 52 65 66 20 3d 20 73 71 6c 69 b;. nRef = sqli
15913 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
15914 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 (pBt->pPager);.
15915 20 69 66 28 20 6c 6f 63 6b 42 74 72 65 65 57 69 if( lockBtreeWi
15916 74 68 52 65 74 72 79 28 70 29 21 3d 53 51 4c 49 thRetry(p)!=SQLI
15917 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 6e TE_OK ){. *pn
15918 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c Err = 1;. sql
15919 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1591a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 );. return sq
1591b 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c lite3DbStrDup(0,
1591c 20 22 63 61 6e 6e 6f 74 20 61 63 71 75 69 72 65 "cannot acquire
1591d 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 a read lock on
1591e 74 68 65 20 64 61 74 61 62 61 73 65 22 29 3b 0a the database");.
1591f 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 }. sCheck.pBt
15920 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 65 63 6b = pBt;. sCheck
15921 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 .pPager = pBt->p
15922 50 61 67 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e Pager;. sCheck.
15923 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 nPage = pagerPag
15924 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 50 ecount(sCheck.pP
15925 61 67 65 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e ager);. sCheck.
15926 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 mxErr = mxErr;.
15927 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 sCheck.nErr = 0
15928 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f ;. sCheck.mallo
15929 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a cFailed = 0;. *
1592a 70 6e 45 72 72 20 3d 20 30 3b 0a 23 69 66 6e 64 pnErr = 0;.#ifnd
1592b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1592c 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20 UTOVACUUM. if(
1592d 70 42 74 2d 3e 6e 54 72 75 6e 63 21 3d 30 20 29 pBt->nTrunc!=0 )
1592e 7b 0a 20 20 20 20 73 43 68 65 63 6b 2e 6e 50 61 {. sCheck.nPa
1592f 67 65 20 3d 20 70 42 74 2d 3e 6e 54 72 75 6e 63 ge = pBt->nTrunc
15930 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 ;. }.#endif. i
15931 66 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d f( sCheck.nPage=
15932 3d 30 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b =0 ){. unlock
15933 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 BtreeIfUnused(pB
15934 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 t);. sqlite3B
15935 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
15936 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
15937 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d sCheck.anRef =
15938 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 sqlite3Malloc(
15939 28 73 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 (sCheck.nPage+1)
1593a 2a 73 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 *sizeof(sCheck.a
1593b 6e 52 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 nRef[0]) );. if
1593c 28 20 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 ( !sCheck.anRef
1593d 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 ){. unlockBtr
1593e 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b eeIfUnused(pBt);
1593f 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b . *pnErr = 1;
15940 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
15941 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 eLeave(p);. r
15942 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 eturn 0;. }. f
15943 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 or(i=0; i<=sChec
15944 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 k.nPage; i++){ s
15945 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d Check.anRef[i] =
15946 20 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 0; }. i = PEND
15947 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
15948 74 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 t);. if( i<=sCh
15949 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 eck.nPage ){.
1594a 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d sCheck.anRef[i]
1594b 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 = 1;. }. sqli
1594c 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 te3StrAccumInit(
1594d 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 &sCheck.errMsg,
1594e 7a 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 zErr, sizeof(zEr
1594f 72 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f r), 20000);.. /
15950 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 * Check the inte
15951 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 grity of the fre
15952 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 elist. */. che
15953 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 ckList(&sCheck,
15954 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74 1, get4byte(&pBt
15955 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
15956 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 32]),.
15957 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d get4byte(&pBt-
15958 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
15959 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 6]), "Main freel
1595a 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 ist: ");.. /* C
1595b 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 heck all the tab
1595c 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 les.. */. for(
1595d 69 3d 30 3b 20 69 3c 6e 52 6f 6f 74 20 26 26 20 i=0; i<nRoot &&
1595e 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b sCheck.mxErr; i+
1595f 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f +){. if( aRoo
15960 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e t[i]==0 ) contin
15961 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ue;.#ifndef SQLI
15962 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
15963 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e UM. if( pBt->
15964 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52 autoVacuum && aR
15965 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 oot[i]>1 ){.
15966 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73 checkPtrmap(&s
15967 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c Check, aRoot[i],
15968 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 PTRMAP_ROOTPAGE
15969 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 , 0, 0);. }.#
1596a 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54 endif. checkT
1596b 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c reePage(&sCheck,
1596c 20 61 52 6f 6f 74 5b 69 5d 2c 20 30 2c 20 22 4c aRoot[i], 0, "L
1596d 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 ist of tree root
1596e 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a s: ");. }.. /*
1596f 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 Make sure every
15970 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c page in the fil
15971 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a e is referenced.
15972 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 */. for(i=1;
15973 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 i<=sCheck.nPage
15974 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b && sCheck.mxErr;
15975 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 i++){.#ifdef SQ
15976 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
15977 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 CUUM. if( sCh
15978 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 eck.anRef[i]==0
15979 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 ){. checkAp
1597a 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c pendMsg(&sCheck,
1597b 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 0, "Page %d is
1597c 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b never used", i);
1597d 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 . }.#else.
1597e 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 /* If the datab
1597f 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 ase supports aut
15980 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 o-vacuum, make s
15981 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f ure no tables co
15982 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 ntain. ** ref
15983 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 erences to point
15984 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 er-map pages..
15985 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 */. if( sCh
15986 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 eck.anRef[i]==0
15987 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d && . (PTRM
15988 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 AP_PAGENO(pBt, i
15989 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 )!=i || !pBt->au
1598a 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 toVacuum) ){.
1598b 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
1598c 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 g(&sCheck, 0, "P
1598d 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 age %d is never
1598e 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d used", i);. }
1598f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e . if( sCheck.
15990 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a anRef[i]!=0 && .
15991 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 (PTRMAP_P
15992 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 AGENO(pBt, i)==i
15993 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 && pBt->autoVac
15994 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 uum) ){. ch
15995 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 eckAppendMsg(&sC
15996 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 heck, 0, "Pointe
15997 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 r map page %d is
15998 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 referenced", i)
15999 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
1599a 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 }.. /* Make su
1599b 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 re this analysis
1599c 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 did not leave a
1599d 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 ny unref() pages
1599e 0a 20 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 . */. unlockBt
1599f 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
159a0 3b 0a 20 20 69 66 28 20 6e 52 65 66 20 21 3d 20 ;. if( nRef !=
159a1 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
159a2 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
159a3 29 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 ) ){. checkAp
159a4 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c pendMsg(&sCheck,
159a5 20 30 2c 20 0a 20 20 20 20 20 20 22 4f 75 74 73 0, . "Outs
159a6 74 61 6e 64 69 6e 67 20 70 61 67 65 20 63 6f 75 tanding page cou
159a7 6e 74 20 67 6f 65 73 20 66 72 6f 6d 20 25 64 20 nt goes from %d
159a8 74 6f 20 25 64 20 64 75 72 69 6e 67 20 74 68 69 to %d during thi
159a9 73 20 61 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 s analysis",.
159aa 20 20 20 6e 52 65 66 2c 20 73 71 6c 69 74 65 33 nRef, sqlite3
159ab 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 PagerRefcount(pB
159ac 74 2d 3e 70 50 61 67 65 72 29 0a 20 20 20 20 29 t->pPager). )
159ad 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 ;. }.. /* Clea
159ae 6e 20 20 75 70 20 61 6e 64 20 72 65 70 6f 72 74 n up and report
159af 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 errors.. */.
159b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
159b1 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f e(p);. sqlite3_
159b2 66 72 65 65 28 73 43 68 65 63 6b 2e 61 6e 52 65 free(sCheck.anRe
159b3 66 29 3b 0a 20 20 69 66 28 20 73 43 68 65 63 6b f);. if( sCheck
159b4 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b .mallocFailed ){
159b5 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 . sqlite3StrA
159b6 63 63 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 ccumReset(&sChec
159b7 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 2a k.errMsg);. *
159b8 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e pnErr = sCheck.n
159b9 45 72 72 2b 31 3b 0a 20 20 20 20 72 65 74 75 72 Err+1;. retur
159ba 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 6e 45 72 n 0;. }. *pnEr
159bb 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 3b r = sCheck.nErr;
159bc 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 45 . if( sCheck.nE
159bd 72 72 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 53 rr==0 ) sqlite3S
159be 74 72 41 63 63 75 6d 52 65 73 65 74 28 26 73 43 trAccumReset(&sC
159bf 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 heck.errMsg);.
159c0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 return sqlite3St
159c1 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 73 43 rAccumFinish(&sC
159c2 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 7d 0a heck.errMsg);.}.
159c3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
159c4 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
159c5 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 CHECK */../*.**
159c6 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 Return the full
159c7 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 pathname of the
159c8 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 underlying datab
159c9 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
159ca 54 68 65 20 70 61 67 65 72 20 66 69 6c 65 6e 61 The pager filena
159cb 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 me is invariant
159cc 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 as long as the p
159cd 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 ager is.** open
159ce 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f so it is safe to
159cf 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 access without
159d0 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 the BtShared mut
159d1 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ex..*/.SQLITE_PR
159d2 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
159d3 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 *sqlite3BtreeGe
159d4 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 tFilename(Btree
159d5 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
159d6 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 ->pBt->pPager!=0
159d7 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c );. return sql
159d8 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d ite3PagerFilenam
159d9 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 e(p->pBt->pPager
159da 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
159db 72 6e 20 74 68 65 20 70 61 74 68 6e 61 6d 65 20 rn the pathname
159dc 6f 66 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 of the directory
159dd 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
159de 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
159df 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 ..**.** The page
159e0 72 20 64 69 72 65 63 74 6f 72 79 20 6e 61 6d 65 r directory name
159e1 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73 is invariant as
159e2 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67 long as the pag
159e3 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f er is.** open so
159e4 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 it is safe to a
159e5 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68 ccess without th
159e6 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 e BtShared mutex
159e7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
159e8 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
159e9 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 44 sqlite3BtreeGetD
159ea 69 72 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29 irname(Btree *p)
159eb 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 {. assert( p->p
159ec 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b Bt->pPager!=0 );
159ed 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
159ee 33 50 61 67 65 72 44 69 72 6e 61 6d 65 28 70 2d 3PagerDirname(p-
159ef 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d >pBt->pPager);.}
159f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
159f1 68 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 he pathname of t
159f2 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
159f3 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 for this databas
159f4 65 2e 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a e. The return.**
159f5 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 value of this r
159f6 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 73 61 outine is the sa
159f7 6d 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 me regardless of
159f8 20 77 68 65 74 68 65 72 20 74 68 65 20 6a 6f 75 whether the jou
159f9 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73 rnal file.** has
159fa 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f 72 been created or
159fb 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 not..**.** The
159fc 70 61 67 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 pager journal fi
159fd 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 lename is invari
159fe 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 ant as long as t
159ff 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f he pager is.** o
15a00 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 pen so it is saf
15a01 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 e to access with
15a02 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65 64 out the BtShared
15a03 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 mutex..*/.SQLIT
15a04 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
15a05 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 char *sqlite3Btr
15a06 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 eeGetJournalname
15a07 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 (Btree *p){. as
15a08 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 sert( p->pBt->pP
15a09 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 ager!=0 );. ret
15a0a 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 urn sqlite3Pager
15a0b 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 Journalname(p->p
15a0c 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a Bt->pPager);.}..
15a0d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15a0e 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a MIT_VACUUM./*.**
15a0f 20 43 6f 70 79 20 74 68 65 20 63 6f 6d 70 6c 65 Copy the comple
15a10 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 42 te content of pB
15a11 74 46 72 6f 6d 20 69 6e 74 6f 20 70 42 74 54 6f tFrom into pBtTo
15a12 2e 20 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e . A transaction
15a13 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 63 74 69 .** must be acti
15a14 76 65 20 66 6f 72 20 62 6f 74 68 20 66 69 6c 65 ve for both file
15a15 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a s..**.** The siz
15a16 65 20 6f 66 20 66 69 6c 65 20 70 54 6f 20 6d 61 e of file pTo ma
15a17 79 20 62 65 20 72 65 64 75 63 65 64 20 62 79 20 y be reduced by
15a18 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a this operation..
15a19 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e 67 20 67 ** If anything g
15a1a 6f 65 73 20 77 72 6f 6e 67 2c 20 74 68 65 20 74 oes wrong, the t
15a1b 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 70 54 ransaction on pT
15a1c 6f 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b o is rolled back
15a1d 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 . .**.** If succ
15a1e 65 73 73 66 75 6c 2c 20 43 6f 6d 6d 69 74 50 68 essful, CommitPh
15a1f 61 73 65 4f 6e 65 28 29 20 6d 61 79 20 62 65 20 aseOne() may be
15a20 63 61 6c 6c 65 64 20 6f 6e 20 70 54 6f 20 62 65 called on pTo be
15a21 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 fore returning.
15a22 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 73 .** The caller s
15a23 68 6f 75 6c 64 20 66 69 6e 69 73 68 20 63 6f 6d hould finish com
15a24 6d 69 74 74 69 6e 67 20 74 68 65 20 74 72 61 6e mitting the tran
15a25 73 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 62 saction on pTo b
15a26 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c y calling.** sql
15a27 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 ite3BtreeCommit(
15a28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
15a29 20 62 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42 btreeCopyFile(B
15a2a 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 65 65 tree *pTo, Btree
15a2b 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 *pFrom){. int
15a2c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
15a2d 20 20 50 67 6e 6f 20 69 3b 0a 0a 20 20 50 67 6e Pgno i;.. Pgn
15a2e 6f 20 6e 46 72 6f 6d 50 61 67 65 3b 20 20 20 20 o nFromPage;
15a2f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
15a30 67 65 73 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a ges in pFrom */.
15a31 20 20 50 67 6e 6f 20 6e 54 6f 50 61 67 65 3b 20 Pgno nToPage;
15a32 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
15a33 6f 66 20 70 61 67 65 73 20 69 6e 20 70 54 6f 20 of pages in pTo
15a34 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4e 65 77 50 61 */. Pgno nNewPa
15a35 67 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ge; /* Numb
15a36 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 70 er of pages in p
15a37 54 6f 20 61 66 74 65 72 20 74 68 65 20 63 6f 70 To after the cop
15a38 79 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 69 53 6b y */.. Pgno iSk
15a39 69 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 ip; /* P
15a3a 65 6e 64 69 6e 67 20 62 79 74 65 20 70 61 67 65 ending byte page
15a3b 20 69 6e 20 70 54 6f 20 2a 2f 0a 20 20 69 6e 74 in pTo */. int
15a3c 20 6e 54 6f 50 61 67 65 53 69 7a 65 3b 20 20 20 nToPageSize;
15a3d 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 6f 66 /* Page size of
15a3e 20 70 54 6f 20 69 6e 20 62 79 74 65 73 20 2a 2f pTo in bytes */
15a3f 0a 20 20 69 6e 74 20 6e 46 72 6f 6d 50 61 67 65 . int nFromPage
15a40 53 69 7a 65 3b 20 20 2f 2a 20 50 61 67 65 20 73 Size; /* Page s
15a41 69 7a 65 20 6f 66 20 70 46 72 6f 6d 20 69 6e 20 ize of pFrom in
15a42 62 79 74 65 73 20 2a 2f 0a 0a 20 20 42 74 53 68 bytes */.. BtSh
15a43 61 72 65 64 20 2a 70 42 74 54 6f 20 3d 20 70 54 ared *pBtTo = pT
15a44 6f 2d 3e 70 42 74 3b 0a 20 20 42 74 53 68 61 72 o->pBt;. BtShar
15a45 65 64 20 2a 70 42 74 46 72 6f 6d 20 3d 20 70 46 ed *pBtFrom = pF
15a46 72 6f 6d 2d 3e 70 42 74 3b 0a 20 20 70 42 74 54 rom->pBt;. pBtT
15a47 6f 2d 3e 64 62 20 3d 20 70 54 6f 2d 3e 64 62 3b o->db = pTo->db;
15a48 0a 20 20 70 42 74 46 72 6f 6d 2d 3e 64 62 20 3d . pBtFrom->db =
15a49 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a 0a 20 20 6e pFrom->db;.. n
15a4a 54 6f 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 ToPageSize = pBt
15a4b 54 6f 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 To->pageSize;.
15a4c 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 20 3d 20 nFromPageSize =
15a4d 70 42 74 46 72 6f 6d 2d 3e 70 61 67 65 53 69 7a pBtFrom->pageSiz
15a4e 65 3b 0a 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 e;.. if( pTo->i
15a4f 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 nTrans!=TRANS_WR
15a50 49 54 45 20 7c 7c 20 70 46 72 6f 6d 2d 3e 69 6e ITE || pFrom->in
15a51 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 Trans!=TRANS_WRI
15a52 54 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e TE ){. return
15a53 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
15a54 20 7d 0a 20 20 69 66 28 20 70 42 74 54 6f 2d 3e }. if( pBtTo->
15a55 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 72 pCursor ){. r
15a56 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
15a57 59 3b 0a 20 20 7d 0a 0a 20 20 6e 54 6f 50 61 67 Y;. }.. nToPag
15a58 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 e = pagerPagecou
15a59 6e 74 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 nt(pBtTo->pPager
15a5a 29 3b 0a 20 20 6e 46 72 6f 6d 50 61 67 65 20 3d );. nFromPage =
15a5b 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 pagerPagecount(
15a5c 70 42 74 46 72 6f 6d 2d 3e 70 50 61 67 65 72 29 pBtFrom->pPager)
15a5d 3b 0a 20 20 69 53 6b 69 70 20 3d 20 50 45 4e 44 ;. iSkip = PEND
15a5e 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
15a5f 74 54 6f 29 3b 0a 0a 20 20 2f 2a 20 56 61 72 69 tTo);.. /* Vari
15a60 61 62 6c 65 20 6e 4e 65 77 50 61 67 65 20 69 73 able nNewPage is
15a61 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
15a62 61 67 65 73 20 72 65 71 75 69 72 65 64 20 74 6f ages required to
15a63 20 73 74 6f 72 65 20 74 68 65 0a 20 20 2a 2a 20 store the. **
15a64 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46 72 6f contents of pFro
15a65 6d 20 75 73 69 6e 67 20 74 68 65 20 63 75 72 72 m using the curr
15a66 65 6e 74 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 ent page-size of
15a67 20 70 54 6f 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 65 pTo.. */. nNe
15a68 77 50 61 67 65 20 3d 20 28 28 69 36 34 29 6e 46 wPage = ((i64)nF
15a69 72 6f 6d 50 61 67 65 20 2a 20 28 69 36 34 29 6e romPage * (i64)n
15a6a 46 72 6f 6d 50 61 67 65 53 69 7a 65 20 2b 20 28 FromPageSize + (
15a6b 69 36 34 29 6e 54 6f 50 61 67 65 53 69 7a 65 20 i64)nToPageSize
15a6c 2d 20 31 29 20 2f 20 0a 20 20 20 20 20 20 28 69 - 1) / . (i
15a6d 36 34 29 6e 54 6f 50 61 67 65 53 69 7a 65 3b 0a 64)nToPageSize;.
15a6e 0a 20 20 66 6f 72 28 69 3d 31 3b 20 72 63 3d 3d . for(i=1; rc==
15a6f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 69 3c SQLITE_OK && (i<
15a70 3d 6e 54 6f 50 61 67 65 20 7c 7c 20 69 3c 3d 6e =nToPage || i<=n
15a71 4e 65 77 50 61 67 65 29 3b 20 69 2b 2b 29 7b 0a NewPage); i++){.
15a72 0a 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 . /* Journal
15a73 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 the original pag
15a74 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a e.. **. **
15a75 20 69 53 6b 69 70 20 69 73 20 74 68 65 20 70 61 iSkip is the pa
15a76 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
15a77 20 6c 6f 63 6b 69 6e 67 20 70 61 67 65 20 28 50 locking page (P
15a78 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
15a79 29 0a 20 20 20 20 2a 2a 20 69 6e 20 64 61 74 61 ). ** in data
15a7a 62 61 73 65 20 2a 70 54 6f 20 28 62 65 66 6f 72 base *pTo (befor
15a7b 65 20 74 68 65 20 63 6f 70 79 29 2e 20 54 68 69 e the copy). Thi
15a7c 73 20 70 61 67 65 20 69 73 20 6e 65 76 65 72 20 s page is never
15a7d 77 72 69 74 74 65 6e 20 0a 20 20 20 20 2a 2a 20 written . **
15a7e 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
15a7f 20 66 69 6c 65 2e 20 55 6e 6c 65 73 73 20 69 3d file. Unless i=
15a80 3d 69 53 6b 69 70 20 6f 72 20 74 68 65 20 70 61 =iSkip or the pa
15a81 67 65 20 77 61 73 20 6e 6f 74 0a 20 20 20 20 2a ge was not. *
15a82 2a 20 70 72 65 73 65 6e 74 20 69 6e 20 70 54 6f * present in pTo
15a83 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79 before the copy
15a84 20 6f 70 65 72 61 74 69 6f 6e 2c 20 6a 6f 75 72 operation, jour
15a85 6e 61 6c 20 70 61 67 65 20 69 20 66 72 6f 6d 20 nal page i from
15a86 70 54 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 pTo.. */.
15a87 69 66 28 20 69 21 3d 69 53 6b 69 70 20 26 26 20 if( i!=iSkip &&
15a88 69 3c 3d 6e 54 6f 50 61 67 65 20 29 7b 0a 20 20 i<=nToPage ){.
15a89 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 DbPage *pDbP
15a8a 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 age = 0;. r
15a8b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
15a8c 47 65 74 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 Get(pBtTo->pPage
15a8d 72 2c 20 69 2c 20 26 70 44 62 50 61 67 65 29 3b r, i, &pDbPage);
15a8e 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
15a8f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
15a90 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
15a91 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61 PagerWrite(pDbPa
15a92 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ge);. if(
15a93 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
15a94 26 20 69 3e 6e 46 72 6f 6d 50 61 67 65 20 29 7b & i>nFromPage ){
15a95 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 59 65 . /* Ye
15a96 61 68 2e 20 20 49 74 20 73 65 65 6d 73 20 77 69 ah. It seems wi
15a97 65 72 64 20 74 6f 20 63 61 6c 6c 20 44 6f 6e 74 erd to call Dont
15a98 57 72 69 74 65 28 29 20 72 69 67 68 74 20 61 66 Write() right af
15a99 74 65 72 20 57 72 69 74 65 28 29 2e 20 42 75 74 ter Write(). But
15a9a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 . ** th
15a9b 61 74 20 69 73 20 62 65 63 61 75 73 65 20 74 68 at is because th
15a9c 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 6f 73 65 e names of those
15a9d 20 70 72 6f 63 65 64 75 72 65 73 20 64 6f 20 6e procedures do n
15a9e 6f 74 20 65 78 61 63 74 6c 79 20 0a 20 20 20 20 ot exactly .
15a9f 20 20 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 ** represe
15aa0 6e 74 20 77 68 61 74 20 74 68 65 79 20 64 6f 2e nt what they do.
15aa1 20 20 57 72 69 74 65 28 29 20 72 65 61 6c 6c 79 Write() really
15aa2 20 6d 65 61 6e 73 20 22 70 75 74 20 74 68 69 73 means "put this
15aa3 20 70 61 67 65 20 69 6e 20 74 68 65 0a 20 20 20 page in the.
15aa4 20 20 20 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 ** rollba
15aa5 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 6d ck journal and m
15aa6 61 72 6b 20 69 74 20 61 73 20 64 69 72 74 79 20 ark it as dirty
15aa7 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 so that it will
15aa8 62 65 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20 be written.
15aa9 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 ** to the d
15aaa 61 74 61 62 61 73 65 20 66 69 6c 65 20 6c 61 74 atabase file lat
15aab 65 72 2e 22 20 20 44 6f 6e 74 57 72 69 74 65 28 er." DontWrite(
15aac 29 20 75 6e 64 6f 65 73 20 74 68 65 20 73 65 63 ) undoes the sec
15aad 6f 6e 64 20 70 61 72 74 20 6f 66 0a 20 20 20 20 ond part of.
15aae 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 61 6e ** that an
15aaf 64 20 70 72 65 76 65 6e 74 73 20 74 68 65 20 70 d prevents the p
15ab0 61 67 65 20 66 72 6f 6d 20 62 65 69 6e 67 20 77 age from being w
15ab1 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 ritten to the da
15ab2 74 61 62 61 73 65 2e 20 54 68 65 0a 20 20 20 20 tabase. The.
15ab3 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73 ** page is
15ab4 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 72 6f still on the ro
15ab5 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 llback journal,
15ab6 74 68 6f 75 67 68 2e 20 20 41 6e 64 20 74 68 61 though. And tha
15ab7 74 20 69 73 20 74 68 65 20 0a 20 20 20 20 20 20 t is the .
15ab8 20 20 20 20 2a 2a 20 77 68 6f 6c 65 20 70 6f 69 ** whole poi
15ab9 6e 74 20 6f 66 20 74 68 69 73 20 62 6c 6f 63 6b nt of this block
15aba 3a 20 74 6f 20 70 75 74 20 70 61 67 65 73 20 6f : to put pages o
15abb 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a n the rollback j
15abc 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 20 20 20 ournal. .
15abd 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
15abe 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15abf 72 44 6f 6e 74 57 72 69 74 65 28 70 44 62 50 61 rDontWrite(pDbPa
15ac0 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ge);. }.
15ac1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
15ac2 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 gerUnref(pDbPage
15ac3 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
15ac4 0a 0a 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69 .. /* Overwri
15ac5 74 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 70 te the data in p
15ac6 61 67 65 20 69 20 6f 66 20 74 68 65 20 74 61 72 age i of the tar
15ac7 67 65 74 20 64 61 74 61 62 61 73 65 20 2a 2f 0a get database */.
15ac8 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
15ac9 54 45 5f 4f 4b 20 26 26 20 69 21 3d 69 53 6b 69 TE_OK && i!=iSki
15aca 70 20 26 26 20 69 3c 3d 6e 4e 65 77 50 61 67 65 p && i<=nNewPage
15acb 20 29 7b 0a 0a 20 20 20 20 20 20 44 62 50 61 67 ){.. DbPag
15acc 65 20 2a 70 54 6f 50 61 67 65 20 3d 20 30 3b 0a e *pToPage = 0;.
15acd 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e sqlite3_in
15ace 74 36 34 20 69 4f 66 66 3b 0a 0a 20 20 20 20 20 t64 iOff;..
15acf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
15ad0 65 72 47 65 74 28 70 42 74 54 6f 2d 3e 70 50 61 erGet(pBtTo->pPa
15ad1 67 65 72 2c 20 69 2c 20 26 70 54 6f 50 61 67 65 ger, i, &pToPage
15ad2 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
15ad3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15ad4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
15ad5 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 6f e3PagerWrite(pTo
15ad6 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a Page);. }..
15ad7 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 for(.
15ad8 20 20 20 69 4f 66 66 3d 28 69 2d 31 29 2a 6e 54 iOff=(i-1)*nT
15ad9 6f 50 61 67 65 53 69 7a 65 3b 20 0a 20 20 20 20 oPageSize; .
15ada 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rc==SQLITE_O
15adb 4b 20 26 26 20 69 4f 66 66 3c 69 2a 6e 54 6f 50 K && iOff<i*nToP
15adc 61 67 65 53 69 7a 65 3b 20 0a 20 20 20 20 20 20 ageSize; .
15add 20 20 69 4f 66 66 20 2b 3d 20 6e 46 72 6f 6d 50 iOff += nFromP
15ade 61 67 65 53 69 7a 65 0a 20 20 20 20 20 20 29 7b ageSize. ){
15adf 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20 . DbPage
15ae0 2a 70 46 72 6f 6d 50 61 67 65 20 3d 20 30 3b 0a *pFromPage = 0;.
15ae1 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 Pgno iFr
15ae2 6f 6d 20 3d 20 28 69 4f 66 66 2f 6e 46 72 6f 6d om = (iOff/nFrom
15ae3 50 61 67 65 53 69 7a 65 29 2b 31 3b 0a 0a 20 20 PageSize)+1;..
15ae4 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d if( iFrom=
15ae5 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
15ae6 47 45 28 70 42 74 46 72 6f 6d 29 20 29 7b 0a 20 GE(pBtFrom) ){.
15ae7 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 continu
15ae8 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 e;. }..
15ae9 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
15aea 65 33 50 61 67 65 72 47 65 74 28 70 42 74 46 72 e3PagerGet(pBtFr
15aeb 6f 6d 2d 3e 70 50 61 67 65 72 2c 20 69 46 72 6f om->pPager, iFro
15aec 6d 2c 20 26 70 46 72 6f 6d 50 61 67 65 29 3b 0a m, &pFromPage);.
15aed 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
15aee 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15aef 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f char *zTo
15af0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
15af1 65 74 44 61 74 61 28 70 54 6f 50 61 67 65 29 3b etData(pToPage);
15af2 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 . char
15af3 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 *zFrom = sqlite3
15af4 50 61 67 65 72 47 65 74 44 61 74 61 28 70 46 72 PagerGetData(pFr
15af5 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 omPage);.
15af6 20 20 20 69 6e 74 20 6e 43 6f 70 79 3b 0a 0a 20 int nCopy;..
15af7 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 46 72 if( nFr
15af8 6f 6d 50 61 67 65 53 69 7a 65 3e 3d 6e 54 6f 50 omPageSize>=nToP
15af9 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 ageSize ){.
15afa 20 20 20 20 20 20 20 7a 46 72 6f 6d 20 2b 3d 20 zFrom +=
15afb 28 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65 53 69 ((i-1)*nToPageSi
15afc 7a 65 20 2d 20 28 28 69 46 72 6f 6d 2d 31 29 2a ze - ((iFrom-1)*
15afd 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 29 3b nFromPageSize));
15afe 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f . nCo
15aff 70 79 20 3d 20 6e 54 6f 50 61 67 65 53 69 7a 65 py = nToPageSize
15b00 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
15b01 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a e{. z
15b02 54 6f 20 2b 3d 20 28 28 28 69 46 72 6f 6d 2d 31 To += (((iFrom-1
15b03 29 2a 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 )*nFromPageSize)
15b04 20 2d 20 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65 - (i-1)*nToPage
15b05 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Size);.
15b06 20 20 20 6e 43 6f 70 79 20 3d 20 6e 46 72 6f 6d nCopy = nFrom
15b07 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 PageSize;.
15b08 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 }..
15b09 20 6d 65 6d 63 70 79 28 7a 54 6f 2c 20 7a 46 72 memcpy(zTo, zFr
15b0a 6f 6d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 om, nCopy);.
15b0b 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
15b0c 65 72 55 6e 72 65 66 28 70 46 72 6f 6d 50 61 67 erUnref(pFromPag
15b0d 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 e);. }.
15b0e 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 }.. if(
15b0f 20 70 54 6f 50 61 67 65 20 29 7b 0a 20 20 20 20 pToPage ){.
15b10 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 20 3d MemPage *p =
15b11 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 (MemPage *)sqli
15b12 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
15b13 28 70 54 6f 50 61 67 65 29 3b 0a 20 20 20 20 20 (pToPage);.
15b14 20 20 20 70 2d 3e 69 73 49 6e 69 74 20 3d 20 30 p->isInit = 0
15b15 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
15b16 33 50 61 67 65 72 55 6e 72 65 66 28 70 54 6f 50 3PagerUnref(pToP
15b17 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 age);. }.
15b18 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
15b19 20 74 68 69 6e 67 73 20 68 61 76 65 20 77 6f 72 things have wor
15b1a 6b 65 64 20 73 6f 20 66 61 72 2c 20 74 68 65 20 ked so far, the
15b1b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 database file ma
15b1c 79 20 6e 65 65 64 20 74 6f 20 62 65 20 0a 20 20 y need to be .
15b1d 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 54 68 ** truncated. Th
15b1e 65 20 63 6f 6d 70 6c 65 78 20 70 61 72 74 20 69 e complex part i
15b1f 73 20 74 68 61 74 20 69 74 20 6d 61 79 20 6e 65 s that it may ne
15b20 65 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 ed to be truncat
15b21 65 64 20 74 6f 0a 20 20 2a 2a 20 61 20 73 69 7a ed to. ** a siz
15b22 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 6e e that is not an
15b23 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c integer multipl
15b24 65 20 6f 66 20 6e 54 6f 50 61 67 65 53 69 7a 65 e of nToPageSize
15b25 20 2d 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 - the current.
15b26 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 20 75 73 ** page size us
15b27 65 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 ed by the pager
15b28 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
15b29 42 2d 54 72 65 65 20 70 54 6f 2e 0a 20 20 2a 2a B-Tree pTo.. **
15b2a 0a 20 20 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c . ** For exampl
15b2b 65 2c 20 73 61 79 20 74 68 65 20 70 61 67 65 2d e, say the page-
15b2c 73 69 7a 65 20 6f 66 20 70 54 6f 20 69 73 20 32 size of pTo is 2
15b2d 30 34 38 20 62 79 74 65 73 20 61 6e 64 20 74 68 048 bytes and th
15b2e 65 20 6f 72 69 67 69 6e 61 6c 20 0a 20 20 2a 2a e original . **
15b2f 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
15b30 20 69 73 20 35 20 28 31 30 20 4b 42 20 66 69 6c is 5 (10 KB fil
15b31 65 29 2e 20 49 66 20 70 46 72 6f 6d 20 68 61 73 e). If pFrom has
15b32 20 61 20 70 61 67 65 20 73 69 7a 65 20 6f 66 20 a page size of
15b33 31 30 32 34 20 0a 20 20 2a 2a 20 62 79 74 65 73 1024 . ** bytes
15b34 20 61 6e 64 20 39 20 70 61 67 65 73 2c 20 74 68 and 9 pages, th
15b35 65 6e 20 74 68 65 20 66 69 6c 65 20 6e 65 65 64 en the file need
15b36 73 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 65 s to be truncate
15b37 64 20 74 6f 20 39 4b 42 2e 0a 20 20 2a 2f 0a 20 d to 9KB.. */.
15b38 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
15b39 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 46 OK ){. if( nF
15b3a 72 6f 6d 50 61 67 65 53 69 7a 65 21 3d 6e 54 6f romPageSize!=nTo
15b3b 50 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 PageSize ){.
15b3c 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
15b3d 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 pFile = sqlite3P
15b3e 61 67 65 72 46 69 6c 65 28 70 42 74 54 6f 2d 3e agerFile(pBtTo->
15b3f 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69 pPager);. i
15b40 36 34 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29 64 iSize = (i64)
15b41 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 20 2a 20 nFromPageSize *
15b42 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65 3b 0a (i64)nFromPage;.
15b43 20 20 20 20 20 20 69 36 34 20 69 4e 6f 77 20 3d i64 iNow =
15b44 20 28 69 36 34 29 28 28 6e 54 6f 50 61 67 65 3e (i64)((nToPage>
15b45 6e 4e 65 77 50 61 67 65 29 3f 6e 54 6f 50 61 67 nNewPage)?nToPag
15b46 65 3a 6e 4e 65 77 50 61 67 65 29 20 2a 20 28 69 e:nNewPage) * (i
15b47 36 34 29 6e 54 6f 50 61 67 65 53 69 7a 65 3b 20 64)nToPageSize;
15b48 0a 20 20 20 20 20 20 69 36 34 20 69 50 65 6e 64 . i64 iPend
15b49 69 6e 67 20 3d 20 28 28 69 36 34 29 50 45 4e 44 ing = ((i64)PEND
15b4a 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
15b4b 74 54 6f 29 2d 31 29 20 2a 28 69 36 34 29 6e 54 tTo)-1) *(i64)nT
15b4c 6f 50 61 67 65 53 69 7a 65 3b 0a 20 20 0a 20 20 oPageSize;. .
15b4d 20 20 20 20 61 73 73 65 72 74 28 20 69 53 69 7a assert( iSiz
15b4e 65 3c 3d 69 4e 6f 77 20 29 3b 0a 20 20 0a 20 20 e<=iNow );. .
15b4f 20 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 70 68 /* Commit ph
15b50 61 73 65 20 6f 6e 65 20 73 79 6e 63 73 20 74 68 ase one syncs th
15b51 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 e journal file a
15b52 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 ssociated with p
15b53 54 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e To . ** con
15b54 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72 69 67 taining the orig
15b55 69 6e 61 6c 20 64 61 74 61 2e 20 49 74 20 64 6f inal data. It do
15b56 65 73 20 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 es not sync the
15b57 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 database file.
15b58 20 20 20 20 2a 2a 20 69 74 73 65 6c 66 2e 20 41 ** itself. A
15b59 66 74 65 72 20 64 6f 69 6e 67 20 74 68 69 73 20 fter doing this
15b5a 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 75 73 it is safe to us
15b5b 65 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20 61 e OsTruncate() a
15b5c 6e 64 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a nd other. *
15b5d 2a 20 66 69 6c 65 20 41 50 49 73 20 6f 6e 20 74 * file APIs on t
15b5e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
15b5f 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20 20 20 directly..
15b60 20 2a 2f 0a 20 20 20 20 20 20 70 42 74 54 6f 2d */. pBtTo-
15b61 3e 64 62 20 3d 20 70 54 6f 2d 3e 64 62 3b 0a 20 >db = pTo->db;.
15b62 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
15b63 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
15b64 65 4f 6e 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 eOne(pBtTo->pPag
15b65 65 72 2c 20 30 2c 20 30 2c 20 31 29 3b 0a 20 20 er, 0, 0, 1);.
15b66 20 20 20 20 69 66 28 20 69 53 69 7a 65 3c 69 4e if( iSize<iN
15b67 6f 77 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 ow && rc==SQLITE
15b68 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
15b69 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 c = sqlite3OsTru
15b6a 6e 63 61 74 65 28 70 46 69 6c 65 2c 20 69 53 69 ncate(pFile, iSi
15b6b 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a ze);. }. .
15b6c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f /* The loo
15b6d 70 20 74 68 61 74 20 63 6f 70 69 65 64 20 64 61 p that copied da
15b6e 74 61 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 ta from database
15b6f 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 20 64 69 pFrom to pTo di
15b70 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 70 d not. ** p
15b71 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 63 6b opulate the lock
15b72 69 6e 67 20 70 61 67 65 20 6f 66 20 64 61 74 61 ing page of data
15b73 62 61 73 65 20 70 54 6f 2e 20 49 66 20 74 68 65 base pTo. If the
15b74 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 0a 20 20 page-size of.
15b75 20 20 20 20 2a 2a 20 70 46 72 6f 6d 20 69 73 20 ** pFrom is
15b76 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 61 smaller than tha
15b77 74 20 6f 66 20 70 54 6f 2c 20 74 68 69 73 20 6d t of pTo, this m
15b78 65 61 6e 73 20 73 6f 6d 65 20 64 61 74 61 20 77 eans some data w
15b79 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 74 ill. ** not
15b7a 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70 69 65 have been copie
15b7b 64 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 d. . **.
15b7c 20 20 20 2a 2a 20 54 68 69 73 20 62 6c 6f 63 6b ** This block
15b7d 20 63 6f 70 69 65 73 20 74 68 65 20 6d 69 73 73 copies the miss
15b7e 69 6e 67 20 64 61 74 61 20 66 72 6f 6d 20 64 61 ing data from da
15b7f 74 61 62 61 73 65 20 70 46 72 6f 6d 20 74 6f 20 tabase pFrom to
15b80 70 54 6f 20 0a 20 20 20 20 20 20 2a 2a 20 75 73 pTo . ** us
15b81 69 6e 67 20 66 69 6c 65 20 41 50 49 73 2e 20 54 ing file APIs. T
15b82 68 69 73 20 69 73 20 73 61 66 65 20 62 65 63 61 his is safe beca
15b83 75 73 65 20 61 74 20 74 68 69 73 20 70 6f 69 6e use at this poin
15b84 74 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 0a 20 t we know that.
15b85 20 20 20 20 20 2a 2a 20 61 6c 6c 20 6f 66 20 74 ** all of t
15b86 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
15b87 20 66 72 6f 6d 20 70 54 6f 20 68 61 73 20 62 65 from pTo has be
15b88 65 6e 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 en synced into t
15b89 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 he . ** jou
15b8a 72 6e 61 6c 20 66 69 6c 65 2e 20 41 74 20 74 68 rnal file. At th
15b8b 69 73 20 70 6f 69 6e 74 20 69 74 20 77 6f 75 6c is point it woul
15b8c 64 20 62 65 20 73 61 66 65 20 74 6f 20 64 6f 20 d be safe to do
15b8d 61 6e 79 74 68 69 6e 67 20 61 74 0a 20 20 20 20 anything at.
15b8e 20 20 2a 2a 20 61 6c 6c 20 74 6f 20 74 68 65 20 ** all to the
15b8f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 65 78 database file ex
15b90 63 65 70 74 20 74 72 75 6e 63 61 74 65 20 69 74 cept truncate it
15b91 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 2e 0a to zero bytes..
15b92 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
15b93 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15b94 20 26 26 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a && nFromPageSiz
15b95 65 3c 6e 54 6f 50 61 67 65 53 69 7a 65 20 26 26 e<nToPageSize &&
15b96 20 69 53 69 7a 65 3e 69 50 65 6e 64 69 6e 67 29 iSize>iPending)
15b97 7b 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4f {. i64 iO
15b98 66 66 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 ff;. for(
15b99 0a 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 3d . iOff=
15b9a 69 50 65 6e 64 69 6e 67 3b 20 0a 20 20 20 20 20 iPending; .
15b9b 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f rc==SQLITE_
15b9c 4f 4b 20 26 26 20 69 4f 66 66 3c 28 69 50 65 6e OK && iOff<(iPen
15b9d 64 69 6e 67 2b 6e 54 6f 50 61 67 65 53 69 7a 65 ding+nToPageSize
15b9e 29 3b 20 0a 20 20 20 20 20 20 20 20 20 20 69 4f ); . iO
15b9f 66 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67 65 53 ff += nFromPageS
15ba0 69 7a 65 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 ize. ){.
15ba1 20 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20 DbPage
15ba2 2a 70 46 72 6f 6d 50 61 67 65 20 3d 20 30 3b 0a *pFromPage = 0;.
15ba3 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 Pgno i
15ba4 46 72 6f 6d 20 3d 20 28 69 4f 66 66 2f 6e 46 72 From = (iOff/nFr
15ba5 6f 6d 50 61 67 65 53 69 7a 65 29 2b 31 3b 0a 20 omPageSize)+1;.
15ba6 20 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 . if(
15ba7 69 46 72 6f 6d 3d 3d 50 45 4e 44 49 4e 47 5f 42 iFrom==PENDING_B
15ba8 59 54 45 5f 50 41 47 45 28 70 42 74 46 72 6f 6d YTE_PAGE(pBtFrom
15ba9 29 20 7c 7c 20 69 46 72 6f 6d 3e 6e 46 72 6f 6d ) || iFrom>nFrom
15baa 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
15bab 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 continue;.
15bac 20 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. .
15bad 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
15bae 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 46 te3PagerGet(pBtF
15baf 72 6f 6d 2d 3e 70 50 61 67 65 72 2c 20 69 46 72 rom->pPager, iFr
15bb0 6f 6d 2c 20 26 70 46 72 6f 6d 50 61 67 65 29 3b om, &pFromPage);
15bb1 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
15bb2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
15bb3 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 char
15bb4 20 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 *zFrom = sqlite
15bb5 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 46 3PagerGetData(pF
15bb6 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 romPage);.
15bb7 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
15bb8 65 33 4f 73 57 72 69 74 65 28 70 46 69 6c 65 2c e3OsWrite(pFile,
15bb9 20 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 50 61 67 zFrom, nFromPag
15bba 65 53 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 20 eSize, iOff);.
15bbb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
15bbc 33 50 61 67 65 72 55 6e 72 65 66 28 70 46 72 6f 3PagerUnref(pFro
15bbd 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 mPage);.
15bbe 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
15bbf 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 2f }. . /
15bc0 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 * Sync the datab
15bc1 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 ase file */.
15bc2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
15bc3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
15bc4 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
15bc5 53 79 6e 63 28 70 42 74 54 6f 2d 3e 70 50 61 67 Sync(pBtTo->pPag
15bc6 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 er);. }.
15bc7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
15bc8 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 = sqlite3PagerT
15bc9 72 75 6e 63 61 74 65 28 70 42 74 54 6f 2d 3e 70 runcate(pBtTo->p
15bca 50 61 67 65 72 2c 20 6e 4e 65 77 50 61 67 65 29 Pager, nNewPage)
15bcb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
15bcc 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
15bcd 0a 20 20 20 20 20 20 70 42 74 54 6f 2d 3e 70 61 . pBtTo->pa
15bce 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 30 3b geSizeFixed = 0;
15bcf 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
15bd0 28 20 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 ( rc ){. sqli
15bd1 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b te3BtreeRollback
15bd2 28 70 54 6f 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 (pTo);. }.. re
15bd3 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c turn rc; .}.SQL
15bd4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15bd5 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 sqlite3BtreeCopy
15bd6 46 69 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c File(Btree *pTo,
15bd7 20 42 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a Btree *pFrom){.
15bd8 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 int rc;. sqli
15bd9 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 54 te3BtreeEnter(pT
15bda 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 o);. sqlite3Btr
15bdb 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29 3b 0a eeEnter(pFrom);.
15bdc 20 20 72 63 20 3d 20 62 74 72 65 65 43 6f 70 79 rc = btreeCopy
15bdd 46 69 6c 65 28 70 54 6f 2c 20 70 46 72 6f 6d 29 File(pTo, pFrom)
15bde 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
15bdf 4c 65 61 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20 Leave(pFrom);.
15be0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
15be1 65 28 70 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e e(pTo);. return
15be2 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f rc;.}..#endif /
15be3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 * SQLITE_OMIT_VA
15be4 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 CUUM */../*.** R
15be5 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 eturn non-zero i
15be6 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
15be7 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 is active..*/.SQ
15be8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15be9 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
15bea 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29 nTrans(Btree *p)
15beb 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 {. assert( p==0
15bec 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
15bed 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
15bee 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e tex) );. return
15bef 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72 61 (p && (p->inTra
15bf0 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 ns==TRANS_WRITE)
15bf1 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
15bf2 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 rn non-zero if a
15bf3 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
15bf4 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
15bf5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
15bf6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
15bf7 74 72 65 65 49 73 49 6e 53 74 6d 74 28 42 74 72 treeIsInStmt(Btr
15bf8 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ee *p){. assert
15bf9 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
15bfa 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
15bfb 20 72 65 74 75 72 6e 20 28 70 2d 3e 70 42 74 20 return (p->pBt
15bfc 26 26 20 70 2d 3e 70 42 74 2d 3e 69 6e 53 74 6d && p->pBt->inStm
15bfd 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 t);.}../*.** Ret
15bfe 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 urn non-zero if
15bff 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65 a read (or write
15c00 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 ) transaction is
15c01 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 active..*/.SQLI
15c02 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
15c03 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 qlite3BtreeIsInR
15c04 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a eadTrans(Btree *
15c05 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 p){. assert( sq
15c06 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15c07 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
15c08 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 ;. return (p &&
15c09 20 28 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 (p->inTrans!=TR
15c0a 41 4e 53 5f 4e 4f 4e 45 29 29 3b 0a 7d 0a 0a 2f ANS_NONE));.}../
15c0b 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
15c0c 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 on returns a poi
15c0d 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 62 20 6f nter to a blob o
15c0e 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 f memory associa
15c0f 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 ted with.** a si
15c10 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 74 72 65 ngle shared-btre
15c11 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 20 69 73 e. The memory is
15c12 20 75 73 65 64 20 62 79 20 63 6c 69 65 6e 74 20 used by client
15c13 63 6f 64 65 20 66 6f 72 20 69 74 73 20 6f 77 6e code for its own
15c14 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 28 66 6f .** purposes (fo
15c15 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 73 74 r example, to st
15c16 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 76 65 6c ore a high-level
15c17 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 schema associat
15c18 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 20 ed with .** the
15c19 73 68 61 72 65 64 2d 62 74 72 65 65 29 2e 20 54 shared-btree). T
15c1a 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d he btree layer m
15c1b 61 6e 61 67 65 73 20 72 65 66 65 72 65 6e 63 65 anages reference
15c1c 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 75 65 73 counting issues
15c1d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ..**.** The firs
15c1e 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 t time this is c
15c1f 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 65 alled on a share
15c20 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 65 73 20 d-btree, nBytes
15c21 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a bytes of memory.
15c22 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 ** are allocated
15c23 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20 72 65 , zeroed, and re
15c24 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 turned to the ca
15c25 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 68 20 73 ller. For each s
15c26 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20 63 61 ubsequent .** ca
15c27 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 20 70 61 ll the nBytes pa
15c28 72 61 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72 rameter is ignor
15c29 65 64 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 ed and a pointer
15c2a 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f to the same blo
15c2b 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 72 b.** of memory r
15c2c 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 eturned. .**.**
15c2d 49 66 20 74 68 65 20 6e 42 79 74 65 73 20 70 61 If the nBytes pa
15c2e 72 61 6d 65 74 65 72 20 69 73 20 30 20 61 6e 64 rameter is 0 and
15c2f 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d the blob of mem
15c30 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 65 74 20 ory has not yet
15c31 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 been.** allocate
15c32 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 d, a null pointe
15c33 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 r is returned. I
15c34 66 20 74 68 65 20 62 6c 6f 62 20 68 61 73 20 61 f the blob has a
15c35 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a 20 61 lready been.** a
15c36 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 69 73 20 llocated, it is
15c37 72 65 74 75 72 6e 65 64 20 61 73 20 6e 6f 72 6d returned as norm
15c38 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62 al..**.** Just b
15c39 65 66 6f 72 65 20 74 68 65 20 73 68 61 72 65 64 efore the shared
15c3a 2d 62 74 72 65 65 20 69 73 20 63 6c 6f 73 65 64 -btree is closed
15c3b 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 , the function p
15c3c 61 73 73 65 64 20 61 73 20 74 68 65 20 0a 2a 2a assed as the .**
15c3d 20 78 46 72 65 65 20 61 72 67 75 6d 65 6e 74 20 xFree argument
15c3e 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 when the memory
15c3f 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6d allocation was m
15c40 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f ade is invoked o
15c41 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f n the .** blob o
15c42 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f f allocated memo
15c43 72 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f ry. This functio
15c44 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 61 6c n should not cal
15c45 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 l sqlite3_free()
15c46 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 .** on the memor
15c47 79 2c 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 y, the btree lay
15c48 65 72 20 64 6f 65 73 20 74 68 61 74 2e 0a 2a 2f er does that..*/
15c49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15c4a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 void *sqlite3Btr
15c4b 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a eeSchema(Btree *
15c4c 70 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 76 p, int nBytes, v
15c4d 6f 69 64 28 2a 78 46 72 65 65 29 28 76 6f 69 64 oid(*xFree)(void
15c4e 20 2a 29 29 7b 0a 20 20 42 74 53 68 61 72 65 64 *)){. BtShared
15c4f 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
15c50 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15c51 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 21 70 ter(p);. if( !p
15c52 42 74 2d 3e 70 53 63 68 65 6d 61 20 26 26 20 6e Bt->pSchema && n
15c53 42 79 74 65 73 20 29 7b 0a 20 20 20 20 70 42 74 Bytes ){. pBt
15c54 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 ->pSchema = sqli
15c55 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 te3MallocZero(nB
15c56 79 74 65 73 29 3b 0a 20 20 20 20 70 42 74 2d 3e ytes);. pBt->
15c57 78 46 72 65 65 53 63 68 65 6d 61 20 3d 20 78 46 xFreeSchema = xF
15c58 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ree;. }. sqlit
15c59 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
15c5a 0a 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 70 . return pBt->p
15c5b 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Schema;.}../*.**
15c5c 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
15c5d 61 6e 6f 74 68 65 72 20 75 73 65 72 20 6f 66 20 another user of
15c5e 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64 20 the same shared
15c5f 62 74 72 65 65 20 61 73 20 74 68 65 20 61 72 67 btree as the arg
15c60 75 6d 65 6e 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 ument.** handle
15c61 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75 73 69 holds an exclusi
15c62 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 ve lock on the s
15c63 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
15c64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
15c65 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15c66 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 3BtreeSchemaLock
15c67 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 ed(Btree *p){.
15c68 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
15c69 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
15c6a 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
15c6b 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 x) );. sqlite3B
15c6c 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
15c6d 72 63 20 3d 20 28 71 75 65 72 79 54 61 62 6c 65 rc = (queryTable
15c6e 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 Lock(p, MASTER_R
15c6f 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 21 OOT, READ_LOCK)!
15c70 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 73 =SQLITE_OK);. s
15c71 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
15c72 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
15c73 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 ;.}...#ifndef SQ
15c74 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
15c75 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 _CACHE./*.** Obt
15c76 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 ain a lock on th
15c77 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f e table whose ro
15c78 6f 74 20 70 61 67 65 20 69 73 20 69 54 61 62 2e ot page is iTab.
15c79 20 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 The.** lock is
15c7a 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 a write lock if
15c7b 20 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73 20 isWritelock is
15c7c 74 72 75 65 20 6f 72 20 61 20 72 65 61 64 20 6c true or a read l
15c7d 6f 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 ock.** if it is
15c7e 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 false..*/.SQLITE
15c7f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15c80 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 ite3BtreeLockTab
15c81 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 le(Btree *p, int
15c82 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 iTab, u8 isWrit
15c83 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 eLock){. int rc
15c84 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
15c85 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
15c86 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 79 ){. u8 lockTy
15c87 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b pe = READ_LOCK +
15c88 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 isWriteLock;.
15c89 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f 4c assert( READ_L
15c8a 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 OCK+1==WRITE_LOC
15c8b 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 K );. assert(
15c8c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 isWriteLock==0
15c8d 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d || isWriteLock==
15c8e 31 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 1 );. sqlite3
15c8f 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
15c90 20 20 20 72 63 20 3d 20 71 75 65 72 79 54 61 62 rc = queryTab
15c91 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 leLock(p, iTab,
15c92 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 69 lockType);. i
15c93 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15c94 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c ){. rc = l
15c95 6f 63 6b 54 61 62 6c 65 28 70 2c 20 69 54 61 62 ockTable(p, iTab
15c96 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 , lockType);.
15c97 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 }. sqlite3Bt
15c98 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d reeLeave(p);. }
15c99 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15c9a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
15c9b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
15c9c 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d BLOB./*.** Argum
15c9d 65 6e 74 20 70 43 73 72 20 6d 75 73 74 20 62 65 ent pCsr must be
15c9e 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 a cursor opened
15c9f 20 66 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 for writing on
15ca0 61 6e 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 an .** INTKEY ta
15ca1 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f ble currently po
15ca2 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 inting at a vali
15ca3 64 20 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a d table entry. .
15ca4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
15ca5 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20 64 61 modifies the da
15ca6 74 61 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 ta stored as par
15ca7 74 20 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e t of that entry.
15ca8 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 .** Only the dat
15ca9 61 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e a content may on
15caa 6c 79 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 ly be modified,
15cab 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 it is not possib
15cac 6c 65 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 le.** to change
15cad 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 the length of th
15cae 65 20 64 61 74 61 20 73 74 6f 72 65 64 2e 0a 2a e data stored..*
15caf 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
15cb0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
15cb1 65 50 75 74 44 61 74 61 28 42 74 43 75 72 73 6f ePutData(BtCurso
15cb2 72 20 2a 70 43 73 72 2c 20 75 33 32 20 6f 66 66 r *pCsr, u32 off
15cb3 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f set, u32 amt, vo
15cb4 69 64 20 2a 7a 29 7b 0a 20 20 61 73 73 65 72 74 id *z){. assert
15cb5 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
15cb6 65 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 73 ex(pCsr) );. as
15cb7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15cb8 74 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70 tex_held(pCsr->p
15cb9 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
15cba 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 43 ) );. assert(pC
15cbb 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 sr->isIncrblobHa
15cbc 6e 64 6c 65 29 3b 0a 0a 20 20 72 65 73 74 6f 72 ndle);.. restor
15cbd 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
15cbe 70 43 73 72 29 3b 0a 20 20 61 73 73 65 72 74 28 pCsr);. assert(
15cbf 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCsr->eState!=C
15cc0 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
15cc1 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72 2d K );. if( pCsr-
15cc2 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
15cc3 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 VALID ){. ret
15cc4 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 urn SQLITE_ABORT
15cc5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
15cc6 6b 20 73 6f 6d 65 20 70 72 65 63 6f 6e 64 69 74 k some precondit
15cc7 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61 ions: . ** (a
15cc8 29 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 ) the cursor is
15cc9 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 open for writing
15cca 2c 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65 ,. ** (b) the
15ccb 72 65 20 69 73 20 6e 6f 20 72 65 61 64 2d 6c 6f re is no read-lo
15ccc 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 ck on the table
15ccd 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20 61 being modified a
15cce 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 68 nd. ** (c) th
15ccf 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 e cursor points
15cd0 61 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20 6f at a valid row o
15cd1 66 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c f an intKey tabl
15cd2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 e.. */. if( !p
15cd3 43 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 Csr->wrFlag ){.
15cd4 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15cd5 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 _READONLY;. }.
15cd6 20 61 73 73 65 72 74 28 20 21 70 43 73 72 2d 3e assert( !pCsr->
15cd7 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 0a 20 pBt->readOnly .
15cd8 20 20 20 20 20 20 20 20 20 26 26 20 70 43 73 72 && pCsr
15cd9 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ->pBt->inTransac
15cda 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
15cdb 45 20 29 3b 0a 20 20 69 66 28 20 63 68 65 63 6b E );. if( check
15cdc 52 65 61 64 4c 6f 63 6b 73 28 70 43 73 72 2d 3e ReadLocks(pCsr->
15cdd 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 pBtree, pCsr->pg
15cde 6e 6f 52 6f 6f 74 2c 20 70 43 73 72 2c 20 30 29 noRoot, pCsr, 0)
15cdf 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
15ce0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a QLITE_LOCKED; /*
15ce1 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20 The table pCur
15ce2 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20 points to has a
15ce3 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d read lock */. }
15ce4 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53 74 . if( pCsr->eSt
15ce5 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
15ce6 4c 49 44 20 7c 7c 20 21 70 43 73 72 2d 3e 61 70 LID || !pCsr->ap
15ce7 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67 65 Page[pCsr->iPage
15ce8 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 ]->intKey ){.
15ce9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
15cea 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 RROR;. }.. ret
15ceb 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 urn accessPayloa
15cec 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20 d(pCsr, offset,
15ced 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 amt, (unsigned c
15cee 68 61 72 20 2a 29 7a 2c 20 30 2c 20 31 29 3b 0a har *)z, 0, 1);.
15cef 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20 }../* .** Set a
15cf0 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75 72 flag on this cur
15cf1 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68 65 sor to cache the
15cf2 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70 61 locations of pa
15cf3 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a ges from the .**
15cf4 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 66 overflow list f
15cf5 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 or the current r
15cf6 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 ow. This is used
15cf7 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65 6e by cursors open
15cf8 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d ed.** for increm
15cf9 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e ental blob IO on
15cfa 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ly..**.** This f
15cfb 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20 66 unction sets a f
15cfc 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63 lag only. The ac
15cfd 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74 69 tual page locati
15cfe 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f on cache.** (sto
15cff 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e red in BtCursor.
15d00 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20 aOverflow[]) is
15d01 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75 73 allocated and us
15d02 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a ed by function.*
15d03 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 * accessPayload(
15d04 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66 75 ) (the worker fu
15d05 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74 nction for sqlit
15d06 65 33 42 74 72 65 65 44 61 74 61 28 29 20 61 6e e3BtreeData() an
15d07 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 d.** sqlite3Btre
15d08 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a ePutData())..*/.
15d09 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
15d0a 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
15d0b 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 CacheOverflow(Bt
15d0c 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
15d0d 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
15d0e 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
15d0f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
15d10 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15d11 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
15d12 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
15d13 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49 6e sert(!pCur->isIn
15d14 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 crblobHandle);.
15d15 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 assert(!pCur->a
15d16 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 Overflow);. pCu
15d17 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e r->isIncrblobHan
15d18 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 dle = 1;.}.#endi
15d19 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
15d1a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e ** End of btree.
15d1b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
15d1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15d1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15d1e 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
15d1f 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 ** Begin file vd
15d20 62 65 66 69 66 6f 2e 63 20 2a 2a 2a 2a 2a 2a 2a befifo.c *******
15d21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15d22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15d23 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 */./*.** 2005 Ju
15d24 6e 65 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ne 16.**.** The
15d25 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
15d26 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
15d27 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
15d28 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
15d29 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
15d2a 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
15d2b 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
15d2c 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
15d2d 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
15d2e 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
15d2f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
15d30 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
15d31 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
15d32 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
15d33 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
15d34 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
15d35 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
15d36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15d37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15d38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15d39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15d3a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
15d3b 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 46 e implements a F
15d3c 49 46 4f 20 71 75 65 75 65 20 6f 66 20 72 6f 77 IFO queue of row
15d3d 69 64 73 20 75 73 65 64 20 66 6f 72 20 70 72 6f ids used for pro
15d3e 63 65 73 73 69 6e 67 0a 2a 2a 20 55 50 44 41 54 cessing.** UPDAT
15d3f 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61 E and DELETE sta
15d40 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 tements..**.** $
15d41 49 64 3a 20 76 64 62 65 66 69 66 6f 2e 63 2c 76 Id: vdbefifo.c,v
15d42 20 31 2e 38 20 32 30 30 38 2f 30 37 2f 32 38 20 1.8 2008/07/28
15d43 31 39 3a 33 34 3a 35 34 20 64 72 68 20 45 78 70 19:34:54 drh Exp
15d44 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e $.*/../*.** Con
15d45 73 74 61 6e 74 73 20 46 49 46 4f 53 49 5a 45 5f stants FIFOSIZE_
15d46 46 49 52 53 54 20 61 6e 64 20 46 49 46 4f 53 49 FIRST and FIFOSI
15d47 5a 45 5f 4d 41 58 20 61 72 65 20 74 68 65 20 69 ZE_MAX are the i
15d48 6e 69 74 69 61 6c 0a 2a 2a 20 6e 75 6d 62 65 72 nitial.** number
15d49 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
15d4a 20 66 69 66 6f 20 70 61 67 65 20 61 6e 64 20 74 fifo page and t
15d4b 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
15d4c 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 65 73 20 r of.** entries
15d4d 69 6e 20 61 20 66 69 66 6f 20 70 61 67 65 2e 0a in a fifo page..
15d4e 2a 2f 0a 23 64 65 66 69 6e 65 20 46 49 46 4f 53 */.#define FIFOS
15d4f 49 5a 45 5f 46 49 52 53 54 20 28 28 28 31 32 38 IZE_FIRST (((128
15d50 2d 73 69 7a 65 6f 66 28 46 69 66 6f 50 61 67 65 -sizeof(FifoPage
15d51 29 29 2f 38 29 2b 31 29 0a 23 69 66 64 65 66 20 ))/8)+1).#ifdef
15d52 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f SQLITE_MALLOC_SO
15d53 46 54 5f 4c 49 4d 49 54 0a 23 20 64 65 66 69 6e FT_LIMIT.# defin
15d54 65 20 46 49 46 4f 53 49 5a 45 5f 4d 41 58 20 20 e FIFOSIZE_MAX
15d55 20 28 28 28 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f (((SQLITE_MALLO
15d56 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 2d 73 69 7a C_SOFT_LIMIT-siz
15d57 65 6f 66 28 46 69 66 6f 50 61 67 65 29 29 2f 38 eof(FifoPage))/8
15d58 29 2b 31 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 )+1).#else.# def
15d59 69 6e 65 20 46 49 46 4f 53 49 5a 45 5f 4d 41 58 ine FIFOSIZE_MAX
15d5a 20 20 20 28 28 28 32 36 32 31 34 34 2d 73 69 7a (((262144-siz
15d5b 65 6f 66 28 46 69 66 6f 50 61 67 65 29 29 2f 38 eof(FifoPage))/8
15d5c 29 2b 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a )+1).#endif../*.
15d5d 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 ** Allocate a ne
15d5e 77 20 46 69 66 6f 50 61 67 65 20 61 6e 64 20 72 w FifoPage and r
15d5f 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
15d60 74 6f 20 69 74 2e 20 20 52 65 74 75 72 6e 20 4e to it. Return N
15d61 55 4c 4c 20 69 66 0a 2a 2a 20 77 65 20 72 75 6e ULL if.** we run
15d62 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 out of memory.
15d63 20 4c 65 61 76 65 20 73 70 61 63 65 20 6f 6e 20 Leave space on
15d64 74 68 65 20 70 61 67 65 20 66 6f 72 20 6e 45 6e the page for nEn
15d65 74 72 79 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a try entries..*/.
15d66 73 74 61 74 69 63 20 46 69 66 6f 50 61 67 65 20 static FifoPage
15d67 2a 61 6c 6c 6f 63 61 74 65 46 69 66 6f 50 61 67 *allocateFifoPag
15d68 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 e(sqlite3 *db, i
15d69 6e 74 20 6e 45 6e 74 72 79 29 7b 0a 20 20 46 69 nt nEntry){. Fi
15d6a 66 6f 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 foPage *pPage;.
15d6b 20 69 66 28 20 6e 45 6e 74 72 79 3e 46 49 46 4f if( nEntry>FIFO
15d6c 53 49 5a 45 5f 4d 41 58 20 29 7b 0a 20 20 20 20 SIZE_MAX ){.
15d6d 6e 45 6e 74 72 79 20 3d 20 46 49 46 4f 53 49 5a nEntry = FIFOSIZ
15d6e 45 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 70 50 61 E_MAX;. }. pPa
15d6f 67 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ge = sqlite3DbMa
15d70 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 llocRaw(db, size
15d71 6f 66 28 46 69 66 6f 50 61 67 65 29 20 2b 20 73 of(FifoPage) + s
15d72 69 7a 65 6f 66 28 69 36 34 29 2a 28 6e 45 6e 74 izeof(i64)*(nEnt
15d73 72 79 2d 31 29 20 29 3b 0a 20 20 69 66 28 20 70 ry-1) );. if( p
15d74 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 Page ){. pPag
15d75 65 2d 3e 6e 53 6c 6f 74 20 3d 20 6e 45 6e 74 72 e->nSlot = nEntr
15d76 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 57 y;. pPage->iW
15d77 72 69 74 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 rite = 0;. pP
15d78 61 67 65 2d 3e 69 52 65 61 64 20 3d 20 30 3b 0a age->iRead = 0;.
15d79 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 pPage->pNext
15d7a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 0;. }. retu
15d7b 72 6e 20 70 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a rn pPage;.}../*.
15d7c 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 ** Initialize a
15d7d 46 69 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a Fifo structure..
15d7e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
15d7f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
15d80 62 65 46 69 66 6f 49 6e 69 74 28 46 69 66 6f 20 beFifoInit(Fifo
15d81 2a 70 46 69 66 6f 2c 20 73 71 6c 69 74 65 33 20 *pFifo, sqlite3
15d82 2a 64 62 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 *db){. memset(p
15d83 46 69 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Fifo, 0, sizeof(
15d84 2a 70 46 69 66 6f 29 29 3b 0a 20 20 70 46 69 66 *pFifo));. pFif
15d85 6f 2d 3e 64 62 20 3d 20 64 62 3b 0a 7d 0a 0a 2f o->db = db;.}../
15d86 2a 0a 2a 2a 20 50 75 73 68 20 61 20 73 69 6e 67 *.** Push a sing
15d87 6c 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 le 64-bit intege
15d88 72 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 r value into the
15d89 20 46 69 66 6f 2e 20 20 52 65 74 75 72 6e 20 53 Fifo. Return S
15d8a 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6e 6f 72 6d QLITE_OK.** norm
15d8b 61 6c 6c 79 2e 20 20 20 53 51 4c 49 54 45 5f 4e ally. SQLITE_N
15d8c 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
15d8d 20 69 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c if we are unabl
15d8e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a e to allocate.**
15d8f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 memory..*/.SQLI
15d90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
15d91 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f 50 75 qlite3VdbeFifoPu
15d92 73 68 28 46 69 66 6f 20 2a 70 46 69 66 6f 2c 20 sh(Fifo *pFifo,
15d93 69 36 34 20 76 61 6c 29 7b 0a 20 20 46 69 66 6f i64 val){. Fifo
15d94 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 Page *pPage;. p
15d95 50 61 67 65 20 3d 20 70 46 69 66 6f 2d 3e 70 4c Page = pFifo->pL
15d96 61 73 74 3b 0a 20 20 69 66 28 20 70 50 61 67 65 ast;. if( pPage
15d97 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 ==0 ){. pPage
15d98 20 3d 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20 = pFifo->pLast
15d99 3d 20 70 46 69 66 6f 2d 3e 70 46 69 72 73 74 20 = pFifo->pFirst
15d9a 3d 0a 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 63 =. alloc
15d9b 61 74 65 46 69 66 6f 50 61 67 65 28 70 46 69 66 ateFifoPage(pFif
15d9c 6f 2d 3e 64 62 2c 20 46 49 46 4f 53 49 5a 45 5f o->db, FIFOSIZE_
15d9d 46 49 52 53 54 29 3b 0a 20 20 20 20 69 66 28 20 FIRST);. if(
15d9e 70 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 pPage==0 ){.
15d9f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15da0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d NOMEM;. }. }
15da1 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e else if( pPage->
15da2 69 57 72 69 74 65 3e 3d 70 50 61 67 65 2d 3e 6e iWrite>=pPage->n
15da3 53 6c 6f 74 20 29 7b 0a 20 20 20 20 70 50 61 67 Slot ){. pPag
15da4 65 2d 3e 70 4e 65 78 74 20 3d 20 61 6c 6c 6f 63 e->pNext = alloc
15da5 61 74 65 46 69 66 6f 50 61 67 65 28 70 46 69 66 ateFifoPage(pFif
15da6 6f 2d 3e 64 62 2c 20 70 46 69 66 6f 2d 3e 6e 45 o->db, pFifo->nE
15da7 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28 20 70 ntry);. if( p
15da8 50 61 67 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29 Page->pNext==0 )
15da9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
15daa 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
15dab 20 7d 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 }. pPage = p
15dac 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 70 50 Fifo->pLast = pP
15dad 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a age->pNext;. }.
15dae 20 20 70 50 61 67 65 2d 3e 61 53 6c 6f 74 5b 70 pPage->aSlot[p
15daf 50 61 67 65 2d 3e 69 57 72 69 74 65 2b 2b 5d 20 Page->iWrite++]
15db0 3d 20 76 61 6c 3b 0a 20 20 70 46 69 66 6f 2d 3e = val;. pFifo->
15db1 6e 45 6e 74 72 79 2b 2b 3b 0a 20 20 72 65 74 75 nEntry++;. retu
15db2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
15db3 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61 ./*.** Extract a
15db4 20 73 69 6e 67 6c 65 20 36 34 2d 62 69 74 20 69 single 64-bit i
15db5 6e 74 65 67 65 72 20 76 61 6c 75 65 20 66 72 6f nteger value fro
15db6 6d 20 74 68 65 20 46 69 66 6f 2e 20 20 54 68 65 m the Fifo. The
15db7 20 69 6e 74 65 67 65 72 0a 2a 2a 20 65 78 74 72 integer.** extr
15db8 61 63 74 65 64 20 69 73 20 74 68 65 20 6f 6e 65 acted is the one
15db9 20 6c 65 61 73 74 20 72 65 63 65 6e 74 6c 79 20 least recently
15dba 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74 68 inserted. If th
15dbb 65 20 46 69 66 6f 20 69 73 20 65 6d 70 74 79 0a e Fifo is empty.
15dbc 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ** return SQLITE
15dbd 5f 44 4f 4e 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 _DONE..*/.SQLITE
15dbe 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15dbf 69 74 65 33 56 64 62 65 46 69 66 6f 50 6f 70 28 ite3VdbeFifoPop(
15dc0 46 69 66 6f 20 2a 70 46 69 66 6f 2c 20 69 36 34 Fifo *pFifo, i64
15dc1 20 2a 70 56 61 6c 29 7b 0a 20 20 46 69 66 6f 50 *pVal){. FifoP
15dc2 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 66 age *pPage;. if
15dc3 28 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d ( pFifo->nEntry=
15dc4 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
15dc5 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
15dc6 7d 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 66 }. assert( pFif
15dc7 6f 2d 3e 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20 o->nEntry>0 );.
15dc8 20 70 50 61 67 65 20 3d 20 70 46 69 66 6f 2d 3e pPage = pFifo->
15dc9 70 46 69 72 73 74 3b 0a 20 20 61 73 73 65 72 74 pFirst;. assert
15dca 28 20 70 50 61 67 65 21 3d 30 20 29 3b 0a 20 20 ( pPage!=0 );.
15dcb 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
15dcc 57 72 69 74 65 3e 70 50 61 67 65 2d 3e 69 52 65 Write>pPage->iRe
15dcd 61 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ad );. assert(
15dce 70 50 61 67 65 2d 3e 69 57 72 69 74 65 3c 3d 70 pPage->iWrite<=p
15dcf 50 61 67 65 2d 3e 6e 53 6c 6f 74 20 29 3b 0a 20 Page->nSlot );.
15dd0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15dd1 69 52 65 61 64 3c 70 50 61 67 65 2d 3e 6e 53 6c iRead<pPage->nSl
15dd2 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ot );. assert(
15dd3 70 50 61 67 65 2d 3e 69 52 65 61 64 3e 3d 30 20 pPage->iRead>=0
15dd4 29 3b 0a 20 20 2a 70 56 61 6c 20 3d 20 70 50 61 );. *pVal = pPa
15dd5 67 65 2d 3e 61 53 6c 6f 74 5b 70 50 61 67 65 2d ge->aSlot[pPage-
15dd6 3e 69 52 65 61 64 2b 2b 5d 3b 0a 20 20 70 46 69 >iRead++];. pFi
15dd7 66 6f 2d 3e 6e 45 6e 74 72 79 2d 2d 3b 0a 20 20 fo->nEntry--;.
15dd8 69 66 28 20 70 50 61 67 65 2d 3e 69 52 65 61 64 if( pPage->iRead
15dd9 3e 3d 70 50 61 67 65 2d 3e 69 57 72 69 74 65 20 >=pPage->iWrite
15dda 29 7b 0a 20 20 20 20 70 46 69 66 6f 2d 3e 70 46 ){. pFifo->pF
15ddb 69 72 73 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e irst = pPage->pN
15ddc 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ext;. sqlite3
15ddd 44 62 46 72 65 65 28 70 46 69 66 6f 2d 3e 64 62 DbFree(pFifo->db
15dde 2c 20 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 , pPage);. if
15ddf 28 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d ( pFifo->nEntry=
15de0 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
15de1 72 74 28 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 rt( pFifo->pLast
15de2 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20 ==pPage );.
15de3 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 pFifo->pLast =
15de4 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
15de5 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 66 assert( pFif
15de6 6f 2d 3e 70 46 69 72 73 74 21 3d 30 20 29 3b 0a o->pFirst!=0 );.
15de7 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
15de8 20 20 20 61 73 73 65 72 74 28 20 70 46 69 66 6f assert( pFifo
15de9 2d 3e 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20 20 ->nEntry>0 );.
15dea 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
15deb 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 E_OK;.}../*.** D
15dec 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d elete all inform
15ded 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 46 69 66 ation from a Fif
15dee 6f 20 6f 62 6a 65 63 74 2e 20 20 20 46 72 65 65 o object. Free
15def 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 68 65 6c 64 all memory held
15df0 0a 2a 2a 20 62 79 20 74 68 65 20 46 69 66 6f 2e .** by the Fifo.
15df1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15df2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
15df3 64 62 65 46 69 66 6f 43 6c 65 61 72 28 46 69 66 dbeFifoClear(Fif
15df4 6f 20 2a 70 46 69 66 6f 29 7b 0a 20 20 46 69 66 o *pFifo){. Fif
15df5 6f 50 61 67 65 20 2a 70 50 61 67 65 2c 20 2a 70 oPage *pPage, *p
15df6 4e 65 78 74 50 61 67 65 3b 0a 20 20 66 6f 72 28 NextPage;. for(
15df7 70 50 61 67 65 3d 70 46 69 66 6f 2d 3e 70 46 69 pPage=pFifo->pFi
15df8 72 73 74 3b 20 70 50 61 67 65 3b 20 70 50 61 67 rst; pPage; pPag
15df9 65 3d 70 4e 65 78 74 50 61 67 65 29 7b 0a 20 20 e=pNextPage){.
15dfa 20 20 70 4e 65 78 74 50 61 67 65 20 3d 20 70 50 pNextPage = pP
15dfb 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 age->pNext;.
15dfc 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 46 sqlite3DbFree(pF
15dfd 69 66 6f 2d 3e 64 62 2c 20 70 50 61 67 65 29 3b ifo->db, pPage);
15dfe 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 . }. sqlite3Vd
15dff 62 65 46 69 66 6f 49 6e 69 74 28 70 46 69 66 6f beFifoInit(pFifo
15e00 2c 20 70 46 69 66 6f 2d 3e 64 62 29 3b 0a 7d 0a , pFifo->db);.}.
15e01 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
15e02 20 45 6e 64 20 6f 66 20 76 64 62 65 66 69 66 6f End of vdbefifo
15e03 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
15e04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15e05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
15e06 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
15e07 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 Begin file vdbe
15e08 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mem.c **********
15e09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15e0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
15e0b 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
15e0c 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 26.**.** The aut
15e0d 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
15e0e 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
15e0f 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
15e10 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
15e11 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
15e12 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
15e13 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
15e14 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
15e15 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
15e16 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
15e17 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
15e18 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
15e19 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
15e1a 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
15e1b 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
15e1c 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
15e1d 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
15e1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15e1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15e21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15e22 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
15e23 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
15e24 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 use to manipulat
15e25 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75 72 e "Mem" structur
15e26 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 e. A "Mem".** s
15e27 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20 76 tores a single v
15e28 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42 45 alue in the VDBE
15e29 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 . Mem is an opa
15e2a 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76 69 que structure vi
15e2b 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 sible.** only wi
15e2c 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 thin the VDBE.
15e2d 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e Interface routin
15e2e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d 65 es refer to a Me
15e2f 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e m using the.** n
15e30 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 ame sqlite_value
15e31 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
15e32 6d 65 6d 2e 63 2c 76 20 31 2e 31 32 35 20 32 30 mem.c,v 1.125 20
15e33 30 38 2f 31 31 2f 30 35 20 31 37 3a 34 31 3a 31 08/11/05 17:41:1
15e34 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 9 drh Exp $.*/..
15e35 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 /*.** Call sqlit
15e36 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 e3VdbeMemExpandB
15e37 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 lob() on the sup
15e38 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 plied value (typ
15e39 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 e Mem*).** P if
15e3a 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 required..*/.#de
15e3b 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 fine expandBlob(
15e3c 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 P) (((P)->flags&
15e3d 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 MEM_Zero)?sqlite
15e3e 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 3VdbeMemExpandBl
15e3f 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 ob(P):0)../*.**
15e40 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 If pMem is an ob
15e41 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 6c 69 ject with a vali
15e42 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 d string represe
15e43 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f ntation, this ro
15e44 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 utine.** ensures
15e45 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e the internal en
15e46 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73 coding for the s
15e47 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 tring representa
15e48 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 tion is.** 'desi
15e49 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 redEnc', one of
15e4a 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c SQLITE_UTF8, SQL
15e4b 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 ITE_UTF16LE or S
15e4c 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a QLITE_UTF16BE..*
15e4d 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 *.** If pMem is
15e4e 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a not a string obj
15e4f 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f ect, or the enco
15e50 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69 ding of the stri
15e51 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 ng.** representa
15e52 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 tion is already
15e53 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65 stored using the
15e54 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64 requested encod
15e55 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a ing, then this.*
15e56 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e * routine is a n
15e57 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 o-op..**.** SQLI
15e58 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
15e59 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 d if the convers
15e5a 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 ion is successfu
15e5b 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 l (or not requir
15e5c 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e ed)..** SQLITE_N
15e5d 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 OMEM may be retu
15e5e 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 rned if a malloc
15e5f 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 () fails during
15e60 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 conversion.** be
15e61 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a tween formats..*
15e62 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
15e63 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
15e64 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d ChangeEncoding(M
15e65 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 em *pMem, int de
15e66 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 siredEnc){. int
15e67 20 72 63 3b 0a 20 20 69 66 28 20 21 28 70 4d 65 rc;. if( !(pMe
15e68 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 m->flags&MEM_Str
15e69 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d ) || pMem->enc==
15e6a 64 65 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20 desiredEnc ){.
15e6b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15e6c 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 OK;. }. assert
15e6d 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
15e6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15e6f 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
15e70 74 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 tex) );.#ifdef S
15e71 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
15e72 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15e73 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20 _ERROR;.#else..
15e74 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65 /* MemTranslate
15e75 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 () may return SQ
15e76 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 LITE_OK or SQLIT
15e77 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45 E_NOMEM. If NOME
15e78 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20 M is returned,.
15e79 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63 ** then the enc
15e7a 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c oding of the val
15e7b 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 ue may not have
15e7c 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 changed.. */.
15e7d 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
15e7e 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65 MemTranslate(pMe
15e7f 6d 2c 20 64 65 73 69 72 65 64 45 6e 63 29 3b 0a m, desiredEnc);.
15e80 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c assert(rc==SQL
15e81 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d ITE_OK || rc=
15e82 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a =SQLITE_NOMEM);.
15e83 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c assert(rc==SQL
15e84 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 ITE_OK || pMe
15e85 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 m->enc!=desiredE
15e86 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 nc);. assert(rc
15e87 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c ==SQLITE_NOMEM |
15e88 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 | pMem->enc==des
15e89 69 72 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 iredEnc);. retu
15e8a 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a rn rc;.#endif.}.
15e8b 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
15e8c 20 70 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 pMem->z points
15e8d 74 6f 20 61 20 77 72 69 74 61 62 6c 65 20 61 6c to a writable al
15e8e 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c location of at l
15e8f 65 61 73 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 east .** n bytes
15e90 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d ..**.** If the m
15e91 65 6d 6f 72 79 20 63 65 6c 6c 20 63 75 72 72 65 emory cell curre
15e92 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 ntly contains st
15e93 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 ring or blob dat
15e94 61 0a 2a 2a 20 61 6e 64 20 74 68 65 20 74 68 69 a.** and the thi
15e95 72 64 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 rd argument pass
15e96 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 ed to this funct
15e97 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65 ion is true, the
15e98 20 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e .** current con
15e99 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c tent of the cell
15e9a 20 69 73 20 70 72 65 73 65 72 76 65 64 2e 20 4f is preserved. O
15e9b 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 therwise, it may
15e9c 0a 2a 2a 20 62 65 20 64 69 73 63 61 72 64 65 64 .** be discarded
15e9d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 . .**.** This f
15e9e 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 unction sets the
15e9f 20 4d 45 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e MEM_Dyn flag an
15ea0 64 20 63 6c 65 61 72 73 20 61 6e 79 20 78 44 65 d clears any xDe
15ea1 6c 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 l callback..** I
15ea2 74 20 61 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 t also clears ME
15ea3 4d 5f 45 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f M_Ephem and MEM_
15ea4 53 74 61 74 69 63 2e 20 49 66 20 74 68 65 20 70 Static. If the p
15ea5 72 65 73 65 72 76 65 20 66 6c 61 67 20 69 73 20 reserve flag is
15ea6 0a 2a 2a 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d .** not set, Mem
15ea7 2e 6e 20 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f .n is zeroed..*/
15ea8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15ea9 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
15eaa 65 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d emGrow(Mem *pMem
15eab 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 , int n, int pre
15eac 73 65 72 76 65 29 7b 0a 20 20 61 73 73 65 72 74 serve){. assert
15ead 28 20 31 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 ( 1 >=. ((pMe
15eae 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d m->zMalloc && pM
15eaf 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 em->zMalloc==pMe
15eb0 6d 2d 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b m->z) ? 1 : 0) +
15eb1 0a 20 20 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c . (((pMem->fl
15eb2 61 67 73 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d ags&MEM_Dyn)&&pM
15eb3 65 6d 2d 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 em->xDel) ? 1 :
15eb4 30 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 0) + . ((pMem
15eb5 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 ->flags&MEM_Ephe
15eb6 6d 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 m) ? 1 : 0) + .
15eb7 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 ((pMem->flags
15eb8 26 4d 45 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 &MEM_Static) ? 1
15eb9 20 3a 20 30 29 0a 20 20 29 3b 0a 0a 20 20 69 66 : 0). );.. if
15eba 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b ( n<32 ) n = 32;
15ebb 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 44 62 . if( sqlite3Db
15ebc 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d MallocSize(pMem-
15ebd 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c >db, pMem->zMall
15ebe 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28 oc)<n ){. if(
15ebf 20 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 preserve && pMe
15ec0 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c m->z==pMem->zMal
15ec1 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 loc ){. pMe
15ec2 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 m->z = pMem->zMa
15ec3 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 lloc = sqlite3Db
15ec4 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d ReallocOrFree(pM
15ec5 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c em->db, pMem->z,
15ec6 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 n);. if( !
15ec7 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 pMem->z ){.
15ec8 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
15ec9 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 MEM_Null;.
15eca 20 7d 0a 20 20 20 20 20 20 70 72 65 73 65 72 76 }. preserv
15ecb 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 e = 0;. }else
15ecc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
15ecd 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 bFree(pMem->db,
15ece 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a pMem->zMalloc);.
15ecf 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c pMem->zMal
15ed0 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d loc = sqlite3DbM
15ed1 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64 allocRaw(pMem->d
15ed2 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d b, n);. }. }
15ed3 0a 0a 20 20 69 66 28 20 70 72 65 73 65 72 76 65 .. if( preserve
15ed4 20 26 26 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 && pMem->z && p
15ed5 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 Mem->zMalloc &&
15ed6 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a pMem->z!=pMem->z
15ed7 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d 65 Malloc ){. me
15ed8 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c mcpy(pMem->zMall
15ed9 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d 65 oc, pMem->z, pMe
15eda 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 m->n);. }. if(
15edb 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d pMem->flags&MEM
15edc 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78 44 _Dyn && pMem->xD
15edd 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e el ){. pMem->
15ede 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70 4d xDel((void *)(pM
15edf 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 20 em->z));. }..
15ee0 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e pMem->z = pMem->
15ee1 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 70 4d 65 6d 2d zMalloc;. pMem-
15ee2 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
15ee3 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 Ephem|MEM_Static
15ee4 29 3b 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 );. pMem->xDel
15ee5 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 = 0;. return (p
15ee6 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f Mem->z ? SQLITE_
15ee7 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 OK : SQLITE_NOME
15ee8 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b M);.}../*.** Mak
15ee9 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 e the given Mem
15eea 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 object MEM_Dyn.
15eeb 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
15eec 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74 make it so.** t
15eed 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20 hat any TEXT or
15eee 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20 BLOB content is
15eef 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 stored in memory
15ef0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
15ef1 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 * malloc(). In
15ef2 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f this way, we kno
15ef3 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 w that the memor
15ef4 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a y is safe to be.
15ef5 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f ** overwritten o
15ef6 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a r altered..**.**
15ef7 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
15ef8 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 K on success or
15ef9 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 SQLITE_NOMEM if
15efa 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f malloc fails..*/
15efb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15efc 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
15efd 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
15efe 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e Mem *pMem){. in
15eff 74 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t f;. assert( p
15f00 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
15f01 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15f02 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
15f03 29 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f ) );. expandBlo
15f04 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 b(pMem);. f = p
15f05 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 Mem->flags;. if
15f06 28 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 ( (f&(MEM_Str|ME
15f07 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d M_Blob)) && pMem
15f08 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c ->z!=pMem->zMall
15f09 6f 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 oc ){. if( sq
15f0a 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
15f0b 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b (pMem, pMem->n +
15f0c 20 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 2, 1) ){.
15f0d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
15f0e 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 MEM;. }. p
15f0f 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 Mem->z[pMem->n]
15f10 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a = 0;. pMem->z
15f11 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b [pMem->n+1] = 0;
15f12 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
15f13 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 |= MEM_Term;.
15f14 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
15f15 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
15f16 49 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d If the given Mem
15f17 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c * has a zero-fil
15f18 6c 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 led tail, turn i
15f19 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 t into an ordina
15f1a 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 ry.** blob store
15f1b 64 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 d in dynamically
15f1c 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
15f1d 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
15f1e 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
15f1f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 B.SQLITE_PRIVATE
15f20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
15f21 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 MemExpandBlob(Me
15f22 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 m *pMem){. if(
15f23 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
15f24 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e M_Zero ){. in
15f25 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 t nByte;. ass
15f26 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 ert( pMem->flags
15f27 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 &MEM_Blob );.
15f28 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
15f29 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
15f2a 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
15f2b 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a >db->mutex) );..
15f2c 20 20 20 20 2f 2a 20 53 65 74 20 6e 42 79 74 65 /* Set nByte
15f2d 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
15f2e 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
15f2f 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 65 78 to store the ex
15f30 70 61 6e 64 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a panded blob. */.
15f31 20 20 20 20 6e 42 79 74 65 20 3d 20 70 4d 65 6d nByte = pMem
15f32 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75 2e 69 3b ->n + pMem->u.i;
15f33 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d . if( nByte<=
15f34 30 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 0 ){. nByte
15f35 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 1;. }.
15f36 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
15f37 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 emGrow(pMem, nBy
15f38 74 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 te, 1) ){.
15f39 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
15f3a 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 MEM;. }..
15f3b 6d 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b memset(&pMem->z[
15f3c 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 pMem->n], 0, pMe
15f3d 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 70 4d 65 m->u.i);. pMe
15f3e 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e m->n += pMem->u.
15f3f 69 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 i;. pMem->fla
15f40 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f gs &= ~(MEM_Zero
15f41 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a |MEM_Term);. }.
15f42 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15f43 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f OK;.}.#endif.../
15f44 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 *.** Make sure t
15f45 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20 he given Mem is
15f46 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65 \u0000 terminate
15f47 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
15f48 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
15f49 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
15f4a 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ate(Mem *pMem){.
15f4b 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
15f4c 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
15f4d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
15f4e 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
15f4f 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 if( (pMem->fla
15f50 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d gs & MEM_Term)!=
15f51 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 0 || (pMem->flag
15f52 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 s & MEM_Str)==0
15f53 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
15f54 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f LITE_OK; /* No
15f55 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 thing to do */.
15f56 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
15f57 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
15f58 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20 , pMem->n+2, 1)
15f59 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
15f5a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
15f5b 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
15f5c 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e n] = 0;. pMem->
15f5d 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 z[pMem->n+1] = 0
15f5e 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 ;. pMem->flags
15f5f 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 72 |= MEM_Term;. r
15f60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15f61 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45 .}../*.** Add ME
15f62 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74 M_Str to the set
15f63 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69 of representati
15f64 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65 ons for the give
15f65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a n Mem. Numbers.
15f66 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 ** are converted
15f67 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73 using sqlite3_s
15f68 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76 nprintf(). Conv
15f69 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f erting a BLOB to
15f6a 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 a string.** is
15f6b 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 a no-op..**.** E
15f6c 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e xisting represen
15f6d 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20 tations MEM_Int
15f6e 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65 and MEM_Real are
15f6f 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74 *not* invalidat
15f70 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f ed..**.** A MEM_
15f71 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20 Null value will
15f72 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20 never be passed
15f73 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
15f74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
15f75 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63 is.** used for c
15f76 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73 onverting values
15f77 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74 to text for ret
15f78 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73 urning to the us
15f79 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20 er (i.e. via.**
15f7a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
15f7b 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e xt()), or for en
15f7c 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75 suring that valu
15f7d 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73 es to be used as
15f7e 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61 btree.** keys a
15f7f 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74 re strings. In t
15f80 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61 he former case a
15f81 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 NULL pointer is
15f82 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a returned the.**
15f83 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61 user and the la
15f84 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e ter is an intern
15f85 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65 al programming e
15f86 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rror..*/.SQLITE_
15f87 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
15f88 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 te3VdbeMemString
15f89 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 ify(Mem *pMem, i
15f8a 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72 nt enc){. int r
15f8b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
15f8c 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e int fg = pMem->
15f8d 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 69 flags;. const i
15f8e 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a 0a nt nByte = 32;..
15f8f 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
15f90 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
15f91 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
15f92 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
15f93 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 4d assert( !(fg&M
15f94 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 73 EM_Zero) );. as
15f95 73 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d 5f sert( !(fg&(MEM_
15f96 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 29 Str|MEM_Blob)) )
15f97 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 28 ;. assert( fg&(
15f98 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c MEM_Int|MEM_Real
15f99 29 20 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 ) );.. if( sqli
15f9a 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
15f9b 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 Mem, nByte, 0) )
15f9c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
15f9d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a ITE_NOMEM;. }..
15f9e 20 20 2f 2a 20 46 6f 72 20 61 20 52 65 61 6c 20 /* For a Real
15f9f 6f 72 20 49 6e 74 65 67 65 72 2c 20 75 73 65 20 or Integer, use
15fa0 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 sqlite3_mprintf(
15fa1 29 20 74 6f 20 70 72 6f 64 75 63 65 20 74 68 65 ) to produce the
15fa2 20 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74 72 69 UTF-8. ** stri
15fa3 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ng representatio
15fa4 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 n of the value.
15fa5 54 68 65 6e 2c 20 69 66 20 74 68 65 20 72 65 71 Then, if the req
15fa6 75 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 0a 20 uired encoding.
15fa7 20 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c 65 20 ** is UTF-16le
15fa8 6f 72 20 55 54 46 2d 31 36 62 65 20 64 6f 20 61 or UTF-16be do a
15fa9 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a 20 20 translation..
15faa 2a 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d 45 3a ** . ** FIX ME:
15fab 20 49 74 20 77 6f 75 6c 64 20 62 65 20 62 65 74 It would be bet
15fac 74 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f 73 ter if sqlite3_s
15fad 6e 70 72 69 6e 74 66 28 29 20 63 6f 75 6c 64 20 nprintf() could
15fae 64 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a 2f 0a do UTF-16.. */.
15faf 20 20 69 66 28 20 66 67 20 26 20 4d 45 4d 5f 49 if( fg & MEM_I
15fb0 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 nt ){. sqlite
15fb1 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 65 3_snprintf(nByte
15fb2 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 6c 6c 64 , pMem->z, "%lld
15fb3 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 ", pMem->u.i);.
15fb4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
15fb5 72 74 28 20 66 67 20 26 20 4d 45 4d 5f 52 65 61 rt( fg & MEM_Rea
15fb6 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 l );. sqlite3
15fb7 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 65 2c _snprintf(nByte,
15fb8 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21 2e 31 35 pMem->z, "%!.15
15fb9 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 g", pMem->r);.
15fba 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 }. pMem->n = st
15fbb 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 rlen(pMem->z);.
15fbc 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
15fbd 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d ITE_UTF8;. pMem
15fbe 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 ->flags |= MEM_S
15fbf 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 tr|MEM_Term;. s
15fc0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
15fc1 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 Encoding(pMem, e
15fc2 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 nc);. return rc
15fc3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 ;.}../*.** Memor
15fc4 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 y cell pMem cont
15fc5 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 ains the context
15fc6 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 of an aggregate
15fc7 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 function..** Th
15fc8 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 is routine calls
15fc9 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 the finalize me
15fca 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 thod for that fu
15fcb 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 nction. The.**
15fcc 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 result of the ag
15fcd 67 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 gregate is store
15fce 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d d back into pMem
15fcf 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ..**.** Return S
15fd0 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 QLITE_ERROR if t
15fd1 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 he finalizer rep
15fd2 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 orts an error.
15fd3 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 SQLITE_OK.** oth
15fd4 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erwise..*/.SQLIT
15fd5 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
15fd6 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 lite3VdbeMemFina
15fd7 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 lize(Mem *pMem,
15fd8 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b FuncDef *pFunc){
15fd9 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
15fda 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 75 TE_OK;. if( pFu
15fdb 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 46 69 nc && pFunc->xFi
15fdc 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 nalize ){. sq
15fdd 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 lite3_context ct
15fde 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 x;. assert( (
15fdf 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
15fe0 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20 70 46 M_Null)!=0 || pF
15fe1 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70 44 65 unc==pMem->u.pDe
15fe2 66 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 f );. assert(
15fe3 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
15fe4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
15fe5 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
15fe6 65 78 29 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 ex) );. memse
15fe7 74 28 26 63 74 78 2c 20 30 2c 20 73 69 7a 65 6f t(&ctx, 0, sizeo
15fe8 66 28 63 74 78 29 29 3b 0a 20 20 20 20 63 74 78 f(ctx));. ctx
15fe9 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e .s.flags = MEM_N
15fea 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73 2e 64 ull;. ctx.s.d
15feb 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 b = pMem->db;.
15fec 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d 65 ctx.pMem = pMe
15fed 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 m;. ctx.pFunc
15fee 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20 70 46 = pFunc;. pF
15fef 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 28 26 unc->xFinalize(&
15ff0 63 74 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ctx);. assert
15ff1 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 ( 0==(pMem->flag
15ff2 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70 s&MEM_Dyn) && !p
15ff3 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 Mem->xDel );.
15ff4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
15ff5 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a Mem->db, pMem->z
15ff6 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 2a 70 4d Malloc);. *pM
15ff7 65 6d 20 3d 20 63 74 78 2e 73 3b 0a 20 20 20 20 em = ctx.s;.
15ff8 72 63 20 3d 20 28 63 74 78 2e 69 73 45 72 72 6f rc = (ctx.isErro
15ff9 72 3f 53 51 4c 49 54 45 5f 45 52 52 4f 52 3a 53 r?SQLITE_ERROR:S
15ffa 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 7d 0a 20 QLITE_OK);. }.
15ffb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
15ffc 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f *.** If the memo
15ffd 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73 ry cell contains
15ffe 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 a string value
15fff 74 68 61 74 20 6d 75 73 74 20 62 65 20 66 72 65 that must be fre
16000 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e ed by.** invokin
16001 67 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 63 61 g an external ca
16002 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20 69 74 20 llback, free it
16003 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69 now. Calling thi
16004 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f s function.** do
16005 65 73 20 6e 6f 74 20 66 72 65 65 20 61 6e 79 20 es not free any
16006 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66 Mem.zMalloc buff
16007 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
16008 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
16009 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1600a 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 External(Mem *p)
1600b 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 {. assert( p->d
1600c 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
1600d 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
1600e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
1600f 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 ( p->flags&MEM_A
16010 67 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 gg ){. sqlite
16011 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 3VdbeMemFinalize
16012 28 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a (p, p->u.pDef);.
16013 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e assert( (p->
16014 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 flags & MEM_Agg)
16015 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ==0 );. sqlit
16016 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
16017 28 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 (p);. }else if(
16018 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 p->flags&MEM_Dy
16019 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a n && p->xDel ){.
1601a 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 p->xDel((voi
1601b 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 d *)p->z);. p
1601c 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 0a ->xDel = 0;. }.
1601d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 }../*.** Release
1601e 20 61 6e 79 20 6d 65 6d 6f 72 79 20 68 65 6c 64 any memory held
1601f 20 62 79 20 74 68 65 20 4d 65 6d 2e 20 54 68 69 by the Mem. Thi
16020 73 20 6d 61 79 20 6c 65 61 76 65 20 74 68 65 20 s may leave the
16021 4d 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 Mem in an.** inc
16022 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2c onsistent state,
16023 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 69 74 for example wit
16024 68 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20 61 6e 64 h (Mem.z==0) and
16025 0a 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 3d 3d 53 .** (Mem.type==S
16026 51 4c 49 54 45 5f 54 45 58 54 29 2e 0a 2a 2f 0a QLITE_TEXT)..*/.
16027 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
16028 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
16029 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 emRelease(Mem *p
1602a 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ){. sqlite3Vdbe
1602b 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
1602c 61 6c 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 al(p);. sqlite3
1602d 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d DbFree(p->db, p-
1602e 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 70 2d 3e >zMalloc);. p->
1602f 7a 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a 4d 61 6c z = 0;. p->zMal
16030 6c 6f 63 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 44 loc = 0;. p->xD
16031 65 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a el = 0;.}../*.**
16032 20 43 6f 6e 76 65 72 74 20 61 20 36 34 2d 62 69 Convert a 64-bi
16033 74 20 49 45 45 45 20 64 6f 75 62 6c 65 20 69 6e t IEEE double in
16034 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e to a 64-bit sign
16035 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 ed integer..** I
16036 66 20 74 68 65 20 64 6f 75 62 6c 65 20 69 73 20 f the double is
16037 74 6f 6f 20 6c 61 72 67 65 2c 20 72 65 74 75 72 too large, retur
16038 6e 20 30 78 38 30 30 30 30 30 30 30 30 30 30 30 n 0x800000000000
16039 30 30 30 30 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 0000..**.** Most
1603a 20 73 79 73 74 65 6d 73 20 61 70 70 65 61 72 20 systems appear
1603b 74 6f 20 64 6f 20 74 68 69 73 20 73 69 6d 70 6c to do this simpl
1603c 79 20 62 79 20 61 73 73 69 67 6e 69 6e 67 0a 2a y by assigning.*
1603d 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 * variables and
1603e 77 69 74 68 6f 75 74 20 74 68 65 20 65 78 74 72 without the extr
1603f 61 20 72 61 6e 67 65 20 74 65 73 74 73 2e 20 20 a range tests.
16040 42 75 74 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 But.** there are
16041 20 72 65 70 6f 72 74 73 20 74 68 61 74 20 77 69 reports that wi
16042 6e 64 6f 77 73 20 74 68 72 6f 77 73 20 61 6e 20 ndows throws an
16043 65 78 70 65 63 74 69 6f 6e 0a 2a 2a 20 69 66 20 expection.** if
16044 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 the floating poi
16045 6e 74 20 76 61 6c 75 65 20 69 73 20 6f 75 74 20 nt value is out
16046 6f 66 20 72 61 6e 67 65 2e 20 28 53 65 65 20 74 of range. (See t
16047 69 63 6b 65 74 20 23 32 38 38 30 2e 29 0a 2a 2a icket #2880.).**
16048 20 42 65 63 61 75 73 65 20 77 65 20 64 6f 20 6e Because we do n
16049 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 75 6e ot completely un
1604a 64 65 72 73 74 61 6e 64 20 74 68 65 20 70 72 6f derstand the pro
1604b 62 6c 65 6d 2c 20 77 65 20 77 69 6c 6c 0a 2a 2a blem, we will.**
1604c 20 74 61 6b 65 20 74 68 65 20 63 6f 6e 73 65 72 take the conser
1604d 76 61 74 69 76 65 20 61 70 70 72 6f 61 63 68 20 vative approach
1604e 61 6e 64 20 61 6c 77 61 79 73 20 64 6f 20 72 61 and always do ra
1604f 6e 67 65 20 74 65 73 74 73 0a 2a 2a 20 62 65 66 nge tests.** bef
16050 6f 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 ore attempting t
16051 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a he conversion..*
16052 2f 0a 73 74 61 74 69 63 20 69 36 34 20 64 6f 75 /.static i64 dou
16053 62 6c 65 54 6f 49 6e 74 36 34 28 64 6f 75 62 6c bleToInt64(doubl
16054 65 20 72 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 e r){. /*. **
16055 4d 61 6e 79 20 63 6f 6d 70 69 6c 65 72 73 20 77 Many compilers w
16056 65 20 65 6e 63 6f 75 6e 74 65 72 20 64 6f 20 6e e encounter do n
16057 6f 74 20 64 65 66 69 6e 65 20 63 6f 6e 73 74 61 ot define consta
16058 6e 74 73 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a nts for the. **
16059 20 6d 69 6e 69 6d 75 6d 20 61 6e 64 20 6d 61 78 minimum and max
1605a 69 6d 75 6d 20 36 34 2d 62 69 74 20 69 6e 74 65 imum 64-bit inte
1605b 67 65 72 73 2c 20 6f 72 20 74 68 65 79 20 64 65 gers, or they de
1605c 66 69 6e 65 20 74 68 65 6d 0a 20 20 2a 2a 20 69 fine them. ** i
1605d 6e 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 20 20 nconsistently.
1605e 41 6e 64 20 6d 61 6e 79 20 64 6f 20 6e 6f 74 20 And many do not
1605f 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 22 understand the "
16060 4c 4c 22 20 6e 6f 74 61 74 69 6f 6e 2e 0a 20 20 LL" notation..
16061 2a 2a 20 53 6f 20 77 65 20 64 65 66 69 6e 65 20 ** So we define
16062 6f 75 72 20 6f 77 6e 20 73 74 61 74 69 63 20 63 our own static c
16063 6f 6e 73 74 61 6e 74 73 20 68 65 72 65 20 75 73 onstants here us
16064 69 6e 67 20 6e 6f 74 68 69 6e 67 0a 20 20 2a 2a ing nothing. **
16065 20 6c 61 72 67 65 72 20 74 68 61 6e 20 61 20 33 larger than a 3
16066 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 63 6f 2-bit integer co
16067 6e 73 74 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 nstant.. */. s
16068 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20 tatic const i64
16069 6d 61 78 49 6e 74 20 3d 20 4c 41 52 47 45 53 54 maxInt = LARGEST
1606a 5f 49 4e 54 36 34 3b 0a 20 20 73 74 61 74 69 63 _INT64;. static
1606b 20 63 6f 6e 73 74 20 69 36 34 20 6d 69 6e 49 6e const i64 minIn
1606c 74 20 3d 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 t = SMALLEST_INT
1606d 36 34 3b 0a 0a 20 20 69 66 28 20 72 3c 28 64 6f 64;.. if( r<(do
1606e 75 62 6c 65 29 6d 69 6e 49 6e 74 20 29 7b 0a 20 uble)minInt ){.
1606f 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 return minInt
16070 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 3e ;. }else if( r>
16071 28 64 6f 75 62 6c 65 29 6d 61 78 49 6e 74 20 29 (double)maxInt )
16072 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e {. return min
16073 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 Int;. }else{.
16074 20 20 72 65 74 75 72 6e 20 28 69 36 34 29 72 3b return (i64)r;
16075 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
16076 74 75 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f turn some kind o
16077 66 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 f integer value
16078 77 68 69 63 68 20 69 73 20 74 68 65 20 62 65 73 which is the bes
16079 74 20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 t we can do.** a
1607a 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 t representing t
1607b 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 he value that *p
1607c 4d 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 Mem describes as
1607d 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 an integer..**
1607e 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e If pMem is an in
1607f 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 teger, then the
16080 76 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20 value is exact.
16081 20 49 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 If pMem is.** a
16082 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 floating-point
16083 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 then the value r
16084 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 69 eturned is the i
16085 6e 74 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 nteger part..**
16086 49 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 If pMem is a str
16087 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 ing or blob, the
16088 6e 20 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 n we make an att
16089 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a empt to convert.
1608a 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 ** it into a int
1608b 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 eger and return
1608c 74 68 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 69 that. If pMem i
1608d 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 s NULL, return 0
1608e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 ..**.** If pMem
1608f 69 73 20 61 20 73 74 72 69 6e 67 2c 20 69 74 73 is a string, its
16090 20 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 encoding might
16091 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 be changed..*/.S
16092 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 QLITE_PRIVATE i6
16093 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 4 sqlite3VdbeInt
16094 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 Value(Mem *pMem)
16095 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 {. int flags;.
16096 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
16097 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
16098 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
16099 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
1609a 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 flags = pMem->f
1609b 6c 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 lags;. if( flag
1609c 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 s & MEM_Int ){.
1609d 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e return pMem->
1609e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 u.i;. }else if(
1609f 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 flags & MEM_Rea
160a0 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 l ){. return
160a1 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d doubleToInt64(pM
160a2 65 6d 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 em->r);. }else
160a3 69 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d if( flags & (MEM
160a4 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 _Str|MEM_Blob) )
160a5 7b 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b {. i64 value;
160a6 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
160a7 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 |= MEM_Str;.
160a8 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
160a9 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 ChangeEncoding(p
160aa 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Mem, SQLITE_UTF8
160ab 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 ). || sqli
160ac 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 te3VdbeMemNulTer
160ad 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a minate(pMem) ){.
160ae 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
160af 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
160b0 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 ( pMem->z );.
160b1 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 sqlite3Atoi64(p
160b2 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b Mem->z, &value);
160b3 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 . return valu
160b4 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
160b5 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a return 0;. }.}.
160b6 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
160b7 65 20 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 e best represent
160b8 61 74 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 ation of pMem th
160b9 61 74 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e at we can get in
160ba 74 6f 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 to a.** double.
160bb 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 If pMem is alre
160bc 61 64 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 ady a double or
160bd 61 6e 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 an integer, retu
160be 72 6e 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e rn its.** value.
160bf 20 20 49 66 20 69 74 20 69 73 20 61 20 73 74 72 If it is a str
160c0 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 ing or blob, try
160c1 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 to convert it t
160c2 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 o a double..** I
160c3 66 20 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 f it is a NULL,
160c4 72 65 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 return 0.0..*/.S
160c5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f QLITE_PRIVATE do
160c6 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 uble sqlite3Vdbe
160c7 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 RealValue(Mem *p
160c8 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Mem){. assert(
160c9 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
160ca 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
160cb 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
160cc 78 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d x) );. if( pMem
160cd 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 ->flags & MEM_Re
160ce 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e al ){. return
160cf 20 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73 pMem->r;. }els
160d0 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 e if( pMem->flag
160d1 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 s & MEM_Int ){.
160d2 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c return (doubl
160d3 65 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d e)pMem->u.i;. }
160d4 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 else if( pMem->f
160d5 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c lags & (MEM_Str|
160d6 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 MEM_Blob) ){.
160d7 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 30 2e double val = 0.
160d8 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 0;. pMem->fla
160d9 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 gs |= MEM_Str;.
160da 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
160db 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
160dc 28 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 (pMem, SQLITE_UT
160dd 46 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 F8). || sq
160de 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 lite3VdbeMemNulT
160df 65 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 erminate(pMem) )
160e0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
160e1 2e 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 .0;. }. as
160e2 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b sert( pMem->z );
160e3 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 . sqlite3AtoF
160e4 28 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b (pMem->z, &val);
160e5 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b . return val;
160e6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
160e7 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 7d 0a turn 0.0;. }.}.
160e8 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73 ./*.** The MEM s
160e9 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65 tructure is alre
160ea 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 ady a MEM_Real.
160eb 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b Try to also mak
160ec 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e e it a.** MEM_In
160ed 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a t if we can..*/.
160ee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
160ef 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 oid sqlite3VdbeI
160f0 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d ntegerAffinity(M
160f1 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 em *pMem){. ass
160f2 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 ert( pMem->flags
160f3 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 & MEM_Real );.
160f4 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
160f5 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
160f6 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
160f7 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a >db->mutex) );..
160f8 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f pMem->u.i = do
160f9 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d ubleToInt64(pMem
160fa 2d 3e 72 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d ->r);. if( pMem
160fb 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 29 70 4d 65 ->r==(double)pMe
160fc 6d 2d 3e 75 2e 69 20 29 7b 0a 20 20 20 20 70 4d m->u.i ){. pM
160fd 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d em->flags |= MEM
160fe 5f 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 _Int;. }.}..sta
160ff 74 69 63 20 76 6f 69 64 20 73 65 74 54 79 70 65 tic void setType
16100 46 6c 61 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Flag(Mem *pMem,
16101 69 6e 74 20 66 29 7b 0a 20 20 4d 65 6d 53 65 74 int f){. MemSet
16102 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 66 TypeFlag(pMem, f
16103 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 );.}../*.** Conv
16104 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65 ert pMem to type
16105 20 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c integer. Inval
16106 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
16107 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
16108 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
16109 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1610a 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
1610b 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
1610c 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
1610d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
1610e 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
1610f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d ->mutex) );. pM
16110 65 6d 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65 em->u.i = sqlite
16111 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 4d 3VdbeIntValue(pM
16112 65 6d 29 3b 0a 20 20 73 65 74 54 79 70 65 46 6c em);. setTypeFl
16113 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 ag(pMem, MEM_Int
16114 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
16115 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
16116 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 Convert pMem so
16117 74 68 61 74 20 69 74 20 69 73 20 6f 66 20 74 79 that it is of ty
16118 70 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 pe MEM_Real..**
16119 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 Invalidate any p
1611a 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 rior representat
1611b 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
1611c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1611d 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 te3VdbeMemRealif
1611e 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 y(Mem *pMem){.
1611f 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
16120 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
16121 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
16122 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
16123 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65 pMem->r = sqlite
16124 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 3VdbeRealValue(p
16125 4d 65 6d 29 3b 0a 20 20 73 65 74 54 79 70 65 46 Mem);. setTypeF
16126 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 lag(pMem, MEM_Re
16127 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 al);. return SQ
16128 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
16129 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 * Convert pMem s
1612a 6f 20 74 68 61 74 20 69 74 20 68 61 73 20 74 79 o that it has ty
1612b 70 65 73 20 4d 45 4d 5f 52 65 61 6c 20 6f 72 20 pes MEM_Real or
1612c 4d 45 4d 5f 49 6e 74 20 6f 72 20 62 6f 74 68 2e MEM_Int or both.
1612d 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 .** Invalidate a
1612e 6e 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 ny prior represe
1612f 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c ntations..*/.SQL
16130 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16131 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
16132 6d 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d merify(Mem *pMem
16133 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 31 2c 20 ){. double r1,
16134 72 32 3b 0a 20 20 69 36 34 20 69 3b 0a 20 20 61 r2;. i64 i;. a
16135 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
16136 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d ags & (MEM_Int|M
16137 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e 75 6c 6c EM_Real|MEM_Null
16138 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ))==0 );. asser
16139 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 t( (pMem->flags
1613a 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f & (MEM_Blob|MEM_
1613b 53 74 72 29 29 21 3d 30 20 29 3b 0a 20 20 61 73 Str))!=0 );. as
1613c 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
1613d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
1613e 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
1613f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 31 ->mutex) );. r1
16140 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
16141 61 6c 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 alValue(pMem);.
16142 20 69 20 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 i = doubleToInt
16143 36 34 28 72 31 29 3b 0a 20 20 72 32 20 3d 20 28 64(r1);. r2 = (
16144 64 6f 75 62 6c 65 29 69 3b 0a 20 20 69 66 28 20 double)i;. if(
16145 72 31 3d 3d 72 32 20 29 7b 0a 20 20 20 20 73 71 r1==r2 ){. sq
16146 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 lite3VdbeMemInte
16147 67 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 gerify(pMem);.
16148 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d }else{. pMem-
16149 3e 72 20 3d 20 72 31 3b 0a 20 20 20 20 73 65 74 >r = r1;. set
1614a 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d TypeFlag(pMem, M
1614b 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 EM_Real);. }.
1614c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1614d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
1614e 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 e any previous v
1614f 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 alue and set the
16150 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
16151 20 2a 70 4d 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a *pMem to NULL..
16152 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16153 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
16154 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d beMemSetNull(Mem
16155 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 65 74 54 79 *pMem){. setTy
16156 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d peFlag(pMem, MEM
16157 5f 4e 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e _Null);. pMem->
16158 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 type = SQLITE_NU
16159 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c LL;.}../*.** Del
1615a 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 ete any previous
1615b 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 value and set t
1615c 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 he value to be a
1615d 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a BLOB of length.
1615e 2a 2a 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ** n containing
1615f 61 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 all zeros..*/.SQ
16160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
16161 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
16162 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 SetZeroBlob(Mem
16163 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 *pMem, int n){.
16164 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
16165 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
16166 73 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d setTypeFlag(pMem
16167 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 70 , MEM_Blob);. p
16168 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
16169 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a _Blob|MEM_Zero;.
1616a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
1616b 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d QLITE_BLOB;. pM
1616c 65 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 em->n = 0;. if(
1616d 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 n<0 ) n = 0;.
1616e 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 6e 3b 0a 20 pMem->u.i = n;.
1616f 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
16170 49 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a 0a ITE_UTF8;.}../*.
16171 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 ** Delete any pr
16172 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 evious value and
16173 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 set the value s
16174 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 tored in *pMem t
16175 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 o val,.** manife
16176 73 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e st type INTEGER.
16177 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
16178 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
16179 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d dbeMemSetInt64(M
1617a 65 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 em *pMem, i64 va
1617b 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 l){. sqlite3Vdb
1617c 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d eMemRelease(pMem
1617d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d );. pMem->u.i =
1617e 20 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c val;. pMem->fl
1617f 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
16180 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
16181 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a LITE_INTEGER;.}.
16182 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e ./*.** Delete an
16183 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 y previous value
16184 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c and set the val
16185 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d ue stored in *pM
16186 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 em to val,.** ma
16187 6e 69 66 65 73 74 20 74 79 70 65 20 52 45 41 4c nifest type REAL
16188 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16189 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1618a 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 VdbeMemSetDouble
1618b 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62 (Mem *pMem, doub
1618c 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 73 le val){. if( s
1618d 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 29 qlite3IsNaN(val)
1618e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1618f 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d dbeMemSetNull(pM
16190 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 em);. }else{.
16191 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
16192 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
16193 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c pMem->r = val
16194 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
16195 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 s = MEM_Real;.
16196 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
16197 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d QLITE_FLOAT;. }
16198 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
16199 20 74 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d true if the Mem
1619a 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 object contains
1619b 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 a TEXT or BLOB
1619c 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c that is.** too l
1619d 61 72 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a arge - whose siz
1619e 65 20 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 e exceeds SQLITE
1619f 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a _MAX_LENGTH..*/.
161a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
161a1 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
161a2 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b mTooBig(Mem *p){
161a3 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 . assert( p->db
161a4 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e !=0 );. if( p->
161a5 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 flags & (MEM_Str
161a6 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 |MEM_Blob) ){.
161a7 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a int n = p->n;.
161a8 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 if( p->flags
161a9 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 & MEM_Zero ){.
161aa 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 69 n += p->u.i
161ab 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
161ac 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d rn n>p->db->aLim
161ad 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
161ae 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72 LENGTH];. }. r
161af 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a eturn 0; .}../*.
161b0 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63 ** Size of struc
161b1 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 t Mem not includ
161b2 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c ing the Mem.zMal
161b3 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 loc member..*/.#
161b4 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 define MEMCELLSI
161b5 5a 45 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28 ZE (size_t)(&(((
161b6 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f Mem *)0)->zMallo
161b7 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 c))../*.** Make
161b8 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 an shallow copy
161b9 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 of pFrom into pT
161ba 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e o. Prior conten
161bb 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65 ts of.** pTo are
161bc 20 66 72 65 65 64 2e 20 20 54 68 65 20 70 46 72 freed. The pFr
161bd 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e om->z field is n
161be 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20 ot duplicated.
161bf 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 If.** pFrom->z i
161c0 73 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f s used, then pTo
161c1 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ->z points to th
161c2 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 e same thing as
161c3 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 pFrom->z.** and
161c4 66 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79 flags gets srcTy
161c5 70 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 pe (either MEM_E
161c6 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 phem or MEM_Stat
161c7 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ic)..*/.SQLITE_P
161c8 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
161c9 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f te3VdbeMemShallo
161ca 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 wCopy(Mem *pTo,
161cb 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d const Mem *pFrom
161cc 2c 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a , int srcType){.
161cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
161ce 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 ReleaseExternal(
161cf 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 pTo);. memcpy(p
161d0 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 To, pFrom, MEMCE
161d1 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e LLSIZE);. pTo->
161d2 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 xDel = 0;. if(
161d3 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 (pFrom->flags&ME
161d4 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 M_Dyn)!=0 || pFr
161d5 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d om->z==pFrom->zM
161d6 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f alloc ){. pTo
161d7 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
161d8 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c _Dyn|MEM_Static|
161d9 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 MEM_Ephem);.
161da 61 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d assert( srcType=
161db 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 =MEM_Ephem || sr
161dc 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 cType==MEM_Stati
161dd 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c c );. pTo->fl
161de 61 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a ags |= srcType;.
161df 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b }.}../*.** Mak
161e0 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 e a full copy of
161e1 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e pFrom into pTo.
161e2 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 Prior contents
161e3 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 of pTo are.** f
161e4 72 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 reed before the
161e5 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f copy is made..*/
161e6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
161e7 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
161e8 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c emCopy(Mem *pTo,
161e9 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f const Mem *pFro
161ea 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 m){. int rc = S
161eb 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c QLITE_OK;.. sql
161ec 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
161ed 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b seExternal(pTo);
161ee 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 . memcpy(pTo, p
161ef 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a From, MEMCELLSIZ
161f0 45 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 E);. pTo->flags
161f1 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 &= ~MEM_Dyn;..
161f2 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 if( pTo->flags&
161f3 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
161f4 62 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d b) ){. if( 0=
161f5 3d 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d =(pFrom->flags&M
161f6 45 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 EM_Static) ){.
161f7 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c pTo->flags |
161f8 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 = MEM_Ephem;.
161f9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
161fa 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 dbeMemMakeWritea
161fb 62 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a ble(pTo);. }.
161fc 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
161fd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 ;.}../*.** Trans
161fe 66 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 fer the contents
161ff 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f of pFrom to pTo
16200 2e 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 . Any existing v
16201 61 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a alue in pTo is.*
16202 2a 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f * freed. If pFro
16203 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d m contains ephem
16204 65 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 eral data, a cop
16205 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a y is made..**.**
16206 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 pFrom contains
16207 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e an SQL NULL when
16208 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
16209 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 turns..*/.SQLITE
1620a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1620b 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
1620c 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a (Mem *pTo, Mem *
1620d 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 pFrom){. assert
1620e 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c ( pFrom->db==0 |
1620f 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
16210 68 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e held(pFrom->db->
16211 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
16212 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c rt( pTo->db==0 |
16213 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
16214 68 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 held(pTo->db->mu
16215 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
16216 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c ( pFrom->db==0 |
16217 7c 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 | pTo->db==0 ||
16218 70 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e pFrom->db==pTo->
16219 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 db );.. sqlite3
1621a 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
1621b 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 To);. memcpy(pT
1621c 6f 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 o, pFrom, sizeof
1621d 28 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d (Mem));. pFrom-
1621e 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
1621f 6c 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c l;. pFrom->xDel
16220 20 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a = 0;. pFrom->z
16221 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f Malloc = 0;.}../
16222 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
16223 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 value of a Mem t
16224 6f 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 o be a string or
16225 20 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 a BLOB..**.** T
16226 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 he memory manage
16227 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 ment strategy de
16228 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c pends on the val
16229 75 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a ue of the xDel.*
1622a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 * parameter. If
1622b 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 the value passed
1622c 20 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 is SQLITE_TRANS
1622d 49 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a IENT, then the .
1622e 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 ** string is cop
1622f 69 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 ied into a (poss
16230 69 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 ibly existing) b
16231 75 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 uffer managed by
16232 20 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 the .** Mem str
16233 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 ucture. Otherwis
16234 65 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 e, any existing
16235 62 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 buffer is freed
16236 61 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 and the.** point
16237 65 72 20 63 6f 70 69 65 64 2e 0a 2a 2f 0a 53 51 er copied..*/.SQ
16238 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16239 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1623a 65 74 53 74 72 28 0a 20 20 4d 65 6d 20 2a 70 4d etStr(. Mem *pM
1623b 65 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 em, /*
1623c 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 Memory cell to s
1623d 65 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c et to string val
1623e 75 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ue */. const ch
1623f 61 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20 53 ar *z, /* S
16240 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f tring pointer */
16241 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 . int n,
16242 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
16243 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65 in string, or ne
16244 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65 gative */. u8 e
16245 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 nc,
16246 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a /* Encoding of z
16247 2e 20 20 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a . 0 for BLOBs *
16248 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 /. void (*xDel)
16249 28 76 6f 69 64 2a 29 20 2f 2a 20 44 65 73 74 72 (void*) /* Destr
1624a 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a uctor function *
1624b 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 /.){. int nByte
1624c 20 3d 20 6e 3b 20 20 20 20 20 20 2f 2a 20 4e 65 = n; /* Ne
1624d 77 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d w value for pMem
1624e 2d 3e 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 ->n */. int iLi
1624f 6d 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 mit; /*
16250 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 Maximum allowed
16251 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 73 string or blob s
16252 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ize */. int fla
16253 67 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 gs = 0; /*
16254 4e 65 77 20 76 61 6c 75 65 20 66 6f 72 20 70 4d New value for pM
16255 65 6d 2d 3e 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 em->flags */..
16256 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
16257 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
16258 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
16259 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
1625a 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 4e 55 /* If z is a NU
1625b 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 74 20 LL pointer, set
1625c 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 6e 20 pMem to contain
1625d 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a 2f 0a an SQL NULL. */.
1625e 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 20 if( !z ){.
1625f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
16260 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 tNull(pMem);.
16261 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
16262 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d K;. }.. if( pM
16263 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 69 4c em->db ){. iL
16264 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 62 2d imit = pMem->db-
16265 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
16266 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 IMIT_LENGTH];.
16267 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69 }else{. iLimi
16268 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c t = SQLITE_MAX_L
16269 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 6c 61 ENGTH;. }. fla
1626a 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d 45 4d gs = (enc==0?MEM
1626b 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 3b 0a _Blob:MEM_Str);.
1626c 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20 29 7b if( nByte<0 ){
1626d 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 6e 63 . assert( enc
1626e 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 65 !=0 );. if( e
1626f 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc==SQLITE_UTF8
16270 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 42 79 ){. for(nBy
16271 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 4c 69 te=0; nByte<=iLi
16272 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 5d 3b mit && z[nByte];
16273 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 20 20 nByte++){}.
16274 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f 72 }else{. for
16275 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c (nByte=0; nByte<
16276 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b 6e 42 =iLimit && (z[nB
16277 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 2b 31 yte] | z[nByte+1
16278 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b 7d 0a ]); nByte+=2){}.
16279 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 73 20 }. flags
1627a 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d |= MEM_Term;. }
1627b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f .. /* The follo
1627c 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 73 20 wing block sets
1627d 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 20 6f the new values o
1627e 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 6d 2e f Mem.z and Mem.
1627f 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 61 6c xDel. It. ** al
16280 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67 20 69 so sets a flag i
16281 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 n local variable
16282 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e 64 69 "flags" to indi
16283 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 0a cate the memory.
16284 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e 74 20 ** management
16285 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 6e 20 (one of MEM_Dyn
16286 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a or MEM_Static)..
16287 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 6c 3d */. if( xDel=
16288 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e =SQLITE_TRANSIEN
16289 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c T ){. int nAl
1628a 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 loc = nByte;.
1628b 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 54 if( flags&MEM_T
1628c 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e 41 6c erm ){. nAl
1628d 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 51 4c loc += (enc==SQL
1628e 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 ITE_UTF8?1:2);.
1628f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 42 79 }. if( nBy
16290 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 te>iLimit ){.
16291 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
16292 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d 0a 20 _TOOBIG;. }.
16293 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
16294 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 beMemGrow(pMem,
16295 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a 20 20 nAlloc, 0) ){.
16296 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16297 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
16298 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e memcpy(pMem->
16299 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b 0a 20 z, z, nAlloc);.
1629a 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65 6c 3d }else if( xDel=
1629b 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 =SQLITE_DYNAMIC
1629c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1629d 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 beMemRelease(pMe
1629e 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d m);. pMem->zM
1629f 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 20 alloc = pMem->z
162a0 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 = (char *)z;.
162a1 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b pMem->xDel = 0;
162a2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
162a3 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
162a4 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 ase(pMem);. p
162a5 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a Mem->z = (char *
162a6 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 )z;. pMem->xD
162a7 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 66 el = xDel;. f
162a8 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c 3d 3d lags |= ((xDel==
162a9 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3f 4d SQLITE_STATIC)?M
162aa 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f 44 79 EM_Static:MEM_Dy
162ab 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 n);. }. if( nB
162ac 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 yte>iLimit ){.
162ad 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
162ae 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 70 TOOBIG;. }.. p
162af 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a Mem->n = nByte;.
162b0 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
162b1 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65 flags;. pMem->e
162b2 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 nc = (enc==0 ? S
162b3 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63 QLITE_UTF8 : enc
162b4 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 );. pMem->type
162b5 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49 = (enc==0 ? SQLI
162b6 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45 TE_BLOB : SQLITE
162b7 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66 _TEXT);..#ifndef
162b8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
162b9 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 16. if( pMem->e
162ba 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc!=SQLITE_UTF8
162bb 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 && sqlite3VdbeMe
162bc 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29 mHandleBom(pMem)
162bd 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
162be 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
162bf 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 .#endif.. retur
162c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
162c1 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 /*.** Compare th
162c2 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 61 69 6e e values contain
162c3 65 64 20 62 79 20 74 68 65 20 74 77 6f 20 6d 65 ed by the two me
162c4 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 65 74 75 mory cells, retu
162c5 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 74 69 76 rning.** negativ
162c6 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 e, zero or posit
162c7 69 76 65 20 69 66 20 70 4d 65 6d 31 20 69 73 20 ive if pMem1 is
162c8 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c less than, equal
162c9 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 0a to, or greater.
162ca 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e 20 53 ** than pMem2. S
162cb 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 69 73 20 orting order is
162cc 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c 20 66 6f NULL's first, fo
162cd 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62 65 72 llowed by number
162ce 73 20 28 69 6e 74 65 67 65 72 73 0a 2a 2a 20 61 s (integers.** a
162cf 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 74 65 64 nd reals) sorted
162d0 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20 66 6f numerically, fo
162d1 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 74 20 6f llowed by text o
162d2 72 64 65 72 65 64 20 62 79 20 74 68 65 20 63 6f rdered by the co
162d3 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 llating.** seque
162d4 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20 66 69 nce pColl and fi
162d5 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f 72 64 nally blob's ord
162d6 65 72 65 64 20 62 79 20 6d 65 6d 63 6d 70 28 29 ered by memcmp()
162d7 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e 55 4c 4c ..**.** Two NULL
162d8 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 values are cons
162d9 69 64 65 72 65 64 20 65 71 75 61 6c 20 62 79 20 idered equal by
162da 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a this function..*
162db 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
162dc 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 int sqlite3MemC
162dd 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d ompare(const Mem
162de 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 74 20 4d *pMem1, const M
162df 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f 6e 73 74 em *pMem2, const
162e0 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 CollSeq *pColl)
162e1 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e {. int rc;. in
162e2 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 6e 74 20 t f1, f2;. int
162e3 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 3b 0a combined_flags;.
162e4 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68 61 6e 67 . /* Interchang
162e5 65 20 70 4d 65 6d 31 20 61 6e 64 20 70 4d 65 6d e pMem1 and pMem
162e6 32 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 2 if the collati
162e7 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70 65 63 ng sequence spec
162e8 69 66 69 65 73 0a 20 20 2a 2a 20 44 45 53 43 20 ifies. ** DESC
162e9 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 31 order.. */. f1
162ea 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67 73 3b = pMem1->flags;
162eb 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d 3e 66 . f2 = pMem2->f
162ec 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69 6e 65 64 lags;. combined
162ed 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 32 3b 0a _flags = f1|f2;.
162ee 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 . /* If one va
162ef 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20 lue is NULL, it
162f0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 is less than the
162f1 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 20 other. If both
162f2 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 20 values. ** are
162f3 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a NULL, return 0..
162f4 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 */. if( combi
162f5 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 ned_flags&MEM_Nu
162f6 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ll ){. return
162f7 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 2d (f2&MEM_Null) -
162f8 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a (f1&MEM_Null);.
162f9 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 }.. /* If one
162fa 20 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d 62 value is a numb
162fb 65 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 er and the other
162fc 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 6d is not, the num
162fd 62 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a ber is less.. *
162fe 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e 75 * If both are nu
162ff 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 61 mbers, compare a
16300 73 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 69 s reals if one i
16301 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 20 s a real, or as
16302 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 66 integers. ** if
16303 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 both values are
16304 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a integers.. */.
16305 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 if( combined_f
16306 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 lags&(MEM_Int|ME
16307 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 69 M_Real) ){. i
16308 66 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74 f( !(f1&(MEM_Int
16309 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 |MEM_Real)) ){.
1630a 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
1630b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 66 }. if( !(f
1630c 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 2&(MEM_Int|MEM_R
1630d 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 eal)) ){. r
1630e 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a eturn -1;. }.
1630f 20 20 20 20 69 66 28 20 28 66 31 20 26 20 66 32 if( (f1 & f2
16310 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 & MEM_Int)==0 )
16311 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 {. double r
16312 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 28 1, r2;. if(
16313 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d (f1&MEM_Real)==
16314 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20 0 ){. r1
16315 3d 20 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 = pMem1->u.i;.
16316 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
16317 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 r1 = pMem1->r
16318 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
16319 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c if( (f2&MEM_Real
1631a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1631b 72 32 20 3d 20 70 4d 65 6d 32 2d 3e 75 2e 69 3b r2 = pMem2->u.i;
1631c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1631d 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32 r2 = pMem2
1631e 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ->r;. }.
1631f 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72 if( r1<r2 ) r
16320 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 eturn -1;.
16321 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 75 if( r1>r2 ) retu
16322 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 rn 1;. retu
16323 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b rn 0;. }else{
16324 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 . assert( f
16325 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 1&MEM_Int );.
16326 20 20 20 61 73 73 65 72 74 28 20 66 32 26 4d 45 assert( f2&ME
16327 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 69 M_Int );. i
16328 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c 20 f( pMem1->u.i <
16329 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 pMem2->u.i ) ret
1632a 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 66 urn -1;. if
1632b 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 70 ( pMem1->u.i > p
1632c 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75 Mem2->u.i ) retu
1632d 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 rn 1;. retu
1632e 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 0;. }. }.
1632f 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c . /* If one val
16330 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 61 ue is a string a
16331 6e 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20 nd the other is
16332 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 69 a blob, the stri
16333 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a ng is less.. **
16334 20 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 72 If both are str
16335 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 73 ings, compare us
16336 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e ing the collatin
16337 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a g functions.. *
16338 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 /. if( combined
16339 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 _flags&MEM_Str )
1633a 7b 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 {. if( (f1 &
1633b 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 MEM_Str)==0 ){.
1633c 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
1633d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 32 }. if( (f2
1633e 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 & MEM_Str)==0 )
1633f 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d {. return -
16340 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 1;. }.. as
16341 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 sert( pMem1->enc
16342 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b 0a ==pMem2->enc );.
16343 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
16344 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 1->enc==SQLITE_U
16345 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 TF8 || .
16346 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d pMem1->enc==
16347 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c SQLITE_UTF16LE |
16348 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 | pMem1->enc==SQ
16349 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a LITE_UTF16BE );.
1634a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c . /* The coll
1634b 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6d ation sequence m
1634c 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 ust be defined a
1634d 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 76 t this point, ev
1634e 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 65 en if. ** the
1634f 20 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 68 user deletes th
16350 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 e collation sequ
16351 65 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 76 ence after the v
16352 64 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a 20 dbe program is.
16353 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28 ** compiled (
16354 74 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c 77 this was not alw
16355 61 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a 20 ays the case)..
16356 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
16357 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c ( !pColl || pCol
16358 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 20 l->xCmp );..
16359 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 if( pColl ){.
1635a 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 6e if( pMem1->en
1635b 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b c==pColl->enc ){
1635c 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
1635d 73 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 65 strings are alre
1635e 61 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 65 ady in the corre
1635f 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 61 ct encoding. Ca
16360 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a ll the. *
16361 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e * comparison fun
16362 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 2a ction directly *
16363 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e /. return
16364 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f pColl->xCmp(pCo
16365 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 2d ll->pUser,pMem1-
16366 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 6d >n,pMem1->z,pMem
16367 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b 0a 2->n,pMem2->z);.
16368 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
16369 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 const void
1636a 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 20 *v1, *v2;.
1636b 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 20 int n1, n2;.
1636c 20 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 20 Mem c1;.
1636d 20 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 20 Mem c2;.
1636e 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 31 memset(&c1
1636f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 29 , 0, sizeof(c1))
16370 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 ;. memset
16371 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 28 (&c2, 0, sizeof(
16372 63 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 c2));. sq
16373 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c lite3VdbeMemShal
16374 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d 65 lowCopy(&c1, pMe
16375 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a m1, MEM_Ephem);.
16376 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
16377 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
16378 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45 y(&c2, pMem2, ME
16379 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 M_Ephem);.
1637a 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61 v1 = sqlite3Va
1637b 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 lueText((sqlite3
1637c 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f _value*)&c1, pCo
1637d 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 ll->enc);.
1637e 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30 n1 = v1==0 ? 0
1637f 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 20 : c1.n;.
16380 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c v2 = sqlite3Val
16381 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f ueText((sqlite3_
16382 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f 6c value*)&c2, pCol
16383 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 l->enc);.
16384 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20 n2 = v2==0 ? 0
16385 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 20 : c2.n;.
16386 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 rc = pColl->xCmp
16387 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e (pColl->pUser, n
16388 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a 1, v1, n2, v2);.
16389 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1638a 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 dbeMemRelease(&c
1638b 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 1);. sqli
1638c 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1638d 65 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 20 e(&c2);.
1638e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1638f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 }. }. /*
16390 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 If a NULL pointe
16391 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 r was passed as
16392 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63 the collate func
16393 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75 tion, fall throu
16394 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 gh. ** to the
16395 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75 blob case and u
16396 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f se memcmp(). */
16397 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68 . }. . /* Both
16398 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 values must be
16399 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20 blobs. Compare
1639a 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 using memcmp().
1639b 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d */. rc = memcm
1639c 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d p(pMem1->z, pMem
1639d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e 2->z, (pMem1->n>
1639e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d pMem2->n)?pMem2-
1639f 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20 >n:pMem1->n);.
163a0 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 if( rc==0 ){.
163a1 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d rc = pMem1->n -
163a2 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 pMem2->n;. }.
163a3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
163a4 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f *.** Move data o
163a5 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65 ut of a btree ke
163a6 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20 y or data field
163a7 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73 and into a Mem s
163a8 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 tructure..** The
163a9 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20 data or key is
163aa 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65 taken from the e
163ab 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20 69 ntry that pCur i
163ac 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
163ad 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66 ting.** to. off
163ae 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65 set and amt dete
163af 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69 rmine what porti
163b0 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f on of the data o
163b1 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76 r key to retriev
163b2 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75 e..** key is tru
163b3 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79 e to get the key
163b4 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74 or false to get
163b5 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 75 data. The resu
163b6 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a lt is written.**
163b7 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65 into the pMem e
163b8 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 lement..**.** Th
163b9 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65 e pMem structure
163ba 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 is assumed to b
163bb 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e e uninitialized.
163bc 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74 Any prior cont
163bd 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72 ent.** is overwr
163be 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65 itten without be
163bf 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a ing freed..**.**
163c0 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 If this routine
163c1 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 fails for any r
163c2 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65 eason (malloc re
163c3 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e turns NULL or un
163c4 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20 able.** to read
163c5 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74 from the disk) t
163c6 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20 hen the pMem is
163c7 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e left in an incon
163c8 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a sistent state..*
163c9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
163ca 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
163cb 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20 MemFromBtree(.
163cc 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
163cd 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e /* Cursor poin
163ce 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74 ting at record t
163cf 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20 o retrieve. */.
163d0 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20 int offset,
163d1 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f /* Offset fro
163d2 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 64 m the start of d
163d3 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79 ata to return by
163d4 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69 tes from. */. i
163d5 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
163d6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
163d7 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a tes to return. *
163d8 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20 /. int key,
163d9 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 /* If true
163da 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20 , retrieve from
163db 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e the btree key, n
163dc 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65 ot data. */. Me
163dd 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 m *pMem
163de 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64 /* OUT: Return d
163df 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20 ata in this Mem
163e0 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b structure. */.){
163e1 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 . char *zData;
163e2 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72 /* Data fr
163e3 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 om the btree lay
163e4 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 er */. int avai
163e5 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75 lable = 0; /* Nu
163e6 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 mber of bytes av
163e7 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c ailable on the l
163e8 6f 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 20 ocal btree page
163e9 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
163ea 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 ; /* Datab
163eb 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
163ec 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
163ed 49 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20 ITE_OK;.. db =
163ee 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
163ef 6f 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73 orDb(pCur);. as
163f0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
163f1 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
163f2 65 78 29 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 ex) );. if( key
163f3 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 ){. zData =
163f4 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 (char *)sqlite3B
163f5 74 72 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 treeKeyFetch(pCu
163f6 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a r, &available);.
163f7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 }else{. zDa
163f8 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c ta = (char *)sql
163f9 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 ite3BtreeDataFet
163fa 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 ch(pCur, &availa
163fb 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 ble);. }. asse
163fc 72 74 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a rt( zData!=0 );.
163fd 0a 20 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d . if( offset+am
163fe 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20 t<=available &&
163ff 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ((pMem->flags&ME
16400 4d 5f 44 79 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65 M_Dyn)==0 || pMe
16401 6d 2d 3e 78 44 65 6c 29 20 29 7b 0a 20 20 20 20 m->xDel) ){.
16402 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
16403 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 lease(pMem);.
16404 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 pMem->z = &zDat
16405 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70 a[offset];. p
16406 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
16407 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b _Blob|MEM_Ephem;
16408 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c . }else if( SQL
16409 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 ITE_OK==(rc = sq
1640a 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1640b 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 (pMem, amt+2, 0)
1640c 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 ) ){. pMem->f
1640d 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c lags = MEM_Blob|
1640e 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d MEM_Dyn|MEM_Term
1640f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 ;. pMem->enc
16410 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 = 0;. pMem->t
16411 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f ype = SQLITE_BLO
16412 42 3b 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 B;. if( key )
16413 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
16414 69 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 ite3BtreeKey(pCu
16415 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 r, offset, amt,
16416 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 pMem->z);. }e
16417 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
16418 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
16419 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 (pCur, offset, a
1641a 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 mt, pMem->z);.
1641b 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b }. pMem->z[
1641c 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d amt] = 0;. pM
1641d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 em->z[amt+1] = 0
1641e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1641f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16420 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
16421 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
16422 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e }. }. pMem->
16423 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75 n = amt;.. retu
16424 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 30 0a rn rc;.}..#if 0.
16425 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 76 61 /*.** Perform va
16426 72 69 6f 75 73 20 63 68 65 63 6b 73 20 6f 6e 20 rious checks on
16427 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
16428 70 4d 65 6d 2e 20 41 6e 20 61 73 73 65 72 74 28 pMem. An assert(
16429 29 20 77 69 6c 6c 0a 2a 2a 20 66 61 69 6c 20 69 ) will.** fail i
1642a 66 20 70 4d 65 6d 20 69 73 20 69 6e 74 65 72 6e f pMem is intern
1642b 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e ally inconsisten
1642c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
1642d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1642e 33 56 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 3VdbeMemSanity(M
1642f 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 em *pMem){. int
16430 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 flags = pMem->f
16431 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 lags;. assert(
16432 66 6c 61 67 73 21 3d 30 20 29 3b 20 20 2f 2a 20 flags!=0 ); /*
16433 4d 75 73 74 20 64 65 66 69 6e 65 20 73 6f 6d 65 Must define some
16434 20 74 79 70 65 20 2a 2f 0a 20 20 69 66 28 20 66 type */. if( f
16435 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c lags & (MEM_Str|
16436 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 MEM_Blob) ){.
16437 20 69 6e 74 20 78 20 3d 20 66 6c 61 67 73 20 26 int x = flags &
16438 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d (MEM_Static|MEM
16439 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d _Dyn|MEM_Ephem|M
1643a 45 4d 5f 53 68 6f 72 74 29 3b 0a 20 20 20 20 61 EM_Short);. a
1643b 73 73 65 72 74 28 20 78 21 3d 30 20 29 3b 20 20 ssert( x!=0 );
1643c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 /* Str
1643d 69 6e 67 73 20 6d 75 73 74 20 64 65 66 69 6e 65 ings must define
1643e 20 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 70 a string subtyp
1643f 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 e */. assert(
16440 20 28 78 20 26 20 28 78 2d 31 29 29 3d 3d 30 20 (x & (x-1))==0
16441 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 ); /* Only one
16442 73 74 72 69 6e 67 20 73 75 62 74 79 70 65 20 63 string subtype c
16443 61 6e 20 62 65 20 64 65 66 69 6e 65 64 20 2a 2f an be defined */
16444 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
16445 6d 2d 3e 7a 21 3d 30 20 29 3b 20 20 20 20 20 20 m->z!=0 );
16446 2f 2a 20 53 74 72 69 6e 67 73 20 6d 75 73 74 20 /* Strings must
16447 68 61 76 65 20 61 20 76 61 6c 75 65 20 2a 2f 0a have a value */.
16448 20 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 /* Mem.z poi
16449 6e 74 73 20 74 6f 20 4d 65 6d 2e 7a 53 68 6f 72 nts to Mem.zShor
1644a 74 20 69 66 66 20 74 68 65 20 73 75 62 74 79 70 t iff the subtyp
1644b 65 20 69 73 20 4d 45 4d 5f 53 68 6f 72 74 20 2a e is MEM_Short *
1644c 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 78 /. assert( (x
1644d 20 26 20 4d 45 4d 5f 53 68 6f 72 74 29 3d 3d 30 & MEM_Short)==0
1644e 20 7c 7c 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 || pMem->z==pMe
1644f 6d 2d 3e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20 m->zShort );.
16450 20 61 73 73 65 72 74 28 20 28 78 20 26 20 4d 45 assert( (x & ME
16451 4d 5f 53 68 6f 72 74 29 21 3d 30 20 7c 7c 20 70 M_Short)!=0 || p
16452 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 53 Mem->z!=pMem->zS
16453 68 6f 72 74 20 29 3b 0a 20 20 20 20 2f 2a 20 4e hort );. /* N
16454 6f 20 64 65 73 74 72 75 63 74 6f 72 20 75 6e 6c o destructor unl
16455 65 73 73 20 74 68 65 72 65 20 69 73 20 4d 45 4d ess there is MEM
16456 5f 44 79 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 _Dyn */. asse
16457 72 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d rt( pMem->xDel==
16458 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 0 || (pMem->flag
16459 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 s & MEM_Dyn)!=0
1645a 29 3b 0a 0a 20 20 20 20 69 66 28 20 28 66 6c 61 );.. if( (fla
1645b 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 20 29 7b gs & MEM_Str) ){
1645c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1645d 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 Mem->enc==SQLITE
1645e 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 _UTF8 || .
1645f 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e pMem->en
16460 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 c==SQLITE_UTF16B
16461 45 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 E ||.
16462 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 pMem->enc==SQ
16463 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20 LITE_UTF16LE .
16464 20 20 20 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 );. /*
16465 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 If the string is
16466 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 61 UTF-8 encoded a
16467 6e 64 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 nd nul terminate
16468 64 2c 20 74 68 65 6e 20 70 4d 65 6d 2d 3e 6e 0a d, then pMem->n.
16469 20 20 20 20 20 20 2a 2a 20 6d 75 73 74 20 62 65 ** must be
1646a 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 the length of t
1646b 68 65 20 73 74 72 69 6e 67 2e 20 20 28 4c 61 74 he string. (Lat
1646c 65 72 3a 29 20 20 49 66 20 74 68 65 20 64 61 74 er:) If the dat
1646d 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 abase file.
1646e 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 72 ** has been cor
1646f 72 75 70 74 65 64 2c 20 27 5c 30 30 30 27 20 63 rupted, '\000' c
16470 68 61 72 61 63 74 65 72 73 20 6d 69 67 68 74 20 haracters might
16471 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 have been insert
16472 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f ed. ** into
16473 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 the middle of t
16474 68 65 20 73 74 72 69 6e 67 2e 20 20 49 6e 20 74 he string. In t
16475 68 61 74 20 63 61 73 65 2c 20 74 68 65 20 73 74 hat case, the st
16476 72 6c 65 6e 28 29 20 6d 69 67 68 74 0a 20 20 20 rlen() might.
16477 20 20 20 2a 2a 20 62 65 20 6c 65 73 73 2e 0a 20 ** be less..
16478 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
16479 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c ( pMem->enc==SQL
1647a 49 54 45 5f 55 54 46 38 20 26 26 20 28 66 6c 61 ITE_UTF8 && (fla
1647b 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 20 29 gs & MEM_Term) )
1647c 7b 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 { . asser
1647d 74 28 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e t( strlen(pMem->
1647e 7a 29 3c 3d 70 4d 65 6d 2d 3e 6e 20 29 3b 0a 20 z)<=pMem->n );.
1647f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
16480 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 3d Mem->z[pMem->n]=
16481 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 =0 );. }.
16482 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
16483 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 65 66 69 6e /* Cannot defin
16484 65 20 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 e a string subty
16485 70 65 20 66 6f 72 20 6e 6f 6e 2d 73 74 72 69 6e pe for non-strin
16486 67 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 20 g objects */.
16487 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
16488 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 61 flags & (MEM_Sta
16489 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f tic|MEM_Dyn|MEM_
1648a 45 70 68 65 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29 Ephem|MEM_Short)
1648b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 )==0 );. asse
1648c 72 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d rt( pMem->xDel==
1648d 30 20 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 4d 45 0 );. }. /* ME
1648e 4d 5f 4e 75 6c 6c 20 65 78 63 6c 75 64 65 73 20 M_Null excludes
1648f 61 6c 6c 20 6f 74 68 65 72 20 74 79 70 65 73 20 all other types
16490 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d */. assert( (pM
16491 65 6d 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 em->flags&(MEM_S
16492 74 72 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 tr|MEM_Int|MEM_R
16493 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d eal|MEM_Blob))==
16494 30 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 0. || (
16495 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
16496 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 2f 2a Null)==0 );. /*
16497 20 49 66 20 74 68 65 20 4d 45 4d 20 69 73 20 62 If the MEM is b
16498 6f 74 68 20 72 65 61 6c 20 61 6e 64 20 69 6e 74 oth real and int
16499 65 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65 73 eger, the values
1649a 20 61 72 65 20 65 71 75 61 6c 20 2a 2f 0a 20 20 are equal */.
1649b 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
1649c 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c lags & (MEM_Int|
1649d 4d 45 4d 5f 52 65 61 6c 29 29 21 3d 28 4d 45 4d MEM_Real))!=(MEM
1649e 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 0a _Int|MEM_Real) .
1649f 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4d 65 || pMe
164a0 6d 2d 3e 72 3d 3d 70 4d 65 6d 2d 3e 75 2e 69 20 m->r==pMem->u.i
164a1 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 );.}.#endif../*
164a2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
164a3 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
164a4 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 internally, it i
164a5 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 s not part of th
164a6 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 e.** external AP
164a7 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 I. It works in a
164a8 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 similar way to
164a9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
164aa 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 xt(),.** except
164ab 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 the data returne
164ac 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f d is in the enco
164ad 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 ding specified b
164ae 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 y the second.**
164af 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 parameter, which
164b0 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 must be one of
164b1 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 SQLITE_UTF16BE,
164b2 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f SQLITE_UTF16LE o
164b3 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 r.** SQLITE_UTF8
164b4 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 ..**.** (2006-02
164b5 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 -16:) The enc v
164b6 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 alue can be or-e
164b7 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 d with SQLITE_UT
164b8 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 F16_ALIGNED..**
164b9 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 If that is the c
164ba 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 ase, then the re
164bb 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 sult must be ali
164bc 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 gned on an even
164bd 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 byte.** boundary
164be 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
164bf 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a ATE const void *
164c0 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
164c1 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 (sqlite3_value*
164c2 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 pVal, u8 enc){.
164c3 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 if( !pVal ) ret
164c4 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 urn 0;.. assert
164c5 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c ( pVal->db==0 ||
164c6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
164c7 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 eld(pVal->db->mu
164c8 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
164c9 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 ( (enc&3)==(enc&
164ca 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c ~SQLITE_UTF16_AL
164cb 49 47 4e 45 44 29 20 29 3b 0a 0a 20 20 69 66 28 IGNED) );.. if(
164cc 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d pVal->flags&MEM
164cd 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 _Null ){. ret
164ce 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 urn 0;. }. ass
164cf 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e ert( (MEM_Blob>>
164d0 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 3) == MEM_Str );
164d1 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c . pVal->flags |
164d2 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 = (pVal->flags &
164d3 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 MEM_Blob)>>3;.
164d4 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c expandBlob(pVal
164d5 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 );. if( pVal->f
164d6 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a lags&MEM_Str ){.
164d7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
164d8 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 hangeEncoding(pV
164d9 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 al, enc & ~SQLIT
164da 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 E_UTF16_ALIGNED)
164db 3b 0a 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 ;. if( (enc &
164dc 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c SQLITE_UTF16_AL
164dd 49 47 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d IGNED)!=0 && 1==
164de 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f (1&SQLITE_PTR_TO
164df 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 _INT(pVal->z)) )
164e0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
164e1 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 (pVal->flags & (
164e2 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 MEM_Ephem|MEM_St
164e3 61 74 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 atic))!=0 );.
164e4 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
164e5 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
164e6 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 le(pVal)!=SQLITE
164e7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
164e8 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d eturn 0;. }
164e9 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
164ea 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d e3VdbeMemNulTerm
164eb 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d inate(pVal);. }
164ec 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
164ed 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d ( (pVal->flags&M
164ee 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 EM_Blob)==0 );.
164ef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
164f0 6d 53 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c mStringify(pVal,
164f1 20 65 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72 enc);. asser
164f2 74 28 20 30 3d 3d 28 31 26 28 69 6e 74 29 70 56 t( 0==(1&(int)pV
164f3 61 6c 2d 3e 7a 29 20 29 3b 0a 20 20 7d 0a 20 20 al->z) );. }.
164f4 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63 assert(pVal->enc
164f5 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 ==(enc & ~SQLITE
164f6 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 _UTF16_ALIGNED)
164f7 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 || pVal->db==0.
164f8 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
164f9 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 pVal->db->malloc
164fa 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 Failed );. if(
164fb 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 pVal->enc==(enc
164fc 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f & ~SQLITE_UTF16_
164fd 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20 ALIGNED) ){.
164fe 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a return pVal->z;.
164ff 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
16500 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a urn 0;. }.}../*
16501 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
16502 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
16503 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 bject..*/.SQLITE
16504 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
16505 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 _value *sqlite3V
16506 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 alueNew(sqlite3
16507 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d *db){. Mem *p =
16508 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
16509 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
1650a 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b *p));. if( p ){
1650b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 . p->flags =
1650c 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d MEM_Null;. p-
1650d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e >type = SQLITE_N
1650e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d ULL;. p->db =
1650f 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 db;. }. retur
16510 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 n p;.}../*.** Cr
16511 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 eate a new sqlit
16512 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c e3_value object,
16513 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
16514 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a value of pExpr..
16515 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 **.** This only
16516 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73 works for very s
16517 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e imple expression
16518 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f s that consist o
16519 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a f one constant.*
1651a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35 * token (i.e. "5
1651b 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74 ", "5.1", "'a st
1651c 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20 ring'"). If the
1651d 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a expression can.*
1651e 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64 * be converted d
1651f 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76 irectly into a v
16520 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 alue, then the v
16521 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 alue is allocate
16522 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 d and.** a point
16523 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 er written to *p
16524 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 pVal. The caller
16525 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
16526 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 for deallocating
16527 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79 .** the value by
16528 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 passing it to s
16529 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 qlite3ValueFree(
1652a 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74 ) later on. If t
1652b 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a he expression.**
1652c 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 cannot be conve
1652d 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c rted to a value,
1652e 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20 then *ppVal is
1652f 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a set to NULL..*/.
16530 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16531 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 nt sqlite3ValueF
16532 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74 romExpr(. sqlit
16533 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
16534 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
16535 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
16536 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 */. Expr *pExpr
16537 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
16538 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
16539 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a to evaluate */.
1653a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 u8 enc,
1653b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
1653c 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a ncoding to use *
1653d 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c /. u8 affinity,
1653e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1653f 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 Affinity to use
16540 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 */. sqlite3_va
16541 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20 lue **ppVal
16542 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 /* Write the new
16543 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29 value here */.)
16544 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68 {. int op;. ch
16545 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 ar *zVal = 0;.
16546 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
16547 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 Val = 0;.. if(
16548 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70 !pExpr ){. *p
16549 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 pVal = 0;. re
1654a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1654b 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 }. op = pExpr
1654c 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70 3d ->op;.. if( op=
1654d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 =TK_STRING || op
1654e 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 ==TK_FLOAT || op
1654f 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a ==TK_INTEGER ){.
16550 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 zVal = sqlit
16551 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 e3DbStrNDup(db,
16552 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f (char*)pExpr->to
16553 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f ken.z, pExpr->to
16554 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61 6c ken.n);. pVal
16555 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e = sqlite3ValueN
16556 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 ew(db);. if(
16557 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 29 !zVal || !pVal )
16558 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
16559 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 sqlite3Dequote
1655a 28 7a 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 (zVal);. sqli
1655b 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 te3ValueSetStr(p
1655c 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 Val, -1, zVal, S
1655d 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 QLITE_UTF8, SQLI
1655e 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 TE_DYNAMIC);.
1655f 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54 if( (op==TK_INT
16560 45 47 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 EGER || op==TK_F
16561 4c 4f 41 54 20 29 20 26 26 20 61 66 66 69 6e 69 LOAT ) && affini
16562 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ty==SQLITE_AFF_N
16563 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ONE ){. sql
16564 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 ite3ValueApplyAf
16565 66 69 6e 69 74 79 28 70 56 61 6c 2c 20 53 51 4c finity(pVal, SQL
16566 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c ITE_AFF_NUMERIC,
16567 20 65 6e 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 enc);. }else
16568 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
16569 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 alueApplyAffinit
1656a 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 y(pVal, affinity
1656b 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 , enc);. }.
1656c 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b }else if( op==TK
1656d 5f 55 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20 _UMINUS ) {.
1656e 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 if( SQLITE_OK==s
1656f 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 qlite3ValueFromE
16570 78 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c xpr(db,pExpr->pL
16571 65 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79 eft,enc,affinity
16572 2c 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 ,&pVal) ){.
16573 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20 pVal->u.i = -1
16574 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 * pVal->u.i;.
16575 20 20 20 70 56 61 6c 2d 3e 72 20 3d 20 2d 31 2e pVal->r = -1.
16576 30 20 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 0 * pVal->r;.
16577 20 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 }. }.#ifndef S
16578 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f QLITE_OMIT_BLOB_
16579 4c 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 69 LITERAL. else i
1657a 66 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 f( op==TK_BLOB )
1657b 7b 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a {. int nVal;.
1657c 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
1657d 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b r->token.n>=3 );
1657e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
1657f 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d pr->token.z[0]==
16580 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 'x' || pExpr->to
16581 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b ken.z[0]=='X' );
16582 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
16583 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d pr->token.z[1]==
16584 27 5c 27 27 20 29 3b 0a 20 20 20 20 61 73 73 65 '\'' );. asse
16585 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e rt( pExpr->token
16586 2e 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e .z[pExpr->token.
16587 6e 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 n-1]=='\'' );.
16588 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 pVal = sqlite3
16589 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 ValueNew(db);.
1658a 20 20 69 66 28 20 21 70 56 61 6c 20 29 20 67 6f if( !pVal ) go
1658b 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 6e to no_mem;. n
1658c 56 61 6c 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b Val = pExpr->tok
1658d 65 6e 2e 6e 20 2d 20 33 3b 0a 20 20 20 20 7a 56 en.n - 3;. zV
1658e 61 6c 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 al = (char*)pExp
1658f 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a r->token.z + 2;.
16590 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
16591 65 6d 53 65 74 53 74 72 28 70 56 61 6c 2c 20 73 emSetStr(pVal, s
16592 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 qlite3HexToBlob(
16593 64 62 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c db, zVal, nVal),
16594 20 6e 56 61 6c 2f 32 2c 0a 20 20 20 20 20 20 20 nVal/2,.
16595 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16596 20 20 30 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 0, SQLITE_DYNA
16597 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 MIC);. }.#endif
16598 0a 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 61 .. *ppVal = pVa
16599 6c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 l;. return SQLI
1659a 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a TE_OK;..no_mem:.
1659b 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
1659c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 ed = 1;. sqlite
1659d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61 6c 3DbFree(db, zVal
1659e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 );. sqlite3Valu
1659f 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 2a eFree(pVal);. *
165a0 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 ppVal = 0;. ret
165a1 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
165a2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
165a3 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 6c e the string val
165a4 75 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 ue of an sqlite3
165a5 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f _value object.*/
165a6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
165a7 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 void sqlite3Valu
165a8 65 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69 74 eSetStr(. sqlit
165a9 65 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 20 e3_value *v,
165aa 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 20 /* Value to be
165ab 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 set */. int n,
165ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
165ad 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 * Length of stri
165ae 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ng z */. const
165af 76 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 20 void *z,
165b0 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6e /* Text of the n
165b1 65 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 75 ew string */. u
165b2 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 8 enc,
165b3 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 /* Encoding
165b4 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f 69 to use */. voi
165b5 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
165b6 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 /* Destructor
165b7 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 for the string
165b8 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29 20 */.){. if( v )
165b9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
165ba 74 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20 7a tStr((Mem *)v, z
165bb 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 3b , n, enc, xDel);
165bc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
165bd 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 n sqlite3_value
165be 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 object.*/.SQLITE
165bf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
165c0 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 lite3ValueFree(s
165c1 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 29 qlite3_value *v)
165c2 7b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 {. if( !v ) ret
165c3 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 urn;. sqlite3Vd
165c4 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d 65 beMemRelease((Me
165c5 6d 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74 65 m *)v);. sqlite
165c6 33 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29 76 3DbFree(((Mem*)v
165c7 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a )->db, v);.}../*
165c8 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
165c9 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
165ca 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 n the sqlite3_va
165cb 6c 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75 6d lue object assum
165cc 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20 75 ing.** that it u
165cd 73 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 ses the encoding
165ce 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 "enc".*/.SQLITE
165cf 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
165d0 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73 ite3ValueBytes(s
165d1 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
165d2 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 4d al, u8 enc){. M
165d3 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 em *p = (Mem*)pV
165d4 61 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c al;. if( (p->fl
165d5 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 ags & MEM_Blob)!
165d6 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61 6c =0 || sqlite3Val
165d7 75 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e 63 ueText(pVal, enc
165d8 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e ) ){. if( p->
165d9 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f flags & MEM_Zero
165da 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
165db 20 70 2d 3e 6e 2b 70 2d 3e 75 2e 69 3b 0a 20 20 p->n+p->u.i;.
165dc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
165dd 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 eturn p->n;.
165de 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
165df 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
165e0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 **** End of vdbe
165e1 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mem.c **********
165e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
165e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
165e4 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
165e5 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
165e6 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a vdbeaux.c ******
165e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
165e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
165e9 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 ***/./*.** 2003
165ea 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a September 6.**.*
165eb 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
165ec 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
165ed 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
165ee 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
165ef 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
165f0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
165f1 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
165f2 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
165f3 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
165f4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
165f5 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
165f6 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
165f7 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
165f8 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
165f9 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
165fa 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
165fb 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
165fc 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
165fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
165fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
165ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
16601 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
16602 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63 code used for c
16603 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79 reating, destroy
16604 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74 ing, and populat
16605 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f ing.** a VDBE (o
16606 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 r an "sqlite3_st
16607 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f mt" as it is kno
16608 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64 wn to the outsid
16609 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72 e world.) Prior
1660a 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32 .** to version 2
1660b 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63 .8.7, all this c
1660c 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64 ode was combined
1660d 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63 into the vdbe.c
1660e 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a source file..**
1660f 20 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 77 But that file w
16610 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62 as getting too b
16611 69 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f ig so this subro
16612 75 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69 utines were spli
16613 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 t out..**.** $Id
16614 3a 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e : vdbeaux.c,v 1.
16615 34 31 38 20 32 30 30 38 2f 31 31 2f 30 35 20 31 418 2008/11/05 1
16616 37 3a 34 31 3a 31 39 20 64 72 68 20 45 78 70 20 7:41:19 drh Exp
16617 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 $.*/..../*.** Wh
16618 65 6e 20 64 65 62 75 67 67 69 6e 67 20 74 68 65 en debugging the
16619 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 code generator
1661a 69 6e 20 61 20 73 79 6d 62 6f 6c 69 63 20 64 65 in a symbolic de
1661b 62 75 67 67 65 72 2c 20 6f 6e 65 20 63 61 6e 0a bugger, one can.
1661c 2a 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 ** set the sqlit
1661d 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 e3VdbeAddopTrace
1661e 20 74 6f 20 31 20 61 6e 64 20 61 6c 6c 20 6f 70 to 1 and all op
1661f 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 70 72 codes will be pr
16620 69 6e 74 65 64 0a 2a 2a 20 61 73 20 74 68 65 79 inted.** as they
16621 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 are added to th
16622 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 73 74 e instruction st
16623 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ream..*/.#ifdef
16624 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
16625 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16626 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 sqlite3VdbeAddop
16627 54 72 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69 Trace = 0;.#endi
16628 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 f.../*.** Create
16629 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 64 a new virtual d
1662a 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 2e 0a atabase engine..
1662b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1662c 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56 E Vdbe *sqlite3V
1662d 64 62 65 43 72 65 61 74 65 28 73 71 6c 69 74 65 dbeCreate(sqlite
1662e 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 3 *db){. Vdbe *
1662f 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p;. p = sqlite3
16630 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
16631 20 73 69 7a 65 6f 66 28 56 64 62 65 29 20 29 3b sizeof(Vdbe) );
16632 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
16633 74 75 72 6e 20 30 3b 0a 20 20 70 2d 3e 64 62 20 turn 0;. p->db
16634 3d 20 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e = db;. if( db->
16635 70 56 64 62 65 20 29 7b 0a 20 20 20 20 64 62 2d pVdbe ){. db-
16636 3e 70 56 64 62 65 2d 3e 70 50 72 65 76 20 3d 20 >pVdbe->pPrev =
16637 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4e 65 78 p;. }. p->pNex
16638 74 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a 20 t = db->pVdbe;.
16639 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 p->pPrev = 0;.
1663a 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 3b 0a db->pVdbe = p;.
1663b 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
1663c 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 E_MAGIC_INIT;.
1663d 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
1663e 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 ** Remember the
1663f 53 51 4c 20 73 74 72 69 6e 67 20 66 6f 72 20 61 SQL string for a
16640 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
16641 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ent..*/.SQLITE_P
16642 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
16643 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 te3VdbeSetSql(Vd
16644 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 be *p, const cha
16645 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 r *z, int n){.
16646 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
16647 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e n;. assert( p->
16648 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e zSql==0 );. p->
16649 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 zSql = sqlite3Db
1664a 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a StrNDup(p->db, z
1664b 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 , n);.}../*.** R
1664c 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61 73 eturn the SQL as
1664d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
1664e 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1664f 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 nt.*/.SQLITE_API
16650 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
16651 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 ite3_sql(sqlite3
16652 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 _stmt *pStmt){.
16653 20 72 65 74 75 72 6e 20 28 28 56 64 62 65 20 2a return ((Vdbe *
16654 29 70 53 74 6d 74 29 2d 3e 7a 53 71 6c 3b 0a 7d )pStmt)->zSql;.}
16655 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c ../*.** Swap all
16656 20 63 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e content between
16657 20 74 77 6f 20 56 44 42 45 20 73 74 72 75 63 74 two VDBE struct
16658 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ures..*/.SQLITE_
16659 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1665a 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 ite3VdbeSwap(Vdb
1665b 65 20 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 e *pA, Vdbe *pB)
1665c 7b 0a 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 {. Vdbe tmp, *p
1665d 54 6d 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d Tmp;. char *zTm
1665e 70 3b 0a 20 20 69 6e 74 20 6e 54 6d 70 3b 0a 20 p;. int nTmp;.
1665f 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a 70 tmp = *pA;. *p
16660 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20 3d A = *pB;. *pB =
16661 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 tmp;. pTmp = p
16662 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d 3e A->pNext;. pA->
16663 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65 78 pNext = pB->pNex
16664 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20 3d t;. pB->pNext =
16665 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 pTmp;. pTmp =
16666 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41 2d pA->pPrev;. pA-
16667 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50 72 >pPrev = pB->pPr
16668 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76 20 ev;. pB->pPrev
16669 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20 3d = pTmp;. zTmp =
1666a 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41 2d pA->zSql;. pA-
1666b 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c >zSql = pB->zSql
1666c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a ;. pB->zSql = z
1666d 54 6d 70 3b 0a 20 20 6e 54 6d 70 20 3d 20 70 41 Tmp;. nTmp = pA
1666e 2d 3e 6e 53 71 6c 3b 0a 20 20 70 41 2d 3e 6e 53 ->nSql;. pA->nS
1666f 71 6c 20 3d 20 70 42 2d 3e 6e 53 71 6c 3b 0a 20 ql = pB->nSql;.
16670 20 70 42 2d 3e 6e 53 71 6c 20 3d 20 6e 54 6d 70 pB->nSql = nTmp
16671 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
16672 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 TE_DEBUG./*.** T
16673 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f urn tracing on o
16674 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f r off.*/.SQLITE_
16675 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
16676 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64 ite3VdbeTrace(Vd
16677 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61 be *p, FILE *tra
16678 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65 20 ce){. p->trace
16679 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64 69 = trace;.}.#endi
1667a 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 f../*.** Resize
1667b 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72 the Vdbe.aOp arr
1667c 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 63 6f ay so that it co
1667d 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20 ntains at least
1667e 4e 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a N.** elements..*
1667f 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d 6f *.** If an out-o
16680 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 6f f-memory error o
16681 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73 69 ccurs while resi
16682 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c 0a zing the array,.
16683 2a 2a 20 56 64 62 65 2e 61 4f 70 20 61 6e 64 20 ** Vdbe.aOp and
16684 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65 Vdbe.nOpAlloc re
16685 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 28 main unchanged (
16686 74 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 0a this is so that.
16687 2a 2a 20 61 6e 79 20 6f 70 63 6f 64 65 73 20 61 ** any opcodes a
16688 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 lready allocated
16689 20 63 61 6e 20 62 65 20 63 6f 72 72 65 63 74 6c can be correctl
1668a 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a y deallocated.**
1668b 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 along with the
1668c 72 65 73 74 20 6f 66 20 74 68 65 20 56 64 62 65 rest of the Vdbe
1668d 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
1668e 64 20 72 65 73 69 7a 65 4f 70 41 72 72 61 79 28 d resizeOpArray(
1668f 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 4e 29 7b Vdbe *p, int N){
16690 0a 20 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b . VdbeOp *pNew;
16691 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
16692 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 3DbRealloc(p->db
16693 2c 20 70 2d 3e 61 4f 70 2c 20 4e 2a 73 69 7a 65 , p->aOp, N*size
16694 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66 28 20 70 of(Op));. if( p
16695 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f New ){. p->nO
16696 70 41 6c 6c 6f 63 20 3d 20 4e 3b 0a 20 20 20 20 pAlloc = N;.
16697 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 p->aOp = pNew;.
16698 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 }.}../*.** Add
16699 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f a new instructio
1669a 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 n to the list of
1669b 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 63 75 instructions cu
1669c 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a 2a 20 rrent in the.**
1669d 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20 74 68 VDBE. Return th
1669e 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
1669f 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e new instruction
166a0 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 ..**.** Paramete
166a1 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 rs:.**.** p
166a2 20 20 20 20 20 20 20 20 20 20 20 20 20 50 6f 69 Poi
166a3 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 42 45 nter to the VDBE
166a4 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 20 20 .**.** op
166a5 20 20 20 20 20 20 20 20 20 20 54 68 65 20 6f 70 The op
166a6 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 69 6e code for this in
166a7 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 struction.**.**
166a8 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20 20 20 p1, p2, p3
166a9 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a Operands.**.*
166aa 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 * Use the sqlite
166ab 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
166ac 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 l() function to
166ad 66 69 78 20 61 6e 20 61 64 64 72 65 73 73 20 61 fix an address a
166ae 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 nd.** the sqlite
166af 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 29 20 3VdbeChangeP4()
166b0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68 61 6e function to chan
166b1 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
166b2 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72 61 6e the P4.** operan
166b3 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
166b4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
166b5 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 65 20 VdbeAddOp3(Vdbe
166b6 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 *p, int op, int
166b7 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e 74 20 p1, int p2, int
166b8 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 p3){. int i;.
166b9 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 VdbeOp *pOp;..
166ba 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 i = p->nOp;. as
166bb 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
166bc 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
166bd 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 );. if( p->nOpA
166be 6c 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 72 lloc<=i ){. r
166bf 65 73 69 7a 65 4f 70 41 72 72 61 79 28 70 2c 20 esizeOpArray(p,
166c0 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d p->nOpAlloc ? p-
166c1 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a 20 31 30 >nOpAlloc*2 : 10
166c2 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 24/sizeof(Op));.
166c3 20 20 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d if( p->db->m
166c4 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
166c5 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
166c6 20 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f }. }. p->nO
166c7 70 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d p++;. pOp = &p-
166c8 3e 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e >aOp[i];. pOp->
166c9 6f 70 63 6f 64 65 20 3d 20 6f 70 3b 0a 20 20 70 opcode = op;. p
166ca 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20 70 4f Op->p5 = 0;. pO
166cb 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f p->p1 = p1;. pO
166cc 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f p->p2 = p2;. pO
166cd 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20 70 4f p->p3 = p3;. pO
166ce 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 70 p->p4.p = 0;. p
166cf 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f Op->p4type = P4_
166d0 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78 NOTUSED;. p->ex
166d1 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 66 64 65 pired = 0;.#ifde
166d2 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
166d3 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d pOp->zComment =
166d4 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 0;. if( sqlite
166d5 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 3VdbeAddopTrace
166d6 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 ) sqlite3VdbePri
166d7 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61 ntOp(0, i, &p->a
166d8 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23 Op[i]);.#endif.#
166d9 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
166da 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 LE. pOp->cycles
166db 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74 = 0;. pOp->cnt
166dc 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 = 0;.#endif. r
166dd 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 eturn i;.}.SQLIT
166de 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
166df 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
166e0 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29 Vdbe *p, int op)
166e1 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
166e2 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
166e3 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a op, 0, 0, 0);.}.
166e4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
166e5 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
166e6 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 69 6e dOp1(Vdbe *p, in
166e7 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20 t op, int p1){.
166e8 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
166e9 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c dbeAddOp3(p, op,
166ea 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51 p1, 0, 0);.}.SQ
166eb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
166ec 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
166ed 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 p2(Vdbe *p, int
166ee 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20 op, int p1, int
166ef 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 p2){. return sq
166f0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
166f1 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30 p, op, p1, p2, 0
166f2 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 );.}.../*.** Add
166f3 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74 20 an opcode that
166f4 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34 20 includes the p4
166f5 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e 74 value as a point
166f6 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
166f7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
166f8 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 56 3VdbeAddOp4(. V
166f9 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 dbe *p,
166fa 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70 /* Add the op
166fb 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20 code to this VM
166fc 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 */. int op,
166fd 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
166fe 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 new opcode */.
166ff 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 20 20 int p1,
16700 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 6f 70 /* The P1 op
16701 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 erand */. int p
16702 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2, /
16703 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 6e 64 * The P2 operand
16704 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 20 20 */. int p3,
16705 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
16706 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 P3 operand */.
16707 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 const char *zP4
16708 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 20 6f , /* The P4 o
16709 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
1670a 70 34 74 79 70 65 20 20 20 20 20 20 20 20 20 20 p4type
1670b 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 74 79 /* P4 operand ty
1670c 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 pe */.){. int a
1670d 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
1670e 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 eAddOp3(p, op, p
1670f 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 1, p2, p3);. sq
16710 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
16711 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20 4(p, addr, zP4,
16712 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72 p4type);. retur
16713 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n addr;.}../*.**
16714 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 79 Create a new sy
16715 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72 mbolic label for
16716 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 an instruction
16717 74 68 61 74 20 68 61 73 20 79 65 74 20 74 6f 20 that has yet to
16718 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68 be.** coded. Th
16719 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c e symbolic label
1671a 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 is really just
1671b 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 a negative numbe
1671c 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c r. The.** label
1671d 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 can be used as
1671e 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f 66 20 the P2 value of
1671f 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c an operation. L
16720 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 ater, when.** th
16721 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73 6f 6c e label is resol
16722 76 65 64 20 74 6f 20 61 20 73 70 65 63 69 66 69 ved to a specifi
16723 63 20 61 64 64 72 65 73 73 2c 20 74 68 65 20 56 c address, the V
16724 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a DBE will scan.**
16725 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f 70 65 through its ope
16726 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20 ration list and
16727 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65 change all value
16728 73 20 6f 66 20 50 32 20 77 68 69 63 68 20 6d 61 s of P2 which ma
16729 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c tch.** the label
1672a 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c 76 into the resolv
1672b 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a ed address..**.*
1672c 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 73 * The VDBE knows
1672d 20 74 68 61 74 20 61 20 50 32 20 76 61 6c 75 65 that a P2 value
1672e 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65 63 61 is a label beca
1672f 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a use labels are.*
16730 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74 69 76 * always negativ
16731 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65 73 20 e and P2 values
16732 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 62 are suppose to b
16733 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a e non-negative..
16734 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 61 ** Hence, a nega
16735 74 69 76 65 20 50 32 20 76 61 6c 75 65 20 69 73 tive P2 value is
16736 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20 68 61 a label that ha
16737 73 20 79 65 74 20 74 6f 20 62 65 20 72 65 73 6f s yet to be reso
16738 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f lved..**.** Zero
16739 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
1673a 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 a malloc() fails
1673b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1673c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1673d 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 dbeMakeLabel(Vdb
1673e 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a e *p){. int i;.
1673f 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b i = p->nLabel+
16740 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e +;. assert( p->
16741 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
16742 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 C_INIT );. if(
16743 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f i>=p->nLabelAllo
16744 63 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 c ){. p->nLab
16745 65 6c 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 4c 61 elAlloc = p->nLa
16746 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 31 30 3b belAlloc*2 + 10;
16747 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d . p->aLabel =
16748 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
16749 63 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 cOrFree(p->db, p
1674a 2d 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 ->aLabel,.
1674b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1674c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2d p-
1674d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a 73 69 7a >nLabelAlloc*siz
1674e 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d eof(p->aLabel[0]
1674f 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d ));. }. if( p-
16750 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 >aLabel ){. p
16751 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 ->aLabel[i] = -1
16752 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d ;. }. return -
16753 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 1-i;.}../*.** Re
16754 73 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 solve label "x"
16755 74 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73 to be the addres
16756 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e s of the next in
16757 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 struction to.**
16758 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 be inserted. Th
16759 65 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 e parameter "x"
1675a 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f must have been o
1675b 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
1675c 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 a prior call to
1675d 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
1675e 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 abel()..*/.SQLIT
1675f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
16760 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
16761 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 eLabel(Vdbe *p,
16762 69 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 int x){. int j
16763 3d 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 = -1-x;. assert
16764 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
16765 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
16766 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 assert( j>=0 &&
16767 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a j<p->nLabel );.
16768 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 if( p->aLabel
16769 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c ){. p->aLabel
1676a 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 [j] = p->nOp;.
1676b 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 }.}../*.** Loop
1676c 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67 through the prog
1676d 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 ram looking for
1676e 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 P2 values that a
1676f 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f re negative.** o
16770 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 n jump instructi
16771 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20 ons. Each such
16772 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c value is a label
16773 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a . Resolve the.*
16774 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 * label by setti
16775 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 ng the P2 value
16776 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e to its correct n
16777 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a on-zero value..*
16778 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
16779 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 e is called once
1677a 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 after all opcod
1677b 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73 es have been ins
1677c 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 erted..**.** Var
1677d 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 iable *pMaxFuncA
1677e 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 rgs is set to th
1677f 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 e maximum value
16780 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 of any P2 argume
16781 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f nt .** to an OP_
16782 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 Function, OP_Agg
16783 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 Step or OP_VFilt
16784 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 er opcode. This
16785 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 is used by .** s
16786 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 qlite3VdbeMakeRe
16787 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68 ady() to size th
16788 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 e Vdbe.apArg[] a
16789 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rray..**.** This
1678a 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f routine also do
1678b 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 es the following
1678c 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20 optimization:
1678d 49 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20 It scans for.**
1678e 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 instructions tha
1678f 74 20 6d 69 67 68 74 20 63 61 75 73 65 20 61 20 t might cause a
16790 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 statement rollba
16791 63 6b 2e 20 20 53 75 63 68 20 69 6e 73 74 72 75 ck. Such instru
16792 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 3a 0a 2a ctions.** are:.*
16793 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c *.** * OP_Hal
16794 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 t with P1=SQLITE
16795 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 _CONSTRAINT and
16796 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 P2=OE_Abort..**
16797 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a * OP_Destroy.
16798 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 ** * OP_VUpda
16799 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 te.** * OP_VR
1679a 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 49 66 20 6e ename.**.** If n
1679b 6f 20 73 75 63 68 20 69 6e 73 74 72 75 63 74 69 o such instructi
1679c 6f 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 on is found, the
1679d 6e 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e n every Statemen
1679e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 2a t instruction .*
1679f 2a 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 * is changed to
167a0 61 20 4e 6f 6f 70 2e 20 20 49 6e 20 74 68 69 73 a Noop. In this
167a1 20 77 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63 way, we avoid c
167a2 72 65 61 74 69 6e 67 20 74 68 65 20 73 74 61 74 reating the stat
167a3 65 6d 65 6e 74 20 0a 2a 2a 20 6a 6f 75 72 6e 61 ement .** journa
167a4 6c 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61 l file unnecessa
167a5 72 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rily..*/.static
167a6 76 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 void resolveP2Va
167a7 6c 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e lues(Vdbe *p, in
167a8 74 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 t *pMaxFuncArgs)
167a9 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
167aa 20 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20 nMaxArgs = 0;.
167ab 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20 Op *pOp;. int
167ac 2a 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61 *aLabel = p->aLa
167ad 62 65 6c 3b 0a 20 20 69 6e 74 20 64 6f 65 73 53 bel;. int doesS
167ae 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b tatementRollback
167af 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 53 = 0;. int hasS
167b0 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20 tatementBegin =
167b1 30 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 0;. p->readOnly
167b2 20 3d 20 31 3b 0a 20 20 70 2d 3e 75 73 65 73 53 = 1;. p->usesS
167b3 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a tmtJournal = 0;.
167b4 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 for(pOp=p->aOp
167b5 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e , i=p->nOp-1; i>
167b6 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b =0; i--, pOp++){
167b7 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d . u8 opcode =
167b8 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 pOp->opcode;..
167b9 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f if( opcode==O
167ba 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 P_Function || op
167bb 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70 code==OP_AggStep
167bc 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f ){. if( pO
167bd 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29 p->p5>nMaxArgs )
167be 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d nMaxArgs = pOp-
167bf 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c >p5;.#ifndef SQL
167c0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
167c1 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 TABLE. }else
167c2 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 if( opcode==OP_V
167c3 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 Update ){.
167c4 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 if( pOp->p2>nMax
167c5 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 Args ) nMaxArgs
167c6 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 = pOp->p2;.#endi
167c7 66 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 f. }. if(
167c8 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 opcode==OP_Halt
167c9 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 ){. if( pOp
167ca 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e ->p1==SQLITE_CON
167cb 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e STRAINT && pOp->
167cc 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a p2==OE_Abort ){.
167cd 20 20 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 doesStat
167ce 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 ementRollback =
167cf 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
167d0 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d else if( opcode=
167d1 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b =OP_Statement ){
167d2 0a 20 20 20 20 20 20 68 61 73 53 74 61 74 65 6d . hasStatem
167d3 65 6e 74 42 65 67 69 6e 20 3d 20 31 3b 0a 20 20 entBegin = 1;.
167d4 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a p->usesStmtJ
167d5 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20 ournal = 1;.
167d6 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 }else if( opcode
167d7 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 29 7b 0a ==OP_Destroy ){.
167d8 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d doesStatem
167d9 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b entRollback = 1;
167da 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f . }else if( o
167db 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 pcode==OP_Transa
167dc 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 ction && pOp->p2
167dd 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e !=0 ){. p->
167de 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 readOnly = 0;.#i
167df 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
167e0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
167e1 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 }else if( opc
167e2 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 ode==OP_VUpdate
167e3 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 || opcode==OP_VR
167e4 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 64 ename ){. d
167e5 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c oesStatementRoll
167e6 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 back = 1;. }e
167e7 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d lse if( opcode==
167e8 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a 20 20 OP_VFilter ){.
167e9 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 int n;.
167ea 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 20 assert( p->nOp
167eb 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 20 20 - i >= 3 );.
167ec 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 assert( pOp[-1
167ed 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 74 ].opcode==OP_Int
167ee 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20 6e 20 eger );. n
167ef 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20 = pOp[-1].p1;.
167f0 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 41 72 if( n>nMaxAr
167f1 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 gs ) nMaxArgs =
167f2 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a n;.#endif. }.
167f3 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
167f4 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f VdbeOpcodeHasPro
167f5 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 4f 50 perty(opcode, OP
167f6 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 4f 70 FLG_JUMP) && pOp
167f7 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 ->p2<0 ){.
167f8 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d 3e assert( -1-pOp->
167f9 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a p2<p->nLabel );.
167fa 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 pOp->p2 =
167fb 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70 aLabel[-1-pOp->p
167fc 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2];. }. }.
167fd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d sqlite3DbFree(p-
167fe 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b >db, p->aLabel);
167ff 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 30 . p->aLabel = 0
16800 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 72 ;.. *pMaxFuncAr
16801 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a 0a gs = nMaxArgs;..
16802 20 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72 /* If we never
16803 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74 rollback a stat
16804 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
16805 6e 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e n, then statemen
16806 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 t. ** transacti
16807 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64 ons are not need
16808 65 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65 ed. So change e
16809 76 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e very OP_Statemen
1680a 74 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e t. ** opcode in
1680b 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20 to an OP_Noop.
1680c 54 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c This avoid a cal
1680d 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70 l to sqlite3OsOp
1680e 65 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20 enExclusive().
1680f 2a 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 ** which can be
16810 65 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d expensive on som
16811 65 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a e platforms.. *
16812 2f 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65 /. if( hasState
16813 6d 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f mentBegin && !do
16814 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 esStatementRollb
16815 61 63 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 75 73 ack ){. p->us
16816 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 esStmtJournal =
16817 30 3b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70 0;. for(pOp=p
16818 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d ->aOp, i=p->nOp-
16819 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 1; i>=0; i--, pO
1681a 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 p++){. if(
1681b 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
1681c 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 Statement ){.
1681d 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 pOp->opcode
1681e 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 = OP_Noop;.
1681f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
16820 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
16821 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
16822 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
16823 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 n to be inserted
16824 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16825 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
16826 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 dbeCurrentAddr(V
16827 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 dbe *p){. asser
16828 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 t( p->magic==VDB
16829 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a E_MAGIC_INIT );.
1682a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b return p->nOp;
1682b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 .}../*.** Add a
1682c 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 whole list of op
1682d 65 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 erations to the
1682e 6f 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e operation stack.
1682f 20 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 Return the.**
16830 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66 address of the f
16831 69 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 irst operation a
16832 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f dded..*/.SQLITE_
16833 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
16834 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
16835 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f (Vdbe *p, int nO
16836 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f p, VdbeOpList co
16837 6e 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 nst *aOp){. int
16838 20 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28 addr;. assert(
16839 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
1683a 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
1683b 69 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 if( p->nOp + nOp
1683c 20 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 29 > p->nOpAlloc )
1683d 7b 0a 20 20 20 20 72 65 73 69 7a 65 4f 70 41 72 {. resizeOpAr
1683e 72 61 79 28 70 2c 20 70 2d 3e 6e 4f 70 41 6c 6c ray(p, p->nOpAll
1683f 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 oc ? p->nOpAlloc
16840 2a 32 20 3a 20 31 30 32 34 2f 73 69 7a 65 6f 66 *2 : 1024/sizeof
16841 28 4f 70 29 29 3b 0a 20 20 20 20 61 73 73 65 72 (Op));. asser
16842 74 28 20 70 2d 3e 6e 4f 70 2b 6e 4f 70 3c 3d 70 t( p->nOp+nOp<=p
16843 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 7c 7c 20 70 2d ->nOpAlloc || p-
16844 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
16845 64 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 d );. }. if( p
16846 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
16847 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ed ){. return
16848 20 30 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 0;. }. addr =
16849 20 70 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 6e p->nOp;. if( n
1684a 4f 70 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 Op>0 ){. int
1684b 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 i;. VdbeOpLis
1684c 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 t const *pIn = a
1684d 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b Op;. for(i=0;
1684e 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e i<nOp; i++, pIn
1684f 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 ++){. int p
16850 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 2 = pIn->p2;.
16851 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 VdbeOp *pOut
16852 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 = &p->aOp[i+addr
16853 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f ];. pOut->o
16854 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 pcode = pIn->opc
16855 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d ode;. pOut-
16856 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 >p1 = pIn->p1;.
16857 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 if( p2<0 &&
16858 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f sqlite3VdbeOpco
16859 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f deHasProperty(pO
1685a 75 74 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c ut->opcode, OPFL
1685b 47 5f 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 G_JUMP) ){.
1685c 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 pOut->p2 = ad
1685d 64 72 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 dr + ADDR(p2);.
1685e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1685f 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 pOut->p2 = p
16860 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 2;. }.
16861 20 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d pOut->p3 = pIn-
16862 3e 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d >p3;. pOut-
16863 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 >p4type = P4_NOT
16864 55 53 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 USED;. pOut
16865 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 ->p4.p = 0;.
16866 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a pOut->p5 = 0;.
16867 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
16868 42 55 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e BUG. pOut->
16869 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 zComment = 0;.
1686a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
1686b 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b dbeAddopTrace ){
1686c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1686d 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 VdbePrintOp(0, i
1686e 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 +addr, &p->aOp[i
1686f 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d +addr]);. }
16870 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 .#endif. }.
16871 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b p->nOp += nOp;
16872 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 . }. return ad
16873 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 dr;.}../*.** Cha
16874 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 nge the value of
16875 20 74 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 the P1 operand
16876 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 for a specific i
16877 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 nstruction..** T
16878 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
16879 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 seful when a lar
1687a 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f ge program is lo
1687b 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 aded from a.** s
1687c 74 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e tatic array usin
1687d 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 g sqlite3VdbeAdd
1687e 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 OpList but we wa
1687f 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 nt to make a.**
16880 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 few minor change
16881 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d s to the program
16882 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16883 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
16884 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 VdbeChangeP1(Vdb
16885 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 e *p, int addr,
16886 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 int val){. asse
16887 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d rt( p==0 || p->m
16888 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
16889 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 _INIT );. if( p
1688a 20 26 26 20 61 64 64 72 3e 3d 30 20 26 26 20 70 && addr>=0 && p
1688b 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d ->nOp>addr && p-
1688c 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 >aOp ){. p->a
1688d 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 Op[addr].p1 = va
1688e 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 l;. }.}../*.**
1688f 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 Change the value
16890 20 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 of the P2 opera
16891 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 nd for a specifi
16892 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a c instruction..*
16893 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
16894 73 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 s useful for set
16895 74 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 ting a jump dest
16896 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 ination..*/.SQLI
16897 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
16898 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
16899 65 50 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 eP2(Vdbe *p, int
1689a 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b addr, int val){
1689b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 . assert( p==0
1689c 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 || p->magic==VDB
1689d 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a E_MAGIC_INIT );.
1689e 20 20 69 66 28 20 70 20 26 26 20 61 64 64 72 3e if( p && addr>
1689f 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 =0 && p->nOp>add
168a0 72 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 r && p->aOp ){.
168a1 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e p->aOp[addr].
168a2 70 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a p2 = val;. }.}.
168a3 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
168a4 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
168a5 33 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 3 operand for a
168a6 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 specific instruc
168a7 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
168a8 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
168a9 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 ite3VdbeChangeP3
168aa 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
168ab 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 dr, int val){.
168ac 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 assert( p==0 ||
168ad 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
168ae 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
168af 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20 f( p && addr>=0
168b0 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 && p->nOp>addr &
168b1 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 & p->aOp ){.
168b2 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 p->aOp[addr].p3
168b3 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a = val;. }.}../*
168b4 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 .** Change the v
168b5 61 6c 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f alue of the P5 o
168b6 70 65 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d perand for the m
168b7 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 ost recently.**
168b8 61 64 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e added operation.
168b9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
168ba 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
168bb 64 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 dbeChangeP5(Vdbe
168bc 20 2a 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 *p, u8 val){.
168bd 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 assert( p==0 ||
168be 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
168bf 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
168c0 66 28 20 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 f( p && p->aOp )
168c1 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d {. assert( p-
168c2 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d >nOp>0 );. p-
168c3 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 >aOp[p->nOp-1].p
168c4 35 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 5 = val;. }.}..
168c5 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
168c6 20 50 32 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 P2 operand of i
168c7 6e 73 74 72 75 63 74 69 6f 6e 20 61 64 64 72 20 nstruction addr
168c8 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 so that it point
168c9 73 20 74 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 s to.** the addr
168ca 65 73 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 ess of the next
168cb 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 instruction to b
168cc 65 20 63 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 e coded..*/.SQLI
168cd 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
168ce 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
168cf 65 72 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ere(Vdbe *p, int
168d0 20 61 64 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 addr){. sqlite
168d1 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 70 2c 3VdbeChangeP2(p,
168d2 20 61 64 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a addr, p->nOp);.
168d3 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 }.../*.** If the
168d4 20 69 6e 70 75 74 20 46 75 6e 63 44 65 66 20 73 input FuncDef s
168d5 74 72 75 63 74 75 72 65 20 69 73 20 65 70 68 65 tructure is ephe
168d6 6d 65 72 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 meral, then free
168d7 20 69 74 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 it. If.** the
168d8 46 75 6e 63 44 65 66 20 69 73 20 6e 6f 74 20 65 FuncDef is not e
168d9 70 68 65 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f phermal, then do
168da 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 nothing..*/.sta
168db 74 69 63 20 76 6f 69 64 20 66 72 65 65 45 70 68 tic void freeEph
168dc 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 emeralFunction(s
168dd 71 6c 69 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 qlite3 *db, Func
168de 44 65 66 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 Def *pDef){. if
168df 28 20 70 44 65 66 20 26 26 20 28 70 44 65 66 2d ( pDef && (pDef-
168e0 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
168e1 46 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 FUNC_EPHEM)!=0 )
168e2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
168e3 72 65 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 ree(db, pDef);.
168e4 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 }.}../*.** Dele
168e5 74 65 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 te a P4 value if
168e6 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 necessary..*/.s
168e7 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 tatic void freeP
168e8 34 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 4(sqlite3 *db, i
168e9 6e 74 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 nt p4type, void
168ea 2a 70 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 *p4){. if( p4 )
168eb 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 34 {. switch( p4
168ec 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 type ){. ca
168ed 73 65 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 se P4_REAL:.
168ee 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a case P4_INT64:
168ef 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d . case P4_M
168f0 50 52 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 PRINTF:. ca
168f1 73 65 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 se P4_DYNAMIC:.
168f2 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 case P4_KEY
168f3 49 4e 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 INFO:. case
168f4 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 P4_INTARRAY:.
168f5 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 case P4_KEYI
168f6 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 NFO_HANDOFF: {.
168f7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
168f8 46 72 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 Free(db, p4);.
168f9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
168fa 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 }. case
168fb 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 P4_VDBEFUNC: {.
168fc 20 20 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 VdbeFunc
168fd 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 *pVdbeFunc = (Vd
168fe 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 beFunc *)p4;.
168ff 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 freeEphemer
16900 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 alFunction(db, p
16901 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 VdbeFunc->pFunc)
16902 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
16903 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 3VdbeDeleteAuxDa
16904 74 61 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 ta(pVdbeFunc, 0)
16905 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
16906 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 3DbFree(db, pVdb
16907 65 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 eFunc);.
16908 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
16909 20 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e case P4_FUN
1690a 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 CDEF: {.
1690b 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e freeEphemeralFun
1690c 63 74 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 ction(db, (FuncD
1690d 65 66 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 ef*)p4);.
1690e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
1690f 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 case P4_ME
16910 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c M: {. sql
16911 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 ite3ValueFree((s
16912 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 qlite3_value*)p4
16913 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b );. break
16914 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
16915 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 }.}.../*.** Ch
16916 61 6e 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 ange N opcodes s
16917 74 61 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 tarting at addr
16918 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 to No-ops..*/.SQ
16919 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1691a 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 d sqlite3VdbeCha
1691b 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a ngeToNoop(Vdbe *
1691c 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 p, int addr, int
1691d 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 N){. if( p &&
1691e 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 p->aOp ){. Vd
1691f 62 65 4f 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e beOp *pOp = &p->
16920 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 aOp[addr];. s
16921 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e qlite3 *db = p->
16922 64 62 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 4e db;. while( N
16923 2d 2d 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 -- ){. free
16924 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 P4(db, pOp->p4ty
16925 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a pe, pOp->p4.p);.
16926 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 memset(pOp
16927 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b , 0, sizeof(pOp[
16928 30 5d 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 0]));. pOp-
16929 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f >opcode = OP_Noo
1692a 70 3b 0a 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a p;. pOp++;.
1692b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1692c 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 ** Change the va
1692d 6c 75 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70 lue of the P4 op
1692e 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 erand for a spec
1692f 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e ific instruction
16930 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
16931 65 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e e is useful when
16932 20 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d a large program
16933 20 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 is loaded from
16934 61 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 a.** static arra
16935 79 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 y using sqlite3V
16936 64 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 dbeAddOpList but
16937 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 we want to make
16938 20 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 a.** few minor
16939 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 changes to the p
1693a 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 rogram..**.** If
1693b 20 6e 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50 n>=0 then the P
1693c 34 20 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e 4 operand is dyn
1693d 61 6d 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 amic, meaning th
1693e 61 74 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 at a copy of.**
1693f 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 the string is ma
16940 64 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f de into memory o
16941 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
16942 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a ite3_malloc()..*
16943 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d * A value of n==
16944 30 20 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74 0 means copy byt
16945 65 73 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 es of zP4 up to
16946 61 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 and including th
16947 65 0a 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 e.** first null
16948 62 79 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 byte. If n>0 th
16949 65 6e 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 en copy n+1 byte
1694a 73 20 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 s of zP4..**.**
1694b 49 66 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f If n==P4_KEYINFO
1694c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a it means that z
1694d 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
1694e 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 to a KeyInfo str
1694f 75 63 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 ucture..** A cop
16950 79 20 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 y is made of the
16951 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
16952 72 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f re into memory o
16953 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
16954 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 sqlite3_malloc,
16955 74 6f 20 62 65 20 66 72 65 65 64 20 77 68 65 6e to be freed when
16956 20 74 68 65 20 56 64 62 65 20 69 73 20 66 69 6e the Vdbe is fin
16957 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 alized..** n==P4
16958 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
16959 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
1695a 7a 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 zP4 points to a
1695b 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
1695c 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d e.** stored in m
1695d 65 6d 6f 72 79 20 74 68 61 74 20 74 68 65 20 63 emory that the c
1695e 61 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e aller has obtain
1695f 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
16960 6d 61 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 malloc. The .**
16961 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f caller should no
16962 74 20 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 t free the alloc
16963 61 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 ation, it will b
16964 65 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 e freed when the
16965 20 56 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 Vdbe is.** fina
16966 6c 69 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 lized..** .** Ot
16967 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 her values of n
16968 28 50 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 (P4_STATIC, P4_C
16969 4f 4c 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 OLLSEQ etc.) ind
1696a 69 63 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 icate that zP4 p
1696b 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 oints.** to a st
1696c 72 69 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 ring or structur
1696d 65 20 74 68 61 74 20 69 73 20 67 75 61 72 61 6e e that is guaran
1696e 74 65 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f teed to exist fo
1696f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f r the lifetime o
16970 66 0a 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 f.** the Vdbe. I
16971 6e 20 74 68 65 73 65 20 63 61 73 65 73 20 77 65 n these cases we
16972 20 63 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 can just copy t
16973 68 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a he pointer..**.*
16974 2a 20 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e * If addr<0 then
16975 20 63 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 change P4 on th
16976 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
16977 69 6e 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 inserted instruc
16978 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
16979 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1697a 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 ite3VdbeChangeP4
1697b 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
1697c 64 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a dr, const char *
1697d 7a 50 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f zP4, int n){. O
1697e 70 20 2a 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 p *pOp;. sqlite
1697f 33 20 2a 64 62 3b 0a 20 20 61 73 73 65 72 74 28 3 *db;. assert(
16980 20 70 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 p!=0 );. db =
16981 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 p->db;. assert(
16982 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
16983 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
16984 69 66 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c if( p->aOp==0 ||
16985 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
16986 64 20 29 7b 0a 20 20 20 20 69 66 20 28 6e 20 21 d ){. if (n !
16987 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 29 20 7b 0a = P4_KEYINFO) {.
16988 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c freeP4(db,
16989 20 6e 2c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 n, (void*)*(cha
1698a 72 2a 2a 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d r**)&zP4);. }
1698b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1698c 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3c . assert( addr<
1698d 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 p->nOp );. if(
1698e 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 addr<0 ){. ad
1698f 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b dr = p->nOp - 1;
16990 0a 20 20 20 20 69 66 28 20 61 64 64 72 3c 30 20 . if( addr<0
16991 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 ) return;. }.
16992 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 pOp = &p->aOp[ad
16993 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 62 dr];. freeP4(db
16994 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 , pOp->p4type, p
16995 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 Op->p4.p);. pOp
16996 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 ->p4.p = 0;. if
16997 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b ( n==P4_INT32 ){
16998 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 . /* Note: th
16999 69 73 20 63 61 73 74 20 69 73 20 73 61 66 65 2c is cast is safe,
1699a 20 62 65 63 61 75 73 65 20 74 68 65 20 6f 72 69 because the ori
1699b 67 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 gin data point w
1699c 61 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a as an int. **
1699d 20 74 68 61 74 20 77 61 73 20 63 61 73 74 20 74 that was cast t
1699e 6f 20 61 20 28 63 6f 6e 73 74 20 63 68 61 72 20 o a (const char
1699f 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e *). */. pOp->
169a0 70 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 p4.i = SQLITE_PT
169a1 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 R_TO_INT(zP4);.
169a2 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d pOp->p4type =
169a3 20 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 n;. }else if(
169a4 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f zP4==0 ){. pO
169a5 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 p->p4.p = 0;.
169a6 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
169a7 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 4_NOTUSED;. }el
169a8 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 se if( n==P4_KEY
169a9 49 4e 46 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 INFO ){. KeyI
169aa 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 nfo *pKeyInfo;.
169ab 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e int nField, n
169ac 42 79 74 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c Byte;.. nFiel
169ad 64 20 3d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a d = ((KeyInfo*)z
169ae 50 34 29 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 P4)->nField;.
169af 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 nByte = sizeof(
169b0 2a 70 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 *pKeyInfo) + (nF
169b1 69 65 6c 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 ield-1)*sizeof(p
169b2 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 KeyInfo->aColl[0
169b3 5d 29 20 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 ]) + nField;.
169b4 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 pKeyInfo = sqli
169b5 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 te3Malloc( nByte
169b6 20 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e );. pOp->p4.
169b7 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 pKeyInfo = pKeyI
169b8 6e 66 6f 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 nfo;. if( pKe
169b9 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 75 yInfo ){. u
169ba 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20 8 *aSortOrder;.
169bb 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 memcpy(pKey
169bc 49 6e 66 6f 2c 20 7a 50 34 2c 20 6e 42 79 74 65 Info, zP4, nByte
169bd 29 3b 0a 20 20 20 20 20 20 61 53 6f 72 74 4f 72 );. aSortOr
169be 64 65 72 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e der = pKeyInfo->
169bf 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 aSortOrder;.
169c0 20 20 69 66 28 20 61 53 6f 72 74 4f 72 64 65 72 if( aSortOrder
169c1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 ){. pKey
169c2 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 Info->aSortOrder
169c3 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
169c4 72 2a 29 26 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 r*)&pKeyInfo->aC
169c5 6f 6c 6c 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 oll[nField];.
169c6 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 memcpy(pKey
169c7 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 Info->aSortOrder
169c8 2c 20 61 53 6f 72 74 4f 72 64 65 72 2c 20 6e 46 , aSortOrder, nF
169c9 69 65 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ield);. }.
169ca 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 pOp->p4type
169cb 20 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 = P4_KEYINFO;.
169cc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
169cd 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->db->mallocFai
169ce 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 led = 1;. p
169cf 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f Op->p4type = P4_
169d0 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20 NOTUSED;. }.
169d1 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 }else if( n==P4
169d2 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
169d3 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e ){. pOp->p4.
169d4 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a p = (void*)zP4;.
169d5 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
169d6 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 = P4_KEYINFO;.
169d7 7d 65 6c 73 65 20 69 66 28 20 6e 3c 30 20 29 7b }else if( n<0 ){
169d8 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d . pOp->p4.p =
169d9 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 (void*)zP4;.
169da 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 6e pOp->p4type = n
169db 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
169dc 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20 73 74 f( n==0 ) n = st
169dd 72 6c 65 6e 28 7a 50 34 29 3b 0a 20 20 20 20 70 rlen(zP4);. p
169de 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 Op->p4.z = sqlit
169df 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 e3DbStrNDup(p->d
169e0 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20 b, zP4, n);.
169e1 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
169e2 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a _DYNAMIC;. }.}.
169e3 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
169e4 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
169e5 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 comment on the
169e6 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
169e7 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74 y coded instruct
169e8 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65 ion. Or.** inse
169e9 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61 rt a No-op and a
169ea 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 dd the comment t
169eb 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74 72 o that new instr
169ec 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a uction. This.**
169ed 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20 makes the code
169ee 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20 64 easier to read d
169ef 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67 2e uring debugging.
169f0 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 68 None of this h
169f1 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70 appens.** in a p
169f2 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e roduction build.
169f3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
169f4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
169f5 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 dbeComment(Vdbe
169f6 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *p, const char *
169f7 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
169f8 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 61 va_list ap;. a
169f9 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 ssert( p->nOp>0
169fa 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a || p->aOp==0 );.
169fb 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 assert( p->aOp
169fc 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d ==0 || p->aOp[p-
169fd 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 >nOp-1].zComment
169fe 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 ==0 || p->db->ma
169ff 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
16a00 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 if( p->nOp ){.
16a01 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 char **pz = &p
16a02 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e ->aOp[p->nOp-1].
16a03 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 zComment;. va
16a04 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
16a05 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 at);. sqlite3
16a06 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 DbFree(p->db, *p
16a07 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 z);. *pz = sq
16a08 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d lite3VMPrintf(p-
16a09 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 >db, zFormat, ap
16a0a 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 );. va_end(ap
16a0b 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f );. }.}.SQLITE_
16a0c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
16a0d 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ite3VdbeNoopComm
16a0e 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e ent(Vdbe *p, con
16a0f 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
16a10 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 , ...){. va_lis
16a11 74 20 61 70 3b 0a 20 20 73 71 6c 69 74 65 33 56 t ap;. sqlite3V
16a12 64 62 65 41 64 64 4f 70 30 28 70 2c 20 4f 50 5f dbeAddOp0(p, OP_
16a13 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65 72 74 28 Noop);. assert(
16a14 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e p->nOp>0 || p->
16a15 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 aOp==0 );. asse
16a16 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c rt( p->aOp==0 ||
16a17 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 p->aOp[p->nOp-1
16a18 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c ].zComment==0 ||
16a19 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 p->db->mallocFa
16a1a 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d iled );. if( p-
16a1b 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 72 >nOp ){. char
16a1c 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b **pz = &p->aOp[
16a1d 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 p->nOp-1].zComme
16a1e 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 74 nt;. va_start
16a1f 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
16a20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
16a21 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 (p->db, *pz);.
16a22 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33 56 *pz = sqlite3V
16a23 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a MPrintf(p->db, z
16a24 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 Format, ap);.
16a25 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7d va_end(ap);. }
16a26 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44 .}.#endif /* ND
16a27 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 EBUG */../*.** R
16a28 65 74 75 72 6e 20 74 68 65 20 6f 70 63 6f 64 65 eturn the opcode
16a29 20 66 6f 72 20 61 20 67 69 76 65 6e 20 61 64 64 for a given add
16a2a 72 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ress..*/.SQLITE_
16a2b 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a PRIVATE VdbeOp *
16a2c 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 sqlite3VdbeGetOp
16a2d 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
16a2e 64 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 dr){. assert( p
16a2f 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
16a30 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 GIC_INIT );. as
16a31 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26 sert( (addr>=0 &
16a32 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c & addr<p->nOp) |
16a33 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 | p->db->mallocF
16a34 61 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 ailed );. retur
16a35 6e 20 28 28 61 64 64 72 3e 3d 30 20 26 26 20 61 n ((addr>=0 && a
16a36 64 64 72 3c 70 2d 3e 6e 4f 70 29 3f 28 26 70 2d ddr<p->nOp)?(&p-
16a37 3e 61 4f 70 5b 61 64 64 72 5d 29 3a 30 29 3b 0a >aOp[addr]):0);.
16a38 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 }..#if !defined(
16a39 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
16a3a 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 AIN) || !defined
16a3b 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 20 20 (NDEBUG) \.
16a3c 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f || defined(VDBE_
16a3d 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 PROFILE) || defi
16a3e 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
16a3f 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 )./*.** Compute
16a40 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 a string that de
16a41 73 63 72 69 62 65 73 20 74 68 65 20 50 34 20 70 scribes the P4 p
16a42 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 arameter for an
16a43 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a opcode..** Use z
16a44 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71 Temp for any req
16a45 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20 uired temporary
16a46 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f buffer space..*/
16a47 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 69 .static char *di
16a48 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c splayP4(Op *pOp,
16a49 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e char *zTemp, in
16a4a 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 t nTemp){. char
16a4b 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 *zP4 = zTemp;.
16a4c 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d assert( nTemp>=
16a4d 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 20 );. switch(
16a4e 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20 pOp->p4type ){.
16a4f 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
16a50 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 20 63 FO_STATIC:. c
16a51 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 ase P4_KEYINFO:
16a52 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a {. int i, j
16a53 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 ;. KeyInfo
16a54 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d *pKeyInfo = pOp-
16a55 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 >p4.pKeyInfo;.
16a56 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
16a57 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
16a58 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c p, "keyinfo(%d",
16a59 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c pKeyInfo->nFiel
16a5a 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 74 d);. i = st
16a5b 72 6c 65 6e 28 7a 54 65 6d 70 29 3b 0a 20 20 20 rlen(zTemp);.
16a5c 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4b for(j=0; j<pK
16a5d 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 20 eyInfo->nField;
16a5e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 43 6f j++){. Co
16a5f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 llSeq *pColl = p
16a60 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a KeyInfo->aColl[j
16a61 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ];. if( p
16a62 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 Coll ){.
16a63 20 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e int n = strlen
16a64 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a (pColl->zName);.
16a65 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 2b if( i+
16a66 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 n>nTemp-6 ){.
16a67 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
16a68 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 &zTemp[i],",..."
16a69 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ,4);.
16a6a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
16a6b 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54 }. zT
16a6c 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a emp[i++] = ',';.
16a6d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4b if( pK
16a6e 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 eyInfo->aSortOrd
16a6f 65 72 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e er && pKeyInfo->
16a70 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b aSortOrder[j] ){
16a71 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 65 . zTe
16a72 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 mp[i++] = '-';.
16a73 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
16a74 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 memcpy(&zTe
16a75 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e mp[i], pColl->zN
16a76 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 ame,n+1);.
16a77 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 i += n;.
16a78 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b }else if( i+
16a79 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 4<nTemp-6 ){.
16a7a 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a memcpy(&z
16a7b 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 Temp[i],",nil",4
16a7c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b );. i +
16a7d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 4;. }.
16a7e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 }. zTe
16a7f 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 mp[i++] = ')';.
16a80 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 zTemp[i] =
16a81 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 0;. assert(
16a82 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 i<nTemp );.
16a83 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
16a84 20 20 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 case P4_COLLS
16a85 45 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c EQ: {. Coll
16a86 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 Seq *pColl = pOp
16a87 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 ->p4.pColl;.
16a88 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
16a89 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c tf(nTemp, zTemp,
16a8a 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 "collseq(%.20s)
16a8b 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 ", pColl->zName)
16a8c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
16a8d 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 }. case P4
16a8e 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 _FUNCDEF: {.
16a8f 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 FuncDef *pDef
16a90 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b = pOp->p4.pFunc;
16a91 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
16a92 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
16a93 54 65 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20 Temp, "%s(%d)",
16a94 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 pDef->zName, pDe
16a95 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 f->nArg);.
16a96 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
16a97 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 case P4_INT64:
16a98 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
16a99 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
16a9a 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a zTemp, "%lld", *
16a9b 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 pOp->p4.pI64);.
16a9c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
16a9d 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e }. case P4_IN
16a9e 54 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c T32: {. sql
16a9f 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
16aa0 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 emp, zTemp, "%d"
16aa1 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 , pOp->p4.i);.
16aa2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
16aa3 0a 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 . case P4_REA
16aa4 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 L: {. sqlit
16aa5 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d e3_snprintf(nTem
16aa6 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 p, zTemp, "%.16g
16aa7 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 ", *pOp->p4.pRea
16aa8 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b l);. break;
16aa9 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
16aaa 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 P4_MEM: {.
16aab 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d Mem *pMem = pOp-
16aac 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 >p4.pMem;.
16aad 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
16aae 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
16aaf 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ==0 );. if(
16ab0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
16ab1 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 EM_Str ){.
16ab2 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b zP4 = pMem->z;
16ab3 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
16ab4 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
16ab5 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 EM_Int ){.
16ab6 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
16ab7 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c tf(nTemp, zTemp,
16ab8 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 "%lld", pMem->u
16ab9 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 .i);. }else
16aba 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
16abb 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 & MEM_Real ){.
16abc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
16abd 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
16abe 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 Temp, "%.16g", p
16abf 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d Mem->r);. }
16ac0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
16ac1 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
16ac2 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
16ac3 41 42 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34 ABLE. case P4
16ac4 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 _VTAB: {. s
16ac5 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
16ac6 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 ab = pOp->p4.pVt
16ac7 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ab;. sqlite
16ac8 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
16ac9 2c 20 7a 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 , zTemp, "vtab:%
16aca 70 3a 25 70 22 2c 20 70 56 74 61 62 2c 20 70 56 p:%p", pVtab, pV
16acb 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 tab->pModule);.
16acc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
16acd 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 }.#endif. cas
16ace 65 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 20 7b e P4_INTARRAY: {
16acf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
16ad0 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
16ad1 54 65 6d 70 2c 20 22 69 6e 74 61 72 72 61 79 22 Temp, "intarray"
16ad2 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
16ad3 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
16ad4 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50 34 20 3d t: {. zP4 =
16ad5 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 pOp->p4.z;.
16ad6 20 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a if( zP4==0 ){.
16ad7 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 7a 54 zP4 = zT
16ad8 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 emp;. zTe
16ad9 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20 mp[0] = 0;.
16ada 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 }. }. }. a
16adb 73 73 65 72 74 28 20 7a 50 34 21 3d 30 20 29 3b ssert( zP4!=0 );
16adc 0a 20 20 72 65 74 75 72 6e 20 7a 50 34 3b 0a 7d . return zP4;.}
16add 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
16ade 65 63 6c 61 72 65 20 74 6f 20 74 68 65 20 56 64 eclare to the Vd
16adf 62 65 20 74 68 61 74 20 74 68 65 20 42 54 72 65 be that the BTre
16ae0 65 20 6f 62 6a 65 63 74 20 61 74 20 64 62 2d 3e e object at db->
16ae1 61 44 62 5b 69 5d 20 69 73 20 75 73 65 64 2e 0a aDb[i] is used..
16ae2 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 **.*/.SQLITE_PRI
16ae3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
16ae4 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56 3VdbeUsesBtree(V
16ae5 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a dbe *p, int i){.
16ae6 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 int mask;. as
16ae7 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
16ae8 70 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 p->db->nDb );.
16ae9 61 73 73 65 72 74 28 20 69 3c 73 69 7a 65 6f 66 assert( i<sizeof
16aea 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 29 2a 38 (p->btreeMask)*8
16aeb 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 31 3c 3c );. mask = 1<<
16aec 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74 72 i;. if( (p->btr
16aed 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d eeMask & mask)==
16aee 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65 0 ){. p->btre
16aef 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 eMask |= mask;.
16af0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d sqlite3BtreeM
16af1 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 utexArrayInsert(
16af2 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64 &p->aMutex, p->d
16af3 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a b->aDb[i].pBt);.
16af4 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69 }.}...#if defi
16af5 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 ned(VDBE_PROFILE
16af6 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
16af7 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a ITE_DEBUG)./*.**
16af8 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 Print a single
16af9 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f opcode. This ro
16afa 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f utine is used fo
16afb 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 r debugging only
16afc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16afd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
16afe 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 VdbePrintOp(FILE
16aff 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 *pOut, int pc,
16b00 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 Op *pOp){. char
16b01 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 *zP4;. char zP
16b02 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 tr[50];. static
16b03 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
16b04 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31 rmat1 = "%4d %-1
16b05 33 73 20 25 34 64 20 25 34 64 20 25 34 64 20 25 3s %4d %4d %4d %
16b06 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a -4s %.2X %s\n";.
16b07 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 if( pOut==0 )
16b08 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 pOut = stdout;.
16b09 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34 zP4 = displayP4
16b0a 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 (pOp, zPtr, size
16b0b 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 of(zPtr));. fpr
16b0c 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d intf(pOut, zForm
16b0d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 at1, pc, .
16b0e 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d sqlite3OpcodeNam
16b0f 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 e(pOp->opcode),
16b10 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 pOp->p1, pOp->p2
16b11 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 , pOp->p3, zP4,
16b12 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 pOp->p5,.#ifdef
16b13 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
16b14 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 pOp->zComment
16b15 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 ? pOp->zComment
16b16 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 : "".#else.
16b17 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b "".#endif. );
16b18 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b . fflush(pOut);
16b19 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
16b1a 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 Release an arra
16b1b 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 y of N Mem eleme
16b1c 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f nts.*/.static vo
16b1d 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 id releaseMemArr
16b1e 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e ay(Mem *p, int N
16b1f 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20 ){. if( p && N
16b20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 ){. Mem *pEnd
16b21 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 ;. sqlite3 *d
16b22 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 69 b = p->db;. i
16b23 6e 74 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 nt malloc_failed
16b24 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 = db->mallocFai
16b25 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 6e led;. for(pEn
16b26 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b d=&p[N]; p<pEnd;
16b27 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 p++){. ass
16b28 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 ert( (&p[1])==pE
16b29 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 nd || p[0].db==p
16b2a 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20 [1].db );..
16b2b 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69 /* This block i
16b2c 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 s really an inli
16b2d 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 ned version of s
16b2e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
16b2f 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 ease(). **
16b30 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e that takes advan
16b31 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63 74 tage of the fact
16b32 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 that the memory
16b33 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a cell value is .
16b34 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73 ** being s
16b35 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72 et to NULL after
16b36 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64 releasing any d
16b37 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 ynamic resources
16b38 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
16b39 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63 ** The justific
16b3a 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 ation for duplic
16b3b 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68 ating code is th
16b3c 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 at according to
16b3d 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72 . ** callgr
16b3e 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65 73 ind, this causes
16b3f 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 74 20 a certain test
16b40 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 65 20 case to hit the
16b41 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a CPU 4.7 . *
16b42 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28 * percent less (
16b43 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76 x86 linux, gcc v
16b44 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f ersion 4.1.2, -O
16b45 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20 6) than if .
16b46 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52 ** sqlite3MemR
16b47 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 63 61 elease() were ca
16b48 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20 lled from here.
16b49 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a With -O2, this j
16b4a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f umps. ** to
16b4b 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68 6.6 percent. Th
16b4c 65 20 74 65 73 74 20 63 61 73 65 20 69 73 20 69 e test case is i
16b4d 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f nserting 1000 ro
16b4e 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 ws into a table
16b4f 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e . ** with n
16b50 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20 o indexes using
16b51 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65 a single prepare
16b52 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 d INSERT stateme
16b53 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20 nt, bind() .
16b54 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29 ** and reset()
16b55 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 67 72 . Inserts are gr
16b56 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61 ouped into a tra
16b57 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 nsaction..
16b58 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e */. if( p->
16b59 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d flags&(MEM_Agg|M
16b5a 45 4d 5f 44 79 6e 29 20 29 7b 0a 20 20 20 20 20 EM_Dyn) ){.
16b5b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
16b5c 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 20 20 mRelease(p);.
16b5d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e }else if( p->
16b5e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 zMalloc ){.
16b5f 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
16b60 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 (db, p->zMalloc)
16b61 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 4d 61 ;. p->zMa
16b62 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 lloc = 0;.
16b63 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66 6c 61 67 }.. p->flag
16b64 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
16b65 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c }. db->mall
16b66 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 6c 6c 6f ocFailed = mallo
16b67 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 0a 7d 0a c_failed;. }.}.
16b68 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
16b69 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e NABLE_MEMORY_MAN
16b6a 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 AGEMENT.SQLITE_P
16b6b 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16b6c 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 e3VdbeReleaseBuf
16b6d 66 65 72 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 fers(Vdbe *p){.
16b6e 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6e int ii;. int n
16b6f 46 72 65 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 Free = 0;. asse
16b70 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
16b71 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
16b72 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 69 tex) );. for(ii
16b73 3d 31 3b 20 69 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b =1; ii<=p->nMem;
16b74 20 69 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 20 ii++){. Mem
16b75 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d *pMem = &p->aMem
16b76 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 4d [ii];. if( pM
16b77 65 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 66 em->z && pMem->f
16b78 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 29 7b 0a lags&MEM_Dyn ){.
16b79 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
16b7a 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 Mem->xDel );.
16b7b 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 nFree += sqli
16b7c 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
16b7d 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e pMem->db, pMem->
16b7e 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 z);. sqlite
16b7f 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
16b80 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d pMem);. }. }
16b81 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b . return nFree;
16b82 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
16b83 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 ef SQLITE_OMIT_E
16b84 58 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 XPLAIN./*.** Giv
16b85 65 20 61 20 6c 69 73 74 69 6e 67 20 6f 66 20 74 e a listing of t
16b86 68 65 20 70 72 6f 67 72 61 6d 20 69 6e 20 74 68 he program in th
16b87 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e e virtual machin
16b88 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 e..**.** The int
16b89 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 erface is the sa
16b8a 6d 65 20 61 73 20 73 71 6c 69 74 65 33 56 64 62 me as sqlite3Vdb
16b8b 65 45 78 65 63 28 29 2e 20 20 42 75 74 20 69 6e eExec(). But in
16b8c 73 74 65 61 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e stead of.** runn
16b8d 69 6e 67 20 74 68 65 20 63 6f 64 65 2c 20 69 74 ing the code, it
16b8e 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 61 6c invokes the cal
16b8f 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 lback once for e
16b90 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ach instruction.
16b91 0a 2a 2a 20 54 68 69 73 20 66 65 61 74 75 72 65 .** This feature
16b92 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c is used to impl
16b93 65 6d 65 6e 74 20 22 45 58 50 4c 41 49 4e 22 2e ement "EXPLAIN".
16b94 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 .**.** When p->e
16b95 78 70 6c 61 69 6e 3d 3d 31 2c 20 65 61 63 68 20 xplain==1, each
16b96 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6c instruction is l
16b97 69 73 74 65 64 2e 20 20 57 68 65 6e 0a 2a 2a 20 isted. When.**
16b98 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f p->explain==2, o
16b99 6e 6c 79 20 4f 50 5f 45 78 70 6c 61 69 6e 20 69 nly OP_Explain i
16b9a 6e 73 74 72 75 63 74 69 6f 6e 73 20 61 72 65 20 nstructions are
16b9b 6c 69 73 74 65 64 20 61 6e 64 20 74 68 65 73 65 listed and these
16b9c 0a 2a 2a 20 61 72 65 20 73 68 6f 77 6e 20 69 6e .** are shown in
16b9d 20 61 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 a different for
16b9e 6d 61 74 2e 20 20 70 2d 3e 65 78 70 6c 61 69 6e mat. p->explain
16b9f 3d 3d 32 20 69 73 20 75 73 65 64 20 74 6f 20 69 ==2 is used to i
16ba0 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c mplement.** EXPL
16ba1 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 2e 0a AIN QUERY PLAN..
16ba2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16ba3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
16ba4 65 4c 69 73 74 28 0a 20 20 56 64 62 65 20 2a 70 eList(. Vdbe *p
16ba5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ba6 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a /* The VDBE *
16ba7 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a /.){. sqlite3 *
16ba8 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e db = p->db;. in
16ba9 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 t i;. int rc =
16baa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d SQLITE_OK;. Mem
16bab 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 *pMem = p->pRes
16bac 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 ultSet = &p->aMe
16bad 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 m[1];.. assert(
16bae 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 p->explain );.
16baf 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 if( p->magic!=V
16bb0 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 20 DBE_MAGIC_RUN )
16bb1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
16bb2 53 55 53 45 3b 0a 20 20 61 73 73 65 72 74 28 20 SUSE;. assert(
16bb3 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 db->magic==SQLIT
16bb4 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a E_MAGIC_BUSY );.
16bb5 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d assert( p->rc=
16bb6 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d =SQLITE_OK || p-
16bb7 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 >rc==SQLITE_BUSY
16bb8 20 29 3b 0a 0a 20 20 2f 2a 20 45 76 65 6e 20 74 );.. /* Even t
16bb9 68 6f 75 67 68 20 74 68 69 73 20 6f 70 63 6f 64 hough this opcod
16bba 65 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 64 e does not use d
16bbb 79 6e 61 6d 69 63 20 73 74 72 69 6e 67 73 20 66 ynamic strings f
16bbc 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72 65 73 75 or. ** the resu
16bbd 6c 74 2c 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d lt, result colum
16bbe 6e 73 20 6d 61 79 20 62 65 63 6f 6d 65 20 64 79 ns may become dy
16bbf 6e 61 6d 69 63 20 69 66 20 74 68 65 20 75 73 65 namic if the use
16bc0 72 20 63 61 6c 6c 73 0a 20 20 2a 2a 20 73 71 6c r calls. ** sql
16bc1 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
16bc2 31 36 28 29 2c 20 63 61 75 73 69 6e 67 20 61 20 16(), causing a
16bc3 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f 20 55 translation to U
16bc4 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 2e 0a TF-16 encoding..
16bc5 20 20 2a 2f 0a 20 20 72 65 6c 65 61 73 65 4d 65 */. releaseMe
16bc6 6d 41 72 72 61 79 28 70 4d 65 6d 2c 20 70 2d 3e mArray(pMem, p->
16bc7 6e 4d 65 6d 29 3b 0a 0a 20 20 64 6f 7b 0a 20 20 nMem);.. do{.
16bc8 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 i = p->pc++;.
16bc9 20 7d 77 68 69 6c 65 28 20 69 3c 70 2d 3e 6e 4f }while( i<p->nO
16bca 70 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d p && p->explain=
16bcb 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e =2 && p->aOp[i].
16bcc 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 opcode!=OP_Expla
16bcd 69 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 in );. if( i>=p
16bce 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e ->nOp ){. p->
16bcf 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
16bd0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
16bd1 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 DONE;. }else if
16bd2 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 ( db->u1.isInter
16bd3 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20 70 2d rupted ){. p-
16bd4 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 >rc = SQLITE_INT
16bd5 45 52 52 55 50 54 3b 0a 20 20 20 20 72 63 20 3d ERRUPT;. rc =
16bd6 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
16bd7 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
16bd8 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
16bd9 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 db, "%s", sqlit
16bda 65 33 45 72 72 53 74 72 28 70 2d 3e 72 63 29 29 e3ErrStr(p->rc))
16bdb 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 ;. }else{. c
16bdc 68 61 72 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a har *z;. Op *
16bdd 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d pOp = &p->aOp[i]
16bde 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 ;. if( p->exp
16bdf 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 lain==1 ){.
16be0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
16be1 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d EM_Int;. pM
16be2 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
16be3 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 E_INTEGER;.
16be4 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 3b 20 pMem->u.i = i;
16be5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16be6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16be7 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 * Program counte
16be8 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2b r */. pMem+
16be9 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d 65 6d +;. . pMem
16bea 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 ->flags = MEM_St
16beb 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d atic|MEM_Str|MEM
16bec 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 _Term;. pMe
16bed 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 m->z = (char*)sq
16bee 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 lite3OpcodeName(
16bef 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 20 2f pOp->opcode); /
16bf0 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 * Opcode */.
16bf1 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
16bf2 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d z!=0 );. pM
16bf3 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70 em->n = strlen(p
16bf4 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 Mem->z);. p
16bf5 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
16bf6 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 TE_TEXT;. p
16bf7 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
16bf8 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d E_UTF8;. pM
16bf9 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 em++;. }..
16bfa 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
16bfb 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d EM_Int;. pMem
16bfc 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b ->u.i = pOp->p1;
16bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bfe 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20 /* P1
16bff 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 */. pMem->typ
16c00 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 e = SQLITE_INTEG
16c01 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a ER;. pMem++;.
16c02 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
16c03 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
16c04 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d pMem->u.i = pOp-
16c05 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 >p2;
16c06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16c07 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d P2 */. pMem-
16c08 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 >type = SQLITE_I
16c09 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d NTEGER;. pMem
16c0a 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e ++;.. if( p->
16c0b 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 explain==1 ){.
16c0c 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
16c0d 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 = MEM_Int;.
16c0e 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 pMem->u.i = pOp
16c0f 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 ->p3;
16c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16c11 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 70 4d * P3 */. pM
16c12 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
16c13 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 E_INTEGER;.
16c14 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a pMem++;. }..
16c15 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
16c16 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c dbeMemGrow(pMem,
16c17 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20 20 32, 0) ){
16c18 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a 20 /* P4 */.
16c19 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c p->db->mall
16c1a 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
16c1b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16c1c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
16c1d 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
16c1e 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 MEM_Dyn|MEM_Str
16c1f 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 7a |MEM_Term;. z
16c20 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 = displayP4(pOp
16c21 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29 3b 0a , pMem->z, 32);.
16c22 20 20 20 20 69 66 28 20 7a 21 3d 70 4d 65 6d 2d if( z!=pMem-
16c23 3e 7a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 >z ){. sqli
16c24 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
16c25 28 70 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20 53 51 (pMem, z, -1, SQ
16c26 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 LITE_UTF8, 0);.
16c27 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
16c28 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 assert( pMem->z!
16c29 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d =0 );. pMem
16c2a 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70 4d 65 ->n = strlen(pMe
16c2b 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 m->z);. pMe
16c2c 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f m->enc = SQLITE_
16c2d 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 UTF8;. }.
16c2e 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
16c2f 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d ITE_TEXT;. pM
16c30 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 em++;.. if( p
16c31 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a ->explain==1 ){.
16c32 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
16c33 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
16c34 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20 m, 4, 0) ){.
16c35 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f p->db->mallo
16c36 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
16c37 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
16c38 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
16c39 7d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c }. pMem->fl
16c3a 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 ags = MEM_Dyn|ME
16c3b 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
16c3c 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 pMem->n =
16c3d 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 2;. sqlite3
16c3e 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d 65 _snprintf(3, pMe
16c3f 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70 4f m->z, "%.2x", pO
16c40 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35 20 p->p5); /* P5
16c41 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 */. pMem->t
16c42 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 ype = SQLITE_TEX
16c43 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 T;. pMem->e
16c44 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 nc = SQLITE_UTF8
16c45 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a ;. pMem++;.
16c46 20 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 .#ifdef SQLITE
16c47 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 28 _DEBUG. if(
16c48 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 29 pOp->zComment )
16c49 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e {. pMem->
16c4a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c flags = MEM_Str|
16c4b 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 MEM_Term;.
16c4c 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 2d pMem->z = pOp-
16c4d 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 20 >zComment;.
16c4e 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72 pMem->n = str
16c4f 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 len(pMem->z);.
16c50 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 pMem->enc
16c51 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 = SQLITE_UTF8;.
16c52 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 pMem->typ
16c53 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b e = SQLITE_TEXT;
16c54 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e . }else.#en
16c55 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 dif. {.
16c56 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
16c57 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 = MEM_Null;
16c58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c59 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a /* Comment */.
16c5a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 pMem->ty
16c5b 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c pe = SQLITE_NULL
16c5c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
16c5d 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 . p->nResColu
16c5e 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 mn = 8 - 5*(p->e
16c5f 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 xplain-1);. p
16c60 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b ->rc = SQLITE_OK
16c61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
16c62 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 E_ROW;. }. ret
16c63 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
16c64 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
16c65 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 EXPLAIN */..#ifd
16c66 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
16c67 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 /*.** Print the
16c68 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65 SQL that was use
16c69 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 d to generate a
16c6a 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f VDBE program..*/
16c6b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16c6c 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
16c6d 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 PrintSql(Vdbe *p
16c6e 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 ){. int nOp = p
16c6f 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 ->nOp;. VdbeOp
16c70 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c *pOp;. if( nOp<
16c71 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 1 ) return;. pO
16c72 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a p = &p->aOp[0];.
16c73 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
16c74 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 e==OP_Trace && p
16c75 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 Op->p4.z!=0 ){.
16c76 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
16c77 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 = pOp->p4.z;.
16c78 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 while( isspace
16c79 28 2a 28 75 38 2a 29 7a 29 20 29 20 7a 2b 2b 3b (*(u8*)z) ) z++;
16c7a 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 51 4c . printf("SQL
16c7b 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 : [%s]\n", z);.
16c7c 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 }.}.#endif..#if
16c7d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
16c7e 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26 20 _OMIT_TRACE) &&
16c7f 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 defined(SQLITE_E
16c80 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a 2f NABLE_IOTRACE)./
16c81 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 49 4f *.** Print an IO
16c82 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 73 68 TRACE message sh
16c83 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 65 6e owing SQL conten
16c84 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
16c85 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
16c86 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 3VdbeIOTraceSql(
16c87 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
16c88 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 nOp = p->nOp;.
16c89 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 VdbeOp *pOp;. i
16c8a 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 f( sqlite3IoTrac
16c8b 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 e==0 ) return;.
16c8c 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 74 if( nOp<1 ) ret
16c8d 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d urn;. pOp = &p-
16c8e 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 70 >aOp[0];. if( p
16c8f 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 Op->opcode==OP_T
16c90 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e race && pOp->p4.
16c91 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 z!=0 ){. int
16c92 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 20 7a i, j;. char z
16c93 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 [1000];. sqli
16c94 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
16c95 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22 2c eof(z), z, "%s",
16c96 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 pOp->p4.z);.
16c97 20 66 6f 72 28 69 3d 30 3b 20 69 73 73 70 61 63 for(i=0; isspac
16c98 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 e((unsigned char
16c99 29 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 )z[i]); i++){}.
16c9a 20 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d for(j=0; z[i]
16c9b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
16c9c 28 20 69 73 73 70 61 63 65 28 28 75 6e 73 69 67 ( isspace((unsig
16c9d 6e 65 64 20 63 68 61 72 29 7a 5b 69 5d 29 20 29 ned char)z[i]) )
16c9e 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b {. if( z[
16c9f 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 20 20 i-1]!=' ' ){.
16ca0 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 z[j++] =
16ca1 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ' ';. }.
16ca2 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
16ca3 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 z[j++] = z[i
16ca4 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d ];. }. }
16ca5 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 . z[j] = 0;.
16ca6 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 sqlite3IoTrac
16ca7 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 7a 29 e("SQL %s\n", z)
16ca8 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f ;. }.}.#endif /
16ca9 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 * !SQLITE_OMIT_T
16caa 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 5f 45 RACE && SQLITE_E
16cab 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 2a 2f NABLE_IOTRACE */
16cac 0a 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 .../*.** Prepare
16cad 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 a virtual machi
16cae 6e 65 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e ne for execution
16caf 2e 20 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 . This involves
16cb0 20 74 68 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 things such.**
16cb1 61 73 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 as allocating st
16cb2 61 63 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e ack space and in
16cb3 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 itializing the p
16cb4 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a rogram counter..
16cb5 2a 2a 20 41 66 74 65 72 20 74 68 65 20 56 44 42 ** After the VDB
16cb6 45 20 68 61 73 20 62 65 20 70 72 65 70 70 65 64 E has be prepped
16cb7 2c 20 69 74 20 63 61 6e 20 62 65 20 65 78 65 63 , it can be exec
16cb8 75 74 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d uted by one or m
16cb9 6f 72 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 ore.** calls to
16cba 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
16cbb 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ). .**.** This
16cbc 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 is the only way
16cbd 74 6f 20 6d 6f 76 65 20 61 20 56 44 42 45 20 66 to move a VDBE f
16cbe 72 6f 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 rom VDBE_MAGIC_I
16cbf 4e 49 54 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d NIT to.** VDBE_M
16cc0 41 47 49 43 5f 52 55 4e 2e 0a 2a 2f 0a 53 51 4c AGIC_RUN..*/.SQL
16cc1 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
16cc2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
16cc3 52 65 61 64 79 28 0a 20 20 56 64 62 65 20 2a 70 Ready(. Vdbe *p
16cc4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
16cc5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
16cc6 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 DBE */. int nVa
16cc7 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
16cc8 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
16cc9 72 20 6f 66 20 27 3f 27 20 73 65 65 20 69 6e 20 r of '?' see in
16cca 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
16ccb 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c t */. int nMem,
16ccc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ccd 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
16cce 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 of memory cells
16ccf 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 to allocate */.
16cd0 20 69 6e 74 20 6e 43 75 72 73 6f 72 2c 20 20 20 int nCursor,
16cd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16cd2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 /* Number of cur
16cd3 73 6f 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 sors to allocate
16cd4 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 78 70 6c */. int isExpl
16cd5 61 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ain
16cd6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
16cd7 74 68 65 20 45 58 50 4c 41 49 4e 20 6b 65 79 77 the EXPLAIN keyw
16cd8 6f 72 64 73 20 69 73 20 70 72 65 73 65 6e 74 20 ords is present
16cd9 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 */.){. int n;.
16cda 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
16cdb 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 ->db;.. assert(
16cdc 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 p!=0 );. asser
16cdd 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 t( p->magic==VDB
16cde 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a E_MAGIC_INIT );.
16cdf 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 . /* There shou
16ce0 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f ld be at least o
16ce1 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a ne opcode.. */.
16ce2 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 assert( p->nOp
16ce3 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 >0 );.. /* Set
16ce4 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42 the magic to VDB
16ce5 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e E_MAGIC_RUN soon
16ce6 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c er rather than l
16ce7 61 74 65 72 2e 20 54 68 69 73 0a 20 20 20 2a 20 ater. This. *
16ce8 69 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63 is because the c
16ce9 61 6c 6c 20 74 6f 20 72 65 73 69 7a 65 4f 70 41 all to resizeOpA
16cea 72 72 61 79 28 29 20 62 65 6c 6f 77 20 6d 61 79 rray() below may
16ceb 20 73 68 72 69 6e 6b 20 74 68 65 0a 20 20 20 2a shrink the. *
16cec 20 70 2d 3e 61 4f 70 5b 5d 20 61 72 72 61 79 20 p->aOp[] array
16ced 74 6f 20 73 61 76 65 20 6d 65 6d 6f 72 79 20 69 to save memory i
16cee 66 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 69 6e f called when in
16cef 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 VDBE_MAGIC_RUN
16cf0 0a 20 20 20 2a 20 73 74 61 74 65 2e 0a 20 20 20 . * state..
16cf1 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 */. p->magic =
16cf2 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a VDBE_MAGIC_RUN;.
16cf3 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 . /* For each c
16cf4 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 2c 20 ursor required,
16cf5 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 also allocate a
16cf6 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d 65 6d memory cell. Mem
16cf7 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 28 ory. ** cells (
16cf8 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29 2e nMem+1-nCursor).
16cf9 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76 65 .nMem, inclusive
16cfa 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 , will never be
16cfb 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 65 used by. ** the
16cfc 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 49 vdbe program. I
16cfd 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 65 20 nstead they are
16cfe 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 used to allocate
16cff 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 space for. **
16d00 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75 72 VdbeCursor/BtCur
16d01 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e 20 sor structures.
16d02 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f The blob of memo
16d03 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ry associated wi
16d04 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 th . ** cursor
16d05 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 0 is stored in m
16d06 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d 2e emory cell nMem.
16d07 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e 4d Memory cell (nM
16d08 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f 72 65 em-1). ** store
16d09 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 s the blob of me
16d0a 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 mory associated
16d0b 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c 20 65 with cursor 1, e
16d0c 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 65 tc.. **. ** Se
16d0d 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 74 65 e also: allocate
16d0e 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 Cursor().. */.
16d0f 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 6f 72 nMem += nCursor
16d10 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c ;.. /*. ** All
16d11 6f 63 61 74 69 6f 6e 20 73 70 61 63 65 20 66 6f ocation space fo
16d12 72 20 72 65 67 69 73 74 65 72 73 2e 0a 20 20 2a r registers.. *
16d13 2f 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 3d /. if( p->aMem=
16d14 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 =0 ){. int nA
16d15 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 rg; /* Max
16d16 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 imum number of a
16d17 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61 20 rgs passed to a
16d18 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 2a user function. *
16d19 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 56 /. resolveP2V
16d1a 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 3b alues(p, &nArg);
16d1b 0a 20 20 20 20 2f 2a 72 65 73 69 7a 65 4f 70 41 . /*resizeOpA
16d1c 72 72 61 79 28 70 2c 20 70 2d 3e 6e 4f 70 29 3b rray(p, p->nOp);
16d1d 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e */. assert( n
16d1e 56 61 72 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 Var>=0 );. if
16d1f 28 20 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e ( isExplain && n
16d20 4d 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 Mem<10 ){.
16d21 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d nMem = 10;. }
16d22 0a 20 20 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 73 . p->aMem = s
16d23 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
16d24 72 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 6e ro(db,. n
16d25 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 20 Mem*sizeof(Mem)
16d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16d27 20 61 4d 65 6d 20 2a 2f 0a 20 20 20 20 20 20 2b aMem */. +
16d28 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d nVar*sizeof(Mem
16d29 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
16d2a 2f 2a 20 61 56 61 72 20 2a 2f 0a 20 20 20 20 20 /* aVar */.
16d2b 20 2b 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d + nArg*sizeof(M
16d2c 65 6d 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 em*)
16d2d 20 20 2f 2a 20 61 70 41 72 67 20 2a 2f 0a 20 20 /* apArg */.
16d2e 20 20 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f + nVar*sizeo
16d2f 66 28 63 68 61 72 2a 29 20 20 20 20 20 20 20 20 f(char*)
16d30 20 20 20 20 20 2f 2a 20 61 7a 56 61 72 20 2a 2f /* azVar */
16d31 0a 20 20 20 20 20 20 2b 20 6e 43 75 72 73 6f 72 . + nCursor
16d32 2a 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 *sizeof(VdbeCurs
16d33 6f 72 2a 29 2b 31 20 20 2f 2a 20 61 70 43 73 72 or*)+1 /* apCsr
16d34 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 */. );. i
16d35 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 f( !db->mallocFa
16d36 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d iled ){. p-
16d37 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20 >aMem--;
16d38 20 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 /* aMem[] g
16d39 6f 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d oes from 1..nMem
16d3a 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 */. p->nMe
16d3b 6d 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 m = nMem;
16d3c 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20 66 72 /* not fr
16d3d 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a 2f 0a om 0..nMem-1 */.
16d3e 20 20 20 20 20 20 70 2d 3e 61 56 61 72 20 3d 20 p->aVar =
16d3f 26 70 2d 3e 61 4d 65 6d 5b 6e 4d 65 6d 2b 31 5d &p->aMem[nMem+1]
16d40 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 ;. p->nVar
16d41 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 20 70 2d = nVar;. p-
16d42 3e 6f 6b 56 61 72 20 3d 20 30 3b 0a 20 20 20 20 >okVar = 0;.
16d43 20 20 70 2d 3e 61 70 41 72 67 20 3d 20 28 4d 65 p->apArg = (Me
16d44 6d 2a 2a 29 26 70 2d 3e 61 56 61 72 5b 6e 56 61 m**)&p->aVar[nVa
16d45 72 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a 56 r];. p->azV
16d46 61 72 20 3d 20 28 63 68 61 72 2a 2a 29 26 70 2d ar = (char**)&p-
16d47 3e 61 70 41 72 67 5b 6e 41 72 67 5d 3b 0a 20 20 >apArg[nArg];.
16d48 20 20 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28 p->apCsr = (
16d49 56 64 62 65 43 75 72 73 6f 72 2a 2a 29 26 70 2d VdbeCursor**)&p-
16d4a 3e 61 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20 20 >azVar[nVar];.
16d4b 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d p->nCursor =
16d4c 20 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 nCursor;.
16d4d 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b for(n=0; n<nVar;
16d4e 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 n++){. p
16d4f 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 ->aVar[n].flags
16d50 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
16d51 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 p->aVar[n].d
16d52 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a b = db;. }.
16d53 20 20 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e for(n=1; n
16d54 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 <=nMem; n++){.
16d55 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d p->aMem[n]
16d56 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c .flags = MEM_Nul
16d57 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d l;. p->aM
16d58 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 em[n].db = db;.
16d59 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
16d5a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
16d5b 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20 EBUG. for(n=1;
16d5c 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b n<p->nMem; n++){
16d5d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
16d5e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29 aMem[n].db==db )
16d5f 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
16d60 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d p->pc = -1;. p-
16d61 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b >rc = SQLITE_OK;
16d62 0a 20 20 70 2d 3e 75 6e 69 71 75 65 43 6e 74 20 . p->uniqueCnt
16d63 3d 20 30 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 = 0;. p->errorA
16d64 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 ction = OE_Abort
16d65 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c ;. p->explain |
16d66 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 = isExplain;. p
16d67 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d ->magic = VDBE_M
16d68 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e AGIC_RUN;. p->n
16d69 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d Change = 0;. p-
16d6a 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 >cacheCtr = 1;.
16d6b 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 p->minWriteFile
16d6c 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 Format = 255;.
16d6d 70 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 p->openedStateme
16d6e 6e 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 nt = 0;.#ifdef V
16d6f 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a DBE_PROFILE. {.
16d70 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 int i;. f
16d71 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 or(i=0; i<p->nOp
16d72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d ; i++){. p-
16d73 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b >aOp[i].cnt = 0;
16d74 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d . p->aOp[i]
16d75 2e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 .cycles = 0;.
16d76 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a }. }.#endif.}.
16d77 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 ./*.** Close a V
16d78 44 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 DBE cursor and r
16d79 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 elease all the r
16d7a 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 esources that cu
16d7b 72 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 rsor .** happens
16d7c 20 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c to hold..*/.SQL
16d7d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
16d7e 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 sqlite3VdbeFree
16d7f 43 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 Cursor(Vdbe *p,
16d80 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 VdbeCursor *pCx)
16d81 7b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 {. if( pCx==0 )
16d82 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
16d83 7d 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 }. if( pCx->pBt
16d84 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
16d85 74 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 treeClose(pCx->p
16d86 42 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 Bt);. /* The
16d87 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c pCx->pCursor wil
16d88 6c 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d l be close autom
16d89 61 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 atically, if it
16d8a 65 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a exists, by. *
16d8b 2a 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 * the call above
16d8c 2e 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 . */. }else if(
16d8d 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b pCx->pCursor ){
16d8e 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
16d8f 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 eCloseCursor(pCx
16d90 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a ->pCursor);. }.
16d91 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
16d92 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
16d93 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 . if( pCx->pVta
16d94 62 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 bCursor ){. s
16d95 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
16d96 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 or *pVtabCursor
16d97 3d 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 = pCx->pVtabCurs
16d98 6f 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 or;. const sq
16d99 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
16d9a 6f 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f odule = pCx->pMo
16d9b 64 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 dule;. p->inV
16d9c 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 tabMethod = 1;.
16d9d 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
16d9e 53 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 SafetyOff(p->db)
16d9f 3b 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 ;. pModule->x
16da0 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f Close(pVtabCurso
16da1 72 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 r);. (void)sq
16da2 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d lite3SafetyOn(p-
16da3 3e 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 >db);. p->inV
16da4 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 tabMethod = 0;.
16da5 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 }.#endif. if(
16da6 21 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 !pCx->ephemPseud
16da7 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 oTable ){. sq
16da8 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 lite3DbFree(p->d
16da9 62 2c 20 70 43 78 2d 3e 70 44 61 74 61 29 3b 0a b, pCx->pData);.
16daa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f }.}../*.** Clo
16dab 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 se all cursors e
16dac 78 63 65 70 74 20 66 6f 72 20 56 54 61 62 20 63 xcept for VTab c
16dad 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 ursors that are
16dae 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 69 6e 20 currently.** in
16daf 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 use..*/.static v
16db0 6f 69 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 oid closeAllCurs
16db1 6f 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56 orsExceptActiveV
16db2 74 61 62 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 tabs(Vdbe *p){.
16db3 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 2d int i;. if( p-
16db4 3e 61 70 43 73 72 3d 3d 30 20 29 20 72 65 74 75 >apCsr==0 ) retu
16db5 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 rn;. for(i=0; i
16db6 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b <p->nCursor; i++
16db7 29 7b 0a 20 20 20 20 56 64 62 65 43 75 72 73 6f ){. VdbeCurso
16db8 72 20 2a 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 r *pC = p->apCsr
16db9 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 20 [i];. if( pC
16dba 26 26 20 28 21 70 2d 3e 69 6e 56 74 61 62 4d 65 && (!p->inVtabMe
16dbb 74 68 6f 64 20 7c 7c 20 21 70 43 2d 3e 70 56 74 thod || !pC->pVt
16dbc 61 62 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 abCursor) ){.
16dbd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 sqlite3VdbeFr
16dbe 65 65 43 75 72 73 6f 72 28 70 2c 20 70 43 29 3b eeCursor(p, pC);
16dbf 0a 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b . p->apCsr[
16dc0 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 i] = 0;. }.
16dc1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e }.}../*.** Clean
16dc2 20 75 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 up the VM after
16dc3 20 65 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a execution..**.*
16dc4 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
16dc5 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ill automaticall
16dc6 79 20 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 y close any curs
16dc7 6f 72 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f ors, lists, and/
16dc8 6f 72 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 or.** sorters th
16dc9 61 74 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 at were left ope
16dca 6e 2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 n. It also dele
16dcb 74 65 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f tes the values o
16dcc 66 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 f.** variables i
16dcd 6e 20 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 n the aVar[] arr
16dce 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ay..*/.static vo
16dcf 69 64 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 id Cleanup(Vdbe
16dd0 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 *p){. int i;.
16dd1 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d sqlite3 *db = p-
16dd2 3e 64 62 3b 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 >db;. closeAllC
16dd3 75 72 73 6f 72 73 45 78 63 65 70 74 41 63 74 69 ursorsExceptActi
16dd4 76 65 56 74 61 62 73 28 70 29 3b 0a 20 20 66 6f veVtabs(p);. fo
16dd5 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 r(i=1; i<=p->nMe
16dd6 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d m; i++){. Mem
16dd7 53 65 74 54 79 70 65 46 6c 61 67 28 26 70 2d 3e SetTypeFlag(&p->
16dd8 61 4d 65 6d 5b 69 5d 2c 20 4d 45 4d 5f 4e 75 6c aMem[i], MEM_Nul
16dd9 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 l);. }. releas
16dda 65 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d eMemArray(&p->aM
16ddb 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b em[1], p->nMem);
16ddc 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 . sqlite3VdbeFi
16ddd 66 6f 43 6c 65 61 72 28 26 70 2d 3e 73 46 69 66 foClear(&p->sFif
16dde 6f 29 3b 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e o);. if( p->con
16ddf 74 65 78 74 53 74 61 63 6b 20 29 7b 0a 20 20 20 textStack ){.
16de0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 63 for(i=0; i<p->c
16de1 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 3b 20 ontextStackTop;
16de2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
16de3 74 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72 te3VdbeFifoClear
16de4 28 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 (&p->contextStac
16de5 6b 5b 69 5d 2e 73 46 69 66 6f 29 3b 0a 20 20 20 k[i].sFifo);.
16de6 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 }. sqlite3Db
16de7 46 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f 6e 74 Free(db, p->cont
16de8 65 78 74 53 74 61 63 6b 29 3b 0a 20 20 7d 0a 20 extStack);. }.
16de9 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b p->contextStack
16dea 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74 65 = 0;. p->conte
16deb 78 74 53 74 61 63 6b 44 65 70 74 68 20 3d 20 30 xtStackDepth = 0
16dec 3b 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 ;. p->contextSt
16ded 61 63 6b 54 6f 70 20 3d 20 30 3b 0a 20 20 73 71 ackTop = 0;. sq
16dee 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
16def 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 p->zErrMsg);. p
16df0 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
16df1 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d p->pResultSet =
16df2 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 0;.}../*.** Set
16df3 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
16df4 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 74 68 esult columns th
16df5 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 at will be retur
16df6 6e 65 64 20 62 79 20 74 68 69 73 20 53 51 4c 0a ned by this SQL.
16df7 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 ** statement. Th
16df8 69 73 20 69 73 20 6e 6f 77 20 73 65 74 20 61 74 is is now set at
16df9 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 72 compile time, r
16dfa 61 74 68 65 72 20 74 68 61 6e 20 64 75 72 69 6e ather than durin
16dfb 67 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f g.** execution o
16dfc 66 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 f the vdbe progr
16dfd 61 6d 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 am so that sqlit
16dfe 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 e3_column_count(
16dff 29 20 63 61 6e 0a 2a 2a 20 62 65 20 63 61 6c 6c ) can.** be call
16e00 65 64 20 6f 6e 20 61 6e 20 53 51 4c 20 73 74 61 ed on an SQL sta
16e01 74 65 6d 65 6e 74 20 62 65 66 6f 72 65 20 73 71 tement before sq
16e02 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2f lite3_step()..*/
16e03 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16e04 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
16e05 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 20 SetNumCols(Vdbe
16e06 2a 70 2c 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 *p, int nResColu
16e07 6d 6e 29 7b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c mn){. Mem *pCol
16e08 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 Name;. int n;.
16e09 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
16e0a 2d 3e 64 62 3b 0a 0a 20 20 72 65 6c 65 61 73 65 ->db;.. release
16e0b 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c MemArray(p->aCol
16e0c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c Name, p->nResCol
16e0d 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a umn*COLNAME_N);.
16e0e 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
16e0f 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 db, p->aColName)
16e10 3b 0a 20 20 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 ;. n = nResColu
16e11 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 mn*COLNAME_N;.
16e12 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 p->nResColumn =
16e13 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d nResColumn;. p-
16e14 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c >aColName = pCol
16e15 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c Name = (Mem*)sql
16e16 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
16e17 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 (db, sizeof(Mem)
16e18 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 *n );. if( p->a
16e19 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 ColName==0 ) ret
16e1a 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d urn;. while( n-
16e1b 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f - > 0 ){. pCo
16e1c 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d lName->flags = M
16e1d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f EM_Null;. pCo
16e1e 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 lName->db = p->d
16e1f 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b b;. pColName+
16e20 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 +;. }.}../*.**
16e21 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 Set the name of
16e22 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d the idx'th colum
16e23 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 n to be returned
16e24 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 by the SQL stat
16e25 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 ement..** zName
16e26 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 must be a pointe
16e27 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 r to a nul termi
16e28 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a nated string..**
16e29 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 .** This call mu
16e2a 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 st be made after
16e2b 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
16e2c 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 e3VdbeSetNumCols
16e2d 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ()..**.** The fi
16e2e 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 nal parameter, x
16e2f 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 Del, must be one
16e30 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d of SQLITE_DYNAM
16e31 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 IC, SQLITE_STATI
16e32 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 C.** or SQLITE_T
16e33 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 RANSIENT. If it
16e34 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 is SQLITE_DYNAMI
16e35 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 C, then the buff
16e36 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f er pointed.** to
16e37 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 by zName will b
16e38 65 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 e freed by sqlit
16e39 65 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 e3DbFree() when
16e3a 74 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74 the vdbe is dest
16e3b 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 royed..*/.SQLITE
16e3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16e3d 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 ite3VdbeSetColNa
16e3e 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 me(. Vdbe *p,
16e3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16e40 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 /* Vdbe b
16e41 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 eing configured
16e42 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 */. int idx,
16e43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16e44 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
16e45 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 f column zName a
16e46 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 pplies to */. i
16e47 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 nt var,
16e48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16e49 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f /* One of the CO
16e4a 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 LNAME_* constant
16e4b 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 s */. const cha
16e4c 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 r *zName,
16e4d 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
16e4e 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e er to buffer con
16e4f 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a taining name */.
16e50 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
16e51 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 oid*)
16e52 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e /* Memory man
16e53 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 agement strategy
16e54 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b for zName */.){
16e55 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d . int rc;. Mem
16e56 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 *pColName;. as
16e57 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 sert( idx<p->nRe
16e58 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 sColumn );. ass
16e59 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 ert( var<COLNAME
16e5a 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 _N );. if( p->d
16e5b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
16e5c 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
16e5d 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 zName || xDel!=S
16e5e 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b QLITE_DYNAMIC );
16e5f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
16e60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
16e61 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e assert( p->aColN
16e62 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c ame!=0 );. pCol
16e63 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c Name = &(p->aCol
16e64 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e Name[idx+var*p->
16e65 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 nResColumn]);.
16e66 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
16e67 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 MemSetStr(pColNa
16e68 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 me, zName, -1, S
16e69 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c QLITE_UTF8, xDel
16e6a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 );. assert( rc!
16e6b 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 =0 || !zName ||
16e6c 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 (pColName->flags
16e6d 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b &MEM_Term)!=0 );
16e6e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
16e6f 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 ./*.** A read or
16e70 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
16e71 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f on may or may no
16e72 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 t be active on d
16e73 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a atabase handle.*
16e74 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 * db. If a trans
16e75 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
16e76 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 , commit it. If
16e77 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 there is a.** wr
16e78 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
16e79 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 spanning more th
16e7a 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 an one database
16e7b 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 file, this routi
16e7c 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 ne.** takes care
16e7d 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a of the master j
16e7e 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e ournal trickery.
16e7f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 .*/.static int v
16e80 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 dbeCommit(sqlite
16e81 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 3 *db, Vdbe *p){
16e82 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
16e83 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 nTrans = 0; /*
16e84 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 Number of databa
16e85 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 ses with an acti
16e86 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 ve write-transac
16e87 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 tion */. int rc
16e88 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
16e89 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 int needXcommit
16e8a 3d 20 30 3b 0a 0a 20 20 2f 2a 20 42 65 66 6f 72 = 0;.. /* Befor
16e8b 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 e doing anything
16e8c 20 65 6c 73 65 2c 20 63 61 6c 6c 20 74 68 65 20 else, call the
16e8d 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b xSync() callback
16e8e 20 66 6f 72 20 61 6e 79 0a 20 20 2a 2a 20 76 69 for any. ** vi
16e8f 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74 61 62 rtual module tab
16e90 6c 65 73 20 77 72 69 74 74 65 6e 20 69 6e 20 74 les written in t
16e91 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e his transaction.
16e92 20 54 68 69 73 20 68 61 73 20 74 6f 0a 20 20 2a This has to. *
16e93 2a 20 62 65 20 64 6f 6e 65 20 62 65 66 6f 72 65 * be done before
16e94 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 determining whe
16e95 74 68 65 72 20 61 20 6d 61 73 74 65 72 20 6a 6f ther a master jo
16e96 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 20 urnal file is .
16e97 20 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 61 73 ** required, as
16e98 20 61 6e 20 78 53 79 6e 63 28 29 20 63 61 6c 6c an xSync() call
16e99 62 61 63 6b 20 6d 61 79 20 61 64 64 20 61 6e 20 back may add an
16e9a 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
16e9b 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 72 e. ** to the tr
16e9c 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a ansaction.. */.
16e9d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 rc = sqlite3Vt
16e9e 61 62 53 79 6e 63 28 64 62 2c 20 26 70 2d 3e 7a abSync(db, &p->z
16e9f 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72 ErrMsg);. if( r
16ea0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
16ea1 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
16ea2 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f }.. /* This lo
16ea3 6f 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61 op determines (a
16ea4 29 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 ) if the commit
16ea5 68 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69 hook should be i
16ea6 6e 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20 nvoked and. **
16ea7 28 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74 (b) how many dat
16ea8 61 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65 abase files have
16ea9 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e open write tran
16eaa 73 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20 sactions, not .
16eab 20 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 ** including th
16eac 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e e temp database.
16ead 20 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e (b) is importan
16eae 74 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72 t because if mor
16eaf 65 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65 e than . ** one
16eb0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 database file h
16eb1 61 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 as an open write
16eb2 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 transaction, a
16eb3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20 master journal.
16eb4 20 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75 ** file is requ
16eb5 69 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d ired for an atom
16eb6 69 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20 ic commit.. */
16eb7 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
16eb8 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
16eb9 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 Btree *pBt = d
16eba 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
16ebb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 if( sqlite3Bt
16ebc 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 reeIsInTrans(pBt
16ebd 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58 ) ){. needX
16ebe 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 commit = 1;.
16ebf 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72 if( i!=1 ) nTr
16ec0 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d ans++;. }. }
16ec1 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
16ec2 61 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74 72 are any write-tr
16ec3 61 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61 6c ansactions at al
16ec4 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f l, invoke the co
16ec5 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69 mmit hook */. i
16ec6 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 26 f( needXcommit &
16ec7 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c & db->xCommitCal
16ec8 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 28 76 6f lback ){. (vo
16ec9 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
16eca 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20 Off(db);. rc
16ecb 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c = db->xCommitCal
16ecc 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 lback(db->pCommi
16ecd 74 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69 64 tArg);. (void
16ece 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e )sqlite3SafetyOn
16ecf 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 (db);. if( rc
16ed0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
16ed1 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
16ed2 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 NT;. }. }..
16ed3 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 63 /* The simple c
16ed4 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68 ase - no more th
16ed5 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 an one database
16ed6 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69 file (not counti
16ed7 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d 50 ng the. ** TEMP
16ed8 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20 61 database) has a
16ed9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 transaction act
16eda 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 20 ive. There is
16edb 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0a no need for the.
16edc 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 ** master-jour
16edd 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 nal.. **. ** I
16ede 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c f the return val
16edf 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72 ue of sqlite3Btr
16ee0 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 20 eeGetFilename()
16ee1 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 68 is a zero length
16ee2 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 74 . ** string, it
16ee3 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 means the main
16ee4 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 6d database is :mem
16ee5 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 66 ory: or a temp f
16ee6 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 74 ile. In . ** t
16ee7 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 6e hat case we do n
16ee8 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d 69 ot support atomi
16ee9 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f 6d c multi-file com
16eea 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 65 mits, so use the
16eeb 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 61 . ** simple ca
16eec 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 2a se then too.. *
16eed 2f 0a 20 20 69 66 28 20 30 3d 3d 73 74 72 6c 65 /. if( 0==strle
16eee 6e 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 n(sqlite3BtreeGe
16eef 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 tFilename(db->aD
16ef0 62 5b 30 5d 2e 70 42 74 29 29 20 7c 7c 20 6e 54 b[0].pBt)) || nT
16ef1 72 61 6e 73 3c 3d 31 20 29 7b 0a 20 20 20 20 66 rans<=1 ){. f
16ef2 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 or(i=0; rc==SQLI
16ef3 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e TE_OK && i<db->n
16ef4 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 Db; i++){ .
16ef5 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 Btree *pBt = db
16ef6 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
16ef7 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 if( pBt ){.
16ef8 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
16ef9 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
16efa 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a aseOne(pBt, 0);.
16efb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
16efc 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d /* Do the com
16efd 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 mit only if all
16efe 64 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73 databases succes
16eff 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 sfully complete
16f00 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a phase 1. . **
16f01 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 If one of the B
16f02 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
16f03 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 ne() calls fails
16f04 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 , this indicates
16f05 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 an. ** IO er
16f06 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 ror while deleti
16f07 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 ng or truncating
16f08 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e a journal file.
16f09 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c It is unlikely,
16f0a 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c . ** but coul
16f0b 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 d happen. In thi
16f0c 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 s case abandon p
16f0d 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 rocessing and re
16f0e 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a turn the error..
16f0f 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 */. for(i
16f10 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f =0; rc==SQLITE_O
16f11 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 K && i<db->nDb;
16f12 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 i++){. Btre
16f13 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 e *pBt = db->aDb
16f14 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 [i].pBt;. i
16f15 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 f( pBt ){.
16f16 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
16f17 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 reeCommitPhaseTw
16f18 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a o(pBt);. }.
16f19 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
16f1a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
16f1b 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 sqlite3Vtab
16f1c 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 Commit(db);.
16f1d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 }. }.. /* The
16f1e 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54 complex case - T
16f1f 68 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d here is a multi-
16f20 66 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 file write-trans
16f21 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 action active..
16f22 20 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65 ** This require
16f23 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e s a master journ
16f24 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 al file to ensur
16f25 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f e the transactio
16f26 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 n is. ** commit
16f27 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 ted atomicly..
16f28 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
16f29 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 E_OMIT_DISKIO.
16f2a 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
16f2b 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 3_vfs *pVfs = db
16f2c 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 ->pVfs;. int
16f2d 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
16f2e 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 char *zMaster
16f2f 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e = 0; /* File-n
16f30 61 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74 ame for the mast
16f31 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 er journal */.
16f32 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d char const *zM
16f33 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 ainFile = sqlite
16f34 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 3BtreeGetFilenam
16f35 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 e(db->aDb[0].pBt
16f36 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
16f37 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 ile *pMaster = 0
16f38 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 ;. i64 offset
16f39 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 = 0;. int re
16f3a 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 s;.. /* Selec
16f3b 74 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e t a master journ
16f3c 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a al file name */.
16f3d 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 do {. u
16f3e 33 32 20 72 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 32 random;.
16f3f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
16f40 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 b, zMaster);.
16f41 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f sqlite3_rando
16f42 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 61 6e mness(sizeof(ran
16f43 64 6f 6d 29 2c 20 26 72 61 6e 64 6f 6d 29 3b 0a dom), &random);.
16f44 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 zMaster =
16f45 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
16f46 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20 b, "%s-mj%08X",
16f47 7a 4d 61 69 6e 46 69 6c 65 2c 20 72 61 6e 64 6f zMainFile, rando
16f48 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 m&0x7fffffff);.
16f49 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 if( !zMaste
16f4a 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 r ){. ret
16f4b 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
16f4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
16f4d 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 rc = sqlite3OsAc
16f4e 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 cess(pVfs, zMast
16f4f 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 er, SQLITE_ACCES
16f50 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b S_EXISTS, &res);
16f51 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d . }while( rc=
16f52 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 =SQLITE_OK && re
16f53 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d s );. if( rc=
16f54 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16f55 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 /* Open the
16f56 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 master journal.
16f57 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
16f58 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f lite3OsOpenMallo
16f59 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c c(pVfs, zMaster,
16f5a 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20 20 &pMaster, .
16f5b 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 SQLITE_OPE
16f5c 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 N_READWRITE|SQLI
16f5d 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a TE_OPEN_CREATE|.
16f5e 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
16f5f 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 7c _OPEN_EXCLUSIVE|
16f60 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 SQLITE_OPEN_MAST
16f61 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20 20 ER_JOURNAL, 0.
16f62 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 );. }.
16f63 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16f64 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
16f65 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d te3DbFree(db, zM
16f66 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 aster);. re
16f67 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
16f68 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 . /* Write th
16f69 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20 64 e name of each d
16f6a 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 atabase file in
16f6b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
16f6c 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 20 into the new.
16f6d 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e ** master journ
16f6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20 65 al file. If an e
16f6f 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20 74 rror occurs at t
16f70 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65 0a his point close.
16f71 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65 74 ** and delet
16f72 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
16f73 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20 74 rnal file. All t
16f74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f he individual jo
16f75 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20 20 urnal files.
16f76 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27 6e ** still have 'n
16f77 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73 74 ull' as the mast
16f78 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 er journal point
16f79 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c 6c er, so they will
16f7a 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61 63 roll. ** bac
16f7b 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 k independently
16f7c 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 if a failure occ
16f7d 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 urs.. */.
16f7e 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e for(i=0; i<db->n
16f7f 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 Db; i++){.
16f80 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d Btree *pBt = db-
16f81 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
16f82 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f if( i==1 ) co
16f83 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 67 6e ntinue; /* Ign
16f84 6f 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 ore the TEMP dat
16f85 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 69 abase */. i
16f86 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 f( sqlite3BtreeI
16f87 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 7b sInTrans(pBt) ){
16f88 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63 6f . char co
16f89 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71 6c nst *zFile = sql
16f8a 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 ite3BtreeGetJour
16f8b 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a 20 20 nalname(pBt);.
16f8c 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 5b if( zFile[
16f8d 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 0]==0 ) continue
16f8e 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a 6d 65 ; /* Ignore :me
16f8f 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73 20 mory: databases
16f90 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 */. if( !
16f91 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73 71 6c needSync && !sql
16f92 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 ite3BtreeSyncDis
16f93 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20 20 abled(pBt) ){.
16f94 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 needSync
16f95 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
16f96 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
16f97 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 73 ite3OsWrite(pMas
16f98 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 74 72 6c ter, zFile, strl
16f99 65 6e 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 en(zFile)+1, off
16f9a 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 set);. of
16f9b 66 73 65 74 20 2b 3d 20 73 74 72 6c 65 6e 28 7a fset += strlen(z
16f9c 46 69 6c 65 29 2b 31 3b 0a 20 20 20 20 20 20 20 File)+1;.
16f9d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16f9e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
16f9f 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 sqlite3OsCloseFr
16fa0 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 ee(pMaster);.
16fa1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 sqlite3Os
16fa2 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 Delete(pVfs, zMa
16fa3 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 ster, 0);.
16fa4 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
16fa5 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a e(db, zMaster);.
16fa6 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
16fa7 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
16fa8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
16fa9 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6d 61 /* Sync the ma
16faa 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
16fab 65 2e 20 49 66 20 74 68 65 20 49 4f 43 41 50 5f e. If the IOCAP_
16fac 53 45 51 55 45 4e 54 49 41 4c 20 64 65 76 69 63 SEQUENTIAL devic
16fad 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 e. ** flag is
16fae 20 73 65 74 20 74 68 69 73 20 69 73 20 6e 6f 74 set this is not
16faf 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a required.. *
16fb0 2f 0a 20 20 20 20 7a 4d 61 69 6e 46 69 6c 65 20 /. zMainFile
16fb1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
16fb2 74 44 69 72 6e 61 6d 65 28 64 62 2d 3e 61 44 62 tDirname(db->aDb
16fb3 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66 [0].pBt);. if
16fb4 28 20 28 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 ( (needSync .
16fb5 20 20 26 26 20 28 30 3d 3d 28 73 71 6c 69 74 65 && (0==(sqlite
16fb6 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 3OsDeviceCharact
16fb7 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 72 eristics(pMaster
16fb8 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 )&SQLITE_IOCAP_S
16fb9 45 51 55 45 4e 54 49 41 4c 29 29 0a 20 20 20 20 EQUENTIAL)).
16fba 20 26 26 20 28 72 63 3d 73 71 6c 69 74 65 33 4f && (rc=sqlite3O
16fbb 73 53 79 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 sSync(pMaster, S
16fbc 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 QLITE_SYNC_NORMA
16fbd 4c 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 L))!=SQLITE_OK)
16fbe 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
16fbf 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 OsCloseFree(pMas
16fc0 74 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ter);. sqli
16fc1 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 te3OsDelete(pVfs
16fc2 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 , zMaster, 0);.
16fc3 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
16fc4 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
16fc5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
16fc6 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
16fc7 53 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20 Sync all the db
16fc8 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 files involved i
16fc9 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f n the transactio
16fca 6e 2e 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c n. The same call
16fcb 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 . ** sets the
16fcc 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
16fcd 70 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20 pointer in each
16fce 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e individual journ
16fcf 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e al. If. ** an
16fd0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65 error occurs he
16fd1 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 re, do not delet
16fd2 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
16fd3 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a rnal file.. *
16fd4 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 *. ** If the
16fd5 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
16fd6 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 ing the first ca
16fd7 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c ll to. ** sql
16fd8 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
16fd9 68 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 haseOne(), then
16fda 74 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 there is a chanc
16fdb 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a e that the. *
16fdc 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c * master journal
16fdd 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 file will be or
16fde 70 68 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63 phaned. But we c
16fdf 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c annot delete it,
16fe0 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 . ** in case
16fe1 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
16fe2 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 al file name was
16fe3 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
16fe4 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a e journal. **
16fe5 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65 file before the
16fe6 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 65 64 failure occured
16fe7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
16fe8 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 (i=0; rc==SQLITE
16fe9 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 _OK && i<db->nDb
16fea 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 ; i++){ . B
16feb 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e tree *pBt = db->
16fec 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
16fed 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 if( pBt ){.
16fee 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
16fef 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
16ff0 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 eOne(pBt, zMaste
16ff1 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 r);. }.
16ff2 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 }. sqlite3OsC
16ff3 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 loseFree(pMaster
16ff4 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
16ff5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16ff6 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
16ff7 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 db, zMaster);.
16ff8 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
16ff9 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c }.. /* Del
16ffa 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ete the master j
16ffb 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 ournal file. Thi
16ffc 73 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 s commits the tr
16ffd 61 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 ansaction. After
16ffe 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 . ** doing th
16fff 69 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 is the directory
17000 20 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e is synced again
17001 20 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 before any indi
17002 76 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 vidual. ** tr
17003 61 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 ansaction files
17004 61 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 are deleted..
17005 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
17006 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 ite3OsDelete(pVf
17007 73 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a s, zMaster, 1);.
17008 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
17009 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a e(db, zMaster);.
1700a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b zMaster = 0;
1700b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
1700c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1700d 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c }.. /* Al
1700e 6c 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 l files and dire
1700f 63 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 ctories have alr
17010 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 eady been synced
17011 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 , so the followi
17012 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 ng. ** calls
17013 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 to sqlite3BtreeC
17014 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 ommitPhaseTwo()
17015 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 are only closing
17016 20 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a files and. *
17017 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 * deleting or tr
17018 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c uncating journal
17019 73 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 s. If something
1701a 67 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 goes wrong while
1701b 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 . ** this is
1701c 68 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e happening we don
1701d 27 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 't really care.
1701e 54 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 The integrity of
1701f 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e the. ** tran
17020 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 saction is alrea
17021 64 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 dy guaranteed, b
17022 75 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 ut some stray 'c
17023 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 old' journals.
17024 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e ** may be lyin
17025 67 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e g around. Return
17026 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 ing an error cod
17027 65 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 e won't help mat
17028 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ters.. */.
17029 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 disable_simulat
1702a 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a ed_io_errors();.
1702b 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e sqlite3Begin
1702c 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
1702d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 for(i=0; i<d
1702e 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 b->nDb; i++){ .
1702f 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 Btree *pBt
17030 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
17031 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 ;. if( pBt
17032 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
17033 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
17034 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 seTwo(pBt);.
17035 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
17036 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
17037 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 lloc();. enab
17038 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f le_simulated_io_
17039 65 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 errors();.. s
1703a 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 qlite3VtabCommit
1703b 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 (db);. }.#endif
1703c 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
1703d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f ../* .** This ro
1703e 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 utine checks tha
1703f 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 t the sqlite3.ac
17040 74 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e tiveVdbeCnt coun
17041 74 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 t variable.** ma
17042 74 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 tches the number
17043 20 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 of vdbe's in th
17044 65 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 e list sqlite3.p
17045 56 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a Vdbe that are.**
17046 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 currently activ
17047 65 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 e. An assertion
17048 66 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f fails if the two
17049 20 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d counts do not m
1704a 61 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 atch..** This is
1704b 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c an internal sel
1704c 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 f-check only - i
1704d 74 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 t is not an esse
1704e 6e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 ntial processing
1704f 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 .** step..**.**
17050 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 This is a no-op
17051 69 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 if NDEBUG is def
17052 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 ined..*/.#ifndef
17053 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 NDEBUG.static v
17054 6f 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 oid checkActiveV
17055 64 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a dbeCnt(sqlite3 *
17056 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a db){. Vdbe *p;.
17057 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 int cnt = 0;.
17058 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b int nWrite = 0;
17059 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 . p = db->pVdbe
1705a 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a ;. while( p ){.
1705b 20 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 if( p->magic
1705c 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e ==VDBE_MAGIC_RUN
1705d 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a && p->pc>=0 ){.
1705e 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 cnt++;.
1705f 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e if( p->readOn
17060 6c 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b ly==0 ) nWrite++
17061 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 ;. }. p =
17062 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 p->pNext;. }.
17063 61 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d assert( cnt==db-
17064 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 >activeVdbeCnt )
17065 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 ;. assert( nWri
17066 74 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 te==db->writeVdb
17067 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a eCnt );.}.#else.
17068 23 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 #define checkAct
17069 69 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 iveVdbeCnt(x).#e
1706a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 ndif../*.** For
1706b 65 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74 every Btree that
1706c 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e in database con
1706d 6e 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 nection db which
1706e 20 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f .** has been mo
1706f 64 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f dified, "trip" o
17070 72 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 r invalidate eac
17071 68 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 h cursor in.** t
17072 68 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 hat Btree might
17073 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 have been modifi
17074 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 ed so that the c
17075 75 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 ursor.** can nev
17076 65 72 20 62 65 20 75 73 65 64 20 61 67 61 69 6e er be used again
17077 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 . This happens
17078 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a when a rollback.
17079 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 *** occurs. We
1707a 68 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c have to trip all
1707b 20 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f the other curso
1707c 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 rs, even.** curs
1707d 6f 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d or from other VM
1707e 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 s in different d
1707f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
17080 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 ons,.** so that
17081 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 none of them try
17082 20 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 to use the data
17083 20 61 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a at which they.*
17084 2a 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 * were pointing
17085 61 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 and which now ma
17086 79 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e y have been chan
17087 67 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 ged due.** to th
17088 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a e rollback..**.*
17089 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 * Remember that
1708a 61 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 a rollback can d
1708b 65 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d elete tables com
1708c 70 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f plete and.** reo
1708d 72 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 rder rootpages.
1708e 20 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 So it is not su
1708f 66 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f fficient just to
17090 20 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 save.** the sta
17091 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 te of the cursor
17092 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e . We have to in
17093 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 validate the cur
17094 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 sor.** so that i
17095 74 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 t is never used
17096 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 again..*/.static
17097 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 void invalidate
17098 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 CursorsOnModifie
17099 64 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 dBtrees(sqlite3
1709a 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *db){. int i;.
1709b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
1709c 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 nDb; i++){. B
1709d 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 tree *p = db->aD
1709e 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 b[i].pBt;. if
1709f 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 ( p && sqlite3Bt
170a0 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 reeIsInTrans(p)
170a1 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
170a2 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 BtreeTripAllCurs
170a3 6f 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 ors(p, SQLITE_AB
170a4 4f 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ORT);. }. }.
170a5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
170a6 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
170a7 74 68 65 20 77 68 65 6e 20 61 20 56 44 42 45 20 the when a VDBE
170a8 74 72 69 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 tries to halt.
170a9 49 66 20 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 If the VDBE.** h
170aa 61 73 20 6d 61 64 65 20 63 68 61 6e 67 65 73 20 as made changes
170ab 61 6e 64 20 69 73 20 69 6e 20 61 75 74 6f 63 6f and is in autoco
170ac 6d 6d 69 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 mmit mode, then
170ad 63 6f 6d 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 commit those.**
170ae 63 68 61 6e 67 65 73 2e 20 20 49 66 20 61 20 72 changes. If a r
170af 6f 6c 6c 62 61 63 6b 20 69 73 20 6e 65 65 64 65 ollback is neede
170b0 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 20 72 d, then do the r
170b1 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 ollback..**.** T
170b2 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 his routine is t
170b3 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d he only way to m
170b4 6f 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 ove the state of
170b5 20 61 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 a VM from.** SQ
170b6 4c 49 54 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 LITE_MAGIC_RUN t
170b7 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 o SQLITE_MAGIC_H
170b8 41 4c 54 2e 20 20 49 74 20 69 73 20 68 61 72 6d ALT. It is harm
170b9 6c 65 73 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 less to.** call
170ba 74 68 69 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 this on a VM tha
170bb 74 20 69 73 20 69 6e 20 74 68 65 20 53 51 4c 49 t is in the SQLI
170bc 54 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 TE_MAGIC_HALT st
170bd 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ate..**.** Retur
170be 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e n an error code.
170bf 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 If the commit
170c0 63 6f 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 could not comple
170c1 74 65 20 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a te because of.**
170c2 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e lock contention
170c3 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
170c4 42 55 53 59 2e 20 20 49 66 20 53 51 4c 49 54 45 BUSY. If SQLITE
170c5 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 _BUSY is returne
170c6 64 2c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 d, it.** means t
170c7 68 65 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 he close did not
170c8 20 68 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 happen and need
170c9 73 20 74 6f 20 62 65 20 72 65 70 65 61 74 65 64 s to be repeated
170ca 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
170cb 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
170cc 64 62 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29 dbeHalt(Vdbe *p)
170cd 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
170ce 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 = p->db;. int i
170cf 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 ;. int (*xFunc)
170d0 28 42 74 72 65 65 20 2a 70 42 74 29 20 3d 20 30 (Btree *pBt) = 0
170d1 3b 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 ; /* Function t
170d2 6f 20 63 61 6c 6c 20 6f 6e 20 65 61 63 68 20 62 o call on each b
170d3 74 72 65 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a tree backend */.
170d4 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c 45 int isSpecialE
170d5 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 rror;
170d6 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 /* Set to true
170d7 69 66 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 if SQLITE_NOMEM
170d8 6f 72 20 49 4f 45 52 52 20 2a 2f 0a 0a 20 20 2f or IOERR */.. /
170d9 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
170da 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 contains the log
170db 69 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e ic that determin
170dc 65 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e es if a statemen
170dd 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 t or. ** transa
170de 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f ction will be co
170df 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 mmitted or rolle
170e0 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 d back as a resu
170e1 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 lt of the. ** e
170e2 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 xecution of this
170e3 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
170e4 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 . . **. ** If
170e5 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f any of the follo
170e6 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 wing errors occu
170e7 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 r:. **. **
170e8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 SQLITE_NOMEM.
170e9 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f ** SQLITE_IO
170ea 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c ERR. ** SQL
170eb 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 ITE_FULL. **
170ec 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 SQLITE_INTERRU
170ed 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 PT. **. ** The
170ee 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 n the internal c
170ef 61 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 ache might have
170f0 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 been left in an
170f1 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a inconsistent. *
170f2 2a 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 * state. We nee
170f3 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 d to rollback th
170f4 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e e statement tran
170f5 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 saction, if ther
170f6 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f e is. ** one, o
170f7 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 r the complete t
170f8 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 ransaction if th
170f9 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d ere is no statem
170fa 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ent transaction.
170fb 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e . */.. if( p->
170fc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
170fd 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
170fe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
170ff 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 }. closeAllCurs
17100 6f 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56 orsExceptActiveV
17101 74 61 62 73 28 70 29 3b 0a 20 20 69 66 28 20 70 tabs(p);. if( p
17102 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 ->magic!=VDBE_MA
17103 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 GIC_RUN ){. r
17104 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
17105 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 . }. checkActi
17106 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a veVdbeCnt(db);..
17107 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f /* No commit o
17108 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 r rollback neede
17109 64 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d d if the program
1710a 20 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a never started *
1710b 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 /. if( p->pc>=0
1710c 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b ){. int mrc;
1710d 20 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 /* Primary er
1710e 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d ror code from p-
1710f 3e 72 63 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c >rc */.. /* L
17110 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 20 75 ock all btrees u
17111 73 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 sed by the state
17112 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 ment */. sqli
17113 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
17114 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 ayEnter(&p->aMut
17115 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 ex);.. /* Che
17116 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74 68 ck for one of th
17117 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73 e special errors
17118 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 70 2d */. mrc = p-
17119 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20 20 20 >rc & 0xff;.
1711a 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 20 3d isSpecialError =
1711b 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d mrc==SQLITE_NOM
1711c 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 EM || mrc==SQLIT
1711d 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 20 20 E_IOERR.
1711e 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
1711f 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 mrc==SQLITE_INTE
17120 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53 51 RRUPT || mrc==SQ
17121 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 69 LITE_FULL;. i
17122 66 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f f( isSpecialErro
17123 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 r ){. /* If
17124 20 74 68 65 20 71 75 65 72 79 20 77 61 73 20 72 the query was r
17125 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e 65 65 ead-only, we nee
17126 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b d do no rollback
17127 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 77 69 at all. Otherwi
17128 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f se,. ** pro
17129 63 65 65 64 20 77 69 74 68 20 74 68 65 20 73 70 ceed with the sp
1712a 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a ecial handling..
1712b 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
1712c 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 f( !p->readOnly
1712d 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f 49 || mrc!=SQLITE_I
1712e 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20 20 NTERRUPT ){.
1712f 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 if( p->rc==S
17130 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 QLITE_IOERR_BLOC
17131 4b 45 44 20 26 26 20 70 2d 3e 75 73 65 73 53 74 KED && p->usesSt
17132 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 mtJournal ){.
17133 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 xFunc = s
17134 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
17135 61 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 ackStmt;.
17136 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 p->rc = SQLIT
17137 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 E_BUSY;.
17138 7d 65 6c 73 65 20 69 66 28 20 28 6d 72 63 3d 3d }else if( (mrc==
17139 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 SQLITE_NOMEM ||
1713a 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c mrc==SQLITE_FULL
1713b 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
1713c 20 20 20 20 20 26 26 20 70 2d 3e 75 73 65 73 53 && p->usesS
1713d 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 tmtJournal ){.
1713e 20 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 xFunc =
1713f 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c sqlite3BtreeRoll
17140 62 61 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 backStmt;.
17141 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
17142 20 20 20 2f 2a 20 57 65 20 61 72 65 20 66 6f 72 /* We are for
17143 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b ced to roll back
17144 20 74 68 65 20 61 63 74 69 76 65 20 74 72 61 6e the active tran
17145 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65 20 saction. Before
17146 64 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 doing.
17147 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e 79 ** so, abort any
17148 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 other statement
17149 73 20 74 68 69 73 20 68 61 6e 64 6c 65 20 63 75 s this handle cu
1714a 72 72 65 6e 74 6c 79 20 68 61 73 20 61 63 74 69 rrently has acti
1714b 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f ve.. */
1714c 0a 20 20 20 20 20 20 20 20 20 20 69 6e 76 61 6c . inval
1714d 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f idateCursorsOnMo
1714e 64 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29 difiedBtrees(db)
1714f 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
17150 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 te3RollbackAll(d
17151 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 b);. db
17152 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 ->autoCommit = 1
17153 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
17154 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
17155 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d /* If the auto-
17156 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 commit flag is s
17157 65 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 74 et and this is t
17158 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 76 he only active v
17159 64 62 65 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a dbe, then. **
1715a 20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 we do either a
1715b 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 commit or rollba
1715c 63 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ck of the curren
1715d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a t transaction. .
1715e 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f **. ** No
1715f 74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 te: This block a
17160 6c 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 lso runs if one
17161 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 of the special e
17162 72 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 rrors handled .
17163 20 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 ** above has
17164 6f 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a occurred. . *
17165 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 75 /. if( db->au
17166 74 6f 43 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e toCommit && db->
17167 77 72 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 writeVdbeCnt==(p
17168 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 29 ->readOnly==0) )
17169 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 {. if( p->r
1716a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
1716b 28 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d (p->errorAction=
1716c 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21 69 73 53 =OE_Fail && !isS
1716d 70 65 63 69 61 6c 45 72 72 6f 72 29 20 29 7b 0a pecialError) ){.
1716e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 /* The a
1716f 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 uto-commit flag
17170 69 73 20 74 72 75 65 2c 20 61 6e 64 20 74 68 65 is true, and the
17171 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 61 vdbe program wa
17172 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 75 s . ** su
17173 63 63 65 73 73 66 75 6c 20 6f 72 20 68 69 74 20 ccessful or hit
17174 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 63 6f 6e an 'OR FAIL' con
17175 73 74 72 61 69 6e 74 2e 20 54 68 69 73 20 6d 65 straint. This me
17176 61 6e 73 20 61 20 63 6f 6d 6d 69 74 20 0a 20 20 ans a commit .
17177 20 20 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75 ** is requ
17178 69 72 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f ired.. */
17179 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 . int rc
1717a 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c = vdbeCommit(db,
1717b 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 p);. if(
1717c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
1717d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
1717e 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
1717f 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d rrayLeave(&p->aM
17180 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 utex);.
17181 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
17182 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c USY;. }el
17183 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 se if( rc!=SQLIT
17184 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
17185 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
17186 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 sqlite3R
17187 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a ollbackAll(db);.
17188 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
17189 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1718a 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 CommitInternalCh
1718b 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20 anges(db);.
1718c 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1718d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1718e 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3RollbackAll(db)
1718f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
17190 6c 73 65 20 69 66 28 20 21 78 46 75 6e 63 20 29 lse if( !xFunc )
17191 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 {. if( p->r
17192 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
17193 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d p->errorAction==
17194 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20 20 20 OE_Fail ){.
17195 20 20 20 69 66 28 20 70 2d 3e 6f 70 65 6e 65 64 if( p->opened
17196 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 Statement ){.
17197 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 xFunc = s
17198 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
17199 74 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d tStmt;. }
1719a 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 . }else if
1719b 28 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e ( p->errorAction
1719c 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 ==OE_Abort ){.
1719d 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 71 xFunc = sq
1719e 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
1719f 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 7d 65 ckStmt;. }e
171a0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 lse{. inv
171a1 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e alidateCursorsOn
171a2 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 64 ModifiedBtrees(d
171a3 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 b);. sqli
171a4 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 te3RollbackAll(d
171a5 62 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e b);. db->
171a6 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a autoCommit = 1;.
171a7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
171a8 0a 20 20 20 20 2f 2a 20 49 66 20 78 46 75 6e 63 . /* If xFunc
171a9 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 is not NULL, th
171aa 65 6e 20 69 74 20 69 73 20 6f 6e 65 20 6f 66 20 en it is one of
171ab 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c sqlite3BtreeRoll
171ac 62 61 63 6b 53 74 6d 74 20 6f 72 0a 20 20 20 20 backStmt or.
171ad 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 ** sqlite3BtreeC
171ae 6f 6d 6d 69 74 53 74 6d 74 2e 20 43 61 6c 6c 20 ommitStmt. Call
171af 69 74 20 6f 6e 63 65 20 6f 6e 20 65 61 63 68 20 it once on each
171b0 62 61 63 6b 65 6e 64 2e 20 49 66 20 61 6e 20 65 backend. If an e
171b1 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 20 20 rror occurs.
171b2 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 74 75 72 ** and the retur
171b3 6e 20 63 6f 64 65 20 69 73 20 73 74 69 6c 6c 20 n code is still
171b4 53 51 4c 49 54 45 5f 4f 4b 2c 20 73 65 74 20 74 SQLITE_OK, set t
171b5 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 he return code t
171b6 6f 20 74 68 65 20 6e 65 77 0a 20 20 20 20 2a 2a o the new. **
171b7 20 65 72 72 6f 72 20 76 61 6c 75 65 2e 0a 20 20 error value..
171b8 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
171b9 21 78 46 75 6e 63 20 7c 7c 0a 20 20 20 20 20 20 !xFunc ||.
171ba 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65 33 42 74 xFunc==sqlite3Bt
171bb 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 20 7c 7c reeCommitStmt ||
171bc 0a 20 20 20 20 20 20 78 46 75 6e 63 3d 3d 73 71 . xFunc==sq
171bd 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
171be 63 6b 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 ckStmt. );.
171bf 20 20 66 6f 72 28 69 3d 30 3b 20 78 46 75 6e 63 for(i=0; xFunc
171c0 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 && i<db->nDb; i
171c1 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 ++){ . int
171c2 72 63 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 rc;. Btree
171c3 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 *pBt = db->aDb[i
171c4 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 ].pBt;. if(
171c5 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 pBt ){.
171c6 72 63 20 3d 20 78 46 75 6e 63 28 70 42 74 29 3b rc = xFunc(pBt);
171c7 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
171c8 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 && (p->rc==SQLIT
171c9 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 E_OK || p->rc==S
171ca 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
171cb 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
171cc 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 ->rc = rc;.
171cd 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
171ce 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
171cf 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d g);. p-
171d0 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
171d1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
171d2 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
171d3 49 66 20 74 68 69 73 20 77 61 73 20 61 6e 20 49 If this was an I
171d4 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f 72 NSERT, UPDATE or
171d5 20 44 45 4c 45 54 45 20 61 6e 64 20 74 68 65 20 DELETE and the
171d6 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 63 6f statement was co
171d7 6d 6d 69 74 74 65 64 2c 20 0a 20 20 20 20 2a 2a mmitted, . **
171d8 20 73 65 74 20 74 68 65 20 63 68 61 6e 67 65 20 set the change
171d9 63 6f 75 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f counter. . */
171da 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 68 61 6e . if( p->chan
171db 67 65 43 6e 74 4f 6e 20 26 26 20 70 2d 3e 70 63 geCntOn && p->pc
171dc 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 >=0 ){. if(
171dd 20 21 78 46 75 6e 63 20 7c 7c 20 78 46 75 6e 63 !xFunc || xFunc
171de 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f ==sqlite3BtreeCo
171df 6d 6d 69 74 53 74 6d 74 20 29 7b 0a 20 20 20 20 mmitStmt ){.
171e0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 sqlite3VdbeS
171e1 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 2d etChanges(db, p-
171e2 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 >nChange);.
171e3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
171e4 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 sqlite3VdbeSetCh
171e5 61 6e 67 65 73 28 64 62 2c 20 30 29 3b 0a 20 20 anges(db, 0);.
171e6 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 6e }. p->n
171e7 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 Change = 0;.
171e8 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 6f 6c 6c }. . /* Roll
171e9 62 61 63 6b 20 6f 72 20 63 6f 6d 6d 69 74 20 61 back or commit a
171ea 6e 79 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 ny schema change
171eb 73 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 2e s that occurred.
171ec 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 */. if( p->r
171ed 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c!=SQLITE_OK &&
171ee 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 db->flags&SQLITE
171ef 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 20 29 _InternChanges )
171f0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 {. sqlite3R
171f1 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 esetInternalSche
171f2 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 ma(db, 0);.
171f3 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 28 64 62 db->flags = (db
171f4 2d 3e 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 ->flags | SQLITE
171f5 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 3b _InternChanges);
171f6 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 . }.. /* R
171f7 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 73 elease the locks
171f8 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 */. sqlite3B
171f9 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 treeMutexArrayLe
171fa 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b ave(&p->aMutex);
171fb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 68 61 . }.. /* We ha
171fc 76 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ve successfully
171fd 68 61 6c 74 65 64 20 61 6e 64 20 63 6c 6f 73 65 halted and close
171fe 64 20 74 68 65 20 56 4d 2e 20 20 52 65 63 6f 72 d the VM. Recor
171ff 64 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a d this fact. */.
17200 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 if( p->pc>=0 )
17201 7b 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65 {. db->active
17202 56 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 69 VdbeCnt--;. i
17203 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 f( !p->readOnly
17204 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 77 72 69 ){. db->wri
17205 74 65 56 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 teVdbeCnt--;.
17206 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 }. assert( d
17207 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
17208 3e 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 >=db->writeVdbeC
17209 6e 74 20 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d nt );. }. p->m
1720a 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
1720b 43 5f 48 41 4c 54 3b 0a 20 20 63 68 65 63 6b 41 C_HALT;. checkA
1720c 63 74 69 76 65 56 64 62 65 43 6e 74 28 64 62 29 ctiveVdbeCnt(db)
1720d 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d ;. if( p->db->m
1720e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1720f 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 p->rc = SQLIT
17210 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 E_NOMEM;. }..
17211 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
17212 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 ;.}.../*.** Each
17213 20 56 44 42 45 20 68 6f 6c 64 73 20 74 68 65 20 VDBE holds the
17214 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6d 6f result of the mo
17215 73 74 20 72 65 63 65 6e 74 20 73 71 6c 69 74 65 st recent sqlite
17216 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 0a 2a 2a 3_step() call.**
17217 20 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68 69 73 in p->rc. This
17218 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
17219 61 74 20 72 65 73 75 6c 74 20 62 61 63 6b 20 74 at result back t
1721a 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a o SQLITE_OK..*/.
1721b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1721c 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 oid sqlite3VdbeR
1721d 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 esetStepResult(V
1721e 64 62 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 72 63 dbe *p){. p->rc
1721f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a = SQLITE_OK;.}.
17220 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 ./*.** Clean up
17221 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 a VDBE after exe
17222 63 75 74 69 6f 6e 20 62 75 74 20 64 6f 20 6e 6f cution but do no
17223 74 20 64 65 6c 65 74 65 20 74 68 65 20 56 44 42 t delete the VDB
17224 45 20 6a 75 73 74 20 79 65 74 2e 0a 2a 2a 20 57 E just yet..** W
17225 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d rite any error m
17226 65 73 73 61 67 65 73 20 69 6e 74 6f 20 2a 70 7a essages into *pz
17227 45 72 72 4d 73 67 2e 20 20 52 65 74 75 72 6e 20 ErrMsg. Return
17228 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e the result code.
17229 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 .**.** After thi
1722a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e s routine is run
1722b 2c 20 74 68 65 20 56 44 42 45 20 73 68 6f 75 6c , the VDBE shoul
1722c 64 20 62 65 20 72 65 61 64 79 20 74 6f 20 62 65 d be ready to be
1722d 20 65 78 65 63 75 74 65 64 0a 2a 2a 20 61 67 61 executed.** aga
1722e 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f 6f in..**.** To loo
1722f 6b 20 61 74 20 69 74 20 61 6e 6f 74 68 65 72 20 k at it another
17230 77 61 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e way, this routin
17231 65 20 72 65 73 65 74 73 20 74 68 65 20 73 74 61 e resets the sta
17232 74 65 20 6f 66 20 74 68 65 0a 2a 2a 20 76 69 72 te of the.** vir
17233 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 72 6f tual machine fro
17234 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e m VDBE_MAGIC_RUN
17235 20 6f 72 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 or VDBE_MAGIC_H
17236 41 4c 54 20 62 61 63 6b 20 74 6f 0a 2a 2a 20 56 ALT back to.** V
17237 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 2e 0a DBE_MAGIC_INIT..
17238 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17239 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1723a 65 52 65 73 65 74 28 56 64 62 65 20 2a 70 29 7b eReset(Vdbe *p){
1723b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1723c 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 db = p->db;..
1723d 20 2f 2a 20 49 66 20 74 68 65 20 56 4d 20 64 69 /* If the VM di
1723e 64 20 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f 6d d not run to com
1723f 70 6c 65 74 69 6f 6e 20 6f 72 20 69 66 20 69 74 pletion or if it
17240 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 6e 0a encountered an.
17241 20 20 2a 2a 20 65 72 72 6f 72 2c 20 74 68 65 6e ** error, then
17242 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 it might not ha
17243 76 65 20 62 65 65 6e 20 68 61 6c 74 65 64 20 70 ve been halted p
17244 72 6f 70 65 72 6c 79 2e 20 20 53 6f 20 68 61 6c roperly. So hal
17245 74 0a 20 20 2a 2a 20 69 74 20 6e 6f 77 2e 0a 20 t. ** it now..
17246 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 */. (void)sqli
17247 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b te3SafetyOn(db);
17248 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 . sqlite3VdbeHa
17249 6c 74 28 70 29 3b 0a 20 20 28 76 6f 69 64 29 73 lt(p);. (void)s
1724a 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1724b 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 db);.. /* If th
1724c 65 20 56 44 42 45 20 68 61 73 20 62 65 20 72 75 e VDBE has be ru
1724d 6e 20 65 76 65 6e 20 70 61 72 74 69 61 6c 6c 79 n even partially
1724e 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 , then transfer
1724f 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0a 20 the error code.
17250 20 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 ** and error me
17251 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 56 ssage from the V
17252 44 42 45 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 DBE into the mai
17253 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 n database struc
17254 74 75 72 65 2e 20 20 42 75 74 0a 20 20 2a 2a 20 ture. But. **
17255 69 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20 if the VDBE has
17256 6a 75 73 74 20 62 65 65 6e 20 73 65 74 20 74 6f just been set to
17257 20 72 75 6e 20 62 75 74 20 68 61 73 20 6e 6f 74 run but has not
17258 20 61 63 74 75 61 6c 6c 79 20 65 78 65 63 75 74 actually execut
17259 65 64 20 61 6e 79 0a 20 20 2a 2a 20 69 6e 73 74 ed any. ** inst
1725a 72 75 63 74 69 6f 6e 73 20 79 65 74 2c 20 6c 65 ructions yet, le
1725b 61 76 65 20 74 68 65 20 6d 61 69 6e 20 64 61 74 ave the main dat
1725c 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 66 6f abase error info
1725d 72 6d 61 74 69 6f 6e 20 75 6e 63 68 61 6e 67 65 rmation unchange
1725e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d d.. */. if( p-
1725f 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 >pc>=0 ){. if
17260 28 20 70 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a ( p->zErrMsg ){.
17261 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 sqlite3Beg
17262 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
17263 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
17264 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 alueSetStr(db->p
17265 45 72 72 2c 2d 31 2c 70 2d 3e 7a 45 72 72 4d 73 Err,-1,p->zErrMs
17266 67 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51 g,SQLITE_UTF8,SQ
17267 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
17268 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e . sqlite3En
17269 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
1726a 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f . db->errCo
1726b 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20 de = p->rc;.
1726c 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1726d 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
1726e 0a 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 . p->zErrMs
1726f 67 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 g = 0;. }else
17270 20 69 66 28 20 70 2d 3e 72 63 20 29 7b 0a 20 20 if( p->rc ){.
17271 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
17272 28 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a (db, p->rc, 0);.
17273 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
17274 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 sqlite3Error(db
17275 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b , SQLITE_OK, 0);
17276 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
17277 66 28 20 70 2d 3e 72 63 20 26 26 20 70 2d 3e 65 f( p->rc && p->e
17278 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20 2f 2a xpired ){. /*
17279 20 54 68 65 20 65 78 70 69 72 65 64 20 66 6c 61 The expired fla
1727a 67 20 77 61 73 20 73 65 74 20 6f 6e 20 74 68 65 g was set on the
1727b 20 56 44 42 45 20 62 65 66 6f 72 65 20 74 68 65 VDBE before the
1727c 20 66 69 72 73 74 20 63 61 6c 6c 0a 20 20 20 20 first call.
1727d 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 74 ** to sqlite3_st
1727e 65 70 28 29 2e 20 46 6f 72 20 63 6f 6e 73 69 73 ep(). For consis
1727f 74 65 6e 63 79 20 28 73 69 6e 63 65 20 73 71 6c tency (since sql
17280 69 74 65 33 5f 73 74 65 70 28 29 20 77 61 73 0a ite3_step() was.
17281 20 20 20 20 2a 2a 20 63 61 6c 6c 65 64 29 2c 20 ** called),
17282 73 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 set the database
17283 20 65 72 72 6f 72 20 69 6e 20 74 68 69 73 20 63 error in this c
17284 61 73 65 20 61 73 20 77 65 6c 6c 2e 0a 20 20 20 ase as well..
17285 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 */. sqlite3E
17286 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c 20 rror(db, p->rc,
17287 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 0);. sqlite3V
17288 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 alueSetStr(db->p
17289 45 72 72 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 Err, -1, p->zErr
1728a 4d 73 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Msg, SQLITE_UTF8
1728b 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
1728c 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 NT);. sqlite3
1728d 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1728e 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
1728f 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a ErrMsg = 0;. }.
17290 0a 20 20 2f 2a 20 52 65 63 6c 61 69 6d 20 61 6c . /* Reclaim al
17291 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 l memory used by
17292 20 74 68 65 20 56 44 42 45 0a 20 20 2a 2f 0a 20 the VDBE. */.
17293 20 43 6c 65 61 6e 75 70 28 70 29 3b 0a 0a 20 20 Cleanup(p);..
17294 2f 2a 20 53 61 76 65 20 70 72 6f 66 69 6c 69 6e /* Save profilin
17295 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 g information fr
17296 6f 6d 20 74 68 69 73 20 56 44 42 45 20 72 75 6e om this VDBE run
17297 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 .. */.#ifdef VD
17298 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 BE_PROFILE. {.
17299 20 20 20 46 49 4c 45 20 2a 6f 75 74 20 3d 20 66 FILE *out = f
1729a 6f 70 65 6e 28 22 76 64 62 65 5f 70 72 6f 66 69 open("vdbe_profi
1729b 6c 65 2e 6f 75 74 22 2c 20 22 61 22 29 3b 0a 20 le.out", "a");.
1729c 20 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 if( out ){.
1729d 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
1729e 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2d fprintf(out, "-
1729f 2d 2d 2d 20 22 29 3b 0a 20 20 20 20 20 20 66 6f --- ");. fo
172a0 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
172a1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 i++){. f
172a2 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 30 32 printf(out, "%02
172a3 78 22 2c 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 x", p->aOp[i].op
172a4 63 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 code);. }.
172a5 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
172a6 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 20 20 66 , "\n");. f
172a7 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 or(i=0; i<p->nOp
172a8 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
172a9 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 36 fprintf(out, "%6
172aa 64 20 25 31 30 6c 6c 64 20 25 38 6c 6c 64 20 22 d %10lld %8lld "
172ab 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e ,. p->
172ac 61 4f 70 5b 69 5d 2e 63 6e 74 2c 0a 20 20 20 20 aOp[i].cnt,.
172ad 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d p->aOp[i]
172ae 2e 63 79 63 6c 65 73 2c 0a 20 20 20 20 20 20 20 .cycles,.
172af 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e p->aOp[i].cn
172b0 74 3e 30 20 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e t>0 ? p->aOp[i].
172b1 63 79 63 6c 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d cycles/p->aOp[i]
172b2 2e 63 6e 74 20 3a 20 30 0a 20 20 20 20 20 20 20 .cnt : 0.
172b3 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
172b4 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 6f te3VdbePrintOp(o
172b5 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 ut, i, &p->aOp[i
172b6 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ]);. }.
172b7 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 fclose(out);.
172b8 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
172b9 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
172ba 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 E_MAGIC_INIT;.
172bb 72 65 74 75 72 6e 20 70 2d 3e 72 63 20 26 20 64 return p->rc & d
172bc 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a b->errMask;.}. .
172bd 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 /*.** Clean up a
172be 6e 64 20 64 65 6c 65 74 65 20 61 20 56 44 42 45 nd delete a VDBE
172bf 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e after execution
172c0 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 . Return an int
172c1 65 67 65 72 20 77 68 69 63 68 20 69 73 0a 2a 2a eger which is.**
172c2 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 the result code
172c3 2e 20 20 57 72 69 74 65 20 61 6e 79 20 65 72 72 . Write any err
172c4 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 or message text
172c5 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a into *pzErrMsg..
172c6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
172c7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
172c8 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 20 2a eFinalize(Vdbe *
172c9 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 p){. int rc = S
172ca 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
172cb 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
172cc 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d AGIC_RUN || p->m
172cd 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
172ce 5f 48 41 4c 54 20 29 7b 0a 20 20 20 20 72 63 20 _HALT ){. rc
172cf 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 = sqlite3VdbeRes
172d0 65 74 28 70 29 3b 0a 20 20 20 20 61 73 73 65 72 et(p);. asser
172d1 74 28 20 28 72 63 20 26 20 70 2d 3e 64 62 2d 3e t( (rc & p->db->
172d2 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a errMask)==rc );.
172d3 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6d }else if( p->m
172d4 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 agic!=VDBE_MAGIC
172d5 5f 49 4e 49 54 20 29 7b 0a 20 20 20 20 72 65 74 _INIT ){. ret
172d6 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 urn SQLITE_MISUS
172d7 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 E;. }. sqlite3
172d8 56 64 62 65 44 65 6c 65 74 65 28 70 29 3b 0a 20 VdbeDelete(p);.
172d9 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
172da 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 64 65 *.** Call the de
172db 73 74 72 75 63 74 6f 72 20 66 6f 72 20 65 61 63 structor for eac
172dc 68 20 61 75 78 64 61 74 61 20 65 6e 74 72 79 20 h auxdata entry
172dd 69 6e 20 70 56 64 62 65 46 75 6e 63 20 66 6f 72 in pVdbeFunc for
172de 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 63 6f which.** the co
172df 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 rresponding bit
172e0 69 6e 20 6d 61 73 6b 20 69 73 20 63 6c 65 61 72 in mask is clear
172e1 2e 20 20 41 75 78 64 61 74 61 20 65 6e 74 72 69 . Auxdata entri
172e2 65 73 20 62 65 79 6f 6e 64 20 33 31 0a 2a 2a 20 es beyond 31.**
172e3 61 72 65 20 61 6c 77 61 79 73 20 64 65 73 74 72 are always destr
172e4 6f 79 65 64 2e 20 20 54 6f 20 64 65 73 74 72 6f oyed. To destro
172e5 79 20 61 6c 6c 20 61 75 78 64 61 74 61 20 65 6e y all auxdata en
172e6 74 72 69 65 73 2c 20 63 61 6c 6c 20 74 68 69 73 tries, call this
172e7 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 74 68 .** routine with
172e8 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c mask==0..*/.SQL
172e9 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
172ea 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
172eb 74 65 41 75 78 44 61 74 61 28 56 64 62 65 46 75 teAuxData(VdbeFu
172ec 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 2c 20 69 nc *pVdbeFunc, i
172ed 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 69 6e 74 20 nt mask){. int
172ee 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
172ef 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3b pVdbeFunc->nAux;
172f0 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 i++){. struc
172f1 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 78 20 t AuxData *pAux
172f2 3d 20 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 = &pVdbeFunc->ap
172f3 41 75 78 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 Aux[i];. if(
172f4 28 69 3e 33 31 20 7c 7c 20 21 28 6d 61 73 6b 26 (i>31 || !(mask&
172f5 28 31 3c 3c 69 29 29 29 20 26 26 20 70 41 75 78 (1<<i))) && pAux
172f6 2d 3e 70 41 75 78 20 29 7b 0a 20 20 20 20 20 20 ->pAux ){.
172f7 69 66 28 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 if( pAux->xDelet
172f8 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 41 75 e ){. pAu
172f9 78 2d 3e 78 44 65 6c 65 74 65 28 70 41 75 78 2d x->xDelete(pAux-
172fa 3e 70 41 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a >pAux);. }.
172fb 20 20 20 20 20 20 70 41 75 78 2d 3e 70 41 75 78 pAux->pAux
172fc 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
172fd 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
172fe 61 6e 20 65 6e 74 69 72 65 20 56 44 42 45 2e 0a an entire VDBE..
172ff 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17300 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
17301 62 65 44 65 6c 65 74 65 28 56 64 62 65 20 2a 70 beDelete(Vdbe *p
17302 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 ){. int i;. sq
17303 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 lite3 *db;.. if
17304 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( p==0 ) return;
17305 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 . db = p->db;.
17306 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 7b if( p->pPrev ){
17307 0a 20 20 20 20 70 2d 3e 70 50 72 65 76 2d 3e 70 . p->pPrev->p
17308 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b Next = p->pNext;
17309 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
1730a 73 65 72 74 28 20 64 62 2d 3e 70 56 64 62 65 3d sert( db->pVdbe=
1730b 3d 70 20 29 3b 0a 20 20 20 20 64 62 2d 3e 70 56 =p );. db->pV
1730c 64 62 65 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a dbe = p->pNext;.
1730d 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 }. if( p->pNe
1730e 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 xt ){. p->pNe
1730f 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 xt->pPrev = p->p
17310 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Prev;. }. if(
17311 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 4f 70 p->aOp ){. Op
17312 20 2a 70 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a *pOp = p->aOp;.
17313 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
17314 2d 3e 6e 4f 70 3b 20 69 2b 2b 2c 20 70 4f 70 2b ->nOp; i++, pOp+
17315 2b 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 +){. freeP4
17316 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 (db, pOp->p4type
17317 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 , pOp->p4.p);.#i
17318 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
17319 47 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 G. sqlite3D
1731a 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a bFree(db, pOp->z
1731b 43 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 Comment);.#endif
1731c 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 20 20 . }.
1731d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1731e 2c 20 70 2d 3e 61 4f 70 29 3b 0a 20 20 7d 0a 20 , p->aOp);. }.
1731f 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 releaseMemArray
17320 28 70 2d 3e 61 56 61 72 2c 20 70 2d 3e 6e 56 61 (p->aVar, p->nVa
17321 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 r);. sqlite3DbF
17322 72 65 65 28 64 62 2c 20 70 2d 3e 61 4c 61 62 65 ree(db, p->aLabe
17323 6c 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 l);. if( p->aMe
17324 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 m ){. sqlite3
17325 44 62 46 72 65 65 28 64 62 2c 20 26 70 2d 3e 61 DbFree(db, &p->a
17326 4d 65 6d 5b 31 5d 29 3b 0a 20 20 7d 0a 20 20 72 Mem[1]);. }. r
17327 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 eleaseMemArray(p
17328 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e ->aColName, p->n
17329 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d ResColumn*COLNAM
1732a 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 E_N);. sqlite3D
1732b 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f bFree(db, p->aCo
1732c 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 lName);. sqlite
1732d 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1732e 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 Sql);. p->magic
1732f 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 = VDBE_MAGIC_DE
17330 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 AD;. sqlite3DbF
17331 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f ree(db, p);.}../
17332 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f *.** If a MoveTo
17333 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 65 operation is pe
17334 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 76 nding on the giv
17335 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 en cursor, then
17336 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 do that.** MoveT
17337 6f 20 6e 6f 77 2e 20 20 52 65 74 75 72 6e 20 61 o now. Return a
17338 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 49 n error code. I
17339 66 20 6e 6f 20 4d 6f 76 65 54 6f 20 69 73 20 70 f no MoveTo is p
1733a 65 6e 64 69 6e 67 2c 20 74 68 69 73 0a 2a 2a 20 ending, this.**
1733b 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 routine does not
1733c 68 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 hing and returns
1733d 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 SQLITE_OK..*/.S
1733e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1733f 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 t sqlite3VdbeCur
17340 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 sorMoveto(VdbeCu
17341 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 rsor *p){. if(
17342 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 p->deferredMovet
17343 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 o ){. int res
17344 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c , rc;.#ifdef SQL
17345 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74 ITE_TEST. ext
17346 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f ern int sqlite3_
17347 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 search_count;.#e
17348 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 ndif. assert(
17349 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 p->isTable );.
1734a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1734b 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
1734c 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30 ed(p->pCursor, 0
1734d 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 , p->movetoTarge
1734e 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 t, 0, &res);.
1734f 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
17350 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74 rc;. p->last
17351 52 6f 77 69 64 20 3d 20 6b 65 79 54 6f 49 6e 74 Rowid = keyToInt
17352 28 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 (p->movetoTarget
17353 29 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 49 );. p->rowidI
17354 73 56 61 6c 69 64 20 3d 20 72 65 73 3d 3d 30 3b sValid = res==0;
17355 0a 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 29 . if( res<0 )
17356 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
17357 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 2d ite3BtreeNext(p-
17358 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b >pCursor, &res);
17359 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
1735a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1735b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1735c 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f EST. sqlite3_
1735d 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a search_count++;.
1735e 23 65 6e 64 69 66 0a 20 20 20 20 70 2d 3e 64 65 #endif. p->de
1735f 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 ferredMoveto = 0
17360 3b 0a 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 ;. p->cacheSt
17361 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
17362 4c 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 LE;. }else if(
17363 70 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 p->pCursor ){.
17364 20 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a int hasMoved;.
17365 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c int rc = sql
17366 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 ite3BtreeCursorH
17367 61 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 asMoved(p->pCurs
17368 6f 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a or, &hasMoved);.
17369 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
1736a 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 urn rc;. if(
1736b 68 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 hasMoved ){.
1736c 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 p->cacheStatus
1736d 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1736e 20 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 p->nullRow
1736f 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
17370 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17371 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 OK;.}../*.** The
17372 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 following funct
17373 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 ions:.**.** sqli
17374 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
17375 65 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 e().** sqlite3Vd
17376 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
17377 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 ).** sqlite3Vdbe
17378 53 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 SerialLen().** s
17379 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1737a 50 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 Put().** sqlite3
1737b 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a VdbeSerialGet().
1737c 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 **.** encapsulat
1737d 65 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 e the code that
1737e 73 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 serializes value
1737f 73 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e s for storage in
17380 20 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 SQLite.** data
17381 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 and index record
17382 73 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a s. Each serializ
17383 65 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 ed value consist
17384 73 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 s of a.** 'seria
17385 6c 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c l-type' and a bl
17386 6f 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 ob of data. The
17387 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 serial type is a
17388 6e 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 n 8-byte unsigne
17389 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 d.** integer, st
1738a 6f 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 ored as a varint
1738b 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 ..**.** In an SQ
1738c 4c 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 Lite index recor
1738d 64 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 d, the serial ty
1738e 70 65 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 pe is stored dir
1738f 65 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 ectly before.**
17390 74 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 the blob of data
17391 20 74 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 that it corresp
17392 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 onds to. In a ta
17393 62 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 ble record, all
17394 73 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 serial.** types
17395 61 72 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 are stored at th
17396 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 e start of the r
17397 65 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 ecord, and the b
17398 6c 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a lobs of data at.
17399 2a 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 ** the end. Henc
1739a 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e e these function
1739b 73 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c s allow the call
1739c 65 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 er to handle the
1739d 0a 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 .** serial-type
1739e 61 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 and data blob se
1739f 70 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 perately..**.**
173a0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 The following ta
173a1 62 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 ble describes th
173a2 65 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 e various storag
173a3 65 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 e classes for da
173a4 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 ta:.**.** seri
173a5 61 6c 20 74 79 70 65 20 20 20 20 20 20 20 20 62 al type b
173a6 79 74 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 ytes of data
173a7 20 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d type.** ----
173a8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d ---------- -
173a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 --------------
173aa 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
173ab 2d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 -.** 0
173ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
173ad 30 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 0 NUL
173ae 4c 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 L.** 1
173af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
173b0 31 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 1 sig
173b1 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 ned integer.**
173b2 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 2
173b3 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 2
173b4 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e signed in
173b5 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 teger.** 3
173b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
173b7 20 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 3
173b8 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a signed integer.
173b9 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20 ** 4
173ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20 4
173bb 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 signe
173bc 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 d integer.**
173bd 20 20 35 20 20 20 20 20 20 20 20 20 20 20 20 20 5
173be 20 20 20 20 20 20 20 20 36 20 20 20 20 20 20 20 6
173bf 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 signed inte
173c0 67 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 ger.** 6
173c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
173c2 20 20 38 20 20 20 20 20 20 20 20 20 20 20 20 73 8 s
173c3 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
173c4 20 20 20 20 20 20 37 20 20 20 20 20 20 20 20 20 7
173c5 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 8
173c6 20 20 20 20 20 20 20 20 20 49 45 45 45 20 66 6c IEEE fl
173c7 6f 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 oat.** 8
173c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
173c9 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49 0 I
173ca 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 nteger constant
173cb 30 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 0.** 9
173cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
173cd 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 0 Int
173ce 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a eger constant 1.
173cf 2a 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 ** 10,11
173d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
173d1 20 20 20 20 20 20 20 20 20 20 20 72 65 73 65 72 reser
173d2 76 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f ved for expansio
173d3 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e n.** N>=12 an
173d4 64 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d d even (N-
173d5 31 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 12)/2 BLO
173d6 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e B.** N>=13 an
173d7 64 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d d odd (N-
173d8 31 33 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 13)/2 tex
173d9 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e t.**.** The 8 an
173da 64 20 39 20 74 79 70 65 73 20 77 65 72 65 20 61 d 9 types were a
173db 64 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 dded in 3.3.0, f
173dc 69 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 ile format 4. P
173dd 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a rior versions.**
173de 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 of SQLite will
173df 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 not understand t
173e0 68 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 hose serial type
173e1 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 s..*/../*.** Ret
173e2 75 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 urn the serial-t
173e3 79 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 ype for the valu
173e4 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d e stored in pMem
173e5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
173e6 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 ATE u32 sqlite3V
173e7 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 dbeSerialType(Me
173e8 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c m *pMem, int fil
173e9 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 e_format){. int
173ea 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 flags = pMem->f
173eb 6c 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a lags;. int n;..
173ec 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f if( flags&MEM_
173ed 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 Null ){. retu
173ee 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
173ef 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b flags&MEM_Int ){
173f0 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f . /* Figure o
173f1 75 74 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 ut whether to us
173f2 65 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 e 1, 2, 4, 6 or
173f3 38 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 8 bytes. */.#
173f4 64 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 define MAX_6BYTE
173f5 20 28 28 28 28 69 36 34 29 30 78 30 30 30 30 38 ((((i64)0x00008
173f6 30 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 000)<<32)-1).
173f7 20 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 i64 i = pMem->u
173f8 2e 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 .i;. u64 u;.
173f9 20 20 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d if( file_form
173fa 61 74 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d at>=4 && (i&1)==
173fb 69 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 i ){. retur
173fc 6e 20 38 2b 69 3b 0a 20 20 20 20 7d 0a 20 20 20 n 8+i;. }.
173fd 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 20 3a 20 u = i<0 ? -i :
173fe 69 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 31 32 i;. if( u<=12
173ff 37 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7 ) return 1;.
17400 20 20 69 66 28 20 75 3c 3d 33 32 37 36 37 20 29 if( u<=32767 )
17401 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 69 return 2;. i
17402 66 28 20 75 3c 3d 38 33 38 38 36 30 37 20 29 20 f( u<=8388607 )
17403 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 69 66 return 3;. if
17404 28 20 75 3c 3d 32 31 34 37 34 38 33 36 34 37 20 ( u<=2147483647
17405 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 ) return 4;.
17406 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 59 54 45 if( u<=MAX_6BYTE
17407 20 29 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 20 ) return 5;.
17408 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 20 return 6;. }.
17409 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 52 if( flags&MEM_R
1740a 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 eal ){. retur
1740b 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 n 7;. }. asser
1740c 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 61 6c t( pMem->db->mal
1740d 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66 6c 61 locFailed || fla
1740e 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f gs&(MEM_Str|MEM_
1740f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 3d 20 70 Blob) );. n = p
17410 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20 66 6c Mem->n;. if( fl
17411 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
17412 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d 65 6d 2d {. n += pMem-
17413 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 >u.i;. }. asse
17414 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65 rt( n>=0 );. re
17415 74 75 72 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32 turn ((n*2) + 12
17416 20 2b 20 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53 + ((flags&MEM_S
17417 74 72 29 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a tr)!=0));.}../*.
17418 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 ** Return the le
17419 6e 67 74 68 20 6f 66 20 74 68 65 20 64 61 74 61 ngth of the data
1741a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1741b 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 o the supplied s
1741c 65 72 69 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53 erial-type..*/.S
1741d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1741e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 t sqlite3VdbeSer
1741f 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 20 73 ialTypeLen(u32 s
17420 65 72 69 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69 erial_type){. i
17421 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d f( serial_type>=
17422 31 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 12 ){. return
17423 20 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 (serial_type-12
17424 29 2f 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 )/2;. }else{.
17425 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
17426 38 20 61 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c 8 aSize[] = { 0,
17427 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20 1, 2, 3, 4, 6,
17428 38 2c 20 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30 8, 8, 0, 0, 0, 0
17429 20 7d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 };. return a
1742a 53 69 7a 65 5b 73 65 72 69 61 6c 5f 74 79 70 65 Size[serial_type
1742b 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ];. }.}../*.**
1742c 49 66 20 77 65 20 61 72 65 20 6f 6e 20 61 6e 20 If we are on an
1742d 61 72 63 68 69 74 65 63 74 75 72 65 20 77 69 74 architecture wit
1742e 68 20 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66 h mixed-endian f
1742f 6c 6f 61 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e loating .** poin
17430 74 73 20 28 65 78 3a 20 41 52 4d 37 29 20 74 68 ts (ex: ARM7) th
17431 65 6e 20 73 77 61 70 20 74 68 65 20 6c 6f 77 65 en swap the lowe
17432 72 20 34 20 62 79 74 65 73 20 77 69 74 68 20 74 r 4 bytes with t
17433 68 65 20 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 he .** upper 4 b
17434 79 74 65 73 2e 20 20 52 65 74 75 72 6e 20 74 68 ytes. Return th
17435 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 e result..**.**
17436 46 6f 72 20 6d 6f 73 74 20 61 72 63 68 69 74 65 For most archite
17437 63 74 75 72 65 73 2c 20 74 68 69 73 20 69 73 20 ctures, this is
17438 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 a no-op..**.** (
17439 6c 61 74 65 72 29 3a 20 20 49 74 20 69 73 20 72 later): It is r
1743a 65 70 6f 72 74 65 64 20 74 6f 20 6d 65 20 74 68 eported to me th
1743b 61 74 20 74 68 65 20 6d 69 78 65 64 2d 65 6e 64 at the mixed-end
1743c 69 61 6e 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f ian problem.** o
1743d 6e 20 41 52 4d 37 20 69 73 20 61 6e 20 69 73 73 n ARM7 is an iss
1743e 75 65 20 77 69 74 68 20 47 43 43 2c 20 6e 6f 74 ue with GCC, not
1743f 20 77 69 74 68 20 74 68 65 20 41 52 4d 37 20 63 with the ARM7 c
17440 68 69 70 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a hip. It seems.*
17441 2a 20 74 68 61 74 20 65 61 72 6c 79 20 76 65 72 * that early ver
17442 73 69 6f 6e 73 20 6f 66 20 47 43 43 20 73 74 6f sions of GCC sto
17443 72 65 64 20 74 68 65 20 74 77 6f 20 77 6f 72 64 red the two word
17444 73 20 6f 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a s of a 64-bit.**
17445 20 66 6c 6f 61 74 20 69 6e 20 74 68 65 20 77 72 float in the wr
17446 6f 6e 67 20 6f 72 64 65 72 2e 20 20 41 6e 64 20 ong order. And
17447 74 68 61 74 20 65 72 72 6f 72 20 68 61 73 20 62 that error has b
17448 65 65 6e 20 70 72 6f 70 61 67 61 74 65 64 0a 2a een propagated.*
17449 2a 20 65 76 65 72 20 73 69 6e 63 65 2e 20 20 54 * ever since. T
1744a 68 65 20 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20 he blame is not
1744b 6e 65 63 65 73 73 61 72 69 6c 79 20 77 69 74 68 necessarily with
1744c 20 47 43 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a GCC, though..**
1744d 20 47 43 43 20 6d 69 67 68 74 20 68 61 76 65 20 GCC might have
1744e 6a 75 73 74 20 63 6f 70 79 69 6e 67 20 74 68 65 just copying the
1744f 20 70 72 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20 problem from a
17450 70 72 69 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a prior compiler..
17451 2a 2a 20 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c ** I am also tol
17452 64 20 74 68 61 74 20 6e 65 77 65 72 20 76 65 72 d that newer ver
17453 73 69 6f 6e 73 20 6f 66 20 47 43 43 20 74 68 61 sions of GCC tha
17454 74 20 66 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65 t follow a diffe
17455 72 65 6e 74 0a 2a 2a 20 41 42 49 20 67 65 74 20 rent.** ABI get
17456 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 20 72 the byte order r
17457 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65 ight..**.** Deve
17458 6c 6f 70 65 72 73 20 75 73 69 6e 67 20 53 51 4c lopers using SQL
17459 69 74 65 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73 ite on an ARM7 s
1745a 68 6f 75 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e hould compile an
1745b 64 20 72 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61 d run their.** a
1745c 70 70 6c 69 63 61 74 69 6f 6e 20 75 73 69 6e 67 pplication using
1745d 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d -DSQLITE_DEBUG=
1745e 31 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 1 at least once.
1745f 20 20 57 69 74 68 20 44 45 42 55 47 0a 2a 2a 20 With DEBUG.**
17460 65 6e 61 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73 enabled, some as
17461 73 65 72 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c serts below will
17462 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 ensure that the
17463 20 62 79 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a byte order of.*
17464 2a 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 * floating point
17465 20 76 61 6c 75 65 73 20 69 73 20 63 6f 72 72 65 values is corre
17466 63 74 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d ct..**.** (2007-
17467 30 38 2d 33 30 29 20 20 46 72 61 6e 6b 20 76 61 08-30) Frank va
17468 6e 20 56 75 67 74 20 68 61 73 20 73 74 75 64 69 n Vugt has studi
17469 65 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ed this problem
1746a 63 6c 6f 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68 closely.** and h
1746b 61 73 20 73 65 6e 64 20 68 69 73 20 66 69 6e 64 as send his find
1746c 69 6e 67 73 20 74 6f 20 74 68 65 20 53 51 4c 69 ings to the SQLi
1746d 74 65 20 64 65 76 65 6c 6f 70 65 72 73 2e 20 20 te developers.
1746e 46 72 61 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20 Frank.** writes
1746f 74 68 61 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20 that some Linux
17470 6b 65 72 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c kernels offer fl
17471 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 68 61 72 oating point har
17472 64 77 61 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69 dware.** emulati
17473 6f 6e 20 74 68 61 74 20 75 73 65 73 20 6f 6e 6c on that uses onl
17474 79 20 33 32 2d 62 69 74 20 6d 61 6e 74 69 73 73 y 32-bit mantiss
17475 61 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 as instead of a
17476 66 75 6c 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73 full .** 48-bits
17477 20 61 73 20 72 65 71 75 69 72 65 64 20 62 79 20 as required by
17478 74 68 65 20 49 45 45 45 20 73 74 61 6e 64 61 72 the IEEE standar
17479 64 2e 20 20 28 54 68 69 73 20 69 73 20 74 68 65 d. (This is the
1747a 0a 2a 2a 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46 .** CONFIG_FPE_F
1747b 41 53 54 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20 ASTFPE option.)
1747c 20 4f 6e 20 73 75 63 68 20 73 79 73 74 65 6d 73 On such systems
1747d 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 , floating point
1747e 0a 2a 2a 20 62 79 74 65 20 73 77 61 70 70 69 6e .** byte swappin
1747f 67 20 62 65 63 6f 6d 65 73 20 76 65 72 79 20 63 g becomes very c
17480 6f 6d 70 6c 69 63 61 74 65 64 2e 20 20 54 6f 20 omplicated. To
17481 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a avoid problems,.
17482 2a 2a 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 ** the necessary
17483 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20 69 byte swapping i
17484 73 20 63 61 72 72 69 65 64 20 6f 75 74 20 75 73 s carried out us
17485 69 6e 67 20 61 20 36 34 2d 62 69 74 20 69 6e 74 ing a 64-bit int
17486 65 67 65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74 eger.** rather t
17487 68 61 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f han a 64-bit flo
17488 61 74 2e 20 20 46 72 61 6e 6b 20 61 73 73 75 72 at. Frank assur
17489 65 73 20 75 73 20 74 68 61 74 20 74 68 65 20 63 es us that the c
1748a 6f 64 65 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b ode here.** work
1748b 73 20 66 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20 s for him. We,
1748c 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 2c 20 the developers,
1748d 68 61 76 65 20 6e 6f 20 77 61 79 20 74 6f 20 69 have no way to i
1748e 6e 64 65 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20 ndependently.**
1748f 76 65 72 69 66 79 20 74 68 69 73 2c 20 62 75 74 verify this, but
17490 20 46 72 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20 Frank seems to
17491 6b 6e 6f 77 20 77 68 61 74 20 68 65 20 69 73 20 know what he is
17492 74 61 6c 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a talking about.**
17493 20 73 6f 20 77 65 20 74 72 75 73 74 20 68 69 6d so we trust him
17494 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
17495 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f TE_MIXED_ENDIAN_
17496 36 34 42 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74 64BIT_FLOAT.stat
17497 69 63 20 75 36 34 20 66 6c 6f 61 74 53 77 61 70 ic u64 floatSwap
17498 28 75 36 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f (u64 in){. unio
17499 6e 20 7b 0a 20 20 20 20 75 36 34 20 72 3b 0a 20 n {. u64 r;.
1749a 20 20 20 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d u32 i[2];. }
1749b 20 75 3b 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 u;. u32 t;..
1749c 75 2e 72 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 u.r = in;. t =
1749d 75 2e 69 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d u.i[0];. u.i[0]
1749e 20 3d 20 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 = u.i[1];. u.i
1749f 5b 31 5d 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 [1] = t;. retur
174a0 6e 20 75 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e n u.r;.}.# defin
174a1 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 e swapMixedEndia
174a2 6e 46 6c 6f 61 74 28 58 29 20 20 58 20 3d 20 66 nFloat(X) X = f
174a3 6c 6f 61 74 53 77 61 70 28 58 29 0a 23 65 6c 73 loatSwap(X).#els
174a4 65 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d e.# define swapM
174a5 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 ixedEndianFloat(
174a6 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a X).#endif../*.**
174a7 20 57 72 69 74 65 20 74 68 65 20 73 65 72 69 61 Write the seria
174a8 6c 69 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20 lized data blob
174a9 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 for the value st
174aa 6f 72 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 ored in pMem int
174ab 6f 20 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 o .** buf. It is
174ac 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
174ad 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c e caller has all
174ae 6f 63 61 74 65 64 20 73 75 66 66 69 63 69 65 6e ocated sufficien
174af 74 20 73 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 t space..** Retu
174b0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
174b1 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0a bytes written..
174b2 2a 2a 0a 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 **.** nBuf is th
174b3 65 20 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 e amount of spac
174b4 65 20 6c 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e e left in buf[].
174b5 20 20 6e 42 75 66 20 6d 75 73 74 20 61 6c 77 61 nBuf must alwa
174b6 79 73 20 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 ys be.** large e
174b7 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 nough to hold th
174b8 65 20 65 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 e entire field.
174b9 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 Except, if the
174ba 66 69 65 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c field is.** a bl
174bb 6f 62 20 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 ob with a zero-f
174bc 69 6c 6c 65 64 20 74 61 69 6c 2c 20 74 68 65 6e illed tail, then
174bd 20 62 75 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 buf[] might be
174be 6a 75 73 74 20 74 68 65 20 72 69 67 68 74 0a 2a just the right.*
174bf 2a 20 73 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 * size to hold e
174c0 76 65 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 verything except
174c1 20 66 6f 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 for the zero-fi
174c2 6c 6c 65 64 20 74 61 69 6c 2e 20 20 49 66 20 62 lled tail. If b
174c3 75 66 5b 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 uf[].** is only
174c4 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f big enough to ho
174c5 6c 64 20 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 ld the non-zero
174c6 70 72 65 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c prefix, then onl
174c7 79 20 77 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 y write that.**
174c8 70 72 65 66 69 78 20 69 6e 74 6f 20 62 75 66 5b prefix into buf[
174c9 5d 2e 20 20 42 75 74 20 69 66 20 62 75 66 5b 5d ]. But if buf[]
174ca 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 is large enough
174cb 20 74 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 to hold both th
174cc 65 0a 2a 2a 20 70 72 65 66 69 78 20 61 6e 64 20 e.** prefix and
174cd 74 68 65 20 74 61 69 6c 20 74 68 65 6e 20 77 72 the tail then wr
174ce 69 74 65 20 74 68 65 20 70 72 65 66 69 78 20 61 ite the prefix a
174cf 6e 64 20 73 65 74 20 74 68 65 20 74 61 69 6c 20 nd set the tail
174d0 74 6f 20 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e to all.** zeros.
174d1 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
174d2 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
174d3 73 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 s actually writt
174d4 65 6e 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 en into buf[].
174d5 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 The number.** of
174d6 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65 bytes in the ze
174d7 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 ro-filled tail i
174d8 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 s included in th
174d9 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f e return value o
174da 6e 6c 79 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 nly.** if those
174db 62 79 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65 bytes were zeroe
174dc 64 20 69 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a d in buf[]..*/ .
174dd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
174de 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 nt sqlite3VdbeSe
174df 72 69 61 6c 50 75 74 28 75 38 20 2a 62 75 66 2c rialPut(u8 *buf,
174e0 20 69 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a int nBuf, Mem *
174e1 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 pMem, int file_f
174e2 6f 72 6d 61 74 29 7b 0a 20 20 75 33 32 20 73 65 ormat){. u32 se
174e3 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 rial_type = sqli
174e4 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
174e5 65 28 70 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 e(pMem, file_for
174e6 6d 61 74 29 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b mat);. int len;
174e7 0a 0a 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 .. /* Integer a
174e8 6e 64 20 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 nd Real */. if(
174e9 20 73 65 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 serial_type<=7
174ea 26 26 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 && serial_type>0
174eb 20 29 7b 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 ){. u64 v;.
174ec 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 int i;. if
174ed 28 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 ( serial_type==7
174ee 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
174ef 28 20 73 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a ( sizeof(v)==siz
174f0 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a eof(pMem->r) );.
174f1 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 76 2c memcpy(&v,
174f2 20 26 70 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f &pMem->r, sizeo
174f3 66 28 76 29 29 3b 0a 20 20 20 20 20 20 73 77 61 f(v));. swa
174f4 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 pMixedEndianFloa
174f5 74 28 76 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b t(v);. }else{
174f6 0a 20 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d . v = pMem-
174f7 3e 75 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 >u.i;. }.
174f8 6c 65 6e 20 3d 20 69 20 3d 20 73 71 6c 69 74 65 len = i = sqlite
174f9 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 3VdbeSerialTypeL
174fa 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b en(serial_type);
174fb 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e . assert( len
174fc 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68 <=nBuf );. wh
174fd 69 6c 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 ile( i-- ){.
174fe 20 20 62 75 66 5b 69 5d 20 3d 20 28 76 26 30 78 buf[i] = (v&0x
174ff 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d FF);. v >>=
17500 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 8;. }. re
17501 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 turn len;. }..
17502 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c /* String or bl
17503 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 ob */. if( seri
17504 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 al_type>=12 ){.
17505 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
17506 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 >n + ((pMem->fla
17507 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 gs & MEM_Zero)?p
17508 4d 65 6d 2d 3e 75 2e 69 3a 30 29 0a 20 20 20 20 Mem->u.i:0).
17509 20 20 20 20 20 20 20 20 20 3d 3d 20 73 71 6c 69 == sqli
1750a 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1750b 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 eLen(serial_type
1750c 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ) );. assert(
1750d 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 pMem->n<=nBuf )
1750e 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d ;. len = pMem
1750f 2d 3e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 ->n;. memcpy(
17510 62 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 buf, pMem->z, le
17511 6e 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d n);. if( pMem
17512 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 ->flags & MEM_Ze
17513 72 6f 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20 ro ){. len
17514 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 += pMem->u.i;.
17515 20 20 20 20 69 66 28 20 6c 65 6e 3e 6e 42 75 66 if( len>nBuf
17516 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 ){. len
17517 3d 20 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d 0a = nBuf;. }.
17518 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62 75 memset(&bu
17519 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 6c f[pMem->n], 0, l
1751a 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 en-pMem->n);.
1751b 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65 }. return le
1751c 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55 4c n;. }.. /* NUL
1751d 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20 30 L or constants 0
1751e 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75 72 or 1 */. retur
1751f 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 n 0;.}../*.** De
17520 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 64 61 serialize the da
17521 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64 20 ta blob pointed
17522 74 6f 20 62 79 20 62 75 66 20 61 73 20 73 65 72 to by buf as ser
17523 69 61 6c 20 74 79 70 65 20 73 65 72 69 61 6c 5f ial type serial_
17524 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 type.** and stor
17525 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 e the result in
17526 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 74 68 pMem. Return th
17527 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
17528 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c 49 s read..*/ .SQLI
17529 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1752a 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1752b 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 Get(. const uns
1752c 69 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66 2c igned char *buf,
1752d 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 /* Buffer t
1752e 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66 72 o deserialize fr
1752f 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 om */. u32 seri
17530 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20 20 al_type,
17531 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 /* Serial
17532 74 79 70 65 20 74 6f 20 64 65 73 65 72 69 61 6c type to deserial
17533 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d ize */. Mem *pM
17534 65 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 em
17535 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
17536 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20 76 cell to write v
17537 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b 0a alue into */.){.
17538 20 20 73 77 69 74 63 68 28 20 73 65 72 69 61 6c switch( serial
17539 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 _type ){. cas
1753a 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 65 72 e 10: /* Reser
1753b 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 ved for future u
1753c 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 se */. case 1
1753d 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65 64 1: /* Reserved
1753e 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 20 for future use
1753f 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 3a 20 7b */. case 0: {
17540 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 /* NULL */.
17541 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
17542 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 MEM_Null;.
17543 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
17544 20 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20 31 case 1: { /* 1
17545 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 -byte signed int
17546 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d eger */. pM
17547 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e 65 em->u.i = (signe
17548 64 20 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a 20 d char)buf[0];.
17549 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1754a 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1754b 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
1754c 7d 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b 20 }. case 2: {
1754d 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 2-byte signed
1754e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 integer */.
1754f 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 pMem->u.i = ((
17550 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 (signed char)buf
17551 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 [0])<<8) | buf[1
17552 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 ];. pMem->f
17553 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
17554 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a return 2;.
17555 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 33 }. case 3
17556 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73 69 : { /* 3-byte si
17557 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
17558 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 pMem->u.i
17559 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 = (((signed char
1755a 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c 20 )buf[0])<<16) |
1755b 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62 75 (buf[1]<<8) | bu
1755c 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d f[2];. pMem
1755d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1755e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 t;. return
1755f 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 3;. }. cas
17560 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74 65 e 4: { /* 4-byte
17561 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
17562 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 */. pMem->u
17563 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 .i = (buf[0]<<24
17564 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 ) | (buf[1]<<16)
17565 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c | (buf[2]<<8) |
17566 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 70 buf[3];. p
17567 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
17568 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
17569 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 4;. }.
1756a 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d 62 case 5: { /* 6-b
1756b 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
1756c 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 er */. u64
1756d 78 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 x = (((signed ch
1756e 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c ar)buf[0])<<8) |
1756f 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 75 buf[1];. u
17570 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c 3c 32 y = (buf[2]<<
17571 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c 31 24) | (buf[3]<<1
17572 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38 29 6) | (buf[4]<<8)
17573 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20 20 | buf[5];.
17574 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 79 x = (x<<32) | y
17575 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e ;. pMem->u.
17576 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 i = *(i64*)&x;.
17577 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
17578 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
17579 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 20 20 return 6;.
1757a 7d 0a 20 20 20 20 63 61 73 65 20 36 3a 20 20 20 }. case 6:
1757b 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 8-byte signed
1757c 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 integer */.
1757d 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45 45 case 7: { /* IEE
1757e 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 E floating point
1757f 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78 3b */. u64 x;
17580 0a 20 20 20 20 20 20 75 33 32 20 79 3b 0a 23 69 . u32 y;.#i
17581 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 f !defined(NDEBU
17582 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 G) && !defined(S
17583 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
17584 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20 20 ING_POINT).
17585 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 /* Verify that
17586 69 6e 74 65 67 65 72 73 20 61 6e 64 20 66 6c 6f integers and flo
17587 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
17588 65 73 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a es use the same.
17589 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 6f 72 ** byte or
1758a 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 20 69 der. Or, that i
1758b 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 f SQLITE_MIXED_E
1758c 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 NDIAN_64BIT_FLOA
1758d 54 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65 T is. ** de
1758e 66 69 6e 65 64 20 74 68 61 74 20 36 34 2d 62 69 fined that 64-bi
1758f 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 t floating point
17590 20 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 20 61 values really a
17591 72 65 20 6d 69 78 65 64 0a 20 20 20 20 20 20 2a re mixed. *
17592 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20 20 * endian..
17593 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 */. static
17594 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20 28 const u64 t1 = (
17595 28 75 36 34 29 30 78 33 66 66 30 30 30 30 30 29 (u64)0x3ff00000)
17596 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 61 74 <<32;. stat
17597 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65 20 ic const double
17598 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 r1 = 1.0;.
17599 75 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 20 20 u64 t2 = t1;.
1759a 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 swapMixedEndi
1759b 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20 20 anFloat(t2);.
1759c 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
1759d 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74 32 f(r1)==sizeof(t2
1759e 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 31 2c ) && memcmp(&r1,
1759f 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 31 29 &t2, sizeof(r1)
175a0 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a )==0 );.#endif..
175a1 20 20 20 20 20 20 78 20 3d 20 28 62 75 66 5b 30 x = (buf[0
175a2 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d ]<<24) | (buf[1]
175a3 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c <<16) | (buf[2]<
175a4 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 <8) | buf[3];.
175a5 20 20 20 20 79 20 3d 20 28 62 75 66 5b 34 5d 3c y = (buf[4]<
175a6 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c 3c <24) | (buf[5]<<
175a7 31 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c 38 16) | (buf[6]<<8
175a8 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20 20 ) | buf[7];.
175a9 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 x = (x<<32) |
175aa 79 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 y;. if( ser
175ab 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a 20 ial_type==6 ){.
175ac 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 pMem->u.i
175ad 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 20 = *(i64*)&x;.
175ae 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
175af 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
175b0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
175b1 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
175b2 28 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f 66 (x)==8 && sizeof
175b3 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b 0a (pMem->r)==8 );.
175b4 20 20 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 swapMixe
175b5 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29 3b dEndianFloat(x);
175b6 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
175b7 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73 69 &pMem->r, &x, si
175b8 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 20 20 zeof(x));.
175b9 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
175ba 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d 65 sqlite3IsNaN(pMe
175bb 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c 6c m->r) ? MEM_Null
175bc 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 : MEM_Real;.
175bd 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
175be 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 n 8;. }. c
175bf 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 6e 74 ase 8: /* Int
175c0 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 63 61 eger 0 */. ca
175c1 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e 74 65 se 9: { /* Inte
175c2 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 20 70 ger 1 */. p
175c3 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 69 61 Mem->u.i = seria
175c4 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 20 20 l_type-8;.
175c5 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
175c6 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 M_Int;. ret
175c7 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 0;. }.
175c8 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
175c9 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 73 65 72 int len = (ser
175ca 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a ial_type-12)/2;.
175cb 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 pMem->z =
175cc 28 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 20 20 (char *)buf;.
175cd 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65 6e pMem->n = len
175ce 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 44 ;. pMem->xD
175cf 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 el = 0;. if
175d0 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30 78 ( serial_type&0x
175d1 30 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 01 ){. pM
175d2 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
175d3 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b Str | MEM_Ephem;
175d4 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
175d5 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
175d6 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d s = MEM_Blob | M
175d7 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 EM_Ephem;.
175d8 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 6c }. return l
175d9 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 en;. }. }.
175da 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a return 0;.}.../*
175db 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 4b .** Given the nK
175dc 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 6e 67 ey-byte encoding
175dd 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 6e 20 of a record in
175de 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 74 68 pKey[], parse th
175df 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 74 6f e.** record into
175e0 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 a UnpackedRecor
175e1 64 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 d structure. Re
175e2 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
175e3 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 63 74 o.** that struct
175e4 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ure..**.** The c
175e5 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
175e6 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20 73 7a might provide sz
175e7 53 70 61 63 65 20 62 79 74 65 73 20 6f 66 20 6d Space bytes of m
175e8 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 61 emory.** space a
175e9 74 20 70 53 70 61 63 65 2e 20 20 54 68 69 73 20 t pSpace. This
175ea 73 70 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 space can be use
175eb 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 d to hold the re
175ec 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 50 61 turned.** VDbePa
175ed 72 73 65 64 52 65 63 6f 72 64 20 73 74 72 75 63 rsedRecord struc
175ee 74 75 72 65 20 69 66 20 69 74 20 69 73 20 6c 61 ture if it is la
175ef 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 66 20 rge enough. If
175f0 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 69 67 it is.** not big
175f1 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 20 69 enough, space i
175f2 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
175f3 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
175f4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
175f5 72 6e 65 64 20 73 74 72 75 63 74 75 72 65 20 73 rned structure s
175f6 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 hould be closed
175f7 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 by a call to.**
175f8 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
175f9 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 eUnpackedRecord(
175fa 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 )..*/ .SQLITE_PR
175fb 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52 65 IVATE UnpackedRe
175fc 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64 62 cord *sqlite3Vdb
175fd 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 0a 20 eRecordUnpack(.
175fe 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
175ff 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 fo, /* Infor
17600 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 mation about the
17601 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 2a record format *
17602 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 /. int nKey,
17603 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
17604 7a 65 20 6f 66 20 74 68 65 20 62 69 6e 61 72 79 ze of the binary
17605 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e record */. con
17606 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 st void *pKey,
17607 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61 72 /* The binar
17608 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 55 6e y record */. Un
17609 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 53 packedRecord *pS
1760a 70 61 63 65 2c 2f 2a 20 53 70 61 63 65 20 61 76 pace,/* Space av
1760b 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 ailable to hold
1760c 72 65 73 75 6c 74 69 6e 67 20 6f 62 6a 65 63 74 resulting object
1760d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 70 61 63 */. int szSpac
1760e 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e /*
1760f 53 69 7a 65 20 6f 66 20 70 53 70 61 63 65 5b 5d Size of pSpace[]
17610 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a in bytes */.){.
17611 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
17612 20 63 68 61 72 20 2a 61 4b 65 79 20 3d 20 28 63 char *aKey = (c
17613 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
17614 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 55 6e 70 ar *)pKey;. Unp
17615 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 3b 0a ackedRecord *p;.
17616 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 int nByte;. i
17617 6e 74 20 69 64 78 2c 20 64 3b 0a 20 20 75 31 36 nt idx, d;. u16
17618 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 u;
17619 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 /* Unsigned
1761a 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a loop counter */.
1761b 20 20 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d u32 szHdr;. M
1761c 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 0a 20 20 61 em *pMem;. . a
1761d 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65 ssert( sizeof(Me
1761e 6d 29 3e 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b m)>sizeof(*p) );
1761f 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f . nByte = sizeo
17620 66 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f f(Mem)*(pKeyInfo
17621 2d 3e 6e 46 69 65 6c 64 2b 32 29 3b 0a 20 20 69 ->nField+2);. i
17622 66 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65 f( nByte>szSpace
17623 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 ){. p = sqli
17624 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
17625 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 KeyInfo->db, nBy
17626 74 65 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d te);. if( p==
17627 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
17628 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 p->flags = UNP
17629 41 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 ACKED_NEED_FREE
1762a 7c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f | UNPACKED_NEED_
1762b 44 45 53 54 52 4f 59 3b 0a 20 20 7d 65 6c 73 65 DESTROY;. }else
1762c 7b 0a 20 20 20 20 70 20 3d 20 70 53 70 61 63 65 {. p = pSpace
1762d 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d ;. p->flags =
1762e 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 UNPACKED_NEED_D
1762f 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 2d ESTROY;. }. p-
17630 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 >pKeyInfo = pKey
17631 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c Info;. p->nFiel
17632 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 d = pKeyInfo->nF
17633 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 2d 3e 61 ield + 1;. p->a
17634 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 28 28 Mem = pMem = &((
17635 4d 65 6d 2a 29 70 29 5b 31 5d 3b 0a 20 20 69 64 Mem*)p)[1];. id
17636 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 x = getVarint32(
17637 61 4b 65 79 2c 20 73 7a 48 64 72 29 3b 0a 20 20 aKey, szHdr);.
17638 64 20 3d 20 73 7a 48 64 72 3b 0a 20 20 75 20 3d d = szHdr;. u =
17639 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78 0;. while( idx
1763a 3c 73 7a 48 64 72 20 26 26 20 75 3c 70 2d 3e 6e <szHdr && u<p->n
1763b 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 Field ){. u32
1763c 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 serial_type;..
1763d 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 72 idx += getVar
1763e 69 6e 74 33 32 28 20 61 4b 65 79 2b 69 64 78 2c int32( aKey+idx,
1763f 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 serial_type);.
17640 20 20 20 69 66 28 20 64 3e 3d 6e 4b 65 79 20 26 if( d>=nKey &
17641 26 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 & sqlite3VdbeSer
17642 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 ialTypeLen(seria
17643 6c 5f 74 79 70 65 29 3e 30 20 29 20 62 72 65 61 l_type)>0 ) brea
17644 6b 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 k;. pMem->enc
17645 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 = pKeyInfo->enc
17646 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d ;. pMem->db =
17647 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 pKeyInfo->db;.
17648 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
17649 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 0;. pMem->zM
1764a 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 alloc = 0;. d
1764b 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 += sqlite3VdbeS
1764c 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 erialGet(&aKey[d
1764d 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 ], serial_type,
1764e 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b pMem);. pMem+
1764f 2b 3b 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a +;. u++;. }.
17650 20 20 61 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 assert( u<=pKe
17651 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 yInfo->nField +
17652 31 20 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 1 );. p->nField
17653 20 3d 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 = u;. return (
17654 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a void*)p;.}../*.*
17655 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
17656 65 73 74 72 6f 79 73 20 61 20 55 6e 70 61 63 6b estroys a Unpack
17657 65 64 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 0a edRecord object.
17658 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17659 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1765a 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 beDeleteUnpacked
1765b 52 65 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 Record(UnpackedR
1765c 65 63 6f 72 64 20 2a 70 29 7b 0a 20 20 69 66 28 ecord *p){. if(
1765d 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p ){. if( p-
1765e 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 >flags & UNPACKE
1765f 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29 D_NEED_DESTROY )
17660 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 {. int i;.
17661 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a Mem *pMem;.
17662 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 for(i=0, p
17663 4d 65 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 Mem=p->aMem; i<p
17664 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 ->nField; i++, p
17665 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 Mem++){.
17666 69 66 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f if( pMem->zMallo
17667 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 c ){. s
17668 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
17669 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 ease(pMem);.
1766a 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1766b 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 }. if( p->f
1766c 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1766d 4e 45 45 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 NEED_FREE ){.
1766e 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1766f 28 70 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 (p->pKeyInfo->db
17670 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a , p);. }. }.
17671 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
17672 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72 65 73 20 nction compares
17673 74 68 65 20 74 77 6f 20 74 61 62 6c 65 20 72 6f the two table ro
17674 77 73 20 6f 72 20 69 6e 64 65 78 20 72 65 63 6f ws or index reco
17675 72 64 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 rds.** specified
17676 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 by {nKey1, pKey
17677 31 7d 20 61 6e 64 20 70 50 4b 65 79 32 2e 20 20 1} and pPKey2.
17678 49 74 20 72 65 74 75 72 6e 73 20 61 20 6e 65 67 It returns a neg
17679 61 74 69 76 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f ative, zero.** o
1767a 72 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 r positive integ
1767b 65 72 20 69 66 20 6b 65 79 31 20 69 73 20 6c 65 er if key1 is le
1767c 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 ss than, equal t
1767d 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61 74 65 72 o or .** greater
1767e 20 74 68 61 6e 20 6b 65 79 32 2e 20 20 54 68 65 than key2. The
1767f 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 {nKey1, pKey1}
17680 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 62 6c key must be a bl
17681 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 ob.** created by
17682 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 th OP_MakeRecor
17683 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 d opcode of the
17684 56 44 42 45 2e 20 20 54 68 65 20 70 50 4b 65 79 VDBE. The pPKey
17685 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74 20 62 65 2.** key must be
17686 20 61 20 70 61 72 73 65 64 20 6b 65 79 20 73 75 a parsed key su
17687 63 68 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66 ch as obtained f
17688 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 rom.** sqlite3Vd
17689 62 65 50 61 72 73 65 52 65 63 6f 72 64 2e 0a 2a beParseRecord..*
1768a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64 20 4b 65 *.** Key1 and Ke
1768b 79 32 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 y2 do not have t
1768c 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 73 61 o contain the sa
1768d 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 me number of fie
1768e 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 lds..** The key
1768f 77 69 74 68 20 66 65 77 65 72 20 66 69 65 6c 64 with fewer field
17690 73 20 69 73 20 75 73 75 61 6c 6c 79 20 63 6f 6d s is usually com
17691 70 61 72 65 73 20 6c 65 73 73 20 74 68 61 6e 20 pares less than
17692 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 65 72 20 6b the .** longer k
17693 65 79 2e 20 20 48 6f 77 65 76 65 72 20 69 66 20 ey. However if
17694 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 the UNPACKED_INC
17695 52 4b 45 59 20 66 6c 61 67 73 20 69 6e 20 70 50 RKEY flags in pP
17696 4b 65 79 32 20 69 73 20 73 65 74 0a 2a 2a 20 61 Key2 is set.** a
17697 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 nd the common pr
17698 65 66 69 78 65 73 20 61 72 65 20 65 71 75 61 6c efixes are equal
17699 2c 20 74 68 65 6e 20 6b 65 79 31 20 69 73 20 6c , then key1 is l
1769a 65 73 73 20 74 68 61 6e 20 6b 65 79 32 2e 0a 2a ess than key2..*
1769b 2a 20 4f 72 20 69 66 20 74 68 65 20 55 4e 50 41 * Or if the UNPA
1769c 43 4b 45 44 5f 4d 41 54 43 48 5f 50 52 45 46 49 CKED_MATCH_PREFI
1769d 58 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e X flag is set an
1769e 64 20 74 68 65 20 70 72 65 66 69 78 65 73 20 61 d the prefixes a
1769f 72 65 0a 2a 2a 20 65 71 75 61 6c 2c 20 74 68 65 re.** equal, the
176a0 6e 20 74 68 65 20 6b 65 79 73 20 61 72 65 20 63 n the keys are c
176a1 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 onsidered to be
176a2 65 71 75 61 6c 20 61 6e 64 0a 2a 2a 20 74 68 65 equal and.** the
176a3 20 70 61 72 74 73 20 62 65 79 6f 6e 64 20 74 68 parts beyond th
176a4 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20 e common prefix
176a5 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a are ignored..**.
176a6 2a 2a 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b ** If the UNPACK
176a7 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 ED_IGNORE_ROWID
176a8 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
176a9 6e 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 20 n the last byte
176aa 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 of.** the header
176ab 20 6f 66 20 70 4b 65 79 31 20 69 73 20 69 67 6e of pKey1 is ign
176ac 6f 72 65 64 2e 20 20 49 74 20 69 73 20 61 73 73 ored. It is ass
176ad 75 6d 65 64 20 74 68 61 74 20 70 4b 65 79 31 20 umed that pKey1
176ae 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 20 6b is.** an index k
176af 65 79 2c 20 61 6e 64 20 74 68 75 73 20 65 6e 64 ey, and thus end
176b0 73 20 77 69 74 68 20 61 20 72 6f 77 69 64 20 76 s with a rowid v
176b1 61 6c 75 65 2e 20 20 54 68 65 20 6c 61 73 74 20 alue. The last
176b2 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 68 byte.** of the h
176b3 65 61 64 65 72 20 77 69 6c 6c 20 74 68 65 72 65 eader will there
176b4 66 6f 72 65 20 62 65 20 74 68 65 20 73 65 72 69 fore be the seri
176b5 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 72 al type of the r
176b6 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 20 6f 66 20 owid:.** one of
176b7 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35 2c 20 36 1, 2, 3, 4, 5, 6
176b8 2c 20 38 2c 20 6f 72 20 39 20 2d 20 74 68 65 20 , 8, or 9 - the
176b9 69 6e 74 65 67 65 72 20 73 65 72 69 61 6c 20 74 integer serial t
176ba 79 70 65 73 2e 0a 2a 2a 20 54 68 65 20 73 65 72 ypes..** The ser
176bb 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 ial type of the
176bc 66 69 6e 61 6c 20 72 6f 77 69 64 20 77 69 6c 6c final rowid will
176bd 20 61 6c 77 61 79 73 20 62 65 20 61 20 73 69 6e always be a sin
176be 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 42 79 20 gle byte..** By
176bf 69 67 6e 6f 72 69 6e 67 20 74 68 69 73 20 6c 61 ignoring this la
176c0 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 68 st byte of the h
176c1 65 61 64 65 72 2c 20 77 65 20 66 6f 72 63 65 20 eader, we force
176c2 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a the comparison.*
176c3 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 * to ignore the
176c4 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 rowid at the end
176c5 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f 0a 53 51 4c of key1..*/.SQL
176c6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
176c7 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 sqlite3VdbeRecor
176c8 64 43 6f 6d 70 61 72 65 28 0a 20 20 69 6e 74 20 dCompare(. int
176c9 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 nKey1, const voi
176ca 64 20 2a 70 4b 65 79 31 2c 20 2f 2a 20 4c 65 66 d *pKey1, /* Lef
176cb 74 20 6b 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63 t key */. Unpac
176cc 6b 65 64 52 65 63 6f 72 64 20 2a 70 50 4b 65 79 kedRecord *pPKey
176cd 32 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 2 /* Righ
176ce 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a 20 20 75 33 t key */.){. u3
176cf 32 20 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 2 d1;
176d0 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
176d1 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64 aKey[] of next d
176d2 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 ata element */.
176d3 20 75 33 32 20 69 64 78 31 3b 20 20 20 20 20 20 u32 idx1;
176d4 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
176d5 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 to aKey[] of nex
176d6 74 20 68 65 61 64 65 72 20 65 6c 65 6d 65 6e 74 t header element
176d7 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 31 */. u32 szHdr1
176d8 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
176d9 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 68 er of bytes in h
176da 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 eader */. int i
176db 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69 65 = 0;. int nFie
176dc 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 ld;. int rc = 0
176dd 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e ;. const unsign
176de 65 64 20 63 68 61 72 20 2a 61 4b 65 79 31 20 3d ed char *aKey1 =
176df 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 (const unsigned
176e0 20 63 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a 20 char *)pKey1;.
176e1 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
176e2 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b 0a fo;. Mem mem1;.
176e3 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 70 50 . pKeyInfo = pP
176e4 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a Key2->pKeyInfo;.
176e5 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20 70 4b 65 mem1.enc = pKe
176e6 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65 yInfo->enc;. me
176e7 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f m1.db = pKeyInfo
176e8 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31 2e 66 6c 61 ->db;. mem1.fla
176e9 67 73 20 3d 20 30 3b 0a 20 20 6d 65 6d 31 2e 7a gs = 0;. mem1.z
176ea 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 0a 20 Malloc = 0;. .
176eb 20 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 6e idx1 = getVarin
176ec 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 72 t32(aKey1, szHdr
176ed 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 72 1);. d1 = szHdr
176ee 31 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32 2d 1;. if( pPKey2-
176ef 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 >flags & UNPACKE
176f0 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 D_IGNORE_ROWID )
176f1 7b 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b 0a {. szHdr1--;.
176f2 20 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 }. nField = p
176f3 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b KeyInfo->nField;
176f4 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c 73 . while( idx1<s
176f5 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 79 zHdr1 && i<pPKey
176f6 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 2->nField ){.
176f7 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 u32 serial_type
176f8 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 1;.. /* Read
176f9 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 the serial types
176fa 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 6c for the next el
176fb 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b 65 ement in each ke
176fc 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 2b y. */. idx1 +
176fd 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 61 = getVarint32( a
176fe 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61 Key1+idx1, seria
176ff 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69 l_type1 );. i
17700 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20 f( d1>=nKey1 &&
17701 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
17702 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f lTypeLen(serial_
17703 74 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b type1)>0 ) break
17704 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 ;.. /* Extrac
17705 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 t the values to
17706 62 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20 be compared..
17707 20 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71 */. d1 += sq
17708 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 lite3VdbeSerialG
17709 65 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73 et(&aKey1[d1], s
1770a 65 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65 erial_type1, &me
1770b 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 m1);.. /* Do
1770c 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 the comparison.
1770d 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
1770e 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 qlite3MemCompare
1770f 28 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d (&mem1, &pPKey2-
17710 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 >aMem[i],.
17711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17712 20 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20 i<nField ?
17713 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b pKeyInfo->aColl[
17714 69 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 i] : 0);. if(
17715 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 rc!=0 ){.
17716 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
17717 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 i++;. }. if(
17718 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 29 20 73 mem1.zMalloc ) s
17719 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1771a 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a 20 20 ease(&mem1);..
1771b 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 if( rc==0 ){.
1771c 20 2f 2a 20 72 63 3d 3d 30 20 68 65 72 65 20 6d /* rc==0 here m
1771d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 66 eans that one of
1771e 20 74 68 65 20 6b 65 79 73 20 72 61 6e 20 6f 75 the keys ran ou
1771f 74 20 6f 66 20 66 69 65 6c 64 73 20 61 6e 64 0a t of fields and.
17720 20 20 20 20 2a 2a 20 61 6c 6c 20 74 68 65 20 66 ** all the f
17721 69 65 6c 64 73 20 75 70 20 74 6f 20 74 68 61 74 ields up to that
17722 20 70 6f 69 6e 74 20 77 65 72 65 20 65 71 75 61 point were equa
17723 6c 2e 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b l. If the UNPACK
17724 45 44 5f 49 4e 43 52 4b 45 59 0a 20 20 20 20 2a ED_INCRKEY. *
17725 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 * flag is set, t
17726 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20 74 69 hen break the ti
17727 65 20 62 79 20 74 72 65 61 74 69 6e 67 20 6b 65 e by treating ke
17728 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0a 20 20 y2 as larger..
17729 20 20 2a 2a 20 49 66 20 74 68 65 20 55 50 41 43 ** If the UPAC
1772a 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 KED_PREFIX_MATCH
1772b 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
1772c 65 6e 20 6b 65 79 73 20 77 69 74 68 20 63 6f 6d en keys with com
1772d 6d 6f 6e 20 70 72 65 66 69 78 65 73 0a 20 20 20 mon prefixes.
1772e 20 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 ** are consider
1772f 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c 2e 20 ed to be equal.
17730 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
17731 6c 6f 6e 67 65 72 20 6b 65 79 20 69 73 20 74 68 longer key is th
17732 65 20 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 72 e . ** larger
17733 2e 20 20 41 73 20 69 74 20 68 61 70 70 65 6e 73 . As it happens
17734 2c 20 74 68 65 20 70 50 4b 65 79 32 20 77 69 6c , the pPKey2 wil
17735 6c 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 l always be the
17736 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 69 66 longer. ** if
17737 20 74 68 65 72 65 20 69 73 20 61 20 64 69 66 66 there is a diff
17738 65 72 65 6e 63 65 2e 0a 20 20 20 20 2a 2f 0a 20 erence.. */.
17739 20 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 if( pPKey2->f
1773a 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1773b 49 4e 43 52 4b 45 59 20 29 7b 0a 20 20 20 20 20 INCRKEY ){.
1773c 20 72 63 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 rc = -1;. }e
1773d 6c 73 65 20 69 66 28 20 70 50 4b 65 79 32 2d 3e lse if( pPKey2->
1773e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 flags & UNPACKED
1773f 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 29 7b _PREFIX_MATCH ){
17740 0a 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 . /* Leave
17741 72 63 3d 3d 30 20 2a 2f 0a 20 20 20 20 7d 65 6c rc==0 */. }el
17742 73 65 20 69 66 28 20 69 64 78 31 3c 73 7a 48 64 se if( idx1<szHd
17743 72 31 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d r1 ){. rc =
17744 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 1;. }. }els
17745 65 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e e if( pKeyInfo->
17746 61 53 6f 72 74 4f 72 64 65 72 20 26 26 20 69 3c aSortOrder && i<
17747 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
17748 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
17749 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f && pKeyInfo->aSo
1774a 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 rtOrder[i] ){.
1774b 20 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 7d 0a rc = -rc;. }.
1774c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1774d 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20 70 6f ../*.** pCur po
1774e 69 6e 74 73 20 61 74 20 61 6e 20 69 6e 64 65 78 ints at an index
1774f 20 65 6e 74 72 79 20 63 72 65 61 74 65 64 20 75 entry created u
17750 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 6b 65 sing the OP_Make
17751 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 0a 2a Record opcode..*
17752 2a 20 52 65 61 64 20 74 68 65 20 72 6f 77 69 64 * Read the rowid
17753 20 28 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 (the last field
17754 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 29 20 in the record)
17755 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e 20 and store it in
17756 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74 75 72 *rowid..** Retur
17757 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 n SQLITE_OK if e
17758 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2c verything works,
17759 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 or an error cod
1775a 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a e otherwise..*/.
1775b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1775c 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 nt sqlite3VdbeId
1775d 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 20 xRowid(BtCursor
1775e 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77 69 *pCur, i64 *rowi
1775f 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b d){. i64 nCellK
17760 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 ey = 0;. int rc
17761 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20 20 ;. u32 szHdr;
17762 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
17763 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 the header */.
17764 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b 20 u32 typeRowid;
17765 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 /* Serial typ
17766 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a e of the rowid *
17767 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69 64 /. u32 lenRowid
17768 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 ; /* Size of
17769 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 the rowid */.
1776a 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 73 71 6c Mem m, v;.. sql
1776b 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
1776c 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 (pCur, &nCellKey
1776d 29 3b 0a 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 );. if( nCellKe
1776e 79 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 y<=0 ){. retu
1776f 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
17770 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 6d 2e T_BKPT;. }. m.
17771 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 2e 64 flags = 0;. m.d
17772 62 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c b = 0;. m.zMall
17773 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 oc = 0;. rc = s
17774 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
17775 6d 42 74 72 65 65 28 70 43 75 72 2c 20 30 2c 20 mBtree(pCur, 0,
17776 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 nCellKey, 1, &m)
17777 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
17778 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
17779 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 69 . (void)getVari
1777a 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20 73 nt32((u8*)m.z, s
1777b 7a 48 64 72 29 3b 0a 20 20 28 76 6f 69 64 29 67 zHdr);. (void)g
1777c 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29 etVarint32((u8*)
1777d 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74 &m.z[szHdr-1], t
1777e 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 6c 65 6e ypeRowid);. len
1777f 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 56 Rowid = sqlite3V
17780 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e dbeSerialTypeLen
17781 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 73 (typeRowid);. s
17782 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
17783 47 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e Get((u8*)&m.z[m.
17784 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 n-lenRowid], typ
17785 65 52 6f 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a eRowid, &v);. *
17786 72 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 rowid = v.u.i;.
17787 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
17788 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 elease(&m);. re
17789 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1778a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 }../*.** Compare
1778b 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 the key of the
1778c 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74 index entry that
1778d 20 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f cursor pC is po
1778e 69 6e 74 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a int to against.*
1778f 2a 20 74 68 65 20 6b 65 79 20 73 74 72 69 6e 67 * the key string
17790 20 69 6e 20 70 4b 65 79 20 28 6f 66 20 6c 65 6e in pKey (of len
17791 67 74 68 20 6e 4b 65 79 29 2e 20 20 57 72 69 74 gth nKey). Writ
17792 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e e into *pRes a n
17793 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 umber.** that is
17794 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c negative, zero,
17795 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 or positive if
17796 70 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c pC is less than,
17797 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 equal to,.** or
17798 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 4b greater than pK
17799 65 79 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 ey. Return SQLI
1779a 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
1779b 2e 0a 2a 2a 0a 2a 2a 20 70 4b 65 79 20 69 73 20 ..**.** pKey is
1779c 65 69 74 68 65 72 20 63 72 65 61 74 65 64 20 77 either created w
1779d 69 74 68 6f 75 74 20 61 20 72 6f 77 69 64 20 6f ithout a rowid o
1779e 72 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 73 r is truncated s
1779f 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69 o that it.** omi
177a0 74 73 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 ts the rowid at
177a1 74 68 65 20 65 6e 64 2e 20 20 54 68 65 20 72 6f the end. The ro
177a2 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f wid at the end o
177a3 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 f the index entr
177a4 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 20 y.** is ignored
177a5 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c as well. Hence,
177a6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e this routine on
177a7 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 ly compares the
177a8 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20 prefixes .** of
177a9 74 68 65 20 6b 65 79 73 20 70 72 69 6f 72 20 74 the keys prior t
177aa 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 o the final rowi
177ab 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 69 72 d, not the entir
177ac 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e e key..**.** pUn
177ad 70 61 63 6b 65 64 20 6d 61 79 20 62 65 20 61 6e packed may be an
177ae 20 75 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f unpacked versio
177af 6e 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 n of pKey,nKey.
177b0 20 49 66 20 70 55 6e 70 61 63 6b 65 64 20 69 73 If pUnpacked is
177b1 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 69 74 20 .** supplied it
177b2 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 is used in place
177b3 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 0a 2a of pKey,nKey..*
177b4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
177b5 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
177b6 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 IdxKeyCompare(.
177b7 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c VdbeCursor *pC,
177b8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
177b9 54 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f The cursor to co
177ba 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f mpare against */
177bb 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
177bc 64 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f d *pUnpacked, /
177bd 2a 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 * Unpacked versi
177be 6f 6e 20 6f 66 20 70 4b 65 79 20 61 6e 64 20 6e on of pKey and n
177bf 4b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 Key */. int *re
177c0 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
177c1 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
177c2 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73 e comparison res
177c3 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ult here */.){.
177c4 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 i64 nCellKey =
177c5 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 0;. int rc;. B
177c6 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 tCursor *pCur =
177c7 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 4d pC->pCursor;. M
177c8 65 6d 20 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 em m;.. sqlite3
177c9 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 BtreeKeySize(pCu
177ca 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 r, &nCellKey);.
177cb 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30 if( nCellKey<=0
177cc 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 30 ){. *res = 0
177cd 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
177ce 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6d 2e ITE_OK;. }. m.
177cf 64 62 20 3d 20 30 3b 0a 20 20 6d 2e 66 6c 61 67 db = 0;. m.flag
177d0 73 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c s = 0;. m.zMall
177d1 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 oc = 0;. rc = s
177d2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
177d3 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75 72 73 mBtree(pC->pCurs
177d4 6f 72 2c 20 30 2c 20 6e 43 65 6c 6c 4b 65 79 2c or, 0, nCellKey,
177d5 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72 1, &m);. if( r
177d6 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 c ){. return
177d7 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rc;. }. assert
177d8 28 20 70 55 6e 70 61 63 6b 65 64 2d 3e 66 6c 61 ( pUnpacked->fla
177d9 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 gs & UNPACKED_IG
177da 4e 4f 52 45 5f 52 4f 57 49 44 20 29 3b 0a 20 20 NORE_ROWID );.
177db 2a 72 65 73 20 3d 20 73 71 6c 69 74 65 33 56 64 *res = sqlite3Vd
177dc 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
177dd 6d 2e 6e 2c 20 6d 2e 7a 2c 20 70 55 6e 70 61 63 m.n, m.z, pUnpac
177de 6b 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 ked);. sqlite3V
177df 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d dbeMemRelease(&m
177e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
177e1 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
177e2 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 This routine set
177e3 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 s the value to b
177e4 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 e returned by su
177e5 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
177e6 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 68 61 o.** sqlite3_cha
177e7 6e 67 65 73 28 29 20 6f 6e 20 74 68 65 20 64 61 nges() on the da
177e8 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 27 64 tabase handle 'd
177e9 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 b'. .*/.SQLITE_P
177ea 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
177eb 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 te3VdbeSetChange
177ec 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 s(sqlite3 *db, i
177ed 6e 74 20 6e 43 68 61 6e 67 65 29 7b 0a 20 20 61 nt nChange){. a
177ee 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
177ef 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
177f0 74 65 78 29 20 29 3b 0a 20 20 64 62 2d 3e 6e 43 tex) );. db->nC
177f1 68 61 6e 67 65 20 3d 20 6e 43 68 61 6e 67 65 3b hange = nChange;
177f2 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 . db->nTotalCha
177f3 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a nge += nChange;.
177f4 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 20 66 }../*.** Set a f
177f5 6c 61 67 20 69 6e 20 74 68 65 20 76 64 62 65 20 lag in the vdbe
177f6 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 to update the ch
177f7 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 77 68 65 ange counter whe
177f8 6e 20 69 74 20 69 73 20 66 69 6e 61 6c 69 73 65 n it is finalise
177f9 64 0a 2a 2a 20 6f 72 20 72 65 73 65 74 2e 0a 2a d.** or reset..*
177fa 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
177fb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
177fc 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 eCountChanges(Vd
177fd 62 65 20 2a 76 29 7b 0a 20 20 76 2d 3e 63 68 61 be *v){. v->cha
177fe 6e 67 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a ngeCntOn = 1;.}.
177ff 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76 65 72 ./*.** Mark ever
17800 79 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 y prepared state
17801 6d 65 6e 74 20 61 73 73 6f 63 69 61 74 65 64 20 ment associated
17802 77 69 74 68 20 61 20 64 61 74 61 62 61 73 65 20 with a database
17803 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 73 connection.** as
17804 20 65 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 expired..**.**
17805 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 74 65 An expired state
17806 6d 65 6e 74 20 6d 65 61 6e 73 20 74 68 61 74 20 ment means that
17807 72 65 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 recompilation of
17808 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 the statement i
17809 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 2e 20 s.** recommend.
1780a 20 53 74 61 74 65 6d 65 6e 74 73 20 65 78 70 69 Statements expi
1780b 72 65 20 77 68 65 6e 20 74 68 69 6e 67 73 20 68 re when things h
1780c 61 70 70 65 6e 20 74 68 61 74 20 6d 61 6b 65 20 appen that make
1780d 74 68 65 69 72 0a 2a 2a 20 70 72 6f 67 72 61 6d their.** program
1780e 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 52 65 6d s obsolete. Rem
1780f 6f 76 69 6e 67 20 75 73 65 72 2d 64 65 66 69 6e oving user-defin
17810 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 ed functions or
17811 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 collating.** seq
17812 75 65 6e 63 65 73 2c 20 6f 72 20 63 68 61 6e 67 uences, or chang
17813 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 ing an authoriza
17814 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 61 72 tion function ar
17815 65 20 74 68 65 20 74 79 70 65 73 20 6f 66 0a 2a e the types of.*
17816 2a 20 74 68 69 6e 67 73 20 74 68 61 74 20 6d 61 * things that ma
17817 6b 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 ke prepared stat
17818 65 6d 65 6e 74 73 20 6f 62 73 6f 6c 65 74 65 2e ements obsolete.
17819 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1781a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
1781b 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 xpirePreparedSta
1781c 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 20 tements(sqlite3
1781d 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b *db){. Vdbe *p;
1781e 0a 20 20 66 6f 72 28 70 20 3d 20 64 62 2d 3e 70 . for(p = db->p
1781f 56 64 62 65 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e Vdbe; p; p=p->pN
17820 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 ext){. p->exp
17821 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a ired = 1;. }.}.
17822 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
17823 65 20 64 61 74 61 62 61 73 65 20 61 73 73 6f 63 e database assoc
17824 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 56 iated with the V
17825 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 dbe..*/.SQLITE_P
17826 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a RIVATE sqlite3 *
17827 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 56 64 sqlite3VdbeDb(Vd
17828 62 65 20 2a 76 29 7b 0a 20 20 72 65 74 75 72 6e be *v){. return
17829 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a v->db;.}../****
1782a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1782b 66 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a f vdbeaux.c ****
1782c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1782d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1782e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1782f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
17830 20 66 69 6c 65 20 76 64 62 65 61 70 69 2e 63 20 file vdbeapi.c
17831 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17832 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17833 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
17834 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2004 May 26.**.
17835 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
17836 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
17837 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
17838 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
17839 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1783a 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1783b 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1783c 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1783d 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1783e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1783f 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
17840 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
17841 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
17842 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
17843 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
17844 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
17845 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
17846 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
17847 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17848 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17849 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1784a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
1784b 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
1784c 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f ains code use to
1784d 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50 49 73 20 implement APIs
1784e 74 68 61 74 20 61 72 65 20 70 61 72 74 20 6f 66 that are part of
1784f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 0a 2a 2a the.** VDBE..**
17850 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 61 70 69 .** $Id: vdbeapi
17851 2e 63 2c 76 20 31 2e 31 34 38 20 32 30 30 38 2f .c,v 1.148 2008/
17852 31 31 2f 30 35 20 31 36 3a 33 37 3a 33 35 20 64 11/05 16:37:35 d
17853 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 rh Exp $.*/..#if
17854 20 30 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 0 && defined(SQ
17855 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f LITE_ENABLE_MEMO
17856 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 29 0a 2f RY_MANAGEMENT)./
17857 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
17858 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e ng structure con
17859 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 tains pointers t
1785a 6f 20 74 68 65 20 65 6e 64 20 70 6f 69 6e 74 73 o the end points
1785b 20 6f 66 20 61 0a 2a 2a 20 64 6f 75 62 6c 79 2d of a.** doubly-
1785c 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 linked list of a
1785d 6c 6c 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 ll compiled SQL
1785e 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 statements that
1785f 6d 61 79 20 62 65 20 68 6f 6c 64 69 6e 67 0a 2a may be holding.*
17860 2a 20 62 75 66 66 65 72 73 20 65 6c 69 67 69 62 * buffers eligib
17861 6c 65 20 66 6f 72 20 72 65 6c 65 61 73 65 20 77 le for release w
17862 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f hen the sqlite3_
17863 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
17864 20 69 6e 74 65 72 66 61 63 65 20 69 73 0a 2a 2a interface is.**
17865 20 69 6e 76 6f 6b 65 64 2e 20 41 63 63 65 73 73 invoked. Access
17866 20 74 6f 20 74 68 69 73 20 6c 69 73 74 20 69 73 to this list is
17867 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 protected by th
17868 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
17869 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 6d 75 TATIC_LRU2.** mu
1786a 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 tex..**.** State
1786b 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64 20 ments are added
1786c 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 to the end of th
1786d 69 73 20 6c 69 73 74 20 77 68 65 6e 20 73 71 6c is list when sql
1786e 69 74 65 33 5f 72 65 73 65 74 28 29 20 69 73 0a ite3_reset() is.
1786f 2a 2a 20 63 61 6c 6c 65 64 2e 20 54 68 65 79 20 ** called. They
17870 61 72 65 20 72 65 6d 6f 76 65 64 20 65 69 74 68 are removed eith
17871 65 72 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f er when sqlite3_
17872 73 74 65 70 28 29 20 6f 72 20 73 71 6c 69 74 65 step() or sqlite
17873 33 5f 66 69 6e 61 6c 69 7a 65 28 29 0a 2a 2a 20 3_finalize().**
17874 69 73 20 63 61 6c 6c 65 64 2e 20 57 68 65 6e 20 is called. When
17875 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 61 statements are a
17876 64 64 65 64 20 74 6f 20 74 68 69 73 20 6c 69 73 dded to this lis
17877 74 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 t, the associate
17878 64 20 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 d .** register a
17879 72 72 61 79 20 28 70 2d 3e 61 4d 65 6d 5b 31 2e rray (p->aMem[1.
1787a 2e 70 2d 3e 6e 4d 65 6d 5d 29 20 6d 61 79 20 63 .p->nMem]) may c
1787b 6f 6e 74 61 69 6e 20 64 79 6e 61 6d 69 63 20 62 ontain dynamic b
1787c 75 66 66 65 72 73 20 74 68 61 74 0a 2a 2a 20 63 uffers that.** c
1787d 61 6e 20 62 65 20 66 72 65 65 64 20 75 73 69 6e an be freed usin
1787e 67 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c g sqlite3VdbeRel
1787f 65 61 73 65 4d 65 6d 6f 72 79 28 29 2e 0a 2a 2a easeMemory()..**
17880 0a 2a 2a 20 57 68 65 6e 20 73 74 61 74 65 6d 65 .** When stateme
17881 6e 74 73 20 61 72 65 20 61 64 64 65 64 20 6f 72 nts are added or
17882 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 removed from th
17883 69 73 20 6c 69 73 74 2c 20 74 68 65 20 6d 75 74 is list, the mut
17884 65 78 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 ex.** associated
17885 20 77 69 74 68 20 74 68 65 20 56 64 62 65 20 62 with the Vdbe b
17886 65 69 6e 67 20 61 64 64 65 64 20 6f 72 20 72 65 eing added or re
17887 6d 6f 76 65 64 20 28 56 64 62 65 2e 64 62 2d 3e moved (Vdbe.db->
17888 6d 75 74 65 78 29 20 69 73 0a 2a 2a 20 61 6c 72 mutex) is.** alr
17889 65 61 64 79 20 68 65 6c 64 2e 20 54 68 65 20 4c eady held. The L
1788a 52 55 32 20 6d 75 74 65 78 20 69 73 20 74 68 65 RU2 mutex is the
1788b 6e 20 6f 62 74 61 69 6e 65 64 2c 20 62 6c 6f 63 n obtained, bloc
1788c 6b 69 6e 67 20 69 66 20 6e 65 63 65 73 73 61 72 king if necessar
1788d 79 2c 0a 2a 2a 20 74 68 65 20 6c 69 6e 6b 65 64 y,.** the linked
1788e 2d 6c 69 73 74 20 70 6f 69 6e 74 65 72 73 20 6d -list pointers m
1788f 61 6e 69 70 75 6c 61 74 65 64 20 61 6e 64 20 74 anipulated and t
17890 68 65 20 4c 52 55 32 20 6d 75 74 65 78 20 72 65 he LRU2 mutex re
17891 6c 69 6e 71 75 69 73 68 65 64 2e 0a 2a 2f 0a 73 linquished..*/.s
17892 74 72 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c truct StatementL
17893 72 75 4c 69 73 74 20 7b 0a 20 20 56 64 62 65 20 ruList {. Vdbe
17894 2a 70 46 69 72 73 74 3b 0a 20 20 56 64 62 65 20 *pFirst;. Vdbe
17895 2a 70 4c 61 73 74 3b 0a 7d 3b 0a 73 74 61 74 69 *pLast;.};.stati
17896 63 20 73 74 72 75 63 74 20 53 74 61 74 65 6d 65 c struct Stateme
17897 6e 74 4c 72 75 4c 69 73 74 20 73 71 6c 69 74 65 ntLruList sqlite
17898 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 3b 0a 3LruStatements;.
17899 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 ./*.** Check tha
1789a 74 20 74 68 65 20 6c 69 73 74 20 6c 6f 6f 6b 73 t the list looks
1789b 20 74 6f 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c to be internall
1789c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 20 54 68 y consistent. Th
1789d 69 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 61 73 is is used.** as
1789e 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 part of an asse
1789f 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 rt() statement a
178a0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
178a1 20 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c assert( stmtL
178a2 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 2a 2f 0a ruCheck() );.*/.
178a3 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 #ifndef NDEBUG.s
178a4 74 61 74 69 63 20 69 6e 74 20 73 74 6d 74 4c 72 tatic int stmtLr
178a5 75 43 68 65 63 6b 28 29 7b 0a 20 20 56 64 62 65 uCheck(){. Vdbe
178a6 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c *p;. for(p=sql
178a7 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
178a8 73 2e 70 46 69 72 73 74 3b 20 70 3b 20 70 3d 70 s.pFirst; p; p=p
178a9 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 ->pLruNext){.
178aa 20 61 73 73 65 72 74 28 70 2d 3e 70 4c 72 75 4e assert(p->pLruN
178ab 65 78 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 ext || p==sqlite
178ac 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 3LruStatements.p
178ad 4c 61 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72 Last);. asser
178ae 74 28 21 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c t(!p->pLruNext |
178af 7c 20 70 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 | p->pLruNext->p
178b0 4c 72 75 50 72 65 76 3d 3d 70 29 3b 0a 20 20 20 LruPrev==p);.
178b1 20 61 73 73 65 72 74 28 70 2d 3e 70 4c 72 75 50 assert(p->pLruP
178b2 72 65 76 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 rev || p==sqlite
178b3 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 3LruStatements.p
178b4 46 69 72 73 74 29 3b 0a 20 20 20 20 61 73 73 65 First);. asse
178b5 72 74 28 21 70 2d 3e 70 4c 72 75 50 72 65 76 20 rt(!p->pLruPrev
178b6 7c 7c 20 70 2d 3e 70 4c 72 75 50 72 65 76 2d 3e || p->pLruPrev->
178b7 70 4c 72 75 4e 65 78 74 3d 3d 70 29 3b 0a 20 20 pLruNext==p);.
178b8 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a }. return 1;.}.
178b9 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 #endif../*.** Ad
178ba 64 20 76 64 62 65 20 70 20 74 6f 20 74 68 65 20 d vdbe p to the
178bb 65 6e 64 20 6f 66 20 74 68 65 20 73 74 61 74 65 end of the state
178bc 6d 65 6e 74 20 6c 72 75 20 6c 69 73 74 2e 20 49 ment lru list. I
178bd 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
178be 74 0a 2a 2a 20 70 20 69 73 20 6e 6f 74 20 61 6c t.** p is not al
178bf 72 65 61 64 79 20 70 61 72 74 20 6f 66 20 74 68 ready part of th
178c0 65 20 6c 69 73 74 20 77 68 65 6e 20 74 68 69 73 e list when this
178c1 20 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20 is called. The
178c2 6c 72 75 20 6c 69 73 74 0a 2a 2a 20 69 73 20 70 lru list.** is p
178c3 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 rotected by the
178c4 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
178c5 54 49 43 5f 4c 52 55 20 6d 75 74 65 78 2e 0a 2a TIC_LRU mutex..*
178c6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 /.static void st
178c7 6d 74 4c 72 75 41 64 64 28 56 64 62 65 20 2a 70 mtLruAdd(Vdbe *p
178c8 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ){. sqlite3_mut
178c9 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
178ca 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
178cb 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
178cc 52 55 32 29 29 3b 0a 0a 20 20 69 66 28 20 70 2d RU2));.. if( p-
178cd 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e >pLruPrev || p->
178ce 70 4c 72 75 4e 65 78 74 20 7c 7c 20 73 71 6c 69 pLruNext || sqli
178cf 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
178d0 2e 70 46 69 72 73 74 3d 3d 70 20 29 7b 0a 20 20 .pFirst==p ){.
178d1 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
178d2 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 leave(sqlite3Mut
178d3 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
178d4 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 UTEX_STATIC_LRU2
178d5 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ));. return;.
178d6 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 73 }.. assert( s
178d7 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b tmtLruCheck() );
178d8 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 .. if( !sqlite3
178d9 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 LruStatements.pF
178da 69 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65 irst ){. asse
178db 72 74 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53 rt( !sqlite3LruS
178dc 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20 tatements.pLast
178dd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 );. sqlite3Lr
178de 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 uStatements.pFir
178df 73 74 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 st = p;. sqli
178e0 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
178e1 2e 70 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d 65 .pLast = p;. }e
178e2 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
178e3 20 21 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 !sqlite3LruStat
178e4 65 6d 65 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c ements.pLast->pL
178e5 72 75 4e 65 78 74 20 29 3b 0a 20 20 20 20 70 2d ruNext );. p-
178e6 3e 70 4c 72 75 50 72 65 76 20 3d 20 73 71 6c 69 >pLruPrev = sqli
178e7 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
178e8 2e 70 4c 61 73 74 3b 0a 20 20 20 20 73 71 6c 69 .pLast;. sqli
178e9 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
178ea 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65 78 74 .pLast->pLruNext
178eb 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 = p;. sqlite
178ec 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 3LruStatements.p
178ed 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 Last = p;. }..
178ee 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 assert( stmtLru
178ef 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20 73 71 Check() );.. sq
178f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
178f1 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c e(sqlite3MutexAl
178f2 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
178f3 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a _STATIC_LRU2));.
178f4 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e }../*.** Assumin
178f5 67 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 g the SQLITE_MUT
178f6 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 6d EX_STATIC_LRU2 m
178f7 75 74 65 78 74 20 69 73 20 61 6c 72 65 61 64 79 utext is already
178f8 20 68 65 6c 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a held, remove.**
178f9 20 73 74 61 74 65 6d 65 6e 74 20 70 20 66 72 6f statement p fro
178fa 6d 20 74 68 65 20 6c 65 61 73 74 2d 72 65 63 65 m the least-rece
178fb 6e 74 6c 79 2d 75 73 65 64 20 73 74 61 74 65 6d ntly-used statem
178fc 65 6e 74 20 6c 69 73 74 2e 20 49 66 20 74 68 65 ent list. If the
178fd 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 .** statement i
178fe 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
178ff 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74 part of the list
17900 2c 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 , this call is a
17901 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
17902 63 20 76 6f 69 64 20 73 74 6d 74 4c 72 75 52 65 c void stmtLruRe
17903 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 56 64 62 65 moveNomutex(Vdbe
17904 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 *p){. if( p->p
17905 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c LruPrev || p->pL
17906 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d 73 71 6c ruNext || p==sql
17907 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
17908 73 2e 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20 s.pFirst ){.
17909 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 assert( stmtLruC
1790a 68 65 63 6b 28 29 20 29 3b 0a 20 20 20 20 69 66 heck() );. if
1790b 28 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b ( p->pLruNext ){
1790c 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65 . p->pLruNe
1790d 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 xt->pLruPrev = p
1790e 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 ->pLruPrev;.
1790f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
17910 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
17911 73 2e 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 72 s.pLast = p->pLr
17912 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 uPrev;. }.
17913 20 69 66 28 20 70 2d 3e 70 4c 72 75 50 72 65 76 if( p->pLruPrev
17914 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72 ){. p->pLr
17915 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 20 uPrev->pLruNext
17916 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 = p->pLruNext;.
17917 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17918 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
17919 65 6e 74 73 2e 70 46 69 72 73 74 20 3d 20 70 2d ents.pFirst = p-
1791a 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d >pLruNext;. }
1791b 0a 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65 78 74 . p->pLruNext
1791c 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 72 = 0;. p->pLr
1791d 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 61 uPrev = 0;. a
1791e 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 ssert( stmtLruCh
1791f 65 63 6b 28 29 20 29 3b 0a 20 20 7d 0a 7d 0a 0a eck() );. }.}..
17920 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 /*.** Assuming t
17921 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f he SQLITE_MUTEX_
17922 53 54 41 54 49 43 5f 4c 52 55 32 20 6d 75 74 65 STATIC_LRU2 mute
17923 78 74 20 69 73 20 6e 6f 74 20 68 65 6c 64 2c 20 xt is not held,
17924 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 74 65 6d remove.** statem
17925 65 6e 74 20 70 20 66 72 6f 6d 20 74 68 65 20 6c ent p from the l
17926 65 61 73 74 2d 72 65 63 65 6e 74 6c 79 2d 75 73 east-recently-us
17927 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6c 69 73 ed statement lis
17928 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 73 74 t. If the .** st
17929 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 63 atement is not c
1792a 75 72 72 65 6e 74 6c 79 20 70 61 72 74 20 6f 66 urrently part of
1792b 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20 the list, this
1792c 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e call is a no-op.
1792d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1792e 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 56 64 stmtLruRemove(Vd
1792f 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 be *p){. sqlite
17930 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 3_mutex_enter(sq
17931 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
17932 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
17933 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20 20 73 74 TIC_LRU2));. st
17934 6d 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 mtLruRemoveNomut
17935 65 78 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex(p);. sqlite3
17936 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c _mutex_leave(sql
17937 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
17938 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
17939 49 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a 0a 2f 2a IC_LRU2));.}../*
1793a 0a 2a 2a 20 54 72 79 20 74 6f 20 72 65 6c 65 61 .** Try to relea
1793b 73 65 20 6e 20 62 79 74 65 73 20 6f 66 20 6d 65 se n bytes of me
1793c 6d 6f 72 79 20 62 79 20 66 72 65 65 69 6e 67 20 mory by freeing
1793d 62 75 66 66 65 72 73 20 61 73 73 6f 63 69 61 74 buffers associat
1793e 65 64 20 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 ed .** with the
1793f 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 memory registers
17940 20 6f 66 20 63 75 72 72 65 6e 74 6c 79 20 75 6e of currently un
17941 75 73 65 64 20 76 64 62 65 73 2e 0a 2a 2f 0a 53 used vdbes..*/.S
17942 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
17943 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c t sqlite3VdbeRel
17944 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e easeMemory(int n
17945 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 ){. Vdbe *p;.
17946 56 64 62 65 20 2a 70 4e 65 78 74 3b 0a 20 20 69 Vdbe *pNext;. i
17947 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 0a 20 nt nFree = 0;..
17948 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
17949 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 nter(sqlite3Mute
1794a 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
1794b 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 TEX_STATIC_LRU2)
1794c 29 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 );. for(p=sqlit
1794d 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e e3LruStatements.
1794e 70 46 69 72 73 74 3b 20 70 20 26 26 20 6e 46 72 pFirst; p && nFr
1794f 65 65 3c 6e 3b 20 70 3d 70 4e 65 78 74 29 7b 0a ee<n; p=pNext){.
17950 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 pNext = p->p
17951 4c 72 75 4e 65 78 74 3b 0a 0a 20 20 20 20 2f 2a LruNext;.. /*
17952 20 46 6f 72 20 65 61 63 68 20 73 74 61 74 65 6d For each statem
17953 65 6e 74 20 68 61 6e 64 6c 65 20 69 6e 20 74 68 ent handle in th
17954 65 20 6c 72 75 20 6c 69 73 74 2c 20 61 74 74 65 e lru list, atte
17955 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 mpt to obtain th
17956 65 0a 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 e. ** associa
17957 74 65 64 20 64 61 74 61 62 61 73 65 20 6d 75 74 ted database mut
17958 65 78 2e 20 49 66 20 69 74 20 63 61 6e 6e 6f 74 ex. If it cannot
17959 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 63 6f be obtained, co
1795a 6e 74 69 6e 75 65 0a 20 20 20 20 2a 2a 20 74 6f ntinue. ** to
1795b 20 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d the next statem
1795c 65 6e 74 20 68 61 6e 64 6c 65 2e 20 49 74 20 69 ent handle. It i
1795d 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 s not possible t
1795e 6f 20 62 6c 6f 63 6b 20 6f 6e 0a 20 20 20 20 2a o block on. *
1795f 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d * the database m
17960 75 74 65 78 20 2d 20 74 68 61 74 20 63 6f 75 6c utex - that coul
17961 64 20 63 61 75 73 65 20 64 65 61 64 6c 6f 63 6b d cause deadlock
17962 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
17963 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 SQLITE_OK==sqli
17964 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d te3_mutex_try(p-
17965 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 >db->mutex) ){.
17966 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 nFree += sq
17967 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 lite3VdbeRelease
17968 42 75 66 66 65 72 73 28 70 29 3b 0a 20 20 20 20 Buffers(p);.
17969 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e stmtLruRemoveN
1796a 6f 6d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 20 omutex(p);.
1796b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1796c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
1796d 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 x);. }. }.
1796e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1796f 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ave(sqlite3Mutex
17970 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
17971 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 EX_STATIC_LRU2))
17972 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 ;.. return nFre
17973 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c e;.}../*.** Call
17974 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 sqlite3Reprepar
17975 65 28 29 20 6f 6e 20 74 68 65 20 73 74 61 74 65 e() on the state
17976 6d 65 6e 74 2e 20 52 65 6d 6f 76 65 20 69 74 20 ment. Remove it
17977 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c 72 75 20 from the.** lru
17978 6c 69 73 74 20 62 65 66 6f 72 65 20 64 6f 69 6e list before doin
17979 67 20 73 6f 2c 20 61 73 20 52 65 70 72 65 70 61 g so, as Reprepa
1797a 72 65 28 29 20 77 69 6c 6c 20 66 72 65 65 20 61 re() will free a
1797b 6c 6c 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 ll the.** memory
1797c 20 72 65 67 69 73 74 65 72 20 62 75 66 66 65 72 register buffer
1797d 73 20 61 6e 79 77 61 79 2e 0a 2a 2f 0a 69 6e 74 s anyway..*/.int
1797e 20 76 64 62 65 52 65 70 72 65 70 61 72 65 28 56 vdbeReprepare(V
1797f 64 62 65 20 2a 70 29 7b 0a 20 20 73 74 6d 74 4c dbe *p){. stmtL
17980 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 72 ruRemove(p);. r
17981 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 70 eturn sqlite3Rep
17982 72 65 70 61 72 65 28 70 29 3b 0a 7d 0a 0a 23 65 repare(p);.}..#e
17983 6c 73 65 20 20 20 20 20 20 20 2f 2a 20 21 53 51 lse /* !SQ
17984 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f LITE_ENABLE_MEMO
17985 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f RY_MANAGEMENT */
17986 0a 20 20 23 64 65 66 69 6e 65 20 73 74 6d 74 4c . #define stmtL
17987 72 75 52 65 6d 6f 76 65 28 78 29 0a 20 20 23 64 ruRemove(x). #d
17988 65 66 69 6e 65 20 73 74 6d 74 4c 72 75 41 64 64 efine stmtLruAdd
17989 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 76 64 (x). #define vd
1798a 62 65 52 65 70 72 65 70 61 72 65 28 78 29 20 73 beReprepare(x) s
1798b 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 qlite3Reprepare(
1798c 78 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e x).#endif...#ifn
1798d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1798e 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a DEPRECATED./*.**
1798f 20 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e 6f Return TRUE (no
17990 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20 73 n-zero) of the s
17991 74 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69 65 tatement supplie
17992 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 d as an argument
17993 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 needs.** to be
17994 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41 20 73 recompiled. A s
17995 74 61 74 65 6d 65 6e 74 20 6e 65 65 64 73 20 74 tatement needs t
17996 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 o be recompiled
17997 77 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 whenever the.**
17998 65 78 65 63 75 74 69 6f 6e 20 65 6e 76 69 72 6f execution enviro
17999 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 69 6e nment changes in
1799a 20 61 20 77 61 79 20 74 68 61 74 20 77 6f 75 6c a way that woul
1799b 64 20 61 6c 74 65 72 20 74 68 65 20 70 72 6f 67 d alter the prog
1799c 72 61 6d 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 ram.** that sqli
1799d 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 67 65 te3_prepare() ge
1799e 6e 65 72 61 74 65 73 2e 20 20 46 6f 72 20 65 78 nerates. For ex
1799f 61 6d 70 6c 65 2c 20 69 66 20 6e 65 77 20 66 75 ample, if new fu
179a0 6e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f nctions or.** co
179a1 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
179a2 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 s are registered
179a3 20 6f 72 20 69 66 20 61 6e 20 61 75 74 68 6f 72 or if an author
179a4 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 izer function is
179a5 0a 2a 2a 20 61 64 64 65 64 20 6f 72 20 63 68 61 .** added or cha
179a6 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f nged..*/.SQLITE_
179a7 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
179a8 65 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f expired(sqlite3_
179a9 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
179aa 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a Vdbe *p = (Vdbe*
179ab 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e )pStmt;. return
179ac 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 78 70 69 p==0 || p->expi
179ad 72 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f red;.}.#endif../
179ae 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
179af 6e 67 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 ng routine destr
179b0 6f 79 73 20 61 20 76 69 72 74 75 61 6c 20 6d 61 oys a virtual ma
179b1 63 68 69 6e 65 20 74 68 61 74 20 69 73 20 63 72 chine that is cr
179b2 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 eated by.** the
179b3 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 sqlite3_compile(
179b4 29 20 72 6f 75 74 69 6e 65 2e 20 54 68 65 20 69 ) routine. The i
179b5 6e 74 65 67 65 72 20 72 65 74 75 72 6e 65 64 20 nteger returned
179b6 69 73 20 61 6e 20 53 51 4c 49 54 45 5f 0a 2a 2a is an SQLITE_.**
179b7 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65 success/failure
179b8 20 63 6f 64 65 20 74 68 61 74 20 64 65 73 63 72 code that descr
179b9 69 62 65 73 20 74 68 65 20 72 65 73 75 6c 74 20 ibes the result
179ba 6f 66 20 65 78 65 63 75 74 69 6e 67 20 74 68 65 of executing the
179bb 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 virtual.** mach
179bc 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ine..**.** This
179bd 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 routine sets the
179be 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 error code and
179bf 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 string returned
179c0 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 by.** sqlite3_er
179c1 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 33 rcode(), sqlite3
179c2 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 _errmsg() and sq
179c3 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 lite3_errmsg16()
179c4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
179c5 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 int sqlite3_fina
179c6 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d lize(sqlite3_stm
179c7 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 t *pStmt){. int
179c8 20 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 rc;. if( pStmt
179c9 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
179ca 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
179cb 73 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 se{. Vdbe *v
179cc 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a = (Vdbe*)pStmt;.
179cd 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
179ce 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 DSAFE. sqlite
179cf 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 3_mutex *mutex =
179d0 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 v->db->mutex;.#
179d1 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 endif. sqlite
179d2 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
179d3 74 65 78 29 3b 0a 20 20 20 20 73 74 6d 74 4c 72 tex);. stmtLr
179d4 75 52 65 6d 6f 76 65 28 76 29 3b 0a 20 20 20 20 uRemove(v);.
179d5 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
179d6 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 Finalize(v);.
179d7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
179d8 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d eave(mutex);. }
179d9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
179da 0a 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 ./*.** Terminate
179db 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 78 65 the current exe
179dc 63 75 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c cution of an SQL
179dd 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 statement and r
179de 65 73 65 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 eset it.** back
179df 74 6f 20 69 74 73 20 73 74 61 72 74 69 6e 67 20 to its starting
179e0 73 74 61 74 65 20 73 6f 20 74 68 61 74 20 69 74 state so that it
179e1 20 63 61 6e 20 62 65 20 72 65 75 73 65 64 2e 20 can be reused.
179e2 41 20 73 75 63 63 65 73 73 20 63 6f 64 65 20 66 A success code f
179e3 72 6f 6d 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 rom.** the prior
179e4 20 65 78 65 63 75 74 69 6f 6e 20 69 73 20 72 65 execution is re
179e5 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 turned..**.** Th
179e6 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
179e7 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 the error code a
179e8 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e nd string return
179e9 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 ed by.** sqlite3
179ea 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 _errcode(), sqli
179eb 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 te3_errmsg() and
179ec 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 sqlite3_errmsg1
179ed 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 6()..*/.SQLITE_A
179ee 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 PI int sqlite3_r
179ef 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d eset(sqlite3_stm
179f0 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 t *pStmt){. int
179f1 20 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 rc;. if( pStmt
179f2 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
179f3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
179f4 73 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 se{. Vdbe *v
179f5 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a = (Vdbe*)pStmt;.
179f6 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
179f7 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d x_enter(v->db->m
179f8 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 utex);. rc =
179f9 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 sqlite3VdbeReset
179fa 28 76 29 3b 0a 20 20 20 20 73 74 6d 74 4c 72 75 (v);. stmtLru
179fb 41 64 64 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 Add(v);. sqli
179fc 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
179fd 28 76 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 29 (v, -1, 0, 0, 0)
179fe 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72 ;. assert( (r
179ff 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 72 4d c & (v->db->errM
17a00 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20 20 20 ask))==rc );.
17a01 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
17a02 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 eave(v->db->mute
17a03 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e x);. }. return
17a04 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 rc;.}../*.** Se
17a05 74 20 61 6c 6c 20 74 68 65 20 70 61 72 61 6d 65 t all the parame
17a06 74 65 72 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 ters in the comp
17a07 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 iled SQL stateme
17a08 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 nt to NULL..*/.S
17a09 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
17a0a 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 lite3_clear_bind
17a0b 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ings(sqlite3_stm
17a0c 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 t *pStmt){. int
17a0d 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 i;. int rc = S
17a0e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 65 QLITE_OK;. Vdbe
17a0f 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 *p = (Vdbe*)pSt
17a10 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 mt;.#if SQLITE_T
17a11 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 HREADSAFE. sqli
17a12 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
17a13 20 3d 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74 = ((Vdbe*)pStmt
17a14 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 )->db->mutex;.#e
17a15 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d ndif. sqlite3_m
17a16 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
17a17 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c );. for(i=0; i<
17a18 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 p->nVar; i++){.
17a19 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
17a1a 6d 52 65 6c 65 61 73 65 28 26 70 2d 3e 61 56 61 mRelease(&p->aVa
17a1b 72 5b 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56 r[i]);. p->aV
17a1c 61 72 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 ar[i].flags = ME
17a1d 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71 M_Null;. }. sq
17a1e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
17a1f 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 e(mutex);. retu
17a20 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a rn rc;.}.../****
17a21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17a22 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 ******** sqlite3
17a23 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a _value_ *******
17a24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17a25 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
17a26 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
17a27 65 73 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 es extract infor
17a28 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 mation from a Me
17a29 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c m or sqlite3_val
17a2a 75 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ue.** structure.
17a2b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
17a2c 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
17a2d 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 e3_value_blob(sq
17a2e 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
17a2f 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 l){. Mem *p = (
17a30 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 Mem*)pVal;. if(
17a31 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d p->flags & (MEM
17a32 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 _Blob|MEM_Str) )
17a33 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
17a34 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 eMemExpandBlob(p
17a35 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 );. p->flags
17a36 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 &= ~MEM_Str;.
17a37 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d p->flags |= MEM
17a38 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 _Blob;. retur
17a39 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b n p->z;. }else{
17a3a 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
17a3b 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 te3_value_text(p
17a3c 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 Val);. }.}.SQLI
17a3d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
17a3e 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 e3_value_bytes(s
17a3f 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
17a40 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 al){. return sq
17a41 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 lite3ValueBytes(
17a42 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 pVal, SQLITE_UTF
17a43 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 8);.}.SQLITE_API
17a44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
17a45 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 ue_bytes16(sqlit
17a46 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b e3_value *pVal){
17a47 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
17a48 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 3ValueBytes(pVal
17a49 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 , SQLITE_UTF16NA
17a4a 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f TIVE);.}.SQLITE_
17a4b 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 API double sqlit
17a4c 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 e3_value_double(
17a4d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
17a4e 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 Val){. return s
17a4f 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
17a50 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b lue((Mem*)pVal);
17a51 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
17a52 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
17a53 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 int(sqlite3_valu
17a54 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e *pVal){. retu
17a55 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e rn sqlite3VdbeIn
17a56 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 tValue((Mem*)pVa
17a57 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 l);.}.SQLITE_API
17a58 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 sqlite_int64 sq
17a59 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 lite3_value_int6
17a5a 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 4(sqlite3_value
17a5b 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e *pVal){. return
17a5c 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
17a5d 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 alue((Mem*)pVal)
17a5e 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 ;.}.SQLITE_API c
17a5f 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
17a60 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 ar *sqlite3_valu
17a61 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 e_text(sqlite3_v
17a62 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 alue *pVal){. r
17a63 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 eturn (const uns
17a64 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c igned char *)sql
17a65 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 ite3ValueText(pV
17a66 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 al, SQLITE_UTF8)
17a67 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
17a68 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
17a69 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
17a6a 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c oid *sqlite3_val
17a6b 75 65 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 ue_text16(sqlite
17a6c 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 3_value* pVal){.
17a6d 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
17a6e 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 ValueText(pVal,
17a6f 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 SQLITE_UTF16NATI
17a70 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 VE);.}.SQLITE_AP
17a71 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
17a72 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
17a73 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 16be(sqlite3_val
17a74 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue *pVal){. ret
17a75 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 urn sqlite3Value
17a76 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 Text(pVal, SQLIT
17a77 45 5f 55 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51 E_UTF16BE);.}.SQ
17a78 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
17a79 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c oid *sqlite3_val
17a7a 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 ue_text16le(sqli
17a7b 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 te3_value *pVal)
17a7c 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
17a7d 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c e3ValueText(pVal
17a7e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 , SQLITE_UTF16LE
17a7f 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
17a80 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
17a81 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
17a82 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
17a83 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 _type(sqlite3_va
17a84 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 lue* pVal){. re
17a85 74 75 72 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b turn pVal->type;
17a86 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
17a87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17a88 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 * sqlite3_result
17a89 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a _ *************
17a8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17a8b 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
17a8c 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ing routines are
17a8d 20 75 73 65 64 20 62 79 20 75 73 65 72 2d 64 65 used by user-de
17a8e 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 fined functions
17a8f 74 6f 20 73 70 65 63 69 66 79 0a 2a 2a 20 74 68 to specify.** th
17a90 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c e function resul
17a91 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
17a92 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
17a93 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c sult_blob(. sql
17a94 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
17a95 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 tx, . const voi
17a96 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 d *z, . int n,
17a97 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
17a98 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 void *).){. ass
17a99 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 ert( n>=0 );. a
17a9a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
17a9b 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
17a9c 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
17a9d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
17a9e 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
17a9f 20 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b z, n, 0, xDel);
17aa0 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
17aa1 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
17aa2 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 t_double(sqlite3
17aa3 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
17aa4 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 double rVal){.
17aa5 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
17aa6 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
17aa7 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
17aa8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
17aa9 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 mSetDouble(&pCtx
17aaa 2d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 ->s, rVal);.}.SQ
17aab 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
17aac 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
17aad 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 or(sqlite3_conte
17aae 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 xt *pCtx, const
17aaf 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b char *z, int n){
17ab0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17ab1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
17ab2 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
17ab3 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 );. pCtx->isEr
17ab4 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 ror = SQLITE_ERR
17ab5 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 OR;. sqlite3Vdb
17ab6 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 eMemSetStr(&pCtx
17ab7 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 ->s, z, n, SQLIT
17ab8 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 E_UTF8, SQLITE_T
17ab9 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 RANSIENT);.}.#if
17aba 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17abb 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
17abc 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
17abd 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 esult_error16(sq
17abe 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
17abf 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 Ctx, const void
17ac0 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 *z, int n){. as
17ac1 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
17ac2 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
17ac3 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
17ac4 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d pCtx->isError =
17ac5 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
17ac6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
17ac7 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
17ac8 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
17ac9 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 16NATIVE, SQLITE
17aca 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 _TRANSIENT);.}.#
17acb 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 endif.SQLITE_API
17acc 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
17acd 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 sult_int(sqlite3
17ace 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
17acf 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 int iVal){. ass
17ad0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
17ad1 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
17ad2 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
17ad3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
17ad4 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c tInt64(&pCtx->s,
17ad5 20 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 (i64)iVal);.}.S
17ad6 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
17ad7 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
17ad8 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 t64(sqlite3_cont
17ad9 65 78 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 ext *pCtx, i64 i
17ada 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Val){. assert(
17adb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
17adc 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
17add 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
17ade 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
17adf 34 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 4(&pCtx->s, iVal
17ae0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
17ae1 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
17ae2 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 ult_null(sqlite3
17ae3 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b _context *pCtx){
17ae4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17ae5 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
17ae6 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
17ae7 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
17ae8 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 eMemSetNull(&pCt
17ae9 78 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f x->s);.}.SQLITE_
17aea 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
17aeb 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 _result_text(.
17aec 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
17aed 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 *pCtx, . const
17aee 63 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 char *z, . int
17aef 6e 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c n,. void (*xDel
17af0 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 )(void *).){. a
17af1 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
17af2 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
17af3 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
17af4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
17af5 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
17af6 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
17af7 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69 66 F8, xDel);.}.#if
17af8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17af9 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
17afa 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
17afb 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0a 20 20 esult_text16(.
17afc 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
17afd 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 *pCtx, . const
17afe 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 void *z, . int
17aff 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 n, . void (*xDe
17b00 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 l)(void *).){.
17b01 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
17b02 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
17b03 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
17b04 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
17b05 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 mSetStr(&pCtx->s
17b06 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 , z, n, SQLITE_U
17b07 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c TF16NATIVE, xDel
17b08 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
17b09 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
17b0a 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 ult_text16be(.
17b0b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
17b0c 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 *pCtx, . const
17b0d 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 void *z, . int
17b0e 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 n, . void (*xDe
17b0f 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 l)(void *).){.
17b10 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
17b11 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
17b12 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
17b13 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
17b14 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 mSetStr(&pCtx->s
17b15 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 , z, n, SQLITE_U
17b16 54 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d TF16BE, xDel);.}
17b17 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
17b18 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
17b19 74 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 text16le(. sqli
17b1a 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
17b1b 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 x, . const void
17b1c 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a *z, . int n, .
17b1d 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
17b1e 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 oid *).){. asse
17b1f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
17b20 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
17b21 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
17b22 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
17b23 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c Str(&pCtx->s, z,
17b24 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 n, SQLITE_UTF16
17b25 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e LE, xDel);.}.#en
17b26 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
17b27 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 IT_UTF16 */.SQLI
17b28 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
17b29 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
17b2a 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
17b2b 20 2a 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f *pCtx, sqlite3_
17b2c 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a value *pValue){.
17b2d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17b2e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
17b2f 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
17b30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
17b31 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 2d 3e 73 MemCopy(&pCtx->s
17b32 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c , pValue);.}.SQL
17b33 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
17b34 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f ite3_result_zero
17b35 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e blob(sqlite3_con
17b36 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 text *pCtx, int
17b37 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 n){. assert( sq
17b38 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
17b39 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
17b3a 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
17b3b 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c VdbeMemSetZeroBl
17b3c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e 29 3b ob(&pCtx->s, n);
17b3d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
17b3e 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
17b3f 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c t_error_code(sql
17b40 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
17b41 74 78 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 tx, int errCode)
17b42 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f {. pCtx->isErro
17b43 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a r = errCode;.}..
17b44 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49 /* Force an SQLI
17b45 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e TE_TOOBIG error.
17b46 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 */.SQLITE_API v
17b47 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
17b48 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 lt_error_toobig(
17b49 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
17b4a 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 *pCtx){. assert
17b4b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
17b4c 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
17b4d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 >mutex) );. pCt
17b4e 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c x->isError = SQL
17b4f 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71 ITE_TOOBIG;. sq
17b50 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
17b51 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74 tr(&pCtx->s, "st
17b52 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f ring or blob too
17b53 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20 big", -1, .
17b54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17b55 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 SQLITE_UTF8,
17b56 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a SQLITE_STATIC);.
17b57 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f }../* An SQLITE_
17b58 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a NOMEM error. */.
17b59 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
17b5a 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
17b5b 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 rror_nomem(sqlit
17b5c 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
17b5d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
17b5e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17b5f 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
17b60 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 x) );. sqlite3V
17b61 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 dbeMemSetNull(&p
17b62 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d Ctx->s);. pCtx-
17b63 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 >isError = SQLIT
17b64 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d E_NOMEM;. pCtx-
17b65 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 >s.db->mallocFai
17b66 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a led = 1;.}../*.*
17b67 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74 * Execute the st
17b68 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 atement pStmt, e
17b69 69 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f ither until a ro
17b6a 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 w of data is rea
17b6b 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 dy, the.** state
17b6c 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 ment is complete
17b6d 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61 ly executed or a
17b6e 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a n error occurs..
17b6f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
17b70 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 ne implements th
17b71 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f e bulk of the lo
17b72 67 69 63 20 62 65 68 69 6e 64 20 74 68 65 20 73 gic behind the s
17b73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20 qlite_step().**
17b74 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74 API. The only t
17b75 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20 hing omitted is
17b76 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65 the automatic re
17b77 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a compile if a .**
17b78 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68 schema change h
17b79 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68 as occurred. Th
17b7a 61 74 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e at detail is han
17b7b 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f dled by the.** o
17b7c 75 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 uter sqlite3_ste
17b7d 70 28 29 20 77 72 61 70 70 65 72 20 70 72 6f 63 p() wrapper proc
17b7e 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 edure..*/.static
17b7f 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70 int sqlite3Step
17b80 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c (Vdbe *p){. sql
17b81 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 ite3 *db;. int
17b82 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29 rc;.. assert(p)
17b83 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 ;. if( p->magic
17b84 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e !=VDBE_MAGIC_RUN
17b85 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
17b86 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
17b87 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 }.. /* Assert t
17b88 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 hat malloc() has
17b89 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20 not failed */.
17b8a 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 db = p->db;. i
17b8b 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
17b8c 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 led ){. retur
17b8d 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
17b8e 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 70 63 }.. if( p->pc
17b8f 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 69 72 65 <=0 && p->expire
17b90 64 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e d ){. if( p->
17b91 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17b92 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 . p->rc = S
17b93 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 QLITE_SCHEMA;.
17b94 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c }. rc = SQL
17b95 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 ITE_ERROR;. g
17b96 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 70 3b oto end_of_step;
17b97 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
17b98 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
17b99 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
17b9a 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 LITE_MISUSE;.
17b9b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d return SQLITE_M
17b9c 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 ISUSE;. }. if(
17b9d 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 20 20 p->pc<0 ){.
17b9e 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 /* If there are
17b9f 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 no other stateme
17ba0 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 nts currently ru
17ba1 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20 20 20 nning, then.
17ba2 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 ** reset the int
17ba3 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20 54 68 errupt flag. Th
17ba4 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 63 61 is prevents a ca
17ba5 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e ll to sqlite3_in
17ba6 74 65 72 72 75 70 74 0a 20 20 20 20 2a 2a 20 66 terrupt. ** f
17ba7 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69 6e 67 rom interrupting
17ba8 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 a statement tha
17ba9 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 73 74 t has not yet st
17baa 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 arted.. */.
17bab 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 if( db->active
17bac 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 VdbeCnt==0 ){.
17bad 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 db->u1.isInt
17bae 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 errupted = 0;.
17baf 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c }..#ifndef SQL
17bb0 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 ITE_OMIT_TRACE.
17bb1 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 66 if( db->xProf
17bb2 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 ile && !db->init
17bb3 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 64 .busy ){. d
17bb4 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 ouble rNow;.
17bb5 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 sqlite3OsCurre
17bb6 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c ntTime(db->pVfs,
17bb7 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20 20 70 &rNow);. p
17bb8 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d 20 28 72 ->startTime = (r
17bb9 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 Now - (int)rNow)
17bba 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 *3600.0*24.0*100
17bbb 30 30 30 30 30 30 30 2e 30 3b 0a 20 20 20 20 7d 0000000.0;. }
17bbc 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 64 62 2d .#endif.. db-
17bbd 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2b 2b >activeVdbeCnt++
17bbe 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61 ;. if( p->rea
17bbf 64 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d 3e 77 dOnly==0 ) db->w
17bc0 72 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 riteVdbeCnt++;.
17bc1 20 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a 20 20 p->pc = 0;.
17bc2 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 stmtLruRemove(
17bc3 70 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 p);. }.#ifndef
17bc4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
17bc5 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 AIN. if( p->exp
17bc6 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d lain ){. rc =
17bc7 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 sqlite3VdbeList
17bc8 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e (p);. }else.#en
17bc9 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
17bca 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20 20 IT_EXPLAIN */.
17bcb 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
17bcc 65 33 56 64 62 65 45 78 65 63 28 70 29 3b 0a 20 e3VdbeExec(p);.
17bcd 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }.. if( sqlite
17bce 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
17bcf 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
17bd0 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 23 E_MISUSE;. }..#
17bd1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
17bd2 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20 49 6e IT_TRACE. /* In
17bd3 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 69 6c 65 voke the profile
17bd4 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 callback if the
17bd5 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f 0a 20 re is one. */.
17bd6 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17bd7 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 72 6f 66 ROW && db->xProf
17bd8 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 ile && !db->init
17bd9 2e 62 75 73 79 20 26 26 20 70 2d 3e 6e 4f 70 3e .busy && p->nOp>
17bda 30 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 0. &&
17bdb 70 2d 3e 61 4f 70 5b 30 5d 2e 6f 70 63 6f 64 65 p->aOp[0].opcode
17bdc 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 2d ==OP_Trace && p-
17bdd 3e 61 4f 70 5b 30 5d 2e 70 34 2e 7a 21 3d 30 20 >aOp[0].p4.z!=0
17bde 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 4e ){. double rN
17bdf 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c 61 70 ow;. u64 elap
17be0 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73 71 6c seTime;.. sql
17be1 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d ite3OsCurrentTim
17be2 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f e(db->pVfs, &rNo
17be3 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 w);. elapseTi
17be4 6d 65 20 3d 20 28 72 4e 6f 77 20 2d 20 28 69 6e me = (rNow - (in
17be5 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 t)rNow)*3600.0*2
17be6 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 4.0*1000000000.0
17be7 20 2d 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 3b - p->startTime;
17be8 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c . db->xProfil
17be9 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 e(db->pProfileAr
17bea 67 2c 20 70 2d 3e 61 4f 70 5b 30 5d 2e 70 34 2e g, p->aOp[0].p4.
17beb 7a 2c 20 65 6c 61 70 73 65 54 69 6d 65 29 3b 0a z, elapseTime);.
17bec 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 }.#endif.. db
17bed 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a ->errCode = rc;.
17bee 20 20 2f 2a 73 71 6c 69 74 65 33 45 72 72 6f 72 /*sqlite3Error
17bef 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b 2a (p->db, rc, 0);*
17bf0 2f 0a 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 /. p->rc = sqli
17bf1 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 te3ApiExit(p->db
17bf2 2c 20 70 2d 3e 72 63 29 3b 0a 65 6e 64 5f 6f 66 , p->rc);.end_of
17bf3 5f 73 74 65 70 3a 0a 20 20 61 73 73 65 72 74 28 _step:. assert(
17bf4 20 28 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 (rc&0xff)==rc )
17bf5 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 53 71 6c 20 ;. if( p->zSql
17bf6 26 26 20 28 72 63 26 30 78 66 66 29 3c 53 51 4c && (rc&0xff)<SQL
17bf7 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f ITE_ROW ){. /
17bf8 2a 20 54 68 69 73 20 62 65 68 61 76 69 6f 72 20 * This behavior
17bf9 6f 63 63 75 72 73 20 69 66 20 73 71 6c 69 74 65 occurs if sqlite
17bfa 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20 77 3_prepare_v2() w
17bfb 61 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 as used to build
17bfc 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 72 65 70 . ** the prep
17bfd 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 20 ared statement.
17bfe 20 52 65 74 75 72 6e 20 65 72 72 6f 72 20 63 6f Return error co
17bff 64 65 73 20 64 69 72 65 63 74 6c 79 20 2a 2f 0a des directly */.
17c00 20 20 20 20 70 2d 3e 64 62 2d 3e 65 72 72 43 6f p->db->errCo
17c01 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20 de = p->rc;.
17c02 2f 2a 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 /* sqlite3Error(
17c03 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29 p->db, p->rc, 0)
17c04 3b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 ; */. return
17c05 70 2d 3e 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a p->rc;. }else{.
17c06 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 66 /* This is f
17c07 6f 72 20 6c 65 67 61 63 79 20 73 71 6c 69 74 65 or legacy sqlite
17c08 33 5f 70 72 65 70 61 72 65 28 29 20 62 75 69 6c 3_prepare() buil
17c09 64 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 ds and when the
17c0a 63 6f 64 65 0a 20 20 20 20 2a 2a 20 69 73 20 53 code. ** is S
17c0b 51 4c 49 54 45 5f 52 4f 57 20 6f 72 20 53 51 4c QLITE_ROW or SQL
17c0c 49 54 45 5f 44 4f 4e 45 20 2a 2f 0a 20 20 20 20 ITE_DONE */.
17c0d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d return rc;. }.}
17c0e 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ../*.** This is
17c0f 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 69 6d the top-level im
17c10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
17c11 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 sqlite3_step().
17c12 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 Call.** sqlite3
17c13 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d 6f 73 Step() to do mos
17c14 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e 20 20 t of the work.
17c15 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 72 6f If a schema erro
17c16 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 61 6c r occurs,.** cal
17c17 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 l sqlite3Reprepa
17c18 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 67 61 re() and try aga
17c19 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 in..*/.#ifdef SQ
17c1a 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 LITE_OMIT_PARSER
17c1b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
17c1c 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c sqlite3_step(sql
17c1d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
17c1e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
17c1f 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 LITE_MISUSE;. i
17c20 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 f( pStmt ){.
17c21 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 76 20 3d Vdbe *v;. v =
17c22 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
17c23 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
17c24 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75 _enter(v->db->mu
17c25 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 tex);. rc = s
17c26 71 6c 69 74 65 33 53 74 65 70 28 76 29 3b 0a 20 qlite3Step(v);.
17c27 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
17c28 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 _leave(v->db->mu
17c29 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
17c2a 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65 0a 53 rn rc;.}.#else.S
17c2b 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
17c2c 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69 74 lite3_step(sqlit
17c2d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
17c2e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
17c2f 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 66 28 TE_MISUSE;. if(
17c30 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 69 6e pStmt ){. in
17c31 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 56 t cnt = 0;. V
17c32 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 dbe *v = (Vdbe*)
17c33 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 pStmt;. sqlit
17c34 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b 0a e3 *db = v->db;.
17c35 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
17c36 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 x_enter(db->mute
17c37 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 x);. while( (
17c38 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65 70 rc = sqlite3Step
17c39 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43 48 (v))==SQLITE_SCH
17c3a 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 20 26 EMA. &
17c3b 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 20 20 & cnt++ < 5.
17c3c 20 20 20 20 20 20 20 26 26 20 76 64 62 65 52 65 && vdbeRe
17c3d 70 72 65 70 61 72 65 28 76 29 20 29 7b 0a 20 20 prepare(v) ){.
17c3e 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 sqlite3_rese
17c3f 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 t(pStmt);.
17c40 76 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a v->expired = 0;.
17c41 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
17c42 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 ==SQLITE_SCHEMA
17c43 26 26 20 76 2d 3e 7a 53 71 6c 20 26 26 20 64 62 && v->zSql && db
17c44 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20 20 20 ->pErr ){.
17c45 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63 63 /* This case occ
17c46 75 72 73 20 61 66 74 65 72 20 66 61 69 6c 69 6e urs after failin
17c47 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 20 61 g to recompile a
17c48 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e 74 2e n sql statement.
17c49 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65 . ** The e
17c4a 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f rror message fro
17c4b 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70 69 6c m the SQL compil
17c4c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 er has already b
17c4d 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20 20 20 een loaded .
17c4e 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 61 ** into the da
17c4f 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 tabase handle. T
17c50 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 his block copies
17c51 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
17c52 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f ge . ** fro
17c53 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 m the database h
17c54 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 20 73 andle into the s
17c55 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 65 74 tatement and set
17c56 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a s the statement.
17c57 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d ** program
17c58 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 74 6f counter to 0 to
17c59 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 68 65 ensure that whe
17c5a 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 n the statement
17c5b 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e is . ** fin
17c5c 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65 74 20 alized or reset
17c5d 74 68 65 20 70 61 72 73 65 72 20 65 72 72 6f 72 the parser error
17c5e 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 message is avai
17c5f 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20 20 20 lable via.
17c60 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 ** sqlite3_errms
17c61 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f g() and sqlite3_
17c62 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20 20 20 errcode()..
17c63 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 */. const
17c64 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28 63 6f char *zErr = (co
17c65 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 nst char *)sqlit
17c66 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64 62 e3_value_text(db
17c67 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20 20 20 ->pErr); .
17c68 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
17c69 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , v->zErrMsg);.
17c6a 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 if( !db->ma
17c6b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
17c6c 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 v->zErrMsg
17c6d 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
17c6e 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 up(db, zErr);.
17c6f 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
17c70 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 v->zErrMsg
17c71 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 76 2d 3e = 0;. v->
17c72 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
17c73 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d M;. }. }
17c74 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
17c75 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3ApiExit(db, rc)
17c76 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
17c77 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 tex_leave(db->mu
17c78 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
17c79 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a rn rc;.}.#endif.
17c7a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 ./*.** Extract t
17c7b 68 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f he user data fro
17c7c 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 m a sqlite3_cont
17c7d 65 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e ext structure an
17c7e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f d return a.** po
17c7f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a inter to it..*/.
17c80 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
17c81 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 *sqlite3_user_da
17c82 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ta(sqlite3_conte
17c83 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 xt *p){. assert
17c84 28 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 ( p && p->pFunc
17c85 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 );. return p->p
17c86 46 75 6e 63 2d 3e 70 55 73 65 72 44 61 74 61 3b Func->pUserData;
17c87 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 .}../*.** Extrac
17c88 74 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 t the user data
17c89 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 from a sqlite3_c
17c8a 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 ontext structure
17c8b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a and return a.**
17c8c 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a pointer to it..
17c8d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
17c8e 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 lite3 *sqlite3_c
17c8f 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
17c90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
17c91 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
17c92 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b p && p->pFunc );
17c93 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 73 2e 64 . return p->s.d
17c94 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 b;.}../*.** The
17c95 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 68 65 following is the
17c96 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
17c97 6f 66 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 of an SQL functi
17c98 6f 6e 20 74 68 61 74 20 61 6c 77 61 79 73 0a 2a on that always.*
17c99 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 * fails with an
17c9a 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 error message st
17c9b 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 66 ating that the f
17c9c 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
17c9d 69 6e 20 74 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 in the.** wrong
17c9e 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 73 71 context. The sq
17c9f 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 lite3_overload_f
17ca0 75 6e 63 74 69 6f 6e 28 29 20 41 50 49 20 6d 69 unction() API mi
17ca1 67 68 74 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a ght construct.**
17ca2 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 SQL function th
17ca3 61 74 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 at use this rout
17ca4 69 6e 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 ine so that the
17ca5 66 75 6e 63 74 69 6f 6e 73 20 77 69 6c 6c 20 65 functions will e
17ca6 78 69 73 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 xist.** for name
17ca7 20 72 65 73 6f 6c 75 74 69 6f 6e 20 62 75 74 20 resolution but
17ca8 61 72 65 20 61 63 74 75 61 6c 6c 79 20 6f 76 65 are actually ove
17ca9 72 6c 6f 61 64 65 64 20 62 79 20 74 68 65 20 78 rloaded by the x
17caa 46 69 6e 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 FindFunction.**
17cab 6d 65 74 68 6f 64 20 6f 66 20 76 69 72 74 75 61 method of virtua
17cac 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c l tables..*/.SQL
17cad 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
17cae 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 sqlite3InvalidF
17caf 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 unction(. sqlit
17cb0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
17cb1 65 78 74 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e ext, /* The fun
17cb2 63 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f ction calling co
17cb3 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 61 ntext */. int a
17cb4 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 rgc,
17cb5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
17cb6 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 of arguments to
17cb7 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a the function */.
17cb8 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
17cb9 2a 2a 61 72 67 76 20 20 20 20 20 20 20 2f 2a 20 **argv /*
17cba 56 61 6c 75 65 20 6f 66 20 65 61 63 68 20 61 72 Value of each ar
17cbb 67 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 gument */.){. c
17cbc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
17cbd 20 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46 75 6e = context->pFun
17cbe 63 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 68 61 72 c->zName;. char
17cbf 20 2a 7a 45 72 72 3b 0a 20 20 7a 45 72 72 20 3d *zErr;. zErr =
17cc0 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
17cc1 30 2c 0a 20 20 20 20 20 20 22 75 6e 61 62 6c 65 0,. "unable
17cc2 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e to use function
17cc3 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 75 65 %s in the reque
17cc4 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c 20 7a sted context", z
17cc5 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 Name);. sqlite3
17cc6 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f _result_error(co
17cc7 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 ntext, zErr, -1)
17cc8 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
17cc9 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a (zErr);.}../*.**
17cca 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 65 74 Allocate or ret
17ccb 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 urn the aggregat
17ccc 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 61 20 e context for a
17ccd 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 user function.
17cce 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 78 74 A new.** context
17ccf 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e is allocated on
17cd0 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 2e the first call.
17cd1 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c Subsequent cal
17cd2 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a ls return the.**
17cd3 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 68 same context th
17cd4 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 at was returned
17cd5 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a on prior calls..
17cd6 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
17cd7 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 id *sqlite3_aggr
17cd8 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 egate_context(sq
17cd9 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
17cda 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 , int nByte){.
17cdb 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 Mem *pMem;. ass
17cdc 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75 ert( p && p->pFu
17cdd 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e nc && p->pFunc->
17cde 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 65 72 xStep );. asser
17cdf 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
17ce0 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d _held(p->s.db->m
17ce1 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 6d 20 utex) );. pMem
17ce2 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 66 28 = p->pMem;. if(
17ce3 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 (pMem->flags &
17ce4 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b 0a 20 MEM_Agg)==0 ){.
17ce5 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 if( nByte==0
17ce6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
17ce7 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 VdbeMemReleaseEx
17ce8 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20 20 ternal(pMem);.
17ce9 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
17cea 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
17ceb 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 pMem->z = 0;.
17cec 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17ced 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
17cee 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 ow(pMem, nByte,
17cef 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 0);. pMem->
17cf0 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 67 3b flags = MEM_Agg;
17cf1 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 . pMem->u.p
17cf2 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a Def = p->pFunc;.
17cf3 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e if( pMem->
17cf4 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d z ){. mem
17cf5 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 set(pMem->z, 0,
17cf6 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a nByte);. }.
17cf7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
17cf8 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e rn (void*)pMem->
17cf9 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 z;.}../*.** Retu
17cfa 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79 20 rn the auxilary
17cfb 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69 66 data pointer, if
17cfc 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69 41 any, for the iA
17cfd 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20 74 rg'th argument t
17cfe 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66 75 o.** the user-fu
17cff 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 nction defined b
17d00 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 y pCtx..*/.SQLIT
17d01 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
17d02 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
17d03 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
17d04 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 67 29 *pCtx, int iArg)
17d05 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 {. VdbeFunc *pV
17d06 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 73 65 dbeFunc;.. asse
17d07 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
17d08 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
17d09 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
17d0a 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d VdbeFunc = pCtx-
17d0b 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 >pVdbeFunc;. if
17d0c 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 ( !pVdbeFunc ||
17d0d 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d iArg>=pVdbeFunc-
17d0e 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c 30 20 >nAux || iArg<0
17d0f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
17d10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 56 . }. return pV
17d11 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 dbeFunc->apAux[i
17d12 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a Arg].pAux;.}../*
17d13 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 78 69 .** Set the auxi
17d14 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 lary data pointe
17d15 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 75 6e r and delete fun
17d16 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 20 69 ction, for the i
17d17 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 6d 65 Arg'th.** argume
17d18 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 2d 66 nt to the user-f
17d19 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 unction defined
17d1a 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 72 65 by pCtx. Any pre
17d1b 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a vious value is.*
17d1c 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 61 6c * deleted by cal
17d1d 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 20 ling the delete
17d1e 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 function specifi
17d1f 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 20 73 ed when it was s
17d20 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 et..*/.SQLITE_AP
17d21 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 I void sqlite3_s
17d22 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73 71 et_auxdata(. sq
17d23 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
17d24 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 72 67 Ctx, . int iArg
17d25 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c , . void *pAux,
17d26 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 . void (*xDele
17d27 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 te)(void*).){.
17d28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a struct AuxData *
17d29 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 62 65 pAuxData;. Vdbe
17d2a 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b Func *pVdbeFunc;
17d2b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 29 20 . if( iArg<0 )
17d2c 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 20 goto failed;..
17d2d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
17d2e 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
17d2f 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
17d30 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 . pVdbeFunc = p
17d31 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a Ctx->pVdbeFunc;.
17d32 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 if( !pVdbeFunc
17d33 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e || pVdbeFunc->n
17d34 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 20 20 Aux<=iArg ){.
17d35 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 56 64 int nAux = (pVd
17d36 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 46 75 beFunc ? pVdbeFu
17d37 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b 0a 20 nc->nAux : 0);.
17d38 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d int nMalloc =
17d39 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e 63 sizeof(VdbeFunc
17d3a 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75 63 ) + sizeof(struc
17d3b 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 67 3b t AuxData)*iArg;
17d3c 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 20 3d . pVdbeFunc =
17d3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
17d3e 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 70 56 c(pCtx->s.db, pV
17d3f 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 dbeFunc, nMalloc
17d40 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64 62 );. if( !pVdb
17d41 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 67 eFunc ){. g
17d42 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
17d43 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64 62 }. pCtx->pVdb
17d44 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75 6e eFunc = pVdbeFun
17d45 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 c;. memset(&p
17d46 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b VdbeFunc->apAux[
17d47 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 nAux], 0, sizeof
17d48 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29 (struct AuxData)
17d49 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b *(iArg+1-nAux));
17d4a 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e . pVdbeFunc->
17d4b 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b 0a 20 nAux = iArg+1;.
17d4c 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 pVdbeFunc->pF
17d4d 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e unc = pCtx->pFun
17d4e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 44 61 c;. }.. pAuxDa
17d4f 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d ta = &pVdbeFunc-
17d50 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a 20 20 >apAux[iArg];.
17d51 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e 70 41 if( pAuxData->pA
17d52 75 78 20 26 26 20 70 41 75 78 44 61 74 61 2d 3e ux && pAuxData->
17d53 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 70 xDelete ){. p
17d54 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 AuxData->xDelete
17d55 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 29 (pAuxData->pAux)
17d56 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 74 61 ;. }. pAuxData
17d57 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 ->pAux = pAux;.
17d58 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 pAuxData->xDele
17d59 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a 20 20 te = xDelete;.
17d5a 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a return;..failed:
17d5b 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 20 29 . if( xDelete )
17d5c 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 70 41 {. xDelete(pA
17d5d 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e ux);. }.}..#ifn
17d5e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
17d5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a DEPRECATED./*.**
17d60 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
17d61 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20 er of times the
17d62 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66 Step function of
17d63 20 61 20 61 67 67 72 65 67 61 74 65 20 68 61 73 a aggregate has
17d64 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 been .** called
17d65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
17d66 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 ction is depreca
17d67 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65 ted. Do not use
17d68 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f 64 65 it for new code
17d69 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 . It is.** prov
17d6a 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69 ide only to avoi
17d6b 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 61 63 d breaking legac
17d6c 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 67 67 y code. New agg
17d6d 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a regate function.
17d6e 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
17d6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 20 74 ns should keep t
17d70 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20 heir own counts
17d71 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 67 67 within their agg
17d72 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78 regate.** contex
17d73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
17d74 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 int sqlite3_agg
17d75 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c regate_count(sql
17d76 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 ite3_context *p)
17d77 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 {. assert( p &&
17d78 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e p->pFunc && p->
17d79 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a pFunc->xStep );.
17d7a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d return p->pMem
17d7b 2d 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ->n;.}.#endif../
17d7c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
17d7d 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
17d7e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
17d7f 73 65 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 set for the stat
17d80 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a ement pStmt..*/.
17d81 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
17d82 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f qlite3_column_co
17d83 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 unt(sqlite3_stmt
17d84 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 *pStmt){. Vdbe
17d85 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 *pVm = (Vdbe *)
17d86 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 pStmt;. return
17d87 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 pVm ? pVm->nResC
17d88 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a olumn : 0;.}../*
17d89 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
17d8a 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 umber of values
17d8b 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 available from t
17d8c 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f he current row o
17d8d 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 f the.** current
17d8e 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 ly executing sta
17d8f 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f tement pStmt..*/
17d90 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
17d91 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 sqlite3_data_cou
17d92 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 nt(sqlite3_stmt
17d93 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
17d94 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 *pVm = (Vdbe *)p
17d95 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d Stmt;. if( pVm=
17d96 3d 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 =0 || pVm->pResu
17d97 6c 74 53 65 74 3d 3d 30 20 29 20 72 65 74 75 72 ltSet==0 ) retur
17d98 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 n 0;. return pV
17d99 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d m->nResColumn;.}
17d9a 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 .../*.** Check t
17d9b 6f 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 o see if column
17d9c 69 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 iCol of the give
17d9d 6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 n statement is v
17d9e 61 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 alid. If.** it
17d9f 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 is, return a poi
17da0 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 nter to the Mem
17da1 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 for the value of
17da2 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a that column..**
17da3 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 If iCol is not
17da4 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20 valid, return a
17da5 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d pointer to a Mem
17da6 20 77 68 69 63 68 20 68 61 73 20 61 20 76 61 6c which has a val
17da7 75 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a ue.** of NULL..*
17da8 2f 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f /.static Mem *co
17da9 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f lumnMem(sqlite3_
17daa 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
17dab 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d i){. Vdbe *pVm
17dac 3b 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 ;. int vals;.
17dad 4d 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 Mem *pOut;.. pV
17dae 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d m = (Vdbe *)pStm
17daf 74 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20 t;. if( pVm &&
17db0 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 pVm->pResultSet!
17db1 3d 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 =0 && i<pVm->nRe
17db2 73 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 sColumn && i>=0
17db3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
17db4 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e utex_enter(pVm->
17db5 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 db->mutex);.
17db6 76 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 vals = sqlite3_d
17db7 61 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 ata_count(pStmt)
17db8 3b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 56 ;. pOut = &pV
17db9 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d m->pResultSet[i]
17dba 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
17dbb 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65 6d 20 tatic const Mem
17dbc 6e 75 6c 6c 4d 65 6d 20 3d 20 7b 7b 30 7d 2c 20 nullMem = {{0},
17dbd 30 2e 30 2c 20 30 2c 20 22 22 2c 20 30 2c 20 4d 0.0, 0, "", 0, M
17dbe 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f EM_Null, SQLITE_
17dbf 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 7d 3b NULL, 0, 0, 0 };
17dc0 0a 20 20 20 20 69 66 28 20 70 56 6d 2d 3e 64 62 . if( pVm->db
17dc1 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
17dc2 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 3_mutex_enter(pV
17dc3 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 m->db->mutex);.
17dc4 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
17dc5 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54 r(pVm->db, SQLIT
17dc6 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 E_RANGE, 0);.
17dc7 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d }. pOut = (M
17dc8 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 em*)&nullMem;.
17dc9 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b }. return pOut;
17dca 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
17dcb 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
17dcc 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 d after invoking
17dcd 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 an sqlite3_valu
17dce 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f e_XXX function o
17dcf 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 n a .** column v
17dd0 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c alue (i.e. a val
17dd1 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 65 ue returned by e
17dd2 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c valuating an SQL
17dd3 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 expression in t
17dd4 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73 he.** select lis
17dd5 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 t of a SELECT st
17dd6 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61 atement) that ma
17dd7 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63 y cause a malloc
17dd8 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a () failure. If .
17dd9 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 ** malloc() has
17dda 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65 failed, the thre
17ddb 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 ads mallocFailed
17ddc 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 flag is cleared
17ddd 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a and the result.
17dde 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65 ** code of state
17ddf 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74 ment pStmt set t
17de0 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a o SQLITE_NOMEM..
17de1 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c **.** Specifical
17de2 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c ly, this is call
17de3 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a ed from within:.
17de4 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 **.** sqlite
17de5 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 3_column_int().*
17de6 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
17de7 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 lumn_int64().**
17de8 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 sqlite3_colu
17de9 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20 mn_text().**
17dea 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
17deb 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20 text16().**
17dec 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 sqlite3_column_r
17ded 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c eal().** sql
17dee 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
17def 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 s().** sqlit
17df0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 e3_column_bytes1
17df1 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 6().**.** But no
17df2 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f t for sqlite3_co
17df3 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69 lumn_blob(), whi
17df4 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d ch never calls m
17df5 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 alloc()..*/.stat
17df6 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 ic void columnMa
17df7 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69 llocFailure(sqli
17df8 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
17df9 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f .{. /* If mallo
17dfa 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e c() failed durin
17dfb 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f g an encoding co
17dfc 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20 nversion within
17dfd 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f an. ** sqlite3_
17dfe 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20 column_XXX API,
17dff 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 65 74 then set the ret
17e00 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20 urn code of the
17e01 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a statement to. *
17e02 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 * SQLITE_NOMEM.
17e03 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f The next call to
17e04 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e 79 _step() (if any
17e05 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 ) will return SQ
17e06 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 LITE_ERROR. **
17e07 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20 and _finalize()
17e08 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45 will return NOME
17e09 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a M.. */. Vdbe *
17e0a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d p = (Vdbe *)pStm
17e0b 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 t;. if( p ){.
17e0c 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65 p->rc = sqlite
17e0d 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 3ApiExit(p->db,
17e0e 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 p->rc);. sqli
17e0f 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
17e10 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
17e11 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
17e12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17e13 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 *** sqlite3_colu
17e14 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mn_ ***********
17e15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17e16 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ****.** The foll
17e17 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 owing routines a
17e18 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 re used to acces
17e19 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 s elements of th
17e1a 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a e current row.**
17e1b 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
17e1c 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 et..*/.SQLITE_AP
17e1d 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
17e1e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f lite3_column_blo
17e1f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a b(sqlite3_stmt *
17e20 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
17e21 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c const void *val
17e22 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 ;. val = sqlite
17e23 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f 3_value_blob( co
17e24 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 lumnMem(pStmt,i)
17e25 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 );. /* Even th
17e26 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f ough there is no
17e27 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 encoding conver
17e28 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62 sion, value_blob
17e29 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65 () might. ** ne
17e2a 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f ed to call mallo
17e2b 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68 c() to expand th
17e2c 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65 e result of a ze
17e2d 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65 roblob() . ** e
17e2e 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f xpression. . */
17e2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
17e30 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
17e31 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
17e32 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
17e33 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
17e34 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 es(sqlite3_stmt
17e35 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
17e36 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 int val = sqli
17e37 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
17e38 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
17e39 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
17e3a 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
17e3b 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 mt);. return va
17e3c 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 l;.}.SQLITE_API
17e3d 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 int sqlite3_colu
17e3e 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 mn_bytes16(sqlit
17e3f 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
17e40 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 int i){. int va
17e41 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
17e42 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d e_bytes16( colum
17e43 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
17e44 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
17e45 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
17e46 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
17e47 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 QLITE_API double
17e48 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
17e49 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 double(sqlite3_s
17e4a 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
17e4b 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c i){. double val
17e4c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
17e4d 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d _double( columnM
17e4e 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
17e4f 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
17e50 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
17e51 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
17e52 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
17e53 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 te3_column_int(s
17e54 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
17e55 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e mt, int i){. in
17e56 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f t val = sqlite3_
17e57 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d value_int( colum
17e58 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
17e59 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
17e5a 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
17e5b 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
17e5c 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
17e5d 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 _int64 sqlite3_c
17e5e 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 olumn_int64(sqli
17e5f 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
17e60 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74 int i){. sqlit
17e61 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71 e_int64 val = sq
17e62 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 lite3_value_int6
17e63 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 4( columnMem(pSt
17e64 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
17e65 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
17e66 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
17e67 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
17e68 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
17e69 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
17e6a 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 olumn_text(sqlit
17e6b 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
17e6c 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 int i){. const
17e6d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76 unsigned char *v
17e6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
17e6f 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d ue_text( columnM
17e70 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
17e71 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
17e72 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
17e73 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
17e74 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
17e75 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 value *sqlite3_c
17e76 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 olumn_value(sqli
17e77 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
17e78 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a int i){. Mem *
17e79 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d pOut = columnMem
17e7a 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 66 (pStmt, i);. if
17e7b 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d 45 ( pOut->flags&ME
17e7c 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 M_Static ){.
17e7d 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pOut->flags &= ~
17e7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 MEM_Static;.
17e7f 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pOut->flags |= M
17e80 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 20 EM_Ephem;. }.
17e81 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c columnMallocFail
17e82 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 ure(pStmt);. re
17e83 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 61 turn (sqlite3_va
17e84 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69 lue *)pOut;.}.#i
17e85 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
17e86 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 T_UTF16.SQLITE_A
17e87 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
17e88 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
17e89 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d xt16(sqlite3_stm
17e8a 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
17e8b 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a {. const void *
17e8c 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
17e8d 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f 6c 75 lue_text16( colu
17e8e 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
17e8f 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
17e90 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
17e91 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a return val;.}.
17e92 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
17e93 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 _OMIT_UTF16 */.S
17e94 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
17e95 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 lite3_column_typ
17e96 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
17e97 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
17e98 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71 6c int iType = sql
17e99 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
17e9a 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
17e9b 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
17e9c 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
17e9d 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 54 mt);. return iT
17e9e 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 ype;.}../* The f
17e9f 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f ollowing functio
17ea0 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 n is experimenta
17ea1 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f l and subject to
17ea2 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 change or.** re
17ea3 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 moval */./*int s
17ea4 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 qlite3_column_nu
17ea5 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 meric_type(sqlit
17ea6 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
17ea7 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75 int i){.** retu
17ea8 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 rn sqlite3_value
17ea9 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 20 63 _numeric_type( c
17eaa 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
17eab 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a ) );.**}.*/../*.
17eac 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e ** Convert the N
17ead 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 -th element of p
17eae 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d Stmt->pColName[]
17eaf 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75 into a string u
17eb0 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20 sing.** xFunc()
17eb1 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74 then return that
17eb2 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69 string. If N i
17eb3 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 s out of range,
17eb4 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 return 0..**.**
17eb5 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f 20 There are up to
17eb6 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 68 5 names for each
17eb7 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70 column. useTyp
17eb8 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 e determines whi
17eb9 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65 ch.** name is re
17eba 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 61 72 turned. Here ar
17ebb 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a e the names:.**.
17ebc 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 68 65 ** 0 The
17ebd 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 column name as
17ebe 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 69 73 it should be dis
17ebf 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 70 75 played for outpu
17ec0 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 20 54 t.** 1 T
17ec1 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 6d 65 he datatype name
17ec2 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a for the column.
17ec3 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 68 65 ** 2 The
17ec4 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
17ec5 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 63 abase that the c
17ec6 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 olumn derives fr
17ec7 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 20 om.** 3
17ec8 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
17ec9 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 63 table that the c
17eca 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 olumn derives fr
17ecb 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 20 om.** 4
17ecc 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
17ecd 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 table column tha
17ece 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c t the result col
17ecf 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d umn derives from
17ed0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 .**.** If the re
17ed1 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 69 sult is not a si
17ed2 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 mple column refe
17ed3 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 73 20 rence (if it is
17ed4 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a an expression.**
17ed5 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 20 or a constant)
17ed6 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 32 2c then useTypes 2,
17ed7 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 72 6e 3, and 4 return
17ed8 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 NULL..*/.static
17ed9 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c const void *col
17eda 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74 umnName(. sqlit
17edb 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a e3_stmt *pStmt,.
17edc 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 int N,. const
17edd 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28 void *(*xFunc)(
17ede 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65 Mem*),. int use
17edf 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 Type.){. const
17ee0 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20 void *ret = 0;.
17ee1 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
17ee2 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 *)pStmt;. int
17ee3 6e 3b 0a 20 20 0a 0a 20 20 69 66 28 20 70 21 3d n;. .. if( p!=
17ee4 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 0 ){. n = sql
17ee5 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e ite3_column_coun
17ee6 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 t(pStmt);. if
17ee7 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 29 7b ( N<n && N>=0 ){
17ee8 0a 20 20 20 20 20 20 4e 20 2b 3d 20 75 73 65 54 . N += useT
17ee9 79 70 65 2a 6e 3b 0a 20 20 20 20 20 20 73 71 6c ype*n;. sql
17eea 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
17eeb 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
17eec 20 20 20 20 20 20 72 65 74 20 3d 20 78 46 75 6e ret = xFun
17eed 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e c(&p->aColName[N
17eee 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 20 ]);.. /* A
17eef 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 malloc may have
17ef0 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 failed inside of
17ef1 20 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c the xFunc() cal
17ef2 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 20 l. If this.
17ef3 20 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c ** is the case,
17ef4 20 63 6c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f clear the mallo
17ef5 63 46 61 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 cFailed flag and
17ef6 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 return NULL..
17ef7 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
17ef8 20 70 2d 3e 64 62 20 26 26 20 70 2d 3e 64 62 2d p->db && p->db-
17ef9 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
17efa 0a 20 20 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e . p->db->
17efb 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 mallocFailed = 0
17efc 3b 0a 20 20 20 20 20 20 20 20 72 65 74 20 3d 20 ;. ret =
17efd 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
17efe 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
17eff 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
17f00 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 x);. }. }.
17f01 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f return ret;.}../
17f02 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
17f03 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e 74 68 20 name of the Nth
17f04 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 column of the re
17f05 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 sult set returne
17f06 64 20 62 79 20 53 51 4c 0a 2a 2a 20 73 74 61 74 d by SQL.** stat
17f07 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a ement pStmt..*/.
17f08 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
17f09 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
17f0a 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 olumn_name(sqlit
17f0b 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
17f0c 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
17f0d 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
17f0e 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
17f0f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
17f10 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
17f11 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e _text, COLNAME_N
17f12 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 AME);.}.#ifndef
17f13 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
17f14 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 6.SQLITE_API con
17f15 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
17f16 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 _column_name16(s
17f17 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
17f18 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
17f19 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
17f1a 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
17f1b 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
17f1c 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
17f1d 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c alue_text16, COL
17f1e 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 65 NAME_NAME);.}.#e
17f1f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 ndif../*.** Cons
17f20 74 72 61 69 6e 74 3a 20 20 49 66 20 79 6f 75 20 traint: If you
17f21 68 61 76 65 20 45 4e 41 42 4c 45 5f 43 4f 4c 55 have ENABLE_COLU
17f22 4d 4e 5f 4d 45 54 41 44 41 54 41 20 74 68 65 6e MN_METADATA then
17f23 20 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 6e 6f 74 you must.** not
17f24 20 64 65 66 69 6e 65 20 4f 4d 49 54 5f 44 45 43 define OMIT_DEC
17f25 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 20 64 65 LTYPE..*/.#if de
17f26 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
17f27 54 5f 44 45 43 4c 54 59 50 45 29 20 26 26 20 64 T_DECLTYPE) && d
17f28 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
17f29 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 ABLE_COLUMN_META
17f2a 44 41 54 41 29 0a 23 20 65 72 72 6f 72 20 22 4d DATA).# error "M
17f2b 75 73 74 20 6e 6f 74 20 64 65 66 69 6e 65 20 62 ust not define b
17f2c 6f 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f oth SQLITE_OMIT_
17f2d 44 45 43 4c 54 59 50 45 20 5c 0a 20 20 20 20 20 DECLTYPE \.
17f2e 20 20 20 20 61 6e 64 20 53 51 4c 49 54 45 5f 45 and SQLITE_E
17f2f 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 NABLE_COLUMN_MET
17f30 41 44 41 54 41 22 0a 23 65 6e 64 69 66 0a 0a 23 ADATA".#endif..#
17f31 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
17f32 49 54 5f 44 45 43 4c 54 59 50 45 0a 2f 2a 0a 2a IT_DECLTYPE./*.*
17f33 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c * Return the col
17f34 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 umn declaration
17f35 74 79 70 65 20 28 69 66 20 61 70 70 6c 69 63 61 type (if applica
17f36 62 6c 65 29 20 6f 66 20 74 68 65 20 27 69 27 74 ble) of the 'i't
17f37 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 h column.** of t
17f38 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 he result set of
17f39 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 SQL statement p
17f3a 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f Stmt..*/.SQLITE_
17f3b 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
17f3c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 sqlite3_column_d
17f3d 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f ecltype(sqlite3_
17f3e 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
17f3f 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
17f40 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
17f41 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
17f42 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
17f43 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
17f44 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c xt, COLNAME_DECL
17f45 54 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 TYPE);.}.#ifndef
17f46 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
17f47 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 16.SQLITE_API co
17f48 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
17f49 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 3_column_decltyp
17f4a 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 e16(sqlite3_stmt
17f4b 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
17f4c 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
17f4d 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
17f4e 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
17f4f 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
17f50 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
17f51 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 , COLNAME_DECLTY
17f52 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a PE);.}.#endif /*
17f53 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
17f54 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 16 */.#endif /*
17f55 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
17f56 54 59 50 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20 TYPE */..#ifdef
17f57 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f SQLITE_ENABLE_CO
17f58 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 2f 2a LUMN_METADATA./*
17f59 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
17f5a 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
17f5b 61 73 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 ase from which a
17f5c 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 result column d
17f5d 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 erives..** NULL
17f5e 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
17f5f 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e he result column
17f60 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f is an expressio
17f61 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 n or constant or
17f62 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 .** anything els
17f63 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 e which is not a
17f64 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 n unabiguous ref
17f65 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 erence to a data
17f66 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a base column..*/.
17f67 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
17f68 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
17f69 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e olumn_database_n
17f6a 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
17f6b 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
17f6c 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
17f6d 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
17f6e 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
17f6f 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
17f70 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 te3_value_text,
17f71 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 COLNAME_DATABASE
17f72 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c );.}.#ifndef SQL
17f73 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 ITE_OMIT_UTF16.S
17f74 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
17f75 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
17f76 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 lumn_database_na
17f77 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d me16(sqlite3_stm
17f78 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 t *pStmt, int N)
17f79 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d {. return colum
17f7a 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 nName(. pSt
17f7b 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f mt, N, (const vo
17f7c 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c id*(*)(Mem*))sql
17f7d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
17f7e 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 6, COLNAME_DATAB
17f7f 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f ASE);.}.#endif /
17f80 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
17f81 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 F16 */../*.** Re
17f82 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
17f83 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 the table from
17f84 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 which a result c
17f85 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a olumn derives..*
17f86 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e * NULL is return
17f87 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 ed if the result
17f88 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 column is an ex
17f89 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 pression or cons
17f8a 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 tant or.** anyth
17f8b 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 ing else which i
17f8c 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 s not an unabigu
17f8d 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f ous reference to
17f8e 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 a database colu
17f8f 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 mn..*/.SQLITE_AP
17f90 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
17f91 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 lite3_column_tab
17f92 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f le_name(sqlite3_
17f93 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
17f94 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
17f95 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
17f96 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
17f97 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
17f98 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
17f99 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c xt, COLNAME_TABL
17f9a 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 E);.}.#ifndef SQ
17f9b 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
17f9c 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
17f9d 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 void *sqlite3_c
17f9e 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 olumn_table_name
17f9f 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 16(sqlite3_stmt
17fa0 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
17fa1 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
17fa2 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
17fa3 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
17fa4 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
17fa5 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c e3_value_text16,
17fa6 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b COLNAME_TABLE);
17fa7 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
17fa8 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
17fa9 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
17faa 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
17fab 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f table column fro
17fac 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 m which a result
17fad 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e column derives.
17fae 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 .** NULL is retu
17faf 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75 rned if the resu
17fb0 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 lt column is an
17fb1 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f expression or co
17fb2 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 nstant or.** any
17fb3 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 thing else which
17fb4 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 is not an unabi
17fb5 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 guous reference
17fb6 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f to a database co
17fb7 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lumn..*/.SQLITE_
17fb8 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
17fb9 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f sqlite3_column_o
17fba 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 rigin_name(sqlit
17fbb 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
17fbc 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
17fbd 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
17fbe 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
17fbf 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
17fc0 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
17fc1 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43 _text, COLNAME_C
17fc2 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65 OLUMN);.}.#ifnde
17fc3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
17fc4 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 F16.SQLITE_API c
17fc5 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
17fc6 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e e3_column_origin
17fc7 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f _name16(sqlite3_
17fc8 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
17fc9 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
17fca 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
17fcb 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
17fcc 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
17fcd 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
17fce 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f xt16, COLNAME_CO
17fcf 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 LUMN);.}.#endif
17fd0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 /* SQLITE_OMIT_U
17fd1 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f TF16 */.#endif /
17fd2 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
17fd3 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 COLUMN_METADATA
17fd4 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */.../**********
17fd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17fd6 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 ***** sqlite3_bi
17fd7 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nd_ ***********
17fd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17fd9 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 .** .** Routines
17fda 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 20 used to attach
17fdb 76 61 6c 75 65 73 20 74 6f 20 77 69 6c 64 63 61 values to wildca
17fdc 72 64 73 20 69 6e 20 61 20 63 6f 6d 70 69 6c 65 rds in a compile
17fdd 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e d SQL statement.
17fde 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64 .*/./*.** Unbind
17fdf 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e 64 the value bound
17fe0 20 74 6f 20 76 61 72 69 61 62 6c 65 20 69 20 69 to variable i i
17fe1 6e 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e n virtual machin
17fe2 65 20 70 2e 20 54 68 69 73 20 69 73 20 74 68 65 e p. This is the
17fe3 20 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 .** the same as
17fe4 20 62 69 6e 64 69 6e 67 20 61 20 4e 55 4c 4c 20 binding a NULL
17fe5 76 61 6c 75 65 20 74 6f 20 74 68 65 20 63 6f 6c value to the col
17fe6 75 6d 6e 2e 20 49 66 20 74 68 65 20 22 69 22 20 umn. If the "i"
17fe7 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 parameter is.**
17fe8 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 out of range, th
17fe9 65 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 en SQLITE_RANGE
17fea 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 is returned. Oth
17feb 65 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e ewise SQLITE_OK.
17fec 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 65 73 73 .**.** A success
17fed 66 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f ful evaluation o
17fee 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 f this routine a
17fef 63 71 75 69 72 65 73 20 74 68 65 20 6d 75 74 65 cquires the mute
17ff0 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 65 20 6d x on p..** the m
17ff1 75 74 65 78 20 69 73 20 72 65 6c 65 61 73 65 64 utex is released
17ff2 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 if any kind of
17ff3 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a error occurs..**
17ff4 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f .** The error co
17ff5 64 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 de stored in dat
17ff6 61 62 61 73 65 20 70 2d 3e 64 62 20 69 73 20 6f abase p->db is o
17ff7 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 verwritten with
17ff8 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 the return.** va
17ff9 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 73 65 2e lue in any case.
17ffa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 .*/.static int v
17ffb 64 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a dbeUnbind(Vdbe *
17ffc 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d p, int i){. Mem
17ffd 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 70 3d *pVar;. if( p=
17ffe 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
17fff 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 73 71 6c TE_MISUSE;. sql
18000 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
18001 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
18002 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d if( p->magic!=
18003 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c VDBE_MAGIC_RUN |
18004 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 | p->pc>=0 ){.
18005 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 sqlite3Error(p
18006 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53 ->db, SQLITE_MIS
18007 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c USE, 0);. sql
18008 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
18009 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1800a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1800b 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
1800c 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e if( i<1 || i>p->
1800d 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 nVar ){. sqli
1800e 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 te3Error(p->db,
1800f 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 SQLITE_RANGE, 0)
18010 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
18011 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
18012 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 >mutex);. ret
18013 75 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 urn SQLITE_RANGE
18014 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a 20 20 70 ;. }. i--;. p
18015 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69 Var = &p->aVar[i
18016 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ];. sqlite3Vdbe
18017 4d 65 6d 52 65 6c 65 61 73 65 28 70 56 61 72 29 MemRelease(pVar)
18018 3b 0a 20 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 ;. pVar->flags
18019 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 73 71 = MEM_Null;. sq
1801a 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 lite3Error(p->db
1801b 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b , SQLITE_OK, 0);
1801c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1801d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 _OK;.}../*.** Bi
1801e 6e 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c 4f nd a text or BLO
1801f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 B value..*/.stat
18020 69 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 28 ic int bindText(
18021 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
18022 2a 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 65 *pStmt, /* The
18023 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 69 statement to bi
18024 6e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 nd against */.
18025 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 int i,
18026 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
18027 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 of the parameter
18028 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 6f to bind */. co
18029 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c nst void *zData,
1802a 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
1802b 74 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 62 to the data to b
1802c 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 e bound */. int
1802d 20 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 nData,
1802e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1802f 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 74 bytes of data t
18030 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 o be bound */.
18031 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
18032 64 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 75 d*), /* Destru
18033 63 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 ctor for the dat
18034 61 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 63 6f 64 a */. int encod
18035 69 6e 67 20 20 20 20 20 20 20 20 20 20 20 2f 2a ing /*
18036 20 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 Encoding for th
18037 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 56 e data */.){. V
18038 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a dbe *p = (Vdbe *
18039 29 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70 )pStmt;. Mem *p
1803a 56 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a Var;. int rc;..
1803b 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e rc = vdbeUnbin
1803c 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 d(p, i);. if( r
1803d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1803e 20 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d 30 if( zData!=0
1803f 20 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 3d ){. pVar =
18040 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a &p->aVar[i-1];.
18041 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
18042 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
18043 70 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 pVar, zData, nDa
18044 74 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 ta, encoding, xD
18045 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 el);. if( r
18046 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
18047 65 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20 encoding!=0 ){.
18048 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
18049 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1804a 6f 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 28 oding(pVar, ENC(
1804b 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 7d p->db));. }
1804c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
1804d 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 ror(p->db, rc, 0
1804e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 );. rc = sq
1804f 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e lite3ApiExit(p->
18050 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 db, rc);. }.
18051 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
18052 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 _leave(p->db->mu
18053 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
18054 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
18055 20 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c Bind a blob val
18056 75 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 61 ue to an SQL sta
18057 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e tement variable.
18058 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
18059 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1805a 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f blob(. sqlite3_
1805b 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 stmt *pStmt, .
1805c 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 int i, . const
1805d 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 void *zData, .
1805e 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f int nData, . vo
1805f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a id (*xDel)(void*
18060 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 ).){. return bi
18061 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c ndText(pStmt, i,
18062 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 zData, nData, x
18063 44 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 Del, 0);.}.SQLIT
18064 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
18065 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 3_bind_double(sq
18066 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
18067 74 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65 t, int i, double
18068 20 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 rValue){. int
18069 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 rc;. Vdbe *p =
1806a 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe *)pStmt;.
1806b 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 rc = vdbeUnbind
1806c 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 (p, i);. if( rc
1806d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1806e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1806f 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e 61 mSetDouble(&p->a
18070 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 65 Var[i-1], rValue
18071 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
18072 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 utex_leave(p->db
18073 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
18074 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c return rc;.}.SQL
18075 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
18076 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c te3_bind_int(sql
18077 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e ite3_stmt *p, in
18078 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29 t i, int iValue)
18079 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
1807a 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c e3_bind_int64(p,
1807b 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29 i, (i64)iValue)
1807c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
1807d 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1807e 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 int64(sqlite3_st
1807f 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
18080 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 , sqlite_int64 i
18081 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 Value){. int rc
18082 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 ;. Vdbe *p = (V
18083 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 dbe *)pStmt;. r
18084 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 c = vdbeUnbind(p
18085 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , i);. if( rc==
18086 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
18087 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
18088 65 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 72 etInt64(&p->aVar
18089 5b 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b 0a [i-1], iValue);.
1808a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1808b 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d x_leave(p->db->m
1808c 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 utex);. }. ret
1808d 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 urn rc;.}.SQLITE
1808e 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1808f 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 _bind_null(sqlit
18090 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
18091 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72 63 int i){. int rc
18092 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 ;. Vdbe *p = (V
18093 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 dbe*)pStmt;. rc
18094 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c = vdbeUnbind(p,
18095 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 i);. if( rc==S
18096 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18097 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
18098 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
18099 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1809a 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 rc;.}.SQLITE_API
1809b 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1809c 64 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74 d_text( . sqlit
1809d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1809e 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e . int i, . con
1809f 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20 st char *zData,
180a0 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 . int nData, .
180a1 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
180a2 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e id*).){. return
180a3 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c bindText(pStmt,
180a4 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 i, zData, nData
180a5 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 , xDel, SQLITE_U
180a6 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 TF8);.}.#ifndef
180a7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
180a8 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 6.SQLITE_API int
180a9 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 sqlite3_bind_te
180aa 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f xt16(. sqlite3_
180ab 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 stmt *pStmt, .
180ac 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 int i, . const
180ad 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 void *zData, .
180ae 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f int nData, . vo
180af 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a id (*xDel)(void*
180b0 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 ).){. return bi
180b1 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c ndText(pStmt, i,
180b2 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 zData, nData, x
180b3 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 Del, SQLITE_UTF1
180b4 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 6NATIVE);.}.#end
180b5 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
180b6 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 T_UTF16 */.SQLIT
180b7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
180b8 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 3_bind_value(sql
180b9 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
180ba 2c 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 73 , int i, const s
180bb 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
180bc 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b alue){. int rc;
180bd 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
180be 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 be *)pStmt;. rc
180bf 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c = vdbeUnbind(p,
180c0 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 i);. if( rc==S
180c1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
180c2 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
180c3 4d 65 6d 43 6f 70 79 28 26 70 2d 3e 61 56 61 72 MemCopy(&p->aVar
180c4 5b 69 2d 31 5d 2c 20 70 56 61 6c 75 65 29 3b 0a [i-1], pValue);.
180c5 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
180c6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
180c7 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 c = sqlite3VdbeC
180c8 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 70 hangeEncoding(&p
180c9 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 45 4e 43 ->aVar[i-1], ENC
180ca 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 7d 0a (p->db));. }.
180cb 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
180cc 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d x_leave(p->db->m
180cd 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20 utex);. }. rc
180ce 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 = sqlite3ApiExit
180cf 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 72 (p->db, rc);. r
180d0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 eturn rc;.}.SQLI
180d1 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
180d2 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 e3_bind_zeroblob
180d3 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
180d4 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 Stmt, int i, int
180d5 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 n){. int rc;.
180d6 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
180d7 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d *)pStmt;. rc =
180d8 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 vdbeUnbind(p, i
180d9 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
180da 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 ITE_OK ){. sq
180db 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a lite3VdbeMemSetZ
180dc 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72 eroBlob(&p->aVar
180dd 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73 [i-1], n);. s
180de 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
180df 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
180e0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
180e1 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
180e2 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
180e3 20 77 69 6c 64 63 61 72 64 73 20 74 68 61 74 20 wildcards that
180e4 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c can be potential
180e5 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 ly bound to..**
180e6 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
180e7 61 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 added to support
180e8 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a DBD::SQLite. .
180e9 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
180ea 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 t sqlite3_bind_p
180eb 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 arameter_count(s
180ec 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
180ed 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d mt){. Vdbe *p =
180ee 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
180ef 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e return p ? p->n
180f0 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Var : 0;.}../*.*
180f1 2a 20 43 72 65 61 74 65 20 61 20 6d 61 70 70 69 * Create a mappi
180f2 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 ng from variable
180f3 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69 numbers to vari
180f4 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e able names.** in
180f5 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b the Vdbe.azVar[
180f6 5d 20 61 72 72 61 79 2c 20 69 66 20 73 75 63 68 ] array, if such
180f7 20 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 a mapping does
180f8 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 not already.** e
180f9 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 xist..*/.static
180fa 76 6f 69 64 20 63 72 65 61 74 65 56 61 72 4d 61 void createVarMa
180fb 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 p(Vdbe *p){. if
180fc 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 ( !p->okVar ){.
180fd 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
180fe 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 _enter(p->db->mu
180ff 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 21 70 tex);. if( !p
18100 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 20 ->okVar ){.
18101 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 4f 70 int j;. Op
18102 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 *pOp;. for
18103 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 (j=0, pOp=p->aOp
18104 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c ; j<p->nOp; j++,
18105 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 pOp++){.
18106 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
18107 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b ==OP_Variable ){
18108 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
18109 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 t( pOp->p1>0 &&
1810a 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 pOp->p1<=p->nVar
1810b 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d );. p-
1810c 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31 2d 31 >azVar[pOp->p1-1
1810d 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 ] = pOp->p4.z;.
1810e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1810f 0a 20 20 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20 . p->okVar
18110 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 = 1;. }. s
18111 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
18112 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
18113 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
18114 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f eturn the name o
18115 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 f a wildcard par
18116 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e 20 ameter. Return
18117 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 65 NULL if the inde
18118 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 72 x.** is out of r
18119 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 77 ange or if the w
1811a 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 6d ildcard is unnam
1811b 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 ed..**.** The re
1811c 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 55 sult is always U
1811d 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f TF-8..*/.SQLITE_
1811e 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1811f 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
18120 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 ameter_name(sqli
18121 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
18122 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20 int i){. Vdbe
18123 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *p = (Vdbe*)pStm
18124 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c t;. if( p==0 ||
18125 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 i<1 || i>p->nVa
18126 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 r ){. return
18127 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 0;. }. createV
18128 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 75 arMap(p);. retu
18129 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d rn p->azVar[i-1]
1812a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e ;.}../*.** Given
1812b 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 a wildcard para
1812c 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 75 meter name, retu
1812d 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 rn the index of
1812e 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 the variable.**
1812f 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20 with that name.
18130 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
18131 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 68 variable with th
18132 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a e given name,.**
18133 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 return 0..*/.SQ
18134 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
18135 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
18136 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 ter_index(sqlite
18137 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 3_stmt *pStmt, c
18138 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
18139 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 ){. Vdbe *p = (
1813a 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 Vdbe*)pStmt;. i
1813b 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 nt i;. if( p==0
1813c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
1813d 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61 ;. }. createVa
1813e 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28 20 rMap(p); . if(
1813f 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 zName ){. for
18140 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b (i=0; i<p->nVar;
18141 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e i++){. con
18142 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d 3e st char *z = p->
18143 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20 20 azVar[i];.
18144 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70 28 if( z && strcmp(
18145 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 z,zName)==0 ){.
18146 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b return i+
18147 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
18148 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
18149 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 .}../*.** Transf
1814a 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20 er all bindings
1814b 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 73 from the first s
1814c 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f tatement over to
1814d 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2a 20 the second..**
1814e 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 74 65 If the two state
1814f 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 61 20 ments contain a
18150 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 72 different number
18151 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 74 68 of bindings, th
18152 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 45 5f en.** an SQLITE_
18153 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65 ERROR is returne
18154 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
18155 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
18156 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 TransferBindings
18157 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
18158 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 FromStmt, sqlite
18159 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 3_stmt *pToStmt)
1815a 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 {. Vdbe *pFrom
1815b 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 = (Vdbe*)pFromSt
1815c 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 mt;. Vdbe *pTo
1815d 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 = (Vdbe*)pToStmt
1815e 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 ;. int i, rc =
1815f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 SQLITE_OK;. if(
18160 20 28 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 21 3d (pFrom->magic!=
18161 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 VDBE_MAGIC_RUN &
18162 26 20 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 21 3d & pFrom->magic!=
18163 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29 VDBE_MAGIC_HALT)
18164 0a 20 20 20 20 7c 7c 20 28 70 54 6f 2d 3e 6d 61 . || (pTo->ma
18165 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic!=VDBE_MAGIC_
18166 52 55 4e 20 26 26 20 70 54 6f 2d 3e 6d 61 67 69 RUN && pTo->magi
18167 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 c!=VDBE_MAGIC_HA
18168 4c 54 29 0a 20 20 20 20 7c 7c 20 70 54 6f 2d 3e LT). || pTo->
18169 64 62 21 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 7b db!=pFrom->db ){
1816a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1816b 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 TE_MISUSE;. }.
1816c 20 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72 if( pFrom->nVar
1816d 21 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20 !=pTo->nVar ){.
1816e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1816f 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 _ERROR;. }. sq
18170 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
18171 72 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 r(pTo->db->mutex
18172 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 );. for(i=0; rc
18173 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
18174 3c 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b <pFrom->nVar; i+
18175 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 +){. sqlite3V
18176 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d dbeMemMove(&pTo-
18177 3e 61 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d >aVar[i], &pFrom
18178 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a ->aVar[i]);. }.
18179 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1817a 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d leave(pTo->db->m
1817b 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 utex);. assert(
1817c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc==SQLITE_OK |
1817d 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d | rc==SQLITE_NOM
1817e 45 4d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 EM );. return r
1817f 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 c;.}..#ifndef SQ
18180 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 LITE_OMIT_DEPREC
18181 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 ATED./*.** Depre
18182 63 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 cated external i
18183 6e 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 nterface. Inter
18184 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 nal/core SQLite
18185 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 code.** should c
18186 61 6c 6c 20 73 71 6c 69 74 65 33 54 72 61 6e 73 all sqlite3Trans
18187 66 65 72 42 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a ferBindings..*/.
18188 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
18189 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f qlite3_transfer_
1818a 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 bindings(sqlite3
1818b 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74 _stmt *pFromStmt
1818c 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a , sqlite3_stmt *
1818d 70 54 6f 53 74 6d 74 29 7b 0a 20 20 72 65 74 75 pToStmt){. retu
1818e 72 6e 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 rn sqlite3Transf
1818f 65 72 42 69 6e 64 69 6e 67 73 28 70 46 72 6f 6d erBindings(pFrom
18190 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29 3b 0a Stmt, pToStmt);.
18191 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
18192 52 65 74 75 72 6e 20 74 68 65 20 73 71 6c 69 74 Return the sqlit
18193 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e e3* database han
18194 64 6c 65 20 74 6f 20 77 68 69 63 68 20 74 68 65 dle to which the
18195 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
18196 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69 6e 20 ent given.** in
18197 74 68 65 20 61 72 67 75 6d 65 6e 74 20 62 65 6c the argument bel
18198 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 74 ongs. This is t
18199 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
1819a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73 handle that was
1819b 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 .** the first ar
1819c 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 gument to the sq
1819d 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 lite3_prepare()
1819e 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f that was used to
1819f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 65 20 73 create.** the s
181a0 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 tatement in the
181a1 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f 0a first place..*/.
181a2 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
181a3 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 e3 *sqlite3_db_h
181a4 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 andle(sqlite3_st
181a5 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 72 65 mt *pStmt){. re
181a6 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 28 28 56 turn pStmt ? ((V
181a7 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 20 dbe*)pStmt)->db
181a8 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 : 0;.}../*.** Re
181a9 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
181aa 6f 20 74 68 65 20 6e 65 78 74 20 70 72 65 70 61 o the next prepa
181ab 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 66 red statement af
181ac 74 65 72 20 70 53 74 6d 74 20 61 73 73 6f 63 69 ter pStmt associ
181ad 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 64 61 74 ated.** with dat
181ae 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
181af 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74 20 pDb. If pStmt
181b0 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 is NULL, return
181b1 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 72 65 the first.** pre
181b2 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pared statement
181b3 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 for the database
181b4 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 52 65 connection. Re
181b5 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 turn NULL if the
181b6 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 re.** are no mor
181b7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
181b8 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 sqlite3_stmt *s
181b9 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 qlite3_next_stmt
181ba 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 (sqlite3 *pDb, s
181bb 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
181bc 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 mt){. sqlite3_s
181bd 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20 73 71 tmt *pNext;. sq
181be 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
181bf 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 r(pDb->mutex);.
181c0 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b if( pStmt==0 ){
181c1 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71 . pNext = (sq
181c2 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 44 62 2d lite3_stmt*)pDb-
181c3 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73 65 7b >pVdbe;. }else{
181c4 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71 . pNext = (sq
181c5 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 56 64 lite3_stmt*)((Vd
181c6 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e 65 78 be*)pStmt)->pNex
181c7 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 t;. }. sqlite3
181c8 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44 62 _mutex_leave(pDb
181c9 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 ->mutex);. retu
181ca 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a rn pNext;.}../*.
181cb 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 ** Return the va
181cc 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 lue of a status
181cd 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 20 70 72 counter for a pr
181ce 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
181cf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
181d0 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f nt sqlite3_stmt_
181d1 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 5f 73 status(sqlite3_s
181d2 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
181d3 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 61 op, int resetFla
181d4 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 g){. Vdbe *pVdb
181d5 65 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 e = (Vdbe*)pStmt
181d6 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 56 64 62 ;. int v = pVdb
181d7 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 e->aCounter[op-1
181d8 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c ];. if( resetFl
181d9 61 67 20 29 20 70 56 64 62 65 2d 3e 61 43 6f 75 ag ) pVdbe->aCou
181da 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30 3b 0a nter[op-1] = 0;.
181db 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a 2f return v;.}../
181dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
181dd 6e 64 20 6f 66 20 76 64 62 65 61 70 69 2e 63 20 nd of vdbeapi.c
181de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
181e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
181e2 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e 63 egin file vdbe.c
181e3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
181e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
181e6 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d *.** 2001 Septem
181e7 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 15.**.** The
181e8 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
181e9 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
181ea 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
181eb 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
181ec 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
181ed 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
181ee 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
181ef 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
181f0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
181f1 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
181f2 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
181f3 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
181f4 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
181f5 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
181f6 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
181f7 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
181f8 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
181f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181fd 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 *****.** The cod
181fe 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 e in this file i
181ff 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 75 74 mplements execut
18200 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 ion method of th
18201 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 44 61 e .** Virtual Da
18202 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20 28 56 tabase Engine (V
18203 44 42 45 29 2e 20 20 41 20 73 65 70 61 72 61 74 DBE). A separat
18204 65 20 66 69 6c 65 20 28 22 76 64 62 65 61 75 78 e file ("vdbeaux
18205 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 .c").** handles
18206 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 65 74 housekeeping det
18207 61 69 6c 73 20 73 75 63 68 20 61 73 20 63 72 65 ails such as cre
18208 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 69 ating and deleti
18209 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 74 61 ng.** VDBE insta
1820a 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69 6c 65 nces. This file
1820b 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 65 72 is solely inter
1820c 65 73 74 65 64 20 69 6e 20 65 78 65 63 75 74 69 ested in executi
1820d 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 70 ng.** the VDBE p
1820e 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e rogram..**.** In
1820f 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20 69 6e the external in
18210 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73 71 6c terface, an "sql
18211 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 20 61 ite3_stmt*" is a
18212 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 65 72 n opaque pointer
18213 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e 0a 2a .** to a VDBE..*
18214 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 61 72 *.** The SQL par
18215 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 61 20 ser generates a
18216 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20 69 73 program which is
18217 20 74 68 65 6e 20 65 78 65 63 75 74 65 64 20 62 then executed b
18218 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 74 6f y.** the VDBE to
18219 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 do the work of
1821a 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
1821b 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72 61 6d t. VDBE program
1821c 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 6c 61 s are .** simila
1821d 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 73 73 r in form to ass
1821e 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 2e 20 embly language.
1821f 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 6e The program con
18220 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 6c 69 sists of.** a li
18221 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20 6f 66 near sequence of
18222 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 45 61 operations. Ea
18223 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73 ch operation has
18224 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a 20 61 an opcode .** a
18225 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e 20 20 nd 5 operands.
18226 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50 32 2c Operands P1, P2,
18227 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e 74 65 and P3 are inte
18228 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 20 50 gers. Operand P
18229 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c 6c 2d 4 .** is a null-
1822a 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e terminated strin
1822b 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 20 69 g. Operand P5 i
1822c 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 s an unsigned ch
1822d 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 77 20 aracter..** Few
1822e 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c 6c 20 opcodes use all
1822f 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a 0a 2a 5 operands..**.*
18230 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 72 65 * Computation re
18231 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 sults are stored
18232 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 65 67 on a set of reg
18233 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65 64 20 isters numbered
18234 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 beginning.** wit
18235 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 75 70 h 1 and going up
18236 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e 20 20 to Vdbe.nMem.
18237 45 61 63 68 20 72 65 67 69 73 74 65 72 20 63 61 Each register ca
18238 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 68 65 n store.** eithe
18239 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 61 20 r an integer, a
1823a 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 null-terminated
1823b 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 74 69 string, a floati
1823c 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 6d 62 ng point.** numb
1823d 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c 20 22 er, or the SQL "
1823e 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 41 6e NULL" value. An
1823f 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 65 72 implicit conver
18240 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a sion from one.**
18241 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f 74 68 type to the oth
18242 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e 65 63 er occurs as nec
18243 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 4d essary..** .** M
18244 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65 20 ost of the code
18245 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 in this file is
18246 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68 65 20 taken up by the
18247 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
18248 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 68 ).** function wh
18249 69 63 68 20 64 6f 65 73 20 74 68 65 20 77 6f 72 ich does the wor
1824a 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74 69 6e k of interpretin
1824b 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d g a VDBE program
1824c 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 20 72 ..** But other r
1824d 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 73 6f outines are also
1824e 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68 65 6c provided to hel
1824f 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 p in building up
18250 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 69 6e .** a program in
18251 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69 6e 73 struction by ins
18252 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 truction..**.**
18253 56 61 72 69 6f 75 73 20 73 63 72 69 70 74 73 20 Various scripts
18254 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72 63 65 scan this source
18255 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 file in order t
18256 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d 4c 0a o generate HTML.
18257 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e ** documentation
18258 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65 73 2c , headers files,
18259 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69 76 65 or other derive
1825a 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20 66 6f d files. The fo
1825b 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 rmatting.** of t
1825c 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 he code in this
1825d 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65 66 6f file is, therefo
1825e 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 re, important.
1825f 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d 65 6e See other commen
18260 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 66 69 ts.** in this fi
18261 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 le for details.
18262 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 64 6f If in doubt, do
18263 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 6f not deviate fro
18264 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 63 6f m existing.** co
18265 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 6e 64 mmenting and ind
18266 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 69 63 entation practic
18267 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69 6e 67 es when changing
18268 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 65 2e or adding code.
18269 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
1826a 2e 63 2c 76 20 31 2e 37 38 36 20 32 30 30 38 2f .c,v 1.786 2008/
1826b 31 31 2f 30 35 20 31 36 3a 33 37 3a 33 35 20 64 11/05 16:37:35 d
1826c 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a rh Exp $.*/../*.
1826d 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
1826e 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
1826f 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
18270 65 76 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 every time a cur
18271 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 sor.** moves, ei
18272 74 68 65 72 20 62 79 20 74 68 65 20 4f 50 5f 4d ther by the OP_M
18273 6f 76 65 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 oveXX, OP_Next,
18274 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 or OP_Prev opcod
18275 65 73 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a es. The test.**
18276 20 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 procedures use
18277 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e this information
18278 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
18279 61 74 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a at indices are.*
1827a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 * working correc
1827b 74 6c 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 tly. This varia
1827c 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 ble has no funct
1827d 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 ion other than t
1827e 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 o.** help verify
1827f 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 the correct ope
18280 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 ration of the li
18281 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 brary..*/.#ifdef
18282 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
18283 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
18284 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 te3_search_count
18285 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a = 0;.#endif../*
18286 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c .** When this gl
18287 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 obal variable is
18288 20 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 positive, it ge
18289 74 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f ts decremented o
1828a 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 nce before.** ea
1828b 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 ch instruction i
1828c 6e 20 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 n the VDBE. Whe
1828d 6e 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 n reaches zero,
1828e 74 68 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 the u1.isInterru
1828f 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 pted.** field of
18290 20 74 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 the sqlite3 str
18291 75 63 74 75 72 65 20 69 73 20 73 65 74 20 69 6e ucture is set in
18292 20 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 order to simula
18293 74 65 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 te and interrupt
18294 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 ..**.** This fac
18295 69 6c 69 74 79 20 69 73 20 75 73 65 64 20 66 6f ility is used fo
18296 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 r testing purpos
18297 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 es only. It doe
18298 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a s not function.*
18299 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 * in an ordinary
1829a 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 build..*/.#ifde
1829b 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
1829c 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1829d 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 ite3_interrupt_c
1829e 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
1829f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ../*.** The next
182a0 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
182a1 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
182a2 65 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 each type the OP
182a3 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 _Sort opcode.**
182a4 69 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 is executed. Th
182a5 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 e test procedure
182a6 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 s use this infor
182a7 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 mation to make s
182a8 75 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 ure that.** sort
182a9 69 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 ing is occurring
182aa 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e or not occurrin
182ab 67 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 g at appropriate
182ac 20 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 times. This v
182ad 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e ariable.** has n
182ae 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 o function other
182af 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 than to help ve
182b0 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 rify the correct
182b1 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 operation of th
182b2 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f e.** library..*/
182b3 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
182b4 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
182b5 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f nt sqlite3_sort_
182b6 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 count = 0;.#endi
182b7 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 f../*.** The nex
182b8 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c t global variabl
182b9 65 20 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 e records the si
182ba 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 ze of the larges
182bb 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 t MEM_Blob.** or
182bc 20 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 MEM_Str that ha
182bd 73 20 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 s been used by a
182be 20 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 VDBE opcode. T
182bf 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 he test procedur
182c0 65 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 es.** use this i
182c1 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 nformation to ma
182c2 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ke sure that the
182c3 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 zero-blob funct
182c4 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 ionality.** is w
182c5 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 orking correctly
182c6 2e 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c . This variabl
182c7 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f e has no functio
182c8 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a n other than to.
182c9 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 ** help verify t
182ca 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 he correct opera
182cb 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 tion of the libr
182cc 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ary..*/.#ifdef S
182cd 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
182ce 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
182cf 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 3_max_blobsize =
182d0 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 0;.static void
182d1 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a updateMaxBlobsiz
182d2 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 e(Mem *p){. if(
182d3 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 (p->flags & (ME
182d4 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 M_Str|MEM_Blob))
182d5 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 !=0 && p->n>sqli
182d6 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 te3_max_blobsize
182d7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
182d8 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 max_blobsize = p
182d9 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 ->n;. }.}.#endi
182da 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 f../*.** Test a
182db 72 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20 register to see
182dc 69 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68 if it exceeds th
182dd 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 e current maximu
182de 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 m blob size..**
182df 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f If it does, reco
182e0 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d rd the new maxim
182e1 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f um blob size..*/
182e2 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
182e3 49 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65 ITE_TEST) && !de
182e4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
182e5 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a T_BUILTIN_TEST).
182e6 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f # define UPDATE_
182e7 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 MAX_BLOBSIZE(P)
182e8 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 updateMaxBlobsi
182e9 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 ze(P).#else.# de
182ea 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f fine UPDATE_MAX_
182eb 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 BLOBSIZE(P).#end
182ec 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 if../*.** Conver
182ed 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69 t the given regi
182ee 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 ster into a stri
182ef 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f ng if it isn't o
182f0 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 ne.** already. R
182f1 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 eturn non-zero i
182f2 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
182f3 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ls..*/.#define S
182f4 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 tringify(P, enc)
182f5 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66 \. if(((P)->f
182f6 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 lags&(MEM_Str|ME
182f7 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 M_Blob))==0 && s
182f8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 qlite3VdbeMemStr
182f9 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c ingify(P,enc)) \
182fa 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f . { goto no_
182fb 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e mem; }../*.** An
182fc 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e ephemeral strin
182fd 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69 g value (signifi
182fe 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 ed by the MEM_Ep
182ff 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 hem flag) contai
18300 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 ns.** a pointer
18301 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 to a dynamically
18302 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e allocated strin
18303 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 g where some oth
18304 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 er entity.** is
18305 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
18306 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 deallocating tha
18307 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75 t string. Becau
18308 73 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a se the register.
18309 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 ** does not cont
1830a 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 rol the string,
1830b 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 it might be dele
1830c 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 ted without the
1830d 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 register.** know
1830e 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ing it..**.** Th
1830f 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 is routine conve
18310 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c rts an ephemeral
18311 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 string into a d
18312 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
18313 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 ated.** string t
18314 68 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72 hat the register
18315 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 itself controls
18316 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
18317 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 s, it.** convert
18318 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 s an MEM_Ephem s
18319 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 tring into an ME
1831a 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f M_Dyn string..*/
1831b 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d .#define Deephem
1831c 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 eralize(P) \.
1831d 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 if( ((P)->flags&
1831e 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a MEM_Ephem)!=0 \.
1831f 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 && sqlite
18320 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 3VdbeMemMakeWrit
18321 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f eable(P) ){ goto
18322 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a no_mem;}../*.**
18323 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 Call sqlite3Vdb
18324 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 eMemExpandBlob()
18325 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 on the supplied
18326 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d value (type Mem
18327 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 *).** P if requi
18328 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 red..*/.#define
18329 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 ExpandBlob(P) ((
1832a 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a (P)->flags&MEM_Z
1832b 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 ero)?sqlite3Vdbe
1832c 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 MemExpandBlob(P)
1832d 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d :0)../*.** Argum
1832e 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 ent pMem points
1832f 61 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68 at a register th
18330 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 at will be passe
18331 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 d to a.** user-d
18332 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
18333 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 or returned to t
18334 68 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72 he user as the r
18335 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79 esult of a query
18336 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ..** The second
18337 61 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f 65 6e argument, 'db_en
18338 63 27 20 69 73 20 74 68 65 20 74 65 78 74 20 65 c' is the text e
18339 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 ncoding used by
1833a 74 68 65 20 76 64 62 65 20 66 6f 72 0a 2a 2a 20 the vdbe for.**
1833b 72 65 67 69 73 74 65 72 20 76 61 72 69 61 62 6c register variabl
1833c 65 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e es. This routin
1833d 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d e sets the pMem-
1833e 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d 3e 74 >enc and pMem->t
1833f 79 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 ype.** variables
18340 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c used by the sql
18341 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 72 ite3_value_*() r
18342 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64 65 66 outines..*/.#def
18343 69 6e 65 20 73 74 6f 72 65 54 79 70 65 49 6e 66 ine storeTypeInf
18344 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65 54 79 70 o(A,B) _storeTyp
18345 65 49 6e 66 6f 28 41 29 0a 73 74 61 74 69 63 20 eInfo(A).static
18346 76 6f 69 64 20 5f 73 74 6f 72 65 54 79 70 65 49 void _storeTypeI
18347 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a nfo(Mem *pMem){.
18348 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d int flags = pM
18349 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 em->flags;. if(
1834a 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c flags & MEM_Nul
1834b 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 l ){. pMem->t
1834c 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c ype = SQLITE_NUL
1834d 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 L;. }. else if
1834e 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ( flags & MEM_In
1834f 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 t ){. pMem->t
18350 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 ype = SQLITE_INT
18351 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 EGER;. }. else
18352 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d if( flags & MEM
18353 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 70 4d 65 _Real ){. pMe
18354 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
18355 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20 65 6c _FLOAT;. }. el
18356 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d se if( flags & M
18357 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 70 4d EM_Str ){. pM
18358 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
18359 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73 65 7b E_TEXT;. }else{
1835a 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 . pMem->type
1835b 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 = SQLITE_BLOB;.
1835c 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 70 }.}../*.** Prop
1835d 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f 64 65 erties of opcode
1835e 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f 49 4e s. The OPFLG_IN
1835f 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72 6f 20 ITIALIZER macro
18360 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 is.** created by
18361 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 64 mkopcodeh.awk d
18362 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f uring compilatio
18363 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62 74 61 n. Data is obta
18364 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 ined.** from the
18365 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 comments follow
18366 69 6e 67 20 74 68 65 20 22 63 61 73 65 20 4f 50 ing the "case OP
18367 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d 65 6e _xxxx:" statemen
18368 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20 66 69 ts in.** this fi
18369 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 le. .*/.static
1836a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1836b 68 61 72 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 har opcodeProper
1836c 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49 4e 49 ty[] = OPFLG_INI
1836d 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 0a 2a 2a TIALIZER;../*.**
1836e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
1836f 61 6e 20 6f 70 63 6f 64 65 20 68 61 73 20 61 6e an opcode has an
18370 79 20 6f 66 20 74 68 65 20 4f 50 46 4c 47 5f 78 y of the OPFLG_x
18371 78 78 20 70 72 6f 70 65 72 74 69 65 73 0a 2a 2a xx properties.**
18372 20 73 70 65 63 69 66 69 65 64 20 62 79 20 6d 61 specified by ma
18373 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 sk..*/.SQLITE_PR
18374 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18375 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 3VdbeOpcodeHasPr
18376 6f 70 65 72 74 79 28 69 6e 74 20 6f 70 63 6f 64 operty(int opcod
18377 65 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 e, int mask){.
18378 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65 3e 30 assert( opcode>0
18379 20 26 26 20 6f 70 63 6f 64 65 3c 73 69 7a 65 6f && opcode<sizeo
1837a 66 28 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 f(opcodeProperty
1837b 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 6f ) );. return (o
1837c 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 pcodeProperty[op
1837d 63 6f 64 65 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a code]&mask)!=0;.
1837e 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 }../*.** Allocat
1837f 65 20 56 64 62 65 43 75 72 73 6f 72 20 6e 75 6d e VdbeCursor num
18380 62 65 72 20 69 43 75 72 2e 20 20 52 65 74 75 72 ber iCur. Retur
18381 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 n a pointer to i
18382 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 0a t. Return NULL.
18383 2a 2a 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 ** if we run out
18384 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 of memory..*/.s
18385 74 61 74 69 63 20 56 64 62 65 43 75 72 73 6f 72 tatic VdbeCursor
18386 20 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 *allocateCursor
18387 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 (. Vdbe *p,
18388 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
18389 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1838a 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 */. int iCur,
1838b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1838c 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 ndex of the new
1838d 56 64 62 65 43 75 72 73 6f 72 20 2a 2f 0a 20 20 VdbeCursor */.
1838e 4f 70 20 2a 70 4f 70 2c 20 20 20 20 20 20 20 20 Op *pOp,
1838f 20 20 20 20 20 20 2f 2a 20 2a 2f 0a 20 20 69 6e /* */. in
18390 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 t iDb,
18391 20 20 20 20 2f 2a 20 2a 2f 0a 20 20 69 6e 74 20 /* */. int
18392 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 20 20 isBtreeCursor
18393 20 20 2f 2a 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 /* */.){. /*
18394 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 Find the memory
18395 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 cell that will b
18396 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 e used to store
18397 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f the blob of memo
18398 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 ry. ** required
18399 20 66 6f 72 20 74 68 69 73 20 56 64 62 65 43 75 for this VdbeCu
1839a 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20 rsor structure.
1839b 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 It is convenient
1839c 20 74 6f 20 75 73 65 20 61 20 0a 20 20 2a 2a 20 to use a . **
1839d 76 64 62 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c vdbe memory cell
1839e 20 74 6f 20 6d 61 6e 61 67 65 20 74 68 65 20 6d to manage the m
1839f 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
183a0 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 0a required for a.
183a1 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 20 ** VdbeCursor
183a2 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 structure for th
183a3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73 e following reas
183a4 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 ons:. **. **
183a5 20 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63 75 72 * Sometimes cur
183a6 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 sor numbers are
183a7 75 73 65 64 20 66 6f 72 20 61 20 63 6f 75 70 6c used for a coupl
183a8 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 0a 20 e of different.
183a9 20 2a 2a 20 20 20 20 20 70 75 72 70 6f 73 65 73 ** purposes
183aa 20 69 6e 20 61 20 76 64 62 65 20 70 72 6f 67 72 in a vdbe progr
183ab 61 6d 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e am. The differen
183ac 74 20 75 73 65 73 20 6d 69 67 68 74 20 72 65 71 t uses might req
183ad 75 69 72 65 0a 20 20 2a 2a 20 20 20 20 20 64 69 uire. ** di
183ae 66 66 65 72 65 6e 74 20 73 69 7a 65 64 20 61 6c fferent sized al
183af 6c 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 locations. Memor
183b0 79 20 63 65 6c 6c 73 20 70 72 6f 76 69 64 65 20 y cells provide
183b1 67 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 20 20 growable. **
183b2 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 allocations..
183b3 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 **. ** * Whe
183b4 6e 20 75 73 69 6e 67 20 45 4e 41 42 4c 45 5f 4d n using ENABLE_M
183b5 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
183b6 2c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 62 75 , memory cell bu
183b7 66 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a 20 20 ffers can. **
183b8 20 20 20 62 65 20 66 72 65 65 64 20 6c 61 7a 69 be freed lazi
183b9 6c 79 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 ly via the sqlit
183ba 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
183bb 79 28 29 20 41 50 49 2e 20 54 68 69 73 0a 20 20 y() API. This.
183bc 2a 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73 ** minimizes
183bd 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d the number of m
183be 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65 alloc calls made
183bf 20 62 79 20 74 68 65 20 73 79 73 74 65 6d 2e 0a by the system..
183c0 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 **. ** Memory
183c1 20 63 65 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f cells for curso
183c2 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 rs are allocated
183c3 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 at the top of t
183c4 68 65 20 61 64 64 72 65 73 73 0a 20 20 2a 2a 20 he address. **
183c5 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65 space. Memory ce
183c6 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 ll (p->nMem) cor
183c7 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75 72 73 responds to curs
183c8 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f 72 0a or 0. Space for.
183c9 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20 69 73 ** cursor 1 is
183ca 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65 6d 6f managed by memo
183cb 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d ry cell (p->nMem
183cc 2d 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 -1), etc.. */.
183cd 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d Mem *pMem = &p-
183ce 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 >aMem[p->nMem-iC
183cf 75 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 ur];.. int nByt
183d0 65 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 e;. VdbeCursor
183d1 2a 70 43 78 20 3d 20 30 3b 0a 20 20 2f 2a 20 49 *pCx = 0;. /* I
183d2 66 20 74 68 65 20 6f 70 63 6f 64 65 20 6f 66 20 f the opcode of
183d3 70 4f 70 20 69 73 20 4f 50 5f 53 65 74 4e 75 6d pOp is OP_SetNum
183d4 43 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 70 4f Columns, then pO
183d5 70 2d 3e 70 32 20 63 6f 6e 74 61 69 6e 73 0a 20 p->p2 contains.
183d6 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ** the number o
183d7 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 f fields in the
183d8 72 65 63 6f 72 64 73 20 63 6f 6e 74 61 69 6e 65 records containe
183d9 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f d in the table o
183da 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20 62 65 69 r. ** index bei
183db 6e 67 20 6f 70 65 6e 65 64 2e 20 55 73 65 20 74 ng opened. Use t
183dc 68 69 73 20 74 6f 20 72 65 73 65 72 76 65 20 73 his to reserve s
183dd 70 61 63 65 20 66 6f 72 20 74 68 65 20 0a 20 20 pace for the .
183de 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2e 61 54 ** VdbeCursor.aT
183df 79 70 65 5b 5d 20 61 72 72 61 79 2e 0a 20 20 2a ype[] array.. *
183e0 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 20 3d /. int nField =
183e1 20 30 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 0;. if( pOp->o
183e2 70 63 6f 64 65 3d 3d 4f 50 5f 53 65 74 4e 75 6d pcode==OP_SetNum
183e3 43 6f 6c 75 6d 6e 73 20 7c 7c 20 70 4f 70 2d 3e Columns || pOp->
183e4 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 45 opcode==OP_OpenE
183e5 70 68 65 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20 phemeral ){.
183e6 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32 nField = pOp->p2
183e7 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 ;. }. nByte =
183e8 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 56 64 . sizeof(Vd
183e9 62 65 43 75 72 73 6f 72 29 20 2b 20 0a 20 20 20 beCursor) + .
183ea 20 20 20 28 69 73 42 74 72 65 65 43 75 72 73 6f (isBtreeCurso
183eb 72 3f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 r?sqlite3BtreeCu
183ec 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 20 2b 20 rsorSize():0) +
183ed 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 6c 64 2a . 2*nField*
183ee 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a 20 20 sizeof(u32);..
183ef 61 73 73 65 72 74 28 20 69 43 75 72 3c 70 2d 3e assert( iCur<p->
183f0 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 nCursor );. if(
183f1 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 p->apCsr[iCur]
183f2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
183f3 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 beFreeCursor(p,
183f4 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 29 3b p->apCsr[iCur]);
183f5 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 . p->apCsr[iC
183f6 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 ur] = 0;. }. i
183f7 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 f( SQLITE_OK==sq
183f8 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
183f9 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 (pMem, nByte, 0)
183fa 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 ){. p->apCsr
183fb 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 3d 20 28 [iCur] = pCx = (
183fc 56 64 62 65 43 75 72 73 6f 72 2a 29 70 4d 65 6d VdbeCursor*)pMem
183fd 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 ->z;. memset(
183fe 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 pMem->z, 0, nByt
183ff 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 62 e);. pCx->iDb
18400 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78 2d = iDb;. pCx-
18401 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 >nField = nField
18402 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c 64 ;. if( nField
18403 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 61 ){. pCx->a
18404 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26 70 Type = (u32 *)&p
18405 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 Mem->z[sizeof(Vd
18406 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20 20 20 beCursor)];.
18407 7d 0a 20 20 20 20 69 66 28 20 69 73 42 74 72 65 }. if( isBtre
18408 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 eCursor ){.
18409 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 3d 20 pCx->pCursor =
1840a 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 20 20 20 (BtCursor*).
1840b 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a 5b 73 &pMem->z[s
1840c 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
1840d 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f )+2*nField*sizeo
1840e 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d 0a 20 f(u32)];. }.
1840f 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 78 3b }. return pCx;
18410 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f .}../*.** Try to
18411 20 63 6f 6e 76 65 72 74 20 61 20 76 61 6c 75 65 convert a value
18412 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 into a numeric
18413 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 representation i
18414 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 f we can.** do s
18415 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f o without loss o
18416 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 f information.
18417 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
18418 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a if the string.**
18419 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 looks like a nu
1841a 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 mber, convert it
1841b 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 into a number.
1841c 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a If it does not.
1841d 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e ** look like a n
1841e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 74 20 umber, leave it
1841f 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 alone..*/.static
18420 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 void applyNumer
18421 69 63 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a icAffinity(Mem *
18422 70 52 65 63 29 7b 0a 20 20 69 66 28 20 28 70 52 pRec){. if( (pR
18423 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d ec->flags & (MEM
18424 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d _Real|MEM_Int))=
18425 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 =0 ){. int re
18426 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 alnum;. sqlit
18427 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d e3VdbeMemNulTerm
18428 69 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 20 20 inate(pRec);.
18429 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 if( (pRec->flag
1842a 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 s&MEM_Str).
1842b 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 49 73 && sqlite3Is
1842c 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 Number(pRec->z,
1842d 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e &realnum, pRec->
1842e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 69 36 enc) ){. i6
1842f 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 4 value;. s
18430 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
18431 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 Encoding(pRec, S
18432 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 QLITE_UTF8);.
18433 20 20 20 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 if( !realnum
18434 26 26 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 && sqlite3Atoi64
18435 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 (pRec->z, &value
18436 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65 ) ){. pRe
18437 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a c->u.i = value;.
18438 20 20 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79 MemSetTy
18439 70 65 46 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d peFlag(pRec, MEM
1843a 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c _Int);. }el
1843b 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
1843c 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 te3VdbeMemRealif
1843d 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20 20 7d y(pRec);. }
1843e 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1843f 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 69 .** Processing i
18440 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79 20 74 s determine by t
18441 68 65 20 61 66 66 69 6e 69 74 79 20 70 61 72 61 he affinity para
18442 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c meter:.**.** SQL
18443 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a ITE_AFF_INTEGER:
18444 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 .** SQLITE_AFF_R
18445 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 EAL:.** SQLITE_A
18446 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 FF_NUMERIC:.**
18447 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 Try to convert
18448 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 pRec to an inte
18449 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ger representati
1844a 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 on or a .** f
1844b 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 loating-point re
1844c 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 presentation if
1844d 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 an integer repre
1844e 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 sentation.**
1844f 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e is not possible.
18450 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 Note that the
18451 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e integer represen
18452 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 tation is.**
18453 61 6c 77 61 79 73 20 70 72 65 66 65 72 72 65 64 always preferred
18454 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 61 66 , even if the af
18455 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c 2c 20 finity is REAL,
18456 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e because.** an
18457 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 integer represe
18458 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 ntation is more
18459 73 70 61 63 65 20 65 66 66 69 63 69 65 6e 74 20 space efficient
1845a 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 on disk..**.** S
1845b 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a QLITE_AFF_TEXT:.
1845c 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 70 52 ** Convert pR
1845d 65 63 20 74 6f 20 61 20 74 65 78 74 20 72 65 70 ec to a text rep
1845e 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a resentation..**.
1845f 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f ** SQLITE_AFF_NO
18460 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e NE:.** No-op.
18461 20 20 70 52 65 63 20 69 73 20 75 6e 63 68 61 6e pRec is unchan
18462 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ged..*/.static v
18463 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e 69 74 oid applyAffinit
18464 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 y(. Mem *pRec,
18465 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
18466 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 20 61 value to apply a
18467 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 ffinity to */.
18468 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c 20 20 char affinity,
18469 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e /* The affin
1846a 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 ity to be applie
1846b 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 d */. u8 enc
1846c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
1846d 65 20 74 68 69 73 20 74 65 78 74 20 65 6e 63 6f e this text enco
1846e 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 ding */.){. if(
1846f 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
18470 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 E_AFF_TEXT ){.
18471 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 /* Only attemp
18472 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e t the conversion
18473 20 74 6f 20 54 45 58 54 20 69 66 20 74 68 65 72 to TEXT if ther
18474 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 e is an integer
18475 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 or real. ** r
18476 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 epresentation (b
18477 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 lob and NULL do
18478 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 74 65 not get converte
18479 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 d) but no string
1847a 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e . ** represen
1847b 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 tation.. */.
1847c 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65 63 2d if( 0==(pRec-
1847d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 >flags&MEM_Str)
1847e 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 && (pRec->flags&
1847f 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e (MEM_Real|MEM_In
18480 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c t)) ){. sql
18481 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e ite3VdbeMemStrin
18482 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63 29 3b gify(pRec, enc);
18483 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 63 2d . }. pRec-
18484 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
18485 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Real|MEM_Int);.
18486 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66 69 6e }else if( affin
18487 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity!=SQLITE_AFF_
18488 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73 65 NONE ){. asse
18489 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 rt( affinity==SQ
1848a 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
1848b 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 || affinity==SQ
1848c 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 LITE_AFF_REAL.
1848d 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 61 66 || af
1848e 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
1848f 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 FF_NUMERIC );.
18490 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 applyNumericAf
18491 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20 finity(pRec);.
18492 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 if( pRec->flag
18493 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a s & MEM_Real ){.
18494 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
18495 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 eIntegerAffinity
18496 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 (pRec);. }.
18497 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 }.}../*.** Try t
18498 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 74 79 o convert the ty
18499 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e pe of a function
1849a 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 argument or a r
1849b 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 esult column.**
1849c 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 into a numeric r
1849d 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 epresentation.
1849e 55 73 65 20 65 69 74 68 65 72 20 49 4e 54 45 47 Use either INTEG
1849f 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69 63 68 ER or REAL which
184a0 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f ever.** is appro
184a1 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c priate. But onl
184a2 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 y do the convers
184a3 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70 6f 73 ion if it is pos
184a4 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a sible without.**
184a5 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 loss of informa
184a6 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 tion and return
184a7 74 68 65 20 72 65 76 69 73 65 64 20 74 79 70 65 the revised type
184a8 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 of the argument
184a9 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ..**.** This is
184aa 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 an EXPERIMENTAL
184ab 61 70 69 20 61 6e 64 20 69 73 20 73 75 62 6a 65 api and is subje
184ac 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 ct to change or
184ad 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 removal..*/.SQLI
184ae 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
184af 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 e3_value_numeric
184b0 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 _type(sqlite3_va
184b1 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 lue *pVal){. Me
184b2 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 m *pMem = (Mem*)
184b3 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d pVal;. applyNum
184b4 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 4d 65 ericAffinity(pMe
184b5 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79 70 65 49 m);. storeTypeI
184b6 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a 20 20 nfo(pMem, 0);.
184b7 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70 return pMem->typ
184b8 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f e;.}../*.** Expo
184b9 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 rted version of
184ba 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29 2e applyAffinity().
184bb 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20 This one works
184bc 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 on sqlite3_value
184bd 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69 *, .** not the i
184be 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70 nternal Mem* typ
184bf 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
184c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
184c1 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 3ValueApplyAffin
184c2 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ity(. sqlite3_v
184c3 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75 alue *pVal, . u
184c4 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 20 75 8 affinity, . u
184c5 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79 8 enc.){. apply
184c6 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29 Affinity((Mem *)
184c7 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 pVal, affinity,
184c8 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 enc);.}..#ifdef
184c9 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a SQLITE_DEBUG./*.
184ca 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63 65 20 ** Write a nice
184cb 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 string represent
184cc 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e ation of the con
184cd 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d tents of cell pM
184ce 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65 em.** into buffe
184cf 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e r zBuf, length n
184d0 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 Buf..*/.SQLITE_P
184d1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
184d2 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 te3VdbeMemPretty
184d3 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c Print(Mem *pMem,
184d4 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 char *zBuf){.
184d5 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75 char *zCsr = zBu
184d6 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d 65 f;. int f = pMe
184d7 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61 m->flags;.. sta
184d8 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
184d9 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d const encnames[]
184da 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 29 22 = {"(X)", "(8)"
184db 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28 31 36 , "(16LE)", "(16
184dc 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26 BE)"};.. if( f&
184dd 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20 MEM_Blob ){.
184de 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 int i;. char
184df 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d c;. if( f & M
184e0 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 EM_Dyn ){.
184e1 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 c = 'z';. a
184e2 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d ssert( (f & (MEM
184e3 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 _Static|MEM_Ephe
184e4 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 m))==0 );. }e
184e5 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f lse if( f & MEM_
184e6 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 Static ){.
184e7 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61 c = 't';. a
184e8 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d ssert( (f & (MEM
184e9 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 _Dyn|MEM_Ephem))
184ea 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ==0 );. }else
184eb 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 if( f & MEM_Eph
184ec 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 em ){. c =
184ed 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 'e';. asser
184ee 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 t( (f & (MEM_Sta
184ef 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 tic|MEM_Dyn))==0
184f0 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 );. }else{.
184f1 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a 20 20 c = 's';.
184f2 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 }.. sqlite3
184f3 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a _snprintf(100, z
184f4 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20 Csr, "%c", c);.
184f5 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 6c 65 zCsr += strle
184f6 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 73 71 6c n(zCsr);. sql
184f7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 ite3_snprintf(10
184f8 30 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20 0, zCsr, "%d[",
184f9 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 pMem->n);. zC
184fa 73 72 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 sr += strlen(zCs
184fb 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b r);. for(i=0;
184fc 20 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d i<16 && i<pMem-
184fd 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 >n; i++){.
184fe 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
184ff 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30 32 (100, zCsr, "%02
18500 58 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e X", ((int)pMem->
18501 7a 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a 20 z[i] & 0xFF));.
18502 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 zCsr += str
18503 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d len(zCsr);. }
18504 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
18505 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 16 && i<pMem->n;
18506 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 i++){. cha
18507 72 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d r z = pMem->z[i]
18508 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32 ;. if( z<32
18509 20 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 || z>126 ) *zCs
1850a 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 r++ = '.';.
1850b 20 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 else *zCsr++ =
1850c 7a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 z;. }.. sq
1850d 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 lite3_snprintf(1
1850e 30 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 00, zCsr, "]%s",
1850f 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e encnames[pMem->
18510 65 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 enc]);. zCsr
18511 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29 3b += strlen(zCsr);
18512 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d . if( f & MEM
18513 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 73 _Zero ){. s
18514 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
18515 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 6c 6c 64 100, zCsr,"+%lld
18516 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 z",pMem->u.i);.
18517 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 zCsr += str
18518 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d len(zCsr);. }
18519 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c 30 . *zCsr = '\0
1851a 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 ';. }else if( f
1851b 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 & MEM_Str ){.
1851c 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 int j, k;.
1851d 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 zBuf[0] = ' ';.
1851e 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44 if( f & MEM_D
1851f 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 yn ){. zBuf
18520 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 [1] = 'z';.
18521 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
18522 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 EM_Static|MEM_Ep
18523 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 hem))==0 );.
18524 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 }else if( f & ME
18525 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 M_Static ){.
18526 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 3b zBuf[1] = 't';
18527 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
18528 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d f & (MEM_Dyn|MEM
18529 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 _Ephem))==0 );.
1852a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 }else if( f &
1852b 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 MEM_Ephem ){.
1852c 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 65 zBuf[1] = 'e
1852d 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ';. assert(
1852e 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 (f & (MEM_Stati
1852f 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 c|MEM_Dyn))==0 )
18530 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
18531 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 27 zBuf[1] = 's'
18532 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d 20 ;. }. k =
18533 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 2;. sqlite3_s
18534 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a 42 nprintf(100, &zB
18535 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d 65 uf[k], "%d", pMe
18536 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 m->n);. k +=
18537 73 74 72 6c 65 6e 28 26 7a 42 75 66 5b 6b 5d 29 strlen(&zBuf[k])
18538 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 ;. zBuf[k++]
18539 3d 20 27 5b 27 3b 0a 20 20 20 20 66 6f 72 28 6a = '[';. for(j
1853a 3d 30 3b 20 6a 3c 31 35 20 26 26 20 6a 3c 70 4d =0; j<15 && j<pM
1853b 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 em->n; j++){.
1853c 20 20 20 75 38 20 63 20 3d 20 70 4d 65 6d 2d 3e u8 c = pMem->
1853d 7a 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 z[j];. if(
1853e 63 3e 3d 30 78 32 30 20 26 26 20 63 3c 30 78 37 c>=0x20 && c<0x7
1853f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 f ){. zBu
18540 66 5b 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 f[k++] = c;.
18541 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
18542 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 zBuf[k++] = '.'
18543 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
18544 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 zBuf[k++] =
18545 27 5d 27 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ']';. sqlite3
18546 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 26 7a _snprintf(100,&z
18547 42 75 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 Buf[k], encnames
18548 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 [pMem->enc]);.
18549 20 20 6b 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a k += strlen(&z
1854a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 Buf[k]);. zBu
1854b 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a f[k++] = 0;. }.
1854c 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 }.#endif..#ifdef
1854d 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a SQLITE_DEBUG./*
1854e 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61 .** Print the va
1854f 6c 75 65 20 6f 66 20 61 20 72 65 67 69 73 74 65 lue of a registe
18550 72 20 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75 r for tracing pu
18551 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 rposes:.*/.stati
18552 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 63 65 50 c void memTraceP
18553 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 74 2c 20 rint(FILE *out,
18554 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 Mem *p){. if( p
18555 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
18556 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 ll ){. fprint
18557 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 29 3b f(out, " NULL");
18558 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d . }else if( (p-
18559 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e >flags & (MEM_In
1855a 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 t|MEM_Str))==(ME
1855b 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29 M_Int|MEM_Str) )
1855c 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 {. fprintf(ou
1855d 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70 t, " si:%lld", p
1855e 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 ->u.i);. }else
1855f 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d if( p->flags & M
18560 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 66 70 EM_Int ){. fp
18561 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 69 3a 25 rintf(out, " i:%
18562 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 lld", p->u.i);.
18563 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c }else if( p->fl
18564 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 ags & MEM_Real )
18565 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 {. fprintf(ou
18566 74 2c 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72 t, " r:%g", p->r
18567 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
18568 63 68 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a char zBuf[200];.
18569 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1856a 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 2c emPrettyPrint(p,
1856b 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 zBuf);. fpri
1856c 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 3b 0a 20 ntf(out, " ");.
1856d 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
1856e 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d "%s", zBuf);. }
1856f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 .}.static void r
18570 65 67 69 73 74 65 72 54 72 61 63 65 28 46 49 4c egisterTrace(FIL
18571 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 52 65 67 E *out, int iReg
18572 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 66 70 72 , Mem *p){. fpr
18573 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 47 5b 25 intf(out, "REG[%
18574 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 3b 0a 20 d] = ", iReg);.
18575 20 6d 65 6d 54 72 61 63 65 50 72 69 6e 74 28 6f memTracePrint(o
18576 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 69 6e 74 ut, p);. fprint
18577 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 7d 0a f(out, "\n");.}.
18578 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
18579 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 20 64 QLITE_DEBUG.# d
1857a 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 efine REGISTER_T
1857b 52 41 43 45 28 52 2c 4d 29 20 69 66 28 70 2d 3e RACE(R,M) if(p->
1857c 74 72 61 63 65 29 72 65 67 69 73 74 65 72 54 72 trace)registerTr
1857d 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 52 2c 4d ace(p->trace,R,M
1857e 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e ).#else.# defin
1857f 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 e REGISTER_TRACE
18580 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 (R,M).#endif...#
18581 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
18582 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d LE../* .** hwtim
18583 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c e.h contains inl
18584 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f ine assembler co
18585 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 de for implement
18586 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 ing .** high-per
18587 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 formance timing
18588 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a routines..*/./**
18589 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
1858a 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e lude hwtime.h in
1858b 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 the middle of v
1858c 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dbe.c **********
1858d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
1858e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
1858f 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 in file hwtime.h
18590 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
18591 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18592 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
18593 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a ** 2008 May 27.*
18594 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
18595 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
18596 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
18597 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
18598 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
18599 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
1859a 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
1859b 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
1859c 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1859d 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1859e 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1859f 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
185a0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
185a1 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
185a2 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
185a3 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
185a4 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
185a5 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
185a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
185a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
185a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
185a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
185aa 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
185ab 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 le contains inli
185ac 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 ne asm code for
185ad 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 retrieving "high
185ae 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a -performance".**
185af 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 counters for x8
185b0 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 6 class CPUs..**
185b1 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e .** $Id: hwtime.
185b2 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f h,v 1.3 2008/08/
185b3 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 01 14:33:15 shan
185b4 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 e Exp $.*/.#ifnd
185b5 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 ef _HWTIME_H_.#d
185b6 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f efine _HWTIME_H_
185b7 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
185b8 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e owing routine on
185b9 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 ly works on pent
185ba 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 ium-class (or ne
185bb 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e wer) processors.
185bc 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 .** It uses the
185bd 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 RDTSC opcode to
185be 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 read the cycle c
185bf 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f ount value out o
185c0 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 f the.** process
185c1 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 or and returns t
185c2 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 hat value. This
185c3 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 can be used for
185c4 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f high-res.** pro
185c5 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 filing..*/.#if (
185c6 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
185c7 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 ) || defined(_MS
185c8 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 C_VER)) && \.
185c9 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 (defined(i386
185ca 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 ) || defined(__i
185cb 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 386__) || define
185cc 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 d(_M_IX86)).. #
185cd 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 if defined(__GNU
185ce 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 C__).. __inline
185cf 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
185d0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
185d1 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 oid){. unsig
185d2 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a ned int lo, hi;.
185d3 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
185d4 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 olatile__ ("rdts
185d5 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 c" : "=a" (lo),
185d6 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 "=d" (hi));.
185d7 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f return (sqlite_
185d8 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 uint64)hi << 32
185d9 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c | lo;. }.. #el
185da 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f if defined(_MSC_
185db 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 VER).. __declsp
185dc 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 ec(naked) __inli
185dd 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ne sqlite_uint64
185de 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 __cdecl sqlite3
185df 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
185e0 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 __asm {.
185e1 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 rdtsc.
185e2 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 ret ; ret
185e3 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 urn value at EDX
185e4 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a :EAX. }. }.
185e5 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 . #endif..#elif
185e6 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
185e7 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f __) && defined(_
185e8 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f _x86_64__)).. _
185e9 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
185ea 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
185eb 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
185ec 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
185ed 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 val;. __as
185ee 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
185ef 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 ("rdtsc" : "=A"
185f0 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 (val));. r
185f1 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 eturn val;. }.
185f2 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 .#elif (defined(
185f3 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 __GNUC__) && def
185f4 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a ined(__ppc__))..
185f5 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
185f6 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
185f7 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
185f8 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c unsigned l
185f9 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b ong long retval;
185fa 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
185fb 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 long junk;.
185fc 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
185fd 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 ile__ ("\n\.
185fe 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 1: mf
185ff 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 tbu %1\n\.
18600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 mf
18601 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 tb %L0\n\.
18602 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
18603 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 ftbu %0\n\.
18604 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
18605 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a mpw %0,%1\n\.
18606 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18607 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 bne 1b".
18608 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18609 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c : "=r" (retval),
1860a 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 "=r" (junk));.
1860b 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 return retv
1860c 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a al;. }..#else..
1860d 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d #error Need im
1860e 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
1860f 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
18610 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 for your platfor
18611 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f m... /*. ** To
18612 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 compile without
18613 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 implementing sq
18614 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
18615 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c r your platform,
18616 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 . ** you can re
18617 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 move the above #
18618 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 error and use th
18619 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a e following. **
1861a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 stub function.
1861b 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 You will lose t
1861c 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f iming support fo
1861d 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 r many. ** of t
1861e 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 he debugging and
1861f 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 testing utiliti
18620 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c es, but it shoul
18621 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 d at. ** least
18622 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e compile and run.
18623 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 . */.SQLITE_PRI
18624 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 VATE sqlite_ui
18625 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
18626 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e me(void){ return
18627 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ((sqlite_uint64
18628 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a )0); }..#endif..
18629 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
1862a 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a ed(_HWTIME_H_) *
1862b 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
1862c 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 ** End of hwtime
1862d 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
1862e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1862f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18630 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
18631 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
18632 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
18633 69 6e 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a in vdbe.c ******
18634 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18635 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a */..#endif../*.*
18636 2a 20 54 68 65 20 43 48 45 43 4b 5f 46 4f 52 5f * The CHECK_FOR_
18637 49 4e 54 45 52 52 55 50 54 20 6d 61 63 72 6f 20 INTERRUPT macro
18638 64 65 66 69 6e 65 64 20 68 65 72 65 20 6c 6f 6f defined here loo
18639 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 ks to see if the
1863a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 .** sqlite3_inte
1863b 72 72 75 70 74 28 29 20 72 6f 75 74 69 6e 65 20 rrupt() routine
1863c 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e has been called.
1863d 20 20 49 66 20 69 74 20 68 61 73 20 62 65 65 6e If it has been
1863e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 , then.** proces
1863f 73 69 6e 67 20 6f 66 20 74 68 65 20 56 44 42 45 sing of the VDBE
18640 20 70 72 6f 67 72 61 6d 20 69 73 20 69 6e 74 65 program is inte
18641 72 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 rrupted..**.** T
18642 68 69 73 20 6d 61 63 72 6f 20 61 64 64 65 64 20 his macro added
18643 74 6f 20 65 76 65 72 79 20 69 6e 73 74 72 75 63 to every instruc
18644 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 61 tion that does a
18645 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65 72 20 74 jump in order t
18646 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 61 o.** implement a
18647 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20 74 65 73 loop. This tes
18648 74 20 75 73 65 64 20 74 6f 20 62 65 20 6f 6e 20 t used to be on
18649 65 76 65 72 79 20 73 69 6e 67 6c 65 20 69 6e 73 every single ins
1864a 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 truction,.** but
1864b 20 74 68 61 74 20 6d 65 61 6e 74 20 77 65 20 6d that meant we m
1864c 6f 72 65 20 74 65 73 74 69 6e 67 20 74 68 61 74 ore testing that
1864d 20 77 65 20 6e 65 65 64 65 64 2e 20 20 42 79 20 we needed. By
1864e 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20 74 68 65 only testing the
1864f 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a 75 6d 70 .** flag on jump
18650 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2c 20 77 instructions, w
18651 65 20 67 65 74 20 61 20 28 73 6d 61 6c 6c 29 20 e get a (small)
18652 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e speed improvemen
18653 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 t..*/.#define CH
18654 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 ECK_FOR_INTERRUP
18655 54 20 5c 0a 20 20 20 69 66 28 20 64 62 2d 3e 75 T \. if( db->u
18656 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 1.isInterrupted
18657 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
18658 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3b 0a 0a _to_interrupt;..
18659 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1865a 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20 66 BUG.static int f
1865b 69 6c 65 45 78 69 73 74 73 28 73 71 6c 69 74 65 ileExists(sqlite
1865c 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
1865d 72 20 2a 7a 46 69 6c 65 29 7b 0a 20 20 69 6e 74 r *zFile){. int
1865e 20 72 65 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 res = 0;. int
1865f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
18660 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
18661 53 54 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 ST. /* If we ar
18662 65 20 63 75 72 72 65 6e 74 6c 79 20 74 65 73 74 e currently test
18663 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 2c 20 74 ing IO errors, t
18664 68 65 6e 20 64 6f 20 6e 6f 74 20 63 61 6c 6c 20 hen do not call
18665 4f 73 41 63 63 65 73 73 28 29 20 74 6f 0a 20 20 OsAccess() to.
18666 2a 2a 20 74 65 73 74 20 66 6f 72 20 74 68 65 20 ** test for the
18667 70 72 65 73 65 6e 63 65 20 6f 66 20 7a 46 69 6c presence of zFil
18668 65 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 e. This is becau
18669 73 65 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 se any IO error
1866a 74 68 61 74 0a 20 20 2a 2a 20 6f 63 63 75 72 73 that. ** occurs
1866b 20 68 65 72 65 20 77 69 6c 6c 20 6e 6f 74 20 62 here will not b
1866c 65 20 72 65 70 6f 72 74 65 64 2c 20 63 61 75 73 e reported, caus
1866d 69 6e 67 20 74 68 65 20 74 65 73 74 20 74 6f 20 ing the test to
1866e 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 65 78 74 fail.. */. ext
1866f 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f ern int sqlite3_
18670 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 io_error_pending
18671 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f ;. if( sqlite3_
18672 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 io_error_pending
18673 3c 3d 30 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 <=0 ).#endif.
18674 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
18675 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73 2c 20 ccess(db->pVfs,
18676 7a 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f 41 43 zFile, SQLITE_AC
18677 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 CESS_EXISTS, &re
18678 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 72 65 s);. return (re
18679 73 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f s && rc==SQLITE_
1867a 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f OK);.}.#endif../
1867b 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 73 20 *.** Execute as
1867c 6d 75 63 68 20 6f 66 20 61 20 56 44 42 45 20 70 much of a VDBE p
1867d 72 6f 67 72 61 6d 20 61 73 20 77 65 20 63 61 6e rogram as we can
1867e 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a then return..**
1867f 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d .** sqlite3VdbeM
18680 61 6b 65 52 65 61 64 79 28 29 20 6d 75 73 74 20 akeReady() must
18681 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 be called before
18682 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e this routine in
18683 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f order to.** clo
18684 73 65 20 74 68 65 20 70 72 6f 67 72 61 6d 20 77 se the program w
18685 69 74 68 20 61 20 66 69 6e 61 6c 20 4f 50 5f 48 ith a final OP_H
18686 61 6c 74 20 61 6e 64 20 74 6f 20 73 65 74 20 75 alt and to set u
18687 70 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 0a p the callbacks.
18688 2a 2a 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 ** and the error
18689 20 6d 65 73 73 61 67 65 20 70 6f 69 6e 74 65 72 message pointer
1868a 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65 76 65 72 ..**.** Whenever
1868b 20 61 20 72 6f 77 20 6f 72 20 72 65 73 75 6c 74 a row or result
1868c 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 62 data is availab
1868d 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 le, this routine
1868e 20 77 69 6c 6c 20 65 69 74 68 65 72 0a 2a 2a 20 will either.**
1868f 69 6e 76 6f 6b 65 20 74 68 65 20 72 65 73 75 6c invoke the resul
18690 74 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 t callback (if t
18691 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 here is one) or
18692 72 65 74 75 72 6e 20 77 69 74 68 0a 2a 2a 20 53 return with.** S
18693 51 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a QLITE_ROW..**.**
18694 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 If an attempt i
18695 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 s made to open a
18696 20 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73 65 locked database
18697 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
18698 69 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65 69 74 68 ine.** will eith
18699 65 72 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 er invoke the bu
1869a 73 79 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 sy callback (if
1869b 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 there is one) or
1869c 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 it will.** retu
1869d 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a rn SQLITE_BUSY..
1869e 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
1869f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 r occurs, an err
186a0 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 77 72 or message is wr
186a1 69 74 74 65 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 itten to memory
186a2 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d obtained.** from
186a3 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
186a4 29 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d 73 67 ) and p->zErrMsg
186a5 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e is made to poin
186a6 74 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 t to that memory
186a7 2e 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 ..** The error c
186a8 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e ode is stored in
186a9 20 70 2d 3e 72 63 20 61 6e 64 20 74 68 69 73 20 p->rc and this
186aa 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
186ab 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a SQLITE_ERROR..**
186ac 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 .** If the callb
186ad 61 63 6b 20 65 76 65 72 20 72 65 74 75 72 6e 73 ack ever returns
186ae 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
186af 74 68 65 20 70 72 6f 67 72 61 6d 20 65 78 69 74 the program exit
186b0 73 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 s.** immediately
186b1 2e 20 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 . There will be
186b2 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61 67 no error messag
186b3 65 20 62 75 74 20 74 68 65 20 70 2d 3e 72 63 20 e but the p->rc
186b4 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 field is.** set
186b5 74 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 to SQLITE_ABORT
186b6 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 and this routine
186b7 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c will return SQL
186b8 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a ITE_ERROR..**.**
186b9 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 A memory alloca
186ba 74 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73 65 tion error cause
186bb 73 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73 65 s p->rc to be se
186bc 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 t to SQLITE_NOME
186bd 4d 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f M and this.** ro
186be 75 74 69 6e 65 20 74 6f 20 72 65 74 75 72 6e 20 utine to return
186bf 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a SQLITE_ERROR..**
186c0 0a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61 6c 20 .** Other fatal
186c1 65 72 72 6f 72 73 20 72 65 74 75 72 6e 20 53 51 errors return SQ
186c2 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a LITE_ERROR..**.*
186c3 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 * After this rou
186c4 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68 65 tine has finishe
186c5 64 2c 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 d, sqlite3VdbeFi
186c6 6e 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64 20 nalize() should
186c7 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 6c be.** used to cl
186c8 65 61 6e 20 75 70 20 74 68 65 20 6d 65 73 73 20 ean up the mess
186c9 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 62 65 that was left be
186ca 68 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f hind..*/.SQLITE_
186cb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
186cc 74 65 33 56 64 62 65 45 78 65 63 28 0a 20 20 56 te3VdbeExec(. V
186cd 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 dbe *p
186ce 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
186cf 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e VDBE */.){. in
186d0 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 t pc;
186d1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
186d2 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 program counter
186d3 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 */. Op *pOp;
186d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
186d5 2f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61 /* Current opera
186d6 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 tion */. int rc
186d7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
186d8 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
186d9 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c return */. sql
186da 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
186db 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 ; /* The d
186dc 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 atabase */. u8
186dd 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28 64 encoding = ENC(d
186de 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 64 b); /* The d
186df 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 atabase encoding
186e0 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 2c */. Mem *pIn1,
186e1 20 2a 70 49 6e 32 2c 20 2a 70 49 6e 33 3b 20 20 *pIn2, *pIn3;
186e2 20 2f 2a 20 49 6e 70 75 74 20 6f 70 65 72 61 6e /* Input operan
186e3 64 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4f 75 ds */. Mem *pOu
186e4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
186e5 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 6f 70 65 /* Output ope
186e6 72 61 6e 64 20 2a 2f 0a 20 20 75 38 20 6f 70 50 rand */. u8 opP
186e7 72 6f 70 65 72 74 79 3b 0a 20 20 69 6e 74 20 69 roperty;. int i
186e8 43 6f 6d 70 61 72 65 20 3d 20 30 3b 20 20 20 20 Compare = 0;
186e9 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
186ea 6f 66 20 6c 61 73 74 20 4f 50 5f 43 6f 6d 70 61 of last OP_Compa
186eb 72 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a re operation */.
186ec 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74 65 20 int *aPermute
186ed 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
186ee 50 65 72 6d 75 61 74 69 6f 6e 20 6f 66 20 63 6f Permuation of co
186ef 6c 75 6d 6e 73 20 66 6f 72 20 4f 50 5f 43 6f 6d lumns for OP_Com
186f0 70 61 72 65 20 2a 2f 0a 23 69 66 64 65 66 20 56 pare */.#ifdef V
186f1 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 75 36 DBE_PROFILE. u6
186f2 34 20 73 74 61 72 74 3b 20 20 20 20 20 20 20 20 4 start;
186f3 20 20 20 20 20 20 20 20 20 2f 2a 20 43 50 55 20 /* CPU
186f4 63 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 74 20 73 clock count at s
186f5 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a tart of opcode *
186f6 2f 0a 20 20 69 6e 74 20 6f 72 69 67 50 63 3b 20 /. int origPc;
186f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
186f8 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 * Program counte
186f9 72 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 r at start of op
186fa 63 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 code */.#endif.#
186fb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
186fc 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c IT_PROGRESS_CALL
186fd 42 41 43 4b 0a 20 20 69 6e 74 20 6e 50 72 6f 67 BACK. int nProg
186fe 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20 20 ressOps = 0;
186ff 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78 65 /* Opcodes exe
18700 63 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f 67 cuted since prog
18701 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20 2a ress callback. *
18702 2f 0a 23 65 6e 64 69 66 0a 20 20 55 6e 70 61 63 /.#endif. Unpac
18703 6b 65 64 52 65 63 6f 72 64 20 61 54 65 6d 70 52 kedRecord aTempR
18704 65 63 5b 31 36 5d 3b 20 2f 2a 20 53 70 61 63 65 ec[16]; /* Space
18705 20 74 6f 20 68 6f 6c 64 20 61 20 74 72 61 6e 73 to hold a trans
18706 69 65 6e 74 20 55 6e 70 61 63 6b 65 64 52 65 63 ient UnpackedRec
18707 6f 72 64 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 ord */... asser
18708 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 t( p->magic==VDB
18709 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20 20 E_MAGIC_RUN );
1870a 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 /* sqlite3_step(
1870b 29 20 76 65 72 69 66 69 65 73 20 74 68 69 73 20 ) verifies this
1870c 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d */. assert( db-
1870d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d >magic==SQLITE_M
1870e 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 73 AGIC_BUSY );. s
1870f 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
18710 41 72 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 ArrayEnter(&p->a
18711 4d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 2d Mutex);. if( p-
18712 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 >rc==SQLITE_NOME
18713 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 M ){. /* This
18714 20 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 happens if a ma
18715 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 lloc() inside a
18716 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
18717 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 column_text() or
18718 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f . ** sqlite3_
18719 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 column_text16()
1871a 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 failed. */.
1871b 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d goto no_mem;. }
1871c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 . assert( p->rc
1871d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 ==SQLITE_OK || p
1871e 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ->rc==SQLITE_BUS
1871f 59 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 Y );. p->rc = S
18720 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 QLITE_OK;. asse
18721 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d rt( p->explain==
18722 30 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 0 );. p->pResul
18723 74 53 65 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e tSet = 0;. db->
18724 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 busyHandler.nBus
18725 79 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b 5f 46 y = 0;. CHECK_F
18726 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 OR_INTERRUPT;.
18727 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 sqlite3VdbeIOTra
18728 63 65 53 71 6c 28 70 29 3b 0a 23 69 66 64 65 66 ceSql(p);.#ifdef
18729 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
1872a 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
1872b 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 gnMalloc();. if
1872c 28 20 70 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 ( p->pc==0 . &
1872d 26 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 & ((p->db->flags
1872e 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 & SQLITE_VdbeLi
1872f 73 74 69 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 sting) || fileEx
18730 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f 65 ists(db, "vdbe_e
18731 78 70 6c 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 xplain")). ){.
18732 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 int i;. pr
18733 69 6e 74 66 28 22 56 44 42 45 20 50 72 6f 67 72 intf("VDBE Progr
18734 61 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b am Listing:\n");
18735 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
18736 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 PrintSql(p);.
18737 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
18738 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 Op; i++){.
18739 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 sqlite3VdbePrint
1873a 4f 70 28 73 74 64 6f 75 74 2c 20 69 2c 20 26 70 Op(stdout, i, &p
1873b 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 7d ->aOp[i]);. }
1873c 0a 20 20 7d 0a 20 20 69 66 28 20 66 69 6c 65 45 . }. if( fileE
1873d 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f xists(db, "vdbe_
1873e 74 72 61 63 65 22 29 20 29 7b 0a 20 20 20 20 70 trace") ){. p
1873f 2d 3e 74 72 61 63 65 20 3d 20 73 74 64 6f 75 74 ->trace = stdout
18740 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 ;. }. sqlite3E
18741 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
18742 3b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 70 ;.#endif. for(p
18743 63 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 51 4c c=p->pc; rc==SQL
18744 49 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 ITE_OK; pc++){.
18745 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 30 assert( pc>=0
18746 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b && pc<p->nOp );
18747 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c . if( db->mal
18748 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f locFailed ) goto
18749 20 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 66 20 no_mem;.#ifdef
1874a 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 VDBE_PROFILE.
1874b 20 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a 20 20 origPc = pc;.
1874c 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65 start = sqlite
1874d 33 48 77 74 69 6d 65 28 29 3b 0a 23 65 6e 64 69 3Hwtime();.#endi
1874e 66 0a 20 20 20 20 70 4f 70 20 3d 20 26 70 2d 3e f. pOp = &p->
1874f 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a aOp[pc];.. /*
18750 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20 74 72 61 63 Only allow trac
18751 69 6e 67 20 69 66 20 53 51 4c 49 54 45 5f 44 45 ing if SQLITE_DE
18752 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a BUG is defined..
18753 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 */.#ifdef SQ
18754 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 LITE_DEBUG. i
18755 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 f( p->trace ){.
18756 20 20 20 20 20 69 66 28 20 70 63 3d 3d 30 20 29 if( pc==0 )
18757 7b 0a 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 {. printf
18758 28 22 56 44 42 45 20 45 78 65 63 75 74 69 6f 6e ("VDBE Execution
18759 20 54 72 61 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 Trace:\n");.
1875a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1875b 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 PrintSql(p);.
1875c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
1875d 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 70 2d e3VdbePrintOp(p-
1875e 3e 74 72 61 63 65 2c 20 70 63 2c 20 70 4f 70 29 >trace, pc, pOp)
1875f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
18760 70 2d 3e 74 72 61 63 65 3d 3d 30 20 26 26 20 70 p->trace==0 && p
18761 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 c==0 ){. sq
18762 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
18763 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
18764 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 if( fileExists(d
18765 62 2c 20 22 76 64 62 65 5f 73 71 6c 74 72 61 63 b, "vdbe_sqltrac
18766 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 e") ){. s
18767 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 qlite3VdbePrintS
18768 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ql(p);. }.
18769 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 sqlite3EndB
1876a 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
1876b 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
1876c 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b .. /* Check
1876d 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 to see if we ne
1876e 65 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 ed to simulate a
1876f 6e 20 69 6e 74 65 72 72 75 70 74 2e 20 20 54 68 n interrupt. Th
18770 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 0a is only happens.
18771 20 20 20 20 2a 2a 20 69 66 20 77 65 20 68 61 76 ** if we hav
18772 65 20 61 20 73 70 65 63 69 61 6c 20 74 65 73 74 e a special test
18773 20 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 build.. */.#
18774 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
18775 54 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 T. if( sqlite
18776 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 3_interrupt_coun
18777 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c t>0 ){. sql
18778 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 ite3_interrupt_c
18779 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 69 66 ount--;. if
1877a 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 ( sqlite3_interr
1877b 75 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a upt_count==0 ){.
1877c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
1877d 69 6e 74 65 72 72 75 70 74 28 64 62 29 3b 0a 20 interrupt(db);.
1877e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e }. }.#en
1877f 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
18780 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 ITE_OMIT_PROGRES
18781 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20 20 2f S_CALLBACK. /
18782 2a 20 43 61 6c 6c 20 74 68 65 20 70 72 6f 67 72 * Call the progr
18783 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 ess callback if
18784 69 74 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 it is configured
18785 20 61 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 and the require
18786 64 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2a 20 d number. **
18787 6f 66 20 56 44 42 45 20 6f 70 73 20 68 61 76 65 of VDBE ops have
18788 20 62 65 65 6e 20 65 78 65 63 75 74 65 64 20 28 been executed (
18789 65 69 74 68 65 72 20 73 69 6e 63 65 20 74 68 69 either since thi
1878a 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a s invocation of.
1878b 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 ** sqlite3Vd
1878c 62 65 45 78 65 63 28 29 20 6f 72 20 73 69 6e 63 beExec() or sinc
1878d 65 20 6c 61 73 74 20 74 69 6d 65 20 74 68 65 20 e last time the
1878e 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
1878f 6b 20 77 61 73 20 63 61 6c 6c 65 64 29 2e 0a 20 k was called)..
18790 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 72 6f ** If the pro
18791 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 gress callback r
18792 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c eturns non-zero,
18793 20 65 78 69 74 20 74 68 65 20 76 69 72 74 75 61 exit the virtua
18794 6c 20 6d 61 63 68 69 6e 65 20 77 69 74 68 0a 20 l machine with.
18795 20 20 20 2a 2a 20 61 20 72 65 74 75 72 6e 20 63 ** a return c
18796 6f 64 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 ode SQLITE_ABORT
18797 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
18798 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 29 db->xProgress )
18799 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e {. if( db->
1879a 6e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d 6e 50 nProgressOps==nP
1879b 72 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a 20 20 rogressOps ){.
1879c 20 20 20 20 20 20 69 6e 74 20 70 72 63 3b 0a 20 int prc;.
1879d 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
1879e 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1879f 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
187a0 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 _to_misuse;.
187a1 20 20 20 20 70 72 63 20 3d 64 62 2d 3e 78 50 72 prc =db->xPr
187a2 6f 67 72 65 73 73 28 64 62 2d 3e 70 50 72 6f 67 ogress(db->pProg
187a3 72 65 73 73 41 72 67 29 3b 0a 20 20 20 20 20 20 ressArg);.
187a4 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
187a5 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f etyOn(db) ) goto
187a6 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
187a7 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20 69 66 suse;. if
187a8 28 20 70 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 ( prc!=0 ){.
187a9 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
187aa 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 E_INTERRUPT;.
187ab 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 goto vdbe
187ac 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 _error_halt;.
187ad 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e }. n
187ae 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b ProgressOps = 0;
187af 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e . }. n
187b0 50 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 ProgressOps++;.
187b1 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
187b2 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 /* Do common se
187b3 74 75 70 20 70 72 6f 63 65 73 73 69 6e 67 20 66 tup processing f
187b4 6f 72 20 61 6e 79 20 6f 70 63 6f 64 65 20 74 68 or any opcode th
187b5 61 74 20 69 73 20 6d 61 72 6b 65 64 0a 20 20 20 at is marked.
187b6 20 2a 2a 20 77 69 74 68 20 74 68 65 20 22 6f 75 ** with the "ou
187b7 74 32 2d 70 72 65 72 65 6c 65 61 73 65 22 20 74 t2-prerelease" t
187b8 61 67 2e 20 20 53 75 63 68 20 6f 70 63 6f 64 65 ag. Such opcode
187b9 73 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 0a s have a single.
187ba 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 77 68 ** output wh
187bb 69 63 68 20 69 73 20 73 70 65 63 69 66 69 65 64 ich is specified
187bc 20 62 79 20 74 68 65 20 50 32 20 70 61 72 61 6d by the P2 param
187bd 65 74 65 72 2e 20 20 54 68 65 20 50 32 20 72 65 eter. The P2 re
187be 67 69 73 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 gister. ** is
187bf 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 initialized to
187c0 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 a NULL.. */.
187c1 20 20 20 6f 70 50 72 6f 70 65 72 74 79 20 3d 20 opProperty =
187c2 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 70 opcodeProperty[p
187c3 4f 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 Op->opcode];.
187c4 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 if( (opProperty
187c5 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 & OPFLG_OUT2_PR
187c6 45 52 45 4c 45 41 53 45 29 21 3d 30 20 29 7b 0a ERELEASE)!=0 ){.
187c7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
187c8 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 p->p2>0 );.
187c9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
187ca 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 <=p->nMem );.
187cb 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d pOut = &p->aM
187cc 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 em[pOp->p2];.
187cd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
187ce 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c mReleaseExternal
187cf 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f (pOut);. pO
187d0 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
187d1 4e 75 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 0a Null;. }else.
187d2 20 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d . /* Do comm
187d3 6f 6e 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63 on setup for opc
187d4 6f 64 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68 odes marked with
187d5 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
187d6 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d owing. ** com
187d7 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f binations of pro
187d8 70 65 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a perties.. **.
187d9 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
187da 20 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 in1. **
187db 20 20 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20 in1 in2.
187dc 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 ** i
187dd 6e 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20 n1 in2 out3.
187de 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 ** in1
187df 20 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20 in3. **.
187e0 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e ** Variables pIn
187e1 31 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e 1, pIn2, and pIn
187e2 33 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f 3 are made to po
187e3 69 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61 int to appropria
187e4 74 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 te. ** regist
187e5 65 72 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20 ers for inputs.
187e6 20 56 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70 Variable pOut p
187e7 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74 oints to the out
187e8 70 75 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20 put register..
187e9 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70 */. if( (op
187ea 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 Property & OPFLG
187eb 5f 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 _IN1)!=0 ){.
187ec 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
187ed 31 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 1>0 );. ass
187ee 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d ert( pOp->p1<=p-
187ef 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 >nMem );. p
187f0 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In1 = &p->aMem[p
187f1 4f 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52 Op->p1];. R
187f2 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
187f3 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 p->p1, pIn1);.
187f4 20 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 if( (opPrope
187f5 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 rty & OPFLG_IN2)
187f6 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 !=0 ){. a
187f7 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 ssert( pOp->p2>0
187f8 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 );. asse
187f9 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e rt( pOp->p2<=p->
187fa 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 nMem );.
187fb 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pIn2 = &p->aMem[
187fc 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 pOp->p2];.
187fd 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
187fe 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b (pOp->p2, pIn2);
187ff 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6f 70 . if( (op
18800 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 Property & OPFLG
18801 5f 4f 55 54 33 29 21 3d 30 20 29 7b 0a 20 20 20 _OUT3)!=0 ){.
18802 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
18803 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 Op->p3>0 );.
18804 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
18805 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
18806 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 ;. pOut
18807 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
18808 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a >p3];. }.
18809 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1880a 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 (opProperty & OP
1880b 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 FLG_IN3)!=0 ){.
1880c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1880d 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 Op->p3>0 );.
1880e 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1880f 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
18810 20 20 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 pIn3 = &
18811 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
18812 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54 ;. REGIST
18813 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 ER_TRACE(pOp->p3
18814 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d , pIn3);. }
18815 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 . }else if( (
18816 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
18817 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 LG_IN2)!=0 ){.
18818 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
18819 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 >p2>0 );. a
1881a 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d ssert( pOp->p2<=
1881b 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 p->nMem );.
1881c 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn2 = &p->aMem
1881d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 [pOp->p2];.
1881e 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1881f 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a pOp->p2, pIn2);.
18820 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f }else if( (o
18821 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c pProperty & OPFL
18822 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 G_IN3)!=0 ){.
18823 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
18824 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 p3>0 );. as
18825 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 sert( pOp->p3<=p
18826 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 ->nMem );.
18827 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pIn3 = &p->aMem[
18828 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 pOp->p3];.
18829 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1882a 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 Op->p3, pIn3);.
1882b 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 }.. switch
1882c 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b ( pOp->opcode ){
1882d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1882e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1882f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18831 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18832 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73 .** What follows
18833 20 69 73 20 61 20 6d 61 73 73 69 76 65 20 73 77 is a massive sw
18834 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 itch statement w
18835 68 65 72 65 20 65 61 63 68 20 63 61 73 65 20 69 here each case i
18836 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 mplements a.** s
18837 65 70 61 72 61 74 65 20 69 6e 73 74 72 75 63 74 eparate instruct
18838 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75 ion in the virtu
18839 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20 al machine. If
1883a 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73 we follow the us
1883b 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69 ual.** indentati
1883c 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 on conventions,
1883d 65 61 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64 each case should
1883e 20 62 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20 be indented by
1883f 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a 6 spaces. But.*
18840 2a 20 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20 * that is a lot
18841 6f 66 20 77 61 73 74 65 64 20 73 70 61 63 65 20 of wasted space
18842 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67 on the left marg
18843 69 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65 in. So the code
18844 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73 within.** the s
18845 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 witch statement
18846 77 69 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20 will break with
18847 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62 convention and b
18848 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e e flush-left. An
18849 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d other.** big com
1884a 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f ment (similar to
1884b 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20 this one) will
1884c 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69 mark the point i
1884d 6e 20 74 68 65 20 63 6f 64 65 20 77 68 65 72 65 n the code where
1884e 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f .** we transitio
1884f 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c n back to normal
18850 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a indentation..**
18851 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69 .** The formatti
18852 6e 67 20 6f 66 20 65 61 63 68 20 63 61 73 65 20 ng of each case
18853 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 is important. T
18854 68 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20 he makefile for
18855 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61 SQLite.** genera
18856 74 65 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20 tes two C files
18857 22 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20 "opcodes.h" and
18858 22 6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73 "opcodes.c" by s
18859 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20 canning this.**
1885a 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 file looking for
1885b 20 6c 69 6e 65 73 20 74 68 61 74 20 62 65 67 69 lines that begi
1885c 6e 20 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f n with "case OP_
1885d 22 2e 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e ". The opcodes.
1885e 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 h files.** will
1885f 62 65 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23 be filled with #
18860 64 65 66 69 6e 65 73 20 74 68 61 74 20 67 69 76 defines that giv
18861 65 20 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72 e unique integer
18862 20 76 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a values to each.
18863 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 ** opcode and th
18864 65 20 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 e opcodes.c file
18865 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 is filled with
18866 61 6e 20 61 72 72 61 79 20 6f 66 20 73 74 72 69 an array of stri
18867 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 ngs where.** eac
18868 68 20 73 74 72 69 6e 67 20 69 73 20 74 68 65 20 h string is the
18869 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f symbolic name fo
1886a 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 r the correspond
1886b 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 ing opcode. If
1886c 74 68 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 the.** case stat
1886d 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 ement is followe
1886e 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f d by a comment o
1886f 66 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 f the form "/# s
18870 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a ame as ... #/".*
18871 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69 * that comment i
18872 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
18873 69 6e 65 20 74 68 65 20 70 61 72 74 69 63 75 6c ine the particul
18874 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 ar value of the
18875 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 opcode..**.** Ot
18876 68 65 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 her keywords in
18877 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 the comment that
18878 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61 follows each ca
18879 73 65 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a se are used to.*
1887a 2a 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 * construct the
1887b 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 OPFLG_INITIALIZE
1887c 52 20 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69 R value that ini
1887d 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50 tializes opcodeP
1887e 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 roperty[]..** Ke
1887f 79 77 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20 ywords include:
18880 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f in1, in2, in3, o
18881 75 74 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20 ut2_prerelease,
18882 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65 out2, out3. See
18883 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 .** the mkopcode
18884 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 h.awk script for
18885 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
18886 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 rmation..**.** D
18887 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f ocumentation abo
18888 75 74 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 ut VDBE opcodes
18889 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 is generated by
1888a 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 scanning this fi
1888b 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 le.** for lines
1888c 6f 66 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 of that contain
1888d 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 "Opcode:". That
1888e 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 line and all su
1888f 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d bsequent.** comm
18890 65 6e 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73 ent lines are us
18891 65 64 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61 ed in the genera
18892 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f tion of the opco
18893 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 de.html document
18894 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a ation.** file..*
18895 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a *.** SUMMARY:.**
18896 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69 .** Formatti
18897 6e 67 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 ng is important
18898 74 6f 20 73 63 72 69 70 74 73 20 74 68 61 74 20 to scripts that
18899 73 63 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a scan this file..
1889a 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 ** Do not de
1889b 76 69 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66 viate from the f
1889c 6f 72 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20 ormatting style
1889d 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 currently in use
1889e 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1889f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
188a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
188a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
188a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
188a3 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 **/../* Opcode:
188a4 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a Goto * P2 * * *
188a5 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 .**.** An uncond
188a6 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 itional jump to
188a7 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54 address P2..** T
188a8 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 he next instruct
188a9 69 6f 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c ion executed wil
188aa 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 l be .** the one
188ab 20 61 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f at index P2 fro
188ac 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 m the beginning
188ad 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 of.** the progra
188ae 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f m..*/.case OP_Go
188af 74 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 to: {
188b0 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 /* jump */. C
188b1 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 HECK_FOR_INTERRU
188b2 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e PT;. pc = pOp->
188b3 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b p2 - 1;. break;
188b4 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 .}../* Opcode:
188b5 47 6f 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20 Gosub P1 P2 * *
188b6 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 *.**.** Write th
188b7 65 20 63 75 72 72 65 6e 74 20 61 64 64 72 65 73 e current addres
188b8 73 20 6f 6e 74 6f 20 72 65 67 69 73 74 65 72 20 s onto register
188b9 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a P1.** and then j
188ba 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 ump to address P
188bb 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 2..*/.case OP_Go
188bc 73 75 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 sub: {
188bd 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 /* jump */. a
188be 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 ssert( pOp->p1>0
188bf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
188c0 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p1<=p->nMem )
188c1 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 ;. pIn1 = &p->a
188c2 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
188c3 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 assert( (pIn1->f
188c4 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d lags & MEM_Dyn)=
188c5 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c =0 );. pIn1->fl
188c6 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
188c7 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b pIn1->u.i = pc;
188c8 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
188c9 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 E(pOp->p1, pIn1)
188ca 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 ;. pc = pOp->p2
188cb 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d - 1;. break;.}
188cc 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 ../* Opcode: Re
188cd 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a turn P1 * * * *.
188ce 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 **.** Jump to th
188cf 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
188d0 6f 6e 20 61 66 74 65 72 20 74 68 65 20 61 64 64 on after the add
188d1 72 65 73 73 20 69 6e 20 72 65 67 69 73 74 65 72 ress in register
188d2 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P1..*/.case OP_
188d3 52 65 74 75 72 6e 3a 20 7b 20 20 20 20 20 20 20 Return: {
188d4 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 /* in1 */.
188d5 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c assert( pIn1->fl
188d6 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b ags & MEM_Int );
188d7 0a 20 20 70 63 20 3d 20 70 49 6e 31 2d 3e 75 2e . pc = pIn1->u.
188d8 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f i;. break;.}../
188d9 2a 20 4f 70 63 6f 64 65 3a 20 20 59 69 65 6c 64 * Opcode: Yield
188da 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 * * * *.**.*
188db 2a 20 53 77 61 70 20 74 68 65 20 70 72 6f 67 72 * Swap the progr
188dc 61 6d 20 63 6f 75 6e 74 65 72 20 77 69 74 68 20 am counter with
188dd 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
188de 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 ister P1..*/.cas
188df 65 20 4f 50 5f 59 69 65 6c 64 3a 20 7b 0a 20 20 e OP_Yield: {.
188e0 69 6e 74 20 70 63 44 65 73 74 3b 0a 20 20 61 73 int pcDest;. as
188e1 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 sert( pOp->p1>0
188e2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
188e3 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p1<=p->nMem );
188e4 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d . pIn1 = &p->aM
188e5 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 em[pOp->p1];. a
188e6 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c ssert( (pIn1->fl
188e7 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d ags & MEM_Dyn)==
188e8 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 0 );. pIn1->fla
188e9 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
188ea 70 63 44 65 73 74 20 3d 20 70 49 6e 31 2d 3e 75 pcDest = pIn1->u
188eb 2e 69 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 .i;. pIn1->u.i
188ec 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52 = pc;. REGISTER
188ed 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 _TRACE(pOp->p1,
188ee 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70 63 pIn1);. pc = pc
188ef 44 65 73 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Dest;. break;.}
188f0 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 .../* Opcode: H
188f1 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20 2a alt P1 P2 * P4 *
188f2 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65 .**.** Exit imme
188f3 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 diately. All op
188f4 65 6e 20 63 75 72 73 6f 72 73 2c 20 46 69 66 6f en cursors, Fifo
188f5 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f 73 65 s, etc are close
188f6 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c d.** automatical
188f7 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 ly..**.** P1 is
188f8 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 20 the result code
188f9 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
188fa 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71 6c 69 te3_exec(), sqli
188fb 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a 2a 20 te3_reset(),.**
188fc 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c or sqlite3_final
188fd 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20 6e 6f ize(). For a no
188fe 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69 73 20 rmal halt, this
188ff 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 54 45 should be SQLITE
18900 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 _OK (0)..** For
18901 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e 20 62 errors, it can b
18902 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76 61 6c e some other val
18903 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20 74 68 ue. If P1!=0 th
18904 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74 65 72 en P2 will deter
18905 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 mine.** whether
18906 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 or not to rollba
18907 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 ck the current t
18908 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44 6f 20 ransaction. Do
18909 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 not rollback.**
1890a 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 if P2==OE_Fail.
1890b 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 Do the rollback
1890c 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 if P2==OE_Rollba
1890d 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45 5f 41 ck. If P2==OE_A
1890e 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 bort,.** then ba
1890f 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61 6e 67 ck out all chang
18910 65 73 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 es that have occ
18911 75 72 72 65 64 20 64 75 72 69 6e 67 20 74 68 69 urred during thi
18912 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 s execution of t
18913 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75 74 20 he.** VDBE, but
18914 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 do not rollback
18915 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
18916 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 .**.** If P4 is
18917 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 69 not null then i
18918 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 t is an error me
18919 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2a ssage string..**
1891a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20 .** There is an
1891b 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20 30 20 implied "Halt 0
1891c 30 20 30 22 20 69 6e 73 74 72 75 63 74 69 6f 6e 0 0" instruction
1891d 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 68 65 inserted at the
1891e 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 very end of.**
1891f 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e 20 20 every program.
18920 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74 20 74 So a jump past t
18921 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 he last instruct
18922 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f 67 72 ion of the progr
18923 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d am.** is the sam
18924 65 20 61 73 20 65 78 65 63 75 74 69 6e 67 20 48 e as executing H
18925 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f alt..*/.case OP_
18926 48 61 6c 74 3a 20 7b 0a 20 20 70 2d 3e 72 63 20 Halt: {. p->rc
18927 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e = pOp->p1;. p->
18928 70 63 20 3d 20 70 63 3b 0a 20 20 70 2d 3e 65 72 pc = pc;. p->er
18929 72 6f 72 41 63 74 69 6f 6e 20 3d 20 70 4f 70 2d rorAction = pOp-
1892a 3e 70 32 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e >p2;. if( pOp->
1892b 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 p4.z ){. sqli
1892c 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1892d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
1892e 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a s", pOp->p4.z);.
1892f 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
18930 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 e3VdbeHalt(p);.
18931 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
18932 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d ITE_BUSY || rc==
18933 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 SQLITE_OK );. i
18934 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 f( rc==SQLITE_BU
18935 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 SY ){. p->rc
18936 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 = rc = SQLITE_BU
18937 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 SY;. }else{.
18938 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53 51 rc = p->rc ? SQ
18939 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c LITE_ERROR : SQL
1893a 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 ITE_DONE;. }.
1893b 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1893c 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1893d 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a 20 Integer P1 P2 *
1893e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32 * *.**.** The 32
1893f 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c -bit integer val
18940 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65 6e ue P1 is written
18941 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
18942 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 2..*/.case OP_In
18943 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20 20 teger: {
18944 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
18945 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 ase */. pOut->f
18946 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
18947 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 4f pOut->u.i = pO
18948 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b 3b 0a p->p1;. break;.
18949 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e }../* Opcode: In
1894a 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a t64 * P2 * P4 *.
1894b 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f **.** P4 is a po
1894c 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62 69 inter to a 64-bi
1894d 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e t integer value.
1894e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 .** Write that v
1894f 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 alue into regist
18950 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f er P2..*/.case O
18951 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 20 20 P_Int64: {
18952 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
18953 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 release */. ass
18954 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 49 36 ert( pOp->p4.pI6
18955 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 4!=0 );. pOut->
18956 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
18957 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a . pOut->u.i = *
18958 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20 pOp->p4.pI64;.
18959 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1895a 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a ode: Real * P2 *
1895b 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 P4 *.**.** P4 i
1895c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1895d 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 64-bit floating
1895e 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a point value..**
1895f 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 Write that valu
18960 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 e into register
18961 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 P2..*/.case OP_R
18962 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 eal: {
18963 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
18964 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 72 FLOAT, out2-prer
18965 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 elease */. pOut
18966 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 ->flags = MEM_Re
18967 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 al;. assert( !s
18968 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f 70 qlite3IsNaN(*pOp
18969 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a 20 ->p4.pReal) );.
1896a 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d pOut->r = *pOp-
1896b 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 65 >p4.pReal;. bre
1896c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1896d 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 2a : String8 * P2 *
1896e 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70 P4 *.**.** P4 p
1896f 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74 oints to a nul t
18970 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 erminated UTF-8
18971 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 63 string. This opc
18972 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d ode is transform
18973 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f ed .** into an O
18974 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65 20 P_String before
18975 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20 66 it is executed f
18976 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 6d or the first tim
18977 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 e..*/.case OP_St
18978 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 20 ring8: {
18979 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 /* same as TK_S
1897a 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 72 TRING, out2-prer
1897b 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 elease */. asse
1897c 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 rt( pOp->p4.z!=0
1897d 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 );. pOp->opcod
1897e 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a 20 e = OP_String;.
1897f 20 70 4f 70 2d 3e 70 31 20 3d 20 73 74 72 6c 65 pOp->p1 = strle
18980 6e 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 0a 23 n(pOp->p4.z);..#
18981 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18982 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 65 IT_UTF16. if( e
18983 6e 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54 45 5f ncoding!=SQLITE_
18984 55 54 46 38 20 29 7b 0a 20 20 20 20 73 71 6c 69 UTF8 ){. sqli
18985 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
18986 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a (pOut, pOp->p4.z
18987 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 , -1, SQLITE_UTF
18988 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 8, SQLITE_STATIC
18989 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 );. if( SQLIT
1898a 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 E_OK!=sqlite3Vdb
1898b 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1898c 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67 29 20 pOut, encoding)
1898d 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 ) goto no_mem;.
1898e 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
1898f 21 3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d !=sqlite3VdbeMem
18990 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4f MakeWriteable(pO
18991 75 74 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ut) ) goto no_me
18992 6d 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 m;. pOut->zMa
18993 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f lloc = 0;. pO
18994 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d ut->flags |= MEM
18995 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 _Static;. pOu
18996 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d t->flags &= ~MEM
18997 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f _Dyn;. if( pO
18998 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 p->p4type==P4_DY
18999 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 NAMIC ){. s
1899a 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1899b 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 pOp->p4.z);.
1899c 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 }. pOp->p4ty
1899d 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b pe = P4_DYNAMIC;
1899e 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d . pOp->p4.z =
1899f 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f pOut->z;. pO
189a0 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b p->p1 = pOut->n;
189a1 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 . if( pOp->p1
189a2 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
189a3 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
189a4 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 ){. goto t
189a5 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 oo_big;. }.
189a6 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
189a7 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 20 BSIZE(pOut);.
189a8 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 23 65 6e 64 break;. }.#end
189a9 69 66 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 if. if( pOp->p1
189aa 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
189ab 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
189ac 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f ){. goto too
189ad 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 _big;. }. /* F
189ae 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 all through to t
189af 68 65 20 6e 65 78 74 20 63 61 73 65 2c 20 4f 50 he next case, OP
189b0 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20 20 0a _String */.}. .
189b1 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e /* Opcode: Strin
189b2 67 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a g P1 P2 * P4 *.*
189b3 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 *.** The string
189b4 76 61 6c 75 65 20 50 34 20 6f 66 20 6c 65 6e 67 value P4 of leng
189b5 74 68 20 50 31 20 28 62 79 74 65 73 29 20 69 73 th P1 (bytes) is
189b6 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
189b7 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 ter P2..*/.case
189b8 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 20 20 20 20 OP_String: {
189b9 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
189ba 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 erelease */. as
189bb 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 sert( pOp->p4.z!
189bc 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c =0 );. pOut->fl
189bd 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 ags = MEM_Str|ME
189be 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54 65 72 M_Static|MEM_Ter
189bf 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d 20 70 m;. pOut->z = p
189c0 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f 75 74 Op->p4.z;. pOut
189c1 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 ->n = pOp->p1;.
189c2 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 pOut->enc = enc
189c3 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f oding;. UPDATE_
189c4 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 MAX_BLOBSIZE(pOu
189c5 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
189c6 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 20 /* Opcode: Null
189c7 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a * P2 * * *.**.**
189c8 20 57 72 69 74 65 20 61 20 4e 55 4c 4c 20 69 6e Write a NULL in
189c9 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
189ca 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a */.case OP_Null:
189cb 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
189cc 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
189cd 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a */. break;.}...
189ce 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
189cf 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c MIT_BLOB_LITERAL
189d0 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 ./* Opcode: Blob
189d1 20 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a P1 P2 * P4.**.*
189d2 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 * P4 points to a
189d3 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50 31 blob of data P1
189d4 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74 bytes long. St
189d5 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62 ore this.** blob
189d6 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
189d7 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
189d8 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20 64 n is not coded d
189d9 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68 irectly.** by th
189da 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74 e compiler. Inst
189db 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 ead, the compile
189dc 72 20 6c 61 79 65 72 20 73 70 65 63 69 66 69 65 r layer specifie
189dd 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c s.** an OP_HexBl
189de 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68 20 ob opcode, with
189df 74 68 65 20 68 65 78 20 73 74 72 69 6e 67 20 72 the hex string r
189e0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
189e1 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73 20 .** the blob as
189e2 50 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 P4. This opcode
189e3 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 74 is transformed t
189e4 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 o an OP_Blob.**
189e5 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 the first time i
189e6 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a t is executed..*
189e7 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20 /.case OP_Blob:
189e8 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
189e9 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
189ea 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ase */. assert(
189eb 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49 pOp->p1 <= SQLI
189ec 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b TE_MAX_LENGTH );
189ed 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
189ee 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f mSetStr(pOut, pO
189ef 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31 p->p4.z, pOp->p1
189f0 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d , 0, 0);. pOut-
189f1 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b >enc = encoding;
189f2 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
189f3 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 OBSIZE(pOut);.
189f4 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
189f5 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 /* SQLITE_OMIT_B
189f6 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 2a 2f 0a 0a LOB_LITERAL */..
189f7 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 72 69 61 /* Opcode: Varia
189f8 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ble P1 P2 * * *.
189f9 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 **.** The value
189fa 6f 66 20 76 61 72 69 61 62 6c 65 20 50 31 20 69 of variable P1 i
189fb 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 72 s written into r
189fc 65 67 69 73 74 65 72 20 50 32 2e 20 41 20 76 61 egister P2. A va
189fd 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 61 6e 20 riable is.** an
189fe 75 6e 6b 6e 6f 77 6e 20 69 6e 20 74 68 65 20 6f unknown in the o
189ff 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 72 69 riginal SQL stri
18a00 6e 67 20 61 73 20 68 61 6e 64 65 64 20 74 6f 20 ng as handed to
18a01 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 sqlite3_compile(
18a02 29 2e 0a 2a 2a 20 41 6e 79 20 6f 63 63 75 72 72 )..** Any occurr
18a03 65 6e 63 65 20 6f 66 20 74 68 65 20 27 3f 27 20 ence of the '?'
18a04 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65 character in the
18a05 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 69 73 original SQL is
18a06 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 61 considered.** a
18a07 20 76 61 72 69 61 62 6c 65 2e 20 20 56 61 72 69 variable. Vari
18a08 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53 51 4c ables in the SQL
18a09 20 73 74 72 69 6e 67 20 61 72 65 20 6e 75 6d 62 string are numb
18a0a 65 72 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 0a er from left to.
18a0b 2a 2a 20 72 69 67 68 74 20 62 65 67 69 6e 6e 69 ** right beginni
18a0c 6e 67 20 77 69 74 68 20 31 2e 20 20 54 68 65 20 ng with 1. The
18a0d 76 61 6c 75 65 73 20 6f 66 20 76 61 72 69 61 62 values of variab
18a0e 6c 65 73 20 61 72 65 20 73 65 74 20 75 73 69 6e les are set usin
18a0f 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 g the.** sqlite3
18a10 5f 62 69 6e 64 28 29 20 41 50 49 2e 0a 2a 2f 0a _bind() API..*/.
18a11 63 61 73 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 case OP_Variable
18a12 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a : { /*
18a13 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
18a14 20 2a 2f 0a 20 20 69 6e 74 20 6a 20 3d 20 70 4f */. int j = pO
18a15 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 4d 65 6d p->p1 - 1;. Mem
18a16 20 2a 70 56 61 72 3b 0a 20 20 61 73 73 65 72 74 *pVar;. assert
18a17 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e ( j>=0 && j<p->n
18a18 56 61 72 20 29 3b 0a 0a 20 20 70 56 61 72 20 3d Var );.. pVar =
18a19 20 26 70 2d 3e 61 56 61 72 5b 6a 5d 3b 0a 20 20 &p->aVar[j];.
18a1a 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
18a1b 65 6d 54 6f 6f 42 69 67 28 70 56 61 72 29 20 29 emTooBig(pVar) )
18a1c 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 {. goto too_b
18a1d 69 67 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ig;. }. sqlite
18a1e 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 3VdbeMemShallowC
18a1f 6f 70 79 28 70 4f 75 74 2c 20 26 70 2d 3e 61 56 opy(pOut, &p->aV
18a20 61 72 5b 6a 5d 2c 20 4d 45 4d 5f 53 74 61 74 69 ar[j], MEM_Stati
18a21 63 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 c);. UPDATE_MAX
18a22 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b _BLOBSIZE(pOut);
18a23 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
18a24 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 50 31 20 Opcode: Move P1
18a25 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
18a26 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 73 20 Move the values
18a27 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 2e in register P1..
18a28 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 69 6e 74 P1+P3-1 over int
18a29 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 50 o.** registers P
18a2a 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 52 65 67 2..P2+P3-1. Reg
18a2b 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 31 isters P1..P1+P1
18a2c 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 74 20 68 -1 are.** left h
18a2d 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c 2e 20 20 olding a NULL.
18a2e 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66 It is an error f
18a2f 6f 72 20 72 65 67 69 73 74 65 72 20 72 61 6e 67 or register rang
18a30 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 2d es.** P1..P1+P3-
18a31 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b 50 33 2d 1 and P2..P2+P3-
18a32 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e 0a 2a 2f 1 to overlap..*/
18a33 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b .case OP_Move: {
18a34 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 . char *zMalloc
18a35 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d ;. int n = pOp-
18a36 3e 70 33 3b 0a 20 20 69 6e 74 20 70 31 20 3d 20 >p3;. int p1 =
18a37 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70 pOp->p1;. int p
18a38 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 2 = pOp->p2;. a
18a39 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 ssert( n>0 );.
18a3a 61 73 73 65 72 74 28 20 70 31 3e 30 20 29 3b 0a assert( p1>0 );.
18a3b 20 20 61 73 73 65 72 74 28 20 70 31 2b 6e 3c 70 assert( p1+n<p
18a3c 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 ->nMem );. pIn1
18a3d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 31 5d 3b = &p->aMem[p1];
18a3e 0a 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 20 . assert( p2>0
18a3f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 32 2b );. assert( p2+
18a40 6e 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 n<p->nMem );. p
18a41 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
18a42 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31 2];. assert( p1
18a43 2b 6e 3c 3d 70 32 20 7c 7c 20 70 32 2b 6e 3c 3d +n<=p2 || p2+n<=
18a44 70 31 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e p1 );. while( n
18a45 2d 2d 20 29 7b 0a 20 20 20 20 7a 4d 61 6c 6c 6f -- ){. zMallo
18a46 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f c = pOut->zMallo
18a47 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 c;. pOut->zMa
18a48 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 lloc = 0;. sq
18a49 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
18a4a 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 (pOut, pIn1);.
18a4b 20 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 pIn1->zMalloc
18a4c 3d 20 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 52 = zMalloc;. R
18a4d 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 EGISTER_TRACE(p2
18a4e 2b 2b 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 70 ++, pOut);. p
18a4f 49 6e 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b In1++;. pOut+
18a50 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a +;. }. break;.
18a51 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f }../* Opcode: Co
18a52 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a py P1 P2 * * *.*
18a53 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 *.** Make a copy
18a54 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 of register P1
18a55 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 into register P2
18a56 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 ..**.** This ins
18a57 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 truction makes a
18a58 20 64 65 65 70 20 63 6f 70 79 20 6f 66 20 74 68 deep copy of th
18a59 65 20 76 61 6c 75 65 2e 20 20 41 20 64 75 70 6c e value. A dupl
18a5a 69 63 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65 icate.** is made
18a5b 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f of any string o
18a5c 72 20 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e r blob constant.
18a5d 20 20 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43 See also OP_SC
18a5e 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f opy..*/.case OP_
18a5f 43 6f 70 79 3a 20 7b 0a 20 20 61 73 73 65 72 74 Copy: {. assert
18a60 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 ( pOp->p1>0 );.
18a61 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
18a62 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
18a63 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In1 = &p->aMem[p
18a64 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
18a65 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a t( pOp->p2>0 );.
18a66 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
18a67 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 2<=p->nMem );.
18a68 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
18a69 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 pOp->p2];. asse
18a6a 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 rt( pOut!=pIn1 )
18a6b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
18a6c 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f emShallowCopy(pO
18a6d 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 ut, pIn1, MEM_Ep
18a6e 68 65 6d 29 3b 0a 20 20 44 65 65 70 68 65 6d 65 hem);. Deepheme
18a6f 72 61 6c 69 7a 65 28 70 4f 75 74 29 3b 0a 20 20 ralize(pOut);.
18a70 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
18a71 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 Op->p2, pOut);.
18a72 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
18a73 63 6f 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50 code: SCopy P1 P
18a74 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 2 * * *.**.** Ma
18a75 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 ke a shallow cop
18a76 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 y of register P1
18a77 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
18a78 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 2..**.** This in
18a79 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 struction makes
18a7a 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f a shallow copy o
18a7b 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66 f the value. If
18a7c 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73 the value.** is
18a7d 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f a string or blo
18a7e 62 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79 b, then the copy
18a7f 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 is only a point
18a80 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69 er to the.** ori
18a81 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20 ginal and hence
18a82 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 if the original
18a83 63 68 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20 changes so will
18a84 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72 the copy..** Wor
18a85 73 65 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69 se, if the origi
18a86 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74 nal is deallocat
18a87 65 64 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63 ed, the copy bec
18a88 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a omes invalid..**
18a89 20 54 68 75 73 20 74 68 65 20 70 72 6f 67 72 61 Thus the progra
18a8a 6d 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 m must guarantee
18a8b 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e that the origin
18a8c 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e al will not chan
18a8d 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 ge.** during the
18a8e 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 lifetime of the
18a8f 20 63 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43 copy. Use OP_C
18a90 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f opy to make a co
18a91 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a mplete.** copy..
18a92 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79 */.case OP_SCopy
18a93 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f : {. assert( pO
18a94 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 p->p1>0 );. ass
18a95 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d ert( pOp->p1<=p-
18a96 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 20 >nMem );. pIn1
18a97 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
18a98 70 31 5d 3b 0a 20 20 52 45 47 49 53 54 45 52 5f p1];. REGISTER_
18a99 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 TRACE(pOp->p1, p
18a9a 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 In1);. assert(
18a9b 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 61 pOp->p2>0 );. a
18a9c 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d ssert( pOp->p2<=
18a9d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 p->nMem );. pOu
18a9e 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
18a9f 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p2];. assert(
18aa0 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20 pOut!=pIn1 );.
18aa1 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
18aa2 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c hallowCopy(pOut,
18aa3 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 6d pIn1, MEM_Ephem
18aa4 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 );. REGISTER_TR
18aa5 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 ACE(pOp->p2, pOu
18aa6 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
18aa7 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 6c /* Opcode: Resul
18aa8 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 2a tRow P1 P2 * * *
18aa9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 .**.** The regis
18aaa 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 68 20 ters P1 through
18aab 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e 20 P1+P2-1 contain
18aac 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 0a a single row of.
18aad 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68 69 73 ** results. This
18aae 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 opcode causes t
18aaf 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 he sqlite3_step(
18ab0 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69 6e ) call to termin
18ab1 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20 53 ate.** with an S
18ab2 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75 72 6e QLITE_ROW return
18ab3 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73 65 74 code and it set
18ab4 73 20 75 70 20 74 68 65 20 73 71 6c 69 74 65 33 s up the sqlite3
18ab5 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74 75 _stmt.** structu
18ab6 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 63 re to provide ac
18ab7 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f 70 20 cess to the top
18ab8 50 31 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 P1 values as the
18ab9 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e 0a result.** row..
18aba 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 75 6c */.case OP_Resul
18abb 74 52 6f 77 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 tRow: {. Mem *p
18abc 4d 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 Mem;. int i;.
18abd 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 73 43 assert( p->nResC
18abe 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70 32 20 29 olumn==pOp->p2 )
18abf 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
18ac0 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 >p1>0 );. asser
18ac1 74 28 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e t( pOp->p1+pOp->
18ac2 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 0a p2<=p->nMem );..
18ac3 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 /* Invalidate
18ac4 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75 all ephemeral cu
18ac5 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20 rsor row caches
18ac6 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 */. p->cacheCtr
18ac7 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20 = (p->cacheCtr
18ac8 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 + 2)|1;.. /* Ma
18ac9 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75 ke sure the resu
18aca 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 lts of the curre
18acb 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20 nt row are \000
18acc 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 terminated. **
18acd 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69 and have an assi
18ace 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20 gned type. The
18acf 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65 results are de-e
18ad0 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a phemeralized as.
18ad1 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66 ** as side eff
18ad2 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 70 4d 65 6d ect.. */. pMem
18ad3 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 = p->pResultSet
18ad4 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
18ad5 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b >p1];. for(i=0;
18ad6 20 69 3c 70 4f 70 2d 3e 70 32 3b 20 69 2b 2b 29 i<pOp->p2; i++)
18ad7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
18ad8 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 eMemNulTerminate
18ad9 28 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 20 20 (&pMem[i]);.
18ada 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 26 70 storeTypeInfo(&p
18adb 4d 65 6d 5b 69 5d 2c 20 65 6e 63 6f 64 69 6e 67 Mem[i], encoding
18adc 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f );. REGISTER_
18add 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 69 2c TRACE(pOp->p1+i,
18ade 20 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 7d 0a &pMem[i]);. }.
18adf 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
18ae0 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f Failed ) goto no
18ae1 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 _mem;.. /* Retu
18ae2 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 20 rn SQLITE_ROW.
18ae3 2a 2f 0a 20 20 70 2d 3e 6e 43 61 6c 6c 62 61 63 */. p->nCallbac
18ae4 6b 2b 2b 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 k++;. p->pc = p
18ae5 63 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 c + 1;. rc = SQ
18ae6 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f LITE_ROW;. goto
18ae7 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a vdbe_return;.}.
18ae8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 ./* Opcode: Conc
18ae9 61 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a at P1 P2 P3 * *.
18aea 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 74 65 **.** Add the te
18aeb 78 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 xt in register P
18aec 31 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 1 onto the end o
18aed 66 20 74 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a f the text in.**
18aee 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 register P2 and
18aef 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
18af0 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
18af1 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 74 ..** If either t
18af2 68 65 20 50 31 20 6f 72 20 50 32 20 74 65 78 74 he P1 or P2 text
18af3 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 are NULL then s
18af4 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e tore NULL in P3.
18af5 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 .**.** P3 = P2
18af6 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 || P1.**.** It
18af7 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 is illegal for P
18af8 31 20 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74 1 and P3 to be t
18af9 68 65 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 he same register
18afa 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 . Sometimes,.**
18afb 69 66 20 50 33 20 69 73 20 74 68 65 20 73 61 6d if P3 is the sam
18afc 65 20 72 65 67 69 73 74 65 72 20 61 73 20 50 32 e register as P2
18afd 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 , the implementa
18afe 74 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 tion is able.**
18aff 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 to avoid a memcp
18b00 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f y()..*/.case OP_
18b01 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 20 20 Concat: {
18b02 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
18b03 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 K_CONCAT, in1, i
18b04 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 36 n2, out3 */. i6
18b05 34 20 6e 42 79 74 65 3b 0a 0a 20 20 61 73 73 65 4 nByte;.. asse
18b06 72 74 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29 rt( pIn1!=pOut )
18b07 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 ;. if( (pIn1->f
18b08 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 lags | pIn2->fla
18b09 67 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 gs) & MEM_Null )
18b0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
18b0b 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 eMemSetNull(pOut
18b0c 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 );. break;.
18b0d 7d 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 }. ExpandBlob(p
18b0e 49 6e 31 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 In1);. Stringif
18b0f 79 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 y(pIn1, encoding
18b10 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 );. ExpandBlob(
18b11 70 49 6e 32 29 3b 0a 20 20 53 74 72 69 6e 67 69 pIn2);. Stringi
18b12 66 79 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e fy(pIn2, encodin
18b13 67 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 70 49 g);. nByte = pI
18b14 6e 31 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b n1->n + pIn2->n;
18b15 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d . if( nByte>db-
18b16 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
18b17 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
18b18 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
18b19 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 ;. }. MemSetTy
18b1a 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
18b1b 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c _Str);. if( sql
18b1c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
18b1d 70 4f 75 74 2c 20 6e 42 79 74 65 2b 32 2c 20 70 pOut, nByte+2, p
18b1e 4f 75 74 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 Out==pIn2) ){.
18b1f 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
18b20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 21 3d 70 }. if( pOut!=p
18b21 49 6e 32 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 In2 ){. memcp
18b22 79 28 70 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d y(pOut->z, pIn2-
18b23 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 >z, pIn2->n);.
18b24 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 }. memcpy(&pOut
18b25 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 ->z[pIn2->n], pI
18b26 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b n1->z, pIn1->n);
18b27 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65 . pOut->z[nByte
18b28 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a ] = 0;. pOut->z
18b29 5b 6e 42 79 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 [nByte+1] = 0;.
18b2a 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 pOut->flags |=
18b2b 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 MEM_Term;. pOut
18b2c 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20 20 70 ->n = nByte;. p
18b2d 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 Out->enc = encod
18b2e 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 ing;. UPDATE_MA
18b2f 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 X_BLOBSIZE(pOut)
18b30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
18b31 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20 Opcode: Add P1
18b32 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
18b33 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e Add the value in
18b34 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
18b35 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
18b36 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 ister P2.** and
18b37 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
18b38 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
18b39 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e .** If either in
18b3a 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 put is NULL, the
18b3b 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
18b3c 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d .*/./* Opcode: M
18b3d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50 33 ultiply P1 P2 P3
18b3e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 * *.**.**.** Mu
18b3f 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65 ltiply the value
18b40 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
18b41 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 by the value in
18b42 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 register P2.** a
18b43 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
18b44 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
18b45 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
18b46 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
18b47 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
18b48 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
18b49 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50 32 : Subtract P1 P2
18b4a 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75 P3 * *.**.** Su
18b4b 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 btract the value
18b4c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
18b4d 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69 from the value i
18b4e 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a n register P2.**
18b4f 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 and store the r
18b50 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 esult in registe
18b51 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 r P3..** If eith
18b52 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
18b53 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 , the result is
18b54 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f NULL..*/./* Opco
18b55 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50 32 de: Divide P1 P2
18b56 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69 P3 * *.**.** Di
18b57 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20 69 vide the value i
18b58 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79 n register P1 by
18b59 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
18b5a 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 gister P2.** and
18b5b 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
18b5c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
18b5d 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 . If the value
18b5e 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a in register P2.*
18b5f 2a 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 * is zero, then
18b60 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
18b61 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 LL..** If either
18b62 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
18b63 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
18b64 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
18b65 3a 20 52 65 6d 61 69 6e 64 65 72 20 50 31 20 50 : Remainder P1 P
18b66 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 2 P3 * *.**.** C
18b67 6f 6d 70 75 74 65 20 74 68 65 20 72 65 6d 61 69 ompute the remai
18b68 6e 64 65 72 20 61 66 74 65 72 20 69 6e 74 65 67 nder after integ
18b69 65 72 20 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 er division of t
18b6a 68 65 20 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 he value in.** r
18b6b 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74 68 egister P1 by th
18b6c 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
18b6d 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65 ter P2 and store
18b6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 50 the result in P
18b6f 33 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 3. .** If the va
18b70 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
18b71 50 32 20 69 73 20 7a 65 72 6f 20 74 68 65 20 72 P2 is zero the r
18b72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
18b73 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70 65 72 * If either oper
18b74 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 and is NULL, the
18b75 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
18b76 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a .*/.case OP_Add:
18b77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b78 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
18b79 5f 50 4c 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c _PLUS, in1, in2,
18b7a 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 out3 */.case OP
18b7b 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 20 20 _Subtract:
18b7c 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
18b7d 61 73 20 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 as TK_MINUS, in1
18b7e 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 , in2, out3 */.c
18b7f 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a ase OP_Multiply:
18b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18b81 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 same as TK_STAR
18b82 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 , in1, in2, out3
18b83 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 */.case OP_Divi
18b84 64 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 de:
18b85 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
18b86 5f 53 4c 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 _SLASH, in1, in2
18b87 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
18b88 50 5f 52 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 P_Remainder: {
18b89 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
18b8a 20 61 73 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c as TK_REM, in1,
18b8b 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 in2, out3 */.
18b8c 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 61 70 70 int flags;. app
18b8d 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 lyNumericAffinit
18b8e 79 28 70 49 6e 31 29 3b 0a 20 20 61 70 70 6c 79 y(pIn1);. apply
18b8f 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 NumericAffinity(
18b90 70 49 6e 32 29 3b 0a 20 20 66 6c 61 67 73 20 3d pIn2);. flags =
18b91 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 pIn1->flags | p
18b92 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 In2->flags;. if
18b93 28 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e ( (flags & MEM_N
18b94 75 6c 6c 29 21 3d 30 20 29 20 67 6f 74 6f 20 61 ull)!=0 ) goto a
18b95 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 rithmetic_result
18b96 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 _is_null;. if(
18b97 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 (pIn1->flags & p
18b98 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In2->flags & MEM
18b99 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 _Int)==MEM_Int )
18b9a 7b 0a 20 20 20 20 69 36 34 20 61 2c 20 62 3b 0a {. i64 a, b;.
18b9b 20 20 20 20 61 20 3d 20 70 49 6e 31 2d 3e 75 2e a = pIn1->u.
18b9c 69 3b 0a 20 20 20 20 62 20 3d 20 70 49 6e 32 2d i;. b = pIn2-
18b9d 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 74 63 68 >u.i;. switch
18b9e 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b ( pOp->opcode ){
18b9f 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 . case OP_A
18ba0 64 64 3a 20 20 20 20 20 20 20 20 20 62 20 2b 3d dd: b +=
18ba1 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b a; break;
18ba2 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 . case OP_S
18ba3 75 62 74 72 61 63 74 3a 20 20 20 20 62 20 2d 3d ubtract: b -=
18ba4 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b a; break;
18ba5 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 4d . case OP_M
18ba6 75 6c 74 69 70 6c 79 3a 20 20 20 20 62 20 2a 3d ultiply: b *=
18ba7 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b a; break;
18ba8 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 . case OP_D
18ba9 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 ivide: {.
18baa 20 69 66 28 20 61 3d 3d 30 20 29 20 67 6f 74 6f if( a==0 ) goto
18bab 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 arithmetic_resu
18bac 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 lt_is_null;.
18bad 20 20 20 20 2f 2a 20 44 69 76 69 64 69 6e 67 20 /* Dividing
18bae 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 the largest poss
18baf 69 62 6c 65 20 6e 65 67 61 74 69 76 65 20 36 34 ible negative 64
18bb0 2d 62 69 74 20 69 6e 74 65 67 65 72 20 28 31 3c -bit integer (1<
18bb1 3c 36 33 29 20 62 79 20 0a 20 20 20 20 20 20 20 <63) by .
18bb2 20 2a 2a 20 2d 31 20 72 65 74 75 72 6e 73 20 61 ** -1 returns a
18bb3 6e 20 69 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61 n integer too la
18bb4 72 67 65 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 rge to store in
18bb5 61 20 36 34 2d 62 69 74 20 64 61 74 61 2d 74 79 a 64-bit data-ty
18bb6 70 65 2e 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a pe. On. *
18bb7 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74 * some architect
18bb8 75 72 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 ures, the value
18bb9 6f 76 65 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c overflows to (1<
18bba 3c 36 33 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c <63). On others,
18bbb 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 53 49 . ** a SI
18bbc 47 46 50 45 20 69 73 20 69 73 73 75 65 64 2e 20 GFPE is issued.
18bbd 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 The following st
18bbe 61 74 65 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a atement normaliz
18bbf 65 73 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 es this.
18bc0 2a 2a 20 62 65 68 61 76 69 6f 72 20 73 6f 20 74 ** behavior so t
18bc1 68 61 74 20 61 6c 6c 20 61 72 63 68 69 74 65 63 hat all architec
18bc2 74 75 72 65 73 20 62 65 68 61 76 65 20 61 73 20 tures behave as
18bc3 69 66 20 69 6e 74 65 67 65 72 20 0a 20 20 20 20 if integer .
18bc4 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 ** overflow
18bc5 6f 63 63 75 72 72 65 64 2e 0a 20 20 20 20 20 20 occurred..
18bc6 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 */. if(
18bc7 20 61 3d 3d 2d 31 20 26 26 20 62 3d 3d 53 4d 41 a==-1 && b==SMA
18bc8 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 20 61 20 LLEST_INT64 ) a
18bc9 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 20 2f = 1;. b /
18bca 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65 = a;. bre
18bcb 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
18bcc 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
18bcd 20 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20 if( a==0 )
18bce 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f goto arithmetic_
18bcf 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a result_is_null;.
18bd0 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d 2d if( a==-
18bd1 31 20 29 20 61 20 3d 20 31 3b 0a 20 20 20 20 20 1 ) a = 1;.
18bd2 20 20 20 62 20 25 3d 20 61 3b 0a 20 20 20 20 20 b %= a;.
18bd3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
18bd4 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 }. }. pOut
18bd5 2d 3e 75 2e 69 20 3d 20 62 3b 0a 20 20 20 20 4d ->u.i = b;. M
18bd6 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
18bd7 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 ut, MEM_Int);.
18bd8 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 75 62 6c }else{. doubl
18bd9 65 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20 e a, b;. a =
18bda 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 sqlite3VdbeRealV
18bdb 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 20 alue(pIn1);.
18bdc 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 b = sqlite3VdbeR
18bdd 65 61 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a ealValue(pIn2);.
18bde 20 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d switch( pOp-
18bdf 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 >opcode ){.
18be0 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 case OP_Add:
18be1 20 20 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 b += a;
18be2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
18be3 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 case OP_Subtrac
18be4 74 3a 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 t: b -= a;
18be5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
18be6 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c case OP_Multipl
18be7 79 3a 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 y: b *= a;
18be8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
18be9 20 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a case OP_Divide:
18bea 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 {. if( a
18beb 3d 3d 30 2e 30 20 29 20 67 6f 74 6f 20 61 72 69 ==0.0 ) goto ari
18bec 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
18bed 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 s_null;.
18bee 62 20 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 b /= a;.
18bef 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
18bf0 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a default: {.
18bf1 20 20 20 20 20 20 20 20 69 36 34 20 69 61 20 3d i64 ia =
18bf2 20 28 69 36 34 29 61 3b 0a 20 20 20 20 20 20 20 (i64)a;.
18bf3 20 69 36 34 20 69 62 20 3d 20 28 69 36 34 29 62 i64 ib = (i64)b
18bf4 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 61 ;. if( ia
18bf5 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 ==0 ) goto arith
18bf6 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f metic_result_is_
18bf7 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 null;. if
18bf8 28 20 69 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20 ( ia==-1 ) ia =
18bf9 31 3b 0a 20 20 20 20 20 20 20 20 62 20 3d 20 69 1;. b = i
18bfa 62 20 25 20 69 61 3b 0a 20 20 20 20 20 20 20 20 b % ia;.
18bfb 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
18bfc 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
18bfd 69 74 65 33 49 73 4e 61 4e 28 62 29 20 29 7b 0a ite3IsNaN(b) ){.
18bfe 20 20 20 20 20 20 67 6f 74 6f 20 61 72 69 74 68 goto arith
18bff 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f metic_result_is_
18c00 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 null;. }.
18c01 70 4f 75 74 2d 3e 72 20 3d 20 62 3b 0a 20 20 20 pOut->r = b;.
18c02 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
18c03 70 4f 75 74 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b pOut, MEM_Real);
18c04 0a 20 20 20 20 69 66 28 20 28 66 6c 61 67 73 20 . if( (flags
18c05 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 & MEM_Real)==0 )
18c06 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
18c07 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 dbeIntegerAffini
18c08 74 79 28 70 4f 75 74 29 3b 0a 20 20 20 20 7d 0a ty(pOut);. }.
18c09 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 0a 61 72 }. break;..ar
18c0a 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f ithmetic_result_
18c0b 69 73 5f 6e 75 6c 6c 3a 0a 20 20 73 71 6c 69 74 is_null:. sqlit
18c0c 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c e3VdbeMemSetNull
18c0d 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b (pOut);. break;
18c0e 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 .}../* Opcode: C
18c0f 6f 6c 6c 53 65 71 20 2a 20 2a 20 50 34 0a 2a 2a ollSeq * * P4.**
18c10 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e .** P4 is a poin
18c11 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c 53 65 71 ter to a CollSeq
18c12 20 73 74 72 75 63 74 2e 20 49 66 20 74 68 65 20 struct. If the
18c13 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 20 75 next call to a u
18c14 73 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 ser function.**
18c15 6f 72 20 61 67 67 72 65 67 61 74 65 20 63 61 6c or aggregate cal
18c16 6c 73 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e ls sqlite3GetFun
18c17 63 43 6f 6c 6c 53 65 71 28 29 2c 20 74 68 69 73 cCollSeq(), this
18c18 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
18c19 6e 63 65 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 72 nce will.** be r
18c1a 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 69 73 eturned. This is
18c1b 20 75 73 65 64 20 62 79 20 74 68 65 20 62 75 69 used by the bui
18c1c 6c 74 2d 69 6e 20 6d 69 6e 28 29 2c 20 6d 61 78 lt-in min(), max
18c1d 28 29 20 61 6e 64 20 6e 75 6c 6c 69 66 28 29 0a () and nullif().
18c1e 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a ** functions..**
18c1f 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 .** The interfac
18c20 65 20 75 73 65 64 20 62 79 20 74 68 65 20 69 6d e used by the im
18c21 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
18c22 74 68 65 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e the aforemention
18c23 65 64 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 ed functions.**
18c24 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 to retrieve the
18c25 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
18c26 63 65 20 73 65 74 20 62 79 20 74 68 69 73 20 6f ce set by this o
18c27 70 63 6f 64 65 20 69 73 20 6e 6f 74 20 61 76 61 pcode is not ava
18c28 69 6c 61 62 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 ilable.** public
18c29 6c 79 2c 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72 ly, only to user
18c2a 20 66 75 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e functions defin
18c2b 65 64 20 69 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f ed in func.c..*/
18c2c 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 .case OP_CollSeq
18c2d 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f : {. assert( pO
18c2e 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f p->p4type==P4_CO
18c2f 4c 4c 53 45 51 20 29 3b 0a 20 20 62 72 65 61 6b LLSEQ );. break
18c30 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
18c31 46 75 6e 63 74 69 6f 6e 20 50 31 20 50 32 20 50 Function P1 P2 P
18c32 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 3 P4 P5.**.** In
18c33 76 6f 6b 65 20 61 20 75 73 65 72 20 66 75 6e 63 voke a user func
18c34 74 69 6f 6e 20 28 50 34 20 69 73 20 61 20 70 6f tion (P4 is a po
18c35 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 74 inter to a Funct
18c36 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 74 68 ion structure th
18c37 61 74 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 at.** defines th
18c38 65 20 66 75 6e 63 74 69 6f 6e 29 20 77 69 74 68 e function) with
18c39 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 74 61 P5 arguments ta
18c3a 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 ken from registe
18c3b 72 20 50 32 20 61 6e 64 0a 2a 2a 20 73 75 63 63 r P2 and.** succ
18c3c 65 73 73 6f 72 73 2e 20 20 54 68 65 20 72 65 73 essors. The res
18c3d 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 ult of the funct
18c3e 69 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e ion is stored in
18c3f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
18c40 20 52 65 67 69 73 74 65 72 20 50 33 20 6d 75 73 Register P3 mus
18c41 74 20 6e 6f 74 20 62 65 20 6f 6e 65 20 6f 66 20 t not be one of
18c42 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 70 the function inp
18c43 75 74 73 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 uts..**.** P1 is
18c44 20 61 20 33 32 2d 62 69 74 20 62 69 74 6d 61 73 a 32-bit bitmas
18c45 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65 k indicating whe
18c46 74 68 65 72 20 6f 72 20 6e 6f 74 20 65 61 63 68 ther or not each
18c47 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
18c48 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 .** function wa
18c49 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 6f 20 s determined to
18c4a 62 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 be constant at c
18c4b 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20 49 66 20 ompile time. If
18c4c 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 the first.** arg
18c4d 75 6d 65 6e 74 20 77 61 73 20 63 6f 6e 73 74 61 ument was consta
18c4e 6e 74 20 74 68 65 6e 20 62 69 74 20 30 20 6f 66 nt then bit 0 of
18c4f 20 50 31 20 69 73 20 73 65 74 2e 20 54 68 69 73 P1 is set. This
18c50 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 is used to dete
18c51 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 rmine.** whether
18c52 20 6d 65 74 61 20 64 61 74 61 20 61 73 73 6f 63 meta data assoc
18c53 69 61 74 65 64 20 77 69 74 68 20 61 20 75 73 65 iated with a use
18c54 72 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d r function argum
18c55 65 6e 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a ent using the.**
18c56 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 sqlite3_set_aux
18c57 64 61 74 61 28 29 20 41 50 49 20 6d 61 79 20 62 data() API may b
18c58 65 20 73 61 66 65 6c 79 20 72 65 74 61 69 6e 65 e safely retaine
18c59 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 d until the next
18c5a 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f .** invocation o
18c5b 66 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a f this opcode..*
18c5c 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 41 *.** See also: A
18c5d 67 67 53 74 65 70 20 61 6e 64 20 41 67 67 46 69 ggStep and AggFi
18c5e 6e 61 6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 nal.*/.case OP_F
18c5f 75 6e 63 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74 unction: {. int
18c60 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 3b i;. Mem *pArg;
18c61 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
18c62 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 xt ctx;. sqlite
18c63 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 3_value **apVal;
18c64 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e . int n = pOp->
18c65 70 35 3b 0a 0a 20 20 61 70 56 61 6c 20 3d 20 70 p5;.. apVal = p
18c66 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 ->apArg;. asser
18c67 74 28 20 61 70 56 61 6c 20 7c 7c 20 6e 3d 3d 30 t( apVal || n==0
18c68 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e );.. assert( n
18c69 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e ==0 || (pOp->p2>
18c6a 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 6e 3c 3d 0 && pOp->p2+n<=
18c6b 70 2d 3e 6e 4d 65 6d 29 20 29 3b 0a 20 20 61 73 p->nMem) );. as
18c6c 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f sert( pOp->p3<pO
18c6d 70 2d 3e 70 32 20 7c 7c 20 70 4f 70 2d 3e 70 33 p->p2 || pOp->p3
18c6e 3e 3d 70 4f 70 2d 3e 70 32 2b 6e 20 29 3b 0a 20 >=pOp->p2+n );.
18c6f 20 70 41 72 67 20 3d 20 26 70 2d 3e 61 4d 65 6d pArg = &p->aMem
18c70 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 [pOp->p2];. for
18c71 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 (i=0; i<n; i++,
18c72 70 41 72 67 2b 2b 29 7b 0a 20 20 20 20 61 70 56 pArg++){. apV
18c73 61 6c 5b 69 5d 20 3d 20 70 41 72 67 3b 0a 20 20 al[i] = pArg;.
18c74 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 storeTypeInfo(
18c75 70 41 72 67 2c 20 65 6e 63 6f 64 69 6e 67 29 3b pArg, encoding);
18c76 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 . REGISTER_TR
18c77 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 41 72 ACE(pOp->p2, pAr
18c78 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 g);. }.. asser
18c79 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
18c7a 50 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 70 4f P4_FUNCDEF || pO
18c7b 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 44 p->p4type==P4_VD
18c7c 42 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 28 20 BEFUNC );. if(
18c7d 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
18c7e 46 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 20 63 FUNCDEF ){. c
18c7f 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e tx.pFunc = pOp->
18c80 70 34 2e 70 46 75 6e 63 3b 0a 20 20 20 20 63 74 p4.pFunc;. ct
18c81 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 30 3b x.pVdbeFunc = 0;
18c82 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 74 . }else{. ct
18c83 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 x.pVdbeFunc = (V
18c84 64 62 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 dbeFunc*)pOp->p4
18c85 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 .pVdbeFunc;.
18c86 63 74 78 2e 70 46 75 6e 63 20 3d 20 63 74 78 2e ctx.pFunc = ctx.
18c87 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 pVdbeFunc->pFunc
18c88 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
18c89 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f pOp->p3>0 && pO
18c8a 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
18c8b 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 ;. pOut = &p->a
18c8c 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
18c8d 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 ctx.s.flags = ME
18c8e 4d 5f 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e M_Null;. ctx.s.
18c8f 64 62 20 3d 20 64 62 3b 0a 20 20 63 74 78 2e 73 db = db;. ctx.s
18c90 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 63 74 78 .xDel = 0;. ctx
18c91 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a .s.zMalloc = 0;.
18c92 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 . /* The output
18c93 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 cell may alread
18c94 79 20 68 61 76 65 20 61 20 62 75 66 66 65 72 20 y have a buffer
18c95 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a allocated. Move.
18c96 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 ** the pointer
18c97 20 74 6f 20 63 74 78 2e 73 20 73 6f 20 69 6e 20 to ctx.s so in
18c98 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 75 case the user-fu
18c99 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a 20 nction can use.
18c9a 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79 20 ** the already
18c9b 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
18c9c 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f instead of allo
18c9d 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 cating a new one
18c9e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
18c9f 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 63 74 78 VdbeMemMove(&ctx
18ca0 2e 73 2c 20 70 4f 75 74 29 3b 0a 20 20 4d 65 6d .s, pOut);. Mem
18ca1 53 65 74 54 79 70 65 46 6c 61 67 28 26 63 74 78 SetTypeFlag(&ctx
18ca2 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a .s, MEM_Null);..
18ca3 20 20 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 ctx.isError =
18ca4 30 3b 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75 0;. if( ctx.pFu
18ca5 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 nc->flags & SQLI
18ca6 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
18ca7 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
18ca8 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 pOp>p->aOp );.
18ca9 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 assert( pOp[-1
18caa 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c ].p4type==P4_COL
18cab 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 LSEQ );. asse
18cac 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f rt( pOp[-1].opco
18cad 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 de==OP_CollSeq )
18cae 3b 0a 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 ;. ctx.pColl
18caf 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f = pOp[-1].p4.pCo
18cb0 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 ll;. }. if( sq
18cb1 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
18cb2 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
18cb3 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
18cb4 20 28 2a 63 74 78 2e 70 46 75 6e 63 2d 3e 78 46 (*ctx.pFunc->xF
18cb5 75 6e 63 29 28 26 63 74 78 2c 20 6e 2c 20 61 70 unc)(&ctx, n, ap
18cb6 56 61 6c 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 Val);. if( sqli
18cb7 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
18cb8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
18cb9 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74 beMemRelease(&ct
18cba 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 x.s);. goto a
18cbb 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
18cbc 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 se;. }. if( db
18cbd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
18cbe 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 {. /* Even th
18cbf 6f 75 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 ough a malloc()
18cc0 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 has failed, the
18cc1 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
18cc2 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 f the. ** use
18cc3 72 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 r function may h
18cc4 61 76 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 ave called an sq
18cc5 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 lite3_result_XXX
18cc6 28 29 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 () function.
18cc7 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76 ** to return a v
18cc8 61 6c 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 alue. The follow
18cc9 69 6e 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 ing call release
18cca 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a s any resources.
18ccb 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 ** associate
18ccc 64 20 77 69 74 68 20 73 75 63 68 20 61 20 76 61 d with such a va
18ccd 6c 75 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 lue.. **.
18cce 2a 2a 20 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d ** Note: Maybe M
18ccf 65 6d 52 65 6c 65 61 73 65 28 29 20 73 68 6f 75 emRelease() shou
18cd0 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 ld be called if
18cd1 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
18cd2 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 ). ** fails a
18cd3 6c 73 6f 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 lso (the if(...)
18cd4 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 statement above
18cd5 29 2e 20 42 75 74 20 69 66 20 70 65 6f 70 6c 65 ). But if people
18cd6 20 61 72 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 are. ** misu
18cd7 73 69 6e 67 20 73 71 6c 69 74 65 2c 20 74 68 65 sing sqlite, the
18cd8 79 20 68 61 76 65 20 62 69 67 67 65 72 20 70 72 y have bigger pr
18cd9 6f 62 6c 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 oblems than a le
18cda 61 6b 65 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 aked value..
18cdb 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
18cdc 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74 beMemRelease(&ct
18cdd 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6e x.s);. goto n
18cde 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a o_mem;. }.. /*
18cdf 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69 61 72 If any auxiliar
18ce0 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f 6e 73 y data functions
18ce1 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 have been calle
18ce2 64 20 62 79 20 74 68 69 73 20 75 73 65 72 20 66 d by this user f
18ce3 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 69 6d unction,. ** im
18ce4 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c 20 74 mediately call t
18ce5 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f he destructor fo
18ce6 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74 69 63 r any non-static
18ce7 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a 20 20 values.. */.
18ce8 69 66 28 20 63 74 78 2e 70 56 64 62 65 46 75 6e if( ctx.pVdbeFun
18ce9 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 c ){. sqlite3
18cea 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 VdbeDeleteAuxDat
18ceb 61 28 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2c a(ctx.pVdbeFunc,
18cec 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70 pOp->p1);. p
18ced 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 Op->p4.pVdbeFunc
18cee 20 3d 20 63 74 78 2e 70 56 64 62 65 46 75 6e 63 = ctx.pVdbeFunc
18cef 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 ;. pOp->p4typ
18cf0 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43 3b e = P4_VDBEFUNC;
18cf1 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
18cf2 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 e function retur
18cf3 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 74 68 ned an error, th
18cf4 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e row an exception
18cf5 20 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e 69 73 */. if( ctx.is
18cf6 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c Error ){. sql
18cf7 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
18cf8 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
18cf9 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c %s", sqlite3_val
18cfa 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73 29 29 ue_text(&ctx.s))
18cfb 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 78 2e 69 ;. rc = ctx.i
18cfc 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f sError;. }.. /
18cfd 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c * Copy the resul
18cfe 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f t of the functio
18cff 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 n into register
18d00 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 P3 */. sqlite3V
18d01 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
18d02 67 28 26 63 74 78 2e 73 2c 20 65 6e 63 6f 64 69 g(&ctx.s, encodi
18d03 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 ng);. sqlite3Vd
18d04 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 beMemMove(pOut,
18d05 26 63 74 78 2e 73 29 3b 0a 20 20 69 66 28 20 73 &ctx.s);. if( s
18d06 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f qlite3VdbeMemToo
18d07 42 69 67 28 70 4f 75 74 29 20 29 7b 0a 20 20 20 Big(pOut) ){.
18d08 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
18d09 20 7d 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 }. REGISTER_TR
18d0a 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 ACE(pOp->p3, pOu
18d0b 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 t);. UPDATE_MAX
18d0c 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b _BLOBSIZE(pOut);
18d0d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
18d0e 4f 70 63 6f 64 65 3a 20 42 69 74 41 6e 64 20 50 Opcode: BitAnd P
18d0f 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
18d10 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77 * Take the bit-w
18d11 69 73 65 20 41 4e 44 20 6f 66 20 74 68 65 20 76 ise AND of the v
18d12 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 alues in registe
18d13 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a r P1 and P2 and.
18d14 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 ** store the res
18d15 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
18d16 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
18d17 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
18d18 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
18d19 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
18d1a 3a 20 42 69 74 4f 72 20 50 31 20 50 32 20 50 33 : BitOr P1 P2 P3
18d1b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 * *.**.** Take
18d1c 74 68 65 20 62 69 74 2d 77 69 73 65 20 4f 52 20 the bit-wise OR
18d1d 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
18d1e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 register P1 and
18d1f 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 P2 and.** store
18d20 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
18d21 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
18d22 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
18d23 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
18d24 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
18d25 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 4c * Opcode: ShiftL
18d26 65 66 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a eft P1 P2 P3 * *
18d27 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 .**.** Shift the
18d28 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 integer value i
18d29 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f n register P2 to
18d2a 20 74 68 65 20 6c 65 66 74 20 62 79 20 74 68 65 the left by the
18d2b 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 .** number of bi
18d2c 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ts specified by
18d2d 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 the integer in r
18d2e 65 67 69 73 65 72 20 50 31 2e 0a 2a 2a 20 53 74 egiser P1..** St
18d2f 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
18d30 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
18d31 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
18d32 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
18d33 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
18d34 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 /./* Opcode: Shi
18d35 66 74 52 69 67 68 74 20 50 31 20 50 32 20 50 33 ftRight P1 P2 P3
18d36 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 * *.**.** Shift
18d37 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c the integer val
18d38 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
18d39 32 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 62 2 to the right b
18d3a 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 y the.** number
18d3b 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 69 65 of bits specifie
18d3c 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 65 72 d by the integer
18d3d 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e in register P1.
18d3e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 .** Store the re
18d3f 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 sult in register
18d40 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 P3..** If eithe
18d41 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c r input is NULL,
18d42 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e the result is N
18d43 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
18d44 42 69 74 41 6e 64 3a 20 20 20 20 20 20 20 20 20 BitAnd:
18d45 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
18d46 61 73 20 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e as TK_BITAND, in
18d47 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
18d48 63 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 case OP_BitOr:
18d49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18d4a 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 /* same as TK_BI
18d4b 54 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f TOR, in1, in2, o
18d4c 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 ut3 */.case OP_S
18d4d 68 69 66 74 4c 65 66 74 3a 20 20 20 20 20 20 20 hiftLeft:
18d4e 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
18d4f 73 20 54 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31 s TK_LSHIFT, in1
18d50 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 , in2, out3 */.c
18d51 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 ase OP_ShiftRigh
18d52 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f t: { /
18d53 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 * same as TK_RSH
18d54 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f IFT, in1, in2, o
18d55 75 74 33 20 2a 2f 0a 20 20 69 36 34 20 61 2c 20 ut3 */. i64 a,
18d56 62 3b 0a 0a 20 20 69 66 28 20 28 70 49 6e 31 2d b;.. if( (pIn1-
18d57 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 >flags | pIn2->f
18d58 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c lags) & MEM_Null
18d59 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
18d5a 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f dbeMemSetNull(pO
18d5b 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a ut);. break;.
18d5c 20 20 7d 0a 20 20 61 20 3d 20 73 71 6c 69 74 65 }. a = sqlite
18d5d 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 3VdbeIntValue(pI
18d5e 6e 32 29 3b 0a 20 20 62 20 3d 20 73 71 6c 69 74 n2);. b = sqlit
18d5f 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 e3VdbeIntValue(p
18d60 49 6e 31 29 3b 0a 20 20 73 77 69 74 63 68 28 20 In1);. switch(
18d61 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 pOp->opcode ){.
18d62 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 41 6e case OP_BitAn
18d63 64 3a 20 20 20 20 20 20 61 20 26 3d 20 62 3b 20 d: a &= b;
18d64 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
18d65 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 ase OP_BitOr:
18d66 20 20 20 20 61 20 7c 3d 20 62 3b 20 20 20 20 20 a |= b;
18d67 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
18d68 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20 OP_ShiftLeft:
18d69 61 20 3c 3c 3d 20 62 3b 20 20 20 20 62 72 65 61 a <<= b; brea
18d6a 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 k;. default:
18d6b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 assert( pOp->op
18d6c 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69 code==OP_ShiftRi
18d6d 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ght );.
18d6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18d6f 61 20 3e 3e 3d 20 62 3b 20 20 20 20 62 72 65 61 a >>= b; brea
18d70 6b 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 k;. }. pOut->u
18d71 2e 69 20 3d 20 61 3b 0a 20 20 4d 65 6d 53 65 74 .i = a;. MemSet
18d72 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
18d73 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b EM_Int);. break
18d74 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
18d75 41 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a 20 AddImm P1 P2 *
18d76 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 * *.** .** Add t
18d77 68 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 74 he constant P2 t
18d78 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 o the value in r
18d79 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 54 egister P1..** T
18d7a 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 he result is alw
18d7b 61 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a ays an integer..
18d7c 2a 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 61 **.** To force a
18d7d 6e 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 62 ny register to b
18d7e 65 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a 75 e an integer, ju
18d7f 73 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 st add 0..*/.cas
18d80 65 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 e OP_AddImm: {
18d81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 /* in1
18d82 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 */. sqlite3Vdb
18d83 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
18d84 49 6e 31 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e In1);. pIn1->u.
18d85 69 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 i += pOp->p2;.
18d86 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
18d87 6f 64 65 3a 20 46 6f 72 63 65 49 6e 74 20 50 31 ode: ForceInt P1
18d88 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
18d89 20 43 6f 6e 76 65 72 74 20 76 61 6c 75 65 20 69 Convert value i
18d8a 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 6e n register P1 in
18d8b 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 to an integer.
18d8c 49 66 20 74 68 65 20 76 61 6c 75 65 20 0a 2a 2a If the value .**
18d8d 20 69 6e 20 50 31 20 69 73 20 6e 6f 74 20 6e 75 in P1 is not nu
18d8e 6d 65 72 69 63 20 28 6d 65 61 6e 69 6e 67 20 74 meric (meaning t
18d8f 68 61 74 20 69 73 20 69 73 20 61 20 4e 55 4c 4c hat is is a NULL
18d90 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74 68 61 or a string tha
18d91 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6c 6f t.** does not lo
18d92 6f 6b 20 6c 69 6b 65 20 61 6e 20 69 6e 74 65 67 ok like an integ
18d93 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70 er or floating p
18d94 6f 69 6e 74 20 6e 75 6d 62 65 72 29 20 74 68 65 oint number) the
18d95 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e n.** jump to P2.
18d96 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 If the value i
18d97 6e 20 50 31 20 69 73 20 6e 75 6d 65 72 69 63 20 n P1 is numeric
18d98 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 then.** convert
18d99 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 65 61 73 it into the leas
18d9a 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 t integer that i
18d9b 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
18d9c 72 20 65 71 75 61 6c 20 74 6f 20 69 74 73 0a 2a r equal to its.*
18d9d 2a 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 * current value
18d9e 69 66 20 50 33 3d 3d 30 2c 20 6f 72 20 74 6f 20 if P3==0, or to
18d9f 74 68 65 20 6c 65 61 73 74 20 69 6e 74 65 67 65 the least intege
18da0 72 20 74 68 61 74 20 69 73 20 73 74 72 69 63 74 r that is strict
18da1 6c 79 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 ly.** greater th
18da2 61 6e 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 an its current v
18da3 61 6c 75 65 20 69 66 20 50 33 3d 3d 31 2e 0a 2a alue if P3==1..*
18da4 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 72 63 65 49 /.case OP_ForceI
18da5 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 nt: {
18da6 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f /* jump, in1 */
18da7 0a 20 20 69 36 34 20 76 3b 0a 20 20 61 70 70 6c . i64 v;. appl
18da8 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 yAffinity(pIn1,
18da9 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
18daa 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 IC, encoding);.
18dab 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
18dac 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d s & (MEM_Int|MEM
18dad 5f 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20 20 _Real))==0 ){.
18dae 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
18daf 20 31 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 1;. break;.
18db0 20 7d 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 }. if( pIn1->f
18db1 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 lags & MEM_Int )
18db2 7b 0a 20 20 20 20 76 20 3d 20 70 49 6e 31 2d 3e {. v = pIn1->
18db3 75 2e 69 20 2b 20 28 70 4f 70 2d 3e 70 33 21 3d u.i + (pOp->p3!=
18db4 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0);. }else{.
18db5 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 assert( pIn1->f
18db6 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
18db7 29 3b 0a 20 20 20 20 76 20 3d 20 28 73 71 6c 69 );. v = (sqli
18db8 74 65 33 5f 69 6e 74 36 34 29 70 49 6e 31 2d 3e te3_int64)pIn1->
18db9 72 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 31 2d r;. if( pIn1-
18dba 3e 72 3e 28 64 6f 75 62 6c 65 29 76 20 29 20 76 >r>(double)v ) v
18dbb 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d ++;. if( pOp-
18dbc 3e 70 33 20 26 26 20 70 49 6e 31 2d 3e 72 3d 3d >p3 && pIn1->r==
18dbd 28 64 6f 75 62 6c 65 29 76 20 29 20 76 2b 2b 3b (double)v ) v++;
18dbe 0a 20 20 7d 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 . }. pIn1->u.i
18dbf 20 3d 20 76 3b 0a 20 20 4d 65 6d 53 65 74 54 79 = v;. MemSetTy
18dc0 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d peFlag(pIn1, MEM
18dc1 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a _Int);. break;.
18dc2 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 }../* Opcode: Mu
18dc3 73 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 stBeInt P1 P2 *
18dc4 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 * *.** .** Force
18dc5 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
18dc6 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 gister P1 to be
18dc7 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 an integer. If
18dc8 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 the value.** in
18dc9 50 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 P1 is not an int
18dca 65 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 eger and cannot
18dcb 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 be converted int
18dcc 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 o an integer.**
18dcd 77 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 without data los
18dce 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d s, then jump imm
18dcf 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 ediately to P2,
18dd0 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 or if P2==0.** r
18dd1 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d aise an SQLITE_M
18dd2 49 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f ISMATCH exceptio
18dd3 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 n..*/.case OP_Mu
18dd4 73 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 stBeInt: {
18dd5 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 /* jump, i
18dd6 6e 31 20 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66 n1 */. applyAff
18dd7 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 inity(pIn1, SQLI
18dd8 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 TE_AFF_NUMERIC,
18dd9 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 encoding);. if(
18dda 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
18ddb 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 MEM_Int)==0 ){.
18ddc 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d if( pOp->p2==
18ddd 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
18dde 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b SQLITE_MISMATCH;
18ddf 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
18de0 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
18de1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18de2 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
18de3 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 1;. }. }else
18de4 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 {. MemSetType
18de5 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 Flag(pIn1, MEM_I
18de6 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b nt);. }. break
18de7 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
18de8 52 65 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20 RealAffinity P1
18de9 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 * * * *.**.** If
18dea 20 72 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c register P1 hol
18deb 64 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f ds an integer co
18dec 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 72 65 nvert it to a re
18ded 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 al value..**.**
18dee 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 This opcode is u
18def 73 65 64 20 77 68 65 6e 20 65 78 74 72 61 63 74 sed when extract
18df0 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ing information
18df1 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 from a column th
18df2 61 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 at.** has REAL a
18df3 66 66 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63 ffinity. Such c
18df4 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 olumn values may
18df5 20 73 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 still be stored
18df6 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c as.** integers,
18df7 20 66 6f 72 20 73 70 61 63 65 20 65 66 66 69 63 for space effic
18df8 69 65 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72 iency, but after
18df9 20 65 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77 extraction we w
18dfa 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 ant them.** to h
18dfb 61 76 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 ave only a real
18dfc 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f value..*/.case O
18dfd 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 P_RealAffinity:
18dfe 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
18dff 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 /* in1 */. i
18e00 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
18e01 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
18e02 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
18e03 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d alify(pIn1);. }
18e04 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 . break;.}..#if
18e05 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
18e06 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a _CAST./* Opcode:
18e07 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a ToText P1 * * *
18e08 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 *.**.** Force t
18e09 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
18e0a 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 ster P1 to be te
18e0b 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 xt..** If the va
18e0c 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 lue is numeric,
18e0d 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 convert it to a
18e0e 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 string using the
18e0f 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f .** equivalent o
18e10 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f f printf(). Blo
18e11 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 b values are unc
18e12 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 hanged and.** ar
18e13 65 20 61 66 74 65 72 77 61 72 64 73 20 73 69 6d e afterwards sim
18e14 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 ply interpreted
18e15 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 as text..**.** A
18e16 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e NULL value is n
18e17 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 ot changed by th
18e18 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 is routine. It
18e19 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f remains NULL..*/
18e1a 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a .case OP_ToText:
18e1b 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
18e1c 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
18e1d 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a K_TO_TEXT, in1 *
18e1e 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c /. if( pIn1->fl
18e1f 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
18e20 20 62 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74 break;. assert
18e21 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f ( MEM_Str==(MEM_
18e22 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49 Blob>>3) );. pI
18e23 6e 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49 n1->flags |= (pI
18e24 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c n1->flags&MEM_Bl
18e25 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41 ob)>>3;. applyA
18e26 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 ffinity(pIn1, SQ
18e27 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 LITE_AFF_TEXT, e
18e28 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d ncoding);. rc =
18e29 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 ExpandBlob(pIn1
18e2a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e );. assert( pIn
18e2b 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 1->flags & MEM_S
18e2c 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 tr || db->malloc
18e2d 46 61 69 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31 Failed );. pIn1
18e2e 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
18e2f 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 _Int|MEM_Real|ME
18e30 4d 5f 42 6c 6f 62 29 3b 0a 20 20 55 50 44 41 54 M_Blob);. UPDAT
18e31 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
18e32 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d In1);. break;.}
18e33 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 42 ../* Opcode: ToB
18e34 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a lob P1 * * * *.*
18e35 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 *.** Force the v
18e36 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
18e37 20 50 31 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 P1 to be a BLOB
18e38 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 ..** If the valu
18e39 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f e is numeric, co
18e3a 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 nvert it to a st
18e3b 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20 53 ring first..** S
18e3c 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70 6c trings are simpl
18e3d 79 20 72 65 69 6e 74 65 72 70 72 65 74 65 64 20 y reinterpreted
18e3e 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e 6f as blobs with no
18e3f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74 68 change.** to th
18e40 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 e underlying dat
18e41 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 a..**.** A NULL
18e42 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 value is not cha
18e43 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 nged by this rou
18e44 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e tine. It remain
18e45 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 s NULL..*/.case
18e46 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20 20 OP_ToBlob: {
18e47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18e48 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 same as TK_TO_B
18e49 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 LOB, in1 */. if
18e4a 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 ( pIn1->flags &
18e4b 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b MEM_Null ) break
18e4c 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 ;. if( (pIn1->f
18e4d 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 lags & MEM_Blob)
18e4e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 70 70 6c 79 ==0 ){. apply
18e4f 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 Affinity(pIn1, S
18e50 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 QLITE_AFF_TEXT,
18e51 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 encoding);. a
18e52 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 ssert( pIn1->fla
18e53 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 gs & MEM_Str ||
18e54 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
18e55 20 29 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 );. }. MemSet
18e56 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d TypeFlag(pIn1, M
18e57 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 55 50 44 41 EM_Blob);. UPDA
18e58 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
18e59 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a pIn1);. break;.
18e5a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f }../* Opcode: To
18e5b 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20 2a Numeric P1 * * *
18e5c 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 *.**.** Force t
18e5d 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
18e5e 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e 75 ster P1 to be nu
18e5f 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 61 6e meric (either an
18e60 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 61 .** integer or a
18e61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 floating-point
18e62 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 74 number.).** If t
18e63 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 he value is text
18e64 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f or blob, try to
18e65 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
18e66 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 n using the.** e
18e67 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f quivalent of ato
18e68 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 6e i() or atof() an
18e69 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 d store 0 if no
18e6a 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 such conversion
18e6b 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 2e .** is possible.
18e6c 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 .**.** A NULL va
18e6d 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 lue is not chang
18e6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 ed by this routi
18e6f 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 ne. It remains
18e70 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 NULL..*/.case OP
18e71 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20 _ToNumeric: {
18e72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18e73 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
18e74 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f 0a NUMERIC, in1 */.
18e75 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
18e76 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d gs & (MEM_Null|M
18e77 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 EM_Int|MEM_Real)
18e78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 )==0 ){. sqli
18e79 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 te3VdbeMemNumeri
18e7a 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 fy(pIn1);. }.
18e7b 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
18e7c 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 /* SQLITE_OMIT_C
18e7d 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 AST */../* Opcod
18e7e 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20 e: ToInt P1 * *
18e7f 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 * *.**.** Force
18e80 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
18e81 69 73 74 65 72 20 50 31 20 62 65 20 61 6e 20 69 ister P1 be an i
18e82 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 nteger. If.** T
18e83 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 he value is curr
18e84 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d ently a real num
18e85 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 ber, drop its fr
18e86 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a actional part..*
18e87 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 * If the value i
18e88 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 s text or blob,
18e89 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 try to convert i
18e8a 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 t to an integer
18e8b 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 using the.** equ
18e8c 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 ivalent of atoi(
18e8d 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 ) and store 0 if
18e8e 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 no such convers
18e8f 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e ion is possible.
18e90 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 .**.** A NULL va
18e91 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 lue is not chang
18e92 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 ed by this routi
18e93 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 ne. It remains
18e94 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 NULL..*/.case OP
18e95 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 _ToInt: {
18e96 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
18e97 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c me as TK_TO_INT,
18e98 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 in1 */. if( (p
18e99 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
18e9a 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 _Null)==0 ){.
18e9b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
18e9c 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b ntegerify(pIn1);
18e9d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
18e9e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18e9f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 OMIT_CAST./* Opc
18ea0 6f 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 20 2a ode: ToReal P1 *
18ea1 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 * * *.**.** For
18ea2 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 ce the value in
18ea3 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 register P1 to b
18ea4 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 e a floating poi
18ea5 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49 66 nt number..** If
18ea6 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 The value is cu
18ea7 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65 67 rrently an integ
18ea8 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e 0a er, convert it..
18ea9 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ** If the value
18eaa 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c is text or blob,
18eab 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 try to convert
18eac 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 it to an integer
18ead 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 using the.** eq
18eae 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 uivalent of atoi
18eaf 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 2e 30 () and store 0.0
18eb0 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 if no such conv
18eb1 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 ersion is possib
18eb2 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c le..**.** A NULL
18eb3 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 value is not ch
18eb4 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f anged by this ro
18eb5 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 utine. It remai
18eb6 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 ns NULL..*/.case
18eb7 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 20 20 OP_ToReal: {
18eb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18eb9 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
18eba 52 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 REAL, in1 */. i
18ebb 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
18ebc 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 & MEM_Null)==0 )
18ebd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
18ebe 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 eMemRealify(pIn1
18ebf 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
18ec0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
18ec1 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a TE_OMIT_CAST */.
18ec2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 ./* Opcode: Lt P
18ec3 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
18ec4 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 .** Compare the
18ec5 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 values in regist
18ec6 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49 er P1 and P3. I
18ec7 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31 f reg(P3)<reg(P1
18ec8 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 ) then.** jump t
18ec9 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a o address P2. .
18eca 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
18ecb 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 ITE_JUMPIFNULL b
18ecc 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 it of P5 is set
18ecd 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50 and either reg(P
18ece 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 1) or.** reg(P3)
18ecf 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 is NULL then ta
18ed0 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 ke the jump. If
18ed1 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 the SQLITE_JUMP
18ed2 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 IFNULL .** bit i
18ed3 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c s clear then fal
18ed4 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72 l thru if either
18ed5 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
18ed6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
18ed7 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74 TE_AFF_MASK port
18ed8 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62 ion of P5 must b
18ed9 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 e an affinity ch
18eda 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c aracter -.** SQL
18edb 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 ITE_AFF_TEXT, SQ
18edc 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
18edd 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 , and so forth.
18ede 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 An attempt is ma
18edf 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 de .** to coerce
18ee0 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63 both inputs acc
18ee1 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61 ording to this a
18ee2 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74 ffinity before t
18ee3 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e he.** comparison
18ee4 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65 is made. If the
18ee5 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b SQLITE_AFF_MASK
18ee6 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e is 0x00, then n
18ee7 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69 umeric.** affini
18ee8 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65 ty is used. Note
18ee9 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 that the affini
18eea 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 ty conversions a
18eeb 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63 re stored.** bac
18eec 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 k into the input
18eed 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
18eee 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f d P3. So this o
18eef 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a pcode can cause.
18ef0 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68 ** persistent ch
18ef1 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65 anges to registe
18ef2 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a rs P1 and P3..**
18ef3 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e .** Once any con
18ef4 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61 versions have ta
18ef5 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e ken place, and n
18ef6 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20 either value is
18ef7 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 NULL, .** the va
18ef8 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65 lues are compare
18ef9 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 d. If both value
18efa 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e s are blobs then
18efb 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20 memcmp() is.**
18efc 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
18efd 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 e the results of
18efe 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e the comparison.
18eff 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 If both values
18f00 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68 .** are text, th
18f01 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 en the appropria
18f02 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e te collating fun
18f03 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 ction specified
18f04 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65 in.** P4 is use
18f05 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 d to do the comp
18f06 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69 arison. If P4 i
18f07 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 s not specified
18f08 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29 then.** memcmp()
18f09 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 is used to comp
18f0a 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e are text string.
18f0b 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 If both values
18f0c 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c are.** numeric,
18f0d 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20 then a numeric
18f0e 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73 comparison is us
18f0f 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76 ed. If the two v
18f10 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 alues.** are of
18f11 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c different types,
18f12 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72 then numbers ar
18f13 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 e considered les
18f14 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67 s than.** string
18f15 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72 s and strings ar
18f16 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 e considered les
18f17 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a s than blobs..**
18f18 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 .** If the SQLIT
18f19 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66 E_STOREP2 bit of
18f1a 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e P5 is set, then
18f1b 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 do not jump. I
18f1c 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65 nstead,.** store
18f1d 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c a boolean resul
18f1e 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20 t (either 0, or
18f1f 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72 1, or NULL) in r
18f20 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f egister P2..*/./
18f21 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 * Opcode: Ne P1
18f22 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
18f23 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 * This works jus
18f24 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 t like the Lt op
18f25 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 code except that
18f26 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b the jump is tak
18f27 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 en if.** the ope
18f28 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 rands in registe
18f29 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 rs P1 and P3 are
18f2a 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65 not equal. See
18f2b 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 the Lt opcode f
18f2c 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c or.** additional
18f2d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
18f2e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 71 20 50 ./* Opcode: Eq P
18f2f 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
18f30 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a .** This works j
18f31 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 ust like the Lt
18f32 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 opcode except th
18f33 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 at the jump is t
18f34 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f aken if.** the o
18f35 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 perands in regis
18f36 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 ters P1 and P3 a
18f37 72 65 20 65 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 re equal..** See
18f38 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 the Lt opcode f
18f39 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
18f3a 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a formation..*/./*
18f3b 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20 50 Opcode: Le P1 P
18f3c 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
18f3d 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 This works just
18f3e 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 like the Lt opc
18f3f 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 ode except that
18f40 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 the jump is take
18f41 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 n if.** the cont
18f42 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 ent of register
18f43 50 33 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 P3 is less than
18f44 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
18f45 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 content of.** re
18f46 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 gister P1. See
18f47 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f the Lt opcode fo
18f48 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
18f49 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 ormation..*/./*
18f4a 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 20 50 32 Opcode: Gt P1 P2
18f4b 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
18f4c 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 This works just
18f4d 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f like the Lt opco
18f4e 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 de except that t
18f4f 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
18f50 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 if.** the conte
18f51 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 nt of register P
18f52 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 3 is greater tha
18f53 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 n the content of
18f54 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e .** register P1.
18f55 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 See the Lt opc
18f56 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ode for addition
18f57 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
18f58 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 65 */./* Opcode: Ge
18f59 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
18f5a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 **.** This works
18f5b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c just like the L
18f5c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 t opcode except
18f5d 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 that the jump is
18f5e 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 taken if.** the
18f5f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
18f60 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74 ster P3 is great
18f61 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
18f62 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 to the content
18f63 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 of.** register P
18f64 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 1. See the Lt o
18f65 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 pcode for additi
18f66 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
18f67 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71 3a ..*/.case OP_Eq:
18f68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18f69 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 2c * same as TK_EQ,
18f6a 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
18f6b 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 */.case OP_Ne:
18f6c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18f6d 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20 6a same as TK_NE, j
18f6e 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f ump, in1, in3 */
18f6f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20 .case OP_Lt:
18f70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
18f71 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a 75 6d me as TK_LT, jum
18f72 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 p, in1, in3 */.c
18f73 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 20 20 ase OP_Le:
18f74 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
18f75 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d 70 2c as TK_LE, jump,
18f76 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 in1, in3 */.cas
18f77 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 20 20 e OP_Gt:
18f78 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
18f79 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c 20 69 s TK_GT, jump, i
18f7a 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 n1, in3 */.case
18f7b 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 20 20 OP_Ge: {
18f7c 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
18f7d 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 TK_GE, jump, in1
18f7e 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 66 , in3 */. int f
18f7f 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 73 3b lags;. int res;
18f80 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 . char affinity
18f81 3b 0a 0a 20 20 66 6c 61 67 73 20 3d 20 70 49 6e ;.. flags = pIn
18f82 31 2d 3e 66 6c 61 67 73 7c 70 49 6e 33 2d 3e 66 1->flags|pIn3->f
18f83 6c 61 67 73 3b 0a 0a 20 20 69 66 28 20 66 6c 61 lags;.. if( fla
18f84 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 gs&MEM_Null ){.
18f85 20 20 20 2f 2a 20 49 66 20 65 69 74 68 65 72 20 /* If either
18f86 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 operand is NULL
18f87 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 then the result
18f88 69 73 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a is always NULL..
18f89 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 ** The jump
18f8a 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 is taken if the
18f8b 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c SQLITE_JUMPIFNUL
18f8c 4c 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 L bit is set..
18f8d 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 */. if( pOp
18f8e 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 ->p5 & SQLITE_ST
18f8f 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20 70 OREP2 ){. p
18f90 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
18f91 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 4d Op->p2];. M
18f92 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
18f93 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 ut, MEM_Null);.
18f94 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 REGISTER_TR
18f95 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 ACE(pOp->p2, pOu
18f96 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 t);. }else if
18f97 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 ( pOp->p5 & SQLI
18f98 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29 7b TE_JUMPIFNULL ){
18f99 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
18f9a 3e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 >p2-1;. }.
18f9b 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 61 break;. }.. a
18f9c 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 ffinity = pOp->p
18f9d 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 5 & SQLITE_AFF_M
18f9e 41 53 4b 3b 0a 20 20 69 66 28 20 61 66 66 69 6e ASK;. if( affin
18f9f 69 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c 79 ity ){. apply
18fa0 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 61 Affinity(pIn1, a
18fa1 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e ffinity, encodin
18fa2 67 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 g);. applyAff
18fa3 69 6e 69 74 79 28 70 49 6e 33 2c 20 61 66 66 69 inity(pIn3, affi
18fa4 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b nity, encoding);
18fa5 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
18fa6 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
18fa7 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70 2d 3e COLLSEQ || pOp->
18fa8 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0a 20 p4.pColl==0 );.
18fa9 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 ExpandBlob(pIn1
18faa 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 );. ExpandBlob(
18fab 70 49 6e 33 29 3b 0a 20 20 72 65 73 20 3d 20 73 pIn3);. res = s
18fac 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 qlite3MemCompare
18fad 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 (pIn3, pIn1, pOp
18fae 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 73 ->p4.pColl);. s
18faf 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f witch( pOp->opco
18fb0 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f de ){. case O
18fb1 50 5f 45 71 3a 20 20 20 20 72 65 73 20 3d 20 72 P_Eq: res = r
18fb2 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 6b es==0; break
18fb3 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e 65 ;. case OP_Ne
18fb4 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 21 3d : res = res!=
18fb5 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 0; break;.
18fb6 20 20 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 case OP_Lt:
18fb7 20 72 65 73 20 3d 20 72 65 73 3c 30 3b 20 20 20 res = res<0;
18fb8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
18fb9 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 72 65 73 se OP_Le: res
18fba 20 3d 20 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 = res<=0; b
18fbb 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f reak;. case O
18fbc 50 5f 47 74 3a 20 20 20 20 72 65 73 20 3d 20 72 P_Gt: res = r
18fbd 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65 61 6b es>0; break
18fbe 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 ;. default:
18fbf 20 20 20 20 20 72 65 73 20 3d 20 72 65 73 3e 3d res = res>=
18fc0 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 0; break;.
18fc1 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 }.. if( pOp->p5
18fc2 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 & SQLITE_STOREP
18fc3 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 20 3d 20 2 ){. pOut =
18fc4 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
18fc5 5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 ];. MemSetTyp
18fc6 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
18fc7 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e Int);. pOut->
18fc8 75 2e 69 20 3d 20 72 65 73 3b 0a 20 20 20 20 52 u.i = res;. R
18fc9 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
18fca 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 p->p2, pOut);.
18fcb 7d 65 6c 73 65 20 69 66 28 20 72 65 73 20 29 7b }else if( res ){
18fcc 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
18fcd 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 2-1;. }. break
18fce 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
18fcf 50 65 72 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20 Permutation * *
18fd0 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 * P4 *.**.** Set
18fd1 20 74 68 65 20 70 65 72 6d 75 61 74 69 6f 6e 20 the permuation
18fd2 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43 used by the OP_C
18fd3 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20 ompare operator
18fd4 74 6f 20 62 65 20 74 68 65 20 61 72 72 61 79 0a to be the array.
18fd5 2a 2a 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69 ** of integers i
18fd6 6e 20 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 n P4..**.** The
18fd7 70 65 72 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f permutation is o
18fd8 6e 6c 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 nly valid until
18fd9 74 68 65 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d the next OP_Perm
18fda 75 74 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 utation, OP_Comp
18fdb 61 72 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c are,.** OP_Halt,
18fdc 20 6f 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 or OP_ResultRow
18fdd 2e 20 20 54 79 70 69 63 61 6c 6c 79 20 74 68 65 . Typically the
18fde 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 OP_Permutation
18fdf 73 68 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 should occur.**
18fe0 69 6d 6d 65 64 69 61 74 65 6c 79 20 70 72 69 6f immediately prio
18fe1 72 20 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 r to the OP_Comp
18fe2 61 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f are..*/.case OP_
18fe3 50 65 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 Permutation: {.
18fe4 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
18fe5 74 79 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 type==P4_INTARRA
18fe6 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 Y );. assert( p
18fe7 4f 70 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 Op->p4.ai );. a
18fe8 50 65 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 Permute = pOp->p
18fe9 34 2e 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 4.ai;. break;.}
18fea 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d ../* Opcode: Com
18feb 70 61 72 65 20 50 31 20 50 32 20 50 33 20 50 34 pare P1 P2 P3 P4
18fec 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 *.**.** Compare
18fed 20 74 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72 to vectors of r
18fee 65 67 69 73 74 65 72 73 20 69 6e 20 72 65 67 28 egisters in reg(
18fef 50 31 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 P1)..reg(P1+P3-1
18ff0 29 20 28 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f ) (all this.** o
18ff1 6e 65 20 22 41 22 29 20 61 6e 64 20 69 6e 20 72 ne "A") and in r
18ff2 65 67 28 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 eg(P2)..reg(P2+P
18ff3 33 2d 31 29 20 28 22 42 22 29 2e 20 20 53 61 76 3-1) ("B"). Sav
18ff4 65 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a e the result of.
18ff5 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f ** the compariso
18ff6 6e 20 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 n for use by the
18ff7 20 6e 65 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e next OP_Jump in
18ff8 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 struct..**.** P4
18ff9 20 69 73 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 is a KeyInfo st
18ffa 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 66 ructure that def
18ffb 69 6e 65 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 ines collating s
18ffc 65 71 75 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 equences and sor
18ffd 74 0a 2a 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 t.** orders for
18ffe 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 the comparison.
18fff 20 54 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e The permutation
19000 20 61 70 70 6c 69 65 73 20 74 6f 20 72 65 67 69 applies to regi
19001 73 74 65 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 sters.** only.
19002 54 68 65 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d The KeyInfo elem
19003 65 6e 74 73 20 61 72 65 20 75 73 65 64 20 73 65 ents are used se
19004 71 75 65 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a quentially..**.*
19005 2a 20 54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e * The comparison
19006 20 69 73 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 is a sort compa
19007 72 69 73 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 rison, so NULLs
19008 63 6f 6d 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a compare equal,.*
19009 2a 20 4e 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 * NULLs are less
1900a 20 74 68 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e than numbers, n
1900b 75 6d 62 65 72 73 20 61 72 65 20 6c 65 73 73 20 umbers are less
1900c 74 68 61 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a than strings,.**
1900d 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 and strings are
1900e 20 6c 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 less than blobs
1900f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d ..*/.case OP_Com
19010 70 61 72 65 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 pare: {. int n
19011 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 = pOp->p3;. int
19012 20 69 2c 20 70 31 2c 20 70 32 3b 0a 20 20 63 6f i, p1, p2;. co
19013 6e 73 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 nst KeyInfo *pKe
19014 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e yInfo = pOp->p4.
19015 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 pKeyInfo;. asse
19016 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 rt( n>0 );. ass
19017 65 72 74 28 20 70 4b 65 79 49 6e 66 6f 21 3d 30 ert( pKeyInfo!=0
19018 20 29 3b 0a 20 20 70 31 20 3d 20 70 4f 70 2d 3e );. p1 = pOp->
19019 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31 p1;. assert( p1
1901a 3e 30 20 26 26 20 70 31 2b 6e 2d 31 3c 70 2d 3e >0 && p1+n-1<p->
1901b 6e 4d 65 6d 20 29 3b 0a 20 20 70 32 20 3d 20 70 nMem );. p2 = p
1901c 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 Op->p2;. assert
1901d 28 20 70 32 3e 30 20 26 26 20 70 32 2b 6e 2d 31 ( p2>0 && p2+n-1
1901e 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 66 6f <p->nMem );. fo
1901f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 r(i=0; i<n; i++)
19020 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 {. int idx =
19021 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d aPermute ? aPerm
19022 75 74 65 5b 69 5d 20 3a 20 69 3b 0a 20 20 20 20 ute[i] : i;.
19023 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 CollSeq *pColl;
19024 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 /* Collating
19025 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 20 sequence to use
19026 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a on this term */.
19027 20 20 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 int bRev;
19028 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 /* True f
19029 6f 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f or DESCENDING so
1902a 72 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 rt order */.
1902b 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1902c 31 2b 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 1+idx, &p->aMem[
1902d 70 31 2b 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 p1+idx]);. RE
1902e 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b GISTER_TRACE(p2+
1902f 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 idx, &p->aMem[p2
19030 2b 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65 +idx]);. asse
19031 72 74 28 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e rt( i<pKeyInfo->
19032 6e 46 69 65 6c 64 20 29 3b 0a 20 20 20 20 70 43 nField );. pC
19033 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e oll = pKeyInfo->
19034 61 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 62 52 aColl[i];. bR
19035 65 76 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 ev = pKeyInfo->a
19036 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 SortOrder[i];.
19037 20 20 69 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c iCompare = sql
19038 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 ite3MemCompare(&
19039 70 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d 2c p->aMem[p1+idx],
1903a 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78 &p->aMem[p2+idx
1903b 5d 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 ], pColl);. i
1903c 66 28 20 69 43 6f 6d 70 61 72 65 20 29 7b 0a 20 f( iCompare ){.
1903d 20 20 20 20 20 69 66 28 20 62 52 65 76 20 29 20 if( bRev )
1903e 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d iCompare = -iCom
1903f 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61 pare;. brea
19040 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 k;. }. }. a
19041 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 Permute = 0;. b
19042 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
19043 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50 de: Jump P1 P2 P
19044 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
19045 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 to the instruct
19046 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 ion at address P
19047 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 1, P2, or P3 dep
19048 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
19049 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 r.** in the most
1904a 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 recent OP_Compa
1904b 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 re instruction t
1904c 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 he P1 vector was
1904d 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 less than.** eq
1904e 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
1904f 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76 er than the P2 v
19050 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76 ector, respectiv
19051 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ely..*/.case OP_
19052 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20 Jump: {
19053 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 /* jump */.
19054 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 if( iCompare<0
19055 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
19056 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 >p1 - 1;. }else
19057 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 if( iCompare==0
19058 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
19059 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 ->p2 - 1;. }els
1905a 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d e{. pc = pOp-
1905b 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p3 - 1;. }. b
1905c 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1905d 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33 de: And P1 P2 P3
1905e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 * *.**.** Take
1905f 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 the logical AND
19060 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
19061 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
19062 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 d P2 and.** writ
19063 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 e the result int
19064 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a o register P3..*
19065 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 *.** If either P
19066 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61 1 or P2 is 0 (fa
19067 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 lse) then the re
19068 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69 sult is 0 even i
19069 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 f.** the other i
1906a 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 nput is NULL. A
1906b 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f NULL and true o
1906c 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 r two NULLs give
1906d 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 .** a NULL outpu
1906e 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a t..*/./* Opcode:
1906f 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a Or P1 P2 P3 * *
19070 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 .**.** Take the
19071 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 logical OR of th
19072 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
19073 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 ster P1 and P2 a
19074 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 nd.** store the
19075 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74 answer in regist
19076 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P3..**.** If
19077 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 either P1 or P2
19078 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 is nonzero (true
19079 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c ) then the resul
1907a 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a t is 1 (true).**
1907b 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68 even if the oth
1907c 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1907d 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 . A NULL and fa
1907e 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 lse or two NULLs
1907f 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 .** give a NULL
19080 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 output..*/.case
19081 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 OP_And:
19082 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
19083 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 TK_AND, in1, in2
19084 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
19085 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20 P_Or: {
19086 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
19087 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 K_OR, in1, in2,
19088 6f 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 76 31 out3 */. int v1
19089 2c 20 76 32 3b 20 20 20 20 2f 2a 20 30 3d 3d 46 , v2; /* 0==F
1908a 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1908b 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1908c 4c 20 2a 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 L */.. if( pIn1
1908d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1908e 6c 6c 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 32 ll ){. v1 = 2
1908f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 ;. }else{. v
19090 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 1 = sqlite3VdbeI
19091 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 ntValue(pIn1)!=0
19092 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 ;. }. if( pIn2
19093 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
19094 6c 6c 20 29 7b 0a 20 20 20 20 76 32 20 3d 20 32 ll ){. v2 = 2
19095 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 ;. }else{. v
19096 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 2 = sqlite3VdbeI
19097 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d 30 ntValue(pIn2)!=0
19098 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d ;. }. if( pOp-
19099 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 >opcode==OP_And
1909a 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f ){. static co
1909b 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1909c 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 r and_logic[] =
1909d 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c { 0, 0, 0, 0, 1,
1909e 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20 2, 0, 2, 2 };.
1909f 20 20 20 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 v1 = and_logi
190a0 63 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 65 c[v1*3+v2];. }e
190a1 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 lse{. static
190a2 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
190a3 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d har or_logic[] =
190a4 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 { 0, 1, 2, 1, 1
190a5 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a , 1, 2, 1, 2 };.
190a6 20 20 20 20 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 v1 = or_logi
190a7 63 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 0a c[v1*3+v2];. }.
190a8 20 20 69 66 28 20 76 31 3d 3d 32 20 29 7b 0a 20 if( v1==2 ){.
190a9 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
190aa 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c g(pOut, MEM_Null
190ab 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
190ac 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 31 3b 0a pOut->u.i = v1;.
190ad 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
190ae 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 ag(pOut, MEM_Int
190af 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
190b0 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f }../* Opcode: No
190b1 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a t P1 * * * *.**.
190b2 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 ** Interpret the
190b3 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
190b4 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f 6c 65 er P1 as a boole
190b5 61 6e 20 76 61 6c 75 65 2e 20 20 52 65 70 6c 61 an value. Repla
190b6 63 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 69 74 ce it.** with it
190b7 73 20 63 6f 6d 70 6c 65 6d 65 6e 74 2e 20 20 49 s complement. I
190b8 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 f the value in r
190b9 65 67 69 73 74 65 72 20 50 31 20 69 73 20 4e 55 egister P1 is NU
190ba 4c 4c 20 69 74 73 20 76 61 6c 75 65 0a 2a 2a 20 LL its value.**
190bb 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f is unchanged..*/
190bc 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 .case OP_Not: {
190bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
190be 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 * same as TK_NOT
190bf 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 , in1 */. if( p
190c0 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
190c1 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 20 20 _Null ) break;
190c2 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 74 6f /* Do nothing to
190c3 20 4e 55 4c 4c 73 20 2a 2f 0a 20 20 73 71 6c 69 NULLs */. sqli
190c4 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
190c5 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 49 rify(pIn1);. pI
190c6 6e 31 2d 3e 75 2e 69 20 3d 20 21 70 49 6e 31 2d n1->u.i = !pIn1-
190c7 3e 75 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 20 >u.i;. assert(
190c8 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pIn1->flags&MEM_
190c9 49 6e 74 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a Int );. break;.
190ca 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 }../* Opcode: Bi
190cb 74 4e 6f 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a tNot P1 * * * *.
190cc 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 **.** Interpret
190cd 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
190ce 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e egister P1 as an
190cf 20 69 6e 74 65 67 65 72 2e 20 20 52 65 70 6c 61 integer. Repla
190d0 63 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 69 74 ce it.** with it
190d1 73 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e s ones-complemen
190d2 74 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 t. If the value
190d3 20 69 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 4e is originally N
190d4 55 4c 4c 2c 20 6c 65 61 76 65 0a 2a 2a 20 69 74 ULL, leave.** it
190d5 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 63 unchanged..*/.c
190d6 61 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b ase OP_BitNot: {
190d7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
190d8 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f same as TK_BITNO
190d9 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 T, in1 */. if(
190da 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
190db 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 20 M_Null ) break;
190dc 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 74 /* Do nothing t
190dd 6f 20 4e 55 4c 4c 73 20 2a 2f 0a 20 20 73 71 6c o NULLs */. sql
190de 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
190df 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 erify(pIn1);. p
190e0 49 6e 31 2d 3e 75 2e 69 20 3d 20 7e 70 49 6e 31 In1->u.i = ~pIn1
190e1 2d 3e 75 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 ->u.i;. assert(
190e2 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d pIn1->flags&MEM
190e3 5f 49 6e 74 20 29 3b 0a 20 20 62 72 65 61 6b 3b _Int );. break;
190e4 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
190e5 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a f P1 P2 P3 * *.*
190e6 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 *.** Jump to P2
190e7 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 if the value in
190e8 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 74 register P1 is t
190e9 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 rue. The value
190ea 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 is.** is conside
190eb 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 69 red true if it i
190ec 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e 6f s numeric and no
190ed 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 20 n-zero. If the
190ee 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69 value.** in P1 i
190ef 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 s NULL then take
190f0 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 20 the jump if P3
190f1 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 4f is true..*/./* O
190f2 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31 20 pcode: IfNot P1
190f3 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
190f4 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 Jump to P2 if th
190f5 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
190f6 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65 2e ter P1 is False.
190f7 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a 2a The value is.*
190f8 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 * is considered
190f9 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20 61 true if it has a
190fa 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 6f numeric value o
190fb 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 20 f zero. If the
190fc 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69 value.** in P1 i
190fd 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 s NULL then take
190fe 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 20 the jump if P3
190ff 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73 65 is true..*/.case
19100 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20 20 OP_If:
19101 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
19102 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50 5f in1 */.case OP_
19103 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 IfNot: {
19104 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 /* jump, in1
19105 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 20 20 69 */. int c;. i
19106 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
19107 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
19108 20 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 c = pOp->p3;.
19109 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51 }else{.#ifdef SQ
1910a 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
1910b 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 63 20 3d NG_POINT. c =
1910c 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
1910d 61 6c 75 65 28 70 49 6e 31 29 3b 0a 23 65 6c 73 alue(pIn1);.#els
1910e 65 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 e. c = sqlite
1910f 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 3VdbeRealValue(p
19110 49 6e 31 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 69 In1)!=0.0;.#endi
19111 66 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f f. if( pOp->o
19112 70 63 6f 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20 pcode==OP_IfNot
19113 29 20 63 20 3d 20 21 63 3b 0a 20 20 7d 0a 20 20 ) c = !c;. }.
19114 69 66 28 20 63 20 29 7b 0a 20 20 20 20 70 63 20 if( c ){. pc
19115 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d = pOp->p2-1;. }
19116 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
19117 4f 70 63 6f 64 65 3a 20 49 73 4e 75 6c 6c 20 50 Opcode: IsNull P
19118 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
19119 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 * Jump to P2 if
1911a 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1911b 69 73 74 65 72 20 50 31 20 69 73 20 4e 55 4c 4c ister P1 is NULL
1911c 2e 20 20 49 66 20 50 33 20 69 73 20 67 72 65 61 . If P3 is grea
1911d 74 65 72 0a 2a 2a 20 74 68 61 6e 20 7a 65 72 6f ter.** than zero
1911e 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 61 6c 6c , then check all
1911f 20 76 61 6c 75 65 73 20 72 65 67 28 50 31 29 2c values reg(P1),
19120 20 72 65 67 28 50 31 2b 31 29 2c 20 0a 2a 2a 20 reg(P1+1), .**
19121 72 65 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2c 20 reg(P1+2), ...,
19122 72 65 67 28 50 31 2b 50 33 2d 31 29 2e 0a 2a 2f reg(P1+P3-1)..*/
19123 0a 63 61 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a .case OP_IsNull:
19124 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
19125 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 same as TK_ISNU
19126 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f LL, jump, in1 */
19127 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e . int n = pOp->
19128 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f p3;. assert( pO
19129 70 2d 3e 70 33 3d 3d 30 20 7c 7c 20 70 4f 70 2d p->p3==0 || pOp-
1912a 3e 70 31 3e 30 20 29 3b 0a 20 20 64 6f 7b 0a 20 >p1>0 );. do{.
1912b 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c if( (pIn1->fl
1912c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 ags & MEM_Null)!
1912d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d =0 ){. pc =
1912e 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1912f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
19130 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 7d . pIn1++;. }
19131 77 68 69 6c 65 28 20 2d 2d 6e 20 3e 20 30 20 29 while( --n > 0 )
19132 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
19133 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c Opcode: NotNull
19134 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
19135 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 ** Jump to P2 if
19136 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
19137 67 69 73 74 65 72 20 50 31 20 69 73 20 6e 6f 74 gister P1 is not
19138 20 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65 NULL. .*/.case
19139 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 OP_NotNull: {
1913a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1913b 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c e as TK_NOTNULL,
1913c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 jump, in1 */.
1913d 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1913e 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 & MEM_Null)==0
1913f 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
19140 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
19141 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
19142 64 65 3a 20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e de: SetNumColumn
19143 73 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a s * P2 * * *.**.
19144 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 73 ** This opcode s
19145 65 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ets the number o
19146 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68 f columns for th
19147 65 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 e cursor opened
19148 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 by the.** follow
19149 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ing instruction
1914a 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 to P2..**.** An
1914b 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 OP_SetNumColumns
1914c 20 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c 20 is only useful
1914d 69 66 20 69 74 20 6f 63 63 75 72 73 20 69 6d 6d if it occurs imm
1914e 65 64 69 61 74 65 6c 79 20 62 65 66 6f 72 65 20 ediately before
1914f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 .** one of the f
19150 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 65 73 ollowing opcodes
19151 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4f 70 65 6e :.**.** Open
19152 52 65 61 64 0a 2a 2a 20 20 20 20 20 4f 70 65 6e Read.** Open
19153 57 72 69 74 65 0a 2a 2a 20 20 20 20 20 4f 70 65 Write.** Ope
19154 6e 50 73 65 75 64 6f 0a 2a 2a 0a 2a 2a 20 49 66 nPseudo.**.** If
19155 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f the OP_Column o
19156 70 63 6f 64 65 20 69 73 20 74 6f 20 62 65 20 65 pcode is to be e
19157 78 65 63 75 74 65 64 20 6f 6e 20 61 20 63 75 72 xecuted on a cur
19158 73 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 sor, then.** thi
19159 73 20 6f 70 63 6f 64 65 20 6d 75 73 74 20 62 65 s opcode must be
1915a 20 70 72 65 73 65 6e 74 20 69 6d 6d 65 64 69 61 present immedia
1915b 74 65 6c 79 20 62 65 66 6f 72 65 20 74 68 65 20 tely before the
1915c 6f 70 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 6f opcode that.** o
1915d 70 65 6e 73 20 74 68 65 20 63 75 72 73 6f 72 2e pens the cursor.
1915e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 4e .*/.case OP_SetN
1915f 75 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20 62 umColumns: {. b
19160 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
19161 64 65 3a 20 43 6f 6c 75 6d 6e 20 50 31 20 50 32 de: Column P1 P2
19162 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 P3 P4 *.**.** I
19163 6e 74 65 72 70 72 65 74 20 74 68 65 20 64 61 74 nterpret the dat
19164 61 20 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 a that cursor P1
19165 20 70 6f 69 6e 74 73 20 74 6f 20 61 73 20 61 20 points to as a
19166 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 structure built
19167 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 4d 61 6b using.** the Mak
19168 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74 eRecord instruct
19169 69 6f 6e 2e 20 20 28 53 65 65 20 74 68 65 20 4d ion. (See the M
1916a 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 akeRecord opcode
1916b 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a for additional.
1916c 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ** information a
1916d 62 6f 75 74 20 74 68 65 20 66 6f 72 6d 61 74 20 bout the format
1916e 6f 66 20 74 68 65 20 64 61 74 61 2e 29 20 20 45 of the data.) E
1916f 78 74 72 61 63 74 20 74 68 65 20 50 32 2d 74 68 xtract the P2-th
19170 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 column.** from
19171 74 68 69 73 20 72 65 63 6f 72 64 2e 20 20 49 66 this record. If
19172 20 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 there are less
19173 74 68 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a 20 that (P2+1) .**
19174 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 72 65 values in the re
19175 63 6f 72 64 2c 20 65 78 74 72 61 63 74 20 61 20 cord, extract a
19176 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 NULL..**.** The
19177 76 61 6c 75 65 20 65 78 74 72 61 63 74 65 64 20 value extracted
19178 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 is stored in reg
19179 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 ister P3..**.**
1917a 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f If the column co
1917b 6e 74 61 69 6e 73 20 66 65 77 65 72 20 74 68 61 ntains fewer tha
1917c 6e 20 50 32 20 66 69 65 6c 64 73 2c 20 74 68 65 n P2 fields, the
1917d 6e 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c n extract a NULL
1917e 2e 20 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 . Or,.** if the
1917f 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 P4 argument is
19180 61 20 50 34 5f 4d 45 4d 20 75 73 65 20 74 68 65 a P4_MEM use the
19181 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 value of the P4
19182 20 61 72 67 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 argument as.**
19183 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 63 the result..*/.c
19184 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20 7b ase OP_Column: {
19185 0a 20 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 . u32 payloadSi
19186 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ze; /* Number
19187 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 of bytes in the
19188 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 record */. int
19189 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 2f p1 = pOp->p1; /
1918a 2a 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74 68 * P1 value of th
1918b 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e e opcode */. in
1918c 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 t p2 = pOp->p2;
1918d 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 /* column numbe
1918e 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f r to retrieve */
1918f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
19190 43 20 3d 20 30 3b 2f 2a 20 54 68 65 20 56 44 42 C = 0;/* The VDB
19191 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 68 E cursor */. ch
19192 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 20 20 ar *zRec;
19193 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 /* Pointer to c
19194 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 2d 64 omplete record-d
19195 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f ata */. BtCurso
19196 72 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 r *pCrsr; /* T
19197 68 65 20 42 54 72 65 65 20 63 75 72 73 6f 72 20 he BTree cursor
19198 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 3b */. u32 *aType;
19199 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 65 /* aType
1919a 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 [i] holds the nu
1919b 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 68 meric type of th
1919c 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f e i-th column */
1919d 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b . u32 *aOffset;
1919e 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 /* aOffset
1919f 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f [i] is offset to
191a0 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 66 start of data f
191a1 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a or i-th column *
191a2 2f 0a 20 20 75 33 32 20 6e 46 69 65 6c 64 3b 20 /. u32 nField;
191a3 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 /* number
191a4 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 of fields in th
191a5 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e e record */. in
191a6 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 t len;
191a7 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f /* The length o
191a8 66 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 f the serialized
191a9 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 6f data for the co
191aa 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 3b lumn */. int i;
191ab 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
191ac 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
191ad 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 char *zData;
191ae 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 /* Part of
191af 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 the record being
191b0 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 4d 65 decoded */. Me
191b1 6d 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 m *pDest;
191b2 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 /* Where to wri
191b3 74 65 20 74 68 65 20 65 78 74 72 61 63 74 65 64 te the extracted
191b4 20 76 61 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d 20 value */. Mem
191b5 73 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f sMem; /
191b6 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 * For storing th
191b7 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 e record being d
191b8 65 63 6f 64 65 64 20 2a 2f 0a 0a 20 20 73 4d 65 ecoded */.. sMe
191b9 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 73 m.flags = 0;. s
191ba 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 73 4d Mem.db = 0;. sM
191bb 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a em.zMalloc = 0;.
191bc 20 20 61 73 73 65 72 74 28 20 70 31 3c 70 2d 3e assert( p1<p->
191bd 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 nCursor );. ass
191be 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 ert( pOp->p3>0 &
191bf 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d & pOp->p3<=p->nM
191c0 65 6d 20 29 3b 0a 20 20 70 44 65 73 74 20 3d 20 em );. pDest =
191c1 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
191c2 5d 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 ];. MemSetTypeF
191c3 6c 61 67 28 70 44 65 73 74 2c 20 4d 45 4d 5f 4e lag(pDest, MEM_N
191c4 75 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 ull);.. /* This
191c5 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 block sets the
191c6 76 61 72 69 61 62 6c 65 20 70 61 79 6c 6f 61 64 variable payload
191c7 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65 20 74 Size to be the t
191c8 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 20 otal number of.
191c9 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65 ** bytes in the
191ca 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a 20 20 record.. **.
191cb 2a 2a 20 7a 52 65 63 20 69 73 20 73 65 74 20 74 ** zRec is set t
191cc 6f 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 o be the complet
191cd 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 e text of the re
191ce 63 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76 cord if it is av
191cf 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 ailable.. ** Th
191d0 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 e complete recor
191d1 64 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 d text is always
191d2 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 available for p
191d3 73 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a seudo-tables. *
191d4 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 * If the record
191d5 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 is stored in a c
191d6 75 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c ursor, the compl
191d7 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a ete record text.
191d8 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 ** might be av
191d9 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 ailable in the
191da 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 pC->aRow cache.
191db 20 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 Or it might not
191dc 20 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 be.. ** If the
191dd 20 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c data is unavail
191de 61 62 6c 65 2c 20 20 7a 52 65 63 20 69 73 20 73 able, zRec is s
191df 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2a et to NULL.. **
191e0 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 6f . ** We also co
191e1 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 mpute the number
191e2 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 of columns in t
191e3 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 20 he record. For
191e4 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 68 cursors,. ** th
191e5 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
191e6 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e mns is stored in
191e7 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e the VdbeCursor.
191e8 6e 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e 0a nField element..
191e9 20 20 2a 2f 0a 20 20 70 43 20 3d 20 70 2d 3e 61 */. pC = p->a
191ea 70 43 73 72 5b 70 31 5d 3b 0a 20 20 61 73 73 65 pCsr[p1];. asse
191eb 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 23 69 66 rt( pC!=0 );.#if
191ec 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
191ed 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
191ee 61 73 73 65 72 74 28 20 70 43 2d 3e 70 56 74 61 assert( pC->pVta
191ef 62 43 75 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 bCursor==0 );.#e
191f0 6e 64 69 66 0a 20 20 69 66 28 20 70 43 2d 3e 70 ndif. if( pC->p
191f1 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 Cursor!=0 ){.
191f2 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 20 69 /* The record i
191f3 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 42 2d s stored in a B-
191f4 54 72 65 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d Tree */. rc =
191f5 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 sqlite3VdbeCurs
191f6 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 orMoveto(pC);.
191f7 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
191f8 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
191f9 6f 72 3b 0a 20 20 20 20 7a 52 65 63 20 3d 20 30 or;. zRec = 0
191fa 3b 0a 20 20 20 20 70 43 72 73 72 20 3d 20 70 43 ;. pCrsr = pC
191fb 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 ->pCursor;. i
191fc 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 f( pC->nullRow )
191fd 7b 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 {. payloadS
191fe 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c ize = 0;. }el
191ff 73 65 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65 se if( pC->cache
19200 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 Status==p->cache
19201 43 74 72 20 29 7b 0a 20 20 20 20 20 20 70 61 79 Ctr ){. pay
19202 6c 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d 3e 70 loadSize = pC->p
19203 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 ayloadSize;.
19204 20 20 7a 52 65 63 20 3d 20 28 63 68 61 72 2a 29 zRec = (char*)
19205 70 43 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 pC->aRow;. }e
19206 6c 73 65 20 69 66 28 20 70 43 2d 3e 69 73 49 6e lse if( pC->isIn
19207 64 65 78 20 29 7b 0a 20 20 20 20 20 20 69 36 34 dex ){. i64
19208 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a payloadSize64;.
19209 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1920a 65 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c eeKeySize(pCrsr,
1920b 20 26 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29 &payloadSize64)
1920c 3b 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 ;. payloadS
1920d 69 7a 65 20 3d 20 70 61 79 6c 6f 61 64 53 69 7a ize = payloadSiz
1920e 65 36 34 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a e64;. }else{.
1920f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
19210 65 65 44 61 74 61 53 69 7a 65 28 70 43 72 73 72 eeDataSize(pCrsr
19211 2c 20 26 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b , &payloadSize);
19212 0a 20 20 20 20 7d 0a 20 20 20 20 6e 46 69 65 6c . }. nFiel
19213 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a d = pC->nField;.
19214 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
19215 65 72 74 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 ert( pC->pseudoT
19216 61 62 6c 65 20 29 3b 0a 20 20 20 20 2f 2a 20 54 able );. /* T
19217 68 65 20 72 65 63 6f 72 64 20 69 73 20 74 68 65 he record is the
19218 20 73 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20 61 sole entry of a
19219 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f pseudo-table */
1921a 0a 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 . payloadSize
1921b 20 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20 20 = pC->nData;.
1921c 20 20 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44 61 zRec = pC->pDa
1921d 74 61 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 ta;. pC->cach
1921e 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1921f 53 54 41 4c 45 3b 0a 20 20 20 20 61 73 73 65 72 STALE;. asser
19220 74 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d t( payloadSize==
19221 30 20 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b 0a 0 || zRec!=0 );.
19222 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 2d nField = pC-
19223 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 43 72 >nField;. pCr
19224 73 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f sr = 0;. }.. /
19225 2a 20 49 66 20 70 61 79 6c 6f 61 64 53 69 7a 65 * If payloadSize
19226 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 74 is 0, then just
19227 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a 2f store a NULL */
19228 0a 20 20 69 66 28 20 70 61 79 6c 6f 61 64 53 69 . if( payloadSi
19229 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 ze==0 ){. ass
1922a 65 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 ert( pDest->flag
1922b 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 s&MEM_Null );.
1922c 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e goto op_column
1922d 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 _out;. }. if(
1922e 70 61 79 6c 6f 61 64 53 69 7a 65 3e 64 62 2d 3e payloadSize>db->
1922f 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
19230 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
19231 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
19232 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
19233 70 32 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20 p2<nField );..
19234 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 73 /* Read and pars
19235 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 61 64 e the table head
19236 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72 er. Store the r
19237 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 70 61 esults of the pa
19238 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 rse. ** into th
19239 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 e record header
1923a 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f 66 20 cache fields of
1923b 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f the cursor.. */
1923c 0a 20 20 61 54 79 70 65 20 3d 20 70 43 2d 3e 61 . aType = pC->a
1923d 54 79 70 65 3b 0a 20 20 69 66 28 20 70 43 2d 3e Type;. if( pC->
1923e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e cacheStatus==p->
1923f 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 cacheCtr ){.
19240 61 4f 66 66 73 65 74 20 3d 20 70 43 2d 3e 61 4f aOffset = pC->aO
19241 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a ffset;. }else{.
19242 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 u8 *zIdx;
19243 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
19244 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 to header */.
19245 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 u8 *zEndHdr;
19246 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
19247 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 first byte after
19248 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 the header */.
19249 20 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20 u32 offset;
1924a 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
1924b 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 to the data */.
1924c 20 20 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b 20 int szHdrSz;
1924d 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
1924e 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 he header size f
1924f 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 ield at start of
19250 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 record */. i
19251 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 nt avail;
19252 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
19253 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 es of available
19254 64 61 74 61 20 2a 2f 0a 0a 20 20 20 20 61 73 73 data */.. ass
19255 65 72 74 28 61 54 79 70 65 29 3b 0a 20 20 20 20 ert(aType);.
19256 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 4f pC->aOffset = aO
19257 66 66 73 65 74 20 3d 20 26 61 54 79 70 65 5b 6e ffset = &aType[n
19258 46 69 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d 3e Field];. pC->
19259 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61 payloadSize = pa
1925a 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 70 yloadSize;. p
1925b 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1925c 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 p->cacheCtr;..
1925d 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 /* Figure out
1925e 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 how many bytes
1925f 61 72 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 are in the heade
19260 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 65 r */. if( zRe
19261 63 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61 c ){. zData
19262 20 3d 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c = zRec;. }el
19263 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 se{. if( pC
19264 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 ->isIndex ){.
19265 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 zData = (ch
19266 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 ar*)sqlite3Btree
19267 4b 65 79 46 65 74 63 68 28 70 43 72 73 72 2c 20 KeyFetch(pCrsr,
19268 26 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d &avail);. }
19269 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 44 else{. zD
1926a 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c ata = (char*)sql
1926b 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 ite3BtreeDataFet
1926c 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c ch(pCrsr, &avail
1926d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1926e 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28 /* If KeyFetch(
1926f 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61 )/DataFetch() ma
19270 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65 naged to get the
19271 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c entire payload,
19272 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74 . ** save t
19273 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68 he payload in th
19274 65 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 e pC->aRow cache
19275 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76 . That will sav
19276 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 e us from.
19277 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b ** having to mak
19278 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c e additional cal
19279 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 ls to fetch the
1927a 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 content portion
1927b 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 of. ** the
1927c 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f record.. */
1927d 0a 20 20 20 20 20 20 69 66 28 20 61 76 61 69 6c . if( avail
1927e 3e 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b >=payloadSize ){
1927f 0a 20 20 20 20 20 20 20 20 7a 52 65 63 20 3d 20 . zRec =
19280 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70 zData;. p
19281 43 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 7a C->aRow = (u8*)z
19282 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73 Data;. }els
19283 65 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 e{. pC->a
19284 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d Row = 0;. }
19285 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 . }. /* Th
19286 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
19287 72 74 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c rt is true in al
19288 6c 20 63 61 73 65 73 20 61 63 63 65 70 74 20 77 l cases accept w
19289 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 hen. ** the d
1928a 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 atabase file has
1928b 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 been corrupted
1928c 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 externally..
1928d 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 7a 52 ** assert( zR
1928e 65 63 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e 3d ec!=0 || avail>=
1928f 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 61 payloadSize || a
19290 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20 vail>=9 ); */.
19291 20 20 73 7a 48 64 72 53 7a 20 3d 20 67 65 74 56 szHdrSz = getV
19292 61 72 69 6e 74 33 32 28 28 75 38 2a 29 7a 44 61 arint32((u8*)zDa
19293 74 61 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 ta, offset);..
19294 20 20 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63 /* The KeyFetc
19295 68 28 29 20 6f 72 20 44 61 74 61 46 65 74 63 68 h() or DataFetch
19296 28 29 20 61 62 6f 76 65 20 61 72 65 20 66 61 73 () above are fas
19297 74 20 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74 t and will get t
19298 68 65 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a he entire. **
19299 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 record header i
1929a 6e 20 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42 n most cases. B
1929b 75 74 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69 ut they will fai
1929c 6c 20 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d l to get the com
1929d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63 plete. ** rec
1929e 6f 72 64 20 68 65 61 64 65 72 20 69 66 20 74 68 ord header if th
1929f 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 e record header
192a0 64 6f 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20 does not fit on
192a1 61 20 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20 a single page.
192a2 20 20 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72 ** in the B-Tr
192a3 65 65 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 ee. When that h
192a4 61 70 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69 appens, use sqli
192a5 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
192a6 72 65 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 ree() to. **
192a7 61 63 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70 acquire the comp
192a8 6c 65 74 65 20 68 65 61 64 65 72 20 74 65 78 74 lete header text
192a9 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
192aa 20 21 7a 52 65 63 20 26 26 20 61 76 61 69 6c 3c !zRec && avail<
192ab 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 offset ){.
192ac 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a sMem.flags = 0;.
192ad 20 20 20 20 20 20 73 4d 65 6d 2e 64 62 20 3d 20 sMem.db =
192ae 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 0;. rc = sq
192af 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d lite3VdbeMemFrom
192b0 42 74 72 65 65 28 70 43 72 73 72 2c 20 30 2c 20 Btree(pCrsr, 0,
192b1 6f 66 66 73 65 74 2c 20 70 43 2d 3e 69 73 49 6e offset, pC->isIn
192b2 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 dex, &sMem);.
192b3 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
192b4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
192b5 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f goto op_column_o
192b6 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ut;. }.
192b7 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a zData = sMem.z
192b8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 6e 64 ;. }. zEnd
192b9 48 64 72 20 3d 20 28 75 38 20 2a 29 26 7a 44 61 Hdr = (u8 *)&zDa
192ba 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 ta[offset];.
192bb 7a 49 64 78 20 3d 20 28 75 38 20 2a 29 26 7a 44 zIdx = (u8 *)&zD
192bc 61 74 61 5b 73 7a 48 64 72 53 7a 5d 3b 0a 0a 20 ata[szHdrSz];..
192bd 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68 /* Scan the h
192be 65 61 64 65 72 20 61 6e 64 20 75 73 65 20 69 74 eader and use it
192bf 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 to fill in the
192c0 61 54 79 70 65 5b 5d 20 61 6e 64 20 61 4f 66 66 aType[] and aOff
192c1 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 set[]. ** arr
192c2 61 79 73 2e 20 20 61 54 79 70 65 5b 69 5d 20 77 ays. aType[i] w
192c3 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 ill contain the
192c4 74 79 70 65 20 69 6e 74 65 67 65 72 20 66 6f 72 type integer for
192c5 20 74 68 65 20 69 2d 74 68 0a 20 20 20 20 2a 2a the i-th. **
192c6 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 61 4f 66 66 column and aOff
192c7 73 65 74 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 set[i] will cont
192c8 61 69 6e 20 74 68 65 20 6f 66 66 73 65 74 20 66 ain the offset f
192c9 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
192ca 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 g. ** of the
192cb 72 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 record to the st
192cc 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 art of the data
192cd 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 6f 6c for the i-th col
192ce 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 umn. */. f
192cf 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 69 65 6c 64 or(i=0; i<nField
192d0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
192d1 28 20 7a 49 64 78 3c 7a 45 6e 64 48 64 72 20 29 ( zIdx<zEndHdr )
192d2 7b 0a 20 20 20 20 20 20 20 20 61 4f 66 66 73 65 {. aOffse
192d3 74 5b 69 5d 20 3d 20 6f 66 66 73 65 74 3b 0a 20 t[i] = offset;.
192d4 20 20 20 20 20 20 20 7a 49 64 78 20 2b 3d 20 67 zIdx += g
192d5 65 74 56 61 72 69 6e 74 33 32 28 7a 49 64 78 2c etVarint32(zIdx,
192d6 20 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20 aType[i]);.
192d7 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 71 offset += sq
192d8 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
192d9 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d 29 ypeLen(aType[i])
192da 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
192db 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 20 69 /* If i i
192dc 73 20 6c 65 73 73 20 74 68 61 74 20 6e 46 69 65 s less that nFie
192dd 6c 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 ld, then there a
192de 72 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69 re less fields i
192df 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a n this. *
192e0 2a 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65 * record than Se
192e1 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69 tNumColumns indi
192e2 63 61 74 65 64 20 74 68 65 72 65 20 61 72 65 20 cated there are
192e3 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 columns in the.
192e4 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e ** table.
192e5 20 53 65 74 20 74 68 65 20 6f 66 66 73 65 74 20 Set the offset
192e6 66 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63 6f for any extra co
192e7 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 6e lumns not presen
192e8 74 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 t in. **
192e9 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30 2e the record to 0.
192ea 20 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 65 This tells code
192eb 20 62 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20 below to store
192ec 61 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 2a a NULL. *
192ed 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73 * instead of des
192ee 65 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c erializing a val
192ef 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f ue from the reco
192f0 72 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 rd.. */.
192f1 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69 aOffset[i
192f2 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 ] = 0;. }.
192f3 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
192f4 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
192f5 73 4d 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d 2e sMem);. sMem.
192f6 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
192f7 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 ;.. /* If we
192f8 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68 have read more h
192f9 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20 eader data than
192fa 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e was contained in
192fb 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20 the header,.
192fc 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e ** or if the en
192fd 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 d of the last fi
192fe 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 eld appears to b
192ff 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
19300 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 f the. ** rec
19301 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65 ord, or if the e
19302 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 nd of the last f
19303 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 ield appears to
19304 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e be before the en
19305 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 d. ** of the
19306 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c record (when all
19307 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29 fields present)
19308 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62 , then we must b
19309 65 20 64 65 61 6c 69 6e 67 20 0a 20 20 20 20 2a e dealing . *
1930a 2a 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 * with a corrupt
1930b 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a database.. *
1930c 2f 0a 20 20 20 20 69 66 28 20 7a 49 64 78 3e 7a /. if( zIdx>z
1930d 45 6e 64 48 64 72 20 7c 7c 20 6f 66 66 73 65 74 EndHdr || offset
1930e 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 0a 20 20 >payloadSize .
1930f 20 20 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45 6e || (zIdx==zEn
19310 64 48 64 72 20 26 26 20 6f 66 66 73 65 74 21 3d dHdr && offset!=
19311 70 61 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a payloadSize) ){.
19312 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
19313 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
19314 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f goto op_co
19315 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a lumn_out;. }.
19316 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 }.. /* Get th
19317 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 e column informa
19318 74 69 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65 74 tion. If aOffset
19319 5b 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f [p2] is non-zero
1931a 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65 73 , then . ** des
1931b 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c erialize the val
1931c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f ue from the reco
1931d 72 64 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70 rd. If aOffset[p
1931e 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 2] is zero,. **
1931f 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 then there are
19320 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64 not enough field
19321 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
19322 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 20 to satisfy the.
19323 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e ** request. In
19324 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 20 this case, set
19325 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f the value NULL o
19326 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69 73 r to P4 if P4 is
19327 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 . ** a pointer
19328 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e to a Mem object.
19329 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66 66 . */. if( aOff
1932a 73 65 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20 61 set[p2] ){. a
1932b 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1932c 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 E_OK );. if(
1932d 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71 zRec ){. sq
1932e 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1932f 61 73 65 45 78 74 65 72 6e 61 6c 28 70 44 65 73 aseExternal(pDes
19330 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 t);. sqlite
19331 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 3VdbeSerialGet((
19332 75 38 20 2a 29 26 7a 52 65 63 5b 61 4f 66 66 73 u8 *)&zRec[aOffs
19333 65 74 5b 70 32 5d 5d 2c 20 61 54 79 70 65 5b 70 et[p2]], aType[p
19334 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 2], pDest);.
19335 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 65 6e }else{. len
19336 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
19337 72 69 61 6c 54 79 70 65 4c 65 6e 28 61 54 79 70 rialTypeLen(aTyp
19338 65 5b 70 32 5d 29 3b 0a 20 20 20 20 20 20 73 71 e[p2]);. sq
19339 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
1933a 28 26 73 4d 65 6d 2c 20 70 44 65 73 74 29 3b 0a (&sMem, pDest);.
1933b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1933c 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 e3VdbeMemFromBtr
1933d 65 65 28 70 43 72 73 72 2c 20 61 4f 66 66 73 65 ee(pCrsr, aOffse
1933e 74 5b 70 32 5d 2c 20 6c 65 6e 2c 20 70 43 2d 3e t[p2], len, pC->
1933f 69 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b isIndex, &sMem);
19340 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
19341 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
19342 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 goto op_colu
19343 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a mn_out;. }.
19344 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 73 4d zData = sM
19345 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 em.z;. sqli
19346 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 te3VdbeSerialGet
19347 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 61 54 79 ((u8*)zData, aTy
19348 70 65 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a pe[p2], pDest);.
19349 20 20 20 20 7d 0a 20 20 20 20 70 44 65 73 74 2d }. pDest-
1934a 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b >enc = encoding;
1934b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
1934c 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1934d 34 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 4_MEM ){. s
1934e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
1934f 6c 6c 6f 77 43 6f 70 79 28 70 44 65 73 74 2c 20 llowCopy(pDest,
19350 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45 pOp->p4.pMem, ME
19351 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 7d M_Static);. }
19352 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
19353 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 rt( pDest->flags
19354 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 &MEM_Null );.
19355 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
19356 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 we dynamically a
19357 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 llocated space t
19358 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 o hold the data
19359 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c (in the. ** sql
1935a 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 ite3VdbeMemFromB
1935b 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 tree() call abov
1935c 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 e) then transfer
1935d 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 control of that
1935e 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c . ** dynamicall
1935f 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 y allocated spac
19360 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20 70 44 e over to the pD
19361 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20 est structure..
19362 20 2a 2a 20 54 68 69 73 20 70 72 65 76 65 6e 74 ** This prevent
19363 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79 2e s a memory copy.
19364 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 4d 65 6d . */. if( sMem
19365 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 .zMalloc ){.
19366 61 73 73 65 72 74 28 20 73 4d 65 6d 2e 7a 3d 3d assert( sMem.z==
19367 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a sMem.zMalloc );.
19368 20 20 20 20 61 73 73 65 72 74 28 20 21 28 70 44 assert( !(pD
19369 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d est->flags & MEM
1936a 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73 _Dyn) );. ass
1936b 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e 66 6c ert( !(pDest->fl
1936c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c ags & (MEM_Blob|
1936d 4d 45 4d 5f 53 74 72 29 29 20 7c 7c 20 70 44 65 MEM_Str)) || pDe
1936e 73 74 2d 3e 7a 3d 3d 73 4d 65 6d 2e 7a 20 29 3b st->z==sMem.z );
1936f 0a 20 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 . pDest->flag
19370 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d s &= ~(MEM_Ephem
19371 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 |MEM_Static);.
19372 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c pDest->flags |
19373 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 = MEM_Term;.
19374 70 44 65 73 74 2d 3e 7a 20 3d 20 73 4d 65 6d 2e pDest->z = sMem.
19375 7a 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 7a 4d z;. pDest->zM
19376 61 6c 6c 6f 63 20 3d 20 73 4d 65 6d 2e 7a 4d 61 alloc = sMem.zMa
19377 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 lloc;. }.. rc
19378 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
19379 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 44 MakeWriteable(pD
1937a 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e est);..op_column
1937b 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45 5f 4d _out:. UPDATE_M
1937c 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 73 AX_BLOBSIZE(pDes
1937d 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 t);. REGISTER_T
1937e 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44 RACE(pOp->p3, pD
1937f 65 73 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d est);. break;.}
19380 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66 66 ../* Opcode: Aff
19381 69 6e 69 74 79 20 50 31 20 50 32 20 2a 20 50 34 inity P1 P2 * P4
19382 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 *.**.** Apply a
19383 66 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20 72 ffinities to a r
19384 61 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69 73 ange of P2 regis
19385 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 ters starting wi
19386 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 th P1..**.** P4
19387 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 is a string that
19388 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 is P2 character
19389 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68 20 s long. The nth
1938a 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 character of the
1938b 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 .** string indic
1938c 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 ates the column
1938d 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
1938e 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
1938f 20 74 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f the nth.** memo
19390 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72 ry cell in the r
19391 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 ange..*/.case OP
19392 5f 41 66 66 69 6e 69 74 79 3a 20 7b 0a 20 20 63 _Affinity: {. c
19393 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 20 3d har *zAffinity =
19394 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 4d 65 pOp->p4.z;. Me
19395 6d 20 2a 70 44 61 74 61 30 20 3d 20 26 70 2d 3e m *pData0 = &p->
19396 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
19397 20 4d 65 6d 20 2a 70 4c 61 73 74 20 3d 20 26 70 Mem *pLast = &p
19398 44 61 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d Data0[pOp->p2-1]
19399 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a 0a ;. Mem *pRec;..
1939a 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 for(pRec=pData
1939b 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 0; pRec<=pLast;
1939c 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 70 pRec++){. Exp
1939d 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 andBlob(pRec);.
1939e 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 applyAffinity
1939f 28 70 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 79 (pRec, zAffinity
193a0 5b 70 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 65 [pRec-pData0], e
193a1 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 ncoding);. }.
193a2 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
193a3 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 20 ode: MakeRecord
193a4 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
193a5 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32 20 72 .** Convert P2 r
193a6 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 egisters beginni
193a7 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74 6f 20 ng with P1 into
193a8 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a a single entry.*
193a9 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 * suitable for u
193aa 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65 63 se as a data rec
193ab 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 ord in a databas
193ac 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61 20 e table or as a
193ad 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e 64 key.** in an ind
193ae 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 6c 73 ex. The details
193af 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 61 of the format a
193b0 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20 61 73 re irrelevant as
193b1 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65 20 long as.** the
193b2 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 OP_Column opcode
193b3 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65 20 can decode the
193b4 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a 2a 2a record later..**
193b5 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72 63 65 Refer to source
193b6 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 code comments f
193b7 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f or the details o
193b8 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20 f the record.**
193b9 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 format..**.** P4
193ba 20 6d 61 79 20 62 65 20 61 20 73 74 72 69 6e 67 may be a string
193bb 20 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 that is P2 char
193bc 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 acters long. Th
193bd 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 e nth character
193be 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 of the.** string
193bf 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 indicates the c
193c0 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 olumn affinity t
193c1 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 hat should be us
193c2 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a ed for the nth.*
193c3 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 * field of the i
193c4 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 ndex key..**.**
193c5 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d The mapping from
193c6 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 66 character to af
193c7 66 69 6e 69 74 79 20 69 73 20 67 69 76 65 6e 20 finity is given
193c8 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 by the SQLITE_AF
193c9 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65 66 F_.** macros def
193ca 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e ined in sqliteIn
193cb 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 t.h..**.** If P4
193cc 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 6c is NULL then al
193cd 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73 20 68 l index fields h
193ce 61 76 65 20 74 68 65 20 61 66 66 69 6e 69 74 79 ave the affinity
193cf 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f NONE..*/.case O
193d0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b 0a P_MakeRecord: {.
193d1 20 20 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 /* Assuming th
193d2 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 61 69 6e e record contain
193d3 73 20 4e 20 66 69 65 6c 64 73 2c 20 74 68 65 20 s N fields, the
193d4 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c 6f record format lo
193d5 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74 68 oks. ** like th
193d6 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d is:. **. ** --
193d7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
193d8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
193d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
193da 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
193db 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 ------. ** | hd
193dc 72 2d 73 69 7a 65 20 7c 20 74 79 70 65 20 30 20 r-size | type 0
193dd 7c 20 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20 7c | type 1 | ... |
193de 20 74 79 70 65 20 4e 2d 31 20 7c 20 64 61 74 61 type N-1 | data
193df 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 0 | ... | data N
193e0 2d 31 20 7c 20 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d -1 | . ** -----
193e1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
193e2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
193e3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
193e4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
193e5 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 61 ---. **. ** Da
193e6 74 61 28 30 29 20 69 73 20 74 61 6b 65 6e 20 66 ta(0) is taken f
193e7 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 2e rom register P1.
193e8 20 20 44 61 74 61 28 31 29 20 63 6f 6d 65 73 20 Data(1) comes
193e9 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 from register P1
193ea 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 +1. ** and so f
193eb 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 roth.. **. **
193ec 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64 20 Each type field
193ed 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70 72 is a varint repr
193ee 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72 esenting the ser
193ef 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 ial type of the
193f0 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 . ** correspond
193f1 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 ing data element
193f2 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 64 62 (see sqlite3Vdb
193f3 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20 eSerialType()).
193f4 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a The. ** hdr-siz
193f5 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20 e field is also
193f6 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20 69 a varint which i
193f7 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f s the offset fro
193f8 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a m the beginning.
193f9 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f ** of the reco
193fa 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 2a rd to data0.. *
193fb 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f /. u8 *zNewReco
193fc 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 rd; /* A
193fd 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 buffer to hold t
193fe 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 he data for the
193ff 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 new record */.
19400 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 Mem *pRec;
19401 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 /* The ne
19402 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 w record */. u6
19403 34 20 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 20 4 nData = 0;
19404 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
19405 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 f bytes of data
19406 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e space */. int n
19407 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Hdr = 0;
19408 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
19409 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 ytes of header s
1940a 70 61 63 65 20 2a 2f 0a 20 20 75 36 34 20 6e 42 pace */. u64 nB
1940b 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 yte = 0;
1940c 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 /* Data space r
1940d 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 equired for this
1940e 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 record */. int
1940f 20 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 nZero = 0;
19410 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
19411 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 zero bytes at t
19412 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 he end of the re
19413 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 cord */. int nV
19414 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 arint;
19415 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
19416 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 tes in a varint
19417 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f */. u32 serial_
19418 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 type; /* T
19419 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d ype field */. M
1941a 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 em *pData0;
1941b 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 /* First f
1941c 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 ield to be combi
1941d 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 ned into the rec
1941e 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c ord */. Mem *pL
1941f 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ast;
19420 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 /* Last field of
19421 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
19422 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 int nField;
19423 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
19424 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
19425 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 he record */. c
19426 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 har *zAffinity;
19427 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 /* The aff
19428 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 inity string for
19429 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1942a 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 int file_format
1942b 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 ; /* File
1942c 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f format to use fo
1942d 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 r encoding */.
1942e 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
1942f 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
19430 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f used in zNewReco
19431 72 64 5b 5d 20 2a 2f 0a 0a 20 20 6e 46 69 65 6c rd[] */.. nFiel
19432 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 7a d = pOp->p1;. z
19433 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e Affinity = pOp->
19434 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 p4.z;. assert(
19435 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d nField>0 && pOp-
19436 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 >p2>0 && pOp->p2
19437 2b 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d +nField<=p->nMem
19438 20 29 3b 0a 20 20 70 44 61 74 61 30 20 3d 20 26 );. pData0 = &
19439 70 2d 3e 61 4d 65 6d 5b 6e 46 69 65 6c 64 5d 3b p->aMem[nField];
1943a 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d . nField = pOp-
1943b 3e 70 32 3b 0a 20 20 70 4c 61 73 74 20 3d 20 26 >p2;. pLast = &
1943c 70 44 61 74 61 30 5b 6e 46 69 65 6c 64 2d 31 5d pData0[nField-1]
1943d 3b 0a 20 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 ;. file_format
1943e 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c = p->minWriteFil
1943f 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c eFormat;.. /* L
19440 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 oop through the
19441 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 69 elements that wi
19442 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68 65 20 72 ll make up the r
19443 65 63 6f 72 64 20 74 6f 20 66 69 67 75 72 65 0a ecord to figure.
19444 20 20 2a 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63 ** out how muc
19445 68 20 73 70 61 63 65 20 69 73 20 72 65 71 75 69 h space is requi
19446 72 65 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 red for the new
19447 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 record.. */. f
19448 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 or(pRec=pData0;
19449 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 pRec<=pLast; pRe
1944a 63 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 65 c++){. int le
1944b 6e 3b 0a 20 20 20 20 69 66 28 20 7a 41 66 66 69 n;. if( zAffi
1944c 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 61 70 nity ){. ap
1944d 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 63 plyAffinity(pRec
1944e 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63 , zAffinity[pRec
1944f 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 -pData0], encodi
19450 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ng);. }. i
19451 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d f( pRec->flags&M
19452 45 4d 5f 5a 65 72 6f 20 26 26 20 70 52 65 63 2d EM_Zero && pRec-
19453 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 >n>0 ){. sq
19454 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 lite3VdbeMemExpa
19455 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 ndBlob(pRec);.
19456 20 20 7d 0a 20 20 20 20 73 65 72 69 61 6c 5f 74 }. serial_t
19457 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ype = sqlite3Vdb
19458 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 65 63 eSerialType(pRec
19459 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a , file_format);.
1945a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 len = sqlite
1945b 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 3VdbeSerialTypeL
1945c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b en(serial_type);
1945d 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 6c 65 . nData += le
1945e 6e 3b 0a 20 20 20 20 6e 48 64 72 20 2b 3d 20 73 n;. nHdr += s
1945f 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
19460 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 serial_type);.
19461 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 if( pRec->flag
19462 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a s & MEM_Zero ){.
19463 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 /* Only pu
19464 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 re zero-filled B
19465 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 6e 70 75 LOBs can be inpu
19466 74 20 74 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 t to this Opcode
19467 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 20 64 6f .. ** We do
19468 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 not allow blobs
19469 20 77 69 74 68 20 61 20 70 72 65 66 69 78 20 61 with a prefix a
1946a 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 nd a zero-filled
1946b 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 tail. */.
1946c 6e 5a 65 72 6f 20 2b 3d 20 70 52 65 63 2d 3e 75 nZero += pRec->u
1946d 2e 69 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 .i;. }else if
1946e 28 20 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 6e ( len ){. n
1946f 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Zero = 0;. }.
19470 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 }.. /* Add th
19471 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72 e initial header
19472 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61 varint and tota
19473 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 l the size */.
19474 6e 48 64 72 20 2b 3d 20 6e 56 61 72 69 6e 74 20 nHdr += nVarint
19475 3d 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c = sqlite3VarintL
19476 65 6e 28 6e 48 64 72 29 3b 0a 20 20 69 66 28 20 en(nHdr);. if(
19477 6e 56 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 nVarint<sqlite3V
19478 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29 20 29 arintLen(nHdr) )
19479 7b 0a 20 20 20 20 6e 48 64 72 2b 2b 3b 0a 20 20 {. nHdr++;.
1947a 7d 0a 20 20 6e 42 79 74 65 20 3d 20 6e 48 64 72 }. nByte = nHdr
1947b 2b 6e 44 61 74 61 2d 6e 5a 65 72 6f 3b 0a 20 20 +nData-nZero;.
1947c 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c if( nByte>db->aL
1947d 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1947e 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
1947f 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
19480 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 }.. /* Make su
19481 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 re the output re
19482 67 69 73 74 65 72 20 68 61 73 20 61 20 62 75 66 gister has a buf
19483 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 fer large enough
19484 20 74 6f 20 73 74 6f 72 65 20 0a 20 20 2a 2a 20 to store . **
19485 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 the new record.
19486 54 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 The output regis
19487 74 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73 ter (pOp->p3) is
19488 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a not allowed to.
19489 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 ** be one of t
1948a 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 he input registe
1948b 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65 20 rs (because the
1948c 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 following call t
1948d 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 o. ** sqlite3Vd
1948e 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c beMemGrow() coul
1948f 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61 d clobber the va
19490 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 lue before it is
19491 20 75 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61 used).. */. a
19492 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 ssert( pOp->p3<p
19493 4f 70 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 Op->p1 || pOp->p
19494 33 3e 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 3>=pOp->p1+pOp->
19495 70 32 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 p2 );. pOut = &
19496 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
19497 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 ;. if( sqlite3V
19498 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c dbeMemGrow(pOut,
19499 20 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 nByte, 0) ){.
1949a 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1949b 20 7d 0a 20 20 7a 4e 65 77 52 65 63 6f 72 64 20 }. zNewRecord
1949c 3d 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b = (u8 *)pOut->z;
1949d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 .. /* Write the
1949e 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 20 3d record */. i =
1949f 20 70 75 74 56 61 72 69 6e 74 33 32 28 7a 4e 65 putVarint32(zNe
194a0 77 52 65 63 6f 72 64 2c 20 6e 48 64 72 29 3b 0a wRecord, nHdr);.
194a1 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 for(pRec=pData
194a2 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 0; pRec<=pLast;
194a3 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 73 65 72 pRec++){. ser
194a4 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 ial_type = sqlit
194a5 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
194a6 28 70 52 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d (pRec, file_form
194a7 61 74 29 3b 0a 20 20 20 20 69 20 2b 3d 20 70 75 at);. i += pu
194a8 74 56 61 72 69 6e 74 33 32 28 26 7a 4e 65 77 52 tVarint32(&zNewR
194a9 65 63 6f 72 64 5b 69 5d 2c 20 73 65 72 69 61 6c ecord[i], serial
194aa 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f 2a 20 _type); /*
194ab 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20 serial type */.
194ac 20 7d 0a 20 20 66 6f 72 28 70 52 65 63 3d 70 44 }. for(pRec=pD
194ad 61 74 61 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 ata0; pRec<=pLas
194ae 74 3b 20 70 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 t; pRec++){ /*
194af 73 65 72 69 61 6c 20 64 61 74 61 20 2a 2f 0a 20 serial data */.
194b0 20 20 20 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 i += sqlite3V
194b1 64 62 65 53 65 72 69 61 6c 50 75 74 28 26 7a 4e dbeSerialPut(&zN
194b2 65 77 52 65 63 6f 72 64 5b 69 5d 2c 20 6e 42 79 ewRecord[i], nBy
194b3 74 65 2d 69 2c 20 70 52 65 63 2c 20 66 69 6c 65 te-i, pRec, file
194b4 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 _format);. }.
194b5 61 73 73 65 72 74 28 20 69 3d 3d 6e 42 79 74 65 assert( i==nByte
194b6 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 );.. assert( p
194b7 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d Op->p3>0 && pOp-
194b8 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
194b9 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 6e 42 79 74 pOut->n = nByt
194ba 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 e;. pOut->flags
194bb 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 = MEM_Blob | ME
194bc 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78 M_Dyn;. pOut->x
194bd 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e Del = 0;. if( n
194be 5a 65 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 Zero ){. pOut
194bf 2d 3e 75 2e 69 20 3d 20 6e 5a 65 72 6f 3b 0a 20 ->u.i = nZero;.
194c0 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c pOut->flags |
194c1 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a = MEM_Zero;. }.
194c2 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 pOut->enc = SQ
194c3 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 LITE_UTF8; /* I
194c4 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 n case the blob
194c5 69 73 20 65 76 65 72 20 63 6f 6e 76 65 72 74 65 is ever converte
194c6 64 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 52 d to text */. R
194c7 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
194c8 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 p->p3, pOut);.
194c9 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
194ca 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 IZE(pOut);. bre
194cb 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
194cc 3a 20 53 74 61 74 65 6d 65 6e 74 20 50 31 20 2a : Statement P1 *
194cd 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 * * *.**.** Beg
194ce 69 6e 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c in an individual
194cf 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
194d0 61 63 74 69 6f 6e 20 77 68 69 63 68 20 69 73 20 action which is
194d1 70 61 72 74 20 6f 66 20 61 20 6c 61 72 67 65 72 part of a larger
194d2 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e .** transaction.
194d3 20 20 54 68 69 73 20 69 73 20 6e 65 65 64 65 64 This is needed
194d4 20 73 6f 20 74 68 61 74 20 74 68 65 20 73 74 61 so that the sta
194d5 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 tement.** can be
194d6 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 rolled back aft
194d7 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69 74 68 er an error with
194d8 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f out having to ro
194d9 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65 ll back the.** e
194da 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f ntire transactio
194db 6e 2e 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e n. The statemen
194dc 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 t transaction wi
194dd 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ll automatically
194de 0a 2a 2a 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 .** commit when
194df 74 68 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a the VDBE halts..
194e0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 **.** If the dat
194e1 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
194e2 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e is currently in
194e3 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 autocommit mode
194e4 20 28 74 68 61 74 20 0a 2a 2a 20 69 73 20 74 6f (that .** is to
194e5 20 73 61 79 2c 20 69 66 20 69 74 20 69 73 20 69 say, if it is i
194e6 6e 20 62 65 74 77 65 65 6e 20 42 45 47 49 4e 20 n between BEGIN
194e7 61 6e 64 20 43 4f 4d 4d 49 54 29 0a 2a 2a 20 61 and COMMIT).** a
194e8 6e 64 20 69 66 20 74 68 65 72 65 20 61 72 65 20 nd if there are
194e9 6e 6f 20 6f 74 68 65 72 20 61 63 74 69 76 65 20 no other active
194ea 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 20 74 68 statements on th
194eb 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a e same database.
194ec 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 ** connection, t
194ed 68 65 6e 20 74 68 69 73 20 6f 70 65 72 61 74 69 hen this operati
194ee 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 on is a no-op.
194ef 4e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 No statement tra
194f0 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e nsaction.** is n
194f1 65 65 64 65 64 20 73 69 6e 63 65 20 61 6e 79 20 eeded since any
194f2 65 72 72 6f 72 20 63 61 6e 20 75 73 65 20 74 68 error can use th
194f3 65 20 6e 6f 72 6d 61 6c 20 52 4f 4c 4c 42 41 43 e normal ROLLBAC
194f4 4b 20 70 72 6f 63 65 73 73 20 74 6f 0a 2a 2a 20 K process to.**
194f5 75 6e 64 6f 20 63 68 61 6e 67 65 73 2e 0a 2a 2a undo changes..**
194f6 0a 2a 2a 20 49 66 20 61 20 73 74 61 74 65 6d 65 .** If a stateme
194f7 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
194f8 73 20 73 74 61 72 74 65 64 2c 20 74 68 65 6e 20 s started, then
194f9 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 a statement jour
194fa 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 77 69 6c 6c nal file.** will
194fb 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e be allocated an
194fc 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a d initialized..*
194fd 2a 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 *.** The stateme
194fe 6e 74 20 69 73 20 62 65 67 75 6e 20 6f 6e 20 74 nt is begun on t
194ff 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
19500 20 77 69 74 68 20 69 6e 64 65 78 20 50 31 2e 20 with index P1.
19501 20 54 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 The main.** dat
19502 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 abase file has a
19503 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 61 6e 64 n index of 0 and
19504 20 74 68 65 20 66 69 6c 65 20 75 73 65 64 20 66 the file used f
19505 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 or temporary tab
19506 6c 65 73 0a 2a 2a 20 68 61 73 20 61 6e 20 69 6e les.** has an in
19507 64 65 78 20 6f 66 20 31 2e 0a 2a 2f 0a 63 61 73 dex of 1..*/.cas
19508 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74 3a 20 e OP_Statement:
19509 7b 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f {. if( db->auto
1950a 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d Commit==0 || db-
1950b 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 >activeVdbeCnt>1
1950c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 ){. int i =
1950d 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 42 74 72 pOp->p1;. Btr
1950e 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 61 73 73 ee *pBt;. ass
1950f 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 ert( i>=0 && i<d
19510 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 b->nDb );. as
19511 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d sert( db->aDb[i]
19512 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 .pBt!=0 );. p
19513 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e Bt = db->aDb[i].
19514 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 pBt;. assert(
19515 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
19516 6e 54 72 61 6e 73 28 70 42 74 29 20 29 3b 0a 20 nTrans(pBt) );.
19517 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 assert( (p->b
19518 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 treeMask & (1<<i
19519 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 ))!=0 );. if(
1951a 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 !sqlite3BtreeIs
1951b 49 6e 53 74 6d 74 28 70 42 74 29 20 29 7b 0a 20 InStmt(pBt) ){.
1951c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1951d 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 3BtreeBeginStmt(
1951e 70 42 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f pBt);. p->o
1951f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e 74 20 3d penedStatement =
19520 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
19521 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
19522 6f 64 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 ode: AutoCommit
19523 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
19524 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 61 * Set the databa
19525 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 se auto-commit f
19526 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 lag to P1 (1 or
19527 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 0). If P2 is tru
19528 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 e, roll.** back
19529 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 any currently ac
1952a 74 69 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 tive btree trans
1952b 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 actions. If ther
1952c 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 e are any active
1952d 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 .** VMs (apart f
1952e 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 rom this one), t
1952f 68 65 6e 20 74 68 65 20 43 4f 4d 4d 49 54 20 6f hen the COMMIT o
19530 72 20 52 4f 4c 4c 42 41 43 4b 20 73 74 61 74 65 r ROLLBACK state
19531 6d 65 6e 74 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a ment fails..**.*
19532 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 * This instructi
19533 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d on causes the VM
19534 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 to halt..*/.cas
19535 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a e OP_AutoCommit:
19536 20 7b 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 {. int desired
19537 41 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70 AutoCommit = pOp
19538 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 72 6f 6c 6c ->p1;. int roll
19539 62 61 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a back = pOp->p2;.
1953a 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 20 3d int turnOnAC =
1953b 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d desiredAutoComm
1953c 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 it && !db->autoC
1953d 6f 6d 6d 69 74 3b 0a 0a 20 20 61 73 73 65 72 74 ommit;.. assert
1953e 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d ( desiredAutoCom
1953f 6d 69 74 3d 3d 31 20 7c 7c 20 64 65 73 69 72 65 mit==1 || desire
19540 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 dAutoCommit==0 )
19541 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69 ;. assert( desi
19542 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 redAutoCommit==1
19543 20 7c 7c 20 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20 || rollback==0
19544 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 );.. assert( db
19545 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
19546 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 0 ); /* At leas
19547 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 t this one VM is
19548 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 active */.. if
19549 28 20 74 75 72 6e 4f 6e 41 43 20 26 26 20 72 6f ( turnOnAC && ro
1954a 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 llback && db->ac
1954b 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b tiveVdbeCnt>1 ){
1954c 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
1954d 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c instruction impl
1954e 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 ements a ROLLBAC
1954f 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 K and other VMs
19550 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c are. ** still
19551 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 running, and a
19552 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
19553 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e ctive, return an
19554 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e error indicatin
19555 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 g. ** that th
19556 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 e other VMs must
19557 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e complete first.
19558 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c . */. sql
19559 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1955a 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1955b 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 cannot rollback
1955c 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a transaction - ".
1955d 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 "SQL sta
1955e 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 tements in progr
1955f 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 ess");. rc =
19560 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
19561 65 6c 73 65 20 69 66 28 20 74 75 72 6e 4f 6e 41 else if( turnOnA
19562 43 20 26 26 20 21 72 6f 6c 6c 62 61 63 6b 20 26 C && !rollback &
19563 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 & db->writeVdbeC
19564 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 nt>1 ){. /* I
19565 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 f this instructi
19566 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 on implements a
19567 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 74 68 65 72 COMMIT and other
19568 20 56 4d 73 20 61 72 65 20 77 72 69 74 69 6e 67 VMs are writing
19569 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 . ** return a
1956a 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 n error indicati
1956b 6e 67 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 ng that the othe
1956c 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c r VMs must compl
1956d 65 74 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20 ete first. .
1956e 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 */. sqlite3Se
1956f 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
19570 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 Msg, db, "cannot
19571 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74 commit transact
19572 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 ion - ".
19573 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 "SQL statements
19574 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 in progress");.
19575 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
19576 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 USY;. }else if(
19577 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d desiredAutoComm
19578 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d it!=db->autoComm
19579 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f it ){. if( pO
1957a 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 20 20 61 p->p2 ){. a
1957b 73 73 65 72 74 28 20 64 65 73 69 72 65 64 41 75 ssert( desiredAu
1957c 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a 20 toCommit==1 );.
1957d 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c sqlite3Roll
1957e 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 backAll(db);.
1957f 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 db->autoCommi
19580 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 t = 1;. }else
19581 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f {. db->auto
19582 43 6f 6d 6d 69 74 20 3d 20 64 65 73 69 72 65 64 Commit = desired
19583 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 AutoCommit;.
19584 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
19585 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 eHalt(p)==SQLITE
19586 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 _BUSY ){.
19587 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 p->pc = pc;.
19588 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d db->autoCom
19589 6d 69 74 20 3d 20 31 2d 64 65 73 69 72 65 64 41 mit = 1-desiredA
1958a 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 utoCommit;.
1958b 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 p->rc = rc =
1958c 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
1958d 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 goto vdbe_r
1958e 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 eturn;. }.
1958f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e }. if( p->
19590 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
19591 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
19592 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c TE_DONE;. }el
19593 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
19594 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
19595 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 65 }. goto vdbe
19596 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 _return;. }else
19597 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 {. sqlite3Set
19598 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
19599 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 sg, db,.
1959a 28 21 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d (!desiredAutoCom
1959b 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20 73 74 61 mit)?"cannot sta
1959c 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e rt a transaction
1959d 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 within a transa
1959e 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20 20 ction":(.
1959f 20 28 72 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e (rollback)?"can
195a0 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e not rollback - n
195a1 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 o transaction is
195a2 20 61 63 74 69 76 65 22 3a 0a 20 20 20 20 20 20 active":.
195a3 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63 61 "ca
195a4 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f nnot commit - no
195a5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
195a6 61 63 74 69 76 65 22 29 29 3b 0a 20 20 20 20 20 active"));.
195a7 20 20 20 20 0a 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
195a8 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
195a9 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
195aa 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 69 pcode: Transacti
195ab 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a on P1 P2 * * *.*
195ac 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 *.** Begin a tra
195ad 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 nsaction. The t
195ae 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20 ransaction ends
195af 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72 when a Commit or
195b0 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 Rollback.** opc
195b1 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 ode is encounter
195b2 65 64 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f ed. Depending o
195b3 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 n the ON CONFLIC
195b4 54 20 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a T setting, the.*
195b5 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 * transaction mi
195b6 67 68 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c ght also be roll
195b7 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72 ed back if an er
195b8 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 ror is encounter
195b9 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 ed..**.** P1 is
195ba 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
195bb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f database file o
195bc 6e 20 77 68 69 63 68 20 74 68 65 20 74 72 61 6e n which the tran
195bd 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 saction is.** st
195be 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20 arted. Index 0
195bf 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 is the main data
195c0 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e base file and in
195c1 64 65 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20 dex 1 is the.**
195c2 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 file used for te
195c3 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 mporary tables.
195c4 20 49 6e 64 69 63 65 73 20 6f 66 20 32 20 6f 72 Indices of 2 or
195c5 20 6d 6f 72 65 20 61 72 65 20 75 73 65 64 20 66 more are used f
195c6 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 or.** attached d
195c7 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 atabases..**.**
195c8 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 If P2 is non-zer
195c9 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 74 65 2d o, then a write-
195ca 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
195cb 74 61 72 74 65 64 2e 20 20 41 20 52 45 53 45 52 tarted. A RESER
195cc 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f VED lock is.** o
195cd 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 btained on the d
195ce 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 atabase file whe
195cf 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 n a write-transa
195d0 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 ction is started
195d1 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 . No.** other p
195d2 72 6f 63 65 73 73 20 63 61 6e 20 73 74 61 72 74 rocess can start
195d3 20 61 6e 6f 74 68 65 72 20 77 72 69 74 65 20 74 another write t
195d4 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c 65 ransaction while
195d5 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
195d6 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 79 n is.** underway
195d7 2e 20 20 53 74 61 72 74 69 6e 67 20 61 20 77 72 . Starting a wr
195d8 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite transaction
195d9 61 6c 73 6f 20 63 72 65 61 74 65 73 20 61 20 72 also creates a r
195da 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
195db 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 6e A.** write tran
195dc 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 saction must be
195dd 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 started before a
195de 6e 79 20 63 68 61 6e 67 65 73 20 63 61 6e 20 62 ny changes can b
195df 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a 2a e made to the.**
195e0 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 50 database. If P
195e1 32 20 69 73 20 32 20 6f 72 20 67 72 65 61 74 65 2 is 2 or greate
195e2 72 20 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53 r then an EXCLUS
195e3 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f IVE lock is also
195e4 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 obtained.** on
195e5 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 the file..**.**
195e6 49 66 20 50 32 20 69 73 20 7a 65 72 6f 2c 20 74 If P2 is zero, t
195e7 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 hen a read-lock
195e8 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 is obtained on t
195e9 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
195ea 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 ..*/.case OP_Tra
195eb 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e nsaction: {. in
195ec 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 t i = pOp->p1;.
195ed 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 Btree *pBt;..
195ee 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
195ef 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 i<db->nDb );. a
195f0 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 ssert( (p->btree
195f1 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29 21 3d Mask & (1<<i))!=
195f2 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 0 );. pBt = db-
195f3 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20 >aDb[i].pBt;..
195f4 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 72 if( pBt ){. r
195f5 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
195f6 42 65 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20 BeginTrans(pBt,
195f7 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69 66 pOp->p2);. if
195f8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ( rc==SQLITE_BUS
195f9 59 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 Y ){. p->pc
195fa 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e = pc;. p->
195fb 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = rc = SQLITE
195fc 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 _BUSY;. got
195fd 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 o vdbe_return;.
195fe 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
195ff 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 =SQLITE_OK && rc
19600 21 3d 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c !=SQLITE_READONL
19601 59 20 2f 2a 20 26 26 20 72 63 21 3d 53 51 4c 49 Y /* && rc!=SQLI
19602 54 45 5f 42 55 53 59 20 2a 2f 20 29 7b 0a 20 20 TE_BUSY */ ){.
19603 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
19604 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
19605 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
19606 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 }../* Opcode: Re
19607 61 64 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 adCookie P1 P2 P
19608 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 3 * *.**.** Read
19609 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 cookie number P
1960a 33 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 3 from database
1960b 50 31 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 P1 and write it
1960c 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 into register P2
1960d 2e 0a 2a 2a 20 50 33 3d 3d 30 20 69 73 20 74 68 ..** P3==0 is th
1960e 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e e schema version
1960f 2e 20 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 . P3==1 is the
19610 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e database format.
19611 0a 2a 2a 20 50 33 3d 3d 32 20 69 73 20 74 68 65 .** P3==2 is the
19612 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 recommended pag
19613 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 er cache size, a
19614 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 nd so forth. P1
19615 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 ==0 is.** the ma
19616 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
19617 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 and P1==1 is th
19618 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
19619 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a used to store.**
1961a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1961b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 s..**.** If P1 i
1961c 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e s negative, then
1961d 20 74 68 69 73 20 69 73 20 61 20 72 65 71 75 65 this is a reque
1961e 73 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 st to read the s
1961f 69 7a 65 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61 ize of a.** data
19620 62 61 73 65 73 20 66 72 65 65 2d 6c 69 73 74 2e bases free-list.
19621 20 50 33 20 6d 75 73 74 20 62 65 20 73 65 74 20 P3 must be set
19622 74 6f 20 31 20 69 6e 20 74 68 69 73 20 63 61 73 to 1 in this cas
19623 65 2e 20 54 68 65 20 61 63 74 75 61 6c 0a 2a 2a e. The actual.**
19624 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 database access
19625 65 64 20 69 73 20 28 28 50 31 2b 31 29 2a 2d 31 ed is ((P1+1)*-1
19626 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 ). For example,
19627 61 20 50 31 20 70 61 72 61 6d 65 74 65 72 20 6f a P1 parameter o
19628 66 20 2d 31 0a 2a 2a 20 63 6f 72 72 65 73 70 6f f -1.** correspo
19629 6e 64 73 20 74 6f 20 64 61 74 61 62 61 73 65 20 nds to database
1962a 30 20 28 22 6d 61 69 6e 22 29 2c 20 61 20 50 31 0 ("main"), a P1
1962b 20 6f 66 20 2d 32 20 69 73 20 64 61 74 61 62 61 of -2 is databa
1962c 73 65 20 31 20 28 22 74 65 6d 70 22 29 2e 0a 2a se 1 ("temp")..*
1962d 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 *.** There must
1962e 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f be a read-lock o
1962f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 n the database (
19630 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 either a transac
19631 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 tion.** must be
19632 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 72 65 started or there
19633 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e must be an open
19634 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 65 0a cursor) before.
19635 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 ** executing thi
19636 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a s instruction..*
19637 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 43 6f /.case OP_ReadCo
19638 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 20 20 okie: {
19639 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1963a 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e erelease */. in
1963b 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 t iMeta;. int i
1963c 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 Db = pOp->p1;.
1963d 69 6e 74 20 69 43 6f 6f 6b 69 65 20 3d 20 70 4f int iCookie = pO
1963e 70 2d 3e 70 33 3b 0a 0a 20 20 61 73 73 65 72 74 p->p3;.. assert
1963f 28 20 70 4f 70 2d 3e 70 33 3c 53 51 4c 49 54 45 ( pOp->p3<SQLITE
19640 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b _N_BTREE_META );
19641 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a . if( iDb<0 ){.
19642 20 20 20 20 69 44 62 20 3d 20 28 2d 31 2a 28 69 iDb = (-1*(i
19643 44 62 2b 31 29 29 3b 0a 20 20 20 20 69 43 6f 6f Db+1));. iCoo
19644 6b 69 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 0a 20 kie *= -1;. }.
19645 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
19646 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
19647 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e ;. assert( db->
19648 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 aDb[iDb].pBt!=0
19649 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1964a 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1964b 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 2f <iDb))!=0 );. /
1964c 2a 20 54 68 65 20 69 6e 64 65 78 69 6e 67 20 6f * The indexing o
1964d 66 20 6d 65 74 61 20 76 61 6c 75 65 73 20 61 74 f meta values at
1964e 20 74 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 the schema laye
1964f 72 20 69 73 20 6f 66 66 20 62 79 20 6f 6e 65 20 r is off by one
19650 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 69 6e from. ** the in
19651 64 65 78 69 6e 67 20 69 6e 20 74 68 65 20 62 74 dexing in the bt
19652 72 65 65 20 6c 61 79 65 72 2e 20 20 54 68 65 20 ree layer. The
19653 62 74 72 65 65 20 63 6f 6e 73 69 64 65 72 73 20 btree considers
19654 6d 65 74 61 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20 meta[0] to. **
19655 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 be the number of
19656 20 66 72 65 65 20 70 61 67 65 73 20 69 6e 20 74 free pages in t
19657 68 65 20 64 61 74 61 62 61 73 65 20 28 61 20 72 he database (a r
19658 65 61 64 2d 6f 6e 6c 79 20 76 61 6c 75 65 29 0a ead-only value).
19659 20 20 2a 2a 20 61 6e 64 20 6d 65 74 61 5b 31 5d ** and meta[1]
1965a 20 74 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d to be the schem
1965b 61 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 20 73 a cookie. The s
1965c 63 68 65 6d 61 20 6c 61 79 65 72 20 63 6f 6e 73 chema layer cons
1965d 69 64 65 72 73 0a 20 20 2a 2a 20 6d 65 74 61 5b iders. ** meta[
1965e 31 5d 20 74 6f 20 62 65 20 74 68 65 20 73 63 68 1] to be the sch
1965f 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 53 6f 20 ema cookie. So
19660 77 65 20 68 61 76 65 20 74 6f 20 73 68 69 66 74 we have to shift
19661 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 the index. **
19662 62 79 20 6f 6e 65 20 69 6e 20 74 68 65 20 66 6f by one in the fo
19663 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e llowing statemen
19664 74 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 t.. */. rc = s
19665 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 qlite3BtreeGetMe
19666 74 61 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e ta(db->aDb[iDb].
19667 70 42 74 2c 20 31 20 2b 20 69 43 6f 6f 6b 69 65 pBt, 1 + iCookie
19668 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 74 61 29 , (u32 *)&iMeta)
19669 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 ;. pOut->u.i =
1966a 69 4d 65 74 61 3b 0a 20 20 4d 65 6d 53 65 74 54 iMeta;. MemSetT
1966b 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1966c 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b M_Int);. break;
1966d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1966e 65 74 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 etCookie P1 P2 P
1966f 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 3 * *.**.** Writ
19670 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
19671 20 72 65 67 69 73 74 65 72 20 50 33 20 28 69 6e register P3 (in
19672 74 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 terpreted as an
19673 69 6e 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f integer).** into
19674 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 cookie number P
19675 32 20 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2 of database P1
19676 2e 0a 2a 2a 20 50 32 3d 3d 30 20 69 73 20 74 68 ..** P2==0 is th
19677 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e e schema version
19678 2e 20 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 . P2==1 is the
19679 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e database format.
1967a 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 .** P2==2 is the
1967b 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 recommended pag
1967c 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 er cache size, a
1967d 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 nd so forth. P1
1967e 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 ==0 is.** the ma
1967f 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
19680 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 and P1==1 is th
19681 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
19682 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a used to store.**
19683 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
19684 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 s..**.** A trans
19685 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 action must be s
19686 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 tarted before ex
19687 65 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 ecuting this opc
19688 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ode..*/.case OP_
19689 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 SetCookie: {
1968a 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 44 /* in3 */. D
1968b 62 20 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74 b *pDb;. assert
1968c 28 20 70 4f 70 2d 3e 70 32 3c 53 51 4c 49 54 45 ( pOp->p2<SQLITE
1968d 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b _N_BTREE_META );
1968e 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1968f 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
19690 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 <db->nDb );. as
19691 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d sert( (p->btreeM
19692 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 ask & (1<<pOp->p
19693 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 1))!=0 );. pDb
19694 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e = &db->aDb[pOp->
19695 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 p1];. assert( p
19696 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 Db->pBt!=0 );.
19697 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e sqlite3VdbeMemIn
19698 74 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a tegerify(pIn3);.
19699 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20 61 62 /* See note ab
1969a 6f 75 74 20 69 6e 64 65 78 20 73 68 69 66 74 69 out index shifti
1969b 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43 6f 6f ng on OP_ReadCoo
1969c 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 kie */. rc = sq
1969d 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
1969e 4d 65 74 61 28 70 44 62 2d 3e 70 42 74 2c 20 31 Meta(pDb->pBt, 1
1969f 2b 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 +pOp->p2, (int)p
196a0 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 In3->u.i);. if(
196a1 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 pOp->p2==0 ){.
196a2 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 /* When the s
196a3 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 chema cookie cha
196a4 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65 nges, record the
196a5 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 new cookie inte
196a6 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 70 44 rnally */. pD
196a7 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 b->pSchema->sche
196a8 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 70 49 6e 33 ma_cookie = pIn3
196a9 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e 66 ->u.i;. db->f
196aa 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 lags |= SQLITE_I
196ab 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 nternChanges;.
196ac 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 }else if( pOp->p
196ad 32 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 52 2==1 ){. /* R
196ae 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69 6e ecord changes in
196af 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 the file format
196b0 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 */. pDb->pSc
196b1 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 hema->file_forma
196b2 74 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 t = pIn3->u.i;.
196b3 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 }. if( pOp->p1
196b4 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e ==1 ){. /* In
196b5 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 validate all pre
196b6 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
196b7 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 54 45 whenever the TE
196b8 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 MP database.
196b9 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 63 68 61 ** schema is cha
196ba 6e 67 65 64 2e 20 20 54 69 63 6b 65 74 20 23 31 nged. Ticket #1
196bb 36 34 34 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 644 */. sqlit
196bc 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 e3ExpirePrepared
196bd 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a Statements(db);.
196be 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
196bf 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 65 72 69 66 /* Opcode: Verif
196c0 79 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 2a 0a yCookie P1 P2 *.
196c1 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 **.** Check the
196c2 76 61 6c 75 65 20 6f 66 20 67 6c 6f 62 61 6c 20 value of global
196c3 64 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 database paramet
196c4 65 72 20 6e 75 6d 62 65 72 20 30 20 28 74 68 65 er number 0 (the
196c5 0a 2a 2a 20 73 63 68 65 6d 61 20 76 65 72 73 69 .** schema versi
196c6 6f 6e 29 20 61 6e 64 20 6d 61 6b 65 20 73 75 72 on) and make sur
196c7 65 20 69 74 20 69 73 20 65 71 75 61 6c 20 74 6f e it is equal to
196c8 20 50 32 2e 20 20 0a 2a 2a 20 50 31 20 69 73 20 P2. .** P1 is
196c9 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 75 6d the database num
196ca 62 65 72 20 77 68 69 63 68 20 69 73 20 30 20 66 ber which is 0 f
196cb 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 or the main data
196cc 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 base file.** and
196cd 20 31 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 1 for the file
196ce 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 holding temporar
196cf 79 20 74 61 62 6c 65 73 20 61 6e 64 20 73 6f 6d y tables and som
196d0 65 20 68 69 67 68 65 72 20 6e 75 6d 62 65 72 0a e higher number.
196d1 2a 2a 20 66 6f 72 20 61 75 78 69 6c 69 61 72 79 ** for auxiliary
196d2 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a databases..**.*
196d3 2a 20 54 68 65 20 63 6f 6f 6b 69 65 20 63 68 61 * The cookie cha
196d4 6e 67 65 73 20 69 74 73 20 76 61 6c 75 65 20 77 nges its value w
196d5 68 65 6e 65 76 65 72 20 74 68 65 20 64 61 74 61 henever the data
196d6 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e base schema chan
196d7 67 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 65 ges..** This ope
196d8 72 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 ration is used t
196d9 6f 20 64 65 74 65 63 74 20 77 68 65 6e 20 74 68 o detect when th
196da 61 74 20 74 68 65 20 63 6f 6f 6b 69 65 20 68 61 at the cookie ha
196db 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 s changed.** and
196dc 20 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e that the curren
196dd 74 20 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20 t process needs
196de 74 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63 to reread the sc
196df 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 hema..**.** Eith
196e0 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e er a transaction
196e1 20 6e 65 65 64 73 20 74 6f 20 68 61 76 65 20 62 needs to have b
196e2 65 65 6e 20 73 74 61 72 74 65 64 20 6f 72 20 61 een started or a
196e3 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65 65 64 73 0a n OP_Open needs.
196e4 2a 2a 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 ** to be execute
196e5 64 20 28 74 6f 20 65 73 74 61 62 6c 69 73 68 20 d (to establish
196e6 61 20 72 65 61 64 20 6c 6f 63 6b 29 20 62 65 66 a read lock) bef
196e7 6f 72 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20 ore this opcode
196e8 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a is.** invoked..*
196e9 2f 0a 63 61 73 65 20 4f 50 5f 56 65 72 69 66 79 /.case OP_Verify
196ea 43 6f 6f 6b 69 65 3a 20 7b 0a 20 20 69 6e 74 20 Cookie: {. int
196eb 69 4d 65 74 61 3b 0a 20 20 42 74 72 65 65 20 2a iMeta;. Btree *
196ec 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 pBt;. assert( p
196ed 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
196ee 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a ->p1<db->nDb );.
196ef 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
196f0 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
196f1 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 p->p1))!=0 );.
196f2 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f pBt = db->aDb[pO
196f3 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 69 66 p->p1].pBt;. if
196f4 28 20 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 ( pBt ){. rc
196f5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
196f6 74 4d 65 74 61 28 70 42 74 2c 20 31 2c 20 28 75 tMeta(pBt, 1, (u
196f7 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20 20 32 *)&iMeta);.
196f8 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
196f9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 SQLITE_OK;. i
196fa 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Meta = 0;. }.
196fb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
196fc 4b 20 26 26 20 69 4d 65 74 61 21 3d 70 4f 70 2d K && iMeta!=pOp-
196fd 3e 70 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >p2 ){. sqlit
196fe 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
196ff 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d zErrMsg);. p-
19700 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 >zErrMsg = sqlit
19701 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 22 e3DbStrDup(db, "
19702 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
19703 68 61 73 20 63 68 61 6e 67 65 64 22 29 3b 0a 20 has changed");.
19704 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 63 68 /* If the sch
19705 65 6d 61 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 ema-cookie from
19706 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
19707 65 20 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f e matches the co
19708 6f 6b 69 65 20 0a 20 20 20 20 2a 2a 20 73 74 6f okie . ** sto
19709 72 65 64 20 77 69 74 68 20 74 68 65 20 69 6e 2d red with the in-
1970a 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 memory represent
1970b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 68 ation of the sch
1970c 65 6d 61 2c 20 64 6f 0a 20 20 20 20 2a 2a 20 6e ema, do. ** n
1970d 6f 74 20 72 65 6c 6f 61 64 20 74 68 65 20 73 63 ot reload the sc
1970e 68 65 6d 61 20 66 72 6f 6d 20 74 68 65 20 64 61 hema from the da
1970f 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 tabase file..
19710 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 76 69 **. ** If vi
19711 72 74 75 61 6c 2d 74 61 62 6c 65 73 20 61 72 65 rtual-tables are
19712 20 69 6e 20 75 73 65 2c 20 74 68 69 73 20 69 73 in use, this is
19713 20 6e 6f 74 20 6a 75 73 74 20 61 6e 20 6f 70 74 not just an opt
19714 69 6d 69 7a 61 74 69 6f 6e 2e 0a 20 20 20 20 2a imization.. *
19715 2a 20 4f 66 74 65 6e 2c 20 76 2d 74 61 62 6c 65 * Often, v-table
19716 73 20 73 74 6f 72 65 20 74 68 65 69 72 20 64 61 s store their da
19717 74 61 20 69 6e 20 6f 74 68 65 72 20 53 51 4c 69 ta in other SQLi
19718 74 65 20 74 61 62 6c 65 73 2c 20 77 68 69 63 68 te tables, which
19719 0a 20 20 20 20 2a 2a 20 61 72 65 20 71 75 65 72 . ** are quer
1971a 69 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 ied from within
1971b 78 4e 65 78 74 28 29 20 61 6e 64 20 6f 74 68 65 xNext() and othe
1971c 72 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 r v-table method
1971d 73 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 70 s using. ** p
1971e 72 65 70 61 72 65 64 20 71 75 65 72 69 65 73 2e repared queries.
1971f 20 49 66 20 73 75 63 68 20 61 20 71 75 65 72 79 If such a query
19720 20 69 73 20 6f 75 74 2d 6f 66 2d 64 61 74 65 2c is out-of-date,
19721 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 we do not want
19722 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 63 61 72 to. ** discar
19723 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 d the database s
19724 63 68 65 6d 61 2c 20 61 73 20 74 68 65 20 75 73 chema, as the us
19725 65 72 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e er code implemen
19726 74 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 ting the. **
19727 76 2d 74 61 62 6c 65 20 77 6f 75 6c 64 20 68 61 v-table would ha
19728 76 65 20 74 6f 20 62 65 20 72 65 61 64 79 20 66 ve to be ready f
19729 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 or the sqlite3_v
1972a 74 61 62 20 73 74 72 75 63 74 75 72 65 20 69 74 tab structure it
1972b 73 65 6c 66 0a 20 20 20 20 2a 2a 20 74 6f 20 62 self. ** to b
1972c 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 77 68 e invalidated wh
1972d 65 6e 65 76 65 72 20 73 71 6c 69 74 65 33 5f 73 enever sqlite3_s
1972e 74 65 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 tep() is called
1972f 66 72 6f 6d 20 77 69 74 68 69 6e 20 0a 20 20 20 from within .
19730 20 2a 2a 20 61 20 76 2d 74 61 62 6c 65 20 6d 65 ** a v-table me
19731 74 68 6f 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 thod.. */.
19732 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 70 4f 70 if( db->aDb[pOp
19733 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 ->p1].pSchema->s
19734 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 21 3d 69 4d chema_cookie!=iM
19735 65 74 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c eta ){. sql
19736 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
19737 6c 53 63 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d lSchema(db, pOp-
19738 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 >p1);. }..
19739 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 sqlite3ExpirePr
1973a 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 eparedStatements
1973b 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 (db);. rc = S
1973c 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 QLITE_SCHEMA;.
1973d 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1973e 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61 Opcode: OpenRea
1973f 64 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 d P1 P2 P3 P4 P5
19740 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 .**.** Open a re
19741 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 ad-only cursor f
19742 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 or the database
19743 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 table whose root
19744 20 70 61 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 page is.** P2 i
19745 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c n a database fil
19746 65 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 e. The database
19747 20 66 69 6c 65 20 69 73 20 64 65 74 65 72 6d 69 file is determi
19748 6e 65 64 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50 ned by P3. .** P
19749 33 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 3==0 means the m
1974a 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 50 33 ain database, P3
1974b 3d 3d 31 20 6d 65 61 6e 73 20 74 68 65 20 64 61 ==1 means the da
1974c 74 61 62 61 73 65 20 75 73 65 64 20 66 6f 72 20 tabase used for
1974d 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 .** temporary ta
1974e 62 6c 65 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d bles, and P3>1 m
1974f 65 61 6e 73 20 75 73 65 64 20 74 68 65 20 63 6f eans used the co
19750 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 74 74 61 rresponding atta
19751 63 68 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 ched.** database
19752 2e 20 20 47 69 76 65 20 74 68 65 20 6e 65 77 20 . Give the new
19753 63 75 72 73 6f 72 20 61 6e 20 69 64 65 6e 74 69 cursor an identi
19754 66 69 65 72 20 6f 66 20 50 31 2e 20 20 54 68 65 fier of P1. The
19755 20 50 31 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 P1.** values ne
19756 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 ed not be contig
19757 75 6f 75 73 20 62 75 74 20 61 6c 6c 20 50 31 20 uous but all P1
19758 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20 62 65 values should be
19759 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e small integers.
1975a 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72 .** It is an err
1975b 6f 72 20 66 6f 72 20 50 31 20 74 6f 20 62 65 20 or for P1 to be
1975c 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 negative..**.**
1975d 49 66 20 50 35 21 3d 30 20 74 68 65 6e 20 75 73 If P5!=0 then us
1975e 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
1975f 20 72 65 67 69 73 74 65 72 20 50 32 20 61 73 20 register P2 as
19760 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e the root page, n
19761 6f 74 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 ot.** the value
19762 6f 66 20 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a of P2 itself..**
19763 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c 6c 20 62 .** There will b
19764 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e e a read lock on
19765 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 68 the database wh
19766 65 6e 65 76 65 72 20 74 68 65 72 65 20 69 73 20 enever there is
19767 61 6e 0a 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f an.** open curso
19768 72 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 r. If the datab
19769 61 73 65 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64 ase was unlocked
1976a 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20 69 prior to this i
1976b 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 nstruction.** th
1976c 65 6e 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 en a read lock i
1976d 73 20 61 63 71 75 69 72 65 64 20 61 73 20 70 61 s acquired as pa
1976e 72 74 20 6f 66 20 74 68 69 73 20 69 6e 73 74 72 rt of this instr
1976f 75 63 74 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a uction. A read.
19770 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f ** lock allows o
19771 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 ther processes t
19772 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62 o read the datab
19773 61 73 65 20 62 75 74 20 70 72 6f 68 69 62 69 74 ase but prohibit
19774 73 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 72 20 70 s.** any other p
19775 72 6f 63 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69 rocess from modi
19776 66 79 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 fying the databa
19777 73 65 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f se. The read lo
19778 63 6b 20 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65 ck is.** release
19779 64 20 77 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f d when all curso
1977a 72 73 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 20 rs are closed.
1977b 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 If this instruct
1977c 69 6f 6e 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 ion attempts.**
1977d 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f to get a read lo
1977e 63 6b 20 62 75 74 20 66 61 69 6c 73 2c 20 74 68 ck but fails, th
1977f 65 20 73 63 72 69 70 74 20 74 65 72 6d 69 6e 61 e script termina
19780 74 65 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 tes with an.** S
19781 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 QLITE_BUSY error
19782 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 code..**.** The
19783 20 50 34 20 76 61 6c 75 65 20 69 73 20 61 20 70 P4 value is a p
19784 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 ointer to a KeyI
19785 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 nfo structure th
19786 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 0a 2a at defines the.*
19787 2a 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f * content and co
19788 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
19789 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 50 34 of indices. P4
1978a 20 69 73 20 4e 55 4c 4c 20 66 6f 72 20 63 75 72 is NULL for cur
1978b 73 6f 72 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 sors.** that are
1978c 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f not pointing to
1978d 20 69 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 indices..**.**
1978e 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 See also OpenWri
1978f 74 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 te..*/./* Opcode
19790 3a 20 4f 70 65 6e 57 72 69 74 65 20 50 31 20 50 : OpenWrite P1 P
19791 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
19792 20 4f 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 Open a read/wri
19793 74 65 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 te cursor named
19794 50 31 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 P1 on the table
19795 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 or index whose r
19796 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 oot.** page is P
19797 32 2e 20 20 4f 72 20 69 66 20 50 35 21 3d 30 20 2. Or if P5!=0
19798 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 use the content
19799 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20 74 of register P2 t
1979a 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f o find the.** ro
1979b 6f 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 ot page..**.** T
1979c 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20 61 he P4 value is a
1979d 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 pointer to a Ke
1979e 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 yInfo structure
1979f 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 that defines the
197a0 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 .** content and
197a1 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
197a2 63 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 ce of indices.
197a3 50 34 20 69 73 20 4e 55 4c 4c 20 66 6f 72 20 63 P4 is NULL for c
197a4 75 72 73 6f 72 73 0a 2a 2a 20 74 68 61 74 20 61 ursors.** that a
197a5 72 65 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 re not pointing
197a6 74 6f 20 69 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a to indices..**.*
197a7 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 * This instructi
197a8 6f 6e 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 on works just li
197a9 6b 65 20 4f 70 65 6e 52 65 61 64 20 65 78 63 65 ke OpenRead exce
197aa 70 74 20 74 68 61 74 20 69 74 20 6f 70 65 6e 73 pt that it opens
197ab 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 the cursor.** i
197ac 6e 20 72 65 61 64 2f 77 72 69 74 65 20 6d 6f 64 n read/write mod
197ad 65 2e 20 20 46 6f 72 20 61 20 67 69 76 65 6e 20 e. For a given
197ae 74 61 62 6c 65 2c 20 74 68 65 72 65 20 63 61 6e table, there can
197af 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 be one or more
197b0 72 65 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 read-only.** cur
197b1 73 6f 72 73 20 6f 72 20 61 20 73 69 6e 67 6c 65 sors or a single
197b2 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 read/write curs
197b3 6f 72 20 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e or but not both.
197b4 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
197b5 4f 70 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 OpenRead..*/.cas
197b6 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 e OP_OpenRead:.c
197b7 61 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 ase OP_OpenWrite
197b8 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f : {. int i = pO
197b9 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70 32 20 p->p1;. int p2
197ba 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74 = pOp->p2;. int
197bb 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a iDb = pOp->p3;.
197bc 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 int wrFlag;.
197bd 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 64 62 Btree *pX;. Vdb
197be 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 eCursor *pCur;.
197bf 20 44 62 20 2a 70 44 62 3b 0a 20 20 0a 20 20 61 Db *pDb;. . a
197c0 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 ssert( iDb>=0 &&
197c1 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a iDb<db->nDb );.
197c2 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
197c3 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 reeMask & (1<<iD
197c4 62 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 b))!=0 );. pDb
197c5 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b = &db->aDb[iDb];
197c6 0a 20 20 70 58 20 3d 20 70 44 62 2d 3e 70 42 74 . pX = pDb->pBt
197c7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 58 21 3d ;. assert( pX!=
197c8 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 0 );. if( pOp->
197c9 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 opcode==OP_OpenW
197ca 72 69 74 65 20 29 7b 0a 20 20 20 20 77 72 46 6c rite ){. wrFl
197cb 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 ag = 1;. if(
197cc 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 pDb->pSchema->fi
197cd 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d le_format < p->m
197ce 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 inWriteFileForma
197cf 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 t ){. p->mi
197d0 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 nWriteFileFormat
197d1 20 3d 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d = pDb->pSchema-
197d2 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 >file_format;.
197d3 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
197d4 20 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d wrFlag = 0;. }
197d5 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 . if( pOp->p5 )
197d6 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 32 {. assert( p2
197d7 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 >0 );. assert
197d8 28 20 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ( p2<=p->nMem );
197d9 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e . pIn2 = &p->
197da 61 4d 65 6d 5b 70 32 5d 3b 0a 20 20 20 20 73 71 aMem[p2];. sq
197db 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 lite3VdbeMemInte
197dc 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 gerify(pIn2);.
197dd 20 20 70 32 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 p2 = pIn2->u.i
197de 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 32 ;. assert( p2
197df 3e 3d 32 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 >=2 );. }. ass
197e0 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 ert( i>=0 );. p
197e1 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 Cur = allocateCu
197e2 72 73 6f 72 28 70 2c 20 69 2c 20 26 70 4f 70 5b rsor(p, i, &pOp[
197e3 2d 31 5d 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20 -1], iDb, 1);.
197e4 69 66 28 20 70 43 75 72 3d 3d 30 20 29 20 67 6f if( pCur==0 ) go
197e5 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 75 to no_mem;. pCu
197e6 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a r->nullRow = 1;.
197e7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
197e8 72 65 65 43 75 72 73 6f 72 28 70 58 2c 20 70 32 reeCursor(pX, p2
197e9 2c 20 77 72 46 6c 61 67 2c 20 70 4f 70 2d 3e 70 , wrFlag, pOp->p
197ea 34 2e 70 2c 20 70 43 75 72 2d 3e 70 43 75 72 73 4.p, pCur->pCurs
197eb 6f 72 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e or);. if( pOp->
197ec 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e p4type==P4_KEYIN
197ed 46 4f 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e FO ){. pCur->
197ee 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e pKeyInfo = pOp->
197ef 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 p4.pKeyInfo;.
197f0 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d pCur->pKeyInfo-
197f1 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 >enc = ENC(p->db
197f2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
197f3 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d pCur->pKeyInfo =
197f4 20 30 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 0;. }. switch
197f5 28 20 72 63 20 29 7b 0a 20 20 20 20 63 61 73 65 ( rc ){. case
197f6 20 53 51 4c 49 54 45 5f 42 55 53 59 3a 20 7b 0a SQLITE_BUSY: {.
197f7 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 p->pc = pc
197f8 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 ;. p->rc =
197f9 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
197fa 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 ;. goto vdb
197fb 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a e_return;. }.
197fc 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
197fd 4f 4b 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 OK: {. int
197fe 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 42 flags = sqlite3B
197ff 74 72 65 65 46 6c 61 67 73 28 70 43 75 72 2d 3e treeFlags(pCur->
19800 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 pCursor);.
19801 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 /* Sanity checki
19802 6e 67 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6c 6f ng. Only the lo
19803 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f 66 wer four bits of
19804 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 the flags byte
19805 73 68 6f 75 6c 64 0a 20 20 20 20 20 20 2a 2a 20 should. **
19806 62 65 20 75 73 65 64 2e 20 20 42 69 74 20 33 20 be used. Bit 3
19807 28 6d 61 73 6b 20 30 78 30 38 29 20 69 73 20 75 (mask 0x08) is u
19808 6e 70 72 65 64 69 63 74 61 62 6c 65 2e 20 20 54 npredictable. T
19809 68 65 20 6c 6f 77 65 72 20 33 20 62 69 74 73 0a he lower 3 bits.
1980a 20 20 20 20 20 20 2a 2a 20 28 6d 61 73 6b 20 30 ** (mask 0
1980b 78 30 37 29 20 73 68 6f 75 6c 64 20 62 65 20 65 x07) should be e
1980c 69 74 68 65 72 20 35 20 28 69 6e 74 6b 65 79 2b ither 5 (intkey+
1980d 6c 65 61 66 64 61 74 61 20 66 6f 72 20 74 61 62 leafdata for tab
1980e 6c 65 73 29 20 6f 72 0a 20 20 20 20 20 20 2a 2a les) or. **
1980f 20 32 20 28 7a 65 72 6f 64 61 74 61 20 66 6f 72 2 (zerodata for
19810 20 69 6e 64 69 63 65 73 29 2e 20 20 49 66 20 74 indices). If t
19811 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 hese conditions
19812 61 72 65 20 6e 6f 74 20 6d 65 74 20 69 74 20 63 are not met it c
19813 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 6c 79 an. ** only
19814 20 6d 65 61 6e 20 74 68 61 74 20 77 65 20 61 72 mean that we ar
19815 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 e dealing with a
19816 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
19817 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2f 0a e file. */.
19818 20 20 20 20 20 20 69 66 28 20 28 66 6c 61 67 73 if( (flags
19819 20 26 20 30 78 66 30 29 21 3d 30 20 7c 7c 20 28 & 0xf0)!=0 || (
1981a 28 66 6c 61 67 73 20 26 20 30 78 30 37 29 21 3d (flags & 0x07)!=
1981b 35 20 26 26 20 28 66 6c 61 67 73 20 26 20 30 78 5 && (flags & 0x
1981c 30 37 29 21 3d 32 29 20 29 7b 0a 20 20 20 20 20 07)!=2) ){.
1981d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
1981e 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1981f 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f goto abort_
19820 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
19821 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 72 }. pCur
19822 2d 3e 69 73 54 61 62 6c 65 20 3d 20 28 66 6c 61 ->isTable = (fla
19823 67 73 20 26 20 42 54 52 45 45 5f 49 4e 54 4b 45 gs & BTREE_INTKE
19824 59 29 21 3d 30 3b 0a 20 20 20 20 20 20 70 43 75 Y)!=0;. pCu
19825 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20 28 66 6c r->isIndex = (fl
19826 61 67 73 20 26 20 42 54 52 45 45 5f 5a 45 52 4f ags & BTREE_ZERO
19827 44 41 54 41 29 21 3d 30 3b 0a 20 20 20 20 20 20 DATA)!=0;.
19828 2f 2a 20 49 66 20 50 34 3d 3d 30 20 69 74 20 6d /* If P4==0 it m
19829 65 61 6e 73 20 77 65 20 61 72 65 20 65 78 70 65 eans we are expe
1982a 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74 cted to open a t
1982b 61 62 6c 65 2e 20 20 49 66 20 50 34 21 3d 30 20 able. If P4!=0
1982c 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65 then. ** we
1982d 20 65 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70 expect to be op
1982e 65 6e 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20 ening an index.
1982f 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 If this is not
19830 77 68 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20 what happened,.
19831 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 ** then the
19832 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 database is cor
19833 72 75 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 rupt. */.
19834 20 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69 if( (pCur->i
19835 73 54 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70 sTable && pOp->p
19836 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4type==P4_KEYINF
19837 4f 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43 O). || (pC
19838 75 72 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70 ur->isIndex && p
19839 4f 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b Op->p4type!=P4_K
1983a 45 59 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20 EYINFO) ){.
1983b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
1983c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1983d 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f goto abort_
1983e 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1983f 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
19840 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
19841 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20 e SQLITE_EMPTY:
19842 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 {. pCur->is
19843 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 Table = pOp->p4t
19844 79 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b ype!=P4_KEYINFO;
19845 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 . pCur->isI
19846 6e 64 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73 ndex = !pCur->is
19847 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 Table;. pCu
19848 72 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a r->pCursor = 0;.
19849 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1984a 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 E_OK;. brea
1984b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
1984c 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f ault: {. go
1984d 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1984e 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d error;. }. }
1984f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
19850 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 Opcode: OpenEphe
19851 6d 65 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 meral P1 P2 * P4
19852 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 *.**.** Open a
19853 6e 65 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f new cursor P1 to
19854 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 a transient tab
19855 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f le..** The curso
19856 72 20 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e r is always open
19857 65 64 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 ed read/write ev
19858 65 6e 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 en if .** the ma
19859 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72 in database is r
1985a 65 61 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 ead-only. The t
1985b 72 61 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 ransient or virt
1985c 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 ual.** table is
1985d 64 65 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 deleted automati
1985e 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 cally when the c
1985f 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e ursor is closed.
19860 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 .**.** P2 is the
19861 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
19862 6e 73 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 ns in the virtua
19863 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 l table..** The
19864 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f cursor points to
19865 20 61 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 a BTree table i
19866 66 20 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 f P4==0 and to a
19867 20 42 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 BTree index.**
19868 69 66 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 if P4 is not 0.
19869 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 If P4 is not NU
1986a 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f LL, it points to
1986b 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
1986c 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 ture.** that def
1986d 69 6e 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 ines the format
1986e 6f 66 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 of keys in the i
1986f 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ndex..**.** This
19870 20 6f 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 opcode was once
19871 20 63 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 called OpenTemp
19872 2e 20 20 42 75 74 20 74 68 61 74 20 63 72 65 61 . But that crea
19873 74 65 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e ted.** confusion
19874 20 62 65 63 61 75 73 65 20 74 68 65 20 74 65 72 because the ter
19875 6d 20 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 m "temp table",
19876 6d 69 67 68 74 20 72 65 66 65 72 20 65 69 74 68 might refer eith
19877 65 72 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 er.** to a TEMP
19878 74 61 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c table at the SQL
19879 20 6c 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 level, or to a
1987a 74 61 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a table opened by.
1987b 2a 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 ** this opcode.
1987c 20 54 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 Then this opcod
1987d 65 20 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 e was call OpenV
1987e 69 72 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 irtual. But.**
1987f 74 68 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e that created con
19880 66 75 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 fusion with the
19881 77 68 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 whole virtual-ta
19882 62 6c 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 ble idea..*/.cas
19883 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 e OP_OpenEphemer
19884 61 6c 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 al: {. int i =
19885 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 pOp->p1;. VdbeC
19886 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 73 74 ursor *pCx;. st
19887 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f atic const int o
19888 70 65 6e 46 6c 61 67 73 20 3d 20 0a 20 20 20 20 penFlags = .
19889 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 SQLITE_OPEN_RE
1988a 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 ADWRITE |.
1988b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
1988c 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 TE |. SQLIT
1988d 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
1988e 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f |. SQLITE_
1988f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
19890 53 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 SE |. SQLIT
19891 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 E_OPEN_TRANSIENT
19892 5f 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 _DB;.. assert(
19893 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 i>=0 );. pCx =
19894 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 allocateCursor(p
19895 2c 20 69 2c 20 70 4f 70 2c 20 2d 31 2c 20 31 29 , i, pOp, -1, 1)
19896 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 ;. if( pCx==0 )
19897 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
19898 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 pCx->nullRow = 1
19899 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1989a 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c BtreeFactory(db,
1989b 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 0, 1, SQLITE_DE
1989c 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 FAULT_TEMP_CACHE
1989d 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 _SIZE, openFlags
1989e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1989f 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 43 &pC
198a0 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 x->pBt);. if( r
198a1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
198a2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
198a3 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 BtreeBeginTrans(
198a4 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a 20 20 pCx->pBt, 1);.
198a5 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
198a6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 TE_OK ){. /*
198a7 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 69 If a transient i
198a8 6e 64 65 78 20 69 73 20 72 65 71 75 69 72 65 64 ndex is required
198a9 2c 20 63 72 65 61 74 65 20 69 74 20 62 79 20 63 , create it by c
198aa 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 73 71 alling. ** sq
198ab 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 lite3BtreeCreate
198ac 54 61 62 6c 65 28 29 20 77 69 74 68 20 74 68 65 Table() with the
198ad 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 BTREE_ZERODATA
198ae 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 20 20 flag before.
198af 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20 49 ** opening it. I
198b0 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 f a transient ta
198b1 62 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 2c ble is required,
198b2 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a 20 20 just use the.
198b3 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c ** automatical
198b4 6c 79 20 63 72 65 61 74 65 64 20 74 61 62 6c 65 ly created table
198b5 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 with root-page
198b6 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 1 (an INTKEY tab
198b7 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 le).. */.
198b8 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 if( pOp->p4.pKey
198b9 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 69 6e Info ){. in
198ba 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 61 73 t pgno;. as
198bb 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 sert( pOp->p4typ
198bc 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 3b e==P4_KEYINFO );
198bd 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
198be 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 te3BtreeCreateTa
198bf 62 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20 26 70 ble(pCx->pBt, &p
198c0 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 gno, BTREE_ZEROD
198c1 41 54 41 29 3b 20 0a 20 20 20 20 20 20 69 66 28 ATA); . if(
198c2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
198c3 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
198c4 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 ( pgno==MASTER_R
198c5 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 OOT+1 );.
198c6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
198c7 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 42 eeCursor(pCx->pB
198c8 74 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20 20 20 t, pgno, 1, .
198c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
198ca 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4b 65 (Ke
198cb 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a yInfo*)pOp->p4.z
198cc 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b , pCx->pCursor);
198cd 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e 70 4b . pCx->pK
198ce 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 eyInfo = pOp->p4
198cf 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 .pKeyInfo;.
198d0 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f pCx->pKeyInfo
198d1 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 ->enc = ENC(p->d
198d2 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 b);. }.
198d3 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d pCx->isTable =
198d4 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
198d5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
198d6 33 42 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 3BtreeCursor(pCx
198d7 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f ->pBt, MASTER_RO
198d8 4f 54 2c 20 31 2c 20 30 2c 20 70 43 78 2d 3e 70 OT, 1, 0, pCx->p
198d9 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 70 Cursor);. p
198da 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b Cx->isTable = 1;
198db 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 78 . }. }. pCx
198dc 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 43 78 ->isIndex = !pCx
198dd 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 ->isTable;. bre
198de 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
198df 3a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 31 20 : OpenPseudo P1
198e0 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f P2 * * *.**.** O
198e1 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 pen a new cursor
198e2 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 that points to
198e3 61 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61 a fake table tha
198e4 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e t contains a sin
198e5 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 gle.** row of da
198e6 74 61 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 ta. Any attempt
198e7 20 74 6f 20 77 72 69 74 65 20 61 20 73 65 63 6f to write a seco
198e8 6e 64 20 72 6f 77 20 6f 66 20 64 61 74 61 20 63 nd row of data c
198e9 61 75 73 65 73 20 74 68 65 0a 2a 2a 20 66 69 72 auses the.** fir
198ea 73 74 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c st row to be del
198eb 65 74 65 64 2e 20 20 41 6c 6c 20 64 61 74 61 20 eted. All data
198ec 69 73 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 is deleted when
198ed 74 68 65 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a the cursor is.**
198ee 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 closed..**.** A
198ef 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 72 pseudo-table cr
198f0 65 61 74 65 64 20 62 79 20 74 68 69 73 20 6f 70 eated by this op
198f1 63 6f 64 65 20 69 73 20 75 73 65 66 75 6c 20 66 code is useful f
198f2 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a or holding the.*
198f3 2a 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 * NEW or OLD tab
198f4 6c 65 73 20 69 6e 20 61 20 74 72 69 67 67 65 72 les in a trigger
198f5 2e 20 20 41 6c 73 6f 20 75 73 65 64 20 74 6f 20 . Also used to
198f6 68 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67 6c hold the a singl
198f7 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74 20 e.** row output
198f8 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20 from the sorter
198f9 73 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77 20 so that the row
198fa 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73 65 can be decompose
198fb 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 d into.** indivi
198fc 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69 dual columns usi
198fd 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e ng the OP_Column
198fe 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 opcode..**.** W
198ff 68 65 6e 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 hen OP_Insert is
19900 20 65 78 65 63 75 74 65 64 20 74 6f 20 69 6e 73 executed to ins
19901 65 72 74 20 61 20 72 6f 77 20 69 6e 20 74 6f 20 ert a row in to
19902 74 68 65 20 70 73 65 75 64 6f 20 74 61 62 6c 65 the pseudo table
19903 2c 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d ,.** the pseudo-
19904 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6d 61 79 table cursor may
19905 20 6f 72 20 6d 61 79 20 6e 6f 74 20 6d 61 6b 65 or may not make
19906 20 69 74 27 73 20 6f 77 6e 20 63 6f 70 79 20 6f it's own copy o
19907 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 f the.** origina
19908 6c 20 72 6f 77 20 64 61 74 61 2e 20 49 66 20 50 l row data. If P
19909 32 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 2 is 0, then the
1990a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 77 69 pseudo-table wi
1990b 6c 6c 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 6f ll copy the.** o
1990c 72 69 67 69 6e 61 6c 20 72 6f 77 20 64 61 74 61 riginal row data
1990d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70 . Otherwise, a p
1990e 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 72 ointer to the or
1990f 69 67 69 6e 61 6c 20 6d 65 6d 6f 72 79 20 63 65 iginal memory ce
19910 6c 6c 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e ll.** is stored.
19911 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 In this case, t
19912 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 he vdbe program
19913 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 must ensure that
19914 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 the .** memory
19915 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 cell containing
19916 74 68 65 20 72 6f 77 20 64 61 74 61 20 69 73 20 the row data is
19917 6e 6f 74 20 6f 76 65 72 77 72 69 74 74 65 6e 20 not overwritten
19918 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 70 73 65 until the.** pse
19919 75 64 6f 20 74 61 62 6c 65 20 69 73 20 63 6c 6f udo table is clo
1991a 73 65 64 20 28 6f 72 20 61 20 6e 65 77 20 72 6f sed (or a new ro
1991b 77 20 69 73 20 69 6e 73 65 72 74 65 64 20 69 6e w is inserted in
1991c 74 6f 20 69 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 to it)..*/.case
1991d 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b OP_OpenPseudo: {
1991e 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
1991f 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p1;. VdbeCursor
19920 20 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28 *pCx;. assert(
19921 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d i>=0 );. pCx =
19922 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 allocateCursor(
19923 70 2c 20 69 2c 20 26 70 4f 70 5b 2d 31 5d 2c 20 p, i, &pOp[-1],
19924 2d 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 -1, 0);. if( pC
19925 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d x==0 ) goto no_m
19926 65 6d 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 em;. pCx->nullR
19927 6f 77 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 70 ow = 1;. pCx->p
19928 73 65 75 64 6f 54 61 62 6c 65 20 3d 20 31 3b 0a seudoTable = 1;.
19929 20 20 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 pCx->ephemPseu
1992a 64 6f 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 doTable = pOp->p
1992b 32 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 2;. pCx->isTabl
1992c 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 69 73 e = 1;. pCx->is
1992d 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65 Index = 0;. bre
1992e 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1992f 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a : Close P1 * * *
19930 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 *.**.** Close a
19931 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 cursor previous
19932 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e ly opened as P1.
19933 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a If P1 is not.*
19934 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e * currently open
19935 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 , this instructi
19936 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
19937 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a /.case OP_Close:
19938 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 {. int i = pOp
19939 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 ->p1;. assert(
1993a 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1993b 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 rsor );. sqlite
1993c 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 3VdbeFreeCursor(
1993d 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 3b p, p->apCsr[i]);
1993e 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d . p->apCsr[i] =
1993f 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0;. break;.}..
19940 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 47 /* Opcode: MoveG
19941 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a e P1 P2 P3 P4 *.
19942 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 **.** If cursor
19943 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 P1 refers to an
19944 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 SQL table (B-Tre
19945 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 e that uses inte
19946 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 ger keys), .** u
19947 73 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 se the integer v
19948 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
19949 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 P3 as a key. If
1994a 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 cursor P1 refer
1994b 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 s .** to an SQL
1994c 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 index, then P3 i
1994d 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 s the first in a
1994e 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 n array of P4 re
1994f 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 gisters .** that
19950 20 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 are used as an
19951 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b unpacked index k
19952 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 ey. .**.** Repos
19953 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 ition cursor P1
19954 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e so that it poin
19955 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 ts to the smalle
19956 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a st entry that .*
19957 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 * is greater tha
19958 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 n or equal to th
19959 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 e key value. If
1995a 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 there are no rec
1995b 6f 72 64 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 ords .** greater
1995c 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
1995d 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 o the key and P2
1995e 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 is not zero, th
1995f 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a en jump to P2..*
19960 2a 0a 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 66 *.** A special f
19961 65 61 74 75 72 65 20 6f 66 20 74 68 69 73 20 6f eature of this o
19962 70 63 6f 64 65 20 28 61 6e 64 20 64 69 66 66 65 pcode (and diffe
19963 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a rent from the.**
19964 20 72 65 6c 61 74 65 64 20 4f 50 5f 4d 6f 76 65 related OP_Move
19965 47 74 2c 20 4f 50 5f 4d 6f 76 65 4c 74 2c 20 61 Gt, OP_MoveLt, a
19966 6e 64 20 4f 50 5f 4d 6f 76 65 4c 65 29 20 69 73 nd OP_MoveLe) is
19967 20 74 68 61 74 20 69 66 20 50 32 20 69 73 0a 2a that if P2 is.*
19968 2a 20 7a 65 72 6f 20 61 6e 64 20 50 31 20 69 73 * zero and P1 is
19969 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 61 an SQL table (a
1996a 20 62 2d 74 72 65 65 20 77 69 74 68 20 69 6e 74 b-tree with int
1996b 65 67 65 72 20 6b 65 79 73 29 20 74 68 65 6e 0a eger keys) then.
1996c 2a 2a 20 74 68 65 20 73 65 65 6b 20 69 73 20 64 ** the seek is d
1996d 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 69 74 eferred until it
1996e 20 69 73 20 61 63 74 75 61 6c 6c 79 20 6e 65 65 is actually nee
1996f 64 65 64 2e 20 20 49 74 20 6d 69 67 68 74 20 62 ded. It might b
19970 65 0a 2a 2a 20 74 68 65 20 63 61 73 65 20 74 68 e.** the case th
19971 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 at the cursor is
19972 20 6e 65 76 65 72 20 61 63 63 65 73 73 65 64 2e never accessed.
19973 20 20 42 79 20 64 65 66 65 72 72 69 6e 67 20 74 By deferring t
19974 68 65 0a 2a 2a 20 73 65 65 6b 2c 20 77 65 20 61 he.** seek, we a
19975 76 6f 69 64 20 75 6e 6e 65 63 65 73 73 61 72 79 void unnecessary
19976 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 seeks..**.** Se
19977 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e e also: Found, N
19978 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 otFound, Distinc
19979 74 2c 20 4d 6f 76 65 4c 74 2c 20 4d 6f 76 65 47 t, MoveLt, MoveG
1997a 74 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 t, MoveLe.*/./*
1997b 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 47 74 20 50 Opcode: MoveGt P
1997c 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1997d 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 ** If cursor P1
1997e 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c refers to an SQL
1997f 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 table (B-Tree t
19980 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 hat uses integer
19981 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 keys), .** use
19982 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 the integer valu
19983 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 e in register P3
19984 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 as a key. If cu
19985 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a rsor P1 refers .
19986 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 ** to an SQL ind
19987 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 ex, then P3 is t
19988 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 he first in an a
19989 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 rray of P4 regis
1998a 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 ters .** that ar
1998b 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 e used as an unp
1998c 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e acked index key.
1998d 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 .**.** Repositi
1998e 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 on cursor P1 so
1998f 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 that it points
19990 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 to the smallest
19991 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 entry that .** i
19992 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 s greater than t
19993 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 he key value. If
19994 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 there are no re
19995 63 6f 72 64 73 20 67 72 65 61 74 65 72 20 74 68 cords greater th
19996 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 an .** the key a
19997 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 nd P2 is not zer
19998 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 o, then jump to
19999 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c P2..**.** See al
1999a 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f so: Found, NotFo
1999b 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 4d und, Distinct, M
1999c 6f 76 65 4c 74 2c 20 4d 6f 76 65 47 65 2c 20 4d oveLt, MoveGe, M
1999d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f oveLe.*/./* Opco
1999e 64 65 3a 20 4d 6f 76 65 4c 74 20 50 31 20 50 32 de: MoveLt P1 P2
1999f 20 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 P3 P4 * .**.**
199a0 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 If cursor P1 ref
199a1 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 ers to an SQL ta
199a2 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 ble (B-Tree that
199a3 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 uses integer ke
199a4 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 ys), .** use the
199a5 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 integer value i
199a6 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 n register P3 as
199a7 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f a key. If curso
199a8 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 r P1 refers .**
199a9 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c to an SQL index,
199aa 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 then P3 is the
199ab 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 first in an arra
199ac 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 y of P4 register
199ad 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 s .** that are u
199ae 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b sed as an unpack
199af 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a ed index key. .*
199b0 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 *.** Reposition
199b1 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 cursor P1 so tha
199b2 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 t it points to
199b3 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 the largest entr
199b4 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 y that .** is le
199b5 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 ss than the key
199b6 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 value. If there
199b7 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c are no records l
199b8 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 ess than .** the
199b9 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e key and P2 is n
199ba 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 ot zero, then ju
199bb 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 mp to P2..**.**
199bc 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c See also: Found,
199bd 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 NotFound, Disti
199be 6e 63 74 2c 20 4d 6f 76 65 47 74 2c 20 4d 6f 76 nct, MoveGt, Mov
199bf 65 47 65 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f eGe, MoveLe.*/./
199c0 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 4c 65 * Opcode: MoveLe
199c1 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
199c2 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 *.** If cursor P
199c3 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 1 refers to an S
199c4 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 QL table (B-Tree
199c5 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 that uses integ
199c6 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 er keys), .** us
199c7 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 e the integer va
199c8 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
199c9 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 P3 as a key. If
199ca 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 cursor P1 refers
199cb 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 .** to an SQL i
199cc 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 ndex, then P3 is
199cd 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e the first in an
199ce 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 array of P4 reg
199cf 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 isters .** that
199d0 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 are used as an u
199d1 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 npacked index ke
199d2 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 y. .**.** Reposi
199d3 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 tion cursor P1 s
199d4 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 o that it points
199d5 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 to the largest
199d6 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 entry that .** i
199d7 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
199d8 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 qual to the key
199d9 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 value. If there
199da 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a are no records .
199db 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 ** less than or
199dc 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 equal to the key
199dd 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a and P2 is not z
199de 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 ero, then jump t
199df 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 o P2..**.** See
199e0 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 also: Found, Not
199e1 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c Found, Distinct,
199e2 20 4d 6f 76 65 47 74 2c 20 4d 6f 76 65 47 65 2c MoveGt, MoveGe,
199e3 20 4d 6f 76 65 4c 74 0a 2a 2f 0a 63 61 73 65 20 MoveLt.*/.case
199e4 4f 50 5f 4d 6f 76 65 4c 74 3a 20 20 20 20 20 20 OP_MoveLt:
199e5 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 /* jump, in3
199e6 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 4c */.case OP_MoveL
199e7 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 e: /* ju
199e8 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 mp, in3 */.case
199e9 4f 50 5f 4d 6f 76 65 47 65 3a 20 20 20 20 20 20 OP_MoveGe:
199ea 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 /* jump, in3
199eb 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 47 */.case OP_MoveG
199ec 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 t: { /* ju
199ed 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 mp, in3 */. int
199ee 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 i = pOp->p1;.
199ef 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
199f0 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
199f1 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 && i<p->nCursor
199f2 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 );. pC = p->apC
199f3 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 sr[i];. assert(
199f4 20 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 pC!=0 );. if(
199f5 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 pC->pCursor!=0 )
199f6 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 6f {. int res, o
199f7 63 3b 0a 20 20 20 20 6f 63 20 3d 20 70 4f 70 2d c;. oc = pOp-
199f8 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 70 43 2d >opcode;. pC-
199f9 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 >nullRow = 0;.
199fa 20 20 69 66 28 20 70 43 2d 3e 69 73 54 61 62 6c if( pC->isTabl
199fb 65 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69 e ){. i64 i
199fc 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 Key = sqlite3Vdb
199fd 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b eIntValue(pIn3);
199fe 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e . if( pOp->
199ff 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 p2==0 ){.
19a00 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 assert( pOp->op
19a01 63 6f 64 65 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20 code==OP_MoveGe
19a02 29 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6d );. pC->m
19a03 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 69 4b ovetoTarget = iK
19a04 65 79 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e ey;. pC->
19a05 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
19a06 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 64 65 ;. pC->de
19a07 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31 ferredMoveto = 1
19a08 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
19a09 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
19a0a 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
19a0b 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 MovetoUnpacked(p
19a0c 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 C->pCursor, 0, (
19a0d 75 36 34 29 69 4b 65 79 2c 20 30 2c 20 26 72 65 u64)iKey, 0, &re
19a0e 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 s);. if( rc
19a0f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
19a10 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 goto abor
19a11 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
19a12 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 }. pC
19a13 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 69 4b ->lastRowid = iK
19a14 65 79 3b 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f ey;. pC->ro
19a15 77 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 73 widIsValid = res
19a16 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ==0;. }else{.
19a17 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 UnpackedRe
19a18 63 6f 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e cord r;. in
19a19 74 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e t nField = pOp->
19a1a 70 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 p4.i;. asse
19a1b 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d rt( pOp->p4type=
19a1c 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 =P4_INT32 );.
19a1d 20 20 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c assert( nFiel
19a1e 64 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 2e 70 d>0 );. r.p
19a1f 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b KeyInfo = pC->pK
19a20 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 72 2e eyInfo;. r.
19a21 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b nField = nField;
19a22 0a 20 20 20 20 20 20 69 66 28 20 6f 63 3d 3d 4f . if( oc==O
19a23 50 5f 4d 6f 76 65 47 74 20 7c 7c 20 6f 63 3d 3d P_MoveGt || oc==
19a24 4f 50 5f 4d 6f 76 65 4c 65 20 29 7b 0a 20 20 20 OP_MoveLe ){.
19a25 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 r.flags = U
19a26 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 3b NPACKED_INCRKEY;
19a27 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19a28 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 r.flags =
19a29 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
19a2a 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d r.aMem = &p->aM
19a2b 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 em[pOp->p3];.
19a2c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
19a2d 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
19a2e 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 ed(pC->pCursor,
19a2f 26 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b &r, 0, 0, &res);
19a30 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
19a31 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
19a32 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
19a33 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
19a34 20 20 20 7d 0a 20 20 20 20 20 20 70 43 2d 3e 72 }. pC->r
19a35 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b owidIsValid = 0;
19a36 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e 64 . }. pC->d
19a37 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
19a38 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 0;. pC->cache
19a39 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
19a3a 54 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 4c TALE;.#ifdef SQL
19a3b 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c ITE_TEST. sql
19a3c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e ite3_search_coun
19a3d 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 t++;.#endif.
19a3e 69 66 28 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 47 if( oc==OP_MoveG
19a3f 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 e || oc==OP_Move
19a40 47 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 Gt ){. if(
19a41 72 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 res<0 ){.
19a42 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
19a43 65 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73 eeNext(pC->pCurs
19a44 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 or, &res);.
19a45 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
19a46 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 E_OK ) goto abor
19a47 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
19a48 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 pC->rowi
19a49 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
19a4a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
19a4b 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 res = 0;.
19a4c 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
19a4d 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d assert( oc=
19a4e 3d 4f 50 5f 4d 6f 76 65 4c 74 20 7c 7c 20 6f 63 =OP_MoveLt || oc
19a4f 3d 3d 4f 50 5f 4d 6f 76 65 4c 65 20 29 3b 0a 20 ==OP_MoveLe );.
19a50 20 20 20 20 20 69 66 28 20 72 65 73 3e 3d 30 20 if( res>=0
19a51 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
19a52 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
19a53 69 6f 75 73 28 70 43 2d 3e 70 43 75 72 73 6f 72 ious(pC->pCursor
19a54 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 , &res);.
19a55 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
19a56 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f OK ) goto abort_
19a57 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
19a58 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 pC->rowidI
19a59 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 sValid = 0;.
19a5a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
19a5b 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 20 62 65 /* res might be
19a5c 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 75 73 negative becaus
19a5d 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 e the table is e
19a5e 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f 0a mpty. Check to.
19a5f 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69 ** see i
19a60 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 f this is the ca
19a61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 se.. */.
19a62 20 20 20 20 20 20 20 72 65 73 20 3d 20 73 71 6c res = sql
19a63 69 74 65 33 42 74 72 65 65 45 6f 66 28 70 43 2d ite3BtreeEof(pC-
19a64 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 >pCursor);.
19a65 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 }. }. ass
19a66 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 ert( pOp->p2>0 )
19a67 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
19a68 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
19a69 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 >p2 - 1;. }.
19a6a 20 7d 65 6c 73 65 20 69 66 28 20 21 70 43 2d 3e }else if( !pC->
19a6b 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 pseudoTable ){.
19a6c 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 /* This happe
19a6d 6e 73 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 ns when attempti
19a6e 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 73 ng to open the s
19a6f 71 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61 qlite3_master ta
19a70 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20 72 ble. ** for r
19a71 65 61 64 20 61 63 63 65 73 73 20 72 65 74 75 72 ead access retur
19a72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e ns SQLITE_EMPTY.
19a73 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c In this case al
19a74 77 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 6b 65 ways. ** take
19a75 20 74 68 65 20 6a 75 6d 70 20 28 73 69 6e 63 65 the jump (since
19a76 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 there are no re
19a77 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 62 cords in the tab
19a78 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 le).. */.
19a79 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
19a7a 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
19a7b 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75 ../* Opcode: Fou
19a7c 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a nd P1 P2 P3 * *.
19a7d 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 **.** Register P
19a7e 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 3 holds a blob c
19a7f 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61 onstructed by Ma
19a80 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73 keRecord. P1 is
19a81 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 an index..** If
19a82 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d an entry that m
19a83 61 74 63 68 65 73 20 74 68 65 20 76 61 6c 75 65 atches the value
19a84 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 33 20 in register p3
19a85 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 65 exists in P1 the
19a86 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e n.** jump to P2.
19a87 20 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 If the P3 valu
19a88 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 e does not match
19a89 20 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31 any entry in P1
19a8a 0a 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 .** then fall th
19a8b 72 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73 ru. The P1 curs
19a8c 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 or is left point
19a8d 69 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 ing at the match
19a8e 69 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20 ing entry.** if
19a8f 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a it exists..**.**
19a90 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
19a91 6e 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 n is used to imp
19a92 6c 65 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70 lement the IN op
19a93 65 72 61 74 6f 72 20 77 68 65 72 65 20 74 68 65 erator where the
19a94 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 .** left-hand si
19a95 64 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 de is a SELECT s
19a96 74 61 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61 tatement. P1 ma
19a97 79 20 62 65 20 61 20 74 72 75 65 20 69 6e 64 65 y be a true inde
19a98 78 2c 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20 x, or it.** may
19a99 62 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69 be a temporary i
19a9a 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 ndex that holds
19a9b 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 the results of t
19a9c 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 he SELECT.** sta
19a9d 74 65 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69 tement. This i
19a9e 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c nstruction is al
19a9f 73 6f 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 so used to imple
19aa0 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54 ment the.** DIST
19aa1 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20 INCT keyword in
19aa2 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
19aa3 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e s..**.** This in
19aa4 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 struction checks
19aa5 20 69 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e if index P1 con
19aa6 74 61 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66 tains a record f
19aa7 6f 72 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 or which .** the
19aa8 20 66 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69 first N seriali
19aa9 7a 65 64 20 76 61 6c 75 65 73 20 65 78 61 63 74 zed values exact
19aaa 6c 79 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73 ly match the N s
19aab 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 erialized values
19aac 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72 .** in the recor
19aad 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
19aae 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 , where N is the
19aaf 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
19ab0 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68 values in.** th
19ab1 65 20 50 33 20 72 65 63 6f 72 64 20 28 74 68 65 e P3 record (the
19ab2 20 50 33 20 72 65 63 6f 72 64 20 69 73 20 61 20 P3 record is a
19ab3 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 50 31 prefix of the P1
19ab4 20 72 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a record). .**.**
19ab5 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f See also: NotFo
19ab6 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e und, IsUnique, N
19ab7 6f 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f otExists.*/./* O
19ab8 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 pcode: NotFound
19ab9 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
19aba 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 68 ** Register P3 h
19abb 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 olds a blob cons
19abc 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 tructed by MakeR
19abd 65 63 6f 72 64 2e 20 20 50 31 20 69 73 0a 2a 2a ecord. P1 is.**
19abe 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 6e an index. If n
19abf 6f 20 65 6e 74 72 79 20 65 78 69 73 74 73 20 69 o entry exists i
19ac0 6e 20 50 31 20 74 68 61 74 20 6d 61 74 63 68 65 n P1 that matche
19ac1 73 20 74 68 65 20 62 6c 6f 62 20 74 68 65 6e 20 s the blob then
19ac2 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50 32 2e 20 20 jump.** to P2.
19ac3 49 66 20 61 6e 20 65 6e 74 72 79 20 64 6f 65 73 If an entry does
19ac4 20 65 78 69 73 74 69 6e 67 2c 20 66 61 6c 6c 20 existing, fall
19ac5 74 68 72 6f 75 67 68 2e 20 20 54 68 65 20 63 75 through. The cu
19ac6 72 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20 rsor is left.**
19ac7 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
19ac8 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 entry that match
19ac9 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c es..**.** See al
19aca 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 so: Found, NotEx
19acb 69 73 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a ists, IsUnique.*
19acc 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75 /.case OP_NotFou
19acd 6e 64 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d nd: /* jum
19ace 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f p, in3 */.case O
19acf 50 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20 P_Found: {
19ad0 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
19ad1 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d /. int i = pOp-
19ad2 3e 70 31 3b 0a 20 20 69 6e 74 20 61 6c 72 65 61 >p1;. int alrea
19ad3 64 79 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 dyExists = 0;.
19ad4 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
19ad5 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
19ad6 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
19ad7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
19ad8 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 pCsr[i]!=0 );.
19ad9 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 if( (pC = p->apC
19ada 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 sr[i])->pCursor!
19adb 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 =0 ){. int re
19adc 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 s;. UnpackedR
19add 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a ecord *pIdxKey;.
19ade 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
19adf 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 >isTable==0 );.
19ae0 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d assert( pIn3-
19ae1 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f >flags & MEM_Blo
19ae2 62 20 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 b );. pIdxKey
19ae3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
19ae4 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 2d 3e 70 cordUnpack(pC->p
19ae5 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e KeyInfo, pIn3->n
19ae6 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20 , pIn3->z,.
19ae7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ae9 20 61 54 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f aTempRec, sizeo
19aea 66 28 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 20 f(aTempRec));.
19aeb 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 if( pIdxKey==0
19aec 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e ){. goto n
19aed 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 o_mem;. }.
19aee 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
19aef 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 ==OP_Found ){.
19af0 20 20 20 20 70 49 64 78 4b 65 79 2d 3e 66 6c 61 pIdxKey->fla
19af1 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50 gs |= UNPACKED_P
19af2 52 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20 REFIX_MATCH;.
19af3 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
19af4 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
19af5 70 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 packed(pC->pCurs
19af6 6f 72 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20 or, pIdxKey, 0,
19af7 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 73 71 0, &res);. sq
19af8 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 lite3VdbeDeleteU
19af9 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 npackedRecord(pI
19afa 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 dxKey);. if(
19afb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
19afc 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
19afd 20 20 7d 0a 20 20 20 20 61 6c 72 65 61 64 79 45 }. alreadyE
19afe 78 69 73 74 73 20 3d 20 28 72 65 73 3d 3d 30 29 xists = (res==0)
19aff 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72 ;. pC->deferr
19b00 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 edMoveto = 0;.
19b01 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 pC->cacheStatu
19b02 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
19b03 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e . }. if( pOp->
19b04 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 opcode==OP_Found
19b05 20 29 7b 0a 20 20 20 20 69 66 28 20 61 6c 72 65 ){. if( alre
19b06 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d adyExists ) pc =
19b07 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
19b08 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21 }else{. if( !
19b09 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 alreadyExists )
19b0a 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
19b0b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
19b0c 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 ../* Opcode: IsU
19b0d 6e 69 71 75 65 20 50 31 20 50 32 20 50 33 20 50 nique P1 P2 P3 P
19b0e 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 4 *.**.** The P3
19b0f 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 register contai
19b10 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 ns an integer re
19b11 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 43 61 cord number. Ca
19b12 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 65 63 6f 72 ll this.** recor
19b13 64 20 6e 75 6d 62 65 72 20 52 2e 20 20 54 68 65 d number R. The
19b14 20 50 34 20 72 65 67 69 73 74 65 72 20 63 6f 6e P4 register con
19b15 74 61 69 6e 73 20 61 6e 20 69 6e 64 65 78 20 6b tains an index k
19b16 65 79 20 63 72 65 61 74 65 64 0a 2a 2a 20 75 73 ey created.** us
19b17 69 6e 67 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 ing MakeRecord.
19b18 20 43 61 6c 6c 20 69 74 20 4b 2e 0a 2a 2a 0a 2a Call it K..**.*
19b19 2a 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 * P1 is an index
19b1a 2e 20 20 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 . So it has no
19b1b 64 61 74 61 20 61 6e 64 20 69 74 73 20 6b 65 79 data and its key
19b1c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a consists of a.*
19b1d 2a 20 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 * record generat
19b1e 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 ed by OP_MakeRec
19b1f 6f 72 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 ord where the la
19b20 73 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 st field is the
19b21 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 .** rowid of the
19b22 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
19b23 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e index refers to.
19b24 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 69 6e 73 .** .** This ins
19b25 74 72 75 63 74 69 6f 6e 20 61 73 6b 73 20 69 66 truction asks if
19b26 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 6e 74 there is an ent
19b27 72 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 74 ry in P1 where t
19b28 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6d 61 74 he.** fields mat
19b29 63 68 65 73 20 4b 20 62 75 74 20 74 68 65 20 72 ches K but the r
19b2a 6f 77 69 64 20 69 73 20 64 69 66 66 65 72 65 6e owid is differen
19b2b 74 20 66 72 6f 6d 20 52 2e 0a 2a 2a 20 49 66 20 t from R..** If
19b2c 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 there is no such
19b2d 20 65 6e 74 72 79 2c 20 74 68 65 6e 20 74 68 65 entry, then the
19b2e 72 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61 re is an immedia
19b2f 74 65 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 te.** jump to P2
19b30 2e 20 20 49 66 20 61 6e 79 20 65 6e 74 72 79 20 . If any entry
19b31 64 6f 65 73 20 65 78 69 73 74 20 77 68 65 72 65 does exist where
19b32 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 69 6e the index strin
19b33 67 0a 2a 2a 20 6d 61 74 63 68 65 73 20 4b 20 62 g.** matches K b
19b34 75 74 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 ut the record nu
19b35 6d 62 65 72 20 69 73 20 6e 6f 74 20 52 2c 20 74 mber is not R, t
19b36 68 65 6e 20 74 68 65 20 72 65 63 6f 72 64 0a 2a hen the record.*
19b37 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 * number for tha
19b38 74 20 65 6e 74 72 79 20 69 73 20 77 72 69 74 74 t entry is writt
19b39 65 6e 20 69 6e 74 6f 20 50 33 20 61 6e 64 20 63 en into P3 and c
19b3a 6f 6e 74 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20 ontrol.** falls
19b3b 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
19b3c 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ext instruction.
19b3d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
19b3e 20 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 NotFound, NotEx
19b3f 69 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 ists, Found.*/.c
19b40 61 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a ase OP_IsUnique:
19b41 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
19b42 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 p, in3 */. int
19b43 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i = pOp->p1;. V
19b44 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a dbeCursor *pCx;.
19b45 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
19b46 72 3b 0a 20 20 4d 65 6d 20 2a 70 4b 3b 0a 20 20 r;. Mem *pK;.
19b47 69 36 34 20 52 3b 0a 0a 20 20 2f 2a 20 50 6f 70 i64 R;.. /* Pop
19b48 20 74 68 65 20 76 61 6c 75 65 20 52 20 6f 66 66 the value R off
19b49 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 the top of the
19b4a 73 74 61 63 6b 0a 20 20 2a 2f 0a 20 20 61 73 73 stack. */. ass
19b4b 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
19b4c 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 ==P4_INT32 );.
19b4d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e assert( pOp->p4.
19b4e 69 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 i>0 && pOp->p4.i
19b4f 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
19b50 4b 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 K = &p->aMem[pOp
19b51 2d 3e 70 34 2e 69 5d 3b 0a 20 20 73 71 6c 69 74 ->p4.i];. sqlit
19b52 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
19b53 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 52 20 3d ify(pIn3);. R =
19b54 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 61 73 pIn3->u.i;. as
19b55 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
19b56 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
19b57 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 pCx = p->apCsr[i
19b58 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 78 ];. assert( pCx
19b59 21 3d 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d !=0 );. pCrsr =
19b5a 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 pCx->pCursor;.
19b5b 20 69 66 28 20 70 43 72 73 72 21 3d 30 20 29 7b if( pCrsr!=0 ){
19b5c 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 . int res;.
19b5d 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 i64 v;
19b5e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19b5f 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 The record numbe
19b60 72 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 4b r that matches K
19b61 20 2a 2f 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 */. Unpacked
19b62 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b Record *pIdxKey;
19b63 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 76 /* Unpacked v
19b64 65 72 73 69 6f 6e 20 6f 66 20 50 34 20 2a 2f 0a ersion of P4 */.
19b65 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 . /* Make sur
19b66 65 20 4b 20 69 73 20 61 20 73 74 72 69 6e 67 20 e K is a string
19b67 61 6e 64 20 6d 61 6b 65 20 7a 4b 65 79 20 70 6f and make zKey po
19b68 69 6e 74 20 74 6f 20 4b 0a 20 20 20 20 2a 2f 0a int to K. */.
19b69 20 20 20 20 61 73 73 65 72 74 28 20 70 4b 2d 3e assert( pK->
19b6a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 flags & MEM_Blob
19b6b 20 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 );. pIdxKey
19b6c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 = sqlite3VdbeRec
19b6d 6f 72 64 55 6e 70 61 63 6b 28 70 43 78 2d 3e 70 ordUnpack(pCx->p
19b6e 4b 65 79 49 6e 66 6f 2c 20 70 4b 2d 3e 6e 2c 20 KeyInfo, pK->n,
19b6f 70 4b 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 pK->z,.
19b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19b71 20 20 20 20 20 20 20 20 20 20 20 20 20 61 54 65 aTe
19b72 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 61 54 mpRec, sizeof(aT
19b73 65 6d 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 empRec));. if
19b74 28 20 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a ( pIdxKey==0 ){.
19b75 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 goto no_me
19b76 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64 m;. }. pId
19b77 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 xKey->flags |= U
19b78 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 NPACKED_IGNORE_R
19b79 4f 57 49 44 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 OWID;.. /* Se
19b7a 61 72 63 68 20 66 6f 72 20 61 6e 20 65 6e 74 72 arch for an entr
19b7b 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 61 6c y in P1 where al
19b7c 6c 20 62 75 74 20 74 68 65 20 6c 61 73 74 20 72 l but the last r
19b7d 6f 77 69 64 20 6d 61 74 63 68 20 4b 0a 20 20 20 owid match K.
19b7e 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 ** If there is
19b7f 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 6a no such entry, j
19b80 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ump immediately
19b81 74 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 20 to P2.. */.
19b82 20 20 61 73 73 65 72 74 28 20 70 43 78 2d 3e 64 assert( pCx->d
19b83 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 eferredMoveto==0
19b84 20 29 3b 0a 20 20 20 20 70 43 78 2d 3e 63 61 63 );. pCx->cac
19b85 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
19b86 5f 53 54 41 4c 45 3b 0a 20 20 20 20 72 63 20 3d _STALE;. rc =
19b87 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
19b88 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 72 73 etoUnpacked(pCrs
19b89 72 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 r, pIdxKey, 0, 0
19b8a 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 , &res);. if(
19b8b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
19b8c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
19b8d 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 dbeDeleteUnpacke
19b8e 64 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 dRecord(pIdxKey)
19b8f 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f ;. goto abo
19b90 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
19b91 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
19b92 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 es<0 ){. rc
19b93 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e = sqlite3BtreeN
19b94 65 78 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 ext(pCrsr, &res)
19b95 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 20 ;. if( res
19b96 29 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 ){. pc =
19b97 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
19b98 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
19b99 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 DeleteUnpackedRe
19b9a 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 cord(pIdxKey);.
19b9b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
19b9c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
19b9d 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
19b9e 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 70 43 IdxKeyCompare(pC
19b9f 78 2c 20 70 49 64 78 4b 65 79 2c 20 26 72 65 73 x, pIdxKey, &res
19ba0 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ); . sqlite3V
19ba1 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 dbeDeleteUnpacke
19ba2 64 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 dRecord(pIdxKey)
19ba3 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
19ba4 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 LITE_OK ) goto a
19ba5 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
19ba6 72 3b 0a 20 20 20 20 69 66 28 20 72 65 73 3e 30 r;. if( res>0
19ba7 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 ){. pc = p
19ba8 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
19ba9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a break;. }..
19baa 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 /* At this p
19bab 6f 69 6e 74 2c 20 70 43 72 73 72 20 69 73 20 70 oint, pCrsr is p
19bac 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e 20 65 6e ointing to an en
19bad 74 72 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 try in P1 where
19bae 61 6c 6c 20 62 75 74 0a 20 20 20 20 2a 2a 20 74 all but. ** t
19baf 68 65 20 66 69 6e 61 6c 20 65 6e 74 72 79 20 28 he final entry (
19bb0 74 68 65 20 72 6f 77 69 64 29 20 6d 61 74 63 68 the rowid) match
19bb1 65 73 20 4b 2e 20 20 43 68 65 63 6b 20 74 6f 20 es K. Check to
19bb2 73 65 65 20 69 66 20 74 68 65 0a 20 20 20 20 2a see if the. *
19bb3 2a 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 63 6f * final rowid co
19bb4 6c 75 6d 6e 20 69 73 20 64 69 66 66 65 72 65 6e lumn is differen
19bb5 74 20 66 72 6f 6d 20 52 2e 20 20 49 66 20 69 74 t from R. If it
19bb6 20 65 71 75 61 6c 73 20 52 20 74 68 65 6e 20 6a equals R then j
19bb7 75 6d 70 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 ump. ** immed
19bb8 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 20 20 iately to P2..
19bb9 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
19bba 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 lite3VdbeIdxRowi
19bbb 64 28 70 43 72 73 72 2c 20 26 76 29 3b 0a 20 20 d(pCrsr, &v);.
19bbc 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
19bbd 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 _OK ){. got
19bbe 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
19bbf 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rror;. }.
19bc0 69 66 28 20 76 3d 3d 52 20 29 7b 0a 20 20 20 20 if( v==R ){.
19bc1 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
19bc2 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 1;. break;
19bc3 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 . }.. /* T
19bc4 68 65 20 66 69 6e 61 6c 20 76 61 72 69 6e 74 20 he final varint
19bc5 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 64 69 of the key is di
19bc6 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 52 2e 20 fferent from R.
19bc7 20 53 74 6f 72 65 20 69 74 20 62 61 63 6b 0a 20 Store it back.
19bc8 20 20 20 2a 2a 20 69 6e 74 6f 20 72 65 67 69 73 ** into regis
19bc9 74 65 72 20 52 33 2e 20 20 28 54 68 65 20 72 65 ter R3. (The re
19bca 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 61 cord number of a
19bcb 6e 20 65 6e 74 72 79 20 74 68 61 74 20 76 69 6f n entry that vio
19bcc 6c 61 74 65 73 0a 20 20 20 20 2a 2a 20 61 20 55 lates. ** a U
19bcd 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 NIQUE constraint
19bce 2e 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 .). */. pI
19bcf 6e 33 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 n3->u.i = v;.
19bd0 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 assert( pIn3->f
19bd1 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a lags&MEM_Int );.
19bd2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
19bd3 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 78 /* Opcode: NotEx
19bd4 69 73 74 73 20 50 31 20 50 32 20 50 33 20 2a 20 ists P1 P2 P3 *
19bd5 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 *.**.** Use the
19bd6 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 content of regis
19bd7 74 65 72 20 50 33 20 61 73 20 61 20 69 6e 74 65 ter P3 as a inte
19bd8 67 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20 72 ger key. If a r
19bd9 65 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20 74 ecord .** with t
19bda 68 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 hat key does not
19bdb 20 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65 20 exist in table
19bdc 6f 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d 70 of P1, then jump
19bdd 20 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20 74 to P2. .** If t
19bde 68 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20 65 he record does e
19bdf 78 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 xist, then fall
19be0 74 68 72 75 2e 20 20 54 68 65 20 63 75 72 73 6f thru. The curso
19be1 72 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f r is left .** po
19be2 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 65 inting to the re
19be3 63 6f 72 64 20 69 66 20 69 74 20 65 78 69 73 74 cord if it exist
19be4 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 s..**.** The dif
19be5 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 ference between
19be6 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 this operation a
19be7 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 nd NotFound is t
19be8 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 hat this.** oper
19be9 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ation assumes th
19bea 65 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65 e key is an inte
19beb 67 65 72 20 61 6e 64 20 74 68 61 74 20 50 31 20 ger and that P1
19bec 69 73 20 61 20 74 61 62 6c 65 20 77 68 65 72 65 is a table where
19bed 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 as.** NotFound a
19bee 73 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 20 ssumes key is a
19bef 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 blob constructed
19bf0 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 from MakeRecord
19bf1 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e and.** P1 is an
19bf2 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65 index..**.** Se
19bf3 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e e also: Found, N
19bf4 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 otFound, IsUniqu
19bf5 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 e.*/.case OP_Not
19bf6 45 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20 20 Exists: {
19bf7 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f /* jump, in3 */
19bf8 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
19bf9 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p1;. VdbeCursor
19bfa 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 *pC;. BtCursor
19bfb 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72 *pCrsr;. asser
19bfc 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e t( i>=0 && i<p->
19bfd 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 nCursor );. ass
19bfe 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d ert( p->apCsr[i]
19bff 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 !=0 );. if( (pC
19c00 72 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 rsr = (pC = p->a
19c01 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f pCsr[i])->pCurso
19c02 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 r)!=0 ){. int
19c03 20 72 65 73 3b 0a 20 20 20 20 75 36 34 20 69 4b res;. u64 iK
19c04 65 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ey;. assert(
19c05 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn3->flags & ME
19c06 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 M_Int );. ass
19c07 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d ert( p->apCsr[i]
19c08 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 ->isTable );.
19c09 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 iKey = intToKey
19c0a 28 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 (pIn3->u.i);.
19c0b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
19c0c 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
19c0d 28 70 43 72 73 72 2c 20 30 2c 20 69 4b 65 79 2c (pCrsr, 0, iKey,
19c0e 20 30 2c 26 72 65 73 29 3b 0a 20 20 20 20 70 43 0,&res);. pC
19c0f 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 49 ->lastRowid = pI
19c10 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 43 2d n3->u.i;. pC-
19c11 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
19c12 72 65 73 3d 3d 30 3b 0a 20 20 20 20 70 43 2d 3e res==0;. pC->
19c13 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 nullRow = 0;.
19c14 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 pC->cacheStatus
19c15 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
19c16 20 20 20 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 /* res might
19c17 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 be uninitialize
19c18 64 20 69 66 20 72 63 21 3d 53 51 4c 49 54 45 5f d if rc!=SQLITE_
19c19 4f 4b 2e 20 20 42 75 74 20 69 66 20 72 63 21 3d OK. But if rc!=
19c1a 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 2a 2a SQLITE_OK. **
19c1b 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 61 processing is a
19c1c 62 6f 75 74 20 74 6f 20 61 62 6f 72 74 20 73 6f bout to abort so
19c1d 20 77 65 20 72 65 61 6c 6c 79 20 64 6f 20 6e 6f we really do no
19c1e 74 20 63 61 72 65 20 77 68 65 74 68 65 72 20 6f t care whether o
19c1f 72 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 65 r not. ** the
19c20 20 66 6f 6c 6c 6f 77 69 6e 67 20 6a 75 6d 70 20 following jump
19c21 69 73 20 74 61 6b 65 6e 2e 20 20 28 49 6e 20 6f is taken. (In o
19c22 74 68 65 72 20 77 6f 72 64 73 2c 20 64 6f 20 6e ther words, do n
19c23 6f 74 20 73 74 72 65 73 73 20 6f 76 65 72 0a 20 ot stress over.
19c24 20 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 ** the error
19c25 74 68 61 74 20 76 61 6c 67 72 69 6e 64 20 73 6f that valgrind so
19c26 6d 65 74 69 6d 65 73 20 73 68 6f 77 73 20 6f 6e metimes shows on
19c27 20 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d the next statem
19c28 65 6e 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 ent when. **
19c29 72 75 6e 6e 69 6e 67 20 69 6f 65 72 72 2e 74 65 running ioerr.te
19c2a 73 74 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 66 st and similar f
19c2b 61 69 6c 75 72 65 2d 72 65 63 6f 76 65 72 79 20 ailure-recovery
19c2c 74 65 73 74 20 73 63 72 69 70 74 73 2e 29 20 2a test scripts.) *
19c2d 2f 0a 20 20 20 20 69 66 28 20 72 65 73 21 3d 30 /. if( res!=0
19c2e 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 ){. pc = p
19c2f 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
19c30 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 6f assert( pC->ro
19c31 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b widIsValid==0 );
19c32 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
19c33 66 28 20 21 70 43 2d 3e 70 73 65 75 64 6f 54 61 f( !pC->pseudoTa
19c34 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ble ){. /* Th
19c35 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 is happens when
19c36 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 70 an attempt to op
19c37 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 en a read cursor
19c38 20 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 on the . **
19c39 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
19c3a 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ble returns SQLI
19c3b 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f TE_EMPTY.. */
19c3c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
19c3d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
19c3e 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
19c3f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
19c40 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d ->rowidIsValid==
19c41 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 0 );. }. break
19c42 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
19c43 53 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a Sequence P1 P2 *
19c44 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 * *.**.** Find
19c45 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 the next availab
19c46 6c 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 le sequence numb
19c47 65 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 er for cursor P1
19c48 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 ..** Write the s
19c49 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 equence number i
19c4a 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e nto register P2.
19c4b 0a 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 .** The sequence
19c4c 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 number on the c
19c4d 75 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 ursor is increme
19c4e 6e 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a nted after this.
19c4f 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 ** instruction.
19c50 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 .*/.case OP_Seq
19c51 75 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 uence: {
19c52 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
19c53 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 lease */. int i
19c54 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 = pOp->p1;. as
19c55 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
19c56 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
19c57 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 assert( p->apCsr
19c58 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 [i]!=0 );. pOut
19c59 2d 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 ->u.i = p->apCsr
19c5a 5b 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b [i]->seqCount++;
19c5b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
19c5c 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
19c5d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f ;. break;.}.../
19c5e 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 * Opcode: NewRow
19c5f 69 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a id P1 P2 P3 * *.
19c60 2a 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 **.** Get a new
19c61 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e integer record n
19c62 75 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f umber (a.k.a "ro
19c63 77 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68 wid") used as th
19c64 65 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 e key to a table
19c65 2e 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 ..** The record
19c66 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 number is not pr
19c67 65 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 eviously used as
19c68 20 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 a key in the da
19c69 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 tabase.** table
19c6a 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 that cursor P1 p
19c6b 6f 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e oints to. The n
19c6c 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 ew record number
19c6d 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 is written.** w
19c6e 72 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 ritten to regist
19c6f 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P2..**.** If
19c70 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 P3>0 then P3 is
19c71 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 a register that
19c72 68 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 73 holds the larges
19c73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 t previously.**
19c74 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 generated record
19c75 20 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 77 number. No new
19c76 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 record numbers
19c77 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 are allowed to b
19c78 65 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 e less.** than t
19c79 68 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 6e his value. When
19c7a 20 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 this value reac
19c7b 68 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c hes its maximum,
19c7c 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a a SQLITE_FULL.*
19c7d 2a 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 * error is gener
19c7e 61 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 65 ated. The P3 re
19c7f 67 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65 gister is update
19c80 64 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 72 d with the gener
19c81 61 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 6e ated.** record n
19c82 75 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 20 umber. This P3
19c83 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 mechanism is use
19c84 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d d to help implem
19c85 65 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 ent the.** AUTOI
19c86 4e 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 NCREMENT feature
19c87 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 ..*/.case OP_New
19c88 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 Rowid: {
19c89 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
19c8a 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 lease */. int i
19c8b 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 = pOp->p1;. i6
19c8c 34 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 4 v = 0;. VdbeC
19c8d 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 ursor *pC;. ass
19c8e 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
19c8f 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
19c90 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b ssert( p->apCsr[
19c91 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 i]!=0 );. if( (
19c92 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
19c93 29 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b )->pCursor==0 ){
19c94 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f . /* The zero
19c95 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
19c96 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 above is all tha
19c97 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 t is needed */.
19c98 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
19c99 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 he next rowid or
19c9a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 record number (
19c9b 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 different terms
19c9c 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 for the same.
19c9d 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 ** thing) is ob
19c9e 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d tained in a two-
19c9f 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a step algorithm..
19ca0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 **. ** Fi
19ca1 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 rst we attempt t
19ca2 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 o find the large
19ca3 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 st existing rowi
19ca4 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 d and add one.
19ca5 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 ** to that. B
19ca6 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 ut if the larges
19ca7 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 t existing rowid
19ca8 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 is already the
19ca9 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 maximum. ** p
19caa 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c ositive integer,
19cab 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c we have to fall
19cac 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
19cad 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 second. ** pr
19cae 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f obabilistic algo
19caf 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 rithm. **.
19cb0 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 ** The second a
19cb1 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 lgorithm is to s
19cb2 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 elect a rowid at
19cb3 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 random and see
19cb4 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 if. ** it alr
19cb5 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 eady exists in t
19cb6 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 he table. If it
19cb7 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c does not exist,
19cb8 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 we have. **
19cb9 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 succeeded. If t
19cba 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 he random rowid
19cbb 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 does exist, we s
19cbc 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a elect a new one.
19cbd 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 ** and try a
19cbe 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30 gain, up to 1000
19cbf 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 times.. **.
19cc0 20 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c ** For a tabl
19cc1 65 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e e with less than
19cc2 20 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69 2 billion entri
19cc3 65 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c es, the probabil
19cc4 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f ity. ** of no
19cc5 74 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73 t finding a unus
19cc6 65 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75 ed rowid is abou
19cc7 74 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69 t 1.0e-300. Thi
19cc8 73 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e s is a . ** n
19cc9 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c on-zero probabil
19cca 69 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73 ity, but it is s
19ccb 74 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79 till vanishingly
19ccc 20 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c small and shoul
19ccd 64 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63 d. ** never c
19cce 61 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20 ause a problem.
19ccf 20 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d You are much, m
19cd0 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 uch more likely
19cd1 74 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a to have a. **
19cd2 20 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72 hardware failur
19cd3 65 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20 e than for this
19cd4 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69 algorithm to fai
19cd5 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a l.. **. **
19cd6 20 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e The analysis in
19cd7 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 the previous pa
19cd8 72 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20 ragraph assumes
19cd9 74 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20 that you have a
19cda 67 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72 good. ** sour
19cdb 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d ce of random num
19cdc 62 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72 bers. Is a libr
19cdd 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b ary function lik
19cde 65 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20 e lrand48().
19cdf 2a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20 ** good enough?
19ce0 20 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f Maybe. Maybe no
19ce1 74 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20 t. It's hard to
19ce2 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65 know whether the
19ce3 72 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 re. ** might
19ce4 62 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69 be subtle bugs i
19ce5 73 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 s some implement
19ce6 61 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34 ations of lrand4
19ce7 38 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 8() that. **
19ce8 63 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 could cause prob
19ce9 6c 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75 lems. To avoid u
19cea 6e 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69 ncertainty, SQLi
19ceb 74 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20 te uses its own
19cec 0a 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e . ** random n
19ced 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
19cee 62 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34 based on the RC4
19cef 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 algorithm..
19cf0 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f **. ** To pro
19cf1 6d 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66 mote locality of
19cf2 20 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72 reference for r
19cf3 65 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74 epetitive insert
19cf4 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 s, the. ** fi
19cf5 72 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73 rst few attempts
19cf6 20 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 72 at choosing a r
19cf7 61 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b andom rowid pick
19cf8 20 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c values just a l
19cf9 69 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72 ittle. ** lar
19cfa 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 ger than the pre
19cfb 76 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68 vious rowid. Th
19cfc 69 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77 is has been show
19cfd 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79 n experimentally
19cfe 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c . ** to doubl
19cff 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74 e the speed of t
19d00 68 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f he COPY operatio
19d01 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e n.. */. in
19d02 74 20 72 65 73 2c 20 72 78 3d 53 51 4c 49 54 45 t res, rx=SQLITE
19d03 5f 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 36 _OK, cnt;. i6
19d04 34 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 30 4 x;. cnt = 0
19d05 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 ;. if( (sqlit
19d06 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d e3BtreeFlags(pC-
19d07 3e 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 45 >pCursor)&(BTREE
19d08 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a 45 _INTKEY|BTREE_ZE
19d09 52 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 20 RODATA)) !=.
19d0a 20 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b BTREE_INTK
19d0b 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d EY ){. rc =
19d0c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
19d0d 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f BKPT;. goto
19d0e 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
19d0f 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 ror;. }. a
19d10 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 42 ssert( (sqlite3B
19d11 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 43 treeFlags(pC->pC
19d12 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f 49 ursor) & BTREE_I
19d13 4e 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 20 NTKEY)!=0 );.
19d14 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 assert( (sqlite
19d15 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 3BtreeFlags(pC->
19d16 70 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 pCursor) & BTREE
19d17 5f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 3b _ZERODATA)==0 );
19d18 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
19d19 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 32BIT_ROWID.#
19d1a 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 define MAX_ROWID
19d1b 20 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 0x7fffffff.#els
19d1c 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f e. /* Some co
19d1d 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e mpilers complain
19d1e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 about constants
19d1f 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 of the form 0x7
19d20 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e fffffffffffffff.
19d21 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 . ** Others c
19d22 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 omplain about 0x
19d23 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 7fffffffffffffff
19d24 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 fLL. The follow
19d25 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a ing macro seems.
19d26 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 ** to provid
19d27 65 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 e the constant w
19d28 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 hile making all
19d29 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e compilers happy.
19d2a 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 . */.# defi
19d2b 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 20 ne MAX_ROWID (
19d2c 28 28 28 75 36 34 29 30 78 37 66 66 66 66 66 66 (((u64)0x7ffffff
19d2d 66 29 3c 3c 33 32 29 20 7c 20 28 75 36 34 29 30 f)<<32) | (u64)0
19d2e 78 66 66 66 66 66 66 66 66 20 29 0a 23 65 6e 64 xffffffff ).#end
19d2f 69 66 0a 0a 20 20 20 20 69 66 28 20 21 70 43 2d if.. if( !pC-
19d30 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 >useRandomRowid
19d31 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 2d ){. if( pC-
19d32 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20 >nextRowidValid
19d33 29 7b 0a 20 20 20 20 20 20 20 20 76 20 3d 20 70 ){. v = p
19d34 43 2d 3e 6e 65 78 74 52 6f 77 69 64 3b 0a 20 20 C->nextRowid;.
19d35 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
19d36 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
19d37 74 72 65 65 4c 61 73 74 28 70 43 2d 3e 70 43 75 treeLast(pC->pCu
19d38 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 rsor, &res);.
19d39 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
19d3a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
19d3b 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
19d3c 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
19d3d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
19d3e 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 f( res ){.
19d3f 20 20 20 20 76 20 3d 20 31 3b 0a 20 20 20 20 20 v = 1;.
19d40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
19d41 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
19d42 4b 65 79 53 69 7a 65 28 70 43 2d 3e 70 43 75 72 KeySize(pC->pCur
19d43 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20 20 20 20 sor, &v);.
19d44 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74 v = keyToInt
19d45 28 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 (v);. i
19d46 66 28 20 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 f( v==MAX_ROWID
19d47 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
19d48 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 C->useRandomRowi
19d49 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 d = 1;.
19d4a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
19d4b 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20 v++;.
19d4c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
19d4d 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 }..#ifndef
19d4e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
19d4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 20 20 INCREMENT.
19d50 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 if( pOp->p3 ){.
19d51 20 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d Mem *pMem
19d52 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
19d53 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 ( pOp->p3>0 && p
19d54 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p3<=p->nMem
19d55 29 3b 20 2f 2a 20 50 33 20 69 73 20 61 20 76 61 ); /* P3 is a va
19d56 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 lid memory cell
19d57 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 20 */. pMem
19d58 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
19d59 70 33 5d 3b 0a 09 52 45 47 49 53 54 45 52 5f 54 p3];..REGISTER_T
19d5a 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4d RACE(pOp->p3, pM
19d5b 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c em);. sql
19d5c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
19d5d 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 20 erify(pMem);.
19d5e 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d assert( (pM
19d5f 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
19d60 49 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d Int)!=0 ); /* m
19d61 65 6d 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 em(P3) holds an
19d62 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 integer */.
19d63 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 75 2e 69 if( pMem->u.i
19d64 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c 20 70 ==MAX_ROWID || p
19d65 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 C->useRandomRowi
19d66 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 d ){. r
19d67 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b c = SQLITE_FULL;
19d68 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
19d69 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
19d6a 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 or;. }.
19d6b 20 20 20 20 20 20 69 66 28 20 76 3c 70 4d 65 6d if( v<pMem
19d6c 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20 20 ->u.i+1 ){.
19d6d 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 v = pMem->u
19d6e 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 .i + 1;.
19d6f 7d 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
19d70 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20 20 20 7d u.i = v;. }
19d71 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 69 .#endif.. i
19d72 66 28 20 76 3c 4d 41 58 5f 52 4f 57 49 44 20 29 f( v<MAX_ROWID )
19d73 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65 {. pC->ne
19d74 78 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 31 xtRowidValid = 1
19d75 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65 ;. pC->ne
19d76 78 74 52 6f 77 69 64 20 3d 20 76 2b 31 3b 0a 20 xtRowid = v+1;.
19d77 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
19d78 20 20 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 pC->nextRowi
19d79 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 dValid = 0;.
19d7a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
19d7b 28 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 ( pC->useRandomR
19d7c 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73 owid ){. as
19d7d 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 sert( pOp->p3==0
19d7e 20 29 3b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 ); /* SQLITE_F
19d7f 55 4c 4c 20 6d 75 73 74 20 68 61 76 65 20 6f 63 ULL must have oc
19d80 63 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 curred prior to
19d81 74 68 69 73 20 2a 2f 0a 20 20 20 20 20 20 76 20 this */. v
19d82 3d 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f = db->priorNewRo
19d83 77 69 64 3b 0a 20 20 20 20 20 20 63 6e 74 20 3d wid;. cnt =
19d84 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20 0;. do{.
19d85 20 20 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 if( cnt==0
19d86 20 26 26 20 28 76 26 30 78 66 66 66 66 66 66 29 && (v&0xffffff)
19d87 3d 3d 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==v ){.
19d88 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 v++;. }e
19d89 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 lse{. s
19d8a 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
19d8b 73 28 73 69 7a 65 6f 66 28 76 29 2c 20 26 76 29 s(sizeof(v), &v)
19d8c 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
19d8d 63 6e 74 3c 35 20 29 20 76 20 26 3d 20 30 78 66 cnt<5 ) v &= 0xf
19d8e 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 20 7d fffff;. }
19d8f 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d . if( v==
19d90 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
19d91 20 20 20 20 20 20 78 20 3d 20 69 6e 74 54 6f 4b x = intToK
19d92 65 79 28 76 29 3b 0a 20 20 20 20 20 20 20 20 72 ey(v);. r
19d93 78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 x = sqlite3Btree
19d94 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 MovetoUnpacked(p
19d95 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 C->pCursor, 0, (
19d96 75 36 34 29 78 2c 20 30 2c 20 26 72 65 73 29 3b u64)x, 0, &res);
19d97 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a . cnt++;.
19d98 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 63 6e }while( cn
19d99 74 3c 31 30 30 20 26 26 20 72 78 3d 3d 53 51 4c t<100 && rx==SQL
19d9a 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 ITE_OK && res==0
19d9b 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 72 );. db->pr
19d9c 69 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 76 3b iorNewRowid = v;
19d9d 0a 20 20 20 20 20 20 69 66 28 20 72 78 3d 3d 53 . if( rx==S
19d9e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d QLITE_OK && res=
19d9f 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 =0 ){. rc
19da0 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a = SQLITE_FULL;.
19da1 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f goto abo
19da2 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
19da3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
19da4 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 pC->rowidIsVa
19da5 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d lid = 0;. pC-
19da6 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
19da7 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 = 0;. pC->cac
19da8 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
19da9 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d 65 _STALE;. }. Me
19daa 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
19dab 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70 t, MEM_Int);. p
19dac 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 Out->u.i = v;.
19dad 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
19dae 6f 64 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 ode: Insert P1 P
19daf 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
19db0 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 Write an entry
19db1 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f into the table o
19db2 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 f cursor P1. A
19db3 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 new entry is.**
19db4 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f created if it do
19db5 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 esn't already ex
19db6 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 ist or the data
19db7 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a for an existing.
19db8 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 ** entry is over
19db9 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 written. The da
19dba 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 ta is the value
19dbb 73 74 6f 72 65 64 20 72 65 67 69 73 74 65 72 0a stored register.
19dbc 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20 54 68 ** number P2. Th
19dbd 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65 64 20 e key is stored
19dbe 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 in register P3.
19dbf 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a 20 The key must.**
19dc0 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a be an integer..*
19dc1 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c *.** If the OPFL
19dc2 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 AG_NCHANGE flag
19dc3 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 of P5 is set, th
19dc4 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 en the row chang
19dc5 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e e count is.** in
19dc6 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 cremented (other
19dc7 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74 wise not). If t
19dc8 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f he OPFLAG_LASTRO
19dc9 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69 WID flag of P5 i
19dca 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 s set,.** then r
19dcb 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 66 owid is stored f
19dcc 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 or subsequent re
19dcd 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73 turn by the.** s
19dce 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
19dcf 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74 rt_rowid() funct
19dd0 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69 ion (otherwise i
19dd1 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29 t is unmodified)
19dd2 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 ..**.** Paramete
19dd3 72 20 50 34 20 6d 61 79 20 70 6f 69 6e 74 20 74 r P4 may point t
19dd4 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 o a string conta
19dd5 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2d ining the table-
19dd6 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20 name, or.** may
19dd7 62 65 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 be NULL. If it i
19dd8 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
19dd9 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b the update-hook
19dda 20 0a 2a 2a 20 28 73 71 6c 69 74 65 33 2e 78 55 .** (sqlite3.xU
19ddb 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 20 69 pdateCallback) i
19ddc 73 20 69 6e 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77 s invoked follow
19ddd 69 6e 67 20 61 20 73 75 63 63 65 73 73 66 75 6c ing a successful
19dde 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 insert..**.** (
19ddf 57 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 WARNING/TODO: If
19de0 20 50 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d P1 is a pseudo-
19de1 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73 cursor and P2 is
19de2 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 dynamically.**
19de3 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20 allocated, then
19de4 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20 ownership of P2
19de5 69 73 20 74 72 61 6e 73 66 65 72 72 65 64 20 74 is transferred t
19de6 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72 o the pseudo-cur
19de7 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73 sor.** and regis
19de8 74 65 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65 ter P2 becomes e
19de9 70 68 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68 phemeral. If th
19dea 65 20 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e e cursor is chan
19deb 67 65 64 2c 20 74 68 65 0a 2a 2a 20 76 61 6c 75 ged, the.** valu
19dec 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 e of register P2
19ded 20 77 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67 will then chang
19dee 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 e. Make sure th
19def 69 73 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 is does not.** c
19df0 61 75 73 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d ause any problem
19df1 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 s.).**.** This i
19df2 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 nstruction only
19df3 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e works on tables.
19df4 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 The equivalent
19df5 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 instruction.**
19df6 66 6f 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f for indices is O
19df7 50 5f 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a P_IdxInsert..*/.
19df8 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 case OP_Insert:
19df9 7b 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 20 3d {. Mem *pData =
19dfa 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
19dfb 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 20 2];. Mem *pKey
19dfc 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
19dfd 70 33 5d 3b 0a 0a 20 20 69 36 34 20 69 4b 65 79 p3];.. i64 iKey
19dfe 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 ; /* The integ
19dff 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 er ROWID or key
19e00 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 for the record t
19e01 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f o be inserted */
19e02 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
19e03 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p1;. VdbeCursor
19e04 20 2a 70 43 3b 0a 20 20 61 73 73 65 72 74 28 20 *pC;. assert(
19e05 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
19e06 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 rsor );. pC = p
19e07 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 ->apCsr[i];. as
19e08 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 sert( pC!=0 );.
19e09 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 assert( pC->pCu
19e0a 72 73 6f 72 21 3d 30 20 7c 7c 20 70 43 2d 3e 70 rsor!=0 || pC->p
19e0b 73 65 75 64 6f 54 61 62 6c 65 20 29 3b 0a 20 20 seudoTable );.
19e0c 61 73 73 65 72 74 28 20 70 4b 65 79 2d 3e 66 6c assert( pKey->fl
19e0d 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b ags & MEM_Int );
19e0e 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 . assert( pC->i
19e0f 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49 sTable );. REGI
19e10 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
19e11 70 32 2c 20 70 44 61 74 61 29 3b 0a 20 20 52 45 p2, pData);. RE
19e12 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
19e13 2d 3e 70 33 2c 20 70 4b 65 79 29 3b 0a 0a 20 20 ->p3, pKey);..
19e14 69 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 iKey = intToKey(
19e15 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 pKey->u.i);. if
19e16 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c ( pOp->p5 & OPFL
19e17 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e AG_NCHANGE ) p->
19e18 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 nChange++;. if(
19e19 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 pOp->p5 & OPFLA
19e1a 47 5f 4c 41 53 54 52 4f 57 49 44 20 29 20 64 62 G_LASTROWID ) db
19e1b 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 4b ->lastRowid = pK
19e1c 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 70 ey->u.i;. if( p
19e1d 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 C->nextRowidVali
19e1e 64 20 26 26 20 70 4b 65 79 2d 3e 75 2e 69 3e 3d d && pKey->u.i>=
19e1f 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 20 29 7b pC->nextRowid ){
19e20 0a 20 20 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77 . pC->nextRow
19e21 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d idValid = 0;. }
19e22 0a 20 20 69 66 28 20 70 44 61 74 61 2d 3e 66 6c . if( pData->fl
19e23 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
19e24 7b 0a 20 20 20 20 70 44 61 74 61 2d 3e 7a 20 3d {. pData->z =
19e25 20 30 3b 0a 20 20 20 20 70 44 61 74 61 2d 3e 6e 0;. pData->n
19e26 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
19e27 20 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61 assert( pData
19e28 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 ->flags & (MEM_B
19e29 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 3b 0a lob|MEM_Str) );.
19e2a 20 20 7d 0a 20 20 69 66 28 20 70 43 2d 3e 70 73 }. if( pC->ps
19e2b 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 eudoTable ){.
19e2c 20 69 66 28 20 21 70 43 2d 3e 65 70 68 65 6d 50 if( !pC->ephemP
19e2d 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 seudoTable ){.
19e2e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
19e2f 65 28 64 62 2c 20 70 43 2d 3e 70 44 61 74 61 29 e(db, pC->pData)
19e30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e ;. }. pC->
19e31 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 20 iKey = iKey;.
19e32 20 70 43 2d 3e 6e 44 61 74 61 20 3d 20 70 44 61 pC->nData = pDa
19e33 74 61 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70 ta->n;. if( p
19e34 44 61 74 61 2d 3e 7a 3d 3d 70 44 61 74 61 2d 3e Data->z==pData->
19e35 7a 4d 61 6c 6c 6f 63 20 7c 7c 20 70 43 2d 3e 65 zMalloc || pC->e
19e36 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20 phemPseudoTable
19e37 29 7b 0a 20 20 20 20 20 20 70 43 2d 3e 70 44 61 ){. pC->pDa
19e38 74 61 20 3d 20 70 44 61 74 61 2d 3e 7a 3b 0a 20 ta = pData->z;.
19e39 20 20 20 20 20 69 66 28 20 21 70 43 2d 3e 65 70 if( !pC->ep
19e3a 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20 29 hemPseudoTable )
19e3b 7b 0a 20 20 20 20 20 20 20 20 70 44 61 74 61 2d {. pData-
19e3c 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 >flags &= ~MEM_D
19e3d 79 6e 3b 0a 20 20 20 20 20 20 20 20 70 44 61 74 yn;. pDat
19e3e 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f a->flags |= MEM_
19e3f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 20 20 70 Ephem;. p
19e40 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 Data->zMalloc =
19e41 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
19e42 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 2d 3e else{. pC->
19e43 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 4d pData = sqlite3M
19e44 61 6c 6c 6f 63 28 20 70 43 2d 3e 6e 44 61 74 61 alloc( pC->nData
19e45 2b 32 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +2 );. if(
19e46 21 70 43 2d 3e 70 44 61 74 61 20 29 20 67 6f 74 !pC->pData ) got
19e47 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 o no_mem;.
19e48 6d 65 6d 63 70 79 28 70 43 2d 3e 70 44 61 74 61 memcpy(pC->pData
19e49 2c 20 70 44 61 74 61 2d 3e 7a 2c 20 70 43 2d 3e , pData->z, pC->
19e4a 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 70 43 nData);. pC
19e4b 2d 3e 70 44 61 74 61 5b 70 43 2d 3e 6e 44 61 74 ->pData[pC->nDat
19e4c 61 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43 a] = 0;. pC
19e4d 2d 3e 70 44 61 74 61 5b 70 43 2d 3e 6e 44 61 74 ->pData[pC->nDat
19e4e 61 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a a+1] = 0;. }.
19e4f 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 pC->nullRow
19e50 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
19e51 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 0a 20 20 20 int nZero;.
19e52 20 69 66 28 20 70 44 61 74 61 2d 3e 66 6c 61 67 if( pData->flag
19e53 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a s & MEM_Zero ){.
19e54 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20 70 44 nZero = pD
19e55 61 74 61 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d 65 ata->u.i;. }e
19e56 6c 73 65 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f lse{. nZero
19e57 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
19e58 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
19e59 65 49 6e 73 65 72 74 28 70 43 2d 3e 70 43 75 72 eInsert(pC->pCur
19e5a 73 6f 72 2c 20 30 2c 20 69 4b 65 79 2c 0a 20 20 sor, 0, iKey,.
19e5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e5c 20 20 20 20 20 20 20 20 20 20 70 44 61 74 61 2d pData-
19e5d 3e 7a 2c 20 70 44 61 74 61 2d 3e 6e 2c 20 6e 5a >z, pData->n, nZ
19e5e 65 72 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ero,.
19e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e60 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 pOp->p5 & OPFLA
19e61 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 7d 0a 20 G_APPEND);. }.
19e62 20 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 . pC->rowidIsV
19e63 61 6c 69 64 20 3d 20 30 3b 0a 20 20 70 43 2d 3e alid = 0;. pC->
19e64 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d deferredMoveto =
19e65 20 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 0;. pC->cacheS
19e66 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
19e67 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b ALE;.. /* Invok
19e68 65 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f e the update-hoo
19e69 6b 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a k if required. *
19e6a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
19e6b 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 TE_OK && db->xUp
19e6c 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 dateCallback &&
19e6d 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 pOp->p4.z ){.
19e6e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
19e6f 20 3d 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 = db->aDb[pC->i
19e70 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 Db].zName;. c
19e71 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 onst char *zTbl
19e72 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 = pOp->p4.z;.
19e73 20 69 6e 74 20 6f 70 20 3d 20 28 28 70 4f 70 2d int op = ((pOp-
19e74 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 49 53 55 >p5 & OPFLAG_ISU
19e75 50 44 41 54 45 29 20 3f 20 53 51 4c 49 54 45 5f PDATE) ? SQLITE_
19e76 55 50 44 41 54 45 20 3a 20 53 51 4c 49 54 45 5f UPDATE : SQLITE_
19e77 49 4e 53 45 52 54 29 3b 0a 20 20 20 20 61 73 73 INSERT);. ass
19e78 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 ert( pC->isTable
19e79 20 29 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 );. db->xUpd
19e7a 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e ateCallback(db->
19e7b 70 55 70 64 61 74 65 41 72 67 2c 20 6f 70 2c 20 pUpdateArg, op,
19e7c 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29 zDb, zTbl, iKey)
19e7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
19e7e 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a ->iDb>=0 );. }.
19e7f 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
19e80 70 63 6f 64 65 3a 20 44 65 6c 65 74 65 20 50 31 pcode: Delete P1
19e81 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 * P4 *.**.**
19e82 20 44 65 6c 65 74 65 20 74 68 65 20 72 65 63 6f Delete the reco
19e83 72 64 20 61 74 20 77 68 69 63 68 20 74 68 65 20 rd at which the
19e84 50 31 20 63 75 72 73 6f 72 20 69 73 20 63 75 72 P1 cursor is cur
19e85 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e rently pointing.
19e86 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f .**.** The curso
19e87 72 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70 r will be left p
19e88 6f 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68 65 ointing at eithe
19e89 72 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74 68 r the next or th
19e8a 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72 65 e previous.** re
19e8b 63 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c cord in the tabl
19e8c 65 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66 74 e. If it is left
19e8d 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 pointing at the
19e8e 20 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74 68 next record, th
19e8f 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 4e en.** the next N
19e90 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
19e91 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70 2e will be a no-op.
19e92 20 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f 4b Hence it is OK
19e93 20 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61 20 to delete.** a
19e94 72 65 63 6f 72 64 20 66 72 6f 6d 20 77 69 74 68 record from with
19e95 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e in an Next loop.
19e96 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 .**.** If the OP
19e97 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 FLAG_NCHANGE fla
19e98 67 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c 20 g of P2 is set,
19e99 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 then the row cha
19e9a 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 nge count is.**
19e9b 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 incremented (oth
19e9c 65 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a erwise not)..**.
19e9d 2a 2a 20 50 31 20 6d 75 73 74 20 6e 6f 74 20 62 ** P1 must not b
19e9e 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20 e pseudo-table.
19e9f 20 49 74 20 68 61 73 20 74 6f 20 62 65 20 61 20 It has to be a
19ea0 72 65 61 6c 20 74 61 62 6c 65 20 77 69 74 68 0a real table with.
19ea1 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 ** multiple rows
19ea2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 ..**.** If P4 is
19ea3 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
19ea4 69 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f it is the name o
19ea5 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 f the table that
19ea6 20 50 31 20 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 P1 is.** pointi
19ea7 6e 67 20 74 6f 2e 20 20 54 68 65 20 75 70 64 61 ng to. The upda
19ea8 74 65 20 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 te hook will be
19ea9 69 6e 76 6f 6b 65 64 2c 20 69 66 20 69 74 20 65 invoked, if it e
19eaa 78 69 73 74 73 2e 0a 2a 2a 20 49 66 20 50 34 20 xists..** If P4
19eab 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e is not NULL then
19eac 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d the P1 cursor m
19ead 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 70 6f ust have been po
19eae 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e sitioned.** usin
19eaf 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 g OP_NotFound pr
19eb0 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 ior to invoking
19eb1 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a this opcode..*/.
19eb2 63 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a 20 case OP_Delete:
19eb3 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d {. int i = pOp-
19eb4 3e 70 31 3b 0a 20 20 69 36 34 20 69 4b 65 79 3b >p1;. i64 iKey;
19eb5 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
19eb6 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e C;.. assert( i>
19eb7 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 =0 && i<p->nCurs
19eb8 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e or );. pC = p->
19eb9 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 apCsr[i];. asse
19eba 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 rt( pC!=0 );. a
19ebb 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73 ssert( pC->pCurs
19ebc 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c or!=0 ); /* Onl
19ebd 79 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c y valid for real
19ebe 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 tables, no pseu
19ebf 64 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f dotables */.. /
19ec0 2a 20 49 66 20 74 68 65 20 75 70 64 61 74 65 2d * If the update-
19ec1 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 hook will be inv
19ec2 6f 6b 65 64 2c 20 73 65 74 20 69 4b 65 79 20 74 oked, set iKey t
19ec3 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 o the rowid of t
19ec4 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e he. ** row bein
19ec5 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a g deleted.. */.
19ec6 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74 if( db->xUpdat
19ec7 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 eCallback && pOp
19ec8 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 ->p4.z ){. as
19ec9 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c sert( pC->isTabl
19eca 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 e );. assert(
19ecb 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 pC->rowidIsVali
19ecc 64 20 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 d ); /* lastRow
19ecd 69 64 20 73 65 74 20 62 79 20 70 72 65 76 69 6f id set by previo
19ece 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a us OP_NotFound *
19ecf 2f 0a 20 20 20 20 69 4b 65 79 20 3d 20 70 43 2d /. iKey = pC-
19ed0 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a >lastRowid;. }.
19ed1 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 . rc = sqlite3V
19ed2 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 dbeCursorMoveto(
19ed3 70 43 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 pC);. if( rc )
19ed4 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
19ed5 6f 5f 65 72 72 6f 72 3b 0a 20 20 72 63 20 3d 20 o_error;. rc =
19ed6 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 sqlite3BtreeDele
19ed7 74 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b te(pC->pCursor);
19ed8 0a 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 . pC->nextRowid
19ed9 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 70 43 2d Valid = 0;. pC-
19eda 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
19edb 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f ACHE_STALE;.. /
19edc 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 * Invoke the upd
19edd 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 ate-hook if requ
19ede 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72 ired. */. if( r
19edf 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
19ee0 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 db->xUpdateCallb
19ee1 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a ack && pOp->p4.z
19ee2 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 ){. const ch
19ee3 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 ar *zDb = db->aD
19ee4 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 b[pC->iDb].zName
19ee5 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 ;. const char
19ee6 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 *zTbl = pOp->p4
19ee7 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 .z;. db->xUpd
19ee8 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e ateCallback(db->
19ee9 70 55 70 64 61 74 65 41 72 67 2c 20 53 51 4c 49 pUpdateArg, SQLI
19eea 54 45 5f 44 45 4c 45 54 45 2c 20 7a 44 62 2c 20 TE_DELETE, zDb,
19eeb 7a 54 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 20 zTbl, iKey);.
19eec 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 44 62 assert( pC->iDb
19eed 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 >=0 );. }. if(
19eee 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 pOp->p2 & OPFLA
19eef 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e G_NCHANGE ) p->n
19ef0 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 65 61 Change++;. brea
19ef1 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
19ef2 20 52 65 73 65 74 43 6f 75 6e 74 20 50 31 20 2a ResetCount P1 *
19ef3 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 *.**.** This op
19ef4 63 6f 64 65 20 72 65 73 65 74 73 20 74 68 65 20 code resets the
19ef5 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68 61 VMs internal cha
19ef6 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 nge counter to 0
19ef7 2e 20 49 66 20 50 31 20 69 73 20 74 72 75 65 2c . If P1 is true,
19ef8 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c .** then the val
19ef9 75 65 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 ue of the change
19efa 20 63 6f 75 6e 74 65 72 20 69 73 20 63 6f 70 69 counter is copi
19efb 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ed to the databa
19efc 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 63 68 61 se handle.** cha
19efd 6e 67 65 20 63 6f 75 6e 74 65 72 20 28 72 65 74 nge counter (ret
19efe 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 urned by subsequ
19eff 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ent calls to sql
19f00 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 29 0a ite3_changes()).
19f01 2a 2a 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 ** before it is
19f02 72 65 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 reset. This is u
19f03 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20 70 sed by trigger p
19f04 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73 65 rograms..*/.case
19f05 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 3a 20 OP_ResetCount:
19f06 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 {. if( pOp->p1
19f07 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
19f08 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c beSetChanges(db,
19f09 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 p->nChange);.
19f0a 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d }. p->nChange =
19f0b 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0;. break;.}..
19f0c 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 61 /* Opcode: RowDa
19f0d 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ta P1 P2 * * *.*
19f0e 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 *.** Write into
19f0f 72 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 register P2 the
19f10 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 61 74 complete row dat
19f11 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e a for cursor P1.
19f12 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 .** There is no
19f13 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f interpretation o
19f14 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a f the data. .**
19f15 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70 69 It is just copi
19f16 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 20 72 ed onto the P2 r
19f17 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 egister exactly
19f18 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 as .** it is fou
19f19 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 nd in the databa
19f1a 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 se file..**.** I
19f1b 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 f the P1 cursor
19f1c 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 must be pointing
19f1d 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 to a valid row
19f1e 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 (not a NULL row)
19f1f 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 .** of a real ta
19f20 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 ble, not a pseud
19f21 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 4f o-table..*/./* O
19f22 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50 31 pcode: RowKey P1
19f23 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
19f24 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 Write into regis
19f25 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c ter P2 the compl
19f26 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 20 ete row key for
19f27 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 cursor P1..** Th
19f28 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 ere is no interp
19f29 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 retation of the
19f2a 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20 6b data. .** The k
19f2b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e 74 ey is copied ont
19f2c 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65 o the P3 registe
19f2d 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a r exactly as .**
19f2e 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 it is found in
19f2f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
19f30 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
19f31 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 P1 cursor must b
19f32 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 e pointing to a
19f33 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 valid row (not a
19f34 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 NULL row).** of
19f35 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e a real table, n
19f36 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c ot a pseudo-tabl
19f37 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f e..*/.case OP_Ro
19f38 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52 6f wKey:.case OP_Ro
19f39 77 44 61 74 61 3a 20 7b 0a 20 20 69 6e 74 20 69 wData: {. int i
19f3a 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 = pOp->p1;. Vd
19f3b 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
19f3c 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
19f3d 0a 20 20 75 33 32 20 6e 3b 0a 0a 20 20 70 4f 75 . u32 n;.. pOu
19f3e 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
19f3f 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 ->p2];.. /* Not
19f40 65 20 74 68 61 74 20 52 6f 77 4b 65 79 20 61 6e e that RowKey an
19f41 64 20 52 6f 77 44 61 74 61 20 61 72 65 20 72 65 d RowData are re
19f42 61 6c 6c 79 20 65 78 61 63 74 6c 79 20 74 68 65 ally exactly the
19f43 20 73 61 6d 65 20 69 6e 73 74 72 75 63 74 69 6f same instructio
19f44 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 n */. assert( i
19f45 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 >=0 && i<p->nCur
19f46 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d sor );. pC = p-
19f47 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 >apCsr[i];. ass
19f48 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 ert( pC->isTable
19f49 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d || pOp->opcode=
19f4a 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 20 =OP_RowKey );.
19f4b 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 49 6e assert( pC->isIn
19f4c 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f dex || pOp->opco
19f4d 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20 29 de==OP_RowData )
19f4e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d ;. assert( pC!=
19f4f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
19f50 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b C->nullRow==0 );
19f51 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 . assert( pC->p
19f52 73 65 75 64 6f 54 61 62 6c 65 3d 3d 30 20 29 3b seudoTable==0 );
19f53 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 . assert( pC->p
19f54 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 70 Cursor!=0 );. p
19f55 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 Crsr = pC->pCurs
19f56 6f 72 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 or;. rc = sqlit
19f57 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
19f58 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 63 to(pC);. if( rc
19f59 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
19f5a 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 69 66 e_to_error;. if
19f5b 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b ( pC->isIndex ){
19f5c 0a 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 20 . i64 n64;.
19f5d 20 20 61 73 73 65 72 74 28 20 21 70 43 2d 3e 69 assert( !pC->i
19f5e 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 73 71 sTable );. sq
19f5f 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a lite3BtreeKeySiz
19f60 65 28 70 43 72 73 72 2c 20 26 6e 36 34 29 3b 0a e(pCrsr, &n64);.
19f61 20 20 20 20 69 66 28 20 6e 36 34 3e 64 62 2d 3e if( n64>db->
19f62 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
19f63 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
19f64 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 goto too_bi
19f65 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 3d g;. }. n =
19f66 20 6e 36 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 n64;. }else{.
19f67 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 sqlite3BtreeD
19f68 61 74 61 53 69 7a 65 28 70 43 72 73 72 2c 20 26 ataSize(pCrsr, &
19f69 6e 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 64 62 n);. if( n>db
19f6a 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
19f6b 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b LIMIT_LENGTH] ){
19f6c 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f . goto too_
19f6d 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 big;. }. }.
19f6e 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
19f6f 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e 2c MemGrow(pOut, n,
19f70 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 0) ){. goto
19f71 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 4f no_mem;. }. pO
19f72 75 74 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 4d 65 6d ut->n = n;. Mem
19f73 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
19f74 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 , MEM_Blob);. i
19f75 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 f( pC->isIndex )
19f76 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
19f77 65 33 42 74 72 65 65 4b 65 79 28 70 43 72 73 72 e3BtreeKey(pCrsr
19f78 2c 20 30 2c 20 6e 2c 20 70 4f 75 74 2d 3e 7a 29 , 0, n, pOut->z)
19f79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
19f7a 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
19f7b 44 61 74 61 28 70 43 72 73 72 2c 20 30 2c 20 6e Data(pCrsr, 0, n
19f7c 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 0a , pOut->z);. }.
19f7d 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 pOut->enc = SQ
19f7e 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 LITE_UTF8; /* I
19f7f 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 n case the blob
19f80 69 73 20 65 76 65 72 20 63 61 73 74 20 74 6f 20 is ever cast to
19f81 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 54 45 text */. UPDATE
19f82 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f _MAX_BLOBSIZE(pO
19f83 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ut);. break;.}.
19f84 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 69 ./* Opcode: Rowi
19f85 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a d P1 P2 * * *.**
19f86 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 65 67 .** Store in reg
19f87 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e 74 65 ister P2 an inte
19f88 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 ger which is the
19f89 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62 6c key of the tabl
19f8a 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 e entry that.**
19f8b 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 P1 is currently
19f8c 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2f 0a 63 61 73 point to..*/.cas
19f8d 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 20 20 e OP_Rowid: {
19f8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19f8f 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
19f90 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f */. int i = pO
19f91 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 p->p1;. VdbeCur
19f92 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 sor *pC;. i64 v
19f93 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d ;.. assert( i>=
19f94 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 0 && i<p->nCurso
19f95 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 r );. pC = p->a
19f96 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 pCsr[i];. asser
19f97 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 72 63 t( pC!=0 );. rc
19f98 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 = sqlite3VdbeCu
19f99 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a rsorMoveto(pC);.
19f9a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
19f9b 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
19f9c 6f 72 3b 0a 20 20 69 66 28 20 70 43 2d 3e 72 6f or;. if( pC->ro
19f9d 77 69 64 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 widIsValid ){.
19f9e 20 20 76 20 3d 20 70 43 2d 3e 6c 61 73 74 52 6f v = pC->lastRo
19f9f 77 69 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 wid;. }else if(
19fa0 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 pC->pseudoTable
19fa1 20 29 7b 0a 20 20 20 20 76 20 3d 20 6b 65 79 54 ){. v = keyT
19fa2 6f 49 6e 74 28 70 43 2d 3e 69 4b 65 79 29 3b 0a oInt(pC->iKey);.
19fa3 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e }else if( pC->
19fa4 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 2f nullRow ){. /
19fa5 2a 20 4c 65 61 76 65 20 74 68 65 20 72 6f 77 69 * Leave the rowi
19fa6 64 20 73 65 74 20 74 6f 20 61 20 4e 55 4c 4c 20 d set to a NULL
19fa7 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 */. break;.
19fa8 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
19fa9 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d t( pC->pCursor!=
19faa 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 );. sqlite3
19fab 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d BtreeKeySize(pC-
19fac 3e 70 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 >pCursor, &v);.
19fad 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28 v = keyToInt(
19fae 76 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e v);. }. pOut->
19faf 75 2e 69 20 3d 20 76 3b 0a 20 20 4d 65 6d 53 65 u.i = v;. MemSe
19fb0 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
19fb1 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 MEM_Int);. brea
19fb2 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
19fb3 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a 20 NullRow P1 * *
19fb4 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 * *.**.** Move t
19fb5 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 he cursor P1 to
19fb6 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e 79 a null row. Any
19fb7 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 61 OP_Column opera
19fb8 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f 63 tions.** that oc
19fb9 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63 75 cur while the cu
19fba 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 6e rsor is on the n
19fbb 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c 77 ull row will alw
19fbc 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 20 4e ays.** write a N
19fbd 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
19fbe 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 20 20 69 6e 74 NullRow: {. int
19fbf 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 i = pOp->p1;.
19fc0 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
19fc1 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
19fc2 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 && i<p->nCursor
19fc3 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 );. pC = p->apC
19fc4 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 sr[i];. assert(
19fc5 20 70 43 21 3d 30 20 29 3b 0a 20 20 70 43 2d 3e pC!=0 );. pC->
19fc6 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 70 nullRow = 1;. p
19fc7 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
19fc8 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 2d 3e 70 = 0;. if( pC->p
19fc9 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 Cursor ){. sq
19fca 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 lite3BtreeClearC
19fcb 75 72 73 6f 72 28 70 43 2d 3e 70 43 75 72 73 6f ursor(pC->pCurso
19fcc 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b r);. }. break;
19fcd 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c .}../* Opcode: L
19fce 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ast P1 P2 * * *.
19fcf 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 **.** The next u
19fd0 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 se of the Rowid
19fd1 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 or Column or Nex
19fd2 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f t instruction fo
19fd3 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 r P1 .** will re
19fd4 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 fer to the last
19fd5 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 entry in the dat
19fd6 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 abase table or i
19fd7 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 ndex..** If the
19fd8 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 table or index i
19fd9 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 s empty and P2>0
19fda 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 , then jump imme
19fdb 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a diately to P2..*
19fdc 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 * If P2 is 0 or
19fdd 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 if the table or
19fde 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 index is not emp
19fdf 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 ty, fall through
19fe0 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f .** to the follo
19fe1 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e wing instruction
19fe2 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 73 ..*/.case OP_Las
19fe3 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a t: { /* j
19fe4 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d ump */. int i =
19fe5 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 pOp->p1;. Vdbe
19fe6 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 Cursor *pC;. Bt
19fe7 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
19fe8 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 73 int res;.. ass
19fe9 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
19fea 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 ->nCursor );. p
19feb 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b C = p->apCsr[i];
19fec 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 . assert( pC!=0
19fed 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 43 );. pCrsr = pC
19fee 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 ->pCursor;. ass
19fef 65 72 74 28 20 70 43 72 73 72 21 3d 30 20 29 3b ert( pCrsr!=0 );
19ff0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
19ff1 74 72 65 65 4c 61 73 74 28 70 43 72 73 72 2c 20 treeLast(pCrsr,
19ff2 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c &res);. pC->nul
19ff3 6c 52 6f 77 20 3d 20 72 65 73 3b 0a 20 20 70 43 lRow = res;. pC
19ff4 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
19ff5 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 = 0;. pC->cach
19ff6 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
19ff7 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 STALE;. if( res
19ff8 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20 29 7b && pOp->p2>0 ){
19ff9 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
19ffa 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
19ffb 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 ak;.}.../* Opcod
19ffc 65 3a 20 53 6f 72 74 20 50 31 20 50 32 20 2a 20 e: Sort P1 P2 *
19ffd 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f * *.**.** This o
19ffe 70 63 6f 64 65 20 64 6f 65 73 20 65 78 61 63 74 pcode does exact
19fff 6c 79 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e ly the same thin
1a000 67 20 61 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 g as OP_Rewind e
1a001 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 xcept that.** it
1a002 20 69 6e 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 increments an u
1a003 6e 64 6f 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 ndocumented glob
1a004 61 6c 20 76 61 72 69 61 62 6c 65 20 75 73 65 64 al variable used
1a005 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a for testing..**
1a006 0a 2a 2a 20 53 6f 72 74 69 6e 67 20 69 73 20 61 .** Sorting is a
1a007 63 63 6f 6d 70 6c 69 73 68 65 64 20 62 79 20 77 ccomplished by w
1a008 72 69 74 69 6e 67 20 72 65 63 6f 72 64 73 20 69 riting records i
1a009 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e nto a sorting in
1a00a 64 65 78 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 dex,.** then rew
1a00b 69 6e 64 69 6e 67 20 74 68 61 74 20 69 6e 64 65 inding that inde
1a00c 78 20 61 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 x and playing it
1a00d 20 62 61 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e back from begin
1a00e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 ning to.** end.
1a00f 20 57 65 20 75 73 65 20 74 68 65 20 4f 50 5f 53 We use the OP_S
1a010 6f 72 74 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 ort opcode inste
1a011 61 64 20 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 ad of OP_Rewind
1a012 74 6f 20 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 to do the.** rew
1a013 69 6e 64 69 6e 67 20 73 6f 20 74 68 61 74 20 74 inding so that t
1a014 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 he global variab
1a015 6c 65 20 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 le will be incre
1a016 6d 65 6e 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 mented and.** re
1a017 67 72 65 73 73 69 6f 6e 20 74 65 73 74 73 20 63 gression tests c
1a018 61 6e 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 an determine whe
1a019 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
1a01a 6f 70 74 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 optimizer is.**
1a01b 63 6f 72 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 correctly optimi
1a01c 7a 69 6e 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a zing out sorts..
1a01d 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a */.case OP_Sort:
1a01e 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1a01f 70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 p */.#ifdef SQLI
1a020 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 TE_TEST. sqlite
1a021 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 3_sort_count++;.
1a022 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 sqlite3_search
1a023 5f 63 6f 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 _count--;.#endif
1a024 0a 20 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 . p->aCounter[S
1a025 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
1a026 5f 53 4f 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a _SORT-1]++;. /*
1a027 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e Fall through in
1a028 74 6f 20 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a to OP_Rewind */.
1a029 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 }./* Opcode: Rew
1a02a 69 6e 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ind P1 P2 * * *.
1a02b 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 **.** The next u
1a02c 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 se of the Rowid
1a02d 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 or Column or Nex
1a02e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f t instruction fo
1a02f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 r P1 .** will re
1a030 66 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 fer to the first
1a031 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 entry in the da
1a032 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 tabase table or
1a033 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 index..** If the
1a034 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1a035 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e is empty and P2>
1a036 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 0, then jump imm
1a037 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a ediately to P2..
1a038 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 ** If P2 is 0 or
1a039 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 if the table or
1a03a 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d index is not em
1a03b 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 pty, fall throug
1a03c 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c h.** to the foll
1a03d 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f owing instructio
1a03e 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 n..*/.case OP_Re
1a03f 77 69 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f wind: { /
1a040 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 * jump */. int
1a041 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i = pOp->p1;. V
1a042 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1a043 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1a044 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 ;. int res;..
1a045 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
1a046 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a i<p->nCursor );.
1a047 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b pC = p->apCsr[
1a048 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 i];. assert( pC
1a049 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 !=0 );. if( (pC
1a04a 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f rsr = pC->pCurso
1a04b 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 r)!=0 ){. rc
1a04c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 = sqlite3BtreeFi
1a04d 72 73 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 rst(pCrsr, &res)
1a04e 3b 0a 20 20 20 20 70 43 2d 3e 61 74 46 69 72 73 ;. pC->atFirs
1a04f 74 20 3d 20 72 65 73 3d 3d 30 3b 0a 20 20 20 20 t = res==0;.
1a050 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1a051 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e to = 0;. pC->
1a052 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1a053 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c CHE_STALE;. }el
1a054 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20 31 3b se{. res = 1;
1a055 0a 20 20 7d 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 . }. pC->nullR
1a056 6f 77 20 3d 20 72 65 73 3b 0a 20 20 61 73 73 65 ow = res;. asse
1a057 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 rt( pOp->p2>0 &&
1a058 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 pOp->p2<p->nOp
1a059 29 3b 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a );. if( res ){.
1a05a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1a05b 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1a05c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1a05d 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 Next P1 P2 * *
1a05e 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 *.**.** Advance
1a05f 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 cursor P1 so tha
1a060 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 t it points to t
1a061 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 74 61 he next key/data
1a062 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 pair in its.**
1a063 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 table or index.
1a064 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1a065 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65 20 more key/value
1a066 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 pairs then fall
1a067 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 through.** to th
1a068 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 e following inst
1a069 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 ruction. But if
1a06a 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76 61 the cursor adva
1a06b 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73 66 nce was successf
1a06c 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 ul,.** jump imme
1a06d 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a diately to P2..*
1a06e 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72 73 *.** The P1 curs
1a06f 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20 61 or must be for a
1a070 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 real table, not
1a071 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e a pseudo-table.
1a072 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1a073 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f Prev.*/./* Opco
1a074 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 20 2a de: Prev P1 P2 *
1a075 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20 * *.**.** Back
1a076 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 up cursor P1 so
1a077 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
1a078 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6b o the previous k
1a079 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20 ey/data pair in
1a07a 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 its.** table or
1a07b 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 65 index. If there
1a07c 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 is no previous
1a07d 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 key/value pairs
1a07e 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 then fall throug
1a07f 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c h.** to the foll
1a080 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f owing instructio
1a081 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 n. But if the c
1a082 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61 73 ursor backup was
1a083 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 successful,.**
1a084 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 jump immediately
1a085 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 to P2..**.** Th
1a086 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 e P1 cursor must
1a087 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74 be for a real t
1a088 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 able, not a pseu
1a089 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 do-table..*/.cas
1a08a 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20 20 e OP_Prev:
1a08b 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 /* jump */.c
1a08c 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20 ase OP_Next: {
1a08d 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1a08e 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1a08f 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 C;. BtCursor *p
1a090 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b Crsr;. int res;
1a091 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e .. CHECK_FOR_IN
1a092 54 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72 TERRUPT;. asser
1a093 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1a094 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1a095 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d sor );. pC = p-
1a096 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1a097 0a 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 0a . if( pC==0 ){.
1a098 20 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 break; /* S
1a099 65 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 20 ee ticket #2273
1a09a 2a 2f 0a 20 20 7d 0a 20 20 70 43 72 73 72 20 3d */. }. pCrsr =
1a09b 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 pC->pCursor;.
1a09c 61 73 73 65 72 74 28 20 70 43 72 73 72 20 29 3b assert( pCrsr );
1a09d 0a 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 61 73 . res = 1;. as
1a09e 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 sert( pC->deferr
1a09f 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 edMoveto==0 );.
1a0a0 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 rc = pOp->opcod
1a0a1 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c e==OP_Next ? sql
1a0a2 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 ite3BtreeNext(pC
1a0a3 72 73 72 2c 20 26 72 65 73 29 20 3a 0a 20 20 20 rsr, &res) :.
1a0a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a0a5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1a0a6 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 e3BtreePrevious(
1a0a7 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 pCrsr, &res);.
1a0a8 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 72 65 pC->nullRow = re
1a0a9 73 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74 s;. pC->cacheSt
1a0aa 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1a0ab 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 3d 3d 30 LE;. if( res==0
1a0ac 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1a0ad 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 69 66 ->p2 - 1;. if
1a0ae 28 20 70 4f 70 2d 3e 70 35 20 29 20 70 2d 3e 61 ( pOp->p5 ) p->a
1a0af 43 6f 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 35 2d Counter[pOp->p5-
1a0b0 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 20 53 51 4c 1]++;.#ifdef SQL
1a0b1 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c ITE_TEST. sql
1a0b2 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e ite3_search_coun
1a0b3 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a t++;.#endif. }.
1a0b4 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c pC->rowidIsVal
1a0b5 69 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b id = 0;. break;
1a0b6 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1a0b7 64 78 49 6e 73 65 72 74 20 50 31 20 50 32 20 50 dxInsert P1 P2 P
1a0b8 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 3 * *.**.** Regi
1a0b9 73 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61 20 ster P2 holds a
1a0ba 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61 SQL index key ma
1a0bb 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 de using the.**
1a0bc 4d 61 6b 65 49 64 78 52 65 63 20 69 6e 73 74 72 MakeIdxRec instr
1a0bd 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f uctions. This o
1a0be 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 68 61 pcode writes tha
1a0bf 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68 t key.** into th
1a0c0 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74 e index P1. Dat
1a0c1 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 a for the entry
1a0c2 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33 is nil..**.** P3
1a0c3 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74 20 is a flag that
1a0c4 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20 provides a hint
1a0c5 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 to the b-tree la
1a0c6 79 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a yer that this.**
1a0c7 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c insert is likel
1a0c8 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e y to be an appen
1a0c9 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e d..**.** This in
1a0ca 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 struction only w
1a0cb 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73 orks for indices
1a0cc 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e . The equivalen
1a0cd 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a t instruction.**
1a0ce 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f for tables is O
1a0cf 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 P_Insert..*/.cas
1a0d0 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20 e OP_IdxInsert:
1a0d1 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 { /* in2
1a0d2 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 */. int i = pOp
1a0d3 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 ->p1;. VdbeCurs
1a0d4 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 or *pC;. BtCurs
1a0d5 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 or *pCrsr;. ass
1a0d6 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
1a0d7 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1a0d8 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b ssert( p->apCsr[
1a0d9 69 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 i]!=0 );. asser
1a0da 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 t( pIn2->flags &
1a0db 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 69 MEM_Blob );. i
1a0dc 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 43 20 f( (pCrsr = (pC
1a0dd 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e = p->apCsr[i])->
1a0de 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 pCursor)!=0 ){.
1a0df 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 assert( pC->i
1a0e0 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 sTable==0 );.
1a0e1 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 rc = ExpandBlob
1a0e2 28 70 49 6e 32 29 3b 0a 20 20 20 20 69 66 28 20 (pIn2);. if(
1a0e3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1a0e4 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20 . int nKey
1a0e5 3d 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 = pIn2->n;.
1a0e6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 const char *zKe
1a0e7 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20 y = pIn2->z;.
1a0e8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1a0e9 74 72 65 65 49 6e 73 65 72 74 28 70 43 72 73 72 treeInsert(pCrsr
1a0ea 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 22 22 , zKey, nKey, ""
1a0eb 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 29 , 0, 0, pOp->p3)
1a0ec 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1a0ed 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1a0ee 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 to==0 );. p
1a0ef 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1a0f0 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1a0f1 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b }. }. break;
1a0f2 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1a0f3 64 78 44 65 6c 65 74 65 20 50 31 20 50 32 20 50 dxDelete P1 P2 P
1a0f4 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 3 * *.**.** The
1a0f5 63 6f 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 65 content of P3 re
1a0f6 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 gisters starting
1a0f7 20 61 74 20 72 65 67 69 73 74 65 72 20 50 32 20 at register P2
1a0f8 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 form.** an unpac
1a0f9 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 ked index key. T
1a0fa 68 69 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 his opcode remov
1a0fb 65 73 20 74 68 61 74 20 65 6e 74 72 79 20 66 72 es that entry fr
1a0fc 6f 6d 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78 om the .** index
1a0fd 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f opened by curso
1a0fe 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P1..*/.case OP
1a0ff 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b 0a 20 20 _IdxDelete: {.
1a100 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b int i = pOp->p1;
1a101 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1a102 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 C;. BtCursor *p
1a103 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20 Crsr;. assert(
1a104 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61 pOp->p3>0 );. a
1a105 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 ssert( pOp->p2>0
1a106 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d && pOp->p2+pOp-
1a107 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1a108 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1a109 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1a10a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
1a10b 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 pCsr[i]!=0 );.
1a10c 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 43 if( (pCrsr = (pC
1a10d 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d = p->apCsr[i])-
1a10e 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a >pCursor)!=0 ){.
1a10f 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1a110 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1a111 72 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49 6e 66 r;. r.pKeyInf
1a112 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e 66 6f o = pC->pKeyInfo
1a113 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 20 3d ;. r.nField =
1a114 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 72 2e pOp->p3;. r.
1a115 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 72 flags = 0;. r
1a116 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d .aMem = &p->aMem
1a117 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 72 [pOp->p2];. r
1a118 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1a119 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 MovetoUnpacked(p
1a11a 43 72 73 72 2c 20 26 72 2c 20 30 2c 20 30 2c 20 Crsr, &r, 0, 0,
1a11b 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 &res);. if( r
1a11c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1a11d 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 res==0 ){.
1a11e 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1a11f 65 44 65 6c 65 74 65 28 70 43 72 73 72 29 3b 0a eDelete(pCrsr);.
1a120 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
1a121 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f ( pC->deferredMo
1a122 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70 veto==0 );. p
1a123 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1a124 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1a125 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1a126 20 4f 70 63 6f 64 65 3a 20 49 64 78 52 6f 77 69 Opcode: IdxRowi
1a127 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a d P1 P2 * * *.**
1a128 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 .** Write into r
1a129 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e egister P2 an in
1a12a 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
1a12b 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
1a12c 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a 2a the record at.*
1a12d 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 * the end of the
1a12e 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e 74 index key point
1a12f 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72 20 ed to by cursor
1a130 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67 65 P1. This intege
1a131 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 74 r should be.** t
1a132 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 he rowid of the
1a133 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20 77 table entry to w
1a134 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78 20 hich this index
1a135 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a 2a entry points..**
1a136 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52 6f .** See also: Ro
1a137 77 69 64 2c 20 4d 61 6b 65 49 64 78 52 65 63 2e wid, MakeIdxRec.
1a138 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 52 .*/.case OP_IdxR
1a139 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 owid: {
1a13a 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1a13b 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 release */. int
1a13c 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 i = pOp->p1;.
1a13d 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1a13e 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1a13f 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e C;.. assert( i>
1a140 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 =0 && i<p->nCurs
1a141 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 or );. assert(
1a142 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 p->apCsr[i]!=0 )
1a143 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d ;. if( (pCrsr =
1a144 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b (pC = p->apCsr[
1a145 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 i])->pCursor)!=0
1a146 20 29 7b 0a 20 20 20 20 69 36 34 20 72 6f 77 69 ){. i64 rowi
1a147 64 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 d;.. assert(
1a148 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1a149 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 to==0 );. ass
1a14a 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 ert( pC->isTable
1a14b 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 ==0 );. if( !
1a14c 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 pC->nullRow ){.
1a14d 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1a14e 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 70 43 3VdbeIdxRowid(pC
1a14f 72 73 72 2c 20 26 72 6f 77 69 64 29 3b 0a 20 20 rsr, &rowid);.
1a150 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1a151 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1a152 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1a153 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1a154 7d 0a 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79 }. MemSetTy
1a155 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1a156 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 _Int);. pOu
1a157 74 2d 3e 75 2e 69 20 3d 20 72 6f 77 69 64 3b 0a t->u.i = rowid;.
1a158 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 }. }. brea
1a159 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1a15a 20 49 64 78 47 45 20 50 31 20 50 32 20 50 33 20 IdxGE P1 P2 P3
1a15b 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 P4 P5.**.** The
1a15c 50 34 20 72 65 67 69 73 74 65 72 20 76 61 6c 75 P4 register valu
1a15d 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 es beginning wit
1a15e 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 h P3 form an unp
1a15f 61 63 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 acked index .**
1a160 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73 20 74 key that omits t
1a161 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 he ROWID. Compa
1a162 72 65 20 74 68 69 73 20 6b 65 79 20 76 61 6c 75 re this key valu
1a163 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e e against the in
1a164 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 dex .** that P1
1a165 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
1a166 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 nting to, ignori
1a167 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 ng the ROWID on
1a168 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a the P1 index..**
1a169 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 69 6e .** If the P1 in
1a16a 64 65 78 20 65 6e 74 72 79 20 69 73 20 67 72 65 dex entry is gre
1a16b 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
1a16c 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 al to the key va
1a16d 6c 75 65 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 lue.** then jump
1a16e 20 74 6f 20 50 32 2e 20 20 4f 74 68 65 72 77 69 to P2. Otherwi
1a16f 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 se fall through
1a170 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 to the next inst
1a171 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 ruction..**.** I
1a172 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f f P5 is non-zero
1a173 20 74 68 65 6e 20 74 68 65 20 6b 65 79 20 76 61 then the key va
1a174 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 lue is increased
1a175 20 62 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0a by an epsilon .
1a176 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 ** prior to the
1a177 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 comparison. Thi
1a178 73 20 6d 61 6b 65 20 74 68 65 20 6f 70 63 6f 64 s make the opcod
1a179 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47 e work like IdxG
1a17a 54 20 65 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 T except.** that
1a17b 20 69 66 20 74 68 65 20 6b 65 79 20 66 72 6f 6d if the key from
1a17c 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 register P3 is
1a17d 61 20 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 a prefix of the
1a17e 6b 65 79 20 69 6e 20 74 68 65 20 63 75 72 73 6f key in the curso
1a17f 72 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 r,.** the result
1a180 20 69 73 20 66 61 6c 73 65 20 77 68 65 72 65 61 is false wherea
1a181 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 74 72 s it would be tr
1a182 75 65 20 77 69 74 68 20 49 64 78 47 54 2e 0a 2a ue with IdxGT..*
1a183 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 /./* Opcode: Idx
1a184 4c 54 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 LT P1 P2 P3 * P5
1a185 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 .**.** The P4 re
1a186 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 62 65 gister values be
1a187 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20 ginning with P3
1a188 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 form an unpacked
1a189 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 index .** key t
1a18a 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f hat omits the RO
1a18b 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 WID. Compare th
1a18c 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61 is key value aga
1a18d 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a inst the index .
1a18e 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 63 75 ** that P1 is cu
1a18f 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
1a190 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 to, ignoring th
1a191 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 e ROWID on the P
1a192 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 1 index..**.** I
1a193 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 f the P1 index e
1a194 6e 74 72 79 20 69 73 20 6c 65 73 73 20 74 68 61 ntry is less tha
1a195 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 n the key value
1a196 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e then jump to P2.
1a197 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61 .** Otherwise fa
1a198 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1a199 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1a19a 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 on..**.** If P5
1a19b 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e is non-zero then
1a19c 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69 the key value i
1a19d 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 61 s increased by a
1a19e 6e 20 65 70 73 69 6c 6f 6e 20 70 72 69 6f 72 20 n epsilon prior
1a19f 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 .** to the compa
1a1a0 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b rison. This mak
1a1a1 65 73 20 74 68 65 20 6f 70 63 6f 64 65 20 77 6f es the opcode wo
1a1a2 72 6b 20 6c 69 6b 65 20 49 64 78 4c 45 2e 0a 2a rk like IdxLE..*
1a1a3 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a /.case OP_IdxLT:
1a1a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d /* jum
1a1a5 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f p, in3 */.case O
1a1a6 50 5f 49 64 78 47 45 3a 20 7b 20 20 20 20 20 20 P_IdxGE: {
1a1a7 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1a1a8 2f 0a 20 20 69 6e 74 20 69 3d 20 70 4f 70 2d 3e /. int i= pOp->
1a1a9 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p1;. VdbeCursor
1a1aa 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 *pC;.. assert(
1a1ab 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 i>=0 && i<p->nC
1a1ac 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 ursor );. asser
1a1ad 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d t( p->apCsr[i]!=
1a1ae 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 20 3d 0 );. if( (pC =
1a1af 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 p->apCsr[i])->p
1a1b0 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 Cursor!=0 ){.
1a1b1 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 55 6e int res;. Un
1a1b2 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a packedRecord r;.
1a1b3 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e assert( pC->
1a1b4 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d deferredMoveto==
1a1b5 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
1a1b6 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70 pOp->p5==0 || p
1a1b7 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20 Op->p5==1 );.
1a1b8 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1a1b9 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 type==P4_INT32 )
1a1ba 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49 6e 66 6f ;. r.pKeyInfo
1a1bb 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b = pC->pKeyInfo;
1a1bc 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20 . r.nField =
1a1bd 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 pOp->p4.i;. i
1a1be 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 f( pOp->p5 ){.
1a1bf 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e r.flags = UN
1a1c0 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c PACKED_INCRKEY |
1a1c1 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 UNPACKED_IGNORE
1a1c2 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 6c 73 _ROWID;. }els
1a1c3 65 7b 0a 20 20 20 20 20 20 72 2e 66 6c 61 67 73 e{. r.flags
1a1c4 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f = UNPACKED_IGNO
1a1c5 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a RE_ROWID;. }.
1a1c6 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d r.aMem = &p-
1a1c7 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1a1c8 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1a1c9 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 VdbeIdxKeyCompar
1a1ca 65 28 70 43 2c 20 26 72 2c 20 26 72 65 73 29 3b e(pC, &r, &res);
1a1cb 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 . if( pOp->op
1a1cc 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 code==OP_IdxLT )
1a1cd 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 2d 72 {. res = -r
1a1ce 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 es;. }else{.
1a1cf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1a1d0 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 ->opcode==OP_Idx
1a1d1 47 45 20 29 3b 0a 20 20 20 20 20 20 72 65 73 2b GE );. res+
1a1d2 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 +;. }. if(
1a1d3 20 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 res>0 ){.
1a1d4 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1a1d5 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 ;. }. }. b
1a1d6 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1a1d7 64 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 de: Destroy P1 P
1a1d8 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 2 P3 * *.**.** D
1a1d9 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 elete an entire
1a1da 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f database table o
1a1db 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f r index whose ro
1a1dc 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ot page in the d
1a1dd 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 atabase.** file
1a1de 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a is given by P1..
1a1df 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 **.** The table
1a1e0 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 being destroyed
1a1e1 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 is in the main d
1a1e2 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 atabase file if
1a1e3 50 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 P3==0. If.** P3
1a1e4 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 ==1 then the tab
1a1e5 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 le to be clear i
1a1e6 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 s in the auxilia
1a1e7 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ry database file
1a1e8 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 .** that is used
1a1e9 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 to store tables
1a1ea 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 create using CR
1a1eb 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 EATE TEMPORARY T
1a1ec 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 ABLE..**.** If A
1a1ed 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 UTOVACUUM is ena
1a1ee 62 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 bled then it is
1a1ef 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e possible that an
1a1f0 6f 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a other root page.
1a1f1 2a 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 ** might be move
1a1f2 64 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 d into the newly
1a1f3 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 deleted root pa
1a1f4 67 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b ge in order to k
1a1f5 65 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 eep all.** root
1a1f6 70 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 pages contiguous
1a1f7 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
1a1f8 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 g of the databas
1a1f9 65 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a e. The former.*
1a1fa 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 * value of the r
1a1fb 6f 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f oot page that mo
1a1fc 76 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 ved - its value
1a1fd 62 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 before the move
1a1fe 6f 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 occurred -.** is
1a1ff 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1a200 74 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 ter P2. If no p
1a201 61 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 age .** movement
1a202 20 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62 was required (b
1a203 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 ecause the table
1a204 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 being dropped w
1a205 61 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 as already .** t
1a206 68 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 he last one in t
1a207 68 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 he database) the
1a208 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 n a zero is stor
1a209 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1a20a 32 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 2..** If AUTOVAC
1a20b 55 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 UUM is disabled
1a20c 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 then a zero is s
1a20d 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1a20e 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 r P2..**.** See
1a20f 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 also: Clear.*/.c
1a210 61 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 ase OP_Destroy:
1a211 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 { /* out2-pr
1a212 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e erelease */. in
1a213 74 20 69 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 t iMoved;. int
1a214 69 43 6e 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 iCnt;.#ifndef SQ
1a215 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1a216 4c 54 41 42 4c 45 0a 20 20 56 64 62 65 20 2a 70 LTABLE. Vdbe *p
1a217 56 64 62 65 3b 0a 20 20 69 43 6e 74 20 3d 20 30 Vdbe;. iCnt = 0
1a218 3b 0a 20 20 66 6f 72 28 70 56 64 62 65 3d 64 62 ;. for(pVdbe=db
1a219 2d 3e 70 56 64 62 65 3b 20 70 56 64 62 65 3b 20 ->pVdbe; pVdbe;
1a21a 70 56 64 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65 pVdbe=pVdbe->pNe
1a21b 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 56 64 xt){. if( pVd
1a21c 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f be->magic==VDBE_
1a21d 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70 56 64 MAGIC_RUN && pVd
1a21e 62 65 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 be->inVtabMethod
1a21f 3c 32 20 26 26 20 70 56 64 62 65 2d 3e 70 63 3e <2 && pVdbe->pc>
1a220 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 43 6e 74 =0 ){. iCnt
1a221 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 ++;. }. }.#e
1a222 6c 73 65 0a 20 20 69 43 6e 74 20 3d 20 64 62 2d lse. iCnt = db-
1a223 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 0a >activeVdbeCnt;.
1a224 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69 43 6e #endif. if( iCn
1a225 74 3e 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 t>1 ){. rc =
1a226 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 SQLITE_LOCKED;.
1a227 20 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f p->errorActio
1a228 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 n = OE_Abort;.
1a229 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 }else{. int i
1a22a 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 Db = pOp->p3;.
1a22b 20 20 61 73 73 65 72 74 28 20 69 43 6e 74 3d 3d assert( iCnt==
1a22c 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 1 );. assert(
1a22d 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
1a22e 20 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b (1<<iDb))!=0 );
1a22f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1a230 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 3BtreeDropTable(
1a231 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 db->aDb[iDb].pBt
1a232 2c 20 70 4f 70 2d 3e 70 31 2c 20 26 69 4d 6f 76 , pOp->p1, &iMov
1a233 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 ed);. MemSetT
1a234 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1a235 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 M_Int);. pOut
1a236 2d 3e 75 2e 69 20 3d 20 69 4d 6f 76 65 64 3b 0a ->u.i = iMoved;.
1a237 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a238 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1a239 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1a23a 45 5f 4f 4b 20 26 26 20 69 4d 6f 76 65 64 21 3d E_OK && iMoved!=
1a23b 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1a23c 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 e3RootPageMoved(
1a23d 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2c 20 69 &db->aDb[iDb], i
1a23e 4d 6f 76 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b Moved, pOp->p1);
1a23f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1a240 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1a241 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61 72 20 50 Opcode: Clear P
1a242 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 65 1 P2 P3.**.** De
1a243 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 lete all content
1a244 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 s of the databas
1a245 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 e table or index
1a246 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 whose root page
1a247 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 .** in the datab
1a248 61 73 65 20 66 69 6c 65 20 69 73 20 67 69 76 65 ase file is give
1a249 6e 20 62 79 20 50 31 2e 20 20 42 75 74 2c 20 75 n by P1. But, u
1a24a 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c 20 64 nlike Destroy, d
1a24b 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 20 o not.** remove
1a24c 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 the table or ind
1a24d 65 78 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ex from the data
1a24e 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
1a24f 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 The table being
1a250 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 clear is in the
1a251 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
1a252 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20 20 49 ile if P2==0. I
1a253 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 f.** P2==1 then
1a254 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 the table to be
1a255 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 clear is in the
1a256 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 auxiliary databa
1a257 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 se file.** that
1a258 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 is used to store
1a259 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 75 tables create u
1a25a 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50 sing CREATE TEMP
1a25b 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a ORARY TABLE..**.
1a25c 2a 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 6c ** If the P3 val
1a25d 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 ue is non-zero,
1a25e 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 72 then the table r
1a25f 65 66 65 72 65 64 20 74 6f 20 6d 75 73 74 20 62 efered to must b
1a260 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 74 e an.** intkey t
1a261 61 62 6c 65 20 28 61 6e 20 53 51 4c 20 74 61 62 able (an SQL tab
1a262 6c 65 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 65 78 le, not an index
1a263 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 ). In this case
1a264 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 0a the row change .
1a265 2a 2a 20 63 6f 75 6e 74 20 69 73 20 69 6e 63 72 ** count is incr
1a266 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e emented by the n
1a267 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e umber of rows in
1a268 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
1a269 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 49 66 cleared. .** If
1a26a 20 50 33 20 69 73 20 67 72 65 61 74 65 72 20 74 P3 is greater t
1a26b 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 han zero, then t
1a26c 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 he value stored
1a26d 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 69 in register P3 i
1a26e 73 0a 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 65 6d s.** also increm
1a26f 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d ented by the num
1a270 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 ber of rows in t
1a271 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 he table being c
1a272 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 leared..**.** Se
1a273 65 20 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79 0a e also: Destroy.
1a274 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61 72 */.case OP_Clear
1a275 3a 20 7b 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 : {. int nChang
1a276 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 e = 0;. assert(
1a277 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
1a278 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d (1<<pOp->p2))!=
1a279 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 0 );. rc = sqli
1a27a 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 te3BtreeClearTab
1a27b 6c 65 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 le(. db->aD
1a27c 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 b[pOp->p2].pBt,
1a27d 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 pOp->p1, (pOp->p
1a27e 33 20 3f 20 26 6e 43 68 61 6e 67 65 20 3a 20 30 3 ? &nChange : 0
1a27f 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 ). );. if( pOp
1a280 2d 3e 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e 6e ->p3 ){. p->n
1a281 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 Change += nChang
1a282 65 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e e;. if( pOp->
1a283 70 33 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 2d p3>0 ){. p-
1a284 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 >aMem[pOp->p3].u
1a285 2e 69 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 .i += nChange;.
1a286 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1a287 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1a288 43 72 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 CreateTable P1 P
1a289 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 2 * * *.**.** Al
1a28a 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 locate a new tab
1a28b 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 le in the main d
1a28c 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 atabase file if
1a28d 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a P1==0 or in the.
1a28e 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 ** auxiliary dat
1a28f 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 abase file if P1
1a290 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 ==1 or in an att
1a291 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 ached database i
1a292 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 f.** P1>1. Writ
1a293 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 e the root page
1a294 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 number of the ne
1a295 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 w table into.**
1a296 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a register P2.**.*
1a297 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 * The difference
1a298 20 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 between a table
1a299 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 and an index is
1a29a 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 this: A table
1a29b 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 must.** have a 4
1a29c 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 -byte integer ke
1a29d 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 y and can have a
1a29e 72 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 rbitrary data.
1a29f 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 An index.** has
1a2a0 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 an arbitrary key
1a2a1 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a but no data..**
1a2a2 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 .** See also: Cr
1a2a3 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 eateIndex.*/./*
1a2a4 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e Opcode: CreateIn
1a2a5 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a dex P1 P2 * * *.
1a2a6 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 **.** Allocate a
1a2a7 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 new index in th
1a2a8 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1a2a9 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 file if P1==0 or
1a2aa 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c in the.** auxil
1a2ab 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 iary database fi
1a2ac 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 le if P1==1 or i
1a2ad 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 n an attached da
1a2ae 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e tabase if.** P1>
1a2af 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 1. Write the ro
1a2b0 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f ot page number o
1a2b1 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 f the new table
1a2b2 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 into.** register
1a2b3 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 P2..**.** See d
1a2b4 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 ocumentation on
1a2b5 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 OP_CreateTable f
1a2b6 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
1a2b7 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 formation..*/.ca
1a2b8 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 se OP_CreateInde
1a2b9 78 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a x: /*
1a2ba 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1a2bb 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 */.case OP_Crea
1a2bc 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 teTable: {
1a2bd 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1a2be 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 elease */. int
1a2bf 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 pgno;. int flag
1a2c0 73 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 s;. Db *pDb;.
1a2c1 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1a2c2 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 =0 && pOp->p1<db
1a2c3 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1a2c4 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1a2c5 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 & (1<<pOp->p1))
1a2c6 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 !=0 );. pDb = &
1a2c7 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d db->aDb[pOp->p1]
1a2c8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 2d ;. assert( pDb-
1a2c9 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 >pBt!=0 );. if(
1a2ca 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1a2cb 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29 7b 0a _CreateTable ){.
1a2cc 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d 20 42 /* flags = B
1a2cd 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a TREE_INTKEY; */.
1a2ce 20 20 20 20 66 6c 61 67 73 20 3d 20 42 54 52 45 flags = BTRE
1a2cf 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 45 45 E_LEAFDATA|BTREE
1a2d0 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 65 _INTKEY;. }else
1a2d1 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 42 54 {. flags = BT
1a2d2 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 20 REE_ZERODATA;.
1a2d3 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 }. rc = sqlite3
1a2d4 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 BtreeCreateTable
1a2d5 28 70 44 62 2d 3e 70 42 74 2c 20 26 70 67 6e 6f (pDb->pBt, &pgno
1a2d6 2c 20 66 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 , flags);. if(
1a2d7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1a2d8 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d . pOut->u.i =
1a2d9 20 70 67 6e 6f 3b 0a 20 20 20 20 4d 65 6d 53 65 pgno;. MemSe
1a2da 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1a2db 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 MEM_Int);. }.
1a2dc 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1a2dd 6f 64 65 3a 20 50 61 72 73 65 53 63 68 65 6d 61 ode: ParseSchema
1a2de 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a P1 P2 * P4 *.**
1a2df 0a 2a 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 .** Read and par
1a2e0 73 65 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 se all entries f
1a2e1 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 45 5f 4d rom the SQLITE_M
1a2e2 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66 20 64 ASTER table of d
1a2e3 61 74 61 62 61 73 65 20 50 31 0a 2a 2a 20 74 68 atabase P1.** th
1a2e4 61 74 20 6d 61 74 63 68 20 74 68 65 20 57 48 45 at match the WHE
1a2e5 52 45 20 63 6c 61 75 73 65 20 50 34 2e 20 20 50 RE clause P4. P
1a2e6 32 20 69 73 20 74 68 65 20 22 66 6f 72 63 65 22 2 is the "force"
1a2e7 20 66 6c 61 67 2e 20 20 20 41 6c 77 61 79 73 20 flag. Always
1a2e8 64 6f 0a 2a 2a 20 74 68 65 20 70 61 72 73 69 6e do.** the parsin
1a2e9 67 20 69 66 20 50 32 20 69 73 20 74 72 75 65 2e g if P2 is true.
1a2ea 20 20 49 66 20 50 32 20 69 73 20 66 61 6c 73 65 If P2 is false
1a2eb 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
1a2ec 69 6e 65 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f ine is a.** no-o
1a2ed 70 20 69 66 20 74 68 65 20 73 63 68 65 6d 61 20 p if the schema
1a2ee 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
1a2ef 20 6c 6f 61 64 65 64 2e 20 20 49 6e 20 6f 74 68 loaded. In oth
1a2f0 65 72 20 77 6f 72 64 73 2c 20 69 66 20 50 32 0a er words, if P2.
1a2f1 2a 2a 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 ** is false, the
1a2f2 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 SQLITE_MASTER t
1a2f3 61 62 6c 65 20 69 73 20 6f 6e 6c 79 20 70 61 72 able is only par
1a2f4 73 65 64 20 69 66 20 74 68 65 20 72 65 73 74 20 sed if the rest
1a2f5 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 of the.** schema
1a2f6 20 69 73 20 61 6c 72 65 61 64 79 20 6c 6f 61 64 is already load
1a2f7 65 64 20 69 6e 74 6f 20 74 68 65 20 73 79 6d 62 ed into the symb
1a2f8 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ol table..**.**
1a2f9 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f This opcode invo
1a2fa 6b 65 73 20 74 68 65 20 70 61 72 73 65 72 20 74 kes the parser t
1a2fb 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 o create a new v
1a2fc 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2c 0a irtual machine,.
1a2fd 2a 2a 20 74 68 65 6e 20 72 75 6e 73 20 74 68 65 ** then runs the
1a2fe 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63 new virtual mac
1a2ff 68 69 6e 65 2e 20 20 49 74 20 69 73 20 74 68 75 hine. It is thu
1a300 73 20 61 20 72 65 2d 65 6e 74 72 61 6e 74 20 6f s a re-entrant o
1a301 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f pcode..*/.case O
1a302 50 5f 50 61 72 73 65 53 63 68 65 6d 61 3a 20 7b P_ParseSchema: {
1a303 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 . char *zSql;.
1a304 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e int iDb = pOp->
1a305 70 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 p1;. const char
1a306 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 49 6e 69 *zMaster;. Ini
1a307 74 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a tData initData;.
1a308 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d . assert( iDb>=
1a309 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 0 && iDb<db->nDb
1a30a 20 29 3b 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e );. if( !pOp->
1a30b 70 32 20 26 26 20 21 44 62 48 61 73 50 72 6f 70 p2 && !DbHasProp
1a30c 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 erty(db, iDb, DB
1a30d 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 _SchemaLoaded) )
1a30e 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d {. break;. }
1a30f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 53 43 48 . zMaster = SCH
1a310 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a EMA_TABLE(iDb);.
1a311 20 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 initData.db =
1a312 64 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 69 db;. initData.i
1a313 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 Db = pOp->p1;.
1a314 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 initData.pzErrMs
1a315 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b g = &p->zErrMsg;
1a316 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 . zSql = sqlite
1a317 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20 20 3MPrintf(db,.
1a318 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20 "SELECT name,
1a319 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52 rootpage, sql FR
1a31a 4f 4d 20 27 25 71 27 2e 25 73 20 57 48 45 52 45 OM '%q'.%s WHERE
1a31b 20 25 73 22 2c 0a 20 20 20 20 20 64 62 2d 3e 61 %s",. db->a
1a31c 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a Db[iDb].zName, z
1a31d 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 34 2e Master, pOp->p4.
1a31e 7a 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d z);. if( zSql==
1a31f 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0 ) goto no_mem;
1a320 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 . (void)sqlite3
1a321 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 SafetyOff(db);.
1a322 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 assert( db->ini
1a323 74 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 64 t.busy==0 );. d
1a324 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 b->init.busy = 1
1a325 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 72 63 20 ;. initData.rc
1a326 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 = SQLITE_OK;. a
1a327 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c ssert( !db->mall
1a328 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63 ocFailed );. rc
1a329 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 = sqlite3_exec(
1a32a 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 db, zSql, sqlite
1a32b 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26 3InitCallback, &
1a32c 69 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 initData, 0);.
1a32d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1a32e 4b 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61 74 K ) rc = initDat
1a32f 61 2e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 44 a.rc;. sqlite3D
1a330 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b bFree(db, zSql);
1a331 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 . db->init.busy
1a332 20 3d 20 30 3b 0a 20 20 28 76 6f 69 64 29 73 71 = 0;. (void)sq
1a333 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1a334 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1a335 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 ITE_NOMEM ){.
1a336 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1a337 7d 0a 20 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 0a }. break; .}..
1a338 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
1a339 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 ITE_OMIT_ANALYZE
1a33a 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
1a33b 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 LITE_OMIT_PARSER
1a33c 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 )./* Opcode: Loa
1a33d 64 41 6e 61 6c 79 73 69 73 20 50 31 20 2a 20 2a dAnalysis P1 * *
1a33e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 * *.**.** Read
1a33f 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 the sqlite_stat1
1a340 20 74 61 62 6c 65 20 66 6f 72 20 64 61 74 61 62 table for datab
1a341 61 73 65 20 50 31 20 61 6e 64 20 6c 6f 61 64 20 ase P1 and load
1a342 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f the content.** o
1a343 66 20 74 68 61 74 20 74 61 62 6c 65 20 69 6e 74 f that table int
1a344 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 69 o the internal i
1a345 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 2e ndex hash table.
1a346 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 This will caus
1a347 65 0a 2a 2a 20 74 68 65 20 61 6e 61 6c 79 73 69 e.** the analysi
1a348 73 20 74 6f 20 62 65 20 75 73 65 64 20 77 68 65 s to be used whe
1a349 6e 20 70 72 65 70 61 72 69 6e 67 20 61 6c 6c 20 n preparing all
1a34a 73 75 62 73 65 71 75 65 6e 74 20 71 75 65 72 69 subsequent queri
1a34b 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c es..*/.case OP_L
1a34c 6f 61 64 41 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 oadAnalysis: {.
1a34d 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e int iDb = pOp->
1a34e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 p1;. assert( iD
1a34f 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e b>=0 && iDb<db->
1a350 6e 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 nDb );. rc = sq
1a351 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 lite3AnalysisLoa
1a352 64 28 64 62 2c 20 69 44 62 29 3b 0a 20 20 62 72 d(db, iDb);. br
1a353 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 eak; .}.#endif
1a354 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 /* !defined(SQLI
1a355 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 TE_OMIT_ANALYZE)
1a356 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
1a357 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 29 ITE_OMIT_PARSER)
1a358 20 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a */../* Opcode:
1a359 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20 2a 20 DropTable P1 *
1a35a 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d * P4 *.**.** Rem
1a35b 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ove the internal
1a35c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 (in-memory) dat
1a35d 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 a structures tha
1a35e 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 t describe.** th
1a35f 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 50 34 e table named P4
1a360 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e in database P1.
1a361 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 This is called
1a362 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 0a 2a after a table.*
1a363 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 * is dropped in
1a364 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 order to keep th
1a365 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 e internal repre
1a366 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 sentation of the
1a367 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 .** schema consi
1a368 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 stent with what
1a369 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 is on disk..*/.c
1a36a 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 ase OP_DropTable
1a36b 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c : {. sqlite3Unl
1a36c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c inkAndDeleteTabl
1a36d 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 e(db, pOp->p1, p
1a36e 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 Op->p4.z);. bre
1a36f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1a370 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31 20 2a : DropIndex P1 *
1a371 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 * P4 *.**.** Re
1a372 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 move the interna
1a373 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 l (in-memory) da
1a374 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68 ta structures th
1a375 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 at describe.** t
1a376 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 50 he index named P
1a377 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 4 in database P1
1a378 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 . This is calle
1a379 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 65 78 d after an index
1a37a 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 .** is dropped i
1a37b 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
1a37c 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
1a37d 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
1a37e 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e he.** schema con
1a37f 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 sistent with wha
1a380 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f t is on disk..*/
1a381 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64 .case OP_DropInd
1a382 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 ex: {. sqlite3U
1a383 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e nlinkAndDeleteIn
1a384 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c dex(db, pOp->p1,
1a385 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 pOp->p4.z);. b
1a386 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1a387 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65 72 20 de: DropTrigger
1a388 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a P1 * * P4 *.**.*
1a389 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 * Remove the int
1a38a 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 ernal (in-memory
1a38b 29 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 ) data structure
1a38c 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a s that describe.
1a38d 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 6e ** the trigger n
1a38e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 61 62 amed P4 in datab
1a38f 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 69 73 ase P1. This is
1a390 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20 called after a
1a391 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 64 72 trigger.** is dr
1a392 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74 opped in order t
1a393 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72 o keep the inter
1a394 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 nal representati
1a395 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 on of the.** sch
1a396 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 ema consistent w
1a397 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 ith what is on d
1a398 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f isk..*/.case OP_
1a399 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b 0a 20 DropTrigger: {.
1a39a 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e sqlite3UnlinkAn
1a39b 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 dDeleteTrigger(d
1a39c 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d b, pOp->p1, pOp-
1a39d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b >p4.z);. break;
1a39e 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c .}...#ifndef SQL
1a39f 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 ITE_OMIT_INTEGRI
1a3a0 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f TY_CHECK./* Opco
1a3a1 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43 6b 20 de: IntegrityCk
1a3a2 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a P1 P2 P3 * P5.**
1a3a3 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73 .** Do an analys
1a3a4 69 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e is of the curren
1a3a5 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62 61 73 tly open databas
1a3a6 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a 2a 20 e. Store in.**
1a3a7 72 65 67 69 73 74 65 72 20 50 31 20 74 68 65 20 register P1 the
1a3a8 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 text of an error
1a3a9 20 6d 65 73 73 61 67 65 20 64 65 73 63 72 69 62 message describ
1a3aa 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 ing any problems
1a3ab 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f 62 6c ..** If no probl
1a3ac 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c 20 73 ems are found, s
1a3ad 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 72 tore a NULL in r
1a3ae 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a egister P1..**.*
1a3af 2a 20 54 68 65 20 72 65 67 69 73 74 65 72 20 50 * The register P
1a3b0 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 3 contains the m
1a3b1 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
1a3b2 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e allowed errors.
1a3b3 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65 67 28 .** At most reg(
1a3b4 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c 6c 20 P3) errors will
1a3b5 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20 be reported..**
1a3b6 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1a3b7 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 74 6f the analysis sto
1a3b8 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 72 65 ps as soon as re
1a3b9 67 28 50 31 29 20 65 72 72 6f 72 73 20 61 72 65 g(P1) errors are
1a3ba 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65 67 28 .** seen. Reg(
1a3bb 50 31 29 20 69 73 20 75 70 64 61 74 65 64 20 77 P1) is updated w
1a3bc 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ith the number o
1a3bd 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 f errors remaini
1a3be 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f ng..**.** The ro
1a3bf 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 ot page numbers
1a3c0 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e of all tables in
1a3c1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 72 the database ar
1a3c2 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73 74 6f e integer.** sto
1a3c3 72 65 64 20 69 6e 20 72 65 67 28 50 31 29 2c 20 red in reg(P1),
1a3c4 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67 28 50 reg(P1+1), reg(P
1a3c5 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 72 1+2), .... Ther
1a3c6 65 20 61 72 65 20 50 32 20 74 61 62 6c 65 73 0a e are P2 tables.
1a3c7 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 ** total..**.**
1a3c8 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a 65 72 If P5 is not zer
1a3c9 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69 73 20 o, the check is
1a3ca 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75 78 69 done on the auxi
1a3cb 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a liary database.*
1a3cc 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68 65 20 * file, not the
1a3cd 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1a3ce 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f le..**.** This o
1a3cf 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f pcode is used to
1a3d0 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 69 implement the i
1a3d1 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 70 ntegrity_check p
1a3d2 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f ragma..*/.case O
1a3d3 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a 20 7b P_IntegrityCk: {
1a3d4 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 . int nRoot;
1a3d5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1a3d6 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e tables to check.
1a3d7 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f (Number of roo
1a3d8 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 t pages.) */. i
1a3d9 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f nt *aRoot; /
1a3da 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 * Array of rootp
1a3db 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 age numbers for
1a3dc 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 tables to be che
1a3dd 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b cked */. int j;
1a3de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1a3df 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
1a3e0 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f nt nErr; /
1a3e1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f * Number of erro
1a3e2 72 73 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 rs reported */.
1a3e3 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 char *z;
1a3e4 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
1a3e5 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a error report */.
1a3e6 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 Mem *pnErr;
1a3e7 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 /* Register ke
1a3e8 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 eping track of e
1a3e9 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 rrors remaining
1a3ea 2a 2f 0a 20 20 0a 20 20 6e 52 6f 6f 74 20 3d 20 */. . nRoot =
1a3eb 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 pOp->p2;. asser
1a3ec 74 28 20 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20 t( nRoot>0 );.
1a3ed 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 44 aRoot = sqlite3D
1a3ee 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 bMallocRaw(db, s
1a3ef 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 52 6f 6f izeof(int)*(nRoo
1a3f0 74 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 61 52 t+1) );. if( aR
1a3f1 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f oot==0 ) goto no
1a3f2 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 _mem;. assert(
1a3f3 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 pOp->p3>0 && pOp
1a3f4 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1a3f5 0a 20 20 70 6e 45 72 72 20 3d 20 26 70 2d 3e 61 . pnErr = &p->a
1a3f6 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1a3f7 61 73 73 65 72 74 28 20 28 70 6e 45 72 72 2d 3e assert( (pnErr->
1a3f8 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 flags & MEM_Int)
1a3f9 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1a3fa 20 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 (pnErr->flags &
1a3fb 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c (MEM_Str|MEM_Bl
1a3fc 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e ob))==0 );. pIn
1a3fd 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 1 = &p->aMem[pOp
1a3fe 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 6a 3d 30 ->p1];. for(j=0
1a3ff 3b 20 6a 3c 6e 52 6f 6f 74 3b 20 6a 2b 2b 29 7b ; j<nRoot; j++){
1a400 0a 20 20 20 20 61 52 6f 6f 74 5b 6a 5d 20 3d 20 . aRoot[j] =
1a401 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1a402 6c 75 65 28 26 70 49 6e 31 5b 6a 5d 29 3b 0a 20 lue(&pIn1[j]);.
1a403 20 7d 0a 20 20 61 52 6f 6f 74 5b 6a 5d 20 3d 20 }. aRoot[j] =
1a404 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 0;. assert( pOp
1a405 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a ->p5<db->nDb );.
1a406 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1a407 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
1a408 70 2d 3e 70 35 29 29 21 3d 30 20 29 3b 0a 20 20 p->p5))!=0 );.
1a409 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 z = sqlite3Btree
1a40a 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 64 IntegrityCheck(d
1a40b 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e b->aDb[pOp->p5].
1a40c 70 42 74 2c 20 61 52 6f 6f 74 2c 20 6e 52 6f 6f pBt, aRoot, nRoo
1a40d 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
1a40e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a40f 20 20 20 20 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 pnErr->u.i,
1a410 26 6e 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 &nErr);. sqlite
1a411 33 44 62 46 72 65 65 28 64 62 2c 20 61 52 6f 6f 3DbFree(db, aRoo
1a412 74 29 3b 0a 20 20 70 6e 45 72 72 2d 3e 75 2e 69 t);. pnErr->u.i
1a413 20 2d 3d 20 6e 45 72 72 3b 0a 20 20 73 71 6c 69 -= nErr;. sqli
1a414 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1a415 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 6e l(pIn1);. if( n
1a416 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 Err==0 ){. as
1a417 73 65 72 74 28 20 7a 3d 3d 30 20 29 3b 0a 20 20 sert( z==0 );.
1a418 7d 65 6c 73 65 20 69 66 28 20 7a 3d 3d 30 20 29 }else if( z==0 )
1a419 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 {. goto no_me
1a41a 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 m;. }else{.
1a41b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1a41c 74 53 74 72 28 70 49 6e 31 2c 20 7a 2c 20 2d 31 tStr(pIn1, z, -1
1a41d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73 , SQLITE_UTF8, s
1a41e 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 qlite3_free);.
1a41f 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 }. UPDATE_MAX_B
1a420 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 LOBSIZE(pIn1);.
1a421 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1a422 67 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31 2c geEncoding(pIn1,
1a423 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72 encoding);. br
1a424 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1a425 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
1a426 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a EGRITY_CHECK */.
1a427 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 69 66 6f ./* Opcode: Fifo
1a428 57 72 69 74 65 20 50 31 20 2a 20 2a 20 2a 20 2a Write P1 * * * *
1a429 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 .**.** Write the
1a42a 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 72 65 integer from re
1a42b 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 74 gister P1 into t
1a42c 68 65 20 46 69 66 6f 2e 0a 2a 2f 0a 63 61 73 65 he Fifo..*/.case
1a42d 20 4f 50 5f 46 69 66 6f 57 72 69 74 65 3a 20 7b OP_FifoWrite: {
1a42e 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a /* in1 *
1a42f 2f 0a 20 20 70 2d 3e 73 46 69 66 6f 2e 64 62 20 /. p->sFifo.db
1a430 3d 20 64 62 3b 0a 20 20 69 66 28 20 73 71 6c 69 = db;. if( sqli
1a431 74 65 33 56 64 62 65 46 69 66 6f 50 75 73 68 28 te3VdbeFifoPush(
1a432 26 70 2d 3e 73 46 69 66 6f 2c 20 73 71 6c 69 74 &p->sFifo, sqlit
1a433 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 e3VdbeIntValue(p
1a434 49 6e 31 29 29 3d 3d 53 51 4c 49 54 45 5f 4e 4f In1))==SQLITE_NO
1a435 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 MEM ){. goto
1a436 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 no_mem;. }. br
1a437 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1a438 65 3a 20 46 69 66 6f 52 65 61 64 20 50 31 20 50 e: FifoRead P1 P
1a439 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 74 2 * * *.**.** At
1a43a 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 61 20 tempt to read a
1a43b 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72 20 66 single integer f
1a43c 72 6f 6d 20 74 68 65 20 46 69 66 6f 2e 20 20 53 rom the Fifo. S
1a43d 74 6f 72 65 20 74 68 61 74 0a 2a 2a 20 69 6e 74 tore that.** int
1a43e 65 67 65 72 20 69 6e 20 72 65 67 69 73 74 65 72 eger in register
1a43f 20 50 31 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 P1..** .** If t
1a440 68 65 20 46 69 66 6f 20 69 73 20 65 6d 70 74 79 he Fifo is empty
1a441 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2f 0a jump to P2..*/.
1a442 63 61 73 65 20 4f 50 5f 46 69 66 6f 52 65 61 64 case OP_FifoRead
1a443 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6a : { /* j
1a444 75 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 ump */. CHECK_F
1a445 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 OR_INTERRUPT;.
1a446 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1a447 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 0 && pOp->p1<=p-
1a448 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 >nMem );. pOut
1a449 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1a44a 70 31 5d 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 p1];. MemSetTyp
1a44b 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1a44c 49 6e 74 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 Int);. if( sqli
1a44d 74 65 33 56 64 62 65 46 69 66 6f 50 6f 70 28 26 te3VdbeFifoPop(&
1a44e 70 2d 3e 73 46 69 66 6f 2c 20 26 70 4f 75 74 2d p->sFifo, &pOut-
1a44f 3e 75 2e 69 29 3d 3d 53 51 4c 49 54 45 5f 44 4f >u.i)==SQLITE_DO
1a450 4e 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 NE ){. pc = p
1a451 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a Op->p2 - 1;. }.
1a452 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e break;.}..#ifn
1a453 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1a454 54 52 49 47 47 45 52 0a 2f 2a 20 4f 70 63 6f 64 TRIGGER./* Opcod
1a455 65 3a 20 43 6f 6e 74 65 78 74 50 75 73 68 20 2a e: ContextPush *
1a456 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a 20 53 61 76 65 * * .**.** Save
1a457 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62 the current Vdb
1a458 65 20 63 6f 6e 74 65 78 74 20 73 75 63 68 20 74 e context such t
1a459 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 hat it can be re
1a45a 73 74 6f 72 65 64 20 62 79 20 61 20 43 6f 6e 74 stored by a Cont
1a45b 65 78 74 50 6f 70 0a 2a 2a 20 6f 70 63 6f 64 65 extPop.** opcode
1a45c 2e 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 . The context st
1a45d 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e ores the last in
1a45e 73 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 sert row id, the
1a45f 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 20 last statement
1a460 63 68 61 6e 67 65 0a 2a 2a 20 63 6f 75 6e 74 2c change.** count,
1a461 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 and the current
1a462 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 statement chang
1a463 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 e count..*/.case
1a464 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73 68 3a OP_ContextPush:
1a465 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 2d 3e {. int i = p->
1a466 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 2b contextStackTop+
1a467 2b 3b 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 +;. Context *pC
1a468 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 ontext;.. asser
1a469 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 2f 2a 20 t( i>=0 );. /*
1a46a 46 49 58 20 4d 45 3a 20 54 68 69 73 20 73 68 6f FIX ME: This sho
1a46b 75 6c 64 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 uld be allocated
1a46c 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
1a46d 76 64 62 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d vdbe at compile-
1a46e 74 69 6d 65 20 2a 2f 0a 20 20 69 66 28 20 69 3e time */. if( i>
1a46f 3d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b =p->contextStack
1a470 44 65 70 74 68 20 29 7b 0a 20 20 20 20 70 2d 3e Depth ){. p->
1a471 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 contextStackDept
1a472 68 20 3d 20 69 2b 31 3b 0a 20 20 20 20 70 2d 3e h = i+1;. p->
1a473 63 6f 6e 74 65 78 74 53 74 61 63 6b 20 3d 20 73 contextStack = s
1a474 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f qlite3DbReallocO
1a475 72 46 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f 6e rFree(db, p->con
1a476 74 65 78 74 53 74 61 63 6b 2c 0a 20 20 20 20 20 textStack,.
1a477 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a478 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a479 20 20 20 20 20 73 69 7a 65 6f 66 28 43 6f 6e 74 sizeof(Cont
1a47a 65 78 74 29 2a 28 69 2b 31 29 29 3b 0a 20 20 20 ext)*(i+1));.
1a47b 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53 if( p->contextS
1a47c 74 61 63 6b 3d 3d 30 20 29 20 67 6f 74 6f 20 6e tack==0 ) goto n
1a47d 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 43 6f o_mem;. }. pCo
1a47e 6e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74 ntext = &p->cont
1a47f 65 78 74 53 74 61 63 6b 5b 69 5d 3b 0a 20 20 70 extStack[i];. p
1a480 43 6f 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f 77 Context->lastRow
1a481 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 id = db->lastRow
1a482 69 64 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e id;. pContext->
1a483 6e 43 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 nChange = p->nCh
1a484 61 6e 67 65 3b 0a 20 20 70 43 6f 6e 74 65 78 74 ange;. pContext
1a485 2d 3e 73 46 69 66 6f 20 3d 20 70 2d 3e 73 46 69 ->sFifo = p->sFi
1a486 66 6f 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 fo;. sqlite3Vdb
1a487 65 46 69 66 6f 49 6e 69 74 28 26 70 2d 3e 73 46 eFifoInit(&p->sF
1a488 69 66 6f 2c 20 64 62 29 3b 0a 20 20 62 72 65 61 ifo, db);. brea
1a489 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1a48a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a 20 2a 20 ContextPop * *
1a48b 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 * .**.** Restore
1a48c 20 74 68 65 20 56 64 62 65 20 63 6f 6e 74 65 78 the Vdbe contex
1a48d 74 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 69 t to the state i
1a48e 74 20 77 61 73 20 69 6e 20 77 68 65 6e 20 63 6f t was in when co
1a48f 6e 74 65 78 74 50 75 73 68 20 77 61 73 20 6c 61 ntextPush was la
1a490 73 74 0a 2a 2a 20 65 78 65 63 75 74 65 64 2e 20 st.** executed.
1a491 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 6f 72 The context stor
1a492 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 es the last inse
1a493 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 20 6c rt row id, the l
1a494 61 73 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a ast statement.**
1a495 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c 20 61 change count, a
1a496 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 nd the current s
1a497 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 tatement change
1a498 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f count..*/.case O
1a499 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a 20 7b 0a P_ContextPop: {.
1a49a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 Context *pCont
1a49b 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74 65 78 ext = &p->contex
1a49c 74 53 74 61 63 6b 5b 2d 2d 70 2d 3e 63 6f 6e 74 tStack[--p->cont
1a49d 65 78 74 53 74 61 63 6b 54 6f 70 5d 3b 0a 20 20 extStackTop];.
1a49e 61 73 73 65 72 74 28 20 70 2d 3e 63 6f 6e 74 65 assert( p->conte
1a49f 78 74 53 74 61 63 6b 54 6f 70 3e 3d 30 20 29 3b xtStackTop>=0 );
1a4a0 0a 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 . db->lastRowid
1a4a1 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61 73 = pContext->las
1a4a2 74 52 6f 77 69 64 3b 0a 20 20 70 2d 3e 6e 43 68 tRowid;. p->nCh
1a4a3 61 6e 67 65 20 3d 20 70 43 6f 6e 74 65 78 74 2d ange = pContext-
1a4a4 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 73 71 6c 69 >nChange;. sqli
1a4a5 74 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72 te3VdbeFifoClear
1a4a6 28 26 70 2d 3e 73 46 69 66 6f 29 3b 0a 20 20 70 (&p->sFifo);. p
1a4a7 2d 3e 73 46 69 66 6f 20 3d 20 70 43 6f 6e 74 65 ->sFifo = pConte
1a4a8 78 74 2d 3e 73 46 69 66 6f 3b 0a 20 20 62 72 65 xt->sFifo;. bre
1a4a9 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1a4aa 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a4ab 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a MIT_TRIGGER */..
1a4ac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a4ad 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e MIT_AUTOINCREMEN
1a4ae 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d T./* Opcode: Mem
1a4af 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Max P1 P2 * * *.
1a4b0 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 **.** Set the va
1a4b1 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 lue of register
1a4b2 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 P1 to the maximu
1a4b3 6d 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e 74 m of its current
1a4b4 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20 74 68 value.** and th
1a4b5 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1a4b6 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 ter P2..**.** Th
1a4b7 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 is instruction t
1a4b8 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 20 69 hrows an error i
1a4b9 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c f the memory cel
1a4ba 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c l is not initial
1a4bb 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 ly.** an integer
1a4bc 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d ..*/.case OP_Mem
1a4bd 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a Max: { /*
1a4be 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20 20 73 in1, in2 */. s
1a4bf 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1a4c0 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 egerify(pIn1);.
1a4c1 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1a4c2 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b ntegerify(pIn2);
1a4c3 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 . if( pIn1->u.i
1a4c4 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 20 <pIn2->u.i){.
1a4c5 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e pIn1->u.i = pIn
1a4c6 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 2->u.i;. }. br
1a4c7 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1a4c8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1a4c9 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f OINCREMENT */../
1a4ca 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20 * Opcode: IfPos
1a4cb 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1a4cc 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f * If the value o
1a4cd 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 f register P1 is
1a4ce 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a 1 or greater, j
1a4cf 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a ump to P2..**.**
1a4d0 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 It is illegal t
1a4d1 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 o use this instr
1a4d2 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 uction on a regi
1a4d3 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a ster that does.*
1a4d4 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e * not contain an
1a4d5 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 integer. An as
1a4d6 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 sertion fault wi
1a4d7 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 ll result if you
1a4d8 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 try..*/.case OP
1a4d9 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20 _IfPos: {
1a4da 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f /* jump, in1 */
1a4db 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d . assert( pIn1-
1a4dc 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 >flags&MEM_Int )
1a4dd 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e ;. if( pIn1->u.
1a4de 69 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d i>0 ){. pc =
1a4df 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1a4e0 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1a4e1 20 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20 50 Opcode: IfNeg P
1a4e2 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1a4e3 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 If the value of
1a4e4 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1a4e5 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 less than zero,
1a4e6 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a jump to P2. .**.
1a4e7 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c ** It is illegal
1a4e8 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 to use this ins
1a4e9 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 truction on a re
1a4ea 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 gister that does
1a4eb 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 .** not contain
1a4ec 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 an integer. An
1a4ed 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
1a4ee 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 will result if y
1a4ef 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 ou try..*/.case
1a4f0 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20 20 OP_IfNeg: {
1a4f1 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 /* jump, in1
1a4f2 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e */. assert( pIn
1a4f3 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 1->flags&MEM_Int
1a4f4 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e );. if( pIn1->
1a4f5 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70 63 u.i<0 ){. pc
1a4f6 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1a4f7 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1a4f8 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65 72 /* Opcode: IfZer
1a4f9 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a o P1 P2 * * *.**
1a4fa 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1a4fb 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 of register P1
1a4fc 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75 is exactly 0, ju
1a4fd 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a mp to P2. .**.**
1a4fe 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 It is illegal t
1a4ff 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 o use this instr
1a500 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 uction on a regi
1a501 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a ster that does.*
1a502 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e * not contain an
1a503 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 integer. An as
1a504 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 sertion fault wi
1a505 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 ll result if you
1a506 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 try..*/.case OP
1a507 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 _IfZero: {
1a508 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a /* jump, in1 *
1a509 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 /. assert( pIn1
1a50a 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 ->flags&MEM_Int
1a50b 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 );. if( pIn1->u
1a50c 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63 .i==0 ){. pc
1a50d 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1a50e 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1a50f 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74 /* Opcode: AggSt
1a510 65 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35 ep * P2 P3 P4 P5
1a511 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 .**.** Execute t
1a512 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e he step function
1a513 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 for an aggregat
1a514 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74 e. The.** funct
1a515 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d ion has P5 argum
1a516 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20 ents. P4 is a
1a517 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 pointer to the F
1a518 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 uncDef.** struct
1a519 75 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69 ure that specifi
1a51a 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e es the function.
1a51b 20 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a Use register.*
1a51c 2a 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75 * P3 as the accu
1a51d 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 mulator..**.** T
1a51e 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 he P5 arguments
1a51f 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 are taken from r
1a520 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69 egister P2 and i
1a521 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 ts.** successors
1a522 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 ..*/.case OP_Agg
1a523 53 74 65 70 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 Step: {. int n
1a524 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 69 6e 74 = pOp->p5;. int
1a525 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c i;. Mem *pMem,
1a526 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74 65 *pRec;. sqlite
1a527 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 3_context ctx;.
1a528 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1a529 2a 61 70 56 61 6c 3b 0a 0a 20 20 61 73 73 65 72 *apVal;.. asser
1a52a 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 70 52 65 t( n>=0 );. pRe
1a52b 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 c = &p->aMem[pOp
1a52c 2d 3e 70 32 5d 3b 0a 20 20 61 70 56 61 6c 20 3d ->p2];. apVal =
1a52d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 p->apArg;. ass
1a52e 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20 6e 3d ert( apVal || n=
1a52f 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b =0 );. for(i=0;
1a530 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 52 65 63 2b i<n; i++, pRec+
1a531 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d +){. apVal[i]
1a532 20 3d 20 70 52 65 63 3b 0a 20 20 20 20 73 74 6f = pRec;. sto
1a533 72 65 54 79 70 65 49 6e 66 6f 28 70 52 65 63 2c reTypeInfo(pRec,
1a534 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a encoding);. }.
1a535 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f ctx.pFunc = pO
1a536 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61 p->p4.pFunc;. a
1a537 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1a538 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e && pOp->p3<=p->
1a539 6e 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e 70 4d nMem );. ctx.pM
1a53a 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70 2d 3e em = pMem = &p->
1a53b 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1a53c 20 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63 74 pMem->n++;. ct
1a53d 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f x.s.flags = MEM_
1a53e 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 7a 20 Null;. ctx.s.z
1a53f 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 = 0;. ctx.s.zMa
1a540 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 63 74 78 2e lloc = 0;. ctx.
1a541 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 63 74 s.xDel = 0;. ct
1a542 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 63 x.s.db = db;. c
1a543 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a tx.isError = 0;.
1a544 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b ctx.pColl = 0;
1a545 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75 6e 63 . if( ctx.pFunc
1a546 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
1a547 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 _FUNC_NEEDCOLL )
1a548 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f {. assert( pO
1a549 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 p>p->aOp );.
1a54a 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e assert( pOp[-1].
1a54b 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 p4type==P4_COLLS
1a54c 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 EQ );. assert
1a54d 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 ( pOp[-1].opcode
1a54e 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a ==OP_CollSeq );.
1a54f 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 ctx.pColl =
1a550 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c pOp[-1].p4.pColl
1a551 3b 0a 20 20 7d 0a 20 20 28 63 74 78 2e 70 46 75 ;. }. (ctx.pFu
1a552 6e 63 2d 3e 78 53 74 65 70 29 28 26 63 74 78 2c nc->xStep)(&ctx,
1a553 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66 n, apVal);. if
1a554 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b ( ctx.isError ){
1a555 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1a556 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1a557 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c g, db, "%s", sql
1a558 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1a559 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 &ctx.s));. rc
1a55a 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a = ctx.isError;.
1a55b 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
1a55c 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74 78 eMemRelease(&ctx
1a55d 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a .s);. break;.}.
1a55e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 46 ./* Opcode: AggF
1a55f 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 inal P1 P2 * P4
1a560 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 *.**.** Execute
1a561 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 66 75 the finalizer fu
1a562 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 nction for an ag
1a563 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 73 0a gregate. P1 is.
1a564 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f ** the memory lo
1a565 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 74 cation that is t
1a566 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 66 he accumulator f
1a567 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 74 65 or the aggregate
1a568 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 ..**.** P2 is th
1a569 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 e number of argu
1a56a 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 73 ments that the s
1a56b 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b tep function tak
1a56c 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 73 20 es and.** P4 is
1a56d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1a56e 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 68 69 FuncDef for thi
1a56f 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 s function. The
1a570 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 P2.** argument
1a571 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20 74 is not used by t
1a572 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 74 20 his opcode. It
1a573 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 74 6f is only there to
1a574 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a 2a 2a disambiguate.**
1a575 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
1a576 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 6e 67 can take varying
1a577 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 67 75 numbers of argu
1a578 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20 50 ments. The.** P
1a579 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 4 argument is on
1a57a 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 74 68 ly needed for th
1a57b 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 61 73 e degenerate cas
1a57c 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 73 e where.** the s
1a57d 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 tep function was
1a57e 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 not previously
1a57f 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 called..*/.case
1a580 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 0a 20 OP_AggFinal: {.
1a581 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 Mem *pMem;. as
1a582 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 sert( pOp->p1>0
1a583 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e && pOp->p1<=p->n
1a584 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 Mem );. pMem =
1a585 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1a586 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d ];. assert( (pM
1a587 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28 4d 45 em->flags & ~(ME
1a588 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29 M_Null|MEM_Agg))
1a589 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ==0 );. rc = sq
1a58a 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 lite3VdbeMemFina
1a58b 6c 69 7a 65 28 70 4d 65 6d 2c 20 70 4f 70 2d 3e lize(pMem, pOp->
1a58c 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66 28 p4.pFunc);. if(
1a58d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f rc==SQLITE_ERRO
1a58e 52 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 R ){. sqlite3
1a58f 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1a590 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c rrMsg, db, "%s",
1a591 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1a592 65 78 74 28 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a ext(pMem));. }.
1a593 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1a594 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d ngeEncoding(pMem
1a595 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 55 , encoding);. U
1a596 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1a597 5a 45 28 70 4d 65 6d 29 3b 0a 20 20 69 66 28 20 ZE(pMem);. if(
1a598 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f sqlite3VdbeMemTo
1a599 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a 20 20 oBig(pMem) ){.
1a59a 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1a59b 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1a59c 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
1a59d 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d LITE_OMIT_VACUUM
1a59e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
1a59f 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 LITE_OMIT_ATTACH
1a5a0 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63 )./* Opcode: Vac
1a5a1 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a uum * * * * *.**
1a5a2 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65 .** Vacuum the e
1a5a3 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e 20 ntire database.
1a5a4 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c This opcode wil
1a5a5 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 69 l cause other vi
1a5a6 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 rtual.** machine
1a5a7 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 s to be created
1a5a8 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79 and run. It may
1a5a9 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66 not be called f
1a5aa 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20 rom within.** a
1a5ab 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a transaction..*/.
1a5ac 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20 case OP_Vacuum:
1a5ad 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 {. if( sqlite3S
1a5ae 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 afetyOff(db) ) g
1a5af 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1a5b0 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d _misuse; . rc =
1a5b1 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 sqlite3RunVacuu
1a5b2 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 m(&p->zErrMsg, d
1a5b3 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 b);. if( sqlite
1a5b4 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 3SafetyOn(db) )
1a5b5 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1a5b6 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61 o_misuse;. brea
1a5b7 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 k;.}.#endif..#if
1a5b8 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1a5b9 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1a5ba 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63 )./* Opcode: Inc
1a5bb 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20 rVacuum P1 P2 *
1a5bc 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 * *.**.** Perfor
1a5bd 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 m a single step
1a5be 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 of the increment
1a5bf 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64 al vacuum proced
1a5c0 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 ure on.** the P1
1a5c1 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 database. If th
1a5c2 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e e vacuum has fin
1a5c3 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69 ished, jump to i
1a5c4 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 nstruction.** P2
1a5c5 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c . Otherwise, fal
1a5c6 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 l through to the
1a5c7 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1a5c8 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e n..*/.case OP_In
1a5c9 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20 crVacuum: {
1a5ca 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 /* jump */.
1a5cb 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 Btree *pBt;.. a
1a5cc 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1a5cd 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 0 && pOp->p1<db-
1a5ce 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1a5cf 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1a5d0 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 & (1<<pOp->p1))!
1a5d1 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 =0 );. pBt = db
1a5d2 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 ->aDb[pOp->p1].p
1a5d3 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 Bt;. rc = sqlit
1a5d4 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 e3BtreeIncrVacuu
1a5d5 6d 28 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 m(pBt);. if( rc
1a5d6 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
1a5d7 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1a5d8 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 2 - 1;. rc =
1a5d9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1a5da 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1a5db 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 78 70 ../* Opcode: Exp
1a5dc 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a ire P1 * * * *.*
1a5dd 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 63 6f *.** Cause preco
1a5de 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 mpiled statement
1a5df 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 70 69 s to become expi
1a5e0 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 64 20 red. An expired
1a5e1 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 61 69 statement.** fai
1a5e2 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 ls with an error
1a5e3 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f code of SQLITE_
1a5e4 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 73 20 SCHEMA if it is
1a5e5 65 76 65 72 20 65 78 65 63 75 74 65 64 20 0a 2a ever executed .*
1a5e6 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73 * (via sqlite3_s
1a5e7 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 tep())..** .** I
1a5e8 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20 f P1 is 0, then
1a5e9 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e all SQL statemen
1a5ea 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65 ts become expire
1a5eb 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d d. If P1 is non-
1a5ec 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e zero,.** then on
1a5ed 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 ly the currently
1a5ee 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 executing state
1a5ef 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65 64 ment is affected
1a5f0 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78 . .*/.case OP_Ex
1a5f1 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 21 70 pire: {. if( !p
1a5f2 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 Op->p1 ){. sq
1a5f3 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
1a5f4 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 redStatements(db
1a5f5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1a5f6 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a p->expired = 1;.
1a5f7 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1a5f8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a5f9 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1a5fa 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 62 6c ./* Opcode: Tabl
1a5fb 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 20 50 eLock P1 P2 P3 P
1a5fc 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e 4 *.**.** Obtain
1a5fd 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72 a lock on a par
1a5fe 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54 ticular table. T
1a5ff 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1a600 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 is only used whe
1a601 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d n.** the shared-
1a602 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73 cache feature is
1a603 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a enabled. .**.**
1a604 20 49 66 20 50 31 20 69 73 20 20 74 68 65 20 69 If P1 is the i
1a605 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 ndex of the data
1a606 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e base in sqlite3.
1a607 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 aDb[] of the dat
1a608 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 abase.** on whic
1a609 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63 h the lock is ac
1a60a 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c quired. A readl
1a60b 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ock is obtained
1a60c 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 if P3==0 or.** a
1a60d 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50 write lock if P
1a60e 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 3==1..**.** P2 c
1a60f 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74 ontains the root
1a610 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 -page of the tab
1a611 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a le to lock..**.*
1a612 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 * P4 contains a
1a613 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
1a614 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1a615 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 being locked. T
1a616 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 his is only.** u
1a617 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 sed to generate
1a618 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1a619 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e if the lock can
1a61a 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e not be obtained.
1a61b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c .*/.case OP_Tabl
1a61c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 69 6e 74 20 70 eLock: {. int p
1a61d 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 1 = pOp->p1; .
1a61e 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d u8 isWriteLock =
1a61f 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 pOp->p3;. asse
1a620 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 31 3c rt( p1>=0 && p1<
1a621 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 db->nDb );. ass
1a622 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
1a623 73 6b 20 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 sk & (1<<p1))!=0
1a624 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 );. assert( is
1a625 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 WriteLock==0 ||
1a626 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 isWriteLock==1 )
1a627 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1a628 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 BtreeLockTable(d
1a629 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 b->aDb[p1].pBt,
1a62a 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 pOp->p2, isWrite
1a62b 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 20 72 63 3d Lock);. if( rc=
1a62c 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 =SQLITE_LOCKED )
1a62d 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 {. const char
1a62e 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b *z = pOp->p4.z;
1a62f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1a630 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1a631 67 2c 20 64 62 2c 20 22 64 61 74 61 62 61 73 65 g, db, "database
1a632 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 table is locked
1a633 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 7d 0a 20 : %s", z);. }.
1a634 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1a635 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1a636 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a SHARED_CACHE */.
1a637 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a638 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1a639 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 E./* Opcode: VBe
1a63a 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a gin * * * P4 *.*
1a63b 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 *.** P4 may be a
1a63c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 pointer to an s
1a63d 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1a63e 63 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 cture. If so, ca
1a63f 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 ll the .** xBegi
1a640 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 n method for tha
1a641 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 t table..**.** A
1a642 6c 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20 lso, whether or
1a643 6e 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63 not P4 is set, c
1a644 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 heck that this i
1a645 73 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c s not being call
1a646 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 ed from.** withi
1a647 6e 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 n a callback to
1a648 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
1a649 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 xSync() method.
1a64a 49 66 20 69 74 20 69 73 2c 20 73 65 74 20 74 68 If it is, set th
1a64b 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 e.** error code
1a64c 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 to SQLITE_LOCKED
1a64d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65 ..*/.case OP_VBe
1a64e 67 69 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 gin: {. sqlite3
1a64f 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 _vtab *pVtab = p
1a650 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 Op->p4.pVtab;.
1a651 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 rc = sqlite3Vtab
1a652 42 65 67 69 6e 28 64 62 2c 20 70 56 74 61 62 29 Begin(db, pVtab)
1a653 3b 0a 20 20 69 66 28 20 70 56 74 61 62 20 29 7b ;. if( pVtab ){
1a654 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1a655 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1a656 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d g);. p->zErrM
1a657 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 sg = pVtab->zErr
1a658 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e Msg;. pVtab->
1a659 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d zErrMsg = 0;. }
1a65a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1a65b 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1a65c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1a65d 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1a65e 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1a65f 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1a660 43 72 65 61 74 65 20 50 31 20 2a 20 2a 20 50 34 Create P1 * * P4
1a661 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 *.**.** P4 is t
1a662 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 he name of a vir
1a663 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 tual table in da
1a664 74 61 62 61 73 65 20 50 31 2e 20 43 61 6c 6c 20 tabase P1. Call
1a665 74 68 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 the xCreate meth
1a666 6f 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20 74 od.** for that t
1a667 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 able..*/.case OP
1a668 5f 56 43 72 65 61 74 65 3a 20 7b 0a 20 20 72 63 _VCreate: {. rc
1a669 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 = sqlite3VtabCa
1a66a 6c 6c 43 72 65 61 74 65 28 64 62 2c 20 70 4f 70 llCreate(db, pOp
1a66b 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c ->p1, pOp->p4.z,
1a66c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 &p->zErrMsg);.
1a66d 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1a66e 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1a66f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a VIRTUALTABLE */.
1a670 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a671 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1a672 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 44 65 E./* Opcode: VDe
1a673 73 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 34 20 stroy P1 * * P4
1a674 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 *.**.** P4 is th
1a675 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74 e name of a virt
1a676 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74 ual table in dat
1a677 61 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c 20 abase P1. Call
1a678 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74 the xDestroy met
1a679 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 hod.** of that t
1a67a 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 able..*/.case OP
1a67b 5f 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20 20 70 _VDestroy: {. p
1a67c 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d ->inVtabMethod =
1a67d 20 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 2;. rc = sqlit
1a67e 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f e3VtabCallDestro
1a67f 79 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 y(db, pOp->p1, p
1a680 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e Op->p4.z);. p->
1a681 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 inVtabMethod = 0
1a682 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e ;. break;.}.#en
1a683 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1a684 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 IT_VIRTUALTABLE
1a685 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
1a686 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1a687 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ABLE./* Opcode:
1a688 56 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50 34 20 VOpen P1 * * P4
1a689 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 *.**.** P4 is a
1a68a 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 pointer to a vir
1a68b 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 tual table objec
1a68c 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 t, an sqlite3_vt
1a68d 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ab structure..**
1a68e 20 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20 P1 is a cursor
1a68f 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f 70 number. This op
1a690 63 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75 72 code opens a cur
1a691 73 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74 75 sor to the virtu
1a692 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 al.** table and
1a693 73 74 6f 72 65 73 20 74 68 61 74 20 63 75 72 73 stores that curs
1a694 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 or in P1..*/.cas
1a695 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a 20 20 e OP_VOpen: {.
1a696 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 VdbeCursor *pCur
1a697 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f = 0;. sqlite3_
1a698 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 vtab_cursor *pVt
1a699 61 62 43 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 abCursor = 0;..
1a69a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1a69b 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Vtab = pOp->p4.p
1a69c 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f Vtab;. sqlite3_
1a69d 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 module *pModule
1a69e 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c = (sqlite3_modul
1a69f 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 e *)pVtab->pModu
1a6a0 6c 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 56 le;.. assert(pV
1a6a1 74 61 62 20 26 26 20 70 4d 6f 64 75 6c 65 29 3b tab && pModule);
1a6a2 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1a6a3 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1a6a4 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1a6a5 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70 misuse;. rc = p
1a6a6 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 56 Module->xOpen(pV
1a6a7 74 61 62 2c 20 26 70 56 74 61 62 43 75 72 73 6f tab, &pVtabCurso
1a6a8 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 r);. sqlite3DbF
1a6a9 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1a6aa 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 sg);. p->zErrMs
1a6ab 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d g = pVtab->zErrM
1a6ac 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 sg;. pVtab->zEr
1a6ad 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 rMsg = 0;. if(
1a6ae 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1a6af 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1a6b0 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1a6b1 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
1a6b2 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e =rc ){. /* In
1a6b3 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33 itialize sqlite3
1a6b4 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 _vtab_cursor bas
1a6b5 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 70 e class */. p
1a6b6 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 VtabCursor->pVta
1a6b7 62 20 3d 20 70 56 74 61 62 3b 0a 0a 20 20 20 20 b = pVtab;..
1a6b8 2f 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 76 64 /* Initialise vd
1a6b9 62 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 be cursor object
1a6ba 20 2a 2f 0a 20 20 20 20 70 43 75 72 20 3d 20 61 */. pCur = a
1a6bb 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c llocateCursor(p,
1a6bc 20 70 4f 70 2d 3e 70 31 2c 20 26 70 4f 70 5b 2d pOp->p1, &pOp[-
1a6bd 31 5d 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 1], -1, 0);.
1a6be 69 66 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 if( pCur ){.
1a6bf 20 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 pCur->pVtabCur
1a6c0 73 6f 72 20 3d 20 70 56 74 61 62 43 75 72 73 6f sor = pVtabCurso
1a6c1 72 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 r;. pCur->p
1a6c2 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62 43 75 Module = pVtabCu
1a6c3 72 73 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f rsor->pVtab->pMo
1a6c4 64 75 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b dule;. }else{
1a6c5 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f . db->mallo
1a6c6 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
1a6c7 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f pModule->xClo
1a6c8 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b se(pVtabCursor);
1a6c9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 . }. }. bre
1a6ca 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1a6cb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1a6cc 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1a6cd 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1a6ce 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1a6cf 20 4f 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 Opcode: VFilter
1a6d0 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1a6d1 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 *.** P1 is a cur
1a6d2 73 6f 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 sor opened using
1a6d3 20 56 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 VOpen. P2 is a
1a6d4 6e 20 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d n address to jum
1a6d5 70 20 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 p to if.** the f
1a6d6 69 6c 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 iltered result s
1a6d7 65 74 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a et is empty..**.
1a6d8 2a 2a 20 50 34 20 69 73 20 65 69 74 68 65 72 20 ** P4 is either
1a6d9 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 NULL or a string
1a6da 20 74 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 that was genera
1a6db 74 65 64 20 62 79 20 74 68 65 20 78 42 65 73 74 ted by the xBest
1a6dc 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 Index.** method
1a6dd 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 of the module.
1a6de 54 68 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 The interpretati
1a6df 6f 6e 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 on of the P4 str
1a6e0 69 6e 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 ing is left.** t
1a6e1 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 o the module imp
1a6e2 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a lementation..**.
1a6e3 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 ** This opcode i
1a6e4 6e 76 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 nvokes the xFilt
1a6e5 65 72 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 er method on the
1a6e6 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 virtual table s
1a6e7 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 pecified.** by P
1a6e8 31 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 1. The integer
1a6e9 71 75 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d query plan param
1a6ea 65 74 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 eter to xFilter
1a6eb 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 is stored in reg
1a6ec 69 73 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 ister.** P3. Reg
1a6ed 69 73 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 ister P3+1 store
1a6ee 73 20 74 68 65 20 61 72 67 63 20 70 61 72 61 6d s the argc param
1a6ef 65 74 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 eter to be passe
1a6f0 64 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c d to the.** xFil
1a6f1 74 65 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 ter method. Regi
1a6f2 73 74 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 sters P3+2..P3+1
1a6f3 2b 61 72 67 63 20 61 72 65 20 74 68 65 20 61 72 +argc are the ar
1a6f4 67 63 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c gc.** additional
1a6f5 20 70 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 parameters whic
1a6f6 68 20 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a h are passed to.
1a6f7 2a 2a 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 ** xFilter as ar
1a6f8 67 76 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b gv. Register P3+
1a6f9 32 20 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 2 becomes argv[0
1a6fa 5d 20 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f ] when passed to
1a6fb 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 xFilter..**.**
1a6fc 41 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 A jump is made t
1a6fd 6f 20 50 32 20 69 66 20 74 68 65 20 72 65 73 75 o P2 if the resu
1a6fe 6c 74 20 73 65 74 20 61 66 74 65 72 20 66 69 6c lt set after fil
1a6ff 74 65 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 tering would be
1a700 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f empty..*/.case O
1a701 50 5f 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f P_VFilter: { /
1a702 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 * jump */. int
1a703 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 51 75 65 nArg;. int iQue
1a704 72 79 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 ry;. const sqli
1a705 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1a706 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75 65 ule;. Mem *pQue
1a707 72 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ry = &p->aMem[pO
1a708 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 20 2a 70 p->p3];. Mem *p
1a709 41 72 67 63 20 3d 20 26 70 51 75 65 72 79 5b 31 Argc = &pQuery[1
1a70a 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 ];. sqlite3_vta
1a70b 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 b_cursor *pVtabC
1a70c 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 ursor;. sqlite3
1a70d 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 0a 20 _vtab *pVtab;..
1a70e 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1a70f 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 r = p->apCsr[pOp
1a710 2d 3e 70 31 5d 3b 0a 0a 20 20 52 45 47 49 53 54 ->p1];.. REGIST
1a711 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 ER_TRACE(pOp->p3
1a712 2c 20 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 , pQuery);. ass
1a713 65 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 ert( pCur->pVtab
1a714 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 56 74 61 Cursor );. pVta
1a715 62 43 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e bCursor = pCur->
1a716 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 70 pVtabCursor;. p
1a717 56 74 61 62 20 3d 20 70 56 74 61 62 43 75 72 73 Vtab = pVtabCurs
1a718 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f or->pVtab;. pMo
1a719 64 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d dule = pVtab->pM
1a71a 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61 odule;.. /* Gra
1a71b 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62 b the index numb
1a71c 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72 61 er and argc para
1a71d 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 meters */. asse
1a71e 72 74 28 20 28 70 51 75 65 72 79 2d 3e 66 6c 61 rt( (pQuery->fla
1a71f 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 26 gs&MEM_Int)!=0 &
1a720 26 20 70 41 72 67 63 2d 3e 66 6c 61 67 73 3d 3d & pArgc->flags==
1a721 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 6e 41 72 MEM_Int );. nAr
1a722 67 20 3d 20 70 41 72 67 63 2d 3e 75 2e 69 3b 0a g = pArgc->u.i;.
1a723 20 20 69 51 75 65 72 79 20 3d 20 70 51 75 65 72 iQuery = pQuer
1a724 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49 6e y->u.i;.. /* In
1a725 76 6f 6b 65 20 74 68 65 20 78 46 69 6c 74 65 72 voke the xFilter
1a726 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a 20 method */. {.
1a727 20 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a int res = 0;.
1a728 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4d int i;. M
1a729 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e em **apArg = p->
1a72a 61 70 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 69 apArg;. for(i
1a72b 20 3d 20 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b = 0; i<nArg; i+
1a72c 2b 29 7b 0a 20 20 20 20 20 20 61 70 41 72 67 5b +){. apArg[
1a72d 69 5d 20 3d 20 26 70 41 72 67 63 5b 69 2b 31 5d i] = &pArgc[i+1]
1a72e 3b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 79 70 ;. storeTyp
1a72f 65 49 6e 66 6f 28 61 70 41 72 67 5b 69 5d 2c 20 eInfo(apArg[i],
1a730 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 0);. }.. i
1a731 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1a732 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 Off(db) ) goto a
1a733 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1a734 73 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 se;. sqlite3V
1a735 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a tabLock(pVtab);.
1a736 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 p->inVtabMet
1a737 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 hod = 1;. rc
1a738 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 = pModule->xFilt
1a739 65 72 28 70 56 74 61 62 43 75 72 73 6f 72 2c 20 er(pVtabCursor,
1a73a 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70 34 2e iQuery, pOp->p4.
1a73b 7a 2c 20 6e 41 72 67 2c 20 61 70 41 72 67 29 3b z, nArg, apArg);
1a73c 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 . p->inVtabMe
1a73d 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 thod = 0;. sq
1a73e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1a73f 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 p->zErrMsg);.
1a740 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 p->zErrMsg = pV
1a741 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 tab->zErrMsg;.
1a742 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 pVtab->zErrMsg
1a743 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 0;. sqlite
1a744 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 3VtabUnlock(db,
1a745 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20 pVtab);. if(
1a746 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1a747 0a 20 20 20 20 20 20 72 65 73 20 3d 20 70 4d 6f . res = pMo
1a748 64 75 6c 65 2d 3e 78 45 6f 66 28 70 56 74 61 62 dule->xEof(pVtab
1a749 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 Cursor);. }.
1a74a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1a74b 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1a74c 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1a74d 69 73 75 73 65 3b 0a 0a 20 20 20 20 69 66 28 20 isuse;.. if(
1a74e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 res ){. pc
1a74f 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1a750 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d }. }. pCur-
1a751 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 >nullRow = 0;..
1a752 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1a753 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1a754 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a VIRTUALTABLE */.
1a755 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a756 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1a757 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 6f E./* Opcode: VRo
1a758 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a wid P1 P2 * * *.
1a759 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 74 6f **.** Store into
1a75a 20 72 65 67 69 73 74 65 72 20 50 32 20 20 74 68 register P2 th
1a75b 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 74 68 e rowid of.** th
1a75c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 e virtual-table
1a75d 74 68 61 74 20 74 68 65 20 50 31 20 63 75 72 73 that the P1 curs
1a75e 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
1a75f 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 o..*/.case OP_VR
1a760 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 owid: {
1a761 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1a762 65 6c 65 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 elease */. sqli
1a763 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1a764 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 . const sqlite3
1a765 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 _module *pModule
1a766 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 ;. sqlite_int64
1a767 20 69 52 6f 77 3b 0a 20 20 56 64 62 65 43 75 72 iRow;. VdbeCur
1a768 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 sor *pCur = p->a
1a769 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a pCsr[pOp->p1];..
1a76a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1a76b 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 pVtabCursor );.
1a76c 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 if( pCur->nullR
1a76d 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b ow ){. break;
1a76e 0a 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 . }. pVtab = p
1a76f 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 Cur->pVtabCursor
1a770 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 ->pVtab;. pModu
1a771 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 le = pVtab->pMod
1a772 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ule;. assert( p
1a773 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 Module->xRowid )
1a774 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 ;. if( sqlite3S
1a775 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 afetyOff(db) ) g
1a776 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1a777 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 _misuse;. rc =
1a778 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 pModule->xRowid(
1a779 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f pCur->pVtabCurso
1a77a 72 2c 20 26 69 52 6f 77 29 3b 0a 20 20 73 71 6c r, &iRow);. sql
1a77b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1a77c 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d ->zErrMsg);. p-
1a77d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 >zErrMsg = pVtab
1a77e 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 ->zErrMsg;. pVt
1a77f 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1a780 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1a781 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1a782 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1a783 69 73 75 73 65 3b 0a 20 20 4d 65 6d 53 65 74 54 isuse;. MemSetT
1a784 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1a785 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e M_Int);. pOut->
1a786 75 2e 69 20 3d 20 69 52 6f 77 3b 0a 20 20 62 72 u.i = iRow;. br
1a787 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1a788 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1a789 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 TUALTABLE */..#i
1a78a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1a78b 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f T_VIRTUALTABLE./
1a78c 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d * Opcode: VColum
1a78d 6e 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a n P1 P2 P3 * *.*
1a78e 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 76 *.** Store the v
1a78f 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 2d 74 alue of the P2-t
1a790 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 h column of.** t
1a791 68 65 20 72 6f 77 20 6f 66 20 74 68 65 20 76 69 he row of the vi
1a792 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 74 rtual-table that
1a793 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 73 the .** P1 curs
1a794 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
1a795 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 o into register
1a796 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 P3..*/.case OP_V
1a797 43 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 73 71 6c 69 Column: {. sqli
1a798 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1a799 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 . const sqlite3
1a79a 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 _module *pModule
1a79b 3b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a ;. Mem *pDest;.
1a79c 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1a79d 74 20 73 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 56 t sContext;.. V
1a79e 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 dbeCursor *pCur
1a79f 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1a7a0 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 p1];. assert( p
1a7a1 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 Cur->pVtabCursor
1a7a2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1a7a3 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e p->p3>0 && pOp->
1a7a4 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p3<=p->nMem );.
1a7a5 20 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 pDest = &p->aMe
1a7a6 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 m[pOp->p3];. if
1a7a7 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 ( pCur->nullRow
1a7a8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1a7a9 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 44 65 beMemSetNull(pDe
1a7aa 73 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a st);. break;.
1a7ab 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 }. pVtab = pC
1a7ac 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d ur->pVtabCursor-
1a7ad 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c >pVtab;. pModul
1a7ae 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 e = pVtab->pModu
1a7af 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d le;. assert( pM
1a7b0 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 odule->xColumn )
1a7b1 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e ;. memset(&sCon
1a7b2 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 text, 0, sizeof(
1a7b3 73 43 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 20 2f sContext));.. /
1a7b4 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c * The output cel
1a7b5 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 l may already ha
1a7b6 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f ve a buffer allo
1a7b7 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a cated. Move. **
1a7b8 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e the current con
1a7b9 74 65 6e 74 73 20 74 6f 20 73 43 6f 6e 74 65 78 tents to sContex
1a7ba 74 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 t.s so in case t
1a7bb 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e he user-function
1a7bc 20 0a 20 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 . ** can use t
1a7bd 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 he already alloc
1a7be 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 ated buffer inst
1a7bf 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e ead of allocatin
1a7c0 67 20 61 20 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e g a . ** new on
1a7c1 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 e.. */. sqlite
1a7c2 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 43 3VdbeMemMove(&sC
1a7c3 6f 6e 74 65 78 74 2e 73 2c 20 70 44 65 73 74 29 ontext.s, pDest)
1a7c4 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c ;. MemSetTypeFl
1a7c5 61 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 ag(&sContext.s,
1a7c6 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 69 66 MEM_Null);.. if
1a7c7 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1a7c8 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1a7c9 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1a7ca 65 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c e;. rc = pModul
1a7cb 65 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d e->xColumn(pCur-
1a7cc 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 73 >pVtabCursor, &s
1a7cd 43 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 Context, pOp->p2
1a7ce 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1a7cf 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1a7d0 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 g);. p->zErrMsg
1a7d1 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 = pVtab->zErrMs
1a7d2 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 g;. pVtab->zErr
1a7d3 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 Msg = 0;.. /* C
1a7d4 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f opy the result o
1a7d5 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 f the function t
1a7d6 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65 o the P3 registe
1a7d7 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 r. We. ** do th
1a7d8 69 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 is regardless of
1a7d9 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1a7da 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 an error occured
1a7db 20 74 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 to ensure any.
1a7dc 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f ** dynamic allo
1a7dd 63 61 74 69 6f 6e 20 69 6e 20 73 43 6f 6e 74 65 cation in sConte
1a7de 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75 xt.s (a Mem stru
1a7df 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65 64 ct) is released
1a7e0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
1a7e1 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
1a7e2 6e 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 ng(&sContext.s,
1a7e3 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47 encoding);. REG
1a7e4 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1a7e5 3e 70 33 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 >p3, pDest);. s
1a7e6 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1a7e7 65 28 70 44 65 73 74 2c 20 26 73 43 6f 6e 74 65 e(pDest, &sConte
1a7e8 78 74 2e 73 29 3b 0a 20 20 55 50 44 41 54 45 5f xt.s);. UPDATE_
1a7e9 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 MAX_BLOBSIZE(pDe
1a7ea 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 st);.. if( sqli
1a7eb 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1a7ec 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 ){. goto abor
1a7ed 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1a7ee 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
1a7ef 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 e3VdbeMemTooBig(
1a7f0 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f pDest) ){. go
1a7f1 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1a7f2 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1a7f3 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1a7f4 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1a7f5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1a7f6 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1a7f7 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e LE./* Opcode: VN
1a7f8 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ext P1 P2 * * *.
1a7f9 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 **.** Advance vi
1a7fa 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 rtual table P1 t
1a7fb 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 o the next row i
1a7fc 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 n its result set
1a7fd 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 and.** jump to
1a7fe 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 instruction P2.
1a7ff 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 Or, if the virt
1a800 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 ual table has re
1a801 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 ached.** the end
1a802 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 of its result s
1a803 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 et, then fall th
1a804 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 rough to the nex
1a805 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
1a806 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a /.case OP_VNext:
1a807 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a { /* jump */.
1a808 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1a809 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 pVtab;. const s
1a80a 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
1a80b 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 Module;. int re
1a80c 73 20 3d 20 30 3b 0a 0a 20 20 56 64 62 65 43 75 s = 0;.. VdbeCu
1a80d 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e rsor *pCur = p->
1a80e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1a80f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1a810 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 pVtabCursor );.
1a811 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 if( pCur->nullR
1a812 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b ow ){. break;
1a813 0a 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 . }. pVtab = p
1a814 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 Cur->pVtabCursor
1a815 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 ->pVtab;. pModu
1a816 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 le = pVtab->pMod
1a817 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ule;. assert( p
1a818 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 20 29 3b Module->xNext );
1a819 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 .. /* Invoke th
1a81a 65 20 78 4e 65 78 74 28 29 20 6d 65 74 68 6f 64 e xNext() method
1a81b 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 of the module.
1a81c 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 There is no way
1a81d 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64 for the. ** und
1a81e 65 72 6c 79 69 6e 67 20 69 6d 70 6c 65 6d 65 6e erlying implemen
1a81f 74 61 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e tation to return
1a820 20 61 6e 20 65 72 72 6f 72 20 69 66 20 6f 6e 65 an error if one
1a821 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 20 occurs during.
1a822 20 2a 2a 20 78 4e 65 78 74 28 29 2e 20 49 6e 73 ** xNext(). Ins
1a823 74 65 61 64 2c 20 69 66 20 61 6e 20 65 72 72 6f tead, if an erro
1a824 72 20 6f 63 63 75 72 73 2c 20 74 72 75 65 20 69 r occurs, true i
1a825 73 20 72 65 74 75 72 6e 65 64 20 28 69 6e 64 69 s returned (indi
1a826 63 61 74 69 6e 67 20 74 68 61 74 20 0a 20 20 2a cating that . *
1a827 2a 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 * data is availa
1a828 62 6c 65 29 20 61 6e 64 20 74 68 65 20 65 72 72 ble) and the err
1a829 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 or code returned
1a82a 20 77 68 65 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 when xColumn or
1a82b 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72 . ** some other
1a82c 20 6d 65 74 68 6f 64 20 69 73 20 6e 65 78 74 20 method is next
1a82d 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 73 invoked on the s
1a82e 61 76 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c ave virtual tabl
1a82f 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 e cursor.. */.
1a830 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1a831 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1a832 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1a833 73 75 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 56 suse;. sqlite3V
1a834 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a tabLock(pVtab);.
1a835 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f p->inVtabMetho
1a836 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 70 4d d = 1;. rc = pM
1a837 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 odule->xNext(pCu
1a838 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b r->pVtabCursor);
1a839 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 . p->inVtabMeth
1a83a 6f 64 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 od = 0;. sqlite
1a83b 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1a83c 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
1a83d 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a rrMsg = pVtab->z
1a83e 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d ErrMsg;. pVtab-
1a83f 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1a840 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 sqlite3VtabUnloc
1a841 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 k(db, pVtab);.
1a842 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1a843 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 70 K ){. res = p
1a844 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 70 43 75 Module->xEof(pCu
1a845 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b r->pVtabCursor);
1a846 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
1a847 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1a848 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1a849 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 to_misuse;.. if
1a84a 28 20 21 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a ( !res ){. /*
1a84b 20 49 66 20 74 68 65 72 65 20 69 73 20 64 61 74 If there is dat
1a84c 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f a, jump to P2 */
1a84d 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1a84e 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1a84f 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1a850 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1a851 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1a852 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1a853 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1a854 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d 65 Opcode: VRename
1a855 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 * * P4 *.**.
1a856 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1a857 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 er to a virtual
1a858 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e table object, an
1a859 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 sqlite3_vtab st
1a85a 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73 ructure..** This
1a85b 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 opcode invokes
1a85c 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1a85d 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 g xRename method
1a85e 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 . The value.** i
1a85f 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 n register P1 is
1a860 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 7a passed as the z
1a861 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 6f Name argument to
1a862 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 the xRename met
1a863 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f hod..*/.case OP_
1a864 56 52 65 6e 61 6d 65 3a 20 7b 0a 20 20 73 71 6c VRename: {. sql
1a865 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1a866 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 = pOp->p4.pVtab
1a867 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 20 3d ;. Mem *pName =
1a868 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1a869 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 1];. assert( pV
1a86a 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 tab->pModule->xR
1a86b 65 6e 61 6d 65 20 29 3b 0a 20 20 52 45 47 49 53 ename );. REGIS
1a86c 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1a86d 31 2c 20 70 4e 61 6d 65 29 3b 0a 0a 20 20 53 74 1, pName);.. St
1a86e 72 69 6e 67 69 66 79 28 70 4e 61 6d 65 2c 20 65 ringify(pName, e
1a86f 6e 63 6f 64 69 6e 67 29 3b 0a 0a 20 20 69 66 28 ncoding);.. if(
1a870 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1a871 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1a872 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1a873 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c ;. sqlite3VtabL
1a874 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 72 63 ock(pVtab);. rc
1a875 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c = pVtab->pModul
1a876 65 2d 3e 78 52 65 6e 61 6d 65 28 70 56 74 61 62 e->xRename(pVtab
1a877 2c 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 , pName->z);. s
1a878 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1a879 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1a87a 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 p->zErrMsg = pVt
1a87b 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 ab->zErrMsg;. p
1a87c 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 Vtab->zErrMsg =
1a87d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 0;. sqlite3Vtab
1a87e 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 Unlock(db, pVtab
1a87f 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
1a880 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1a881 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1a882 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61 _misuse;.. brea
1a883 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 k;.}.#endif..#if
1a884 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1a885 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1a886 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65 Opcode: VUpdate
1a887 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1a888 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 *.** P4 is a poi
1a889 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 nter to a virtua
1a88a 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 l table object,
1a88b 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 an sqlite3_vtab
1a88c 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 structure..** Th
1a88d 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 is opcode invoke
1a88e 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 s the correspond
1a88f 69 6e 67 20 78 55 70 64 61 74 65 20 6d 65 74 68 ing xUpdate meth
1a890 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a od. P2 values.**
1a891 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 are contiguous
1a892 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61 memory cells sta
1a893 72 74 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70 rting at P3 to p
1a894 61 73 73 20 74 6f 20 74 68 65 20 78 55 70 64 61 ass to the xUpda
1a895 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f te .** invocatio
1a896 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 n. The value in
1a897 72 65 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d register (P3+P2-
1a898 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 1) corresponds t
1a899 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65 o the .** p2th e
1a89a 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 lement of the ar
1a89b 67 76 20 61 72 72 61 79 20 70 61 73 73 65 64 20 gv array passed
1a89c 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a to xUpdate..**.*
1a89d 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 * The xUpdate me
1a89e 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44 thod will do a D
1a89f 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45 ELETE or an INSE
1a8a0 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 RT or both..** T
1a8a1 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65 he argv[0] eleme
1a8a2 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72 65 73 nt (which corres
1a8a3 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20 ponds to memory
1a8a4 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74 cell P3).** is t
1a8a5 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f he rowid of a ro
1a8a6 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66 w to delete. If
1a8a7 20 61 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c argv[0] is NULL
1a8a8 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c then no .** del
1a8a9 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54 etion occurs. T
1a8aa 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65 he argv[1] eleme
1a8ab 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 nt is the rowid
1a8ac 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 of the new .** r
1a8ad 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 ow. This can be
1a8ae 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68 NULL to have th
1a8af 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
1a8b0 73 65 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a select the new .
1a8b1 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73 ** rowid for its
1a8b2 65 6c 66 2e 20 20 54 68 65 20 73 75 62 73 65 71 elf. The subseq
1a8b3 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e uent elements in
1a8b4 20 74 68 65 20 61 72 72 61 79 20 61 72 65 20 0a the array are .
1a8b5 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 ** the values of
1a8b6 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
1a8b7 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 new row..**.** I
1a8b8 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 f P2==1 then no
1a8b9 69 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f 72 insert is perfor
1a8ba 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 med. argv[0] is
1a8bb 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a the rowid of.**
1a8bc 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 a row to delete
1a8bd 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 ..**.** P1 is a
1a8be 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66 boolean flag. If
1a8bf 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 it is set to tr
1a8c0 75 65 20 61 6e 64 20 74 68 65 20 78 55 70 64 61 ue and the xUpda
1a8c1 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 te call.** is su
1a8c2 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74 ccessful, then t
1a8c3 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
1a8c4 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73 d by sqlite3_las
1a8c5 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 t_insert_rowid()
1a8c6 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 .** is set to t
1a8c7 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1a8c8 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f rowid for the ro
1a8c9 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e w just inserted.
1a8ca 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64 .*/.case OP_VUpd
1a8cb 61 74 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 ate: {. sqlite3
1a8cc 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 _vtab *pVtab = p
1a8cd 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 Op->p4.pVtab;.
1a8ce 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
1a8cf 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 pModule = (sqlit
1a8d0 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 e3_module *)pVta
1a8d1 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e b->pModule;. in
1a8d2 74 20 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 t nArg = pOp->p2
1a8d3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1a8d4 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 >p4type==P4_VTAB
1a8d5 20 29 3b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c );. if( pModul
1a8d6 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 20 29 7b e->xUpdate==0 ){
1a8d7 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1a8d8 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1a8d9 67 2c 20 64 62 2c 20 22 72 65 61 64 2d 6f 6e 6c g, db, "read-onl
1a8da 79 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 72 y table");. r
1a8db 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1a8dc 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
1a8dd 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 nt i;. sqlite
1a8de 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 _int64 rowid;.
1a8df 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 Mem **apArg =
1a8e0 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 4d 65 p->apArg;. Me
1a8e1 6d 20 2a 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d m *pX = &p->aMem
1a8e2 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 [pOp->p3];. f
1a8e3 6f 72 28 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 or(i=0; i<nArg;
1a8e4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 i++){. stor
1a8e5 65 54 79 70 65 49 6e 66 6f 28 70 58 2c 20 30 29 eTypeInfo(pX, 0)
1a8e6 3b 0a 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d ;. apArg[i]
1a8e7 20 3d 20 70 58 3b 0a 20 20 20 20 20 20 70 58 2b = pX;. pX+
1a8e8 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 +;. }. if(
1a8e9 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1a8ea 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1a8eb 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1a8ec 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 ;. sqlite3Vta
1a8ed 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 bLock(pVtab);.
1a8ee 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e rc = pModule->
1a8ef 78 55 70 64 61 74 65 28 70 56 74 61 62 2c 20 6e xUpdate(pVtab, n
1a8f0 41 72 67 2c 20 61 70 41 72 67 2c 20 26 72 6f 77 Arg, apArg, &row
1a8f1 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 id);. sqlite3
1a8f2 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1a8f3 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
1a8f4 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e ErrMsg = pVtab->
1a8f5 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 zErrMsg;. pVt
1a8f6 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1a8f7 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 . sqlite3Vtab
1a8f8 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 Unlock(db, pVtab
1a8f9 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 );. if( sqlit
1a8fa 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1a8fb 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1a8fc 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 69 to_misuse;. i
1a8fd 66 28 20 70 4f 70 2d 3e 70 31 20 26 26 20 72 63 f( pOp->p1 && rc
1a8fe 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1a8ff 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72 assert( nAr
1a900 67 3e 31 20 26 26 20 61 70 41 72 67 5b 30 5d 20 g>1 && apArg[0]
1a901 26 26 20 28 61 70 41 72 67 5b 30 5d 2d 3e 66 6c && (apArg[0]->fl
1a902 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b ags&MEM_Null) );
1a903 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74 52 . db->lastR
1a904 6f 77 69 64 20 3d 20 72 6f 77 69 64 3b 0a 20 20 owid = rowid;.
1a905 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e }. p->nChan
1a906 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 ge++;. }. brea
1a907 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1a908 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1a909 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e ALTABLE */..#ifn
1a90a 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 def SQLITE_OMIT
1a90b 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f _PAGER_PRAGMAS./
1a90c 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f * Opcode: Pageco
1a90d 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a unt P1 P2 * * *.
1a90e 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
1a90f 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f current number o
1a910 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 f pages in datab
1a911 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 ase P1 to memory
1a912 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 cell P2..*/.cas
1a913 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 e OP_Pagecount:
1a914 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1a915 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1a916 2a 2f 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f */. int p1 = pO
1a917 70 2d 3e 70 31 3b 20 0a 20 20 69 6e 74 20 6e 50 p->p1; . int nP
1a918 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 age;. Pager *pP
1a919 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 ager = sqlite3Bt
1a91a 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 reePager(db->aDb
1a91b 5b 70 31 5d 2e 70 42 74 29 3b 0a 0a 20 20 72 63 [p1].pBt);.. rc
1a91c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 = sqlite3PagerP
1a91d 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
1a91e 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 &nPage);. if(
1a91f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1a920 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 . pOut->flags
1a921 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1a922 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 6e 50 61 67 pOut->u.i = nPag
1a923 65 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a e;. }. break;.
1a924 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
1a925 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1a926 41 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 ACE./* Opcode: T
1a927 72 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a race * * * P4 *.
1a928 2a 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67 **.** If tracing
1a929 20 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20 is enabled (by
1a92a 74 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 the sqlite3_trac
1a92b 65 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20 e()) interface,
1a92c 74 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d then.** the UTF-
1a92d 38 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 8 string contain
1a92e 65 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74 ed in P4 is emit
1a92f 74 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65 ted on the trace
1a930 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 callback..*/.ca
1a931 73 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 20 se OP_Trace: {.
1a932 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 if( pOp->p4.z )
1a933 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 54 {. if( db->xT
1a934 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 64 62 race ){. db
1a935 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72 ->xTrace(db->pTr
1a936 61 63 65 41 72 67 2c 20 70 4f 70 2d 3e 70 34 2e aceArg, pOp->p4.
1a937 7a 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 z);. }.#ifdef
1a938 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
1a939 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 if( (db->flags
1a93a 20 26 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 & SQLITE_SqlTra
1a93b 63 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ce)!=0 ){.
1a93c 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
1a93d 74 66 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 25 tf("SQL-trace: %
1a93e 73 5c 6e 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 s\n", pOp->p4.z)
1a93f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f ;. }.#endif /
1a940 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a * SQLITE_DEBUG *
1a941 2f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d /. }. break;.}
1a942 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4f 70 63 .#endif.../* Opc
1a943 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a 20 ode: Noop * * *
1a944 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f 74 * *.**.** Do not
1a945 68 69 6e 67 2e 20 20 54 68 69 73 20 69 6e 73 74 hing. This inst
1a946 72 75 63 74 69 6f 6e 20 69 73 20 6f 66 74 65 6e ruction is often
1a947 20 75 73 65 66 75 6c 20 61 73 20 61 20 6a 75 6d useful as a jum
1a948 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e p.** destination
1a949 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d ..*/./*.** The m
1a94a 61 67 69 63 20 45 78 70 6c 61 69 6e 20 6f 70 63 agic Explain opc
1a94b 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e 73 ode are only ins
1a94c 65 72 74 65 64 20 77 68 65 6e 20 65 78 70 6c 61 erted when expla
1a94d 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a 20 in==2 (which.**
1a94e 69 73 20 74 6f 20 73 61 79 20 77 68 65 6e 20 74 is to say when t
1a94f 68 65 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 he EXPLAIN QUERY
1a950 20 50 4c 41 4e 20 73 79 6e 74 61 78 20 69 73 20 PLAN syntax is
1a951 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20 6f used.).** This o
1a952 70 63 6f 64 65 20 72 65 63 6f 72 64 73 20 69 6e pcode records in
1a953 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 formation from t
1a954 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 49 he optimizer. I
1a955 74 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 65 20 t is the.** the
1a956 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70 2e same as a no-op.
1a957 20 20 54 68 69 73 20 6f 70 63 6f 64 65 73 6e 65 This opcodesne
1a958 76 65 72 20 61 70 70 65 61 72 73 20 69 6e 20 61 ver appears in a
1a959 20 72 65 61 6c 20 56 4d 20 70 72 6f 67 72 61 6d real VM program
1a95a 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a 20 7b 20 ..*/.default: {
1a95b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
1a95c 20 69 73 20 72 65 61 6c 6c 79 20 4f 50 5f 4e 6f is really OP_No
1a95d 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70 6c 61 69 op and OP_Explai
1a95e 6e 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a n */. break;.}.
1a95f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1a960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a961 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a962 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1a964 2a 2a 20 54 68 65 20 63 61 73 65 73 20 6f 66 20 ** The cases of
1a965 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 65 the switch state
1a966 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 69 73 20 ment above this
1a967 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c 20 line should all
1a968 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 62 be indented.** b
1a969 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 y 6 spaces. But
1a96a 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 36 the left-most 6
1a96b 20 73 70 61 63 65 73 20 68 61 76 65 20 62 65 65 spaces have bee
1a96c 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d 70 n removed to imp
1a96d 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 65 61 64 rove the.** read
1a96e 61 62 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74 ability. From t
1a96f 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77 his point on dow
1a970 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e n, the normal in
1a971 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73 20 dentation rules
1a972 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e are.** restored.
1a973 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1a974 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a975 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a976 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a977 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1a978 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 56 44 }..#ifdef VD
1a979 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 7b BE_PROFILE. {
1a97a 0a 20 20 20 20 20 20 75 36 34 20 65 6c 61 70 73 . u64 elaps
1a97b 65 64 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 ed = sqlite3Hwti
1a97c 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a 20 20 me() - start;.
1a97d 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20 pOp->cycles
1a97e 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20 20 20 20 += elapsed;.
1a97f 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23 69 pOp->cnt++;.#i
1a980 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72 69 f 0. fpri
1a981 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31 30 ntf(stdout, "%10
1a982 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65 64 29 3b llu ", elapsed);
1a983 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a984 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f VdbePrintOp(stdo
1a985 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d 3e ut, origPc, &p->
1a986 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65 aOp[origPc]);.#e
1a987 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69 ndif. }.#endi
1a988 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f f.. /* The fo
1a989 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 64 llowing code add
1a98a 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 s nothing to the
1a98b 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e actual function
1a98c 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 ality. ** of
1a98d 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 74 the program. It
1a98e 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f is only here fo
1a98f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 r testing and de
1a990 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20 bugging.. **
1a991 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
1a992 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 6e 20 d, it does burn
1a993 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 72 79 CPU cycles every
1a994 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 20 time through.
1a995 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74 ** the evaluat
1a996 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20 or loop. So we
1a997 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74 can leave it out
1a998 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20 when NDEBUG is
1a999 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a defined.. */.
1a99a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
1a99b 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 2d assert( pc>=-
1a99c 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 1 && pc<p->nOp )
1a99d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
1a99e 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70 _DEBUG. if( p
1a99f 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 ->trace ){.
1a9a0 20 69 66 28 20 72 63 21 3d 30 20 29 20 66 70 72 if( rc!=0 ) fpr
1a9a1 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 72 intf(p->trace,"r
1a9a2 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 20 c=%d\n",rc);.
1a9a3 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74 if( opPropert
1a9a4 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 y & OPFLG_OUT2_P
1a9a5 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 RERELEASE ){.
1a9a6 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 61 registerTra
1a9a7 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 ce(p->trace, pOp
1a9a8 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 ->p2, pOut);.
1a9a9 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f }. if( o
1a9aa 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c pProperty & OPFL
1a9ab 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20 G_OUT3 ){.
1a9ac 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28 registerTrace(
1a9ad 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 p->trace, pOp->p
1a9ae 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 3, pOut);.
1a9af 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 20 }. }.#endif
1a9b0 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 /* SQLITE_DEBUG
1a9b1 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44 */.#endif /* ND
1a9b2 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a 20 EBUG */. } /*
1a9b3 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 The end of the f
1a9b4 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 20 or(;;) loop the
1a9b5 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 6f 70 loops through op
1a9b6 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 codes */.. /* I
1a9b7 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 f we reach this
1a9b8 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 point, it means
1a9b9 74 68 61 74 20 65 78 65 63 75 74 69 6f 6e 20 69 that execution i
1a9ba 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 0a s finished with.
1a9bb 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 66 ** an error of
1a9bc 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a 2f some kind.. */
1a9bd 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 .vdbe_error_halt
1a9be 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 20 29 :. assert( rc )
1a9bf 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a ;. p->rc = rc;.
1a9c0 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c sqlite3VdbeHal
1a9c1 74 28 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d t(p);. if( rc==
1a9c2 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d SQLITE_IOERR_NOM
1a9c3 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 EM ) db->mallocF
1a9c4 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63 20 ailed = 1;. rc
1a9c5 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1a9c6 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 . /* This is th
1a9c7 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20 6f e only way out o
1a9c8 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 f this procedure
1a9c9 2e 20 20 57 65 20 68 61 76 65 20 74 6f 0a 20 20 . We have to.
1a9ca 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d ** release the m
1a9cb 75 74 65 78 65 73 20 6f 6e 20 62 74 72 65 65 73 utexes on btrees
1a9cc 20 74 68 61 74 20 77 65 72 65 20 61 63 71 75 69 that were acqui
1a9cd 72 65 64 20 61 74 20 74 68 65 0a 20 20 2a 2a 20 red at the. **
1a9ce 74 6f 70 2e 20 2a 2f 0a 76 64 62 65 5f 72 65 74 top. */.vdbe_ret
1a9cf 75 72 6e 3a 0a 20 20 73 71 6c 69 74 65 33 42 74 urn:. sqlite3Bt
1a9d0 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 reeMutexArrayLea
1a9d1 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a ve(&p->aMutex);.
1a9d2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 return rc;..
1a9d3 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 /* Jump to here
1a9d4 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 if a string or b
1a9d5 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e 20 lob larger than
1a9d6 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 SQLITE_MAX_LENGT
1a9d7 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e H. ** is encoun
1a9d8 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f tered.. */.too_
1a9d9 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 65 big:. sqlite3Se
1a9da 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1a9db 4d 73 67 2c 20 64 62 2c 20 22 73 74 72 69 6e 67 Msg, db, "string
1a9dc 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 or blob too big
1a9dd 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 ");. rc = SQLIT
1a9de 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 6f E_TOOBIG;. goto
1a9df 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 vdbe_error_halt
1a9e0 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 ;.. /* Jump to
1a9e1 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63 here if a malloc
1a9e2 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 6e () fails.. */.n
1a9e3 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c o_mem:. db->mal
1a9e4 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
1a9e5 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1a9e6 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1a9e7 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 b, "out of memor
1a9e8 79 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 y");. rc = SQLI
1a9e9 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f TE_NOMEM;. goto
1a9ea 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 vdbe_error_halt
1a9eb 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 ;.. /* Jump to
1a9ec 68 65 72 65 20 66 6f 72 20 61 6e 20 53 51 4c 49 here for an SQLI
1a9ed 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 2e TE_MISUSE error.
1a9ee 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f . */.abort_due_
1a9ef 74 6f 5f 6d 69 73 75 73 65 3a 0a 20 20 72 63 20 to_misuse:. rc
1a9f0 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b = SQLITE_MISUSE;
1a9f1 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 . /* Fall thru
1a9f2 69 6e 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 into abort_due_t
1a9f3 6f 5f 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 2f 2a o_error */.. /*
1a9f4 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f Jump to here fo
1a9f5 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
1a9f6 20 6f 66 20 66 61 74 61 6c 20 65 72 72 6f 72 2e of fatal error.
1a9f7 20 20 54 68 65 20 22 72 63 22 20 76 61 72 69 61 The "rc" varia
1a9f8 62 6c 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 ble. ** should
1a9f9 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6e hold the error n
1a9fa 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 umber.. */.abor
1a9fb 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3a 0a t_due_to_error:.
1a9fc 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 45 72 assert( p->zEr
1a9fd 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 rMsg==0 );. if(
1a9fe 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1a9ff 64 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f d ) rc = SQLITE_
1aa00 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63 21 NOMEM;. if( rc!
1aa01 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f =SQLITE_IOERR_NO
1aa02 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 MEM ){. sqlit
1aa03 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1aa04 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1aa05 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 ", sqlite3ErrStr
1aa06 28 72 63 29 29 3b 0a 20 20 7d 0a 20 20 67 6f 74 (rc));. }. got
1aa07 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c o vdbe_error_hal
1aa08 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f t;.. /* Jump to
1aa09 20 68 65 72 65 20 69 66 20 74 68 65 20 73 71 6c here if the sql
1aa0a 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
1aa0b 20 41 50 49 20 73 65 74 73 20 74 68 65 20 69 6e API sets the in
1aa0c 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c 61 terrupt. ** fla
1aa0d 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 g.. */.abort_du
1aa0e 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a 0a e_to_interrupt:.
1aa0f 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 75 31 assert( db->u1
1aa10 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 .isInterrupted )
1aa11 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f ;. rc = SQLITE_
1aa12 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 2d 3e INTERRUPT;. p->
1aa13 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 rc = rc;. sqlit
1aa14 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1aa15 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1aa16 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 ", sqlite3ErrStr
1aa17 28 72 63 29 29 3b 0a 20 20 67 6f 74 6f 20 76 64 (rc));. goto vd
1aa18 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 7d be_error_halt;.}
1aa19 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1aa1a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 63 20 * End of vdbe.c
1aa1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa1e 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1aa1f 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
1aa20 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a eblob.c ********
1aa21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa23 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 /./*.** 2007 May
1aa24 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 1.**.** The aut
1aa25 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
1aa26 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1aa27 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1aa28 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1aa29 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1aa2a 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1aa2b 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1aa2c 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
1aa2d 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
1aa2e 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
1aa2f 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
1aa30 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
1aa31 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
1aa32 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
1aa33 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
1aa34 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
1aa35 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
1aa36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aa3a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
1aa3b 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
1aa3c 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
1aa3d 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c t incremental BL
1aa3e 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49 OB I/O..**.** $I
1aa3f 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76 20 d: vdbeblob.c,v
1aa40 31 2e 32 36 20 32 30 30 38 2f 31 30 2f 30 32 20 1.26 2008/10/02
1aa41 31 34 3a 34 39 3a 30 32 20 64 61 6e 69 65 6c 6b 14:49:02 danielk
1aa42 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 1977 Exp $.*/...
1aa43 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1aa44 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a MIT_INCRBLOB../*
1aa45 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65 .** Valid sqlite
1aa46 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20 3_blob* handles
1aa47 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f point to Incrblo
1aa48 62 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f b structures..*/
1aa49 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1aa4a 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f Incrblob Incrblo
1aa4b 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c b;.struct Incrbl
1aa4c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 ob {. int flags
1aa4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1aa4e 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73 * Copy of "flags
1aa4f 22 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 " passed to sqli
1aa50 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20 te3_blob_open()
1aa51 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 */. int nByte;
1aa52 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1aa53 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f Size of open blo
1aa54 62 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 b, in bytes */.
1aa55 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20 int iOffset;
1aa56 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
1aa57 20 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20 offset of blob
1aa58 69 6e 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a in cursor data *
1aa59 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
1aa5a 73 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 sr; /* C
1aa5b 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 ursor pointing a
1aa5c 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20 t blob row */.
1aa5d 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1aa5e 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65 tmt; /* State
1aa5f 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72 ment holding cur
1aa60 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 sor open */. sq
1aa61 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
1aa62 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 /* The ass
1aa63 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 ociated database
1aa64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 */.};../*.** Op
1aa65 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 en a blob handle
1aa66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1aa67 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1aa68 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 _open(. sqlite3
1aa69 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 * db,
1aa6a 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1aa6b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
1aa6c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
1aa6d 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 , /* The
1aa6e 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
1aa6f 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
1aa70 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 blob */. const
1aa71 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20 char *zTable,
1aa72 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
1aa73 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 containing the b
1aa74 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 lob */. const c
1aa75 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20 har *zColumn,
1aa76 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63 /* The column c
1aa77 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c ontaining the bl
1aa78 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 ob */. sqlite_i
1aa79 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 nt64 iRow,
1aa7a 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61 /* The row conta
1aa7b 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a ining the glob *
1aa7c 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
1aa7d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1aa7e 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74 rue -> read/writ
1aa7f 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20 e access, false
1aa80 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a -> read-only */.
1aa81 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a sqlite3_blob *
1aa82 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e *ppBlob /* Han
1aa83 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e dle for accessin
1aa84 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72 g the blob retur
1aa85 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ned here */.){.
1aa86 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20 int nAttempt =
1aa87 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 0;. int iCol;
1aa88 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1aa89 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e Index of zColumn
1aa8a 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a in row-record *
1aa8b 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42 /.. /* This VDB
1aa8c 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20 E program seeks
1aa8d 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 20 74 a btree cursor t
1aa8e 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 64 o the identified
1aa8f 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f . ** db/table/
1aa90 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72 row entry. The r
1aa91 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 eason for using
1aa92 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 a vdbe program i
1aa93 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77 nstead. ** of w
1aa94 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 riting code to u
1aa95 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 se the b-tree la
1aa96 79 65 72 20 64 69 72 65 63 74 6c 79 20 69 73 20 yer directly is
1aa97 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64 that the. ** vd
1aa98 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 be program will
1aa99 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f take advantage o
1aa9a 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 72 f the various tr
1aa9b 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 ansaction,. **
1aa9c 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f locking and erro
1aa9d 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61 r handling infra
1aa9e 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 structure built
1aa9f 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20 into the vdbe..
1aaa0 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73 **. ** After s
1aaa1 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f eeking the curso
1aaa2 72 2c 20 74 68 65 20 76 64 62 65 20 65 78 65 63 r, the vdbe exec
1aaa3 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 73 75 6c utes an OP_Resul
1aaa4 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65 20 tRow.. ** Code
1aaa5 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20 external to the
1aaa6 56 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f Vdbe then "borro
1aaa7 77 73 22 20 74 68 65 20 62 2d 74 72 65 65 20 63 ws" the b-tree c
1aaa8 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75 ursor and. ** u
1aaa9 73 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d ses it to implem
1aaaa 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61 ent the blob_rea
1aaab 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28 d(), blob_write(
1aaac 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62 ) and . ** blob
1aaad 5f 62 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f _bytes() functio
1aaae 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 ns.. **. ** Th
1aaaf 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 e sqlite3_blob_c
1aab0 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 lose() function
1aab1 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64 finalizes the vd
1aab2 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a be program,. **
1aab3 20 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68 which closes th
1aab4 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 e b-tree cursor
1aab5 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63 and (possibly) c
1aab6 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a ommits the . **
1aab7 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 transaction..
1aab8 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 */. static cons
1aab9 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65 t VdbeOpList ope
1aaba 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20 nBlob[] = {.
1aabb 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c {OP_Transaction,
1aabc 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f 0, 0, 0}, /
1aabd 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74 72 61 * 0: Start a tra
1aabe 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 nsaction */.
1aabf 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 {OP_VerifyCookie
1aac0 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f , 0, 0, 0}, /
1aac1 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73 * 1: Check the s
1aac2 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a chema cookie */.
1aac3 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 . /* One of t
1aac4 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f he following two
1aac5 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73 instructions is
1aac6 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e 0a replaced by an.
1aac7 20 20 20 20 2a 2a 20 4f 50 5f 4e 6f 6f 70 20 62 ** OP_Noop b
1aac8 65 66 6f 72 65 20 65 78 65 63 74 69 6f 6e 2e 0a efore exection..
1aac9 20 20 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 53 */. {OP_S
1aaca 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c etNumColumns, 0,
1aacb 20 30 2c 20 30 7d 2c 20 20 20 2f 2a 20 32 3a 20 0, 0}, /* 2:
1aacc 4e 75 6d 20 63 6f 6c 73 20 66 6f 72 20 63 75 72 Num cols for cur
1aacd 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f sor */. {OP_O
1aace 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30 penRead, 0, 0, 0
1aacf 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 }, /* 3:
1aad0 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f Open cursor 0 fo
1aad1 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20 r reading */.
1aad2 20 7b 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d {OP_SetNumColum
1aad3 6e 73 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 ns, 0, 0, 0},
1aad4 2f 2a 20 34 3a 20 4e 75 6d 20 63 6f 6c 73 20 66 /* 4: Num cols f
1aad5 6f 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 or cursor */.
1aad6 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 {OP_OpenWrite,
1aad7 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 0, 0, 0},
1aad8 2f 2a 20 35 3a 20 4f 70 65 6e 20 63 75 72 73 6f /* 5: Open curso
1aad9 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69 r 0 for read/wri
1aada 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 te */.. {OP_V
1aadb 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 30 ariable, 1, 1, 0
1aadc 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 36 3a 20 }, /* 6:
1aadd 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20 74 Push the rowid t
1aade 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 o the stack */.
1aadf 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 {OP_NotExists
1aae0 2c 20 30 2c 20 31 30 2c 20 31 7d 2c 20 20 20 20 , 0, 10, 1},
1aae1 20 20 2f 2a 20 37 3a 20 53 65 65 6b 20 74 68 65 /* 7: Seek the
1aae2 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b cursor */. {
1aae3 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c OP_Column, 0, 0,
1aae4 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 1}, /*
1aae5 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52 8 */. {OP_R
1aae6 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20 esultRow, 1, 0,
1aae7 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 39 20 20 0}, /* 9
1aae8 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 */. {OP_Close
1aae9 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 , 0, 0, 0},
1aaea 20 20 20 20 20 20 2f 2a 20 31 30 20 20 2a 2f 0a /* 10 */.
1aaeb 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c {OP_Halt, 0,
1aaec 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 0, 0},
1aaed 20 20 20 2f 2a 20 31 31 20 2a 2f 0a 20 20 7d 3b /* 11 */. };
1aaee 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b .. Vdbe *v = 0;
1aaef 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1aaf0 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 7a 45 TE_OK;. char zE
1aaf1 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 7a 45 72 72 rr[128];.. zErr
1aaf2 5b 30 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 [0] = 0;. sqlit
1aaf3 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1aaf4 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 6f 20 b->mutex);. do
1aaf5 7b 0a 20 20 20 20 50 61 72 73 65 20 73 50 61 72 {. Parse sPar
1aaf6 73 65 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 se;. Table *p
1aaf7 54 61 62 3b 0a 0a 20 20 20 20 6d 65 6d 73 65 74 Tab;.. memset
1aaf8 28 26 73 50 61 72 73 65 2c 20 30 2c 20 73 69 7a (&sParse, 0, siz
1aaf9 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20 20 eof(Parse));.
1aafa 20 73 50 61 72 73 65 2e 64 62 20 3d 20 64 62 3b sParse.db = db;
1aafb 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 .. if( sqlite
1aafc 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 3SafetyOn(db) ){
1aafd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
1aafe 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d utex_leave(db->m
1aaff 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74 utex);. ret
1ab00 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 urn SQLITE_MISUS
1ab01 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 E;. }.. sq
1ab02 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
1ab03 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62 ll(db);. pTab
1ab04 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
1ab05 54 61 62 6c 65 28 26 73 50 61 72 73 65 2c 20 30 Table(&sParse, 0
1ab06 2c 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a , zTable, zDb);.
1ab07 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26 20 if( pTab &&
1ab08 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 IsVirtual(pTab)
1ab09 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 ){. pTab =
1ab0a 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 0;. sqlite3
1ab0b 45 72 72 6f 72 4d 73 67 28 26 73 50 61 72 73 65 ErrorMsg(&sParse
1ab0c 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 , "cannot open v
1ab0d 69 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 irtual table: %s
1ab0e 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 ", zTable);.
1ab0f 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1ab10 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 _OMIT_VIEW. i
1ab11 66 28 20 70 54 61 62 20 26 26 20 70 54 61 62 2d f( pTab && pTab-
1ab12 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 >pSelect ){.
1ab13 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 pTab = 0;.
1ab14 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1ab15 67 28 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e g(&sParse, "cann
1ab16 6f 74 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 ot open view: %s
1ab17 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 ", zTable);.
1ab18 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 }.#endif. if(
1ab19 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 !pTab ){.
1ab1a 69 66 28 20 73 50 61 72 73 65 2e 7a 45 72 72 4d if( sParse.zErrM
1ab1b 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 sg ){. sq
1ab1c 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
1ab1d 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 izeof(zErr), zEr
1ab1e 72 2c 20 22 25 73 22 2c 20 73 50 61 72 73 65 2e r, "%s", sParse.
1ab1f 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 zErrMsg);.
1ab20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 }. sqlite3D
1ab21 62 46 72 65 65 28 64 62 2c 20 73 50 61 72 73 65 bFree(db, sParse
1ab22 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 .zErrMsg);.
1ab23 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
1ab24 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 OR;. (void)
1ab25 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1ab26 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 (db);. sqli
1ab27 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c te3BtreeLeaveAll
1ab28 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f (db);. goto
1ab29 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a blob_open_out;.
1ab2a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f }.. /* No
1ab2b 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66 6f w search pTab fo
1ab2c 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75 r the exact colu
1ab2d 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 mn. */. for(i
1ab2e 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 Col=0; iCol < pT
1ab2f 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b ab->nCol; iCol++
1ab30 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 ) {. if( sq
1ab31 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 lite3StrICmp(pTa
1ab32 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e b->aCol[iCol].zN
1ab33 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 ame, zColumn)==0
1ab34 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 ){. brea
1ab35 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
1ab36 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 . if( iCol==p
1ab37 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 Tab->nCol ){.
1ab38 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1ab39 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 ntf(sizeof(zErr)
1ab3a 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 , zErr, "no such
1ab3b 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 column: \"%s\""
1ab3c 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 , zColumn);.
1ab3d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1ab3e 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 ROR;. (void
1ab3f 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 )sqlite3SafetyOf
1ab40 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c f(db);. sql
1ab41 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c ite3BtreeLeaveAl
1ab42 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 l(db);. got
1ab43 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b o blob_open_out;
1ab44 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 . }.. /* I
1ab45 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 62 f the value is b
1ab46 65 69 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 eing opened for
1ab47 77 72 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 writing, check t
1ab48 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 hat the. ** c
1ab49 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 olumn is not ind
1ab4a 65 78 65 64 2e 20 49 74 20 69 73 20 61 67 61 69 exed. It is agai
1ab4b 6e 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f nst the rules to
1ab4c 20 6f 70 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 open an. **
1ab4d 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 indexed column f
1ab4e 6f 72 20 77 72 69 74 69 6e 67 2e 0a 20 20 20 20 or writing..
1ab4f 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 */. if( flags
1ab50 20 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 ){. Index
1ab51 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 66 6f 72 *pIdx;. for
1ab52 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 (pIdx=pTab->pInd
1ab53 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 ex; pIdx; pIdx=p
1ab54 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Idx->pNext){.
1ab55 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 int j;.
1ab56 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 for(j=0; j<p
1ab57 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b Idx->nColumn; j+
1ab58 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
1ab59 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e ( pIdx->aiColumn
1ab5a 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 [j]==iCol ){.
1ab5b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1ab5c 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
1ab5d 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 (zErr), zErr,.
1ab5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ab5f 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e "cann
1ab60 6f 74 20 6f 70 65 6e 20 69 6e 64 65 78 65 64 20 ot open indexed
1ab61 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 column for writi
1ab62 6e 67 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ng");.
1ab63 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1ab64 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ROR;.
1ab65 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1ab66 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 fetyOff(db);.
1ab67 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1ab68 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 BtreeLeaveAll(db
1ab69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 );. g
1ab6a 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 oto blob_open_ou
1ab6b 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 t;. }.
1ab6c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1ab6d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 . }.. v =
1ab6e 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 sqlite3VdbeCreat
1ab6f 65 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 e(db);. if( v
1ab70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 ){. int iD
1ab71 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
1ab72 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 aToIndex(db, pTa
1ab73 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 b->pSchema);.
1ab74 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1ab75 64 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f dOpList(v, sizeo
1ab76 66 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 f(openBlob)/size
1ab77 6f 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 of(VdbeOpList),
1ab78 6f 70 65 6e 42 6c 6f 62 29 3b 0a 0a 20 20 20 20 openBlob);..
1ab79 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 /* Configure t
1ab7a 68 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f he OP_Transactio
1ab7b 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 n */. sqlit
1ab7c 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 e3VdbeChangeP1(v
1ab7d 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 , 0, iDb);.
1ab7e 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1ab7f 67 65 50 32 28 76 2c 20 30 2c 20 28 66 6c 61 67 geP2(v, 0, (flag
1ab80 73 20 3f 20 31 20 3a 20 30 29 29 3b 0a 0a 20 20 s ? 1 : 0));..
1ab81 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 /* Configure
1ab82 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43 6f the OP_VerifyCo
1ab83 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 okie */. sq
1ab84 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1ab85 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 1(v, 1, iDb);.
1ab86 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1ab87 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 54 hangeP2(v, 1, pT
1ab88 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 ab->pSchema->sch
1ab89 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20 ema_cookie);..
1ab8a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 /* Make sure
1ab8b 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 a mutex is held
1ab8c 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f on the table to
1ab8d 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a be accessed */.
1ab8e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1ab8f 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 eUsesBtree(v, iD
1ab90 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 52 b); .. /* R
1ab91 65 6d 6f 76 65 20 65 69 74 68 65 72 20 74 68 65 emove either the
1ab92 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 6f 72 OP_OpenWrite or
1ab93 20 4f 70 65 6e 52 65 61 64 2e 20 53 65 74 20 74 OpenRead. Set t
1ab94 68 65 20 50 32 20 0a 20 20 20 20 20 20 2a 2a 20 he P2 . **
1ab95 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 parameter of the
1ab96 20 6f 74 68 65 72 20 74 6f 20 70 54 61 62 2d 3e other to pTab->
1ab97 74 6e 75 6d 2e 20 0a 20 20 20 20 20 20 2a 2f 0a tnum. . */.
1ab98 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1ab99 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c eChangeToNoop(v,
1ab9a 20 28 66 6c 61 67 73 20 3f 20 33 20 3a 20 35 29 (flags ? 3 : 5)
1ab9b 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , 1);. sqli
1ab9c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1ab9d 76 2c 20 28 66 6c 61 67 73 20 3f 20 35 20 3a 20 v, (flags ? 5 :
1ab9e 33 29 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 3), pTab->tnum);
1ab9f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1aba0 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 28 66 beChangeP3(v, (f
1aba1 6c 61 67 73 20 3f 20 35 20 3a 20 33 29 2c 20 69 lags ? 5 : 3), i
1aba2 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 Db);.. /* C
1aba3 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f onfigure the OP_
1aba4 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2e 20 43 SetNumColumns. C
1aba5 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72 onfigure the cur
1aba6 73 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 sor to. **
1aba7 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 think that the t
1aba8 61 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 able has one mor
1aba9 65 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 e column than it
1abaa 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a really. **
1abab 20 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c does. An OP_Col
1abac 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 umn to retrieve
1abad 74 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 this imaginary c
1abae 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 olumn will.
1abaf 20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 ** always retur
1abb0 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 n an SQL NULL. T
1abb1 68 69 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 his is useful be
1abb2 63 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 cause it means.
1abb3 20 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 ** we can i
1abb4 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 nvoke OP_Column
1abb5 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 to fill in the v
1abb6 64 62 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 dbe cursors type
1abb7 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f . ** and o
1abb8 66 66 73 65 74 20 63 61 63 68 65 20 77 69 74 68 ffset cache with
1abb9 6f 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 out causing any
1abba 49 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 IO.. */.
1abbb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1abbc 61 6e 67 65 50 32 28 76 2c 20 66 6c 61 67 73 20 angeP2(v, flags
1abbd 3f 20 34 20 3a 20 32 2c 20 70 54 61 62 2d 3e 6e ? 4 : 2, pTab->n
1abbe 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 Col+1);. sq
1abbf 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1abc0 32 28 76 2c 20 38 2c 20 70 54 61 62 2d 3e 6e 43 2(v, 8, pTab->nC
1abc1 6f 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ol);. if( !
1abc2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1abc3 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1abc4 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
1abc5 28 76 2c 20 31 2c 20 31 2c 20 31 2c 20 30 29 3b (v, 1, 1, 1, 0);
1abc6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1abc7 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
1abc8 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b reeLeaveAll(db);
1abc9 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1abca 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 3SafetyOff(db);.
1abcb 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1abcc 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c TE_OK || db->mal
1abcd 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1abce 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 goto blob_ope
1abcf 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 n_out;. }..
1abd0 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 sqlite3_bind_i
1abd1 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f 73 74 nt64((sqlite3_st
1abd2 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f 77 29 mt *)v, 1, iRow)
1abd3 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1abd4 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74 65 33 e3_step((sqlite3
1abd5 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 _stmt *)v);.
1abd6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 if( rc!=SQLITE_R
1abd7 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 74 74 OW ){. nAtt
1abd8 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 72 63 empt++;. rc
1abd9 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c = sqlite3_final
1abda 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d ize((sqlite3_stm
1abdb 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 73 71 t *)v);. sq
1abdc 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
1abdd 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 izeof(zErr), zEr
1abde 72 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 r, sqlite3_errms
1abdf 67 28 64 62 29 29 3b 0a 20 20 20 20 20 20 76 20 g(db));. v
1abe0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 77 = 0;. }. } w
1abe1 68 69 6c 65 28 20 6e 41 74 74 65 6d 70 74 3c 35 hile( nAttempt<5
1abe2 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 && rc==SQLITE_S
1abe3 43 48 45 4d 41 20 29 3b 0a 0a 20 20 69 66 28 20 CHEMA );.. if(
1abe4 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 rc==SQLITE_ROW )
1abe5 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 {. /* The row
1abe6 2d 72 65 63 6f 72 64 20 68 61 73 20 62 65 65 6e -record has been
1abe7 20 6f 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 opened successf
1abe8 75 6c 6c 79 2e 20 43 68 65 63 6b 20 74 68 61 74 ully. Check that
1abe9 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 the. ** colu
1abea 6d 6e 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 63 mn in question c
1abeb 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 6f 72 20 ontains text or
1abec 61 20 62 6c 6f 62 2e 20 49 66 20 69 74 20 63 6f a blob. If it co
1abed 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 65 ntains. ** te
1abee 78 74 2c 20 69 74 20 69 73 20 75 70 20 74 6f 20 xt, it is up to
1abef 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 67 65 the caller to ge
1abf0 74 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 72 t the encoding r
1abf1 69 67 68 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ight.. */.
1abf2 20 49 6e 63 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 Incrblob *pBlob
1abf3 3b 0a 20 20 20 20 75 33 32 20 74 79 70 65 20 3d ;. u32 type =
1abf4 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 54 v->apCsr[0]->aT
1abf5 79 70 65 5b 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 ype[iCol];..
1abf6 69 66 28 20 74 79 70 65 3c 31 32 20 29 7b 0a 20 if( type<12 ){.
1abf7 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1abf8 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 rintf(sizeof(zEr
1abf9 72 29 2c 20 7a 45 72 72 2c 20 22 63 61 6e 6e 6f r), zErr, "canno
1abfa 74 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 t open value of
1abfb 74 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 type %s",.
1abfc 20 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c type==0?"nul
1abfd 6c 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 l": type==7?"rea
1abfe 6c 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 l": "integer".
1abff 20 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 );. rc
1ac00 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1ac01 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f goto blob_
1ac02 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a open_out;. }.
1ac03 20 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 pBlob = (Inc
1ac04 72 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 rblob *)sqlite3D
1ac05 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1ac06 73 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 sizeof(Incrblob)
1ac07 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
1ac08 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1ac09 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1ac0a 65 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 ee(db, pBlob);.
1ac0b 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1ac0c 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
1ac0d 20 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 pBlob->flags
1ac0e 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c = flags;. pBl
1ac0f 6f 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 ob->pCsr = v->a
1ac10 70 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 pCsr[0]->pCursor
1ac11 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1ac12 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 eeEnterCursor(pB
1ac13 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 lob->pCsr);.
1ac14 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 sqlite3BtreeCach
1ac15 65 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d eOverflow(pBlob-
1ac16 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 >pCsr);. sqli
1ac17 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 te3BtreeLeaveCur
1ac18 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 sor(pBlob->pCsr)
1ac19 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 ;. pBlob->pSt
1ac1a 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 mt = (sqlite3_st
1ac1b 6d 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f mt *)v;. pBlo
1ac1c 62 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e b->iOffset = v->
1ac1d 61 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 apCsr[0]->aOffse
1ac1e 74 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c t[iCol];. pBl
1ac1f 6f 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 ob->nByte = sqli
1ac20 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1ac21 65 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 eLen(type);.
1ac22 70 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a pBlob->db = db;.
1ac23 20 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 *ppBlob = (s
1ac24 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 qlite3_blob *)pB
1ac25 6c 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 lob;. rc = SQ
1ac26 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
1ac27 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1ac28 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
1ac29 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
1ac2a 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 f(zErr), zErr, "
1ac2b 6e 6f 20 73 75 63 68 20 72 6f 77 69 64 3a 20 25 no such rowid: %
1ac2c 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 20 lld", iRow);.
1ac2d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
1ac2e 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 OR;. }..blob_op
1ac2f 65 6e 5f 6f 75 74 3a 0a 20 20 7a 45 72 72 5b 73 en_out:. zErr[s
1ac30 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d izeof(zErr)-1] =
1ac31 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 72 63 21 '\0';. if( rc!
1ac32 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 =SQLITE_OK || db
1ac33 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1ac34 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 {. sqlite3_fi
1ac35 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f nalize((sqlite3_
1ac36 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 7d 0a 20 stmt *)v);. }.
1ac37 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 sqlite3Error(db
1ac38 2c 20 72 63 2c 20 28 72 63 3d 3d 53 51 4c 49 54 , rc, (rc==SQLIT
1ac39 45 5f 4f 4b 3f 30 3a 7a 45 72 72 29 29 3b 0a 20 E_OK?0:zErr));.
1ac3a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 rc = sqlite3Api
1ac3b 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 Exit(db, rc);.
1ac3c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1ac3d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ave(db->mutex);.
1ac3e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1ac3f 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c /*.** Close a bl
1ac40 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 ob handle that w
1ac41 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 as previously cr
1ac42 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 eated using.** s
1ac43 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
1ac44 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
1ac45 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1ac46 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 ob_close(sqlite3
1ac47 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 _blob *pBlob){.
1ac48 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 Incrblob *p = (
1ac49 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 Incrblob *)pBlob
1ac4a 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 ;. int rc;.. r
1ac4b 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 c = sqlite3_fina
1ac4c 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a lize(p->pStmt);.
1ac4d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ac4e 70 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 72 65 74 p->db, p);. ret
1ac4f 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1ac50 20 50 65 72 66 6f 72 6d 20 61 20 72 65 61 64 20 Perform a read
1ac51 6f 72 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 or write operati
1ac52 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 0a 2a 2f 0a on on a blob.*/.
1ac53 73 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 62 52 static int blobR
1ac54 65 61 64 57 72 69 74 65 28 0a 20 20 73 71 6c 69 eadWrite(. sqli
1ac55 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c te3_blob *pBlob,
1ac56 20 0a 20 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 . void *z, .
1ac57 69 6e 74 20 6e 2c 20 0a 20 20 69 6e 74 20 69 4f int n, . int iO
1ac58 66 66 73 65 74 2c 20 0a 20 20 69 6e 74 20 28 2a ffset, . int (*
1ac59 78 43 61 6c 6c 29 28 42 74 43 75 72 73 6f 72 2a xCall)(BtCursor*
1ac5a 2c 20 75 33 32 2c 20 75 33 32 2c 20 76 6f 69 64 , u32, u32, void
1ac5b 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *).){. int rc;.
1ac5c 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 Incrblob *p =
1ac5d 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f (Incrblob *)pBlo
1ac5e 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 b;. Vdbe *v;.
1ac5f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d sqlite3 *db = p-
1ac60 3e 64 62 3b 20 20 0a 0a 20 20 73 71 6c 69 74 65 >db; .. sqlite
1ac61 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 3_mutex_enter(db
1ac62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 ->mutex);. v =
1ac63 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b (Vdbe*)p->pStmt;
1ac64 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 .. if( n<0 || i
1ac65 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 Offset<0 || (iOf
1ac66 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 fset+n)>p->nByte
1ac67 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 ){. /* Reque
1ac68 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e st is out of ran
1ac69 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72 61 ge. Return a tra
1ac6a 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f nsient error. */
1ac6b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1ac6c 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 _ERROR;. sqli
1ac6d 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c te3Error(db, SQL
1ac6e 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 ITE_ERROR, 0);.
1ac6f 20 7d 20 65 6c 73 65 20 69 66 28 20 76 3d 3d 30 } else if( v==0
1ac70 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
1ac71 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d ere is no statem
1ac72 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e ent handle, then
1ac73 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 the blob-handle
1ac74 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 has. ** alre
1ac75 61 64 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 ady been invalid
1ac76 61 74 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c ated. Return SQL
1ac77 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 ITE_ABORT in thi
1ac78 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 s case.. */.
1ac79 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 rc = SQLITE_A
1ac7a 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 BORT;. }else{.
1ac7b 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65 /* Call eithe
1ac7c 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f 72 r BtreeData() or
1ac7d 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 2e BtreePutData().
1ac7e 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 If SQLITE_ABORT
1ac7f 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 is. ** retur
1ac80 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 ned, clean-up th
1ac81 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 e statement hand
1ac82 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 le.. */. a
1ac83 73 73 65 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e ssert( db == v->
1ac84 64 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 db );. sqlite
1ac85 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 3BtreeEnterCurso
1ac86 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 r(p->pCsr);.
1ac87 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43 rc = xCall(p->pC
1ac88 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69 sr, iOffset+p->i
1ac89 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 Offset, n, z);.
1ac8a 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
1ac8b 65 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 eaveCursor(p->pC
1ac8c 73 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d sr);. if( rc=
1ac8d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b =SQLITE_ABORT ){
1ac8e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1ac8f 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 beFinalize(v);.
1ac90 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20 p->pStmt =
1ac91 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
1ac92 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 db->errCode
1ac93 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72 = rc;. v->r
1ac94 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 c = rc;. }.
1ac95 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 }. rc = sqlite3
1ac96 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b ApiExit(db, rc);
1ac97 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
1ac98 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 _leave(db->mutex
1ac99 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1ac9a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 }../*.** Read da
1ac9b 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68 ta from a blob h
1ac9c 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 andle..*/.SQLITE
1ac9d 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1ac9e 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 _blob_read(sqlit
1ac9f 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 e3_blob *pBlob,
1aca0 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 void *z, int n,
1aca1 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 int iOffset){.
1aca2 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 return blobReadW
1aca3 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e rite(pBlob, z, n
1aca4 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 , iOffset, sqlit
1aca5 65 33 42 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a e3BtreeData);.}.
1aca6 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 ./*.** Write dat
1aca7 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 a to a blob hand
1aca8 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 le..*/.SQLITE_AP
1aca9 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1acaa 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 ob_write(sqlite3
1acab 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f _blob *pBlob, co
1acac 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 nst void *z, int
1acad 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 n, int iOffset)
1acae 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 {. return blobR
1acaf 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 eadWrite(pBlob,
1acb0 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f (void *)z, n, iO
1acb1 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 ffset, sqlite3Bt
1acb2 72 65 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a reePutData);.}..
1acb3 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 6c /*.** Query a bl
1acb4 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 ob handle for th
1acb5 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
1acb6 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e ta..**.** The In
1acb7 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65 crblob.nByte fie
1acb8 6c 64 20 69 73 20 66 69 78 65 64 20 66 6f 72 20 ld is fixed for
1acb9 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 the lifetime of
1acba 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 the Incrblob.**
1acbb 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 72 so no mutex is r
1acbc 65 71 75 69 72 65 64 20 66 6f 72 20 61 63 63 65 equired for acce
1acbd 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ss..*/.SQLITE_AP
1acbe 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1acbf 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 ob_bytes(sqlite3
1acc0 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 _blob *pBlob){.
1acc1 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 Incrblob *p = (
1acc2 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 Incrblob *)pBlob
1acc3 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 42 ;. return p->nB
1acc4 79 74 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f yte;.}..#endif /
1acc5 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 * #ifndef SQLITE
1acc6 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a _OMIT_INCRBLOB *
1acc7 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
1acc8 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 62 6c ** End of vdbebl
1acc9 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ob.c ***********
1acca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1accb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1accc 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
1accd 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6a 6f ** Begin file jo
1acce 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a urnal.c ********
1accf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1acd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1acd1 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 */./*.** 2007 Au
1acd2 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 gust 22.**.** Th
1acd3 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1acd4 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1acd5 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1acd6 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1acd7 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1acd8 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1acd9 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1acda 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1acdb 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1acdc 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1acdd 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1acde 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1acdf 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1ace0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1ace1 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1ace2 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1ace3 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1ace4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ace5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ace6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ace7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ace8 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 40 28 23 ******.**.** @(#
1ace9 29 20 24 49 64 3a 20 6a 6f 75 72 6e 61 6c 2e 63 ) $Id: journal.c
1acea 2c 76 20 31 2e 38 20 32 30 30 38 2f 30 35 2f 30 ,v 1.8 2008/05/0
1aceb 31 20 31 38 3a 30 31 3a 34 37 20 64 72 68 20 45 1 18:01:47 drh E
1acec 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 xp $.*/..#ifdef
1aced 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
1acee 4f 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a 2a OMIC_WRITE../*.*
1acef 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c * This file impl
1acf0 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c ements a special
1acf1 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 kind of sqlite3
1acf2 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 _file object use
1acf3 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74 d.** by SQLite t
1acf4 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c o create journal
1acf5 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 74 files if the at
1acf6 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d omic-write optim
1acf7 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e ization.** is en
1acf8 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 abled..**.** The
1acf9 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 61 distinctive cha
1acfa 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 74 racteristic of t
1acfb 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 his sqlite3_file
1acfc 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 is that the.**
1acfd 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 actual on disk f
1acfe 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 6c ile is created l
1acff 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 azily. When the
1ad00 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c file is created,
1ad01 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73 .** the caller s
1ad02 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66 65 pecifies a buffe
1ad03 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e r size for an in
1ad04 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 -memory buffer t
1ad05 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 o.** be used to
1ad06 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20 61 service read() a
1ad07 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75 65 nd write() reque
1ad08 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20 sts. The actual
1ad09 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 file.** on disk
1ad0a 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f is not created o
1ad0b 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 r populated unti
1ad0c 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 l either:.**.**
1ad0d 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 1) The in-memo
1ad0e 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ry representatio
1ad0f 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 n grows too larg
1ad10 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 e for the alloca
1ad11 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 ted .** buf
1ad12 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 fer, or.** 2)
1ad13 54 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 The xSync() meth
1ad14 6f 64 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f od is called..*/
1ad15 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 ..../*.** A Jour
1ad16 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69 nalFile object i
1ad17 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 s a subclass of
1ad18 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65 sqlite3_file use
1ad19 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 d by.** as an op
1ad1a 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 en file handle f
1ad1b 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 or journal files
1ad1c 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72 ..*/.struct Jour
1ad1d 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 nalFile {. sqli
1ad1e 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
1ad1f 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49 pMethod; /* I
1ad20 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f /O methods on jo
1ad21 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 urnal files */.
1ad22 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20 int nBuf;
1ad23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad24 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66 /* Size of zBuf
1ad25 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 [] in bytes */.
1ad26 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20 char *zBuf;
1ad27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad28 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66 /* Space to buf
1ad29 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74 fer journal writ
1ad2a 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a es */. int iSiz
1ad2b 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1ad2c 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e /* Amoun
1ad2d 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72 t of zBuf[] curr
1ad2e 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 ently used */.
1ad2f 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 int flags;
1ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad31 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a /* xOpen flags *
1ad32 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 /. sqlite3_vfs
1ad33 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 *pVfs;
1ad34 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c /* The "real
1ad35 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 " underlying VFS
1ad36 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
1ad37 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 le *pReal;
1ad38 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 /* The "re
1ad39 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 al" underlying f
1ad3a 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
1ad3b 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1ad3c 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 zJournal;
1ad3d 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
1ad3e 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1ad3f 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 */.};.typedef st
1ad40 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 ruct JournalFile
1ad41 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f JournalFile;../
1ad42 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20 *.** If it does
1ad43 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 not already exis
1ad44 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70 ts, create and p
1ad45 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64 opulate the on-d
1ad46 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 isk file .** for
1ad47 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a JournalFile p..
1ad48 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 */.static int cr
1ad49 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c eateFile(Journal
1ad4a 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 File *p){. int
1ad4b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1ad4c 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 if( !p->pReal
1ad4d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
1ad4e 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71 ile *pReal = (sq
1ad4f 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b lite3_file *)&p[
1ad50 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 1];. rc = sql
1ad51 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 ite3OsOpen(p->pV
1ad52 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c fs, p->zJournal,
1ad53 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 pReal, p->flags
1ad54 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
1ad55 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1ad56 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20 p->pReal =
1ad57 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 pReal;. if(
1ad58 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20 p->iSize>0 ){.
1ad59 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 2d assert(p-
1ad5a 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 >iSize<=p->nBuf)
1ad5b 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
1ad5c 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d qlite3OsWrite(p-
1ad5d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c >pReal, p->zBuf,
1ad5e 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20 p->iSize, 0);.
1ad5f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1ad60 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1ad61 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 ./*.** Close the
1ad62 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1ad63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 int jrnlClose(s
1ad64 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1ad65 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c d){. JournalFil
1ad66 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 e *p = (JournalF
1ad67 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 ile *)pJfd;. if
1ad68 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 ( p->pReal ){.
1ad69 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
1ad6a 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a (p->pReal);. }.
1ad6b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
1ad6c 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 ->zBuf);. retur
1ad6d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1ad6e 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 /*.** Read data
1ad6f 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a from the file..*
1ad70 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e /.static int jrn
1ad71 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 lRead(. sqlite3
1ad72 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 _file *pJfd,
1ad73 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
1ad74 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 ile from which t
1ad75 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 o read */. void
1ad76 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 *zBuf,
1ad77 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 /* Put the re
1ad78 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
1ad79 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1ad7a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1ad7b 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
1ad7c 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e d */. sqlite_in
1ad7d 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1ad7e 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
1ad7f 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
1ad80 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 .){. int rc = S
1ad81 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 QLITE_OK;. Jour
1ad82 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1ad83 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1ad84 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c ;. if( p->pReal
1ad85 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1ad86 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52 ite3OsRead(p->pR
1ad87 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c eal, zBuf, iAmt,
1ad88 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65 iOfst);. }else
1ad89 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 41 {. assert( iA
1ad8a 6d 74 2b 69 4f 66 73 74 3c 3d 70 2d 3e 69 53 69 mt+iOfst<=p->iSi
1ad8b 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 ze );. memcpy
1ad8c 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b (zBuf, &p->zBuf[
1ad8d 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 iOfst], iAmt);.
1ad8e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1ad8f 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 }../*.** Write d
1ad90 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e ata to the file.
1ad91 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a .*/.static int j
1ad92 72 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 rnlWrite(. sqli
1ad93 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 te3_file *pJfd,
1ad94 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /* The journa
1ad95 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 l file into whic
1ad96 68 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 h to write */.
1ad97 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 const void *zBuf
1ad98 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 , /* Take d
1ad99 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 ata to be writte
1ad9a 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 n from here */.
1ad9b 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 int iAmt,
1ad9c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1ad9d 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 r of bytes to wr
1ad9e 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f ite */. sqlite_
1ad9f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 int64 iOfst
1ada0 2f 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 /* Begin writing
1ada1 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 at this offset
1ada2 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f into the file */
1ada3 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 .){. int rc = S
1ada4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 QLITE_OK;. Jour
1ada5 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1ada6 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1ada7 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 ;. if( !p->pRea
1ada8 6c 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 l && (iOfst+iAmt
1ada9 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 )>p->nBuf ){.
1adaa 20 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 rc = createFile
1adab 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 (p);. }. if( r
1adac 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1adad 20 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c if( p->pReal
1adae 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
1adaf 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d qlite3OsWrite(p-
1adb0 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 >pReal, zBuf, iA
1adb1 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 mt, iOfst);.
1adb2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
1adb3 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 cpy(&p->zBuf[iOf
1adb4 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 st], zBuf, iAmt)
1adb5 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 ;. if( p->i
1adb6 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 Size<(iOfst+iAmt
1adb7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e ) ){. p->
1adb8 69 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 iSize = (iOfst+i
1adb9 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Amt);. }.
1adba 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1adbb 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 rc;.}../*.** Tr
1adbc 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e uncate the file.
1adbd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a .*/.static int j
1adbe 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 rnlTruncate(sqli
1adbf 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 te3_file *pJfd,
1adc0 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a sqlite_int64 siz
1adc1 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 e){. int rc = S
1adc2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 QLITE_OK;. Jour
1adc3 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1adc4 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1adc5 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c ;. if( p->pReal
1adc6 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1adc7 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
1adc8 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a ->pReal, size);.
1adc9 20 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 }else if( size
1adca 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 <p->iSize ){.
1adcb 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 p->iSize = size
1adcc 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1adcd 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 c;.}../*.** Sync
1adce 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 the file..*/.st
1adcf 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e atic int jrnlSyn
1add0 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a c(sqlite3_file *
1add1 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 pJfd, int flags)
1add2 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f {. int rc;. Jo
1add3 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 urnalFile *p = (
1add4 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a JournalFile *)pJ
1add5 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 fd;. if( p->pRe
1add6 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 al ){. rc = s
1add7 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e qlite3OsSync(p->
1add8 70 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 pReal, flags);.
1add9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
1adda 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
1addb 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1addc 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 /*.** Query the
1addd 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
1adde 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 in bytes..*/.st
1addf 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c atic int jrnlFil
1ade0 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 eSize(sqlite3_fi
1ade1 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 le *pJfd, sqlite
1ade2 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a _int64 *pSize){.
1ade3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1ade4 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 E_OK;. JournalF
1ade5 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1ade6 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1ade7 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a if( p->pReal ){.
1ade8 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ade9 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 OsFileSize(p->pR
1adea 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d eal, pSize);. }
1adeb 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 else{. *pSize
1adec 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 = (sqlite_int64
1aded 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a ) p->iSize;. }.
1adee 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1adef 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d /*.** Table of m
1adf0 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e ethods for Journ
1adf1 61 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 alFile sqlite3_f
1adf2 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 ile object..*/.s
1adf3 74 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c tatic struct sql
1adf4 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
1adf5 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f JournalFileMetho
1adf6 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 ds = {. 1,
1adf7 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
1adf8 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f ion */. jrnlClo
1adf9 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 se, /* xClos
1adfa 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c e */. jrnlRead,
1adfb 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a /* xRead *
1adfc 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 /. jrnlWrite,
1adfd 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a /* xWrite */.
1adfe 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 jrnlTruncate,
1adff 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f /* xTruncate */
1ae00 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 . jrnlSync,
1ae01 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 /* xSync */.
1ae02 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f jrnlFileSize, /
1ae03 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 * xFileSize */.
1ae04 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1ae05 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c /* xLock */. 0,
1ae06 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ae07 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 xUnlock */. 0,
1ae08 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1ae09 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
1ae0a 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 k */. 0,
1ae0b 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f /* xFileCo
1ae0c 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 ntrol */. 0,
1ae0d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 /* xSe
1ae0e 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 ctorSize */. 0
1ae0f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ae10 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 xDeviceCharacter
1ae11 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a istics */.};../*
1ae12 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 .** Open a jour
1ae13 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c nal file..*/.SQL
1ae14 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1ae15 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 sqlite3JournalOp
1ae16 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 en(. sqlite3_vf
1ae17 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
1ae18 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 /* The VFS to u
1ae19 73 65 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 se for actual fi
1ae1a 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 le I/O */. cons
1ae1b 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 t char *zName,
1ae1c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
1ae1d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
1ae1e 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f le */. sqlite3_
1ae1f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 file *pJfd,
1ae20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 /* Preallocat
1ae21 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 ed, blank file h
1ae22 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 andle */. int f
1ae23 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
1ae24 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 /* Opening
1ae25 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 flags */. int
1ae26 6e 42 75 66 20 20 20 20 20 20 20 20 20 20 20 20 nBuf
1ae27 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
1ae28 62 75 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 buffered before
1ae29 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 opening the file
1ae2a 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c */.){. Journal
1ae2b 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e File *p = (Journ
1ae2c 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 alFile *)pJfd;.
1ae2d 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 memset(p, 0, sq
1ae2e 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 lite3JournalSize
1ae2f 28 70 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e (pVfs));. if( n
1ae30 42 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e Buf>0 ){. p->
1ae31 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 zBuf = sqlite3Ma
1ae32 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a llocZero(nBuf);.
1ae33 20 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 if( !p->zBuf
1ae34 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1ae35 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1ae36 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1ae37 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1ae38 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 OsOpen(pVfs, zNa
1ae39 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c me, pJfd, flags,
1ae3a 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 0);. }. p->pM
1ae3b 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c ethod = &Journal
1ae3c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 FileMethods;. p
1ae3d 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 ->nBuf = nBuf;.
1ae3e 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 p->flags = flag
1ae3f 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c s;. p->zJournal
1ae40 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 = zName;. p->p
1ae41 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 Vfs = pVfs;. re
1ae42 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1ae43 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
1ae44 61 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 argument p point
1ae45 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 s to a JournalFi
1ae46 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e le structure, an
1ae47 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 d the underlying
1ae48 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 .** file has not
1ae49 20 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 yet been create
1ae4a 64 2c 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 d, create it now
1ae4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1ae4c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a ATE int sqlite3J
1ae4d 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c ournalCreate(sql
1ae4e 69 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 ite3_file *p){.
1ae4f 20 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 if( p->pMethods
1ae50 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 !=&JournalFileMe
1ae51 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 thods ){. ret
1ae52 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1ae53 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 }. return crea
1ae54 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 teFile((JournalF
1ae55 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 ile *)p);.}../*
1ae56 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1ae57 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 umber of bytes r
1ae58 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 equired to store
1ae59 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 a JournalFile t
1ae5a 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 hat uses vfs.**
1ae5b 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 pVfs to create t
1ae5c 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e he underlying on
1ae5d 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a -disk files..*/.
1ae5e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1ae5f 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 nt sqlite3Journa
1ae60 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 lSize(sqlite3_vf
1ae61 73 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 s *pVfs){. retu
1ae62 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 rn (pVfs->szOsFi
1ae63 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 le+sizeof(Journa
1ae64 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 lFile));.}.#endi
1ae65 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
1ae66 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 ** End of journa
1ae67 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a l.c ************
1ae68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ae69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ae6a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
1ae6b 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 ** Begin file me
1ae6c 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a mjournal.c *****
1ae6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ae6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ae6f 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 */./*.** 2008 Oc
1ae70 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 tober 7.**.** Th
1ae71 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1ae72 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1ae73 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1ae74 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1ae75 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1ae76 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1ae77 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1ae78 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1ae79 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1ae7a 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1ae7b 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1ae7c 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1ae7d 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1ae7e 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1ae7f 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1ae80 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1ae81 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1ae82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ae83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ae84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ae85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ae86 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
1ae87 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1ae88 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c code use to impl
1ae89 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f ement an in-memo
1ae8a 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 ry rollback jour
1ae8b 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d nal..** The in-m
1ae8c 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a emory rollback j
1ae8d 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 ournal is used t
1ae8e 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 o journal transa
1ae8f 63 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a ctions for.** ":
1ae90 6d 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 memory:" databas
1ae91 65 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 es and when the
1ae92 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d journal_mode=MEM
1ae93 4f 52 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 ORY pragma is us
1ae94 65 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 ed..**.** @(#) $
1ae95 49 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 Id: memjournal.c
1ae96 2c 76 20 31 2e 32 20 32 30 30 38 2f 31 30 2f 32 ,v 1.2 2008/10/2
1ae97 38 20 31 38 3a 31 32 3a 33 36 20 64 72 68 20 45 8 18:12:36 drh E
1ae98 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 xp $.*/../* Forw
1ae99 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 ard references t
1ae9a 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 o internal struc
1ae9b 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 tures */.typedef
1ae9c 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e struct MemJourn
1ae9d 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 al MemJournal;.t
1ae9e 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 69 ypedef struct Fi
1ae9f 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e lePoint FilePoin
1aea0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
1aea1 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 t FileChunk File
1aea2 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 Chunk;../* Space
1aea3 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c to hold the rol
1aea4 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 lback journal is
1aea5 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e allocated in in
1aea6 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 crements of.** t
1aea7 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a his many bytes..
1aea8 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e */.#define JOURN
1aea9 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 31 30 32 AL_CHUNKSIZE 102
1aeaa 34 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 4../* Macro to f
1aeab 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 ind the minimum
1aeac 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 of two numeric v
1aead 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e alues..*/.#defin
1aeae 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c e MIN(x,y) ((x)<
1aeaf 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 0a 2f 2a (y)?(x):(y))../*
1aeb0 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b .** The rollback
1aeb1 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 6f 6d 70 journal is comp
1aeb2 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 osed of a linked
1aeb3 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20 73 list of these s
1aeb4 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 tructures..*/.st
1aeb5 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 7b ruct FileChunk {
1aeb6 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e . FileChunk *pN
1aeb7 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
1aeb8 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b /* Next chunk
1aeb9 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
1aeba 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a */. u8 zChunk[J
1aebb 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1aebc 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 ]; /* Content
1aebd 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f of this chunk */
1aebe 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e .};../*.** An in
1aebf 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
1aec0 62 6a 65 63 74 20 73 65 72 76 65 73 20 61 73 20 bject serves as
1aec1 61 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 a cursor into th
1aec2 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
1aec3 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f al..** The curso
1aec4 72 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 r can be either
1aec5 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77 for reading or w
1aec6 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 riting..*/.struc
1aec7 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b 0a 20 20 t FilePoint {.
1aec8 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f sqlite3_int64 iO
1aec9 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
1aeca 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 74 /* Offset from t
1aecb 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
1aecc 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 the file */. Fi
1aecd 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b leChunk *pChunk;
1aece 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1aecf 20 53 70 65 63 69 66 69 63 20 63 68 75 6e 6b 20 Specific chunk
1aed0 69 6e 74 6f 20 77 68 69 63 68 20 63 75 72 73 6f into which curso
1aed1 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a r points */.};..
1aed2 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 62 63 6c /*.** This subcl
1aed3 61 73 73 20 69 73 20 61 20 73 75 62 63 6c 61 73 ass is a subclas
1aed4 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c s of sqlite3_fil
1aed5 65 2e 20 20 45 61 63 68 20 6f 70 65 6e 20 6d 65 e. Each open me
1aed6 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 mory-journal.**
1aed7 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
1aed8 66 20 74 68 69 73 20 63 6c 61 73 73 2e 0a 2a 2f f this class..*/
1aed9 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e .struct MemJourn
1aeda 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 al {. sqlite3_i
1aedb 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 o_methods *pMeth
1aedc 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 od; /* Parent
1aedd 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 42 45 20 class. MUST BE
1aede 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 6c 65 43 FIRST */. FileC
1aedf 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b 20 20 20 hunk *pFirst;
1aee0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 /* He
1aee1 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 ad of in-memory
1aee2 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 chunk-list */.
1aee3 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69 FilePoint endpoi
1aee4 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nt;
1aee5 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
1aee6 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c e end of the fil
1aee7 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 e */. FilePoint
1aee8 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 20 20 20 readpoint;
1aee9 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1aeea 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 r to the end of
1aeeb 74 68 65 20 6c 61 73 74 20 78 52 65 61 64 28 29 the last xRead()
1aeec 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 */.};../*.** Re
1aeed 61 64 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 ad data from the
1aeee 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1aeef 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 int memjrnlRead
1aef0 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
1aef1 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 *pJfd, /* Th
1aef2 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 e journal file f
1aef3 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 rom which to rea
1aef4 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 d */. void *zBu
1aef5 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a f, /*
1aef6 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 Put the results
1aef7 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 here */. int i
1aef8 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 Amt,
1aef9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1aefa 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a ytes to read */.
1aefb 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 sqlite_int64 i
1aefc 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 Ofst /* Begi
1aefd 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 n reading at thi
1aefe 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 s offset */.){.
1aeff 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d MemJournal *p =
1af00 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 (MemJournal *)p
1af01 4a 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 74 20 Jfd;. u8 *zOut
1af02 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 6e 52 = zBuf;. int nR
1af03 65 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20 69 6e ead = iAmt;. in
1af04 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a t iChunkOffset;.
1af05 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 FileChunk *pCh
1af06 75 6e 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 unk;.. assert(
1af07 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65 iOfst+iAmt<=p->e
1af08 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1af09 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 65 61 );.. if( p->rea
1af0a 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d dpoint.iOffset!=
1af0b 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d iOfst || iOfst==
1af0c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1af0d 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 30 3b _int64 iOff = 0;
1af0e 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d . for(pChunk=
1af0f 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 20 20 p->pFirst; .
1af10 20 20 20 20 70 43 68 75 6e 6b 20 26 26 20 28 69 pChunk && (i
1af11 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e Off+JOURNAL_CHUN
1af12 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20 KSIZE)<=iOfst;.
1af13 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d 70 43 pChunk=pC
1af14 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20 hunk->pNext.
1af15 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20 2b 3d ){. iOff +=
1af16 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 JOURNAL_CHUNKSI
1af17 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 ZE;. }. }els
1af18 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 e{. pChunk =
1af19 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 p->readpoint.pCh
1af1a 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75 unk;. }.. iChu
1af1b 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 4f 66 73 nkOffset = (iOfs
1af1c 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 t%JOURNAL_CHUNKS
1af1d 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 IZE);. do {.
1af1e 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20 4a 4f int iSpace = JO
1af1f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 URNAL_CHUNKSIZE
1af20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a - iChunkOffset;.
1af21 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 int nCopy =
1af22 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52 MIN(nRead, (JOUR
1af23 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 NAL_CHUNKSIZE -
1af24 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0a iChunkOffset));.
1af25 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c memcpy(zOut,
1af26 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b &pChunk->zChunk
1af27 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 [iChunkOffset],
1af28 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a 4f 75 74 nCopy);. zOut
1af29 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20 20 20 6e += nCopy;. n
1af2a 52 65 61 64 20 2d 3d 20 69 53 70 61 63 65 3b 0a Read -= iSpace;.
1af2b 20 20 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 iChunkOffset
1af2c 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 = 0;. } while(
1af2d 20 6e 52 65 61 64 3e 3d 30 20 26 26 20 28 70 43 nRead>=0 && (pC
1af2e 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 hunk=pChunk->pNe
1af2f 78 74 29 20 26 26 20 6e 52 65 61 64 3e 30 20 29 xt) && nRead>0 )
1af30 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 ;. p->readpoint
1af31 2e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74 .iOffset = iOfst
1af32 2b 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64 +iAmt;. p->read
1af33 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 point.pChunk = p
1af34 43 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e Chunk;.. return
1af35 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1af36 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 *.** Write data
1af37 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a to the file..*/.
1af38 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 static int memjr
1af39 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 nlWrite(. sqlit
1af3a 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1af3b 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1af3c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 file into which
1af3d 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 to write */. c
1af3e 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c onst void *zBuf,
1af3f 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 /* Take da
1af40 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e ta to be written
1af41 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 from here */.
1af42 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1af43 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1af44 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 of bytes to wri
1af45 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 te */. sqlite_i
1af46 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f nt64 iOfst /
1af47 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 * Begin writing
1af48 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 at this offset i
1af49 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a nto the file */.
1af4a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ){. MemJournal
1af4b 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c *p = (MemJournal
1af4c 20 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e *)pJfd;. int n
1af4d 57 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20 Write = iAmt;.
1af4e 75 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38 u8 *zWrite = (u8
1af4f 20 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41 *)zBuf;.. /* A
1af50 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
1af51 6e 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 nal file should
1af52 6f 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70 only ever be app
1af53 65 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d ended to. Random
1af54 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69 . ** access wri
1af55 74 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75 tes are not requ
1af56 69 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a ired by sqlite..
1af57 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 69 4f */. assert(iO
1af58 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 fst==p->endpoint
1af59 2e 69 4f 66 66 73 65 74 29 3b 0a 0a 20 20 77 68 .iOffset);.. wh
1af5a 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 20 29 7b ile( nWrite>0 ){
1af5b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a . FileChunk *
1af5c 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70 pChunk = p->endp
1af5d 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20 oint.pChunk;.
1af5e 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 int iChunkOffse
1af5f 74 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e t = p->endpoint.
1af60 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f iOffset%JOURNAL_
1af61 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 69 CHUNKSIZE;. i
1af62 6e 74 20 69 53 70 61 63 65 20 3d 20 4d 49 4e 28 nt iSpace = MIN(
1af63 6e 57 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f nWrite, JOURNAL_
1af64 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 CHUNKSIZE - iChu
1af65 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 nkOffset);..
1af66 69 66 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 if( iChunkOffset
1af67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ==0 ){. /*
1af68 4e 65 77 20 63 68 75 6e 6b 20 69 73 20 72 65 71 New chunk is req
1af69 75 69 72 65 64 20 74 6f 20 65 78 74 65 6e 64 20 uired to extend
1af6a 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 the file. */.
1af6b 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e FileChunk *pN
1af6c 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ew = sqlite3_mal
1af6d 6c 6f 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 loc(sizeof(FileC
1af6e 68 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 66 hunk));. if
1af6f 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 ( !pNew ){.
1af70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1af71 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 _IOERR_NOMEM;.
1af72 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 }. pNew
1af73 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 ->pNext = 0;.
1af74 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20 29 7b if( pChunk ){
1af75 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1af76 20 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 p->pFirst );.
1af77 20 20 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e pChunk->pN
1af78 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 ext = pNew;.
1af79 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1af7a 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 46 69 assert( !p->pFi
1af7b 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 rst );. p
1af7c 2d 3e 70 46 69 72 73 74 20 3d 20 70 4e 65 77 3b ->pFirst = pNew;
1af7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1af7e 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e ->endpoint.pChun
1af7f 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a k = pNew;. }.
1af80 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e . memcpy(&p->
1af81 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d endpoint.pChunk-
1af82 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 >zChunk[iChunkOf
1af83 66 73 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69 fset], zWrite, i
1af84 53 70 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 69 Space);. zWri
1af85 74 65 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 te += iSpace;.
1af86 20 20 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 nWrite -= iSpa
1af87 63 65 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f ce;. p->endpo
1af88 69 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 int.iOffset += i
1af89 53 70 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 Space;. }.. re
1af8a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1af8b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 }../*.** Truncat
1af8c 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 e the file..*/.s
1af8d 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e tatic int memjrn
1af8e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 lTruncate(sqlite
1af8f 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 3_file *pJfd, sq
1af90 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 lite_int64 size)
1af91 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a {. MemJournal *
1af92 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 p = (MemJournal
1af93 2a 29 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68 *)pJfd;. FileCh
1af94 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 unk *pChunk;. a
1af95 73 73 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a ssert(size==0);.
1af96 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 46 pChunk = p->pF
1af97 69 72 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 irst;. while( p
1af98 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 46 69 6c Chunk ){. Fil
1af99 65 43 68 75 6e 6b 20 2a 70 54 6d 70 20 3d 20 70 eChunk *pTmp = p
1af9a 43 68 75 6e 6b 3b 0a 20 20 20 20 70 43 68 75 6e Chunk;. pChun
1af9b 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 k = pChunk->pNex
1af9c 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 t;. sqlite3_f
1af9d 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 ree(pTmp);. }.
1af9e 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e sqlite3MemJourn
1af9f 61 6c 4f 70 65 6e 28 70 4a 66 64 29 3b 0a 20 20 alOpen(pJfd);.
1afa0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1afa1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
1afa2 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 the file..*/.st
1afa3 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c atic int memjrnl
1afa4 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 Close(sqlite3_fi
1afa5 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 6d 65 6d le *pJfd){. mem
1afa6 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 70 4a 66 jrnlTruncate(pJf
1afa7 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 d, 0);. return
1afa8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
1afa9 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 *.** Sync the fi
1afaa 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
1afab 74 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 t memjrnlSync(sq
1afac 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1afad 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
1afae 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1afaf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 ;.}../*.** Query
1afb0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1afb1 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a file in bytes..
1afb2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
1afb3 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 mjrnlFileSize(sq
1afb4 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1afb5 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a , sqlite_int64 *
1afb6 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 pSize){. MemJou
1afb7 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f rnal *p = (MemJo
1afb8 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 urnal *)pJfd;.
1afb9 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 *pSize = (sqlite
1afba 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64 70 6f _int64) p->endpo
1afbb 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20 20 72 int.iOffset;. r
1afbc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1afbd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 .}../*.** Table
1afbe 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d of methods for M
1afbf 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65 emJournal sqlite
1afc0 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 3_file object..*
1afc1 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
1afc2 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
1afc3 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 ds MemJournalMet
1afc4 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 hods = {. 1,
1afc5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1afc6 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6d 65 iVersion */. me
1afc7 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 mjrnlClose,
1afc8 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6d /* xClose */. m
1afc9 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 emjrnlRead,
1afca 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6d /* xRead */. m
1afcb 65 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 emjrnlWrite,
1afcc 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 /* xWrite */.
1afcd 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c memjrnlTruncate,
1afce 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a /* xTruncate *
1afcf 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c /. memjrnlSync,
1afd0 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a /* xSync *
1afd1 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 /. memjrnlFileS
1afd2 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 ize, /* xFileSi
1afd3 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 ze */. 0,
1afd4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f /* xLo
1afd5 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 ck */. 0,
1afd6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e /* xUn
1afd7 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 lock */. 0,
1afd8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1afd9 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
1afda 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 k */. 0,
1afdb 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c /* xFil
1afdc 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c eControl */. 0,
1afdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1afde 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a /* xSectorSize *
1afdf 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 /. 0
1afe0 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 /* xDevice
1afe1 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 Characteristics
1afe2 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 */.};../* .** Op
1afe3 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c en a journal fil
1afe4 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1afe5 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1afe6 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 3MemJournalOpen(
1afe7 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1afe8 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 fd){. MemJourna
1afe9 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e l *p = (MemJourn
1afea 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 6d 65 6d al *)pJfd;. mem
1afeb 73 65 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 set(p, 0, sqlite
1afec 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
1afed 29 29 3b 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 ));. p->pMethod
1afee 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 = &MemJournalMe
1afef 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 thods;.}../*.**
1aff0 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
1aff1 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 70 he file-handle p
1aff2 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 assed as an argu
1aff3 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 61 6e 20 69 ment is .** an i
1aff4 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c n-memory journal
1aff5 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
1aff6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
1aff7 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 sMemJournal(sqli
1aff8 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b te3_file *pJfd){
1aff9 0a 20 20 72 65 74 75 72 6e 20 70 4a 66 64 2d 3e . return pJfd->
1affa 70 4d 65 74 68 6f 64 73 3d 3d 26 4d 65 6d 4a 6f pMethods==&MemJo
1affb 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a urnalMethods;.}.
1affc 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ./* .** Return t
1affd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
1affe 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 es required to s
1afff 74 6f 72 65 20 61 20 4d 65 6d 4a 6f 75 72 6e 61 tore a MemJourna
1b000 6c 20 74 68 61 74 20 75 73 65 73 20 76 66 73 0a l that uses vfs.
1b001 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 ** pVfs to creat
1b002 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 e the underlying
1b003 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a on-disk files..
1b004 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1b005 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d E int sqlite3Mem
1b006 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 7b 0a 20 JournalSize(){.
1b007 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 4d return sizeof(M
1b008 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f emJournal);.}../
1b009 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
1b00a 6e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c nd of memjournal
1b00b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1b00c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b00d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1b00e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1b00f 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b 65 72 egin file walker
1b010 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1b011 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b012 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1b013 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 *.** 2008 August
1b014 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 16.**.** The au
1b015 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
1b016 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
1b017 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
1b018 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
1b019 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
1b01a 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
1b01b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
1b01c 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
1b01d 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
1b01e 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
1b01f 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
1b020 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
1b021 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
1b022 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
1b023 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
1b024 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
1b025 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
1b026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b027 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b028 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b029 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b02a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
1b02b 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 contains routine
1b02c 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 s used for walki
1b02d 6e 67 20 74 68 65 20 70 61 72 73 65 72 20 74 72 ng the parser tr
1b02e 65 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c ee for.** an SQL
1b02f 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a statement..**.*
1b030 2a 20 24 49 64 3a 20 77 61 6c 6b 65 72 2e 63 2c * $Id: walker.c,
1b031 76 20 31 2e 31 20 32 30 30 38 2f 30 38 2f 32 30 v 1.1 2008/08/20
1b032 20 31 36 3a 33 35 3a 31 30 20 64 72 68 20 45 78 16:35:10 drh Ex
1b033 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 57 p $.*/.../*.** W
1b034 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f alk an expressio
1b035 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f 6b 65 20 n tree. Invoke
1b036 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 the callback onc
1b037 65 20 66 6f 72 20 65 61 63 68 20 6e 6f 64 65 0a e for each node.
1b038 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 ** of the expres
1b039 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 65 63 65 sion, while dece
1b03a 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f 74 68 65 nding. (In othe
1b03b 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c r words, the cal
1b03c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f lback.** is invo
1b03d 6b 65 64 20 62 65 66 6f 72 65 20 76 69 73 69 74 ked before visit
1b03e 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e 29 0a 2a ing children.).*
1b03f 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 *.** The return
1b040 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 63 value from the c
1b041 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 62 allback should b
1b042 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 57 52 43 e one of the WRC
1b043 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 73 20 _*.** constants
1b044 74 6f 20 73 70 65 63 69 66 79 20 68 6f 77 20 74 to specify how t
1b045 6f 20 70 72 6f 63 65 65 64 20 77 69 74 68 20 74 o proceed with t
1b046 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 20 he walk..**.**
1b047 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 WRC_Continue
1b048 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 64 65 73 Continue des
1b049 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 74 68 65 cending down the
1b04a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 tree..**.**
1b04b 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20 WRC_Prune
1b04c 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65 6e 64 Do not descend
1b04d 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f 64 65 into child node
1b04e 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0a 2a 2a s. But allow.**
1b04f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b050 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b 20 74 the walk t
1b051 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 o continue with
1b052 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e 0a 2a sibling nodes..*
1b053 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 62 6f 72 *.** WRC_Abor
1b054 74 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 20 t Do no
1b055 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b 73 2e 20 more callbacks.
1b056 20 55 6e 77 69 6e 64 20 74 68 65 20 73 74 61 63 Unwind the stac
1b057 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 k and.**
1b058 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
1b059 74 75 72 6e 20 74 68 65 20 74 6f 70 2d 6c 65 76 turn the top-lev
1b05a 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e 0a 2a 2a el walk call..**
1b05b 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 .** The return v
1b05c 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 20 72 alue from this r
1b05d 6f 75 74 69 6e 65 20 69 73 20 57 52 43 5f 41 62 outine is WRC_Ab
1b05e 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74 ort to abandon t
1b05f 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a 2a 20 he tree walk.**
1b060 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 and WRC_Continue
1b061 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a 2a 2f to continue..*/
1b062 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1b063 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 int sqlite3WalkE
1b064 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c xpr(Walker *pWal
1b065 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 ker, Expr *pExpr
1b066 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 ){. int rc;. i
1b067 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 f( pExpr==0 ) re
1b068 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1b069 65 3b 0a 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 e;. rc = pWalke
1b06a 72 2d 3e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b r->xExprCallback
1b06b 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 (pWalker, pExpr)
1b06c 3b 0a 20 20 69 66 28 20 72 63 3d 3d 57 52 43 5f ;. if( rc==WRC_
1b06d 43 6f 6e 74 69 6e 75 65 20 29 7b 0a 20 20 20 20 Continue ){.
1b06e 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 if( sqlite3WalkE
1b06f 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 xpr(pWalker, pEx
1b070 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 pr->pLeft) ) ret
1b071 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1b072 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 if( sqlite3Wa
1b073 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 lkExpr(pWalker,
1b074 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20 29 pExpr->pRight) )
1b075 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1b076 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 t;. if( sqlit
1b077 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 e3WalkExprList(p
1b078 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 Walker, pExpr->p
1b079 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57 List) ) return W
1b07a 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 RC_Abort;. if
1b07b 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c ( sqlite3WalkSel
1b07c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 ect(pWalker, pEx
1b07d 70 72 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a pr->pSelect) ){.
1b07e 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1b07f 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
1b080 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 }. return rc &
1b081 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a WRC_Abort;.}../*
1b082 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 .** Call sqlite3
1b083 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 WalkExpr() for e
1b084 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 very expression
1b085 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 in list p or unt
1b086 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 il.** an abort r
1b087 65 71 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a equest is seen..
1b088 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1b089 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c E int sqlite3Wal
1b08a 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 kExprList(Walker
1b08b 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c *pWalker, ExprL
1b08c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 ist *p){. int i
1b08d 2c 20 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 , rc = WRC_Conti
1b08e 6e 75 65 3b 0a 20 20 73 74 72 75 63 74 20 45 78 nue;. struct Ex
1b08f 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
1b090 65 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 em;. if( p ){.
1b091 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 for(i=p->nExp
1b092 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 r, pItem=p->a; i
1b093 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b >0; i--, pItem++
1b094 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
1b095 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1b096 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 lker, pItem->pEx
1b097 70 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 pr) ) return WRC
1b098 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
1b099 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 }. return rc &
1b09a 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a WRC_Continue;.}.
1b09b 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6c 6c 20 ./*.** Walk all
1b09c 65 78 70 72 65 73 73 69 6f 6e 73 20 61 73 73 6f expressions asso
1b09d 63 69 61 74 65 64 20 77 69 74 68 20 53 45 4c 45 ciated with SELE
1b09e 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 CT statement p.
1b09f 20 44 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 6f 6b Do.** not invok
1b0a0 65 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6c e the SELECT cal
1b0a1 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20 lback on p, but
1b0a2 64 6f 20 28 6f 66 20 63 6f 75 72 73 65 29 20 69 do (of course) i
1b0a3 6e 76 6f 6b 65 0a 2a 2a 20 61 6e 79 20 65 78 70 nvoke.** any exp
1b0a4 72 20 63 61 6c 6c 62 61 63 6b 73 20 61 6e 64 20 r callbacks and
1b0a5 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 73 SELECT callbacks
1b0a6 20 74 68 61 74 20 63 6f 6d 65 20 66 72 6f 6d 20 that come from
1b0a7 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 52 subqueries..** R
1b0a8 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 eturn WRC_Abort
1b0a9 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 2e or WRC_Continue.
1b0aa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b0ab 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 TE int sqlite3Wa
1b0ac 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 57 61 6c lkSelectExpr(Wal
1b0ad 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 ker *pWalker, Se
1b0ae 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 lect *p){. if(
1b0af 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c sqlite3WalkExprL
1b0b0 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e ist(pWalker, p->
1b0b1 70 45 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e pEList) ) return
1b0b2 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 WRC_Abort;. if
1b0b3 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 ( sqlite3WalkExp
1b0b4 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 57 r(pWalker, p->pW
1b0b5 68 65 72 65 29 20 29 20 72 65 74 75 72 6e 20 57 here) ) return W
1b0b6 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 RC_Abort;. if(
1b0b7 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c sqlite3WalkExprL
1b0b8 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e ist(pWalker, p->
1b0b9 70 47 72 6f 75 70 42 79 29 20 29 20 72 65 74 75 pGroupBy) ) retu
1b0ba 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1b0bb 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 if( sqlite3WalkE
1b0bc 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e xpr(pWalker, p->
1b0bd 70 48 61 76 69 6e 67 29 20 29 20 72 65 74 75 72 pHaving) ) retur
1b0be 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 n WRC_Abort;. i
1b0bf 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 f( sqlite3WalkEx
1b0c0 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 prList(pWalker,
1b0c1 70 2d 3e 70 4f 72 64 65 72 42 79 29 20 29 20 72 p->pOrderBy) ) r
1b0c2 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1b0c3 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 . if( sqlite3Wa
1b0c4 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 lkExpr(pWalker,
1b0c5 70 2d 3e 70 4c 69 6d 69 74 29 20 29 20 72 65 74 p->pLimit) ) ret
1b0c6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1b0c7 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1b0c8 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d Expr(pWalker, p-
1b0c9 3e 70 4f 66 66 73 65 74 29 20 29 20 72 65 74 75 >pOffset) ) retu
1b0ca 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1b0cb 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
1b0cc 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 nue;.}../*.** Wa
1b0cd 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 72 65 lk the parse tre
1b0ce 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
1b0cf 74 68 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65 th all subquerie
1b0d0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 46 52 4f 4d s in the.** FROM
1b0d1 20 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 43 clause of SELEC
1b0d2 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 20 T statement p.
1b0d3 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 Do not invoke th
1b0d4 65 20 73 65 6c 65 63 74 0a 2a 2a 20 63 61 6c 6c e select.** call
1b0d5 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20 64 back on p, but d
1b0d6 6f 20 69 6e 76 6f 6b 65 20 69 74 20 6f 6e 20 65 o invoke it on e
1b0d7 61 63 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 ach FROM clause
1b0d8 73 75 62 71 75 65 72 79 0a 2a 2a 20 61 6e 64 20 subquery.** and
1b0d9 6f 6e 20 61 6e 79 20 73 75 62 71 75 65 72 69 65 on any subquerie
1b0da 73 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 69 s further down i
1b0db 6e 20 74 68 65 20 74 72 65 65 2e 20 20 52 65 74 n the tree. Ret
1b0dc 75 72 6e 20 0a 2a 2a 20 57 52 43 5f 41 62 6f 72 urn .** WRC_Abor
1b0dd 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75 t or WRC_Continu
1b0de 65 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e;.*/.SQLITE_PRI
1b0df 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1b0e0 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 WalkSelectFrom(W
1b0e1 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 alker *pWalker,
1b0e2 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 Select *p){. Sr
1b0e3 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 69 cList *pSrc;. i
1b0e4 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53 nt i;. struct S
1b0e5 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 rcList_item *pIt
1b0e6 65 6d 3b 0a 0a 20 20 70 53 72 63 20 3d 20 70 2d em;.. pSrc = p-
1b0e7 3e 70 53 72 63 3b 0a 20 20 69 66 28 20 70 53 72 >pSrc;. if( pSr
1b0e8 63 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 c ){. for(i=p
1b0e9 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d Src->nSrc, pItem
1b0ea 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 =pSrc->a; i>0; i
1b0eb 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 --, pItem++){.
1b0ec 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 if( sqlite3W
1b0ed 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 alkSelect(pWalke
1b0ee 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 r, pItem->pSelec
1b0ef 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 t) ){. re
1b0f0 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1b0f1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1b0f2 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 }. return WRC_C
1b0f3 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a ontinue;.} ../*.
1b0f4 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 ** Call sqlite3W
1b0f5 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 alkExpr() for ev
1b0f6 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 ery expression i
1b0f7 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 n Select stateme
1b0f8 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 nt p..** Invoke
1b0f9 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1b0fa 74 28 29 20 66 6f 72 20 73 75 62 71 75 65 72 69 t() for subqueri
1b0fb 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 es in the FROM c
1b0fc 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 lause and.** on
1b0fd 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c the compound sel
1b0fe 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 ect chain, p->pP
1b0ff 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 rior..**.** Retu
1b100 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 rn WRC_Continue
1b101 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e under normal con
1b102 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e ditions. Return
1b103 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a WRC_Abort if.**
1b104 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f there is an abo
1b105 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a rt request..**.*
1b106 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20 * If the Walker
1b107 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e does not have an
1b108 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b xSelectCallback
1b109 28 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 () then this rou
1b10a 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d tine.** is a no-
1b10b 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43 op returning WRC
1b10c 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 _Continue..*/.SQ
1b10d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1b10e 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
1b10f 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b ct(Walker *pWalk
1b110 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a er, Select *p){.
1b111 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 int rc;. if(
1b112 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d p==0 || pWalker-
1b113 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b >xSelectCallback
1b114 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 ==0 ) return WRC
1b115 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 _Continue;. rc
1b116 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a = WRC_Continue;.
1b117 20 20 77 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 while( p ){.
1b118 20 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d rc = pWalker-
1b119 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b >xSelectCallback
1b11a 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 (pWalker, p);.
1b11b 20 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b if( rc ) break
1b11c 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1b11d 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 3WalkSelectExpr(
1b11e 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 pWalker, p) ) re
1b11f 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1b120 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 if( sqlite3W
1b121 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 alkSelectFrom(pW
1b122 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 alker, p) ) retu
1b123 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1b124 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b p = p->pPrior;
1b125 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1b126 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a & WRC_Abort;.}.
1b127 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1b128 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 End of walker.c
1b129 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1b12a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b12b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1b12c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1b12d 20 42 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f Begin file reso
1b12e 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a lve.c **********
1b12f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1b131 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 ./*.** 2008 Augu
1b132 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 18.**.** The
1b133 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1b134 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1b135 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1b136 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1b137 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1b138 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1b139 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1b13a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1b13b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1b13c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1b13d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1b13e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1b13f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1b140 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1b141 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1b142 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1b143 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1b144 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b145 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b146 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b147 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b148 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
1b149 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f file contains ro
1b14a 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 utines used for
1b14b 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 walking the pars
1b14c 65 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 er tree and.** r
1b14d 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 esolve all ident
1b14e 69 66 69 65 72 73 20 62 79 20 61 73 73 6f 63 69 ifiers by associ
1b14f 61 74 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20 ating them with
1b150 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 a particular.**
1b151 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e table and column
1b152 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 73 ..**.** $Id: res
1b153 6f 6c 76 65 2e 63 2c 76 20 31 2e 31 30 20 32 30 olve.c,v 1.10 20
1b154 30 38 2f 31 30 2f 31 39 20 32 31 3a 30 33 3a 32 08/10/19 21:03:2
1b155 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 7 drh Exp $.*/..
1b156 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 /*.** Turn the p
1b157 45 78 70 72 20 65 78 70 72 65 73 73 69 6f 6e 20 Expr expression
1b158 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f into an alias fo
1b159 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f r the iCol-th co
1b15a 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 lumn of the.** r
1b15b 65 73 75 6c 74 20 73 65 74 20 69 6e 20 70 45 4c esult set in pEL
1b15c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ist..**.** If th
1b15d 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c e result set col
1b15e 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 20 umn is a simple
1b15f 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 column reference
1b160 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
1b161 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 ine.** makes an
1b162 65 78 61 63 74 20 63 6f 70 79 2e 20 20 42 75 74 exact copy. But
1b163 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b for any other k
1b164 69 6e 64 20 6f 66 20 65 78 70 72 65 73 73 69 6f ind of expressio
1b165 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 n, this.** routi
1b166 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f ne make a copy o
1b167 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
1b168 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 column as the a
1b169 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a rgument to the.*
1b16a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 * TK_AS operator
1b16b 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 . The TK_AS ope
1b16c 72 61 74 6f 72 20 63 61 75 73 65 73 20 74 68 65 rator causes the
1b16d 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 expression to b
1b16e 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6a e.** evaluated j
1b16f 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 ust once and the
1b170 6e 20 72 65 75 73 65 64 20 66 6f 72 20 65 61 63 n reused for eac
1b171 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 h alias..**.** T
1b172 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 he reason for su
1b173 70 70 72 65 73 73 69 6e 67 20 74 68 65 20 54 4b ppressing the TK
1b174 5f 41 53 20 74 65 72 6d 20 77 68 65 6e 20 74 68 _AS term when th
1b175 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
1b176 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 a simple.** colu
1b177 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 mn reference is
1b178 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 so that the colu
1b179 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 77 69 6c mn reference wil
1b17a 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 l be recognized
1b17b 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 as.** usable by
1b17c 69 6e 64 69 63 65 73 20 77 69 74 68 69 6e 20 74 indices within t
1b17d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
1b17e 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 processing logic
1b17f 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 . .**.** Hack:
1b180 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 The TK_AS operat
1b181 6f 72 20 69 73 20 69 6e 68 69 62 69 74 65 64 20 or is inhibited
1b182 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 if zType[0]=='G'
1b183 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a . This means.**
1b184 20 74 68 61 74 20 69 6e 20 61 20 47 52 4f 55 50 that in a GROUP
1b185 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 20 BY clause, the
1b186 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76 expression is ev
1b187 61 6c 75 61 74 65 64 20 74 77 69 63 65 2e 20 20 aluated twice.
1b188 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 Hence:.**.**
1b189 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 SELECT random()
1b18a 25 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a %5 AS x, count(*
1b18b 29 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 ) FROM tab GROUP
1b18c 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 BY x.**.** Is e
1b18d 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a quivalent to:.**
1b18e 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72 .** SELECT r
1b18f 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 andom()%5 AS x,
1b190 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 count(*) FROM ta
1b191 62 20 47 52 4f 55 50 20 42 59 20 72 61 6e 64 6f b GROUP BY rando
1b192 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 m()%5.**.** The
1b193 72 65 73 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d result of random
1b194 28 29 25 35 20 69 6e 20 74 68 65 20 47 52 4f 55 ()%5 in the GROU
1b195 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 70 P BY clause is p
1b196 72 6f 62 61 62 6c 79 20 64 69 66 66 65 72 65 6e robably differen
1b197 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 t.** from the re
1b198 73 75 6c 74 20 69 6e 20 74 68 65 20 72 65 73 75 sult in the resu
1b199 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d 69 67 68 lt-set. We migh
1b19a 74 20 66 69 78 20 74 68 69 73 20 73 6f 6d 65 64 t fix this somed
1b19b 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 ay. Or.** then
1b19c 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 again, we might
1b19d 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 not....*/.static
1b19e 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 41 6c 69 void resolveAli
1b19f 61 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 as(. Parse *pPa
1b1a0 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 rse, /*
1b1a1 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
1b1a2 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
1b1a3 45 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 41 EList, /* A
1b1a4 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
1b1a5 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 int iCol,
1b1a6 20 20 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c /* A col
1b1a7 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c umn in the resul
1b1a8 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73 t set. 0..pELis
1b1a9 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20 t->nExpr-1 */.
1b1aa 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 Expr *pExpr,
1b1ab 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 /* Transf
1b1ac 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f 20 61 6e orm this into an
1b1ad 20 61 6c 69 61 73 20 74 6f 20 74 68 65 20 72 65 alias to the re
1b1ae 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f sult set */. co
1b1af 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 nst char *zType
1b1b0 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 50 22 20 /* "GROUP"
1b1b1 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 22 or "ORDER" or ""
1b1b2 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 */.){. Expr *p
1b1b3 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 Orig;
1b1b4 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 68 20 63 /* The iCol-th c
1b1b5 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 olumn of the res
1b1b6 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 ult set */. Exp
1b1b7 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20 r *pDup;
1b1b8 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 /* Copy of p
1b1b9 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 Orig */. sqlite
1b1ba 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 3 *db;
1b1bb 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1b1bc 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a connection */..
1b1bd 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d assert( iCol>=
1b1be 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74 0 && iCol<pEList
1b1bf 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 70 4f 72 ->nExpr );. pOr
1b1c0 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 ig = pEList->a[i
1b1c1 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 Col].pExpr;. as
1b1c2 73 65 72 74 28 20 70 4f 72 69 67 21 3d 30 20 29 sert( pOrig!=0 )
1b1c3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 ;. assert( pOri
1b1c4 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 52 65 g->flags & EP_Re
1b1c5 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 62 20 3d solved );. db =
1b1c6 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 pParse->db;. p
1b1c7 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 Dup = sqlite3Exp
1b1c8 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 29 3b rDup(db, pOrig);
1b1c9 0a 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29 . if( pDup==0 )
1b1ca 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 return;. if( p
1b1cb 44 75 70 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 Dup->op!=TK_COLU
1b1cc 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d 21 3d MN && zType[0]!=
1b1cd 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75 70 20 'G' ){. pDup
1b1ce 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 = sqlite3PExpr(p
1b1cf 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 44 Parse, TK_AS, pD
1b1d0 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 up, 0, 0);. i
1b1d1 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74 f( pDup==0 ) ret
1b1d2 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 4c urn;. if( pEL
1b1d3 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c ist->a[iCol].iAl
1b1d4 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ias==0 ){.
1b1d5 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e pEList->a[iCol].
1b1d6 69 41 6c 69 61 73 20 3d 20 2b 2b 70 50 61 72 73 iAlias = ++pPars
1b1d7 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 20 20 7d e->nAlias;. }
1b1d8 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61 62 6c . pDup->iTabl
1b1d9 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 e = pEList->a[iC
1b1da 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a ol].iAlias;. }.
1b1db 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 if( pExpr->fla
1b1dc 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 gs & EP_ExpColla
1b1dd 74 65 20 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e te ){. pDup->
1b1de 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 pColl = pExpr->p
1b1df 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e Coll;. pDup->
1b1e0 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 flags |= EP_ExpC
1b1e1 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 20 73 71 ollate;. }. sq
1b1e2 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 lite3ExprClear(d
1b1e3 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d b, pExpr);. mem
1b1e4 63 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c cpy(pExpr, pDup,
1b1e5 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29 sizeof(*pExpr))
1b1e6 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1b1e7 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a e(db, pDup);.}..
1b1e8 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 /*.** Given the
1b1e9 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e name of a column
1b1ea 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 of the form X.Y
1b1eb 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 .Z or Y.Z or jus
1b1ec 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 t Z, look up.**
1b1ed 74 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 that name in the
1b1ee 20 73 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74 set of source t
1b1ef 61 62 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73 ables in pSrcLis
1b1f0 74 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 t and make the p
1b1f1 45 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 Expr .** express
1b1f2 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62 ion node refer b
1b1f3 61 63 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72 ack to that sour
1b1f4 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 ce column. The
1b1f5 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 following change
1b1f6 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f s.** are made to
1b1f7 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 pExpr:.**.**
1b1f8 20 70 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20 pExpr->iDb
1b1f9 20 20 20 20 20 20 53 65 74 20 74 68 65 20 69 6e Set the in
1b1fa 64 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d dex in db->aDb[]
1b1fb 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1b1fc 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 X.**
1b1fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 (e
1b1fe 76 65 6e 20 69 66 20 58 20 69 73 20 69 6d 70 6c ven if X is impl
1b1ff 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70 ied)..** pExp
1b200 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20 r->iTable
1b201 20 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73 Set to the curs
1b202 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 or number for th
1b203 65 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 e table obtained
1b204 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1b205 20 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d from
1b206 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 pSrcList..**
1b207 20 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20 pExpr->pTab
1b208 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 Points to
1b209 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 the Table struct
1b20a 75 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e ure of X.Y (even
1b20b 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 if.**
1b20c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58 X
1b20d 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69 6d and/or Y are im
1b20e 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45 plied.).** pE
1b20f 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 xpr->iColumn
1b210 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 6f Set to the co
1b211 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 lumn number with
1b212 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a in the table..**
1b213 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 pExpr->op
1b214 20 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 Set to
1b215 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 TK_COLUMN..**
1b216 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20 20 pExpr->pLeft
1b217 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 Any expres
1b218 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 sion this points
1b219 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a 2a to is deleted.*
1b21a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 * pExpr->pRig
1b21b 68 74 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 ht Any ex
1b21c 70 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f pression this po
1b21d 69 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 ints to is delet
1b21e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 44 ed..**.** The pD
1b21f 62 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 6e 61 bToken is the na
1b220 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1b221 73 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54 se (the "X"). T
1b222 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 his value may be
1b223 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 .** NULL meaning
1b224 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 that name is of
1b225 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 the form Y.Z or
1b226 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62 Z. Any availab
1b227 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 le database.** c
1b228 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65 an be used. The
1b229 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20 pTableToken is
1b22a 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1b22b 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 2e table (the "Y").
1b22c 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20 This.** value
1b22d 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 70 can be NULL if p
1b22e 44 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73 6f 20 DbToken is also
1b22f 4e 55 4c 4c 2e 20 20 49 66 20 70 54 61 62 6c 65 NULL. If pTable
1b230 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 69 74 Token is NULL it
1b231 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74 .** means that t
1b232 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e he form of the n
1b233 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68 61 ame is Z and tha
1b234 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 t columns from a
1b235 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20 ny table.** can
1b236 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 be used..**.** I
1b237 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f f the name canno
1b238 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75 6e t be resolved un
1b239 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61 ambiguously, lea
1b23a 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ve an error mess
1b23b 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 age.** in pParse
1b23c 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d and return non-
1b23d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 zero. Return ze
1b23e 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a ro on success..*
1b23f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f /.static int loo
1b240 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 kupName(. Parse
1b241 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
1b242 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 /* The parsing c
1b243 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 ontext */. Toke
1b244 6e 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20 20 20 n *pDbToken,
1b245 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1b246 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
1b247 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 ing table, or NU
1b248 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 LL */. Token *p
1b249 54 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a 20 TableToken, /*
1b24a 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f Name of table co
1b24b 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c ntaining column,
1b24c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f or NULL */. To
1b24d 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b 65 ken *pColumnToke
1b24e 6e 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 n, /* Name of th
1b24f 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e e column. */. N
1b250 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c ameContext *pNC,
1b251 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 /* The name
1b252 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20 context used to
1b253 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 resolve the name
1b254 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 */. Expr *pExp
1b255 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 r /* Ma
1b256 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64 ke this EXPR nod
1b257 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 e point to the s
1b258 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a elected column *
1b259 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62 /.){. char *zDb
1b25a 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e = 0; /* N
1b25b 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
1b25c 61 73 65 2e 20 20 54 68 65 20 22 58 22 20 69 6e ase. The "X" in
1b25d 20 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72 X.Y.Z */. char
1b25e 20 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20 20 20 *zTab = 0;
1b25f 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1b260 74 61 62 6c 65 2e 20 20 54 68 65 20 22 59 22 20 table. The "Y"
1b261 69 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 in X.Y.Z or Y.Z
1b262 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 */. char *zCol
1b263 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d = 0; /* Nam
1b264 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e e of the column.
1b265 20 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20 20 69 The "Z" */. i
1b266 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 nt i, j;
1b267 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1b268 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e ters */. int cn
1b269 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 t = 0;
1b26a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1b26b 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e umber of matchin
1b26c 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a g column names *
1b26d 2f 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d /. int cntTab =
1b26e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1b26f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1b270 6f 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c of matching tabl
1b271 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c e names */. sql
1b272 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
1b273 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f e->db; /
1b274 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
1b275 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 onnection */. s
1b276 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
1b277 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 em *pItem;
1b278 20 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 /* Use for loop
1b279 69 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 ing over pSrcLis
1b27a 74 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 t items */. str
1b27b 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1b27c 20 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f *pMatch = 0; /
1b27d 2a 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 * The matching p
1b27e 53 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a SrcList item */.
1b27f 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
1b280 54 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 TopNC = pNC;
1b281 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d /* First nam
1b282 65 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 econtext in the
1b283 6c 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 list */. Schema
1b284 20 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 *pSchema = 0;
1b285 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1b286 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 chema of the exp
1b287 72 65 73 73 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 ression */.. as
1b288 73 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54 6f 6b sert( pColumnTok
1b289 65 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b en && pColumnTok
1b28a 65 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68 65 20 en->z ); /* The
1b28b 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f Z in X.Y.Z canno
1b28c 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20 t be NULL */..
1b28d 2f 2a 20 44 65 71 75 6f 74 65 20 61 6e 64 20 7a /* Dequote and z
1b28e 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 74 68 ero-terminate th
1b28f 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 7a 44 62 e names */. zDb
1b290 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
1b291 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 62 54 omToken(db, pDbT
1b292 6f 6b 65 6e 29 3b 0a 20 20 7a 54 61 62 20 3d 20 oken);. zTab =
1b293 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
1b294 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 54 oken(db, pTableT
1b295 6f 6b 65 6e 29 3b 0a 20 20 7a 43 6f 6c 20 3d 20 oken);. zCol =
1b296 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
1b297 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 75 6d 6e oken(db, pColumn
1b298 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 64 62 Token);. if( db
1b299 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1b29a 7b 0a 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 {. goto looku
1b29b 70 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 7d 0a 0a pname_end;. }..
1b29c 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
1b29d 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d the node to no-m
1b29e 61 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d atch */. pExpr-
1b29f 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 >iTable = -1;.
1b2a0 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b pExpr->pTab = 0;
1b2a1 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20 .. /* Start at
1b2a2 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 the inner-most c
1b2a3 6f 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 ontext and move
1b2a4 6f 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20 outward until a
1b2a5 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a match is found *
1b2a6 2f 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 /. while( pNC &
1b2a7 26 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 & cnt==0 ){.
1b2a8 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
1b2a9 3b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 ;. SrcList *p
1b2aa 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 SrcList = pNC->p
1b2ab 53 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 SrcList;.. if
1b2ac 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 ( pSrcList ){.
1b2ad 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 for(i=0, pIt
1b2ae 65 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 em=pSrcList->a;
1b2af 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 i<pSrcList->nSrc
1b2b0 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
1b2b1 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a . Table *
1b2b2 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e pTab;. in
1b2b3 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43 t iDb;. C
1b2b4 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a olumn *pCol;. .
1b2b5 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 pTab = p
1b2b6 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 Item->pTab;.
1b2b7 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 assert( pTab
1b2b8 21 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 !=0 && pTab->zNa
1b2b9 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 me!=0 );.
1b2ba 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
1b2bb 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 hemaToIndex(db,
1b2bc 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
1b2bd 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1b2be 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a pTab->nCol>0 );.
1b2bf 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 if( zTab
1b2c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
1b2c1 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 ( pItem->zAlias
1b2c2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 ){. c
1b2c3 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 har *zTabName =
1b2c4 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 pItem->zAlias;.
1b2c5 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 if( s
1b2c6 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 qlite3StrICmp(zT
1b2c7 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 abName, zTab)!=0
1b2c8 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
1b2c9 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1b2ca 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a char *
1b2cb 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d zTabName = pTab-
1b2cc 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 >zName;.
1b2cd 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65 if( zTabName
1b2ce 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 ==0 || sqlite3St
1b2cf 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 rICmp(zTabName,
1b2d0 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 zTab)!=0 ) conti
1b2d1 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nue;.
1b2d2 20 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 if( zDb!=0 && s
1b2d3 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 qlite3StrICmp(db
1b2d4 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
1b2d5 2c 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 , zDb)!=0 ){.
1b2d6 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 conti
1b2d7 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nue;.
1b2d8 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1b2d9 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1b2da 20 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b if( 0==(cntTab+
1b2db 2b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 +) ){.
1b2dc 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
1b2dd 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a pItem->iCursor;.
1b2de 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1b2df 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 >pTab = pTab;.
1b2e0 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 pSchema
1b2e1 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b = pTab->pSchema;
1b2e2 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 . pMatc
1b2e3 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 h = pItem;.
1b2e4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 }. for
1b2e5 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d (j=0, pCol=pTab-
1b2e6 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e >aCol; j<pTab->n
1b2e7 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b Col; j++, pCol++
1b2e8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
1b2e9 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1b2ea 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f pCol->zName, zCo
1b2eb 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
1b2ec 20 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 IdList *pUs
1b2ed 69 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ing;.
1b2ee 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 cnt++;.
1b2ef 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
1b2f0 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 e = pItem->iCurs
1b2f1 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 or;.
1b2f2 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 pExpr->pTab = pT
1b2f3 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ab;.
1b2f4 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a pMatch = pItem;.
1b2f5 20 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68 pSch
1b2f6 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 ema = pTab->pSch
1b2f7 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ema;.
1b2f8 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 /* Substitute t
1b2f9 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e he rowid (column
1b2fa 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 -1) for the INT
1b2fb 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
1b2fc 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
1b2fd 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d pExpr->iColumn =
1b2fe 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 j==pTab->iPKey
1b2ff 3f 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20 ? -1 : j;.
1b300 20 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63 if( i<pSrc
1b301 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a List->nSrc-1 ){.
1b302 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
1b303 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 ( pItem[1].joint
1b304 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c ype & JT_NATURAL
1b305 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1b306 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d /* If this m
1b307 61 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e atch occurred in
1b308 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 the left table
1b309 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 of a natural joi
1b30a 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n,.
1b30b 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 ** then skip
1b30c 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 the right table
1b30d 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 to avoid a dupli
1b30e 63 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 cate match */.
1b30f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 pI
1b310 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 tem++;.
1b311 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 i++;.
1b312 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 }else
1b313 69 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49 if( (pUsing = pI
1b314 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d tem[1].pUsing)!=
1b315 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1b316 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 /* If this
1b317 6d 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 match occurs on
1b318 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 a column that is
1b319 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c in the USING cl
1b31a 61 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 ause.
1b31b 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 ** of a joi
1b31c 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72 n, skip the sear
1b31d 63 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 ch of the right
1b31e 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 table of the joi
1b31f 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n.
1b320 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 ** to avoid a
1b321 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 duplicate match
1b322 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 there. */.
1b323 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b int k;
1b324 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b325 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 for(k=0; k<pUsi
1b326 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 ng->nId; k++){.
1b327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b328 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
1b329 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d Cmp(pUsing->a[k]
1b32a 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 .zName, zCol)==0
1b32b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1b32c 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b pItem++;
1b32d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b32e 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 i++;.
1b32f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 br
1b330 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 eak;.
1b331 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1b332 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1b333 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1b334 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1b335 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1b336 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1b337 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1b338 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1b339 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 OMIT_TRIGGER.
1b33a 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e /* If we have n
1b33b 6f 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c ot already resol
1b33c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 ved the name, th
1b33d 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a en maybe . **
1b33e 20 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f it is a new.* o
1b33f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 r old.* trigger
1b340 61 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e argument referen
1b341 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 ce. */. if
1b342 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 ( zDb==0 && zTab
1b343 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 !=0 && cnt==0 &&
1b344 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 pParse->trigSta
1b345 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54 ck!=0 ){. T
1b346 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72 riggerStack *pTr
1b347 69 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61 iggerStack = pPa
1b348 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a rse->trigStack;.
1b349 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 Table *pTa
1b34a 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 33 32 b = 0;. u32
1b34b 20 2a 70 69 43 6f 6c 4d 61 73 6b 3b 0a 20 20 20 *piColMask;.
1b34c 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53 if( pTriggerS
1b34d 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 21 3d 20 tack->newIdx !=
1b34e 2d 31 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 -1 && sqlite3Str
1b34f 49 43 6d 70 28 22 6e 65 77 22 2c 20 7a 54 61 62 ICmp("new", zTab
1b350 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 ) == 0 ){.
1b351 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1b352 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d = pTriggerStack-
1b353 3e 6e 65 77 49 64 78 3b 0a 20 20 20 20 20 20 20 >newIdx;.
1b354 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65 assert( pTrigge
1b355 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a rStack->pTab );.
1b356 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 pTab = p
1b357 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 TriggerStack->pT
1b358 61 62 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f ab;. piCo
1b359 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67 lMask = &(pTrigg
1b35a 65 72 53 74 61 63 6b 2d 3e 6e 65 77 43 6f 6c 4d erStack->newColM
1b35b 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ask);. }els
1b35c 65 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74 e if( pTriggerSt
1b35d 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 21 3d 20 2d ack->oldIdx != -
1b35e 31 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 1 && sqlite3StrI
1b35f 43 6d 70 28 22 6f 6c 64 22 2c 20 7a 54 61 62 29 Cmp("old", zTab)
1b360 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
1b361 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 Expr->iTable = p
1b362 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c TriggerStack->ol
1b363 64 49 64 78 3b 0a 20 20 20 20 20 20 20 20 61 73 dIdx;. as
1b364 73 65 72 74 28 20 70 54 72 69 67 67 65 72 53 74 sert( pTriggerSt
1b365 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20 20 ack->pTab );.
1b366 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 72 69 pTab = pTri
1b367 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 3b ggerStack->pTab;
1b368 0a 20 20 20 20 20 20 20 20 70 69 43 6f 6c 4d 61 . piColMa
1b369 73 6b 20 3d 20 26 28 70 54 72 69 67 67 65 72 53 sk = &(pTriggerS
1b36a 74 61 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d 61 73 6b tack->oldColMask
1b36b 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
1b36c 20 20 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20 if( pTab ){ .
1b36d 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b int iCol;
1b36e 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 . Column
1b36f 2a 70 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 *pCol = pTab->aC
1b370 6f 6c 3b 0a 0a 20 20 20 20 20 20 20 20 70 53 63 ol;.. pSc
1b371 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 hema = pTab->pSc
1b372 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e hema;. cn
1b373 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 tTab++;.
1b374 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c for(iCol=0; iCol
1b375 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 < pTab->nCol; i
1b376 43 6f 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 20 7b Col++, pCol++) {
1b377 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 . if( s
1b378 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 qlite3StrICmp(pC
1b379 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 ol->zName, zCol)
1b37a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1b37b 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 cnt++;.
1b37c 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f pExpr->iCo
1b37d 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d 3d 70 54 61 lumn = iCol==pTa
1b37e 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 b->iPKey ? -1 :
1b37f 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 iCol;.
1b380 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 pExpr->pTab =
1b381 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 pTab;.
1b382 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b if( iCol>=0 ){
1b383 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 . t
1b384 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 estcase( iCol==3
1b385 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 1 );.
1b386 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f testcase( iCo
1b387 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 l==32 );.
1b388 20 20 20 20 20 20 20 2a 70 69 43 6f 6c 4d 61 73 *piColMas
1b389 6b 20 7c 3d 20 28 28 75 33 32 29 31 3c 3c 69 43 k |= ((u32)1<<iC
1b38a 6f 6c 29 20 7c 20 28 69 43 6f 6c 3e 3d 33 32 3f ol) | (iCol>=32?
1b38b 30 78 66 66 66 66 66 66 66 66 3a 30 29 3b 0a 20 0xffffffff:0);.
1b38c 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1b38d 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
1b38e 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1b38f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1b390 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 }.#endif /* !d
1b391 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1b392 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a IT_TRIGGER) */..
1b393 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 /*. ** Pe
1b394 72 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 rhaps the name i
1b395 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
1b396 20 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a the ROWID. *
1b397 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 /. if( cnt==0
1b398 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 && cntTab==1 &&
1b399 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 sqlite3IsRowid(
1b39a 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 zCol) ){. c
1b39b 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 nt = 1;. pE
1b39c 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d xpr->iColumn = -
1b39d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 1;. pExpr->
1b39e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 affinity = SQLIT
1b39f 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 E_AFF_INTEGER;.
1b3a0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 }.. /*.
1b3a1 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 ** If the input
1b3a2 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 is of the form
1b3a3 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e Z (not Y.Z or X.
1b3a4 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 Y.Z) then the na
1b3a5 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 me Z. ** migh
1b3a6 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 t refer to an re
1b3a7 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 sult-set alias.
1b3a8 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 This happens, f
1b3a9 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e or example, when
1b3aa 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 . ** we are r
1b3ab 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 esolving names i
1b3ac 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 n the WHERE clau
1b3ad 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 se of the follow
1b3ae 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 ing command:.
1b3af 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 **. ** S
1b3b0 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 ELECT a+b AS x F
1b3b1 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 ROM table WHERE
1b3b2 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 x<10;. **.
1b3b3 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b ** In cases lik
1b3b4 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 e this, replace
1b3b5 70 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 pExpr with a cop
1b3b6 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 y of the express
1b3b7 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 ion that. **
1b3b8 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 forms the result
1b3b9 20 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 set entry ("a+b
1b3ba 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 " in the example
1b3bb 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d ) and return imm
1b3bc 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a ediately.. **
1b3bd 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 Note that the e
1b3be 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 xpression in the
1b3bf 20 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 result set shou
1b3c0 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 ld have already
1b3c1 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f been. ** reso
1b3c2 6c 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 lved by the time
1b3c3 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
1b3c4 65 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 e is resolved..
1b3c5 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e */. if( cn
1b3c6 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 t==0 && (pEList
1b3c7 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d = pNC->pEList)!=
1b3c8 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 0 && zTab==0 ){.
1b3c9 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
1b3ca 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 <pEList->nExpr;
1b3cb 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 j++){. ch
1b3cc 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 ar *zAs = pEList
1b3cd 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 ->a[j].zName;.
1b3ce 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 if( zAs!=0
1b3cf 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1b3d0 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 mp(zAs, zCol)==0
1b3d1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 ){. Ex
1b3d2 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 pr *pOrig;.
1b3d3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
1b3d4 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 pr->pLeft==0 &&
1b3d5 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 pExpr->pRight==0
1b3d6 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 );. as
1b3d7 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 69 sert( pExpr->pLi
1b3d8 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 st==0 );.
1b3d9 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1b3da 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a ->pSelect==0 );.
1b3db 20 20 20 20 20 20 20 20 20 20 70 4f 72 69 67 20 pOrig
1b3dc 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 = pEList->a[j].p
1b3dd 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 Expr;.
1b3de 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 if( !pNC->allowA
1b3df 67 67 20 26 26 20 45 78 70 72 48 61 73 50 72 6f gg && ExprHasPro
1b3e0 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50 5f perty(pOrig, EP_
1b3e1 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 Agg) ){.
1b3e2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1b3e3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 Msg(pParse, "mis
1b3e4 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64 20 61 use of aliased a
1b3e5 67 67 72 65 67 61 74 65 20 25 73 22 2c 20 7a 41 ggregate %s", zA
1b3e6 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 s);.
1b3e7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1b3e8 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 , zCol);.
1b3e9 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 return 2;.
1b3ea 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1b3eb 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 resolveAlia
1b3ec 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 s(pParse, pEList
1b3ed 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 3b , j, pExpr, "");
1b3ee 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d . cnt =
1b3ef 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 1;. pM
1b3f0 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 atch = 0;.
1b3f1 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62 assert( zTab
1b3f2 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b ==0 && zDb==0 );
1b3f3 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1b3f4 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32 lookupname_end_2
1b3f5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1b3f6 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20 } . }..
1b3f7 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 /* Advance to th
1b3f8 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74 e next name cont
1b3f9 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77 ext. The loop w
1b3fa 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69 ill exit when ei
1b3fb 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68 ther. ** we h
1b3fc 61 76 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74 ave a match (cnt
1b3fd 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72 >0) or when we r
1b3fe 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63 un out of name c
1b3ff 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a ontexts.. */.
1b400 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 if( cnt==0 )
1b401 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e {. pNC = pN
1b402 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a C->pNext;. }.
1b403 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 }.. /*. ** I
1b404 66 20 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55 f X and Y are NU
1b405 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 LL (in other wor
1b406 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63 ds if only the c
1b407 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a olumn name Z is.
1b408 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61 ** supplied) a
1b409 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 nd the value of
1b40a 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e Z is enclosed in
1b40b 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20 double-quotes,
1b40c 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61 then. ** Z is a
1b40d 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 string literal
1b40e 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61 if it doesn't ma
1b40f 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e tch any column n
1b410 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20 ames. In that.
1b411 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65 ** case, we nee
1b412 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68 d to return righ
1b413 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d t away and not m
1b414 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 ake any changes
1b415 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20 to. ** pExpr..
1b416 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65 **. ** Because
1b417 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 no reference wa
1b418 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20 s made to outer
1b419 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e contexts, the pN
1b41a 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65 C->nRef. ** fie
1b41b 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e lds are not chan
1b41c 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65 ged in any conte
1b41d 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 xt.. */. if( c
1b41e 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 nt==0 && zTab==0
1b41f 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e && pColumnToken
1b420 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 7b 0a 20 ->z[0]=='"' ){.
1b421 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1b422 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 (db, zCol);.
1b423 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 pExpr->op = TK_S
1b424 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 78 70 72 TRING;. pExpr
1b425 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 ->pTab = 0;.
1b426 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
1b427 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 /*. ** cnt==0
1b428 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61 73 20 means there was
1b429 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e not match. cnt>
1b42a 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 65 1 means there we
1b42b 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d re two or. ** m
1b42c 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20 45 69 ore matches. Ei
1b42d 74 68 65 72 20 77 61 79 2c 20 77 65 20 68 61 76 ther way, we hav
1b42e 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f e an error.. */
1b42f 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 20 29 7b . if( cnt!=1 ){
1b430 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
1b431 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 72 72 20 *zErr;. zErr
1b432 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 = cnt==0 ? "no s
1b433 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 uch column" : "a
1b434 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 mbiguous column
1b435 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 28 20 7a name";. if( z
1b436 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 Db ){. sqli
1b437 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1b438 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 2e 25 se, "%s: %s.%s.%
1b439 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a s", zErr, zDb, z
1b43a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 Tab, zCol);.
1b43b 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 62 20 29 }else if( zTab )
1b43c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1b43d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1b43e 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72 "%s: %s.%s", zEr
1b43f 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a r, zTab, zCol);.
1b440 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1b441 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1b442 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 (pParse, "%s: %s
1b443 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a ", zErr, zCol);.
1b444 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 70 4e 43 }. pTopNC
1b445 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 ->nErr++;. }..
1b446 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 /* If a column
1b447 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e 20 from a table in
1b448 70 53 72 63 4c 69 73 74 20 69 73 20 72 65 66 65 pSrcList is refe
1b449 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 72 65 63 renced, then rec
1b44a 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 20 66 61 ord. ** this fa
1b44b 63 74 20 69 6e 20 74 68 65 20 70 53 72 63 4c 69 ct in the pSrcLi
1b44c 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62 st.a[].colUsed b
1b44d 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20 itmask. Column
1b44e 30 20 63 61 75 73 65 73 0a 20 20 2a 2a 20 62 69 0 causes. ** bi
1b44f 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e 20 20 t 0 to be set.
1b450 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20 62 69 Column 1 sets bi
1b451 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 t 1. And so for
1b452 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 2a 2a th. If the. **
1b453 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 column number i
1b454 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 s greater than t
1b455 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 he number of bit
1b456 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 73 6b s in the bitmask
1b457 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 74 . ** then set t
1b458 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69 he high-order bi
1b459 74 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b t of the bitmask
1b45a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 .. */. if( pEx
1b45b 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 pr->iColumn>=0 &
1b45c 26 20 70 4d 61 74 63 68 21 3d 30 20 29 7b 0a 20 & pMatch!=0 ){.
1b45d 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 int n = pExpr
1b45e 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74 ->iColumn;. t
1b45f 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 estcase( n==size
1b460 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 2d 31 20 of(Bitmask)*8-1
1b461 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d 73 69 );. if( n>=si
1b462 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 20 zeof(Bitmask)*8
1b463 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 73 69 7a ){. n = siz
1b464 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 2d 31 eof(Bitmask)*8-1
1b465 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
1b466 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 72 rt( pMatch->iCur
1b467 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 sor==pExpr->iTab
1b468 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 68 le );. pMatch
1b469 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 ->colUsed |= ((B
1b46a 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20 itmask)1)<<n;.
1b46b 7d 0a 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e }..lookupname_en
1b46c 64 3a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 d:. /* Clean up
1b46d 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f and return. */
1b46e 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1b46f 28 64 62 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c (db, zDb);. sql
1b470 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1b471 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 Tab);. sqlite3E
1b472 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 xprDelete(db, pE
1b473 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 xpr->pLeft);. p
1b474 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b Expr->pLeft = 0;
1b475 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 . sqlite3ExprDe
1b476 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e lete(db, pExpr->
1b477 70 52 69 67 68 74 29 3b 0a 20 20 70 45 78 70 72 pRight);. pExpr
1b478 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 ->pRight = 0;.
1b479 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 pExpr->op = TK_C
1b47a 4f 4c 55 4d 4e 3b 0a 6c 6f 6f 6b 75 70 6e 61 6d OLUMN;.lookupnam
1b47b 65 5f 65 6e 64 5f 32 3a 0a 20 20 73 71 6c 69 74 e_end_2:. sqlit
1b47c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f e3DbFree(db, zCo
1b47d 6c 29 3b 0a 20 20 69 66 28 20 63 6e 74 3d 3d 31 l);. if( cnt==1
1b47e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1b47f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 pNC!=0 );. sq
1b480 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 70 50 lite3AuthRead(pP
1b481 61 72 73 65 2c 20 70 45 78 70 72 2c 20 70 53 63 arse, pExpr, pSc
1b482 68 65 6d 61 2c 20 70 4e 43 2d 3e 70 53 72 63 4c hema, pNC->pSrcL
1b483 69 73 74 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 63 ist);. /* Inc
1b484 72 65 6d 65 6e 74 20 74 68 65 20 6e 52 65 66 20 rement the nRef
1b485 76 61 6c 75 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d value on all nam
1b486 65 20 63 6f 6e 74 65 78 74 73 20 66 72 6f 6d 20 e contexts from
1b487 54 6f 70 4e 43 20 75 70 20 74 6f 0a 20 20 20 20 TopNC up to.
1b488 2a 2a 20 74 68 65 20 70 6f 69 6e 74 20 77 68 65 ** the point whe
1b489 72 65 20 74 68 65 20 6e 61 6d 65 20 6d 61 74 63 re the name matc
1b48a 68 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 hed. */. for(
1b48b 3b 3b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ;;){. asser
1b48c 74 28 20 70 54 6f 70 4e 43 21 3d 30 20 29 3b 0a t( pTopNC!=0 );.
1b48d 20 20 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52 pTopNC->nR
1b48e 65 66 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 ef++;. if(
1b48f 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72 pTopNC==pNC ) br
1b490 65 61 6b 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e eak;. pTopN
1b491 43 20 3d 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78 C = pTopNC->pNex
1b492 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 t;. }. ret
1b493 75 72 6e 20 30 3b 0a 20 20 7d 20 65 6c 73 65 20 urn 0;. } else
1b494 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a {. return 1;.
1b495 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
1b496 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
1b497 6c 62 61 63 6b 20 66 6f 72 20 73 71 6c 69 74 65 lback for sqlite
1b498 33 57 61 6c 6b 45 78 70 72 28 29 2e 0a 2a 2a 0a 3WalkExpr()..**.
1b499 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f ** Resolve symbo
1b49a 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54 lic names into T
1b49b 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f K_COLUMN operato
1b49c 72 73 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 rs for the curre
1b49d 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68 nt.** node in th
1b49e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 e expression tre
1b49f 65 2e 20 20 52 65 74 75 72 6e 20 30 20 74 6f 20 e. Return 0 to
1b4a0 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 73 65 61 continue the sea
1b4a1 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20 rch down.** the
1b4a2 74 72 65 65 20 6f 72 20 32 20 74 6f 20 61 62 6f tree or 2 to abo
1b4a3 72 74 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b rt the tree walk
1b4a4 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1b4a5 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 73 20 65 tine also does e
1b4a6 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 61 6e rror checking an
1b4a7 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f d name resolutio
1b4a8 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f n for.** functio
1b4a9 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 6f 70 n names. The op
1b4aa 65 72 61 74 6f 72 20 66 6f 72 20 61 67 67 72 65 erator for aggre
1b4ab 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 gate functions i
1b4ac 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20 s changed.** to
1b4ad 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e TK_AGG_FUNCTION.
1b4ae 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
1b4af 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 28 57 esolveExprStep(W
1b4b0 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 alker *pWalker,
1b4b1 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 Expr *pExpr){.
1b4b2 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 NameContext *pNC
1b4b3 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 ;. Parse *pPars
1b4b4 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 70 57 61 6c e;.. pNC = pWal
1b4b5 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 61 73 ker->u.pNC;. as
1b4b6 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a sert( pNC!=0 );.
1b4b7 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e pParse = pNC->
1b4b8 70 50 61 72 73 65 3b 0a 20 20 61 73 73 65 72 74 pParse;. assert
1b4b9 28 20 70 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65 ( pParse==pWalke
1b4ba 72 2d 3e 70 50 61 72 73 65 20 29 3b 0a 0a 20 20 r->pParse );..
1b4bb 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 if( ExprHasAnyPr
1b4bc 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1b4bd 5f 52 65 73 6f 6c 76 65 64 29 20 29 20 72 65 74 _Resolved) ) ret
1b4be 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
1b4bf 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 ExprSetProperty
1b4c0 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c (pExpr, EP_Resol
1b4c1 76 65 64 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 ved);.#ifndef ND
1b4c2 45 42 55 47 0a 20 20 69 66 28 20 70 4e 43 2d 3e EBUG. if( pNC->
1b4c3 70 53 72 63 4c 69 73 74 20 26 26 20 70 4e 43 2d pSrcList && pNC-
1b4c4 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f >pSrcList->nAllo
1b4c5 63 3e 30 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 c>0 ){. SrcLi
1b4c6 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 st *pSrcList = p
1b4c7 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 NC->pSrcList;.
1b4c8 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
1b4c9 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70 53 72 (i=0; i<pNC->pSr
1b4ca 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b cList->nSrc; i++
1b4cb 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1b4cc 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e pSrcList->a[i].
1b4cd 69 43 75 72 73 6f 72 3e 3d 30 20 26 26 20 70 53 iCursor>=0 && pS
1b4ce 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 rcList->a[i].iCu
1b4cf 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e 6e 54 61 rsor<pParse->nTa
1b4d0 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 b);. }. }.#e
1b4d1 6e 64 69 66 0a 20 20 73 77 69 74 63 68 28 20 70 ndif. switch( p
1b4d2 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 0a 23 69 66 Expr->op ){..#if
1b4d3 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
1b4d4 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 ENABLE_UPDATE_DE
1b4d5 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 LETE_LIMIT) && !
1b4d6 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1b4d7 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20 MIT_SUBQUERY).
1b4d8 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 61 6c /* The special
1b4d9 20 6f 70 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57 operator TK_ROW
1b4da 20 6d 65 61 6e 73 20 75 73 65 20 74 68 65 20 72 means use the r
1b4db 6f 77 69 64 20 66 6f 72 20 74 68 65 20 66 69 72 owid for the fir
1b4dc 73 74 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e st. ** column
1b4dd 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
1b4de 75 73 65 2e 20 20 54 68 69 73 20 69 73 20 75 73 use. This is us
1b4df 65 64 20 62 79 20 74 68 65 20 4c 49 4d 49 54 20 ed by the LIMIT
1b4e0 61 6e 64 20 4f 52 44 45 52 20 42 59 0a 20 20 20 and ORDER BY.
1b4e1 20 2a 2a 20 63 6c 61 75 73 65 20 70 72 6f 63 65 ** clause proce
1b4e2 73 73 69 6e 67 20 6f 6e 20 55 50 44 41 54 45 20 ssing on UPDATE
1b4e3 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61 74 65 and DELETE state
1b4e4 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 ments.. */.
1b4e5 20 20 63 61 73 65 20 54 4b 5f 52 4f 57 3a 20 7b case TK_ROW: {
1b4e6 0a 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a . SrcList *
1b4e7 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e pSrcList = pNC->
1b4e8 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 pSrcList;.
1b4e9 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
1b4ea 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 tem *pItem;.
1b4eb 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 assert( pSrcLi
1b4ec 73 74 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e st && pSrcList->
1b4ed 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 nSrc==1 );.
1b4ee 20 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 pItem = pSrcLis
1b4ef 74 2d 3e 61 3b 20 0a 20 20 20 20 20 20 70 45 78 t->a; . pEx
1b4f0 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 pr->op = TK_COLU
1b4f1 4d 4e 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d MN;. pExpr-
1b4f2 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 >pTab = pItem->p
1b4f3 54 61 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72 Tab;. pExpr
1b4f4 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d ->iTable = pItem
1b4f5 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 ->iCursor;.
1b4f6 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
1b4f7 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 = -1;. pExp
1b4f8 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 r->affinity = SQ
1b4f9 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
1b4fa 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1b4fb 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 }.#endif /* d
1b4fc 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
1b4fd 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 ABLE_UPDATE_DELE
1b4fe 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 TE_LIMIT) && !de
1b4ff 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1b500 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a T_SUBQUERY) */..
1b501 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64 /* A lone id
1b502 65 6e 74 69 66 69 65 72 20 69 73 20 74 68 65 20 entifier is the
1b503 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e name of a column
1b504 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 .. */. cas
1b505 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20 e TK_ID: {.
1b506 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 lookupName(pPar
1b507 73 65 2c 20 30 2c 20 30 2c 20 26 70 45 78 70 72 se, 0, 0, &pExpr
1b508 2d 3e 74 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70 45 ->token, pNC, pE
1b509 78 70 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 xpr);. retu
1b50a 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
1b50b 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 20 }. . /* A
1b50c 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 63 table name and c
1b50d 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20 20 20 20 olumn name:
1b50e 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20 4f 72 20 ID.ID. ** Or
1b50f 61 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c a database, tabl
1b510 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20 49 e and column: I
1b511 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a 2f 0a 20 D.ID.ID. */.
1b512 20 20 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a 20 case TK_DOT:
1b513 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 {. Token *p
1b514 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 54 6f Column;. To
1b515 6b 65 6e 20 2a 70 54 61 62 6c 65 3b 0a 20 20 20 ken *pTable;.
1b516 20 20 20 54 6f 6b 65 6e 20 2a 70 44 62 3b 0a 20 Token *pDb;.
1b517 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 Expr *pRigh
1b518 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28 t;.. /* if(
1b519 20 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 62 pSrcList==0 ) b
1b51a 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70 reak; */. p
1b51b 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 Right = pExpr->p
1b51c 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 Right;. if(
1b51d 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f pRight->op==TK_
1b51e 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 ID ){. pD
1b51f 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 b = 0;. p
1b520 54 61 62 6c 65 20 3d 20 26 70 45 78 70 72 2d 3e Table = &pExpr->
1b521 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 pLeft->token;.
1b522 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 20 3d 20 pColumn =
1b523 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a &pRight->token;.
1b524 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1b525 20 20 20 20 20 61 73 73 65 72 74 28 20 70 52 69 assert( pRi
1b526 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 ght->op==TK_DOT
1b527 29 3b 0a 20 20 20 20 20 20 20 20 70 44 62 20 3d );. pDb =
1b528 20 26 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e &pExpr->pLeft->
1b529 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 token;. p
1b52a 54 61 62 6c 65 20 3d 20 26 70 52 69 67 68 74 2d Table = &pRight-
1b52b 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 >pLeft->token;.
1b52c 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 20 3d pColumn =
1b52d 20 26 70 52 69 67 68 74 2d 3e 70 52 69 67 68 74 &pRight->pRight
1b52e 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d ->token;. }
1b52f 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d . lookupNam
1b530 65 28 70 50 61 72 73 65 2c 20 70 44 62 2c 20 70 e(pParse, pDb, p
1b531 54 61 62 6c 65 2c 20 70 43 6f 6c 75 6d 6e 2c 20 Table, pColumn,
1b532 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 pNC, pExpr);.
1b533 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
1b534 75 6e 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 une;. }..
1b535 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 6e 63 74 /* Resolve funct
1b536 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 20 2a 2f ion names. */
1b537 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e . case TK_CON
1b538 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 ST_FUNC:. cas
1b539 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b e TK_FUNCTION: {
1b53a 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 . ExprList
1b53b 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e *pList = pExpr->
1b53c 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 pList; /* The
1b53d 20 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a argument list *
1b53e 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 /. int n =
1b53f 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e pList ? pList->n
1b540 45 78 70 72 20 3a 20 30 3b 20 20 2f 2a 20 4e 75 Expr : 0; /* Nu
1b541 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
1b542 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e s */. int n
1b543 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30 3b o_such_func = 0;
1b544 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
1b545 66 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 f no such functi
1b546 6f 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 on exists */.
1b547 20 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d int wrong_num
1b548 5f 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20 2f _args = 0; /
1b549 2a 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67 20 * True if wrong
1b54a 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
1b54b 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 nts */. int
1b54c 20 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20 20 is_agg = 0;
1b54d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
1b54e 20 69 66 20 69 73 20 61 6e 20 61 67 67 72 65 67 if is an aggreg
1b54f 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ate function */.
1b550 20 20 20 20 20 20 69 6e 74 20 61 75 74 68 3b 20 int auth;
1b551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b552 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 /* Authorizati
1b553 6f 6e 20 74 6f 20 75 73 65 20 74 68 65 20 66 75 on to use the fu
1b554 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 nction */.
1b555 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 20 int nId;
1b556 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1b557 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
1b558 65 72 73 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 ers in function
1b559 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f name */. co
1b55a 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 nst char *zId;
1b55b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1b55c 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e 20 function name.
1b55d 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 */. FuncDef
1b55e 20 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 *pDef;
1b55f 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 /* Informat
1b560 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 75 ion about the fu
1b561 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 nction */.
1b562 69 6e 74 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 int enc = ENC(pP
1b563 61 72 73 65 2d 3e 64 62 29 3b 20 20 2f 2a 20 54 arse->db); /* T
1b564 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f he database enco
1b565 64 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 20 20 7a ding */.. z
1b566 49 64 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 Id = (char*)pExp
1b567 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 r->token.z;.
1b568 20 20 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 nId = pExpr->t
1b569 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 oken.n;. pD
1b56a 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 ef = sqlite3Find
1b56b 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d Function(pParse-
1b56c 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e >db, zId, nId, n
1b56d 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 , enc, 0);.
1b56e 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a if( pDef==0 ){.
1b56f 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 pDef = s
1b570 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 qlite3FindFuncti
1b571 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a on(pParse->db, z
1b572 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63 Id, nId, -1, enc
1b573 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 0);. if
1b574 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 ( pDef==0 ){.
1b575 20 20 20 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66 no_such_f
1b576 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 unc = 1;.
1b577 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1b578 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 wrong_num_args
1b579 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
1b57a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1b57b 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 70 44 is_agg = pD
1b57c 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20 ef->xFunc==0;.
1b57d 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1b57e 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
1b57f 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 69 66 IZATION. if
1b580 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 20 20 ( pDef ){.
1b581 20 20 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 auth = sqlite3
1b582 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
1b583 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f , SQLITE_FUNCTIO
1b584 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d N, 0, pDef->zNam
1b585 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 e, 0);. i
1b586 66 28 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f f( auth!=SQLITE_
1b587 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1b588 69 66 28 20 61 75 74 68 3d 3d 53 51 4c 49 54 45 if( auth==SQLITE
1b589 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 _DENY ){.
1b58a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1b58b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f rMsg(pParse, "no
1b58c 74 20 61 75 74 68 6f 72 69 7a 65 64 20 74 6f 20 t authorized to
1b58d 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73 use function: %s
1b58e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
1b58f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b590 20 20 20 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61 pDef->zNa
1b591 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 me);.
1b592 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 pNC->nErr++;.
1b593 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1b594 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 pExpr->op =
1b595 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 TK_NULL;.
1b596 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
1b597 75 6e 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 une;. }.
1b598 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
1b599 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 26 if( is_agg &
1b59a 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 & !pNC->allowAgg
1b59b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1b59c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1b59d 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 se, "misuse of a
1b59e 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
1b59f 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a n %.*s()", nId,z
1b5a0 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 Id);. pNC
1b5a1 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 ->nErr++;.
1b5a2 20 20 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20 is_agg = 0;.
1b5a3 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f }else if( no
1b5a4 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20 _such_func ){.
1b5a5 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1b5a6 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e orMsg(pParse, "n
1b5a7 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a o such function:
1b5a8 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64 %.*s", nId, zId
1b5a9 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e );. pNC->
1b5aa 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 nErr++;. }e
1b5ab 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75 lse if( wrong_nu
1b5ac 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20 m_args ){.
1b5ad 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1b5ae 67 28 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20 g(pParse,"wrong
1b5af 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
1b5b0 6e 74 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 nts to function
1b5b1 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20 %.*s()",.
1b5b2 20 20 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b nId, zId);
1b5b3 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 . pNC->nE
1b5b4 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rr++;. }.
1b5b5 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 if( is_agg )
1b5b6 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d {. pExpr-
1b5b7 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e >op = TK_AGG_FUN
1b5b8 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70 CTION;. p
1b5b9 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a NC->hasAgg = 1;.
1b5ba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1b5bb 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e ( is_agg ) pNC->
1b5bc 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 allowAgg = 0;.
1b5bd 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 sqlite3WalkE
1b5be 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c xprList(pWalker,
1b5bf 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 69 pList);. i
1b5c0 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d f( is_agg ) pNC-
1b5c1 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 >allowAgg = 1;.
1b5c2 20 20 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 /* FIX ME:
1b5c3 20 43 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e Compute pExpr->
1b5c4 61 66 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f affinity based o
1b5c5 6e 20 74 68 65 20 65 78 70 65 63 74 65 64 20 72 n the expected r
1b5c6 65 74 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74 eturn. ** t
1b5c7 79 70 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 ype of the funct
1b5c8 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion . */.
1b5c9 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 return WRC_P
1b5ca 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e rune;. }.#ifn
1b5cb 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1b5cc 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 SUBQUERY. cas
1b5cd 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 e TK_SELECT:.
1b5ce 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a case TK_EXISTS:
1b5cf 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 .#endif. case
1b5d0 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 TK_IN: {.
1b5d1 69 66 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 if( pExpr->pSele
1b5d2 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e ct ){. in
1b5d3 74 20 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 t nRef = pNC->nR
1b5d4 65 66 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ef;.#ifndef SQLI
1b5d5 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 TE_OMIT_CHECK.
1b5d6 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 if( pNC->i
1b5d7 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 sCheck ){.
1b5d8 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1b5d9 4d 73 67 28 70 50 61 72 73 65 2c 22 73 75 62 71 Msg(pParse,"subq
1b5da 75 65 72 69 65 73 20 70 72 6f 68 69 62 69 74 65 ueries prohibite
1b5db 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 d in CHECK const
1b5dc 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 raints");.
1b5dd 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
1b5de 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 sqlite3WalkSe
1b5df 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 lect(pWalker, pE
1b5e0 78 70 72 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 xpr->pSelect);.
1b5e1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1b5e2 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29 NC->nRef>=nRef )
1b5e3 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 52 ;. if( nR
1b5e4 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b ef!=pNC->nRef ){
1b5e5 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53 . ExprS
1b5e6 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 etProperty(pExpr
1b5e7 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 3b , EP_VarSelect);
1b5e8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1b5e9 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1b5ea 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1b5eb 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a LITE_OMIT_CHECK.
1b5ec 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 case TK_VARI
1b5ed 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 ABLE: {. if
1b5ee 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 ( pNC->isCheck )
1b5ef 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1b5f0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1b5f1 2c 22 70 61 72 61 6d 65 74 65 72 73 20 70 72 6f ,"parameters pro
1b5f2 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b hibited in CHECK
1b5f3 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a constraints");.
1b5f4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1b5f5 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 eak;. }.#endi
1b5f6 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 f. }. return (
1b5f7 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 pParse->nErr ||
1b5f8 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
1b5f9 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f ocFailed) ? WRC_
1b5fa 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74 Abort : WRC_Cont
1b5fb 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 inue;.}../*.** p
1b5fc 45 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 EList is a list
1b5fd 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77 of expressions w
1b5fe 68 69 63 68 20 61 72 65 20 72 65 61 6c 6c 79 20 hich are really
1b5ff 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f the result set o
1b600 66 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c 45 43 f the.** a SELEC
1b601 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 45 T statement. pE
1b602 20 69 73 20 61 20 74 65 72 6d 20 69 6e 20 61 6e is a term in an
1b603 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
1b604 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a UP BY clause..**
1b605 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
1b606 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 70 ecks to see if p
1b607 45 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69 64 E is a simple id
1b608 65 6e 74 69 66 69 65 72 20 77 68 69 63 68 20 63 entifier which c
1b609 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 6f orresponds.** to
1b60a 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 20 the AS-name of
1b60b 6f 6e 65 20 6f 66 20 74 68 65 20 74 65 72 6d 73 one of the terms
1b60c 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
1b60d 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 69 74 20 on list. If it
1b60e 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 is,.** this rout
1b60f 69 6e 65 20 72 65 74 75 72 6e 20 61 6e 20 69 6e ine return an in
1b610 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20 teger between 1
1b611 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 and N where N is
1b612 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
1b613 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 45 * elements in pE
1b614 4c 69 73 74 2c 20 63 6f 72 72 65 73 70 6f 6e 64 List, correspond
1b615 69 6e 67 20 74 6f 20 74 68 65 20 6d 61 74 63 68 ing to the match
1b616 69 6e 67 20 65 6e 74 72 79 2e 20 20 49 66 20 74 ing entry. If t
1b617 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61 here is.** no ma
1b618 74 63 68 2c 20 6f 72 20 69 66 20 70 45 20 69 73 tch, or if pE is
1b619 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 69 64 not a simple id
1b61a 65 6e 74 69 66 69 65 72 2c 20 74 68 65 6e 20 74 entifier, then t
1b61b 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 his routine.** r
1b61c 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 eturn 0..**.** p
1b61d 45 4c 69 73 74 20 68 61 73 20 62 65 65 6e 20 72 EList has been r
1b61e 65 73 6f 6c 76 65 64 2e 20 20 70 45 20 68 61 73 esolved. pE has
1b61f 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 not..*/.static
1b620 69 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d int resolveAsNam
1b621 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
1b622 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 se, /* Parsi
1b623 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 ng context for e
1b624 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f rror messages */
1b625 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c . ExprList *pEL
1b626 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ist, /* List of
1b627 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 expressions to
1b628 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a scan */. Expr *
1b629 70 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pE /*
1b62a 45 78 70 72 65 73 73 69 6f 6e 20 77 65 20 61 72 Expression we ar
1b62b 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 74 63 e trying to matc
1b62c 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b h */.){. int i;
1b62d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b62e 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
1b62f 0a 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 . if( pE->op==T
1b630 4b 5f 49 44 20 7c 7c 20 28 70 45 2d 3e 6f 70 3d K_ID || (pE->op=
1b631 3d 54 4b 5f 53 54 52 49 4e 47 20 26 26 20 70 45 =TK_STRING && pE
1b632 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 27 5c ->token.z[0]!='\
1b633 27 27 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 '') ){. sqlit
1b634 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
1b635 3e 64 62 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a >db;. char *z
1b636 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d Col = sqlite3Nam
1b637 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 eFromToken(db, &
1b638 70 45 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 pE->token);.
1b639 69 66 28 20 7a 43 6f 6c 3d 3d 30 20 29 7b 0a 20 if( zCol==0 ){.
1b63a 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a return -1;.
1b63b 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d }. for(i=
1b63c 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 0; i<pEList->nEx
1b63d 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 pr; i++){.
1b63e 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 char *zAs = pELi
1b63f 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a st->a[i].zName;.
1b640 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 if( zAs!=0
1b641 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1b642 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 mp(zAs, zCol)==0
1b643 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1b644 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 te3DbFree(db, zC
1b645 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 ol);. ret
1b646 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d urn i+1;. }
1b647 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1b648 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f e3DbFree(db, zCo
1b649 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e l);. }. return
1b64a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 20 0;.}../*.** pE
1b64b 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1b64c 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 an expression wh
1b64d 69 63 68 20 69 73 20 61 20 73 69 6e 67 6c 65 20 ich is a single
1b64e 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 4f term in the.** O
1b64f 52 44 45 52 20 42 59 20 6f 66 20 61 20 63 6f 6d RDER BY of a com
1b650 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20 20 54 pound SELECT. T
1b651 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 he expression ha
1b652 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e 61 s not been.** na
1b653 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a me resolved..**.
1b654 2a 2a 20 41 74 20 74 68 65 20 70 6f 69 6e 74 20 ** At the point
1b655 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
1b656 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72 65 61 called, we alrea
1b657 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 dy know that the
1b658 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 .** ORDER BY ter
1b659 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 m is not an inte
1b65a 67 65 72 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 ger index into t
1b65b 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 he result set.
1b65c 54 68 61 74 0a 2a 2a 20 63 61 73 65 20 69 73 20 That.** case is
1b65d 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 63 handled by the c
1b65e 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a alling routine..
1b65f 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f **.** Attempt to
1b660 20 6d 61 74 63 68 20 70 45 20 61 67 61 69 6e 73 match pE agains
1b661 74 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c t result set col
1b662 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65 66 74 umns in the left
1b663 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43 54 20 -most.** SELECT
1b664 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 statement. Retu
1b665 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 20 6f rn the index i o
1b666 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 63 f the matching c
1b667 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e 20 olumn,.** as an
1b668 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74 68 indication to th
1b669 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69 74 e caller that it
1b66a 20 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79 20 should sort by
1b66b 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e the i-th column.
1b66c 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 .** The left-mos
1b66d 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20 t column is 1.
1b66e 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1b66f 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
1b670 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d ed is the.** sam
1b671 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 e integer value
1b672 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75 73 that would be us
1b673 65 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74 ed in the SQL st
1b674 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69 63 atement to indic
1b675 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d ate.** the colum
1b676 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 n..**.** If ther
1b677 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 72 e is no match, r
1b678 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e eturn 0. Return
1b679 20 2d 31 20 69 66 20 61 6e 20 65 72 72 6f 72 20 -1 if an error
1b67a 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 occurs..*/.stati
1b67b 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 c int resolveOrd
1b67c 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 erByTermToExprLi
1b67d 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 st(. Parse *pPa
1b67e 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 rse, /* Pars
1b67f 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 ing context for
1b680 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a error messages *
1b681 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c /. Select *pSel
1b682 65 63 74 2c 20 20 20 2f 2a 20 54 68 65 20 53 45 ect, /* The SE
1b683 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 77 LECT statement w
1b684 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 ith the ORDER BY
1b685 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 clause */. Exp
1b686 72 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 r *pE
1b687 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20 /* The specific
1b688 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 2a 2f ORDER BY term */
1b689 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 .){. int i;
1b68a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1b68b 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 counter */. Ex
1b68c 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 prList *pEList;
1b68d 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 /* The columns
1b68e 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
1b68f 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 t */. NameConte
1b690 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d xt nc; /* Nam
1b691 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 72 65 e context for re
1b692 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a 0a 20 solving pE */..
1b693 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b694 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 ExprIsInteger(pE
1b695 2c 20 26 69 29 3d 3d 30 20 29 3b 0a 20 20 70 45 , &i)==0 );. pE
1b696 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e List = pSelect->
1b697 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 pEList;.. /* Re
1b698 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 solve all names
1b699 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 in the ORDER BY
1b69a 74 65 72 6d 20 65 78 70 72 65 73 73 69 6f 6e 0a term expression.
1b69b 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6e */. memset(&n
1b69c 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e 63 29 c, 0, sizeof(nc)
1b69d 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65 20 3d );. nc.pParse =
1b69e 20 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e 70 53 pParse;. nc.pS
1b69f 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 rcList = pSelect
1b6a0 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70 45 4c ->pSrc;. nc.pEL
1b6a1 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 ist = pEList;.
1b6a2 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b nc.allowAgg = 1;
1b6a3 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a . nc.nErr = 0;.
1b6a4 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 if( sqlite3Res
1b6a5 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 6e olveExprNames(&n
1b6a6 63 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 73 71 c, pE) ){. sq
1b6a7 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 lite3ErrorClear(
1b6a8 70 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65 74 pParse);. ret
1b6a9 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 0;. }.. /*
1b6aa 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 Try to match th
1b6ab 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 e ORDER BY expre
1b6ac 73 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e ssion against an
1b6ad 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a expression. **
1b6ae 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1b6af 65 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 31 et. Return an 1
1b6b0 2d 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66 20 -based index of
1b6b1 74 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a the matching. *
1b6b2 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e 74 * result-set ent
1b6b3 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 ry.. */. for(i
1b6b4 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 =0; i<pEList->nE
1b6b5 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 xpr; i++){. i
1b6b6 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f f( sqlite3ExprCo
1b6b7 6d 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b mpare(pEList->a[
1b6b8 69 5d 2e 70 45 78 70 72 2c 20 70 45 29 20 29 7b i].pExpr, pE) ){
1b6b9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b . return i+
1b6ba 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 1;. }. }..
1b6bb 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c 20 /* If no match,
1b6bc 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72 return 0. */. r
1b6bd 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
1b6be 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f 52 * Generate an OR
1b6bf 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 DER BY or GROUP
1b6c0 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d 72 BY term out-of-r
1b6c1 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 ange error..*/.s
1b6c2 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c tatic void resol
1b6c3 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f veOutOfRangeErro
1b6c4 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 r(. Parse *pPar
1b6c5 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 se, /* T
1b6c6 68 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78 74 he error context
1b6c7 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 into which to w
1b6c8 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 20 2a rite the error *
1b6c9 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1b6ca 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f zType, /* "O
1b6cb 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 RDER" or "GROUP"
1b6cc 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 */. int i,
1b6cd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b6ce 54 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61 73 The index (1-bas
1b6cf 65 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d 20 ed) of the term
1b6d0 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a out of range */.
1b6d1 20 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20 20 int mx
1b6d2 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 /* Larg
1b6d3 65 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65 20 est permissible
1b6d4 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29 7b value of i */.){
1b6d5 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d . sqlite3ErrorM
1b6d6 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 sg(pParse, .
1b6d7 22 25 72 20 25 73 20 42 59 20 74 65 72 6d 20 6f "%r %s BY term o
1b6d8 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 ut of range - sh
1b6d9 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22 62 ould be ". "b
1b6da 65 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22 etween 1 and %d"
1b6db 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29 3b , i, zType, mx);
1b6dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a .}../*.** Analyz
1b6dd 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 e the ORDER BY c
1b6de 6c 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70 6f lause in a compo
1b6df 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 und SELECT state
1b6e0 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a 2a ment. Modify.*
1b6e1 2a 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 * each term of t
1b6e2 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
1b6e3 73 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 se is a constant
1b6e4 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e integer between
1b6e5 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65 72 1.** and N wher
1b6e6 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e N is the numbe
1b6e7 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1b6e8 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c the compound SEL
1b6e9 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52 ECT..**.** ORDER
1b6ea 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 BY terms that a
1b6eb 72 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e re already an in
1b6ec 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20 teger between 1
1b6ed 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e 6d and N are.** unm
1b6ee 6f 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52 20 odified. ORDER
1b6ef 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 BY terms that ar
1b6f0 65 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73 69 e integers outsi
1b6f1 64 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66 0a de the range of.
1b6f2 2a 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20 67 ** 1 through N g
1b6f3 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 enerate an error
1b6f4 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d . ORDER BY term
1b6f5 73 20 74 68 61 74 20 61 72 65 20 65 78 70 72 65 s that are expre
1b6f6 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 ssions.** are ma
1b6f7 74 63 68 65 64 20 61 67 61 69 6e 73 74 20 72 65 tched against re
1b6f8 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 sult set express
1b6f9 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 ions of compound
1b6fa 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69 6e SELECT.** begin
1b6fb 6e 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c 65 ning with the le
1b6fc 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 61 ft-most SELECT a
1b6fd 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72 nd working towar
1b6fe 64 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a 20 d the right..**
1b6ff 41 74 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 At the first mat
1b700 63 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42 59 ch, the ORDER BY
1b701 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 expression is t
1b702 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0a ransformed into.
1b703 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 ** the integer c
1b704 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a olumn number..**
1b705 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1b706 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
1b707 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 seen..*/.static
1b708 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f int resolveCompo
1b709 75 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50 61 undOrderBy(. Pa
1b70a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1b70b 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
1b70c 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65 ontext. Leave e
1b70d 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 rror messages he
1b70e 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a re */. Select *
1b70f 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f 2a pSelect /*
1b710 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 The SELECT stat
1b711 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 ement containing
1b712 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a 2f the ORDER BY */
1b713 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 .){. int i;. E
1b714 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 xprList *pOrderB
1b715 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 y;. ExprList *p
1b716 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 EList;. sqlite3
1b717 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 65 *db;. int more
1b718 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f 72 ToDo = 1;.. pOr
1b719 64 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74 2d derBy = pSelect-
1b71a 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28 >pOrderBy;. if(
1b71b 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72 pOrderBy==0 ) r
1b71c 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 eturn 0;. db =
1b71d 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 20 pParse->db;.#if
1b71e 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d SQLITE_MAX_COLUM
1b71f 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 N. if( pOrderBy
1b720 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d ->nExpr>db->aLim
1b721 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1b722 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 COLUMN] ){. s
1b723 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1b724 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 Parse, "too many
1b725 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52 20 terms in ORDER
1b726 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 BY clause");.
1b727 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 return 1;. }.#
1b728 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b endif. for(i=0;
1b729 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 i<pOrderBy->nEx
1b72a 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4f pr; i++){. pO
1b72b 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e rderBy->a[i].don
1b72c 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53 65 e = 0;. }. pSe
1b72d 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b lect->pNext = 0;
1b72e 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 . while( pSelec
1b72f 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 t->pPrior ){.
1b730 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 pSelect->pPrior
1b731 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65 63 ->pNext = pSelec
1b732 74 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d t;. pSelect =
1b733 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 pSelect->pPrior
1b734 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 ;. }. while( p
1b735 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54 6f Select && moreTo
1b736 44 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 Do ){. struct
1b737 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1b738 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 54 pItem;. moreT
1b739 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45 4c oDo = 0;. pEL
1b73a 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 ist = pSelect->p
1b73b 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 EList;. asser
1b73c 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a t( pEList!=0 );.
1b73d 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 for(i=0, pIt
1b73e 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 em=pOrderBy->a;
1b73f 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 i<pOrderBy->nExp
1b740 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 r; i++, pItem++)
1b741 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c {. int iCol
1b742 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78 70 = -1;. Exp
1b743 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20 20 r *pE, *pDup;.
1b744 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 64 if( pItem->d
1b745 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a one ) continue;.
1b746 20 20 20 20 20 20 70 45 20 3d 20 70 49 74 65 6d pE = pItem
1b747 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 ->pExpr;. i
1b748 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 f( sqlite3ExprIs
1b749 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f Integer(pE, &iCo
1b74a 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 l) ){. if
1b74b 28 20 69 43 6f 6c 3c 30 20 7c 7c 20 69 43 6f 6c ( iCol<0 || iCol
1b74c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 >pEList->nExpr )
1b74d 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f {. reso
1b74e 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 lveOutOfRangeErr
1b74f 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45 or(pParse, "ORDE
1b750 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d R", i+1, pEList-
1b751 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 >nExpr);.
1b752 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1b753 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1b754 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c se{. iCol
1b755 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 = resolveAsName
1b756 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c (pParse, pEList,
1b757 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66 pE);. if
1b758 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 ( iCol==0 ){.
1b759 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 pDup = sq
1b75a 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
1b75b 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 20 20 pE);.
1b75c 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( !db->mallocF
1b75d 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 ailed ){.
1b75e 20 20 20 20 20 61 73 73 65 72 74 28 70 44 75 70 assert(pDup
1b75f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
1b760 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 Col = resolveOrd
1b761 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 erByTermToExprLi
1b762 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 st(pParse, pSele
1b763 63 74 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 ct, pDup);.
1b764 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1b765 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
1b766 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 te(db, pDup);.
1b767 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1b768 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 if( iCol<0 ){.
1b769 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 return 1
1b76a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1b76b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43 }. if( iC
1b76c 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ol>0 ){.
1b76d 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d CollSeq *pColl =
1b76e 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 pE->pColl;.
1b76f 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 int flags =
1b770 70 45 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 pE->flags & EP_E
1b771 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 xpCollate;.
1b772 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 sqlite3ExprDe
1b773 6c 65 74 65 28 64 62 2c 20 70 45 29 3b 0a 20 20 lete(db, pE);.
1b774 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 pItem->pEx
1b775 70 72 20 3d 20 70 45 20 3d 20 73 71 6c 69 74 65 pr = pE = sqlite
1b776 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 3Expr(db, TK_INT
1b777 45 47 45 52 2c 20 30 2c 20 30 2c 20 30 29 3b 0a EGER, 0, 0, 0);.
1b778 20 20 20 20 20 20 20 20 69 66 28 20 70 45 3d 3d if( pE==
1b779 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 0 ) return 1;.
1b77a 20 20 20 20 20 20 70 45 2d 3e 70 43 6f 6c 6c 20 pE->pColl
1b77b 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 = pColl;.
1b77c 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 pE->flags |= EP
1b77d 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66 6c 61 67 _IntValue | flag
1b77e 73 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 69 s;. pE->i
1b77f 54 61 62 6c 65 20 3d 20 69 43 6f 6c 3b 0a 20 20 Table = iCol;.
1b780 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f pItem->iCo
1b781 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 l = iCol;.
1b782 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 pItem->done =
1b783 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 1;. }else{.
1b784 20 20 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f moreToDo
1b785 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
1b786 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 }. pSelect
1b787 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 = pSelect->pNext
1b788 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b ;. }. for(i=0;
1b789 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 i<pOrderBy->nEx
1b78a 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 pr; i++){. if
1b78b 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d ( pOrderBy->a[i]
1b78c 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 .done==0 ){.
1b78d 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1b78e 67 28 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52 g(pParse, "%r OR
1b78f 44 45 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73 DER BY term does
1b790 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22 not match any "
1b791 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 63 6f . "co
1b792 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 lumn in the resu
1b793 6c 74 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20 lt set", i+1);.
1b794 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
1b795 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1b796 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 n 0;.}../*.** Ch
1b797 65 63 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 eck every term i
1b798 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f n the ORDER BY o
1b799 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
1b79a 65 20 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a e pOrderBy of.**
1b79b 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 the SELECT stat
1b79c 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 ement pSelect.
1b79d 49 66 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 If any term is r
1b79e 65 66 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a eference to a.**
1b79f 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 result set expr
1b7a0 65 73 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72 ession (as deter
1b7a1 6d 69 6e 65 64 20 62 79 20 74 68 65 20 45 78 70 mined by the Exp
1b7a2 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65 rList.a.iCol fie
1b7a3 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76 ld).** then conv
1b7a4 65 72 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e ert that term in
1b7a5 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 to a copy of the
1b7a6 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 corresponding r
1b7a7 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c esult set.** col
1b7a8 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e umn..**.** If an
1b7a9 79 20 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 y errors are det
1b7aa 65 63 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72 ected, add an er
1b7ab 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 ror message to p
1b7ac 50 61 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74 Parse and.** ret
1b7ad 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 urn non-zero. R
1b7ae 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f eturn zero if no
1b7af 20 65 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e errors are seen
1b7b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1b7b1 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
1b7b2 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
1b7b3 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 By(. Parse *pPa
1b7b4 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 rse, /* P
1b7b5 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 arsing context.
1b7b6 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 Leave error mes
1b7b7 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 sages here */.
1b7b8 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c Select *pSelect,
1b7b9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c /* The SEL
1b7ba 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f ECT statement co
1b7bb 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61 ntaining the cla
1b7bc 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 use */. ExprLis
1b7bd 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f t *pOrderBy, /
1b7be 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f * The ORDER BY o
1b7bf 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
1b7c0 65 20 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65 e to be processe
1b7c1 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 d */. const cha
1b7c2 72 20 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 r *zType /*
1b7c3 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 "ORDER" or "GROU
1b7c4 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 P" */.){. int i
1b7c5 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
1b7c6 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
1b7c7 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
1b7c8 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c ;. struct ExprL
1b7c9 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
1b7ca 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 .. if( pOrderBy
1b7cb 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 ==0 || pParse->d
1b7cc 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1b7cd 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 ) return 0;.#if
1b7ce 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d SQLITE_MAX_COLUM
1b7cf 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 N. if( pOrderBy
1b7d0 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d ->nExpr>db->aLim
1b7d1 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1b7d2 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 COLUMN] ){. s
1b7d3 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1b7d4 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 Parse, "too many
1b7d5 20 74 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20 terms in %s BY
1b7d6 63 6c 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b clause", zType);
1b7d7 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
1b7d8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69 }.#endif. pELi
1b7d9 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 st = pSelect->pE
1b7da 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 List;. assert(
1b7db 70 45 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a pEList!=0 ); /*
1b7dc 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 sqlite3SelectNe
1b7dd 77 28 29 20 67 75 61 72 61 6e 74 65 65 73 20 74 w() guarantees t
1b7de 68 69 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 his */. for(i=0
1b7df 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 , pItem=pOrderBy
1b7e0 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d ->a; i<pOrderBy-
1b7e1 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 >nExpr; i++, pIt
1b7e2 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 em++){. if( p
1b7e3 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 Item->iCol ){.
1b7e4 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 if( pItem->i
1b7e5 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 Col>pEList->nExp
1b7e6 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 r ){. res
1b7e7 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 olveOutOfRangeEr
1b7e8 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 ror(pParse, zTyp
1b7e9 65 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e e, i+1, pEList->
1b7ea 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 nExpr);.
1b7eb 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 return 1;.
1b7ec 7d 0a 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 }. resolveA
1b7ed 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c lias(pParse, pEL
1b7ee 69 73 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c ist, pItem->iCol
1b7ef 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 -1, pItem->pExpr
1b7f0 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a , zType);. }.
1b7f1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
1b7f2 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 }../*.** pOrderB
1b7f3 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 y is an ORDER BY
1b7f4 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 or GROUP BY cla
1b7f5 75 73 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74 use in SELECT st
1b7f6 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e atement pSelect.
1b7f7 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e .** The Name con
1b7f8 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 text of the SELE
1b7f9 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 CT statement is
1b7fa 70 4e 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 pNC. zType is e
1b7fb 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22 ither.** "ORDER"
1b7fc 20 6f 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 or "GROUP" depe
1b7fd 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 nding on which t
1b7fe 79 70 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f ype of clause pO
1b7ff 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a rderBy is..**.**
1b800 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
1b801 73 6f 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d solves each term
1b802 20 6f 66 20 74 68 65 20 63 6c 61 75 73 65 20 69 of the clause i
1b803 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f nto an expressio
1b804 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64 n..** If the ord
1b805 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e er-by term is an
1b806 20 69 6e 74 65 67 65 72 20 49 20 62 65 74 77 65 integer I betwe
1b807 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72 en 1 and N (wher
1b808 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 e N is the.** nu
1b809 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1b80a 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
1b80b 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29 t of the SELECT)
1b80c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 then the expres
1b80d 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 sion.** in the r
1b80e 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63 esolution is a c
1b80f 6f 70 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20 opy of the I-th
1b810 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 result-set expre
1b811 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 ssion. If.** th
1b812 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 e order-by term
1b813 69 73 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74 is an identify t
1b814 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 hat corresponds
1b815 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f to the AS-name o
1b816 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65 f.** a result-se
1b817 74 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 t expression, th
1b818 65 6e 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f en the term reso
1b819 6c 76 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f lves to a copy o
1b81a 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d f the.** result-
1b81b 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 set expression.
1b81c 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
1b81d 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 expression is re
1b81e 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65 solved in.** the
1b81f 20 75 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69 usual way - usi
1b820 6e 67 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 ng sqlite3Resolv
1b821 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a eExprNames()..**
1b822 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1b823 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
1b824 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 ber of errors.
1b825 49 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c If errors occur,
1b826 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72 then.** an appr
1b827 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 opriate error me
1b828 73 73 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c ssage might be l
1b829 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20 eft in pParse.
1b82a 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65 (OOM errors.** e
1b82b 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61 xcepted.).*/.sta
1b82c 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f tic int resolveO
1b82d 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e rderGroupBy(. N
1b82e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c ameContext *pNC,
1b82f 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 /* The name
1b830 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 context of the
1b831 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1b832 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 */. Select *pS
1b833 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 elect, /* T
1b834 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
1b835 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64 ent holding pOrd
1b836 65 72 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 erBy */. ExprLi
1b837 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 st *pOrderBy,
1b838 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f /* An ORDER BY o
1b839 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
1b83a 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a e to resolve */.
1b83b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
1b83c 79 70 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 ype /* Eithe
1b83d 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 r "ORDER" or "GR
1b83e 4f 55 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 OUP", as appropr
1b83f 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 iate */.){. int
1b840 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1b841 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1b842 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
1b843 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 int iCol;
1b844 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b845 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 /* Column number
1b846 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 */. struct Exp
1b847 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
1b848 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f m; /* A term o
1b849 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 f the ORDER BY c
1b84a 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 lause */. Parse
1b84b 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 *pParse;
1b84c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
1b84d 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
1b84e 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20 int nResult;
1b84f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b850 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 /* Number of te
1b851 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c rms in the resul
1b852 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 t set */.. if(
1b853 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 pOrderBy==0 ) re
1b854 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c turn 0;. nResul
1b855 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c t = pSelect->pEL
1b856 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50 ist->nExpr;. pP
1b857 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 arse = pNC->pPar
1b858 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 se;. for(i=0, p
1b859 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 Item=pOrderBy->a
1b85a 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 ; i<pOrderBy->nE
1b85b 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b xpr; i++, pItem+
1b85c 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 +){. Expr *pE
1b85d 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b = pItem->pExpr;
1b85e 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f . iCol = reso
1b85f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 lveAsName(pParse
1b860 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 , pSelect->pELis
1b861 74 2c 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20 t, pE);. if(
1b862 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 iCol<0 ){.
1b863 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a 20 4f 4f return 1; /* OO
1b864 4d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 7d M error */. }
1b865 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 . if( iCol>0
1b866 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 ){. /* If a
1b867 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 20 n AS-name match
1b868 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 74 is found, mark t
1b869 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 6f 6c his ORDER BY col
1b86a 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 20 umn as being.
1b86b 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 ** a copy of
1b86c 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 the iCol-th resu
1b86d 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 20 lt-set column.
1b86e 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 The subsequent c
1b86f 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 all to. **
1b870 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 sqlite3ResolveOr
1b871 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c derGroupBy() wil
1b872 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 l convert the ex
1b873 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 20 pression to a.
1b874 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 ** copy of t
1b875 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c he iCol-th resul
1b876 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
1b877 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d . */. pItem
1b878 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 ->iCol = iCol;.
1b879 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
1b87a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
1b87b 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 ite3ExprIsIntege
1b87c 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a r(pE, &iCol) ){.
1b87d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 /* The ORD
1b87e 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 61 6e ER BY term is an
1b87f 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
1b880 74 2e 20 20 41 67 61 69 6e 2c 20 73 65 74 20 74 t. Again, set t
1b881 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 he column.
1b882 2a 2a 20 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 ** number so tha
1b883 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 t sqlite3Resolve
1b884 4f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 OrderGroupBy() w
1b885 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 0a ill convert the.
1b886 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62 ** order-b
1b887 79 20 74 65 72 6d 20 74 6f 20 61 20 63 6f 70 79 y term to a copy
1b888 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 of the result-s
1b889 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f et expression */
1b88a 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c . if( iCol<
1b88b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 1 ){. res
1b88c 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 olveOutOfRangeEr
1b88d 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 ror(pParse, zTyp
1b88e 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29 e, i+1, nResult)
1b88f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
1b890 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
1b891 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 pItem->iCol =
1b892 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 iCol;. cont
1b893 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 inue;. }..
1b894 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 /* Otherwise, t
1b895 72 65 61 74 20 74 68 65 20 4f 52 44 45 52 20 42 reat the ORDER B
1b896 59 20 74 65 72 6d 20 61 73 20 61 6e 20 6f 72 64 Y term as an ord
1b897 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e inary expression
1b898 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 */. pItem->i
1b899 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 Col = 0;. if(
1b89a 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
1b89b 78 70 72 4e 61 6d 65 73 28 70 4e 43 2c 20 70 45 xprNames(pNC, pE
1b89c 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
1b89d 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
1b89e 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 return sqlite3R
1b89f 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
1b8a0 42 79 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 By(pParse, pSele
1b8a1 63 74 2c 20 70 4f 72 64 65 72 42 79 2c 20 7a 54 ct, pOrderBy, zT
1b8a2 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ype);.}../*.** R
1b8a3 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 esolve names in
1b8a4 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 the SELECT state
1b8a5 6d 65 6e 74 20 70 20 61 6e 64 20 61 6c 6c 20 6f ment p and all o
1b8a6 66 20 69 74 73 20 64 65 73 63 65 6e 64 65 6e 74 f its descendent
1b8a7 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
1b8a8 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 resolveSelectSt
1b8a9 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b ep(Walker *pWalk
1b8aa 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a er, Select *p){.
1b8ab 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
1b8ac 4f 75 74 65 72 4e 43 3b 20 20 2f 2a 20 43 6f 6e OuterNC; /* Con
1b8ad 74 65 78 74 20 74 68 61 74 20 63 6f 6e 74 61 69 text that contai
1b8ae 6e 73 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a ns this SELECT *
1b8af 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 /. NameContext
1b8b0 73 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e sNC; /* N
1b8b1 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 ame context of t
1b8b2 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 his SELECT */.
1b8b3 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64 3b 20 int isCompound;
1b8b4 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1b8b5 69 66 20 70 20 69 73 20 61 20 63 6f 6d 70 6f 75 if p is a compou
1b8b6 6e 64 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20 69 nd select */. i
1b8b7 6e 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 nt nCompound;
1b8b8 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1b8b9 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 74 65 72 of compound ter
1b8ba 6d 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f 20 ms processed so
1b8bb 66 61 72 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a far */. Parse *
1b8bc 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
1b8bd 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
1b8be 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ext */. ExprLis
1b8bf 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 t *pEList;
1b8c0 20 2f 2a 20 52 65 73 75 6c 74 20 73 65 74 20 65 /* Result set e
1b8c1 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 2a xpression list *
1b8c2 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 /. int i;
1b8c3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1b8c4 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
1b8c5 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 ExprList *pGrou
1b8c6 70 42 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 pBy; /* The
1b8c7 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1b8c8 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4c 65 */. Select *pLe
1b8c9 66 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 ftmost; /*
1b8ca 4c 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c Left-most of SEL
1b8cb 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e ECT of a compoun
1b8cc 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a d */. sqlite3 *
1b8cd 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f db; /
1b8ce 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * Database conne
1b8cf 63 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 20 61 ction */. .. a
1b8d0 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1b8d1 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 if( p->selFlags
1b8d2 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29 & SF_Resolved )
1b8d3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 {. return WRC
1b8d4 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 4f _Prune;. }. pO
1b8d5 75 74 65 72 4e 43 20 3d 20 70 57 61 6c 6b 65 72 uterNC = pWalker
1b8d6 2d 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 61 72 73 ->u.pNC;. pPars
1b8d7 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 e = pWalker->pPa
1b8d8 72 73 65 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 rse;. db = pPar
1b8d9 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4e 6f se->db;.. /* No
1b8da 72 6d 61 6c 6c 79 20 73 71 6c 69 74 65 33 53 65 rmally sqlite3Se
1b8db 6c 65 63 74 45 78 70 61 6e 64 28 29 20 77 69 6c lectExpand() wil
1b8dc 6c 20 62 65 20 63 61 6c 6c 65 64 20 66 69 72 73 l be called firs
1b8dd 74 20 61 6e 64 20 77 69 6c 6c 20 68 61 76 65 0a t and will have.
1b8de 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 70 ** already exp
1b8df 61 6e 64 65 64 20 74 68 69 73 20 53 45 4c 45 43 anded this SELEC
1b8e0 54 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 T. However, if
1b8e1 74 68 69 73 20 69 73 20 61 20 73 75 62 71 75 65 this is a subque
1b8e2 72 79 20 77 69 74 68 69 6e 0a 20 20 2a 2a 20 61 ry within. ** a
1b8e3 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 73 71 n expression, sq
1b8e4 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
1b8e5 4e 61 6d 65 73 28 29 20 77 69 6c 6c 20 62 65 20 Names() will be
1b8e6 63 61 6c 6c 65 64 20 77 69 74 68 6f 75 74 20 61 called without a
1b8e7 0a 20 20 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c . ** prior call
1b8e8 20 74 6f 20 73 71 6c 69 74 65 33 53 65 6c 65 63 to sqlite3Selec
1b8e9 74 45 78 70 61 6e 64 28 29 2e 20 20 57 68 65 6e tExpand(). When
1b8ea 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 6c that happens, l
1b8eb 65 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 et. ** sqlite3S
1b8ec 65 6c 65 63 74 50 72 65 70 28 29 20 64 6f 20 61 electPrep() do a
1b8ed 6c 6c 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 ll of the proces
1b8ee 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53 45 sing for this SE
1b8ef 4c 45 43 54 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 LECT.. ** sqlit
1b8f0 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 77 e3SelectPrep() w
1b8f1 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74 68 20 ill invoke both
1b8f2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 sqlite3SelectExp
1b8f3 61 6e 64 28 29 20 61 6e 64 0a 20 20 2a 2a 20 74 and() and. ** t
1b8f4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 his routine in t
1b8f5 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 he correct order
1b8f6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 2d .. */. if( (p-
1b8f7 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 >selFlags & SF_E
1b8f8 78 70 61 6e 64 65 64 29 3d 3d 30 20 29 7b 0a 20 xpanded)==0 ){.
1b8f9 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
1b8fa 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c 20 Prep(pParse, p,
1b8fb 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 72 pOuterNC);. r
1b8fc 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e eturn (pParse->n
1b8fd 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f Err || db->mallo
1b8fe 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f 41 cFailed) ? WRC_A
1b8ff 62 6f 72 74 20 3a 20 57 52 43 5f 50 72 75 6e 65 bort : WRC_Prune
1b900 3b 0a 20 20 7d 0a 0a 20 20 69 73 43 6f 6d 70 6f ;. }.. isCompo
1b901 75 6e 64 20 3d 20 70 2d 3e 70 50 72 69 6f 72 21 und = p->pPrior!
1b902 3d 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 6e 64 20 =0;. nCompound
1b903 3d 20 30 3b 0a 20 20 70 4c 65 66 74 6d 6f 73 74 = 0;. pLeftmost
1b904 20 3d 20 70 3b 0a 20 20 77 68 69 6c 65 28 20 70 = p;. while( p
1b905 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1b906 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 (p->selFlags & S
1b907 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20 29 F_Expanded)!=0 )
1b908 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1b909 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f ->selFlags & SF_
1b90a 52 65 73 6f 6c 76 65 64 29 3d 3d 30 20 29 3b 0a Resolved)==0 );.
1b90b 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 p->selFlags
1b90c 7c 3d 20 53 46 5f 52 65 73 6f 6c 76 65 64 3b 0a |= SF_Resolved;.
1b90d 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 . /* Resolve
1b90e 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 the expressions
1b90f 69 6e 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 in the LIMIT and
1b910 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e OFFSET clauses.
1b911 20 54 68 65 73 65 0a 20 20 20 20 2a 2a 20 61 72 These. ** ar
1b912 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f e not allowed to
1b913 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6e 61 refer to any na
1b914 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 61 6e 20 mes, so pass an
1b915 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 65 78 empty NameContex
1b916 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 t.. */. me
1b917 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 mset(&sNC, 0, si
1b918 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 zeof(sNC));.
1b919 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 sNC.pParse = pPa
1b91a 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c rse;. if( sql
1b91b 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
1b91c 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4c ames(&sNC, p->pL
1b91d 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20 20 20 imit) ||.
1b91e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
1b91f 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 xprNames(&sNC, p
1b920 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a 20 20 ->pOffset) ){.
1b921 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
1b922 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 bort;. }. .
1b923 20 20 20 2f 2a 20 53 65 74 20 75 70 20 74 68 65 /* Set up the
1b924 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74 local name-cont
1b925 65 78 74 20 74 6f 20 70 61 73 73 20 74 6f 20 73 ext to pass to s
1b926 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
1b927 72 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20 20 20 rNames() to.
1b928 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68 65 20 72 ** resolve the r
1b929 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 esult-set expres
1b92a 73 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 20 20 2a sion list.. *
1b92b 2f 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 /. sNC.allowA
1b92c 67 67 20 3d 20 31 3b 0a 20 20 20 20 73 4e 43 2e gg = 1;. sNC.
1b92d 70 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70 53 pSrcList = p->pS
1b92e 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 rc;. sNC.pNex
1b92f 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 t = pOuterNC;.
1b930 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 . /* Resolve
1b931 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72 65 73 names in the res
1b932 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20 20 20 ult set. */.
1b933 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 pEList = p->pELi
1b934 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 st;. assert(
1b935 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 pEList!=0 );.
1b936 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 for(i=0; i<pELi
1b937 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b st->nExpr; i++){
1b938 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 . Expr *pX
1b939 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 = pEList->a[i].p
1b93a 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 Expr;. if(
1b93b 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
1b93c 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 58 prNames(&sNC, pX
1b93d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 ) ){. ret
1b93e 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1b93f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a }. }. .
1b940 20 20 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 /* Recursive
1b941 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 ly resolve names
1b942 20 69 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 in all subqueri
1b943 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f es. */. fo
1b944 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 r(i=0; i<p->pSrc
1b945 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 ->nSrc; i++){.
1b946 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
1b947 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d st_item *pItem =
1b948 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b &p->pSrc->a[i];
1b949 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d . if( pItem
1b94a 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 ->pSelect ){.
1b94b 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1b94c 2a 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 20 3d *zSavedContext =
1b94d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f pParse->zAuthCo
1b94e 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 ntext;. i
1b94f 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 f( pItem->zName
1b950 29 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 ) pParse->zAuthC
1b951 6f 6e 74 65 78 74 20 3d 20 70 49 74 65 6d 2d 3e ontext = pItem->
1b952 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 zName;. s
1b953 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c qlite3ResolveSel
1b954 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c ectNames(pParse,
1b955 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c pItem->pSelect,
1b956 20 26 73 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 &sNC);.
1b957 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e pParse->zAuthCon
1b958 74 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e text = zSavedCon
1b959 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 text;. if
1b95a 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c ( pParse->nErr |
1b95b 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
1b95c 65 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f ed ) return WRC_
1b95d 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 Abort;. }.
1b95e 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 }. . /* I
1b95f 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 f there are no a
1b960 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
1b961 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ns in the result
1b962 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f -set, and no GRO
1b963 55 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78 UP BY . ** ex
1b964 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 pression, do not
1b965 20 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 allow aggregate
1b966 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 s in any of the
1b967 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e other expression
1b968 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 s.. */. as
1b969 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 sert( (p->selFla
1b96a 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 gs & SF_Aggregat
1b96b 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72 e)==0 );. pGr
1b96c 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 oupBy = p->pGrou
1b96d 70 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72 pBy;. if( pGr
1b96e 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 oupBy || sNC.has
1b96f 41 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e Agg ){. p->
1b970 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 selFlags |= SF_A
1b971 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65 ggregate;. }e
1b972 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61 lse{. sNC.a
1b973 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 llowAgg = 0;.
1b974 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 }. . /* If
1b975 61 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 a HAVING clause
1b976 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e is present, then
1b977 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 there must be a
1b978 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
1b979 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1b97a 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 p->pHaving && !
1b97b 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 pGroupBy ){.
1b97c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1b97d 67 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f g(pParse, "a GRO
1b97e 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 UP BY clause is
1b97f 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 required before
1b980 48 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 HAVING");.
1b981 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1b982 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
1b983 2a 20 41 64 64 20 74 68 65 20 65 78 70 72 65 73 * Add the expres
1b984 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 sion list to the
1b985 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 name-context be
1b986 66 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 65 fore parsing the
1b987 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 . ** other ex
1b988 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 pressions in the
1b989 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1b98a 74 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 t. This is so th
1b98b 61 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 at. ** expres
1b98c 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 sions in the WHE
1b98d 52 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29 RE clause (etc.)
1b98e 20 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78 can refer to ex
1b98f 70 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 pressions by.
1b990 20 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74 ** aliases in t
1b991 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 he result set..
1b992 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e **. ** Min
1b993 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 or point: If thi
1b994 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 s is the case, t
1b995 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 hen the expressi
1b996 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a on will be. *
1b997 2a 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 * re-evaluated f
1b998 6f 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 or each referenc
1b999 65 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a e to it.. */.
1b99a 20 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d sNC.pEList =
1b99b 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 p->pEList;.
1b99c 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c if( sqlite3Resol
1b99d 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 veExprNames(&sNC
1b99e 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a , p->pWhere) ||.
1b99f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
1b9a0 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 solveExprNames(&
1b9a1 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 sNC, p->pHaving)
1b9a2 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 . ){. re
1b9a3 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1b9a4 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 }.. /* Th
1b9a5 65 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47 e ORDER BY and G
1b9a6 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 20 ROUP BY clauses
1b9a7 6d 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f may not refer to
1b9a8 20 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a terms in. **
1b9a9 20 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 0a outer queries .
1b9aa 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 */. sNC.p
1b9ab 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e Next = 0;. sN
1b9ac 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a C.allowAgg = 1;.
1b9ad 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 . /* Process
1b9ae 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
1b9af 75 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f use for singleto
1b9b0 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 n SELECT stateme
1b9b1 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 nts.. ** The
1b9b2 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
1b9b3 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 for compounds SE
1b9b4 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 LECT statements
1b9b5 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a is handled. *
1b9b6 2a 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 * below, after a
1b9b7 6c 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ll of the result
1b9b8 2d 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 -sets for all of
1b9b9 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 the elements of
1b9ba 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 . ** the comp
1b9bb 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 ound have been r
1b9bc 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a esolved.. */.
1b9bd 20 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f if( !isCompo
1b9be 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72 und && resolveOr
1b9bf 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c derGroupBy(&sNC,
1b9c0 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c p, p->pOrderBy,
1b9c1 20 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20 "ORDER") ){.
1b9c2 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1b9c3 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ort;. }. i
1b9c4 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1b9c5 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 led ){. ret
1b9c6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1b9c7 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 }. . /* R
1b9c8 65 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50 esolve the GROUP
1b9c9 20 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20 BY clause. At
1b9ca 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d the same time, m
1b9cb 61 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a ake sure . **
1b9cc 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c the GROUP BY cl
1b9cd 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f ause does not co
1b9ce 6e 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 ntain aggregate
1b9cf 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a functions.. *
1b9d0 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 /. if( pGroup
1b9d1 42 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 By ){. stru
1b9d2 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
1b9d3 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20 *pItem;. .
1b9d4 20 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f if( resolveO
1b9d5 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 rderGroupBy(&sNC
1b9d6 2c 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 , p, pGroupBy, "
1b9d7 47 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d GROUP") || db->m
1b9d8 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1b9d9 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 return WR
1b9da 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d C_Abort;. }
1b9db 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 . for(i=0,
1b9dc 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e pItem=pGroupBy->
1b9dd 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e a; i<pGroupBy->n
1b9de 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
1b9df 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 ++){. if(
1b9e0 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1b9e1 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 (pItem->pExpr, E
1b9e2 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 P_Agg) ){.
1b9e3 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1b9e4 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 67 Msg(pParse, "agg
1b9e5 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
1b9e6 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 are not allowed
1b9e7 20 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 in ".
1b9e8 20 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 "the GROUP B
1b9e9 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 Y clause");.
1b9ea 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1b9eb 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 _Abort;.
1b9ec 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1b9ed 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 . /* Advance
1b9ee 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d to the next term
1b9ef 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 of the compound
1b9f0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 . */. p =
1b9f1 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e p->pPrior;. n
1b9f2 43 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a Compound++;. }.
1b9f3 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 . /* Resolve th
1b9f4 65 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20 e ORDER BY on a
1b9f5 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 compound SELECT
1b9f6 61 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20 after all terms
1b9f7 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 of. ** the comp
1b9f8 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 ound have been r
1b9f9 65 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 esolved.. */.
1b9fa 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26 if( isCompound &
1b9fb 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e & resolveCompoun
1b9fc 64 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c dOrderBy(pParse,
1b9fd 20 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20 pLeftmost) ){.
1b9fe 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1b9ff 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ort;. }.. retu
1ba00 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a rn WRC_Prune;.}.
1ba01 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
1ba02 69 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 ine walks an exp
1ba03 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 ression tree and
1ba04 20 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 resolves refere
1ba05 6e 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 nces to.** table
1ba06 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73 columns and res
1ba07 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e ult-set columns.
1ba08 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 At the same ti
1ba09 6d 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 me, do error.**
1ba0a 63 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 checking on func
1ba0b 74 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73 tion usage and s
1ba0c 65 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79 et a flag if any
1ba0d 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
1ba0e 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e ions.** are seen
1ba0f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c ..**.** To resol
1ba10 76 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 ve table columns
1ba11 20 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c references we l
1ba12 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f ook for nodes (o
1ba13 72 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74 r subtrees) of t
1ba14 68 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e he .** form X.Y.
1ba15 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 Z or Y.Z or just
1ba16 20 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 Z where.**.**
1ba17 20 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d X: The nam
1ba18 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e e of a database.
1ba19 20 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72 Ex: "main" or
1ba1a 20 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 "temp" or.**
1ba1b 20 20 20 20 20 20 20 20 74 68 65 20 73 79 6d 62 the symb
1ba1c 6f 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e olic name assign
1ba1d 65 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d ed to an ATTACH-
1ba1e 65 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a ed database..**.
1ba1f 2a 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 65 ** Y: The
1ba20 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 name of a table
1ba21 20 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 in a FROM claus
1ba22 65 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67 e. Or in a trig
1ba23 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ger.**
1ba24 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 one of the spec
1ba25 69 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20 ial names "old"
1ba26 6f 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 or "new"..**.**
1ba27 20 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61 Z: The na
1ba28 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 me of a column i
1ba29 6e 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a n table Y..**.**
1ba2a 20 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65 The node at the
1ba2b 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62 root of the sub
1ba2c 74 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64 tree is modified
1ba2d 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
1ba2e 2a 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20 ** Expr.op
1ba2f 20 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20 Changed to
1ba30 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 TK_COLUMN.**
1ba31 45 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 Expr.pTab P
1ba32 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 oints to the Tab
1ba33 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e le object for X.
1ba34 59 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f Y.** Expr.iCo
1ba35 6c 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d lumn The colum
1ba36 6e 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 n index in X.Y.
1ba37 20 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 -1 for the rowi
1ba38 64 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54 d..** Expr.iT
1ba39 61 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45 able The VDBE
1ba3a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 cursor number f
1ba3b 6f 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 or X.Y.**.**.**
1ba3c 54 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c To resolve resul
1ba3d 74 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 73 t-set references
1ba3e 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65 , look for expre
1ba3f 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74 ssion nodes of t
1ba40 68 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69 he.** form Z (wi
1ba41 74 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72 th no X and Y pr
1ba42 65 66 69 78 29 20 77 68 65 72 65 20 74 68 65 20 efix) where the
1ba43 5a 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 69 Z matches the ri
1ba44 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 ght-hand.** size
1ba45 20 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65 of an AS clause
1ba46 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 in the result-s
1ba47 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20 et of a SELECT.
1ba48 20 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f The Z expressio
1ba49 6e 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64 n.** is replaced
1ba4a 20 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 by a copy of th
1ba4b 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 e left-hand side
1ba4c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 of the result-s
1ba4d 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a et expression..*
1ba4e 2a 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 * Table-name and
1ba4f 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75 function resolu
1ba50 74 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74 tion occurs on t
1ba51 68 65 20 73 75 62 73 74 69 74 75 74 65 64 20 65 he substituted e
1ba52 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 xpression.** tre
1ba53 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c e. For example,
1ba54 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 in:.**.**
1ba55 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c SELECT a+b AS x,
1ba56 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 c+d AS y FROM t
1ba57 31 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a 1 ORDER BY x;.**
1ba58 0a 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d .** The "x" term
1ba59 20 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 79 of the order by
1ba5a 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 is replaced by
1ba5b 22 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a "a+b" to render:
1ba5c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 .**.** SELE
1ba5d 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 CT a+b AS x, c+d
1ba5e 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 AS y FROM t1 OR
1ba5f 44 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a DER BY a+b;.**.*
1ba60 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 * Function calls
1ba61 20 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20 are checked to
1ba62 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
1ba63 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a he function is .
1ba64 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74 ** defined and t
1ba65 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 hat the correct
1ba66 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
1ba67 6e 74 73 20 61 72 65 20 73 70 65 63 69 66 69 65 nts are specifie
1ba68 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e d..** If the fun
1ba69 63 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72 ction is an aggr
1ba6a 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 egate function,
1ba6b 74 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61 then the pNC->ha
1ba6c 73 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61 sAgg is.** set a
1ba6d 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 nd the opcode is
1ba6e 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b changed from TK
1ba6f 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f _FUNCTION to TK_
1ba70 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a AGG_FUNCTION..**
1ba71 20 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f If an expressio
1ba72 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 n contains aggre
1ba73 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 gate functions t
1ba74 68 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a hen the EP_Agg.*
1ba75 2a 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 * property on th
1ba76 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
1ba77 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 set..**.** An er
1ba78 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c ror message is l
1ba79 65 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 66 eft in pParse if
1ba7a 20 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 anything is ami
1ba7b 73 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a ss. The number.
1ba7c 2a 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 20 ** if errors is
1ba7d 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
1ba7e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1ba7f 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
1ba80 70 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 prNames( . Name
1ba81 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 Context *pNC,
1ba82 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 /* Namespace
1ba83 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 to resolve expr
1ba84 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 essions in. */.
1ba85 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 Expr *pExpr
1ba86 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1ba87 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 expression to be
1ba88 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b analyzed. */.){
1ba89 0a 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 . int savedHasA
1ba8a 67 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a gg;. Walker w;.
1ba8b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 . if( pExpr==0
1ba8c 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 ) return 0;.#if
1ba8d 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f SQLITE_MAX_EXPR_
1ba8e 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20 DEPTH>0. {.
1ba8f 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 Parse *pParse =
1ba90 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 pNC->pParse;.
1ba91 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 if( sqlite3Expr
1ba92 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 CheckHeight(pPar
1ba93 73 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 se, pExpr->nHeig
1ba94 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e ht+pNC->pParse->
1ba95 6e 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20 nHeight) ){.
1ba96 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
1ba97 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 }. pParse->nH
1ba98 65 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e eight += pExpr->
1ba99 6e 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e nHeight;. }.#en
1ba9a 64 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 dif. savedHasAg
1ba9b 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b g = pNC->hasAgg;
1ba9c 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d . pNC->hasAgg =
1ba9d 20 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 0;. w.xExprCal
1ba9e 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 lback = resolveE
1ba9f 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 xprStep;. w.xSe
1baa0 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 lectCallback = r
1baa1 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 esolveSelectStep
1baa2 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 ;. w.pParse = p
1baa3 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e NC->pParse;. w.
1baa4 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 u.pNC = pNC;. s
1baa5 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 qlite3WalkExpr(&
1baa6 77 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53 w, pExpr);.#if S
1baa7 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
1baa8 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 EPTH>0. pNC->pP
1baa9 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d arse->nHeight -=
1baaa 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b pExpr->nHeight;
1baab 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e .#endif. if( pN
1baac 43 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 20 20 C->nErr>0 ){.
1baad 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 ExprSetProperty
1baae 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 (pExpr, EP_Error
1baaf 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 43 );. }. if( pNC
1bab0 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20 20 20 20 ->hasAgg ){.
1bab1 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 ExprSetProperty(
1bab2 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29 3b 0a pExpr, EP_Agg);.
1bab3 20 20 7d 65 6c 73 65 20 69 66 28 20 73 61 76 65 }else if( save
1bab4 64 48 61 73 41 67 67 20 29 7b 0a 20 20 20 20 70 dHasAgg ){. p
1bab5 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a NC->hasAgg = 1;.
1bab6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 45 78 70 }. return Exp
1bab7 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
1bab8 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 7d pr, EP_Error);.}
1bab9 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 .../*.** Resolve
1baba 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 61 6c all names in al
1babb 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 l expressions of
1babc 20 61 20 53 45 4c 45 43 54 20 61 6e 64 20 69 6e a SELECT and in
1babd 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e 64 65 6e all.** decenden
1babe 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 ts of the SELECT
1babf 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 6f 6d 70 , including comp
1bac0 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20 70 2d 3e ounds off of p->
1bac1 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75 62 71 75 pPrior,.** subqu
1bac2 65 72 69 65 73 20 69 6e 20 65 78 70 72 65 73 73 eries in express
1bac3 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62 71 75 65 ions, and subque
1bac4 72 69 65 73 20 75 73 65 64 20 61 73 20 46 52 4f ries used as FRO
1bac5 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74 65 72 6d M clause.** term
1bac6 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 73 71 6c s..**.** See sql
1bac7 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
1bac8 61 6d 65 73 28 29 20 66 6f 72 20 61 20 64 65 73 ames() for a des
1bac9 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 cription of the
1baca 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74 72 61 6e kinds of.** tran
1bacb 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74 68 61 74 sformations that
1bacc 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c occur..**.** Al
1bacd 6c 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 l SELECT stateme
1bace 6e 74 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 nts should have
1bacf 62 65 65 6e 20 65 78 70 61 6e 64 65 64 20 75 73 been expanded us
1bad0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 ing.** sqlite3Se
1bad1 6c 65 63 74 45 78 70 61 6e 64 28 29 20 70 72 69 lectExpand() pri
1bad2 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 or to invoking t
1bad3 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a his routine..*/.
1bad4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1bad5 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c oid sqlite3Resol
1bad6 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 0a 20 veSelectNames(.
1bad7 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
1bad8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
1bad9 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f arser context */
1bada 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 . Select *p,
1badb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1badc 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1badd 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a t being coded. *
1bade 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 /. NameContext
1badf 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61 *pOuterNC /* Na
1bae0 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 me context for p
1bae1 61 72 65 6e 74 20 53 45 4c 45 43 54 20 73 74 61 arent SELECT sta
1bae2 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57 tement */.){. W
1bae3 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61 73 73 65 alker w;.. asse
1bae4 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 77 2e rt( p!=0 );. w.
1bae5 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 xExprCallback =
1bae6 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 3b resolveExprStep;
1bae7 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c . w.xSelectCall
1bae8 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 back = resolveSe
1bae9 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 lectStep;. w.pP
1baea 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 arse = pParse;.
1baeb 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f 75 74 65 w.u.pNC = pOute
1baec 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 rNC;. sqlite3Wa
1baed 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 29 3b lkSelect(&w, p);
1baee 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
1baef 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 73 6f 6c *** End of resol
1baf0 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve.c ***********
1baf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1baf2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1baf3 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1baf4 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 65 *** Begin file e
1baf5 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a xpr.c **********
1baf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1baf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1baf8 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
1baf9 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a eptember 15.**.*
1bafa 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1bafb 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1bafc 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1bafd 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1bafe 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1baff 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1bb00 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1bb01 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1bb02 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1bb03 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1bb04 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1bb05 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1bb06 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1bb07 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1bb08 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1bb09 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1bb0a 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1bb0b 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1bb0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bb0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bb0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bb0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1bb10 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1bb11 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 routines used f
1bb12 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 65 78 70 or analyzing exp
1bb13 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 ressions and.**
1bb14 66 6f 72 20 67 65 6e 65 72 61 74 69 6e 67 20 56 for generating V
1bb15 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 65 76 DBE code that ev
1bb16 61 6c 75 61 74 65 73 20 65 78 70 72 65 73 73 69 aluates expressi
1bb17 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a ons in SQLite..*
1bb18 2a 0a 2a 2a 20 24 49 64 3a 20 65 78 70 72 2e 63 *.** $Id: expr.c
1bb19 2c 76 20 31 2e 34 30 31 20 32 30 30 38 2f 31 31 ,v 1.401 2008/11
1bb1a 2f 30 36 20 31 35 3a 33 33 3a 30 34 20 64 72 68 /06 15:33:04 drh
1bb1b 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
1bb1c 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66 66 Return the 'aff
1bb1d 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65 78 inity' of the ex
1bb1e 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 pression pExpr i
1bb1f 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 f any..**.** If
1bb20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d pExpr is a colum
1bb21 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 n, a reference t
1bb22 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 61 o a column via a
1bb23 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a 2a n 'AS' alias,.**
1bb24 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63 74 or a sub-select
1bb25 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61 with a column a
1bb26 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c s the return val
1bb27 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a ue, then the .**
1bb28 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 affinity of tha
1bb29 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 t column is retu
1bb2a 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c rned. Otherwise,
1bb2b 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e 65 0x00 is returne
1bb2c 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 d,.** indicating
1bb2d 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72 no affinity for
1bb2e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e the expression.
1bb2f 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 .**.** i.e. the
1bb30 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 WHERE clause exp
1bb31 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 resssions in the
1bb32 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 following state
1bb33 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 ments all.** hav
1bb34 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a 2a e an affinity:.*
1bb35 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c *.** CREATE TABL
1bb36 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 E t1(a);.** SELE
1bb37 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 CT * FROM t1 WHE
1bb38 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 RE a;.** SELECT
1bb39 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20 57 a AS b FROM t1 W
1bb3a 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 HERE b;.** SELEC
1bb3b 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 T * FROM t1 WHER
1bb3c 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f 6d E (select a from
1bb3d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f t1);.*/.SQLITE_
1bb3e 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c PRIVATE char sql
1bb3f 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 ite3ExprAffinity
1bb40 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 (Expr *pExpr){.
1bb41 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 2d int op = pExpr-
1bb42 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 >op;. if( op==T
1bb43 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 K_SELECT ){.
1bb44 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 return sqlite3Ex
1bb45 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 prAffinity(pExpr
1bb46 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 ->pSelect->pELis
1bb47 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a t->a[0].pExpr);.
1bb48 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
1bb49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69 TE_OMIT_CAST. i
1bb4a 66 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29 f( op==TK_CAST )
1bb4b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c {. return sql
1bb4c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 ite3AffinityType
1bb4d 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b (&pExpr->token);
1bb4e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 . }.#endif. if
1bb4f 28 20 28 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e ( (op==TK_COLUMN
1bb50 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 || op==TK_REGIS
1bb51 54 45 52 29 20 26 26 20 70 45 78 70 72 2d 3e 70 TER) && pExpr->p
1bb52 54 61 62 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a Tab!=0 ){. /*
1bb53 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 op==TK_REGISTER
1bb54 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21 && pExpr->pTab!
1bb55 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 =0 happens when
1bb56 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e pExpr was origin
1bb57 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b ally. ** a TK
1bb58 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 _COLUMN but was
1bb59 70 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 previously evalu
1bb5a 61 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 20 ated and cached
1bb5b 69 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f in a register */
1bb5c 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 45 78 . int j = pEx
1bb5d 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 pr->iColumn;.
1bb5e 20 69 66 28 20 6a 3c 30 20 29 20 72 65 74 75 72 if( j<0 ) retur
1bb5f 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 n SQLITE_AFF_INT
1bb60 45 47 45 52 3b 0a 20 20 20 20 61 73 73 65 72 74 EGER;. assert
1bb61 28 20 70 45 78 70 72 2d 3e 70 54 61 62 20 26 26 ( pExpr->pTab &&
1bb62 20 6a 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e j<pExpr->pTab->
1bb63 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74 75 nCol );. retu
1bb64 72 6e 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e rn pExpr->pTab->
1bb65 61 43 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79 aCol[j].affinity
1bb66 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
1bb67 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a Expr->affinity;.
1bb68 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
1bb69 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
1bb6a 6e 63 65 20 66 6f 72 20 65 78 70 72 65 73 73 69 nce for expressi
1bb6b 6f 6e 20 70 45 78 70 72 20 74 6f 20 62 65 20 74 on pExpr to be t
1bb6c 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 he collating.**
1bb6d 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 62 sequence named b
1bb6e 79 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75 y pToken. Retu
1bb6f 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
1bb70 74 68 65 20 72 65 76 69 73 65 64 20 65 78 70 72 the revised expr
1bb71 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63 ession..** The c
1bb72 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1bb73 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 e is marked as "
1bb74 65 78 70 6c 69 63 69 74 22 20 75 73 69 6e 67 20 explicit" using
1bb75 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 the EP_ExpCollat
1bb76 65 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 e.** flag. An e
1bb77 78 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e xplicit collatin
1bb78 67 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 g sequence will
1bb79 6f 76 65 72 72 69 64 65 20 69 6d 70 6c 69 63 69 override implici
1bb7a 74 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 t.** collating s
1bb7b 65 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c equences..*/.SQL
1bb7c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
1bb7d 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 *sqlite3ExprSet
1bb7e 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 Coll(Parse *pPar
1bb7f 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c se, Expr *pExpr,
1bb80 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 6d Token *pCollNam
1bb81 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c e){. char *zCol
1bb82 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 l = 0;
1bb83 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 /* Dequoted na
1bb84 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 me of collation
1bb85 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f sequence */. Co
1bb86 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 llSeq *pColl;.
1bb87 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1bb88 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c arse->db;. zCol
1bb89 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 l = sqlite3NameF
1bb8a 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f romToken(db, pCo
1bb8b 6c 6c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 llName);. if( p
1bb8c 45 78 70 72 20 26 26 20 7a 43 6f 6c 6c 20 29 7b Expr && zColl ){
1bb8d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c . pColl = sql
1bb8e 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 ite3LocateCollSe
1bb8f 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c q(pParse, zColl,
1bb90 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 70 43 -1);. if( pC
1bb91 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 45 78 oll ){. pEx
1bb92 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c pr->pColl = pCol
1bb93 6c 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e l;. pExpr->
1bb94 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 flags |= EP_ExpC
1bb95 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 ollate;. }.
1bb96 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 }. sqlite3DbFre
1bb97 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 e(db, zColl);.
1bb98 72 65 74 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a return pExpr;.}.
1bb99 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1bb9a 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 e default collat
1bb9b 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 ion sequence for
1bb9c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1bb9d 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20 74 68 65 pExpr. If.** the
1bb9e 72 65 20 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 re is no default
1bb9f 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c collation type,
1bba0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 return 0..*/.SQ
1bba1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c LITE_PRIVATE Col
1bba2 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70 lSeq *sqlite3Exp
1bba3 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a rCollSeq(Parse *
1bba4 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 pParse, Expr *pE
1bba5 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 xpr){. CollSeq
1bba6 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 45 78 *pColl = 0;. Ex
1bba7 70 72 20 2a 70 20 3d 20 70 45 78 70 72 3b 0a 20 pr *p = pExpr;.
1bba8 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 while( p ){.
1bba9 20 69 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43 6f int op;. pCo
1bbaa 6c 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 ll = p->pColl;.
1bbab 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 62 if( pColl ) b
1bbac 72 65 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20 70 reak;. op = p
1bbad 2d 3e 6f 70 3b 0a 20 20 20 20 69 66 28 20 28 6f ->op;. if( (o
1bbae 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 p==TK_COLUMN ||
1bbaf 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 op==TK_REGISTER)
1bbb0 20 26 26 20 70 2d 3e 70 54 61 62 21 3d 30 20 29 && p->pTab!=0 )
1bbb1 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 {. /* op==T
1bbb2 4b 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d K_REGISTER && p-
1bbb3 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 >pTab!=0 happens
1bbb4 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 when pExpr was
1bbb5 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 originally.
1bbb6 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 ** a TK_COLUMN
1bbb7 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 but was previous
1bbb8 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 ly evaluated and
1bbb9 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 cached in a reg
1bbba 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 ister */. c
1bbbb 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c onst char *zColl
1bbbc 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 ;. int j =
1bbbd 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 p->iColumn;.
1bbbe 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 if( j>=0 ){.
1bbbf 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 sqlite3 *d
1bbc0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1bbc1 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 zColl =
1bbc2 70 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d p->pTab->aCol[j]
1bbc3 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 .zColl;.
1bbc4 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 pColl = sqlite3F
1bbc5 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 indCollSeq(db, E
1bbc6 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d NC(db), zColl, -
1bbc7 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 1, 0);. p
1bbc8 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 Expr->pColl = pC
1bbc9 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 oll;. }.
1bbca 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1bbcb 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 43 if( op!=TK_C
1bbcc 41 53 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55 50 AST && op!=TK_UP
1bbcd 4c 55 53 20 29 7b 0a 20 20 20 20 20 20 62 72 65 LUS ){. bre
1bbce 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 ak;. }. p
1bbcf 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a = p->pLeft;. }.
1bbd0 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 68 65 if( sqlite3Che
1bbd1 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 ckCollSeq(pParse
1bbd2 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 , pColl) ){ .
1bbd3 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a pColl = 0;. }.
1bbd4 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a return pColl;.
1bbd5 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 }../*.** pExpr i
1bbd6 73 20 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66 20 s an operand of
1bbd7 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 a comparison ope
1bbd8 72 61 74 6f 72 2e 20 20 61 66 66 32 20 69 73 20 rator. aff2 is
1bbd9 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 the.** type affi
1bbda 6e 69 74 79 20 6f 66 20 74 68 65 20 6f 74 68 65 nity of the othe
1bbdb 72 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73 r operand. This
1bbdc 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
1bbdd 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 the.** type aff
1bbde 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c inity that shoul
1bbdf 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 d be used for th
1bbe0 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 e comparison ope
1bbe1 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rator..*/.SQLITE
1bbe2 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 _PRIVATE char sq
1bbe3 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
1bbe4 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 nity(Expr *pExpr
1bbe5 2c 20 63 68 61 72 20 61 66 66 32 29 7b 0a 20 20 , char aff2){.
1bbe6 63 68 61 72 20 61 66 66 31 20 3d 20 73 71 6c 69 char aff1 = sqli
1bbe7 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 te3ExprAffinity(
1bbe8 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 61 66 pExpr);. if( af
1bbe9 66 31 20 26 26 20 61 66 66 32 20 29 7b 0a 20 20 f1 && aff2 ){.
1bbea 20 20 2f 2a 20 42 6f 74 68 20 73 69 64 65 73 20 /* Both sides
1bbeb 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f of the compariso
1bbec 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 n are columns. I
1bbed 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72 69 f one has numeri
1bbee 63 0a 20 20 20 20 2a 2a 20 61 66 66 69 6e 69 74 c. ** affinit
1bbef 79 2c 20 75 73 65 20 74 68 61 74 2e 20 4f 74 68 y, use that. Oth
1bbf0 65 72 77 69 73 65 20 75 73 65 20 6e 6f 20 61 66 erwise use no af
1bbf1 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 finity.. */.
1bbf2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 if( sqlite3Is
1bbf3 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 NumericAffinity(
1bbf4 61 66 66 31 29 20 7c 7c 20 73 71 6c 69 74 65 33 aff1) || sqlite3
1bbf5 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 IsNumericAffinit
1bbf6 79 28 61 66 66 32 29 20 29 7b 0a 20 20 20 20 20 y(aff2) ){.
1bbf7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 return SQLITE_A
1bbf8 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 FF_NUMERIC;.
1bbf9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 }else{. ret
1bbfa 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e urn SQLITE_AFF_N
1bbfb 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c ONE;. }. }el
1bbfc 73 65 20 69 66 28 20 21 61 66 66 31 20 26 26 20 se if( !aff1 &&
1bbfd 21 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 !aff2 ){. /*
1bbfe 4e 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 Neither side of
1bbff 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 the comparison i
1bc00 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d s a column. Com
1bc01 70 61 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 pare the. **
1bc02 72 65 73 75 6c 74 73 20 64 69 72 65 63 74 6c 79 results directly
1bc03 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 .. */. ret
1bc04 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e urn SQLITE_AFF_N
1bc05 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ONE;. }else{.
1bc06 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69 73 /* One side is
1bc07 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f a column, the o
1bc08 74 68 65 72 20 69 73 20 6e 6f 74 2e 20 55 73 65 ther is not. Use
1bc09 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66 66 the columns aff
1bc0a 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61 73 inity. */. as
1bc0b 73 65 72 74 28 20 61 66 66 31 3d 3d 30 20 7c 7c sert( aff1==0 ||
1bc0c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20 20 aff2==0 );.
1bc0d 72 65 74 75 72 6e 20 28 61 66 66 31 20 2b 20 61 return (aff1 + a
1bc0e 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ff2);. }.}../*.
1bc0f 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 6f ** pExpr is a co
1bc10 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f mparison operato
1bc11 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 r. Return the t
1bc12 79 70 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 ype affinity tha
1bc13 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 t should.** be a
1bc14 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 6f pplied to both o
1bc15 70 65 72 61 6e 64 73 20 70 72 69 6f 72 20 74 6f perands prior to
1bc16 20 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 61 doing the compa
1bc17 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 rison..*/.static
1bc18 20 63 68 61 72 20 63 6f 6d 70 61 72 69 73 6f 6e char comparison
1bc19 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 Affinity(Expr *p
1bc1a 45 78 70 72 29 7b 0a 20 20 63 68 61 72 20 61 66 Expr){. char af
1bc1b 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 f;. assert( pEx
1bc1c 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c pr->op==TK_EQ ||
1bc1d 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 pExpr->op==TK_I
1bc1e 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d N || pExpr->op==
1bc1f 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20 20 TK_LT ||.
1bc20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b pExpr->op==TK
1bc21 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 _GT || pExpr->op
1bc22 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70 72 ==TK_GE || pExpr
1bc23 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 ->op==TK_LE ||.
1bc24 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1bc25 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 61 op==TK_NE );. a
1bc26 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c ssert( pExpr->pL
1bc27 65 66 74 20 29 3b 0a 20 20 61 66 66 20 3d 20 73 eft );. aff = s
1bc28 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 qlite3ExprAffini
1bc29 74 79 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 ty(pExpr->pLeft)
1bc2a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 ;. if( pExpr->p
1bc2b 52 69 67 68 74 20 29 7b 0a 20 20 20 20 61 66 66 Right ){. aff
1bc2c 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 = sqlite3Compar
1bc2d 65 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d eAffinity(pExpr-
1bc2e 3e 70 52 69 67 68 74 2c 20 61 66 66 29 3b 0a 20 >pRight, aff);.
1bc2f 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 70 45 }. else if( pE
1bc30 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a xpr->pSelect ){.
1bc31 20 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 aff = sqlite
1bc32 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 3CompareAffinity
1bc33 28 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 2d (pExpr->pSelect-
1bc34 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 >pEList->a[0].pE
1bc35 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 0a 20 xpr, aff);. }.
1bc36 20 65 6c 73 65 20 69 66 28 20 21 61 66 66 20 29 else if( !aff )
1bc37 7b 0a 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 {. aff = SQLI
1bc38 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d TE_AFF_NONE;. }
1bc39 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d . return aff;.}
1bc3a 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 ../*.** pExpr is
1bc3b 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 a comparison ex
1bc3c 70 72 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d pression, eg. '=
1bc3d 27 2c 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 ', '<', IN(...)
1bc3e 65 74 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 etc..** idx_affi
1bc3f 6e 69 74 79 20 69 73 20 74 68 65 20 61 66 66 69 nity is the affi
1bc40 6e 69 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 nity of an index
1bc41 65 64 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 ed column. Retur
1bc42 6e 20 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 n true.** if the
1bc43 20 69 6e 64 65 78 20 77 69 74 68 20 61 66 66 69 index with affi
1bc44 6e 69 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74 nity idx_affinit
1bc45 79 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f y may be used to
1bc46 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 implement.** th
1bc47 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 e comparison in
1bc48 70 45 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pExpr..*/.SQLITE
1bc49 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1bc4a 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 ite3IndexAffinit
1bc4b 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c yOk(Expr *pExpr,
1bc4c 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 char idx_affini
1bc4d 74 79 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20 ty){. char aff
1bc4e 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 = comparisonAffi
1bc4f 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73 nity(pExpr);. s
1bc50 77 69 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20 witch( aff ){.
1bc51 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 case SQLITE_AF
1bc52 46 5f 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 F_NONE:. re
1bc53 74 75 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 turn 1;. case
1bc54 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
1bc55 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 :. return i
1bc56 64 78 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c dx_affinity==SQL
1bc57 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 ITE_AFF_TEXT;.
1bc58 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 default:.
1bc59 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 return sqlite3I
1bc5a 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 sNumericAffinity
1bc5b 28 69 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a (idx_affinity);.
1bc5c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
1bc5d 75 72 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65 urn the P5 value
1bc5e 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
1bc5f 75 73 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72 used for a binar
1bc60 79 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 y comparison.**
1bc61 6f 70 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f opcode (OP_Eq, O
1bc62 50 5f 47 65 20 65 74 63 2e 29 20 75 73 65 64 20 P_Ge etc.) used
1bc63 74 6f 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72 to compare pExpr
1bc64 31 20 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 1 and pExpr2..*/
1bc65 0a 73 74 61 74 69 63 20 75 38 20 62 69 6e 61 72 .static u8 binar
1bc66 79 43 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20 yCompareP5(Expr
1bc67 2a 70 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70 *pExpr1, Expr *p
1bc68 45 78 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 Expr2, int jumpI
1bc69 66 4e 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 fNull){. u8 aff
1bc6a 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 = (char)sqlite3
1bc6b 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 ExprAffinity(pEx
1bc6c 70 72 32 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 pr2);. aff = sq
1bc6d 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
1bc6e 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66 66 nity(pExpr1, aff
1bc6f 29 20 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a ) | jumpIfNull;.
1bc70 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a return aff;.}.
1bc71 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
1bc72 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 pointer to the c
1bc73 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
1bc74 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 e that should be
1bc75 20 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 used by.** a bi
1bc76 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 nary comparison
1bc77 6f 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 operator compari
1bc78 6e 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 ng pLeft and pRi
1bc79 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ght..**.** If th
1bc7a 65 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 e left hand expr
1bc7b 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c ession has a col
1bc7c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
1bc7d 74 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 type, then it is
1bc7e 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 .** used. Otherw
1bc7f 69 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f ise the collatio
1bc80 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 n sequence for t
1bc81 68 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 he right hand ex
1bc82 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 pression.** is u
1bc83 73 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 sed, or the defa
1bc84 75 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 ult (BINARY) if
1bc85 6e 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69 neither expressi
1bc86 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 on has a collati
1bc87 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a ng.** type..**.*
1bc88 2a 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 * Argument pRigh
1bc89 74 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 t (but not pLeft
1bc8a 29 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 ) may be a null
1bc8b 70 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 pointer. In this
1bc8c 20 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 case,.** it is
1bc8d 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a not considered..
1bc8e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1bc8f 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 E CollSeq *sqlit
1bc90 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 e3BinaryCompareC
1bc91 6f 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 ollSeq(. Parse
1bc92 2a 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 *pParse, . Expr
1bc93 20 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 *pLeft, . Expr
1bc94 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f *pRight.){. Co
1bc95 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 llSeq *pColl;.
1bc96 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b assert( pLeft );
1bc97 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c . if( pLeft->fl
1bc98 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c ags & EP_ExpColl
1bc99 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ate ){. asser
1bc9a 74 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 t( pLeft->pColl
1bc9b 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 );. pColl = p
1bc9c 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d Left->pColl;. }
1bc9d 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 20 else if( pRight
1bc9e 26 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 && pRight->flags
1bc9f 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 & EP_ExpCollate
1bca0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1bca1 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b pRight->pColl );
1bca2 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 . pColl = pRi
1bca3 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 ght->pColl;. }e
1bca4 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d lse{. pColl =
1bca5 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c sqlite3ExprColl
1bca6 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 Seq(pParse, pLef
1bca7 74 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f t);. if( !pCo
1bca8 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c ll ){. pCol
1bca9 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 l = sqlite3ExprC
1bcaa 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 ollSeq(pParse, p
1bcab 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 Right);. }.
1bcac 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c }. return pColl
1bcad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 ;.}../*.** Gener
1bcae 61 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73 ate the operands
1bcaf 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f for a compariso
1bcb0 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 n operation. Be
1bcb1 66 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 fore.** generati
1bcb2 6e 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 ng the code for
1bcb3 65 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 each operand, se
1bcb4 74 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a t the EP_AnyAff.
1bcb5 2a 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 ** flag on the e
1bcb6 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 xpression so tha
1bcb7 74 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c t it will be abl
1bcb8 65 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 e to used a.** c
1bcb9 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c ached column val
1bcba 75 65 20 74 68 61 74 20 68 61 73 20 70 72 65 76 ue that has prev
1bcbb 69 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 iously undergone
1bcbc 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 an.** affinity
1bcbd 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 change..*/.stati
1bcbe 63 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 c void codeCompa
1bcbf 72 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 reOperands(. Pa
1bcc0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1bcc1 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 /* Parsing and c
1bcc2 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 ode generating c
1bcc3 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
1bcc4 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a *pLeft, /*
1bcc5 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e The left operan
1bcc6 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 d */. int *pReg
1bcc7 4c 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 Left, /* Regi
1bcc8 73 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 20 ster where left
1bcc9 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65 operand is store
1bcca 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 d */. int *pFre
1bccb 65 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 eLeft, /* Free
1bccc 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 77 this register w
1bccd 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 hen done */. Ex
1bcce 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 pr *pRight,
1bccf 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 /* The right ope
1bcd0 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 rand */. int *p
1bcd1 52 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 RegRight, /* R
1bcd2 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 69 egister where ri
1bcd3 67 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 ght operand is s
1bcd4 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a tored */. int *
1bcd5 70 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20 pFreeRight /*
1bcd6 57 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 73 Write temp regis
1bcd7 74 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70 ter for right op
1bcd8 65 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 erand there */.)
1bcd9 7b 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 {. while( pLeft
1bcda 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 ->op==TK_UPLUS )
1bcdb 20 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e pLeft = pLeft->
1bcdc 70 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e pLeft;. pLeft->
1bcdd 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 flags |= EP_AnyA
1bcde 66 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 ff;. *pRegLeft
1bcdf 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
1bce0 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c eTemp(pParse, pL
1bce1 65 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b eft, pFreeLeft);
1bce2 0a 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 74 . while( pRight
1bce3 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 ->op==TK_UPLUS )
1bce4 20 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 pRight = pRight
1bce5 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 ->pLeft;. pRigh
1bce6 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 t->flags |= EP_A
1bce7 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 nyAff;. *pRegRi
1bce8 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ght = sqlite3Exp
1bce9 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
1bcea 2c 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 52 , pRight, pFreeR
1bceb 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ight);.}../*.**
1bcec 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f Generate code fo
1bced 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f r a comparison o
1bcee 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 perator..*/.stat
1bcef 69 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 ic int codeCompa
1bcf0 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 re(. Parse *pPa
1bcf1 72 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 rse, /* The p
1bcf2 61 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 arsing (and code
1bcf3 20 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e generating) con
1bcf4 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a text */. Expr *
1bcf5 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 pLeft, /* T
1bcf6 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 he left operand
1bcf7 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 */. Expr *pRigh
1bcf8 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 t, /* The ri
1bcf9 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 ght operand */.
1bcfa 20 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 int opcode,
1bcfb 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 /* The compar
1bcfc 69 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 ison opcode */.
1bcfd 20 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e int in1, int in
1bcfe 32 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 2, /* Register h
1bcff 6f 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 olding operands
1bd00 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20 */. int dest,
1bd01 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 /* Jump h
1bd02 65 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f ere if true. */
1bd03 0a 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c . int jumpIfNul
1bd04 6c 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c l /* If true,
1bd05 20 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20 jump if either
1bd06 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 operand is NULL
1bd07 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a */.){. int p5;.
1bd08 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f int addr;. Co
1bd09 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 llSeq *p4;.. p4
1bd0a 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 = sqlite3Binary
1bd0b 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 CompareCollSeq(p
1bd0c 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 Parse, pLeft, pR
1bd0d 69 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 ight);. p5 = bi
1bd0e 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c naryCompareP5(pL
1bd0f 65 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d eft, pRight, jum
1bd10 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 pIfNull);. addr
1bd11 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
1bd12 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64 dOp4(pParse->pVd
1bd13 62 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c be, opcode, in2,
1bd14 20 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 dest, in1,.
1bd15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bd16 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34 (void*)p4
1bd17 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 , P4_COLLSEQ);.
1bd18 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1bd19 67 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64 geP5(pParse->pVd
1bd1a 62 65 2c 20 70 35 29 3b 0a 20 20 69 66 28 20 28 be, p5);. if( (
1bd1b 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f p5 & SQLITE_AFF_
1bd1c 4d 41 53 4b 29 21 3d 53 51 4c 49 54 45 5f 41 46 MASK)!=SQLITE_AF
1bd1d 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 73 71 F_NONE ){. sq
1bd1e 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 lite3ExprCacheAf
1bd1f 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 finityChange(pPa
1bd20 72 73 65 2c 20 69 6e 31 2c 20 31 29 3b 0a 20 20 rse, in1, 1);.
1bd21 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
1bd22 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
1bd23 28 70 50 61 72 73 65 2c 20 69 6e 32 2c 20 31 29 (pParse, in2, 1)
1bd24 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 ;. }. return a
1bd25 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 ddr;.}..#if SQLI
1bd26 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 TE_MAX_EXPR_DEPT
1bd27 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 H>0./*.** Check
1bd28 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20 6e 48 that argument nH
1bd29 65 69 67 68 74 20 69 73 20 6c 65 73 73 20 74 68 eight is less th
1bd2a 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
1bd2b 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 65 78 he maximum.** ex
1bd2c 70 72 65 73 73 69 6f 6e 20 64 65 70 74 68 20 61 pression depth a
1bd2d 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74 20 69 73 llowed. If it is
1bd2e 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61 6e 20 65 not, leave an e
1bd2f 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 0a rror message in.
1bd30 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 ** pParse..*/.SQ
1bd31 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1bd32 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 sqlite3ExprChec
1bd33 6b 48 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 kHeight(Parse *p
1bd34 50 61 72 73 65 2c 20 69 6e 74 20 6e 48 65 69 67 Parse, int nHeig
1bd35 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ht){. int rc =
1bd36 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
1bd37 20 6d 78 48 65 69 67 68 74 20 3d 20 70 50 61 72 mxHeight = pPar
1bd38 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 se->db->aLimit[S
1bd39 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 QLITE_LIMIT_EXPR
1bd3a 5f 44 45 50 54 48 5d 3b 0a 20 20 69 66 28 20 6e _DEPTH];. if( n
1bd3b 48 65 69 67 68 74 3e 6d 78 48 65 69 67 68 74 20 Height>mxHeight
1bd3c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1bd3d 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a rorMsg(pParse, .
1bd3e 20 20 20 20 20 20 20 22 45 78 70 72 65 73 73 69 "Expressi
1bd3f 6f 6e 20 74 72 65 65 20 69 73 20 74 6f 6f 20 6c on tree is too l
1bd40 61 72 67 65 20 28 6d 61 78 69 6d 75 6d 20 64 65 arge (maximum de
1bd41 70 74 68 20 25 64 29 22 2c 20 6d 78 48 65 69 67 pth %d)", mxHeig
1bd42 68 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 63 ht. );. rc
1bd43 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
1bd44 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1bd45 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c ;.}../* The foll
1bd46 6f 77 69 6e 67 20 74 68 72 65 65 20 66 75 6e 63 owing three func
1bd47 74 69 6f 6e 73 2c 20 68 65 69 67 68 74 4f 66 45 tions, heightOfE
1bd48 78 70 72 28 29 2c 20 68 65 69 67 68 74 4f 66 45 xpr(), heightOfE
1bd49 78 70 72 4c 69 73 74 28 29 0a 2a 2a 20 61 6e 64 xprList().** and
1bd4a 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 heightOfSelect(
1bd4b 29 2c 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 ), are used to d
1bd4c 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 61 78 etermine the max
1bd4d 69 6d 75 6d 20 68 65 69 67 68 74 0a 2a 2a 20 6f imum height.** o
1bd4e 66 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e f any expression
1bd4f 20 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 tree referenced
1bd50 20 62 79 20 74 68 65 20 73 74 72 75 63 74 75 72 by the structur
1bd51 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 0a e passed as the.
1bd52 2a 2a 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e ** first argumen
1bd53 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 t..**.** If this
1bd54 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 maximum height
1bd55 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1bd56 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 the current valu
1bd57 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 e pointed.** to
1bd58 62 79 20 70 6e 48 65 69 67 68 74 2c 20 74 68 65 by pnHeight, the
1bd59 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
1bd5a 72 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 6e 48 r, then set *pnH
1bd5b 65 69 67 68 74 20 74 6f 20 74 68 61 74 0a 2a 2a eight to that.**
1bd5c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 value..*/.stati
1bd5d 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 c void heightOfE
1bd5e 78 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 xpr(Expr *p, int
1bd5f 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 *pnHeight){. i
1bd60 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 f( p ){. if(
1bd61 70 2d 3e 6e 48 65 69 67 68 74 3e 2a 70 6e 48 65 p->nHeight>*pnHe
1bd62 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 2a 70 ight ){. *p
1bd63 6e 48 65 69 67 68 74 20 3d 20 70 2d 3e 6e 48 65 nHeight = p->nHe
1bd64 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ight;. }. }.
1bd65 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 }.static void he
1bd66 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 45 ightOfExprList(E
1bd67 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 xprList *p, int
1bd68 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 *pnHeight){. if
1bd69 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 ( p ){. int i
1bd6a 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1bd6b 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b <p->nExpr; i++){
1bd6c 0a 20 20 20 20 20 20 68 65 69 67 68 74 4f 66 45 . heightOfE
1bd6d 78 70 72 28 70 2d 3e 61 5b 69 5d 2e 70 45 78 70 xpr(p->a[i].pExp
1bd6e 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 r, pnHeight);.
1bd6f 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 }. }.}.static
1bd70 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 53 65 void heightOfSe
1bd71 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 lect(Select *p,
1bd72 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a int *pnHeight){.
1bd73 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 68 if( p ){. h
1bd74 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 eightOfExpr(p->p
1bd75 57 68 65 72 65 2c 20 70 6e 48 65 69 67 68 74 29 Where, pnHeight)
1bd76 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 ;. heightOfEx
1bd77 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20 70 pr(p->pHaving, p
1bd78 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
1bd79 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c ightOfExpr(p->pL
1bd7a 69 6d 69 74 2c 20 70 6e 48 65 69 67 68 74 29 3b imit, pnHeight);
1bd7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 . heightOfExp
1bd7c 72 28 70 2d 3e 70 4f 66 66 73 65 74 2c 20 70 6e r(p->pOffset, pn
1bd7d 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 Height);. hei
1bd7e 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d ghtOfExprList(p-
1bd7f 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65 69 67 68 >pEList, pnHeigh
1bd80 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 t);. heightOf
1bd81 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f ExprList(p->pGro
1bd82 75 70 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b upBy, pnHeight);
1bd83 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 . heightOfExp
1bd84 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 rList(p->pOrderB
1bd85 79 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 y, pnHeight);.
1bd86 20 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 heightOfSelect
1bd87 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70 6e 48 65 (p->pPrior, pnHe
1bd88 69 67 68 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ight);. }.}../*
1bd89 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 .** Set the Expr
1bd8a 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c .nHeight variabl
1bd8b 65 20 69 6e 20 74 68 65 20 73 74 72 75 63 74 75 e in the structu
1bd8c 72 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 re passed as an
1bd8d 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 41 6e .** argument. An
1bd8e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69 74 68 expression with
1bd8f 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2c 20 45 78 no children, Ex
1bd90 70 72 2e 70 4c 69 73 74 20 6f 72 20 0a 2a 2a 20 pr.pList or .**
1bd91 45 78 70 72 2e 70 53 65 6c 65 63 74 20 6d 65 6d Expr.pSelect mem
1bd92 62 65 72 20 68 61 73 20 61 20 68 65 69 67 68 74 ber has a height
1bd93 20 6f 66 20 31 2e 20 41 6e 79 20 6f 74 68 65 72 of 1. Any other
1bd94 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 68 expression.** h
1bd95 61 73 20 61 20 68 65 69 67 68 74 20 65 71 75 61 as a height equa
1bd96 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d l to the maximum
1bd97 20 68 65 69 67 68 74 20 6f 66 20 61 6e 79 20 6f height of any o
1bd98 74 68 65 72 20 0a 2a 2a 20 72 65 66 65 72 65 6e ther .** referen
1bd99 63 65 64 20 45 78 70 72 20 70 6c 75 73 20 6f 6e ced Expr plus on
1bd9a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
1bd9b 64 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 d exprSetHeight(
1bd9c 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Expr *p){. int
1bd9d 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 nHeight = 0;. h
1bd9e 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 eightOfExpr(p->p
1bd9f 4c 65 66 74 2c 20 26 6e 48 65 69 67 68 74 29 3b Left, &nHeight);
1bda0 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 . heightOfExpr(
1bda1 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e 48 65 69 p->pRight, &nHei
1bda2 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 ght);. heightOf
1bda3 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4c 69 73 ExprList(p->pLis
1bda4 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 t, &nHeight);.
1bda5 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 heightOfSelect(p
1bda6 2d 3e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 ->pSelect, &nHei
1bda7 67 68 74 29 3b 0a 20 20 70 2d 3e 6e 48 65 69 67 ght);. p->nHeig
1bda8 68 74 20 3d 20 6e 48 65 69 67 68 74 20 2b 20 31 ht = nHeight + 1
1bda9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
1bdaa 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20 he Expr.nHeight
1bdab 76 61 72 69 61 62 6c 65 20 75 73 69 6e 67 20 74 variable using t
1bdac 68 65 20 65 78 70 72 53 65 74 48 65 69 67 68 74 he exprSetHeight
1bdad 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 0a () function. If.
1bdae 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 69 73 ** the height is
1bdaf 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
1bdb0 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 e maximum allowe
1bdb1 64 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 d expression dep
1bdb2 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 th,.** leave an
1bdb3 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e error in pParse.
1bdb4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1bdb5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
1bdb6 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72 xprSetHeight(Par
1bdb7 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
1bdb8 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 65 74 48 *p){. exprSetH
1bdb9 65 69 67 68 74 28 70 29 3b 0a 20 20 73 71 6c 69 eight(p);. sqli
1bdba 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 te3ExprCheckHeig
1bdbb 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48 ht(pParse, p->nH
1bdbc 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a eight);.}../*.**
1bdbd 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 Return the maxi
1bdbe 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e mum height of an
1bdbf 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 y expression tre
1bdc0 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20 e referenced.**
1bdc1 62 79 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 by the select st
1bdc2 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 atement passed a
1bdc3 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a s an argument..*
1bdc4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1bdc5 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 int sqlite3Sele
1bdc6 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65 6c ctExprHeight(Sel
1bdc7 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e ect *p){. int n
1bdc8 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 Height = 0;. he
1bdc9 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20 ightOfSelect(p,
1bdca 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74 &nHeight);. ret
1bdcb 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 23 urn nHeight;.}.#
1bdcc 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 65 else. #define e
1bdcd 78 70 72 53 65 74 48 65 69 67 68 74 28 79 29 0a xprSetHeight(y).
1bdce 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1bdcf 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e _MAX_EXPR_DEPTH>
1bdd0 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 0 */../*.** Cons
1bdd1 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 truct a new expr
1bdd2 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 ession node and
1bdd3 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
1bdd4 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a to it. Memory.
1bdd5 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 ** for this node
1bdd6 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
1bdd7 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
1bdd8 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 (). The calling
1bdd9 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 function.** is
1bdda 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
1bddb 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 making sure the
1bddc 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 node eventually
1bddd 67 65 74 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 53 gets freed..*/.S
1bdde 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
1bddf 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 pr *sqlite3Expr(
1bde0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
1bde1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
1bde2 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 ndle for sqlite3
1bde3 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 DbMallocZero() (
1bde4 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a may be null) */.
1bde5 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 int op,
1bde6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
1bde7 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a ression opcode *
1bde8 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c /. Expr *pLeft,
1bde9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1bdea 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 eft operand */.
1bdeb 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 Expr *pRight,
1bdec 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 /* Righ
1bded 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 t operand */. c
1bdee 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b onst Token *pTok
1bdef 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 en /* Argume
1bdf0 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 nt token */.){.
1bdf1 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 70 Expr *pNew;. p
1bdf2 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
1bdf3 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
1bdf4 7a 65 6f 66 28 45 78 70 72 29 29 3b 0a 20 20 69 zeof(Expr));. i
1bdf5 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 f( pNew==0 ){.
1bdf6 20 20 2f 2a 20 57 68 65 6e 20 6d 61 6c 6c 6f 63 /* When malloc
1bdf7 20 66 61 69 6c 73 2c 20 64 65 6c 65 74 65 20 70 fails, delete p
1bdf8 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e Left and pRight.
1bdf9 20 45 78 70 72 65 73 73 69 6f 6e 73 20 70 61 73 Expressions pas
1bdfa 73 65 64 20 74 6f 20 0a 20 20 20 20 2a 2a 20 74 sed to . ** t
1bdfb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 his function mus
1bdfc 74 20 61 6c 77 61 79 73 20 62 65 20 61 6c 6c 6f t always be allo
1bdfd 63 61 74 65 64 20 77 69 74 68 20 73 71 6c 69 74 cated with sqlit
1bdfe 65 33 45 78 70 72 28 29 20 66 6f 72 20 74 68 69 e3Expr() for thi
1bdff 73 20 0a 20 20 20 20 2a 2a 20 72 65 61 73 6f 6e s . ** reason
1be00 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 . . */. sq
1be01 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1be02 64 62 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 db, pLeft);.
1be03 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1be04 65 28 64 62 2c 20 70 52 69 67 68 74 29 3b 0a 20 e(db, pRight);.
1be05 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1be06 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 6f 70 . pNew->op = op
1be07 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 ;. pNew->pLeft
1be08 3d 20 70 4c 65 66 74 3b 0a 20 20 70 4e 65 77 2d = pLeft;. pNew-
1be09 3e 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 >pRight = pRight
1be0a 3b 0a 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d ;. pNew->iAgg =
1be0b 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 73 70 61 -1;. pNew->spa
1be0c 6e 2e 7a 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20 n.z = (u8*)"";.
1be0d 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 if( pToken ){.
1be0e 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 assert( pToke
1be0f 6e 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 n->dyn==0 );.
1be10 20 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e pNew->span = pN
1be11 65 77 2d 3e 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f ew->token = *pTo
1be12 6b 65 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ken;. }else if(
1be13 20 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 69 66 pLeft ){. if
1be14 28 20 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 ( pRight ){.
1be15 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 73 70 if( pRight->sp
1be16 61 6e 2e 64 79 6e 3d 3d 30 20 26 26 20 70 4c 65 an.dyn==0 && pLe
1be17 66 74 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d 30 20 ft->span.dyn==0
1be18 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1be19 65 33 45 78 70 72 53 70 61 6e 28 70 4e 65 77 2c e3ExprSpan(pNew,
1be1a 20 26 70 4c 65 66 74 2d 3e 73 70 61 6e 2c 20 26 &pLeft->span, &
1be1b 70 52 69 67 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 pRight->span);.
1be1c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1be1d 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 pRight->flags &
1be1e 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 EP_ExpCollate )
1be1f 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e {. pNew->
1be20 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 flags |= EP_ExpC
1be21 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 ollate;.
1be22 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 pNew->pColl = pR
1be23 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 ight->pColl;.
1be24 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1be25 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 f( pLeft->flags
1be26 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 & EP_ExpCollate
1be27 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 ){. pNew->f
1be28 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f lags |= EP_ExpCo
1be29 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 70 4e 65 llate;. pNe
1be2a 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 w->pColl = pLeft
1be2b 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 ->pColl;. }.
1be2c 20 7d 0a 0a 20 20 65 78 70 72 53 65 74 48 65 69 }.. exprSetHei
1be2d 67 68 74 28 70 4e 65 77 29 3b 0a 20 20 72 65 74 ght(pNew);. ret
1be2e 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
1be2f 2a 2a 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 71 ** Works like sq
1be30 6c 69 74 65 33 45 78 70 72 28 29 20 65 78 63 65 lite3Expr() exce
1be31 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 pt that it takes
1be32 20 61 6e 20 65 78 74 72 61 20 50 61 72 73 65 2a an extra Parse*
1be33 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 .** argument and
1be34 20 6e 6f 74 69 66 69 65 73 20 74 68 65 20 61 73 notifies the as
1be35 73 6f 63 69 61 74 65 64 20 63 6f 6e 6e 65 63 74 sociated connect
1be36 69 6f 6e 20 6f 62 6a 65 63 74 20 69 66 20 6d 61 ion object if ma
1be37 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 lloc fails..*/.S
1be38 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
1be39 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 pr *sqlite3PExpr
1be3a 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1be3b 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 e, /* P
1be3c 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
1be3d 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 /. int op,
1be3e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
1be3f 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 xpression opcode
1be40 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 */. Expr *pLef
1be41 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t, /*
1be42 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f Left operand */
1be43 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c . Expr *pRight,
1be44 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 /* Ri
1be45 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 ght operand */.
1be46 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 const Token *pT
1be47 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 oken /* Argu
1be48 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b ment token */.){
1be49 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c . Expr *p = sql
1be4a 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65 2d ite3Expr(pParse-
1be4b 3e 64 62 2c 20 6f 70 2c 20 70 4c 65 66 74 2c 20 >db, op, pLeft,
1be4c 70 52 69 67 68 74 2c 20 70 54 6f 6b 65 6e 29 3b pRight, pToken);
1be4d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
1be4e 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b sqlite3ExprCheck
1be4f 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 Height(pParse, p
1be50 2d 3e 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a ->nHeight);. }.
1be51 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
1be52 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 *.** When doing
1be53 61 20 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20 a nested parse,
1be54 79 6f 75 20 63 61 6e 20 69 6e 63 6c 75 64 65 20 you can include
1be55 74 65 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72 terms in an expr
1be56 65 73 73 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c ession.** that l
1be57 6f 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 ook like this:
1be58 20 23 31 20 23 32 20 2e 2e 2e 20 20 54 68 65 73 #1 #2 ... Thes
1be59 65 20 74 65 72 6d 73 20 72 65 66 65 72 20 74 6f e terms refer to
1be5a 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 69 6e registers.** in
1be5b 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 the virtual mac
1be5c 68 69 6e 65 2e 20 20 23 4e 20 69 73 20 74 68 65 hine. #N is the
1be5d 20 4e 2d 74 68 20 72 65 67 69 73 74 65 72 2e 0a N-th register..
1be5e 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1be5f 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 ne is called by
1be60 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 64 65 the parser to de
1be61 61 6c 20 77 69 74 68 20 6f 6e 20 6f 66 20 74 68 al with on of th
1be62 6f 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 49 74 ose terms..** It
1be63 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 67 65 6e immediately gen
1be64 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 73 erates code to s
1be65 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 69 tore the value i
1be66 6e 20 61 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 n a memory locat
1be67 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 ion..** The retu
1be68 72 6e 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f rns an expressio
1be69 6e 20 74 68 61 74 20 77 69 6c 6c 20 63 6f 64 65 n that will code
1be6a 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 to extract the
1be6b 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 value from.** th
1be6c 61 74 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 at memory locati
1be6d 6f 6e 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f on as needed..*/
1be6e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1be6f 45 78 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67 Expr *sqlite3Reg
1be70 69 73 74 65 72 45 78 70 72 28 50 61 72 73 65 20 isterExpr(Parse
1be71 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a *pParse, Token *
1be72 70 54 6f 6b 65 6e 29 7b 0a 20 20 56 64 62 65 20 pToken){. Vdbe
1be73 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 *v = pParse->pVd
1be74 62 65 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 be;. Expr *p;.
1be75 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 if( pParse->nes
1be76 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 ted==0 ){. sq
1be77 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1be78 61 72 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 arse, "near \"%T
1be79 5c 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 \": syntax error
1be7a 22 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 ", pToken);.
1be7b 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45 return sqlite3PE
1be7c 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e xpr(pParse, TK_N
1be7d 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 ULL, 0, 0, 0);.
1be7e 20 7d 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 }. if( v==0 )
1be7f 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 return 0;. p =
1be80 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 sqlite3PExpr(pPa
1be81 72 73 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 rse, TK_REGISTER
1be82 2c 20 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b , 0, 0, pToken);
1be83 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 . if( p==0 ){.
1be84 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a return 0; /*
1be85 20 4d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a Malloc failed *
1be86 2f 0a 20 20 7d 0a 20 20 70 2d 3e 69 54 61 62 6c /. }. p->iTabl
1be87 65 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 e = atoi((char*)
1be88 26 70 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a &pToken->z[1]);.
1be89 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
1be8a 2a 0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 *.** Join two ex
1be8b 70 72 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 pressions using
1be8c 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e an AND operator.
1be8d 20 20 49 66 20 65 69 74 68 65 72 20 65 78 70 72 If either expr
1be8e 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c ession is.** NUL
1be8f 4c 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 L, then just ret
1be90 75 72 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 urn the other ex
1be91 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c pression..*/.SQL
1be92 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
1be93 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 *sqlite3ExprAnd
1be94 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 (sqlite3 *db, Ex
1be95 70 72 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 pr *pLeft, Expr
1be96 2a 70 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 *pRight){. if(
1be97 70 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 pLeft==0 ){.
1be98 72 65 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 return pRight;.
1be99 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 }else if( pRigh
1be9a 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 t==0 ){. retu
1be9b 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 rn pLeft;. }els
1be9c 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 e{. return sq
1be9d 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b lite3Expr(db, TK
1be9e 5f 41 4e 44 2c 20 70 4c 65 66 74 2c 20 70 52 69 _AND, pLeft, pRi
1be9f 67 68 74 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a ght, 0);. }.}..
1bea0 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 /*.** Set the Ex
1bea1 70 72 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f 66 pr.span field of
1bea2 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 the given expre
1bea3 73 73 69 6f 6e 20 74 6f 20 73 70 61 6e 20 61 6c ssion to span al
1bea4 6c 0a 2a 2a 20 74 65 78 74 20 62 65 74 77 65 65 l.** text betwee
1bea5 6e 20 74 68 65 20 74 77 6f 20 67 69 76 65 6e 20 n the two given
1bea6 74 6f 6b 65 6e 73 2e 20 20 42 6f 74 68 20 74 6f tokens. Both to
1bea7 6b 65 6e 73 20 6d 75 73 74 20 62 65 20 70 6f 69 kens must be poi
1bea8 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 74 68 65 20 nting.** at the
1bea9 73 61 6d 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a same string..*/.
1beaa 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1beab 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 oid sqlite3ExprS
1beac 70 61 6e 28 45 78 70 72 20 2a 70 45 78 70 72 2c pan(Expr *pExpr,
1bead 20 54 6f 6b 65 6e 20 2a 70 4c 65 66 74 2c 20 54 Token *pLeft, T
1beae 6f 6b 65 6e 20 2a 70 52 69 67 68 74 29 7b 0a 20 oken *pRight){.
1beaf 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74 21 assert( pRight!
1beb0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1beb1 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 69 66 pLeft!=0 );. if
1beb2 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 70 ( pExpr ){. p
1beb3 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 Expr->span.z = p
1beb4 4c 65 66 74 2d 3e 7a 3b 0a 20 20 20 20 70 45 78 Left->z;. pEx
1beb5 70 72 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 52 69 pr->span.n = pRi
1beb6 67 68 74 2d 3e 6e 20 2b 20 28 70 52 69 67 68 74 ght->n + (pRight
1beb7 2d 3e 7a 20 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b ->z - pLeft->z);
1beb8 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f . }.}../*.** Co
1beb9 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 nstruct a new ex
1beba 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f pression node fo
1bebb 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 r a function wit
1bebc 68 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 h multiple.** ar
1bebd 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 guments..*/.SQLI
1bebe 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
1bebf 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 *sqlite3ExprFunc
1bec0 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 tion(Parse *pPar
1bec1 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c se, ExprList *pL
1bec2 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b ist, Token *pTok
1bec3 65 6e 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 en){. Expr *pNe
1bec4 77 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 w;. sqlite3 *db
1bec5 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
1bec6 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 assert( pToken
1bec7 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 );. pNew = sqli
1bec8 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
1bec9 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29 db, sizeof(Expr)
1beca 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d );. if( pNew==
1becb 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1becc 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
1becd 62 2c 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 b, pList); /* Av
1bece 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f oid leaking memo
1becf 72 79 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 ry when malloc f
1bed0 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 ails */. retu
1bed1 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 rn 0;. }. pNew
1bed2 2d 3e 6f 70 20 3d 20 54 4b 5f 46 55 4e 43 54 49 ->op = TK_FUNCTI
1bed3 4f 4e 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 73 ON;. pNew->pLis
1bed4 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 61 73 73 t = pList;. ass
1bed5 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e ert( pToken->dyn
1bed6 3d 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 74 ==0 );. pNew->t
1bed7 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a oken = *pToken;.
1bed8 20 20 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70 pNew->span = p
1bed9 4e 65 77 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 73 New->token;.. s
1beda 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 qlite3ExprSetHei
1bedb 67 68 74 28 70 50 61 72 73 65 2c 20 70 4e 65 77 ght(pParse, pNew
1bedc 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 );. return pNew
1bedd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 ;.}../*.** Assig
1bede 6e 20 61 20 76 61 72 69 61 62 6c 65 20 6e 75 6d n a variable num
1bedf 62 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 ber to an expres
1bee0 73 69 6f 6e 20 74 68 61 74 20 65 6e 63 6f 64 65 sion that encode
1bee1 73 20 61 20 77 69 6c 64 63 61 72 64 0a 2a 2a 20 s a wildcard.**
1bee2 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 in the original
1bee3 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 SQL statement.
1bee4 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 .**.** Wildcards
1bee5 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 consisting of a
1bee6 20 73 69 6e 67 6c 65 20 22 3f 22 20 61 72 65 20 single "?" are
1bee7 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 65 78 assigned the nex
1bee8 74 20 73 65 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 t sequential.**
1bee9 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 2e variable number.
1beea 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 .**.** Wildcards
1beeb 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e of the form "?n
1beec 6e 6e 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 nn" are assigned
1beed 20 74 68 65 20 6e 75 6d 62 65 72 20 22 6e 6e 6e the number "nnn
1beee 22 2e 20 20 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 ". We make.** s
1beef 75 72 65 20 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 ure "nnn" is not
1bef0 20 74 6f 6f 20 62 65 20 74 6f 20 61 76 6f 69 64 too be to avoid
1bef1 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 72 a denial of ser
1bef2 76 69 63 65 20 61 74 74 61 63 6b 20 77 68 65 6e vice attack when
1bef3 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74 .** the SQL stat
1bef4 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d ement comes from
1bef5 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 an external sou
1bef6 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 rce..**.** Wildc
1bef7 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d ards of the form
1bef8 20 22 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 ":aaa" or "$aaa
1bef9 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 " are assigned t
1befa 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a he same number.*
1befb 2a 20 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 * as the previou
1befc 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 s instance of th
1befd 65 20 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e e same wildcard.
1befe 20 20 4f 72 20 69 66 20 74 68 69 73 20 69 73 20 Or if this is
1beff 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 the first.** ins
1bf00 74 61 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c tance of the wil
1bf01 64 63 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20 dcard, the next
1bf02 73 65 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62 sequenial variab
1bf03 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 le number is.**
1bf04 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c assigned..*/.SQL
1bf05 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1bf06 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 sqlite3ExprAssi
1bf07 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 gnVarNumber(Pars
1bf08 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
1bf09 2a 70 45 78 70 72 29 7b 0a 20 20 54 6f 6b 65 6e *pExpr){. Token
1bf0a 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 *pToken;. sqli
1bf0b 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1bf0c 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 45 78 ->db;.. if( pEx
1bf0d 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a pr==0 ) return;.
1bf0e 20 20 70 54 6f 6b 65 6e 20 3d 20 26 70 45 78 70 pToken = &pExp
1bf0f 72 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 r->token;. asse
1bf10 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31 rt( pToken->n>=1
1bf11 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 );. assert( pT
1bf12 6f 6b 65 6e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 oken->z!=0 );.
1bf13 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e assert( pToken->
1bf14 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 z[0]!=0 );. if(
1bf15 20 70 54 6f 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b pToken->n==1 ){
1bf16 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 . /* Wildcard
1bf17 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 of the form "?"
1bf18 2e 20 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 . Assign the ne
1bf19 78 74 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 xt variable numb
1bf1a 65 72 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d er */. pExpr-
1bf1b 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 >iTable = ++pPar
1bf1c 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73 se->nVar;. }els
1bf1d 65 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b e if( pToken->z[
1bf1e 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f 0]=='?' ){. /
1bf1f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 * Wildcard of th
1bf20 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 e form "?nnn".
1bf21 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f Convert "nnn" to
1bf22 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a an integer and.
1bf23 20 20 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73 ** use it as
1bf24 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75 the variable nu
1bf25 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 mber */. int
1bf26 69 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 i;. pExpr->iT
1bf27 61 62 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28 able = i = atoi(
1bf28 28 63 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e (char*)&pToken->
1bf29 7a 5b 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63 z[1]);. testc
1bf2a 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 ase( i==0 );.
1bf2b 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 testcase( i==1
1bf2c 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
1bf2d 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 i==db->aLimit[S
1bf2e 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
1bf2f 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29 ABLE_NUMBER]-1 )
1bf30 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
1bf31 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 i==db->aLimit[SQ
1bf32 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 LITE_LIMIT_VARIA
1bf33 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 BLE_NUMBER] );.
1bf34 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e if( i<1 || i>
1bf35 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1bf36 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 E_LIMIT_VARIABLE
1bf37 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 _NUMBER] ){.
1bf38 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1bf39 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 69 61 g(pParse, "varia
1bf3a 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 ble number must
1bf3b 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e be between ?1 an
1bf3c 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20 d ?%d",.
1bf3d 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c db->aLimit[SQL
1bf3e 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 ITE_LIMIT_VARIAB
1bf3f 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20 LE_NUMBER]);.
1bf40 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 }. if( i>pPa
1bf41 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 rse->nVar ){.
1bf42 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 pParse->nVar
1bf43 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c = i;. }. }el
1bf44 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 se{. /* Wildc
1bf45 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d ards of the form
1bf46 20 22 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 ":aaa" or "$aaa
1bf47 22 2e 20 20 52 65 75 73 65 20 74 68 65 20 73 61 ". Reuse the sa
1bf48 6d 65 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 me variable.
1bf49 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65 ** number as the
1bf4a 20 70 72 69 6f 72 20 61 70 70 65 61 72 61 6e 63 prior appearanc
1bf4b 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 e of the same na
1bf4c 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20 6e 61 me, or if the na
1bf4d 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 65 me. ** has ne
1bf4e 76 65 72 20 61 70 70 65 61 72 65 64 20 62 65 66 ver appeared bef
1bf4f 6f 72 65 2c 20 72 65 75 73 65 20 74 68 65 20 73 ore, reuse the s
1bf50 61 6d 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d ame variable num
1bf51 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ber. */. i
1bf52 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20 6e 20 3d nt i, n;. n =
1bf53 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20 pToken->n;.
1bf54 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 for(i=0; i<pPars
1bf55 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b e->nVarExpr; i++
1bf56 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 ){. Expr *p
1bf57 45 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 45 E;. if( (pE
1bf58 20 3d 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 = pParse->apVar
1bf59 45 78 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20 Expr[i])!=0.
1bf5a 20 20 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b && pE->tok
1bf5b 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20 en.n==n.
1bf5c 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e && memcmp(pE->
1bf5d 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d token.z, pToken-
1bf5e 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 >z, n)==0 ){.
1bf5f 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1bf60 6c 65 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b le = pE->iTable;
1bf61 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1bf62 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1bf63 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d if( i>=pParse-
1bf64 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20 >nVarExpr ){.
1bf65 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1bf66 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 = ++pParse->nVa
1bf67 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 r;. if( pPa
1bf68 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 rse->nVarExpr>=p
1bf69 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 Parse->nVarExprA
1bf6a 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 lloc-1 ){.
1bf6b 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 pParse->nVarEx
1bf6c 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 prAlloc += pPars
1bf6d 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 e->nVarExprAlloc
1bf6e 20 2b 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70 + 10;. p
1bf6f 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
1bf70 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 =. s
1bf71 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f qlite3DbReallocO
1bf72 72 46 72 65 65 28 0a 20 20 20 20 20 20 20 20 20 rFree(.
1bf73 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 db,.
1bf74 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 pParse->a
1bf75 70 56 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20 pVarExpr,.
1bf76 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
1bf77 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 nVarExprAlloc*si
1bf78 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 zeof(pParse->apV
1bf79 61 72 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 arExpr[0]).
1bf7a 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
1bf7b 7d 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d }. if( !db-
1bf7c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
1bf7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1bf7e 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 pParse->apVarEx
1bf7f 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 pr!=0 );.
1bf80 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 pParse->apVarEx
1bf81 70 72 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 pr[pParse->nVarE
1bf82 78 70 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a xpr++] = pExpr;.
1bf83 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1bf84 7d 20 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 } . if( !pParse
1bf85 2d 3e 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 ->nErr && pParse
1bf86 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 ->nVar>db->aLimi
1bf87 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 t[SQLITE_LIMIT_V
1bf88 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 ARIABLE_NUMBER]
1bf89 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1bf8a 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1bf8b 74 6f 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 too many SQL var
1bf8c 69 61 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a iables");. }.}.
1bf8d 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 ./*.** Clear an
1bf8e 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 expression struc
1bf8f 74 75 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c ture without del
1bf90 65 74 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 eting the struct
1bf91 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 ure itself..** S
1bf92 75 62 73 74 72 75 63 74 75 72 65 20 69 73 20 64 ubstructure is d
1bf93 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 eleted..*/.SQLIT
1bf94 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1bf95 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 qlite3ExprClear(
1bf96 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
1bf97 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e r *p){. if( p->
1bf98 73 70 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 span.dyn ) sqlit
1bf99 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 e3DbFree(db, (ch
1bf9a 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a 29 3b 0a ar*)p->span.z);.
1bf9b 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64 if( p->token.d
1bf9c 79 6e 20 29 20 73 71 6c 69 74 65 33 44 62 46 72 yn ) sqlite3DbFr
1bf9d 65 65 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d ee(db, (char*)p-
1bf9e 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 73 71 6c >token.z);. sql
1bf9f 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
1bfa0 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 b, p->pLeft);.
1bfa1 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1bfa2 65 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 29 e(db, p->pRight)
1bfa3 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c ;. sqlite3ExprL
1bfa4 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d istDelete(db, p-
1bfa5 3e 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 >pList);. sqlit
1bfa6 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 e3SelectDelete(d
1bfa7 62 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a b, p->pSelect);.
1bfa8 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 }../*.** Recursi
1bfa9 76 65 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 vely delete an e
1bfaa 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a xpression tree..
1bfab 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1bfac 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
1bfad 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 prDelete(sqlite3
1bfae 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a *db, Expr *p){.
1bfaf 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
1bfb0 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 urn;. sqlite3Ex
1bfb1 70 72 43 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a prClear(db, p);.
1bfb2 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1bfb3 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a db, p);.}../*.**
1bfb4 20 54 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 20 The Expr.token
1bfb5 66 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20 61 field might be a
1bfb6 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 string literal
1bfb7 74 68 61 74 20 69 73 20 71 75 6f 74 65 64 2e 0a that is quoted..
1bfb8 2a 2a 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65 ** If so, remove
1bfb9 20 74 68 65 20 71 75 6f 74 61 74 69 6f 6e 20 6d the quotation m
1bfba 61 72 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f arks..*/.SQLITE_
1bfbb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1bfbc 69 74 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 ite3DequoteExpr(
1bfbd 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
1bfbe 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70 r *p){. if( Exp
1bfbf 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
1bfc0 70 2c 20 45 50 5f 44 65 71 75 6f 74 65 64 29 20 p, EP_Dequoted)
1bfc1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 ){. return;.
1bfc2 20 7d 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 }. ExprSetProp
1bfc3 65 72 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f erty(p, EP_Dequo
1bfc4 74 65 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74 ted);. if( p->t
1bfc5 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20 oken.dyn==0 ){.
1bfc6 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 sqlite3TokenC
1bfc7 6f 70 79 28 64 62 2c 20 26 70 2d 3e 74 6f 6b 65 opy(db, &p->toke
1bfc8 6e 2c 20 26 70 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 n, &p->token);.
1bfc9 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 71 75 }. sqlite3Dequ
1bfca 6f 74 65 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f ote((char*)p->to
1bfcb 6b 65 6e 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ken.z);.}../*.**
1bfcc 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 The following g
1bfcd 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 roup of routines
1bfce 20 6d 61 6b 65 20 64 65 65 70 20 63 6f 70 69 65 make deep copie
1bfcf 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 s of expressions
1bfd0 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 ,.** expression
1bfd1 6c 69 73 74 73 2c 20 49 44 20 6c 69 73 74 73 2c lists, ID lists,
1bfd2 20 61 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 and select stat
1bfd3 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 63 6f 70 ements. The cop
1bfd4 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64 65 ies can.** be de
1bfd5 6c 65 74 65 64 20 28 62 79 20 62 65 69 6e 67 20 leted (by being
1bfd6 70 61 73 73 65 64 20 74 6f 20 74 68 65 69 72 20 passed to their
1bfd7 72 65 73 70 65 63 74 69 76 65 20 2e 2e 2e 44 65 respective ...De
1bfd8 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 73 29 lete() routines)
1bfd9 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65 66 66 65 .** without effe
1bfda 63 74 69 6e 67 20 74 68 65 20 6f 72 69 67 69 6e cting the origin
1bfdb 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 als..**.** The e
1bfdc 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 xpression list,
1bfdd 49 44 2c 20 61 6e 64 20 73 6f 75 72 63 65 20 6c ID, and source l
1bfde 69 73 74 73 20 72 65 74 75 72 6e 20 62 79 20 73 ists return by s
1bfdf 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 qlite3ExprListDu
1bfe0 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 49 p(),.** sqlite3I
1bfe1 64 4c 69 73 74 44 75 70 28 29 2c 20 61 6e 64 20 dListDup(), and
1bfe2 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 sqlite3SrcListDu
1bfe3 70 28 29 20 63 61 6e 20 6e 6f 74 20 62 65 20 66 p() can not be f
1bfe4 75 72 74 68 65 72 20 65 78 70 61 6e 64 65 64 20 urther expanded
1bfe5 0a 2a 2a 20 62 79 20 73 75 62 73 65 71 75 65 6e .** by subsequen
1bfe6 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 t calls to sqlit
1bfe7 65 2a 4c 69 73 74 41 70 70 65 6e 64 28 29 20 72 e*ListAppend() r
1bfe8 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 outines..**.** A
1bfe9 6e 79 20 74 61 62 6c 65 73 20 74 68 61 74 20 74 ny tables that t
1bfea 68 65 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 he SrcList might
1bfeb 20 70 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e 6f point to are no
1bfec 74 20 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a 2f t duplicated..*/
1bfed 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1bfee 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 Expr *sqlite3Exp
1bfef 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 rDup(sqlite3 *db
1bff0 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 45 78 , Expr *p){. Ex
1bff1 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 pr *pNew;. if(
1bff2 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b p==0 ) return 0;
1bff3 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
1bff4 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
1bff5 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 sizeof(*p) );.
1bff6 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 if( pNew==0 ) r
1bff7 65 74 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 63 70 eturn 0;. memcp
1bff8 79 28 70 4e 65 77 2c 20 70 2c 20 73 69 7a 65 6f y(pNew, p, sizeo
1bff9 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 69 66 28 f(*pNew));. if(
1bffa 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 30 20 29 p->token.z!=0 )
1bffb 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 {. pNew->toke
1bffc 6e 2e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 n.z = (u8*)sqlit
1bffd 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 e3DbStrNDup(db,
1bffe 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e (char*)p->token.
1bfff 7a 2c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a z, p->token.n);.
1c000 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e pNew->token.
1c001 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 dyn = 1;. }else
1c002 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e {. assert( pN
1c003 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29 ew->token.z==0 )
1c004 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 73 70 ;. }. pNew->sp
1c005 61 6e 2e 7a 20 3d 20 30 3b 0a 20 20 70 4e 65 77 an.z = 0;. pNew
1c006 2d 3e 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 ->pLeft = sqlite
1c007 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 3ExprDup(db, p->
1c008 70 4c 65 66 74 29 3b 0a 20 20 70 4e 65 77 2d 3e pLeft);. pNew->
1c009 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 pRight = sqlite3
1c00a 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 ExprDup(db, p->p
1c00b 52 69 67 68 74 29 3b 0a 20 20 70 4e 65 77 2d 3e Right);. pNew->
1c00c 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 pList = sqlite3E
1c00d 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 xprListDup(db, p
1c00e 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 70 4e 65 77 ->pList);. pNew
1c00f 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 ->pSelect = sqli
1c010 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c te3SelectDup(db,
1c011 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 p->pSelect);.
1c012 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 return pNew;.}.S
1c013 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1c014 69 64 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 id sqlite3TokenC
1c015 6f 70 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c opy(sqlite3 *db,
1c016 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 54 6f 6b Token *pTo, Tok
1c017 65 6e 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66 en *pFrom){. if
1c018 28 20 70 54 6f 2d 3e 64 79 6e 20 29 20 73 71 6c ( pTo->dyn ) sql
1c019 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 ite3DbFree(db, (
1c01a 63 68 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b 0a 20 char*)pTo->z);.
1c01b 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 20 29 7b if( pFrom->z ){
1c01c 0a 20 20 20 20 70 54 6f 2d 3e 6e 20 3d 20 70 46 . pTo->n = pF
1c01d 72 6f 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54 6f 2d rom->n;. pTo-
1c01e 3e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 >z = (u8*)sqlite
1c01f 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 3DbStrNDup(db, (
1c020 63 68 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a 2c 20 char*)pFrom->z,
1c021 70 46 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20 20 70 pFrom->n);. p
1c022 54 6f 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d To->dyn = 1;. }
1c023 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a else{. pTo->z
1c024 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 = 0;. }.}.SQLI
1c025 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c TE_PRIVATE ExprL
1c026 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 ist *sqlite3Expr
1c027 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 ListDup(sqlite3
1c028 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 *db, ExprList *p
1c029 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 ){. ExprList *p
1c02a 4e 65 77 3b 0a 20 20 73 74 72 75 63 74 20 45 78 New;. struct Ex
1c02b 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
1c02c 65 6d 2c 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 em, *pOldItem;.
1c02d 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d int i;. if( p=
1c02e 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
1c02f 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
1c030 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 bMallocRaw(db, s
1c031 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a izeof(*pNew) );.
1c032 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 if( pNew==0 )
1c033 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 return 0;. pNew
1c034 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 30 3b 0a ->iECursor = 0;.
1c035 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20 pNew->nExpr =
1c036 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 pNew->nAlloc = p
1c037 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d ->nExpr;. pNew-
1c038 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c >a = pItem = sql
1c039 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
1c03a 64 62 2c 20 20 70 2d 3e 6e 45 78 70 72 2a 73 69 db, p->nExpr*si
1c03b 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b zeof(p->a[0]) );
1c03c 0a 20 20 69 66 28 20 70 49 74 65 6d 3d 3d 30 20 . if( pItem==0
1c03d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 ){. sqlite3Db
1c03e 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a Free(db, pNew);.
1c03f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1c040 7d 20 0a 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 } . pOldItem =
1c041 70 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b p->a;. for(i=0;
1c042 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b i<p->nExpr; i++
1c043 2c 20 70 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 , pItem++, pOldI
1c044 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 tem++){. Expr
1c045 20 2a 70 4e 65 77 45 78 70 72 2c 20 2a 70 4f 6c *pNewExpr, *pOl
1c046 64 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d dExpr;. pItem
1c047 2d 3e 70 45 78 70 72 20 3d 20 70 4e 65 77 45 78 ->pExpr = pNewEx
1c048 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 pr = sqlite3Expr
1c049 44 75 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 Dup(db, pOldExpr
1c04a 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 = pOldItem->pEx
1c04b 70 72 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c pr);. if( pOl
1c04c 64 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 dExpr->span.z!=0
1c04d 20 26 26 20 70 4e 65 77 45 78 70 72 20 29 7b 0a && pNewExpr ){.
1c04e 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 /* Always
1c04f 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
1c050 68 65 20 73 70 61 6e 20 66 6f 72 20 74 6f 70 2d he span for top-
1c051 6c 65 76 65 6c 20 65 78 70 72 65 73 73 69 6f 6e level expression
1c052 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a s in the. *
1c053 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 * expression lis
1c054 74 2e 20 20 54 68 65 20 6c 6f 67 69 63 20 69 6e t. The logic in
1c055 20 53 45 4c 45 43 54 20 70 72 6f 63 65 73 73 69 SELECT processi
1c056 6e 67 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e ng that determin
1c057 65 73 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 es. ** the
1c058 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 names of columns
1c059 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1c05a 65 74 20 6e 65 65 64 73 20 74 68 69 73 20 69 6e et needs this in
1c05b 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 formation */.
1c05c 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 sqlite3TokenC
1c05d 6f 70 79 28 64 62 2c 20 26 70 4e 65 77 45 78 70 opy(db, &pNewExp
1c05e 72 2d 3e 73 70 61 6e 2c 20 26 70 4f 6c 64 45 78 r->span, &pOldEx
1c05f 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 7d pr->span);. }
1c060 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 . assert( pNe
1c061 77 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 65 77 wExpr==0 || pNew
1c062 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 Expr->span.z!=0
1c063 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
1c064 70 4f 6c 64 45 78 70 72 2d 3e 73 70 61 6e 2e 7a pOldExpr->span.z
1c065 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 ==0.
1c066 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
1c067 6c 65 64 20 29 3b 0a 20 20 20 20 70 49 74 65 6d led );. pItem
1c068 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 ->zName = sqlite
1c069 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 3DbStrDup(db, pO
1c06a 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a ldItem->zName);.
1c06b 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f pItem->sortO
1c06c 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d rder = pOldItem-
1c06d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 >sortOrder;.
1c06e 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 3b pItem->done = 0;
1c06f 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c . pItem->iCol
1c070 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 6f = pOldItem->iCo
1c071 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 l;. pItem->iA
1c072 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d 2d lias = pOldItem-
1c073 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 72 >iAlias;. }. r
1c074 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
1c075 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c *.** If cursors,
1c076 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77 73 triggers, views
1c077 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 and subqueries
1c078 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20 are all omitted
1c079 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 6c from.** the buil
1c07a 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 d, then none of
1c07b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f the following ro
1c07c 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66 utines, except f
1c07d 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 or .** sqlite3Se
1c07e 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62 lectDup(), can b
1c07f 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65 e called. sqlite
1c080 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73 20 3SelectDup() is
1c081 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 6c sometimes.** cal
1c082 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 led with a NULL
1c083 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 66 argument..*/.#if
1c084 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1c085 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 _OMIT_VIEW) || !
1c086 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1c087 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c 0a 20 MIT_TRIGGER) \.
1c088 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
1c089 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
1c08a 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
1c08b 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
1c08c 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 3SrcListDup(sqli
1c08d 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 te3 *db, SrcList
1c08e 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 *p){. SrcList
1c08f 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a *pNew;. int i;.
1c090 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 int nByte;. i
1c091 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
1c092 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 0;. nByte = si
1c093 7a 65 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e zeof(*p) + (p->n
1c094 53 72 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 Src>0 ? sizeof(p
1c095 2d 3e 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 ->a[0]) * (p->nS
1c096 72 63 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e rc-1) : 0);. pN
1c097 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
1c098 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 llocRaw(db, nByt
1c099 65 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d e );. if( pNew=
1c09a 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
1c09b 20 70 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e pNew->nSrc = pN
1c09c 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e ew->nAlloc = p->
1c09d 6e 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nSrc;. for(i=0;
1c09e 20 69 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 i<p->nSrc; i++)
1c09f 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 {. struct Src
1c0a0 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 List_item *pNewI
1c0a1 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 tem = &pNew->a[i
1c0a2 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 ];. struct Sr
1c0a3 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 cList_item *pOld
1c0a4 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b Item = &p->a[i];
1c0a5 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 . Table *pTab
1c0a6 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
1c0a7 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 zDatabase = sqli
1c0a8 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 te3DbStrDup(db,
1c0a9 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 pOldItem->zDatab
1c0aa 61 73 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 ase);. pNewIt
1c0ab 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 em->zName = sqli
1c0ac 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 te3DbStrDup(db,
1c0ad 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 pOldItem->zName)
1c0ae 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
1c0af 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 zAlias = sqlite3
1c0b0 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c DbStrDup(db, pOl
1c0b1 64 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a dItem->zAlias);.
1c0b2 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f pNewItem->jo
1c0b3 69 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 intype = pOldIte
1c0b4 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 m->jointype;.
1c0b5 20 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 pNewItem->iCurs
1c0b6 6f 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 or = pOldItem->i
1c0b7 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 Cursor;. pNew
1c0b8 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 Item->isPopulate
1c0b9 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 d = pOldItem->is
1c0ba 50 6f 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 Populated;. p
1c0bb 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 NewItem->zIndex
1c0bc 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
1c0bd 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
1c0be 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 zIndex);. pNe
1c0bf 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 wItem->notIndexe
1c0c0 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f d = pOldItem->no
1c0c1 74 49 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e tIndexed;. pN
1c0c2 65 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d ewItem->pIndex =
1c0c3 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 pOldItem->pInde
1c0c4 78 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e x;. pTab = pN
1c0c5 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 ewItem->pTab = p
1c0c6 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 OldItem->pTab;.
1c0c7 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 if( pTab ){.
1c0c8 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b pTab->nRef+
1c0c9 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 +;. }. pNe
1c0ca 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d wItem->pSelect =
1c0cb 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 sqlite3SelectDu
1c0cc 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
1c0cd 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e pSelect);. pN
1c0ce 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 ewItem->pOn = sq
1c0cf 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
1c0d0 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e 29 3b pOldItem->pOn);
1c0d1 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 . pNewItem->p
1c0d2 55 73 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 Using = sqlite3I
1c0d3 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c dListDup(db, pOl
1c0d4 64 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a dItem->pUsing);.
1c0d5 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f pNewItem->co
1c0d6 6c 55 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d lUsed = pOldItem
1c0d7 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 ->colUsed;. }.
1c0d8 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
1c0d9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 SQLITE_PRIVATE I
1c0da 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 dList *sqlite3Id
1c0db 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 ListDup(sqlite3
1c0dc 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b *db, IdList *p){
1c0dd 0a 20 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b . IdList *pNew;
1c0de 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
1c0df 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b p==0 ) return 0;
1c0e0 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
1c0e1 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
1c0e2 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 sizeof(*pNew) )
1c0e3 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
1c0e4 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
1c0e5 65 77 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e ew->nId = pNew->
1c0e6 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b nAlloc = p->nId;
1c0e7 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c . pNew->a = sql
1c0e8 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
1c0e9 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f db, p->nId*sizeo
1c0ea 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 f(p->a[0]) );.
1c0eb 69 66 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 if( pNew->a==0 )
1c0ec 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
1c0ed 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 ree(db, pNew);.
1c0ee 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1c0ef 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d . for(i=0; i<p-
1c0f0 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nId; i++){.
1c0f1 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 struct IdList_it
1c0f2 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 em *pNewItem = &
1c0f3 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 pNew->a[i];.
1c0f4 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 struct IdList_it
1c0f5 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 em *pOldItem = &
1c0f6 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 p->a[i];. pNe
1c0f7 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 wItem->zName = s
1c0f8 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
1c0f9 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 b, pOldItem->zNa
1c0fa 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 me);. pNewIte
1c0fb 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 m->idx = pOldIte
1c0fc 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 m->idx;. }. re
1c0fd 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c turn pNew;.}.SQL
1c0fe 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 ITE_PRIVATE Sele
1c0ff 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 ct *sqlite3Selec
1c100 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 tDup(sqlite3 *db
1c101 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 , Select *p){.
1c102 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 Select *pNew;.
1c103 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1c104 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 n 0;. pNew = sq
1c105 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
1c106 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 (db, sizeof(*p)
1c107 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 );. if( pNew==0
1c108 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
1c109 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 New->pEList = sq
1c10a 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 lite3ExprListDup
1c10b 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b (db, p->pEList);
1c10c 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 . pNew->pSrc =
1c10d 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 sqlite3SrcListDu
1c10e 70 28 64 62 2c 20 70 2d 3e 70 53 72 63 29 3b 0a p(db, p->pSrc);.
1c10f 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d pNew->pWhere =
1c110 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 sqlite3ExprDup(
1c111 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a db, p->pWhere);.
1c112 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 pNew->pGroupBy
1c113 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 = sqlite3ExprLi
1c114 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 stDup(db, p->pGr
1c115 6f 75 70 42 79 29 3b 0a 20 20 70 4e 65 77 2d 3e oupBy);. pNew->
1c116 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 pHaving = sqlite
1c117 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 3ExprDup(db, p->
1c118 70 48 61 76 69 6e 67 29 3b 0a 20 20 70 4e 65 77 pHaving);. pNew
1c119 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c ->pOrderBy = sql
1c11a 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 ite3ExprListDup(
1c11b 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 db, p->pOrderBy)
1c11c 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 ;. pNew->op = p
1c11d 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 ->op;. pNew->pP
1c11e 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 rior = sqlite3Se
1c11f 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 lectDup(db, p->p
1c120 50 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 2d 3e Prior);. pNew->
1c121 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 pLimit = sqlite3
1c122 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 ExprDup(db, p->p
1c123 4c 69 6d 69 74 29 3b 0a 20 20 70 4e 65 77 2d 3e Limit);. pNew->
1c124 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65 pOffset = sqlite
1c125 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 3ExprDup(db, p->
1c126 70 4f 66 66 73 65 74 29 3b 0a 20 20 70 4e 65 77 pOffset);. pNew
1c127 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 ->iLimit = 0;.
1c128 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 pNew->iOffset =
1c129 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 0;. pNew->selFl
1c12a 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 ags = p->selFlag
1c12b 73 20 26 20 7e 53 46 5f 55 73 65 73 45 70 68 65 s & ~SF_UsesEphe
1c12c 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 meral;. pNew->p
1c12d 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 Rightmost = 0;.
1c12e 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 pNew->addrOpenE
1c12f 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 phm[0] = -1;. p
1c130 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 New->addrOpenEph
1c131 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 m[1] = -1;. pNe
1c132 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b w->addrOpenEphm[
1c133 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 2] = -1;. retur
1c134 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a n pNew;.}.#else.
1c135 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
1c136 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 elect *sqlite3Se
1c137 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 20 lectDup(sqlite3
1c138 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b *db, Select *p){
1c139 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 . assert( p==0
1c13a 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
1c13b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
1c13c 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e Add a new elemen
1c13d 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 t to the end of
1c13e 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 an expression li
1c13f 73 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 st. If pList is
1c140 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 .** initially NU
1c141 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 LL, then create
1c142 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e a new expression
1c143 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 list..*/.SQLITE
1c144 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 _PRIVATE ExprLis
1c145 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 t *sqlite3ExprLi
1c146 73 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 stAppend(. Pars
1c147 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
1c148 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
1c149 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
1c14a 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 List *pList,
1c14b 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 /* List to w
1c14c 68 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 hich to append.
1c14d 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f Might be NULL */
1c14e 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 . Expr *pExpr,
1c14f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
1c150 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 pression to be a
1c151 70 70 65 6e 64 65 64 20 2a 2f 0a 20 20 54 6f 6b ppended */. Tok
1c152 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 en *pName
1c153 20 20 20 20 20 2f 2a 20 41 53 20 6b 65 79 77 6f /* AS keywo
1c154 72 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 rd for the expre
1c155 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 ssion */.){. sq
1c156 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
1c157 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c se->db;. if( pL
1c158 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c ist==0 ){. pL
1c159 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d ist = sqlite3DbM
1c15a 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
1c15b 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 zeof(ExprList) )
1c15c 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d ;. if( pList=
1c15d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f =0 ){. goto
1c15e 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 no_mem;. }.
1c15f 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 assert( pList
1c160 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 ->nAlloc==0 );.
1c161 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e }. if( pList->
1c162 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e nAlloc<=pList->n
1c163 45 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 Expr ){. stru
1c164 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
1c165 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d *a;. int n =
1c166 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 pList->nAlloc*2
1c167 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 + 4;. a = sq
1c168 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 lite3DbRealloc(d
1c169 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 b, pList->a, n*s
1c16a 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 izeof(pList->a[0
1c16b 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d ]));. if( a==
1c16c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
1c16d 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 no_mem;. }.
1c16e 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a pList->a = a;.
1c16f 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f pList->nAllo
1c170 63 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 c = n;. }. ass
1c171 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 ert( pList->a!=0
1c172 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 );. if( pExpr
1c173 7c 7c 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 || pName ){.
1c174 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1c175 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 item *pItem = &p
1c176 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e List->a[pList->n
1c177 45 78 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d Expr++];. mem
1c178 73 65 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 set(pItem, 0, si
1c179 7a 65 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20 zeof(*pItem));.
1c17a 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 pItem->zName
1c17b 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
1c17c 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 mToken(db, pName
1c17d 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 );. pItem->pE
1c17e 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 xpr = pExpr;.
1c17f 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d pItem->iAlias =
1c180 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
1c181 20 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a pList;..no_mem:
1c182 20 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 . /* Avoid
1c183 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 leaking memory
1c184 69 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 if malloc has fa
1c185 69 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 iled. */. sqlit
1c186 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
1c187 20 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 pExpr);. sqlit
1c188 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 e3ExprListDelete
1c189 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 (db, pList);. r
1c18a 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
1c18b 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
1c18c 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20 ion list pEList
1c18d 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 contains more th
1c18e 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e an iLimit elemen
1c18f 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 ts,.** leave an
1c190 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
1c191 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 pParse..*/.SQLI
1c192 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1c193 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 sqlite3ExprListC
1c194 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 heckLength(. Pa
1c195 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 rse *pParse,. E
1c196 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c xprList *pEList,
1c197 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1c198 4f 62 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 Object.){. int
1c199 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d mx = pParse->db-
1c19a 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1c19b 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 IMIT_COLUMN];.
1c19c 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 testcase( pEList
1c19d 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 && pEList->nExp
1c19e 72 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 r==mx );. testc
1c19f 61 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 ase( pEList && p
1c1a0 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 EList->nExpr==mx
1c1a1 2b 31 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 +1 );. if( pELi
1c1a2 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 st && pEList->nE
1c1a3 78 70 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 xpr>mx ){. sq
1c1a4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1c1a5 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 arse, "too many
1c1a6 63 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 columns in %s",
1c1a7 7a 4f 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a zObject);. }.}.
1c1a8 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e ./*.** Delete an
1c1a9 20 65 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 entire expressi
1c1aa 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 on list..*/.SQLI
1c1ab 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1c1ac 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
1c1ad 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 elete(sqlite3 *d
1c1ae 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 b, ExprList *pLi
1c1af 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 st){. int i;.
1c1b0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1c1b1 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 item *pItem;. i
1c1b2 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 f( pList==0 ) re
1c1b3 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 turn;. assert(
1c1b4 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 pList->a!=0 || (
1c1b5 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 pList->nExpr==0
1c1b6 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 && pList->nAlloc
1c1b7 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ==0) );. assert
1c1b8 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d ( pList->nExpr<=
1c1b9 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b pList->nAlloc );
1c1ba 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 . for(pItem=pLi
1c1bb 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c st->a, i=0; i<pL
1c1bc 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c ist->nExpr; i++,
1c1bd 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 pItem++){. s
1c1be 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1c1bf 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 (db, pItem->pExp
1c1c0 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 r);. sqlite3D
1c1c1 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d bFree(db, pItem-
1c1c2 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 >zName);. }. s
1c1c3 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1c1c4 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 pList->a);. sq
1c1c5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1c1c6 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pList);.}../*.**
1c1c7 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
1c1c8 61 72 65 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 are Walker callb
1c1c9 61 63 6b 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e acks. Walker.u.
1c1ca 70 69 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a pi is a pointer.
1c1cb 2a 2a 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 ** to an integer
1c1cc 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
1c1cd 73 20 61 72 65 20 63 68 65 63 6b 69 6e 67 20 61 s are checking a
1c1ce 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 n expression to
1c1cf 73 65 65 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 see.** if it is
1c1d0 61 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 a constant. Set
1c1d1 20 2a 57 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f *Walker.u.pi to
1c1d2 20 30 20 69 66 20 74 68 65 20 65 78 70 72 65 73 0 if the expres
1c1d3 73 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 sion is.** not c
1c1d4 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 onstant..**.** T
1c1d5 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f hese callback ro
1c1d6 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 utines are used
1c1d7 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1c1d8 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
1c1d9 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 * sqlite3Exp
1c1da 72 49 73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a rIsConstant().**
1c1db 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1c1dc 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 IsConstantNotJoi
1c1dd 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 n().** sqlit
1c1de 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
1c1df 4f 72 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a OrFunction().**.
1c1e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 */.static int ex
1c1e1 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 prNodeIsConstant
1c1e2 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
1c1e3 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a , Expr *pExpr){.
1c1e4 0a 20 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 . /* If pWalker
1c1e5 2d 3e 75 2e 69 20 69 73 20 33 20 74 68 65 6e 20 ->u.i is 3 then
1c1e6 61 6e 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 any term of the
1c1e7 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 expression that
1c1e8 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 comes from. **
1c1e9 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 the ON or USING
1c1ea 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 clauses of a joi
1c1eb 6e 20 64 69 73 71 75 61 6c 69 66 69 65 73 20 74 n disqualifies t
1c1ec 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 he expression.
1c1ed 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f ** from being co
1c1ee 6e 73 69 64 65 72 65 64 20 63 6f 6e 73 74 61 6e nsidered constan
1c1ef 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c t. */. if( pWal
1c1f0 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 ker->u.i==3 && E
1c1f1 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
1c1f2 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d y(pExpr, EP_From
1c1f3 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 Join) ){. pWa
1c1f4 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 lker->u.i = 0;.
1c1f5 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1c1f6 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 ort;. }.. swit
1c1f7 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b ch( pExpr->op ){
1c1f8 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 . /* Consider
1c1f9 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 functions to be
1c1fa 20 63 6f 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c constant if all
1c1fb 20 74 68 65 69 72 20 61 72 67 75 6d 65 6e 74 73 their arguments
1c1fc 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 are constant.
1c1fd 20 20 2a 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 ** and pWalker
1c1fe 2d 3e 75 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 ->u.i==2 */.
1c1ff 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e case TK_FUNCTION
1c200 3a 0a 20 20 20 20 20 20 69 66 28 20 70 57 61 6c :. if( pWal
1c201 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 ker->u.i==2 ) re
1c202 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a turn 0;. /*
1c203 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f Fall through */
1c204 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a . case TK_ID:
1c205 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c . case TK_COL
1c206 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b UMN:. case TK
1c207 5f 44 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 _DOT:. case T
1c208 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a K_AGG_FUNCTION:.
1c209 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f case TK_AGG_
1c20a 43 4f 4c 55 4d 4e 3a 0a 23 69 66 6e 64 65 66 20 COLUMN:.#ifndef
1c20b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1c20c 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b UERY. case TK
1c20d 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 _SELECT:. cas
1c20e 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 e TK_EXISTS:.
1c20f 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 testcase( pEx
1c210 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 pr->op==TK_SELEC
1c211 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
1c212 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d ase( pExpr->op==
1c213 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 65 6e TK_EXISTS );.#en
1c214 64 69 66 0a 20 20 20 20 20 20 74 65 73 74 63 61 dif. testca
1c215 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
1c216 4b 5f 49 44 20 29 3b 0a 20 20 20 20 20 20 74 65 K_ID );. te
1c217 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
1c218 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a p==TK_COLUMN );.
1c219 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1c21a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f pExpr->op==TK_DO
1c21b 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
1c21c 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d ase( pExpr->op==
1c21d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 TK_AGG_FUNCTION
1c21e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1c21f 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b e( pExpr->op==TK
1c220 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 _AGG_COLUMN );.
1c221 20 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e pWalker->u.
1c222 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 i = 0;. ret
1c223 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1c224 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 default:.
1c225 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e return WRC_Con
1c226 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 tinue;. }.}.sta
1c227 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f tic int selectNo
1c228 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c deIsConstant(Wal
1c229 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 ker *pWalker, Se
1c22a 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a lect *pSelect){.
1c22b 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d pWalker->u.i =
1c22c 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 0;. return WRC
1c22d 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 63 _Abort;.}.static
1c22e 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 74 int exprIsConst
1c22f 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 6e (Expr *p, int in
1c230 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b 65 itFlag){. Walke
1c231 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 69 r w;. w.u.i = i
1c232 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 78 nitFlag;. w.xEx
1c233 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 prCallback = exp
1c234 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b rNodeIsConstant;
1c235 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c . w.xSelectCall
1c236 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f 64 back = selectNod
1c237 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 73 eIsConstant;. s
1c238 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 qlite3WalkExpr(&
1c239 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 w, p);. return
1c23a 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 w.u.i;.}../*.**
1c23b 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 Walk an expressi
1c23c 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e on tree. Return
1c23d 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73 1 if the expres
1c23e 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 sion is constant
1c23f 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74 20 .** and 0 if it
1c240 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c involves variabl
1c241 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63 es or function c
1c242 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 alls..**.** For
1c243 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
1c244 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 this function, a
1c245 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 double-quoted s
1c246 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 tring (ex: "abc"
1c247 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 ).** is consider
1c248 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 ed a variable bu
1c249 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 t a single-quote
1c24a 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 d string (ex: 'a
1c24b 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e bc') is.** a con
1c24c 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 stant..*/.SQLITE
1c24d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1c24e 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
1c24f 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 nt(Expr *p){. r
1c250 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 eturn exprIsCons
1c251 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a t(p, 1);.}../*.*
1c252 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 * Walk an expres
1c253 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 sion tree. Retu
1c254 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 rn 1 if the expr
1c255 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 ession is consta
1c256 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 nt.** that does
1c257 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72 6f no originate fro
1c258 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e m the ON or USIN
1c259 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a G clauses of a j
1c25a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 oin..** Return 0
1c25b 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 if it involves
1c25c 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e variables or fun
1c25d 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 74 ction calls or t
1c25e 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 erms from.** an
1c25f 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 ON or USING clau
1c260 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
1c261 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1c262 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 3ExprIsConstantN
1c263 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 7b otJoin(Expr *p){
1c264 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 . return exprIs
1c265 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a Const(p, 3);.}..
1c266 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 /*.** Walk an ex
1c267 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 pression tree.
1c268 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
1c269 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f expression is co
1c26a 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66 nstant.** or a f
1c26b 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74 unction call wit
1c26c 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d h constant argum
1c26d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e ents. Return an
1c26e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 d 0 if there.**
1c26f 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65 are any variable
1c270 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 s..**.** For the
1c271 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 purposes of thi
1c272 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f s function, a do
1c273 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 uble-quoted stri
1c274 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a ng (ex: "abc").*
1c275 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 * is considered
1c276 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 a variable but a
1c277 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 single-quoted s
1c278 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 tring (ex: 'abc'
1c279 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 ) is.** a consta
1c27a 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
1c27b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1c27c 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 3ExprIsConstantO
1c27d 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a rFunction(Expr *
1c27e 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 p){. return exp
1c27f 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b 0a rIsConst(p, 2);.
1c280 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
1c281 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64 expression p cod
1c282 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e es a constant in
1c283 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73 6d teger that is sm
1c284 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f all enough.** to
1c285 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74 fit in a 32-bit
1c286 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e integer, return
1c287 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 76 1 and put the v
1c288 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 alue of the inte
1c289 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 ger.** in *pValu
1c28a 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 65 e. If the expre
1c28b 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20 ssion is not an
1c28c 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 74 integer or if it
1c28d 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 is too big.** t
1c28e 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65 o fit in a signe
1c28f 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 d 32-bit integer
1c290 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c , return 0 and l
1c291 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63 eave *pValue unc
1c292 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 hanged..*/.SQLIT
1c293 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1c294 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 lite3ExprIsInteg
1c295 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 er(Expr *p, int
1c296 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 *pValue){. int
1c297 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d rc = 0;. if( p-
1c298 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 >flags & EP_IntV
1c299 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 61 alue ){. *pVa
1c29a 6c 75 65 20 3d 20 70 2d 3e 69 54 61 62 6c 65 3b lue = p->iTable;
1c29b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
1c29c 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e }. switch( p->
1c29d 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
1c29e 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 K_INTEGER: {.
1c29f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 rc = sqlite3G
1c2a0 65 74 49 6e 74 33 32 28 28 63 68 61 72 2a 29 70 etInt32((char*)p
1c2a1 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 56 61 6c 75 ->token.z, pValu
1c2a2 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b e);. break;
1c2a3 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1c2a4 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 TK_UPLUS: {.
1c2a5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 45 78 rc = sqlite3Ex
1c2a6 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 prIsInteger(p->p
1c2a7 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20 Left, pValue);.
1c2a8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1c2a9 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d }. case TK_UM
1c2aa 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e INUS: {. in
1c2ab 74 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 73 t v;. if( s
1c2ac 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 qlite3ExprIsInte
1c2ad 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 76 ger(p->pLeft, &v
1c2ae 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 56 ) ){. *pV
1c2af 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 alue = -v;.
1c2b0 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 rc = 1;.
1c2b1 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1c2b2 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
1c2b3 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 t: break;. }.
1c2b4 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 2d if( rc ){. p-
1c2b5 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52 >op = TK_INTEGER
1c2b6 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c ;. p->flags |
1c2b7 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 = EP_IntValue;.
1c2b8 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 2a p->iTable = *
1c2b9 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 20 72 65 pValue;. }. re
1c2ba 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1c2bb 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
1c2bc 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e the given strin
1c2bd 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63 6f g is a row-id co
1c2be 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 lumn name..*/.SQ
1c2bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1c2c0 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 sqlite3IsRowid(
1c2c1 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a const char *z){.
1c2c2 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 if( sqlite3Str
1c2c3 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 44 5f ICmp(z, "_ROWID_
1c2c4 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 ")==0 ) return 1
1c2c5 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 ;. if( sqlite3S
1c2c6 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 49 44 trICmp(z, "ROWID
1c2c7 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 ")==0 ) return 1
1c2c8 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 ;. if( sqlite3S
1c2c9 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 22 29 trICmp(z, "OID")
1c2ca 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a ==0 ) return 1;.
1c2cb 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 return 0;.}..#
1c2cc 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1c2cd 54 0a 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f T. int sqlite3_
1c2ce 65 6e 61 62 6c 65 5f 69 6e 5f 6f 70 74 20 3d 20 enable_in_opt =
1c2cf 31 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 1;.#else. #defi
1c2d0 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c ne sqlite3_enabl
1c2d1 65 5f 69 6e 5f 6f 70 74 20 31 0a 23 65 6e 64 69 e_in_opt 1.#endi
1c2d2 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
1c2d3 74 72 75 65 20 69 66 20 74 68 65 20 49 4e 20 6f true if the IN o
1c2d4 70 65 72 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 perator optimiza
1c2d5 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 tion is enabled
1c2d6 61 6e 64 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 and.** the SELEC
1c2d7 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 65 78 T statement p ex
1c2d8 69 73 74 73 20 61 6e 64 20 69 73 20 6f 66 20 74 ists and is of t
1c2d9 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 20 66 6f 72 he.** simple for
1c2da 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c m:.**.** SEL
1c2db 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f ECT <column> FRO
1c2dc 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 M <table>.**.**
1c2dd 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 If this is the c
1c2de 61 73 65 2c 20 69 74 20 6d 61 79 20 62 65 20 70 ase, it may be p
1c2df 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 ossible to use a
1c2e0 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 n existing table
1c2e1 0a 2a 2a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 .** or index ins
1c2e2 74 65 61 64 20 6f 66 20 67 65 6e 65 72 61 74 69 tead of generati
1c2e3 6e 67 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 ng an epheremal
1c2e4 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 table..*/.#ifnde
1c2e5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 f SQLITE_OMIT_SU
1c2e6 42 51 55 45 52 59 0a 73 74 61 74 69 63 20 69 6e BQUERY.static in
1c2e7 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 t isCandidateFor
1c2e8 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70 29 InOpt(Select *p)
1c2e9 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 {. SrcList *pSr
1c2ea 63 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 c;. ExprList *p
1c2eb 45 4c 69 73 74 3b 0a 20 20 54 61 62 6c 65 20 2a EList;. Table *
1c2ec 70 54 61 62 3b 0a 20 20 69 66 28 20 21 73 71 6c pTab;. if( !sql
1c2ed 69 74 65 33 5f 65 6e 61 62 6c 65 5f 69 6e 5f 6f ite3_enable_in_o
1c2ee 70 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f pt ) return 0; /
1c2ef 2a 20 49 4e 20 6f 70 74 69 6d 69 7a 61 74 69 6f * IN optimizatio
1c2f0 6e 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 n must be enable
1c2f1 64 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 d */. if( p==0
1c2f2 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
1c2f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c2f4 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 right-hand side
1c2f5 20 6f 66 20 49 4e 20 69 73 20 53 45 4c 45 43 54 of IN is SELECT
1c2f6 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 */. if( p->pPr
1c2f7 69 6f 72 20 29 20 72 65 74 75 72 6e 20 30 3b 20 ior ) return 0;
1c2f8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c2f9 4e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 Not a compound S
1c2fa 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 ELECT */. if( p
1c2fb 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 ->selFlags & (SF
1c2fc 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 _Distinct|SF_Agg
1c2fd 72 65 67 61 74 65 29 20 29 7b 0a 20 20 20 20 20 regate) ){.
1c2fe 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f return 0; /* No
1c2ff 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 DISTINCT keywor
1c300 64 20 61 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 d and no aggrega
1c301 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a te functions */.
1c302 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 72 }. if( p->pGr
1c303 6f 75 70 42 79 20 29 20 72 65 74 75 72 6e 20 30 oupBy ) return 0
1c304 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1c305 48 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 Has no GROUP BY
1c306 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 clause */. if(
1c307 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75 p->pLimit ) retu
1c308 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 rn 0;
1c309 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d /* Has no LIM
1c30a 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 IT clause */. i
1c30b 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 20 f( p->pOffset )
1c30c 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
1c30d 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 p->pWhere ) retu
1c30e 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 rn 0;
1c30f 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 /* Has no WHE
1c310 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 RE clause */. p
1c311 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 Src = p->pSrc;.
1c312 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 20 72 if( pSrc==0 ) r
1c313 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
1c314 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e /* A sin
1c315 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 gle table in the
1c316 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a FROM clause */.
1c317 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 if( pSrc->nSrc
1c318 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a !=1 ) return 0;.
1c319 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d if( pSrc->a[0]
1c31a 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72 .pSelect ) retur
1c31b 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d n 0; /* FROM
1c31c 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61 clause is not a
1c31d 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70 subquery */. p
1c31e 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d Tab = pSrc->a[0]
1c31f 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61 .pTab;. if( pTa
1c320 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b b==0 ) return 0;
1c321 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 . if( pTab->pSe
1c322 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b lect ) return 0;
1c323 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f /* FRO
1c324 4d 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 M clause is not
1c325 61 20 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20 a view */. if(
1c326 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 IsVirtual(pTab)
1c327 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
1c328 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 /* FROM claus
1c329 65 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 e not a virtual
1c32a 74 61 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73 table */. pELis
1c32b 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 t = p->pEList;.
1c32c 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 if( pEList->nEx
1c32d 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 pr!=1 ) return 0
1c32e 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 ; /* One c
1c32f 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 olumn in the res
1c330 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28 ult set */. if(
1c331 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 pEList->a[0].pE
1c332 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 xpr->op!=TK_COLU
1c333 4d 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f MN ) return 0; /
1c334 2a 20 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f * Result is a co
1c335 6c 75 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e lumn */. return
1c336 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 1;.}.#endif /*
1c337 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1c338 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 UERY */../*.** T
1c339 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1c33a 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c used by the impl
1c33b 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
1c33c 65 20 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61 e IN (...) opera
1c33d 74 6f 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 tor..** It's job
1c33e 20 69 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63 is to find or c
1c33f 72 65 61 74 65 20 61 20 62 2d 74 72 65 65 20 73 reate a b-tree s
1c340 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 tructure that ma
1c341 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74 y be used.** eit
1c342 68 65 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20 her to test for
1c343 6d 65 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68 membership of th
1c344 65 20 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74 e (...) set or t
1c345 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 o iterate throug
1c346 68 0a 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73 h.** its members
1c347 2c 20 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69 , skipping dupli
1c348 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cates..**.** The
1c349 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 6f cursor opened o
1c34a 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 n the structure
1c34b 28 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c (database table,
1c34c 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 database index
1c34d 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20 .** or ephermal
1c34e 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64 table) is stored
1c34f 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62 in pX->iTable b
1c350 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 efore this funct
1c351 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 ion returns..**
1c352 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c The returned val
1c353 75 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 ue indicates the
1c354 20 73 74 72 75 63 74 75 72 65 20 74 79 70 65 2c structure type,
1c355 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
1c356 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f ** IN_INDEX_RO
1c357 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72 WID - The cursor
1c358 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 was opened on a
1c359 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e database table.
1c35a 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49 .** IN_INDEX_I
1c35b 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f NDEX - The curso
1c35c 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 r was opened on
1c35d 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 a database index
1c35e 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f ..** IN_INDEX_
1c35f 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73 EPH - The curs
1c360 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e or was opened on
1c361 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65 a specially cre
1c362 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20 ated and.**
1c363 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1c364 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d opulated epherem
1c365 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 al table..**.**
1c366 41 6e 20 65 78 69 73 74 69 6e 67 20 73 74 72 75 An existing stru
1c367 63 74 75 72 65 20 6d 61 79 20 6f 6e 6c 79 20 62 cture may only b
1c368 65 20 75 73 65 64 20 69 66 20 74 68 65 20 53 45 e used if the SE
1c369 4c 45 43 54 20 69 73 20 6f 66 20 74 68 65 20 73 LECT is of the s
1c36a 69 6d 70 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a imple.** form:.*
1c36b 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 *.** SELECT
1c36c 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 <column> FROM <t
1c36d 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 able>.**.** If p
1c36e 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 rNotFound parame
1c36f 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 74 ter is 0, then t
1c370 68 65 20 73 74 72 75 63 74 75 72 65 20 77 69 6c he structure wil
1c371 6c 20 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 l be used to ite
1c372 72 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 rate.** through
1c373 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c the set members,
1c374 20 73 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 skipping any du
1c375 70 6c 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 plicates. In thi
1c376 73 20 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 s case an.** eph
1c377 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 eremal table mus
1c378 74 20 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 t be used unless
1c379 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 the selected <c
1c37a 6f 6c 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e olumn> is guaran
1c37b 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e teed.** to be un
1c37c 69 71 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 ique - either be
1c37d 63 61 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 cause it is an I
1c37e 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
1c37f 45 59 20 6f 72 20 69 74 0a 2a 2a 20 69 73 20 75 EY or it.** is u
1c380 6e 69 71 75 65 20 62 79 20 76 69 72 74 75 65 20 nique by virtue
1c381 6f 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 of a constraint
1c382 6f 72 20 69 6d 70 6c 69 63 69 74 20 69 6e 64 65 or implicit inde
1c383 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 x..**.** If the
1c384 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d prNotFound param
1c385 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 eter is not 0, t
1c386 68 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 hen the structur
1c387 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 0a e will be used .
1c388 2a 2a 20 66 6f 72 20 66 61 73 74 20 73 65 74 20 ** for fast set
1c389 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73 membership tests
1c38a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 . In this case a
1c38b 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c n epheremal tabl
1c38c 65 20 6d 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 e must .** be us
1c38d 65 64 20 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d ed unless <colum
1c38e 6e 3e 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 n> is an INTEGER
1c38f 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 PRIMARY KEY or
1c390 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a an index can .**
1c391 20 62 65 20 66 6f 75 6e 64 20 77 69 74 68 20 3c be found with <
1c392 63 6f 6c 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c column> as its l
1c393 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e eft-most column.
1c394 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
1c395 73 74 72 75 63 74 75 72 65 20 69 73 20 62 65 69 structure is bei
1c396 6e 67 20 75 73 65 64 20 66 6f 72 20 73 65 74 20 ng used for set
1c397 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73 membership tests
1c398 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 6e 65 , the user.** ne
1c399 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 eds to know whet
1c39a 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 her or not the s
1c39b 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
1c39c 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a s an SQL NULL .*
1c39d 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 * value in order
1c39e 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 76 to correctly ev
1c39f 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69 6f aluate expressio
1c3a0 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 59 ns like "X IN (Y
1c3a1 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 65 , Z)"..** If the
1c3a2 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 re is a chance t
1c3a3 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72 hat the structur
1c3a4 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20 e may contain a
1c3a5 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a 2a NULL value at.**
1c3a6 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20 61 runtime, then a
1c3a7 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c register is all
1c3a8 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 72 ocated and the r
1c3a9 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 egister number w
1c3aa 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 ritten.** to *pr
1c3ab 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 68 65 NotFound. If the
1c3ac 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 re is no chance
1c3ad 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 that the structu
1c3ae 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a re contains a.**
1c3af 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 NULL value, the
1c3b0 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 n *prNotFound is
1c3b1 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e left unchanged.
1c3b2 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 .**.** If a regi
1c3b3 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 ster is allocate
1c3b4 64 20 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 d and its locati
1c3b5 6f 6e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 on stored in *pr
1c3b6 4e 6f 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a NotFound, then.*
1c3b7 2a 20 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 * its initial va
1c3b8 6c 75 65 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 lue is NULL. If
1c3b9 74 68 65 20 73 74 72 75 63 74 75 72 65 20 64 6f the structure do
1c3ba 65 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20 63 6f es not remain co
1c3bb 6e 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20 74 68 nstant.** for th
1c3bc 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 e duration of th
1c3bd 65 20 71 75 65 72 79 20 28 69 2e 65 2e 20 74 68 e query (i.e. th
1c3be 65 20 73 65 74 20 69 73 20 61 20 63 6f 72 72 65 e set is a corre
1c3bf 6c 61 74 65 64 20 73 75 62 2d 73 65 6c 65 63 74 lated sub-select
1c3c0 29 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 ), .** the value
1c3c1 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 of the allocate
1c3c2 64 20 72 65 67 69 73 74 65 72 20 69 73 20 72 65 d register is re
1c3c3 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68 set to NULL each
1c3c4 20 74 69 6d 65 20 74 68 65 20 0a 2a 2a 20 73 74 time the .** st
1c3c5 72 75 63 74 75 72 65 20 69 73 20 72 65 70 6f 70 ructure is repop
1c3c6 75 6c 61 74 65 64 2e 20 54 68 69 73 20 61 6c 6c ulated. This all
1c3c7 6f 77 73 20 74 68 65 20 63 61 6c 6c 65 72 20 74 ows the caller t
1c3c8 6f 20 75 73 65 20 76 64 62 65 20 63 6f 64 65 20 o use vdbe code
1c3c9 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 74 .** equivalent t
1c3ca 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a o the following:
1c3cb 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 72 65 67 .**.** if( reg
1c3cc 69 73 74 65 72 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a ister==NULL ){.*
1c3cd 2a 20 20 20 20 20 68 61 73 5f 6e 75 6c 6c 20 3d * has_null =
1c3ce 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 73 <test if data s
1c3cf 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
1c3d0 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 20 20 20 72 s null>.** r
1c3d1 65 67 69 73 74 65 72 20 3d 20 31 0a 2a 2a 20 20 egister = 1.**
1c3d2 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 6f 72 64 65 }.**.** in orde
1c3d3 72 20 74 6f 20 61 76 6f 69 64 20 72 75 6e 6e 69 r to avoid runni
1c3d4 6e 67 20 74 68 65 20 3c 74 65 73 74 20 69 66 20 ng the <test if
1c3d5 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 data structure c
1c3d6 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a ontains null>.**
1c3d7 20 74 65 73 74 20 6d 6f 72 65 20 6f 66 74 65 6e test more often
1c3d8 20 74 68 61 6e 20 69 73 20 6e 65 63 65 73 73 61 than is necessa
1c3d9 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ry..*/.#ifndef S
1c3da 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
1c3db 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ERY.SQLITE_PRIVA
1c3dc 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
1c3dd 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 ndInIndex(Parse
1c3de 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1c3df 58 2c 20 69 6e 74 20 2a 70 72 4e 6f 74 46 6f 75 X, int *prNotFou
1c3e0 6e 64 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 nd){. Select *p
1c3e1 3b 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 ;. int eType =
1c3e2 30 3b 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 0;. int iTab =
1c3e3 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a pParse->nTab++;.
1c3e4 20 20 69 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 int mustBeUniq
1c3e5 75 65 20 3d 20 21 70 72 4e 6f 74 46 6f 75 6e 64 ue = !prNotFound
1c3e6 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c ;.. /* The foll
1c3e7 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 65 78 70 wing if(...) exp
1c3e8 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 ression is true
1c3e9 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 69 73 if the SELECT is
1c3ea 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 of the . ** si
1c3eb 6d 70 6c 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a mple form:. **.
1c3ec 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 ** SELECT
1c3ed 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 <column> FROM <t
1c3ee 61 62 6c 65 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 able>. **. **
1c3ef 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 If this is the c
1c3f0 61 73 65 2c 20 69 74 20 6d 61 79 20 62 65 20 70 ase, it may be p
1c3f1 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 ossible to use a
1c3f2 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 n existing table
1c3f3 0a 20 20 2a 2a 20 6f 72 20 69 6e 64 65 78 20 69 . ** or index i
1c3f4 6e 73 74 65 61 64 20 6f 66 20 67 65 6e 65 72 61 nstead of genera
1c3f5 74 69 6e 67 20 61 6e 20 65 70 68 65 72 65 6d 61 ting an epherema
1c3f6 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 l table.. */.
1c3f7 70 20 3d 20 70 58 2d 3e 70 53 65 6c 65 63 74 3b p = pX->pSelect;
1c3f8 0a 20 20 69 66 28 20 69 73 43 61 6e 64 69 64 61 . if( isCandida
1c3f9 74 65 46 6f 72 49 6e 4f 70 74 28 70 29 20 29 7b teForInOpt(p) ){
1c3fa 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
1c3fb 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
1c3fc 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a Index *pIdx;.
1c3fd 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 Expr *pExpr
1c3fe 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 = p->pEList->a[0
1c3ff 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 6e 74 ].pExpr;. int
1c400 20 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 iCol = pExpr->i
1c401 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 56 64 62 65 Column;. Vdbe
1c402 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 *v = sqlite3Get
1c403 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 0a 20 Vdbe(pParse);..
1c404 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 /* This funct
1c405 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c ion is only call
1c406 65 64 20 66 72 6f 6d 20 74 77 6f 20 70 6c 61 63 ed from two plac
1c407 65 73 2e 20 49 6e 20 62 6f 74 68 20 63 61 73 65 es. In both case
1c408 73 20 74 68 65 20 76 64 62 65 0a 20 20 20 20 2a s the vdbe. *
1c409 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 * has already be
1c40a 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2e 20 53 6f en allocated. So
1c40b 20 61 73 73 75 6d 65 20 73 71 6c 69 74 65 33 47 assume sqlite3G
1c40c 65 74 56 64 62 65 28 29 20 69 73 20 61 6c 77 61 etVdbe() is alwa
1c40d 79 73 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 73 ys. ** succes
1c40e 73 66 75 6c 20 68 65 72 65 2e 0a 20 20 20 20 2a sful here.. *
1c40f 2f 0a 20 20 20 20 61 73 73 65 72 74 28 76 29 3b /. assert(v);
1c410 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 . if( iCol<0
1c411 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 ){. int iMe
1c412 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d m = ++pParse->nM
1c413 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 41 em;. int iA
1c414 64 64 72 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 ddr;. Table
1c415 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63 *pTab = p->pSrc
1c416 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 20 ->a[0].pTab;.
1c417 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c int iDb = sql
1c418 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 ite3SchemaToInde
1c419 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 x(db, pTab->pSch
1c41a 65 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ema);. sqli
1c41b 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 te3VdbeUsesBtree
1c41c 28 76 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 (v, iDb);..
1c41d 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 iAddr = sqlite3
1c41e 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
1c41f 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 _If, iMem);.
1c420 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1c421 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
1c422 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 r, 1, iMem);..
1c423 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 sqlite3OpenT
1c424 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 able(pParse, iTa
1c425 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 b, iDb, pTab, OP
1c426 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 _OpenRead);.
1c427 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 eType = IN_IND
1c428 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20 EX_ROWID;..
1c429 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
1c42a 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a Here(v, iAddr);.
1c42b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1c42c 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f /* The collatio
1c42d 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 n sequence used
1c42e 62 79 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f by the compariso
1c42f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 65 78 20 69 n. If an index i
1c430 73 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 62 s to . ** b
1c431 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 e used in place
1c432 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 6c 65 2c of a temp-table,
1c433 20 69 74 20 6d 75 73 74 20 62 65 20 6f 72 64 65 it must be orde
1c434 72 65 64 20 61 63 63 6f 72 64 69 6e 67 0a 20 20 red according.
1c435 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 63 ** to this c
1c436 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
1c437 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
1c438 20 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20 CollSeq *pReq
1c439 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 = sqlite3BinaryC
1c43a 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 ompareCollSeq(pP
1c43b 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c arse, pX->pLeft,
1c43c 20 70 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 pExpr);..
1c43d 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 /* Check that th
1c43e 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 e affinity that
1c43f 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20 will be used to
1c440 70 65 72 66 6f 72 6d 20 74 68 65 20 0a 20 20 20 perform the .
1c441 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e ** comparison
1c442 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
1c443 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 the affinity of
1c444 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 the column. If.
1c445 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f ** it is no
1c446 74 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 t, it is not pos
1c447 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 79 sible to use any
1c448 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f index.. */
1c449 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 . Table *pT
1c44a 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b ab = p->pSrc->a[
1c44b 30 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 63 0].pTab;. c
1c44c 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 har aff = compar
1c44d 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 58 29 isonAffinity(pX)
1c44e 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66 69 ;. int affi
1c44f 6e 69 74 79 5f 6f 6b 20 3d 20 28 70 54 61 62 2d nity_ok = (pTab-
1c450 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 >aCol[iCol].affi
1c451 6e 69 74 79 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d nity==aff||aff==
1c452 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 SQLITE_AFF_NONE)
1c453 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 ;.. for(pId
1c454 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 x=pTab->pIndex;
1c455 70 49 64 78 20 26 26 20 65 54 79 70 65 3d 3d 30 pIdx && eType==0
1c456 20 26 26 20 61 66 66 69 6e 69 74 79 5f 6f 6b 3b && affinity_ok;
1c457 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 pIdx=pIdx->pNex
1c458 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 t){. if(
1c459 28 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b (pIdx->aiColumn[
1c45a 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20 20 20 20 20 0]==iCol).
1c45b 20 20 20 26 26 20 28 70 52 65 71 3d 3d 73 71 6c && (pReq==sql
1c45c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 ite3FindCollSeq(
1c45d 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 70 49 64 db, ENC(db), pId
1c45e 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 2d 31 x->azColl[0], -1
1c45f 2c 20 30 29 29 0a 20 20 20 20 20 20 20 20 20 26 , 0)). &
1c460 26 20 28 21 6d 75 73 74 42 65 55 6e 69 71 75 65 & (!mustBeUnique
1c461 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 || (pIdx->nColu
1c462 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 6f mn==1 && pIdx->o
1c463 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 nError!=OE_None)
1c464 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 ). ){.
1c465 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a int iDb;.
1c466 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 4d int iM
1c467 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e em = ++pParse->n
1c468 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 Mem;. i
1c469 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20 nt iAddr;.
1c46a 20 20 20 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a char *pKey;.
1c46b 20 20 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65 . pKe
1c46c 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 y = (char *)sqli
1c46d 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 te3IndexKeyinfo(
1c46e 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 pParse, pIdx);.
1c46f 20 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 73 iDb = s
1c470 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
1c471 64 65 78 28 64 62 2c 20 70 49 64 78 2d 3e 70 53 dex(db, pIdx->pS
1c472 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 chema);.
1c473 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 sqlite3VdbeUse
1c474 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a sBtree(v, iDb);.
1c475 0a 20 20 20 20 20 20 20 20 20 20 69 41 64 64 72 . iAddr
1c476 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
1c477 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 69 dOp1(v, OP_If, i
1c478 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Mem);.
1c479 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1c47a 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
1c47b 20 31 2c 20 69 4d 65 6d 29 3b 0a 20 20 0a 20 20 1, iMem);. .
1c47c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1c47d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1c47e 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 SetNumColumns, 0
1c47f 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 , pIdx->nColumn)
1c480 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
1c481 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1c482 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 54 OP_OpenRead, iT
1c483 61 62 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 ab, pIdx->tnum,
1c484 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 iDb,.
1c485 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c486 20 20 20 20 70 4b 65 79 2c 50 34 5f 4b 45 59 49 pKey,P4_KEYI
1c487 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 NFO_HANDOFF);.
1c488 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d VdbeComm
1c489 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 ent((v, "%s", pI
1c48a 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 dx->zName));.
1c48b 20 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 eType = I
1c48c 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 3b 0a 0a N_INDEX_INDEX;..
1c48d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1c48e 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
1c48f 20 69 41 64 64 72 29 3b 0a 20 20 20 20 20 20 20 iAddr);.
1c490 20 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e if( prNotFoun
1c491 64 20 26 26 20 21 70 54 61 62 2d 3e 61 43 6f 6c d && !pTab->aCol
1c492 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 [iCol].notNull )
1c493 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 {. *p
1c494 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 rNotFound = ++pP
1c495 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
1c496 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1c497 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1c498 20 20 7d 0a 0a 20 20 69 66 28 20 65 54 79 70 65 }.. if( eType
1c499 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 ==0 ){. int r
1c49a 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b MayHaveNull = 0;
1c49b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f . eType = IN_
1c49c 49 4e 44 45 58 5f 45 50 48 3b 0a 20 20 20 20 69 INDEX_EPH;. i
1c49d 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 29 7b f( prNotFound ){
1c49e 0a 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 . *prNotFou
1c49f 6e 64 20 3d 20 72 4d 61 79 48 61 76 65 4e 75 6c nd = rMayHaveNul
1c4a0 6c 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d l = ++pParse->nM
1c4a1 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 em;. }else if
1c4a2 28 20 70 58 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f ( pX->pLeft->iCo
1c4a3 6c 75 6d 6e 3c 30 20 26 26 20 70 58 2d 3e 70 53 lumn<0 && pX->pS
1c4a4 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 elect==0 ){.
1c4a5 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 eType = IN_IND
1c4a6 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a EX_ROWID;. }.
1c4a7 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 53 sqlite3CodeS
1c4a8 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c ubselect(pParse,
1c4a9 20 70 58 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c pX, rMayHaveNul
1c4aa 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 l, eType==IN_IND
1c4ab 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 7d 65 6c EX_ROWID);. }el
1c4ac 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54 61 62 se{. pX->iTab
1c4ad 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d 0a 20 le = iTab;. }.
1c4ae 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b 0a 7d return eType;.}
1c4af 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 .#endif../*.** G
1c4b0 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 enerate code for
1c4b1 20 73 63 61 6c 61 72 20 73 75 62 71 75 65 72 69 scalar subqueri
1c4b2 65 73 20 75 73 65 64 20 61 73 20 61 6e 20 65 78 es used as an ex
1c4b3 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20 pression.** and
1c4b4 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20 20 45 IN operators. E
1c4b5 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 xamples:.**.**
1c4b6 20 20 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f (SELECT a FRO
1c4b7 4d 20 62 29 20 20 20 20 20 20 20 20 20 20 2d 2d M b) --
1c4b8 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 subquery.**
1c4b9 20 45 58 49 53 54 53 20 28 53 45 4c 45 43 54 20 EXISTS (SELECT
1c4ba 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d 20 45 a FROM b) -- E
1c4bb 58 49 53 54 53 20 73 75 62 71 75 65 72 79 0a 2a XISTS subquery.*
1c4bc 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c 35 2c * x IN (4,5,
1c4bd 31 31 29 20 20 20 20 20 20 20 20 20 20 20 20 20 11)
1c4be 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 -- IN operator
1c4bf 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72 69 67 with list on rig
1c4c0 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a 2a 20 ht-hand side.**
1c4c1 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 x IN (SELECT
1c4c2 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 20 2d a FROM b) -
1c4c3 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 - IN operator wi
1c4c4 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e 20 74 th subquery on t
1c4c5 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54 he right.**.** T
1c4c6 68 65 20 70 45 78 70 72 20 70 61 72 61 6d 65 74 he pExpr paramet
1c4c7 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68 65 er describes the
1c4c8 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 expression that
1c4c9 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4e contains the IN
1c4ca 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 .** operator or
1c4cb 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 subquery..**.**
1c4cc 49 66 20 70 61 72 61 6d 65 74 65 72 20 69 73 52 If parameter isR
1c4cd 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f owid is non-zero
1c4ce 2c 20 74 68 65 6e 20 65 78 70 72 65 73 73 69 6f , then expressio
1c4cf 6e 20 70 45 78 70 72 20 69 73 20 67 75 61 72 61 n pExpr is guara
1c4d0 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 6f nteed.** to be o
1c4d1 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 72 6f 77 f the form "<row
1c4d2 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20 3f 29 id> IN (?, ?, ?)
1c4d3 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 69 64 3e ", where <rowid>
1c4d4 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 0a is a reference.
1c4d5 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e 74 65 67 ** to some integ
1c4d6 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 6f 66 er key column of
1c4d7 20 61 20 74 61 62 6c 65 20 42 2d 54 72 65 65 2e a table B-Tree.
1c4d8 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 75 In this case, u
1c4d9 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 se an.** intkey
1c4da 42 2d 54 72 65 65 20 74 6f 20 73 74 6f 72 65 20 B-Tree to store
1c4db 74 68 65 20 73 65 74 20 6f 66 20 49 4e 28 2e 2e the set of IN(..
1c4dc 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 74 65 61 .) values instea
1c4dd 64 20 6f 66 20 74 68 65 20 75 73 75 61 6c 0a 2a d of the usual.*
1c4de 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 72 69 61 * (slower) varia
1c4df 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79 73 20 ble length keys
1c4e0 42 2d 54 72 65 65 2e 0a 2a 2f 0a 23 69 66 6e 64 B-Tree..*/.#ifnd
1c4e1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
1c4e2 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50 UBQUERY.SQLITE_P
1c4e3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1c4e4 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 te3CodeSubselect
1c4e5 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1c4e6 65 2c 20 0a 20 20 45 78 70 72 20 2a 70 45 78 70 e, . Expr *pExp
1c4e7 72 2c 20 0a 20 20 69 6e 74 20 72 4d 61 79 48 61 r, . int rMayHa
1c4e8 76 65 4e 75 6c 6c 2c 0a 20 20 69 6e 74 20 69 73 veNull,. int is
1c4e9 52 6f 77 69 64 0a 29 7b 0a 20 20 69 6e 74 20 74 Rowid.){. int t
1c4ea 65 73 74 41 64 64 72 20 3d 20 30 3b 20 20 20 20 estAddr = 0;
1c4eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c4ec 20 20 20 2f 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 /* One-time t
1c4ed 65 73 74 20 61 64 64 72 65 73 73 20 2a 2f 0a 20 est address */.
1c4ee 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
1c4ef 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1c4f0 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 );. if( v==0 )
1c4f1 72 65 74 75 72 6e 3b 0a 0a 0a 20 20 2f 2a 20 54 return;... /* T
1c4f2 68 69 73 20 63 6f 64 65 20 6d 75 73 74 20 62 65 his code must be
1c4f3 20 72 75 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 run in its enti
1c4f4 72 65 74 79 20 65 76 65 72 79 20 74 69 6d 65 20 rety every time
1c4f5 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 it is encountere
1c4f6 64 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 d. ** if any of
1c4f7 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
1c4f8 73 20 74 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a s true:. **. *
1c4f9 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69 67 68 * * The righ
1c4fa 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 t-hand side is a
1c4fb 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75 62 71 correlated subq
1c4fc 75 65 72 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 uery. ** *
1c4fd 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 The right-hand s
1c4fe 69 64 65 20 69 73 20 61 6e 20 65 78 70 72 65 73 ide is an expres
1c4ff 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 sion list contai
1c500 6e 69 6e 67 20 76 61 72 69 61 62 6c 65 73 0a 20 ning variables.
1c501 20 2a 2a 20 20 20 20 2a 20 20 57 65 20 61 72 65 ** * We are
1c502 20 69 6e 73 69 64 65 20 61 20 74 72 69 67 67 65 inside a trigge
1c503 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 r. **. ** If a
1c504 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 ll of the above
1c505 61 72 65 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 are false, then
1c506 77 65 20 63 61 6e 20 72 75 6e 20 74 68 69 73 20 we can run this
1c507 63 6f 64 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 code just once.
1c508 20 2a 2a 20 73 61 76 65 20 74 68 65 20 72 65 73 ** save the res
1c509 75 6c 74 73 2c 20 61 6e 64 20 72 65 75 73 65 20 ults, and reuse
1c50a 74 68 65 20 73 61 6d 65 20 72 65 73 75 6c 74 20 the same result
1c50b 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e on subsequent in
1c50c 76 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a vocations.. */.
1c50d 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e if( !ExprHasAn
1c50e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c yProperty(pExpr,
1c50f 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 20 26 EP_VarSelect) &
1c510 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 & !pParse->trigS
1c511 74 61 63 6b 20 29 7b 0a 20 20 20 20 69 6e 74 20 tack ){. int
1c512 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e mem = ++pParse->
1c513 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 nMem;. sqlite
1c514 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
1c515 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 P_If, mem);.
1c516 74 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74 testAddr = sqlit
1c517 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1c518 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d OP_Integer, 1, m
1c519 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 em);. assert(
1c51a 20 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70 testAddr>0 || p
1c51b 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
1c51c 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a cFailed );. }..
1c51d 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d switch( pExpr-
1c51e 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 >op ){. case
1c51f 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63 TK_IN: {. c
1c520 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 har affinity;.
1c521 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49 KeyInfo keyI
1c522 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 nfo;. int a
1c523 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ddr; /* A
1c524 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 ddress of OP_Ope
1c525 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 nEphemeral instr
1c526 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 uction */.
1c527 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 Expr *pLeft = pE
1c528 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 xpr->pLeft;..
1c529 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e if( rMayHaveN
1c52a 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ull ){. s
1c52b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1c52c 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 (v, OP_Null, 0,
1c52d 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 rMayHaveNull);.
1c52e 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66 }.. af
1c52f 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 finity = sqlite3
1c530 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 ExprAffinity(pLe
1c531 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 ft);.. /* W
1c532 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 hether this is a
1c533 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e n 'x IN(SELECT..
1c534 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28 .)' or an 'x IN(
1c535 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20 <exprlist>)'.
1c536 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ** expression
1c537 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74 it is handled t
1c538 68 65 20 73 61 6d 65 20 77 61 79 2e 20 41 20 76 he same way. A v
1c539 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 irtual table is
1c53a 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64 . ** filled
1c53b 20 77 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65 with single-fie
1c53c 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65 ld index keys re
1c53d 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72 presenting the r
1c53e 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 esults. **
1c53f 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20 from the SELECT
1c540 6f 72 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74 or the <exprlist
1c541 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 >.. **.
1c542 20 20 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20 ** If the 'x'
1c543 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 expression is a
1c544 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72 column value, or
1c545 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20 the SELECT....
1c546 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e ** statemen
1c547 74 20 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75 t returns a colu
1c548 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 mn value, then t
1c549 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 he affinity of t
1c54a 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c hat. ** col
1c54b 75 6d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62 umn is used to b
1c54c 75 69 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b uild the index k
1c54d 65 79 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27 eys. If both 'x'
1c54e 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a and the. *
1c54f 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 * SELECT... stat
1c550 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e ement are column
1c551 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 s, then numeric
1c552 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 affinity is used
1c553 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74 . ** if eit
1c554 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e her column has N
1c555 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45 UMERIC or INTEGE
1c556 52 20 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e R affinity. If n
1c557 65 69 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 either. **
1c558 27 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45 'x' nor the SELE
1c559 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 CT... statement
1c55a 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 are columns, the
1c55b 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 n numeric affini
1c55c 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75 ty. ** is u
1c55d 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 sed.. */.
1c55e 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
1c55f 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 e = pParse->nTab
1c560 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d ++;. addr =
1c561 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1c562 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 p2(v, OP_OpenEph
1c563 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69 emeral, pExpr->i
1c564 54 61 62 6c 65 2c 20 21 69 73 52 6f 77 69 64 29 Table, !isRowid)
1c565 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 ;. memset(&
1c566 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 keyInfo, 0, size
1c567 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20 of(keyInfo));.
1c568 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65 keyInfo.nFie
1c569 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 ld = 1;.. i
1c56a 66 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 f( pExpr->pSelec
1c56b 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 t ){. /*
1c56c 43 61 73 65 20 31 3a 20 20 20 20 20 65 78 70 72 Case 1: expr
1c56d 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 IN (SELECT ...)
1c56e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
1c56f 20 20 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 20 ** Generate
1c570 63 6f 64 65 20 74 6f 20 77 72 69 74 65 20 74 68 code to write th
1c571 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 e results of the
1c572 20 73 65 6c 65 63 74 20 69 6e 74 6f 20 74 68 65 select into the
1c573 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 20 temporary.
1c574 20 20 20 2a 2a 20 74 61 62 6c 65 20 61 6c 6c 6f ** table allo
1c575 63 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 cated and opened
1c576 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 above..
1c577 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63 */. Selec
1c578 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 tDest dest;.
1c579 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 ExprList *pE
1c57a 4c 69 73 74 3b 0a 0a 20 20 20 20 20 20 20 20 61 List;.. a
1c57b 73 73 65 72 74 28 20 21 69 73 52 6f 77 69 64 20 ssert( !isRowid
1c57c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1c57d 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 e3SelectDestInit
1c57e 28 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74 2c (&dest, SRT_Set,
1c57f 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b pExpr->iTable);
1c580 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 61 66 . dest.af
1c581 66 69 6e 69 74 79 20 3d 20 28 69 6e 74 29 61 66 finity = (int)af
1c582 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 finity;.
1c583 61 73 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e assert( (pExpr->
1c584 69 54 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 iTable&0x0000FFF
1c585 46 29 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c F)==pExpr->iTabl
1c586 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e );. if(
1c587 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 sqlite3Select(p
1c588 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 53 Parse, pExpr->pS
1c589 65 6c 65 63 74 2c 20 26 64 65 73 74 29 20 29 7b elect, &dest) ){
1c58a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
1c58b 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 n;. }.
1c58c 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 pEList = pE
1c58d 78 70 72 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 45 xpr->pSelect->pE
1c58e 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 List;. if
1c58f 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69 ( pEList && pELi
1c590 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29 7b 20 0a st->nExpr>0 ){ .
1c591 20 20 20 20 20 20 20 20 20 20 6b 65 79 49 6e 66 keyInf
1c592 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c o.aColl[0] = sql
1c593 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 ite3BinaryCompar
1c594 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c eCollSeq(pParse,
1c595 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 0a 20 pExpr->pLeft,.
1c596 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 4c pEL
1c597 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 ist->a[0].pExpr)
1c598 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1c599 20 20 7d 65 6c 73 65 20 69 66 28 20 70 45 78 70 }else if( pExp
1c59a 72 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 r->pList ){.
1c59b 20 20 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 /* Case 2:
1c59c 20 20 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 expr IN (expr
1c59d 6c 69 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a list). **
1c59e 0a 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 . ** For
1c59f 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c each expression,
1c5a0 20 62 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 build an index
1c5a1 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 key from the eva
1c5a2 6c 75 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 luation and.
1c5a3 20 20 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 ** store it
1c5a4 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 in the temporary
1c5a5 20 74 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 table. If <expr
1c5a6 3e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 > is a column, t
1c5a7 68 65 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 hen use.
1c5a8 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 ** that columns
1c5a9 61 66 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 affinity when bu
1c5aa 69 6c 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 ilding index key
1c5ab 73 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 s. If <expr> is
1c5ac 6e 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 not. ** a
1c5ad 20 63 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d column, use num
1c5ae 65 72 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 eric affinity..
1c5af 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1c5b0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 int i;.
1c5b1 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
1c5b2 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b = pExpr->pList;
1c5b3 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 . struct
1c5b4 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
1c5b5 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e Item;. in
1c5b6 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20 t r1, r2, r3;..
1c5b7 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 69 if( !affi
1c5b8 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20 nity ){.
1c5b9 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c affinity = SQL
1c5ba 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 ITE_AFF_NONE;.
1c5bb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1c5bc 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d keyInfo.aColl[0]
1c5bd 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1c5be 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 llSeq(pParse, pE
1c5bf 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 xpr->pLeft);..
1c5c0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 /* Loop th
1c5c1 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65 rough each expre
1c5c2 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69 ssion in <exprli
1c5c3 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 st>. */.
1c5c4 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r1 = sqlite3GetT
1c5c5 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
1c5c6 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c r2 = sql
1c5c7 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
1c5c8 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
1c5c9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1c5ca 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
1c5cb 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 6f r2);. fo
1c5cc 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 r(i=pList->nExpr
1c5cd 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 , pItem=pList->a
1c5ce 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 ; i>0; i--, pIte
1c5cf 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 m++){.
1c5d0 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65 Expr *pE2 = pIte
1c5d1 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 m->pExpr;..
1c5d2 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 /* If the e
1c5d3 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 xpression is not
1c5d4 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77 constant then w
1c5d5 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20 e will need to.
1c5d6 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61 ** disa
1c5d7 62 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61 ble the test tha
1c5d8 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 t was generated
1c5d9 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73 above that makes
1c5da 20 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20 sure.
1c5db 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c ** this code onl
1c5dc 79 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e y executes once.
1c5dd 20 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20 Because for a
1c5de 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20 non-constant.
1c5df 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 ** expres
1c5e0 73 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 sion we need to
1c5e1 72 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 rerun this code
1c5e2 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20 each time..
1c5e3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1c5e4 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 26 if( testAddr &
1c5e5 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 & !sqlite3ExprIs
1c5e6 43 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b Constant(pE2) ){
1c5e7 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
1c5e8 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f ite3VdbeChangeTo
1c5e9 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72 Noop(v, testAddr
1c5ea 2d 31 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 -1, 2);.
1c5eb 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 30 testAddr = 0
1c5ec 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 ;. }..
1c5ed 20 20 20 20 20 20 20 20 20 2f 2a 20 45 76 61 6c /* Eval
1c5ee 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73 uate the express
1c5ef 69 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74 20 69 ion and insert i
1c5f0 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 20 t into the temp
1c5f1 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 table */.
1c5f2 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 pParse->disab
1c5f3 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 leColCache++;.
1c5f4 20 20 20 20 20 20 20 20 72 33 20 3d 20 73 71 6c r3 = sql
1c5f5 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 ite3ExprCodeTarg
1c5f6 65 74 28 70 50 61 72 73 65 2c 20 70 45 32 2c 20 et(pParse, pE2,
1c5f7 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 r1);. a
1c5f8 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 ssert( pParse->d
1c5f9 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 isableColCache>0
1c5fa 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 );. pP
1c5fb 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1c5fc 43 61 63 68 65 2d 2d 3b 0a 0a 20 20 20 20 20 20 Cache--;..
1c5fd 20 20 20 20 69 66 28 20 69 73 52 6f 77 69 64 20 if( isRowid
1c5fe 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
1c5ff 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1c600 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 (v, OP_MustBeInt
1c601 2c 20 72 33 2c 20 73 71 6c 69 74 65 33 56 64 62 , r3, sqlite3Vdb
1c602 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b eCurrentAddr(v)+
1c603 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2);.
1c604 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1c605 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 3(v, OP_Insert,
1c606 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 pExpr->iTable, r
1c607 32 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 20 20 2, r3);.
1c608 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1c609 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1c60a 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b AddOp4(v, OP_Mak
1c60b 65 52 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 eRecord, r3, 1,
1c60c 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 r2, &affinity, 1
1c60d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 );. s
1c60e 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 qlite3ExprCacheA
1c60f 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 ffinityChange(pP
1c610 61 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 arse, r3, 1);.
1c611 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1c612 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1c613 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 78 P_IdxInsert, pEx
1c614 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b pr->iTable, r2);
1c615 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1c616 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 }. s
1c617 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
1c618 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 pReg(pParse, r1)
1c619 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1c61a 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1c61b 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 pParse, r2);.
1c61c 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 }. if( !
1c61d 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 isRowid ){.
1c61e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1c61f 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 angeP4(v, addr,
1c620 28 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f (void *)&keyInfo
1c621 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 , P4_KEYINFO);.
1c622 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
1c623 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 ak;. }.. c
1c624 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 ase TK_EXISTS:.
1c625 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 case TK_SELEC
1c626 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 T: {. /* Th
1c627 69 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 is has to be a s
1c628 63 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 calar SELECT. G
1c629 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
1c62a 70 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a put the. **
1c62b 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 value of this s
1c62c 65 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 elect in a memor
1c62d 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 y cell and recor
1c62e 64 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 d the number.
1c62f 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d ** of the mem
1c630 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c ory cell in iCol
1c631 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 umn.. */.
1c632 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
1c633 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 28 Token one = { (
1c634 75 38 2a 29 22 31 22 2c 20 30 2c 20 31 20 7d 3b u8*)"1", 0, 1 };
1c635 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 . Select *p
1c636 53 65 6c 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 Sel;. Selec
1c637 74 44 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 20 tDest dest;..
1c638 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70 72 2d pSel = pExpr-
1c639 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 >pSelect;.
1c63a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 sqlite3SelectDes
1c63b 74 49 6e 69 74 28 26 64 65 73 74 2c 20 30 2c 20 tInit(&dest, 0,
1c63c 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b ++pParse->nMem);
1c63d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 . if( pExpr
1c63e 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 ->op==TK_SELECT
1c63f 29 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e ){. dest.
1c640 65 44 65 73 74 20 3d 20 53 52 54 5f 4d 65 6d 3b eDest = SRT_Mem;
1c641 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1c642 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1c643 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 74 2e 69 _Null, 0, dest.i
1c644 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 Parm);. V
1c645 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
1c646 49 6e 69 74 20 73 75 62 71 75 65 72 79 20 72 65 Init subquery re
1c647 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d sult"));. }
1c648 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 65 else{. de
1c649 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 45 st.eDest = SRT_E
1c64a 78 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20 73 xists;. s
1c64b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1c64c 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
1c64d 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 0, dest.iParm);.
1c64e 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d VdbeComm
1c64f 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 45 58 ent((v, "Init EX
1c650 49 53 54 53 20 72 65 73 75 6c 74 22 29 29 3b 0a ISTS result"));.
1c651 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
1c652 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1c653 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c pParse->db, pSel
1c654 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 ->pLimit);.
1c655 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20 pSel->pLimit =
1c656 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 sqlite3PExpr(pPa
1c657 72 73 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c rse, TK_INTEGER,
1c658 20 30 2c 20 30 2c 20 26 6f 6e 65 29 3b 0a 20 20 0, 0, &one);.
1c659 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1c65a 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 elect(pParse, pS
1c65b 65 6c 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 el, &dest) ){.
1c65c 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
1c65d 20 20 20 20 7d 0a 20 20 20 20 20 20 70 45 78 70 }. pExp
1c65e 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 64 65 73 r->iColumn = des
1c65f 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 62 t.iParm;. b
1c660 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1c661 0a 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 . if( testAddr
1c662 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1c663 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 65 beJumpHere(v, te
1c664 73 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d 0a 0a stAddr-1);. }..
1c665 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 return;.}.#end
1c666 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1c667 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f T_SUBQUERY */../
1c668 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 61 *.** Duplicate a
1c669 6e 20 38 2d 62 79 74 65 20 76 61 6c 75 65 0a 2a n 8-byte value.*
1c66a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 /.static char *d
1c66b 75 70 38 62 79 74 65 73 28 56 64 62 65 20 2a 76 up8bytes(Vdbe *v
1c66c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e , const char *in
1c66d 29 7b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d ){. char *out =
1c66e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1c66f 52 61 77 28 73 71 6c 69 74 65 33 56 64 62 65 44 Raw(sqlite3VdbeD
1c670 62 28 76 29 2c 20 38 29 3b 0a 20 20 69 66 28 20 b(v), 8);. if(
1c671 6f 75 74 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 out ){. memcp
1c672 79 28 6f 75 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 y(out, in, 8);.
1c673 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b }. return out;
1c674 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
1c675 74 65 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f te an instructio
1c676 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 75 74 20 n that will put
1c677 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 the floating poi
1c678 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 64 65 73 63 nt.** value desc
1c679 72 69 62 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d ribed by z[0..n-
1c67a 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 1] into register
1c67b 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 iMem..**.** The
1c67c 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c z[] string will
1c67d 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 probably not be
1c67e 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
1c67f 2e 20 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a . But the .** z
1c680 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 20 69 73 [n] character is
1c681 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 guaranteed to b
1c682 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 e something that
1c683 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a does not look.*
1c684 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 * like the conti
1c685 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e nuation of the n
1c686 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 umber..*/.static
1c687 20 76 6f 69 64 20 63 6f 64 65 52 65 61 6c 28 56 void codeReal(V
1c688 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 dbe *v, const ch
1c689 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e ar *z, int n, in
1c68a 74 20 6e 65 67 61 74 65 46 6c 61 67 2c 20 69 6e t negateFlag, in
1c68b 74 20 69 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 t iMem){. asser
1c68c 74 28 20 7a 20 7c 7c 20 76 3d 3d 30 20 7c 7c 20 t( z || v==0 ||
1c68d 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 sqlite3VdbeDb(v)
1c68e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1c68f 3b 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 ;. if( z ){.
1c690 20 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20 double value;.
1c691 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 char *zV;.
1c692 20 61 73 73 65 72 74 28 20 21 69 73 64 69 67 69 assert( !isdigi
1c693 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20 20 20 73 t(z[n]) );. s
1c694 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20 26 76 qlite3AtoF(z, &v
1c695 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20 73 alue);. if( s
1c696 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 qlite3IsNaN(valu
1c697 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 e) ){. sqli
1c698 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1c699 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 OP_Null, 0, iMe
1c69a 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 m);. }else{.
1c69b 20 20 20 20 20 69 66 28 20 6e 65 67 61 74 65 46 if( negateF
1c69c 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 lag ) value = -v
1c69d 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d alue;. zV =
1c69e 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 dup8bytes(v, (c
1c69f 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 har*)&value);.
1c6a0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1c6a1 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61 6c ddOp4(v, OP_Real
1c6a2 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 , 0, iMem, 0, zV
1c6a3 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 20 20 , P4_REAL);.
1c6a4 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 }. }.}.../*.**
1c6a5 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74 Generate an inst
1c6a6 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c ruction that wil
1c6a7 6c 20 70 75 74 20 74 68 65 20 69 6e 74 65 67 65 l put the intege
1c6a8 72 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a 2a r describe by.**
1c6a9 20 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 text z[0..n-1]
1c6aa 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d into register iM
1c6ab 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b em..**.** The z[
1c6ac 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 ] string will pr
1c6ad 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 obably not be ze
1c6ae 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 ro-terminated.
1c6af 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d But the .** z[n]
1c6b0 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75 character is gu
1c6b1 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 aranteed to be s
1c6b2 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f omething that do
1c6b3 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c es not look.** l
1c6b4 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 ike the continua
1c6b5 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 tion of the numb
1c6b6 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f er..*/.static vo
1c6b7 69 64 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56 id codeInteger(V
1c6b8 64 62 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 dbe *v, Expr *pE
1c6b9 78 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 xpr, int negFlag
1c6ba 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 63 , int iMem){. c
1c6bb 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 onst char *z;.
1c6bc 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 if( pExpr->flags
1c6bd 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 & EP_IntValue )
1c6be 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 45 {. int i = pE
1c6bf 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 xpr->iTable;.
1c6c0 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69 if( negFlag ) i
1c6c1 20 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 = -i;. sqlit
1c6c2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1c6c3 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 OP_Integer, i, i
1c6c4 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Mem);. }else if
1c6c5 28 20 28 7a 20 3d 20 28 63 68 61 72 2a 29 70 45 ( (z = (char*)pE
1c6c6 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 29 21 3d 30 xpr->token.z)!=0
1c6c7 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
1c6c8 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 int n = pExpr
1c6c9 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 61 ->token.n;. a
1c6ca 73 73 65 72 74 28 20 21 69 73 64 69 67 69 74 28 ssert( !isdigit(
1c6cb 7a 5b 6e 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 z[n]) );. if(
1c6cc 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 sqlite3GetInt32
1c6cd 28 7a 2c 20 26 69 29 20 29 7b 0a 20 20 20 20 20 (z, &i) ){.
1c6ce 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69 if( negFlag ) i
1c6cf 20 3d 20 2d 69 3b 0a 20 20 20 20 20 20 73 71 6c = -i;. sql
1c6d0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1c6d1 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c , OP_Integer, i,
1c6d2 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 iMem);. }els
1c6d3 65 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 74 e if( sqlite3Fit
1c6d4 73 49 6e 36 34 42 69 74 73 28 7a 2c 20 6e 65 67 sIn64Bits(z, neg
1c6d5 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20 69 Flag) ){. i
1c6d6 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 64 value;.
1c6d7 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 20 20 char *zV;.
1c6d8 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c sqlite3Atoi64(z,
1c6d9 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 &value);.
1c6da 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 76 61 if( negFlag ) va
1c6db 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 lue = -value;.
1c6dc 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 zV = dup8byt
1c6dd 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 es(v, (char*)&va
1c6de 6c 75 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 lue);. sqli
1c6df 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1c6e0 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 69 4d OP_Int64, 0, iM
1c6e1 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f 49 4e em, 0, zV, P4_IN
1c6e2 54 36 34 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b T64);. }else{
1c6e3 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 . codeReal(
1c6e4 76 2c 20 7a 2c 20 6e 2c 20 6e 65 67 46 6c 61 67 v, z, n, negFlag
1c6e5 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 , iMem);. }.
1c6e6 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}.../*.** Gen
1c6e7 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 erate code that
1c6e8 77 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68 65 will extract the
1c6e9 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 iColumn-th colu
1c6ea 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 mn from.** table
1c6eb 20 70 54 61 62 20 61 6e 64 20 73 74 6f 72 65 20 pTab and store
1c6ec 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 the column value
1c6ed 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e 20 in a register.
1c6ee 20 41 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69 73 An effort.** is
1c6ef 20 6d 61 64 65 20 74 6f 20 73 74 6f 72 65 20 74 made to store t
1c6f0 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 he column value
1c6f1 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 67 in register iReg
1c6f2 2c 20 62 75 74 20 74 68 69 73 20 69 73 0a 2a 2a , but this is.**
1c6f3 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e not guaranteed.
1c6f4 20 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f The location o
1c6f5 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c f the column val
1c6f6 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a ue is returned..
1c6f7 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 **.** There must
1c6f8 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 be an open curs
1c6f9 6f 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69 54 or to pTab in iT
1c6fa 61 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 able when this r
1c6fb 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c outine.** is cal
1c6fc 6c 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e led. If iColumn
1c6fd 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20 <0 then code is
1c6fe 67 65 6e 65 72 61 74 65 64 20 74 68 61 74 20 65 generated that e
1c6ff 78 74 72 61 63 74 73 20 74 68 65 20 72 6f 77 69 xtracts the rowi
1c700 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f d..**.** This ro
1c701 75 74 69 6e 65 20 6d 69 67 68 74 20 61 74 74 65 utine might atte
1c702 6d 70 74 20 74 6f 20 72 65 75 73 65 20 74 68 65 mpt to reuse the
1c703 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f value of the co
1c704 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 lumn that.** has
1c705 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c 6f already been lo
1c706 61 64 65 64 20 69 6e 74 6f 20 61 20 72 65 67 69 aded into a regi
1c707 73 74 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 ster. The value
1c708 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 will always.**
1c709 62 65 20 75 73 65 64 20 69 66 20 69 74 20 68 61 be used if it ha
1c70a 73 20 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65 20 s not undergone
1c70b 61 6e 79 20 61 66 66 69 6e 69 74 79 20 63 68 61 any affinity cha
1c70c 6e 67 65 73 2e 20 20 42 75 74 20 69 66 0a 2a 2a nges. But if.**
1c70d 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 an affinity cha
1c70e 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 nge has occurred
1c70f 2c 20 74 68 65 6e 20 74 68 65 20 63 61 63 68 65 , then the cache
1c710 64 20 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c d value will onl
1c711 79 20 62 65 0a 2a 2a 20 75 73 65 64 20 69 66 20 y be.** used if
1c712 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 69 73 20 allowAffChng is
1c713 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f true..*/.SQLITE_
1c714 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1c715 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f te3ExprCodeGetCo
1c716 6c 75 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 lumn(. Parse *p
1c717 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 Parse, /* Pars
1c718 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e ing and code gen
1c719 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 erating context
1c71a 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 */. Table *pTab
1c71b 2c 20 20 20 20 20 2f 2a 20 44 65 73 63 72 69 70 , /* Descrip
1c71c 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 6c tion of the tabl
1c71d 65 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 e we are reading
1c71e 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 from */. int i
1c71f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 Column, /* I
1c720 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c ndex of the tabl
1c721 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e e column */. in
1c722 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 2f t iTable, /
1c723 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 * The cursor poi
1c724 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 74 61 62 nting to the tab
1c725 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 le */. int iReg
1c726 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 , /* Stor
1c727 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a e results here *
1c728 2f 0a 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66 66 /. int allowAff
1c729 43 68 6e 67 20 2f 2a 20 54 72 75 65 20 69 66 20 Chng /* True if
1c72a 70 72 69 6f 72 20 61 66 66 69 6e 69 74 79 20 63 prior affinity c
1c72b 68 61 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a 2f hanges are OK */
1c72c 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 .){. Vdbe *v =
1c72d 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
1c72e 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
1c72f 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a yColCache *p;..
1c730 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 for(i=0, p=pPa
1c731 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 rse->aColCache;
1c732 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 i<pParse->nColCa
1c733 63 68 65 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a che; i++, p++){.
1c734 20 20 20 20 69 66 28 20 70 2d 3e 69 54 61 62 6c if( p->iTabl
1c735 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 2d 3e e==iTable && p->
1c736 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e iColumn==iColumn
1c737 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 . && (
1c738 21 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 7c 7c !p->affChange ||
1c739 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 29 20 29 allowAffChng) )
1c73a 7b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 73 71 {.#if 0. sq
1c73b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
1c73c 76 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 20 v, OP_Noop);.
1c73d 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 VdbeComment((
1c73e 76 2c 20 22 4f 50 54 3a 20 74 61 62 25 64 2e 63 v, "OPT: tab%d.c
1c73f 6f 6c 25 64 20 2d 3e 20 72 25 64 22 2c 20 69 54 ol%d -> r%d", iT
1c740 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70 able, iColumn, p
1c741 2d 3e 69 52 65 67 29 29 3b 0a 23 65 6e 64 69 66 ->iReg));.#endif
1c742 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d . return p-
1c743 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d >iReg;. }. }
1c744 20 20 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d . assert( v!=
1c745 30 20 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 0 );. if( iColu
1c746 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 mn<0 ){. int
1c747 6f 70 20 3d 20 28 70 54 61 62 20 26 26 20 49 73 op = (pTab && Is
1c748 56 69 72 74 75 61 6c 28 70 54 61 62 29 29 20 3f Virtual(pTab)) ?
1c749 20 4f 50 5f 56 52 6f 77 69 64 20 3a 20 4f 50 5f OP_VRowid : OP_
1c74a 52 6f 77 69 64 3b 0a 20 20 20 20 73 71 6c 69 74 Rowid;. sqlit
1c74b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1c74c 6f 70 2c 20 69 54 61 62 6c 65 2c 20 69 52 65 67 op, iTable, iReg
1c74d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 );. }else if( p
1c74e 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 Tab==0 ){. sq
1c74f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1c750 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 v, OP_Column, iT
1c751 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 able, iColumn, i
1c752 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Reg);. }else{.
1c753 20 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69 int op = IsVi
1c754 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 rtual(pTab) ? OP
1c755 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f _VColumn : OP_Co
1c756 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 lumn;. sqlite
1c757 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 3VdbeAddOp3(v, o
1c758 70 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 p, iTable, iColu
1c759 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 mn, iReg);. s
1c75a 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 qlite3ColumnDefa
1c75b 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f ult(v, pTab, iCo
1c75c 6c 75 6d 6e 29 3b 0a 23 69 66 6e 64 65 66 20 53 lumn);.#ifndef S
1c75d 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
1c75e 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 69 66 ING_POINT. if
1c75f 28 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f ( pTab->aCol[iCo
1c760 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d lumn].affinity==
1c761 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 SQLITE_AFF_REAL
1c762 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1c763 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
1c764 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20 69 _RealAffinity, i
1c765 52 65 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 Reg);. }.#end
1c766 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 if. }. if( pPa
1c767 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1c768 61 63 68 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 ache==0 ){. i
1c769 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c 43 = pParse->iColC
1c76a 61 63 68 65 3b 0a 20 20 20 20 70 20 3d 20 26 70 ache;. p = &p
1c76b 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
1c76c 5b 69 5d 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 [i];. p->iTab
1c76d 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 le = iTable;.
1c76e 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 p->iColumn = iC
1c76f 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 2d 3e 69 52 olumn;. p->iR
1c770 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 70 eg = iReg;. p
1c771 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b ->affChange = 0;
1c772 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 69 66 . i++;. if
1c773 28 20 69 3e 3d 41 72 72 61 79 53 69 7a 65 28 70 ( i>=ArraySize(p
1c774 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
1c775 29 20 29 20 69 20 3d 20 30 3b 0a 20 20 20 20 69 ) ) i = 0;. i
1c776 66 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 43 6f f( i>pParse->nCo
1c777 6c 43 61 63 68 65 20 29 20 70 50 61 72 73 65 2d lCache ) pParse-
1c778 3e 6e 43 6f 6c 43 61 63 68 65 20 3d 20 69 3b 0a >nColCache = i;.
1c779 20 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c pParse->iCol
1c77a 43 61 63 68 65 20 3d 20 69 3b 0a 20 20 7d 0a 20 Cache = i;. }.
1c77b 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a return iReg;.}.
1c77c 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c 6c ./*.** Clear all
1c77d 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e column cache en
1c77e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 tries associated
1c77f 20 77 69 74 68 20 74 68 65 20 76 64 62 65 0a 2a with the vdbe.*
1c780 2a 20 63 75 72 73 6f 72 20 77 69 74 68 20 63 75 * cursor with cu
1c781 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 54 61 62 rsor number iTab
1c782 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
1c783 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1c784 65 33 45 78 70 72 43 6c 65 61 72 43 6f 6c 75 6d e3ExprClearColum
1c785 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 nCache(Parse *pP
1c786 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65 arse, int iTable
1c787 29 7b 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3c ){. if( iTable<
1c788 30 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 0 ){. pParse-
1c789 3e 6e 43 6f 6c 43 61 63 68 65 20 3d 20 30 3b 0a >nColCache = 0;.
1c78a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c pParse->iCol
1c78b 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 65 6c Cache = 0;. }el
1c78c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 se{. int i;.
1c78d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 for(i=0; i<pP
1c78e 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b arse->nColCache;
1c78f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
1c790 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 pParse->aColCac
1c791 68 65 5b 69 5d 2e 69 54 61 62 6c 65 3d 3d 69 54 he[i].iTable==iT
1c792 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 able ){.
1c793 74 65 73 74 63 61 73 65 28 20 69 3d 3d 70 50 61 testcase( i==pPa
1c794 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 2d 31 rse->nColCache-1
1c795 20 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 );. pPar
1c796 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d se->aColCache[i]
1c797 20 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 = pParse->aColC
1c798 61 63 68 65 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e ache[--pParse->n
1c799 43 6f 6c 43 61 63 68 65 5d 3b 0a 20 20 20 20 20 ColCache];.
1c79a 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c 43 pParse->iColC
1c79b 61 63 68 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e ache = pParse->n
1c79c 43 6f 6c 43 61 63 68 65 3b 0a 20 20 20 20 20 20 ColCache;.
1c79d 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f }. }. }.}../
1c79e 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 *.** Record the
1c79f 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 66 66 fact that an aff
1c7a0 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 inity change has
1c7a1 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43 6f occurred on iCo
1c7a2 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 unt.** registers
1c7a3 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 69 starting with i
1c7a4 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Start..*/.SQLITE
1c7a5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1c7a6 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 lite3ExprCacheAf
1c7a7 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 finityChange(Par
1c7a8 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1c7a9 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75 iStart, int iCou
1c7aa 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 20 nt){. int iEnd
1c7ab 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75 6e = iStart + iCoun
1c7ac 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b 0a t - 1;. int i;.
1c7ad 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
1c7ae 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 rse->nColCache;
1c7af 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 i++){. int r
1c7b0 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 = pParse->aColCa
1c7b1 63 68 65 5b 69 5d 2e 69 52 65 67 3b 0a 20 20 20 che[i].iReg;.
1c7b2 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20 26 if( r>=iStart &
1c7b3 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20 & r<=iEnd ){.
1c7b4 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 pParse->aColC
1c7b5 61 63 68 65 5b 69 5d 2e 61 66 66 43 68 61 6e 67 ache[i].affChang
1c7b6 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d e = 1;. }. }
1c7b7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
1c7b8 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 te code to move
1c7b9 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 content from reg
1c7ba 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 isters iFrom...i
1c7bb 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f From+nReg-1.** o
1c7bc 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b ver to iTo..iTo+
1c7bd 6e 52 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 65 nReg-1. Keep the
1c7be 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 70 column cache up
1c7bf 2d 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c -to-date..*/.SQL
1c7c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1c7c1 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1c7c2 4d 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 72 Move(Parse *pPar
1c7c3 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 se, int iFrom, i
1c7c4 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 nt iTo, int nReg
1c7c5 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 ){. int i;. if
1c7c6 28 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29 20 72 ( iFrom==iTo ) r
1c7c7 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 eturn;. sqlite3
1c7c8 56 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 VdbeAddOp3(pPars
1c7c9 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 e->pVdbe, OP_Mov
1c7ca 65 2c 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e e, iFrom, iTo, n
1c7cb 52 65 67 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b Reg);. for(i=0;
1c7cc 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 i<pParse->nColC
1c7cd 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 ache; i++){.
1c7ce 69 6e 74 20 78 20 3d 20 70 50 61 72 73 65 2d 3e int x = pParse->
1c7cf 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 aColCache[i].iRe
1c7d0 67 3b 0a 20 20 20 20 69 66 28 20 78 3e 3d 69 46 g;. if( x>=iF
1c7d1 72 6f 6d 20 26 26 20 78 3c 69 46 72 6f 6d 2b 6e rom && x<iFrom+n
1c7d2 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70 50 61 Reg ){. pPa
1c7d3 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 rse->aColCache[i
1c7d4 5d 2e 69 52 65 67 20 2b 3d 20 69 54 6f 2d 69 46 ].iReg += iTo-iF
1c7d5 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d rom;. }. }.}
1c7d6 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1c7d7 20 63 6f 64 65 20 74 6f 20 63 6f 70 79 20 63 6f code to copy co
1c7d8 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 ntent from regis
1c7d9 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 ters iFrom...iFr
1c7da 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 om+nReg-1.** ove
1c7db 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 r to iTo..iTo+nR
1c7dc 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eg-1..*/.SQLITE_
1c7dd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1c7de 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 ite3ExprCodeCopy
1c7df 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1c7e0 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 int iFrom, int i
1c7e1 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 To, int nReg){.
1c7e2 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 69 46 int i;. if( iF
1c7e3 72 6f 6d 3d 3d 69 54 6f 20 29 20 72 65 74 75 72 rom==iTo ) retur
1c7e4 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c n;. for(i=0; i<
1c7e5 6e 52 65 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nReg; i++){.
1c7e6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1c7e7 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 2(pParse->pVdbe,
1c7e8 20 4f 50 5f 43 6f 70 79 2c 20 69 46 72 6f 6d 2b OP_Copy, iFrom+
1c7e9 69 2c 20 69 54 6f 2b 69 29 3b 0a 20 20 7d 0a 7d i, iTo+i);. }.}
1c7ea 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1c7eb 72 75 65 20 69 66 20 61 6e 79 20 72 65 67 69 73 rue if any regis
1c7ec 74 65 72 20 69 6e 20 74 68 65 20 72 61 6e 67 65 ter in the range
1c7ed 20 69 46 72 6f 6d 2e 2e 69 54 6f 20 28 69 6e 63 iFrom..iTo (inc
1c7ee 6c 75 73 69 76 65 29 0a 2a 2a 20 69 73 20 75 73 lusive).** is us
1c7ef 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 ed as part of th
1c7f0 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a e column cache..
1c7f1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 73 */.static int us
1c7f2 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 edAsColumnCache(
1c7f3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
1c7f4 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 nt iFrom, int iT
1c7f5 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 o){. int i;. f
1c7f6 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 or(i=0; i<pParse
1c7f7 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b ->nColCache; i++
1c7f8 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 3d 20 70 ){. int r = p
1c7f9 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
1c7fa 5b 69 5d 2e 69 52 65 67 3b 0a 20 20 20 20 69 66 [i].iReg;. if
1c7fb 28 20 72 3e 3d 69 46 72 6f 6d 20 26 26 20 72 3c ( r>=iFrom && r<
1c7fc 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 20 31 3b =iTo ) return 1;
1c7fd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
1c7fe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 73 .}../*.** Theres
1c7ff 20 69 73 20 61 20 76 61 6c 75 65 20 69 6e 20 72 is a value in r
1c800 65 67 69 73 74 65 72 20 69 43 75 72 72 65 6e 74 egister iCurrent
1c801 2e 20 20 57 65 20 75 6c 74 69 6d 61 74 65 6c 79 . We ultimately
1c802 20 77 61 6e 74 0a 2a 2a 20 74 68 65 20 76 61 6c want.** the val
1c803 75 65 20 74 6f 20 62 65 20 69 6e 20 72 65 67 69 ue to be in regi
1c804 73 74 65 72 20 69 54 61 72 67 65 74 2e 20 20 49 ster iTarget. I
1c805 74 20 6d 69 67 68 74 20 62 65 20 74 68 61 74 0a t might be that.
1c806 2a 2a 20 69 43 75 72 72 65 6e 74 20 61 6e 64 20 ** iCurrent and
1c807 69 54 61 72 67 65 74 20 61 72 65 20 74 68 65 20 iTarget are the
1c808 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e 0a 2a same register..*
1c809 2a 0a 2a 2a 20 57 65 20 61 72 65 20 67 6f 69 6e *.** We are goin
1c80a 67 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 g to modify the
1c80b 76 61 6c 75 65 2c 20 73 6f 20 77 65 20 6e 65 65 value, so we nee
1c80c 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 d to make sure i
1c80d 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20 63 61 t.** is not a ca
1c80e 63 68 65 64 20 72 65 67 69 73 74 65 72 2e 20 20 ched register.
1c80f 49 66 20 69 43 75 72 72 65 6e 74 20 69 73 20 61 If iCurrent is a
1c810 20 63 61 63 68 65 64 20 72 65 67 69 73 74 65 72 cached register
1c811 2c 0a 2a 2a 20 74 68 65 6e 20 74 72 79 20 74 6f ,.** then try to
1c812 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 move the value
1c813 6f 76 65 72 20 74 6f 20 69 54 61 72 67 65 74 2e over to iTarget.
1c814 20 20 49 66 20 69 54 61 72 67 65 74 20 69 73 20 If iTarget is
1c815 61 0a 2a 2a 20 63 61 63 68 65 64 20 72 65 67 69 a.** cached regi
1c816 73 74 65 72 2c 20 74 68 65 6e 20 63 6c 65 61 72 ster, then clear
1c817 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1c818 6e 67 20 63 61 63 68 65 20 6c 69 6e 65 2e 0a 2a ng cache line..*
1c819 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1c81a 72 65 67 69 73 74 65 72 20 74 68 61 74 20 74 68 register that th
1c81b 65 20 76 61 6c 75 65 20 65 6e 64 73 20 75 70 20 e value ends up
1c81c 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 in..*/.SQLITE_PR
1c81d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1c81e 33 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 3ExprWritableReg
1c81f 69 73 74 65 72 28 50 61 72 73 65 20 2a 70 50 61 ister(Parse *pPa
1c820 72 73 65 2c 20 69 6e 74 20 69 43 75 72 72 65 6e rse, int iCurren
1c821 74 2c 20 69 6e 74 20 69 54 61 72 67 65 74 29 7b t, int iTarget){
1c822 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 . int i;. asse
1c823 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 rt( pParse->pVdb
1c824 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 75 e!=0 );. if( !u
1c825 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 sedAsColumnCache
1c826 28 70 50 61 72 73 65 2c 20 69 43 75 72 72 65 6e (pParse, iCurren
1c827 74 2c 20 69 43 75 72 72 65 6e 74 29 20 29 7b 0a t, iCurrent) ){.
1c828 20 20 20 20 72 65 74 75 72 6e 20 69 43 75 72 72 return iCurr
1c829 65 6e 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 ent;. }. if( i
1c82a 43 75 72 72 65 6e 74 21 3d 69 54 61 72 67 65 74 Current!=iTarget
1c82b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1c82c 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 dbeAddOp2(pParse
1c82d 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70 ->pVdbe, OP_SCop
1c82e 79 2c 20 69 43 75 72 72 65 6e 74 2c 20 69 54 61 y, iCurrent, iTa
1c82f 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 rget);. }. for
1c830 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e (i=0; i<pParse->
1c831 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b nColCache; i++){
1c832 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d . if( pParse-
1c833 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 >aColCache[i].iR
1c834 65 67 3d 3d 69 54 61 72 67 65 74 20 29 7b 0a 20 eg==iTarget ){.
1c835 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f pParse->aCo
1c836 6c 43 61 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 lCache[i] = pPar
1c837 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d se->aColCache[--
1c838 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 pParse->nColCach
1c839 65 5d 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 e];. pParse
1c83a 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 ->iColCache = pP
1c83b 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b arse->nColCache;
1c83c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1c83d 75 72 6e 20 69 54 61 72 67 65 74 3b 0a 7d 0a 0a urn iTarget;.}..
1c83e 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 /*.** If the las
1c83f 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f t instruction co
1c840 64 65 64 20 69 73 20 61 6e 20 65 70 68 65 6d 65 ded is an epheme
1c841 72 61 6c 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 ral copy of any
1c842 6f 66 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 of.** the regist
1c843 65 72 73 20 69 6e 20 74 68 65 20 6e 52 65 67 20 ers in the nReg
1c844 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e registers beginn
1c845 69 6e 67 20 77 69 74 68 20 69 52 65 67 2c 20 74 ing with iReg, t
1c846 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 hen.** convert t
1c847 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 he last instruct
1c848 69 6f 6e 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 ion from OP_SCop
1c849 79 20 74 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f y to OP_Copy..*/
1c84a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c84b 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1c84c 48 61 72 64 43 6f 70 79 28 50 61 72 73 65 20 2a HardCopy(Parse *
1c84d 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 pParse, int iReg
1c84e 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 , int nReg){. i
1c84f 6e 74 20 61 64 64 72 3b 0a 20 20 56 64 62 65 4f nt addr;. VdbeO
1c850 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a p *pOp;. Vdbe *
1c851 76 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 65 v;.. v = pParse
1c852 2d 3e 70 56 64 62 65 3b 0a 20 20 61 64 64 72 20 ->pVdbe;. addr
1c853 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 = sqlite3VdbeCur
1c854 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 70 rentAddr(v);. p
1c855 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 Op = sqlite3Vdbe
1c856 47 65 74 4f 70 28 76 2c 20 61 64 64 72 2d 31 29 GetOp(v, addr-1)
1c857 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 20 ;. assert( pOp
1c858 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d || pParse->db->m
1c859 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
1c85a 20 69 66 28 20 70 4f 70 20 26 26 20 70 4f 70 2d if( pOp && pOp-
1c85b 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70 >opcode==OP_SCop
1c85c 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e 3d 69 52 y && pOp->p1>=iR
1c85d 65 67 20 26 26 20 70 4f 70 2d 3e 70 31 3c 69 52 eg && pOp->p1<iR
1c85e 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 70 eg+nReg ){. p
1c85f 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f Op->opcode = OP_
1c860 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a Copy;. }.}../*.
1c861 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1c862 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76 61 to store the va
1c863 6c 75 65 20 6f 66 20 74 68 65 20 69 41 6c 69 61 lue of the iAlia
1c864 73 2d 74 68 20 61 6c 69 61 73 20 69 6e 20 72 65 s-th alias in re
1c865 67 69 73 74 65 72 0a 2a 2a 20 74 61 72 67 65 74 gister.** target
1c866 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d . The first tim
1c867 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 e this is called
1c868 2c 20 70 45 78 70 72 20 69 73 20 65 76 61 6c 75 , pExpr is evalu
1c869 61 74 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 0a ated to compute.
1c86a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
1c86b 74 68 65 20 61 6c 69 61 73 2e 20 20 54 68 65 20 the alias. The
1c86c 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 value is stored
1c86d 69 6e 20 61 6e 20 61 75 78 69 6c 69 61 72 79 20 in an auxiliary
1c86e 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64 20 register.** and
1c86f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 the number of th
1c870 61 74 20 72 65 67 69 73 74 65 72 20 69 73 20 72 at register is r
1c871 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20 73 75 62 eturned. On sub
1c872 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 2c 0a 2a sequent calls,.*
1c873 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e * the register n
1c874 75 6d 62 65 72 20 69 73 20 72 65 74 75 72 6e 65 umber is returne
1c875 64 20 77 69 74 68 6f 75 74 20 67 65 6e 65 72 61 d without genera
1c876 74 69 6e 67 20 61 6e 79 20 63 6f 64 65 2e 0a 2a ting any code..*
1c877 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 *.** Note that i
1c878 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73 n order for this
1c879 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64 65 20 6d to work, code m
1c87a 75 73 74 20 62 65 20 67 65 6e 65 72 61 74 65 64 ust be generated
1c87b 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 in the.** same
1c87c 6f 72 64 65 72 20 74 68 61 74 20 69 74 20 69 73 order that it is
1c87d 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a executed..**.**
1c87e 20 41 6c 69 61 73 65 73 20 61 72 65 20 6e 75 6d Aliases are num
1c87f 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 77 bered starting w
1c880 69 74 68 20 31 2e 20 20 53 6f 20 69 41 6c 69 61 ith 1. So iAlia
1c881 73 20 69 73 20 69 6e 20 74 68 65 20 72 61 6e 67 s is in the rang
1c882 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20 70 50 61 e.** of 1 to pPa
1c883 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69 6e 63 6c rse->nAlias incl
1c884 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 70 usive. .**.** p
1c885 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 Parse->aAlias[iA
1c886 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72 64 73 20 lias-1] records
1c887 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d the register num
1c888 62 65 72 20 77 68 65 72 65 20 74 68 65 20 76 61 ber where the va
1c889 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 69 41 lue.** of the iA
1c88a 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 73 lias-th alias is
1c88b 20 73 74 6f 72 65 64 2e 20 20 49 66 20 7a 65 72 stored. If zer
1c88c 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 o, that means th
1c88d 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 at the.** alias
1c88e 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
1c88f 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f 0a 73 74 computed..*/.st
1c890 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c 69 atic int codeAli
1c891 61 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 as(Parse *pParse
1c892 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c 20 45 78 , int iAlias, Ex
1c893 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 pr *pExpr, int t
1c894 61 72 67 65 74 29 7b 0a 20 20 73 71 6c 69 74 65 arget){. sqlite
1c895 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1c896 64 62 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b 0a db;. int iReg;.
1c897 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 41 if( pParse->aA
1c898 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 lias==0 ){. p
1c899 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 20 3d 20 Parse->aAlias =
1c89a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
1c89b 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 20 ero(db, .
1c89c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c89d 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 sizeof
1c89e 28 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b (pParse->aAlias[
1c89f 30 5d 29 2a 70 50 61 72 73 65 2d 3e 6e 41 6c 69 0])*pParse->nAli
1c8a0 61 73 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 as );. if( db
1c8a1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1c8a2 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
1c8a3 20 61 73 73 65 72 74 28 20 69 41 6c 69 61 73 3e assert( iAlias>
1c8a4 30 20 26 26 20 69 41 6c 69 61 73 3c 3d 70 50 61 0 && iAlias<=pPa
1c8a5 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 rse->nAlias );.
1c8a6 20 69 52 65 67 20 3d 20 70 50 61 72 73 65 2d 3e iReg = pParse->
1c8a7 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d aAlias[iAlias-1]
1c8a8 3b 0a 20 20 69 66 28 20 69 52 65 67 3d 3d 30 20 ;. if( iReg==0
1c8a9 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 ){. if( pPars
1c8aa 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 e->disableColCac
1c8ab 68 65 20 29 7b 0a 20 20 20 20 20 20 69 52 65 67 he ){. iReg
1c8ac 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1c8ad 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c deTarget(pParse,
1c8ae 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b pExpr, target);
1c8af 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1c8b0 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 iReg = ++pPars
1c8b1 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 e->nMem;. s
1c8b2 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
1c8b3 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 69 52 Parse, pExpr, iR
1c8b4 65 67 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 eg);. pPars
1c8b5 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 e->aAlias[iAlias
1c8b6 2d 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 -1] = iReg;.
1c8b7 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 }. }. return i
1c8b8 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 Reg;.}../*.** Ge
1c8b9 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f nerate code into
1c8ba 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62 the current Vdb
1c8bb 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 e to evaluate th
1c8bc 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 e given.** expre
1c8bd 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 ssion. Attempt
1c8be 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 to store the res
1c8bf 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 ults in register
1c8c0 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65 "target"..** Re
1c8c1 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 turn the registe
1c8c2 72 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 r where results
1c8c3 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a are stored..**.*
1c8c4 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f 75 74 * With this rout
1c8c5 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f ine, there is no
1c8c6 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
1c8c7 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 results will.**
1c8c8 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61 72 be stored in tar
1c8c9 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 get. The result
1c8ca 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65 64 might be stored
1c8cb 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a in some other.*
1c8cc 2a 20 72 65 67 69 73 74 65 72 20 69 66 20 69 74 * register if it
1c8cd 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 is convenient t
1c8ce 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 63 61 o do so. The ca
1c8cf 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a lling function.*
1c8d0 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 * must check the
1c8d1 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 return code and
1c8d2 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75 6c 74 move the result
1c8d3 73 20 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 s to the desired
1c8d4 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f .** register..*/
1c8d5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c8d6 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
1c8d7 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 65 20 odeTarget(Parse
1c8d8 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1c8d9 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 Expr, int target
1c8da 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 ){. Vdbe *v = p
1c8db 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f Parse->pVdbe; /
1c8dc 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72 20 63 * The VM under c
1c8dd 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 onstruction */.
1c8de 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 int op;
1c8df 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1c8e0 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 20 63 e opcode being c
1c8e1 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e oded */. int in
1c8e2 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20 20 20 Reg = target;
1c8e3 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 73 /* Results s
1c8e4 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1c8e5 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74 r inReg */. int
1c8e6 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20 20 regFree1 = 0;
1c8e7 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e /* If non
1c8e8 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 20 -zero free this
1c8e9 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
1c8ea 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 er */. int regF
1c8eb 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20 20 20 ree2 = 0;
1c8ec 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f /* If non-zero
1c8ed 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f free this tempo
1c8ee 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f rary register */
1c8ef 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20 72 . int r1, r2, r
1c8f0 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f 2a 20 3, r4; /*
1c8f1 56 61 72 69 6f 75 73 20 72 65 67 69 73 74 65 72 Various register
1c8f2 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71 numbers */. sq
1c8f3 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 64 62 lite3 *db;.. db
1c8f4 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
1c8f5 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 7c 7c assert( v!=0 ||
1c8f6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1c8f7 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 d );. assert( t
1c8f8 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 65 arget>0 && targe
1c8f9 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 t<=pParse->nMem
1c8fa 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 );. if( v==0 )
1c8fb 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 return 0;.. if(
1c8fc 20 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 pExpr==0 ){.
1c8fd 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 op = TK_NULL;.
1c8fe 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70 20 3d }else{. op =
1c8ff 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 7d 0a pExpr->op;. }.
1c900 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
1c901 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f case TK_AGG_
1c902 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 COLUMN: {.
1c903 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 AggInfo *pAggInf
1c904 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 o = pExpr->pAggI
1c905 6e 66 6f 3b 0a 20 20 20 20 20 20 73 74 72 75 63 nfo;. struc
1c906 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 t AggInfo_col *p
1c907 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d Col = &pAggInfo-
1c908 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67 >aCol[pExpr->iAg
1c909 67 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 g];. if( !p
1c90a 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d AggInfo->directM
1c90b 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ode ){. a
1c90c 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69 4d 65 ssert( pCol->iMe
1c90d 6d 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 m>0 );. i
1c90e 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e 69 4d 65 nReg = pCol->iMe
1c90f 6d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b m;. break
1c910 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
1c911 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73 65 53 ( pAggInfo->useS
1c912 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a 20 20 20 ortingIdx ){.
1c913 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1c914 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c AddOp3(v, OP_Col
1c915 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73 umn, pAggInfo->s
1c916 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 20 20 ortingIdx,.
1c917 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c918 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 pCol->i
1c919 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c 20 74 61 SorterColumn, ta
1c91a 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 62 rget);. b
1c91b 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1c91c 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 /* Otherwise
1c91d 2c 20 66 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f , fall thru into
1c91e 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63 the TK_COLUMN c
1c91f 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 ase */. }.
1c920 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a case TK_COLUMN:
1c921 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 {. if( pEx
1c922 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 7b 0a pr->iTable<0 ){.
1c923 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
1c924 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 only happens whe
1c925 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 6b 20 63 n coding check c
1c926 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 onstraints */.
1c927 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1c928 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e 30 20 29 arse->ckBase>0 )
1c929 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 ;. inReg
1c92a 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e = pExpr->iColumn
1c92b 20 2b 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 + pParse->ckBas
1c92c 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a e;. }else{.
1c92d 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
1c92e 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 ( (pExpr->flags
1c92f 26 20 45 50 5f 41 6e 79 41 66 66 29 21 3d 30 20 & EP_AnyAff)!=0
1c930 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 );. inReg
1c931 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1c932 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 deGetColumn(pPar
1c933 73 65 2c 20 70 45 78 70 72 2d 3e 70 54 61 62 2c se, pExpr->pTab,
1c934 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1c935 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c936 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e pExpr->iColumn
1c937 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c , pExpr->iTable,
1c938 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 target,.
1c939 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c93a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1c93b 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41 >flags & EP_AnyA
1c93c 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ff);. }.
1c93d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1c93e 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 case TK_INTE
1c93f 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 GER: {. cod
1c940 65 49 6e 74 65 67 65 72 28 76 2c 20 70 45 78 70 eInteger(v, pExp
1c941 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 r, 0, target);.
1c942 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1c943 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c }. case TK_FL
1c944 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 OAT: {. cod
1c945 65 52 65 61 6c 28 76 2c 20 28 63 68 61 72 2a 29 eReal(v, (char*)
1c946 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 pExpr->token.z,
1c947 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 pExpr->token.n,
1c948 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
1c949 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1c94a 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e case TK_STRIN
1c94b 47 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 G: {. sqlit
1c94c 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 64 62 e3DequoteExpr(db
1c94d 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 , pExpr);.
1c94e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1c94f 34 28 76 2c 4f 50 5f 53 74 72 69 6e 67 38 2c 20 4(v,OP_String8,
1c950 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 0a 20 20 0, target, 0,.
1c951 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c952 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 45 78 (char*)pEx
1c953 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 pr->token.z, pEx
1c954 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 pr->token.n);.
1c955 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1c956 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c . case TK_NUL
1c957 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 L: {. sqlit
1c958 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1c959 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 OP_Null, 0, targ
1c95a 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b et);. break
1c95b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
1c95c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 SQLITE_OMIT_BLOB
1c95d 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 _LITERAL. cas
1c95e 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 e TK_BLOB: {.
1c95f 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 int n;.
1c960 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 const char *z;.
1c961 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 char *zBlob
1c962 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1c963 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d pExpr->token.n>=
1c964 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 3 );. asser
1c965 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e t( pExpr->token.
1c966 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 z[0]=='x' || pEx
1c967 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d pr->token.z[0]==
1c968 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 'X' );. ass
1c969 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 ert( pExpr->toke
1c96a 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a n.z[1]=='\'' );.
1c96b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
1c96c 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78 xpr->token.z[pEx
1c96d 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d pr->token.n-1]==
1c96e 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 6e 20 '\'' );. n
1c96f 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e = pExpr->token.n
1c970 20 2d 20 33 3b 0a 20 20 20 20 20 20 7a 20 3d 20 - 3;. z =
1c971 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f (char*)pExpr->to
1c972 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 20 ken.z + 2;.
1c973 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 zBlob = sqlite3
1c974 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 HexToBlob(sqlite
1c975 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 6e 3VdbeDb(v), z, n
1c976 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1c977 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1c978 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 67 _Blob, n/2, targ
1c979 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 34 et, 0, zBlob, P4
1c97a 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 _DYNAMIC);.
1c97b 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
1c97c 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b ndif. case TK
1c97d 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20 _VARIABLE: {.
1c97e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1c97f 64 4f 70 32 28 76 2c 20 4f 50 5f 56 61 72 69 61 dOp2(v, OP_Varia
1c980 62 6c 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 ble, pExpr->iTab
1c981 6c 65 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 le, target);.
1c982 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f if( pExpr->to
1c983 6b 65 6e 2e 6e 3e 31 20 29 7b 0a 20 20 20 20 20 ken.n>1 ){.
1c984 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1c985 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 angeP4(v, -1, (c
1c986 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 har*)pExpr->toke
1c987 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 n.z, pExpr->toke
1c988 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n.n);. }.
1c989 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1c98a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47 . case TK_REG
1c98b 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 ISTER: {. i
1c98c 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54 nReg = pExpr->iT
1c98d 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 61 able;. brea
1c98e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1c98f 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20 e TK_AS: {.
1c990 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 inReg = codeAli
1c991 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 as(pParse, pExpr
1c992 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d ->iTable, pExpr-
1c993 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b >pLeft, target);
1c994 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1c995 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
1c996 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20 TE_OMIT_CAST.
1c997 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b case TK_CAST: {
1c998 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 . /* Expres
1c999 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 sions of the for
1c99a 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74 20 m: CAST(pLeft
1c99b 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20 AS token) */.
1c99c 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f int aff, to_o
1c99d 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d p;. inReg =
1c99e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1c99f 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 Target(pParse, p
1c9a0 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 Expr->pLeft, tar
1c9a1 67 65 74 29 3b 0a 20 20 20 20 20 20 61 66 66 20 get);. aff
1c9a2 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 = sqlite3Affinit
1c9a3 79 54 79 70 65 28 26 70 45 78 70 72 2d 3e 74 6f yType(&pExpr->to
1c9a4 6b 65 6e 29 3b 0a 20 20 20 20 20 20 74 6f 5f 6f ken);. to_o
1c9a5 70 20 3d 20 61 66 66 20 2d 20 53 51 4c 49 54 45 p = aff - SQLITE
1c9a6 5f 41 46 46 5f 54 45 58 54 20 2b 20 4f 50 5f 54 _AFF_TEXT + OP_T
1c9a7 6f 54 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73 oText;. ass
1c9a8 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 ert( to_op==OP_T
1c9a9 6f 54 65 78 74 20 20 20 20 7c 7c 20 61 66 66 21 oText || aff!
1c9aa 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 =SQLITE_AFF_TEXT
1c9ab 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 );. ass
1c9ac 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 ert( to_op==OP_T
1c9ad 6f 42 6c 6f 62 20 20 20 20 7c 7c 20 61 66 66 21 oBlob || aff!
1c9ae 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 =SQLITE_AFF_NONE
1c9af 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 );. ass
1c9b0 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 ert( to_op==OP_T
1c9b1 6f 4e 75 6d 65 72 69 63 20 7c 7c 20 61 66 66 21 oNumeric || aff!
1c9b2 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 =SQLITE_AFF_NUME
1c9b3 52 49 43 20 29 3b 0a 20 20 20 20 20 20 61 73 73 RIC );. ass
1c9b4 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 ert( to_op==OP_T
1c9b5 6f 49 6e 74 20 20 20 20 20 7c 7c 20 61 66 66 21 oInt || aff!
1c9b6 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 =SQLITE_AFF_INTE
1c9b7 47 45 52 20 29 3b 0a 20 20 20 20 20 20 61 73 73 GER );. ass
1c9b8 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 ert( to_op==OP_T
1c9b9 6f 52 65 61 6c 20 20 20 20 7c 7c 20 61 66 66 21 oReal || aff!
1c9ba 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c =SQLITE_AFF_REAL
1c9bb 20 20 20 20 29 3b 0a 20 20 20 20 20 20 74 65 73 );. tes
1c9bc 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 tcase( to_op==OP
1c9bd 5f 54 6f 54 65 78 74 20 29 3b 0a 20 20 20 20 20 _ToText );.
1c9be 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 testcase( to_op
1c9bf 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b 0a 20 ==OP_ToBlob );.
1c9c0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 testcase( t
1c9c1 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 o_op==OP_ToNumer
1c9c2 69 63 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ic );. test
1c9c3 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f case( to_op==OP_
1c9c4 54 6f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 74 ToInt );. t
1c9c5 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d estcase( to_op==
1c9c6 4f 50 5f 54 6f 52 65 61 6c 20 29 3b 0a 20 20 20 OP_ToReal );.
1c9c7 20 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61 if( inReg!=ta
1c9c8 72 67 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 rget ){.
1c9c9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1c9ca 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 2(v, OP_SCopy, i
1c9cb 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20 nReg, target);.
1c9cc 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 inReg = t
1c9cd 61 72 67 65 74 3b 0a 20 20 20 20 20 20 7d 0a 20 arget;. }.
1c9ce 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1c9cf 41 64 64 4f 70 31 28 76 2c 20 74 6f 5f 6f 70 2c AddOp1(v, to_op,
1c9d0 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 74 inReg);. t
1c9d1 65 73 74 63 61 73 65 28 20 75 73 65 64 41 73 43 estcase( usedAsC
1c9d2 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 olumnCache(pPars
1c9d3 65 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65 67 29 e, inReg, inReg)
1c9d4 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
1c9d5 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 3ExprCacheAffini
1c9d6 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c tyChange(pParse,
1c9d7 20 69 6e 52 65 67 2c 20 31 29 3b 0a 20 20 20 20 inReg, 1);.
1c9d8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1c9d9 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1c9da 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 20 20 20 OMIT_CAST */.
1c9db 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 case TK_LT:.
1c9dc 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 case TK_LE:.
1c9dd 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 case TK_GT:.
1c9de 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 case TK_GE:.
1c9df 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 case TK_NE:.
1c9e0 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 case TK_EQ: {.
1c9e1 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1c9e2 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 LT==OP_Lt );.
1c9e3 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 assert( TK_LE
1c9e4 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 ==OP_Le );.
1c9e5 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d assert( TK_GT==
1c9e6 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 OP_Gt );. a
1c9e7 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 ssert( TK_GE==OP
1c9e8 5f 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 _Ge );. ass
1c9e9 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 ert( TK_EQ==OP_E
1c9ea 71 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 q );. asser
1c9eb 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 t( TK_NE==OP_Ne
1c9ec 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1c9ed 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a e( op==TK_LT );.
1c9ee 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1c9ef 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 op==TK_LE );.
1c9f0 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1c9f1 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 =TK_GT );.
1c9f2 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1c9f3 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _GE );. tes
1c9f4 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 tcase( op==TK_EQ
1c9f5 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1c9f6 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b se( op==TK_NE );
1c9f7 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 . codeCompa
1c9f8 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 reOperands(pPars
1c9f9 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
1c9fa 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c &r1, ®Free1,
1c9fb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1c9fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c9fd 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 pExpr->pRight
1c9fe 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 , &r2, ®Free2
1c9ff 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d );. codeCom
1ca00 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 pare(pParse, pEx
1ca01 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 pr->pLeft, pExpr
1ca02 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 ->pRight, op,.
1ca03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ca04 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53 r1, r2, inReg, S
1ca05 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a QLITE_STOREP2);.
1ca06 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ca07 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
1ca08 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1ca09 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 egFree2==0 );.
1ca0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1ca0b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 . case TK_AND
1ca0c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 :. case TK_OR
1ca0d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c :. case TK_PL
1ca0e 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f US:. case TK_
1ca0f 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54 STAR:. case T
1ca10 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73 K_MINUS:. cas
1ca11 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61 e TK_REM:. ca
1ca12 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20 se TK_BITAND:.
1ca13 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a case TK_BITOR:
1ca14 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41 . case TK_SLA
1ca15 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f SH:. case TK_
1ca16 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65 LSHIFT:. case
1ca17 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20 TK_RSHIFT: .
1ca18 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a case TK_CONCAT:
1ca19 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
1ca1a 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20 TK_AND==OP_And
1ca1b 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1ca1c 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b TK_OR==OP_Or );
1ca1d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
1ca1e 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29 K_PLUS==OP_Add )
1ca1f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ca20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62 TK_MINUS==OP_Sub
1ca21 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61 tract );. a
1ca22 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f ssert( TK_REM==O
1ca23 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20 P_Remainder );.
1ca24 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1ca25 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e BITAND==OP_BitAn
1ca26 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 d );. asser
1ca27 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f t( TK_BITOR==OP_
1ca28 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 BitOr );. a
1ca29 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d ssert( TK_SLASH=
1ca2a 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20 =OP_Divide );.
1ca2b 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c assert( TK_L
1ca2c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c SHIFT==OP_ShiftL
1ca2d 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 eft );. ass
1ca2e 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d ert( TK_RSHIFT==
1ca2f 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b OP_ShiftRight );
1ca30 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
1ca31 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e K_CONCAT==OP_Con
1ca32 63 61 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 cat );. tes
1ca33 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 41 4e tcase( op==TK_AN
1ca34 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 D );. testc
1ca35 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 20 29 ase( op==TK_OR )
1ca36 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1ca37 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 29 3b ( op==TK_PLUS );
1ca38 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1ca39 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 29 3b op==TK_MINUS );
1ca3a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1ca3b 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b 0a 20 op==TK_REM );.
1ca3c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1ca3d 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29 3b 0a p==TK_BITAND );.
1ca3e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ca3f 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 3b 0a op==TK_BITOR );.
1ca40 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ca41 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 3b 0a op==TK_SLASH );.
1ca42 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ca43 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 29 3b op==TK_LSHIFT );
1ca44 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1ca45 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54 20 29 op==TK_RSHIFT )
1ca46 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1ca47 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20 ( op==TK_CONCAT
1ca48 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 );. r1 = sq
1ca49 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
1ca4a 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d p(pParse, pExpr-
1ca4b 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 >pLeft, ®Free
1ca4c 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 1);. r2 = s
1ca4d 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
1ca4e 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 mp(pParse, pExpr
1ca4f 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 ->pRight, ®Fr
1ca50 65 65 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ee2);. sqli
1ca51 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
1ca52 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 op, r2, r1, tar
1ca53 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 get);. test
1ca54 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1ca55 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
1ca56 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
1ca57 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
1ca58 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1ca59 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 TK_UMINUS: {.
1ca5a 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d Expr *pLeft =
1ca5b 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 pExpr->pLeft;.
1ca5c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 assert( pLe
1ca5d 66 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ft );. if(
1ca5e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c pLeft->op==TK_FL
1ca5f 4f 41 54 20 7c 7c 20 70 4c 65 66 74 2d 3e 6f 70 OAT || pLeft->op
1ca60 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a ==TK_INTEGER ){.
1ca61 20 20 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 if( pLef
1ca62 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 t->op==TK_FLOAT
1ca63 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 64 ){. cod
1ca64 65 52 65 61 6c 28 76 2c 20 28 63 68 61 72 2a 29 eReal(v, (char*)
1ca65 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 pLeft->token.z,
1ca66 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 pLeft->token.n,
1ca67 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 1, target);.
1ca68 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ca69 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 codeInteger
1ca6a 28 76 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 61 (v, pLeft, 1, ta
1ca6b 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d rget);. }
1ca6c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1ca6d 20 20 20 20 20 20 72 65 67 46 72 65 65 31 20 3d regFree1 =
1ca6e 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 r1 = sqlite3Get
1ca6f 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b TempReg(pParse);
1ca70 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1ca71 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1ca72 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 31 29 _Integer, 0, r1)
1ca73 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 ;. r2 = s
1ca74 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
1ca75 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 mp(pParse, pExpr
1ca76 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 ->pLeft, ®Fre
1ca77 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c e2);. sql
1ca78 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1ca79 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72 , OP_Subtract, r
1ca7a 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 2, r1, target);.
1ca7b 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
1ca7c 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b ( regFree2==0 );
1ca7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1ca7e 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 nReg = target;.
1ca7f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1ca80 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 }. case TK_BI
1ca81 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 TNOT:. case T
1ca82 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 61 K_NOT: {. a
1ca83 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e 4f 54 ssert( TK_BITNOT
1ca84 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b 0a 20 ==OP_BitNot );.
1ca85 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1ca86 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20 NOT==OP_Not );.
1ca87 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1ca88 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 3b 0a p==TK_BITNOT );.
1ca89 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ca8a 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 20 op==TK_NOT );.
1ca8b 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 inReg = sqli
1ca8c 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 te3ExprCodeTarge
1ca8d 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d t(pParse, pExpr-
1ca8e 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b >pLeft, target);
1ca8f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1ca90 20 69 6e 52 65 67 3d 3d 74 61 72 67 65 74 20 29 inReg==target )
1ca91 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1ca92 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 ( usedAsColumnCa
1ca93 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 che(pParse, inRe
1ca94 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 g, inReg) );.
1ca95 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 inReg = sqlit
1ca96 65 33 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 e3ExprWritableRe
1ca97 67 69 73 74 65 72 28 70 50 61 72 73 65 2c 20 69 gister(pParse, i
1ca98 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20 nReg, target);.
1ca99 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1ca9a 41 64 64 4f 70 31 28 76 2c 20 6f 70 2c 20 69 6e AddOp1(v, op, in
1ca9b 52 65 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 Reg);. brea
1ca9c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1ca9d 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 e TK_ISNULL:.
1ca9e 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c case TK_NOTNULL
1ca9f 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 : {. int ad
1caa0 64 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 dr;. assert
1caa1 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f ( TK_ISNULL==OP_
1caa2 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 IsNull );.
1caa3 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 assert( TK_NOTNU
1caa4 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 LL==OP_NotNull )
1caa5 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1caa6 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 ( op==TK_ISNULL
1caa7 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1caa8 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c e( op==TK_NOTNUL
1caa9 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 L );. sqlit
1caaa 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1caab 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 OP_Integer, 1, t
1caac 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31 arget);. r1
1caad 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1caae 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
1caaf 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 Expr->pLeft, &re
1cab0 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 gFree1);. t
1cab1 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
1cab2 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64 1==0 );. ad
1cab3 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
1cab4 41 64 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 AddOp1(v, op, r1
1cab5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1cab6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1cab7 5f 41 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c _AddImm, target,
1cab8 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 -1);. sqli
1cab9 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
1caba 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 v, addr);.
1cabb 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1cabc 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e case TK_AGG_FUN
1cabd 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 CTION: {. A
1cabe 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 ggInfo *pInfo =
1cabf 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b pExpr->pAggInfo;
1cac0 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f . if( pInfo
1cac1 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
1cac2 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1cac3 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f Parse, "misuse o
1cac4 66 20 61 67 67 72 65 67 61 74 65 3a 20 25 54 22 f aggregate: %T"
1cac5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 70 ,. &p
1cac6 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 Expr->span);.
1cac7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1cac8 20 20 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d inReg = pInfo-
1cac9 3e 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 >aFunc[pExpr->iA
1caca 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 gg].iMem;.
1cacb 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1cacc 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
1cacd 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 _CONST_FUNC:.
1cace 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f case TK_FUNCTIO
1cacf 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c N: {. ExprL
1cad0 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 ist *pList = pEx
1cad1 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20 pr->pList;.
1cad2 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70 4c 69 int nExpr = pLi
1cad3 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 st ? pList->nExp
1cad4 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 46 75 6e r : 0;. Fun
1cad5 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 20 20 cDef *pDef;.
1cad6 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20 int nId;.
1cad7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 const char *zId
1cad8 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 ;. int cons
1cad9 74 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 20 tMask = 0;.
1cada 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 75 38 int i;. u8
1cadb 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a enc = ENC(db);.
1cadc 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 CollSeq *p
1cadd 43 6f 6c 6c 20 3d 20 30 3b 0a 0a 20 20 20 20 20 Coll = 0;..
1cade 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1cadf 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a K_CONST_FUNC );.
1cae0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1cae1 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 op==TK_FUNCTION
1cae2 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 );. zId = (
1cae3 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b char*)pExpr->tok
1cae4 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 en.z;. nId
1cae5 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e = pExpr->token.n
1cae6 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 ;. pDef = s
1cae7 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 qlite3FindFuncti
1cae8 6f 6e 28 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c on(db, zId, nId,
1cae9 20 6e 45 78 70 72 2c 20 65 6e 63 2c 20 30 29 3b nExpr, enc, 0);
1caea 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1caeb 44 65 66 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 Def!=0 );.
1caec 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 if( pList ){.
1caed 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70 4c 69 nExpr = pLi
1caee 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 st->nExpr;.
1caef 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 r1 = sqlite3G
1caf0 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 etTempRange(pPar
1caf1 73 65 2c 20 6e 45 78 70 72 29 3b 0a 20 20 20 20 se, nExpr);.
1caf2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
1caf3 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 odeExprList(pPar
1caf4 73 65 2c 20 70 4c 69 73 74 2c 20 72 31 2c 20 31 se, pList, r1, 1
1caf5 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1caf6 20 20 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 nExpr =
1caf7 72 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a r1 = 0;. }.
1caf8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1caf9 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1cafa 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 . /* Possib
1cafb 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 ly overload the
1cafc 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20 function if the
1cafd 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
1cafe 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72 s. ** a vir
1caff 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d tual table colum
1cb00 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 n.. **.
1cb01 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 ** For infix f
1cb02 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 unctions (LIKE,
1cb03 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e GLOB, REGEXP, an
1cb04 64 20 4d 41 54 43 48 29 20 75 73 65 20 74 68 65 d MATCH) use the
1cb05 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 . ** second
1cb06 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 argument, not t
1cb07 68 65 20 66 69 72 73 74 2c 20 61 73 20 74 68 65 he first, as the
1cb08 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 argument to tes
1cb09 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65 t to. ** se
1cb0a 65 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c e if it is a col
1cb0b 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c umn in a virtual
1cb0c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 table. This is
1cb0d 20 64 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20 done because.
1cb0e 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20 ** the left
1cb0f 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78 operand of infix
1cb10 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20 functions (the
1cb11 6f 70 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20 operand we want
1cb12 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 to. ** cont
1cb13 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29 rol overloading)
1cb14 20 65 6e 64 73 20 75 70 20 61 73 20 74 68 65 20 ends up as the
1cb15 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
1cb16 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 to the. **
1cb17 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 function. The e
1cb18 78 70 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f xpression "A glo
1cb19 62 20 42 22 20 69 73 20 65 71 75 69 76 61 6c 65 b B" is equivale
1cb1a 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 nt to . **
1cb1b 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20 "glob(B,A). We
1cb1c 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 want to use the
1cb1d 41 20 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 A in "A glob B"
1cb1e 74 6f 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a to test. **
1cb1f 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76 for function ov
1cb20 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20 erloading. But
1cb21 77 65 20 75 73 65 20 74 68 65 20 42 20 74 65 72 we use the B ter
1cb22 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22 m in "glob(B,A)"
1cb23 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1cb24 20 69 66 28 20 6e 45 78 70 72 3e 3d 32 20 26 26 if( nExpr>=2 &&
1cb25 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 (pExpr->flags &
1cb26 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29 EP_InfixFunc) )
1cb27 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d {. pDef =
1cb28 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 sqlite3VtabOver
1cb29 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c loadFunction(db,
1cb2a 20 70 44 65 66 2c 20 6e 45 78 70 72 2c 20 70 4c pDef, nExpr, pL
1cb2b 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29 ist->a[1].pExpr)
1cb2c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
1cb2d 28 20 6e 45 78 70 72 3e 30 20 29 7b 0a 20 20 20 ( nExpr>0 ){.
1cb2e 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 pDef = sqli
1cb2f 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 te3VtabOverloadF
1cb30 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 unction(db, pDef
1cb31 2c 20 6e 45 78 70 72 2c 20 70 4c 69 73 74 2d 3e , nExpr, pList->
1cb32 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 a[0].pExpr);.
1cb33 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
1cb34 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 for(i=0; i<nEx
1cb35 70 72 20 26 26 20 69 3c 33 32 3b 20 69 2b 2b 29 pr && i<32; i++)
1cb36 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 {. if( sq
1cb37 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
1cb38 61 6e 74 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e ant(pList->a[i].
1cb39 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 pExpr) ){.
1cb3a 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d constMask |=
1cb3b 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 (1<<i);.
1cb3c 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 }. if( (
1cb3d 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 pDef->flags & SQ
1cb3e 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f LITE_FUNC_NEEDCO
1cb3f 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c LL)!=0 && !pColl
1cb40 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 ){. pC
1cb41 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 oll = sqlite3Exp
1cb42 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c rCollSeq(pParse,
1cb43 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 pList->a[i].pEx
1cb44 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 pr);. }.
1cb45 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1cb46 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 pDef->flags & S
1cb47 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 QLITE_FUNC_NEEDC
1cb48 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 OLL ){. i
1cb49 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c f( !pColl ) pCol
1cb4a 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c l = db->pDfltCol
1cb4b 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 l; . sqli
1cb4c 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1cb4d 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 OP_CollSeq, 0,
1cb4e 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 0, 0, (char *)pC
1cb4f 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 oll, P4_COLLSEQ)
1cb50 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1cb51 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1cb52 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 4(v, OP_Function
1cb53 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c , constMask, r1,
1cb54 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 target,.
1cb55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cb56 20 28 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34 (char*)pDef, P4
1cb57 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 _FUNCDEF);.
1cb58 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1cb59 67 65 50 35 28 76 2c 20 6e 45 78 70 72 29 3b 0a geP5(v, nExpr);.
1cb5a 20 20 20 20 20 20 69 66 28 20 6e 45 78 70 72 20 if( nExpr
1cb5b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1cb5c 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e e3ReleaseTempRan
1cb5d 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 6e ge(pParse, r1, n
1cb5e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Expr);. }.
1cb5f 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1cb60 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
1cb61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 nge(pParse, r1,
1cb62 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 62 72 nExpr);. br
1cb63 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 eak;. }.#ifnd
1cb64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
1cb65 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65 UBQUERY. case
1cb66 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 TK_EXISTS:.
1cb67 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 case TK_SELECT:
1cb68 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
1cb69 28 20 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 ( op==TK_EXISTS
1cb6a 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1cb6b 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 e( op==TK_SELECT
1cb6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 );. if( pE
1cb6d 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 xpr->iColumn==0
1cb6e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1cb6f 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 e3CodeSubselect(
1cb70 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30 pParse, pExpr, 0
1cb71 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
1cb72 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70 inReg = pExp
1cb73 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 r->iColumn;.
1cb74 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1cb75 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b case TK_IN: {
1cb76 0a 20 20 20 20 20 20 69 6e 74 20 72 4e 6f 74 46 . int rNotF
1cb77 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 ound = 0;.
1cb78 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c int rMayHaveNull
1cb79 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 = 0;. int
1cb7a 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a j2, j3, j4, j5;.
1cb7b 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69 6e char affin
1cb7c 69 74 79 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 ity;. int e
1cb7d 54 79 70 65 3b 0a 0a 20 20 20 20 20 20 56 64 62 Type;.. Vdb
1cb7e 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c eNoopComment((v,
1cb7f 20 22 62 65 67 69 6e 20 49 4e 20 65 78 70 72 20 "begin IN expr
1cb80 72 25 64 22 2c 20 74 61 72 67 65 74 29 29 3b 0a r%d", target));.
1cb81 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 73 71 eType = sq
1cb82 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 lite3FindInIndex
1cb83 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
1cb84 26 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a &rMayHaveNull);.
1cb85 20 20 20 20 20 20 69 66 28 20 72 4d 61 79 48 61 if( rMayHa
1cb86 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 veNull ){.
1cb87 20 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b rNotFound = ++
1cb88 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
1cb89 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
1cb8a 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 61 Figure out the a
1cb8b 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 74 ffinity to use t
1cb8c 6f 20 63 72 65 61 74 65 20 61 20 6b 65 79 20 66 o create a key f
1cb8d 72 6f 6d 20 74 68 65 20 72 65 73 75 6c 74 73 0a rom the results.
1cb8e 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 ** of the
1cb8f 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 expression. affi
1cb90 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73 20 61 nityStr stores a
1cb91 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 73 static string s
1cb92 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 uitable for.
1cb93 20 20 2a 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 ** P4 of OP_Ma
1cb94 6b 65 52 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 keRecord..
1cb95 2a 2f 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 */. affinit
1cb96 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 y = comparisonAf
1cb97 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 0a finity(pExpr);..
1cb98 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 . /* Code t
1cb99 68 65 20 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22 he <expr> from "
1cb9a 3c 65 78 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22 <expr> IN (...)"
1cb9b 2e 20 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 . The temporary
1cb9c 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 70 table. ** p
1cb9d 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 63 6f 6e Expr->iTable con
1cb9e 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 73 tains the values
1cb9f 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 that make up th
1cba0 65 20 28 2e 2e 2e 29 20 73 65 74 2e 0a 20 20 20 e (...) set..
1cba1 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 72 */. pPar
1cba2 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 se->disableColCa
1cba3 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c che++;. sql
1cba4 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
1cba5 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1cba6 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 t, target);.
1cba7 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c pParse->disabl
1cba8 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 eColCache--;.
1cba9 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 j2 = sqlite3V
1cbaa 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
1cbab 49 73 4e 75 6c 6c 2c 20 74 61 72 67 65 74 29 3b IsNull, target);
1cbac 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 . if( eType
1cbad 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 ==IN_INDEX_ROWID
1cbae 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 33 20 3d ){. j3 =
1cbaf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1cbb0 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 p1(v, OP_MustBeI
1cbb1 6e 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 nt, target);.
1cbb2 20 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 j4 = sqlite
1cbb3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
1cbb4 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 70 45 78 P_NotExists, pEx
1cbb5 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 74 pr->iTable, 0, t
1cbb6 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 arget);.
1cbb7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1cbb8 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
1cbb9 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 1, target);.
1cbba 20 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 j5 = sqlite
1cbbb 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 3VdbeAddOp0(v, O
1cbbc 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20 P_Goto);.
1cbbd 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
1cbbe 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 Here(v, j3);.
1cbbf 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1cbc0 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b JumpHere(v, j4);
1cbc1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1cbc2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1cbc3 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72 _Integer, 0, tar
1cbc4 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 get);. }els
1cbc5 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 e{. r2 =
1cbc6 72 65 67 46 72 65 65 32 20 3d 20 73 71 6c 69 74 regFree2 = sqlit
1cbc7 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
1cbc8 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f rse);.. /
1cbc9 2a 20 43 72 65 61 74 65 20 61 20 72 65 63 6f 72 * Create a recor
1cbca 64 20 61 6e 64 20 74 65 73 74 20 66 6f 72 20 73 d and test for s
1cbcb 65 74 20 6d 65 6d 62 65 72 73 68 69 70 2e 20 49 et membership. I
1cbcc 66 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 69 f the set contai
1cbcd 6e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 ns. ** th
1cbce 65 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 6a 75 e value, then ju
1cbcf 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 mp to the end of
1cbd0 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 2e 20 the test code.
1cbd1 54 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 20 The target.
1cbd2 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20 73 ** register s
1cbd3 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 till contains th
1cbd4 65 20 74 72 75 65 20 28 31 29 20 76 61 6c 75 65 e true (1) value
1cbd5 20 77 72 69 74 74 65 6e 20 74 6f 20 69 74 20 65 written to it e
1cbd6 61 72 6c 69 65 72 2e 0a 20 20 20 20 20 20 20 20 arlier..
1cbd7 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
1cbd8 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
1cbd9 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 74 OP_MakeRecord, t
1cbda 61 72 67 65 74 2c 20 31 2c 20 72 32 2c 20 26 61 arget, 1, r2, &a
1cbdb 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 ffinity, 1);.
1cbdc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1cbdd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
1cbde 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 eger, 1, target)
1cbdf 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73 ;. j5 = s
1cbe0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1cbe1 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 (v, OP_Found, pE
1cbe2 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 xpr->iTable, 0,
1cbe3 72 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a r2);.. /*
1cbe4 20 49 66 20 74 68 65 20 73 65 74 20 6d 65 6d 62 If the set memb
1cbe5 65 72 73 68 69 70 20 74 65 73 74 20 66 61 69 6c ership test fail
1cbe6 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 s, then the resu
1cbe7 6c 74 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 lt of the .
1cbe8 20 20 20 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e ** "x IN (...
1cbe9 29 22 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 )" expression mu
1cbea 73 74 20 62 65 20 65 69 74 68 65 72 20 30 20 6f st be either 0 o
1cbeb 72 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 r NULL. If the s
1cbec 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f et. ** co
1cbed 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 ntains no NULL v
1cbee 61 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 alues, then the
1cbef 72 65 73 75 6c 74 20 69 73 20 30 2e 20 49 66 20 result is 0. If
1cbf0 74 68 65 20 73 65 74 20 0a 20 20 20 20 20 20 20 the set .
1cbf1 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 ** contains one
1cbf2 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 or more NULL va
1cbf3 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 lues, then the r
1cbf4 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 esult of the.
1cbf5 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 ** expressi
1cbf6 6f 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e on is also NULL.
1cbf7 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1cbf8 20 20 20 20 69 66 28 20 72 4e 6f 74 46 6f 75 6e if( rNotFoun
1cbf9 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 d==0 ){.
1cbfa 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 /* This branch
1cbfb 20 72 75 6e 73 20 69 66 20 69 74 20 69 73 20 6b runs if it is k
1cbfc 6e 6f 77 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20 nown at compile
1cbfd 74 69 6d 65 20 28 6e 6f 77 29 20 74 68 61 74 20 time (now) that
1cbfe 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 . ** th
1cbff 65 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e e set contains n
1cc00 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54 o NULL values. T
1cc01 68 69 73 20 68 61 70 70 65 6e 73 20 61 73 20 74 his happens as t
1cc02 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 he result.
1cc03 20 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 ** of a "NOT
1cc04 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e NULL" constrain
1cc05 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 t in the databas
1cc06 65 20 73 63 68 65 6d 61 2e 20 4e 6f 20 6e 65 65 e schema. No nee
1cc07 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 d. ** t
1cc08 6f 20 74 65 73 74 20 74 68 65 20 64 61 74 61 20 o test the data
1cc09 73 74 72 75 63 74 75 72 65 20 61 74 20 72 75 6e structure at run
1cc0a 74 69 6d 65 20 69 6e 20 74 68 69 73 20 63 61 73 time in this cas
1cc0b 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
1cc0c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1cc0d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1cc0e 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 P_Integer, 0, ta
1cc0f 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d rget);. }
1cc10 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1cc11 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 70 6f /* This block po
1cc12 70 75 6c 61 74 65 73 20 74 68 65 20 72 4e 6f 74 pulates the rNot
1cc13 46 6f 75 6e 64 20 72 65 67 69 73 74 65 72 20 77 Found register w
1cc14 69 74 68 20 65 69 74 68 65 72 20 4e 55 4c 4c 0a ith either NULL.
1cc15 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 ** or
1cc16 30 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61 0 (an integer va
1cc17 6c 75 65 29 2e 20 49 66 20 74 68 65 20 64 61 74 lue). If the dat
1cc18 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 a structure cont
1cc19 61 69 6e 73 20 6f 6e 65 0a 20 20 20 20 20 20 20 ains one.
1cc1a 20 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 20 4e 55 ** or more NU
1cc1b 4c 4c 73 2c 20 74 68 65 6e 20 73 65 74 20 72 4e LLs, then set rN
1cc1c 6f 74 46 6f 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e otFound to NULL.
1cc1d 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74 20 Otherwise, set
1cc1e 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 it. **
1cc1f 74 6f 20 30 2e 20 49 66 20 72 65 67 69 73 74 65 to 0. If registe
1cc20 72 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 r rMayHaveNull i
1cc21 73 20 61 6c 72 65 61 64 79 20 73 65 74 20 74 6f s already set to
1cc22 20 73 6f 6d 65 20 76 61 6c 75 65 0a 20 20 20 20 some value.
1cc23 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 74 ** other t
1cc24 68 61 6e 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 han NULL, then t
1cc25 68 65 20 74 65 73 74 20 68 61 73 20 61 6c 72 65 he test has alre
1cc26 61 64 79 20 62 65 65 6e 20 72 75 6e 20 61 6e 64 ady been run and
1cc27 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 . ** r
1cc28 4e 6f 74 46 6f 75 6e 64 20 69 73 20 61 6c 72 65 NotFound is alre
1cc29 61 64 79 20 70 6f 70 75 6c 61 74 65 64 2e 0a 20 ady populated..
1cc2a 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
1cc2b 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e static con
1cc2c 73 74 20 63 68 61 72 20 6e 75 6c 6c 52 65 63 6f st char nullReco
1cc2d 72 64 5b 5d 20 3d 20 7b 20 30 78 30 32 2c 20 30 rd[] = { 0x02, 0
1cc2e 78 30 30 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 x00 };.
1cc2f 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j3 = sqlite3Vdb
1cc30 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f eAddOp1(v, OP_No
1cc31 74 4e 75 6c 6c 2c 20 72 4d 61 79 48 61 76 65 4e tNull, rMayHaveN
1cc32 75 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ull);.
1cc33 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1cc34 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
1cc35 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 rNotFound);.
1cc36 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1cc37 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 beAddOp4(v, OP_B
1cc38 6c 6f 62 2c 20 32 2c 20 72 4d 61 79 48 61 76 65 lob, 2, rMayHave
1cc39 4e 75 6c 6c 2c 20 30 2c 20 0a 20 20 20 20 20 20 Null, 0, .
1cc3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cc3b 20 20 20 20 20 20 20 6e 75 6c 6c 52 65 63 6f 72 nullRecor
1cc3c 64 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 d, P4_STATIC);.
1cc3d 20 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 71 j4 = sq
1cc3e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1cc3f 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 v, OP_Found, pEx
1cc40 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 pr->iTable, 0, r
1cc41 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 MayHaveNull);.
1cc42 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1cc43 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1cc44 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 4e 6f 74 Integer, 0, rNot
1cc45 46 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 Found);.
1cc46 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1cc47 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 pHere(v, j4);.
1cc48 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1cc49 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
1cc4a 33 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 3);.. /
1cc4b 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 * Copy the value
1cc4c 20 6f 66 20 72 65 67 69 73 74 65 72 20 72 4e 6f of register rNo
1cc4d 74 46 6f 75 6e 64 20 28 77 68 69 63 68 20 69 73 tFound (which is
1cc4e 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 either NULL or
1cc4f 30 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 0). **
1cc50 69 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 into the target
1cc51 72 65 67 69 73 74 65 72 2e 20 54 68 69 73 20 77 register. This w
1cc52 69 6c 6c 20 62 65 20 74 68 65 20 72 65 73 75 6c ill be the resul
1cc53 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 t of the.
1cc54 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ** expression
1cc55 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
1cc56 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1cc57 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1cc58 5f 43 6f 70 79 2c 20 72 4e 6f 74 46 6f 75 6e 64 _Copy, rNotFound
1cc59 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1cc5a 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1cc5b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
1cc5c 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 mpHere(v, j2);.
1cc5d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1cc5e 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29 3b JumpHere(v, j5);
1cc5f 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 . VdbeComme
1cc60 6e 74 28 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 nt((v, "end IN e
1cc61 78 70 72 20 72 25 64 22 2c 20 74 61 72 67 65 74 xpr r%d", target
1cc62 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ));. break;
1cc63 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1cc64 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 /*. ** x
1cc65 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a BETWEEN y AND z
1cc66 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1cc67 68 69 73 20 69 73 20 65 71 75 69 76 61 6c 65 6e his is equivalen
1cc68 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 t to. **.
1cc69 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 ** x>=y AND x
1cc6a 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a <=z. **. *
1cc6b 2a 20 58 20 69 73 20 73 74 6f 72 65 64 20 69 6e * X is stored in
1cc6c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 pExpr->pLeft..
1cc6d 20 20 20 2a 2a 20 59 20 69 73 20 73 74 6f 72 65 ** Y is store
1cc6e 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 d in pExpr->pLis
1cc6f 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2e 0a 20 t->a[0].pExpr..
1cc70 20 20 20 2a 2a 20 5a 20 69 73 20 73 74 6f 72 65 ** Z is store
1cc71 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 d in pExpr->pLis
1cc72 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2e 0a 20 t->a[1].pExpr..
1cc73 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 */. case T
1cc74 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 K_BETWEEN: {.
1cc75 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d Expr *pLeft =
1cc76 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 pExpr->pLeft;.
1cc77 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 struct Expr
1cc78 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65 List_item *pLIte
1cc79 6d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 m = pExpr->pList
1cc7a 2d 3e 61 3b 0a 20 20 20 20 20 20 45 78 70 72 20 ->a;. Expr
1cc7b 2a 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d *pRight = pLItem
1cc7c 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 20 ->pExpr;..
1cc7d 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 codeCompareOpera
1cc7e 6e 64 73 28 70 50 61 72 73 65 2c 20 70 4c 65 66 nds(pParse, pLef
1cc7f 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 t, &r1, ®Free
1cc80 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1,.
1cc81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cc82 20 20 20 20 20 70 52 69 67 68 74 2c 20 26 72 32 pRight, &r2
1cc83 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
1cc84 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
1cc85 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 gFree1==0 );.
1cc86 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1cc87 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 Free2==0 );.
1cc88 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 47 65 r3 = sqlite3Ge
1cc89 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 tTempReg(pParse)
1cc8a 3b 0a 20 20 20 20 20 20 72 34 20 3d 20 73 71 6c ;. r4 = sql
1cc8b 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
1cc8c 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 63 6f Parse);. co
1cc8d 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 deCompare(pParse
1cc8e 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c , pLeft, pRight,
1cc8f 20 4f 50 5f 47 65 2c 0a 20 20 20 20 20 20 20 20 OP_Ge,.
1cc90 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 r1, r2
1cc91 2c 20 72 33 2c 20 53 51 4c 49 54 45 5f 53 54 4f , r3, SQLITE_STO
1cc92 52 45 50 32 29 3b 0a 20 20 20 20 20 20 70 4c 49 REP2);. pLI
1cc93 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 70 52 69 tem++;. pRi
1cc94 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 ght = pLItem->pE
1cc95 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 xpr;. sqlit
1cc96 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
1cc97 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 (pParse, regFree
1cc98 32 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 2);. r2 = s
1cc99 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
1cc9a 6d 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 mp(pParse, pRigh
1cc9b 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 t, ®Free2);.
1cc9c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1cc9d 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 egFree2==0 );.
1cc9e 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 codeCompare(
1cc9f 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 pParse, pLeft, p
1cca0 52 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 Right, OP_Le, r1
1cca1 2c 20 72 32 2c 20 72 34 2c 20 53 51 4c 49 54 45 , r2, r4, SQLITE
1cca2 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 _STOREP2);.
1cca3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1cca4 70 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 p3(v, OP_And, r3
1cca5 2c 20 72 34 2c 20 74 61 72 67 65 74 29 3b 0a 20 , r4, target);.
1cca6 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
1cca7 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
1cca8 65 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 73 71 e, r3);. sq
1cca9 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
1ccaa 52 65 67 28 70 50 61 72 73 65 2c 20 72 34 29 3b Reg(pParse, r4);
1ccab 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1ccac 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1ccad 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 UPLUS: {. i
1ccae 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 nReg = sqlite3Ex
1ccaf 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 prCodeTarget(pPa
1ccb0 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1ccb1 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 t, target);.
1ccb2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a break;. }..
1ccb3 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f /*. ** Fo
1ccb4 72 6d 20 41 3a 0a 20 20 20 20 2a 2a 20 20 20 43 rm A:. ** C
1ccb5 41 53 45 20 78 20 57 48 45 4e 20 65 31 20 54 48 ASE x WHEN e1 TH
1ccb6 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 EN r1 WHEN e2 TH
1ccb7 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 EN r2 ... WHEN e
1ccb8 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 N THEN rN ELSE y
1ccb9 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 END. **.
1ccba 2a 2a 20 46 6f 72 6d 20 42 3a 0a 20 20 20 20 2a ** Form B:. *
1ccbb 2a 20 20 20 43 41 53 45 20 57 48 45 4e 20 65 31 * CASE WHEN e1
1ccbc 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32 THEN r1 WHEN e2
1ccbd 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 THEN r2 ... WHE
1ccbe 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 N eN THEN rN ELS
1ccbf 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 E y END. **.
1ccc0 20 20 20 2a 2a 20 46 6f 72 6d 20 41 20 69 73 20 ** Form A is
1ccc1 63 61 6e 20 62 65 20 74 72 61 6e 73 66 6f 72 6d can be transform
1ccc2 65 64 20 69 6e 74 6f 20 74 68 65 20 65 71 75 69 ed into the equi
1ccc3 76 61 6c 65 6e 74 20 66 6f 72 6d 20 42 20 61 73 valent form B as
1ccc4 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a follows:. **
1ccc5 20 20 20 43 41 53 45 20 57 48 45 4e 20 78 3d 65 CASE WHEN x=e
1ccc6 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 78 1 THEN r1 WHEN x
1ccc7 3d 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 0a =e2 THEN r2 ....
1ccc8 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 57 48 ** WH
1ccc9 45 4e 20 78 3d 65 4e 20 54 48 45 4e 20 72 4e 20 EN x=eN THEN rN
1ccca 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a ELSE y END. *
1cccb 2a 0a 20 20 20 20 2a 2a 20 58 20 28 69 66 20 69 *. ** X (if i
1cccc 74 20 65 78 69 73 74 73 29 20 69 73 20 69 6e 20 t exists) is in
1cccd 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 pExpr->pLeft..
1ccce 20 20 2a 2a 20 59 20 69 73 20 69 6e 20 70 45 78 ** Y is in pEx
1cccf 70 72 2d 3e 70 52 69 67 68 74 2e 20 20 54 68 65 pr->pRight. The
1ccd0 20 59 20 69 73 20 61 6c 73 6f 20 6f 70 74 69 6f Y is also optio
1ccd1 6e 61 6c 2e 20 20 49 66 20 74 68 65 72 65 20 69 nal. If there i
1ccd2 73 20 6e 6f 0a 20 20 20 20 2a 2a 20 45 4c 53 45 s no. ** ELSE
1ccd3 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 20 6f clause and no o
1ccd4 74 68 65 72 20 74 65 72 6d 20 6d 61 74 63 68 65 ther term matche
1ccd5 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 s, then the resu
1ccd6 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a lt of the. **
1ccd7 20 65 78 70 72 73 73 69 6f 6e 20 69 73 20 4e 55 exprssion is NU
1ccd8 4c 4c 2e 0a 20 20 20 20 2a 2a 20 45 69 20 69 73 LL.. ** Ei is
1ccd9 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 in pExpr->pList
1ccda 2d 3e 61 5b 69 2a 32 5d 20 61 6e 64 20 52 69 20 ->a[i*2] and Ri
1ccdb 69 73 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d is pExpr->pList-
1ccdc 3e 61 5b 69 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a >a[i*2+1].. *
1ccdd 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 *. ** The res
1ccde 75 6c 74 20 6f 66 20 74 68 65 20 65 78 70 72 65 ult of the expre
1ccdf 73 73 69 6f 6e 20 69 73 20 74 68 65 20 52 69 20 ssion is the Ri
1cce0 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 for the first ma
1cce1 74 63 68 69 6e 67 20 45 69 2c 0a 20 20 20 20 2a tching Ei,. *
1cce2 2a 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73 * or if there is
1cce3 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45 69 2c no matching Ei,
1cce4 20 74 68 65 20 45 4c 53 45 20 74 65 72 6d 20 59 the ELSE term Y
1cce5 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73 , or if there is
1cce6 0a 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53 45 20 . ** no ELSE
1cce7 74 65 72 6d 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20 term, NULL..
1cce8 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 */. case TK_C
1cce9 41 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 ASE: {. int
1ccea 20 65 6e 64 4c 61 62 65 6c 3b 20 20 20 20 20 20 endLabel;
1cceb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ccec 2a 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 * GOTO label for
1cced 20 65 6e 64 20 6f 66 20 43 41 53 45 20 73 74 6d end of CASE stm
1ccee 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e t */. int n
1ccef 65 78 74 43 61 73 65 3b 20 20 20 20 20 20 20 20 extCase;
1ccf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ccf1 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e GOTO label for n
1ccf2 65 78 74 20 57 48 45 4e 20 63 6c 61 75 73 65 20 ext WHEN clause
1ccf3 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 */. int nEx
1ccf4 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pr;
1ccf5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 78 /* 2x
1ccf6 20 6e 75 6d 62 65 72 20 6f 66 20 57 48 45 4e 20 number of WHEN
1ccf7 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 terms */. i
1ccf8 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
1ccf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ccfa 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
1ccfb 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 */. ExprLi
1ccfc 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 st *pEList;
1ccfd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1ccfe 69 73 74 20 6f 66 20 57 48 45 4e 20 74 65 72 6d ist of WHEN term
1ccff 73 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 s */. struc
1cd00 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
1cd01 2a 61 4c 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 *aListelem; /*
1cd02 41 72 72 61 79 20 6f 66 20 57 48 45 4e 20 74 65 Array of WHEN te
1cd03 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 rms */. Exp
1cd04 72 20 6f 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 r opCompare;
1cd05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1cd06 2a 20 54 68 65 20 58 3d 3d 45 69 20 65 78 70 72 * The X==Ei expr
1cd07 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 ession */.
1cd08 45 78 70 72 20 63 61 63 68 65 58 3b 20 20 20 20 Expr cacheX;
1cd09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd0a 20 20 2f 2a 20 43 61 63 68 65 64 20 65 78 70 72 /* Cached expr
1cd0b 65 73 73 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 ession X */.
1cd0c 20 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 Expr *pX;
1cd0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd0e 20 20 20 20 2f 2a 20 54 68 65 20 58 20 65 78 70 /* The X exp
1cd0f 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 ression */.
1cd10 20 45 78 70 72 20 2a 70 54 65 73 74 3b 20 20 20 Expr *pTest;
1cd11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd12 20 20 20 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72 /* X==Ei (for
1cd13 6d 20 41 29 20 6f 72 20 6a 75 73 74 20 45 69 20 m A) or just Ei
1cd14 28 66 6f 72 6d 20 42 29 20 2a 2f 0a 0a 20 20 20 (form B) */..
1cd15 20 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d assert(pExpr-
1cd16 3e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 61 >pList);. a
1cd17 73 73 65 72 74 28 28 70 45 78 70 72 2d 3e 70 4c ssert((pExpr->pL
1cd18 69 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20 ist->nExpr % 2)
1cd19 3d 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 == 0);. ass
1cd1a 65 72 74 28 70 45 78 70 72 2d 3e 70 4c 69 73 74 ert(pExpr->pList
1cd1b 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 20 ->nExpr > 0);.
1cd1c 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 pEList = pEx
1cd1d 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20 pr->pList;.
1cd1e 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20 70 45 4c aListelem = pEL
1cd1f 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 6e 45 ist->a;. nE
1cd20 78 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 xpr = pEList->nE
1cd21 78 70 72 3b 0a 20 20 20 20 20 20 65 6e 64 4c 61 xpr;. endLa
1cd22 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 bel = sqlite3Vdb
1cd23 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 eMakeLabel(v);.
1cd24 20 20 20 20 20 69 66 28 20 28 70 58 20 3d 20 70 if( (pX = p
1cd25 45 78 70 72 2d 3e 70 4c 65 66 74 29 21 3d 30 20 Expr->pLeft)!=0
1cd26 29 7b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 ){. cache
1cd27 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20 20 20 20 X = *pX;.
1cd28 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f testcase( pX->o
1cd29 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 p==TK_COLUMN ||
1cd2a 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 pX->op==TK_REGIS
1cd2b 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 63 TER );. c
1cd2c 61 63 68 65 58 2e 69 54 61 62 6c 65 20 3d 20 73 acheX.iTable = s
1cd2d 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
1cd2e 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2c 20 26 mp(pParse, pX, &
1cd2f 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 regFree1);.
1cd30 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1cd31 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
1cd32 20 20 20 20 63 61 63 68 65 58 2e 6f 70 20 3d 20 cacheX.op =
1cd33 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 TK_REGISTER;.
1cd34 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 6f opCompare.o
1cd35 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20 p = TK_EQ;.
1cd36 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 4c 65 opCompare.pLe
1cd37 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0a 20 20 ft = &cacheX;.
1cd38 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 26 6f pTest = &o
1cd39 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 20 20 pCompare;.
1cd3a 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e }. pParse->
1cd3b 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b disableColCache+
1cd3c 2b 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 +;. for(i=0
1cd3d 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 2b 32 ; i<nExpr; i=i+2
1cd3e 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
1cd3f 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f X ){. o
1cd40 70 43 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20 pCompare.pRight
1cd41 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 = aListelem[i].p
1cd42 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65 Expr;. }e
1cd43 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 lse{. p
1cd44 54 65 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d Test = aListelem
1cd45 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [i].pExpr;.
1cd46 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78 }. nex
1cd47 74 43 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56 tCase = sqlite3V
1cd48 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b dbeMakeLabel(v);
1cd49 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
1cd4a 65 28 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b e( pTest->op==TK
1cd4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 54 65 73 74 _COLUMN || pTest
1cd4c 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 ->op==TK_REGISTE
1cd4d 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c R );. sql
1cd4e 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
1cd4f 70 50 61 72 73 65 2c 20 70 54 65 73 74 2c 20 6e pParse, pTest, n
1cd50 65 78 74 43 61 73 65 2c 20 53 51 4c 49 54 45 5f extCase, SQLITE_
1cd51 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 JUMPIFNULL);.
1cd52 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 testcase( a
1cd53 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 Listelem[i+1].pE
1cd54 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 xpr->op==TK_COLU
1cd55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 MN );. te
1cd56 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65 stcase( aListele
1cd57 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 m[i+1].pExpr->op
1cd58 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b ==TK_REGISTER );
1cd59 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1cd5a 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c ExprCode(pParse,
1cd5b 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e aListelem[i+1].
1cd5c 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a pExpr, target);.
1cd5d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1cd5e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1cd5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62 65 Goto, 0, endLabe
1cd60 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 l);. sqli
1cd61 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
1cd62 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 73 65 29 bel(v, nextCase)
1cd63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1cd64 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 if( pExpr->pRigh
1cd65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c t ){. sql
1cd66 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
1cd67 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 rse, pExpr->pRig
1cd68 68 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 ht, target);.
1cd69 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1cd6a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1cd6b 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 Op2(v, OP_Null,
1cd6c 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
1cd6d 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1cd6e 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
1cd6f 6c 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a l(v, endLabel);.
1cd70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1cd71 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1cd72 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 Cache>0 );.
1cd73 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 pParse->disable
1cd74 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 ColCache--;.
1cd75 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1cd76 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1cd77 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 63 IT_TRIGGER. c
1cd78 61 73 65 20 54 4b 5f 52 41 49 53 45 3a 20 7b 0a ase TK_RAISE: {.
1cd79 20 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73 if( !pPars
1cd7a 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a e->trigStack ){.
1cd7b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1cd7c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a rrorMsg(pParse,.
1cd7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd7e 20 20 20 20 20 20 20 22 52 41 49 53 45 28 29 20 "RAISE()
1cd7f 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 may only be used
1cd80 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 within a trigge
1cd81 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 r-program");.
1cd82 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
1cd83 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1cd84 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 pExpr->iColumn!
1cd85 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 =OE_Ignore ){.
1cd86 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1cd87 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 4f Expr->iColumn==O
1cd88 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 0a 20 20 E_Rollback ||.
1cd89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1cd8a 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 3d Expr->iColumn ==
1cd8b 20 4f 45 5f 41 62 6f 72 74 20 7c 7c 0a 20 20 20 OE_Abort ||.
1cd8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
1cd8d 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 3d 20 xpr->iColumn ==
1cd8e 4f 45 5f 46 61 69 6c 20 29 3b 0a 20 20 20 20 20 OE_Fail );.
1cd8f 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f sqlite3Dequo
1cd90 74 65 45 78 70 72 28 64 62 2c 20 70 45 78 70 72 teExpr(db, pExpr
1cd91 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
1cd92 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1cd93 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 OP_Halt, SQLITE
1cd94 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 70 45 78 _CONSTRAINT, pEx
1cd95 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 30 2c 0a pr->iColumn, 0,.
1cd96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd97 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 (char*)p
1cd98 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 Expr->token.z, p
1cd99 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a Expr->token.n);.
1cd9a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 } else {.
1cd9b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1cd9c 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d pExpr->iColumn =
1cd9d 3d 20 4f 45 5f 49 67 6e 6f 72 65 20 29 3b 0a 20 = OE_Ignore );.
1cd9e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1cd9f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1cda0 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30 2c 20 30 ContextPop, 0, 0
1cda1 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
1cda2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1cda3 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61 OP_Goto, 0, pPa
1cda4 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e rse->trigStack->
1cda5 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20 20 20 ignoreJump);.
1cda6 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e VdbeCommen
1cda7 74 28 28 76 2c 20 22 72 61 69 73 65 28 49 47 4e t((v, "raise(IGN
1cda8 4f 52 45 29 22 29 29 3b 0a 20 20 20 20 20 20 7d ORE)"));. }
1cda9 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1cdaa 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
1cdab 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
1cdac 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
1cdad 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 egFree1);. sqli
1cdae 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
1cdaf 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 g(pParse, regFre
1cdb0 65 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e e2);. return in
1cdb1 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 Reg;.}../*.** Ge
1cdb2 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 nerate code to e
1cdb3 76 61 6c 75 61 74 65 20 61 6e 20 65 78 70 72 65 valuate an expre
1cdb4 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f 72 65 20 ssion and store
1cdb5 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 the results.** i
1cdb6 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 nto a register.
1cdb7 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 Return the regi
1cdb8 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 ster number wher
1cdb9 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a e the results.**
1cdba 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a are stored..**.
1cdbb 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69 73 74 ** If the regist
1cdbc 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 er is a temporar
1cdbd 79 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 y register that
1cdbe 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 can be deallocat
1cdbf 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 69 74 ed,.** then writ
1cdc0 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69 6e 74 e its number int
1cdc1 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74 68 65 o *pReg. If the
1cdc2 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65 72 result register
1cdc3 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 65 6d is not.** a tem
1cdc4 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73 65 74 porary, then set
1cdc5 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f 2e 0a *pReg to zero..
1cdc6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1cdc7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
1cdc8 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73 65 20 rCodeTemp(Parse
1cdc9 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1cdca 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 65 67 29 Expr, int *pReg)
1cdcb 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c {. int r1 = sql
1cdcc 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
1cdcd 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 32 Parse);. int r2
1cdce 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1cdcf 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c deTarget(pParse,
1cdd0 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 69 pExpr, r1);. i
1cdd1 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a 20 20 20 f( r2==r1 ){.
1cdd2 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a 20 20 7d *pReg = r1;. }
1cdd3 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
1cdd4 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1cdd5 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 pParse, r1);.
1cdd6 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d 0a *pReg = 0;. }.
1cdd7 20 20 72 65 74 75 72 6e 20 72 32 3b 0a 7d 0a 0a return r2;.}..
1cdd8 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
1cdd9 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 ode that will ev
1cdda 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69 6f aluate expressio
1cddb 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 n pExpr and stor
1cddc 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 e the.** results
1cddd 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 in register tar
1cdde 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 get. The result
1cddf 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 s are guaranteed
1cde0 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a 20 69 6e to appear.** in
1cde1 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65 74 register target
1cde2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1cde3 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
1cde4 78 70 72 43 6f 64 65 28 50 61 72 73 65 20 2a 70 xprCode(Parse *p
1cde5 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 Parse, Expr *pEx
1cde6 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b pr, int target){
1cde7 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a 0a 20 . int inReg;..
1cde8 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e assert( target>
1cde9 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 0 && target<=pPa
1cdea 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 rse->nMem );. i
1cdeb 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 nReg = sqlite3Ex
1cdec 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 prCodeTarget(pPa
1cded 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 rse, pExpr, targ
1cdee 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 et);. assert( p
1cdef 50 61 72 73 65 2d 3e 70 56 64 62 65 20 7c 7c 20 Parse->pVdbe ||
1cdf0 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
1cdf1 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 ocFailed );. if
1cdf2 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 ( inReg!=target
1cdf3 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 && pParse->pVdbe
1cdf4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1cdf5 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 dbeAddOp2(pParse
1cdf6 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70 ->pVdbe, OP_SCop
1cdf7 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74 y, inReg, target
1cdf8 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1cdf9 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a target;.}../*.**
1cdfa 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
1cdfb 68 61 74 20 65 76 61 6c 75 74 65 73 20 74 68 65 hat evalutes the
1cdfc 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f given expressio
1cdfd 6e 20 61 6e 64 20 70 75 74 73 20 74 68 65 20 72 n and puts the r
1cdfe 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 67 69 esult.** in regi
1cdff 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a 2a 0a ster target..**.
1ce00 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61 20 63 ** Also make a c
1ce01 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 opy of the expre
1ce02 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 20 69 6e ssion results in
1ce03 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 61 63 68 to another "cach
1ce04 65 22 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 e" register.** a
1ce05 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 65 78 nd modify the ex
1ce06 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 pression so that
1ce07 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 69 the next time i
1ce08 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 2c 0a t is evaluated,.
1ce09 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 ** the result is
1ce0a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 a copy of the c
1ce0b 61 63 68 65 20 72 65 67 69 73 74 65 72 2e 0a 2a ache register..*
1ce0c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1ce0d 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 65 78 e is used for ex
1ce0e 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 61 pressions that a
1ce0f 72 65 20 75 73 65 64 20 6d 75 6c 74 69 70 6c 65 re used multiple
1ce10 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 54 68 65 .** times. The
1ce11 79 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 y are evaluated
1ce12 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 72 65 73 once and the res
1ce13 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 70 72 ults of the expr
1ce14 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 72 65 ession.** are re
1ce15 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f used..*/.SQLITE_
1ce16 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1ce17 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 te3ExprCodeAndCa
1ce18 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 che(Parse *pPars
1ce19 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 e, Expr *pExpr,
1ce1a 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56 int target){. V
1ce1b 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d dbe *v = pParse-
1ce1c 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 6e >pVdbe;. int in
1ce1d 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 Reg;. inReg = s
1ce1e 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
1ce1f 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 Parse, pExpr, ta
1ce20 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 rget);. assert(
1ce21 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 69 target>0 );. i
1ce22 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b f( pExpr->op!=TK
1ce23 5f 52 45 47 49 53 54 45 52 20 29 7b 20 20 0a 20 _REGISTER ){ .
1ce24 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 20 20 20 int iMem;.
1ce25 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 iMem = ++pParse
1ce26 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 ->nMem;. sqli
1ce27 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1ce28 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 65 67 2c OP_Copy, inReg,
1ce29 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 45 78 70 iMem);. pExp
1ce2a 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 4d 65 6d r->iTable = iMem
1ce2b 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 ;. pExpr->op
1ce2c 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 = TK_REGISTER;.
1ce2d 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65 }. return inRe
1ce2e 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 g;.}../*.** Retu
1ce2f 72 6e 20 54 52 55 45 20 69 66 20 70 45 78 70 72 rn TRUE if pExpr
1ce30 20 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e 74 20 is an constant
1ce31 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 expression that
1ce32 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a is appropriate.*
1ce33 2a 20 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 * for factoring
1ce34 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 20 20 out of a loop.
1ce35 41 70 70 72 6f 70 72 69 61 74 65 20 65 78 70 72 Appropriate expr
1ce36 65 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a 2a 0a essions are:.**.
1ce37 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 65 78 70 ** * Any exp
1ce38 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 76 61 ression that eva
1ce39 6c 75 61 74 65 73 20 74 6f 20 74 77 6f 20 6f 72 luates to two or
1ce3a 20 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e 0a 2a more opcodes..*
1ce3b 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 4f *.** * Any O
1ce3c 50 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f 52 65 P_Integer, OP_Re
1ce3d 61 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20 4f al, OP_String, O
1ce3e 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c 6c 2c P_Blob, OP_Null,
1ce3f 20 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20 4f 50 .** or OP
1ce40 5f 56 61 72 69 61 62 6c 65 20 74 68 61 74 20 64 _Variable that d
1ce41 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 oes not need to
1ce42 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 0a be placed in a .
1ce43 2a 2a 20 20 20 20 20 20 20 73 70 65 63 69 66 69 ** specifi
1ce44 63 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a c register..**.*
1ce45 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 70 6f * There is no po
1ce46 69 6e 74 20 69 6e 20 66 61 63 74 6f 72 69 6e 67 int in factoring
1ce47 20 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e 73 74 out single-inst
1ce48 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 ruction constant
1ce49 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 .** expressions
1ce4a 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 that need to be
1ce4b 70 6c 61 63 65 64 20 69 6e 20 61 20 70 61 72 74 placed in a part
1ce4c 69 63 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e icular register.
1ce4d 20 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20 66 .** We could f
1ce4e 61 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 2c 20 actor them out,
1ce4f 62 75 74 20 74 68 65 6e 20 77 65 20 77 6f 75 6c but then we woul
1ce50 64 20 65 6e 64 20 75 70 20 61 64 64 69 6e 67 20 d end up adding
1ce51 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 20 69 an.** OP_SCopy i
1ce52 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6d 6f nstruction to mo
1ce53 76 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 74 ve the value int
1ce54 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 65 o the correct re
1ce55 67 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65 72 2e gister.** later.
1ce56 20 20 57 65 20 6d 69 67 68 74 20 61 73 20 77 65 We might as we
1ce57 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68 65 20 ll just use the
1ce58 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 75 63 original instruc
1ce59 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 6f 69 tion and.** avoi
1ce5a 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79 2e 0a d the OP_SCopy..
1ce5b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 */.static int is
1ce5c 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 61 AppropriateForFa
1ce5d 63 74 6f 72 69 6e 67 28 45 78 70 72 20 2a 70 29 ctoring(Expr *p)
1ce5e 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 {. if( !sqlite3
1ce5f 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f ExprIsConstantNo
1ce60 74 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20 20 20 tJoin(p) ){.
1ce61 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4f 6e return 0; /* On
1ce62 6c 79 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 ly constant expr
1ce63 65 73 73 69 6f 6e 73 20 61 72 65 20 61 70 70 72 essions are appr
1ce64 6f 70 72 69 61 74 65 20 66 6f 72 20 66 61 63 74 opriate for fact
1ce65 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 oring */. }. i
1ce66 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 45 f( (p->flags & E
1ce67 50 5f 46 69 78 65 64 44 65 73 74 29 3d 3d 30 20 P_FixedDest)==0
1ce68 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
1ce69 20 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 61 6e /* Any constan
1ce6a 74 20 77 69 74 68 6f 75 74 20 61 20 66 69 78 65 t without a fixe
1ce6b 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 d destination is
1ce6c 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a appropriate */.
1ce6d 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e }. while( p->
1ce6e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 op==TK_UPLUS ) p
1ce6f 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 73 = p->pLeft;. s
1ce70 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a witch( p->op ){.
1ce71 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ce72 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c MIT_BLOB_LITERAL
1ce73 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f . case TK_BLO
1ce74 42 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 B:.#endif. ca
1ce75 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 0a se TK_VARIABLE:.
1ce76 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 case TK_INTE
1ce77 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b GER:. case TK
1ce78 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61 73 65 _FLOAT:. case
1ce79 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 TK_NULL:. ca
1ce7a 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a se TK_STRING: {.
1ce7b 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ce7c 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 p->op==TK_BLOB )
1ce7d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1ce7e 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49 ( p->op==TK_VARI
1ce7f 41 42 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 ABLE );. te
1ce80 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 stcase( p->op==T
1ce81 4b 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 K_INTEGER );.
1ce82 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e testcase( p->
1ce83 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 3b 0a op==TK_FLOAT );.
1ce84 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ce85 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 p->op==TK_NULL )
1ce86 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1ce87 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 ( p->op==TK_STRI
1ce88 4e 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 53 NG );. /* S
1ce89 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f ingle-instructio
1ce8a 6e 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 n constants with
1ce8b 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 a fixed destina
1ce8c 74 69 6f 6e 20 61 72 65 0a 20 20 20 20 20 20 2a tion are. *
1ce8d 2a 20 62 65 74 74 65 72 20 64 6f 6e 65 20 69 6e * better done in
1ce8e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 65 20 66 61 -line. If we fa
1ce8f 63 74 6f 72 20 74 68 65 6d 2c 20 74 68 65 79 20 ctor them, they
1ce90 77 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a 20 20 will just end.
1ce91 20 20 20 20 2a 2a 20 75 70 20 67 65 6e 65 72 61 ** up genera
1ce92 74 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f 70 79 ting an OP_SCopy
1ce93 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c to move the val
1ce94 75 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e ue to the destin
1ce95 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 72 ation. ** r
1ce96 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 20 20 egister. */.
1ce97 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
1ce98 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d }. case TK_UM
1ce99 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 20 69 INUS: {. i
1ce9a 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d f( p->pLeft->op=
1ce9b 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 2d 3e =TK_FLOAT || p->
1ce9c 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e pLeft->op==TK_IN
1ce9d 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 TEGER ){.
1ce9e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
1ce9f 20 20 20 7d 0a 20 20 20 20 20 20 20 62 72 65 61 }. brea
1cea0 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
1cea1 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 ault: {. br
1cea2 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
1cea3 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a return 1;.}../*
1cea4 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 20 .** If pExpr is
1cea5 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 a constant expre
1cea6 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 70 ssion that is ap
1cea7 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a propriate for.**
1cea8 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f factoring out o
1cea9 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e 20 65 f a loop, then e
1ceaa 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 70 72 valuate the expr
1ceab 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f 20 61 ession.** into a
1ceac 20 72 65 67 69 73 74 65 72 20 61 6e 64 20 63 6f register and co
1cead 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 65 73 nvert the expres
1ceae 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52 sion into a TK_R
1ceaf 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70 72 65 EGISTER.** expre
1ceb0 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ssion..*/.static
1ceb1 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 45 78 int evalConstEx
1ceb2 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b pr(Walker *pWalk
1ceb3 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 er, Expr *pExpr)
1ceb4 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 {. Parse *pPars
1ceb5 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 e = pWalker->pPa
1ceb6 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28 20 70 rse;. switch( p
1ceb7 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 Expr->op ){.
1ceb8 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52 case TK_REGISTER
1ceb9 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e : {. return
1ceba 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 1;. }. ca
1cebb 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a se TK_FUNCTION:.
1cebc 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f case TK_AGG_
1cebd 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 FUNCTION:. ca
1cebe 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 se TK_CONST_FUNC
1cebf 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 : {. /* The
1cec0 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 20 arguments to a
1cec1 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20 61 20 function have a
1cec2 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f fixed destinatio
1cec3 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 61 72 6b n.. ** Mark
1cec4 20 74 68 65 6d 20 74 68 69 73 20 77 61 79 20 74 them this way t
1cec5 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 61 74 65 o avoid generate
1cec6 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 5f 53 43 d unneeded OP_SC
1cec7 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 opy. ** ins
1cec8 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 20 20 20 tructions. .
1cec9 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c */. ExprL
1ceca 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 ist *pList = pEx
1cecb 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20 pr->pList;.
1cecc 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 if( pList ){.
1cecd 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4c int i = pL
1cece 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 ist->nExpr;.
1cecf 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
1ced0 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 ist_item *pItem
1ced1 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 = pList->a;.
1ced2 20 20 20 20 66 6f 72 28 3b 20 69 3e 30 3b 20 69 for(; i>0; i
1ced3 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 --, pItem++){.
1ced4 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 if( pIte
1ced5 6d 2d 3e 70 45 78 70 72 20 29 20 70 49 74 65 6d m->pExpr ) pItem
1ced6 2d 3e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c ->pExpr->flags |
1ced7 3d 20 45 50 5f 46 69 78 65 64 44 65 73 74 3b 0a = EP_FixedDest;.
1ced8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1ced9 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1ceda 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 }. }. if( i
1cedb 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 sAppropriateForF
1cedc 61 63 74 6f 72 69 6e 67 28 70 45 78 70 72 29 20 actoring(pExpr)
1cedd 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20 ){. int r1 =
1cede 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
1cedf 20 20 20 20 69 6e 74 20 72 32 3b 0a 20 20 20 20 int r2;.
1cee0 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r2 = sqlite3Expr
1cee1 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 CodeTarget(pPars
1cee2 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 e, pExpr, r1);.
1cee3 20 20 20 69 66 28 20 72 31 21 3d 72 32 20 29 20 if( r1!=r2 )
1cee4 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
1cee5 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 mpReg(pParse, r1
1cee6 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 );. pExpr->op
1cee7 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a = TK_REGISTER;.
1cee8 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
1cee9 65 20 3d 20 72 32 3b 0a 20 20 20 20 72 65 74 75 e = r2;. retu
1ceea 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
1ceeb 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 }. return WRC_C
1ceec 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ontinue;.}../*.*
1ceed 2a 20 50 72 65 65 76 61 6c 75 61 74 65 20 63 6f * Preevaluate co
1ceee 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 73 nstant subexpres
1ceef 73 69 6f 6e 73 20 77 69 74 68 69 6e 20 70 45 78 sions within pEx
1cef0 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 pr and store the
1cef1 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 72 .** results in r
1cef2 65 67 69 73 74 65 72 73 2e 20 20 4d 6f 64 69 66 egisters. Modif
1cef3 79 20 70 45 78 70 72 20 73 6f 20 74 68 61 74 20 y pExpr so that
1cef4 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 the constant sub
1cef5 65 78 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61 72 expresions.** ar
1cef6 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 6f 70 e TK_REGISTER op
1cef7 63 6f 64 65 73 20 74 68 61 74 20 72 65 66 65 72 codes that refer
1cef8 20 74 6f 20 74 68 65 20 70 72 65 63 6f 6d 70 75 to the precompu
1cef9 74 65 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 ted values..*/.S
1cefa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1cefb 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f id sqlite3ExprCo
1cefc 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73 deConstants(Pars
1cefd 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
1cefe 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65 *pExpr){. Walke
1ceff 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 r w;. w.xExprCa
1cf00 6c 6c 62 61 63 6b 20 3d 20 65 76 61 6c 43 6f 6e llback = evalCon
1cf01 73 74 45 78 70 72 3b 0a 20 20 77 2e 78 53 65 6c stExpr;. w.xSel
1cf02 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b ectCallback = 0;
1cf03 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 . w.pParse = pP
1cf04 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 arse;. sqlite3W
1cf05 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 alkExpr(&w, pExp
1cf06 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 r);.}.../*.** Ge
1cf07 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 nerate code that
1cf08 20 70 75 73 68 65 73 20 74 68 65 20 76 61 6c 75 pushes the valu
1cf09 65 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 e of every eleme
1cf0a 6e 74 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0a nt of the given.
1cf0b 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 ** expression li
1cf0c 73 74 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e st into a sequen
1cf0d 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 ce of registers
1cf0e 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 74 61 72 beginning at tar
1cf0f 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 get..**.** Retur
1cf10 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1cf11 65 6c 65 6d 65 6e 74 73 20 65 76 61 6c 75 61 74 elements evaluat
1cf12 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1cf13 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1cf14 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 3ExprCodeExprLis
1cf15 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 t(. Parse *pPar
1cf16 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 se, /* Parsi
1cf17 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
1cf18 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c ExprList *pList,
1cf19 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 /* The expres
1cf1a 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65 20 sion list to be
1cf1b 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 coded */. int t
1cf1c 61 72 67 65 74 2c 20 20 20 20 20 20 20 20 2f 2a arget, /*
1cf1d 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 Where to write
1cf1e 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 results */. int
1cf1f 20 64 6f 48 61 72 64 43 6f 70 79 20 20 20 20 20 doHardCopy
1cf20 2f 2a 20 4d 61 6b 65 20 61 20 68 61 72 64 20 63 /* Make a hard c
1cf21 6f 70 79 20 6f 66 20 65 76 65 72 79 20 65 6c 65 opy of every ele
1cf22 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 ment */.){. str
1cf23 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
1cf24 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 m *pItem;. int
1cf25 69 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 i, n;. assert(
1cf26 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 61 73 pList!=0 );. as
1cf27 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 29 sert( target>0 )
1cf28 3b 0a 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e ;. n = pList->n
1cf29 45 78 70 72 3b 0a 20 20 66 6f 72 28 70 49 74 65 Expr;. for(pIte
1cf2a 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b m=pList->a, i=0;
1cf2b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d i<n; i++, pItem
1cf2c 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 ++){. if( pIt
1cf2d 65 6d 2d 3e 69 41 6c 69 61 73 20 29 7b 0a 20 20 em->iAlias ){.
1cf2e 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d 20 63 int iReg = c
1cf2f 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c odeAlias(pParse,
1cf30 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 2c 20 pItem->iAlias,
1cf31 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61 pItem->pExpr, ta
1cf32 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 56 rget+i);. V
1cf33 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 dbe *v = sqlite3
1cf34 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
1cf35 0a 20 20 20 20 20 20 69 66 28 20 69 52 65 67 21 . if( iReg!
1cf36 3d 74 61 72 67 65 74 2b 69 20 29 7b 0a 20 20 20 =target+i ){.
1cf37 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1cf38 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f AddOp2(v, OP_SCo
1cf39 70 79 2c 20 69 52 65 67 2c 20 74 61 72 67 65 74 py, iReg, target
1cf3a 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 +i);. }.
1cf3b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
1cf3c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 lite3ExprCode(pP
1cf3d 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 arse, pItem->pEx
1cf3e 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 pr, target+i);.
1cf3f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6f 48 }. if( doH
1cf40 61 72 64 43 6f 70 79 20 29 7b 0a 20 20 20 20 20 ardCopy ){.
1cf41 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 64 sqlite3ExprHard
1cf42 43 6f 70 79 28 70 50 61 72 73 65 2c 20 74 61 72 Copy(pParse, tar
1cf43 67 65 74 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 get, n);. }.
1cf44 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d }. return n;.}
1cf45 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1cf46 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c code for a bool
1cf47 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 ean expression s
1cf48 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20 uch that a jump
1cf49 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 is made.** to th
1cf4a 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69 e label "dest" i
1cf4b 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1cf4c 20 69 73 20 74 72 75 65 20 62 75 74 20 65 78 65 is true but exe
1cf4d 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e cution.** contin
1cf4e 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72 ues straight thr
1cf4f 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 u if the express
1cf50 69 6f 6e 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a ion is false..**
1cf51 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 .** If the expre
1cf52 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 ssion evaluates
1cf53 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 to NULL (neither
1cf54 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 true nor false)
1cf55 2c 20 74 68 65 6e 0a 2a 2a 20 74 61 6b 65 20 74 , then.** take t
1cf56 68 65 20 6a 75 6d 70 20 69 66 20 74 68 65 20 6a he jump if the j
1cf57 75 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20 69 umpIfNull flag i
1cf58 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e s SQLITE_JUMPIFN
1cf59 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ULL..**.** This
1cf5a 63 6f 64 65 20 64 65 70 65 6e 64 73 20 6f 6e 20 code depends on
1cf5b 74 68 65 20 66 61 63 74 20 74 68 61 74 20 63 65 the fact that ce
1cf5c 72 74 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c 75 rtain token valu
1cf5d 65 73 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a 2a es (ex: TK_EQ).*
1cf5e 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 * are the same a
1cf5f 73 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20 s opcode values
1cf60 28 65 78 3a 20 4f 50 5f 45 71 29 20 74 68 61 74 (ex: OP_Eq) that
1cf61 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 implement the c
1cf62 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 orresponding.**
1cf63 6f 70 65 72 61 74 69 6f 6e 2e 20 20 53 70 65 63 operation. Spec
1cf64 69 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 ial comments in
1cf65 76 64 62 65 2e 63 20 61 6e 64 20 74 68 65 20 6d vdbe.c and the m
1cf66 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72 kopcodeh.awk scr
1cf67 69 70 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d 61 ipt in.** the ma
1cf68 6b 65 20 70 72 6f 63 65 73 73 20 63 61 75 73 65 ke process cause
1cf69 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 74 6f these values to
1cf6a 20 61 6c 69 67 6e 2e 20 20 41 73 73 65 72 74 28 align. Assert(
1cf6b 29 73 20 69 6e 20 74 68 65 20 63 6f 64 65 0a 2a )s in the code.*
1cf6c 2a 20 62 65 6c 6f 77 20 76 65 72 69 66 79 20 74 * below verify t
1cf6d 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 73 20 hat the numbers
1cf6e 61 72 65 20 61 6c 69 67 6e 65 64 20 63 6f 72 72 are aligned corr
1cf6f 65 63 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ectly..*/.SQLITE
1cf70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1cf71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
1cf72 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
1cf73 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
1cf74 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66 dest, int jumpIf
1cf75 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76 Null){. Vdbe *v
1cf76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
1cf77 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a ;. int op = 0;.
1cf78 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d int regFree1 =
1cf79 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65 0;. int regFre
1cf7a 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31 e2 = 0;. int r1
1cf7b 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 , r2;.. assert(
1cf7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c jumpIfNull==SQL
1cf7d 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c ITE_JUMPIFNULL |
1cf7e 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 | jumpIfNull==0
1cf7f 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c );. if( v==0 ||
1cf80 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 pExpr==0 ) retu
1cf81 72 6e 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 rn;. op = pExpr
1cf82 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20 ->op;. switch(
1cf83 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
1cf84 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69 K_AND: {. i
1cf85 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56 nt d2 = sqlite3V
1cf86 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b dbeMakeLabel(v);
1cf87 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1cf88 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 jumpIfNull==0 )
1cf89 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1cf8a 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c ( pParse->disabl
1cf8b 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 3b 0a eColCache==0 );.
1cf8c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1cf8d 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c rIfFalse(pParse,
1cf8e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 pExpr->pLeft, d
1cf8f 32 2c 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c 2,jumpIfNull^SQL
1cf90 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b ITE_JUMPIFNULL);
1cf91 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 . pParse->d
1cf92 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b isableColCache++
1cf93 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
1cf94 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 xprIfTrue(pParse
1cf95 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c , pExpr->pRight,
1cf96 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c dest, jumpIfNul
1cf97 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 l);. assert
1cf98 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c ( pParse->disabl
1cf99 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 eColCache>0 );.
1cf9a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 pParse->dis
1cf9b 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a ableColCache--;.
1cf9c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1cf9d 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c eResolveLabel(v,
1cf9e 20 64 32 29 3b 0a 20 20 20 20 20 20 62 72 65 61 d2);. brea
1cf9f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1cfa0 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 e TK_OR: {.
1cfa1 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
1cfa2 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
1cfa3 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72 testcase( pPar
1cfa4 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 se->disableColCa
1cfa5 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 che==0 );.
1cfa6 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 sqlite3ExprIfTru
1cfa7 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
1cfa8 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 >pLeft, dest, ju
1cfa9 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 mpIfNull);.
1cfaa 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 pParse->disable
1cfab 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 ColCache++;.
1cfac 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 sqlite3ExprIfT
1cfad 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 rue(pParse, pExp
1cfae 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c r->pRight, dest,
1cfaf 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
1cfb0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 assert( pPar
1cfb1 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 se->disableColCa
1cfb2 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 che>0 );. p
1cfb3 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f Parse->disableCo
1cfb4 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 20 lCache--;.
1cfb5 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1cfb6 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a case TK_NOT: {.
1cfb7 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1cfb8 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b jumpIfNull==0 );
1cfb9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
1cfba 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 prIfFalse(pParse
1cfbb 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
1cfbc 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c dest, jumpIfNull
1cfbd 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1cfbe 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
1cfbf 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 K_LT:. case T
1cfc0 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_LE:. case T
1cfc1 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 K_GT:. case T
1cfc2 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_GE:. case T
1cfc3 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_NE:. case T
1cfc4 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 K_EQ: {. as
1cfc5 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f sert( TK_LT==OP_
1cfc6 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Lt );. asse
1cfc7 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 rt( TK_LE==OP_Le
1cfc8 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1cfc9 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 ( TK_GT==OP_Gt )
1cfca 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1cfcb 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a TK_GE==OP_Ge );.
1cfcc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1cfcd 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 _EQ==OP_Eq );.
1cfce 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e assert( TK_N
1cfcf 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 E==OP_Ne );.
1cfd0 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1cfd1 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 TK_LT );. t
1cfd2 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1cfd3 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LE );. test
1cfd4 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 case( op==TK_GT
1cfd5 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1cfd6 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a e( op==TK_GE );.
1cfd7 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1cfd8 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 op==TK_EQ );.
1cfd9 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1cfda 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 =TK_NE );.
1cfdb 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 testcase( jumpIf
1cfdc 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Null==0 );.
1cfdd 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 codeCompareOper
1cfde 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 ands(pParse, pEx
1cfdf 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 pr->pLeft, &r1,
1cfe0 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 ®Free1,.
1cfe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cfe2 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 pEx
1cfe3 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c pr->pRight, &r2,
1cfe4 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 ®Free2);.
1cfe5 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 codeCompare(p
1cfe6 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
1cfe7 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 eft, pExpr->pRig
1cfe8 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 ht, op,.
1cfe9 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 r1, r2
1cfea 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 , dest, jumpIfNu
1cfeb 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 ll);. testc
1cfec 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
1cfed 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1cfee 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 se( regFree2==0
1cfef 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1cff0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
1cff1 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 K_ISNULL:. ca
1cff2 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b se TK_NOTNULL: {
1cff3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
1cff4 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e K_ISNULL==OP_IsN
1cff5 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 ull );. ass
1cff6 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d ert( TK_NOTNULL=
1cff7 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 =OP_NotNull );.
1cff8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1cff9 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a p==TK_ISNULL );.
1cffa 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1cffb 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 op==TK_NOTNULL )
1cffc 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c ;. r1 = sql
1cffd 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
1cffe 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
1cfff 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 pLeft, ®Free1
1d000 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1d001 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 VdbeAddOp2(v, op
1d002 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 , r1, dest);.
1d003 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1d004 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
1d005 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1d006 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 case TK_BETWE
1d007 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 EN: {. /*
1d008 20 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e x BETWEEN y AN
1d009 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 D z. **.
1d00a 20 20 20 2a 2a 20 49 73 20 65 71 75 69 76 61 6c ** Is equival
1d00b 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a ent to . **
1d00c 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d . ** x>=
1d00d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 y AND x<=z.
1d00e 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 **. ** Cod
1d00f 65 20 69 74 20 61 73 20 73 75 63 68 2c 20 74 61 e it as such, ta
1d010 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 64 6f 20 king care to do
1d011 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 the common subex
1d012 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a pression. *
1d013 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f * elementation o
1d014 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 f x.. */.
1d015 20 20 20 20 45 78 70 72 20 65 78 70 72 41 6e 64 Expr exprAnd
1d016 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d ;. Expr com
1d017 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70 pLeft;. Exp
1d018 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 r compRight;.
1d019 20 20 20 45 78 70 72 20 65 78 70 72 58 3b 0a 0a Expr exprX;..
1d01a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20 2a 70 exprX = *p
1d01b 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 Expr->pLeft;.
1d01c 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 exprAnd.op =
1d01d 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 78 TK_AND;. ex
1d01e 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63 prAnd.pLeft = &c
1d01f 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 ompLeft;. e
1d020 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 xprAnd.pRight =
1d021 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 &compRight;.
1d022 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20 compLeft.op =
1d023 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d TK_GE;. com
1d024 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 pLeft.pLeft = &e
1d025 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 xprX;. comp
1d026 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20 70 45 Left.pRight = pE
1d027 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d xpr->pList->a[0]
1d028 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 63 6f .pExpr;. co
1d029 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f mpRight.op = TK_
1d02a 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 LE;. compRi
1d02b 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 ght.pLeft = &exp
1d02c 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 rX;. compRi
1d02d 67 68 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 ght.pRight = pEx
1d02e 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e pr->pList->a[1].
1d02f 70 45 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 pExpr;. exp
1d030 72 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 rX.iTable = sqli
1d031 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
1d032 70 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 pParse, &exprX,
1d033 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 ®Free1);.
1d034 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
1d035 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree1==0 );.
1d036 20 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 exprX.op = TK_R
1d037 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 EGISTER;. t
1d038 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e estcase( jumpIfN
1d039 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ull==0 );.
1d03a 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 sqlite3ExprIfTru
1d03b 65 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 e(pParse, &exprA
1d03c 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 nd, dest, jumpIf
1d03d 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 Null);. bre
1d03e 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
1d03f 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 fault: {. r
1d040 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 1 = sqlite3ExprC
1d041 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
1d042 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 pExpr, ®Free1
1d043 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1d044 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1d045 5f 49 66 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a _If, r1, dest, j
1d046 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20 umpIfNull!=0);.
1d047 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1d048 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
1d049 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 testcase( ju
1d04a 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 mpIfNull==0 );.
1d04b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1d04c 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 }. }. sqlite3R
1d04d 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
1d04e 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b arse, regFree1);
1d04f 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 . sqlite3Releas
1d050 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
1d051 20 72 65 67 46 72 65 65 32 29 3b 20 20 0a 7d 0a regFree2); .}.
1d052 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
1d053 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c 65 code for a boole
1d054 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 75 an expression su
1d055 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20 69 ch that a jump i
1d056 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65 s made.** to the
1d057 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69 66 label "dest" if
1d058 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1d059 69 73 20 66 61 6c 73 65 20 62 75 74 20 65 78 65 is false but exe
1d05a 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e cution.** contin
1d05b 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72 ues straight thr
1d05c 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 u if the express
1d05d 69 6f 6e 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a ion is true..**.
1d05e 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 ** If the expres
1d05f 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74 sion evaluates t
1d060 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20 o NULL (neither
1d061 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 20 true nor false)
1d062 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 66 20 then.** jump if
1d063 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20 53 51 jumpIfNull is SQ
1d064 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 LITE_JUMPIFNULL
1d065 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 or fall through
1d066 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a 2a 2a if jumpIfNull.**
1d067 20 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 is 0..*/.SQLITE
1d068 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1d069 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 lite3ExprIfFalse
1d06a 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1d06b 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 Expr *pExpr, int
1d06c 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 dest, int jumpI
1d06d 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a fNull){. Vdbe *
1d06e 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
1d06f 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b e;. int op = 0;
1d070 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 . int regFree1
1d071 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 = 0;. int regFr
1d072 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 ee2 = 0;. int r
1d073 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 1, r2;.. assert
1d074 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 ( jumpIfNull==SQ
1d075 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 LITE_JUMPIFNULL
1d076 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 || jumpIfNull==0
1d077 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c );. if( v==0 |
1d078 7c 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 | pExpr==0 ) ret
1d079 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 76 urn;.. /* The v
1d07a 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2d 3e 6f alue of pExpr->o
1d07b 70 20 61 6e 64 20 6f 70 20 61 72 65 20 72 65 6c p and op are rel
1d07c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a ated as follows:
1d07d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 . **. **
1d07e 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 pExpr->op
1d07f 20 20 20 20 20 20 6f 70 0a 20 20 2a 2a 20 20 20 op. **
1d080 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 ---------
1d081 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------
1d082 2d 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f -. ** TK_
1d083 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 ISNULL
1d084 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 2a 2a 20 OP_NotNull. **
1d085 20 20 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c TK_NOTNULL
1d086 20 20 20 20 20 20 20 20 20 4f 50 5f 49 73 4e 75 OP_IsNu
1d087 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b ll. ** TK
1d088 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20 20 20 _NE
1d089 20 4f 50 5f 45 71 0a 20 20 2a 2a 20 20 20 20 20 OP_Eq. **
1d08a 20 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 TK_EQ
1d08b 20 20 20 20 20 4f 50 5f 4e 65 0a 20 20 2a 2a 20 OP_Ne. **
1d08c 20 20 20 20 20 20 54 4b 5f 47 54 20 20 20 20 20 TK_GT
1d08d 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 65 0a 20 OP_Le.
1d08e 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 45 20 ** TK_LE
1d08f 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f OP_
1d090 47 74 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b Gt. ** TK
1d091 5f 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 _GE
1d092 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20 20 20 20 OP_Lt. **
1d093 20 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 TK_LT
1d094 20 20 20 20 20 4f 50 5f 47 65 0a 20 20 2a 2a 0a OP_Ge. **.
1d095 20 20 2a 2a 20 46 6f 72 20 6f 74 68 65 72 20 76 ** For other v
1d096 61 6c 75 65 73 20 6f 66 20 70 45 78 70 72 2d 3e alues of pExpr->
1d097 6f 70 2c 20 6f 70 20 69 73 20 75 6e 64 65 66 69 op, op is undefi
1d098 6e 65 64 20 61 6e 64 20 75 6e 75 73 65 64 2e 0a ned and unused..
1d099 20 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f ** The value o
1d09a 66 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f f TK_ and OP_ co
1d09b 6e 73 74 61 6e 74 73 20 61 72 65 20 61 72 72 61 nstants are arra
1d09c 6e 67 65 64 20 73 75 63 68 20 74 68 61 74 20 77 nged such that w
1d09d 65 0a 20 20 2a 2a 20 63 61 6e 20 63 6f 6d 70 75 e. ** can compu
1d09e 74 65 20 74 68 65 20 6d 61 70 70 69 6e 67 20 61 te the mapping a
1d09f 62 6f 76 65 20 75 73 69 6e 67 20 74 68 65 20 66 bove using the f
1d0a0 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 73 ollowing express
1d0a1 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73 65 72 74 ion.. ** Assert
1d0a2 28 29 73 20 76 65 72 69 66 79 20 74 68 61 74 20 ()s verify that
1d0a3 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 the computation
1d0a4 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f is correct.. */
1d0a5 0a 20 20 6f 70 20 3d 20 28 28 70 45 78 70 72 2d . op = ((pExpr-
1d0a6 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c 26 31 >op+(TK_ISNULL&1
1d0a7 29 29 5e 31 29 2d 28 54 4b 5f 49 53 4e 55 4c 4c ))^1)-(TK_ISNULL
1d0a8 26 31 29 3b 0a 0a 20 20 2f 2a 20 56 65 72 69 66 &1);.. /* Verif
1d0a9 79 20 63 6f 72 72 65 63 74 20 61 6c 69 67 6e 6d y correct alignm
1d0aa 65 6e 74 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f ent of TK_ and O
1d0ab 50 5f 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a P_ constants. *
1d0ac 2f 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 /. assert( pExp
1d0ad 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c r->op!=TK_ISNULL
1d0ae 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74 4e 75 || op==OP_NotNu
1d0af 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ll );. assert(
1d0b0 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 4f pExpr->op!=TK_NO
1d0b1 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f TNULL || op==OP_
1d0b2 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 IsNull );. asse
1d0b3 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 rt( pExpr->op!=T
1d0b4 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 45 K_NE || op==OP_E
1d0b5 71 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 q );. assert( p
1d0b6 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 45 51 20 Expr->op!=TK_EQ
1d0b7 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20 29 3b 0a || op==OP_Ne );.
1d0b8 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
1d0b9 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 6f 70 >op!=TK_LT || op
1d0ba 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 61 73 73 ==OP_Ge );. ass
1d0bb 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d ert( pExpr->op!=
1d0bc 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f TK_LE || op==OP_
1d0bd 47 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Gt );. assert(
1d0be 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 54 pExpr->op!=TK_GT
1d0bf 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 20 29 3b || op==OP_Le );
1d0c0 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 . assert( pExpr
1d0c1 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 6f ->op!=TK_GE || o
1d0c2 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a 20 20 73 p==OP_Lt );.. s
1d0c3 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 witch( pExpr->op
1d0c4 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
1d0c5 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 AND: {. tes
1d0c6 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c tcase( jumpIfNul
1d0c7 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 l==0 );. te
1d0c8 73 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e stcase( pParse->
1d0c9 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d disableColCache=
1d0ca 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =0 );. sqli
1d0cb 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 te3ExprIfFalse(p
1d0cc 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
1d0cd 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 eft, dest, jumpI
1d0ce 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 70 50 fNull);. pP
1d0cf 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1d0d0 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 Cache++;. s
1d0d1 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 qlite3ExprIfFals
1d0d2 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
1d0d3 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a >pRight, dest, j
1d0d4 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 umpIfNull);.
1d0d5 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
1d0d6 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 ->disableColCach
1d0d7 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 e>0 );. pPa
1d0d8 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1d0d9 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 20 62 72 ache--;. br
1d0da 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1d0db 61 73 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 ase TK_OR: {.
1d0dc 20 20 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 int d2 = sqli
1d0dd 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c te3VdbeMakeLabel
1d0de 28 76 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 (v);. testc
1d0df 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d ase( jumpIfNull=
1d0e0 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 =0 );. test
1d0e1 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e 64 69 case( pParse->di
1d0e2 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 sableColCache==0
1d0e3 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
1d0e4 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72 3ExprIfTrue(pPar
1d0e5 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
1d0e6 2c 20 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c , d2, jumpIfNull
1d0e7 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 ^SQLITE_JUMPIFNU
1d0e8 4c 4c 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 LL);. pPars
1d0e9 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 e->disableColCac
1d0ea 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 he++;. sqli
1d0eb 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 te3ExprIfFalse(p
1d0ec 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 Parse, pExpr->pR
1d0ed 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 ight, dest, jump
1d0ee 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 61 IfNull);. a
1d0ef 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 ssert( pParse->d
1d0f0 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 isableColCache>0
1d0f1 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 );. pParse
1d0f2 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 ->disableColCach
1d0f3 65 2d 2d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 e--;. sqlit
1d0f4 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 e3VdbeResolveLab
1d0f5 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 el(v, d2);.
1d0f6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1d0f7 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b case TK_NOT: {
1d0f8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
1d0f9 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c prIfTrue(pParse,
1d0fa 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 pExpr->pLeft, d
1d0fb 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 est, jumpIfNull)
1d0fc 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1d0fd 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
1d0fe 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _LT:. case TK
1d0ff 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _LE:. case TK
1d100 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _GT:. case TK
1d101 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _GE:. case TK
1d102 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _NE:. case TK
1d103 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 _EQ: {. tes
1d104 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 tcase( op==TK_LT
1d105 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1d106 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b se( op==TK_LE );
1d107 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1d108 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 op==TK_GT );.
1d109 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1d10a 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 ==TK_GE );.
1d10b 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1d10c 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65 K_EQ );. te
1d10d 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e stcase( op==TK_N
1d10e 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 E );. testc
1d10f 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d ase( jumpIfNull=
1d110 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 =0 );. code
1d111 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 CompareOperands(
1d112 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1d113 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 Left, &r1, ®F
1d114 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 ree1,.
1d115 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d116 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1d117 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 Right, &r2, ®
1d118 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f Free2);. co
1d119 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 deCompare(pParse
1d11a 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
1d11b 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f pExpr->pRight, o
1d11c 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 p,.
1d11d 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 r1, r2, des
1d11e 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a t, jumpIfNull);.
1d11f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1d120 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
1d121 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1d122 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 egFree2==0 );.
1d123 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1d124 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e . case TK_ISN
1d125 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b ULL:. case TK
1d126 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 _NOTNULL: {.
1d127 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1d128 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 TK_ISNULL );.
1d129 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1d12a 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 =TK_NOTNULL );.
1d12b 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 r1 = sqlite
1d12c 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1d12d 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
1d12e 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a ft, ®Free1);.
1d12f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d130 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 eAddOp2(v, op, r
1d131 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 1, dest);.
1d132 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1d133 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 e1==0 );. b
1d134 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1d135 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a case TK_BETWEEN:
1d136 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78 {. /* x
1d137 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a BETWEEN y AND z
1d138 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
1d139 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 ** Is equivalent
1d13a 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 to . **.
1d13b 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 ** x>=y A
1d13c 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a ND x<=z. **
1d13d 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 . ** Code i
1d13e 74 20 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e t as such, takin
1d13f 67 20 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65 g care to do the
1d140 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 common subexpre
1d141 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 ssion. ** e
1d142 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 lementation of x
1d143 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1d144 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 Expr exprAnd;.
1d145 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 Expr compLe
1d146 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 ft;. Expr c
1d147 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 ompRight;.
1d148 45 78 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 Expr exprX;..
1d149 20 20 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70 exprX = *pExp
1d14a 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 r->pLeft;.
1d14b 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f exprAnd.op = TK_
1d14c 41 4e 44 3b 0a 20 20 20 20 20 20 65 78 70 72 41 AND;. exprA
1d14d 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 nd.pLeft = &comp
1d14e 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 Left;. expr
1d14f 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f And.pRight = &co
1d150 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 63 mpRight;. c
1d151 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f ompLeft.op = TK_
1d152 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 GE;. compLe
1d153 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 ft.pLeft = &expr
1d154 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 X;. compLef
1d155 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 t.pRight = pExpr
1d156 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 ->pList->a[0].pE
1d157 78 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 xpr;. compR
1d158 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b ight.op = TK_LE;
1d159 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 . compRight
1d15a 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b .pLeft = &exprX;
1d15b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 . compRight
1d15c 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d .pRight = pExpr-
1d15d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 >pList->a[1].pEx
1d15e 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e pr;. exprX.
1d15f 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 iTable = sqlite3
1d160 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 ExprCodeTemp(pPa
1d161 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26 72 65 rse, &exprX, &re
1d162 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 gFree1);. t
1d163 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
1d164 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78 1==0 );. ex
1d165 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 prX.op = TK_REGI
1d166 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65 73 74 STER;. test
1d167 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c case( jumpIfNull
1d168 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==0 );. sql
1d169 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
1d16a 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64 pParse, &exprAnd
1d16b 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 , dest, jumpIfNu
1d16c 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ll);. break
1d16d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
1d16e 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 20 ult: {. r1
1d16f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
1d170 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 eTemp(pParse, pE
1d171 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 29 3b xpr, ®Free1);
1d172 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d173 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 beAddOp3(v, OP_I
1d174 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73 74 2c 20 fNot, r1, dest,
1d175 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a jumpIfNull!=0);.
1d176 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1d177 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
1d178 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a testcase( j
1d179 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a umpIfNull==0 );.
1d17a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1d17b 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 }. }. sqlite3
1d17c 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
1d17d 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 Parse, regFree1)
1d17e 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 ;. sqlite3Relea
1d17f 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 seTempReg(pParse
1d180 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 7d 0a 0a , regFree2);.}..
1d181 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 70 20 /*.** Do a deep
1d182 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77 comparison of tw
1d183 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 o expression tre
1d184 65 73 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45 es. Return TRUE
1d185 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a 20 69 (non-zero).** i
1d186 66 20 74 68 65 79 20 61 72 65 20 69 64 65 6e 74 f they are ident
1d187 69 63 61 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 ical and return
1d188 46 41 4c 53 45 20 69 66 20 74 68 65 79 20 64 69 FALSE if they di
1d189 66 66 65 72 20 69 6e 20 61 6e 79 20 77 61 79 2e ffer in any way.
1d18a 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
1d18b 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 this routine wi
1d18c 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 ll return FALSE
1d18d 65 76 65 6e 20 69 66 20 74 68 65 20 74 77 6f 20 even if the two
1d18e 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 72 expressions.** r
1d18f 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69 76 61 eally are equiva
1d190 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63 61 6e lent. If we can
1d191 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74 20 74 not prove that t
1d192 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 he expressions a
1d193 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2c re.** identical,
1d194 20 77 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45 we return FALSE
1d195 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66 65 just to be safe
1d196 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f . So if this ro
1d197 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 utine.** returns
1d198 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 79 6f 75 false, then you
1d199 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6b do not really k
1d19a 6e 6f 77 20 66 6f 72 20 63 65 72 74 61 69 6e 20 now for certain
1d19b 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a 20 65 78 if the two.** ex
1d19c 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68 pressions are th
1d19d 65 20 73 61 6d 65 2e 20 20 42 75 74 20 69 66 20 e same. But if
1d19e 79 6f 75 20 67 65 74 20 61 20 54 52 55 45 20 72 you get a TRUE r
1d19f 65 74 75 72 6e 2c 20 74 68 65 6e 20 79 6f 75 0a eturn, then you.
1d1a0 2a 2a 20 63 61 6e 20 62 65 20 73 75 72 65 20 74 ** can be sure t
1d1a1 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 he expressions a
1d1a2 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 49 6e re the same. In
1d1a3 20 74 68 65 20 70 6c 61 63 65 73 20 77 68 65 72 the places wher
1d1a4 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e e.** this routin
1d1a5 65 20 69 73 20 75 73 65 64 2c 20 69 74 20 64 6f e is used, it do
1d1a6 65 73 20 6e 6f 74 20 68 75 72 74 20 74 6f 20 67 es not hurt to g
1d1a7 65 74 20 61 6e 20 65 78 74 72 61 20 46 41 4c 53 et an extra FALS
1d1a8 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 73 74 E - that.** just
1d1a9 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e might result in
1d1aa 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c 79 20 73 some slightly s
1d1ab 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20 42 75 74 lower code. But
1d1ac 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 61 6e returning.** an
1d1ad 20 69 6e 63 6f 72 72 65 63 74 20 54 52 55 45 20 incorrect TRUE
1d1ae 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 61 20 could lead to a
1d1af 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a malfunction..*/.
1d1b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1d1b1 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
1d1b2 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41 2c 20 mpare(Expr *pA,
1d1b3 45 78 70 72 20 2a 70 42 29 7b 0a 20 20 69 6e 74 Expr *pB){. int
1d1b4 20 69 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30 7c i;. if( pA==0|
1d1b5 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 |pB==0 ){. re
1d1b6 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 20 7d turn pB==pA;. }
1d1b7 0a 20 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 70 . if( pA->op!=p
1d1b8 42 2d 3e 6f 70 20 29 20 72 65 74 75 72 6e 20 30 B->op ) return 0
1d1b9 3b 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 ;. if( (pA->fla
1d1ba 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 gs & EP_Distinct
1d1bb 29 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20 )!=(pB->flags &
1d1bc 45 50 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72 EP_Distinct) ) r
1d1bd 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 eturn 0;. if( !
1d1be 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 sqlite3ExprCompa
1d1bf 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42 re(pA->pLeft, pB
1d1c0 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 75 72 ->pLeft) ) retur
1d1c1 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 n 0;. if( !sqli
1d1c2 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 te3ExprCompare(p
1d1c3 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70 A->pRight, pB->p
1d1c4 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 Right) ) return
1d1c5 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e 70 4c 69 0;. if( pA->pLi
1d1c6 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 st ){. if( pB
1d1c7 2d 3e 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 ->pList==0 ) ret
1d1c8 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 70 urn 0;. if( p
1d1c9 41 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 A->pList->nExpr!
1d1ca 3d 70 42 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 =pB->pList->nExp
1d1cb 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 r ) return 0;.
1d1cc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 2d for(i=0; i<pA-
1d1cd 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 >pList->nExpr; i
1d1ce 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 ++){. if( !
1d1cf 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 sqlite3ExprCompa
1d1d0 72 65 28 70 41 2d 3e 70 4c 69 73 74 2d 3e 61 5b re(pA->pList->a[
1d1d1 69 5d 2e 70 45 78 70 72 2c 20 70 42 2d 3e 70 4c i].pExpr, pB->pL
1d1d2 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 ist->a[i].pExpr)
1d1d3 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
1d1d4 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 0;. }.
1d1d5 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
1d1d6 70 42 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 pB->pList ){.
1d1d7 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
1d1d8 20 69 66 28 20 70 41 2d 3e 70 53 65 6c 65 63 74 if( pA->pSelect
1d1d9 20 7c 7c 20 70 42 2d 3e 70 53 65 6c 65 63 74 20 || pB->pSelect
1d1da 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 ) return 0;. if
1d1db 28 20 70 41 2d 3e 69 54 61 62 6c 65 21 3d 70 42 ( pA->iTable!=pB
1d1dc 2d 3e 69 54 61 62 6c 65 20 7c 7c 20 70 41 2d 3e ->iTable || pA->
1d1dd 69 43 6f 6c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f iColumn!=pB->iCo
1d1de 6c 75 6d 6e 20 29 20 72 65 74 75 72 6e 20 30 3b lumn ) return 0;
1d1df 0a 20 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54 . if( pA->op!=T
1d1e0 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e K_COLUMN && pA->
1d1e1 74 6f 6b 65 6e 2e 7a 20 29 7b 0a 20 20 20 20 69 token.z ){. i
1d1e2 66 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d f( pB->token.z==
1d1e3 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
1d1e4 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e if( pB->token.
1d1e5 6e 21 3d 70 41 2d 3e 74 6f 6b 65 6e 2e 6e 20 29 n!=pA->token.n )
1d1e6 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 return 0;. i
1d1e7 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 f( sqlite3StrNIC
1d1e8 6d 70 28 28 63 68 61 72 2a 29 70 41 2d 3e 74 6f mp((char*)pA->to
1d1e9 6b 65 6e 2e 7a 2c 28 63 68 61 72 2a 29 70 42 2d ken.z,(char*)pB-
1d1ea 3e 74 6f 6b 65 6e 2e 7a 2c 70 42 2d 3e 74 6f 6b >token.z,pB->tok
1d1eb 65 6e 2e 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 en.n)!=0 ){.
1d1ec 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
1d1ed 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 }. }. return 1
1d1ee 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 ;.}.../*.** Add
1d1ef 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f a new element to
1d1f0 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 the pAggInfo->a
1d1f1 43 6f 6c 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 Col[] array. Re
1d1f2 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f turn the index o
1d1f3 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 f.** the new ele
1d1f4 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 ment. Return a
1d1f5 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 negative number
1d1f6 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e if malloc fails.
1d1f7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
1d1f8 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 ddAggInfoColumn(
1d1f9 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 67 sqlite3 *db, Agg
1d1fa 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 Info *pInfo){.
1d1fb 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e int i;. pInfo->
1d1fc 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 41 72 aCol = sqlite3Ar
1d1fd 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 rayAllocate(.
1d1fe 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 70 db,. p
1d1ff 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20 20 20 Info->aCol,.
1d200 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d sizeof(pInfo-
1d201 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20 20 20 >aCol[0]),.
1d202 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 3,. &pIn
1d203 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a 20 20 20 fo->nColumn,.
1d204 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c &pInfo->nCol
1d205 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 umnAlloc,.
1d206 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 72 &i. );. retur
1d207 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a n i;.} ../*.*
1d208 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d * Add a new elem
1d209 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 ent to the pAggI
1d20a 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61 72 72 nfo->aFunc[] arr
1d20b 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 ay. Return the
1d20c 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 index of.** the
1d20d 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 new element. Re
1d20e 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 turn a negative
1d20f 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 number if malloc
1d210 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 fails..*/.stati
1d211 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f c int addAggInfo
1d212 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 Func(sqlite3 *db
1d213 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f , AggInfo *pInfo
1d214 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 ){. int i;. pI
1d215 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d 20 73 71 6c nfo->aFunc = sql
1d216 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 ite3ArrayAllocat
1d217 65 28 0a 20 20 20 20 20 20 20 64 62 2c 20 0a 20 e(. db, .
1d218 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 46 75 pInfo->aFu
1d219 6e 63 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f nc,. sizeo
1d21a 66 28 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 f(pInfo->aFunc[0
1d21b 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 ]),. 3,.
1d21c 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 &pInfo->nFu
1d21d 6e 63 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 nc,. &pInf
1d21e 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f 63 2c 0a 20 o->nFuncAlloc,.
1d21f 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 &i. );.
1d220 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a return i;.} .
1d221 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 ./*.** This is t
1d222 68 65 20 78 45 78 70 72 43 61 6c 6c 62 61 63 6b he xExprCallback
1d223 20 66 6f 72 20 61 20 74 72 65 65 20 77 61 6c 6b for a tree walk
1d224 65 72 2e 20 20 49 74 20 69 73 20 75 73 65 64 20 er. It is used
1d225 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 to.** implement
1d226 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 sqlite3ExprAnaly
1d227 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 2e 20 zeAggregates().
1d228 20 53 65 65 20 73 71 6c 69 74 65 33 45 78 70 72 See sqlite3Expr
1d229 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 AnalyzeAggregate
1d22a 73 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 6f s.** for additio
1d22b 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
1d22c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
1d22d 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 28 nalyzeAggregate(
1d22e 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
1d22f 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 Expr *pExpr){.
1d230 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43 6f int i;. NameCo
1d231 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 57 61 ntext *pNC = pWa
1d232 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 50 lker->u.pNC;. P
1d233 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 arse *pParse = p
1d234 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 53 72 NC->pParse;. Sr
1d235 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20 cList *pSrcList
1d236 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b = pNC->pSrcList;
1d237 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 . AggInfo *pAgg
1d238 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e 70 41 67 67 Info = pNC->pAgg
1d239 49 6e 66 6f 3b 0a 0a 20 20 73 77 69 74 63 68 28 Info;.. switch(
1d23a 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 pExpr->op ){.
1d23b 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f case TK_AGG_CO
1d23c 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 LUMN:. case T
1d23d 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 K_COLUMN: {.
1d23e 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
1d23f 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f r->op==TK_AGG_CO
1d240 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 LUMN );. te
1d241 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
1d242 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a p==TK_COLUMN );.
1d243 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 /* Check t
1d244 6f 20 73 65 65 20 69 66 20 74 68 65 20 63 6f 6c o see if the col
1d245 75 6d 6e 20 69 73 20 69 6e 20 6f 6e 65 20 6f 66 umn is in one of
1d246 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 the tables in t
1d247 68 65 20 46 52 4f 4d 0a 20 20 20 20 20 20 2a 2a he FROM. **
1d248 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 61 clause of the a
1d249 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20 2a ggregate query *
1d24a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 53 72 63 /. if( pSrc
1d24b 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 List ){.
1d24c 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
1d24d 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 tem *pItem = pSr
1d24e 63 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 cList->a;.
1d24f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 for(i=0; i<pSr
1d250 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b cList->nSrc; i++
1d251 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
1d252 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 struct Agg
1d253 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a Info_col *pCol;.
1d254 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45 if( pE
1d255 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 xpr->iTable==pIt
1d256 65 6d 2d 3e 69 43 75 72 73 6f 72 20 29 7b 0a 20 em->iCursor ){.
1d257 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
1d258 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 we reach this p
1d259 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 oint, it means t
1d25a 68 61 74 20 70 45 78 70 72 20 72 65 66 65 72 73 hat pExpr refers
1d25b 20 74 6f 20 61 20 74 61 62 6c 65 0a 20 20 20 20 to a table.
1d25c 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 ** that
1d25d 69 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 is in the FROM c
1d25e 6c 61 75 73 65 20 6f 66 20 74 68 65 20 61 67 67 lause of the agg
1d25f 72 65 67 61 74 65 20 71 75 65 72 79 2e 20 20 0a regate query. .
1d260 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 **.
1d261 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 4d 61 ** Ma
1d262 6b 65 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 ke an entry for
1d263 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 41 the column in pA
1d264 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 69 ggInfo->aCol[] i
1d265 66 20 74 68 65 72 65 0a 20 20 20 20 20 20 20 20 f there.
1d266 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 6e ** is not an
1d267 20 65 6e 74 72 79 20 74 68 65 72 65 20 61 6c 72 entry there alr
1d268 65 61 64 79 2e 0a 20 20 20 20 20 20 20 20 20 20 eady..
1d269 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 */.
1d26a 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 int k;.
1d26b 20 20 20 20 70 43 6f 6c 20 3d 20 70 41 67 67 49 pCol = pAggI
1d26c 6e 66 6f 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 nfo->aCol;.
1d26d 20 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 for(k=0;
1d26e 6b 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c k<pAggInfo->nCol
1d26f 75 6d 6e 3b 20 6b 2b 2b 2c 20 70 43 6f 6c 2b 2b umn; k++, pCol++
1d270 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1d271 20 69 66 28 20 70 43 6f 6c 2d 3e 69 54 61 62 6c if( pCol->iTabl
1d272 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 e==pExpr->iTable
1d273 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 &&.
1d274 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c pCol->iCol
1d275 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69 43 6f 6c umn==pExpr->iCol
1d276 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 umn ){.
1d277 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1d278 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1d279 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1d27a 20 20 20 20 20 20 20 20 69 66 28 20 28 6b 3e 3d if( (k>=
1d27b 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d pAggInfo->nColum
1d27c 6e 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n).
1d27d 26 26 20 28 6b 20 3d 20 61 64 64 41 67 67 49 6e && (k = addAggIn
1d27e 66 6f 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2d foColumn(pParse-
1d27f 3e 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 29 3e >db, pAggInfo))>
1d280 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 =0 .
1d281 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1d282 20 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 pCol = &pAggInf
1d283 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b 0a 20 20 20 20 o->aCol[k];.
1d284 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e pCol->
1d285 70 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 pTab = pExpr->pT
1d286 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ab;.
1d287 20 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 20 3d pCol->iTable =
1d288 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a pExpr->iTable;.
1d289 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 pC
1d28a 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 ol->iColumn = pE
1d28b 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 xpr->iColumn;.
1d28c 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c pCol
1d28d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 ->iMem = ++pPars
1d28e 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 e->nMem;.
1d28f 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f pCol->iSo
1d290 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b rterColumn = -1;
1d291 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
1d292 43 6f 6c 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 Col->pExpr = pEx
1d293 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 pr;.
1d294 20 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e if( pAggInfo->
1d295 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 pGroupBy ){.
1d296 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
1d297 6a 2c 20 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 j, n;.
1d298 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a ExprList *
1d299 70 47 42 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e pGB = pAggInfo->
1d29a 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20 pGroupBy;.
1d29b 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74 struct
1d29c 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1d29d 70 54 65 72 6d 20 3d 20 70 47 42 2d 3e 61 3b 0a pTerm = pGB->a;.
1d29e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d29f 6e 20 3d 20 70 47 42 2d 3e 6e 45 78 70 72 3b 0a n = pGB->nExpr;.
1d2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d2a1 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b for(j=0; j<n; j+
1d2a2 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 +, pTerm++){.
1d2a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
1d2a4 78 70 72 20 2a 70 45 20 3d 20 70 54 65 72 6d 2d xpr *pE = pTerm-
1d2a5 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 >pExpr;.
1d2a6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45 if( pE
1d2a7 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 ->op==TK_COLUMN
1d2a8 26 26 20 70 45 2d 3e 69 54 61 62 6c 65 3d 3d 70 && pE->iTable==p
1d2a9 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a Expr->iTable &&.
1d2aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d2ab 20 20 20 20 20 20 70 45 2d 3e 69 43 6f 6c 75 6d pE->iColum
1d2ac 6e 3d 3d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d n==pExpr->iColum
1d2ad 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 n ){.
1d2ae 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 pCol->i
1d2af 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 6a SorterColumn = j
1d2b0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1d2b1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1d2b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d }
1d2b3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1d2b4 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
1d2b5 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
1d2b6 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 if( pCol->iSort
1d2b7 65 72 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 erColumn<0 ){.
1d2b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 pC
1d2b9 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d ol->iSorterColum
1d2ba 6e 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 53 n = pAggInfo->nS
1d2bb 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2b 2b 3b 0a ortingColumn++;.
1d2bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }.
1d2bd 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1d2be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1d2bf 72 65 20 69 73 20 6e 6f 77 20 61 6e 20 65 6e 74 re is now an ent
1d2c0 72 79 20 66 6f 72 20 70 45 78 70 72 20 69 6e 20 ry for pExpr in
1d2c1 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d pAggInfo->aCol[]
1d2c2 20 28 65 69 74 68 65 72 0a 20 20 20 20 20 20 20 (either.
1d2c3 20 20 20 20 20 2a 2a 20 62 65 63 61 75 73 65 20 ** because
1d2c4 69 74 20 77 61 73 20 74 68 65 72 65 20 62 65 66 it was there bef
1d2c5 6f 72 65 20 6f 72 20 62 65 63 61 75 73 65 20 77 ore or because w
1d2c6 65 20 6a 75 73 74 20 63 72 65 61 74 65 64 20 69 e just created i
1d2c7 74 29 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 t)..
1d2c8 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70 ** Convert the p
1d2c9 45 78 70 72 20 74 6f 20 62 65 20 61 20 54 4b 5f Expr to be a TK_
1d2ca 41 47 47 5f 43 4f 4c 55 4d 4e 20 72 65 66 65 72 AGG_COLUMN refer
1d2cb 72 69 6e 67 20 74 6f 20 74 68 61 74 0a 20 20 20 ring to that.
1d2cc 20 20 20 20 20 20 20 20 20 2a 2a 20 70 41 67 67 ** pAgg
1d2cd 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 65 6e 74 Info->aCol[] ent
1d2ce 72 79 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 ry..
1d2cf 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 */. p
1d2d0 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20 3d Expr->pAggInfo =
1d2d1 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 pAggInfo;.
1d2d2 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 pExpr->op
1d2d3 20 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e = TK_AGG_COLUMN
1d2d4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 ;. pE
1d2d5 78 70 72 2d 3e 69 41 67 67 20 3d 20 6b 3b 0a 20 xpr->iAgg = k;.
1d2d6 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
1d2d7 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20 2f 2a ;. } /*
1d2d8 20 65 6e 64 69 66 20 70 45 78 70 72 2d 3e 69 54 endif pExpr->iT
1d2d9 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 able==pItem->iCu
1d2da 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 rsor */.
1d2db 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f 76 } /* end loop ov
1d2dc 65 72 20 70 53 72 63 4c 69 73 74 20 2a 2f 0a 20 er pSrcList */.
1d2dd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
1d2de 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
1d2df 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
1d2e0 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b _AGG_FUNCTION: {
1d2e1 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 4e . /* The pN
1d2e2 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 74 65 73 C->nDepth==0 tes
1d2e3 74 20 63 61 75 73 65 73 20 61 67 67 72 65 67 61 t causes aggrega
1d2e4 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 te functions in
1d2e5 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 20 subqueries.
1d2e6 20 2a 2a 20 74 6f 20 62 65 20 69 67 6e 6f 72 65 ** to be ignore
1d2e7 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 d */. if( p
1d2e8 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b NC->nDepth==0 ){
1d2e9 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 . /* Chec
1d2ea 6b 20 74 6f 20 73 65 65 20 69 66 20 70 45 78 70 k to see if pExp
1d2eb 72 20 69 73 20 61 20 64 75 70 6c 69 63 61 74 65 r is a duplicate
1d2ec 20 6f 66 20 61 6e 6f 74 68 65 72 20 61 67 67 72 of another aggr
1d2ed 65 67 61 74 65 20 0a 20 20 20 20 20 20 20 20 2a egate . *
1d2ee 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 * function that
1d2ef 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 is already in th
1d2f0 65 20 70 41 67 67 49 6e 66 6f 20 73 74 72 75 63 e pAggInfo struc
1d2f1 74 75 72 65 0a 20 20 20 20 20 20 20 20 2a 2f 0a ture. */.
1d2f2 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41 struct A
1d2f3 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 49 74 ggInfo_func *pIt
1d2f4 65 6d 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61 em = pAggInfo->a
1d2f5 46 75 6e 63 3b 0a 20 20 20 20 20 20 20 20 66 6f Func;. fo
1d2f6 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66 r(i=0; i<pAggInf
1d2f7 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 o->nFunc; i++, p
1d2f8 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 Item++){.
1d2f9 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 if( sqlite3Ex
1d2fa 70 72 43 6f 6d 70 61 72 65 28 70 49 74 65 6d 2d prCompare(pItem-
1d2fb 3e 70 45 78 70 72 2c 20 70 45 78 70 72 29 20 29 >pExpr, pExpr) )
1d2fc 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 {. br
1d2fd 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d eak;. }
1d2fe 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1d2ff 20 20 20 69 66 28 20 69 3e 3d 70 41 67 67 49 6e if( i>=pAggIn
1d300 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0a 20 20 20 fo->nFunc ){.
1d301 20 20 20 20 20 20 20 2f 2a 20 70 45 78 70 72 20 /* pExpr
1d302 69 73 20 6f 72 69 67 69 6e 61 6c 2e 20 20 4d 61 is original. Ma
1d303 6b 65 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 ke a new entry i
1d304 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e n pAggInfo->aFun
1d305 63 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f c[]. */
1d306 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 65 6e . u8 en
1d307 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e c = ENC(pParse->
1d308 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 db);. i
1d309 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 46 75 6e = addAggInfoFun
1d30a 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 c(pParse->db, pA
1d30b 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 ggInfo);.
1d30c 20 20 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a 20 if( i>=0 ){.
1d30d 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d pItem
1d30e 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 46 = &pAggInfo->aF
1d30f 75 6e 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 unc[i];.
1d310 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 pItem->pExpr
1d311 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 = pExpr;.
1d312 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d 65 pItem->iMe
1d313 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d m = ++pParse->nM
1d314 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 em;.
1d315 70 49 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20 73 pItem->pFunc = s
1d316 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 qlite3FindFuncti
1d317 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20 on(pParse->db,.
1d318 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d319 20 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e (char*)pExpr->
1d31a 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e token.z, pExpr->
1d31b 74 6f 6b 65 6e 2e 6e 2c 0a 20 20 20 20 20 20 20 token.n,.
1d31c 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
1d31d 72 2d 3e 70 4c 69 73 74 20 3f 20 70 45 78 70 72 r->pList ? pExpr
1d31e 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a ->pList->nExpr :
1d31f 20 30 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 0, enc, 0);.
1d320 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 if( pEx
1d321 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 pr->flags & EP_D
1d322 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 20 istinct ){.
1d323 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e pItem->
1d324 69 44 69 73 74 69 6e 63 74 20 3d 20 70 50 61 72 iDistinct = pPar
1d325 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 se->nTab++;.
1d326 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1d327 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 pIt
1d328 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 em->iDistinct =
1d329 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 -1;.
1d32a 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1d32b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1d32c 2f 2a 20 4d 61 6b 65 20 70 45 78 70 72 20 70 6f /* Make pExpr po
1d32d 69 6e 74 20 74 6f 20 74 68 65 20 61 70 70 72 6f int to the appro
1d32e 70 72 69 61 74 65 20 70 41 67 67 49 6e 66 6f 2d priate pAggInfo-
1d32f 3e 61 46 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20 >aFunc[] entry.
1d330 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1d331 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 pExpr->iAgg =
1d332 69 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 i;. pExpr
1d333 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 ->pAggInfo = pAg
1d334 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 72 gInfo;. r
1d335 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b eturn WRC_Prune;
1d336 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1d337 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f }. return WRC_
1d338 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 73 74 61 74 Continue;.}.stat
1d339 69 63 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 ic int analyzeAg
1d33a 67 72 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74 gregatesInSelect
1d33b 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
1d33c 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 , Select *pSelec
1d33d 74 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 t){. NameContex
1d33e 74 20 2a 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 t *pNC = pWalker
1d33f 2d 3e 75 2e 70 4e 43 3b 0a 20 20 69 66 28 20 70 ->u.pNC;. if( p
1d340 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b NC->nDepth==0 ){
1d341 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68 . pNC->nDepth
1d342 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 57 ++;. sqlite3W
1d343 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 alkSelect(pWalke
1d344 72 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 r, pSelect);.
1d345 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a pNC->nDepth--;.
1d346 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 return WRC_P
1d347 72 75 6e 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 rune;. }else{.
1d348 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f return WRC_Co
1d349 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f ntinue;. }.}../
1d34a 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 *.** Analyze the
1d34b 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f given expressio
1d34c 6e 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 67 n looking for ag
1d34d 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
1d34e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 76 61 72 s and.** for var
1d34f 69 61 62 6c 65 73 20 74 68 61 74 20 6e 65 65 64 iables that need
1d350 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 to be added to
1d351 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67 the pParse->aAgg
1d352 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b [] array..** Mak
1d353 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 65 6e 74 e additional ent
1d354 72 69 65 73 20 74 6f 20 74 68 65 20 70 50 61 72 ries to the pPar
1d355 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 se->aAgg[] array
1d356 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a as necessary..*
1d357 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1d358 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 e should only be
1d359 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68 called after th
1d35a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 e expression has
1d35b 20 62 65 65 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65 been.** analyze
1d35c 64 20 62 79 20 73 71 6c 69 74 65 33 52 65 73 6f d by sqlite3Reso
1d35d 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a lveExprNames()..
1d35e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1d35f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
1d360 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 prAnalyzeAggrega
1d361 74 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 tes(NameContext
1d362 2a 70 4e 43 2c 20 45 78 70 72 20 2a 70 45 78 70 *pNC, Expr *pExp
1d363 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a r){. Walker w;.
1d364 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 w.xExprCallbac
1d365 6b 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 k = analyzeAggre
1d366 67 61 74 65 3b 0a 20 20 77 2e 78 53 65 6c 65 63 gate;. w.xSelec
1d367 74 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c tCallback = anal
1d368 79 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e 53 yzeAggregatesInS
1d369 65 6c 65 63 74 3b 0a 20 20 77 2e 75 2e 70 4e 43 elect;. w.u.pNC
1d36a 20 3d 20 70 4e 43 3b 0a 20 20 73 71 6c 69 74 65 = pNC;. sqlite
1d36b 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 3WalkExpr(&w, pE
1d36c 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 xpr);.}../*.** C
1d36d 61 6c 6c 20 73 71 6c 69 74 65 33 45 78 70 72 41 all sqlite3ExprA
1d36e 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 nalyzeAggregates
1d36f 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 () for every exp
1d370 72 65 73 73 69 6f 6e 20 69 6e 20 61 6e 0a 2a 2a ression in an.**
1d371 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
1d372 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 . Return the nu
1d373 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a mber of errors..
1d374 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
1d375 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 r is found, the
1d376 61 6e 61 6c 79 73 69 73 20 69 73 20 63 75 74 20 analysis is cut
1d377 73 68 6f 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 short..*/.SQLITE
1d378 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1d379 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 lite3ExprAnalyze
1d37a 41 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 AggList(NameCont
1d37b 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72 4c 69 ext *pNC, ExprLi
1d37c 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 73 74 st *pList){. st
1d37d 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
1d37e 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 em *pItem;. int
1d37f 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 i;. if( pList
1d380 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 6d ){. for(pItem
1d381 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 =pList->a, i=0;
1d382 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 i<pList->nExpr;
1d383 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
1d384 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1d385 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 AnalyzeAggregate
1d386 73 28 70 4e 43 2c 20 70 49 74 65 6d 2d 3e 70 45 s(pNC, pItem->pE
1d387 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a xpr);. }. }.
1d388 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 }../*.** Allocat
1d389 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 20 e or deallocate
1d38a 74 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65 temporary use re
1d38b 67 69 73 74 65 72 73 20 64 75 72 69 6e 67 20 63 gisters during c
1d38c 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 2e 0a ode generation..
1d38d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1d38e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 E int sqlite3Get
1d38f 54 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a 70 TempReg(Parse *p
1d390 50 61 72 73 65 29 7b 0a 20 20 69 66 28 20 70 50 Parse){. if( pP
1d391 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d arse->nTempReg==
1d392 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1d393 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
1d394 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 }. return pPa
1d395 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 2d 2d rse->aTempReg[--
1d396 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 pParse->nTempReg
1d397 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 ];.}.SQLITE_PRIV
1d398 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1d399 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 ReleaseTempReg(P
1d39a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
1d39b 74 20 69 52 65 67 29 7b 0a 20 20 69 66 28 20 69 t iReg){. if( i
1d39c 52 65 67 20 26 26 20 70 50 61 72 73 65 2d 3e 6e Reg && pParse->n
1d39d 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a TempReg<ArraySiz
1d39e 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 e(pParse->aTempR
1d39f 65 67 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 eg) ){. sqlit
1d3a0 65 33 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 e3ExprWritableRe
1d3a1 67 69 73 74 65 72 28 70 50 61 72 73 65 2c 20 69 gister(pParse, i
1d3a2 52 65 67 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 Reg, iReg);.
1d3a3 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 pParse->aTempReg
1d3a4 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 [pParse->nTempRe
1d3a5 67 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d g++] = iReg;. }
1d3a6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
1d3a7 74 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 te or deallocate
1d3a8 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 a block of nReg
1d3a9 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 consecutive reg
1d3aa 69 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45 isters.*/.SQLITE
1d3ab 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1d3ac 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 ite3GetTempRange
1d3ad 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1d3ae 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 int nReg){. int
1d3af 20 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61 i, n;. i = pPa
1d3b0 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a rse->iRangeReg;.
1d3b1 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52 n = pParse->nR
1d3b2 61 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20 6e angeReg;. if( n
1d3b3 52 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64 41 Reg<=n && !usedA
1d3b4 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 sColumnCache(pPa
1d3b5 72 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29 rse, i, i+n-1) )
1d3b6 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 {. pParse->iR
1d3b7 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b angeReg += nReg;
1d3b8 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 . pParse->nRa
1d3b9 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a ngeReg -= nReg;.
1d3ba 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d }else{. i =
1d3bb 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b pParse->nMem+1;
1d3bc 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 . pParse->nMe
1d3bd 6d 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20 m += nReg;. }.
1d3be 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c return i;.}.SQL
1d3bf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1d3c0 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
1d3c1 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a empRange(Parse *
1d3c2 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 pParse, int iReg
1d3c3 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 , int nReg){. i
1d3c4 66 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e f( nReg>pParse->
1d3c5 6e 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20 20 nRangeReg ){.
1d3c6 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 pParse->nRangeR
1d3c7 65 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 eg = nReg;. p
1d3c8 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 Parse->iRangeReg
1d3c9 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a = iReg;. }.}..
1d3ca 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1d3cb 45 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a End of expr.c **
1d3cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d3cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d3ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1d3cf 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1d3d0 42 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72 Begin file alter
1d3d1 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1d3d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d3d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1d3d4 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75 /*.** 2005 Febru
1d3d5 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ary 15.**.** The
1d3d6 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1d3d7 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1d3d8 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1d3d9 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1d3da 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1d3db 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1d3dc 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1d3dd 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1d3de 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1d3df 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1d3e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1d3e1 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1d3e2 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1d3e3 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1d3e4 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1d3e5 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1d3e6 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1d3e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d3e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d3e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d3ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d3eb 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
1d3ec 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f le contains C co
1d3ed 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 de routines that
1d3ee 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 used to generat
1d3ef 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74 e VDBE code.** t
1d3f0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 hat implements t
1d3f1 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 he ALTER TABLE c
1d3f2 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 ommand..**.** $I
1d3f3 64 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31 2e 34 d: alter.c,v 1.4
1d3f4 39 20 32 30 30 38 2f 31 30 2f 33 30 20 31 37 3a 9 2008/10/30 17:
1d3f5 32 31 3a 31 33 20 64 61 6e 69 65 6c 6b 31 39 37 21:13 danielk197
1d3f6 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 7 Exp $.*/../*.*
1d3f7 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 * The code in th
1d3f8 69 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69 is file only exi
1d3f9 73 74 73 20 69 66 20 77 65 20 61 72 65 20 6e 6f sts if we are no
1d3fa 74 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a t omitting the.*
1d3fb 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f * ALTER TABLE lo
1d3fc 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62 75 69 gic from the bui
1d3fd 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ld..*/.#ifndef S
1d3fe 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 QLITE_OMIT_ALTER
1d3ff 54 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 TABLE.../*.** Th
1d400 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
1d401 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 sed by SQL gener
1d402 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e ated to implemen
1d403 74 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45 52 20 t the .** ALTER
1d404 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 TABLE command. T
1d405 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
1d406 74 20 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 t is the text of
1d407 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 a CREATE TABLE
1d408 6f 72 0a 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 or.** CREATE IND
1d409 45 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 EX command. The
1d40a 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c second is a tabl
1d40b 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c e name. The tabl
1d40c 65 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 74 68 e name in .** th
1d40d 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f e CREATE TABLE o
1d40e 72 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 r CREATE INDEX s
1d40f 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c tatement is repl
1d410 61 63 65 64 20 77 69 74 68 20 74 68 65 20 74 68 aced with the th
1d411 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 ird.** argument
1d412 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 72 and the result r
1d413 65 74 75 72 6e 65 64 2e 20 45 78 61 6d 70 6c 65 eturned. Example
1d414 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f s:.**.** sqlite_
1d415 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52 rename_table('CR
1d416 45 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 EATE TABLE abc(a
1d417 2c 20 62 2c 20 63 29 27 2c 20 27 64 65 66 27 29 , b, c)', 'def')
1d418 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41 .** -> 'CREA
1d419 54 45 20 54 41 42 4c 45 20 64 65 66 28 61 2c 20 TE TABLE def(a,
1d41a 62 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c b, c)'.**.** sql
1d41b 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 ite_rename_table
1d41c 28 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 ('CREATE INDEX i
1d41d 20 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 64 65 ON abc(a)', 'de
1d41e 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 f').** -> 'C
1d41f 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e REATE INDEX i ON
1d420 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a def(a, b, c)'.*
1d421 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
1d422 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 0a 20 nameTableFunc(.
1d423 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1d424 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
1d425 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
1d426 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
1d427 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
1d428 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73 const *zSql = s
1d429 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1d42a 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e t(argv[0]);. un
1d42b 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 signed char cons
1d42c 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20 t *zTableName =
1d42d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1d42e 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 xt(argv[1]);..
1d42f 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b int token;. Tok
1d430 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e 73 69 en tname;. unsi
1d431 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 gned char const
1d432 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20 *zCsr = zSql;.
1d433 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63 int len = 0;. c
1d434 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71 har *zRet;.. sq
1d435 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
1d436 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
1d437 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
1d438 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 . /* The princi
1d439 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 ple used to loca
1d43a 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d te the table nam
1d43b 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 e in the CREATE
1d43c 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74 TABLE . ** stat
1d43d 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68 ement is that th
1d43e 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 e table name is
1d43f 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70 the first non-sp
1d440 61 63 65 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20 ace token that.
1d441 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69 61 74 65 ** is immediate
1d442 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 ly followed by a
1d443 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f 55 53 49 TK_LP or TK_USI
1d444 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 NG token.. */.
1d445 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 if( zSql ){.
1d446 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 66 28 20 do {. if(
1d447 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20 !*zCsr ){.
1d448 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 /* Ran out of
1d449 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e input before fin
1d44a 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 6e 67 20 ding an opening
1d44b 62 72 61 63 6b 65 74 2e 20 52 65 74 75 72 6e 20 bracket. Return
1d44c 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 NULL. */.
1d44d 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
1d44e 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 .. /* Store
1d44f 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 the token that
1d450 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 zCsr points to i
1d451 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 n tname. */.
1d452 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 tname.z = zCsr
1d453 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 ;. tname.n
1d454 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a = len;.. /*
1d455 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f Advance zCsr to
1d456 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e the next token.
1d457 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 Store that toke
1d458 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e n type in 'token
1d459 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 ',. ** and
1d45a 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c its length in 'l
1d45b 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 en' (to be used
1d45c 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f next iteration o
1d45d 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 f this loop)..
1d45e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 */. do
1d45f 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b {. zCsr +
1d460 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c = len;. l
1d461 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 en = sqlite3GetT
1d462 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 oken(zCsr, &toke
1d463 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77 68 69 6c n);. } whil
1d464 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 e( token==TK_SPA
1d465 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 CE );. asse
1d466 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 20 20 20 rt( len>0 );.
1d467 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 21 } while( token!
1d468 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b 65 6e 21 =TK_LP && token!
1d469 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a 0a 20 20 =TK_USING );..
1d46a 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65 33 zRet = sqlite3
1d46b 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 2e 2a MPrintf(db, "%.*
1d46c 73 5c 22 25 77 5c 22 25 73 22 2c 20 74 6e 61 6d s\"%w\"%s", tnam
1d46d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c e.z - zSql, zSql
1d46e 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65 , . zTable
1d46f 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e Name, tname.z+tn
1d470 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 ame.n);. sqli
1d471 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
1d472 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d context, zRet, -
1d473 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 1, SQLITE_DYNAMI
1d474 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 C);. }.}..#ifnd
1d475 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1d476 52 49 47 47 45 52 0a 2f 2a 20 54 68 69 73 20 66 RIGGER./* This f
1d477 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
1d478 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 by SQL generated
1d479 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
1d47a 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 e.** ALTER TABLE
1d47b 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69 command. The fi
1d47c 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
1d47d 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43 52 the text of a CR
1d47e 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 2a 2a EATE TRIGGER .**
1d47f 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 statement. The
1d480 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c second is a tabl
1d481 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c e name. The tabl
1d482 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 e name in the CR
1d483 45 41 54 45 20 0a 2a 2a 20 54 52 49 47 47 45 52 EATE .** TRIGGER
1d484 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 statement is re
1d485 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20 placed with the
1d486 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 61 third argument a
1d487 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 0a 2a nd the result .*
1d488 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 * returned. This
1d489 20 69 73 20 61 6e 61 6c 61 67 6f 75 73 20 74 6f is analagous to
1d48a 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 renameTableFunc
1d48b 28 29 20 61 62 6f 76 65 2c 20 65 78 63 65 70 74 () above, except
1d48c 20 66 6f 72 20 43 52 45 41 54 45 0a 2a 2a 20 54 for CREATE.** T
1d48d 52 49 47 47 45 52 2c 20 6e 6f 74 20 43 52 45 41 RIGGER, not CREA
1d48e 54 45 20 49 4e 44 45 58 20 61 6e 64 20 43 52 45 TE INDEX and CRE
1d48f 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f 0a 73 74 ATE TABLE..*/.st
1d490 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 atic void rename
1d491 54 72 69 67 67 65 72 46 75 6e 63 28 0a 20 20 73 TriggerFunc(. s
1d492 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1d493 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
1d494 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
1d495 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
1d496 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 unsigned char c
1d497 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c onst *zSql = sql
1d498 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1d499 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 argv[0]);. unsi
1d49a 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 gned char const
1d49b 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71 *zTableName = sq
1d49c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1d49d 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e (argv[1]);.. in
1d49e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e t token;. Token
1d49f 20 74 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 64 69 tname;. int di
1d4a0 73 74 20 3d 20 33 3b 0a 20 20 75 6e 73 69 67 6e st = 3;. unsign
1d4a1 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a ed char const *z
1d4a2 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e Csr = zSql;. in
1d4a3 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 t len = 0;. cha
1d4a4 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71 6c 69 r *zRet;.. sqli
1d4a5 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 te3 *db = sqlite
1d4a6 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
1d4a7 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 dle(context);..
1d4a8 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c /* The principl
1d4a9 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 e used to locate
1d4aa 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 the table name
1d4ab 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 52 in the CREATE TR
1d4ac 49 47 47 45 52 20 0a 20 20 2a 2a 20 73 74 61 74 IGGER . ** stat
1d4ad 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68 ement is that th
1d4ae 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 e table name is
1d4af 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 the first token
1d4b0 74 68 61 74 20 69 73 20 69 6d 6d 65 64 69 61 74 that is immediat
1d4b1 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65 63 65 64 edly. ** preced
1d4b2 65 64 20 62 79 20 65 69 74 68 65 72 20 54 4b 5f ed by either TK_
1d4b3 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20 61 6e 64 ON or TK_DOT and
1d4b4 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 20 66 6f immediatedly fo
1d4b5 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65 0a 20 20 llowed by one.
1d4b6 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e 2c 20 54 ** of TK_WHEN, T
1d4b7 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b 5f 46 4f K_BEGIN or TK_FO
1d4b8 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 R.. */. if( zS
1d4b9 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 0a ql ){. do {..
1d4ba 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72 if( !*zCsr
1d4bb 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 ){. /* R
1d4bc 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 an out of input
1d4bd 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 74 before finding t
1d4be 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 52 he table name. R
1d4bf 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 eturn NULL. */.
1d4c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
1d4c1 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
1d4c2 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e Store the token
1d4c3 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e 74 that zCsr point
1d4c4 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a s to in tname. *
1d4c5 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20 /. tname.z
1d4c6 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e = zCsr;. tn
1d4c7 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 ame.n = len;..
1d4c8 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a /* Advance z
1d4c9 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 Csr to the next
1d4ca 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 token. Store tha
1d4cb 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 t token type in
1d4cc 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 'token',. *
1d4cd 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 * and its length
1d4ce 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 in 'len' (to be
1d4cf 20 75 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 used next itera
1d4d0 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f tion of this loo
1d4d1 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 p).. */.
1d4d2 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 do {.
1d4d3 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 zCsr += len;.
1d4d4 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 len = sqlit
1d4d5 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c e3GetToken(zCsr,
1d4d6 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 &token);.
1d4d7 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 }while( token==T
1d4d8 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 K_SPACE );.
1d4d9 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 assert( len>0 )
1d4da 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 56 61 72 69 ;.. /* Vari
1d4db 61 62 6c 65 20 27 64 69 73 74 27 20 73 74 6f 72 able 'dist' stor
1d4dc 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 es the number of
1d4dd 20 74 6f 6b 65 6e 73 20 72 65 61 64 20 73 69 6e tokens read sin
1d4de 63 65 20 74 68 65 20 6d 6f 73 74 0a 20 20 20 20 ce the most.
1d4df 20 20 2a 2a 20 72 65 63 65 6e 74 20 54 4b 5f 44 ** recent TK_D
1d4e0 4f 54 20 6f 72 20 54 4b 5f 4f 4e 2e 20 54 68 69 OT or TK_ON. Thi
1d4e1 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 s means that whe
1d4e2 6e 20 61 20 57 48 45 4e 2c 20 46 4f 52 20 6f 72 n a WHEN, FOR or
1d4e3 20 42 45 47 49 4e 20 0a 20 20 20 20 20 20 2a 2a BEGIN . **
1d4e4 20 74 6f 6b 65 6e 20 69 73 20 72 65 61 64 20 61 token is read a
1d4e5 6e 64 20 27 64 69 73 74 27 20 65 71 75 61 6c 73 nd 'dist' equals
1d4e6 20 32 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 2, the conditio
1d4e7 6e 20 73 74 61 74 65 64 20 61 62 6f 76 65 0a 20 n stated above.
1d4e8 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6d 65 ** to be me
1d4e9 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 t.. **.
1d4ea 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 4f ** Note that O
1d4eb 4e 20 63 61 6e 6e 6f 74 20 62 65 20 61 20 64 61 N cannot be a da
1d4ec 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 6f 72 tabase, table or
1d4ed 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20 73 6f column name, so
1d4ee 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20 . ** there
1d4ef 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 6f is no need to wo
1d4f0 72 72 79 20 61 62 6f 75 74 20 73 79 6e 74 61 78 rry about syntax
1d4f1 20 6c 69 6b 65 20 0a 20 20 20 20 20 20 2a 2a 20 like . **
1d4f2 22 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 "CREATE TRIGGER
1d4f3 2e 2e 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 42 45 47 ... ON ON.ON BEG
1d4f4 49 4e 20 2e 2e 2e 22 20 65 74 63 2e 0a 20 20 20 IN ..." etc..
1d4f5 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 69 73 74 */. dist
1d4f6 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 74 6f ++;. if( to
1d4f7 6b 65 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74 ken==TK_DOT || t
1d4f8 6f 6b 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0a 20 oken==TK_ON ){.
1d4f9 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 30 3b dist = 0;
1d4fa 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 77 . }. } w
1d4fb 68 69 6c 65 28 20 64 69 73 74 21 3d 32 20 7c 7c hile( dist!=2 ||
1d4fc 20 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45 4e (token!=TK_WHEN
1d4fd 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46 4f && token!=TK_FO
1d4fe 52 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 42 R && token!=TK_B
1d4ff 45 47 49 4e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a EGIN) );.. /*
1d500 20 56 61 72 69 61 62 6c 65 20 74 6e 61 6d 65 20 Variable tname
1d501 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 now contains the
1d502 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 74 token that is t
1d503 68 65 20 6f 6c 64 20 74 61 62 6c 65 2d 6e 61 6d he old table-nam
1d504 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 e. ** in the
1d505 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73 CREATE TRIGGER s
1d506 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f tatement.. */
1d507 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 . zRet = sqli
1d508 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 te3MPrintf(db, "
1d509 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 74 %.*s\"%w\"%s", t
1d50a 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a name.z - zSql, z
1d50b 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 Sql, . zTa
1d50c 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a bleName, tname.z
1d50d 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 +tname.n);. s
1d50e 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
1d50f 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 xt(context, zRet
1d510 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e , -1, SQLITE_DYN
1d511 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e AMIC);. }.}.#en
1d512 64 69 66 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 dif /* !SQLITE
1d513 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f _OMIT_TRIGGER */
1d514 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 ../*.** Register
1d515 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 built-in functi
1d516 6f 6e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 ons used to help
1d517 20 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52 implement ALTER
1d518 20 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54 45 TABLE.*/.SQLITE
1d519 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1d51a 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 lite3AlterFuncti
1d51b 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ons(sqlite3 *db)
1d51c 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 {. sqlite3Creat
1d51d 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 eFunc(db, "sqlit
1d51e 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22 2c e_rename_table",
1d51f 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 2, SQLITE_UTF8,
1d520 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 0,.
1d521 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e ren
1d522 61 6d 65 54 61 62 6c 65 46 75 6e 63 2c 20 30 2c ameTableFunc, 0,
1d523 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 0);.#ifndef SQL
1d524 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1d525 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 . sqlite3Create
1d526 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65 Func(db, "sqlite
1d527 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 22 _rename_trigger"
1d528 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , 2, SQLITE_UTF8
1d529 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 , 0,.
1d52a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
1d52b 6e 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 2c nameTriggerFunc,
1d52c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0, 0);.#endif.}
1d52d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1d52e 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 57 the text of a W
1d52f 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20 HERE expression
1d530 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 which can be use
1d531 64 20 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a d to select all.
1d532 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 ** temporary tri
1d533 67 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 ggers on table p
1d534 54 61 62 20 66 72 6f 6d 20 74 68 65 20 73 71 6c Tab from the sql
1d535 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 ite_temp_master
1d536 74 61 62 6c 65 2e 20 49 66 0a 2a 2a 20 74 61 62 table. If.** tab
1d537 6c 65 20 70 54 61 62 20 68 61 73 20 6e 6f 20 74 le pTab has no t
1d538 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 emporary trigger
1d539 73 2c 20 6f 72 20 69 73 20 69 74 73 65 6c 66 20 s, or is itself
1d53a 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 0a 2a stored in the .*
1d53b 2a 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 * temporary data
1d53c 62 61 73 65 2c 20 4e 55 4c 4c 20 69 73 20 72 65 base, NULL is re
1d53d 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 turned..*/.stati
1d53e 63 20 63 68 61 72 20 2a 77 68 65 72 65 54 65 6d c char *whereTem
1d53f 70 54 72 69 67 67 65 72 73 28 50 61 72 73 65 20 pTriggers(Parse
1d540 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a *pParse, Table *
1d541 70 54 61 62 29 7b 0a 20 20 54 72 69 67 67 65 72 pTab){. Trigger
1d542 20 2a 70 54 72 69 67 3b 0a 20 20 63 68 61 72 20 *pTrig;. char
1d543 2a 7a 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 63 *zWhere = 0;. c
1d544 68 61 72 20 2a 74 6d 70 20 3d 20 30 3b 0a 20 20 har *tmp = 0;.
1d545 63 6f 6e 73 74 20 53 63 68 65 6d 61 20 2a 70 54 const Schema *pT
1d546 65 6d 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72 empSchema = pPar
1d547 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 se->db->aDb[1].p
1d548 53 63 68 65 6d 61 3b 20 2f 2a 20 54 65 6d 70 20 Schema; /* Temp
1d549 64 62 20 73 63 68 65 6d 61 20 2a 2f 0a 0a 20 20 db schema */..
1d54a 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 /* If the table
1d54b 69 73 20 6e 6f 74 20 6c 6f 63 61 74 65 64 20 69 is not located i
1d54c 6e 20 74 68 65 20 74 65 6d 70 2d 64 62 20 28 69 n the temp-db (i
1d54d 6e 20 77 68 69 63 68 20 63 61 73 65 20 4e 55 4c n which case NUL
1d54e 4c 20 69 73 20 0a 20 20 2a 2a 20 72 65 74 75 72 L is . ** retur
1d54f 6e 65 64 2c 20 6c 6f 6f 70 20 74 68 72 6f 75 67 ned, loop throug
1d550 68 20 74 68 65 20 74 61 62 6c 65 73 20 6c 69 73 h the tables lis
1d551 74 20 6f 66 20 74 72 69 67 67 65 72 73 2e 20 46 t of triggers. F
1d552 6f 72 20 65 61 63 68 20 74 72 69 67 67 65 72 0a or each trigger.
1d553 20 20 2a 2a 20 74 68 61 74 20 69 73 20 6e 6f 74 ** that is not
1d554 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 65 6d part of the tem
1d555 70 2d 64 62 20 73 63 68 65 6d 61 2c 20 61 64 64 p-db schema, add
1d556 20 61 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 a clause to the
1d557 20 57 48 45 52 45 20 0a 20 20 2a 2a 20 65 78 70 WHERE . ** exp
1d558 72 65 73 73 69 6f 6e 20 62 65 69 6e 67 20 62 75 ression being bu
1d559 69 6c 74 20 75 70 20 69 6e 20 7a 57 68 65 72 65 ilt up in zWhere
1d55a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 .. */. if( pTa
1d55b 62 2d 3e 70 53 63 68 65 6d 61 21 3d 70 54 65 6d b->pSchema!=pTem
1d55c 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 73 pSchema ){. s
1d55d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1d55e 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 66 6f 72 rse->db;. for
1d55f 28 20 70 54 72 69 67 3d 70 54 61 62 2d 3e 70 54 ( pTrig=pTab->pT
1d560 72 69 67 67 65 72 3b 20 70 54 72 69 67 3b 20 70 rigger; pTrig; p
1d561 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78 Trig=pTrig->pNex
1d562 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 t ){. if( p
1d563 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 Trig->pSchema==p
1d564 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 TempSchema ){.
1d565 20 20 20 20 20 20 69 66 28 20 21 7a 57 68 65 72 if( !zWher
1d566 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a e ){. z
1d567 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d Where = sqlite3M
1d568 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65 Printf(db, "name
1d569 3d 25 51 22 2c 20 70 54 72 69 67 2d 3e 6e 61 6d =%Q", pTrig->nam
1d56a 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 e);. }els
1d56b 65 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6d 70 e{. tmp
1d56c 20 3d 20 7a 57 68 65 72 65 3b 0a 20 20 20 20 20 = zWhere;.
1d56d 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73 71 zWhere = sq
1d56e 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
1d56f 20 22 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22 "%s OR name=%Q"
1d570 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 69 67 2d , zWhere, pTrig-
1d571 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 >name);.
1d572 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1d573 64 62 2c 20 74 6d 70 29 3b 0a 20 20 20 20 20 20 db, tmp);.
1d574 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1d575 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a }. }. return z
1d576 57 68 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Where;.}../*.**
1d577 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1d578 20 64 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 drop and reload
1d579 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 the internal re
1d57a 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
1d57b 74 61 62 6c 65 0a 2a 2a 20 70 54 61 62 20 66 72 table.** pTab fr
1d57c 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c om the database,
1d57d 20 69 6e 63 6c 75 64 69 6e 67 20 74 72 69 67 67 including trigg
1d57e 65 72 73 20 61 6e 64 20 74 65 6d 70 6f 72 61 72 ers and temporar
1d57f 79 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 20 41 y triggers..** A
1d580 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65 20 69 73 rgument zName is
1d581 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1d582 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 table in the da
1d583 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 74 tabase schema at
1d584 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 74 68 65 .** the time the
1d585 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 generated code
1d586 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68 69 is executed. Thi
1d587 73 20 63 61 6e 20 62 65 20 64 69 66 66 65 72 65 s can be differe
1d588 6e 74 20 66 72 6f 6d 0a 2a 2a 20 70 54 61 62 2d nt from.** pTab-
1d589 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69 73 20 66 >zName if this f
1d58a 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 unction is being
1d58b 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64 65 20 called to code
1d58c 70 61 72 74 20 6f 66 20 61 6e 20 0a 2a 2a 20 22 part of an .** "
1d58d 41 4c 54 45 52 20 54 41 42 4c 45 20 52 45 4e 41 ALTER TABLE RENA
1d58e 4d 45 20 54 4f 22 20 73 74 61 74 65 6d 65 6e 74 ME TO" statement
1d58f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1d590 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 reloadTableSche
1d591 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ma(Parse *pParse
1d592 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 , Table *pTab, c
1d593 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
1d594 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 ){. Vdbe *v;.
1d595 63 68 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 char *zWhere;.
1d596 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
1d597 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1d598 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 dex of database
1d599 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20 containing pTab
1d59a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1d59b 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 E_OMIT_TRIGGER.
1d59c 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b Trigger *pTrig;
1d59d 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73 .#endif.. v = s
1d59e 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
1d59f 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 arse);. if( !v
1d5a0 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
1d5a1 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1d5a2 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
1d5a3 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 pParse->db) );.
1d5a4 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
1d5a5 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 hemaToIndex(pPar
1d5a6 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 se->db, pTab->pS
1d5a7 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 chema);. assert
1d5a8 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 23 69 66 ( iDb>=0 );..#if
1d5a9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d5aa 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 44 72 _TRIGGER. /* Dr
1d5ab 6f 70 20 61 6e 79 20 74 61 62 6c 65 20 74 72 69 op any table tri
1d5ac 67 67 65 72 73 20 66 72 6f 6d 20 74 68 65 20 69 ggers from the i
1d5ad 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20 nternal schema.
1d5ae 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69 67 3d 70 */. for(pTrig=p
1d5af 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 70 Tab->pTrigger; p
1d5b0 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69 Trig; pTrig=pTri
1d5b1 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 g->pNext){. i
1d5b2 6e 74 20 69 54 72 69 67 44 62 20 3d 20 73 71 6c nt iTrigDb = sql
1d5b3 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 ite3SchemaToInde
1d5b4 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 x(pParse->db, pT
1d5b5 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 rig->pSchema);.
1d5b6 20 20 20 61 73 73 65 72 74 28 20 69 54 72 69 67 assert( iTrig
1d5b7 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54 72 69 67 Db==iDb || iTrig
1d5b8 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71 6c Db==1 );. sql
1d5b9 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
1d5ba 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 , OP_DropTrigger
1d5bb 2c 20 69 54 72 69 67 44 62 2c 20 30 2c 20 30 2c , iTrigDb, 0, 0,
1d5bc 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c 20 30 29 pTrig->name, 0)
1d5bd 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
1d5be 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 61 62 6c /* Drop the tabl
1d5bf 65 20 61 6e 64 20 69 6e 64 65 78 20 66 72 6f 6d e and index from
1d5c0 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 the internal sc
1d5c1 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 hema */. sqlite
1d5c2 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
1d5c3 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 69 44 62 P_DropTable, iDb
1d5c4 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e , 0, 0, pTab->zN
1d5c5 61 6d 65 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 52 ame, 0);.. /* R
1d5c6 65 6c 6f 61 64 20 74 68 65 20 74 61 62 6c 65 2c eload the table,
1d5c7 20 69 6e 64 65 78 20 61 6e 64 20 70 65 72 6d 61 index and perma
1d5c8 6e 65 6e 74 20 74 72 69 67 67 65 72 20 73 63 68 nent trigger sch
1d5c9 65 6d 61 73 2e 20 2a 2f 0a 20 20 7a 57 68 65 72 emas. */. zWher
1d5ca 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e e = sqlite3MPrin
1d5cb 74 66 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 tf(pParse->db, "
1d5cc 74 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e tbl_name=%Q", zN
1d5cd 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 57 68 ame);. if( !zWh
1d5ce 65 72 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ere ) return;.
1d5cf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1d5d0 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 4(v, OP_ParseSch
1d5d1 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 ema, iDb, 0, 0,
1d5d2 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d zWhere, P4_DYNAM
1d5d3 49 43 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 IC);..#ifndef SQ
1d5d4 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
1d5d5 52 0a 20 20 2f 2a 20 4e 6f 77 2c 20 69 66 20 74 R. /* Now, if t
1d5d6 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 he table is not
1d5d7 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 74 65 stored in the te
1d5d8 6d 70 20 64 61 74 61 62 61 73 65 2c 20 72 65 6c mp database, rel
1d5d9 6f 61 64 20 61 6e 79 20 74 65 6d 70 20 0a 20 20 oad any temp .
1d5da 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 44 6f 6e ** triggers. Don
1d5db 27 74 20 75 73 65 20 49 4e 28 2e 2e 2e 29 20 69 't use IN(...) i
1d5dc 6e 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4f 4d n case SQLITE_OM
1d5dd 49 54 5f 53 55 42 51 55 45 52 59 20 69 73 20 64 IT_SUBQUERY is d
1d5de 65 66 69 6e 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 efined. . */.
1d5df 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72 if( (zWhere=wher
1d5e0 65 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50 eTempTriggers(pP
1d5e1 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20 arse, pTab))!=0
1d5e2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1d5e3 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 beAddOp4(v, OP_P
1d5e4 61 72 73 65 53 63 68 65 6d 61 2c 20 31 2c 20 30 arseSchema, 1, 0
1d5e5 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f , 0, zWhere, P4_
1d5e6 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 DYNAMIC);. }.#e
1d5e7 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 ndif.}../*.** Ge
1d5e8 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69 nerate code to i
1d5e9 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 22 41 4c mplement the "AL
1d5ea 54 45 52 20 54 41 42 4c 45 20 78 78 78 20 52 45 TER TABLE xxx RE
1d5eb 4e 41 4d 45 20 54 4f 20 79 79 79 22 20 0a 2a 2a NAME TO yyy" .**
1d5ec 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a 53 51 command. .*/.SQ
1d5ed 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1d5ee 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65 d sqlite3AlterRe
1d5ef 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20 50 61 72 nameTable(. Par
1d5f0 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
1d5f1 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 /* Parser
1d5f2 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53 context. */. S
1d5f3 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 rcList *pSrc,
1d5f4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1d5f5 74 61 62 6c 65 20 74 6f 20 72 65 6e 61 6d 65 2e table to rename.
1d5f6 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 */. Token *pNa
1d5f7 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 me
1d5f8 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65 /* The new table
1d5f9 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69 name. */.){. i
1d5fa 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
1d5fb 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
1d5fc 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 base that contai
1d5fd 6e 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a ns the table */.
1d5fe 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 char *zDb;
1d5ff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1d600 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 ame of database
1d601 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a iDb */. Table *
1d602 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 pTab;
1d603 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e /* Table bein
1d604 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 63 g renamed */. c
1d605 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 har *zName = 0;
1d606 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c /* NULL
1d607 2d 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73 -terminated vers
1d608 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20 ion of pName */
1d609 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1d60a 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 pParse->db; /*
1d60b 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 Database connect
1d60c 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 ion */. int nTa
1d60d 62 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 bName;
1d60e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1d60f 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 UTF-8 characters
1d610 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a in zTabName */.
1d611 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
1d612 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f abName; /* O
1d613 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20 riginal name of
1d614 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 56 the table */. V
1d615 64 62 65 20 2a 76 3b 0a 23 69 66 6e 64 65 66 20 dbe *v;.#ifndef
1d616 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
1d617 47 45 52 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 GER. char *zWhe
1d618 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 re = 0;
1d619 2f 2a 20 57 68 65 72 65 20 63 6c 61 75 73 65 20 /* Where clause
1d61a 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d 70 20 74 to locate temp t
1d61b 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69 riggers */.#endi
1d61c 66 0a 20 20 69 6e 74 20 69 73 56 69 72 74 75 61 f. int isVirtua
1d61d 6c 52 65 6e 61 6d 65 20 3d 20 30 3b 20 20 2f 2a lRename = 0; /*
1d61e 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 True if this is
1d61f 20 61 20 76 2d 74 61 62 6c 65 20 77 69 74 68 20 a v-table with
1d620 61 6e 20 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a an xRename() */.
1d621 20 20 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c . if( db->mal
1d622 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f locFailed ) goto
1d623 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 exit_rename_tab
1d624 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 le;. assert( pS
1d625 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 rc->nSrc==1 );.
1d626 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1d627 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 BtreeHoldsAllMut
1d628 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 exes(pParse->db)
1d629 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71 );.. pTab = sq
1d62a 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
1d62b 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63 (pParse, 0, pSrc
1d62c 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 ->a[0].zName, pS
1d62d 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 rc->a[0].zDataba
1d62e 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 se);. if( !pTab
1d62f 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e ) goto exit_ren
1d630 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 69 44 62 ame_table;. iDb
1d631 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 = sqlite3Schema
1d632 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e ToIndex(pParse->
1d633 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d db, pTab->pSchem
1d634 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e a);. zDb = db->
1d635 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a aDb[iDb].zName;.
1d636 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e 55 4c 4c . /* Get a NULL
1d637 20 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73 terminated vers
1d638 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74 ion of the new t
1d639 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 able name. */.
1d63a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e zName = sqlite3N
1d63b 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
1d63c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 pName);. if( !
1d63d 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 65 78 69 zName ) goto exi
1d63e 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a t_rename_table;.
1d63f 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 . /* Check that
1d640 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 a table or inde
1d641 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d 65 27 20 x named 'zName'
1d642 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 does not already
1d643 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 6e 20 64 exist. ** in d
1d644 61 74 61 62 61 73 65 20 69 44 62 2e 20 49 66 20 atabase iDb. If
1d645 73 6f 2c 20 74 68 69 73 20 69 73 20 61 6e 20 65 so, this is an e
1d646 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 rror.. */. if(
1d647 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c sqlite3FindTabl
1d648 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 e(db, zName, zDb
1d649 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 69 6e 64 ) || sqlite3Find
1d64a 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c Index(db, zName,
1d64b 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71 6c zDb) ){. sql
1d64c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1d64d 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 74 rse, . "t
1d64e 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
1d64f 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 6f 72 another table or
1d650 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 69 73 index with this
1d651 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e 61 6d name: %s", zNam
1d652 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 e);. goto exi
1d653 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a t_rename_table;.
1d654 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
1d655 75 72 65 20 69 74 20 69 73 20 6e 6f 74 20 61 20 ure it is not a
1d656 73 79 73 74 65 6d 20 74 61 62 6c 65 20 62 65 69 system table bei
1d657 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f 72 20 61 ng altered, or a
1d658 20 72 65 73 65 72 76 65 64 20 6e 61 6d 65 0a 20 reserved name.
1d659 20 2a 2a 20 74 68 61 74 20 74 68 65 20 74 61 62 ** that the tab
1d65a 6c 65 20 69 73 20 62 65 69 6e 67 20 72 65 6e 61 le is being rena
1d65b 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20 20 69 med to.. */. i
1d65c 66 28 20 73 74 72 6c 65 6e 28 70 54 61 62 2d 3e f( strlen(pTab->
1d65d 7a 4e 61 6d 65 29 3e 36 20 26 26 20 30 3d 3d 73 zName)>6 && 0==s
1d65e 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 qlite3StrNICmp(p
1d65f 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c Tab->zName, "sql
1d660 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20 20 ite_", 7) ){.
1d661 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1d662 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 (pParse, "table
1d663 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c %s may not be al
1d664 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e tered", pTab->zN
1d665 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ame);. goto e
1d666 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
1d667 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 ;. }. if( SQLI
1d668 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 TE_OK!=sqlite3Ch
1d669 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 eckObjectName(pP
1d66a 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a arse, zName) ){.
1d66b 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 goto exit_re
1d66c 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a name_table;. }.
1d66d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1d66e 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 OMIT_VIEW. if(
1d66f 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b pTab->pSelect ){
1d670 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1d671 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 rMsg(pParse, "vi
1d672 65 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 ew %s may not be
1d673 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d altered", pTab-
1d674 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 >zName);. got
1d675 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
1d676 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ble;. }.#endif.
1d677 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1d678 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
1d679 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 ON. /* Invoke t
1d67a 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e he authorization
1d67b 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 callback. */.
1d67c 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 if( sqlite3AuthC
1d67d 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c heck(pParse, SQL
1d67e 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c ITE_ALTER_TABLE,
1d67f 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d zDb, pTab->zNam
1d680 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 e, 0) ){. got
1d681 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
1d682 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ble;. }.#endif.
1d683 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1d684 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1d685 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 E. if( sqlite3V
1d686 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 iewGetColumnName
1d687 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 s(pParse, pTab)
1d688 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 ){. goto exit
1d689 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 _rename_table;.
1d68a 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 }. if( IsVirtu
1d68b 61 6c 28 70 54 61 62 29 20 26 26 20 70 54 61 62 al(pTab) && pTab
1d68c 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d ->pMod->pModule-
1d68d 3e 78 52 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 >xRename ){.
1d68e 69 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 isVirtualRename
1d68f 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a = 1;. }.#endif.
1d690 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 . /* Begin a tr
1d691 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 63 6f ansaction and co
1d692 64 65 20 74 68 65 20 56 65 72 69 66 79 43 6f 6f de the VerifyCoo
1d693 6b 69 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 kie for database
1d694 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54 68 65 6e iDb. . ** Then
1d695 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63 68 65 modify the sche
1d696 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69 6e 63 65 ma cookie (since
1d697 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 the ALTER TABLE
1d698 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0a 20 20 modifies the.
1d699 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f 70 65 6e ** schema). Open
1d69a 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 a statement tra
1d69b 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 20 nsaction if the
1d69c 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75 table is a virtu
1d69d 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 0a 20 al. ** table..
1d69e 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */. v = sqlite
1d69f 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
1d6a0 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a ;. if( v==0 ){.
1d6a1 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 goto exit_re
1d6a2 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a name_table;. }.
1d6a3 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 sqlite3BeginWr
1d6a4 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 iteOperation(pPa
1d6a5 72 73 65 2c 20 69 73 56 69 72 74 75 61 6c 52 65 rse, isVirtualRe
1d6a6 6e 61 6d 65 2c 20 69 44 62 29 3b 0a 20 20 73 71 name, iDb);. sq
1d6a7 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 lite3ChangeCooki
1d6a8 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a e(pParse, iDb);.
1d6a9 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 . /* If this is
1d6aa 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
1d6ab 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52 65 , invoke the xRe
1d6ac 6e 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 name() function
1d6ad 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20 64 if. ** one is d
1d6ae 65 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65 6e efined. The xRen
1d6af 61 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 77 ame() callback w
1d6b0 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 6e ill modify the n
1d6b1 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e 79 ames. ** of any
1d6b2 20 72 65 73 6f 75 72 63 65 73 20 75 73 65 64 20 resources used
1d6b3 62 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20 69 by the v-table i
1d6b4 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 69 mplementation (i
1d6b5 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a 20 ncluding other.
1d6b6 20 2a 2a 20 53 51 4c 69 74 65 20 74 61 62 6c 65 ** SQLite table
1d6b7 73 29 20 74 68 61 74 20 61 72 65 20 69 64 65 6e s) that are iden
1d6b8 74 69 66 69 65 64 20 62 79 20 74 68 65 20 6e 61 tified by the na
1d6b9 6d 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 me of the virtua
1d6ba 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 l table.. */.#i
1d6bb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1d6bc 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
1d6bd 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 52 65 if( isVirtualRe
1d6be 6e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 name ){. int
1d6bf 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d i = ++pParse->nM
1d6c0 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 em;. sqlite3V
1d6c1 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1d6c2 53 74 72 69 6e 67 38 2c 20 30 2c 20 69 2c 20 30 String8, 0, i, 0
1d6c3 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 , zName, 0);.
1d6c4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1d6c5 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d 65 p4(v, OP_VRename
1d6c6 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73 74 , i, 0, 0,(const
1d6c7 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56 74 char*)pTab->pVt
1d6c8 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 ab, P4_VTAB);.
1d6c9 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 66 }.#endif.. /* f
1d6ca 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 igure out how ma
1d6cb 6e 79 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 ny UTF-8 charact
1d6cc 65 72 73 20 61 72 65 20 69 6e 20 7a 4e 61 6d 65 ers are in zName
1d6cd 20 2a 2f 0a 20 20 7a 54 61 62 4e 61 6d 65 20 3d */. zTabName =
1d6ce 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 pTab->zName;.
1d6cf 6e 54 61 62 4e 61 6d 65 20 3d 20 73 71 6c 69 74 nTabName = sqlit
1d6d0 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a 54 e3Utf8CharLen(zT
1d6d1 61 62 4e 61 6d 65 2c 20 2d 31 29 3b 0a 0a 20 20 abName, -1);..
1d6d2 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 73 71 /* Modify the sq
1d6d3 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c lite_master tabl
1d6d4 65 20 74 6f 20 75 73 65 20 74 68 65 20 6e 65 77 e to use the new
1d6d5 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a table name. */.
1d6d6 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 sqlite3NestedP
1d6d7 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 arse(pParse,.
1d6d8 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 "UPDATE %Q.%s
1d6d9 20 53 45 54 20 22 0a 23 69 66 64 65 66 20 53 51 SET ".#ifdef SQ
1d6da 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
1d6db 52 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c R. "sql
1d6dc 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 = sqlite_rename
1d6dd 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 2c _table(sql, %Q),
1d6de 20 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 ".#else.
1d6df 20 20 20 22 73 71 6c 20 3d 20 43 41 53 45 20 22 "sql = CASE "
1d6e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48 . "WH
1d6e1 45 4e 20 74 79 70 65 20 3d 20 27 74 72 69 67 67 EN type = 'trigg
1d6e2 65 72 27 20 54 48 45 4e 20 73 71 6c 69 74 65 5f er' THEN sqlite_
1d6e3 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 28 73 rename_trigger(s
1d6e4 71 6c 2c 20 25 51 29 22 0a 20 20 20 20 20 20 20 ql, %Q)".
1d6e5 20 20 20 20 20 22 45 4c 53 45 20 73 71 6c 69 74 "ELSE sqlit
1d6e6 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73 e_rename_table(s
1d6e7 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20 22 0a 23 ql, %Q) END, ".#
1d6e8 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 endif.
1d6e9 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 2c 20 "tbl_name = %Q,
1d6ea 22 0a 20 20 20 20 20 20 20 20 20 20 22 6e 61 6d ". "nam
1d6eb 65 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 e = CASE ".
1d6ec 20 20 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 "WHEN typ
1d6ed 65 3d 27 74 61 62 6c 65 27 20 54 48 45 4e 20 25 e='table' THEN %
1d6ee 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 Q ".
1d6ef 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49 4b 45 20 "WHEN name LIKE
1d6f0 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 'sqlite_autoinde
1d6f1 78 25 25 27 20 41 4e 44 20 74 79 70 65 3d 27 69 x%%' AND type='i
1d6f2 6e 64 65 78 27 20 54 48 45 4e 20 22 0a 20 20 20 ndex' THEN ".
1d6f3 20 20 20 20 20 20 20 20 20 20 22 27 73 71 6c 69 "'sqli
1d6f4 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 27 20 7c te_autoindex_' |
1d6f5 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72 28 6e | %Q || substr(n
1d6f6 61 6d 65 2c 25 64 2b 31 38 29 20 22 0a 20 20 20 ame,%d+18) ".
1d6f7 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 6e "ELSE n
1d6f8 61 6d 65 20 45 4e 44 20 22 0a 20 20 20 20 20 20 ame END ".
1d6f9 22 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d "WHERE tbl_name=
1d6fa 25 51 20 41 4e 44 20 22 0a 20 20 20 20 20 20 20 %Q AND ".
1d6fb 20 20 20 22 28 74 79 70 65 3d 27 74 61 62 6c 65 "(type='table
1d6fc 27 20 4f 52 20 74 79 70 65 3d 27 69 6e 64 65 78 ' OR type='index
1d6fd 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69 67 67 ' OR type='trigg
1d6fe 65 72 27 29 3b 22 2c 20 0a 20 20 20 20 20 20 7a er');", . z
1d6ff 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 Db, SCHEMA_TABLE
1d700 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c 20 7a 4e (iDb), zName, zN
1d701 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 0a 23 69 66 ame, zName, .#if
1d702 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d703 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 7a _TRIGGER. z
1d704 4e 61 6d 65 2c 0a 23 65 6e 64 69 66 0a 20 20 20 Name,.#endif.
1d705 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 61 62 4e 61 zName, nTabNa
1d706 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 0a 20 20 29 me, zTabName. )
1d707 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
1d708 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 E_OMIT_AUTOINCRE
1d709 4d 45 4e 54 0a 20 20 2f 2a 20 49 66 20 74 68 65 MENT. /* If the
1d70a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 sqlite_sequence
1d70b 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 69 6e table exists in
1d70c 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2c 20 this database,
1d70d 74 68 65 6e 20 75 70 64 61 74 65 20 0a 20 20 2a then update . *
1d70e 2a 20 69 74 20 77 69 74 68 20 74 68 65 20 6e 65 * it with the ne
1d70f 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 0a 20 20 w table name..
1d710 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 */. if( sqlite3
1d711 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 FindTable(db, "s
1d712 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 2c qlite_sequence",
1d713 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71 6c zDb) ){. sql
1d714 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
1d715 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 pParse,.
1d716 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 73 "UPDATE \"%w\".s
1d717 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 73 qlite_sequence s
1d718 65 74 20 6e 61 6d 65 20 3d 20 25 51 20 57 48 45 et name = %Q WHE
1d719 52 45 20 6e 61 6d 65 20 3d 20 25 51 22 2c 0a 20 RE name = %Q",.
1d71a 20 20 20 20 20 20 20 7a 44 62 2c 20 7a 4e 61 6d zDb, zNam
1d71b 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b e, pTab->zName);
1d71c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 . }.#endif..#if
1d71d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d71e 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 49 66 _TRIGGER. /* If
1d71f 20 74 68 65 72 65 20 61 72 65 20 54 45 4d 50 20 there are TEMP
1d720 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73 triggers on this
1d721 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20 74 table, modify t
1d722 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d he sqlite_temp_m
1d723 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 62 6c 65 aster. ** table
1d724 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68 69 73 20 . Don't do this
1d725 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 if the table bei
1d726 6e 67 20 41 4c 54 45 52 65 64 20 69 73 20 69 74 ng ALTERed is it
1d727 73 65 6c 66 20 6c 6f 63 61 74 65 64 20 69 6e 0a self located in.
1d728 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 20 64 61 ** the temp da
1d729 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 tabase.. */. i
1d72a 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72 65 f( (zWhere=where
1d72b 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50 61 TempTriggers(pPa
1d72c 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20 29 rse, pTab))!=0 )
1d72d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 {. sqlite3Nes
1d72e 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
1d72f 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54 . "UPDAT
1d730 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 E sqlite_temp_ma
1d731 73 74 65 72 20 53 45 54 20 22 0a 20 20 20 20 20 ster SET ".
1d732 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 71 "sql = sq
1d733 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 lite_rename_trig
1d734 67 65 72 28 73 71 6c 2c 20 25 51 29 2c 20 22 0a ger(sql, %Q), ".
1d735 20 20 20 20 20 20 20 20 20 20 20 20 22 74 62 6c "tbl
1d736 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a 20 20 20 _name = %Q ".
1d737 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20 "WHERE
1d738 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 %s;", zName, zNa
1d739 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 20 me, zWhere);.
1d73a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1d73b 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 7d 0a b, zWhere);. }.
1d73c 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f #endif.. /* Dro
1d73d 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 p and reload the
1d73e 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 internal table
1d73f 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65 6c schema. */. rel
1d740 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 oadTableSchema(p
1d741 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 4e 61 Parse, pTab, zNa
1d742 6d 65 29 3b 0a 0a 65 78 69 74 5f 72 65 6e 61 6d me);..exit_renam
1d743 65 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74 e_table:. sqlit
1d744 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 e3SrcListDelete(
1d745 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 73 71 6c db, pSrc);. sql
1d746 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1d747 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a Name);.}.../*.**
1d748 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1d749 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 s called after a
1d74a 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e n "ALTER TABLE .
1d74b 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e .. ADD" statemen
1d74c 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 70 61 t.** has been pa
1d74d 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 70 rsed. Argument p
1d74e 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73 20 ColDef contains
1d74f 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 the text of the
1d750 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65 new.** column de
1d751 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 finition..**.**
1d752 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 The Table struct
1d753 75 72 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 ure pParse->pNew
1d754 54 61 62 6c 65 20 77 61 73 20 65 78 74 65 6e 64 Table was extend
1d755 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a 2a ed to include.**
1d756 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 the new column
1d757 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 2e 0a during parsing..
1d758 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1d759 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c E void sqlite3Al
1d75a 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 terFinishAddColu
1d75b 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 mn(Parse *pParse
1d75c 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65 66 , Token *pColDef
1d75d 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 ){. Table *pNew
1d75e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1d75f 2a 20 43 6f 70 79 20 6f 66 20 70 50 61 72 73 65 * Copy of pParse
1d760 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20 ->pNewTable */.
1d761 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 Table *pTab;
1d762 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 /* Ta
1d763 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 ble being altere
1d764 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 d */. int iDb;
1d765 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d766 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d /* Database num
1d767 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ber */. const c
1d768 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 har *zDb;
1d769 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e /* Database n
1d76a 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ame */. const c
1d76b 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 har *zTab;
1d76c 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 /* Table name
1d76d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c */. char *zCol
1d76e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1d76f 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 /* Null-terminat
1d770 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 ed column defini
1d771 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e tion */. Column
1d772 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 *pCol;
1d773 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 63 /* The new c
1d774 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20 olumn */. Expr
1d775 2a 70 44 66 6c 74 3b 20 20 20 20 20 20 20 20 20 *pDflt;
1d776 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 /* Default
1d777 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 value for the ne
1d778 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73 71 w column */. sq
1d779 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
1d77a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
1d77b 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1d77c 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 on; */.. db = p
1d77d 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 Parse->db;. if(
1d77e 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c pParse->nErr ||
1d77f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1d780 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4e d ) return;. pN
1d781 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 ew = pParse->pNe
1d782 77 54 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 wTable;. assert
1d783 28 20 70 4e 65 77 20 29 3b 0a 0a 20 20 61 73 73 ( pNew );.. ass
1d784 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1d785 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 eHoldsAllMutexes
1d786 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 (db) );. iDb =
1d787 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
1d788 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e 70 ndex(db, pNew->p
1d789 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d Schema);. zDb =
1d78a 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e db->aDb[iDb].zN
1d78b 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 70 4e ame;. zTab = pN
1d78c 65 77 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 70 43 6f ew->zName;. pCo
1d78d 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b l = &pNew->aCol[
1d78e 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 pNew->nCol-1];.
1d78f 20 70 44 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 pDflt = pCol->p
1d790 44 66 6c 74 3b 0a 20 20 70 54 61 62 20 3d 20 73 Dflt;. pTab = s
1d791 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
1d792 64 62 2c 20 7a 54 61 62 2c 20 7a 44 62 29 3b 0a db, zTab, zDb);.
1d793 20 20 61 73 73 65 72 74 28 20 70 54 61 62 20 29 assert( pTab )
1d794 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
1d795 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 E_OMIT_AUTHORIZA
1d796 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 TION. /* Invoke
1d797 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 the authorizati
1d798 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a on callback. */.
1d799 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 if( sqlite3Aut
1d79a 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 hCheck(pParse, S
1d79b 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c QLITE_ALTER_TABL
1d79c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e E, zDb, pTab->zN
1d79d 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 ame, 0) ){. r
1d79e 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 eturn;. }.#endi
1d79f 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 f.. /* If the d
1d7a0 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 efault value for
1d7a1 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 the new column
1d7a2 77 61 73 20 73 70 65 63 69 66 69 65 64 20 77 69 was specified wi
1d7a3 74 68 20 61 20 0a 20 20 2a 2a 20 6c 69 74 65 72 th a . ** liter
1d7a4 61 6c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 al NULL, then se
1d7a5 74 20 70 44 66 6c 74 20 74 6f 20 30 2e 20 54 68 t pDflt to 0. Th
1d7a6 69 73 20 73 69 6d 70 6c 69 66 69 65 73 20 63 68 is simplifies ch
1d7a7 65 63 6b 69 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 ecking. ** for
1d7a8 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 64 65 66 61 an SQL NULL defa
1d7a9 75 6c 74 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a ult below.. */.
1d7aa 20 20 69 66 28 20 70 44 66 6c 74 20 26 26 20 70 if( pDflt && p
1d7ab 44 66 6c 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c Dflt->op==TK_NUL
1d7ac 4c 20 29 7b 0a 20 20 20 20 70 44 66 6c 74 20 3d L ){. pDflt =
1d7ad 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 0;. }.. /* Ch
1d7ae 65 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65 77 eck that the new
1d7af 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 column is not s
1d7b0 70 65 63 69 66 69 65 64 20 61 73 20 50 52 49 4d pecified as PRIM
1d7b1 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 ARY KEY or UNIQU
1d7b2 45 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65 E.. ** If there
1d7b3 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 is a NOT NULL c
1d7b4 6f 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e 20 onstraint, then
1d7b5 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 the default valu
1d7b6 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 63 e for the. ** c
1d7b7 6f 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20 62 olumn must not b
1d7b8 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 e NULL.. */. i
1d7b9 66 28 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b f( pCol->isPrimK
1d7ba 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ey ){. sqlite
1d7bb 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1d7bc 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 , "Cannot add a
1d7bd 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 PRIMARY KEY colu
1d7be 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e mn");. return
1d7bf 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 77 ;. }. if( pNew
1d7c0 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 ->pIndex ){.
1d7c1 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1d7c2 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 pParse, "Cannot
1d7c3 61 64 64 20 61 20 55 4e 49 51 55 45 20 63 6f 6c add a UNIQUE col
1d7c4 75 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 umn");. retur
1d7c5 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f n;. }. if( pCo
1d7c6 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 l->notNull && !p
1d7c7 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 Dflt ){. sqli
1d7c8 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1d7c9 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61 se, . "Ca
1d7ca 6e 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e nnot add a NOT N
1d7cb 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 ULL column with
1d7cc 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55 default value NU
1d7cd 4c 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e LL");. return
1d7ce 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 ;. }.. /* Ensu
1d7cf 72 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65 re the default e
1d7d0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d xpression is som
1d7d1 65 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69 ething that sqli
1d7d2 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 te3ValueFromExpr
1d7d3 28 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64 (). ** can hand
1d7d4 6c 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52 le (i.e. not CUR
1d7d5 52 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a RENT_TIME etc.).
1d7d6 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 */. if( pDflt
1d7d7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1d7d8 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20 value *pVal;.
1d7d9 20 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75 if( sqlite3Valu
1d7da 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 eFromExpr(db, pD
1d7db 66 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 flt, SQLITE_UTF8
1d7dc 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e , SQLITE_AFF_NON
1d7dd 45 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 E, &pVal) ){.
1d7de 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
1d7df 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 led = 1;. r
1d7e0 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 eturn;. }.
1d7e1 20 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20 if( !pVal ){.
1d7e2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1d7e3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e Msg(pParse, "Can
1d7e4 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e not add a column
1d7e5 20 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 with non-consta
1d7e6 6e 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20 nt default");.
1d7e7 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
1d7e8 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c }. sqlite3Val
1d7e9 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 ueFree(pVal);.
1d7ea 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 }.. /* Modify t
1d7eb 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 he CREATE TABLE
1d7ec 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 statement. */.
1d7ed 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 zCol = sqlite3Db
1d7ee 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 StrNDup(db, (cha
1d7ef 72 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 r*)pColDef->z, p
1d7f0 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 ColDef->n);. if
1d7f1 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68 ( zCol ){. ch
1d7f2 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c ar *zEnd = &zCol
1d7f3 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a [pColDef->n-1];.
1d7f4 20 20 20 20 77 68 69 6c 65 28 20 28 7a 45 6e 64 while( (zEnd
1d7f5 3e 7a 43 6f 6c 20 26 26 20 2a 7a 45 6e 64 3d 3d >zCol && *zEnd==
1d7f6 27 3b 27 29 20 7c 7c 20 69 73 73 70 61 63 65 28 ';') || isspace(
1d7f7 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 *(unsigned char
1d7f8 2a 29 7a 45 6e 64 29 20 29 7b 0a 20 20 20 20 20 *)zEnd) ){.
1d7f9 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30 27 3b *zEnd-- = '\0';
1d7fa 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1d7fb 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
1d7fc 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 arse, . "
1d7fd 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 25 73 UPDATE \"%w\".%s
1d7fe 20 53 45 54 20 22 0a 20 20 20 20 20 20 20 20 20 SET ".
1d7ff 20 22 73 71 6c 20 3d 20 73 75 62 73 74 72 28 73 "sql = substr(s
1d800 71 6c 2c 31 2c 25 64 29 20 7c 7c 20 27 2c 20 27 ql,1,%d) || ', '
1d801 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72 || %Q || substr
1d802 28 73 71 6c 2c 25 64 29 20 22 0a 20 20 20 20 20 (sql,%d) ".
1d803 20 20 20 22 57 48 45 52 45 20 74 79 70 65 20 3d "WHERE type =
1d804 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 'table' AND nam
1d805 65 20 3d 20 25 51 22 2c 20 0a 20 20 20 20 20 20 e = %Q", .
1d806 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c zDb, SCHEMA_TABL
1d807 45 28 69 44 62 29 2c 20 70 4e 65 77 2d 3e 61 64 E(iDb), pNew->ad
1d808 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43 6f 6c dColOffset, zCol
1d809 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 , pNew->addColOf
1d80a 66 73 65 74 2b 31 2c 0a 20 20 20 20 20 20 7a 54 fset+1,. zT
1d80b 61 62 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 ab. );. sq
1d80c 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1d80d 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a zCol);. }.. /*
1d80e 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 If the default
1d80f 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 77 value of the new
1d810 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 2c column is NULL,
1d811 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 66 69 then set the fi
1d812 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61 74 20 74 le. ** format t
1d813 6f 20 32 2e 20 49 66 20 74 68 65 20 64 65 66 61 o 2. If the defa
1d814 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 ult value of the
1d815 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 6e new column is n
1d816 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a 20 74 68 ot NULL,. ** th
1d817 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 62 65 e file format be
1d818 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f 0a 20 20 comes 3.. */.
1d819 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 sqlite3MinimumFi
1d81a 6c 65 46 6f 72 6d 61 74 28 70 50 61 72 73 65 2c leFormat(pParse,
1d81b 20 69 44 62 2c 20 70 44 66 6c 74 20 3f 20 33 20 iDb, pDflt ? 3
1d81c 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f : 2);.. /* Relo
1d81d 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 ad the schema of
1d81e 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20 74 61 the modified ta
1d81f 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c 6f 61 64 ble. */. reload
1d820 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50 61 72 TableSchema(pPar
1d821 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62 2d 3e se, pTab, pTab->
1d822 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a zName);.}../*.**
1d823 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1d824 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
1d825 70 61 72 73 65 72 20 61 66 74 65 72 20 74 68 65 parser after the
1d826 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 6e 0a 2a table-name in.*
1d827 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42 4c * an "ALTER TABL
1d828 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 3e 20 41 E <table-name> A
1d829 44 44 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 DD" statement is
1d82a 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65 6e parsed. Argumen
1d82b 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 20 74 68 t .** pSrc is th
1d82c 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f 66 20 74 e full-name of t
1d82d 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 he table being a
1d82e 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 ltered..**.** Th
1d82f 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73 is routine makes
1d830 20 61 20 28 70 61 72 74 69 61 6c 29 20 63 6f 70 a (partial) cop
1d831 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 y of the Table s
1d832 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 6f 72 20 tructure.** for
1d833 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
1d834 61 6c 74 65 72 65 64 20 61 6e 64 20 73 65 74 73 altered and sets
1d835 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 Parse.pNewTable
1d836 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 to point.** to
1d837 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 63 61 6c it. Routines cal
1d838 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 led by the parse
1d839 72 20 61 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 r as the column
1d83a 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20 69 73 definition.** is
1d83b 20 70 61 72 73 65 64 20 28 69 2e 65 2e 20 73 71 parsed (i.e. sq
1d83c 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29 lite3AddColumn()
1d83d 29 20 61 64 64 20 74 68 65 20 6e 65 77 20 43 6f ) add the new Co
1d83e 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 0a 2a 2a lumn data to .**
1d83f 20 74 68 65 20 63 6f 70 79 2e 20 54 68 65 20 63 the copy. The c
1d840 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 opy of the Table
1d841 20 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 structure is de
1d842 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 6e 69 7a leted by tokeniz
1d843 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 20 70 61 e.c .** after pa
1d844 72 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 65 rsing is finishe
1d845 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 d..**.** Routine
1d846 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e sqlite3AlterFin
1d847 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29 20 77 ishAddColumn() w
1d848 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f ill be called to
1d849 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 64 complete.** cod
1d84a 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52 20 54 ing the "ALTER T
1d84b 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 ABLE ... ADD" st
1d84c 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 atement..*/.SQLI
1d84d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1d84e 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 sqlite3AlterBegi
1d84f 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 nAddColumn(Parse
1d850 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 *pParse, SrcLis
1d851 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61 62 6c t *pSrc){. Tabl
1d852 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61 62 6c 65 e *pNew;. Table
1d853 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 20 2a *pTab;. Vdbe *
1d854 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 v;. int iDb;.
1d855 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 41 6c int i;. int nAl
1d856 6c 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a loc;. sqlite3 *
1d857 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1d858 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 74 .. /* Look up t
1d859 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 he table being a
1d85a 6c 74 65 72 65 64 2e 20 2a 2f 0a 20 20 61 73 73 ltered. */. ass
1d85b 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 ert( pParse->pNe
1d85c 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 wTable==0 );. a
1d85d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1d85e 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 reeHoldsAllMutex
1d85f 65 73 28 64 62 29 20 29 3b 0a 20 20 69 66 28 20 es(db) );. if(
1d860 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1d861 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 ) goto exit_beg
1d862 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 in_add_column;.
1d863 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c pTab = sqlite3L
1d864 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 ocateTable(pPars
1d865 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d e, 0, pSrc->a[0]
1d866 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b .zName, pSrc->a[
1d867 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 0].zDatabase);.
1d868 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 if( !pTab ) got
1d869 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 o exit_begin_add
1d86a 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66 6e 64 65 _column;..#ifnde
1d86b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1d86c 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 RTUALTABLE. if(
1d86d 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
1d86e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1d86f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1d870 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 "virtual tables
1d871 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 may not be alter
1d872 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ed");. goto e
1d873 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f xit_begin_add_co
1d874 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 lumn;. }.#endif
1d875 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
1d876 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 this is not an
1d877 61 74 74 65 6d 70 74 20 74 6f 20 41 4c 54 45 52 attempt to ALTER
1d878 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 69 66 a view. */. if
1d879 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 ( pTab->pSelect
1d87a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1d87b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1d87c 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c Cannot add a col
1d87d 75 6d 6e 20 74 6f 20 61 20 76 69 65 77 22 29 3b umn to a view");
1d87e 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 . goto exit_b
1d87f 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b egin_add_column;
1d880 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
1d881 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 pTab->addColOffs
1d882 65 74 3e 30 20 29 3b 0a 20 20 69 44 62 20 3d 20 et>0 );. iDb =
1d883 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
1d884 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 ndex(db, pTab->p
1d885 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 50 Schema);.. /* P
1d886 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ut a copy of the
1d887 20 54 61 62 6c 65 20 73 74 72 75 63 74 20 69 6e Table struct in
1d888 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 Parse.pNewTable
1d889 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73 71 for the. ** sq
1d88a 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29 lite3AddColumn()
1d88b 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 66 72 function and fr
1d88c 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66 79 2e iends to modify.
1d88d 0a 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 . */. pNew = (
1d88e 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 Table*)sqlite3Db
1d88f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
1d890 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 izeof(Table));.
1d891 20 69 66 28 20 21 70 4e 65 77 20 29 20 67 6f 74 if( !pNew ) got
1d892 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 o exit_begin_add
1d893 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 _column;. pPars
1d894 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 e->pNewTable = p
1d895 4e 65 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 New;. pNew->nRe
1d896 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 64 f = 1;. pNew->d
1d897 62 20 3d 20 64 62 3b 0a 20 20 70 4e 65 77 2d 3e b = db;. pNew->
1d898 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f nCol = pTab->nCo
1d899 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 l;. assert( pNe
1d89a 77 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 6e w->nCol>0 );. n
1d89b 41 6c 6c 6f 63 20 3d 20 28 28 28 70 4e 65 77 2d Alloc = (((pNew-
1d89c 3e 6e 43 6f 6c 2d 31 29 2f 38 29 2a 38 29 2b 38 >nCol-1)/8)*8)+8
1d89d 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 41 6c 6c ;. assert( nAll
1d89e 6f 63 3e 3d 70 4e 65 77 2d 3e 6e 43 6f 6c 20 26 oc>=pNew->nCol &
1d89f 26 20 6e 41 6c 6c 6f 63 25 38 3d 3d 30 20 26 26 & nAlloc%8==0 &&
1d8a0 20 6e 41 6c 6c 6f 63 2d 70 4e 65 77 2d 3e 6e 43 nAlloc-pNew->nC
1d8a1 6f 6c 3c 38 20 29 3b 0a 20 20 70 4e 65 77 2d 3e ol<8 );. pNew->
1d8a2 61 43 6f 6c 20 3d 20 28 43 6f 6c 75 6d 6e 2a 29 aCol = (Column*)
1d8a3 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
1d8a4 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 43 ero(db, sizeof(C
1d8a5 6f 6c 75 6d 6e 29 2a 6e 41 6c 6c 6f 63 29 3b 0a olumn)*nAlloc);.
1d8a6 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 pNew->zName =
1d8a7 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1d8a8 64 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 db, pTab->zName)
1d8a9 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 ;. if( !pNew->a
1d8aa 43 6f 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e Col || !pNew->zN
1d8ab 61 6d 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d ame ){. db->m
1d8ac 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1d8ad 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 . goto exit_b
1d8ae 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b egin_add_column;
1d8af 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e . }. memcpy(pN
1d8b0 65 77 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e ew->aCol, pTab->
1d8b1 61 43 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c aCol, sizeof(Col
1d8b2 75 6d 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 umn)*pNew->nCol)
1d8b3 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
1d8b4 4e 65 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b New->nCol; i++){
1d8b5 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f . Column *pCo
1d8b6 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b l = &pNew->aCol[
1d8b7 69 5d 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e i];. pCol->zN
1d8b8 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ame = sqlite3DbS
1d8b9 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e trDup(db, pCol->
1d8ba 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c zName);. pCol
1d8bb 2d 3e 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 ->zColl = 0;.
1d8bc 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 pCol->zType = 0
1d8bd 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c ;. pCol->pDfl
1d8be 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 t = 0;. }. pNe
1d8bf 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d w->pSchema = db-
1d8c0 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d >aDb[iDb].pSchem
1d8c1 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 43 6f a;. pNew->addCo
1d8c2 6c 4f 66 66 73 65 74 20 3d 20 70 54 61 62 2d 3e lOffset = pTab->
1d8c3 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b 0a 20 20 addColOffset;.
1d8c4 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a pNew->nRef = 1;.
1d8c5 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 . /* Begin a tr
1d8c6 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 69 6e ansaction and in
1d8c7 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 crement the sche
1d8c8 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a 20 ma cookie. */.
1d8c9 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 sqlite3BeginWri
1d8ca 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 teOperation(pPar
1d8cb 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 76 se, 0, iDb);. v
1d8cc 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
1d8cd 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 e(pParse);. if(
1d8ce 20 21 76 20 29 20 67 6f 74 6f 20 65 78 69 74 5f !v ) goto exit_
1d8cf 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e begin_add_column
1d8d0 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 ;. sqlite3Chang
1d8d1 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 eCookie(pParse,
1d8d2 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62 65 67 69 iDb);..exit_begi
1d8d3 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a 0a 20 20 n_add_column:.
1d8d4 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 sqlite3SrcListDe
1d8d5 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a lete(db, pSrc);.
1d8d6 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 return;.}.#end
1d8d7 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c if /* SQLITE_AL
1d8d8 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a TER_TABLE */../*
1d8d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1d8da 64 20 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a 2a d of alter.c ***
1d8db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1d8de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1d8df 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c 79 7a 65 gin file analyze
1d8e0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1d8e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1d8e3 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79 20 38 0a .** 2005 July 8.
1d8e4 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
1d8e5 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
1d8e6 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
1d8e7 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
1d8e8 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
1d8e9 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
1d8ea 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
1d8eb 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
1d8ec 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
1d8ed 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
1d8ee 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
1d8ef 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
1d8f0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
1d8f1 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
1d8f2 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
1d8f3 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
1d8f4 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
1d8f5 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1d8f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d8f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1d8fa 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
1d8fb 61 69 6e 73 20 63 6f 64 65 20 61 73 73 6f 63 69 ains code associ
1d8fc 61 74 65 64 20 77 69 74 68 20 74 68 65 20 41 4e ated with the AN
1d8fd 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a ALYZE command..*
1d8fe 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 61 *.** @(#) $Id: a
1d8ff 6e 61 6c 79 7a 65 2e 63 2c 76 20 31 2e 34 34 20 nalyze.c,v 1.44
1d900 32 30 30 38 2f 31 31 2f 30 33 20 32 30 3a 35 35 2008/11/03 20:55
1d901 3a 30 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :07 drh Exp $.*/
1d902 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1d903 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a OMIT_ANALYZE../*
1d904 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1d905 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 generates code
1d906 74 68 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73 that opens the s
1d907 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c qlite_stat1 tabl
1d908 65 20 6f 6e 20 63 75 72 73 6f 72 0a 2a 2a 20 69 e on cursor.** i
1d909 53 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 49 StatCur..**.** I
1d90a 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 f the sqlite_sta
1d90b 74 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e t1 tables does n
1d90c 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 ot previously ex
1d90d 69 73 74 2c 20 69 74 20 69 73 20 63 72 65 61 74 ist, it is creat
1d90e 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 ed..** If it doe
1d90f 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 s previously exi
1d910 73 74 2c 20 61 6c 6c 20 65 6e 74 69 72 65 73 20 st, all entires
1d911 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1d912 74 61 62 6c 65 20 7a 57 68 65 72 65 0a 2a 2a 20 table zWhere.**
1d913 61 72 65 20 72 65 6d 6f 76 65 64 2e 20 20 49 66 are removed. If
1d914 20 7a 57 68 65 72 65 3d 3d 30 20 74 68 65 6e 20 zWhere==0 then
1d915 61 6c 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20 all entries are
1d916 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 removed..*/.stat
1d917 69 63 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74 ic void openStat
1d918 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a Table(. Parse *
1d919 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
1d91a 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
1d91b 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 ext */. int iDb
1d91c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1d91d 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1d91e 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 we are looking
1d91f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 in */. int iSta
1d920 74 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 tCur,
1d921 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 /* Open the sqli
1d922 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f te_stat1 table o
1d923 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f n this cursor */
1d924 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1d925 57 68 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65 Where /* De
1d926 6c 65 74 65 20 65 6e 74 72 69 65 73 20 61 73 73 lete entries ass
1d927 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 ociated with thi
1d928 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 s table */.){.
1d929 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1d92a 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a arse->db;. Db *
1d92b 70 44 62 3b 0a 20 20 69 6e 74 20 69 52 6f 6f 74 pDb;. int iRoot
1d92c 50 61 67 65 3b 0a 20 20 69 6e 74 20 63 72 65 61 Page;. int crea
1d92d 74 65 53 74 61 74 31 20 3d 20 30 3b 0a 20 20 54 teStat1 = 0;. T
1d92e 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20 56 able *pStat;. V
1d92f 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 dbe *v = sqlite3
1d930 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
1d931 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 .. if( v==0 ) r
1d932 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 eturn;. assert(
1d933 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
1d934 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 dsAllMutexes(db)
1d935 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
1d936 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3d 3d lite3VdbeDb(v)==
1d937 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 db );. pDb = &d
1d938 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 b->aDb[iDb];. i
1d939 66 28 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69 f( (pStat = sqli
1d93a 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c te3FindTable(db,
1d93b 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c "sqlite_stat1",
1d93c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30 pDb->zName))==0
1d93d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 ){. /* The s
1d93e 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c qlite_stat1 tabl
1d93f 65 73 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 es does not exis
1d940 74 2e 20 20 43 72 65 61 74 65 20 69 74 2e 20 20 t. Create it.
1d941 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 . ** Note tha
1d942 74 20 61 20 73 69 64 65 2d 65 66 66 65 63 74 20 t a side-effect
1d943 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 of the CREATE TA
1d944 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 BLE statement is
1d945 20 74 6f 20 6c 65 61 76 65 0a 20 20 20 20 2a 2a to leave. **
1d946 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 6f 66 the rootpage of
1d947 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 the new table i
1d948 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73 n register pPars
1d949 65 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54 68 69 e->regRoot. Thi
1d94a 73 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d 70 6f s is. ** impo
1d94b 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 74 68 rtant because th
1d94c 65 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f e OpenWrite opco
1d94d 64 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 de below will be
1d94e 20 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a needing it. */.
1d94f 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 sqlite3Neste
1d950 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 dParse(pParse,.
1d951 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42 "CREATE TAB
1d952 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 LE %Q.sqlite_sta
1d953 74 31 28 74 62 6c 2c 69 64 78 2c 73 74 61 74 29 t1(tbl,idx,stat)
1d954 22 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e ",. pDb->zN
1d955 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 ame. );. i
1d956 52 6f 6f 74 50 61 67 65 20 3d 20 70 50 61 72 73 RootPage = pPars
1d957 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20 20 20 e->regRoot;.
1d958 63 72 65 61 74 65 53 74 61 74 31 20 3d 20 31 3b createStat1 = 1;
1d959 20 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f 74 70 /* Cause rootp
1d95a 61 67 65 20 74 6f 20 62 65 20 74 61 6b 65 6e 20 age to be taken
1d95b 66 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74 61 63 from top of stac
1d95c 6b 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 k */. }else if(
1d95d 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20 2f zWhere ){. /
1d95e 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 * The sqlite_sta
1d95f 74 31 20 74 61 62 6c 65 20 65 78 69 73 74 73 2e t1 table exists.
1d960 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 Delete all ent
1d961 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 ries associated
1d962 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 with. ** the
1d963 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 2a 2f table zWhere. */
1d964 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 . sqlite3Nest
1d965 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a edParse(pParse,.
1d966 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 "DELETE F
1d967 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 ROM %Q.sqlite_st
1d968 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25 51 at1 WHERE tbl=%Q
1d969 22 2c 0a 20 20 20 20 20 20 20 70 44 62 2d 3e 7a ",. pDb->z
1d96a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20 20 20 Name, zWhere.
1d96b 20 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 );. iRootPag
1d96c 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b e = pStat->tnum;
1d96d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1d96e 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 The sqlite_stat
1d96f 31 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 1 table already
1d970 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20 exists. Delete
1d971 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 all rows. */.
1d972 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 53 74 iRootPage = pSt
1d973 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 at->tnum;. sq
1d974 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1d975 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 53 74 v, OP_Clear, pSt
1d976 61 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a at->tnum, iDb);.
1d977 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 }.. /* Open t
1d978 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 he sqlite_stat1
1d979 74 61 62 6c 65 20 66 6f 72 20 77 72 69 74 69 6e table for writin
1d97a 67 2e 20 55 6e 6c 65 73 73 20 69 74 20 77 61 73 g. Unless it was
1d97b 20 63 72 65 61 74 65 64 0a 20 20 2a 2a 20 62 79 created. ** by
1d97c 20 74 68 69 73 20 76 64 62 65 20 70 72 6f 67 72 this vdbe progr
1d97d 61 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f 72 20 am, lock it for
1d97e 77 72 69 74 69 6e 67 20 61 74 20 74 68 65 20 73 writing at the s
1d97f 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65 hared-cache leve
1d980 6c 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 l. . ** If this
1d981 20 76 64 62 65 20 64 69 64 20 63 72 65 61 74 65 vdbe did create
1d982 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
1d983 31 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 69 74 1 table, then it
1d984 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20 2a 2a must have . **
1d985 20 61 6c 72 65 61 64 79 20 6f 62 74 61 69 6e 65 already obtaine
1d986 64 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 2c d a schema-lock,
1d987 20 6d 61 6b 69 6e 67 20 74 68 65 20 77 72 69 74 making the writ
1d988 65 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61 6e 74 e-lock redundant
1d989 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 72 .. */. if( !cr
1d98a 65 61 74 65 53 74 61 74 31 20 29 7b 0a 20 20 20 eateStat1 ){.
1d98b 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 sqlite3TableLoc
1d98c 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 69 k(pParse, iDb, i
1d98d 52 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22 73 71 RootPage, 1, "sq
1d98e 6c 69 74 65 5f 73 74 61 74 31 22 29 3b 0a 20 20 lite_stat1");.
1d98f 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 }. sqlite3VdbeA
1d990 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e ddOp2(v, OP_SetN
1d991 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 33 29 umColumns, 0, 3)
1d992 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
1d993 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp3(v, OP_Open
1d994 57 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2c Write, iStatCur,
1d995 20 69 52 6f 6f 74 50 61 67 65 2c 20 69 44 62 29 iRootPage, iDb)
1d996 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 ;. sqlite3VdbeC
1d997 68 61 6e 67 65 50 35 28 76 2c 20 63 72 65 61 74 hangeP5(v, creat
1d998 65 53 74 61 74 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a eStat1);.}../*.*
1d999 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
1d99a 74 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 to do an analysi
1d99b 73 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 s of all indices
1d99c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1d99d 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 .** a single tab
1d99e 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
1d99f 69 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 id analyzeOneTab
1d9a0 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 le(. Parse *pPa
1d9a1 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 rse, /* Parser
1d9a2 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 context */. Ta
1d9a3 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f ble *pTab, /
1d9a4 2a 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e * Table whose in
1d9a5 64 69 63 65 73 20 61 72 65 20 74 6f 20 62 65 20 dices are to be
1d9a6 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e analyzed */. in
1d9a7 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f t iStatCur, /
1d9a8 2a 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43 * Index of VdbeC
1d9a9 75 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65 ursor that write
1d9aa 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 s the sqlite_sta
1d9ab 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e t1 table */. in
1d9ac 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f t iMem /
1d9ad 2a 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f * Available memo
1d9ae 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67 ry locations beg
1d9af 69 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 in here */.){.
1d9b0 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 Index *pIdx;
1d9b1 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20 /* An index to
1d9b2 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a being analyzed *
1d9b3 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b /. int iIdxCur;
1d9b4 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1d9b5 20 56 64 62 65 43 75 72 73 6f 72 20 66 6f 72 20 VdbeCursor for
1d9b6 69 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61 6c index being anal
1d9b7 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 yzed */. int nC
1d9b8 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ol; /* Nu
1d9b9 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1d9ba 69 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a in the index */.
1d9bb 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 Vdbe *v;
1d9bc 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 /* The virtua
1d9bd 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20 l machine being
1d9be 62 75 69 6c 74 20 75 70 20 2a 2f 0a 20 20 69 6e built up */. in
1d9bf 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f t i; /
1d9c0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
1d9c1 2f 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f /. int topOfLoo
1d9c2 70 3b 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 p; /* The top
1d9c3 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 of the loop */.
1d9c4 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 int endOfLoop;
1d9c5 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 /* The end of
1d9c6 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e the loop */. in
1d9c7 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f t addr; /
1d9c8 2a 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66 * The address of
1d9c9 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 an instruction
1d9ca 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 */. int iDb;
1d9cb 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1d9cc 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 f database conta
1d9cd 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 0a 20 ining pTab */..
1d9ce 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 v = sqlite3GetV
1d9cf 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 dbe(pParse);. i
1d9d0 66 28 20 76 3d 3d 30 20 7c 7c 20 70 54 61 62 3d f( v==0 || pTab=
1d9d1 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 =0 || pTab->pInd
1d9d2 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 ex==0 ){. /*
1d9d3 44 6f 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20 66 Do no analysis f
1d9d4 6f 72 20 74 61 62 6c 65 73 20 74 68 61 74 20 68 or tables that h
1d9d5 61 76 65 20 6e 6f 20 69 6e 64 69 63 65 73 20 2a ave no indices *
1d9d6 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 /. return;.
1d9d7 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 }. assert( sqli
1d9d8 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c te3BtreeHoldsAll
1d9d9 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e Mutexes(pParse->
1d9da 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 db) );. iDb = s
1d9db 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
1d9dc 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 dex(pParse->db,
1d9dd 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
1d9de 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 assert( iDb>=0
1d9df 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );.#ifndef SQLI
1d9e0 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
1d9e1 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 ATION. if( sqli
1d9e2 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
1d9e3 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41 4c rse, SQLITE_ANAL
1d9e4 59 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 YZE, pTab->zName
1d9e5 2c 20 30 2c 0a 20 20 20 20 20 20 70 50 61 72 73 , 0,. pPars
1d9e6 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e e->db->aDb[iDb].
1d9e7 7a 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 zName ) ){. r
1d9e8 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 eturn;. }.#endi
1d9e9 66 0a 0a 20 20 2f 2a 20 45 73 74 61 62 6c 69 73 f.. /* Establis
1d9ea 68 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e h a read-lock on
1d9eb 20 74 68 65 20 74 61 62 6c 65 20 61 74 20 74 68 the table at th
1d9ec 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c e shared-cache l
1d9ed 65 76 65 6c 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 evel. */. sqlit
1d9ee 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 e3TableLock(pPar
1d9ef 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 se, iDb, pTab->t
1d9f0 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e num, 0, pTab->zN
1d9f1 61 6d 65 29 3b 0a 0a 20 20 69 49 64 78 43 75 72 ame);.. iIdxCur
1d9f2 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b = pParse->nTab;
1d9f3 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 . for(pIdx=pTab
1d9f4 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 ->pIndex; pIdx;
1d9f5 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 pIdx=pIdx->pNext
1d9f6 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a ){. KeyInfo *
1d9f7 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e pKey = sqlite3In
1d9f8 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 dexKeyinfo(pPars
1d9f9 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 69 6e e, pIdx);. in
1d9fa 74 20 72 65 67 46 69 65 6c 64 73 3b 20 20 20 20 t regFields;
1d9fb 2f 2a 20 52 65 67 69 73 74 65 72 20 62 6c 6f 63 /* Register bloc
1d9fc 6b 20 66 6f 72 20 62 75 69 6c 64 69 6e 67 20 72 k for building r
1d9fd 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 69 6e ecords */. in
1d9fe 74 20 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 t regRec;
1d9ff 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 /* Register hold
1da00 69 6e 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 ing completed re
1da01 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 cord */. int
1da02 72 65 67 54 65 6d 70 3b 20 20 20 20 20 20 2f 2a regTemp; /*
1da03 20 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 Temporary use r
1da04 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 egister */. i
1da05 6e 74 20 72 65 67 43 6f 6c 3b 20 20 20 20 20 20 nt regCol;
1da06 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61 /* Content of a
1da07 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 column from the
1da08 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6e 61 table being ana
1da09 6c 79 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 lyzed */. int
1da0a 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 2f regRowid; /
1da0b 2a 20 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 * Rowid for the
1da0c 69 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 inserted record
1da0d 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 46 32 */. int regF2
1da0e 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 ;.. /* Open a
1da0f 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 cursor to the i
1da10 6e 64 65 78 20 74 6f 20 62 65 20 61 6e 61 6c 79 ndex to be analy
1da11 7a 65 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 zed. */. a
1da12 73 73 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69 ssert( iDb==sqli
1da13 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
1da14 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 64 (pParse->db, pId
1da15 78 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 x->pSchema) );.
1da16 20 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e nCol = pIdx->
1da17 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c nColumn;. sql
1da18 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1da19 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d , OP_SetNumColum
1da1a 6e 73 2c 20 30 2c 20 6e 43 6f 6c 2b 31 29 3b 0a ns, 0, nCol+1);.
1da1b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1da1c 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp4(v, OP_Open
1da1d 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70 Read, iIdxCur, p
1da1e 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a Idx->tnum, iDb,.
1da1f 20 20 20 20 20 20 20 20 28 63 68 61 72 20 2a 29 (char *)
1da20 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f pKey, P4_KEYINFO
1da21 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 _HANDOFF);. V
1da22 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
1da23 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 %s", pIdx->zName
1da24 29 29 3b 0a 20 20 20 20 72 65 67 46 69 65 6c 64 ));. regField
1da25 73 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 3b s = iMem+nCol*2;
1da26 0a 20 20 20 20 72 65 67 54 65 6d 70 20 3d 20 72 . regTemp = r
1da27 65 67 52 6f 77 69 64 20 3d 20 72 65 67 43 6f 6c egRowid = regCol
1da28 20 3d 20 72 65 67 46 69 65 6c 64 73 2b 33 3b 0a = regFields+3;.
1da29 20 20 20 20 72 65 67 52 65 63 20 3d 20 72 65 67 regRec = reg
1da2a 43 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28 20 72 Col+1;. if( r
1da2b 65 67 52 65 63 3e 70 50 61 72 73 65 2d 3e 6e 4d egRec>pParse->nM
1da2c 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 em ){. pPar
1da2d 73 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65 se->nMem = regRe
1da2e 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a c;. }.. /*
1da2f 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 61 72 Memory cells ar
1da30 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 e used as follow
1da31 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s:. **. **
1da32 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a 20 20 mem[iMem]:
1da33 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 74 The t
1da34 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 otal number of r
1da35 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ows in the table
1da36 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b .. ** mem[
1da37 69 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 20 20 iMem+1]:
1da38 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73 Number of dis
1da39 74 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20 tinct values in
1da3a 63 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 column 1. **
1da3b 20 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 .... **
1da3c 20 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a mem[iMem+nCol]:
1da3d 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f Number o
1da3e 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 f distinct value
1da3f 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 s in column N.
1da40 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d ** mem[iMem
1da41 2b 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 20 4c +nCol+1] L
1da42 61 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c ast observed val
1da43 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 0a 20 ue of column 1.
1da44 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 20 ** ....
1da45 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b ** mem[iMem+
1da46 6e 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 4c 61 nCol+nCol]: La
1da47 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75 st observed valu
1da48 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 e of column N.
1da49 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c **. ** Cell
1da4a 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 s iMem through i
1da4b 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 Mem+nCol are ini
1da4c 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 20 tialized to 0.
1da4d 54 68 65 20 6f 74 68 65 72 73 0a 20 20 20 20 2a The others. *
1da4e 2a 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 * are initialize
1da4f 64 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a d to NULL.. *
1da50 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 /. for(i=0; i
1da51 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <=nCol; i++){.
1da52 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1da53 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
1da54 67 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b ger, 0, iMem+i);
1da55 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
1da56 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 =0; i<nCol; i++)
1da57 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1da58 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1da59 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 Null, 0, iMem+nC
1da5a 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a ol+i+1);. }..
1da5b 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 61 6e /* Do the an
1da5c 61 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f 0a 20 alysis.. */.
1da5d 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 endOfLoop = s
1da5e 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 qlite3VdbeMakeLa
1da5f 62 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 bel(v);. sqli
1da60 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1da61 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78 OP_Rewind, iIdx
1da62 43 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b Cur, endOfLoop);
1da63 0a 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d . topOfLoop =
1da64 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
1da65 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 entAddr(v);.
1da66 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1da67 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 2(v, OP_AddImm,
1da68 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 66 6f iMem, 1);. fo
1da69 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 r(i=0; i<nCol; i
1da6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
1da6b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
1da6c 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 OP_Column, iIdxC
1da6d 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b 0a ur, i, regCol);.
1da6e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1da6f 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 eAddOp3(v, OP_Ne
1da70 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65 , regCol, 0, iMe
1da71 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 m+nCol+i+1);.
1da72 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20 /**** TODO:
1da73 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 add collating se
1da74 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20 quence *****/.
1da75 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1da76 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 hangeP5(v, SQLIT
1da77 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 E_JUMPIFNULL);.
1da78 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1da79 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1da7a 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c _Goto, 0, endOfL
1da7b 6f 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d oop);. for(i=
1da7c 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0; i<nCol; i++){
1da7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1da7e 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 6f beJumpHere(v, to
1da7f 70 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69 20 2b pOfLoop + 2*(i +
1da80 20 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 1));. sqli
1da81 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1da82 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d OP_AddImm, iMem
1da83 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 +i+1, 1);.
1da84 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1da85 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 3(v, OP_Column,
1da86 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d iIdxCur, i, iMem
1da87 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 +nCol+i+1);.
1da88 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
1da89 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c eResolveLabel(v,
1da8a 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 endOfLoop);.
1da8b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1da8c 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 p2(v, OP_Next, i
1da8d 49 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f IdxCur, topOfLoo
1da8e 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 p);. sqlite3V
1da8f 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
1da90 43 6c 6f 73 65 2c 20 69 49 64 78 43 75 72 29 3b Close, iIdxCur);
1da91 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 .. /* Store t
1da92 68 65 20 72 65 73 75 6c 74 73 2e 20 20 0a 20 20 he results. .
1da93 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
1da94 72 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67 result is a sing
1da95 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71 le row of the sq
1da96 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 lite_stat1 table
1da97 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20 . The first.
1da98 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20 ** two columns
1da99 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 are the names of
1da9a 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 the table and i
1da9b 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64 ndex. The third
1da9c 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69 column. ** i
1da9d 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f s a string compo
1da9e 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 sed of a list of
1da9f 20 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74 integer statist
1daa0 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20 ics about the.
1daa1 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65 ** index. The
1daa2 20 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69 first integer i
1daa3 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68 n the list is th
1daa4 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
1daa5 66 20 65 6e 74 69 72 65 73 0a 20 20 20 20 2a 2a f entires. **
1daa6 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20 in the index.
1daa7 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64 There is one add
1daa8 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 itional integer
1daa9 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 in the list for
1daaa 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 each. ** colu
1daab 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e mn of the table.
1daac 20 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61 This additiona
1daad 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67 l integer is a g
1daae 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79 uess of how many
1daaf 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20 . ** rows of
1dab0 74 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e the table the in
1dab1 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e dex will select.
1dab2 20 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f If D is the co
1dab3 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a unt of distinct.
1dab4 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e ** values an
1dab5 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c d K is the total
1dab6 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c number of rows,
1dab7 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65 then the intege
1dab8 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20 r is computed.
1dab9 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a ** as:. **.
1daba 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20 ** I
1dabb 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20 = (K+D-1)/D.
1dabc 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d **. ** If K==
1dabd 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20 0 then no entry
1dabe 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65 is made into the
1dabf 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 sqlite_stat1 ta
1dac0 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66 ble. . ** If
1dac1 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 K>0 then it is
1dac2 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20 always the case
1dac3 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73 the D>0 so divis
1dac4 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20 ion by zero.
1dac5 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73 ** is never poss
1dac6 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ible.. */.
1dac7 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 addr = sqlite3V
1dac8 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
1dac9 49 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20 IfNot, iMem);.
1daca 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1dacb 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 Op4(v, OP_String
1dacc 38 2c 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2c 8, 0, regFields,
1dacd 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0, pTab->zName,
1dace 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
1dacf 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1dad0 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 _String8, 0, reg
1dad1 46 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70 49 64 Fields+1, 0, pId
1dad2 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 x->zName, 0);.
1dad3 20 20 72 65 67 46 32 20 3d 20 72 65 67 46 69 65 regF2 = regFie
1dad4 6c 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 lds+2;. sqlit
1dad5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1dad6 4f 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 OP_SCopy, iMem,
1dad7 72 65 67 46 32 29 3b 0a 20 20 20 20 66 6f 72 28 regF2);. for(
1dad8 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b i=0; i<nCol; i++
1dad9 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1dada 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1dadb 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 _String8, 0, reg
1dadc 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30 29 Temp, 0, " ", 0)
1dadd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1dade 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
1dadf 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c Concat, regTemp,
1dae0 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a regF2, regF2);.
1dae1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1dae2 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 eAddOp3(v, OP_Ad
1dae3 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b d, iMem, iMem+i+
1dae4 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 1, regTemp);.
1dae5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1dae6 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d dOp2(v, OP_AddIm
1dae7 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b m, regTemp, -1);
1dae8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1dae9 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 beAddOp3(v, OP_D
1daea 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c ivide, iMem+i+1,
1daeb 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d regTemp, regTem
1daec 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 p);. sqlite
1daed 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
1daee 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70 P_ToInt, regTemp
1daef 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1daf0 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1daf1 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 _Concat, regTemp
1daf2 2c 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b , regF2, regF2);
1daf3 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1daf4 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
1daf5 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 OP_MakeRecord, r
1daf6 65 67 46 69 65 6c 64 73 2c 20 33 2c 20 72 65 67 egFields, 3, reg
1daf7 52 65 63 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a Rec, "aaa", 0);.
1daf8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1daf9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 ddOp2(v, OP_NewR
1dafa 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20 owid, iStatCur,
1dafb 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 regRowid);. s
1dafc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1dafd 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 (v, OP_Insert, i
1dafe 53 74 61 74 43 75 72 2c 20 72 65 67 52 65 63 2c StatCur, regRec,
1daff 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 regRowid);.
1db00 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1db01 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 eP5(v, OPFLAG_AP
1db02 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 PEND);. sqlit
1db03 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
1db04 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a , addr);. }.}..
1db05 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
1db06 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61 ode that will ca
1db07 75 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 use the most rec
1db08 65 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 ent index analys
1db09 69 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64 is to.** be laod
1db0a 65 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c ed into internal
1db0b 20 68 61 73 68 20 74 61 62 6c 65 73 20 77 68 65 hash tables whe
1db0c 72 65 20 69 73 20 63 61 6e 20 62 65 20 75 73 65 re is can be use
1db0d 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
1db0e 64 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 50 d loadAnalysis(P
1db0f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
1db10 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a t iDb){. Vdbe *
1db11 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
1db12 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 be(pParse);. if
1db13 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( v ){. sqlit
1db14 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
1db15 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c OP_LoadAnalysis,
1db16 20 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a iDb);. }.}../*
1db17 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
1db18 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 e that will do a
1db19 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e n analysis of an
1db1a 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 entire database
1db1b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1db1c 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 analyzeDatabase(
1db1d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
1db1e 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 nt iDb){. sqlit
1db1f 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
1db20 3e 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 >db;. Schema *p
1db21 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 Schema = db->aDb
1db22 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 [iDb].pSchema;
1db23 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 /* Schema of d
1db24 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 atabase iDb */.
1db25 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 HashElem *k;.
1db26 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20 int iStatCur;.
1db27 69 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c int iMem;.. sql
1db28 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 ite3BeginWriteOp
1db29 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 eration(pParse,
1db2a 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 0, iDb);. iStat
1db2b 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 Cur = pParse->nT
1db2c 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74 ab++;. openStat
1db2d 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 Table(pParse, iD
1db2e 62 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b b, iStatCur, 0);
1db2f 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 . iMem = pParse
1db30 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28 ->nMem+1;. for(
1db31 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 k=sqliteHashFirs
1db32 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 t(&pSchema->tblH
1db33 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 ash); k; k=sqlit
1db34 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 eHashNext(k)){.
1db35 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d Table *pTab =
1db36 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 (Table*)sqliteH
1db37 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 ashData(k);.
1db38 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 analyzeOneTable(
1db39 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 pParse, pTab, iS
1db3a 74 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 tatCur, iMem);.
1db3b 20 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 }. loadAnalysi
1db3c 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a s(pParse, iDb);.
1db3d 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
1db3e 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c e code that will
1db3f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 do an analysis
1db40 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c of a single tabl
1db41 65 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 e in.** a databa
1db42 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f se..*/.static vo
1db43 69 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 id analyzeTable(
1db44 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 Parse *pParse, T
1db45 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 able *pTab){. i
1db46 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 nt iDb;. int iS
1db47 74 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 tatCur;.. asser
1db48 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 t( pTab!=0 );.
1db49 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1db4a 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
1db4b 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 xes(pParse->db)
1db4c 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 );. iDb = sqlit
1db4d 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1db4e 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 pParse->db, pTab
1db4f 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 ->pSchema);. sq
1db50 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f lite3BeginWriteO
1db51 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c peration(pParse,
1db52 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 0, iDb);. iSta
1db53 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e tCur = pParse->n
1db54 54 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 Tab++;. openSta
1db55 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 tTable(pParse, i
1db56 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54 Db, iStatCur, pT
1db57 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e ab->zName);. an
1db58 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 alyzeOneTable(pP
1db59 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61 arse, pTab, iSta
1db5a 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d tCur, pParse->nM
1db5b 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61 em+1);. loadAna
1db5c 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44 lysis(pParse, iD
1db5d 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e b);.}../*.** Gen
1db5e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 erate code for t
1db5f 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 he ANALYZE comma
1db60 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 nd. The parser
1db61 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 calls this routi
1db62 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65 ne.** when it re
1db63 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c cognizes an ANAL
1db64 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a YZE command..**.
1db65 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a ** ANALYZ
1db66 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
1db67 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 --
1db68 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 1.** ANAL
1db69 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20 YZE <database>
1db6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d -
1db6b 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e - 2.** AN
1db6c 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73 ALYZE ?<databas
1db6d 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 e>.?<tablename>
1db6e 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d -- 3.**.** Form
1db6f 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 1 causes all in
1db70 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 dices in all att
1db71 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 ached databases
1db72 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a to be analyzed..
1db73 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a ** Form 2 analyz
1db74 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74 es all indices t
1db75 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 he single databa
1db76 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72 se named..** For
1db77 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c m 3 analyzes all
1db78 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 indices associa
1db79 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d ted with the nam
1db7a 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c ed table..*/.SQL
1db7b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1db7c 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 sqlite3Analyze(
1db7d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 Parse *pParse, T
1db7e 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f oken *pName1, To
1db7f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 ken *pName2){.
1db80 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1db81 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 arse->db;. int
1db82 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 iDb;. int i;.
1db83 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20 char *z, *zDb;.
1db84 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 Table *pTab;.
1db85 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d Token *pTableNam
1db86 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 e;.. /* Read th
1db87 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
1db88 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f a. If an error o
1db89 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 ccurs, leave an
1db8a 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 error message.
1db8b 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 ** and code in p
1db8c 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e Parse and return
1db8d 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 NULL. */. asse
1db8e 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1db8f 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
1db90 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 pParse->db) );.
1db91 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
1db92 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d sqlite3ReadSchem
1db93 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 a(pParse) ){.
1db94 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 return;. }..
1db95 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b if( pName1==0 ){
1db96 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a 20 . /* Form 1:
1db97 20 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74 68 Analyze everyth
1db98 69 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 ing */. for(i
1db99 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1db9a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 ++){. if( i
1db9b 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 ==1 ) continue;
1db9c 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c 79 /* Do not analy
1db9d 7a 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 ze the TEMP data
1db9e 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 6e base */. an
1db9f 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 alyzeDatabase(pP
1dba0 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a arse, i);. }.
1dba1 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d }else if( pNam
1dba2 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d e2==0 || pName2-
1dba3 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 >n==0 ){. /*
1dba4 46 6f 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65 Form 2: Analyze
1dba5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 the database or
1dba6 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a table named */.
1dba7 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 iDb = sqlite
1dba8 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 3FindDb(db, pNam
1dba9 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 e1);. if( iDb
1dbaa 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61 >=0 ){. ana
1dbab 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 lyzeDatabase(pPa
1dbac 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d rse, iDb);. }
1dbad 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 else{. z =
1dbae 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
1dbaf 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 oken(db, pName1)
1dbb0 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b ;. if( z ){
1dbb1 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 . pTab =
1dbb2 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
1dbb3 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c le(pParse, 0, z,
1dbb4 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 0);. sql
1dbb5 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1dbb6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
1dbb7 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Tab ){.
1dbb8 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 analyzeTable(pP
1dbb9 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 arse, pTab);.
1dbba 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1dbbb 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1dbbc 20 20 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61 /* Form 3: Ana
1dbbd 6c 79 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71 lyze the fully q
1dbbe 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e ualified table n
1dbbf 61 6d 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d ame */. iDb =
1dbc0 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e sqlite3TwoPartN
1dbc1 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d ame(pParse, pNam
1dbc2 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61 e1, pName2, &pTa
1dbc3 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 bleName);. if
1dbc4 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 ( iDb>=0 ){.
1dbc5 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b zDb = db->aDb[
1dbc6 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 iDb].zName;.
1dbc7 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d z = sqlite3Nam
1dbc8 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 eFromToken(db, p
1dbc9 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 TableName);.
1dbca 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 if( z ){.
1dbcb 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 pTab = sqlite
1dbcc 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 3LocateTable(pPa
1dbcd 72 73 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b rse, 0, z, zDb);
1dbce 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1dbcf 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 DbFree(db, z);.
1dbd0 20 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 if( pTab
1dbd1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 ){. ana
1dbd2 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 lyzeTable(pParse
1dbd3 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 , pTab);.
1dbd4 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1dbd5 20 20 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a . }.}../*.**
1dbd6 20 55 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e Used to pass in
1dbd7 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 formation from t
1dbd8 68 65 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64 he analyzer read
1dbd9 65 72 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 er through to th
1dbda 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f e.** callback ro
1dbdb 75 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 utine..*/.typede
1dbdc 66 20 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69 f struct analysi
1dbdd 73 49 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e sInfo analysisIn
1dbde 66 6f 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79 fo;.struct analy
1dbdf 73 69 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 sisInfo {. sqli
1dbe0 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 te3 *db;. const
1dbe1 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 char *zDatabase
1dbe2 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ;.};../*.** This
1dbe3 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 callback is inv
1dbe4 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 oked once for ea
1dbe5 63 68 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65 ch index when re
1dbe6 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c ading the.** sql
1dbe7 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e ite_stat1 table.
1dbe8 20 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67 .**.** arg
1dbe9 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 v[0] = name of t
1dbea 68 65 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 he index.**
1dbeb 61 72 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74 argv[1] = result
1dbec 73 20 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20 s of analysis -
1dbed 6f 6e 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65 on integer for e
1dbee 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 ach column.*/.st
1dbef 61 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69 atic int analysi
1dbf0 73 4c 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44 sLoader(void *pD
1dbf1 61 74 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 ata, int argc, c
1dbf2 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 har **argv, char
1dbf3 20 2a 2a 61 7a 4e 6f 74 55 73 65 64 29 7b 0a 20 **azNotUsed){.
1dbf4 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 2a 70 analysisInfo *p
1dbf5 49 6e 66 6f 20 3d 20 28 61 6e 61 6c 79 73 69 73 Info = (analysis
1dbf6 49 6e 66 6f 2a 29 70 44 61 74 61 3b 0a 20 20 49 Info*)pData;. I
1dbf7 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 ndex *pIndex;.
1dbf8 69 6e 74 20 69 2c 20 63 3b 0a 20 20 75 6e 73 69 int i, c;. unsi
1dbf9 67 6e 65 64 20 69 6e 74 20 76 3b 0a 20 20 63 6f gned int v;. co
1dbfa 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 nst char *z;..
1dbfb 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20 assert( argc==2
1dbfc 29 3b 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30 );. if( argv==0
1dbfd 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d 30 20 7c || argv[0]==0 |
1dbfe 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a | argv[1]==0 ){.
1dbff 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1dc00 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c }. pIndex = sql
1dc01 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 70 49 ite3FindIndex(pI
1dc02 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b 30 5d nfo->db, argv[0]
1dc03 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 62 61 , pInfo->zDataba
1dc04 73 65 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 se);. if( pInde
1dc05 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 x==0 ){. retu
1dc06 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 rn 0;. }. z =
1dc07 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 argv[1];. for(i
1dc08 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d 70 49 6e =0; *z && i<=pIn
1dc09 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b dex->nColumn; i+
1dc0a 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30 3b 0a 20 +){. v = 0;.
1dc0b 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b 30 while( (c=z[0
1dc0c 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 ])>='0' && c<='9
1dc0d 27 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 76 ' ){. v = v
1dc0e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 *10 + c - '0';.
1dc0f 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a z++;. }.
1dc10 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 52 6f pIndex->aiRo
1dc11 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a 20 20 20 wEst[i] = v;.
1dc12 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20 29 20 7a if( *z==' ' ) z
1dc13 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ++;. }. return
1dc14 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 0;.}../*.** Loa
1dc15 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 d the content of
1dc16 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
1dc17 31 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 1 table into the
1dc18 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c index hash tabl
1dc19 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
1dc1a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1dc1b 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 3AnalysisLoad(sq
1dc1c 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 lite3 *db, int i
1dc1d 44 62 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49 Db){. analysisI
1dc1e 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20 20 48 61 73 nfo sInfo;. Has
1dc1f 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 63 68 61 72 hElem *i;. char
1dc20 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 72 63 *zSql;. int rc
1dc21 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 ;.. assert( iDb
1dc22 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e >=0 && iDb<db->n
1dc23 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
1dc24 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 db->aDb[iDb].pBt
1dc25 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1dc26 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
1dc27 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b dsMutex(db->aDb[
1dc28 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20 20 iDb].pBt) );..
1dc29 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20 70 72 69 /* Clear any pri
1dc2a 6f 72 20 73 74 61 74 69 73 74 69 63 73 20 2a 2f or statistics */
1dc2b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 . for(i=sqliteH
1dc2c 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 44 ashFirst(&db->aD
1dc2d 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e b[iDb].pSchema->
1dc2e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d 73 71 6c idxHash);i;i=sql
1dc2f 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b iteHashNext(i)){
1dc30 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 . Index *pIdx
1dc31 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 = sqliteHashDat
1dc32 61 28 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 a(i);. sqlite
1dc33 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 70 3DefaultRowEst(p
1dc34 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Idx);. }.. /*
1dc35 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 Check to make su
1dc36 72 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 re the sqlite_st
1dc37 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74 73 at1 table exists
1dc38 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e 64 62 20 s */. sInfo.db
1dc39 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44 = db;. sInfo.zD
1dc3a 61 74 61 62 61 73 65 20 3d 20 64 62 2d 3e 61 44 atabase = db->aD
1dc3b 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 b[iDb].zName;.
1dc3c 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 if( sqlite3FindT
1dc3d 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 able(db, "sqlite
1dc3e 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 6f 2e 7a _stat1", sInfo.z
1dc3f 44 61 74 61 62 61 73 65 29 3d 3d 30 20 29 7b 0a Database)==0 ){.
1dc40 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1dc41 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 0a TE_ERROR;. }...
1dc42 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 20 73 74 /* Load new st
1dc43 61 74 69 73 74 69 63 73 20 6f 75 74 20 6f 66 20 atistics out of
1dc44 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 the sqlite_stat1
1dc45 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 53 71 6c table */. zSql
1dc46 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
1dc47 66 28 64 62 2c 20 22 53 45 4c 45 43 54 20 69 64 f(db, "SELECT id
1dc48 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25 51 2e x, stat FROM %Q.
1dc49 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 0a 20 sqlite_stat1",.
1dc4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dc4b 20 20 20 20 20 20 20 73 49 6e 66 6f 2e 7a 44 61 sInfo.zDa
1dc4c 74 61 62 61 73 65 29 3b 0a 20 20 28 76 6f 69 64 tabase);. (void
1dc4d 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 )sqlite3SafetyOf
1dc4e 66 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71 f(db);. rc = sq
1dc4f 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a lite3_exec(db, z
1dc50 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f 61 Sql, analysisLoa
1dc51 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29 3b der, &sInfo, 0);
1dc52 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 . (void)sqlite3
1dc53 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 SafetyOn(db);.
1dc54 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1dc55 2c 20 7a 53 71 6c 29 3b 0a 20 20 72 65 74 75 72 , zSql);. retur
1dc56 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 n rc;.}...#endif
1dc57 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1dc58 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a ANALYZE */../***
1dc59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1dc5a 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a of analyze.c ***
1dc5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dc5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dc5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1dc5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1dc5f 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e 63 20 n file attach.c
1dc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dc61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dc62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1dc63 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a * 2003 April 6.*
1dc64 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
1dc65 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
1dc66 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
1dc67 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
1dc68 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
1dc69 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
1dc6a 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
1dc6b 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
1dc6c 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1dc6d 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1dc6e 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1dc6f 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
1dc70 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
1dc71 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
1dc72 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
1dc73 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
1dc74 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
1dc75 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
1dc76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dc77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dc78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dc79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1dc7a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
1dc7b 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f ins code used to
1dc7c 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 41 implement the A
1dc7d 54 54 41 43 48 20 61 6e 64 20 44 45 54 41 43 48 TTACH and DETACH
1dc7e 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a commands..**.**
1dc7f 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63 2c 76 $Id: attach.c,v
1dc80 20 31 2e 37 39 20 32 30 30 38 2f 31 30 2f 32 38 1.79 2008/10/28
1dc81 20 31 37 3a 35 32 3a 33 39 20 64 61 6e 69 65 6c 17:52:39 daniel
1dc82 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
1dc83 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1dc84 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a 2a MIT_ATTACH./*.**
1dc85 20 52 65 73 6f 6c 76 65 20 61 6e 20 65 78 70 72 Resolve an expr
1dc86 65 73 73 69 6f 6e 20 74 68 61 74 20 77 61 73 20 ession that was
1dc87 70 61 72 74 20 6f 66 20 61 6e 20 41 54 54 41 43 part of an ATTAC
1dc88 48 20 6f 72 20 44 45 54 41 43 48 20 73 74 61 74 H or DETACH stat
1dc89 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 69 ement. This.** i
1dc8a 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 s slightly diffe
1dc8b 72 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76 rent from resolv
1dc8c 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c ing a normal SQL
1dc8d 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 65 63 expression, bec
1dc8e 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 69 ause simple.** i
1dc8f 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 20 74 dentifiers are t
1dc90 72 65 61 74 65 64 20 61 73 20 73 74 72 69 6e 67 reated as string
1dc91 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 s, not possible
1dc92 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20 column names or
1dc93 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 aliases..**.** i
1dc94 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72 73 65 .e. if the parse
1dc95 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 r sees:.**.**
1dc96 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 ATTACH DATABAS
1dc97 45 20 61 62 63 20 41 53 20 64 65 66 0a 2a 2a 0a E abc AS def.**.
1dc98 2a 2a 20 69 74 20 74 72 65 61 74 73 20 74 68 65 ** it treats the
1dc99 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 two expressions
1dc9a 20 61 73 20 6c 69 74 65 72 61 6c 20 73 74 72 69 as literal stri
1dc9b 6e 67 73 20 27 61 62 63 27 20 61 6e 64 20 27 64 ngs 'abc' and 'd
1dc9c 65 66 27 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a ef' instead of.*
1dc9d 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 6f * looking for co
1dc9e 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 61 6d lumns of the sam
1dc9f 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e name..**.** Th
1dca0 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 is only applies
1dca1 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 to the root node
1dca2 20 6f 66 20 70 45 78 70 72 2c 20 73 6f 20 74 68 of pExpr, so th
1dca3 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a e statement:.**.
1dca4 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 44 41 ** ATTACH DA
1dca5 54 41 42 41 53 45 20 61 62 63 7c 7c 64 65 66 20 TABASE abc||def
1dca6 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 77 AS 'db2'.**.** w
1dca7 69 6c 6c 20 66 61 69 6c 20 62 65 63 61 75 73 65 ill fail because
1dca8 20 6e 65 69 74 68 65 72 20 61 62 63 20 6f 72 20 neither abc or
1dca9 64 65 66 20 63 61 6e 20 62 65 20 72 65 73 6f 6c def can be resol
1dcaa 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ved..*/.static i
1dcab 6e 74 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 nt resolveAttach
1dcac 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74 Expr(NameContext
1dcad 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70 *pName, Expr *p
1dcae 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20 72 63 Expr).{. int rc
1dcaf 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1dcb0 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 if( pExpr ){.
1dcb1 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d if( pExpr->op!=
1dcb2 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 72 TK_ID ){. r
1dcb3 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 6f 6c c = sqlite3Resol
1dcb4 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 61 6d veExprNames(pNam
1dcb5 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 e, pExpr);.
1dcb6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1dcb7 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78 OK && !sqlite3Ex
1dcb8 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 45 78 prIsConstant(pEx
1dcb9 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 pr) ){. s
1dcba 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1dcbb 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20 22 69 Name->pParse, "i
1dcbc 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 25 nvalid name: \"%
1dcbd 54 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e 73 70 T\"", &pExpr->sp
1dcbe 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 an);. ret
1dcbf 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
1dcc0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
1dcc1 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72 lse{. pExpr
1dcc2 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47 ->op = TK_STRING
1dcc3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1dcc4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1dcc5 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 * An SQL user-fu
1dcc6 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 nction registere
1dcc7 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b d to do the work
1dcc8 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73 74 of an ATTACH st
1dcc9 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 atement. The.**
1dcca 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 three arguments
1dccb 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 to the function
1dccc 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72 come directly fr
1dccd 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74 61 om an attach sta
1dcce 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 tement:.**.**
1dccf 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 ATTACH DATABAS
1dcd0 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a E x AS y KEY z.*
1dcd1 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 *.** SELECT
1dcd2 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78 2c sqlite_attach(x,
1dcd3 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20 y, z).**.** If
1dcd4 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45 the optional "KE
1dcd5 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20 6f Y z" syntax is o
1dcd6 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e mitted, an SQL N
1dcd7 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61 73 ULL is passed as
1dcd8 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 the.** third ar
1dcd9 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 gument..*/.stati
1dcda 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 75 6e c void attachFun
1dcdb 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
1dcdc 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
1dcdd 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
1dcde 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
1dcdf 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 v.){. int i;.
1dce0 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 73 71 int rc = 0;. sq
1dce1 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
1dce2 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
1dce3 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
1dce4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
1dce5 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 ame;. const cha
1dce6 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 44 62 20 2a r *zFile;. Db *
1dce7 61 4e 65 77 3b 0a 20 20 63 68 61 72 20 2a 7a 45 aNew;. char *zE
1dce8 72 72 44 79 6e 20 3d 20 30 3b 0a 20 20 63 68 61 rrDyn = 0;. cha
1dce9 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 r zErr[128];..
1dcea 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 zFile = (const c
1dceb 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 har *)sqlite3_va
1dcec 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
1dced 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f );. zName = (co
1dcee 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 nst char *)sqlit
1dcef 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
1dcf0 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 46 gv[1]);. if( zF
1dcf1 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65 20 3d ile==0 ) zFile =
1dcf2 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 "";. if( zName
1dcf3 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 ==0 ) zName = ""
1dcf4 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f ;.. /* Check fo
1dcf5 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 r the following
1dcf6 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20 20 2a errors:. **. *
1dcf7 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d 61 6e 79 * * Too many
1dcf8 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 attached databa
1dcf9 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20 20 2a 20 ses,. ** *
1dcfa 54 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 Transaction curr
1dcfb 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 2a 2a 20 ently open. **
1dcfc 20 20 20 20 2a 20 53 70 65 63 69 66 69 65 64 20 * Specified
1dcfd 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6c database name al
1dcfe 72 65 61 64 79 20 62 65 69 6e 67 20 75 73 65 64 ready being used
1dcff 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d .. */. if( db-
1dd00 3e 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 >nDb>=db->aLimit
1dd01 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 [SQLITE_LIMIT_AT
1dd02 54 41 43 48 45 44 5d 2b 32 20 29 7b 0a 20 20 20 TACHED]+2 ){.
1dd03 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1dd04 66 28 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 f(. sizeof(
1dd05 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 74 6f zErr), zErr, "to
1dd06 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20 o many attached
1dd07 64 61 74 61 62 61 73 65 73 20 2d 20 6d 61 78 20 databases - max
1dd08 25 64 22 2c 20 0a 20 20 20 20 20 20 64 62 2d 3e %d", . db->
1dd09 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1dd0a 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 0a 20 20 MIT_ATTACHED].
1dd0b 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 );. goto at
1dd0c 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a tach_error;. }.
1dd0d 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 if( !db->autoC
1dd0e 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c ommit ){. sql
1dd0f 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
1dd10 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 zeof(zErr), zErr
1dd11 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1dd12 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 41 "cannot A
1dd13 54 54 41 43 48 20 64 61 74 61 62 61 73 65 20 77 TTACH database w
1dd14 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f ithin transactio
1dd15 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 n");. goto at
1dd16 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a tach_error;. }.
1dd17 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
1dd18 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
1dd19 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d 3e 61 44 char *z = db->aD
1dd1a 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 b[i].zName;.
1dd1b 69 66 28 20 7a 20 26 26 20 7a 4e 61 6d 65 20 26 if( z && zName &
1dd1c 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 & sqlite3StrICmp
1dd1d 28 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b (z, zName)==0 ){
1dd1e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1dd1f 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
1dd20 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a 20 20 20 Err), zErr, .
1dd21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd22 20 20 20 20 22 64 61 74 61 62 61 73 65 20 25 73 "database %s
1dd23 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 is already in u
1dd24 73 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 se", zName);.
1dd25 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 goto attach_e
1dd26 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rror;. }. }.
1dd27 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 . /* Allocate t
1dd28 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 he new entry in
1dd29 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 the db->aDb[] ar
1dd2a 72 61 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ray and initiali
1dd2b 73 65 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 se the schema.
1dd2c 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a ** hash tables..
1dd2d 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 */. if( db->a
1dd2e 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 Db==db->aDbStati
1dd2f 63 20 29 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 c ){. aNew =
1dd30 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
1dd31 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 64 62 aw(db, sizeof(db
1dd32 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b 0a 20 ->aDb[0])*3 );.
1dd33 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 if( aNew==0 )
1dd34 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6d 65 6d return;. mem
1dd35 63 70 79 28 61 4e 65 77 2c 20 64 62 2d 3e 61 44 cpy(aNew, db->aD
1dd36 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 b, sizeof(db->aD
1dd37 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d 65 6c 73 b[0])*2);. }els
1dd38 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 e{. aNew = sq
1dd39 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 lite3DbRealloc(d
1dd3a 62 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65 b, db->aDb, size
1dd3b 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 28 of(db->aDb[0])*(
1dd3c 64 62 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a 20 20 db->nDb+1) );.
1dd3d 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 if( aNew==0 )
1dd3e 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 64 62 return;. }. db
1dd3f 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20 ->aDb = aNew;.
1dd40 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b aNew = &db->aDb[
1dd41 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20 20 6d 65 db->nDb++];. me
1dd42 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c 20 73 69 mset(aNew, 0, si
1dd43 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b 0a 0a 20 zeof(*aNew));..
1dd44 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64 61 74 /* Open the dat
1dd45 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 abase file. If t
1dd46 68 65 20 62 74 72 65 65 20 69 73 20 73 75 63 63 he btree is succ
1dd47 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c essfully opened,
1dd48 20 75 73 65 0a 20 20 2a 2a 20 69 74 20 74 6f 20 use. ** it to
1dd49 6f 62 74 61 69 6e 20 74 68 65 20 64 61 74 61 62 obtain the datab
1dd4a 61 73 65 20 73 63 68 65 6d 61 2e 20 41 74 20 74 ase schema. At t
1dd4b 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 73 63 his point the sc
1dd4c 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a 20 6f 72 hema may. ** or
1dd4d 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 69 74 may not be init
1dd4e 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 ialised.. */.
1dd4f 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1dd50 65 46 61 63 74 6f 72 79 28 64 62 2c 20 7a 46 69 eFactory(db, zFi
1dd51 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 le, 0, SQLITE_DE
1dd52 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 FAULT_CACHE_SIZE
1dd53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1dd54 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d db-
1dd55 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20 53 51 4c >openFlags | SQL
1dd56 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
1dd57 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1dd58 20 20 20 20 20 20 20 20 20 20 20 20 20 26 61 4e &aN
1dd59 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 ew->pBt);. if(
1dd5a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1dd5b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 . Pager *pPag
1dd5c 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e 70 53 er;. aNew->pS
1dd5d 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 chema = sqlite3S
1dd5e 63 68 65 6d 61 47 65 74 28 64 62 2c 20 61 4e 65 chemaGet(db, aNe
1dd5f 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 w->pBt);. if(
1dd60 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 !aNew->pSchema
1dd61 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
1dd62 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
1dd63 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77 2d 3e }else if( aNew->
1dd64 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f pSchema->file_fo
1dd65 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e 70 53 rmat && aNew->pS
1dd66 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e 43 28 chema->enc!=ENC(
1dd67 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c db) ){. sql
1dd68 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
1dd69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 zeof(zErr), zErr
1dd6a 2c 20 0a 20 20 20 20 20 20 20 20 22 61 74 74 61 , . "atta
1dd6b 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d ched databases m
1dd6c 75 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65 ust use the same
1dd6d 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 text encoding a
1dd6e 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22 s main database"
1dd6f 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 74 );. goto at
1dd70 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 tach_error;.
1dd71 7d 0a 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 }. pPager = s
1dd72 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
1dd73 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 (aNew->pBt);.
1dd74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 sqlite3PagerLoc
1dd75 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c kingMode(pPager,
1dd76 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 db->dfltLockMod
1dd77 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 e);. sqlite3P
1dd78 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 agerJournalMode(
1dd79 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74 pPager, db->dflt
1dd7a 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20 JournalMode);.
1dd7b 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20 }. aNew->zName
1dd7c 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
1dd7d 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 p(db, zName);.
1dd7e 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76 aNew->safety_lev
1dd7f 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c el = 3;..#if SQL
1dd80 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 ITE_HAS_CODEC.
1dd81 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 {. extern int
1dd82 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74 sqlite3CodecAtt
1dd83 61 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e ach(sqlite3*, in
1dd84 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 t, const void*,
1dd85 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e int);. extern
1dd86 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 void sqlite3Cod
1dd87 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33 ecGetKey(sqlite3
1dd88 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 *, int, void**,
1dd89 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e int*);. int n
1dd8a 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a Key;. char *z
1dd8b 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d Key;. int t =
1dd8c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1dd8d 79 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 ype(argv[2]);.
1dd8e 20 20 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20 switch( t ){.
1dd8f 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 case SQLITE
1dd90 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20 _INTEGER:.
1dd91 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 case SQLITE_FLOA
1dd92 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44 T:. zErrD
1dd93 79 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 yn = sqlite3DbSt
1dd94 72 44 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69 rDup(db, "Invali
1dd95 64 20 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20 d key value");.
1dd96 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
1dd97 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 TE_ERROR;.
1dd98 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1dd99 20 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c . case SQL
1dd9a 49 54 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 ITE_TEXT:.
1dd9b 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 case SQLITE_BLOB
1dd9c 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d :. nKey =
1dd9d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
1dd9e 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 ytes(argv[2]);.
1dd9f 20 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63 zKey = (c
1dda0 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 har *)sqlite3_va
1dda1 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d lue_blob(argv[2]
1dda2 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1dda3 65 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 e3CodecAttach(db
1dda4 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 , db->nDb-1, zKe
1dda5 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 y, nKey);.
1dda6 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 break;..
1dda7 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c case SQLITE_NULL
1dda8 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 :. /* No
1dda9 6b 65 79 20 73 70 65 63 69 66 69 65 64 2e 20 20 key specified.
1ddaa 55 73 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d Use the key from
1ddab 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
1ddac 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 se */. sq
1ddad 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 lite3CodecGetKey
1ddae 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 (db, 0, (void**)
1ddaf 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 &zKey, &nKey);.
1ddb0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f sqlite3Co
1ddb1 64 65 63 41 74 74 61 63 68 28 64 62 2c 20 64 62 decAttach(db, db
1ddb2 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e ->nDb-1, zKey, n
1ddb3 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72 Key);. br
1ddb4 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 eak;. }. }.#
1ddb5 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 endif.. /* If t
1ddb6 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e he file was open
1ddb7 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ed successfully,
1ddb8 20 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 read the schema
1ddb9 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74 for the new dat
1ddba 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74 abase.. ** If t
1ddbb 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66 his fails, or if
1ddbc 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c opening the fil
1ddbd 65 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63 e failed, then c
1ddbe 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e lose the file an
1ddbf 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 d . ** remove t
1ddc0 68 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 he entry from th
1ddc1 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61 e db->aDb[] arra
1ddc2 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72 y. i.e. put ever
1ddc3 79 74 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20 ything back the
1ddc4 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e way. ** we foun
1ddc5 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 d it.. */. if(
1ddc6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1ddc7 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 {. (void)sqli
1ddc8 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b te3SafetyOn(db);
1ddc9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1ddca 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 eEnterAll(db);.
1ddcb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 rc = sqlite3I
1ddcc 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79 6e nit(db, &zErrDyn
1ddcd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
1ddce 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b reeLeaveAll(db);
1ddcf 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 . (void)sqlit
1ddd0 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b e3SafetyOff(db);
1ddd1 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b . }. if( rc ){
1ddd2 0a 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 64 . int iDb = d
1ddd3 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 20 20 b->nDb - 1;.
1ddd4 61 73 73 65 72 74 28 20 69 44 62 3e 3d 32 20 29 assert( iDb>=2 )
1ddd5 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 ;. if( db->aD
1ddd6 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a 20 20 b[iDb].pBt ){.
1ddd7 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1ddd8 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 69 44 Close(db->aDb[iD
1ddd9 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64 b].pBt);. d
1ddda 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 b->aDb[iDb].pBt
1dddb 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 = 0;. db->a
1dddc 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 Db[iDb].pSchema
1dddd 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 = 0;. }. s
1ddde 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 qlite3ResetInter
1dddf 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 nalSchema(db, 0)
1dde0 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20 ;. db->nDb =
1dde1 69 44 62 3b 0a 20 20 20 20 69 66 28 20 72 63 3d iDb;. if( rc=
1dde2 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c =SQLITE_NOMEM ||
1dde3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 rc==SQLITE_IOER
1dde4 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 R_NOMEM ){.
1dde5 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1dde6 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c d = 1;. sql
1dde7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
1dde8 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c zeof(zErr),zErr,
1dde9 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 "out of memory"
1ddea 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1ddeb 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1ddec 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 intf(sizeof(zErr
1dded 29 2c 7a 45 72 72 2c 20 22 75 6e 61 62 6c 65 20 ),zErr, "unable
1ddee 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 to open database
1ddef 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 : %s", zFile);.
1ddf0 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 61 74 }. goto at
1ddf1 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a tach_error;. }.
1ddf2 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 61 74 . return;..at
1ddf3 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20 2f 2a tach_error:. /*
1ddf4 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
1ddf5 20 69 66 20 77 65 20 67 65 74 20 68 65 72 65 20 if we get here
1ddf6 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72 44 79 6e */. if( zErrDyn
1ddf7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1ddf8 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e result_error(con
1ddf9 74 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c 20 2d text, zErrDyn, -
1ddfa 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 1);. sqlite3D
1ddfb 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 44 79 bFree(db, zErrDy
1ddfc 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 n);. }else{.
1ddfd 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 zErr[sizeof(zEr
1ddfe 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20 20 20 73 r)-1] = 0;. s
1ddff 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1de00 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 ror(context, zEr
1de01 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 69 66 r, -1);. }. if
1de02 28 20 72 63 20 29 20 73 71 6c 69 74 65 33 5f 72 ( rc ) sqlite3_r
1de03 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 esult_error_code
1de04 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a 7d (context, rc);.}
1de05 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 ../*.** An SQL u
1de06 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67 ser-function reg
1de07 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68 istered to do th
1de08 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44 45 54 e work of an DET
1de09 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 ACH statement. T
1de0a 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75 he.** three argu
1de0b 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e ments to the fun
1de0c 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63 ction come direc
1de0d 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 74 61 63 tly from a detac
1de0e 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a h statement:.**.
1de0f 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 44 41 ** DETACH DA
1de10 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a 20 20 TABASE x.**.**
1de11 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65 SELECT sqlite
1de12 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a 73 74 _detach(x).*/.st
1de13 61 74 69 63 20 76 6f 69 64 20 64 65 74 61 63 68 atic void detach
1de14 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
1de15 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
1de16 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
1de17 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
1de18 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 argv.){. const
1de19 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 char *zName = (c
1de1a 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 onst char *)sqli
1de1b 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
1de1c 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74 rgv[0]);. sqlit
1de1d 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 e3 *db = sqlite3
1de1e 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
1de1f 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69 le(context);. i
1de20 6e 74 20 69 3b 0a 20 20 44 62 20 2a 70 44 62 20 nt i;. Db *pDb
1de21 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 = 0;. char zErr
1de22 5b 31 32 38 5d 3b 0a 0a 20 20 69 66 28 20 7a 4e [128];.. if( zN
1de23 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d ame==0 ) zName =
1de24 20 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 "";. for(i=0;
1de25 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
1de26 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d 3e . pDb = &db->
1de27 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 aDb[i];. if(
1de28 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 20 63 6f pDb->pBt==0 ) co
1de29 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 ntinue;. if(
1de2a 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 sqlite3StrICmp(p
1de2b 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 Db->zName, zName
1de2c 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 )==0 ) break;.
1de2d 7d 0a 0a 20 20 69 66 28 20 69 3e 3d 64 62 2d 3e }.. if( i>=db->
1de2e 6e 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 nDb ){. sqlit
1de2f 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 e3_snprintf(size
1de30 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22 of(zErr),zErr, "
1de31 6e 6f 20 73 75 63 68 20 64 61 74 61 62 61 73 65 no such database
1de32 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 : %s", zName);.
1de33 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 goto detach_e
1de34 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rror;. }. if(
1de35 69 3c 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 i<2 ){. sqlit
1de36 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 e3_snprintf(size
1de37 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22 of(zErr),zErr, "
1de38 63 61 6e 6e 6f 74 20 64 65 74 61 63 68 20 64 61 cannot detach da
1de39 74 61 62 61 73 65 20 25 73 22 2c 20 7a 4e 61 6d tabase %s", zNam
1de3a 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 e);. goto det
1de3b 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 ach_error;. }.
1de3c 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f if( !db->autoCo
1de3d 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 mmit ){. sqli
1de3e 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
1de3f 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c eof(zErr), zErr,
1de40 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1de41 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 44 45 "cannot DE
1de42 54 41 43 48 20 64 61 74 61 62 61 73 65 20 77 69 TACH database wi
1de43 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e thin transaction
1de44 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 ");. goto det
1de45 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 ach_error;. }.
1de46 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 if( sqlite3Btre
1de47 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70 eIsInReadTrans(p
1de48 44 62 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 Db->pBt) ){.
1de49 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1de4a 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 (sizeof(zErr),zE
1de4b 72 72 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 rr, "database %s
1de4c 20 69 73 20 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61 is locked", zNa
1de4d 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 me);. goto de
1de4e 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a tach_error;. }.
1de4f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 . sqlite3BtreeC
1de50 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a lose(pDb->pBt);.
1de51 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a pDb->pBt = 0;.
1de52 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d pDb->pSchema =
1de53 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 0;. sqlite3Res
1de54 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 etInternalSchema
1de55 28 64 62 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 (db, 0);. retur
1de56 6e 3b 0a 0a 64 65 74 61 63 68 5f 65 72 72 6f 72 n;..detach_error
1de57 3a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 :. sqlite3_resu
1de58 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 lt_error(context
1de59 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a , zErr, -1);.}..
1de5a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 /*.** This proce
1de5b 64 75 72 65 20 67 65 6e 65 72 61 74 65 73 20 56 dure generates V
1de5c 44 42 45 20 63 6f 64 65 20 66 6f 72 20 61 20 73 DBE code for a s
1de5d 69 6e 67 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e ingle invocation
1de5e 20 6f 66 20 65 69 74 68 65 72 20 74 68 65 0a 2a of either the.*
1de5f 2a 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28 * sqlite_detach(
1de60 29 20 6f 72 20 73 71 6c 69 74 65 5f 61 74 74 61 ) or sqlite_atta
1de61 63 68 28 29 20 53 51 4c 20 75 73 65 72 20 66 75 ch() SQL user fu
1de62 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 nctions..*/.stat
1de63 69 63 20 76 6f 69 64 20 63 6f 64 65 41 74 74 61 ic void codeAtta
1de64 63 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 ch(. Parse *pPa
1de65 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 rse, /* Th
1de66 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 e parser context
1de67 20 2a 2f 0a 20 20 69 6e 74 20 74 79 70 65 2c 20 */. int type,
1de68 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69 /* Ei
1de69 74 68 65 72 20 53 51 4c 49 54 45 5f 41 54 54 41 ther SQLITE_ATTA
1de6a 43 48 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 54 CH or SQLITE_DET
1de6b 41 43 48 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 ACH */. FuncDef
1de6c 20 2a 70 46 75 6e 63 2c 20 20 20 20 20 20 2f 2a *pFunc, /*
1de6d 20 46 75 6e 63 44 65 66 20 77 72 61 70 70 65 72 FuncDef wrapper
1de6e 20 66 6f 72 20 64 65 74 61 63 68 46 75 6e 63 28 for detachFunc(
1de6f 29 20 6f 72 20 61 74 74 61 63 68 46 75 6e 63 28 ) or attachFunc(
1de70 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 41 75 ) */. Expr *pAu
1de71 74 68 41 72 67 2c 20 20 20 20 20 20 2f 2a 20 45 thArg, /* E
1de72 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61 73 xpression to pas
1de73 73 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 s to authorizati
1de74 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 on callback */.
1de75 20 45 78 70 72 20 2a 70 46 69 6c 65 6e 61 6d 65 Expr *pFilename
1de76 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 , /* Name of
1de77 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
1de78 2f 0a 20 20 45 78 70 72 20 2a 70 44 62 6e 61 6d /. Expr *pDbnam
1de79 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 e, /* Name
1de7a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1de7b 20 74 6f 20 75 73 65 20 69 6e 74 65 72 6e 61 6c to use internal
1de7c 6c 79 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4b ly */. Expr *pK
1de7d 65 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ey /*
1de7e 44 61 74 61 62 61 73 65 20 6b 65 79 20 66 6f 72 Database key for
1de7f 20 65 6e 63 72 79 70 74 69 6f 6e 20 65 78 74 65 encryption exte
1de80 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e nsion */.){. in
1de81 74 20 72 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 t rc;. NameCont
1de82 65 78 74 20 73 4e 61 6d 65 3b 0a 20 20 56 64 62 ext sName;. Vdb
1de83 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 2a e *v;. sqlite3*
1de84 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
1de85 3b 0a 20 20 69 6e 74 20 72 65 67 41 72 67 73 3b ;. int regArgs;
1de86 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1de87 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
1de88 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 64 62 ION. assert( db
1de89 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c ->mallocFailed |
1de8a 7c 20 70 41 75 74 68 41 72 67 20 29 3b 0a 20 20 | pAuthArg );.
1de8b 69 66 28 20 70 41 75 74 68 41 72 67 20 29 7b 0a if( pAuthArg ){.
1de8c 20 20 20 20 63 68 61 72 20 2a 7a 41 75 74 68 41 char *zAuthA
1de8d 72 67 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 rg = sqlite3Name
1de8e 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70 FromToken(db, &p
1de8f 41 75 74 68 41 72 67 2d 3e 73 70 61 6e 29 3b 0a AuthArg->span);.
1de90 20 20 20 20 69 66 28 20 21 7a 41 75 74 68 41 72 if( !zAuthAr
1de91 67 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 g ){. goto
1de92 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 attach_end;.
1de93 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1de94 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
1de95 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74 68 41 se, type, zAuthA
1de96 72 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 rg, 0, 0);. s
1de97 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1de98 20 7a 41 75 74 68 41 72 67 29 3b 0a 20 20 20 20 zAuthArg);.
1de99 69 66 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b if(rc!=SQLITE_OK
1de9a 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 ){. goto a
1de9b 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 7d ttach_end;. }
1de9c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
1de9d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
1de9e 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 6d RIZATION */.. m
1de9f 65 6d 73 65 74 28 26 73 4e 61 6d 65 2c 20 30 2c emset(&sName, 0,
1dea0 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e 74 sizeof(NameCont
1dea1 65 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 2e 70 ext));. sName.p
1dea2 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
1dea3 0a 20 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 . if( . SQ
1dea4 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
1dea5 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 esolveAttachExpr
1dea6 28 26 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e 61 (&sName, pFilena
1dea7 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 me)) ||. SQ
1dea8 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
1dea9 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 esolveAttachExpr
1deaa 28 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d 65 (&sName, pDbname
1deab 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49 )) ||. SQLI
1deac 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 TE_OK!=(rc = res
1dead 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26 olveAttachExpr(&
1deae 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0a 20 20 sName, pKey)).
1deaf 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e ){. pParse->n
1deb0 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 Err++;. goto
1deb1 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 7d 0a attach_end;. }.
1deb2 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 . v = sqlite3Ge
1deb3 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
1deb4 20 72 65 67 41 72 67 73 20 3d 20 73 71 6c 69 74 regArgs = sqlit
1deb5 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 e3GetTempRange(p
1deb6 50 61 72 73 65 2c 20 34 29 3b 0a 20 20 73 71 6c Parse, 4);. sql
1deb7 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
1deb8 72 73 65 2c 20 70 46 69 6c 65 6e 61 6d 65 2c 20 rse, pFilename,
1deb9 72 65 67 41 72 67 73 29 3b 0a 20 20 73 71 6c 69 regArgs);. sqli
1deba 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 te3ExprCode(pPar
1debb 73 65 2c 20 70 44 62 6e 61 6d 65 2c 20 72 65 67 se, pDbname, reg
1debc 41 72 67 73 2b 31 29 3b 0a 20 20 73 71 6c 69 74 Args+1);. sqlit
1debd 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 e3ExprCode(pPars
1debe 65 2c 20 70 4b 65 79 2c 20 72 65 67 41 72 67 73 e, pKey, regArgs
1debf 2b 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 +2);.. assert(
1dec0 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 v || db->mallocF
1dec1 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 76 ailed );. if( v
1dec2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1dec3 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
1dec4 46 75 6e 63 74 69 6f 6e 2c 20 30 2c 20 72 65 67 Function, 0, reg
1dec5 41 72 67 73 2b 33 2d 70 46 75 6e 63 2d 3e 6e 41 Args+3-pFunc->nA
1dec6 72 67 2c 20 72 65 67 41 72 67 73 2b 33 29 3b 0a rg, regArgs+3);.
1dec7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1dec8 68 61 6e 67 65 50 35 28 76 2c 20 70 46 75 6e 63 hangeP5(v, pFunc
1dec9 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 73 71 6c ->nArg);. sql
1deca 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 ite3VdbeChangeP4
1decb 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 (v, -1, (char *)
1decc 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 pFunc, P4_FUNCDE
1decd 46 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 F);.. /* Code
1dece 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 an OP_Expire. F
1decf 6f 72 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 or an ATTACH sta
1ded0 74 65 6d 65 6e 74 2c 20 73 65 74 20 50 31 20 74 tement, set P1 t
1ded1 6f 20 74 72 75 65 20 28 65 78 70 69 72 65 20 74 o true (expire t
1ded2 68 69 73 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 his. ** state
1ded3 6d 65 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 ment only). For
1ded4 44 45 54 41 43 48 2c 20 73 65 74 20 69 74 20 74 DETACH, set it t
1ded5 6f 20 66 61 6c 73 65 20 28 65 78 70 69 72 65 20 o false (expire
1ded6 61 6c 6c 20 65 78 69 73 74 69 6e 67 0a 20 20 20 all existing.
1ded7 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e ** statements).
1ded8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
1ded9 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
1deda 20 4f 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70 OP_Expire, (typ
1dedb 65 3d 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48 e==SQLITE_ATTACH
1dedc 29 29 3b 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63 ));. }. .attac
1dedd 68 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 h_end:. sqlite3
1dede 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1dedf 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c Filename);. sql
1dee0 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
1dee1 62 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73 b, pDbname);. s
1dee2 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1dee3 28 64 62 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f (db, pKey);.}../
1dee4 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 *.** Called by t
1dee5 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d he parser to com
1dee6 70 69 6c 65 20 61 20 44 45 54 41 43 48 20 73 74 pile a DETACH st
1dee7 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 atement..**.**
1dee8 20 20 20 44 45 54 41 43 48 20 70 44 62 6e 61 6d DETACH pDbnam
1dee9 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 e.*/.SQLITE_PRIV
1deea 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1deeb 44 65 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 Detach(Parse *pP
1deec 61 72 73 65 2c 20 45 78 70 72 20 2a 70 44 62 6e arse, Expr *pDbn
1deed 61 6d 65 29 7b 0a 20 20 73 74 61 74 69 63 20 46 ame){. static F
1deee 75 6e 63 44 65 66 20 64 65 74 61 63 68 5f 66 75 uncDef detach_fu
1deef 6e 63 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 nc = {. 1,
1def0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1def1 6e 41 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 nArg */. SQLI
1def2 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 2f 2a TE_UTF8, /*
1def3 20 69 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 iPrefEnc */.
1def4 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1def5 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 /* flags */.
1def6 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
1def7 20 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61 74 /* pUserDat
1def8 61 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 a */. 0,
1def9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e /* pN
1defa 65 78 74 20 2a 2f 0a 20 20 20 20 64 65 74 61 63 ext */. detac
1defb 68 46 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 hFunc, /*
1defc 78 46 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 xFunc */. 0,
1defd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1defe 2a 20 78 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 * xStep */. 0
1deff 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1df00 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f /* xFinalize */
1df01 0a 20 20 20 20 22 73 71 6c 69 74 65 5f 64 65 74 . "sqlite_det
1df02 61 63 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 ach", /* zName
1df03 2a 2f 0a 20 20 20 20 30 20 20 20 20 20 20 20 20 */. 0
1df04 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73 /* pHas
1df05 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 h */. };. code
1df06 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 53 Attach(pParse, S
1df07 51 4c 49 54 45 5f 44 45 54 41 43 48 2c 20 26 64 QLITE_DETACH, &d
1df08 65 74 61 63 68 5f 66 75 6e 63 2c 20 70 44 62 6e etach_func, pDbn
1df09 61 6d 65 2c 20 30 2c 20 30 2c 20 70 44 62 6e 61 ame, 0, 0, pDbna
1df0a 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 me);.}../*.** Ca
1df0b 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 lled by the pars
1df0c 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e er to compile an
1df0d 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e ATTACH statemen
1df0e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 t..**.** ATT
1df0f 41 43 48 20 70 20 41 53 20 70 44 62 6e 61 6d 65 ACH p AS pDbname
1df10 20 4b 45 59 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c KEY pKey.*/.SQL
1df11 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1df12 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 sqlite3Attach(P
1df13 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
1df14 70 72 20 2a 70 2c 20 45 78 70 72 20 2a 70 44 62 pr *p, Expr *pDb
1df15 6e 61 6d 65 2c 20 45 78 70 72 20 2a 70 4b 65 79 name, Expr *pKey
1df16 29 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 ){. static Func
1df17 44 65 66 20 61 74 74 61 63 68 5f 66 75 6e 63 20 Def attach_func
1df18 3d 20 7b 0a 20 20 20 20 33 2c 20 20 20 20 20 20 = {. 3,
1df19 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 /* nAr
1df1a 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f g */. SQLITE_
1df1b 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 UTF8, /* iP
1df1c 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c refEnc */. 0,
1df1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1df1e 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 /* flags */.
1df1f 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1df20 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a /* pUserData *
1df21 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
1df22 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
1df23 20 2a 2f 0a 20 20 20 20 61 74 74 61 63 68 46 75 */. attachFu
1df24 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 nc, /* xFu
1df25 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 nc */. 0,
1df26 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1df27 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 Step */. 0,
1df28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1df29 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 xFinalize */.
1df2a 20 20 22 73 71 6c 69 74 65 5f 61 74 74 61 63 68 "sqlite_attach
1df2b 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a ", /* zName */.
1df2c 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 0
1df2d 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a /* pHash *
1df2e 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 /. };. codeAtt
1df2f 61 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 ach(pParse, SQLI
1df30 54 45 5f 41 54 54 41 43 48 2c 20 26 61 74 74 61 TE_ATTACH, &atta
1df31 63 68 5f 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70 ch_func, p, p, p
1df32 44 62 6e 61 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d Dbname, pKey);.}
1df33 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1df34 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f E_OMIT_ATTACH */
1df35 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
1df36 7a 65 20 61 20 44 62 46 69 78 65 72 20 73 74 72 ze a DbFixer str
1df37 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 72 6f ucture. This ro
1df38 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 utine must be ca
1df39 6c 6c 65 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f lled prior.** to
1df3a 20 70 61 73 73 69 6e 67 20 74 68 65 20 73 74 72 passing the str
1df3b 75 63 74 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 ucture to one of
1df3c 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 41 41 the sqliteFixAA
1df3d 41 41 28 29 20 72 6f 75 74 69 6e 65 73 20 62 65 AA() routines be
1df3e 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 low..**.** The r
1df3f 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 eturn value indi
1df40 63 61 74 65 73 20 77 68 65 74 68 65 72 20 6f 72 cates whether or
1df41 20 6e 6f 74 20 66 69 78 61 74 69 6f 6e 20 69 73 not fixation is
1df42 20 72 65 71 75 69 72 65 64 2e 20 20 54 52 55 45 required. TRUE
1df43 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 .** means we do
1df44 6e 65 65 64 20 74 6f 20 66 69 78 20 74 68 65 20 need to fix the
1df45 64 61 74 61 62 61 73 65 20 72 65 66 65 72 65 6e database referen
1df46 63 65 73 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 ces, FALSE means
1df47 20 77 65 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 we do not..*/.S
1df48 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1df49 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 t sqlite3FixInit
1df4a 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 (. DbFixer *pFi
1df4b 78 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 x, /* The f
1df4c 69 78 65 72 20 74 6f 20 62 65 20 69 6e 69 74 69 ixer to be initi
1df4d 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 alized */. Pars
1df4e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
1df4f 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 /* Error message
1df50 73 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 s will be writte
1df51 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 n here */. int
1df52 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 iDb,
1df53 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64 /* This is the d
1df54 61 74 61 62 61 73 65 20 74 68 61 74 20 6d 75 73 atabase that mus
1df55 74 20 62 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 t be used */. c
1df56 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 onst char *zType
1df57 2c 20 20 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 , /* "view", "t
1df58 72 69 67 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 rigger", or "ind
1df59 65 78 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 ex" */. const T
1df5a 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 oken *pName /*
1df5b 4e 61 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 Name of the view
1df5c 2c 20 74 72 69 67 67 65 72 2c 20 6f 72 20 69 6e , trigger, or in
1df5d 64 65 78 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 dex */.){. sqli
1df5e 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 te3 *db;.. if(
1df5f 69 44 62 3c 30 20 7c 7c 20 69 44 62 3d 3d 31 20 iDb<0 || iDb==1
1df60 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 ) return 0;. db
1df61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
1df62 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 assert( db->nDb
1df63 3e 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e >iDb );. pFix->
1df64 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b pParse = pParse;
1df65 0a 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 . pFix->zDb = d
1df66 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
1df67 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 e;. pFix->zType
1df68 20 3d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 = zType;. pFix
1df69 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b ->pName = pName;
1df6a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a . return 1;.}..
1df6b 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
1df6c 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 ing set of routi
1df6d 6e 65 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 nes walk through
1df6e 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 the parse tree
1df6f 61 6e 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 and assign.** a
1df70 73 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73 specific databas
1df71 65 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 e to all table r
1df72 65 66 65 72 65 6e 63 65 73 20 77 68 65 72 65 20 eferences where
1df73 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
1df74 65 0a 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e e.** was left un
1df75 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 specified in the
1df76 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 original SQL st
1df77 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 atement. The pF
1df78 69 78 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 ix structure.**
1df79 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 69 must have been i
1df7a 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20 nitialized by a
1df7b 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 prior call to sq
1df7c 6c 69 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a lite3FixInit()..
1df7d 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
1df7e 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f ines are used to
1df7f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
1df80 61 6e 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65 an index, trigge
1df81 72 2c 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e r, or.** view in
1df82 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f one database do
1df83 65 73 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 es not refer to
1df84 6f 62 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66 objects in a dif
1df85 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e ferent database.
1df86 0a 2a 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 .** (Exception:
1df87 69 6e 64 69 63 65 73 2c 20 74 72 69 67 67 65 72 indices, trigger
1df88 73 2c 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20 s, and views in
1df89 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 the TEMP databas
1df8a 65 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 e are.** allowed
1df8b 20 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 to refer to any
1df8c 74 68 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65 thing.) If a re
1df8d 66 65 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69 ference is expli
1df8e 63 69 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f citly made.** to
1df8f 20 61 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 an object in a
1df90 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 different databa
1df91 73 65 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 se, an error mes
1df92 73 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f sage is added to
1df93 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 .** pParse->zErr
1df94 4d 73 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f Msg and these ro
1df95 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f utines return no
1df96 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72 n-zero. If ever
1df97 79 74 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 ything.** checks
1df98 20 6f 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74 out, these rout
1df99 69 6e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a ines return 0..*
1df9a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1df9b 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 int sqlite3FixS
1df9c 72 63 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 rcList(. DbFixe
1df9d 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f r *pFix, /
1df9e 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 * Context of the
1df9f 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 fixation */. S
1dfa0 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20 rcList *pList
1dfa1 20 20 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 /* The Sourc
1dfa2 65 20 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20 e list to check
1dfa3 61 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b and modify */.){
1dfa4 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 . int i;. cons
1dfa5 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 t char *zDb;. s
1dfa6 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
1dfa7 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 em *pItem;.. if
1dfa8 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 ( pList==0 ) ret
1dfa9 75 72 6e 20 30 3b 0a 20 20 7a 44 62 20 3d 20 70 urn 0;. zDb = p
1dfaa 46 69 78 2d 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 Fix->zDb;. for(
1dfab 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 i=0, pItem=pList
1dfac 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 ->a; i<pList->nS
1dfad 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b rc; i++, pItem++
1dfae 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d ){. if( pItem
1dfaf 2d 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30 20 29 ->zDatabase==0 )
1dfb0 7b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a {. pItem->z
1dfb1 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 Database = sqlit
1dfb2 65 33 44 62 53 74 72 44 75 70 28 70 46 69 78 2d e3DbStrDup(pFix-
1dfb3 3e 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 44 62 >pParse->db, zDb
1dfb4 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
1dfb5 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1dfb6 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 pItem->zDatabase
1dfb7 2c 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 ,zDb)!=0 ){.
1dfb8 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1dfb9 67 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2c 0a g(pFix->pParse,.
1dfba 20 20 20 20 20 20 20 20 20 22 25 73 20 25 54 20 "%s %T
1dfbb 63 61 6e 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 cannot reference
1dfbc 20 6f 62 6a 65 63 74 73 20 69 6e 20 64 61 74 61 objects in data
1dfbd 62 61 73 65 20 25 73 22 2c 0a 20 20 20 20 20 20 base %s",.
1dfbe 20 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 2c 20 pFix->zType,
1dfbf 70 46 69 78 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 pFix->pName, pIt
1dfc0 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a em->zDatabase);.
1dfc1 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
1dfc2 20 20 20 20 7d 0a 23 69 66 20 21 64 65 66 69 6e }.#if !defin
1dfc3 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
1dfc4 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 IEW) || !defined
1dfc5 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 (SQLITE_OMIT_TRI
1dfc6 47 47 45 52 29 0a 20 20 20 20 69 66 28 20 73 71 GGER). if( sq
1dfc7 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 lite3FixSelect(p
1dfc8 46 69 78 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c Fix, pItem->pSel
1dfc9 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b ect) ) return 1;
1dfca 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1dfcb 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 FixExpr(pFix, pI
1dfcc 74 65 6d 2d 3e 70 4f 6e 29 20 29 20 72 65 74 75 tem->pOn) ) retu
1dfcd 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 7d rn 1;.#endif. }
1dfce 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 . return 0;.}.#
1dfcf 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
1dfd0 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c TE_OMIT_VIEW) ||
1dfd1 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1dfd2 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53 _OMIT_TRIGGER).S
1dfd3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1dfd4 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 t sqlite3FixSele
1dfd5 63 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 ct(. DbFixer *p
1dfd6 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f Fix, /* Co
1dfd7 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 ntext of the fix
1dfd8 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 ation */. Selec
1dfd9 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 t *pSelect
1dfda 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
1dfdb 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 66 69 atement to be fi
1dfdc 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 xed to one datab
1dfdd 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c ase */.){. whil
1dfde 65 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 e( pSelect ){.
1dfdf 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 if( sqlite3Fix
1dfe0 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 ExprList(pFix, p
1dfe1 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 29 20 Select->pEList)
1dfe2 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1dfe3 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
1dfe4 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 sqlite3FixSrcLi
1dfe5 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 st(pFix, pSelect
1dfe6 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20 20 ->pSrc) ){.
1dfe7 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
1dfe8 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1dfe9 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 FixExpr(pFix, pS
1dfea 65 6c 65 63 74 2d 3e 70 57 68 65 72 65 29 20 29 elect->pWhere) )
1dfeb 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
1dfec 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1dfed 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 sqlite3FixExpr(p
1dfee 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 48 Fix, pSelect->pH
1dfef 61 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 20 20 aving) ){.
1dff0 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
1dff1 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 pSelect = pS
1dff2 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 elect->pPrior;.
1dff3 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
1dff4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1dff5 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 int sqlite3FixEx
1dff6 70 72 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 pr(. DbFixer *p
1dff7 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 Fix, /* Cont
1dff8 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 ext of the fixat
1dff9 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ion */. Expr *p
1dffa 45 78 70 72 20 20 20 20 20 20 20 20 2f 2a 20 54 Expr /* T
1dffb 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f he expression to
1dffc 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 be fixed to one
1dffd 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a database */.){.
1dffe 20 20 77 68 69 6c 65 28 20 70 45 78 70 72 20 29 while( pExpr )
1dfff 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
1e000 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 3FixSelect(pFix,
1e001 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 29 pExpr->pSelect)
1e002 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1e003 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 1;. }. if
1e004 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 ( sqlite3FixExpr
1e005 4c 69 73 74 28 70 46 69 78 2c 20 70 45 78 70 72 List(pFix, pExpr
1e006 2d 3e 70 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 ->pList) ){.
1e007 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
1e008 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1e009 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 3FixExpr(pFix, p
1e00a 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20 29 7b Expr->pRight) ){
1e00b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
1e00c 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72 . }. pExpr
1e00d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b = pExpr->pLeft;
1e00e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
1e00f 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
1e010 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 E int sqlite3Fix
1e011 45 78 70 72 4c 69 73 74 28 0a 20 20 44 62 46 69 ExprList(. DbFi
1e012 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f xer *pFix, /
1e013 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 * Context of the
1e014 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 fixation */. E
1e015 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 xprList *pList
1e016 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 /* The express
1e017 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 ion to be fixed
1e018 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 to one database
1e019 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 */.){. int i;.
1e01a 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
1e01b 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
1e01c 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 if( pList==0 ) r
1e01d 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 eturn 0;. for(i
1e01e 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d =0, pItem=pList-
1e01f 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 >a; i<pList->nEx
1e020 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b pr; i++, pItem++
1e021 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
1e022 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 e3FixExpr(pFix,
1e023 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 7b pItem->pExpr) ){
1e024 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
1e025 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1e026 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn 0;.}.#endif.
1e027 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1e028 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c OMIT_TRIGGER.SQL
1e029 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1e02a 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65 sqlite3FixTrigge
1e02b 72 53 74 65 70 28 0a 20 20 44 62 46 69 78 65 72 rStep(. DbFixer
1e02c 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 *pFix, /* C
1e02d 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 ontext of the fi
1e02e 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 xation */. Trig
1e02f 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 2f gerStep *pStep /
1e030 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 73 74 * The trigger st
1e031 65 70 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f ep be fixed to o
1e032 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 ne database */.)
1e033 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70 {. while( pStep
1e034 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
1e035 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 te3FixSelect(pFi
1e036 78 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 x, pStep->pSelec
1e037 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 t) ){. retu
1e038 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
1e039 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 if( sqlite3FixEx
1e03a 70 72 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e pr(pFix, pStep->
1e03b 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 20 pWhere) ){.
1e03c 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
1e03d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1e03e 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 78 FixExprList(pFix
1e03f 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 , pStep->pExprLi
1e040 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 st) ){. ret
1e041 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
1e042 20 70 53 74 65 70 20 3d 20 70 53 74 65 70 2d 3e pStep = pStep->
1e043 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 pNext;. }. ret
1e044 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn 0;.}.#endif.
1e045 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1e046 20 45 6e 64 20 6f 66 20 61 74 74 61 63 68 2e 63 End of attach.c
1e047 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
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 2f ***************/
1e04a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1e04b 20 42 65 67 69 6e 20 66 69 6c 65 20 61 75 74 68 Begin file auth
1e04c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .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 2f ***************/
1e04f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61 6e 75 ./*.** 2003 Janu
1e050 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 ary 11.**.** 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 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
1e069 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 nt the sqlite3_s
1e06a 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 0a et_authorizer().
1e06b 2a 2a 20 41 50 49 2e 20 20 54 68 69 73 20 66 61 ** API. This fa
1e06c 63 69 6c 69 74 79 20 69 73 20 61 6e 20 6f 70 74 cility is an opt
1e06d 69 6f 6e 61 6c 20 66 65 61 74 75 72 65 20 6f 66 ional feature of
1e06e 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 45 the library. E
1e06f 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79 73 74 65 mbedded.** syste
1e070 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6e ms that do not n
1e071 65 65 64 20 74 68 69 73 20 66 61 63 69 6c 69 74 eed this facilit
1e072 79 20 6d 61 79 20 6f 6d 69 74 20 69 74 20 62 79 y may omit it by
1e073 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 recompiling.**
1e074 74 68 65 20 6c 69 62 72 61 72 79 20 77 69 74 68 the library with
1e075 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 -DSQLITE_OMIT_A
1e076 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a UTHORIZATION=1.*
1e077 2a 0a 2a 2a 20 24 49 64 3a 20 61 75 74 68 2e 63 *.** $Id: auth.c
1e078 2c 76 20 31 2e 32 39 20 32 30 30 37 2f 30 39 2f ,v 1.29 2007/09/
1e079 31 38 20 31 35 3a 35 35 3a 30 37 20 64 72 68 20 18 15:55:07 drh
1e07a 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
1e07b 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20 All of the code
1e07c 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61 79 in this file may
1e07d 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20 64 be omitted by d
1e07e 65 66 69 6e 69 6e 67 20 61 20 73 69 6e 67 6c 65 efining a single
1e07f 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 .** macro..*/.#i
1e080 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1e081 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a T_AUTHORIZATION.
1e082 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 63 6c ./*.** Set or cl
1e083 65 61 72 20 74 68 65 20 61 63 63 65 73 73 20 61 ear the access a
1e084 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e uthorization fun
1e085 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
1e086 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a access authoriz
1e087 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 69 ation function i
1e088 73 20 62 65 20 63 61 6c 6c 65 64 20 64 75 72 69 s be called duri
1e089 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 ng the compilati
1e08a 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 74 6f 20 76 on.** phase to v
1e08b 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 75 erify that the u
1e08c 73 65 72 20 68 61 73 20 72 65 61 64 20 61 6e 64 ser has read and
1e08d 2f 6f 72 20 77 72 69 74 65 20 61 63 63 65 73 73 /or write access
1e08e 20 70 65 72 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a permission on.*
1e08f 2a 20 76 61 72 69 6f 75 73 20 66 69 65 6c 64 73 * various fields
1e090 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1e091 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 . The first arg
1e092 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 75 74 ument to the aut
1e093 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 h function.** is
1e094 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 33 a copy of the 3
1e095 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 rd argument to t
1e096 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 his routine. Th
1e097 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
1e098 74 0a 2a 2a 20 74 6f 20 74 68 65 20 61 75 74 68 t.** to the auth
1e099 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 65 function is one
1e09a 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 73 74 61 of these consta
1e09b 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 nts:.**.**
1e09c 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 SQLITE_CREATE_I
1e09d 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 NDEX.** SQ
1e09e 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c LITE_CREATE_TABL
1e09f 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 E.** SQLIT
1e0a0 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e E_CREATE_TEMP_IN
1e0a1 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c DEX.** SQL
1e0a2 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
1e0a3 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 TABLE.** S
1e0a4 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d QLITE_CREATE_TEM
1e0a5 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 P_TRIGGER.**
1e0a6 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 SQLITE_CREATE
1e0a7 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20 _TEMP_VIEW.**
1e0a8 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 SQLITE_CREAT
1e0a9 45 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 E_TRIGGER.**
1e0aa 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 SQLITE_CREATE
1e0ab 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 _VIEW.** S
1e0ac 51 4c 49 54 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 QLITE_DELETE.**
1e0ad 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f SQLITE_DRO
1e0ae 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 P_INDEX.**
1e0af 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 SQLITE_DROP_TAB
1e0b0 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 LE.** SQLI
1e0b1 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 TE_DROP_TEMP_IND
1e0b2 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 EX.** SQLI
1e0b3 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 TE_DROP_TEMP_TAB
1e0b4 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 LE.** SQLI
1e0b5 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 TE_DROP_TEMP_TRI
1e0b6 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 GGER.** SQ
1e0b7 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 LITE_DROP_TEMP_V
1e0b8 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c IEW.** SQL
1e0b9 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 ITE_DROP_TRIGGER
1e0ba 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
1e0bb 5f 44 52 4f 50 5f 56 49 45 57 0a 2a 2a 20 20 20 _DROP_VIEW.**
1e0bc 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 SQLITE_INSER
1e0bd 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 T.** SQLIT
1e0be 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 20 20 20 20 E_PRAGMA.**
1e0bf 20 20 53 51 4c 49 54 45 5f 52 45 41 44 0a 2a 2a SQLITE_READ.**
1e0c0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 45 SQLITE_SE
1e0c1 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 LECT.** SQ
1e0c2 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e LITE_TRANSACTION
1e0c3 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
1e0c4 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68 _UPDATE.**.** Th
1e0c5 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 e third and four
1e0c6 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 th arguments to
1e0c7 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f the auth functio
1e0c8 6e 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f n are the name o
1e0c9 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 61 f.** the table a
1e0ca 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 nd the column th
1e0cb 61 74 20 61 72 65 20 62 65 69 6e 67 20 61 63 63 at are being acc
1e0cc 65 73 73 65 64 2e 20 20 54 68 65 20 61 75 74 68 essed. The auth
1e0cd 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f function.** sho
1e0ce 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68 65 uld return eithe
1e0cf 72 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c r SQLITE_OK, SQL
1e0d0 49 54 45 5f 44 45 4e 59 2c 20 6f 72 20 53 51 4c ITE_DENY, or SQL
1e0d1 49 54 45 5f 49 47 4e 4f 52 45 2e 20 20 49 66 0a ITE_IGNORE. If.
1e0d2 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
1e0d3 72 65 74 75 72 6e 65 64 2c 20 69 74 20 6d 65 61 returned, it mea
1e0d4 6e 73 20 74 68 61 74 20 61 63 63 65 73 73 20 69 ns that access i
1e0d5 73 20 61 6c 6c 6f 77 65 64 2e 20 20 53 51 4c 49 s allowed. SQLI
1e0d6 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73 TE_DENY.** means
1e0d7 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 that the SQL st
1e0d8 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 6e 65 76 atement will nev
1e0d9 65 72 2d 72 75 6e 20 2d 20 74 68 65 20 73 71 6c er-run - the sql
1e0da 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c ite3_exec() call
1e0db 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 .** will return
1e0dc 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 with an error.
1e0dd 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6d 65 SQLITE_IGNORE me
1e0de 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 4c ans that the SQL
1e0df 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73 68 statement.** sh
1e0e0 6f 75 6c 64 20 72 75 6e 20 62 75 74 20 61 74 74 ould run but att
1e0e1 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 74 68 empts to read th
1e0e2 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 e specified colu
1e0e3 6d 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e mn will return N
1e0e4 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74 74 65 6d ULL.** and attem
1e0e5 70 74 73 20 74 6f 20 77 72 69 74 65 20 74 68 65 pts to write the
1e0e6 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62 65 20 column will be
1e0e7 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 ignored..**.** S
1e0e8 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 68 20 etting the auth
1e0e9 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c function to NULL
1e0ea 20 64 69 73 61 62 6c 65 73 20 74 68 69 73 20 68 disables this h
1e0eb 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66 61 75 6c ook. The defaul
1e0ec 74 0a 2a 2a 20 73 65 74 74 69 6e 67 20 6f 66 20 t.** setting of
1e0ed 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f the auth functio
1e0ee 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 n is NULL..*/.SQ
1e0ef 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1e0f0 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
1e0f1 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a zer(. sqlite3 *
1e0f2 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 db,. int (*xAut
1e0f3 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e h)(void*,int,con
1e0f4 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
1e0f5 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
1e0f6 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 0a 20 ,const char*),.
1e0f7 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 void *pArg.){.
1e0f8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1e0f9 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b nter(db->mutex);
1e0fa 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 . db->xAuth = x
1e0fb 41 75 74 68 3b 0a 20 20 64 62 2d 3e 70 41 75 74 Auth;. db->pAut
1e0fc 68 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 hArg = pArg;. s
1e0fd 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 qlite3ExpirePrep
1e0fe 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 aredStatements(d
1e0ff 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 b);. sqlite3_mu
1e100 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 tex_leave(db->mu
1e101 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 tex);. return S
1e102 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1e103 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 72 72 6f ** Write an erro
1e104 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70 r message into p
1e105 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 74 Parse->zErrMsg t
1e106 68 61 74 20 65 78 70 6c 61 69 6e 73 20 74 68 61 hat explains tha
1e107 74 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 73 75 t the.** user-su
1e108 70 70 6c 69 65 64 20 61 75 74 68 6f 72 69 7a 61 pplied authoriza
1e109 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 72 65 tion function re
1e10a 74 75 72 6e 65 64 20 61 6e 20 69 6c 6c 65 67 61 turned an illega
1e10b 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 l value..*/.stat
1e10c 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 41 75 ic void sqliteAu
1e10d 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 thBadReturnCode(
1e10e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
1e10f 6e 74 20 72 63 29 7b 0a 20 20 73 71 6c 69 74 65 nt rc){. sqlite
1e110 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1e111 2c 20 22 69 6c 6c 65 67 61 6c 20 72 65 74 75 72 , "illegal retur
1e112 6e 20 76 61 6c 75 65 20 28 25 64 29 20 66 72 6f n value (%d) fro
1e113 6d 20 74 68 65 20 22 0a 20 20 20 20 22 61 75 74 m the ". "aut
1e114 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 horization funct
1e115 69 6f 6e 20 2d 20 73 68 6f 75 6c 64 20 62 65 20 ion - should be
1e116 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 SQLITE_OK, SQLIT
1e117 45 5f 49 47 4e 4f 52 45 2c 20 22 0a 20 20 20 20 E_IGNORE, ".
1e118 22 6f 72 20 53 51 4c 49 54 45 5f 44 45 4e 59 22 "or SQLITE_DENY"
1e119 2c 20 72 63 29 3b 0a 20 20 70 50 61 72 73 65 2d , rc);. pParse-
1e11a 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 >rc = SQLITE_ERR
1e11b 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 OR;.}../*.** The
1e11c 20 70 45 78 70 72 20 73 68 6f 75 6c 64 20 62 65 pExpr should be
1e11d 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 a TK_COLUMN exp
1e11e 72 65 73 73 69 6f 6e 2e 20 20 54 68 65 20 74 61 ression. The ta
1e11f 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 0a ble referred to.
1e120 2a 2a 20 69 73 20 69 6e 20 70 54 61 62 4c 69 73 ** is in pTabLis
1e121 74 20 6f 72 20 65 6c 73 65 20 69 74 20 69 73 20 t or else it is
1e122 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 the NEW or OLD t
1e123 61 62 6c 65 20 6f 66 20 61 20 74 72 69 67 67 65 able of a trigge
1e124 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b 20 74 6f r. .** Check to
1e125 20 73 65 65 20 69 66 20 69 74 20 69 73 20 4f 4b see if it is OK
1e126 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 to read this pa
1e127 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 2e rticular column.
1e128 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 75 .**.** If the au
1e129 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 th function retu
1e12a 72 6e 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 rns SQLITE_IGNOR
1e12b 45 2c 20 63 68 61 6e 67 65 20 74 68 65 20 54 4b E, change the TK
1e12c 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 6e 73 74 _COLUMN .** inst
1e12d 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20 61 20 54 ruction into a T
1e12e 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 K_NULL. If the
1e12f 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65 auth function re
1e130 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 45 4e turns SQLITE_DEN
1e131 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65 6e 65 72 Y,.** then gener
1e132 61 74 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f ate an error..*/
1e133 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1e134 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 void sqlite3Auth
1e135 52 65 61 64 28 0a 20 20 50 61 72 73 65 20 2a 70 Read(. Parse *p
1e136 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Parse, /*
1e137 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 The parser cont
1e138 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ext */. Expr *p
1e139 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f Expr, /
1e13a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
1e13b 20 74 6f 20 63 68 65 63 6b 20 61 75 74 68 6f 72 to check author
1e13c 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0a 20 20 ization on */.
1e13d 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 2c Schema *pSchema,
1e13e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 63 68 /* The sch
1e13f 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 65 ema of the expre
1e140 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 ssion */. SrcLi
1e141 73 74 20 2a 70 54 61 62 4c 69 73 74 20 20 20 20 st *pTabList
1e142 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 20 74 68 /* All table th
1e143 61 74 20 70 45 78 70 72 20 6d 69 67 68 74 20 72 at pExpr might r
1e144 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 efer to */.){.
1e145 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1e146 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 arse->db;. int
1e147 72 63 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 rc;. Table *pTa
1e148 62 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 b = 0; /* T
1e149 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 he table being r
1e14a 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ead */. const c
1e14b 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f har *zCol; /
1e14c 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f * Name of the co
1e14d 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c lumn of the tabl
1e14e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 3b e */. int iSrc;
1e14f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e150 49 6e 64 65 78 20 69 6e 20 70 54 61 62 4c 69 73 Index in pTabLis
1e151 74 2d 3e 61 5b 5d 20 6f 66 20 74 61 62 6c 65 20 t->a[] of table
1e152 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 being read */.
1e153 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 42 61 const char *zDBa
1e154 73 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 se; /* Name of
1e155 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 database being
1e156 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 54 72 accessed */. Tr
1e157 69 67 67 65 72 53 74 61 63 6b 20 2a 70 53 74 61 iggerStack *pSta
1e158 63 6b 3b 20 2f 2a 20 54 68 65 20 73 74 61 63 6b ck; /* The stack
1e159 20 6f 66 20 63 75 72 72 65 6e 74 20 74 72 69 67 of current trig
1e15a 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 gers */. int iD
1e15b 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
1e15c 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 /* The index of
1e15d 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 the database the
1e15e 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65 expression refe
1e15f 72 73 20 74 6f 20 2a 2f 0a 0a 20 20 69 66 28 20 rs to */.. if(
1e160 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29 20 72 db->xAuth==0 ) r
1e161 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 eturn;. if( pEx
1e162 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d pr->op!=TK_COLUM
1e163 4e 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 44 N ) return;. iD
1e164 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
1e165 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d aToIndex(pParse-
1e166 3e 64 62 2c 20 70 53 63 68 65 6d 61 29 3b 0a 20 >db, pSchema);.
1e167 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 if( iDb<0 ){.
1e168 20 20 2f 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 /* An attempt
1e169 74 6f 20 72 65 61 64 20 61 20 63 6f 6c 75 6d 6e to read a column
1e16a 20 6f 75 74 20 6f 66 20 61 20 73 75 62 71 75 65 out of a subque
1e16b 72 79 20 6f 72 20 6f 74 68 65 72 0a 20 20 20 20 ry or other.
1e16c 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 ** temporary tab
1e16d 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 le. */. retur
1e16e 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 53 72 n;. }. for(iSr
1e16f 63 3d 30 3b 20 70 54 61 62 4c 69 73 74 20 26 26 c=0; pTabList &&
1e170 20 69 53 72 63 3c 70 54 61 62 4c 69 73 74 2d 3e iSrc<pTabList->
1e171 6e 53 72 63 3b 20 69 53 72 63 2b 2b 29 7b 0a 20 nSrc; iSrc++){.
1e172 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 if( pExpr->iT
1e173 61 62 6c 65 3d 3d 70 54 61 62 4c 69 73 74 2d 3e able==pTabList->
1e174 61 5b 69 53 72 63 5d 2e 69 43 75 72 73 6f 72 20 a[iSrc].iCursor
1e175 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 ) break;. }. i
1e176 66 28 20 69 53 72 63 3e 3d 30 20 26 26 20 70 54 f( iSrc>=0 && pT
1e177 61 62 4c 69 73 74 20 26 26 20 69 53 72 63 3c 70 abList && iSrc<p
1e178 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 7b TabList->nSrc ){
1e179 0a 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 . pTab = pTab
1e17a 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 70 54 List->a[iSrc].pT
1e17b 61 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ab;. }else if(
1e17c 28 70 53 74 61 63 6b 20 3d 20 70 50 61 72 73 65 (pStack = pParse
1e17d 2d 3e 74 72 69 67 53 74 61 63 6b 29 21 3d 30 20 ->trigStack)!=0
1e17e 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 6d ){. /* This m
1e17f 75 73 74 20 62 65 20 61 6e 20 61 74 74 65 6d 70 ust be an attemp
1e180 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 4e 45 t to read the NE
1e181 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d W or OLD pseudo-
1e182 74 61 62 6c 65 73 0a 20 20 20 20 2a 2a 20 6f 66 tables. ** of
1e183 20 61 20 74 72 69 67 67 65 72 2e 0a 20 20 20 20 a trigger..
1e184 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
1e185 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 53 Expr->iTable==pS
1e186 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 7c 7c 20 tack->newIdx ||
1e187 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 pExpr->iTable==p
1e188 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 29 3b Stack->oldIdx );
1e189 0a 20 20 20 20 70 54 61 62 20 3d 20 70 53 74 61 . pTab = pSta
1e18a 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 7d 0a 20 20 ck->pTab;. }.
1e18b 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65 if( pTab==0 ) re
1e18c 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 turn;. if( pExp
1e18d 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b r->iColumn>=0 ){
1e18e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
1e18f 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 pr->iColumn<pTab
1e190 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 ->nCol );. zC
1e191 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b ol = pTab->aCol[
1e192 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e pExpr->iColumn].
1e193 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 20 69 zName;. }else i
1e194 66 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3e 3d f( pTab->iPKey>=
1e195 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
1e196 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 70 54 61 pTab->iPKey<pTa
1e197 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 7a b->nCol );. z
1e198 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c Col = pTab->aCol
1e199 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e [pTab->iPKey].zN
1e19a 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ame;. }else{.
1e19b 20 20 7a 43 6f 6c 20 3d 20 22 52 4f 57 49 44 22 zCol = "ROWID"
1e19c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1e19d 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 iDb>=0 && iDb<db
1e19e 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 42 61 73 ->nDb );. zDBas
1e19f 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d e = db->aDb[iDb]
1e1a0 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 20 3d 20 64 .zName;. rc = d
1e1a1 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70 41 75 b->xAuth(db->pAu
1e1a2 74 68 41 72 67 2c 20 53 51 4c 49 54 45 5f 52 45 thArg, SQLITE_RE
1e1a3 41 44 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c AD, pTab->zName,
1e1a4 20 7a 43 6f 6c 2c 20 7a 44 42 61 73 65 2c 20 0a zCol, zDBase, .
1e1a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e1a6 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f pParse->zAuthCo
1e1a7 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63 ntext);. if( rc
1e1a8 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 ==SQLITE_IGNORE
1e1a9 29 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 ){. pExpr->op
1e1aa 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 = TK_NULL;. }e
1e1ab 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 lse if( rc==SQLI
1e1ac 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 69 TE_DENY ){. i
1e1ad 66 28 20 64 62 2d 3e 6e 44 62 3e 32 20 7c 7c 20 f( db->nDb>2 ||
1e1ae 69 44 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 iDb!=0 ){.
1e1af 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1e1b0 70 50 61 72 73 65 2c 20 22 61 63 63 65 73 73 20 pParse, "access
1e1b1 74 6f 20 25 73 2e 25 73 2e 25 73 20 69 73 20 70 to %s.%s.%s is p
1e1b2 72 6f 68 69 62 69 74 65 64 22 2c 20 0a 20 20 20 rohibited", .
1e1b3 20 20 20 20 20 20 7a 44 42 61 73 65 2c 20 70 54 zDBase, pT
1e1b4 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 ab->zName, zCol)
1e1b5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1e1b6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1e1b7 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63 65 sg(pParse, "acce
1e1b8 73 73 20 74 6f 20 25 73 2e 25 73 20 69 73 20 70 ss to %s.%s is p
1e1b9 72 6f 68 69 62 69 74 65 64 22 2c 70 54 61 62 2d rohibited",pTab-
1e1ba 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29 3b 0a 20 20 >zName,zCol);.
1e1bb 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e }. pParse->
1e1bc 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 rc = SQLITE_AUTH
1e1bd 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 ;. }else if( rc
1e1be 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1e1bf 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 sqliteAuthBad
1e1c0 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73 ReturnCode(pPars
1e1c1 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f e, rc);. }.}../
1e1c2 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68 6f *.** Do an autho
1e1c3 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20 75 rization check u
1e1c4 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20 61 6e sing the code an
1e1c5 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69 76 65 d arguments give
1e1c6 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 65 69 n. Return.** ei
1e1c7 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28 ther SQLITE_OK (
1e1c8 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54 45 5f zero) or SQLITE_
1e1c9 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54 45 IGNORE or SQLITE
1e1ca 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c 49 54 _DENY. If SQLIT
1e1cb 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72 65 74 E_DENY.** is ret
1e1cc 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 urned, then the
1e1cd 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64 20 error count and
1e1ce 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
1e1cf 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a 20 6d pParse are.** m
1e1d0 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70 72 69 odified appropri
1e1d1 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ately..*/.SQLITE
1e1d2 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1e1d3 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 0a 20 ite3AuthCheck(.
1e1d4 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a Parse *pParse,.
1e1d5 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20 63 6f int code,. co
1e1d6 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 31 2c nst char *zArg1,
1e1d7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1e1d8 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20 63 68 Arg2,. const ch
1e1d9 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20 20 73 ar *zArg3.){. s
1e1da 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1e1db 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 rse->db;. int r
1e1dc 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 64 c;.. /* Don't d
1e1dd 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a 61 74 o any authorizat
1e1de 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74 68 ion checks if th
1e1df 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e e database is in
1e1e0 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a 2a 20 itialising. **
1e1e1 6f 72 20 69 66 20 74 68 65 20 70 61 72 73 65 72 or if the parser
1e1e2 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f 6b 65 is being invoke
1e1e3 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 73 71 d from within sq
1e1e4 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 lite3_declare_vt
1e1e5 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 ab.. */. if( d
1e1e6 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 b->init.busy ||
1e1e7 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
1e1e8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
1e1e9 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
1e1ea 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30 if( db->xAuth==0
1e1eb 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1e1ec 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
1e1ed 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 rc = db->xAuth(d
1e1ee 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 63 6f 64 b->pAuthArg, cod
1e1ef 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 67 32 2c e, zArg1, zArg2,
1e1f0 20 7a 41 72 67 33 2c 20 70 50 61 72 73 65 2d 3e zArg3, pParse->
1e1f1 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20 zAuthContext);.
1e1f2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1e1f3 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 71 6c 69 DENY ){. sqli
1e1f4 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1e1f5 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 69 se, "not authori
1e1f6 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 61 72 73 zed");. pPars
1e1f7 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 e->rc = SQLITE_A
1e1f8 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 UTH;. }else if(
1e1f9 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc!=SQLITE_OK &
1e1fa 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 47 4e & rc!=SQLITE_IGN
1e1fb 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ORE ){. rc =
1e1fc 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20 20 20 SQLITE_DENY;.
1e1fd 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 sqliteAuthBadRe
1e1fe 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73 65 2c turnCode(pParse,
1e1ff 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rc);. }. retu
1e200 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1e201 50 75 73 68 20 61 6e 20 61 75 74 68 6f 72 69 7a Push an authoriz
1e202 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 2e 20 20 ation context.
1e203 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 After this routi
1e204 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 ne is called, th
1e205 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72 67 75 6d e.** zArg3 argum
1e206 65 6e 74 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 ent to authoriza
1e207 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 20 77 tion callbacks w
1e208 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65 78 74 20 ill be zContext
1e209 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e until.** popped.
1e20a 20 20 4f 72 20 69 66 20 70 50 61 72 73 65 3d 3d Or if pParse==
1e20b 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 0, this routine
1e20c 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 is a no-op..*/.S
1e20d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1e20e 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f id sqlite3AuthCo
1e20f 6e 74 65 78 74 50 75 73 68 28 0a 20 20 50 61 72 ntextPush(. Par
1e210 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 41 75 se *pParse,. Au
1e211 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 thContext *pCont
1e212 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 ext, . const ch
1e213 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a ar *zContext.){.
1e214 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 pContext->pPar
1e215 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 69 se = pParse;. i
1e216 66 28 20 70 50 61 72 73 65 20 29 7b 0a 20 20 20 f( pParse ){.
1e217 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 pContext->zAuth
1e218 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 Context = pParse
1e219 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a ->zAuthContext;.
1e21a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 pParse->zAut
1e21b 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43 6f 6e 74 hContext = zCont
1e21c 65 78 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ext;. }.}../*.*
1e21d 2a 20 50 6f 70 20 61 6e 20 61 75 74 68 6f 72 69 * Pop an authori
1e21e 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 74 zation context t
1e21f 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 hat was previous
1e220 6c 79 20 70 75 73 68 65 64 0a 2a 2a 20 62 79 20 ly pushed.** by
1e221 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 sqlite3AuthConte
1e222 78 74 50 75 73 68 0a 2a 2f 0a 53 51 4c 49 54 45 xtPush.*/.SQLITE
1e223 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1e224 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 lite3AuthContext
1e225 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 20 Pop(AuthContext
1e226 2a 70 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 *pContext){. if
1e227 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 ( pContext->pPar
1e228 73 65 20 29 7b 0a 20 20 20 20 70 43 6f 6e 74 65 se ){. pConte
1e229 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a 41 75 74 xt->pParse->zAut
1e22a 68 43 6f 6e 74 65 78 74 20 3d 20 70 43 6f 6e 74 hContext = pCont
1e22b 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 ext->zAuthContex
1e22c 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d t;. pContext-
1e22d 3e 70 50 61 72 73 65 20 3d 20 30 3b 0a 20 20 7d >pParse = 0;. }
1e22e 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
1e22f 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
1e230 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a IZATION */../***
1e231 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1e232 6f 66 20 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a of auth.c ******
1e233 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e234 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e235 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1e236 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1e237 6e 20 66 69 6c 65 20 62 75 69 6c 64 2e 63 20 2a n file build.c *
1e238 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e239 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e23a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1e23b 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
1e23c 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
1e23d 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
1e23e 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
1e23f 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
1e240 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
1e241 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
1e242 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
1e243 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
1e244 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
1e245 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
1e246 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
1e247 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
1e248 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
1e249 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
1e24a 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
1e24b 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
1e24c 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
1e24d 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
1e24e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e24f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e251 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e252 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
1e253 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 contains C code
1e254 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 routines that ar
1e255 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 e called by the
1e256 53 51 4c 69 74 65 20 70 61 72 73 65 72 0a 2a 2a SQLite parser.**
1e257 20 77 68 65 6e 20 73 79 6e 74 61 78 20 72 75 6c when syntax rul
1e258 65 73 20 61 72 65 20 72 65 64 75 63 65 64 2e 20 es are reduced.
1e259 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e The routines in
1e25a 20 74 68 69 73 20 66 69 6c 65 20 68 61 6e 64 6c this file handl
1e25b 65 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 e the.** followi
1e25c 6e 67 20 6b 69 6e 64 73 20 6f 66 20 53 51 4c 20 ng kinds of SQL
1e25d 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 syntax:.**.**
1e25e 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a 2a CREATE TABLE.*
1e25f 2a 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 * DROP TABLE
1e260 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 .** CREATE I
1e261 4e 44 45 58 0a 2a 2a 20 20 20 20 20 44 52 4f 50 NDEX.** DROP
1e262 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 63 72 INDEX.** cr
1e263 65 61 74 69 6e 67 20 49 44 20 6c 69 73 74 73 0a eating ID lists.
1e264 2a 2a 20 20 20 20 20 42 45 47 49 4e 20 54 52 41 ** BEGIN TRA
1e265 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 NSACTION.**
1e266 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 20 20 52 4f COMMIT.** RO
1e267 4c 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 LLBACK.**.** $Id
1e268 3a 20 62 75 69 6c 64 2e 63 2c 76 20 31 2e 35 30 : build.c,v 1.50
1e269 30 20 32 30 30 38 2f 31 31 2f 30 33 20 32 30 3a 0 2008/11/03 20:
1e26a 35 35 3a 30 37 20 64 72 68 20 45 78 70 20 24 0a 55:07 drh Exp $.
1e26b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 */../*.** This r
1e26c 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1e26d 20 77 68 65 6e 20 61 20 6e 65 77 20 53 51 4c 20 when a new SQL
1e26e 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65 67 statement is beg
1e26f 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 inning to.** be
1e270 70 61 72 73 65 64 2e 20 20 49 6e 69 74 69 61 6c parsed. Initial
1e271 69 7a 65 20 74 68 65 20 70 50 61 72 73 65 20 73 ize the pParse s
1e272 74 72 75 63 74 75 72 65 20 61 73 20 6e 65 65 64 tructure as need
1e273 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1e274 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1e275 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 61 72 e3BeginParse(Par
1e276 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1e277 65 78 70 6c 61 69 6e 46 6c 61 67 29 7b 0a 20 20 explainFlag){.
1e278 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 pParse->explain
1e279 3d 20 65 78 70 6c 61 69 6e 46 6c 61 67 3b 0a 20 = explainFlag;.
1e27a 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 pParse->nVar =
1e27b 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 0;.}..#ifndef SQ
1e27c 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1e27d 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65 _CACHE./*.** The
1e27e 20 54 61 62 6c 65 4c 6f 63 6b 20 73 74 72 75 63 TableLock struc
1e27f 74 75 72 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 ture is only use
1e280 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 d by the sqlite3
1e281 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64 0a TableLock() and.
1e282 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b ** codeTableLock
1e283 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a s() functions..*
1e284 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 4c 6f /.struct TableLo
1e285 63 6b 20 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 ck {. int iDb;
1e286 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1e287 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 he database cont
1e288 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 aining the table
1e289 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f to be locked */
1e28a 0a 20 20 69 6e 74 20 69 54 61 62 3b 20 20 20 20 . int iTab;
1e28b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
1e28c 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 oot page of the
1e28d 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b table to be lock
1e28e 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72 69 ed */. u8 isWri
1e28f 74 65 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 teLock; /*
1e290 54 72 75 65 20 66 6f 72 20 77 72 69 74 65 20 6c True for write l
1e291 6f 63 6b 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 ock. False for
1e292 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 a read lock */.
1e293 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
1e294 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 me; /* Name of
1e295 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b the table */.};
1e296 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 ../*.** Record t
1e297 68 65 20 66 61 63 74 20 74 68 61 74 20 77 65 20 he fact that we
1e298 77 61 6e 74 20 74 6f 20 6c 6f 63 6b 20 61 20 74 want to lock a t
1e299 61 62 6c 65 20 61 74 20 72 75 6e 2d 74 69 6d 65 able at run-time
1e29a 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 . .**.** The ta
1e29b 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 ble to be locked
1e29c 20 68 61 73 20 72 6f 6f 74 20 70 61 67 65 20 69 has root page i
1e29d 54 61 62 20 61 6e 64 20 69 73 20 66 6f 75 6e 64 Tab and is found
1e29e 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 in database iDb
1e29f 2e 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 61 ..** A read or a
1e2a0 20 77 72 69 74 65 20 6c 6f 63 6b 20 63 61 6e 20 write lock can
1e2a1 62 65 20 74 61 6b 65 6e 20 64 65 70 65 6e 64 69 be taken dependi
1e2a2 6e 67 20 6f 6e 20 69 73 57 72 69 74 65 6c 6f 63 ng on isWriteloc
1e2a3 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f k..**.** This ro
1e2a4 75 74 69 6e 65 20 6a 75 73 74 20 72 65 63 6f 72 utine just recor
1e2a5 64 73 20 74 68 65 20 66 61 63 74 20 74 68 61 74 ds the fact that
1e2a6 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 65 73 the lock is des
1e2a7 69 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20 63 6f ired. The.** co
1e2a8 64 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 6c de to make the l
1e2a9 6f 63 6b 20 6f 63 63 75 72 20 69 73 20 67 65 6e ock occur is gen
1e2aa 65 72 61 74 65 64 20 62 79 20 61 20 6c 61 74 65 erated by a late
1e2ab 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 63 6f 64 r call to.** cod
1e2ac 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 77 68 eTableLocks() wh
1e2ad 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e ich occurs durin
1e2ae 67 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 g sqlite3FinishC
1e2af 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a 53 51 4c 49 oding()..*/.SQLI
1e2b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1e2b1 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
1e2b2 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1e2b3 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e e, /* Parsin
1e2b4 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 g context */. i
1e2b5 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 nt iDb,
1e2b6 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 /* Index of th
1e2b7 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 e database conta
1e2b8 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 ining the table
1e2b9 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 to lock */. int
1e2ba 20 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 iTab,
1e2bb 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6e 75 6d /* Root page num
1e2bc 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ber of the table
1e2bd 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f to be locked */
1e2be 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 . u8 isWriteLoc
1e2bf 6b 2c 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f k, /* True fo
1e2c0 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a r a write lock *
1e2c1 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1e2c2 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20 6f zName /* Name o
1e2c3 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 f the table to b
1e2c4 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 29 7b 0a 20 e locked */.){.
1e2c5 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 int i;. int nB
1e2c6 79 74 65 73 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 ytes;. TableLoc
1e2c7 6b 20 2a 70 3b 0a 0a 20 20 69 66 28 20 69 44 62 k *p;.. if( iDb
1e2c8 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e <0 ){. return
1e2c9 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 ;. }.. for(i=0
1e2ca 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 ; i<pParse->nTab
1e2cb 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 leLock; i++){.
1e2cc 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 p = &pParse->a
1e2cd 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 TableLock[i];.
1e2ce 20 20 69 66 28 20 70 2d 3e 69 44 62 3d 3d 69 44 if( p->iDb==iD
1e2cf 62 20 26 26 20 70 2d 3e 69 54 61 62 3d 3d 69 54 b && p->iTab==iT
1e2d0 61 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 ab ){. p->i
1e2d1 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 70 2d sWriteLock = (p-
1e2d2 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 >isWriteLock ||
1e2d3 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 isWriteLock);.
1e2d4 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
1e2d5 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65 73 20 }. }.. nBytes
1e2d6 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 4c 6f = sizeof(TableLo
1e2d7 63 6b 29 20 2a 20 28 70 50 61 72 73 65 2d 3e 6e ck) * (pParse->n
1e2d8 54 61 62 6c 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 TableLock+1);.
1e2d9 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f pParse->aTableLo
1e2da 63 6b 20 3d 20 0a 20 20 20 20 20 20 73 71 6c 69 ck = . sqli
1e2db 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 te3DbReallocOrFr
1e2dc 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 ee(pParse->db, p
1e2dd 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 Parse->aTableLoc
1e2de 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 k, nBytes);. if
1e2df 28 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 ( pParse->aTable
1e2e0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d 20 Lock ){. p =
1e2e1 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c &pParse->aTableL
1e2e2 6f 63 6b 5b 70 50 61 72 73 65 2d 3e 6e 54 61 62 ock[pParse->nTab
1e2e3 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 leLock++];. p
1e2e4 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 ->iDb = iDb;.
1e2e5 20 70 2d 3e 69 54 61 62 20 3d 20 69 54 61 62 3b p->iTab = iTab;
1e2e6 0a 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c . p->isWriteL
1e2e7 6f 63 6b 20 3d 20 69 73 57 72 69 74 65 4c 6f 63 ock = isWriteLoc
1e2e8 6b 3b 0a 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 k;. p->zName
1e2e9 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 = zName;. }else
1e2ea 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 {. pParse->nT
1e2eb 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 ableLock = 0;.
1e2ec 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 pParse->db->ma
1e2ed 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
1e2ee 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 }.}../*.** Cod
1e2ef 65 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 e an OP_TableLoc
1e2f0 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f k instruction fo
1e2f1 72 20 65 61 63 68 20 74 61 62 6c 65 20 6c 6f 63 r each table loc
1e2f2 6b 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 74 ked by the.** st
1e2f3 61 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69 67 75 atement (configu
1e2f4 72 65 64 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 red by calls to
1e2f5 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
1e2f6 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ())..*/.static v
1e2f7 6f 69 64 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 oid codeTableLoc
1e2f8 6b 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ks(Parse *pParse
1e2f9 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 ){. int i;. Vd
1e2fa 62 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20 20 69 be *pVdbe; .. i
1e2fb 66 28 20 30 3d 3d 28 70 56 64 62 65 20 3d 20 73 f( 0==(pVdbe = s
1e2fc 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
1e2fd 61 72 73 65 29 29 20 29 7b 0a 20 20 20 20 72 65 arse)) ){. re
1e2fe 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 turn;. }.. for
1e2ff 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e (i=0; i<pParse->
1e300 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 nTableLock; i++)
1e301 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b 20 {. TableLock
1e302 2a 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 *p = &pParse->aT
1e303 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 ableLock[i];.
1e304 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44 62 int p1 = p->iDb
1e305 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1e306 65 41 64 64 4f 70 34 28 70 56 64 62 65 2c 20 4f eAddOp4(pVdbe, O
1e307 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c P_TableLock, p1,
1e308 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73 57 p->iTab, p->isW
1e309 72 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 riteLock,.
1e30a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e30b 70 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 p->zName, P4_STA
1e30c 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 TIC);. }.}.#els
1e30d 65 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64 65 e. #define code
1e30e 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23 65 TableLocks(x).#e
1e30f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
1e310 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
1e311 65 64 20 61 66 74 65 72 20 61 20 73 69 6e 67 6c ed after a singl
1e312 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
1e313 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72 73 has been.** pars
1e314 65 64 20 61 6e 64 20 61 20 56 44 42 45 20 70 72 ed and a VDBE pr
1e315 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65 ogram to execute
1e316 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 that statement
1e317 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 70 has been.** prep
1e318 61 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 ared. This rout
1e319 69 6e 65 20 70 75 74 73 20 74 68 65 20 66 69 6e ine puts the fin
1e31a 69 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20 6f ishing touches o
1e31b 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70 72 n the.** VDBE pr
1e31c 6f 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74 73 ogram and resets
1e31d 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 the pParse stru
1e31e 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e 65 cture for the ne
1e31f 78 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a xt.** parse..**.
1e320 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 ** Note that if
1e321 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 an error occurre
1e322 64 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 d, it might be t
1e323 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a 20 he case that.**
1e324 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61 73 no VDBE code was
1e325 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 generated..*/.S
1e326 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1e327 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 id sqlite3Finish
1e328 43 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70 50 Coding(Parse *pP
1e329 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 arse){. sqlite3
1e32a 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b *db;. Vdbe *v;
1e32b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d .. db = pParse-
1e32c 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d >db;. if( db->m
1e32d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 allocFailed ) re
1e32e 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 turn;. if( pPar
1e32f 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 74 se->nested ) ret
1e330 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 urn;. if( pPars
1e331 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e e->nErr ) return
1e332 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 79 ;.. /* Begin by
1e333 20 67 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d 65 generating some
1e334 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 termination cod
1e335 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 e at the end of
1e336 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 the. ** vdbe pr
1e337 6f 67 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d ogram. */. v =
1e338 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
1e339 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 pParse);. if( v
1e33a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1e33b 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f dbeAddOp0(v, OP_
1e33c 48 61 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 Halt);.. /* T
1e33d 68 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 he cookie mask c
1e33e 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69 74 20 ontains one bit
1e33f 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 for each databas
1e340 65 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 e file open..
1e341 20 2a 2a 20 28 42 69 74 20 30 20 69 73 20 66 6f ** (Bit 0 is fo
1e342 72 20 6d 61 69 6e 2c 20 62 69 74 20 31 20 69 73 r main, bit 1 is
1e343 20 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64 20 73 for temp, and s
1e344 6f 20 66 6f 72 74 68 2e 29 20 20 42 69 74 73 20 o forth.) Bits
1e345 61 72 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 66 are. ** set f
1e346 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65 or each database
1e347 20 74 68 61 74 20 69 73 20 75 73 65 64 2e 20 20 that is used.
1e348 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1e349 20 73 74 61 72 74 20 61 0a 20 20 20 20 2a 2a 20 start a. **
1e34a 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 65 transaction on e
1e34b 61 63 68 20 75 73 65 64 20 64 61 74 61 62 61 73 ach used databas
1e34c 65 20 61 6e 64 20 74 6f 20 76 65 72 69 66 79 20 e and to verify
1e34d 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 the schema cooki
1e34e 65 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63 68 e. ** on each
1e34f 20 75 73 65 64 20 64 61 74 61 62 61 73 65 2e 0a used database..
1e350 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
1e351 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 Parse->cookieGot
1e352 6f 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 33 32 o>0 ){. u32
1e353 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 69 6e 74 mask;. int
1e354 20 69 44 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 iDb;. sqli
1e355 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
1e356 76 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 v, pParse->cooki
1e357 65 47 6f 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20 eGoto-1);.
1e358 66 6f 72 28 69 44 62 3d 30 2c 20 6d 61 73 6b 3d for(iDb=0, mask=
1e359 31 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 1; iDb<db->nDb;
1e35a 6d 61 73 6b 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 mask<<=1, iDb++)
1e35b 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6d {. if( (m
1e35c 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e 63 6f ask & pParse->co
1e35d 6f 6b 69 65 4d 61 73 6b 29 3d 3d 30 20 29 20 63 okieMask)==0 ) c
1e35e 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 ontinue;.
1e35f 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 sqlite3VdbeUses
1e360 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 Btree(v, iDb);.
1e361 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1e362 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 54 72 beAddOp2(v,OP_Tr
1e363 61 6e 73 61 63 74 69 6f 6e 2c 20 69 44 62 2c 20 ansaction, iDb,
1e364 28 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e (mask & pParse->
1e365 77 72 69 74 65 4d 61 73 6b 29 21 3d 30 29 3b 0a writeMask)!=0);.
1e366 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1e367 64 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 56 dbeAddOp2(v,OP_V
1e368 65 72 69 66 79 43 6f 6f 6b 69 65 2c 20 69 44 62 erifyCookie, iDb
1e369 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 , pParse->cookie
1e36a 56 61 6c 75 65 5b 69 44 62 5d 29 3b 0a 20 20 20 Value[iDb]);.
1e36b 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
1e36c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1e36d 54 41 42 4c 45 0a 20 20 20 20 20 20 7b 0a 20 20 TABLE. {.
1e36e 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
1e36f 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
1e370 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 pParse->nVtabLoc
1e371 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 k; i++){.
1e372 20 20 20 63 68 61 72 20 2a 76 74 61 62 20 3d 20 char *vtab =
1e373 28 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e (char *)pParse->
1e374 61 70 56 74 61 62 4c 6f 63 6b 5b 69 5d 2d 3e 70 apVtabLock[i]->p
1e375 56 74 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 Vtab;.
1e376 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1e377 34 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20 4(v, OP_VBegin,
1e378 30 2c 20 30 2c 20 30 2c 20 76 74 61 62 2c 20 50 0, 0, 0, vtab, P
1e379 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20 20 4_VTAB);.
1e37a 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 }. pPars
1e37b 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 20 3d 20 30 e->nVtabLock = 0
1e37c 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
1e37d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 63 65 20 .. /* Once
1e37e 61 6c 6c 20 74 68 65 20 63 6f 6f 6b 69 65 73 20 all the cookies
1e37f 68 61 76 65 20 62 65 65 6e 20 76 65 72 69 66 69 have been verifi
1e380 65 64 20 61 6e 64 20 74 72 61 6e 73 61 63 74 69 ed and transacti
1e381 6f 6e 73 20 6f 70 65 6e 65 64 2c 20 0a 20 20 20 ons opened, .
1e382 20 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 ** obtain the
1e383 20 72 65 71 75 69 72 65 64 20 74 61 62 6c 65 2d required table-
1e384 6c 6f 63 6b 73 2e 20 54 68 69 73 20 69 73 20 61 locks. This is a
1e385 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 74 68 no-op unless th
1e386 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 68 61 72 e . ** shar
1e387 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 ed-cache feature
1e388 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20 is enabled..
1e389 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65 */. code
1e38a 54 61 62 6c 65 4c 6f 63 6b 73 28 70 50 61 72 73 TableLocks(pPars
1e38b 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 e);. sqlite
1e38c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1e38d 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61 72 73 P_Goto, 0, pPars
1e38e 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a e->cookieGoto);.
1e38f 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 }..#ifndef S
1e390 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
1e391 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e . if( !db->in
1e392 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 it.busy ){.
1e393 20 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 /* Change the P
1e394 34 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 4 argument of th
1e395 65 20 66 69 72 73 74 20 6f 70 63 6f 64 65 20 28 e first opcode (
1e396 77 68 69 63 68 20 77 69 6c 6c 20 61 6c 77 61 79 which will alway
1e397 73 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e s be. ** an
1e398 20 4f 50 5f 54 72 61 63 65 29 20 74 6f 20 62 65 OP_Trace) to be
1e399 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 the complete te
1e39a 78 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e xt of the curren
1e39b 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e t SQL statement.
1e39c 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1e39d 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71 VdbeOp *pOp = sq
1e39e 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 lite3VdbeGetOp(v
1e39f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
1e3a0 70 4f 70 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f pOp && pOp->opco
1e3a1 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 29 7b 0a de==OP_Trace ){.
1e3a2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1e3a3 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 30 dbeChangeP4(v, 0
1e3a4 2c 20 70 50 61 72 73 65 2d 3e 7a 53 71 6c 2c 20 , pParse->zSql,
1e3a5 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 70 50 pParse->zTail-pP
1e3a6 61 72 73 65 2d 3e 7a 53 71 6c 29 3b 0a 20 20 20 arse->zSql);.
1e3a7 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
1e3a8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1e3a9 5f 54 52 41 43 45 20 2a 2f 0a 20 20 7d 0a 0a 0a _TRACE */. }...
1e3aa 20 20 2f 2a 20 47 65 74 20 74 68 65 20 56 44 42 /* Get the VDB
1e3ab 45 20 70 72 6f 67 72 61 6d 20 72 65 61 64 79 20 E program ready
1e3ac 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 for execution.
1e3ad 2a 2f 0a 20 20 69 66 28 20 76 20 26 26 20 70 50 */. if( v && pP
1e3ae 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 arse->nErr==0 &&
1e3af 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c !db->mallocFail
1e3b0 65 64 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c ed ){.#ifdef SQL
1e3b1 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 46 49 ITE_DEBUG. FI
1e3b2 4c 45 20 2a 74 72 61 63 65 20 3d 20 28 64 62 2d LE *trace = (db-
1e3b3 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
1e3b4 56 64 62 65 54 72 61 63 65 29 21 3d 30 20 3f 20 VdbeTrace)!=0 ?
1e3b5 73 74 64 6f 75 74 20 3a 20 30 3b 0a 20 20 20 20 stdout : 0;.
1e3b6 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 sqlite3VdbeTrace
1e3b7 28 76 2c 20 74 72 61 63 65 29 3b 0a 23 65 6e 64 (v, trace);.#end
1e3b8 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 if. assert( p
1e3b9 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f Parse->disableCo
1e3ba 6c 43 61 63 68 65 3d 3d 30 20 29 3b 20 20 2f 2a lCache==0 ); /*
1e3bb 20 44 69 73 61 62 6c 65 73 20 61 6e 64 20 72 65 Disables and re
1e3bc 2d 65 6e 61 62 6c 65 73 20 6d 61 74 63 68 20 2a -enables match *
1e3bd 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 /. sqlite3Vdb
1e3be 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 70 50 eMakeReady(v, pP
1e3bf 61 72 73 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72 arse->nVar, pPar
1e3c0 73 65 2d 3e 6e 4d 65 6d 2b 33 2c 0a 20 20 20 20 se->nMem+3,.
1e3c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e3c2 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 pParse->nTa
1e3c3 62 2b 33 2c 20 70 50 61 72 73 65 2d 3e 65 78 70 b+3, pParse->exp
1e3c4 6c 61 69 6e 29 3b 0a 20 20 20 20 70 50 61 72 73 lain);. pPars
1e3c5 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 e->rc = SQLITE_D
1e3c6 4f 4e 45 3b 0a 20 20 20 20 70 50 61 72 73 65 2d ONE;. pParse-
1e3c7 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 30 >colNamesSet = 0
1e3c8 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 ;. }else if( pP
1e3c9 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 arse->rc==SQLITE
1e3ca 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 _OK ){. pPars
1e3cb 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 e->rc = SQLITE_E
1e3cc 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72 RROR;. }. pPar
1e3cd 73 65 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 se->nTab = 0;.
1e3ce 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 pParse->nMem = 0
1e3cf 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 74 ;. pParse->nSet
1e3d0 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e = 0;. pParse->
1e3d1 6e 56 61 72 20 3d 20 30 3b 0a 20 20 70 50 61 72 nVar = 0;. pPar
1e3d2 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d se->cookieMask =
1e3d3 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 0;. pParse->co
1e3d4 6f 6b 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a okieGoto = 0;.}.
1e3d5 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 ./*.** Run the p
1e3d6 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65 20 67 arser and code g
1e3d7 65 6e 65 72 61 74 6f 72 20 72 65 63 75 72 73 69 enerator recursi
1e3d8 76 65 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f vely in order to
1e3d9 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 generate.** cod
1e3da 65 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73 74 e for the SQL st
1e3db 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e atement given on
1e3dc 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 to the end of th
1e3dd 65 20 70 50 61 72 73 65 20 63 6f 6e 74 65 78 74 e pParse context
1e3de 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e .** currently un
1e3df 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e der construction
1e3e0 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 . When the pars
1e3e1 65 72 20 69 73 20 72 75 6e 20 72 65 63 75 72 73 er is run recurs
1e3e2 69 76 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61 ively.** this wa
1e3e3 79 2c 20 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f y, the final OP_
1e3e4 48 61 6c 74 20 69 73 20 6e 6f 74 20 61 70 70 65 Halt is not appe
1e3e5 6e 64 65 64 20 61 6e 64 20 6f 74 68 65 72 20 69 nded and other i
1e3e6 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a nitialization.**
1e3e7 20 61 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f and finalizatio
1e3e8 6e 20 73 74 65 70 73 20 61 72 65 20 6f 6d 69 74 n steps are omit
1e3e9 74 65 64 20 62 65 63 61 75 73 65 20 74 68 6f 73 ted because thos
1e3ea 65 20 61 72 65 20 68 61 6e 64 6c 69 6e 67 20 62 e are handling b
1e3eb 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f y the.** outermo
1e3ec 73 74 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a st parser..**.**
1e3ed 20 4e 6f 74 20 65 76 65 72 79 74 68 69 6e 67 20 Not everything
1e3ee 69 73 20 6e 65 73 74 61 62 6c 65 2e 20 20 54 68 is nestable. Th
1e3ef 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 64 is facility is d
1e3f0 65 73 69 67 6e 65 64 20 74 6f 20 70 65 72 6d 69 esigned to permi
1e3f1 74 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 44 t.** INSERT, UPD
1e3f2 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 ATE, and DELETE
1e3f3 6f 70 65 72 61 74 69 6f 6e 73 20 61 67 61 69 6e operations again
1e3f4 73 74 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 st SQLITE_MASTER
1e3f5 2e 20 20 55 73 65 0a 2a 2a 20 63 61 72 65 20 69 . Use.** care i
1e3f6 66 20 79 6f 75 20 64 65 63 69 64 65 20 74 6f 20 f you decide to
1e3f7 74 72 79 20 74 6f 20 75 73 65 20 74 68 69 73 20 try to use this
1e3f8 72 6f 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65 routine for some
1e3f9 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e other purposes.
1e3fa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1e3fb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e TE void sqlite3N
1e3fc 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73 65 estedParse(Parse
1e3fd 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 *pParse, const
1e3fe 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
1e3ff 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
1e400 70 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b p;. char *zSql;
1e401 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 . char *zErrMsg
1e402 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 = 0;. sqlite3
1e403 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1e404 3b 0a 23 20 64 65 66 69 6e 65 20 53 41 56 45 5f ;.# define SAVE_
1e405 53 5a 20 20 28 73 69 7a 65 6f 66 28 50 61 72 73 SZ (sizeof(Pars
1e406 65 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 50 61 e) - offsetof(Pa
1e407 72 73 65 2c 6e 56 61 72 29 29 0a 20 20 63 68 61 rse,nVar)). cha
1e408 72 20 73 61 76 65 42 75 66 5b 53 41 56 45 5f 53 r saveBuf[SAVE_S
1e409 5a 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 Z];.. if( pPars
1e40a 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e e->nErr ) return
1e40b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 ;. assert( pPar
1e40c 73 65 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b se->nested<10 );
1e40d 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f /* Nesting sho
1e40e 75 6c 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c uld only be of l
1e40f 69 6d 69 74 65 64 20 64 65 70 74 68 20 2a 2f 0a imited depth */.
1e410 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
1e411 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20 Format);. zSql
1e412 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
1e413 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 f(db, zFormat, a
1e414 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 p);. va_end(ap)
1e415 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 ;. if( zSql==0
1e416 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20 ){. return;
1e417 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 /* A malloc mus
1e418 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f t have failed */
1e419 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e . }. pParse->n
1e41a 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 ested++;. memcp
1e41b 79 28 73 61 76 65 42 75 66 2c 20 26 70 50 61 72 y(saveBuf, &pPar
1e41c 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53 se->nVar, SAVE_S
1e41d 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 50 Z);. memset(&pP
1e41e 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 arse->nVar, 0, S
1e41f 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74 AVE_SZ);. sqlit
1e420 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72 e3RunParser(pPar
1e421 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d se, zSql, &zErrM
1e422 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 sg);. sqlite3Db
1e423 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 Free(db, zErrMsg
1e424 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1e425 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 ee(db, zSql);.
1e426 6d 65 6d 63 70 79 28 26 70 50 61 72 73 65 2d 3e memcpy(&pParse->
1e427 6e 56 61 72 2c 20 73 61 76 65 42 75 66 2c 20 53 nVar, saveBuf, S
1e428 41 56 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73 AVE_SZ);. pPars
1e429 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a e->nested--;.}..
1e42a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 /*.** Locate the
1e42b 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 in-memory struc
1e42c 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 ture that descri
1e42d 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 bes a particular
1e42e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 database.** tab
1e42f 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d le given the nam
1e430 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 e of that table
1e431 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 and (optionally)
1e432 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1e433 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
1e434 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c taining the tabl
1e435 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 e. Return NULL
1e436 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a if not found..**
1e437 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 65 .** If zDatabase
1e438 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 is 0, all datab
1e439 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65 ases are searche
1e43a 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 d for the table
1e43b 61 6e 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 and the.** first
1e43c 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 20 matching table
1e43d 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 4e is returned. (N
1e43e 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 o checking for d
1e43f 75 70 6c 69 63 61 74 65 20 74 61 62 6c 65 0a 2a uplicate table.*
1e440 2a 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e * names is done.
1e441 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f 72 ) The search or
1e442 64 65 72 20 69 73 20 54 45 4d 50 20 66 69 72 73 der is TEMP firs
1e443 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 t, then MAIN, th
1e444 65 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 en any.** auxili
1e445 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64 ary databases ad
1e446 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 54 ded using the AT
1e447 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a TACH command..**
1e448 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c .** See also sql
1e449 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 ite3LocateTable(
1e44a 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
1e44b 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 VATE Table *sqli
1e44c 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c te3FindTable(sql
1e44d 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
1e44e 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e char *zName, con
1e44f 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 st char *zDataba
1e450 73 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20 se){. Table *p
1e451 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 = 0;. int i;.
1e452 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 int nName;. ass
1e453 65 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b ert( zName!=0 );
1e454 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 . nName = sqlit
1e455 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 e3Strlen(db, zNa
1e456 6d 65 29 20 2b 20 31 3b 0a 20 20 66 6f 72 28 69 me) + 1;. for(i
1e457 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c =OMIT_TEMPDB; i<
1e458 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
1e459 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 int j = (i<2)
1e45a 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f 2a ? i^1 : i; /*
1e45b 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 Search TEMP bef
1e45c 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 ore MAIN */.
1e45d 69 66 28 20 7a 44 61 74 61 62 61 73 65 21 3d 30 if( zDatabase!=0
1e45e 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1e45f 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64 62 mp(zDatabase, db
1e460 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 ->aDb[j].zName)
1e461 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1e462 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 p = sqlite3HashF
1e463 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e ind(&db->aDb[j].
1e464 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 pSchema->tblHash
1e465 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b , zName, nName);
1e466 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72 65 . if( p ) bre
1e467 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ak;. }. return
1e468 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 p;.}../*.** Loc
1e469 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 ate the in-memor
1e46a 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 y structure that
1e46b 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61 72 describes a par
1e46c 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
1e46d 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e 20 .** table given
1e46e 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 the name of that
1e46f 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 69 table and (opti
1e470 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65 onally) the name
1e471 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 of the.** datab
1e472 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ase containing t
1e473 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 he table. Retur
1e474 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f n NULL if not fo
1e475 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61 76 65 und. Also leave
1e476 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 an.** error mes
1e477 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e sage in pParse->
1e478 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 zErrMsg..**.** T
1e479 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 he difference be
1e47a 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74 69 tween this routi
1e47b 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33 46 69 ne and sqlite3Fi
1e47c 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74 68 61 ndTable() is tha
1e47d 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e t this.** routin
1e47e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72 72 6f e leaves an erro
1e47f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 r message in pPa
1e480 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77 68 65 rse->zErrMsg whe
1e481 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e re.** sqlite3Fin
1e482 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20 6e 6f dTable() does no
1e483 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
1e484 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 VATE Table *sqli
1e485 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 0a te3LocateTable(.
1e486 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
1e487 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6e 74 /* cont
1e488 65 78 74 20 69 6e 20 77 68 69 63 68 20 74 6f 20 ext in which to
1e489 72 65 70 6f 72 74 20 65 72 72 6f 72 73 20 2a 2f report errors */
1e48a 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20 20 . int isView,
1e48b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1e48c 65 20 69 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 e if looking for
1e48d 20 61 20 56 49 45 57 20 72 61 74 68 65 72 20 74 a VIEW rather t
1e48e 68 61 6e 20 61 20 54 41 42 4c 45 20 2a 2f 0a 20 han a TABLE */.
1e48f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
1e490 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 me, /* Name
1e491 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 of the table we
1e492 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 are looking for
1e493 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1e494 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a 20 4e *zDbase /* N
1e495 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
1e496 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65 20 4e ase. Might be N
1e497 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c ULL */.){. Tabl
1e498 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 e *p;.. /* Read
1e499 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
1e49a 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f hema. If an erro
1e49b 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 r occurs, leave
1e49c 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1e49d 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 . ** and code i
1e49e 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 n pParse and ret
1e49f 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 urn NULL. */. i
1e4a0 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 f( SQLITE_OK!=sq
1e4a1 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 lite3ReadSchema(
1e4a2 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72 pParse) ){. r
1e4a3 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 eturn 0;. }..
1e4a4 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 p = sqlite3FindT
1e4a5 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c able(pParse->db,
1e4a6 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65 29 3b zName, zDbase);
1e4a7 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 . if( p==0 ){.
1e4a8 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1e4a9 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f 20 22 Msg = isView ? "
1e4aa 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20 3a 20 no such view" :
1e4ab 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 22 3b "no such table";
1e4ac 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73 65 20 . if( zDbase
1e4ad 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1e4ae 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1e4af 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 4d "%s: %s.%s", zM
1e4b0 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e 61 6d sg, zDbase, zNam
1e4b1 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 e);. }else{.
1e4b2 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1e4b3 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 rMsg(pParse, "%s
1e4b4 3a 20 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e 61 : %s", zMsg, zNa
1e4b5 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 me);. }. p
1e4b6 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 Parse->checkSche
1e4b7 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 ma = 1;. }. re
1e4b8 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
1e4b9 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d Locate the in-m
1e4ba 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 20 emory structure
1e4bb 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 0a that describes .
1e4bc 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 ** a particular
1e4bd 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65 20 index given the
1e4be 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e 64 name of that ind
1e4bf 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61 ex.** and the na
1e4c0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1e4c1 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 se that contains
1e4c2 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 52 the index..** R
1e4c3 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f eturn NULL if no
1e4c4 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 t found..**.** I
1e4c5 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20 30 f zDatabase is 0
1e4c6 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 , all databases
1e4c7 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 are searched for
1e4c8 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e the.** table an
1e4c9 64 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 d the first matc
1e4ca 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72 65 hing index is re
1e4cb 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65 turned. (No che
1e4cc 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 70 cking.** for dup
1e4cd 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61 6d licate index nam
1e4ce 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 68 es is done.) Th
1e4cf 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20 69 e search order i
1e4d0 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74 2c s.** TEMP first,
1e4d1 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e then MAIN, then
1e4d2 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64 any auxiliary d
1e4d3 61 74 61 62 61 73 65 73 20 61 64 64 65 64 0a 2a atabases added.*
1e4d4 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 * using the ATTA
1e4d5 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 CH command..*/.S
1e4d6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e QLITE_PRIVATE In
1e4d7 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 dex *sqlite3Find
1e4d8 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 Index(sqlite3 *d
1e4d9 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
1e4da 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 Name, const char
1e4db 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78 20 *zDb){. Index
1e4dc 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b *p = 0;. int i;
1e4dd 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 . int nName = s
1e4de 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c qlite3Strlen(db,
1e4df 20 7a 4e 61 6d 65 29 2b 31 3b 0a 20 20 66 6f 72 zName)+1;. for
1e4e0 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 (i=OMIT_TEMPDB;
1e4e1 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
1e4e2 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c . int j = (i<
1e4e3 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2) ? i^1 : i; /
1e4e4 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 * Search TEMP be
1e4e5 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 fore MAIN */.
1e4e6 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
1e4e7 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 = db->aDb[j].pS
1e4e8 63 68 65 6d 61 3b 0a 20 20 20 20 69 66 28 20 7a chema;. if( z
1e4e9 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 Db && sqlite3Str
1e4ea 49 43 6d 70 28 7a 44 62 2c 20 64 62 2d 3e 61 44 ICmp(zDb, db->aD
1e4eb 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f b[j].zName) ) co
1e4ec 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 73 65 ntinue;. asse
1e4ed 72 74 28 20 70 53 63 68 65 6d 61 20 7c 7c 20 28 rt( pSchema || (
1e4ee 6a 3d 3d 31 20 26 26 20 21 64 62 2d 3e 61 44 62 j==1 && !db->aDb
1e4ef 5b 31 5d 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 [1].pBt) );.
1e4f0 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a 20 if( pSchema ){.
1e4f1 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p = sqlite3
1e4f2 48 61 73 68 46 69 6e 64 28 26 70 53 63 68 65 6d HashFind(&pSchem
1e4f3 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d a->idxHash, zNam
1e4f4 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d e, nName);. }
1e4f5 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72 65 . if( p ) bre
1e4f6 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ak;. }. return
1e4f7 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 p;.}../*.** Rec
1e4f8 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20 laim the memory
1e4f9 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78 used by an index
1e4fa 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1e4fb 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65 78 20 freeIndex(Index
1e4fc 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a *p){. sqlite3 *
1e4fd 64 62 20 3d 20 70 2d 3e 70 54 61 62 6c 65 2d 3e db = p->pTable->
1e4fe 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 db;. sqlite3DbF
1e4ff 72 65 65 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 ree(db, p->zColA
1e500 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 ff);. sqlite3Db
1e501 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a Free(db, p);.}..
1e502 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 /*.** Remove the
1e503 20 67 69 76 65 6e 20 69 6e 64 65 78 20 66 72 6f given index fro
1e504 6d 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 68 m the index hash
1e505 20 74 61 62 6c 65 2c 20 61 6e 64 20 66 72 65 65 table, and free
1e506 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f 72 79 20 73 .** its memory s
1e507 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a tructures..**.**
1e508 20 54 68 65 20 69 6e 64 65 78 20 69 73 20 72 65 The index is re
1e509 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 64 moved from the d
1e50a 61 74 61 62 61 73 65 20 68 61 73 68 20 74 61 62 atabase hash tab
1e50b 6c 65 73 20 62 75 74 0a 2a 2a 20 69 74 20 69 73 les but.** it is
1e50c 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65 64 20 66 72 not unlinked fr
1e50d 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 74 68 61 om the Table tha
1e50e 74 20 69 74 20 69 6e 64 65 78 65 73 2e 0a 2a 2a t it indexes..**
1e50f 20 55 6e 6c 69 6e 6b 69 6e 67 20 66 72 6f 6d 20 Unlinking from
1e510 74 68 65 20 54 61 62 6c 65 20 6d 75 73 74 20 62 the Table must b
1e511 65 20 64 6f 6e 65 20 62 79 20 74 68 65 20 63 61 e done by the ca
1e512 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a lling function..
1e513 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
1e514 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65 78 qliteDeleteIndex
1e515 28 49 6e 64 65 78 20 2a 70 29 7b 0a 20 20 49 6e (Index *p){. In
1e516 64 65 78 20 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e dex *pOld;. con
1e517 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d st char *zName =
1e518 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f p->zName;.. pO
1e519 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 ld = sqlite3Hash
1e51a 49 6e 73 65 72 74 28 26 70 2d 3e 70 53 63 68 65 Insert(&p->pSche
1e51b 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 ma->idxHash, zNa
1e51c 6d 65 2c 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 me, strlen(zName
1e51d 29 2b 31 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 )+1, 0);. asser
1e51e 74 28 20 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70 4f t( pOld==0 || pO
1e51f 6c 64 3d 3d 70 20 29 3b 0a 20 20 66 72 65 65 49 ld==p );. freeI
1e520 6e 64 65 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ndex(p);.}../*.*
1e521 2a 20 46 6f 72 20 74 68 65 20 69 6e 64 65 78 20 * For the index
1e522 63 61 6c 6c 65 64 20 7a 49 64 78 4e 61 6d 65 20 called zIdxName
1e523 77 68 69 63 68 20 69 73 20 66 6f 75 6e 64 20 69 which is found i
1e524 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 n the database i
1e525 44 62 2c 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74 68 Db,.** unlike th
1e526 61 74 20 69 6e 64 65 78 20 66 72 6f 6d 20 69 74 at index from it
1e527 73 20 54 61 62 6c 65 20 74 68 65 6e 20 72 65 6d s Table then rem
1e528 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 66 72 ove the index fr
1e529 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 om.** the index
1e52a 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 66 hash table and f
1e52b 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 73 ree all memory s
1e52c 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 tructures associ
1e52d 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 ated.** with the
1e52e 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 index..*/.SQLIT
1e52f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1e530 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
1e531 65 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74 eleteIndex(sqlit
1e532 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c e3 *db, int iDb,
1e533 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 const char *zId
1e534 78 4e 61 6d 65 29 7b 0a 20 20 49 6e 64 65 78 20 xName){. Index
1e535 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20 6c *pIndex;. int l
1e536 65 6e 3b 0a 20 20 48 61 73 68 20 2a 70 48 61 73 en;. Hash *pHas
1e537 68 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 h = &db->aDb[iDb
1e538 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 ].pSchema->idxHa
1e539 73 68 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 6c sh;.. len = sql
1e53a 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a ite3Strlen(db, z
1e53b 49 64 78 4e 61 6d 65 29 3b 0a 20 20 70 49 6e 64 IdxName);. pInd
1e53c 65 78 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 ex = sqlite3Hash
1e53d 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 49 Insert(pHash, zI
1e53e 64 78 4e 61 6d 65 2c 20 6c 65 6e 2b 31 2c 20 30 dxName, len+1, 0
1e53f 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20 );. if( pIndex
1e540 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 ){. if( pInde
1e541 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 x->pTable->pInde
1e542 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 x==pIndex ){.
1e543 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c pIndex->pTabl
1e544 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 e->pIndex = pInd
1e545 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d ex->pNext;. }
1e546 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 else{. Inde
1e547 78 20 2a 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 x *p;. for(
1e548 70 3d 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 p=pIndex->pTable
1e549 2d 3e 70 49 6e 64 65 78 3b 20 70 20 26 26 20 70 ->pIndex; p && p
1e54a 2d 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 3b ->pNext!=pIndex;
1e54b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 p=p->pNext){}.
1e54c 20 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d if( p && p-
1e54d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 65 78 20 29 >pNext==pIndex )
1e54e 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 {. p->pNe
1e54f 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 xt = pIndex->pNe
1e550 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 xt;. }.
1e551 7d 0a 20 20 20 20 66 72 65 65 49 6e 64 65 78 28 }. freeIndex(
1e552 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20 64 pIndex);. }. d
1e553 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 b->flags |= SQLI
1e554 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 TE_InternChanges
1e555 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 ;.}../*.** Erase
1e556 20 61 6c 6c 20 73 63 68 65 6d 61 20 69 6e 66 6f all schema info
1e557 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 rmation from the
1e558 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20 in-memory hash
1e559 74 61 62 6c 65 73 20 6f 66 0a 2a 2a 20 61 20 73 tables of.** a s
1e55a 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 2e 20 ingle database.
1e55b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1e55c 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 63 6c 61 called to recla
1e55d 69 6d 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 im memory.** bef
1e55e 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ore the database
1e55f 20 63 6c 6f 73 65 73 2e 20 20 49 74 20 69 73 20 closes. It is
1e560 61 6c 73 6f 20 63 61 6c 6c 65 64 20 64 75 72 69 also called duri
1e561 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a ng a rollback.**
1e562 20 69 66 20 74 68 65 72 65 20 77 65 72 65 20 73 if there were s
1e563 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 64 75 chema changes du
1e564 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 ring the transac
1e565 74 69 6f 6e 20 6f 72 20 69 66 20 61 0a 2a 2a 20 tion or if a.**
1e566 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 6d 69 schema-cookie mi
1e567 73 6d 61 74 63 68 20 6f 63 63 75 72 73 2e 0a 2a smatch occurs..*
1e568 2a 0a 2a 2a 20 49 66 20 69 44 62 3c 3d 30 20 74 *.** If iDb<=0 t
1e569 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69 6e hen reset the in
1e56a 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 74 61 ternal schema ta
1e56b 62 6c 65 73 20 66 6f 72 20 61 6c 6c 20 64 61 74 bles for all dat
1e56c 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 20 abase.** files.
1e56d 20 49 66 20 69 44 62 3e 3d 32 20 74 68 65 6e 20 If iDb>=2 then
1e56e 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e reset the intern
1e56f 61 6c 20 73 63 68 65 6d 61 20 66 6f 72 20 6f 6e al schema for on
1e570 6c 79 20 74 68 65 0a 2a 2a 20 73 69 6e 67 6c 65 ly the.** single
1e571 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 2e file indicated.
1e572 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1e573 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
1e574 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 esetInternalSche
1e575 6d 61 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ma(sqlite3 *db,
1e576 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74 20 int iDb){. int
1e577 69 2c 20 6a 3b 0a 20 20 61 73 73 65 72 74 28 20 i, j;. assert(
1e578 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 iDb>=0 && iDb<db
1e579 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 69 66 28 20 ->nDb );.. if(
1e57a 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 iDb==0 ){. sq
1e57b 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
1e57c 6c 6c 28 64 62 29 3b 0a 20 20 7d 0a 20 20 66 6f ll(db);. }. fo
1e57d 72 28 69 3d 69 44 62 3b 20 69 3c 64 62 2d 3e 6e r(i=iDb; i<db->n
1e57e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44 62 Db; i++){. Db
1e57f 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 *pDb = &db->aDb
1e580 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 [i];. if( pDb
1e581 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 ->pSchema ){.
1e582 20 20 20 61 73 73 65 72 74 28 69 3d 3d 31 20 7c assert(i==1 |
1e583 7c 20 28 70 44 62 2d 3e 70 42 74 20 26 26 20 73 | (pDb->pBt && s
1e584 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1e585 4d 75 74 65 78 28 70 44 62 2d 3e 70 42 74 29 29 Mutex(pDb->pBt))
1e586 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1e587 53 63 68 65 6d 61 46 72 65 65 28 70 44 62 2d 3e SchemaFree(pDb->
1e588 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a pSchema);. }.
1e589 20 20 20 20 69 66 28 20 69 44 62 3e 30 20 29 20 if( iDb>0 )
1e58a 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 return;. }. as
1e58b 73 65 72 74 28 20 69 44 62 3d 3d 30 20 29 3b 0a sert( iDb==0 );.
1e58c 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e db->flags &= ~
1e58d 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
1e58e 6e 67 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 42 nges;. sqlite3B
1e58f 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 treeLeaveAll(db)
1e590 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 6f ;.. /* If one o
1e591 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 61 75 r more of the au
1e592 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 xiliary database
1e593 20 66 69 6c 65 73 20 68 61 73 20 62 65 65 6e 20 files has been
1e594 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a 20 74 68 65 closed,. ** the
1e595 6e 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72 n remove them fr
1e596 6f 6d 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 om the auxiliary
1e597 20 64 61 74 61 62 61 73 65 20 6c 69 73 74 2e 20 database list.
1e598 20 57 65 20 74 61 6b 65 20 74 68 65 0a 20 20 2a We take the. *
1e599 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f * opportunity to
1e59a 20 64 6f 20 74 68 69 73 20 68 65 72 65 20 73 69 do this here si
1e59b 6e 63 65 20 77 65 20 68 61 76 65 20 6a 75 73 74 nce we have just
1e59c 20 64 65 6c 65 74 65 64 20 61 6c 6c 20 6f 66 20 deleted all of
1e59d 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 the. ** schema
1e59e 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64 20 hash tables and
1e59f 74 68 65 72 65 66 6f 72 65 20 64 6f 20 6e 6f 74 therefore do not
1e5a0 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 6e have to make an
1e5a1 79 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 y changes. ** t
1e5a2 6f 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20 74 o any of those t
1e5a3 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f ables.. */. fo
1e5a4 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 r(i=0; i<db->nDb
1e5a5 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 ; i++){. stru
1e5a6 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 ct Db *pDb = &db
1e5a7 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 ->aDb[i];. if
1e5a8 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b ( pDb->pBt==0 ){
1e5a9 0a 20 20 20 20 20 20 69 66 28 20 70 44 62 2d 3e . if( pDb->
1e5aa 70 41 75 78 20 26 26 20 70 44 62 2d 3e 78 46 72 pAux && pDb->xFr
1e5ab 65 65 41 75 78 20 29 20 70 44 62 2d 3e 78 46 72 eeAux ) pDb->xFr
1e5ac 65 65 41 75 78 28 70 44 62 2d 3e 70 41 75 78 29 eeAux(pDb->pAux)
1e5ad 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 41 75 ;. pDb->pAu
1e5ae 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d x = 0;. }. }
1e5af 0a 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c . for(i=j=2; i<
1e5b0 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
1e5b1 20 20 20 73 74 72 75 63 74 20 44 62 20 2a 70 44 struct Db *pD
1e5b2 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b b = &db->aDb[i];
1e5b3 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 . if( pDb->pB
1e5b4 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 t==0 ){. sq
1e5b5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1e5b6 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 pDb->zName);.
1e5b7 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3d 20 pDb->zName =
1e5b8 30 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 0;. continu
1e5b9 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 e;. }. if(
1e5ba 20 6a 3c 69 20 29 7b 0a 20 20 20 20 20 20 64 62 j<i ){. db
1e5bb 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 62 2d 3e 61 ->aDb[j] = db->a
1e5bc 44 62 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 Db[i];. }.
1e5bd 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 j++;. }. mems
1e5be 65 74 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20 et(&db->aDb[j],
1e5bf 30 2c 20 28 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73 0, (db->nDb-j)*s
1e5c0 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 6a 5d izeof(db->aDb[j]
1e5c1 29 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 ));. db->nDb =
1e5c2 6a 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 j;. if( db->nDb
1e5c3 3c 3d 32 20 26 26 20 64 62 2d 3e 61 44 62 21 3d <=2 && db->aDb!=
1e5c4 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 7b db->aDbStatic ){
1e5c5 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e . memcpy(db->
1e5c6 61 44 62 53 74 61 74 69 63 2c 20 64 62 2d 3e 61 aDbStatic, db->a
1e5c7 44 62 2c 20 32 2a 73 69 7a 65 6f 66 28 64 62 2d Db, 2*sizeof(db-
1e5c8 3e 61 44 62 5b 30 5d 29 29 3b 0a 20 20 20 20 73 >aDb[0]));. s
1e5c9 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1e5ca 20 64 62 2d 3e 61 44 62 29 3b 0a 20 20 20 20 64 db->aDb);. d
1e5cb 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44 62 b->aDb = db->aDb
1e5cc 53 74 61 74 69 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f Static;. }.}../
1e5cd 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1e5ce 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e e is called when
1e5cf 20 61 20 63 6f 6d 6d 69 74 20 6f 63 63 75 72 73 a commit occurs
1e5d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1e5d1 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1e5d2 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 CommitInternalCh
1e5d3 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 anges(sqlite3 *d
1e5d4 62 29 7b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 b){. db->flags
1e5d5 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 &= ~SQLITE_Inter
1e5d6 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a nChanges;.}../*.
1e5d7 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6c ** Clear the col
1e5d8 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 61 umn names from a
1e5d9 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2e 0a table or view..
1e5da 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
1e5db 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e qliteResetColumn
1e5dc 4e 61 6d 65 73 28 54 61 62 6c 65 20 2a 70 54 61 Names(Table *pTa
1e5dd 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ble){. int i;.
1e5de 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 Column *pCol;.
1e5df 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1e5e0 54 61 62 6c 65 2d 3e 64 62 3b 0a 20 20 61 73 73 Table->db;. ass
1e5e1 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20 29 ert( pTable!=0 )
1e5e2 3b 0a 20 20 69 66 28 20 28 70 43 6f 6c 20 3d 20 ;. if( (pCol =
1e5e3 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 pTable->aCol)!=0
1e5e4 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ){. for(i=0;
1e5e5 20 69 3c 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b i<pTable->nCol;
1e5e6 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 i++, pCol++){.
1e5e7 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1e5e8 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 ee(db, pCol->zNa
1e5e9 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 me);. sqlit
1e5ea 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
1e5eb 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 pCol->pDflt);.
1e5ec 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1e5ed 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 ee(db, pCol->zTy
1e5ee 70 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 pe);. sqlit
1e5ef 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f e3DbFree(db, pCo
1e5f0 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d l->zColl);. }
1e5f1 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1e5f2 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 ee(db, pTable->a
1e5f3 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54 61 62 Col);. }. pTab
1e5f4 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 le->aCol = 0;.
1e5f5 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 pTable->nCol = 0
1e5f6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 ;.}../*.** Remov
1e5f7 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61 74 e the memory dat
1e5f8 61 20 73 74 72 75 63 74 75 72 65 73 20 61 73 73 a structures ass
1e5f9 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
1e5fa 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e given.** Table.
1e5fb 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 No changes are
1e5fc 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62 79 made to disk by
1e5fd 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
1e5fe 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1e5ff 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20 74 e just deletes t
1e600 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 he data structur
1e601 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 e. It does not
1e602 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 unlink.** the ta
1e603 62 6c 65 20 64 61 74 61 20 73 74 72 75 63 74 75 ble data structu
1e604 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 re from the hash
1e605 20 74 61 62 6c 65 2e 20 20 4e 6f 72 20 64 6f 65 table. Nor doe
1e606 73 20 69 74 20 72 65 6d 6f 76 65 0a 2a 2a 20 66 s it remove.** f
1e607 6f 72 65 69 67 6e 20 6b 65 79 73 20 66 72 6f 6d oreign keys from
1e608 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 4b 65 the sqlite.aFKe
1e609 79 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 42 y hash table. B
1e60a 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74 72 ut it does destr
1e60b 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74 72 oy.** memory str
1e60c 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20 69 uctures of the i
1e60d 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 69 ndices and forei
1e60e 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 74 gn keys associat
1e60f 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 20 ed with .** the
1e610 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 table..*/.SQLITE
1e611 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1e612 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 lite3DeleteTable
1e613 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b (Table *pTable){
1e614 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
1e615 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 46 4b 65 79 , *pNext;. FKey
1e616 20 2a 70 46 4b 65 79 2c 20 2a 70 4e 65 78 74 46 *pFKey, *pNextF
1e617 4b 65 79 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a Key;. sqlite3 *
1e618 64 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 6c db;.. if( pTabl
1e619 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 e==0 ) return;.
1e61a 20 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64 62 db = pTable->db
1e61b 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 ;.. /* Do not d
1e61c 65 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65 20 elete the table
1e61d 75 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72 65 until the refere
1e61e 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 nce count reache
1e61f 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54 61 s zero. */. pTa
1e620 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 ble->nRef--;. i
1e621 66 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e f( pTable->nRef>
1e622 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0 ){. return;
1e623 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1e624 54 61 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 Table->nRef==0 )
1e625 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 ;.. /* Delete a
1e626 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 ll indices assoc
1e627 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 iated with this
1e628 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 table. */. for
1e629 28 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c 65 (pIndex = pTable
1e62a 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 ->pIndex; pIndex
1e62b 3b 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29 7b ; pIndex=pNext){
1e62c 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49 6e . pNext = pIn
1e62d 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 dex->pNext;.
1e62e 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e assert( pIndex->
1e62f 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65 2d pSchema==pTable-
1e630 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 >pSchema );.
1e631 73 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65 sqliteDeleteInde
1e632 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a x(pIndex);. }..
1e633 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1e634 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a MIT_FOREIGN_KEY.
1e635 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 /* Delete all
1e636 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 73 foreign keys ass
1e637 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 ociated with thi
1e638 73 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6b 65 s table. The ke
1e639 79 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 ys. ** should h
1e63a 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e ave already been
1e63b 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74 unlinked from t
1e63c 68 65 20 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 he pSchema->aFKe
1e63d 79 20 68 61 73 68 20 74 61 62 6c 65 20 0a 20 20 y hash table .
1e63e 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 */. for(pFKey=p
1e63f 54 61 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 46 Table->pFKey; pF
1e640 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74 Key; pFKey=pNext
1e641 46 4b 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 74 FKey){. pNext
1e642 46 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 4e FKey = pFKey->pN
1e643 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 61 73 73 extFrom;. ass
1e644 65 72 74 28 20 73 71 6c 69 74 65 33 48 61 73 68 ert( sqlite3Hash
1e645 46 69 6e 64 28 26 70 54 61 62 6c 65 2d 3e 70 53 Find(&pTable->pS
1e646 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 0a 20 20 chema->aFKey,.
1e647 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e648 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e pFKey->
1e649 7a 54 6f 2c 20 73 74 72 6c 65 6e 28 70 46 4b 65 zTo, strlen(pFKe
1e64a 79 2d 3e 7a 54 6f 29 2b 31 29 21 3d 70 46 4b 65 y->zTo)+1)!=pFKe
1e64b 79 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 y );. sqlite3
1e64c 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b 65 79 DbFree(db, pFKey
1e64d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 );. }.#endif..
1e64e 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 54 /* Delete the T
1e64f 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 able structure i
1e650 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 73 71 tself.. */. sq
1e651 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e liteResetColumnN
1e652 61 6d 65 73 28 70 54 61 62 6c 65 29 3b 0a 20 20 ames(pTable);.
1e653 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1e654 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 , pTable->zName)
1e655 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1e656 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 43 e(db, pTable->zC
1e657 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 olAff);. sqlite
1e658 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 3SelectDelete(db
1e659 2c 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 , pTable->pSelec
1e65a 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 t);.#ifndef SQLI
1e65b 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 TE_OMIT_CHECK.
1e65c 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1e65d 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 43 e(db, pTable->pC
1e65e 68 65 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 heck);.#endif.
1e65f 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 sqlite3VtabClear
1e660 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 (pTable);. sqli
1e661 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 te3DbFree(db, pT
1e662 61 62 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 able);.}../*.**
1e663 55 6e 6c 69 6e 6b 20 74 68 65 20 67 69 76 65 6e Unlink the given
1e664 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 table from the
1e665 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64 20 hash tables and
1e666 74 68 65 20 64 65 6c 65 74 65 20 74 68 65 0a 2a the delete the.*
1e667 2a 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 * table structur
1e668 65 20 77 69 74 68 20 61 6c 6c 20 69 74 73 20 69 e with all its i
1e669 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 69 ndices and forei
1e66a 67 6e 20 6b 65 79 73 2e 0a 2a 2f 0a 53 51 4c 49 gn keys..*/.SQLI
1e66b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1e66c 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 sqlite3UnlinkAnd
1e66d 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71 6c 69 DeleteTable(sqli
1e66e 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 te3 *db, int iDb
1e66f 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 , const char *zT
1e670 61 62 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 abName){. Table
1e671 20 2a 70 3b 0a 20 20 46 4b 65 79 20 2a 70 46 31 *p;. FKey *pF1
1e672 2c 20 2a 70 46 32 3b 0a 20 20 44 62 20 2a 70 44 , *pF2;. Db *pD
1e673 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 b;.. assert( db
1e674 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1e675 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 iDb>=0 && iDb<d
1e676 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 b->nDb );. asse
1e677 72 74 28 20 7a 54 61 62 4e 61 6d 65 20 26 26 20 rt( zTabName &&
1e678 7a 54 61 62 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20 zTabName[0] );.
1e679 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b pDb = &db->aDb[
1e67a 69 44 62 5d 3b 0a 20 20 70 20 3d 20 73 71 6c 69 iDb];. p = sqli
1e67b 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 te3HashInsert(&p
1e67c 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c Db->pSchema->tbl
1e67d 48 61 73 68 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 Hash, zTabName,
1e67e 73 74 72 6c 65 6e 28 7a 54 61 62 4e 61 6d 65 29 strlen(zTabName)
1e67f 2b 31 2c 30 29 3b 0a 20 20 69 66 28 20 70 20 29 +1,0);. if( p )
1e680 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
1e681 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 _OMIT_FOREIGN_KE
1e682 59 0a 20 20 20 20 66 6f 72 28 70 46 31 3d 70 2d Y. for(pF1=p-
1e683 3e 70 46 4b 65 79 3b 20 70 46 31 3b 20 70 46 31 >pFKey; pF1; pF1
1e684 3d 70 46 31 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 =pF1->pNextFrom)
1e685 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 20 {. int nTo
1e686 3d 20 73 74 72 6c 65 6e 28 70 46 31 2d 3e 7a 54 = strlen(pF1->zT
1e687 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 46 o) + 1;. pF
1e688 32 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 2 = sqlite3HashF
1e689 69 6e 64 28 26 70 44 62 2d 3e 70 53 63 68 65 6d ind(&pDb->pSchem
1e68a 61 2d 3e 61 46 4b 65 79 2c 20 70 46 31 2d 3e 7a a->aFKey, pF1->z
1e68b 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20 20 20 20 To, nTo);.
1e68c 69 66 28 20 70 46 32 3d 3d 70 46 31 20 29 7b 0a if( pF2==pF1 ){.
1e68d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 48 sqlite3H
1e68e 61 73 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e ashInsert(&pDb->
1e68f 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 pSchema->aFKey,
1e690 70 46 31 2d 3e 7a 54 6f 2c 20 6e 54 6f 2c 20 70 pF1->zTo, nTo, p
1e691 46 31 2d 3e 70 4e 65 78 74 54 6f 29 3b 0a 20 20 F1->pNextTo);.
1e692 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e693 20 20 20 77 68 69 6c 65 28 20 70 46 32 20 26 26 while( pF2 &&
1e694 20 70 46 32 2d 3e 70 4e 65 78 74 54 6f 21 3d 70 pF2->pNextTo!=p
1e695 46 31 20 29 7b 20 70 46 32 3d 70 46 32 2d 3e 70 F1 ){ pF2=pF2->p
1e696 4e 65 78 74 54 6f 3b 20 7d 0a 20 20 20 20 20 20 NextTo; }.
1e697 20 20 69 66 28 20 70 46 32 20 29 7b 0a 20 20 20 if( pF2 ){.
1e698 20 20 20 20 20 20 20 70 46 32 2d 3e 70 4e 65 78 pF2->pNex
1e699 74 54 6f 20 3d 20 70 46 31 2d 3e 70 4e 65 78 74 tTo = pF1->pNext
1e69a 54 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 To;. }.
1e69b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 }. }.#end
1e69c 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 if. sqlite3De
1e69d 6c 65 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20 leteTable(p);.
1e69e 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d }. db->flags |=
1e69f 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 SQLITE_InternCh
1e6a0 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 anges;.}../*.**
1e6a1 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 Given a token, r
1e6a2 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 20 74 eturn a string t
1e6a3 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 hat consists of
1e6a4 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 61 74 the text of that
1e6a5 0a 2a 2a 20 74 6f 6b 65 6e 20 77 69 74 68 20 61 .** token with a
1e6a6 6e 79 20 71 75 6f 74 61 74 69 6f 6e 73 20 72 65 ny quotations re
1e6a7 6d 6f 76 65 64 2e 20 20 53 70 61 63 65 20 74 6f moved. Space to
1e6a8 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e hold the return
1e6a9 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 ed string.** is
1e6aa 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
1e6ab 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 liteMalloc() and
1e6ac 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62 must be freed b
1e6ad 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a y the calling.**
1e6ae 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
1e6af 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f 66 74 65 Tokens are ofte
1e6b0 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65 72 73 20 n just pointers
1e6b1 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 into the origina
1e6b2 6c 20 53 51 4c 20 74 65 78 74 20 61 6e 64 20 73 l SQL text and s
1e6b3 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 5c 30 30 o.** are not \00
1e6b4 30 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 0 terminated and
1e6b5 20 61 72 65 20 6e 6f 74 20 70 65 72 73 69 73 74 are not persist
1e6b6 65 6e 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e ent. The return
1e6b7 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 ed string.** is
1e6b8 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20 \000 terminated
1e6b9 61 6e 64 20 69 73 20 70 65 72 73 69 73 74 65 6e and is persisten
1e6ba 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
1e6bb 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
1e6bc 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
1e6bd 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b sqlite3 *db, Tok
1e6be 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68 en *pName){. ch
1e6bf 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 ar *zName;. if(
1e6c0 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e pName ){. zN
1e6c1 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ame = sqlite3DbS
1e6c2 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 trNDup(db, (char
1e6c3 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d *)pName->z, pNam
1e6c4 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 e->n);. sqlit
1e6c5 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29 e3Dequote(zName)
1e6c6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a ;. }else{. z
1e6c7 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Name = 0;. }.
1e6c8 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a return zName;.}.
1e6c9 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 ./*.** Open the
1e6ca 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
1e6cb 62 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 ble stored in da
1e6cc 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 69 44 tabase number iD
1e6cd 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 b for.** writing
1e6ce 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6f . The table is o
1e6cf 70 65 6e 65 64 20 75 73 69 6e 67 20 63 75 72 73 pened using curs
1e6d0 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f or 0..*/.SQLITE_
1e6d1 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1e6d2 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 ite3OpenMasterTa
1e6d3 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69 6e ble(Parse *p, in
1e6d4 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a t iDb){. Vdbe *
1e6d5 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
1e6d6 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 be(p);. sqlite3
1e6d7 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 62 TableLock(p, iDb
1e6d8 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 , MASTER_ROOT, 1
1e6d9 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 , SCHEMA_TABLE(i
1e6da 44 62 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 Db));. sqlite3V
1e6db 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1e6dc 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 SetNumColumns, 0
1e6dd 2c 20 35 29 3b 2f 2a 20 73 71 6c 69 74 65 5f 6d , 5);/* sqlite_m
1e6de 61 73 74 65 72 20 68 61 73 20 35 20 63 6f 6c 75 aster has 5 colu
1e6df 6d 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 mns */. sqlite3
1e6e0 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1e6e1 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 4d _OpenWrite, 0, M
1e6e2 41 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44 62 29 ASTER_ROOT, iDb)
1e6e3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 ;.}../*.** The t
1e6e4 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 oken *pName cont
1e6e5 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 ains the name of
1e6e6 20 61 20 64 61 74 61 62 61 73 65 20 28 65 69 74 a database (eit
1e6e7 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a her "main" or.**
1e6e8 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e "temp" or the n
1e6e9 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68 ame of an attach
1e6ea 65 64 20 64 62 29 2e 20 54 68 69 73 20 72 6f 75 ed db). This rou
1e6eb 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
1e6ec 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65 .** index of the
1e6ed 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 named database
1e6ee 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 in db->aDb[], or
1e6ef 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65 64 -1 if the named
1e6f0 20 64 62 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 db .** does not
1e6f1 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 exist..*/.SQLIT
1e6f2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1e6f3 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69 lite3FindDb(sqli
1e6f4 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a te3 *db, Token *
1e6f5 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20 pName){. int i
1e6f6 3d 20 2d 31 3b 20 20 20 20 2f 2a 20 44 61 74 61 = -1; /* Data
1e6f7 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 base number */.
1e6f8 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 int n;
1e6f9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 /* Number of cha
1e6fa 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 6e racters in the n
1e6fb 61 6d 65 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 ame */. Db *pDb
1e6fc 3b 20 20 20 20 20 20 20 2f 2a 20 41 20 64 61 74 ; /* A dat
1e6fd 61 62 61 73 65 20 77 68 6f 73 65 20 6e 61 6d 65 abase whose name
1e6fe 20 73 70 61 63 65 20 69 73 20 62 65 69 6e 67 20 space is being
1e6ff 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 68 searched */. ch
1e700 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 ar *zName; /*
1e701 4e 61 6d 65 20 77 65 20 61 72 65 20 73 65 61 72 Name we are sear
1e702 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 0a 20 20 ching for */..
1e703 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e zName = sqlite3N
1e704 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
1e705 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a pName);. if( z
1e706 4e 61 6d 65 20 29 7b 0a 20 20 20 20 6e 20 3d 20 Name ){. n =
1e707 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 strlen(zName);.
1e708 20 20 20 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 for(i=(db->nD
1e709 62 2d 31 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 b-1), pDb=&db->a
1e70a 44 62 5b 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d Db[i]; i>=0; i--
1e70b 2c 20 70 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 , pDb--){.
1e70c 69 66 28 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44 if( (!OMIT_TEMPD
1e70d 42 20 7c 7c 20 69 21 3d 31 20 29 20 26 26 20 6e B || i!=1 ) && n
1e70e 3d 3d 73 74 72 6c 65 6e 28 70 44 62 2d 3e 7a 4e ==strlen(pDb->zN
1e70f 61 6d 65 29 20 26 26 20 0a 20 20 20 20 20 20 20 ame) && .
1e710 20 20 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 0==sqlite3Str
1e711 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c ICmp(pDb->zName,
1e712 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 zName) ){.
1e713 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1e714 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 }. }. sqli
1e715 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e te3DbFree(db, zN
1e716 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ame);. }. retu
1e717 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 rn i;.}../* The
1e718 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6f 72 table or view or
1e719 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73 trigger name is
1e71a 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 passed to this
1e71b 72 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 routine via toke
1e71c 6e 73 0a 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 ns.** pName1 and
1e71d 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 pName2. If the
1e71e 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 table name was f
1e71f 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 ully qualified,
1e720 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a for example:.**.
1e721 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 ** CREATE TABLE
1e722 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a xxx.yyy (...);.*
1e723 2a 20 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 * .** Then pName
1e724 31 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 1 is set to "xxx
1e725 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 " and pName2 "yy
1e726 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 y". On the other
1e727 20 68 61 6e 64 20 69 66 0a 2a 2a 20 74 68 65 20 hand if.** the
1e728 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f table name is no
1e729 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 t fully qualifie
1e72a 64 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 d, i.e.:.**.** C
1e72b 52 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 REATE TABLE yyy(
1e72c 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e ...);.**.** Then
1e72d 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 pName1 is set t
1e72e 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d o "yyy" and pNam
1e72f 65 32 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 e2 is ""..**.**
1e730 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 This routine set
1e731 73 20 74 68 65 20 2a 70 70 55 6e 71 75 61 6c 20 s the *ppUnqual
1e732 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 pointer to point
1e733 20 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 at the token (p
1e734 4e 61 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d Name1 or.** pNam
1e735 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 e2) that stores
1e736 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 the unqualified
1e737 74 61 62 6c 65 20 6e 61 6d 65 2e 20 20 54 68 65 table name. The
1e738 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a index of the.**
1e739 20 64 61 74 61 62 61 73 65 20 22 78 78 78 22 20 database "xxx"
1e73a 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
1e73b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1e73c 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 nt sqlite3TwoPar
1e73d 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a tName(. Parse *
1e73e 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 pParse, /*
1e73f 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 Parsing and code
1e740 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 generating cont
1e741 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a ext */. Token *
1e742 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 pName1, /*
1e743 54 68 65 20 22 78 78 78 22 20 69 6e 20 74 68 65 The "xxx" in the
1e744 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 name "xxx.yyy"
1e745 6f 72 20 22 78 78 78 22 20 2a 2f 0a 20 20 54 6f or "xxx" */. To
1e746 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 ken *pName2,
1e747 20 20 2f 2a 20 54 68 65 20 22 79 79 79 22 20 69 /* The "yyy" i
1e748 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 2e n the name "xxx.
1e749 79 79 79 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 yyy" */. Token
1e74a 2a 2a 70 55 6e 71 75 61 6c 20 20 20 20 20 2f 2a **pUnqual /*
1e74b 20 57 72 69 74 65 20 74 68 65 20 75 6e 71 75 61 Write the unqua
1e74c 6c 69 66 69 65 64 20 6f 62 6a 65 63 74 20 6e 61 lified object na
1e74d 6d 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 me here */.){.
1e74e 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
1e74f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
1e750 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 atabase holding
1e751 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 the object */.
1e752 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1e753 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 arse->db;.. if(
1e754 20 70 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d 65 pName2 && pName
1e755 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73 2->n>0 ){. as
1e756 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e sert( !db->init.
1e757 62 75 73 79 20 29 3b 0a 20 20 20 20 2a 70 55 6e busy );. *pUn
1e758 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 qual = pName2;.
1e759 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 iDb = sqlite3
1e75a 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 FindDb(db, pName
1e75b 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 1);. if( iDb<
1e75c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1e75d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1e75e 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 e, "unknown data
1e75f 62 61 73 65 20 25 54 22 2c 20 70 4e 61 6d 65 31 base %T", pName1
1e760 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d );. pParse-
1e761 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 >nErr++;. r
1e762 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a eturn -1;. }.
1e763 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
1e764 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 ert( db->init.iD
1e765 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 b==0 || db->init
1e766 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 69 44 62 .busy );. iDb
1e767 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b = db->init.iDb;
1e768 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 . *pUnqual =
1e769 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65 pName1;. }. re
1e76a 74 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a turn iDb;.}../*.
1e76b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1e76c 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b is used to check
1e76d 20 69 66 20 74 68 65 20 55 54 46 2d 38 20 73 74 if the UTF-8 st
1e76e 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20 61 20 ring zName is a
1e76f 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 legal.** unquali
1e770 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20 61 20 fied name for a
1e771 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 new schema objec
1e772 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c t (table, index,
1e773 20 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67 view or.** trig
1e774 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 ger). All names
1e775 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65 70 74 are legal except
1e776 20 74 68 6f 73 65 20 74 68 61 74 20 62 65 67 69 those that begi
1e777 6e 20 77 69 74 68 20 74 68 65 20 73 74 72 69 6e n with the strin
1e778 67 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22 20 28 g.** "sqlite_" (
1e779 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65 72 20 in upper, lower
1e77a 6f 72 20 6d 69 78 65 64 20 63 61 73 65 29 2e 20 or mixed case).
1e77b 54 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20 This portion of
1e77c 74 68 65 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a the namespace.**
1e77d 20 69 73 20 72 65 73 65 72 76 65 64 20 66 6f 72 is reserved for
1e77e 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a internal use..*
1e77f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1e780 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 int sqlite3Chec
1e781 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 kObjectName(Pars
1e782 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
1e783 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 char *zName){.
1e784 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64 62 if( !pParse->db
1e785 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70 ->init.busy && p
1e786 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 Parse->nested==0
1e787 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28 . && (
1e788 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 pParse->db->flag
1e789 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65 s & SQLITE_Write
1e78a 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 Schema)==0.
1e78b 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 && 0==sqlit
1e78c 65 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 e3StrNICmp(zName
1e78d 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 , "sqlite_", 7)
1e78e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1e78f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1e790 6f 62 6a 65 63 74 20 6e 61 6d 65 20 72 65 73 65 object name rese
1e791 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 rved for interna
1e792 6c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d l use: %s", zNam
1e793 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 e);. return S
1e794 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
1e795 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1e796 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 _OK;.}../*.** Be
1e797 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 gin constructing
1e798 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72 65 70 a new table rep
1e799 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d resentation in m
1e79a 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 0a emory. This is.
1e79b 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f 66 20 ** the first of
1e79c 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e 20 72 several action r
1e79d 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67 65 74 outines that get
1e79e 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f called in respo
1e79f 6e 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41 nse.** to a CREA
1e7a0 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 TE TABLE stateme
1e7a1 6e 74 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c nt. In particul
1e7a2 61 72 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ar, this routine
1e7a3 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 is called.** af
1e7a4 74 65 72 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e ter seeing token
1e7a5 73 20 22 43 52 45 41 54 45 22 20 61 6e 64 20 22 s "CREATE" and "
1e7a6 54 41 42 4c 45 22 20 61 6e 64 20 74 68 65 20 74 TABLE" and the t
1e7a7 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 69 able name. The i
1e7a8 73 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73 sTemp.** flag is
1e7a9 20 74 72 75 65 20 69 66 20 74 68 65 20 74 61 62 true if the tab
1e7aa 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f le should be sto
1e7ab 72 65 64 20 69 6e 20 74 68 65 20 61 75 78 69 6c red in the auxil
1e7ac 69 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a iary database.**
1e7ad 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 file instead of
1e7ae 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 in the main dat
1e7af 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 abase file. Thi
1e7b0 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 s is normally th
1e7b1 65 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74 e case.** when t
1e7b2 68 65 20 22 54 45 4d 50 22 20 6f 72 20 22 54 45 he "TEMP" or "TE
1e7b3 4d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f 72 64 MPORARY" keyword
1e7b4 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65 occurs in betwe
1e7b5 65 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61 6e 64 en.** CREATE and
1e7b6 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 TABLE..**.** Th
1e7b7 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f e new table reco
1e7b8 72 64 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 rd is initialize
1e7b9 64 20 61 6e 64 20 70 75 74 20 69 6e 20 70 50 61 d and put in pPa
1e7ba 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a rse->pNewTable..
1e7bb 2a 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20 74 68 ** As more of th
1e7bc 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 e CREATE TABLE s
1e7bd 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73 tatement is pars
1e7be 65 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 ed, additional a
1e7bf 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 ction.** routine
1e7c0 73 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 s will be called
1e7c1 20 74 6f 20 61 64 64 20 6d 6f 72 65 20 69 6e 66 to add more inf
1e7c2 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 69 73 ormation to this
1e7c3 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 record..** At t
1e7c4 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52 he end of the CR
1e7c5 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
1e7c6 6d 65 6e 74 2c 20 74 68 65 20 73 71 6c 69 74 65 ment, the sqlite
1e7c7 33 45 6e 64 54 61 62 6c 65 28 29 20 72 6f 75 74 3EndTable() rout
1e7c8 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 ine.** is called
1e7c9 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 to complete the
1e7ca 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 construction of
1e7cb 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 the new table r
1e7cc 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ecord..*/.SQLITE
1e7cd 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1e7ce 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 lite3StartTable(
1e7cf 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1e7d0 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f , /* Parser co
1e7d1 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e ntext */. Token
1e7d2 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46 *pName1, /* F
1e7d3 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 irst part of the
1e7d4 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
1e7d5 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 le or view */.
1e7d6 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 Token *pName2,
1e7d7 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20 /* Second part
1e7d8 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 of the name of t
1e7d9 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 he table or view
1e7da 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 */. int isTemp
1e7db 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 , /* True i
1e7dc 66 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 f this is a TEMP
1e7dd 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
1e7de 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f 2a 20 isView, /*
1e7df 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 True if this is
1e7e0 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20 a VIEW */. int
1e7e1 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20 isVirtual, /*
1e7e2 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 True if this is
1e7e3 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c 65 20 a VIRTUAL table
1e7e4 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 */. int noErr
1e7e5 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 /* Do noth
1e7e6 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61 6c 72 ing if table alr
1e7e7 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 eady exists */.)
1e7e8 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c {. Table *pTabl
1e7e9 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 e;. char *zName
1e7ea 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d = 0; /* The nam
1e7eb 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 e of the new tab
1e7ec 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 le */. sqlite3
1e7ed 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1e7ee 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 ;. Vdbe *v;. i
1e7ef 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
1e7f0 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 /* Database numb
1e7f1 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 er to create the
1e7f2 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 table in */. T
1e7f3 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 oken *pName;
1e7f4 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e /* Unqualified n
1e7f5 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1e7f6 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20 to create */..
1e7f7 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 /* The table or
1e7f8 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20 63 72 view name to cr
1e7f9 65 61 74 65 20 69 73 20 70 61 73 73 65 64 20 74 eate is passed t
1e7fa 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 o this routine v
1e7fb 69 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70 ia tokens. ** p
1e7fc 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 Name1 and pName2
1e7fd 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e . If the table n
1e7fe 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75 ame was fully qu
1e7ff 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61 alified, for exa
1e800 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 mple:. **. **
1e801 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78 CREATE TABLE xxx
1e802 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a .yyy (...);. **
1e803 20 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d . ** Then pNam
1e804 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 e1 is set to "xx
1e805 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 x" and pName2 "y
1e806 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 yy". On the othe
1e807 72 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a 20 74 r hand if. ** t
1e808 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 he table name is
1e809 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 not fully quali
1e80a 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a fied, i.e.:. **
1e80b 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 . ** CREATE TAB
1e80c 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a LE yyy(...);. *
1e80d 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d *. ** Then pNam
1e80e 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79 79 e1 is set to "yy
1e80f 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 y" and pName2 is
1e810 20 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 "".. **. ** T
1e811 68 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20 73 65 he call below se
1e812 74 73 20 74 68 65 20 70 4e 61 6d 65 20 70 6f 69 ts the pName poi
1e813 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 nter to point at
1e814 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d the token (pNam
1e815 65 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d 65 e1 or. ** pName
1e816 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 2) that stores t
1e817 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 he unqualified t
1e818 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 76 able name. The v
1e819 61 72 69 61 62 6c 65 20 69 44 62 20 69 73 0a 20 ariable iDb is.
1e81a 20 2a 2a 20 73 65 74 20 74 6f 20 74 68 65 20 69 ** set to the i
1e81b 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 ndex of the data
1e81c 62 61 73 65 20 74 68 61 74 20 74 68 65 20 74 61 base that the ta
1e81d 62 6c 65 20 6f 72 20 76 69 65 77 20 69 73 20 74 ble or view is t
1e81e 6f 20 62 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 o be. ** create
1e81f 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44 62 d in.. */. iDb
1e820 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 = sqlite3TwoPar
1e821 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e tName(pParse, pN
1e822 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 ame1, pName2, &p
1e823 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62 Name);. if( iDb
1e824 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 <0 ) return;. i
1e825 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 f( !OMIT_TEMPDB
1e826 26 26 20 69 73 54 65 6d 70 20 26 26 20 69 44 62 && isTemp && iDb
1e827 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 >1 ){. /* If
1e828 63 72 65 61 74 69 6e 67 20 61 20 74 65 6d 70 20 creating a temp
1e829 74 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20 table, the name
1e82a 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 may not be quali
1e82b 66 69 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 fied */. sqli
1e82c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1e82d 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74 se, "temporary t
1e82e 61 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 able name must b
1e82f 65 20 75 6e 71 75 61 6c 69 66 69 65 64 22 29 3b e unqualified");
1e830 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1e831 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d . if( !OMIT_TEM
1e832 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 20 PDB && isTemp )
1e833 69 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72 iDb = 1;.. pPar
1e834 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d se->sNameToken =
1e835 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 *pName;. zName
1e836 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
1e837 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d omToken(db, pNam
1e838 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d e);. if( zName=
1e839 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 =0 ) return;. i
1e83a 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 f( SQLITE_OK!=sq
1e83b 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 lite3CheckObject
1e83c 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 Name(pParse, zNa
1e83d 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 me) ){. goto
1e83e 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f begin_table_erro
1e83f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d r;. }. if( db-
1e840 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29 20 69 >init.iDb==1 ) i
1e841 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64 sTemp = 1;.#ifnd
1e842 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1e843 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 UTHORIZATION. a
1e844 73 73 65 72 74 28 20 28 69 73 54 65 6d 70 20 26 ssert( (isTemp &
1e845 20 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20 1)==isTemp );.
1e846 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b {. int code;
1e847 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d . char *zDb =
1e848 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e db->aDb[iDb].zN
1e849 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c ame;. if( sql
1e84a 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
1e84b 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 arse, SQLITE_INS
1e84c 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c ERT, SCHEMA_TABL
1e84d 45 28 69 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44 E(isTemp), 0, zD
1e84e 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f b) ){. goto
1e84f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
1e850 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 or;. }. if
1e851 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 ( isView ){.
1e852 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 if( !OMIT_TEMP
1e853 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a DB && isTemp ){.
1e854 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 code = S
1e855 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d QLITE_CREATE_TEM
1e856 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 P_VIEW;. }e
1e857 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 lse{. cod
1e858 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 e = SQLITE_CREAT
1e859 45 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a E_VIEW;. }.
1e85a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e85b 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 if( !OMIT_TEMPD
1e85c 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 B && isTemp ){.
1e85d 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 code = SQ
1e85e 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 LITE_CREATE_TEMP
1e85f 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 _TABLE;. }e
1e860 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 lse{. cod
1e861 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 e = SQLITE_CREAT
1e862 45 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d E_TABLE;. }
1e863 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 . }. if( !
1e864 69 73 56 69 72 74 75 61 6c 20 26 26 20 73 71 6c isVirtual && sql
1e865 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
1e866 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d arse, code, zNam
1e867 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 e, 0, zDb) ){.
1e868 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 goto begin_t
1e869 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 able_error;.
1e86a 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }. }.#endif..
1e86b 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
1e86c 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 new table name
1e86d 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 does not collide
1e86e 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e with an existin
1e86f 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 g. ** index or
1e870 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 table name in th
1e871 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e e same database.
1e872 20 20 49 73 73 75 65 20 61 6e 20 65 72 72 6f 72 Issue an error
1e873 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20 2a 2a message if. **
1e874 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20 65 78 it does. The ex
1e875 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20 74 68 ception is if th
1e876 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e e statement bein
1e877 67 20 70 61 72 73 65 64 20 77 61 73 20 70 61 73 g parsed was pas
1e878 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73 sed. ** to an s
1e879 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 qlite3_declare_v
1e87a 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74 tab() call. In t
1e87b 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20 74 68 hat case only th
1e87c 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 e column names.
1e87d 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20 77 69 ** and types wi
1e87e 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f 20 74 ll be used, so t
1e87f 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
1e880 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61 6d 65 to test for name
1e881 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 space. ** colli
1e882 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 sions.. */. if
1e883 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 ( !IN_DECLARE_VT
1e884 41 42 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51 AB ){. if( SQ
1e885 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 LITE_OK!=sqlite3
1e886 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 ReadSchema(pPars
1e887 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f e) ){. goto
1e888 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
1e889 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 or;. }. pT
1e88a 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 46 69 able = sqlite3Fi
1e88b 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d ndTable(db, zNam
1e88c 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e e, db->aDb[iDb].
1e88d 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 zName);. if(
1e88e 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 pTable ){.
1e88f 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 if( !noErr ){.
1e890 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1e891 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 orMsg(pParse, "t
1e892 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64 79 20 able %T already
1e893 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b exists", pName);
1e894 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 . }. g
1e895 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f oto begin_table_
1e896 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 error;. }.
1e897 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 if( sqlite3Find
1e898 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c Index(db, zName,
1e899 20 30 29 21 3d 30 20 26 26 20 28 69 44 62 3d 3d 0)!=0 && (iDb==
1e89a 30 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62 0 || !db->init.b
1e89b 75 73 79 29 20 29 7b 0a 20 20 20 20 20 20 73 71 usy) ){. sq
1e89c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1e89d 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 73 20 arse, "there is
1e89e 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 65 78 already an index
1e89f 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d named %s", zNam
1e8a0 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 e);. goto b
1e8a1 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 egin_table_error
1e8a2 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 ;. }. }.. p
1e8a3 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 Table = sqlite3D
1e8a4 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1e8a5 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a sizeof(Table));.
1e8a6 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 if( pTable==0
1e8a7 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f ){. db->mallo
1e8a8 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
1e8a9 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 pParse->rc = SQ
1e8aa 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
1e8ab 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a pParse->nErr++;.
1e8ac 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 goto begin_t
1e8ad 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a able_error;. }.
1e8ae 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 pTable->zName
1e8af 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c = zName;. pTabl
1e8b0 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 e->iPKey = -1;.
1e8b1 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 pTable->pSchema
1e8b2 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
1e8b3 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c pSchema;. pTabl
1e8b4 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 e->nRef = 1;. p
1e8b5 54 61 62 6c 65 2d 3e 64 62 20 3d 20 64 62 3b 0a Table->db = db;.
1e8b6 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 4e if( pParse->pN
1e8b7 65 77 54 61 62 6c 65 20 29 20 73 71 6c 69 74 65 ewTable ) sqlite
1e8b8 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 50 61 3DeleteTable(pPa
1e8b9 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b rse->pNewTable);
1e8ba 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 . pParse->pNewT
1e8bb 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b 0a 0a able = pTable;..
1e8bc 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
1e8bd 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69 74 65 the magic sqlite
1e8be 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 _sequence table
1e8bf 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e 63 72 used by autoincr
1e8c0 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 65 6e ement,. ** then
1e8c1 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 65 record a pointe
1e8c2 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 r to this table
1e8c3 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 in the main data
1e8c4 62 61 73 65 20 73 74 72 75 63 74 75 72 65 0a 20 base structure.
1e8c5 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e 53 45 ** so that INSE
1e8c6 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68 65 20 RT can find the
1e8c7 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a 20 20 table easily..
1e8c8 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1e8c9 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 E_OMIT_AUTOINCRE
1e8ca 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50 61 72 MENT. if( !pPar
1e8cb 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 73 74 se->nested && st
1e8cc 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c rcmp(zName, "sql
1e8cd 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29 3d 3d ite_sequence")==
1e8ce 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c 65 2d 0 ){. pTable-
1e8cf 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 >pSchema->pSeqTa
1e8d0 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 7d 0a b = pTable;. }.
1e8d1 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 #endif.. /* Beg
1e8d2 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68 in generating th
1e8d3 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c e code that will
1e8d4 20 69 6e 73 65 72 74 20 74 68 65 20 74 61 62 6c insert the tabl
1e8d5 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a 20 20 e record into.
1e8d6 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 ** the SQLITE_MA
1e8d7 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e 6f 74 STER table. Not
1e8d8 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 e in particular
1e8d9 74 68 61 74 20 77 65 20 6d 75 73 74 20 67 6f 20 that we must go
1e8da 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64 20 61 ahead. ** and a
1e8db 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65 63 6f llocate the reco
1e8dc 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 rd number for th
1e8dd 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f e table entry no
1e8de 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79 0a 20 w. Before any.
1e8df 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 ** PRIMARY KEY
1e8e0 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77 6f 72 or UNIQUE keywor
1e8e1 64 73 20 61 72 65 20 70 61 72 73 65 64 2e 20 20 ds are parsed.
1e8e2 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 77 Those keywords w
1e8e3 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a 20 69 ill cause. ** i
1e8e4 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 72 65 ndices to be cre
1e8e5 61 74 65 64 20 61 6e 64 20 74 68 65 20 74 61 62 ated and the tab
1e8e6 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74 20 63 le record must c
1e8e7 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65 20 0a ome before the .
1e8e8 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 20 48 ** indices. H
1e8e9 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f 72 64 ence, the record
1e8ea 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
1e8eb 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6c table must be al
1e8ec 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 77 located. ** now
1e8ed 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 .. */. if( !db
1e8ee 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 28 ->init.busy && (
1e8ef 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
1e8f0 62 65 28 70 50 61 72 73 65 29 29 21 3d 30 20 29 be(pParse))!=0 )
1e8f1 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 {. int j1;.
1e8f2 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d 61 74 int fileFormat
1e8f3 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 31 2c 20 ;. int reg1,
1e8f4 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 20 20 20 reg2, reg3;.
1e8f5 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 sqlite3BeginWrit
1e8f6 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 eOperation(pPars
1e8f7 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66 e, 0, iDb);..#if
1e8f8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1e8f9 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1e8fa 20 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 20 if( isVirtual
1e8fb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1e8fc 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 VdbeAddOp0(v, OP
1e8fd 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a _VBegin);. }.
1e8fe 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 #endif.. /* I
1e8ff 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 f the file forma
1e900 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69 t and encoding i
1e901 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 n the database h
1e902 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 ave not been set
1e903 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68 , . ** set th
1e904 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 em now.. */.
1e905 20 20 20 72 65 67 31 20 3d 20 70 50 61 72 73 65 reg1 = pParse
1e906 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70 ->regRowid = ++p
1e907 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
1e908 20 72 65 67 32 20 3d 20 70 50 61 72 73 65 2d 3e reg2 = pParse->
1e909 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50 61 72 regRoot = ++pPar
1e90a 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 se->nMem;. re
1e90b 67 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e g3 = ++pParse->n
1e90c 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Mem;. sqlite3
1e90d 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1e90e 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 _ReadCookie, iDb
1e90f 2c 20 72 65 67 33 2c 20 31 29 3b 20 20 20 2f 2a , reg3, 1); /*
1e910 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 2a 2f 0a file_format */.
1e911 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 sqlite3VdbeU
1e912 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 sesBtree(v, iDb)
1e913 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 ;. j1 = sqlit
1e914 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
1e915 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b 0a 20 20 OP_If, reg3);.
1e916 20 20 66 69 6c 65 46 6f 72 6d 61 74 20 3d 20 28 fileFormat = (
1e917 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
1e918 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 TE_LegacyFileFmt
1e919 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20 20 20 )!=0 ?.
1e91a 20 20 20 20 20 20 20 20 20 31 20 3a 20 53 51 4c 1 : SQL
1e91b 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 ITE_MAX_FILE_FOR
1e91c 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 MAT;. sqlite3
1e91d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1e91e 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c 65 46 6f _Integer, fileFo
1e91f 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a 20 20 20 rmat, reg3);.
1e920 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1e921 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b p3(v, OP_SetCook
1e922 69 65 2c 20 69 44 62 2c 20 31 2c 20 72 65 67 33 ie, iDb, 1, reg3
1e923 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1e924 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
1e925 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 62 29 2c nteger, ENC(db),
1e926 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 reg3);. sqli
1e927 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
1e928 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 OP_SetCookie, i
1e929 44 62 2c 20 34 2c 20 72 65 67 33 29 3b 0a 20 20 Db, 4, reg3);.
1e92a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1e92b 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 pHere(v, j1);..
1e92c 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73 74 20 /* This just
1e92d 63 72 65 61 74 65 73 20 61 20 70 6c 61 63 65 2d creates a place-
1e92e 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20 69 6e holder record in
1e92f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 the sqlite_mast
1e930 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a er table.. **
1e931 20 54 68 65 20 72 65 63 6f 72 64 20 63 72 65 61 The record crea
1e932 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e ted does not con
1e933 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20 79 65 tain anything ye
1e934 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 72 t. It will be r
1e935 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a 20 62 eplaced. ** b
1e936 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74 72 79 y the real entry
1e937 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72 61 74 in code generat
1e938 65 64 20 61 74 20 73 71 6c 69 74 65 33 45 6e 64 ed at sqlite3End
1e939 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2a 0a Table().. **.
1e93a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 64 ** The rowid
1e93b 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65 6e 74 for the new ent
1e93c 72 79 20 69 73 20 6c 65 66 74 20 6f 6e 20 74 68 ry is left on th
1e93d 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 e top of the sta
1e93e 63 6b 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 ck.. ** The r
1e93f 6f 77 69 64 20 76 61 6c 75 65 20 69 73 20 6e 65 owid value is ne
1e940 65 64 65 64 20 62 79 20 74 68 65 20 63 6f 64 65 eded by the code
1e941 20 74 68 61 74 20 73 71 6c 69 74 65 33 45 6e 64 that sqlite3End
1e942 54 61 62 6c 65 20 77 69 6c 6c 0a 20 20 20 20 2a Table will. *
1e943 2a 20 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 20 * generate..
1e944 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
1e945 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
1e946 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 ) || !defined(SQ
1e947 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1e948 4c 54 41 42 4c 45 29 0a 20 20 20 20 69 66 28 20 LTABLE). if(
1e949 69 73 56 69 65 77 20 7c 7c 20 69 73 56 69 72 74 isView || isVirt
1e94a 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ual ){. sql
1e94b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1e94c 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c , OP_Integer, 0,
1e94d 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c 73 reg2);. }els
1e94e 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 e.#endif. {.
1e94f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1e950 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 72 65 AddOp2(v, OP_Cre
1e951 61 74 65 54 61 62 6c 65 2c 20 69 44 62 2c 20 72 ateTable, iDb, r
1e952 65 67 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 eg2);. }.
1e953 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 sqlite3OpenMaste
1e954 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 rTable(pParse, i
1e955 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Db);. sqlite3
1e956 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1e957 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20 72 65 _NewRowid, 0, re
1e958 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 g1);. sqlite3
1e959 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1e95a 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 33 29 3b _Null, 0, reg3);
1e95b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1e95c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 AddOp3(v, OP_Ins
1e95d 65 72 74 2c 20 30 2c 20 72 65 67 33 2c 20 72 65 ert, 0, reg3, re
1e95e 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 g1);. sqlite3
1e95f 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 VdbeChangeP5(v,
1e960 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a OPFLAG_APPEND);.
1e961 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1e962 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 ddOp0(v, OP_Clos
1e963 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f e);. }.. /* No
1e964 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72 29 rmal (non-error)
1e965 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 72 65 return. */. re
1e966 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 turn;.. /* If a
1e967 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
1e968 77 65 20 6a 75 6d 70 20 68 65 72 65 20 2a 2f 0a we jump here */.
1e969 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f begin_table_erro
1e96a 72 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 r:. sqlite3DbFr
1e96b 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 ee(db, zName);.
1e96c 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a return;.}../*.*
1e96d 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 * This macro is
1e96e 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 used to compare
1e96f 74 77 6f 20 73 74 72 69 6e 67 73 20 69 6e 20 61 two strings in a
1e970 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 case-insensitiv
1e971 65 20 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49 74 20 e manner..** It
1e972 69 73 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 is slightly fast
1e973 65 72 20 74 68 61 6e 20 63 61 6c 6c 69 6e 67 20 er than calling
1e974 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 29 sqlite3StrICmp()
1e975 20 64 69 72 65 63 74 6c 79 2c 20 62 75 74 0a 2a directly, but.*
1e976 2a 20 70 72 6f 64 75 63 65 73 20 6c 61 72 67 65 * produces large
1e977 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 41 r code..**.** WA
1e978 52 4e 49 4e 47 3a 20 54 68 69 73 20 6d 61 63 72 RNING: This macr
1e979 6f 20 69 73 20 6e 6f 74 20 63 6f 6d 70 61 74 69 o is not compati
1e97a 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 74 72 ble with the str
1e97b 63 6d 70 28 29 20 66 61 6d 69 6c 79 2e 20 49 74 cmp() family. It
1e97c 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 72 75 65 .** returns true
1e97d 20 69 66 20 74 68 65 20 74 77 6f 20 73 74 72 69 if the two stri
1e97e 6e 67 73 20 61 72 65 20 65 71 75 61 6c 2c 20 6f ngs are equal, o
1e97f 74 68 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0a therwise false..
1e980 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54 52 49 43 */.#define STRIC
1e981 4d 50 28 78 2c 20 79 29 20 28 5c 0a 73 71 6c 69 MP(x, y) (\.sqli
1e982 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b te3UpperToLower[
1e983 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 *(unsigned char
1e984 2a 29 28 78 29 5d 3d 3d 20 20 20 5c 0a 73 71 6c *)(x)]== \.sql
1e985 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 ite3UpperToLower
1e986 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 [*(unsigned char
1e987 20 2a 29 28 79 29 5d 20 20 20 20 20 5c 0a 26 26 *)(y)] \.&&
1e988 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1e989 28 78 29 2b 31 2c 28 79 29 2b 31 29 3d 3d 30 20 (x)+1,(y)+1)==0
1e98a 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e )../*.** Add a n
1e98b 65 77 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 ew column to the
1e98c 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 table currently
1e98d 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 being construct
1e98e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 ed..**.** The pa
1e98f 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 rser calls this
1e990 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20 66 6f 72 routine once for
1e991 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 64 65 63 each column dec
1e992 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 laration.** in a
1e993 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
1e994 61 74 65 6d 65 6e 74 2e 20 20 73 71 6c 69 74 65 atement. sqlite
1e995 33 53 74 61 72 74 54 61 62 6c 65 28 29 20 67 65 3StartTable() ge
1e996 74 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 69 72 ts called.** fir
1e997 73 74 20 74 6f 20 67 65 74 20 74 68 69 6e 67 73 st to get things
1e998 20 67 6f 69 6e 67 2e 20 20 54 68 65 6e 20 74 68 going. Then th
1e999 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
1e99a 6c 6c 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a lled for each.**
1e99b 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 column..*/.SQLI
1e99c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1e99d 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e sqlite3AddColumn
1e99e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1e99f 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 Token *pName){.
1e9a0 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 Table *p;. int
1e9a1 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 i;. char *z;.
1e9a2 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 Column *pCol;.
1e9a3 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1e9a4 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 Parse->db;. if(
1e9a5 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e (p = pParse->pN
1e9a6 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 ewTable)==0 ) re
1e9a7 74 75 72 6e 3b 0a 23 69 66 20 53 51 4c 49 54 45 turn;.#if SQLITE
1e9a8 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 _MAX_COLUMN. if
1e9a9 28 20 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e ( p->nCol+1>db->
1e9aa 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1e9ab 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 MIT_COLUMN] ){.
1e9ac 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1e9ad 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 sg(pParse, "too
1e9ae 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20 many columns on
1e9af 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a %s", p->zName);.
1e9b0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
1e9b1 23 65 6e 64 69 66 0a 20 20 7a 20 3d 20 73 71 6c #endif. z = sql
1e9b2 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
1e9b3 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e n(pParse->db, pN
1e9b4 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 ame);. if( z==0
1e9b5 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 ) return;. for
1e9b6 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b (i=0; i<p->nCol;
1e9b7 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 i++){. if( S
1e9b8 54 52 49 43 4d 50 28 7a 2c 20 70 2d 3e 61 43 6f TRICMP(z, p->aCo
1e9b9 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 l[i].zName) ){.
1e9ba 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1e9bb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 64 75 rMsg(pParse, "du
1e9bc 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d 6e 20 6e plicate column n
1e9bd 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 ame: %s", z);.
1e9be 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1e9bf 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 e(db, z);.
1e9c0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
1e9c1 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f 6c }. if( (p->nCol
1e9c2 20 26 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20 20 & 0x7)==0 ){.
1e9c3 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b 0a Column *aNew;.
1e9c4 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 aNew = sqlit
1e9c5 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 50 61 72 e3DbRealloc(pPar
1e9c6 73 65 2d 3e 64 62 2c 70 2d 3e 61 43 6f 6c 2c 28 se->db,p->aCol,(
1e9c7 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65 6f p->nCol+8)*sizeo
1e9c8 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a f(p->aCol[0]));.
1e9c9 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 if( aNew==0
1e9ca 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1e9cb 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 DbFree(db, z);.
1e9cc 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
1e9cd 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d }. p->aCol =
1e9ce 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 43 6f aNew;. }. pCo
1e9cf 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e l = &p->aCol[p->
1e9d0 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 nCol];. memset(
1e9d1 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 pCol, 0, sizeof(
1e9d2 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 p->aCol[0]));.
1e9d3 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 3b pCol->zName = z;
1e9d4 0a 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 . . /* If there
1e9d5 20 69 73 20 6e 6f 20 74 79 70 65 20 73 70 65 63 is no type spec
1e9d6 69 66 69 65 64 2c 20 63 6f 6c 75 6d 6e 73 20 68 ified, columns h
1e9d7 61 76 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 ave the default
1e9d8 61 66 66 69 6e 69 74 79 0a 20 20 2a 2a 20 27 4e affinity. ** 'N
1e9d9 4f 4e 45 27 2e 20 49 66 20 74 68 65 72 65 20 69 ONE'. If there i
1e9da 73 20 61 20 74 79 70 65 20 73 70 65 63 69 66 69 s a type specifi
1e9db 65 64 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 ed, then sqlite3
1e9dc 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 29 20 AddColumnType()
1e9dd 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 63 61 6c will. ** be cal
1e9de 6c 65 64 20 6e 65 78 74 20 74 6f 20 73 65 74 20 led next to set
1e9df 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 63 pCol->affinity c
1e9e0 6f 72 72 65 63 74 6c 79 2e 0a 20 20 2a 2f 0a 20 orrectly.. */.
1e9e1 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 pCol->affinity
1e9e2 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e = SQLITE_AFF_NON
1e9e3 45 3b 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a E;. p->nCol++;.
1e9e4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
1e9e5 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
1e9e6 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68 by the parser wh
1e9e7 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c ile in the middl
1e9e8 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 e of.** parsing
1e9e9 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 a CREATE TABLE s
1e9ea 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 22 4e 4f tatement. A "NO
1e9eb 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 T NULL" constrai
1e9ec 6e 74 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 73 nt has.** been s
1e9ed 65 65 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e een on a column.
1e9ee 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 This routine s
1e9ef 65 74 73 20 74 68 65 20 6e 6f 74 4e 75 6c 6c 20 ets the notNull
1e9f0 66 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 flag on.** the c
1e9f1 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 olumn currently
1e9f2 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 under constructi
1e9f3 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
1e9f4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1e9f5 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 e3AddNotNull(Par
1e9f6 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1e9f7 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 54 61 62 6c onError){. Tabl
1e9f8 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 e *p;. int i;.
1e9f9 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 if( (p = pParse
1e9fa 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 ->pNewTable)==0
1e9fb 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 ) return;. i =
1e9fc 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66 28 p->nCol-1;. if(
1e9fd 20 69 3e 3d 30 20 29 20 70 2d 3e 61 43 6f 6c 5b i>=0 ) p->aCol[
1e9fe 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 6f 6e 45 i].notNull = onE
1e9ff 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 rror;.}../*.** S
1ea00 63 61 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 can the column t
1ea01 79 70 65 20 6e 61 6d 65 20 7a 54 79 70 65 20 28 ype name zType (
1ea02 6c 65 6e 67 74 68 20 6e 54 79 70 65 29 20 61 6e length nType) an
1ea03 64 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 d return the.**
1ea04 61 73 73 6f 63 69 61 74 65 64 20 61 66 66 69 6e associated affin
1ea05 69 74 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 ity type..**.**
1ea06 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
1ea07 73 20 61 20 63 61 73 65 2d 69 6e 64 65 70 65 6e s a case-indepen
1ea08 64 65 6e 74 20 73 65 61 72 63 68 20 6f 66 20 7a dent search of z
1ea09 54 79 70 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a Type for the .**
1ea0a 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 substrings in t
1ea0b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 he following tab
1ea0c 6c 65 2e 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 le. If one of th
1ea0d 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 73 0a e substrings is.
1ea0e 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65 20 63 6f ** found, the co
1ea0f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 66 66 69 rresponding affi
1ea10 6e 69 74 79 20 69 73 20 72 65 74 75 72 6e 65 64 nity is returned
1ea11 2e 20 49 66 20 7a 54 79 70 65 20 63 6f 6e 74 61 . If zType conta
1ea12 69 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e ins.** more than
1ea13 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 one of the subs
1ea14 74 72 69 6e 67 73 2c 20 65 6e 74 72 69 65 73 20 trings, entries
1ea15 74 6f 77 61 72 64 20 74 68 65 20 74 6f 70 20 6f toward the top o
1ea16 66 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 f .** the table
1ea17 74 61 6b 65 20 70 72 69 6f 72 69 74 79 2e 20 46 take priority. F
1ea18 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 7a or example, if z
1ea19 54 79 70 65 20 69 73 20 27 42 4c 4f 42 49 4e 54 Type is 'BLOBINT
1ea1a 27 2c 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 ', .** SQLITE_AF
1ea1b 46 5f 49 4e 54 45 47 45 52 20 69 73 20 72 65 74 F_INTEGER is ret
1ea1c 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 urned..**.** Sub
1ea1d 73 74 72 69 6e 67 20 20 20 20 20 7c 20 41 66 66 string | Aff
1ea1e 69 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d inity.** -------
1ea1f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1ea20 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e ---------.** 'IN
1ea21 54 27 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c T' | SQL
1ea22 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 0a ITE_AFF_INTEGER.
1ea23 2a 2a 20 27 43 48 41 52 27 20 20 20 20 20 20 20 ** 'CHAR'
1ea24 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 | SQLITE_AFF_TE
1ea25 58 54 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 20 20 XT.** 'CLOB'
1ea26 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 | SQLITE_AFF
1ea27 5f 54 45 58 54 0a 2a 2a 20 27 54 45 58 54 27 20 _TEXT.** 'TEXT'
1ea28 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f | SQLITE_
1ea29 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 42 4c 4f AFF_TEXT.** 'BLO
1ea2a 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 B' | SQLI
1ea2b 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a 2a 20 27 TE_AFF_NONE.** '
1ea2c 52 45 41 4c 27 20 20 20 20 20 20 20 20 7c 20 53 REAL' | S
1ea2d 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a QLITE_AFF_REAL.*
1ea2e 2a 20 27 46 4c 4f 41 27 20 20 20 20 20 20 20 20 * 'FLOA'
1ea2f 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 | SQLITE_AFF_REA
1ea30 4c 0a 2a 2a 20 27 44 4f 55 42 27 20 20 20 20 20 L.** 'DOUB'
1ea31 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f | SQLITE_AFF_
1ea32 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f REAL.**.** If no
1ea33 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 ne of the substr
1ea34 69 6e 67 73 20 69 6e 20 74 68 65 20 61 62 6f 76 ings in the abov
1ea35 65 20 74 61 62 6c 65 20 61 72 65 20 66 6f 75 6e e table are foun
1ea36 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 d,.** SQLITE_AFF
1ea37 5f 4e 55 4d 45 52 49 43 20 69 73 20 72 65 74 75 _NUMERIC is retu
1ea38 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
1ea39 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c PRIVATE char sql
1ea3a 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 ite3AffinityType
1ea3b 28 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 (const Token *pT
1ea3c 79 70 65 29 7b 0a 20 20 75 33 32 20 68 20 3d 20 ype){. u32 h =
1ea3d 30 3b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 0;. char aff =
1ea3e 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
1ea3f 49 43 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 IC;. const unsi
1ea40 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 20 3d gned char *zIn =
1ea41 20 70 54 79 70 65 2d 3e 7a 3b 0a 20 20 63 6f 6e pType->z;. con
1ea42 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
1ea43 20 2a 7a 45 6e 64 20 3d 20 26 70 54 79 70 65 2d *zEnd = &pType-
1ea44 3e 7a 5b 70 54 79 70 65 2d 3e 6e 5d 3b 0a 0a 20 >z[pType->n];..
1ea45 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 45 6e while( zIn!=zEn
1ea46 64 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c d ){. h = (h<
1ea47 3c 38 29 20 2b 20 73 71 6c 69 74 65 33 55 70 70 <8) + sqlite3Upp
1ea48 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 49 6e 5d 3b erToLower[*zIn];
1ea49 0a 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 . zIn++;.
1ea4a 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 if( h==(('c'<<24
1ea4b 29 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27 61 27 )+('h'<<16)+('a'
1ea4c 3c 3c 38 29 2b 27 72 27 29 20 29 7b 20 20 20 20 <<8)+'r') ){
1ea4d 20 20 20 20 20 20 20 20 20 2f 2a 20 43 48 41 52 /* CHAR
1ea4e 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 */. aff =
1ea4f 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b SQLITE_AFF_TEXT;
1ea50 20 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 . }else if(
1ea51 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 h==(('c'<<24)+('
1ea52 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 l'<<16)+('o'<<8)
1ea53 2b 27 62 27 29 20 29 7b 20 20 20 20 20 20 20 2f +'b') ){ /
1ea54 2a 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 * CLOB */.
1ea55 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 aff = SQLITE_AFF
1ea56 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 _TEXT;. }else
1ea57 20 69 66 28 20 68 3d 3d 28 28 27 74 27 3c 3c 32 if( h==(('t'<<2
1ea58 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 78 4)+('e'<<16)+('x
1ea59 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 '<<8)+'t') ){
1ea5a 20 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f 0a 20 /* TEXT */.
1ea5b 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 aff = SQLIT
1ea5c 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 E_AFF_TEXT;.
1ea5d 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 }else if( h==(('
1ea5e 62 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 b'<<24)+('l'<<16
1ea5f 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 )+('o'<<8)+'b')
1ea60 20 20 20 20 20 20 20 20 20 2f 2a 20 42 4c 4f 42 /* BLOB
1ea61 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 28 */. && (
1ea62 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f aff==SQLITE_AFF_
1ea63 4e 55 4d 45 52 49 43 20 7c 7c 20 61 66 66 3d 3d NUMERIC || aff==
1ea64 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 29 SQLITE_AFF_REAL)
1ea65 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 ){. aff =
1ea66 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b SQLITE_AFF_NONE;
1ea67 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1ea68 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
1ea69 49 4e 54 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 INT. }else if
1ea6a 28 20 68 3d 3d 28 28 27 72 27 3c 3c 32 34 29 2b ( h==(('r'<<24)+
1ea6b 28 27 65 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c ('e'<<16)+('a'<<
1ea6c 38 29 2b 27 6c 27 29 20 20 20 20 20 20 20 20 20 8)+'l')
1ea6d 20 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 20 20 20 /* REAL */.
1ea6e 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 && aff==SQLI
1ea6f 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 TE_AFF_NUMERIC )
1ea70 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 {. aff = SQ
1ea71 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 LITE_AFF_REAL;.
1ea72 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d }else if( h==
1ea73 28 28 27 66 27 3c 3c 32 34 29 2b 28 27 6c 27 3c (('f'<<24)+('l'<
1ea74 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 61 <16)+('o'<<8)+'a
1ea75 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 ') /* F
1ea76 4c 4f 41 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 LOA */. &
1ea77 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 & aff==SQLITE_AF
1ea78 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 F_NUMERIC ){.
1ea79 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f aff = SQLITE_
1ea7a 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 AFF_REAL;. }e
1ea7b 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 64 27 lse if( h==(('d'
1ea7c 3c 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 36 29 2b <<24)+('o'<<16)+
1ea7d 28 27 75 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 ('u'<<8)+'b')
1ea7e 20 20 20 20 20 20 20 2f 2a 20 44 4f 55 42 20 2a /* DOUB *
1ea7f 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 /. && aff
1ea80 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d ==SQLITE_AFF_NUM
1ea81 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 ERIC ){. af
1ea82 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 f = SQLITE_AFF_R
1ea83 45 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 EAL;.#endif.
1ea84 7d 65 6c 73 65 20 69 66 28 20 28 68 26 30 78 30 }else if( (h&0x0
1ea85 30 46 46 46 46 46 46 29 3d 3d 28 28 27 69 27 3c 0FFFFFF)==(('i'<
1ea86 3c 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b 27 74 <16)+('n'<<8)+'t
1ea87 27 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e 54 20 ') ){ /* INT
1ea88 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 */. aff = S
1ea89 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
1ea8a 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a R;. break;.
1ea8b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
1ea8c 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a urn aff;.}../*.*
1ea8d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1ea8e 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
1ea8f 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 parser while in
1ea90 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a the middle of.**
1ea91 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 parsing a CREAT
1ea92 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
1ea93 74 2e 20 20 54 68 65 20 70 46 69 72 73 74 20 74 t. The pFirst t
1ea94 6f 6b 65 6e 20 69 73 20 74 68 65 20 66 69 72 73 oken is the firs
1ea95 74 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 t.** token in th
1ea96 65 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 6f e sequence of to
1ea97 6b 65 6e 73 20 74 68 61 74 20 64 65 73 63 72 69 kens that descri
1ea98 62 65 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 be the type of t
1ea99 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63 75 72 he.** column cur
1ea9a 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e rently under con
1ea9b 73 74 72 75 63 74 69 6f 6e 2e 20 20 20 70 4c 61 struction. pLa
1ea9c 73 74 20 69 73 20 74 68 65 20 6c 61 73 74 20 74 st is the last t
1ea9d 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 73 oken.** in the s
1ea9e 65 71 75 65 6e 63 65 2e 20 20 55 73 65 20 74 68 equence. Use th
1ea9f 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 is information t
1eaa0 6f 20 63 6f 6e 73 74 72 75 63 74 20 61 20 73 74 o construct a st
1eaa1 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 63 6f 6e ring.** that con
1eaa2 74 61 69 6e 73 20 74 68 65 20 74 79 70 65 6e 61 tains the typena
1eaa3 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e me of the column
1eaa4 20 61 6e 64 20 73 74 6f 72 65 20 74 68 61 74 20 and store that
1eaa5 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 7a 54 79 string.** in zTy
1eaa6 70 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 pe..*/ .SQLITE_P
1eaa7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1eaa8 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 te3AddColumnType
1eaa9 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1eaaa 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 Token *pType){.
1eaab 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 Table *p;. int
1eaac 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 i;. Column *pC
1eaad 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 ol;. sqlite3 *d
1eaae 62 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d 20 70 b;.. if( (p = p
1eaaf 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
1eab0 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 )==0 ) return;.
1eab1 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a i = p->nCol-1;.
1eab2 20 20 69 66 28 20 69 3c 30 20 29 20 72 65 74 75 if( i<0 ) retu
1eab3 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d rn;. pCol = &p-
1eab4 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 64 62 20 3d >aCol[i];. db =
1eab5 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 73 pParse->db;. s
1eab6 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1eab7 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 pCol->zType);.
1eab8 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 pCol->zType = s
1eab9 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1eaba 6b 65 6e 28 64 62 2c 20 70 54 79 70 65 29 3b 0a ken(db, pType);.
1eabb 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 pCol->affinity
1eabc 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 = sqlite3Affini
1eabd 74 79 54 79 70 65 28 70 54 79 70 65 29 3b 0a 7d tyType(pType);.}
1eabe 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 ../*.** The expr
1eabf 65 73 73 69 6f 6e 20 69 73 20 74 68 65 20 64 65 ession is the de
1eac0 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 fault value for
1eac1 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
1eac2 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a y added column.*
1eac3 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 * of the table c
1eac4 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 urrently under c
1eac5 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a onstruction..**.
1eac6 2a 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 ** Default value
1eac7 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 expressions mus
1eac8 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20 t be constant.
1eac9 52 61 69 73 65 20 61 6e 20 65 78 63 65 70 74 69 Raise an excepti
1eaca 6f 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 on if this.** is
1eacb 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2e 0a 2a not the case..*
1eacc 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1eacd 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 e is called by t
1eace 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 he parser while
1eacf 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
1ead0 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 .** parsing a CR
1ead1 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
1ead2 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ment..*/.SQLITE_
1ead3 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1ead4 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 ite3AddDefaultVa
1ead5 6c 75 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 lue(Parse *pPars
1ead6 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b e, Expr *pExpr){
1ead7 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43 . Table *p;. C
1ead8 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 olumn *pCol;. s
1ead9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1eada 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 rse->db;. if( (
1eadb 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 p = pParse->pNew
1eadc 54 61 62 6c 65 29 21 3d 30 20 29 7b 0a 20 20 20 Table)!=0 ){.
1eadd 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f pCol = &(p->aCo
1eade 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a 20 l[p->nCol-1]);.
1eadf 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 if( !sqlite3E
1eae0 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 xprIsConstantOrF
1eae1 75 6e 63 74 69 6f 6e 28 70 45 78 70 72 29 20 29 unction(pExpr) )
1eae2 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1eae3 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1eae4 22 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f "default value o
1eae5 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69 73 f column [%s] is
1eae6 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22 2c 0a not constant",.
1eae7 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e pCol->
1eae8 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 zName);. }els
1eae9 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 e{. Expr *p
1eaea 43 6f 70 79 3b 0a 20 20 20 20 20 20 73 71 6c 69 Copy;. sqli
1eaeb 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1eaec 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a , pCol->pDflt);.
1eaed 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c pCol->pDfl
1eaee 74 20 3d 20 70 43 6f 70 79 20 3d 20 73 71 6c 69 t = pCopy = sqli
1eaef 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
1eaf0 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 Expr);. if(
1eaf1 20 70 43 6f 70 79 20 29 7b 0a 20 20 20 20 20 20 pCopy ){.
1eaf2 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f sqlite3TokenCo
1eaf3 70 79 28 64 62 2c 20 26 70 43 6f 70 79 2d 3e 73 py(db, &pCopy->s
1eaf4 70 61 6e 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 pan, &pExpr->spa
1eaf5 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 n);. }.
1eaf6 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 }. }. sqlite3E
1eaf7 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 xprDelete(db, pE
1eaf8 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 xpr);.}../*.** D
1eaf9 65 73 69 67 6e 61 74 65 20 74 68 65 20 50 52 49 esignate the PRI
1eafa 4d 41 52 59 20 4b 45 59 20 66 6f 72 20 74 68 65 MARY KEY for the
1eafb 20 74 61 62 6c 65 2e 20 20 70 4c 69 73 74 20 69 table. pList i
1eafc 73 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 s a list of name
1eafd 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 s .** of columns
1eafe 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 70 that form the p
1eaff 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20 rimary key. If
1eb00 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c 2c 20 74 pList is NULL, t
1eb01 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 hen the.** most
1eb02 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 63 recently added c
1eb03 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 olumn of the tab
1eb04 6c 65 20 69 73 20 74 68 65 20 70 72 69 6d 61 72 le is the primar
1eb05 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 y key..**.** A t
1eb06 61 62 6c 65 20 63 61 6e 20 68 61 76 65 20 61 74 able can have at
1eb07 20 6d 6f 73 74 20 6f 6e 65 20 70 72 69 6d 61 72 most one primar
1eb08 79 20 6b 65 79 2e 20 20 49 66 20 74 68 65 20 74 y key. If the t
1eb09 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68 61 73 able already has
1eb0a 0a 2a 2a 20 61 20 70 72 69 6d 61 72 79 20 6b 65 .** a primary ke
1eb0b 79 20 28 61 6e 64 20 74 68 69 73 20 69 73 20 74 y (and this is t
1eb0c 68 65 20 73 65 63 6f 6e 64 20 70 72 69 6d 61 72 he second primar
1eb0d 79 20 6b 65 79 29 20 74 68 65 6e 20 63 72 65 61 y key) then crea
1eb0e 74 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 0a te an.** error..
1eb0f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 52 49 **.** If the PRI
1eb10 4d 41 52 59 20 4b 45 59 20 69 73 20 6f 6e 20 61 MARY KEY is on a
1eb11 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 77 single column w
1eb12 68 6f 73 65 20 64 61 74 61 74 79 70 65 20 69 73 hose datatype is
1eb13 20 49 4e 54 45 47 45 52 2c 0a 2a 2a 20 74 68 65 INTEGER,.** the
1eb14 6e 20 77 65 20 77 69 6c 6c 20 74 72 79 20 74 6f n we will try to
1eb15 20 75 73 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e use that column
1eb16 20 61 73 20 74 68 65 20 72 6f 77 69 64 2e 20 20 as the rowid.
1eb17 53 65 74 20 74 68 65 20 54 61 62 6c 65 2e 69 50 Set the Table.iP
1eb18 4b 65 79 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 Key.** field of
1eb19 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72 20 the table under
1eb1a 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 construction to
1eb1b 62 65 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 be the index of
1eb1c 74 68 65 0a 2a 2a 20 49 4e 54 45 47 45 52 20 50 the.** INTEGER P
1eb1d 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d RIMARY KEY colum
1eb1e 6e 2e 20 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 n. Table.iPKey
1eb1f 69 73 20 73 65 74 20 74 6f 20 2d 31 20 69 66 20 is set to -1 if
1eb20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 49 there is.** no I
1eb21 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
1eb22 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 EY..**.** If the
1eb23 20 6b 65 79 20 69 73 20 6e 6f 74 20 61 6e 20 49 key is not an I
1eb24 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
1eb25 45 59 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 EY, then create
1eb26 61 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 64 65 a unique.** inde
1eb27 78 20 66 6f 72 20 74 68 65 20 6b 65 79 2e 20 20 x for the key.
1eb28 4e 6f 20 69 6e 64 65 78 20 69 73 20 63 72 65 61 No index is crea
1eb29 74 65 64 20 66 6f 72 20 49 4e 54 45 47 45 52 20 ted for INTEGER
1eb2a 50 52 49 4d 41 52 59 20 4b 45 59 73 2e 0a 2a 2f PRIMARY KEYs..*/
1eb2b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1eb2c 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 50 void sqlite3AddP
1eb2d 72 69 6d 61 72 79 4b 65 79 28 0a 20 20 50 61 72 rimaryKey(. Par
1eb2e 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f se *pParse, /
1eb2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
1eb30 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 t */. ExprList
1eb31 2a 70 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 *pList, /* List
1eb32 20 6f 66 20 66 69 65 6c 64 20 6e 61 6d 65 73 20 of field names
1eb33 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f to be indexed */
1eb34 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 . int onError,
1eb35 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 /* What to
1eb36 64 6f 20 77 69 74 68 20 61 20 75 6e 69 71 75 65 do with a unique
1eb37 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 2a 2f ness conflict */
1eb38 0a 20 20 69 6e 74 20 61 75 74 6f 49 6e 63 2c 20 . int autoInc,
1eb39 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1eb3a 74 68 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e the AUTOINCREMEN
1eb3b 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 T keyword is pre
1eb3c 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f sent */. int so
1eb3d 72 74 4f 72 64 65 72 20 20 20 20 20 2f 2a 20 53 rtOrder /* S
1eb3e 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 6f 72 20 QLITE_SO_ASC or
1eb3f 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 2a SQLITE_SO_DESC *
1eb40 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 /.){. Table *pT
1eb41 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 ab = pParse->pNe
1eb42 77 54 61 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a wTable;. char *
1eb43 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 zType = 0;. int
1eb44 20 69 43 6f 6c 20 3d 20 2d 31 2c 20 69 3b 0a 20 iCol = -1, i;.
1eb45 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20 if( pTab==0 ||
1eb46 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
1eb47 29 20 67 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b ) goto primary_k
1eb48 65 79 5f 65 78 69 74 3b 0a 20 20 69 66 28 20 70 ey_exit;. if( p
1eb49 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 Tab->tabFlags &
1eb4a 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 TF_HasPrimaryKey
1eb4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1eb4c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1eb4d 0a 20 20 20 20 20 20 22 74 61 62 6c 65 20 5c 22 . "table \"
1eb4e 25 73 5c 22 20 68 61 73 20 6d 6f 72 65 20 74 68 %s\" has more th
1eb4f 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b an one primary k
1eb50 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 ey", pTab->zName
1eb51 29 3b 0a 20 20 20 20 67 6f 74 6f 20 70 72 69 6d );. goto prim
1eb52 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 ary_key_exit;.
1eb53 7d 0a 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 }. pTab->tabFla
1eb54 67 73 20 7c 3d 20 54 46 5f 48 61 73 50 72 69 6d gs |= TF_HasPrim
1eb55 61 72 79 4b 65 79 3b 0a 20 20 69 66 28 20 70 4c aryKey;. if( pL
1eb56 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 43 ist==0 ){. iC
1eb57 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 ol = pTab->nCol
1eb58 2d 20 31 3b 0a 20 20 20 20 70 54 61 62 2d 3e 61 - 1;. pTab->a
1eb59 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 6d Col[iCol].isPrim
1eb5a 4b 65 79 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 Key = 1;. }else
1eb5b 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 {. for(i=0; i
1eb5c 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 <pList->nExpr; i
1eb5d 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 ++){. for(i
1eb5e 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 Col=0; iCol<pTab
1eb5f 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b ->nCol; iCol++){
1eb60 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
1eb61 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c 69 73 ite3StrICmp(pLis
1eb62 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70 t->a[i].zName, p
1eb63 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e Tab->aCol[iCol].
1eb64 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 zName)==0 ){.
1eb65 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1eb66 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1eb67 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 70 if( iCol<p
1eb68 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 Tab->nCol ){.
1eb69 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b pTab->aCol[
1eb6a 69 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20 iCol].isPrimKey
1eb6b 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 1;. }.
1eb6c 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 }. if( pList
1eb6d 2d 3e 6e 45 78 70 72 3e 31 20 29 20 69 43 6f 6c ->nExpr>1 ) iCol
1eb6e 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 69 66 28 = -1;. }. if(
1eb6f 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c iCol>=0 && iCol
1eb70 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 <pTab->nCol ){.
1eb71 20 20 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d zType = pTab-
1eb72 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 >aCol[iCol].zTyp
1eb73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 54 79 e;. }. if( zTy
1eb74 70 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 pe && sqlite3Str
1eb75 49 43 6d 70 28 7a 54 79 70 65 2c 20 22 49 4e 54 ICmp(zType, "INT
1eb76 45 47 45 52 22 29 3d 3d 30 0a 20 20 20 20 20 20 EGER")==0.
1eb77 20 20 26 26 20 73 6f 72 74 4f 72 64 65 72 3d 3d && sortOrder==
1eb78 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 29 7b SQLITE_SO_ASC ){
1eb79 0a 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 . pTab->iPKey
1eb7a 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 54 61 = iCol;. pTa
1eb7b 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d 20 6f 6e 45 b->keyConf = onE
1eb7c 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72 74 rror;. assert
1eb7d 28 20 61 75 74 6f 49 6e 63 3d 3d 30 20 7c 7c 20 ( autoInc==0 ||
1eb7e 61 75 74 6f 49 6e 63 3d 3d 31 20 29 3b 0a 20 20 autoInc==1 );.
1eb7f 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 pTab->tabFlags
1eb80 20 7c 3d 20 61 75 74 6f 49 6e 63 2a 54 46 5f 41 |= autoInc*TF_A
1eb81 75 74 6f 69 6e 63 72 65 6d 65 6e 74 3b 0a 20 20 utoincrement;.
1eb82 7d 65 6c 73 65 20 69 66 28 20 61 75 74 6f 49 6e }else if( autoIn
1eb83 63 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c c ){.#ifndef SQL
1eb84 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
1eb85 52 45 4d 45 4e 54 0a 20 20 20 20 73 71 6c 69 74 REMENT. sqlit
1eb86 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1eb87 65 2c 20 22 41 55 54 4f 49 4e 43 52 45 4d 45 4e e, "AUTOINCREMEN
1eb88 54 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 T is only allowe
1eb89 64 20 6f 6e 20 61 6e 20 22 0a 20 20 20 20 20 20 d on an ".
1eb8a 20 22 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 "INTEGER PRIMAR
1eb8b 59 20 4b 45 59 22 29 3b 0a 23 65 6e 64 69 66 0a Y KEY");.#endif.
1eb8c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
1eb8d 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 ite3CreateIndex(
1eb8e 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c pParse, 0, 0, 0,
1eb8f 20 70 4c 69 73 74 2c 20 6f 6e 45 72 72 6f 72 2c pList, onError,
1eb90 20 30 2c 20 30 2c 20 73 6f 72 74 4f 72 64 65 72 0, 0, sortOrder
1eb91 2c 20 30 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 , 0);. pList
1eb92 3d 20 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61 72 = 0;. }..primar
1eb93 79 5f 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73 71 y_key_exit:. sq
1eb94 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
1eb95 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ete(pParse->db,
1eb96 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e pList);. return
1eb97 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 ;.}../*.** Add a
1eb98 20 6e 65 77 20 43 48 45 43 4b 20 63 6f 6e 73 74 new CHECK const
1eb99 72 61 69 6e 74 20 74 6f 20 74 68 65 20 74 61 62 raint to the tab
1eb9a 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 le currently und
1eb9b 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e er construction.
1eb9c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1eb9d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
1eb9e 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e ddCheckConstrain
1eb9f 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 t(. Parse *pPar
1eba0 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e se, /* Parsin
1eba1 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 g context */. E
1eba2 78 70 72 20 2a 70 43 68 65 63 6b 45 78 70 72 20 xpr *pCheckExpr
1eba3 20 2f 2a 20 54 68 65 20 63 68 65 63 6b 20 65 78 /* The check ex
1eba4 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 pression */.){.
1eba5 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1eba6 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 Parse->db;.#ifnd
1eba7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
1eba8 48 45 43 4b 0a 20 20 54 61 62 6c 65 20 2a 70 54 HECK. Table *pT
1eba9 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 ab = pParse->pNe
1ebaa 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 wTable;. if( pT
1ebab 61 62 20 26 26 20 21 49 4e 5f 44 45 43 4c 41 52 ab && !IN_DECLAR
1ebac 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 2f 2a E_VTAB ){. /*
1ebad 20 54 68 65 20 43 48 45 43 4b 20 65 78 70 72 65 The CHECK expre
1ebae 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 75 ssion must be du
1ebaf 70 6c 69 63 61 74 65 64 20 73 6f 20 74 68 61 74 plicated so that
1ebb0 20 74 6f 6b 65 6e 73 20 72 65 66 65 72 0a 20 20 tokens refer.
1ebb1 20 20 2a 2a 20 74 6f 20 6d 61 6c 6c 6f 63 65 64 ** to malloced
1ebb2 20 73 70 61 63 65 20 61 6e 64 20 6e 6f 74 20 74 space and not t
1ebb3 68 65 20 28 65 70 68 65 6d 65 72 61 6c 29 20 74 he (ephemeral) t
1ebb4 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 ext of the CREAT
1ebb5 45 20 54 41 42 4c 45 0a 20 20 20 20 2a 2a 20 73 E TABLE. ** s
1ebb6 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 tatement */.
1ebb7 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 3d 20 73 pTab->pCheck = s
1ebb8 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62 qlite3ExprAnd(db
1ebb9 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 , pTab->pCheck,
1ebba 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1ebbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ebbc 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 sqlite3ExprDu
1ebbd 70 28 64 62 2c 20 70 43 68 65 63 6b 45 78 70 72 p(db, pCheckExpr
1ebbe 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ));. }.#endif.
1ebbf 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
1ebc0 74 65 28 64 62 2c 20 70 43 68 65 63 6b 45 78 70 te(db, pCheckExp
1ebc1 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 r);.}../*.** Set
1ebc2 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 the collation f
1ebc3 75 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d unction of the m
1ebc4 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 70 61 72 ost recently par
1ebc5 73 65 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e sed table column
1ebc6 0a 2a 2a 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53 .** to the CollS
1ebc7 65 71 20 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c eq given..*/.SQL
1ebc8 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1ebc9 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 sqlite3AddColla
1ebca 74 65 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 teType(Parse *pP
1ebcb 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f arse, Token *pTo
1ebcc 6b 65 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 ken){. Table *p
1ebcd 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 ;. int i;. cha
1ebce 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 r *zColl;
1ebcf 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f 74 /* Dequot
1ebd0 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 ed name of colla
1ebd1 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f tion sequence */
1ebd2 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1ebd3 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 . if( (p = pPar
1ebd4 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d se->pNewTable)==
1ebd5 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 0 ) return;. i
1ebd6 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 = p->nCol-1;. d
1ebd7 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1ebd8 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 zColl = sqlite
1ebd9 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
1ebda 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 b, pToken);. if
1ebdb 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 ( !zColl ) retur
1ebdc 6e 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 n;.. if( sqlite
1ebdd 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 3LocateCollSeq(p
1ebde 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 Parse, zColl, -1
1ebdf 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a ) ){. Index *
1ebe0 70 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61 43 6f pIdx;. p->aCo
1ebe1 6c 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f l[i].zColl = zCo
1ebe2 6c 6c 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 ll;. . /* If
1ebe3 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 64 the column is d
1ebe4 65 63 6c 61 72 65 64 20 61 73 20 22 3c 6e 61 6d eclared as "<nam
1ebe5 65 3e 20 50 52 49 4d 41 52 59 20 4b 45 59 20 43 e> PRIMARY KEY C
1ebe6 4f 4c 4c 41 54 45 20 3c 74 79 70 65 3e 22 2c 0a OLLATE <type>",.
1ebe7 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 20 69 ** then an i
1ebe8 6e 64 65 78 20 6d 61 79 20 68 61 76 65 20 62 65 ndex may have be
1ebe9 65 6e 20 63 72 65 61 74 65 64 20 6f 6e 20 74 68 en created on th
1ebea 69 73 20 63 6f 6c 75 6d 6e 20 62 65 66 6f 72 65 is column before
1ebeb 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c the. ** coll
1ebec 61 74 69 6f 6e 20 74 79 70 65 20 77 61 73 20 61 ation type was a
1ebed 64 64 65 64 2e 20 43 6f 72 72 65 63 74 20 74 68 dded. Correct th
1ebee 69 73 20 69 66 20 69 74 20 69 73 20 74 68 65 20 is if it is the
1ebef 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 case.. */.
1ebf0 20 66 6f 72 28 70 49 64 78 3d 70 2d 3e 70 49 6e for(pIdx=p->pIn
1ebf1 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d dex; pIdx; pIdx=
1ebf2 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 pIdx->pNext){.
1ebf3 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 assert( pIdx
1ebf4 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a ->nColumn==1 );.
1ebf5 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e if( pIdx->
1ebf6 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 aiColumn[0]==i )
1ebf7 7b 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e {. pIdx->
1ebf8 61 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61 azColl[0] = p->a
1ebf9 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 Col[i].zColl;.
1ebfa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 }. }. }e
1ebfb 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
1ebfc 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c DbFree(db, zColl
1ebfd 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
1ebfe 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
1ebff 74 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61 74 turns the collat
1ec00 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 ion sequence for
1ec01 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 database native
1ec02 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e text.** encodin
1ec03 67 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 g identified by
1ec04 74 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 the string zName
1ec05 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a , length nName..
1ec06 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 **.** If the req
1ec07 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e uested collation
1ec08 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 sequence is not
1ec09 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20 6e available, or n
1ec0a 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 ot available.**
1ec0b 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1ec0c 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c native encoding,
1ec0d 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 the collation f
1ec0e 61 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65 actory is invoke
1ec0f 64 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 d to.** request
1ec10 69 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 it. If the colla
1ec11 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f 65 tion factory doe
1ec12 73 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75 63 s not supply suc
1ec13 68 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a 2a h a sequence,.**
1ec14 20 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 and the sequenc
1ec15 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69 e is available i
1ec16 6e 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20 65 n another text e
1ec17 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 ncoding, then th
1ec18 61 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 at is.** returne
1ec19 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a d instead..**.**
1ec1a 20 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73 20 If no versions
1ec1b 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 of the requested
1ec1c 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71 75 collations sequ
1ec1d 65 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61 62 ence are availab
1ec1e 6c 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 le, or.** anothe
1ec1f 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 r error occurs,
1ec20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 NULL is returned
1ec21 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 and an error me
1ec22 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e ssage written in
1ec23 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2a to.** pParse..**
1ec24 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1ec25 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 is a wrapper ar
1ec26 6f 75 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 ound sqlite3Find
1ec27 43 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68 69 73 CollSeq(). This
1ec28 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f routine.** invo
1ec29 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f kes the collatio
1ec2a 6e 20 66 61 63 74 6f 72 79 20 69 66 20 74 68 65 n factory if the
1ec2b 20 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69 6f 6e named collation
1ec2c 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 cannot be found
1ec2d 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 .** and generate
1ec2e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 s an error messa
1ec2f 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ge..*/.SQLITE_PR
1ec30 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
1ec31 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c qlite3LocateColl
1ec32 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 Seq(Parse *pPars
1ec33 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
1ec34 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 Name, int nName)
1ec35 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
1ec36 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
1ec37 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 u8 enc = ENC(db)
1ec38 3b 0a 20 20 75 38 20 69 6e 69 74 62 75 73 79 20 ;. u8 initbusy
1ec39 3d 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b = db->init.busy;
1ec3a 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c . CollSeq *pCol
1ec3b 6c 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 l;.. pColl = sq
1ec3c 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 lite3FindCollSeq
1ec3d 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c (db, enc, zName,
1ec3e 20 6e 4e 61 6d 65 2c 20 69 6e 69 74 62 75 73 79 nName, initbusy
1ec3f 29 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 62 75 );. if( !initbu
1ec40 73 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 7c 7c sy && (!pColl ||
1ec41 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20 29 !pColl->xCmp) )
1ec42 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 {. pColl = sq
1ec43 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 lite3GetCollSeq(
1ec44 64 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 db, pColl, zName
1ec45 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 , nName);. if
1ec46 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 ( !pColl ){.
1ec47 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29 7b if( nName<0 ){
1ec48 0a 20 20 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d . nName =
1ec49 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 sqlite3Strlen(d
1ec4a 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 b, zName);.
1ec4b 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1ec4c 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1ec4d 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 "no such collat
1ec4e 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 2e ion sequence: %.
1ec4f 2a 73 22 2c 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d *s", nName, zNam
1ec50 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 e);. pColl
1ec51 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a = 0;. }. }..
1ec52 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a return pColl;.
1ec53 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 }.../*.** Genera
1ec54 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c te code that wil
1ec55 6c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 l increment the
1ec56 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a schema cookie..*
1ec57 2a 0a 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 20 *.** The schema
1ec58 63 6f 6f 6b 69 65 20 69 73 20 75 73 65 64 20 74 cookie is used t
1ec59 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 6e o determine when
1ec5a 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 the schema for
1ec5b 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
1ec5c 63 68 61 6e 67 65 73 2e 20 20 41 66 74 65 72 20 changes. After
1ec5d 65 61 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e each schema chan
1ec5e 67 65 2c 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 ge, the cookie v
1ec5f 61 6c 75 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e alue.** changes.
1ec60 20 20 57 68 65 6e 20 61 20 70 72 6f 63 65 73 73 When a process
1ec61 20 66 69 72 73 74 20 72 65 61 64 73 20 74 68 65 first reads the
1ec62 20 73 63 68 65 6d 61 20 69 74 20 72 65 63 6f 72 schema it recor
1ec63 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 ds the.** cookie
1ec64 2e 20 20 54 68 65 72 65 61 66 74 65 72 2c 20 77 . Thereafter, w
1ec65 68 65 6e 65 76 65 72 20 69 74 20 67 6f 65 73 20 henever it goes
1ec66 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61 to access the da
1ec67 74 61 62 61 73 65 2c 0a 2a 2a 20 69 74 20 63 68 tabase,.** it ch
1ec68 65 63 6b 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 ecks the cookie
1ec69 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 to make sure the
1ec6a 20 73 63 68 65 6d 61 20 68 61 73 20 6e 6f 74 20 schema has not
1ec6b 63 68 61 6e 67 65 64 0a 2a 2a 20 73 69 6e 63 65 changed.** since
1ec6c 20 69 74 20 77 61 73 20 6c 61 73 74 20 72 65 61 it was last rea
1ec6d 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 70 6c d..**.** This pl
1ec6e 61 6e 20 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 an is not comple
1ec6f 74 65 6c 79 20 62 75 6c 6c 65 74 2d 70 72 6f 6f tely bullet-proo
1ec70 66 2e 20 20 49 74 20 69 73 20 70 6f 73 73 69 62 f. It is possib
1ec71 6c 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 63 le for.** the sc
1ec72 68 65 6d 61 20 74 6f 20 63 68 61 6e 67 65 20 6d hema to change m
1ec73 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 61 6e ultiple times an
1ec74 64 20 66 6f 72 20 74 68 65 20 63 6f 6f 6b 69 65 d for the cookie
1ec75 20 74 6f 20 62 65 0a 2a 2a 20 73 65 74 20 62 61 to be.** set ba
1ec76 63 6b 20 74 6f 20 70 72 69 6f 72 20 76 61 6c 75 ck to prior valu
1ec77 65 2e 20 20 42 75 74 20 73 63 68 65 6d 61 20 63 e. But schema c
1ec78 68 61 6e 67 65 73 20 61 72 65 20 69 6e 66 72 65 hanges are infre
1ec79 71 75 65 6e 74 0a 2a 2a 20 61 6e 64 20 74 68 65 quent.** and the
1ec7a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 probability of
1ec7b 68 69 74 74 69 6e 67 20 74 68 65 20 73 61 6d 65 hitting the same
1ec7c 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 69 73 cookie value is
1ec7d 20 6f 6e 6c 79 0a 2a 2a 20 31 20 63 68 61 6e 63 only.** 1 chanc
1ec7e 65 20 69 6e 20 32 5e 33 32 2e 20 20 53 6f 20 77 e in 2^32. So w
1ec7f 65 27 72 65 20 73 61 66 65 20 65 6e 6f 75 67 68 e're safe enough
1ec80 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1ec81 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1ec82 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72 ChangeCookie(Par
1ec83 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1ec84 69 44 62 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d iDb){. int r1 =
1ec85 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
1ec86 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 eg(pParse);. sq
1ec87 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
1ec88 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a se->db;. Vdbe *
1ec89 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
1ec8a 65 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 e;. sqlite3Vdbe
1ec8b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
1ec8c 65 67 65 72 2c 20 64 62 2d 3e 61 44 62 5b 69 44 eger, db->aDb[iD
1ec8d 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 b].pSchema->sche
1ec8e 6d 61 5f 63 6f 6f 6b 69 65 2b 31 2c 20 72 31 29 ma_cookie+1, r1)
1ec8f 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
1ec90 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 ddOp3(v, OP_SetC
1ec91 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 30 2c 20 72 ookie, iDb, 0, r
1ec92 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 1);. sqlite3Rel
1ec93 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
1ec94 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a se, r1);.}../*.*
1ec95 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20 6e 75 * Measure the nu
1ec96 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
1ec97 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f 75 74 rs needed to out
1ec98 70 75 74 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a put the given.**
1ec99 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 54 68 identifier. Th
1ec9a 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65 e number returne
1ec9b 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79 20 71 d includes any q
1ec9c 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a 20 62 75 uotes used.** bu
1ec9d 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 t does not inclu
1ec9e 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 65 72 6d de the null term
1ec9f 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 inator..**.** Th
1eca0 65 20 65 73 74 69 6d 61 74 65 20 69 73 20 63 6f e estimate is co
1eca1 6e 73 65 72 76 61 74 69 76 65 2e 20 20 49 74 20 nservative. It
1eca2 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 might be larger
1eca3 74 68 61 74 20 77 68 61 74 20 69 73 0a 2a 2a 20 that what is.**
1eca4 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 2e 0a 2a really needed..*
1eca5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 64 65 /.static int ide
1eca6 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 74 20 63 ntLength(const c
1eca7 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6e har *z){. int n
1eca8 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 2a 7a 3b ;. for(n=0; *z;
1eca9 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 20 20 n++, z++){.
1ecaa 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 7b 20 6e if( *z=='"' ){ n
1ecab 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 ++; }. }. retu
1ecac 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a 0a rn n + 2;.}../*.
1ecad 2a 2a 20 57 72 69 74 65 20 61 6e 20 69 64 65 6e ** Write an iden
1ecae 74 69 66 69 65 72 20 6f 6e 74 6f 20 74 68 65 20 tifier onto the
1ecaf 65 6e 64 20 6f 66 20 74 68 65 20 67 69 76 65 6e end of the given
1ecb0 20 73 74 72 69 6e 67 2e 20 20 41 64 64 0a 2a 2a string. Add.**
1ecb1 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 quote character
1ecb2 73 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a s as needed..*/.
1ecb3 73 74 61 74 69 63 20 76 6f 69 64 20 69 64 65 6e static void iden
1ecb4 74 50 75 74 28 63 68 61 72 20 2a 7a 2c 20 69 6e tPut(char *z, in
1ecb5 74 20 2a 70 49 64 78 2c 20 63 68 61 72 20 2a 7a t *pIdx, char *z
1ecb6 53 69 67 6e 65 64 49 64 65 6e 74 29 7b 0a 20 20 SignedIdent){.
1ecb7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
1ecb8 49 64 65 6e 74 20 3d 20 28 75 6e 73 69 67 6e 65 Ident = (unsigne
1ecb9 64 20 63 68 61 72 2a 29 7a 53 69 67 6e 65 64 49 d char*)zSignedI
1ecba 64 65 6e 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a dent;. int i, j
1ecbb 2c 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 69 , needQuote;. i
1ecbc 20 3d 20 2a 70 49 64 78 3b 0a 20 20 66 6f 72 28 = *pIdx;. for(
1ecbd 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 j=0; zIdent[j];
1ecbe 6a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 69 j++){. if( !i
1ecbf 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 6a 5d salnum(zIdent[j]
1ecc0 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d ) && zIdent[j]!=
1ecc1 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d '_' ) break;. }
1ecc2 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 20 . needQuote =
1ecc3 7a 49 64 65 6e 74 5b 6a 5d 21 3d 30 20 7c 7c 20 zIdent[j]!=0 ||
1ecc4 69 73 64 69 67 69 74 28 7a 49 64 65 6e 74 5b 30 isdigit(zIdent[0
1ecc5 5d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ]).
1ecc6 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 4b || sqlite3K
1ecc7 65 79 77 6f 72 64 43 6f 64 65 28 7a 49 64 65 6e eywordCode(zIden
1ecc8 74 2c 20 6a 29 21 3d 54 4b 5f 49 44 3b 0a 20 20 t, j)!=TK_ID;.
1ecc9 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 if( needQuote )
1ecca 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 z[i++] = '"';.
1eccb 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b for(j=0; zIdent[
1eccc 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 5b j]; j++){. z[
1eccd 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e 74 5b 6a 5d i++] = zIdent[j]
1ecce 3b 0a 20 20 20 20 69 66 28 20 7a 49 64 65 6e 74 ;. if( zIdent
1eccf 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a 5b 69 2b 2b [j]=='"' ) z[i++
1ecd0 5d 20 3d 20 27 22 27 3b 0a 20 20 7d 0a 20 20 69 ] = '"';. }. i
1ecd1 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 7a f( needQuote ) z
1ecd2 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7a [i++] = '"';. z
1ecd3 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 49 64 78 [i] = 0;. *pIdx
1ecd4 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 = i;.}../*.** G
1ecd5 65 6e 65 72 61 74 65 20 61 20 43 52 45 41 54 45 enerate a CREATE
1ecd6 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
1ecd7 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 appropriate for
1ecd8 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 74 61 the given.** ta
1ecd9 62 6c 65 2e 20 20 4d 65 6d 6f 72 79 20 74 6f 20 ble. Memory to
1ecda 68 6f 6c 64 20 74 68 65 20 74 65 78 74 20 6f 66 hold the text of
1ecdb 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 the statement i
1ecdc 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 s obtained.** fr
1ecdd 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
1ecde 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 ) and must be fr
1ecdf 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 eed by the calli
1ece0 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a ng function..*/.
1ece1 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 72 65 static char *cre
1ece2 61 74 65 54 61 62 6c 65 53 74 6d 74 28 73 71 6c ateTableStmt(sql
1ece3 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 ite3 *db, Table
1ece4 2a 70 2c 20 69 6e 74 20 69 73 54 65 6d 70 29 7b *p, int isTemp){
1ece5 0a 20 20 69 6e 74 20 69 2c 20 6b 2c 20 6e 3b 0a . int i, k, n;.
1ece6 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 char *zStmt;.
1ece7 20 63 68 61 72 20 2a 7a 53 65 70 2c 20 2a 7a 53 char *zSep, *zS
1ece8 65 70 32 2c 20 2a 7a 45 6e 64 2c 20 2a 7a 3b 0a ep2, *zEnd, *z;.
1ece9 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a Column *pCol;.
1ecea 20 20 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 n = 0;. for(p
1eceb 43 6f 6c 20 3d 20 70 2d 3e 61 43 6f 6c 2c 20 69 Col = p->aCol, i
1ecec 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 =0; i<p->nCol; i
1eced 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 ++, pCol++){.
1ecee 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 n += identLengt
1ecef 68 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a h(pCol->zName);.
1ecf0 20 20 20 20 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54 z = pCol->zT
1ecf1 79 70 65 3b 0a 20 20 20 20 69 66 28 20 7a 20 29 ype;. if( z )
1ecf2 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 28 73 74 {. n += (st
1ecf3 72 6c 65 6e 28 7a 29 20 2b 20 31 29 3b 0a 20 20 rlen(z) + 1);.
1ecf4 20 20 7d 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20 69 }. }. n += i
1ecf5 64 65 6e 74 4c 65 6e 67 74 68 28 70 2d 3e 7a 4e dentLength(p->zN
1ecf6 61 6d 65 29 3b 0a 20 20 69 66 28 20 6e 3c 35 30 ame);. if( n<50
1ecf7 20 29 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 ){. zSep = "
1ecf8 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22 ";. zSep2 = "
1ecf9 2c 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20 22 ,";. zEnd = "
1ecfa 29 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 )";. }else{.
1ecfb 20 7a 53 65 70 20 3d 20 22 5c 6e 20 20 22 3b 0a zSep = "\n ";.
1ecfc 20 20 20 20 7a 53 65 70 32 20 3d 20 22 2c 5c 6e zSep2 = ",\n
1ecfd 20 20 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20 ";. zEnd =
1ecfe 22 5c 6e 29 22 3b 0a 20 20 7d 0a 20 20 6e 20 2b "\n)";. }. n +
1ecff 3d 20 33 35 20 2b 20 36 2a 70 2d 3e 6e 43 6f 6c = 35 + 6*p->nCol
1ed00 3b 0a 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 ;. zStmt = sqli
1ed01 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a te3Malloc( n );.
1ed02 20 20 69 66 28 20 7a 53 74 6d 74 3d 3d 30 20 29 if( zStmt==0 )
1ed03 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 {. db->malloc
1ed04 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
1ed05 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1ed06 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1ed07 28 6e 2c 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 (n, zStmt,.
1ed08 20 20 20 20 20 20 20 20 20 20 20 20 20 21 4f 4d !OM
1ed09 49 54 5f 54 45 4d 50 44 42 26 26 69 73 54 65 6d IT_TEMPDB&&isTem
1ed0a 70 20 3f 20 22 43 52 45 41 54 45 20 54 45 4d 50 p ? "CREATE TEMP
1ed0b 20 54 41 42 4c 45 20 22 3a 22 43 52 45 41 54 45 TABLE ":"CREATE
1ed0c 20 54 41 42 4c 45 20 22 29 3b 0a 20 20 6b 20 3d TABLE ");. k =
1ed0d 20 73 74 72 6c 65 6e 28 7a 53 74 6d 74 29 3b 0a strlen(zStmt);.
1ed0e 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d 74 identPut(zStmt
1ed0f 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b , &k, p->zName);
1ed10 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 . zStmt[k++] =
1ed11 27 28 27 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 3d '(';. for(pCol=
1ed12 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c p->aCol, i=0; i<
1ed13 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 p->nCol; i++, pC
1ed14 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 ol++){. sqlit
1ed15 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c e3_snprintf(n-k,
1ed16 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 7a 53 65 70 &zStmt[k], zSep
1ed17 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 74 72 6c );. k += strl
1ed18 65 6e 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20 en(&zStmt[k]);.
1ed19 20 20 20 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b zSep = zSep2;
1ed1a 0a 20 20 20 20 69 64 65 6e 74 50 75 74 28 7a 53 . identPut(zS
1ed1b 74 6d 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a tmt, &k, pCol->z
1ed1c 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 28 Name);. if( (
1ed1d 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 z = pCol->zType)
1ed1e 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 !=0 ){. zSt
1ed1f 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 mt[k++] = ' ';.
1ed20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 74 72 assert( str
1ed21 6c 65 6e 28 7a 29 2b 6b 2b 31 3c 3d 6e 20 29 3b len(z)+k+1<=n );
1ed22 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1ed23 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 nprintf(n-k, &zS
1ed24 74 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 29 tmt[k], "%s", z)
1ed25 3b 0a 20 20 20 20 20 20 6b 20 2b 3d 20 73 74 72 ;. k += str
1ed26 6c 65 6e 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 len(z);. }.
1ed27 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 }. sqlite3_snpr
1ed28 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 intf(n-k, &zStmt
1ed29 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 [k], "%s", zEnd)
1ed2a 3b 0a 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 ;. return zStmt
1ed2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
1ed2c 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
1ed2d 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 d to report the
1ed2e 66 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 final ")" that t
1ed2f 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 erminates.** a C
1ed30 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
1ed31 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ement..**.** The
1ed32 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 table structure
1ed33 20 74 68 61 74 20 6f 74 68 65 72 20 61 63 74 69 that other acti
1ed34 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 on routines have
1ed35 20 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a been building.*
1ed36 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 * is added to th
1ed37 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 e internal hash
1ed38 74 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67 tables, assuming
1ed39 20 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a no errors have.
1ed3a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a ** occurred..**.
1ed3b 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20 ** An entry for
1ed3c 74 68 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64 the table is mad
1ed3d 65 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 e in the master
1ed3e 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75 table on disk, u
1ed3f 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73 nless.** this is
1ed40 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 a temporary tab
1ed41 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62 le or db->init.b
1ed42 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62 usy==1. When db
1ed43 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a ->init.busy==1.*
1ed44 2a 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 * it means we ar
1ed45 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 e reading the sq
1ed46 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c lite_master tabl
1ed47 65 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 e because we jus
1ed48 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74 t.** connected t
1ed49 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f o the database o
1ed4a 72 20 62 65 63 61 75 73 65 20 74 68 65 20 73 71 r because the sq
1ed4b 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c lite_master tabl
1ed4c 65 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c e has.** recentl
1ed4d 79 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 y changed, so th
1ed4e 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 e entry for this
1ed4f 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 table already e
1ed50 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 xists in.** the
1ed51 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
1ed52 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 ble. We do not
1ed53 77 61 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 want to create i
1ed54 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 t again..**.** I
1ed55 66 20 74 68 65 20 70 53 65 6c 65 63 74 20 61 72 f the pSelect ar
1ed56 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 gument is not NU
1ed57 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 LL, it means tha
1ed58 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a t this routine.*
1ed59 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 * was called to
1ed5a 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 67 create a table g
1ed5b 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20 enerated from a
1ed5c 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c .** "CREATE TABL
1ed5d 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20 E ... AS SELECT
1ed5e 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 ..." statement.
1ed5f 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 The column name
1ed60 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 s of.** the new
1ed61 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68 table will match
1ed62 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1ed63 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a of the SELECT..*
1ed64 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ed65 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 void sqlite3End
1ed66 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a Table(. Parse *
1ed67 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
1ed68 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 /* Parse contex
1ed69 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 t */. Token *pC
1ed6a 6f 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ons, /
1ed6b 2a 20 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20 * The ',' token
1ed6c 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 after the last c
1ed6d 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20 olumn defn. */.
1ed6e 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 Token *pEnd,
1ed6f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1ed70 66 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20 final ')' token
1ed71 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 in the CREATE TA
1ed72 42 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 BLE */. Select
1ed73 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 20 *pSelect
1ed74 20 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 /* Select from
1ed75 61 20 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 a "CREATE ... AS
1ed76 20 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 SELECT" */.){.
1ed77 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c Table *p;. sql
1ed78 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
1ed79 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 e->db;. int iDb
1ed7a 3b 0a 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d ;.. if( (pEnd==
1ed7b 30 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 0 && pSelect==0)
1ed7c 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 || pParse->nErr
1ed7d 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
1ed7e 69 6c 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74 iled ) {. ret
1ed7f 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 urn;. }. p = p
1ed80 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
1ed81 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
1ed82 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 eturn;.. assert
1ed83 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 ( !db->init.busy
1ed84 20 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a || !pSelect );.
1ed85 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 . iDb = sqlite3
1ed86 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
1ed87 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a , p->pSchema);..
1ed88 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ed89 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 MIT_CHECK. /* R
1ed8a 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 esolve names in
1ed8b 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 all CHECK constr
1ed8c 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 aint expressions
1ed8d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
1ed8e 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72 pCheck ){. Sr
1ed8f 63 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20 20 cList sSrc;
1ed90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ed91 20 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f Fake SrcList fo
1ed92 72 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 r pParse->pNewTa
1ed93 62 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 ble */. NameC
1ed94 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 ontext sNC;
1ed95 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
1ed96 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 me context for p
1ed97 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
1ed98 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 */.. memset(
1ed99 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 &sNC, 0, sizeof(
1ed9a 73 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 sNC));. memse
1ed9b 74 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 t(&sSrc, 0, size
1ed9c 6f 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73 of(sSrc));. s
1ed9d 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 Src.nSrc = 1;.
1ed9e 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d sSrc.a[0].zNam
1ed9f 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 e = p->zName;.
1eda0 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62 sSrc.a[0].pTab
1eda1 20 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61 = p;. sSrc.a
1eda2 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 [0].iCursor = -1
1eda3 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 ;. sNC.pParse
1eda4 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 = pParse;. s
1eda5 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73 NC.pSrcList = &s
1eda6 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43 Src;. sNC.isC
1eda7 68 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66 heck = 1;. if
1eda8 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 ( sqlite3Resolve
1eda9 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 ExprNames(&sNC,
1edaa 70 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 p->pCheck) ){.
1edab 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
1edac 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }. }.#endif /*
1edad 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1edae 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a OMIT_CHECK) */..
1edaf 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e /* If the db->
1edb0 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 69 init.busy is 1 i
1edb1 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 t means we are r
1edb2 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f eading the SQL o
1edb3 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c ff the. ** "sql
1edb4 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 ite_master" or "
1edb5 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 sqlite_temp_mast
1edb6 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 er" table on the
1edb7 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64 disk.. ** So d
1edb8 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 o not write to t
1edb9 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20 he disk again.
1edba 45 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74 Extract the root
1edbb 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a page number. *
1edbc 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 * for the table
1edbd 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69 from the db->ini
1edbe 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e t.newTnum field.
1edbf 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62 (The page numb
1edc0 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 er. ** should h
1edc1 61 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 65 ave been put the
1edc2 72 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 re by the sqlite
1edc3 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29 OpenCb routine.)
1edc4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e . */. if( db->
1edc5 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 init.busy ){.
1edc6 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 p->tnum = db->i
1edc7 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d nit.newTnum;. }
1edc8 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e .. /* If not in
1edc9 69 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e itializing, then
1edca 20 63 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 create a record
1edcb 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 for the new tab
1edcc 6c 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53 le. ** in the S
1edcd 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 QLITE_MASTER tab
1edce 6c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 le of the databa
1edcf 73 65 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20 se. The record
1edd0 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 number. ** for
1edd1 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 65 6e the new table en
1edd2 74 72 79 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 try should alrea
1edd3 64 79 20 62 65 20 6f 6e 20 74 68 65 20 73 74 61 dy be on the sta
1edd4 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 ck.. **. ** If
1edd5 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f this is a TEMPO
1edd6 52 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74 RARY table, writ
1edd7 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f e the entry into
1edd8 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20 the auxiliary.
1edd9 20 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 ** file instead
1edda 20 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 of into the mai
1eddb 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
1eddc 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d . */. if( !db-
1eddd 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 >init.busy ){.
1edde 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62 int n;. Vdb
1eddf 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a e *v;. char *
1ede0 7a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69 zType; /* "vi
1ede1 65 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a ew" or "table" *
1ede2 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 /. char *zTyp
1ede3 65 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20 e2; /* "VIEW"
1ede4 6f 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20 or "TABLE" */.
1ede5 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20 char *zStmt;
1ede6 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 /* Text of the
1ede7 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 CREATE TABLE or
1ede8 20 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61 CREATE VIEW sta
1ede9 74 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76 tement */.. v
1edea 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
1edeb 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 e(pParse);. i
1edec 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e f( v==0 ) return
1eded 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ;.. sqlite3Vd
1edee 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 beAddOp1(v, OP_C
1edef 6c 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f lose, 0);.. /
1edf0 2a 20 43 72 65 61 74 65 20 74 68 65 20 72 6f 6f * Create the roo
1edf1 74 70 61 67 65 20 66 6f 72 20 74 68 65 20 6e 65 tpage for the ne
1edf2 77 20 74 61 62 6c 65 20 61 6e 64 20 70 75 73 68 w table and push
1edf3 20 69 74 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 it onto the sta
1edf4 63 6b 2e 0a 20 20 20 20 2a 2a 20 41 20 76 69 65 ck.. ** A vie
1edf5 77 20 68 61 73 20 6e 6f 20 72 6f 6f 74 70 61 67 w has no rootpag
1edf6 65 2c 20 73 6f 20 6a 75 73 74 20 70 75 73 68 20 e, so just push
1edf7 61 20 7a 65 72 6f 20 6f 6e 74 6f 20 74 68 65 20 a zero onto the
1edf8 73 74 61 63 6b 20 66 6f 72 0a 20 20 20 20 2a 2a stack for. **
1edf9 20 76 69 65 77 73 2e 20 20 49 6e 69 74 69 61 6c views. Initial
1edfa 69 7a 65 20 7a 54 79 70 65 20 61 74 20 74 68 65 ize zType at the
1edfb 20 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 20 20 same time..
1edfc 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 53 */. if( p->pS
1edfd 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 elect==0 ){.
1edfe 20 20 2f 2a 20 41 20 72 65 67 75 6c 61 72 20 74 /* A regular t
1edff 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 able */. zT
1ee00 79 70 65 20 3d 20 22 74 61 62 6c 65 22 3b 0a 20 ype = "table";.
1ee01 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 54 zType2 = "T
1ee02 41 42 4c 45 22 3b 0a 23 69 66 6e 64 65 66 20 53 ABLE";.#ifndef S
1ee03 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a QLITE_OMIT_VIEW.
1ee04 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ee05 20 2f 2a 20 41 20 76 69 65 77 20 2a 2f 0a 20 20 /* A view */.
1ee06 20 20 20 20 7a 54 79 70 65 20 3d 20 22 76 69 65 zType = "vie
1ee07 77 22 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32 w";. zType2
1ee08 20 3d 20 22 56 49 45 57 22 3b 0a 23 65 6e 64 69 = "VIEW";.#endi
1ee09 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 f. }.. /*
1ee0a 49 66 20 74 68 69 73 20 69 73 20 61 20 43 52 45 If this is a CRE
1ee0b 41 54 45 20 54 41 42 4c 45 20 78 78 20 41 53 20 ATE TABLE xx AS
1ee0c 53 45 4c 45 43 54 20 2e 2e 2e 2c 20 65 78 65 63 SELECT ..., exec
1ee0d 75 74 65 20 74 68 65 20 53 45 4c 45 43 54 0a 20 ute the SELECT.
1ee0e 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 ** statement
1ee0f 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 to populate the
1ee10 6e 65 77 20 74 61 62 6c 65 2e 20 54 68 65 20 72 new table. The r
1ee11 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 oot-page number
1ee12 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e for the. ** n
1ee13 65 77 20 74 61 62 6c 65 20 69 73 20 6f 6e 20 74 ew table is on t
1ee14 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 76 64 he top of the vd
1ee15 62 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2a be stack.. **
1ee16 0a 20 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65 . ** Once the
1ee17 20 53 45 4c 45 43 54 20 68 61 73 20 62 65 65 6e SELECT has been
1ee18 20 63 6f 64 65 64 20 62 79 20 73 71 6c 69 74 65 coded by sqlite
1ee19 33 53 65 6c 65 63 74 28 29 2c 20 69 74 20 69 73 3Select(), it is
1ee1a 20 69 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75 69 in a. ** sui
1ee1b 74 61 62 6c 65 20 73 74 61 74 65 20 74 6f 20 71 table state to q
1ee1c 75 65 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c uery for the col
1ee1d 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 79 umn names and ty
1ee1e 70 65 73 20 74 6f 20 62 65 20 75 73 65 64 0a 20 pes to be used.
1ee1f 20 20 20 2a 2a 20 62 79 20 74 68 65 20 6e 65 77 ** by the new
1ee20 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 table.. **.
1ee21 20 20 20 2a 2a 20 41 20 73 68 61 72 65 64 2d 63 ** A shared-c
1ee22 61 63 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 ache write-lock
1ee23 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 is not required
1ee24 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 to write to the
1ee25 6e 65 77 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a new table,. *
1ee26 2a 20 61 73 20 61 20 73 63 68 65 6d 61 2d 6c 6f * as a schema-lo
1ee27 63 6b 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72 ck must have alr
1ee28 65 61 64 79 20 62 65 65 6e 20 6f 62 74 61 69 6e eady been obtain
1ee29 65 64 20 74 6f 20 63 72 65 61 74 65 20 69 74 2e ed to create it.
1ee2a 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61 20 Since. ** a
1ee2b 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63 6c schema-lock excl
1ee2c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 64 udes all other d
1ee2d 61 74 61 62 61 73 65 20 75 73 65 72 73 2c 20 74 atabase users, t
1ee2e 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 6f he write-lock wo
1ee2f 75 6c 64 0a 20 20 20 20 2a 2a 20 62 65 20 72 65 uld. ** be re
1ee30 64 75 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f 0a dundant.. */.
1ee31 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 20 if( pSelect
1ee32 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44 ){. SelectD
1ee33 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20 est dest;.
1ee34 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 0a Table *pSelTab;.
1ee35 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 . assert(pP
1ee36 61 72 73 65 2d 3e 6e 54 61 62 3d 3d 30 29 3b 0a arse->nTab==0);.
1ee37 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1ee38 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 eAddOp3(v, OP_Op
1ee39 65 6e 57 72 69 74 65 2c 20 31 2c 20 70 50 61 72 enWrite, 1, pPar
1ee3a 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44 62 se->regRoot, iDb
1ee3b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1ee3c 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 VdbeChangeP5(v,
1ee3d 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 1);. pParse
1ee3e 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 20 ->nTab = 2;.
1ee3f 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
1ee40 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 estInit(&dest, S
1ee41 52 54 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20 RT_Table, 1);.
1ee42 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
1ee43 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 t(pParse, pSelec
1ee44 74 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 t, &dest);.
1ee45 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1ee46 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 p1(v, OP_Close,
1ee47 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 1);. if( pP
1ee48 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b arse->nErr==0 ){
1ee49 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 . pSelTab
1ee4a 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 = sqlite3Result
1ee4b 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 SetOfSelect(pPar
1ee4c 73 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 se, pSelect);.
1ee4d 20 20 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 if( pSelTa
1ee4e 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 b==0 ) return;.
1ee4f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1ee50 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 ->aCol==0 );.
1ee51 20 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70 p->nCol = p
1ee52 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 SelTab->nCol;.
1ee53 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 p->aCol =
1ee54 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 pSelTab->aCol;.
1ee55 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e pSelTab->
1ee56 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 nCol = 0;.
1ee57 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 pSelTab->aCol
1ee58 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c = 0;. sql
1ee59 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 ite3DeleteTable(
1ee5a 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 pSelTab);.
1ee5b 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
1ee5c 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70 Compute the comp
1ee5d 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 lete text of the
1ee5e 20 43 52 45 41 54 45 20 73 74 61 74 65 6d 65 6e CREATE statemen
1ee5f 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 65 t */. if( pSe
1ee60 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 7a 53 lect ){. zS
1ee61 74 6d 74 20 3d 20 63 72 65 61 74 65 54 61 62 6c tmt = createTabl
1ee62 65 53 74 6d 74 28 64 62 2c 20 70 2c 20 70 2d 3e eStmt(db, p, p->
1ee63 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 pSchema==db->aDb
1ee64 5b 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 [1].pSchema);.
1ee65 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
1ee66 20 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 = pEnd->z - pPa
1ee67 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e rse->sNameToken.
1ee68 7a 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74 z + 1;. zSt
1ee69 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 mt = sqlite3MPri
1ee6a 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 ntf(db, .
1ee6b 20 20 20 22 43 52 45 41 54 45 20 25 73 20 25 2e "CREATE %s %.
1ee6c 2a 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 *s", zType2, n,
1ee6d 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b pParse->sNameTok
1ee6e 65 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20 en.z. );.
1ee6f 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c }.. /* A sl
1ee70 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 ot for the recor
1ee71 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 d has already be
1ee72 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 en allocated in
1ee73 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 the . ** SQLI
1ee74 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e TE_MASTER table.
1ee75 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 We just need t
1ee76 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c o update that sl
1ee77 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 ot with all.
1ee78 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 ** the informati
1ee79 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 on we've collect
1ee7a 65 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 66 ed. The rowid f
1ee7b 6f 72 20 74 68 65 20 70 72 65 61 6c 6c 6f 63 61 or the prealloca
1ee7c 74 65 64 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20 ted. ** slot
1ee7d 69 73 20 74 68 65 20 32 6e 64 20 69 74 65 6d 20 is the 2nd item
1ee7e 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 on the stack. T
1ee7f 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 he top of the st
1ee80 61 63 6b 20 69 73 20 74 68 65 0a 20 20 20 20 2a ack is the. *
1ee81 2a 20 72 6f 6f 74 20 70 61 67 65 20 66 6f 72 20 * root page for
1ee82 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 6f the new table (o
1ee83 72 20 61 20 30 20 69 66 20 74 68 69 73 20 69 73 r a 0 if this is
1ee84 20 61 20 76 69 65 77 29 2e 0a 20 20 20 20 2a 2f a view).. */
1ee85 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 . sqlite3Nest
1ee86 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a edParse(pParse,.
1ee87 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 "UPDATE %Q
1ee88 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22 .%s ". "
1ee89 53 45 54 20 74 79 70 65 3d 27 25 73 27 2c 20 6e SET type='%s', n
1ee8a 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65 ame=%Q, tbl_name
1ee8b 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 23 25 =%Q, rootpage=#%
1ee8c 64 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20 20 d, sql=%Q ".
1ee8d 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64 3d "WHERE rowid=
1ee8e 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e #%d",. db->
1ee8f 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 aDb[iDb].zName,
1ee90 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 SCHEMA_TABLE(iDb
1ee91 29 2c 0a 20 20 20 20 20 20 7a 54 79 70 65 2c 0a ),. zType,.
1ee92 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a p->zName,.
1ee93 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a p->zName,.
1ee94 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 pParse->re
1ee95 67 52 6f 6f 74 2c 0a 20 20 20 20 20 20 7a 53 74 gRoot,. zSt
1ee96 6d 74 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 mt,. pParse
1ee97 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 20 29 ->regRowid. )
1ee98 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
1ee99 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a ree(db, zStmt);.
1ee9a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 sqlite3Chang
1ee9b 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 eCookie(pParse,
1ee9c 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 iDb);..#ifndef S
1ee9d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
1ee9e 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 NCREMENT. /*
1ee9f 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
1eea0 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 we need to creat
1eea1 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 e an sqlite_sequ
1eea2 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72 0a 20 ence table for.
1eea3 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20 74 72 ** keeping tr
1eea4 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63 72 65 ack of autoincre
1eea5 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20 20 2a ment keys.. *
1eea6 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 61 62 /. if( p->tab
1eea7 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 Flags & TF_Autoi
1eea8 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 ncrement ){.
1eea9 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d Db *pDb = &db-
1eeaa 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 20 >aDb[iDb];.
1eeab 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d if( pDb->pSchem
1eeac 61 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b a->pSeqTab==0 ){
1eead 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1eeae 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 NestedParse(pPar
1eeaf 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 se,. "C
1eeb0 52 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 REATE TABLE %Q.s
1eeb1 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e qlite_sequence(n
1eeb2 61 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20 ame,seq)",.
1eeb3 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a pDb->zName.
1eeb4 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
1eeb5 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
1eeb6 0a 20 20 20 20 2f 2a 20 52 65 70 61 72 73 65 20 . /* Reparse
1eeb7 65 76 65 72 79 74 68 69 6e 67 20 74 6f 20 75 70 everything to up
1eeb8 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 date our interna
1eeb9 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 l data structure
1eeba 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 s */. sqlite3
1eebb 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1eebc 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 _ParseSchema, iD
1eebd 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 b, 0, 0,.
1eebe 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1eebf 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 27 25 db, "tbl_name='%
1eec0 71 27 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c 20 50 q'",p->zName), P
1eec1 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 4_DYNAMIC);. }.
1eec2 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 74 .. /* Add the t
1eec3 61 62 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d 6d able to the in-m
1eec4 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 emory representa
1eec5 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 tion of the data
1eec6 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 base.. */. if(
1eec7 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 db->init.busy &
1eec8 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d & pParse->nErr==
1eec9 30 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 0 ){. Table *
1eeca 70 4f 6c 64 3b 0a 20 20 20 20 46 4b 65 79 20 2a pOld;. FKey *
1eecb 70 46 4b 65 79 3b 20 0a 20 20 20 20 53 63 68 65 pFKey; . Sche
1eecc 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d ma *pSchema = p-
1eecd 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 4f >pSchema;. pO
1eece 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 ld = sqlite3Hash
1eecf 49 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d Insert(&pSchema-
1eed0 3e 74 62 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 >tblHash, p->zNa
1eed1 6d 65 2c 20 73 74 72 6c 65 6e 28 70 2d 3e 7a 4e me, strlen(p->zN
1eed2 61 6d 65 29 2b 31 2c 70 29 3b 0a 20 20 20 20 69 ame)+1,p);. i
1eed3 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 f( pOld ){.
1eed4 20 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 assert( p==pOld
1eed5 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d ); /* Malloc m
1eed6 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 ust have failed
1eed7 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 inside HashInser
1eed8 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d t() */. db-
1eed9 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
1eeda 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 1;. return;
1eedb 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
1eedc 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 QLITE_OMIT_FOREI
1eedd 47 4e 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70 GN_KEY. for(p
1eede 46 4b 65 79 3d 70 2d 3e 70 46 4b 65 79 3b 20 70 FKey=p->pFKey; p
1eedf 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 FKey; pFKey=pFKe
1eee0 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 y->pNextFrom){.
1eee1 20 20 20 20 20 76 6f 69 64 20 2a 64 61 74 61 3b void *data;
1eee2 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d . int nTo =
1eee3 20 73 74 72 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a strlen(pFKey->z
1eee4 54 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 To) + 1;. p
1eee5 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 FKey->pNextTo =
1eee6 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 sqlite3HashFind(
1eee7 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c &pSchema->aFKey,
1eee8 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54 6f pFKey->zTo, nTo
1eee9 29 3b 0a 20 20 20 20 20 20 64 61 74 61 20 3d 20 );. data =
1eeea 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 sqlite3HashInser
1eeeb 74 28 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 t(&pSchema->aFKe
1eeec 79 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e y, pFKey->zTo, n
1eeed 54 6f 2c 20 70 46 4b 65 79 29 3b 0a 20 20 20 20 To, pFKey);.
1eeee 20 20 69 66 28 20 64 61 74 61 3d 3d 28 76 6f 69 if( data==(voi
1eeef 64 20 2a 29 70 46 4b 65 79 20 29 7b 0a 20 20 20 d *)pFKey ){.
1eef0 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 db->mallocF
1eef1 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 ailed = 1;.
1eef2 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
1eef3 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 pParse->pNew
1eef4 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 64 Table = 0;. d
1eef5 62 2d 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20 20 b->nTable++;.
1eef6 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 db->flags |= SQ
1eef7 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 LITE_InternChang
1eef8 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c es;..#ifndef SQL
1eef9 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 ITE_OMIT_ALTERTA
1eefa 42 4c 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e BLE. if( !p->
1eefb 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 pSelect ){.
1eefc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
1eefd 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 me = (const char
1eefe 20 2a 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 *)pParse->sName
1eeff 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69 Token.z;. i
1ef00 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 nt nName;.
1ef01 61 73 73 65 72 74 28 20 21 70 53 65 6c 65 63 74 assert( !pSelect
1ef02 20 26 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e && pCons && pEn
1ef03 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 d );. if( p
1ef04 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 Cons->z==0 ){.
1ef05 20 20 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45 pCons = pE
1ef06 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 nd;. }.
1ef07 20 20 6e 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 nName = (const
1ef08 20 63 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a char *)pCons->z
1ef09 20 2d 20 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 - zName;.
1ef0a 70 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 p->addColOffset
1ef0b 3d 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 = 13 + sqlite3Ut
1ef0c 66 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c f8CharLen(zName,
1ef0d 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 nName);. }.#
1ef0e 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 endif. }.}..#if
1ef0f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1ef10 5f 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 _VIEW./*.** The
1ef11 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 parser calls thi
1ef12 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 s routine in ord
1ef13 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e er to create a n
1ef14 65 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 ew VIEW.*/.SQLIT
1ef15 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1ef16 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 qlite3CreateView
1ef17 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1ef18 65 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 e, /* The pa
1ef19 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
1ef1a 0a 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e . Token *pBegin
1ef1b 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 , /* The CRE
1ef1c 41 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 ATE token that b
1ef1d 65 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d egins the statem
1ef1e 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a ent */. Token *
1ef1f 70 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 pName1, /* T
1ef20 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f he token that ho
1ef21 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 lds the name of
1ef22 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f the view */. To
1ef23 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 ken *pName2,
1ef24 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 /* The token th
1ef25 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d at holds the nam
1ef26 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f e of the view */
1ef27 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
1ef28 63 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 ct, /* A SELEC
1ef29 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 T statement that
1ef2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 will become the
1ef2b 20 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 new view */. i
1ef2c 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 nt isTemp,
1ef2d 20 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 /* TRUE for a
1ef2e 54 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a TEMPORARY view *
1ef2f 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 /. int noErr
1ef30 20 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 /* Suppre
1ef31 73 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 ss error message
1ef32 73 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 s if VIEW alread
1ef33 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 y exists */.){.
1ef34 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 Table *p;. int
1ef35 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 n;. const unsi
1ef36 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 gned char *z;.
1ef37 54 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62 Token sEnd;. Db
1ef38 46 69 78 65 72 20 73 46 69 78 3b 0a 20 20 54 6f Fixer sFix;. To
1ef39 6b 65 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e ken *pName;. in
1ef3a 74 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 t iDb;. sqlite3
1ef3b 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
1ef3c 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 b;.. if( pParse
1ef3d 2d 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 ->nVar>0 ){.
1ef3e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1ef3f 70 50 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74 pParse, "paramet
1ef40 65 72 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f ers are not allo
1ef41 77 65 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a wed in views");.
1ef42 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
1ef43 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c tDelete(db, pSel
1ef44 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ect);. return
1ef45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 ;. }. sqlite3S
1ef46 74 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 tartTable(pParse
1ef47 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 , pName1, pName2
1ef48 2c 20 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 , isTemp, 1, 0,
1ef49 6e 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 noErr);. p = pP
1ef4a 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b arse->pNewTable;
1ef4b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 . if( p==0 || p
1ef4c 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 Parse->nErr ){.
1ef4d 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
1ef4e 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 Delete(db, pSele
1ef4f 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b ct);. return;
1ef50 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 54 77 . }. sqlite3Tw
1ef51 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 oPartName(pParse
1ef52 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 , pName1, pName2
1ef53 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 , &pName);. iDb
1ef54 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 = sqlite3Schema
1ef55 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 ToIndex(db, p->p
1ef56 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 73 Schema);. if( s
1ef57 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 qlite3FixInit(&s
1ef58 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 Fix, pParse, iDb
1ef59 2c 20 22 76 69 65 77 22 2c 20 70 4e 61 6d 65 29 , "view", pName)
1ef5a 0a 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 . && sqlite3F
1ef5b 69 78 53 65 6c 65 63 74 28 26 73 46 69 78 2c 20 ixSelect(&sFix,
1ef5c 70 53 65 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20 pSelect). ){.
1ef5d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
1ef5e 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 elete(db, pSelec
1ef5f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a t);. return;.
1ef60 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 }.. /* Make a
1ef61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 6e 74 copy of the ent
1ef62 69 72 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 ire SELECT state
1ef63 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 ment that define
1ef64 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2a s the view.. **
1ef65 20 54 68 69 73 20 77 69 6c 6c 20 66 6f 72 63 65 This will force
1ef66 20 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f all the Expr.to
1ef67 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20 ken.z values to
1ef68 62 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 be dynamically.
1ef69 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 72 61 ** allocated ra
1ef6a 74 68 65 72 20 74 68 61 6e 20 70 6f 69 6e 74 20 ther than point
1ef6b 74 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 to the input str
1ef6c 69 6e 67 20 2d 20 77 68 69 63 68 20 6d 65 61 6e ing - which mean
1ef6d 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 s that. ** they
1ef6e 20 77 69 6c 6c 20 70 65 72 73 69 73 74 20 61 66 will persist af
1ef6f 74 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 ter the current
1ef70 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 sqlite3_exec() c
1ef71 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a all returns.. *
1ef72 2f 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d /. p->pSelect =
1ef73 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 sqlite3SelectDu
1ef74 70 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a p(db, pSelect);.
1ef75 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
1ef76 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 elete(db, pSelec
1ef77 74 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 t);. if( db->ma
1ef78 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1ef79 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
1ef7a 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 if( !db->init.bu
1ef7b 73 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 sy ){. sqlite
1ef7c 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 3ViewGetColumnNa
1ef7d 6d 65 73 28 70 50 61 72 73 65 2c 20 70 29 3b 0a mes(pParse, p);.
1ef7e 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 }.. /* Locate
1ef7f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1ef80 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 CREATE VIEW stat
1ef81 65 6d 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e ement. Make sEn
1ef82 64 20 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 d point to. **
1ef83 74 68 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 the end.. */.
1ef84 73 45 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 sEnd = pParse->s
1ef85 4c 61 73 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 LastToken;. if(
1ef86 20 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30 20 26 26 sEnd.z[0]!=0 &&
1ef87 20 73 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 sEnd.z[0]!=';'
1ef88 29 7b 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d ){. sEnd.z +=
1ef89 20 73 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 sEnd.n;. }. s
1ef8a 45 6e 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d End.n = 0;. n =
1ef8b 20 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e sEnd.z - pBegin
1ef8c 2d 3e 7a 3b 0a 20 20 7a 20 3d 20 28 63 6f 6e 73 ->z;. z = (cons
1ef8d 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a t unsigned char*
1ef8e 29 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 )pBegin->z;. wh
1ef8f 69 6c 65 28 20 6e 3e 30 20 26 26 20 28 7a 5b 6e ile( n>0 && (z[n
1ef90 2d 31 5d 3d 3d 27 3b 27 20 7c 7c 20 69 73 73 70 -1]==';' || issp
1ef91 61 63 65 28 7a 5b 6e 2d 31 5d 29 29 20 29 7b 20 ace(z[n-1])) ){
1ef92 6e 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 n--; }. sEnd.z
1ef93 3d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e = &z[n-1];. sEn
1ef94 64 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 d.n = 1;.. /* U
1ef95 73 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 se sqlite3EndTab
1ef96 6c 65 28 29 20 74 6f 20 61 64 64 20 74 68 65 20 le() to add the
1ef97 76 69 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49 view to the SQLI
1ef98 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 TE_MASTER table
1ef99 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 */. sqlite3EndT
1ef9a 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 able(pParse, 0,
1ef9b 26 73 45 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 &sEnd, 0);. ret
1ef9c 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a urn;.}.#endif /*
1ef9d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 SQLITE_OMIT_VIE
1ef9e 57 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e W */..#if !defin
1ef9f 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
1efa0 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 IEW) || !defined
1efa1 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 (SQLITE_OMIT_VIR
1efa2 54 55 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a TUALTABLE)./*.**
1efa3 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 The Table struc
1efa4 74 75 72 65 20 70 54 61 62 6c 65 20 69 73 20 72 ture pTable is r
1efa5 65 61 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46 eally a VIEW. F
1efa6 69 6c 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 ill in the names
1efa7 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d of.** the colum
1efa8 6e 73 20 6f 66 20 74 68 65 20 76 69 65 77 20 69 ns of the view i
1efa9 6e 20 74 68 65 20 70 54 61 62 6c 65 20 73 74 72 n the pTable str
1efaa 75 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 ucture. Return
1efab 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 the number.** of
1efac 20 65 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 errors. If an
1efad 65 72 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 error is seen le
1efae 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ave an error mes
1efaf 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e sage in pParse->
1efb0 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 zErrMsg..*/.SQLI
1efb1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1efb2 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c qlite3ViewGetCol
1efb3 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a umnNames(Parse *
1efb4 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 pParse, Table *p
1efb5 54 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 Table){. Table
1efb6 2a 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 *pSelTab; /* A
1efb7 20 66 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d fake table from
1efb8 20 77 68 69 63 68 20 77 65 20 67 65 74 20 74 68 which we get th
1efb9 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a e result set */.
1efba 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 Select *pSel;
1efbb 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 /* Copy of t
1efbc 68 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 he SELECT that i
1efbd 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 mplements the vi
1efbe 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 ew */. int nErr
1efbf 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d = 0; /* Num
1efc0 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e ber of errors en
1efc1 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 countered */. i
1efc2 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 nt n;
1efc3 20 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 /* Temporarily
1efc4 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72 holds the number
1efc5 20 6f 66 20 63 75 72 73 6f 72 73 20 61 73 73 69 of cursors assi
1efc6 67 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 gned */. sqlite
1efc7 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1efc8 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 db; /* Database
1efc9 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 connection for
1efca 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f malloc errors */
1efcb 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 . int (*xAuth)(
1efcc 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 void*,int,const
1efcd 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
1efce 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f *,const char*,co
1efcf 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 nst char*);.. a
1efd0 73 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b ssert( pTable );
1efd1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1efd2 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1efd3 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 LE. if( sqlite3
1efd4 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 VtabCallConnect(
1efd5 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 29 20 pParse, pTable)
1efd6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
1efd7 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
1efd8 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 if( IsVirtual(
1efd9 70 54 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e pTable) ) return
1efda 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 0;.#endif..#ifn
1efdb 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1efdc 56 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 VIEW. /* A posi
1efdd 74 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 tive nCol means
1efde 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 the columns name
1efdf 73 20 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 s for this view
1efe0 61 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 are. ** already
1efe1 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 known.. */. i
1efe2 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e f( pTable->nCol>
1efe3 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 0 ) return 0;..
1efe4 20 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e /* A negative n
1efe5 43 6f 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c Col is a special
1efe6 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 marker meaning
1efe7 74 68 61 74 20 77 65 20 61 72 65 20 63 75 72 72 that we are curr
1efe8 65 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e ently. ** tryin
1efe9 67 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 g to compute the
1efea 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 column names.
1efeb 49 66 20 77 65 20 65 6e 74 65 72 20 74 68 69 73 If we enter this
1efec 20 72 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 routine with.
1efed 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43 ** a negative nC
1efee 6f 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f ol, it means two
1efef 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66 or more views f
1eff0 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 orm a loop, like
1eff1 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a this:. **. **
1eff2 20 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 CREATE VIEW
1eff3 20 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a one AS SELECT *
1eff4 20 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 FROM two;. **
1eff5 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 CREATE VIEW
1eff6 74 77 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20 two AS SELECT *
1eff7 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 FROM one;. **.
1eff8 20 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 ** Actually, th
1eff9 69 73 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 is error is caug
1effa 68 74 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6e ht previously an
1effb 64 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 d so the followi
1effc 6e 67 20 74 65 73 74 0a 20 20 2a 2a 20 73 68 6f ng test. ** sho
1effd 75 6c 64 20 61 6c 77 61 79 73 20 66 61 69 6c 2e uld always fail.
1effe 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 But we will le
1efff 61 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 ave it in place
1f000 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e just to be safe.
1f001 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 . */. if( pTab
1f002 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 le->nCol<0 ){.
1f003 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1f004 67 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 g(pParse, "view
1f005 25 73 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79 %s is circularly
1f006 20 64 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c defined", pTabl
1f007 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 e->zName);. r
1f008 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 eturn 1;. }. a
1f009 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e ssert( pTable->n
1f00a 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 Col>=0 );.. /*
1f00b 49 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 If we get this f
1f00c 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20 ar, it means we
1f00d 6e 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 need to compute
1f00e 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e the table names.
1f00f 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 . ** Note that
1f010 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 the call to sqli
1f011 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 te3ResultSetOfSe
1f012 6c 65 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61 lect() will expa
1f013 6e 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 nd any. ** "*"
1f014 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 elements in the
1f015 72 65 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74 results set of t
1f016 68 65 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c he view and will
1f017 20 61 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a assign cursors.
1f018 20 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d ** to the elem
1f019 65 6e 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d ents of the FROM
1f01a 20 63 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65 clause. But we
1f01b 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 do not want the
1f01c 73 65 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 se changes. **
1f01d 74 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e to be permanent.
1f01e 20 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 So the computa
1f01f 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 tion is done on
1f020 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 a copy of the SE
1f021 4c 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d LECT. ** statem
1f022 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 ent that defines
1f023 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a the view.. */.
1f024 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 assert( pTable
1f025 2d 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 ->pSelect );. p
1f026 53 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c Sel = sqlite3Sel
1f027 65 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c ectDup(db, pTabl
1f028 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 e->pSelect);. i
1f029 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 6e f( pSel ){. n
1f02a 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b = pParse->nTab;
1f02b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c . sqlite3SrcL
1f02c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 istAssignCursors
1f02d 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70 (pParse, pSel->p
1f02e 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 Src);. pTable
1f02f 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 23 69 66 ->nCol = -1;.#if
1f030 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1f031 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 _AUTHORIZATION.
1f032 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 xAuth = db->x
1f033 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41 Auth;. db->xA
1f034 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53 65 uth = 0;. pSe
1f035 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 lTab = sqlite3Re
1f036 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 sultSetOfSelect(
1f037 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 20 pParse, pSel);.
1f038 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 db->xAuth = x
1f039 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20 20 Auth;.#else.
1f03a 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 pSelTab = sqlite
1f03b 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 3ResultSetOfSele
1f03c 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 ct(pParse, pSel)
1f03d 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61 ;.#endif. pPa
1f03e 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 rse->nTab = n;.
1f03f 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29 if( pSelTab )
1f040 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1f041 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 pTable->aCol==0
1f042 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d );. pTable-
1f043 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d >nCol = pSelTab-
1f044 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 >nCol;. pTa
1f045 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c ble->aCol = pSel
1f046 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 Tab->aCol;.
1f047 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d pSelTab->nCol =
1f048 20 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 0;. pSelTa
1f049 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 b->aCol = 0;.
1f04a 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 sqlite3Delete
1f04b 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a Table(pSelTab);.
1f04c 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 pTable->pS
1f04d 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 chema->flags |=
1f04e 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b DB_UnresetViews;
1f04f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1f050 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d pTable->nCol =
1f051 20 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 0;. nErr++
1f052 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
1f053 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 te3SelectDelete(
1f054 64 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65 db, pSel);. } e
1f055 6c 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b lse {. nErr++
1f056 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
1f057 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
1f058 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 */. return nEr
1f059 72 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a r; .}.#endif /*
1f05a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1f05b 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 _OMIT_VIEW) || !
1f05c 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1f05d 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1f05e 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 ) */..#ifndef SQ
1f05f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f LITE_OMIT_VIEW./
1f060 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 *.** Clear the c
1f061 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d olumn names from
1f062 20 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 every VIEW in d
1f063 61 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a atabase idx..*/.
1f064 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
1f065 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 teViewResetAll(s
1f066 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
1f067 69 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d idx){. HashElem
1f068 20 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 *i;. if( !DbHa
1f069 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 sProperty(db, id
1f06a 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 x, DB_UnresetVie
1f06b 77 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ws) ) return;.
1f06c 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 for(i=sqliteHash
1f06d 46 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 First(&db->aDb[i
1f06e 64 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c dx].pSchema->tbl
1f06f 48 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 Hash); i;i=sqlit
1f070 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 eHashNext(i)){.
1f071 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d Table *pTab =
1f072 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 sqliteHashData(
1f073 69 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 i);. if( pTab
1f074 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 ->pSelect ){.
1f075 20 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f sqliteResetCo
1f076 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b lumnNames(pTab);
1f077 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 . }. }. DbC
1f078 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c learProperty(db,
1f079 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 idx, DB_Unreset
1f07a 56 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a Views);.}.#else.
1f07b 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 # define sqliteV
1f07c 69 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 iewResetAll(A,B)
1f07d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1f07e 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a E_OMIT_VIEW */..
1f07f 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1f080 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ion is called by
1f081 20 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a the VDBE to adj
1f082 75 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ust the internal
1f083 20 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 schema.** used
1f084 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 by SQLite when t
1f085 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d he btree layer m
1f086 6f 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f oves a table roo
1f087 74 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 t page. The.** r
1f088 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 oot-page of a ta
1f089 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 ble or index in
1f08a 64 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73 database iDb has
1f08b 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 changed from iF
1f08c 72 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a rom.** to iTo..*
1f08d 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 *.** Ticket #172
1f08e 38 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 8: The symbol t
1f08f 61 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c able might still
1f090 20 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 contain informa
1f091 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 tion.** on table
1f092 73 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 s and/or indices
1f093 20 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72 that are the pr
1f094 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 ocess of being d
1f095 65 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f eleted..** If yo
1f096 75 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f u are unlucky, o
1f097 6e 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 ne of those dele
1f098 74 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 ted indices or t
1f099 61 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 ables might.** h
1f09a 61 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f ave the same roo
1f09b 74 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 tpage number as
1f09c 74 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f the real table o
1f09d 72 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a r index that is.
1f09e 2a 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 ** being moved.
1f09f 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 So we cannot st
1f0a0 6f 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 op searching aft
1f0a1 65 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 er the first mat
1f0a2 63 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 ch .** because t
1f0a3 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d he first match m
1f0a4 69 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 ight be for one
1f0a5 6f 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 of the deleted i
1f0a6 6e 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 ndices.** or tab
1f0a7 6c 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 les and not the
1f0a8 74 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 table/index that
1f0a9 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 is actually bei
1f0aa 6e 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 ng moved..** We
1f0ab 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f must continue lo
1f0ac 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 oping until all
1f0ad 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 tables and indic
1f0ae 65 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 es with.** rootp
1f0af 61 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 age==iFrom have
1f0b0 62 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 been converted t
1f0b1 6f 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 o have a rootpag
1f0b2 65 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f e of iTo.** in o
1f0b3 72 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 rder to be certa
1f0b4 69 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 in that we got t
1f0b5 68 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f he right one..*/
1f0b6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f0b7 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1f0b8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1f0b9 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 oid sqlite3RootP
1f0ba 61 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 ageMoved(Db *pDb
1f0bb 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 , int iFrom, int
1f0bc 20 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 iTo){. HashEle
1f0bd 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 m *pElem;. Hash
1f0be 20 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 *pHash;.. pHas
1f0bf 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d h = &pDb->pSchem
1f0c0 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f a->tblHash;. fo
1f0c1 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 r(pElem=sqliteHa
1f0c2 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 shFirst(pHash);
1f0c3 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c pElem; pElem=sql
1f0c4 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 iteHashNext(pEle
1f0c5 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a m)){. Table *
1f0c6 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 pTab = sqliteHas
1f0c7 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 hData(pElem);.
1f0c8 20 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d if( pTab->tnum
1f0c9 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 ==iFrom ){.
1f0ca 20 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 pTab->tnum = iT
1f0cb 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 o;. }. }. p
1f0cc 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 Hash = &pDb->pSc
1f0cd 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 hema->idxHash;.
1f0ce 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 for(pElem=sqlit
1f0cf 65 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 eHashFirst(pHash
1f0d0 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d ); pElem; pElem=
1f0d1 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 sqliteHashNext(p
1f0d2 45 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 Elem)){. Inde
1f0d3 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 x *pIdx = sqlite
1f0d4 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b HashData(pElem);
1f0d5 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 . if( pIdx->t
1f0d6 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 num==iFrom ){.
1f0d7 20 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d pIdx->tnum =
1f0d8 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a iTo;. }. }.
1f0d9 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1f0da 57 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 Write code to er
1f0db 61 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 ase the table wi
1f0dc 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 th root-page iTa
1f0dd 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 ble from databas
1f0de 65 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 e iDb..** Also w
1f0df 72 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 rite code to mod
1f0e0 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d ify the sqlite_m
1f0e1 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 aster table and
1f0e2 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a internal schema.
1f0e3 2a 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 ** if a root-pag
1f0e4 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 e of another tab
1f0e5 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 le is moved by t
1f0e6 68 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 he btree-layer w
1f0e7 68 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 hilst.** erasing
1f0e8 20 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 iTable (this ca
1f0e9 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e n happen with an
1f0ea 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 auto-vacuum dat
1f0eb 61 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 abase)..*/ .stat
1f0ec 69 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 ic void destroyR
1f0ed 6f 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 ootPage(Parse *p
1f0ee 50 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c Parse, int iTabl
1f0ef 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 e, int iDb){. V
1f0f0 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 dbe *v = sqlite3
1f0f1 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
1f0f2 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 . int r1 = sqli
1f0f3 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
1f0f4 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 arse);. sqlite3
1f0f5 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1f0f6 5f 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 _Destroy, iTable
1f0f7 2c 20 72 31 2c 20 69 44 62 29 3b 0a 23 69 66 6e , r1, iDb);.#ifn
1f0f8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f0f9 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 AUTOVACUUM. /*
1f0fa 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65 OP_Destroy store
1f0fb 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20 s an in integer
1f0fc 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65 r1. If this inte
1f0fd 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d ger. ** is non-
1f0fe 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73 zero, then it is
1f0ff 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e the root page n
1f100 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65 umber of a table
1f101 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c moved to. ** l
1f102 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20 ocation iTable.
1f103 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f The following co
1f104 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20 de modifies the
1f105 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
1f106 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c ble to. ** refl
1f107 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20 ect this.. **.
1f108 20 2a 2a 20 54 68 65 20 22 23 25 64 22 20 69 6e ** The "#%d" in
1f109 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 73 70 the SQL is a sp
1f10a 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 74 ecial constant t
1f10b 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65 76 hat means whatev
1f10c 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69 73 er value. ** is
1f10d 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 on the top of t
1f10e 68 65 20 73 74 61 63 6b 2e 20 20 53 65 65 20 73 he stack. See s
1f10f 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78 qlite3RegisterEx
1f110 70 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c pr().. */. sql
1f111 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
1f112 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55 pParse, . "U
1f113 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20 PDATE %Q.%s SET
1f114 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52 rootpage=%d WHER
1f115 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61 E #%d AND rootpa
1f116 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50 ge=#%d",. pP
1f117 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 arse->db->aDb[iD
1f118 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 b].zName, SCHEMA
1f119 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61 _TABLE(iDb), iTa
1f11a 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65 ble, r1, r1);.#e
1f11b 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52 65 ndif. sqlite3Re
1f11c 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
1f11d 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a rse, r1);.}../*.
1f11e 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20 63 6f ** Write VDBE co
1f11f 64 65 20 74 6f 20 65 72 61 73 65 20 74 61 62 6c de to erase tabl
1f120 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20 61 e pTab and all a
1f121 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63 65 ssociated indice
1f122 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f s on disk..** Co
1f123 64 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 de to update the
1f124 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
1f125 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72 6e ables and intern
1f126 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e 69 al schema defini
1f127 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65 tions.** in case
1f128 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c a root-page bel
1f129 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65 onging to anothe
1f12a 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 r table is moved
1f12b 20 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 by the btree la
1f12c 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61 yer.** is also a
1f12d 64 64 65 64 20 28 74 68 69 73 20 63 61 6e 20 68 dded (this can h
1f12e 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75 appen with an au
1f12f 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 to-vacuum databa
1f130 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 se)..*/.static v
1f131 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c 65 oid destroyTable
1f132 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1f133 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69 Table *pTab){.#i
1f134 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
1f135 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e _AUTOVACUUM. In
1f136 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 dex *pIdx;. int
1f137 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
1f138 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 hemaToIndex(pPar
1f139 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 se->db, pTab->pS
1f13a 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f chema);. destro
1f13b 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 yRootPage(pParse
1f13c 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 , pTab->tnum, iD
1f13d 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 b);. for(pIdx=p
1f13e 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 Tab->pIndex; pId
1f13f 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e x; pIdx=pIdx->pN
1f140 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72 6f ext){. destro
1f141 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 yRootPage(pParse
1f142 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 , pIdx->tnum, iD
1f143 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 b);. }.#else.
1f144 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 /* If the databa
1f145 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d 76 se may be auto-v
1f146 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28 69 acuum capable (i
1f147 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1f148 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73 TOVACUUM. ** is
1f149 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 74 not defined), t
1f14a 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72 74 hen it is import
1f14b 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44 ant to call OP_D
1f14c 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20 estroy on the.
1f14d 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 ** table and ind
1f14e 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e ex root-pages in
1f14f 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e 67 order, starting
1f150 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72 69 with the numeri
1f151 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67 cally . ** larg
1f152 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 est root-page nu
1f153 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61 72 61 mber. This guara
1f154 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65 20 ntees that none
1f155 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 of the root-page
1f156 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73 s. ** to be des
1f157 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61 troyed is reloca
1f158 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65 ted by an earlie
1f159 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e r OP_Destroy. i.
1f15a 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 66 e. if the. ** f
1f15b 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f ollowing were co
1f15c 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f ded:. **. ** O
1f15d 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20 20 P_Destroy 4 0.
1f15e 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44 ** .... ** OP_D
1f15f 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a estroy 5 0. **.
1f160 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61 ** and root pa
1f161 67 65 20 35 20 68 61 70 70 65 6e 65 64 20 74 6f ge 5 happened to
1f162 20 62 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 be the largest
1f163 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 root-page number
1f164 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 in the. ** dat
1f165 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74 abase, then root
1f166 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62 65 page 5 would be
1f167 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20 34 moved to page 4
1f168 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f by the . ** "O
1f169 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20 6f P_Destroy 4 0" o
1f16a 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73 65 pcode. The subse
1f16b 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f quent "OP_Destro
1f16c 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69 74 y 5 0" would hit
1f16d 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73 . ** a free-lis
1f16e 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 t page.. */. i
1f16f 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d 3e nt iTab = pTab->
1f170 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73 tnum;. int iDes
1f171 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77 troyed = 0;.. w
1f172 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 49 hile( 1 ){. I
1f173 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 ndex *pIdx;.
1f174 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30 int iLargest = 0
1f175 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73 74 ;.. if( iDest
1f176 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62 royed==0 || iTab
1f177 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a 20 <iDestroyed ){.
1f178 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20 iLargest =
1f179 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20 iTab;. }.
1f17a 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 for(pIdx=pTab->p
1f17b 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 Index; pIdx; pId
1f17c 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a x=pIdx->pNext){.
1f17d 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d int iIdx =
1f17e 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 pIdx->tnum;.
1f17f 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d assert( pIdx-
1f180 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e >pSchema==pTab->
1f181 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 pSchema );.
1f182 20 69 66 28 20 28 69 44 65 73 74 72 6f 79 65 64 if( (iDestroyed
1f183 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44 65 ==0 || (iIdx<iDe
1f184 73 74 72 6f 79 65 64 29 29 20 26 26 20 69 49 64 stroyed)) && iId
1f185 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20 20 x>iLargest ){.
1f186 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d iLargest =
1f187 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20 iIdx;. }.
1f188 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c 61 }. if( iLa
1f189 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 rgest==0 ){.
1f18a 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65 return;. }e
1f18b 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 lse{. int i
1f18c 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
1f18d 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 maToIndex(pParse
1f18e 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 ->db, pTab->pSch
1f18f 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 73 74 ema);. dest
1f190 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 royRootPage(pPar
1f191 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44 se, iLargest, iD
1f192 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74 72 b);. iDestr
1f193 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b oyed = iLargest;
1f194 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
1f195 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 f.}../*.** This
1f196 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
1f197 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b d to do the work
1f198 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45 of a DROP TABLE
1f199 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70 statement..** p
1f19a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 Name is the name
1f19b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
1f19c 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a be dropped..*/.
1f19d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1f19e 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 oid sqlite3DropT
1f19f 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 able(Parse *pPar
1f1a0 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 se, SrcList *pNa
1f1a1 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c 20 me, int isView,
1f1a2 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 61 int noErr){. Ta
1f1a3 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 ble *pTab;. Vdb
1f1a4 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 e *v;. sqlite3
1f1a5 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1f1a6 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 ;. int iDb;..
1f1a7 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 if( pParse->nErr
1f1a8 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
1f1a9 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f iled ){. goto
1f1aa 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 exit_drop_table
1f1ab 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1f1ac 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 pName->nSrc==1 )
1f1ad 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 ;. pTab = sqlit
1f1ae 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 e3LocateTable(pP
1f1af 61 72 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20 arse, isView, .
1f1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f1b1 20 20 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65 pName
1f1b2 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e ->a[0].zName, pN
1f1b3 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 ame->a[0].zDatab
1f1b4 61 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 ase);.. if( pTa
1f1b5 62 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 b==0 ){. if(
1f1b6 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 noErr ){. s
1f1b7 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 qlite3ErrorClear
1f1b8 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a (pParse);. }.
1f1b9 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 goto exit_dr
1f1ba 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 op_table;. }.
1f1bb 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
1f1bc 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 emaToIndex(db, p
1f1bd 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Tab->pSchema);.
1f1be 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
1f1bf 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
1f1c0 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 ;.. /* If pTab
1f1c1 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 is a virtual tab
1f1c2 6c 65 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74 le, call ViewGet
1f1c3 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f ColumnNames() to
1f1c4 20 65 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20 ensure. ** it
1f1c5 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a is initialized..
1f1c6 20 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 */. if( IsVir
1f1c7 74 75 61 6c 28 70 54 61 62 29 20 26 26 20 73 71 tual(pTab) && sq
1f1c8 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 lite3ViewGetColu
1f1c9 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 mnNames(pParse,
1f1ca 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 pTab) ){. got
1f1cb 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c o exit_drop_tabl
1f1cc 65 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 e;. }.#ifndef S
1f1cd 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
1f1ce 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 RIZATION. {.
1f1cf 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 int code;. c
1f1d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 onst char *zTab
1f1d1 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 = SCHEMA_TABLE(i
1f1d2 44 62 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 Db);. const c
1f1d3 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 har *zDb = db->a
1f1d4 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 Db[iDb].zName;.
1f1d5 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1f1d6 41 72 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66 Arg2 = 0;. if
1f1d7 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 ( sqlite3AuthChe
1f1d8 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ck(pParse, SQLIT
1f1d9 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 E_DELETE, zTab,
1f1da 30 2c 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 0, zDb)){.
1f1db 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 goto exit_drop_t
1f1dc 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 able;. }.
1f1dd 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 if( isView ){.
1f1de 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 if( !OMIT_TE
1f1df 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 MPDB && iDb==1 )
1f1e0 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d {. code =
1f1e1 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
1f1e2 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 P_VIEW;. }e
1f1e3 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 lse{. cod
1f1e4 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f e = SQLITE_DROP_
1f1e5 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69 VIEW;. }.#i
1f1e6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f1e7 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
1f1e8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56 }else if( IsV
1f1e9 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a irtual(pTab) ){.
1f1ea 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c code = SQL
1f1eb 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b ITE_DROP_VTABLE;
1f1ec 0a 20 20 20 20 20 20 7a 41 72 67 32 20 3d 20 70 . zArg2 = p
1f1ed 54 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 Tab->pMod->zName
1f1ee 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c ;.#endif. }el
1f1ef 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f se{. if( !O
1f1f0 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 MIT_TEMPDB && iD
1f1f1 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 b==1 ){.
1f1f2 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 code = SQLITE_DR
1f1f3 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 OP_TEMP_TABLE;.
1f1f4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1f1f5 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 code = SQLIT
1f1f6 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20 E_DROP_TABLE;.
1f1f7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1f1f8 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 if( sqlite3AuthC
1f1f9 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 heck(pParse, cod
1f1fa 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 e, pTab->zName,
1f1fb 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20 zArg2, zDb) ){.
1f1fc 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 goto exit_d
1f1fd 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d rop_table;. }
1f1fe 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1f1ff 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
1f200 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c , SQLITE_DELETE,
1f201 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c pTab->zName, 0,
1f202 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 zDb) ){. g
1f203 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 oto exit_drop_ta
1f204 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 ble;. }. }.#
1f205 65 6e 64 69 66 0a 20 20 69 66 28 20 73 71 6c 69 endif. if( sqli
1f206 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 te3StrNICmp(pTab
1f207 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 ->zName, "sqlite
1f208 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 _", 7)==0 ){.
1f209 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1f20a 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 (pParse, "table
1f20b 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 64 72 %s may not be dr
1f20c 6f 70 70 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e opped", pTab->zN
1f20d 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ame);. goto e
1f20e 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a xit_drop_table;.
1f20f 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c }..#ifndef SQL
1f210 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 ITE_OMIT_VIEW.
1f211 2f 2a 20 45 6e 73 75 72 65 20 44 52 4f 50 20 54 /* Ensure DROP T
1f212 41 42 4c 45 20 69 73 20 6e 6f 74 20 75 73 65 64 ABLE is not used
1f213 20 6f 6e 20 61 20 76 69 65 77 2c 20 61 6e 64 20 on a view, and
1f214 44 52 4f 50 20 56 49 45 57 20 69 73 20 6e 6f 74 DROP VIEW is not
1f215 20 75 73 65 64 0a 20 20 2a 2a 20 6f 6e 20 61 20 used. ** on a
1f216 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 table.. */. if
1f217 28 20 69 73 56 69 65 77 20 26 26 20 70 54 61 62 ( isView && pTab
1f218 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a ->pSelect==0 ){.
1f219 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1f21a 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 Msg(pParse, "use
1f21b 20 44 52 4f 50 20 54 41 42 4c 45 20 74 6f 20 64 DROP TABLE to d
1f21c 65 6c 65 74 65 20 74 61 62 6c 65 20 25 73 22 2c elete table %s",
1f21d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pTab->zName);.
1f21e 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
1f21f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 p_table;. }. i
1f220 66 28 20 21 69 73 56 69 65 77 20 26 26 20 70 54 f( !isView && pT
1f221 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 ab->pSelect ){.
1f222 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1f223 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 sg(pParse, "use
1f224 44 52 4f 50 20 56 49 45 57 20 74 6f 20 64 65 6c DROP VIEW to del
1f225 65 74 65 20 76 69 65 77 20 25 73 22 2c 20 70 54 ete view %s", pT
1f226 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ab->zName);.
1f227 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 goto exit_drop_t
1f228 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 able;. }.#endif
1f229 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 .. /* Generate
1f22a 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 code to remove t
1f22b 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 he table from th
1f22c 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 e master table.
1f22d 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a ** on disk.. *
1f22e 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 /. v = sqlite3G
1f22f 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
1f230 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 54 if( v ){. T
1f231 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 rigger *pTrigger
1f232 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 ;. Db *pDb =
1f233 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 &db->aDb[iDb];.
1f234 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 sqlite3BeginW
1f235 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 riteOperation(pP
1f236 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a arse, 1, iDb);..
1f237 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f238 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1f239 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75 . if( IsVirtu
1f23a 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 al(pTab) ){.
1f23b 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 Vdbe *v = sqli
1f23c 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
1f23d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 76 20 e);. if( v
1f23e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1f23f 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 e3VdbeAddOp0(v,
1f240 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 OP_VBegin);.
1f241 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
1f242 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c .. /* Drop al
1f243 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 l triggers assoc
1f244 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 iated with the t
1f245 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 able being dropp
1f246 65 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20 ed. Code. **
1f247 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 is generated to
1f248 72 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66 remove entries f
1f249 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 rom sqlite_maste
1f24a 72 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20 r and/or. **
1f24b 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 sqlite_temp_mast
1f24c 65 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a er if required..
1f24d 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67 */. pTrig
1f24e 67 65 72 20 3d 20 70 54 61 62 2d 3e 70 54 72 69 ger = pTab->pTri
1f24f 67 67 65 72 3b 0a 20 20 20 20 77 68 69 6c 65 28 gger;. while(
1f250 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 pTrigger ){.
1f251 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 assert( pTrig
1f252 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 ger->pSchema==pT
1f253 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a ab->pSchema || .
1f254 20 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67 pTrigg
1f255 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d er->pSchema==db-
1f256 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 >aDb[1].pSchema
1f257 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f258 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 70 DropTriggerPtr(p
1f259 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 29 Parse, pTrigger)
1f25a 3b 0a 20 20 20 20 20 20 70 54 72 69 67 67 65 72 ;. pTrigger
1f25b 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 = pTrigger->pNe
1f25c 78 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 xt;. }..#ifnd
1f25d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1f25e 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 UTOINCREMENT.
1f25f 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 65 /* Remove any e
1f260 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 73 71 ntries of the sq
1f261 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 lite_sequence ta
1f262 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 ble associated w
1f263 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 ith. ** the t
1f264 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 able being dropp
1f265 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 ed. This is done
1f266 20 62 65 66 6f 72 65 20 74 68 65 20 74 61 62 6c before the tabl
1f267 65 20 69 73 20 64 72 6f 70 70 65 64 0a 20 20 20 e is dropped.
1f268 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65 65 ** at the btree
1f269 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20 level, in case
1f26a 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 the sqlite_seque
1f26b 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20 nce table needs
1f26c 74 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 20 61 to. ** move a
1f26d 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 s a result of th
1f26e 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70 e drop (can happ
1f26f 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 en in auto-vacuu
1f270 6d 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f 0a m mode).. */.
1f271 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 if( pTab->ta
1f272 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f bFlags & TF_Auto
1f273 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 increment ){.
1f274 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 sqlite3Nested
1f275 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 Parse(pParse,.
1f276 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 "DELETE FR
1f277 4f 4d 20 25 73 2e 73 71 6c 69 74 65 5f 73 65 71 OM %s.sqlite_seq
1f278 75 65 6e 63 65 20 57 48 45 52 45 20 6e 61 6d 65 uence WHERE name
1f279 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 70 44 =%Q",. pD
1f27a 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e b->zName, pTab->
1f27b 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 zName. );.
1f27c 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
1f27d 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53 51 4c /* Drop all SQL
1f27e 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 ITE_MASTER table
1f27f 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 69 and index entri
1f280 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f es that refer to
1f281 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c the. ** tabl
1f282 65 2e 20 54 68 65 20 70 72 6f 67 72 61 6d 20 6e e. The program n
1f283 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 ame loops throug
1f284 68 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 h the master tab
1f285 6c 65 20 61 6e 64 20 64 65 6c 65 74 65 73 0a 20 le and deletes.
1f286 20 20 20 2a 2a 20 65 76 65 72 79 20 72 6f 77 20 ** every row
1f287 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 that refers to a
1f288 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 73 61 table of the sa
1f289 6d 65 20 6e 61 6d 65 20 61 73 20 74 68 65 20 6f me name as the o
1f28a 6e 65 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 ne being. **
1f28b 64 72 6f 70 70 65 64 2e 20 54 72 69 67 67 65 72 dropped. Trigger
1f28c 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65 s are handled se
1f28d 70 65 72 61 74 65 6c 79 20 62 65 63 61 75 73 65 perately because
1f28e 20 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62 a trigger can b
1f28f 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 e. ** created
1f290 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74 in the temp dat
1f291 61 62 61 73 65 20 74 68 61 74 20 72 65 66 65 72 abase that refer
1f292 73 20 74 6f 20 61 20 74 61 62 6c 65 20 69 6e 20 s to a table in
1f293 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 64 another. ** d
1f294 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a atabase.. */.
1f295 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 sqlite3Neste
1f296 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a dParse(pParse, .
1f297 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 "DELETE
1f298 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 FROM %Q.%s WHERE
1f299 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 tbl_name=%Q and
1f29a 20 74 79 70 65 21 3d 27 74 72 69 67 67 65 72 27 type!='trigger'
1f29b 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e ",. pDb->
1f29c 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 zName, SCHEMA_TA
1f29d 42 4c 45 28 69 44 62 29 2c 20 70 54 61 62 2d 3e BLE(iDb), pTab->
1f29e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 zName);.. /*
1f29f 44 72 6f 70 20 61 6e 79 20 73 74 61 74 69 73 74 Drop any statist
1f2a0 69 63 73 20 66 72 6f 6d 20 74 68 65 20 73 71 6c ics from the sql
1f2a1 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2c ite_stat1 table,
1f2a2 20 69 66 20 69 74 20 65 78 69 73 74 73 20 2a 2f if it exists */
1f2a3 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1f2a4 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 FindTable(db, "s
1f2a5 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 64 62 qlite_stat1", db
1f2a6 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
1f2a7 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) ){. sqlit
1f2a8 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
1f2a9 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 arse,. "D
1f2aa 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 ELETE FROM %Q.sq
1f2ab 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45 52 45 lite_stat1 WHERE
1f2ac 20 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d 3e 7a tbl=%Q", pDb->z
1f2ad 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d Name, pTab->zNam
1f2ae 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d e. );. }
1f2af 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65 .. if( !isVie
1f2b0 77 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 w && !IsVirtual(
1f2b1 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 64 pTab) ){. d
1f2b2 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50 61 72 estroyTable(pPar
1f2b3 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d se, pTab);. }
1f2b4 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 .. /* Remove
1f2b5 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 the table entry
1f2b6 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20 69 6e from SQLite's in
1f2b7 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 61 6e ternal schema an
1f2b8 64 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a 2a 20 d modify. **
1f2b9 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 the schema cooki
1f2ba 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 e.. */. if
1f2bb 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 ( IsVirtual(pTab
1f2bc 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) ){. sqlit
1f2bd 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
1f2be 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 62 OP_VDestroy, iDb
1f2bf 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e , 0, 0, pTab->zN
1f2c0 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 ame, 0);. }.
1f2c1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1f2c2 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 dOp4(v, OP_DropT
1f2c3 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c able, iDb, 0, 0,
1f2c4 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 pTab->zName, 0)
1f2c5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 ;. sqlite3Cha
1f2c6 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 ngeCookie(pParse
1f2c7 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 73 71 , iDb);. }. sq
1f2c8 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c liteViewResetAll
1f2c9 28 64 62 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 (db, iDb);..exit
1f2ca 5f 64 72 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73 _drop_table:. s
1f2cb 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c qlite3SrcListDel
1f2cc 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a ete(db, pName);.
1f2cd 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
1f2ce 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
1f2cf 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 to create a new
1f2d0 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 foreign key on t
1f2d1 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72 he table.** curr
1f2d2 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 ently under cons
1f2d3 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d truction. pFrom
1f2d4 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 20 77 Col determines w
1f2d5 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 hich columns.**
1f2d6 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 in the current t
1f2d7 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 able point to th
1f2d8 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20 e foreign key.
1f2d9 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 If pFromCol==0 t
1f2da 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 hen.** connect t
1f2db 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20 6c 61 he key to the la
1f2dc 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 st column insert
1f2dd 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68 65 20 ed. pTo is the
1f2de 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 name of.** the t
1f2df 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f able referred to
1f2e0 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 20 6c . pToCol is a l
1f2e1 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e ist of tables in
1f2e2 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70 54 the other.** pT
1f2e3 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 o table that the
1f2e4 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f 69 foreign key poi
1f2e5 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 20 63 nts to. flags c
1f2e6 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69 ontains all.** i
1f2e7 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
1f2e8 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 72 65 the conflict re
1f2e9 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 solution algorit
1f2ea 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a hms specified.**
1f2eb 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45 54 in the ON DELET
1f2ec 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 6e 64 E, ON UPDATE and
1f2ed 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 75 73 ON INSERT claus
1f2ee 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65 es..**.** An FKe
1f2ef 79 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 y structure is c
1f2f0 72 65 61 74 65 64 20 61 6e 64 20 61 64 64 65 64 reated and added
1f2f1 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 to the table cu
1f2f2 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65 72 rrently.** under
1f2f3 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e construction in
1f2f4 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 the pParse->pNe
1f2f5 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 20 20 54 wTable field. T
1f2f6 68 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a 20 69 he new FKey.** i
1f2f7 73 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69 6e 74 s not linked int
1f2f8 6f 20 64 62 2d 3e 61 46 4b 65 79 20 61 74 20 74 o db->aFKey at t
1f2f9 68 69 73 20 70 6f 69 6e 74 20 2d 20 74 68 61 74 his point - that
1f2fa 20 64 6f 65 73 20 6e 6f 74 20 68 61 70 70 65 6e does not happen
1f2fb 0a 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69 74 65 .** until sqlite
1f2fc 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a 2a 0a 3EndTable()..**.
1f2fd 2a 2a 20 54 68 65 20 66 6f 72 65 69 67 6e 20 6b ** The foreign k
1f2fe 65 79 20 69 73 20 73 65 74 20 66 6f 72 20 49 4d ey is set for IM
1f2ff 4d 45 44 49 41 54 45 20 70 72 6f 63 65 73 73 69 MEDIATE processi
1f300 6e 67 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e ng. A subsequen
1f301 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c t call.** to sql
1f302 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e ite3DeferForeign
1f303 4b 65 79 28 29 20 6d 69 67 68 74 20 63 68 61 6e Key() might chan
1f304 67 65 20 74 68 69 73 20 74 6f 20 44 45 46 45 52 ge this to DEFER
1f305 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 RED..*/.SQLITE_P
1f306 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1f307 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e te3CreateForeign
1f308 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 Key(. Parse *pP
1f309 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 arse, /* P
1f30a 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
1f30b 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 /. ExprList *pF
1f30c 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 romCol, /* Colu
1f30d 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c mns in this tabl
1f30e 65 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 e that point to
1f30f 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 other table */.
1f310 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20 Token *pTo,
1f311 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1f312 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65 the other table
1f313 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
1f314 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f pToCol, /* Co
1f315 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74 68 lumns in the oth
1f316 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e er table */. in
1f317 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20 t flags
1f318 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 /* Conflict r
1f319 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 esolution algori
1f31a 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 thms. */.){. sq
1f31b 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
1f31c 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 se->db;.#ifndef
1f31d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 SQLITE_OMIT_FORE
1f31e 49 47 4e 5f 4b 45 59 0a 20 20 46 4b 65 79 20 2a IGN_KEY. FKey *
1f31f 70 46 4b 65 79 20 3d 20 30 3b 0a 20 20 54 61 62 pFKey = 0;. Tab
1f320 6c 65 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e le *p = pParse->
1f321 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 pNewTable;. int
1f322 20 6e 42 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b nByte;. int i;
1f323 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 . int nCol;. c
1f324 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 har *z;.. asser
1f325 74 28 20 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 t( pTo!=0 );. i
1f326 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 f( p==0 || pPars
1f327 65 2d 3e 6e 45 72 72 20 7c 7c 20 49 4e 5f 44 45 e->nErr || IN_DE
1f328 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74 CLARE_VTAB ) got
1f329 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 o fk_end;. if(
1f32a 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 pFromCol==0 ){.
1f32b 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d int iCol = p-
1f32c 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 >nCol-1;. if(
1f32d 20 69 43 6f 6c 3c 30 20 29 20 67 6f 74 6f 20 66 iCol<0 ) goto f
1f32e 6b 5f 65 6e 64 3b 0a 20 20 20 20 69 66 28 20 70 k_end;. if( p
1f32f 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d ToCol && pToCol-
1f330 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 >nExpr!=1 ){.
1f331 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1f332 73 67 28 70 50 61 72 73 65 2c 20 22 66 6f 72 65 sg(pParse, "fore
1f333 69 67 6e 20 6b 65 79 20 6f 6e 20 25 73 22 0a 20 ign key on %s".
1f334 20 20 20 20 20 20 20 20 22 20 73 68 6f 75 6c 64 " should
1f335 20 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c 79 20 reference only
1f336 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 one column of ta
1f337 62 6c 65 20 25 54 22 2c 0a 20 20 20 20 20 20 20 ble %T",.
1f338 20 20 70 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e p->aCol[iCol].
1f339 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b 0a 20 20 20 zName, pTo);.
1f33a 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a goto fk_end;.
1f33b 20 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c 20 3d }. nCol =
1f33c 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 1;. }else if(
1f33d 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c pToCol && pToCol
1f33e 2d 3e 6e 45 78 70 72 21 3d 70 46 72 6f 6d 43 6f ->nExpr!=pFromCo
1f33f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 l->nExpr ){.
1f340 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1f341 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 pParse,.
1f342 22 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d "number of colum
1f343 6e 73 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 ns in foreign ke
1f344 79 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 y does not match
1f345 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 22 the number of "
1f346 0a 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e . "column
1f347 73 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e s in the referen
1f348 63 65 64 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 ced table");.
1f349 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 goto fk_end;.
1f34a 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 43 6f 6c 20 }else{. nCol
1f34b 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 = pFromCol->nExp
1f34c 72 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d r;. }. nByte =
1f34d 20 73 69 7a 65 6f 66 28 2a 70 46 4b 65 79 29 20 sizeof(*pFKey)
1f34e 2b 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 70 46 + nCol*sizeof(pF
1f34f 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b 20 Key->aCol[0]) +
1f350 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69 66 pTo->n + 1;. if
1f351 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 ( pToCol ){.
1f352 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 43 6f for(i=0; i<pToCo
1f353 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a l->nExpr; i++){.
1f354 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 nByte += s
1f355 74 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b trlen(pToCol->a[
1f356 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a 20 i].zName) + 1;.
1f357 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 }. }. pFKey
1f358 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1f359 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 65 ocZero(db, nByte
1f35a 20 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79 3d );. if( pFKey=
1f35b 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 =0 ){. goto f
1f35c 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46 4b k_end;. }. pFK
1f35d 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a 20 ey->pFrom = p;.
1f35e 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f pFKey->pNextFro
1f35f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20 20 m = p->pFKey;.
1f360 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b 65 z = (char*)&pFKe
1f361 79 5b 31 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e 61 y[1];. pFKey->a
1f362 43 6f 6c 20 3d 20 28 73 74 72 75 63 74 20 73 43 Col = (struct sC
1f363 6f 6c 4d 61 70 2a 29 7a 3b 0a 20 20 7a 20 2b 3d olMap*)z;. z +=
1f364 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 73 sizeof(struct s
1f365 43 6f 6c 4d 61 70 29 2a 6e 43 6f 6c 3b 0a 20 20 ColMap)*nCol;.
1f366 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a pFKey->zTo = z;.
1f367 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 2d memcpy(z, pTo-
1f368 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20 20 7a >z, pTo->n);. z
1f369 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 [pTo->n] = 0;.
1f36a 7a 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 z += pTo->n+1;.
1f36b 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 pFKey->pNextTo
1f36c 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e 43 = 0;. pFKey->nC
1f36d 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66 28 ol = nCol;. if(
1f36e 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a pFromCol==0 ){.
1f36f 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b pFKey->aCol[
1f370 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e 43 0].iFrom = p->nC
1f371 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ol-1;. }else{.
1f372 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 for(i=0; i<nC
1f373 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ol; i++){.
1f374 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 int j;. for
1f375 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b (j=0; j<p->nCol;
1f376 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 j++){. i
1f377 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
1f378 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 p(p->aCol[j].zNa
1f379 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b me, pFromCol->a[
1f37a 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a i].zName)==0 ){.
1f37b 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d pFKey-
1f37c 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 3d >aCol[i].iFrom =
1f37d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 j;. br
1f37e 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
1f37f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1f380 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 j>=p->nCol ){.
1f381 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
1f382 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a rorMsg(pParse, .
1f383 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e 6f "unkno
1f384 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c 22 wn column \"%s\"
1f385 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 in foreign key
1f386 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20 20 definition", .
1f387 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f 6c pFromCol
1f388 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 ->a[i].zName);.
1f389 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 goto fk_e
1f38a 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 nd;. }.
1f38b 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f 43 }. }. if( pToC
1f38c 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d ol ){. for(i=
1f38d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0; i<nCol; i++){
1f38e 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 . int n = s
1f38f 74 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b trlen(pToCol->a[
1f390 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 i].zName);.
1f391 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e pFKey->aCol[i].
1f392 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20 zCol = z;.
1f393 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43 6f 6c memcpy(z, pToCol
1f394 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 ->a[i].zName, n)
1f395 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 ;. z[n] = 0
1f396 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 ;. z += n+1
1f397 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 ;. }. }. pF
1f398 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 Key->isDeferred
1f399 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 64 65 = 0;. pFKey->de
1f39a 6c 65 74 65 43 6f 6e 66 20 3d 20 66 6c 61 67 73 leteConf = flags
1f39b 20 26 20 30 78 66 66 3b 0a 20 20 70 46 4b 65 79 & 0xff;. pFKey
1f39c 2d 3e 75 70 64 61 74 65 43 6f 6e 66 20 3d 20 28 ->updateConf = (
1f39d 66 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 flags >> 8 ) & 0
1f39e 78 66 66 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e xff;. pFKey->in
1f39f 73 65 72 74 43 6f 6e 66 20 3d 20 28 66 6c 61 67 sertConf = (flag
1f3a0 73 20 3e 3e 20 31 36 20 29 20 26 20 30 78 66 66 s >> 16 ) & 0xff
1f3a1 3b 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 ;.. /* Link the
1f3a2 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 foreign key to
1f3a3 74 68 65 20 74 61 62 6c 65 20 61 73 20 74 68 65 the table as the
1f3a4 20 6c 61 73 74 20 73 74 65 70 2e 0a 20 20 2a 2f last step.. */
1f3a5 0a 20 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70 46 . p->pFKey = pF
1f3a6 4b 65 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20 30 Key;. pFKey = 0
1f3a7 3b 0a 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c ;..fk_end:. sql
1f3a8 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1f3a9 46 4b 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f 2a FKey);.#endif /*
1f3aa 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1f3ab 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 _OMIT_FOREIGN_KE
1f3ac 59 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 Y) */. sqlite3E
1f3ad 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 xprListDelete(db
1f3ae 2c 20 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73 , pFromCol);. s
1f3af 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 qlite3ExprListDe
1f3b0 6c 65 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c 29 lete(db, pToCol)
1f3b1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
1f3b2 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
1f3b3 64 20 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41 d when an INITIA
1f3b4 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72 LLY IMMEDIATE or
1f3b5 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 INITIALLY DEFER
1f3b6 52 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73 RED.** clause is
1f3b7 20 73 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66 seen as part of
1f3b8 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 a foreign key d
1f3b9 65 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 efinition. The
1f3ba 69 73 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61 isDeferred.** pa
1f3bb 72 61 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72 rameter is 1 for
1f3bc 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 INITIALLY DEFER
1f3bd 52 45 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e RED and 0 for IN
1f3be 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 ITIALLY IMMEDIAT
1f3bf 45 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69 E..** The behavi
1f3c0 6f 72 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 or of the most r
1f3c1 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 ecently created
1f3c2 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 foreign key is a
1f3c3 64 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72 djusted.** accor
1f3c4 64 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 dingly..*/.SQLIT
1f3c5 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1f3c6 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 qlite3DeferForei
1f3c7 67 6e 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61 gnKey(Parse *pPa
1f3c8 72 73 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72 rse, int isDefer
1f3c9 72 65 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 red){.#ifndef SQ
1f3ca 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 LITE_OMIT_FOREIG
1f3cb 4e 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70 N_KEY. Table *p
1f3cc 54 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b Tab;. FKey *pFK
1f3cd 65 79 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20 ey;. if( (pTab
1f3ce 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 = pParse->pNewTa
1f3cf 62 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 ble)==0 || (pFKe
1f3d0 79 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 y = pTab->pFKey)
1f3d1 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1f3d2 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 pFKey->isDeferre
1f3d3 64 20 3d 20 69 73 44 65 66 65 72 72 65 64 3b 0a d = isDeferred;.
1f3d4 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
1f3d5 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 Generate code th
1f3d6 61 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e at will erase an
1f3d7 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a d refill index *
1f3d8 70 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a pIdx. This is.*
1f3d9 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 * used to initia
1f3da 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 lize a newly cre
1f3db 61 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f ated index or to
1f3dc 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a recompute the.*
1f3dd 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 * content of an
1f3de 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 index in respons
1f3df 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63 e to a REINDEX c
1f3e0 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 ommand..**.** if
1f3e1 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 memRootPage is
1f3e2 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74 not negative, it
1f3e3 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
1f3e4 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a index is newly.*
1f3e5 2a 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 * created. The
1f3e6 72 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69 register specifi
1f3e7 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67 ed by memRootPag
1f3e8 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a e contains the.*
1f3e9 2a 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 * root page numb
1f3ea 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e er of the index.
1f3eb 20 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 If memRootPage
1f3ec 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 is negative, th
1f3ed 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 en.** the index
1f3ee 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 already exists a
1f3ef 6e 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72 nd must be clear
1f3f0 65 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 ed before being
1f3f1 72 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 refilled and.**
1f3f2 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 the root page nu
1f3f3 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 mber of the inde
1f3f4 78 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 x is taken from
1f3f5 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f pIndex->tnum..*/
1f3f6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
1f3f7 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 ite3RefillIndex(
1f3f8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 Parse *pParse, I
1f3f9 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e ndex *pIndex, in
1f3fa 74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a t memRootPage){.
1f3fb 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
1f3fc 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20 pIndex->pTable;
1f3fd 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 /* The table th
1f3fe 61 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f at is indexed */
1f3ff 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 . int iTab = pP
1f400 61 72 73 65 2d 3e 6e 54 61 62 3b 20 20 20 20 20 arse->nTab;
1f401 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f /* Btree curso
1f402 72 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20 r used for pTab
1f403 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 */. int iIdx =
1f404 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 31 3b 20 pParse->nTab+1;
1f405 20 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 /* Btree cur
1f406 73 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e sor used for pIn
1f407 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 dex */. int add
1f408 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 r1;
1f409 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 /* Addre
1f40a 73 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f ss of top of loo
1f40b 70 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b p */. int tnum;
1f40c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f40d 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 /* Root pa
1f40e 67 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20 ge of index */.
1f40f 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 Vdbe *v;
1f410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f411 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 /* Generate code
1f412 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 into this virtu
1f413 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 al machine */.
1f414 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 KeyInfo *pKey;
1f415 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f416 2a 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e * KeyInfo for in
1f417 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 dex */. int reg
1f418 49 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 IdxKey;
1f419 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
1f41a 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ters containing
1f41b 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f the index key */
1f41c 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 . int regRecord
1f41d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1f41e 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f /* Register ho
1f41f 6c 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64 lding assemblied
1f420 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f index record */
1f421 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1f422 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 pParse->db;
1f423 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
1f424 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
1f425 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 int iDb = sqli
1f426 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
1f427 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 (db, pIndex->pSc
1f428 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 hema);..#ifndef
1f429 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
1f42a 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 ORIZATION. if(
1f42b 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b sqlite3AuthCheck
1f42c 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f (pParse, SQLITE_
1f42d 52 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d REINDEX, pIndex-
1f42e 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 >zName, 0,.
1f42f 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e db->aDb[iDb].zN
1f430 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 ame ) ){. ret
1f431 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a urn;. }.#endif.
1f432 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 . /* Require a
1f433 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 write-lock on th
1f434 65 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f e table to perfo
1f435 72 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f rm this operatio
1f436 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 n */. sqlite3Ta
1f437 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 bleLock(pParse,
1f438 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c iDb, pTab->tnum,
1f439 20 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 1, pTab->zName)
1f43a 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 ;.. v = sqlite3
1f43b 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
1f43c 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 . if( v==0 ) re
1f43d 74 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 turn;. if( memR
1f43e 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 ootPage>=0 ){.
1f43f 20 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 tnum = memRoot
1f440 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Page;. }else{.
1f441 20 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 tnum = pIndex
1f442 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 ->tnum;. sqli
1f443 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1f444 20 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c OP_Clear, tnum,
1f445 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 iDb);. }. pKe
1f446 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 y = sqlite3Index
1f447 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 Keyinfo(pParse,
1f448 70 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 pIndex);. sqlit
1f449 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
1f44a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 OP_OpenWrite, iI
1f44b 64 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a dx, tnum, iDb, .
1f44c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f44d 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 (char *)pKey
1f44e 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e , P4_KEYINFO_HAN
1f44f 44 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d DOFF);. if( mem
1f450 52 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 RootPage>=0 ){.
1f451 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1f452 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 angeP5(v, 1);.
1f453 7d 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 }. sqlite3OpenT
1f454 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 able(pParse, iTa
1f455 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 b, iDb, pTab, OP
1f456 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 _OpenRead);. ad
1f457 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 dr1 = sqlite3Vdb
1f458 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 eAddOp2(v, OP_Re
1f459 77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a wind, iTab, 0);.
1f45a 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 regRecord = sq
1f45b 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
1f45c 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64 pParse);. regId
1f45d 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65 xKey = sqlite3Ge
1f45e 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 nerateIndexKey(p
1f45f 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 Parse, pIndex, i
1f460 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 Tab, regRecord,
1f461 31 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 1);. if( pIndex
1f462 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f ->onError!=OE_No
1f463 6e 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 ne ){. int j1
1f464 2c 20 6a 32 3b 0a 20 20 20 20 69 6e 74 20 72 65 , j2;. int re
1f465 67 52 6f 77 69 64 3b 0a 0a 20 20 20 20 72 65 67 gRowid;.. reg
1f466 52 6f 77 69 64 20 3d 20 72 65 67 49 64 78 4b 65 Rowid = regIdxKe
1f467 79 20 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c y + pIndex->nCol
1f468 75 6d 6e 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 umn;. j1 = sq
1f469 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1f46a 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 v, OP_IsNull, re
1f46b 67 49 64 78 4b 65 79 2c 20 30 2c 20 70 49 6e 64 gIdxKey, 0, pInd
1f46c 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 ex->nColumn);.
1f46d 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 j2 = sqlite3Vd
1f46e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 beAddOp4(v, OP_I
1f46f 73 55 6e 69 71 75 65 2c 20 69 49 64 78 2c 0a 20 sUnique, iIdx,.
1f470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f471 20 20 20 20 20 20 20 20 20 20 30 2c 20 72 65 67 0, reg
1f472 52 6f 77 69 64 2c 20 53 51 4c 49 54 45 5f 49 4e Rowid, SQLITE_IN
1f473 54 5f 54 4f 5f 50 54 52 28 72 65 67 52 65 63 6f T_TO_PTR(regReco
1f474 72 64 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a rd), P4_INT32);.
1f475 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1f476 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 ddOp4(v, OP_Halt
1f477 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 , SQLITE_CONSTRA
1f478 49 4e 54 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30 INT, OE_Abort, 0
1f479 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1f47a 20 20 20 20 20 20 22 69 6e 64 65 78 65 64 20 63 "indexed c
1f47b 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75 olumns are not u
1f47c 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 nique", P4_STATI
1f47d 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 C);. sqlite3V
1f47e 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
1f47f 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 1);. sqlite3V
1f480 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
1f481 32 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 2);. }. sqlite
1f482 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1f483 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64 P_IdxInsert, iId
1f484 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 x, regRecord);.
1f485 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
1f486 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
1f487 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c egRecord);. sql
1f488 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1f489 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c , OP_Next, iTab,
1f48a 20 61 64 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c addr1+1);. sql
1f48b 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
1f48c 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 73 71 (v, addr1);. sq
1f48d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
1f48e 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61 v, OP_Close, iTa
1f48f 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 b);. sqlite3Vdb
1f490 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c eAddOp1(v, OP_Cl
1f491 6f 73 65 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f ose, iIdx);.}../
1f492 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 *.** Create a ne
1f493 77 20 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53 w index for an S
1f494 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65 QL table. pName
1f495 31 2e 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20 1.pName2 is the
1f496 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 name of the inde
1f497 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69 x .** and pTblLi
1f498 73 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f st is the name o
1f499 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 f the table that
1f49a 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 is to be indexe
1f49b 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a d. Both will .*
1f49c 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20 * be NULL for a
1f49d 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61 primary key or a
1f49e 6e 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 n index that is
1f49f 63 72 65 61 74 65 64 20 74 6f 20 73 61 74 69 73 created to satis
1f4a0 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63 fy a.** UNIQUE c
1f4a1 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70 onstraint. If p
1f4a2 54 61 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78 Table and pIndex
1f4a3 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70 are NULL, use p
1f4a4 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
1f4a5 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 .** as the table
1f4a6 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 to be indexed.
1f4a7 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
1f4a8 6c 65 20 69 73 20 61 20 74 61 62 6c 65 20 74 68 le is a table th
1f4a9 61 74 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 at is.** current
1f4aa 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 ly being constru
1f4ab 63 74 65 64 20 62 79 20 61 20 43 52 45 41 54 45 cted by a CREATE
1f4ac 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
1f4ad 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73 ..**.** pList is
1f4ae 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d a list of colum
1f4af 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 ns to be indexed
1f4b0 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65 . pList will be
1f4b1 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a NULL if this.**
1f4b2 20 69 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 is a primary ke
1f4b3 79 20 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73 y or unique-cons
1f4b4 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f traint on the mo
1f4b5 73 74 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e st recent column
1f4b6 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 added.** to the
1f4b7 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 table currently
1f4b8 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 under construct
1f4b9 69 6f 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 ion. .*/.SQLITE
1f4ba 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1f4bb 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 lite3CreateIndex
1f4bc 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1f4bd 65 2c 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e e, /* All in
1f4be 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
1f4bf 74 68 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 this parse */.
1f4c0 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 Token *pName1,
1f4c1 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 /* First part
1f4c2 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 of index name.
1f4c3 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 May be NULL */.
1f4c4 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 Token *pName2,
1f4c5 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 /* Second pa
1f4c6 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 rt of index name
1f4c7 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f . May be NULL */
1f4c8 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c . SrcList *pTbl
1f4c9 4e 61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 Name, /* Table t
1f4ca 6f 20 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61 o index. Use pPa
1f4cb 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 rse->pNewTable i
1f4cc 66 20 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 f 0 */. ExprLis
1f4cd 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 t *pList, /* A
1f4ce 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 list of columns
1f4cf 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a to be indexed *
1f4d0 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c /. int onError,
1f4d1 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f /* OE_Abo
1f4d2 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f rt, OE_Ignore, O
1f4d3 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 E_Replace, or OE
1f4d4 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e _None */. Token
1f4d5 20 2a 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a *pStart, /*
1f4d6 20 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 The CREATE toke
1f4d7 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 n that begins th
1f4d8 69 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a is statement */.
1f4d9 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 Token *pEnd,
1f4da 20 20 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20 /* The ")"
1f4db 74 68 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20 that closes the
1f4dc 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 CREATE INDEX sta
1f4dd 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 tement */. int
1f4de 73 6f 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f sortOrder, /
1f4df 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 * Sort order of
1f4e0 70 72 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e primary key when
1f4e1 20 70 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a pList==NULL */.
1f4e2 20 20 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 int ifNotExist
1f4e3 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 /* Omit err
1f4e4 6f 72 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65 or if index alre
1f4e5 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b ady exists */.){
1f4e6 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d . Table *pTab =
1f4e7 20 30 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 0; /* Table
1f4e8 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a to be indexed *
1f4e9 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 /. Index *pInde
1f4ea 78 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 x = 0; /* The
1f4eb 69 6e 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 index to be crea
1f4ec 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ted */. char *z
1f4ed 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a Name = 0; /*
1f4ee 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 Name of the ind
1f4ef 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d ex */. int nNam
1f4f0 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
1f4f1 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 Number of charac
1f4f2 74 65 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f ters in zName */
1f4f3 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 . int i, j;. T
1f4f4 6f 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 oken nullId;
1f4f5 20 20 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 /* Fake toke
1f4f6 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 n for an empty I
1f4f7 44 20 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 D list */. DbFi
1f4f8 78 65 72 20 73 46 69 78 3b 20 20 20 20 20 20 20 xer sFix;
1f4f9 20 2f 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e /* For assignin
1f4fa 67 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73 g database names
1f4fb 20 74 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 to pTable */.
1f4fc 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 int sortOrderMas
1f4fd 6b 3b 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e k; /* 1 to hon
1f4fe 6f 72 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78 or DESC in index
1f4ff 2e 20 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 . 0 to ignore.
1f500 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
1f501 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
1f502 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 Db *pDb;
1f503 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 /* The spe
1f504 63 69 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 cific table cont
1f505 61 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 aining the index
1f506 65 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 ed database */.
1f507 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
1f508 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1f509 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 f the database t
1f50a 68 61 74 20 69 73 20 62 65 69 6e 67 20 77 72 69 hat is being wri
1f50b 74 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 tten */. Token
1f50c 2a 70 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f *pName = 0; /
1f50d 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 * Unqualified na
1f50e 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 me of the index
1f50f 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 to create */. s
1f510 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
1f511 74 65 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 tem *pListItem;
1f512 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f /* For looping o
1f513 76 65 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 ver pList */. i
1f514 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e nt nCol;. int n
1f515 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 Extra = 0;. cha
1f516 72 20 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 69 66 r *zExtra;.. if
1f517 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c ( pParse->nErr |
1f518 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
1f519 65 64 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 ed || IN_DECLARE
1f51a 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74 _VTAB ){. got
1f51b 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e o exit_create_in
1f51c 64 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 dex;. }.. /*.
1f51d 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62 ** Find the tab
1f51e 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 le that is to be
1f51f 20 69 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72 indexed. Retur
1f520 6e 20 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 n early if not f
1f521 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ound.. */. if(
1f522 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a pTblName!=0 ){.
1f523 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 . /* Use the
1f524 74 77 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e two-part index n
1f525 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ame to determine
1f526 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 the database .
1f527 20 20 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 ** to search
1f528 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 for the table. '
1f529 46 69 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e Fix' the table n
1f52a 61 6d 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20 ame to this db.
1f52b 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f ** before loo
1f52c 6b 69 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c king up the tabl
1f52d 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 e.. */. as
1f52e 73 65 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20 sert( pName1 &&
1f52f 70 4e 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 pName2 );. iD
1f530 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 b = sqlite3TwoPa
1f531 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 rtName(pParse, p
1f532 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 Name1, pName2, &
1f533 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 pName);. if(
1f534 69 44 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 iDb<0 ) goto exi
1f535 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
1f536 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f537 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20 OMIT_TEMPDB.
1f538 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 /* If the index
1f539 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 name was unquali
1f53a 66 69 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74 fied, check if t
1f53b 68 65 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 he the table.
1f53c 20 2a 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 ** is a temp ta
1f53d 62 6c 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 ble. If so, set
1f53e 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
1f53f 31 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 1. Do not do thi
1f540 73 0a 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 s. ** if init
1f541 69 61 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62 ialising a datab
1f542 61 73 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 ase schema..
1f543 2a 2f 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e */. if( !db->
1f544 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 init.busy ){.
1f545 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 pTab = sqlite
1f546 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 3SrcListLookup(p
1f547 50 61 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 Parse, pTblName)
1f548 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d ;. if( pNam
1f549 65 32 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3d e2 && pName2->n=
1f54a 3d 30 20 26 26 20 70 54 61 62 20 26 26 20 70 54 =0 && pTab && pT
1f54b 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d ab->pSchema==db-
1f54c 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 >aDb[1].pSchema
1f54d 29 7b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d ){. iDb =
1f54e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
1f54f 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 }.#endif.. if
1f550 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 ( sqlite3FixInit
1f551 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 (&sFix, pParse,
1f552 69 44 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e iDb, "index", pN
1f553 61 6d 65 29 20 26 26 0a 20 20 20 20 20 20 20 20 ame) &&.
1f554 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 sqlite3FixSrcLis
1f555 74 28 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d t(&sFix, pTblNam
1f556 65 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 e). ){.
1f557 2f 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 70 /* Because the p
1f558 61 72 73 65 72 20 63 6f 6e 73 74 72 75 63 74 73 arser constructs
1f559 20 70 54 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 pTblName from a
1f55a 20 73 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 single identifi
1f55b 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c er,. ** sql
1f55c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 20 63 ite3FixSrcList c
1f55d 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a an never fail. *
1f55e 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 30 /. assert(0
1f55f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 );. }. pTa
1f560 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 b = sqlite3Locat
1f561 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 eTable(pParse, 0
1f562 2c 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d , pTblName->a[0]
1f563 2e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 .zName, .
1f564 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e pTblName->a[0].
1f565 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 zDatabase);.
1f566 69 66 28 20 21 70 54 61 62 20 7c 7c 20 64 62 2d if( !pTab || db-
1f567 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
1f568 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
1f569 5f 69 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 _index;. asse
1f56a 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d rt( db->aDb[iDb]
1f56b 2e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e .pSchema==pTab->
1f56c 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c pSchema );. }el
1f56d 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
1f56e 70 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 pName==0 );.
1f56f 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 pTab = pParse->p
1f570 4e 65 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 NewTable;. if
1f571 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 ( !pTab ) goto e
1f572 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 xit_create_index
1f573 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 ;. iDb = sqli
1f574 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
1f575 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 (db, pTab->pSche
1f576 6d 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d ma);. }. pDb =
1f577 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a &db->aDb[iDb];.
1f578 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c . if( pTab==0 |
1f579 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 | pParse->nErr )
1f57a 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 goto exit_creat
1f57b 65 5f 69 6e 64 65 78 3b 0a 20 20 69 66 28 20 73 e_index;. if( s
1f57c 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 qlite3StrNICmp(p
1f57d 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c Tab->zName, "sql
1f57e 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a ite_", 7)==0 ){.
1f57f 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1f580 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 Msg(pParse, "tab
1f581 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 le %s may not be
1f582 20 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d indexed", pTab-
1f583 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 >zName);. got
1f584 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e o exit_create_in
1f585 64 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 dex;. }.#ifndef
1f586 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 SQLITE_OMIT_VIE
1f587 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 W. if( pTab->pS
1f588 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c elect ){. sql
1f589 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1f58a 72 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20 rse, "views may
1f58b 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29 not be indexed")
1f58c 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f ;. goto exit_
1f58d 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
1f58e 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 }.#endif.#ifndef
1f58f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1f590 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 TUALTABLE. if(
1f591 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 IsVirtual(pTab)
1f592 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1f593 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1f594 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d virtual tables m
1f595 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 ay not be indexe
1f596 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 d");. goto ex
1f597 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b it_create_index;
1f598 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
1f599 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 *. ** Find the
1f59a 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 name of the inde
1f59b 78 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 x. Make sure th
1f59c 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 ere is not alrea
1f59d 64 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 dy another. **
1f59e 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77 index or table w
1f59f 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d ith the same nam
1f5a0 65 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 e. . **. ** E
1f5a1 78 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 xception: If we
1f5a2 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 are reading the
1f5a3 20 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e names of perman
1f5a4 65 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d ent indices from
1f5a5 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 the. ** sqlite
1f5a6 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28 62 _master table (b
1f5a7 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 ecause some othe
1f5a8 72 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65 r process change
1f5a9 64 20 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e d the schema) an
1f5aa 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 d. ** one of th
1f5ab 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f e index names co
1f5ac 6c 6c 69 64 65 73 20 77 69 74 68 20 74 68 65 20 llides with the
1f5ad 6e 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 name of a tempor
1f5ae 61 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a ary table or. *
1f5af 2a 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 * index, then we
1f5b0 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 will continue t
1f5b1 6f 20 70 72 6f 63 65 73 73 20 74 68 69 73 20 69 o process this i
1f5b2 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ndex.. **. **
1f5b3 49 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d If pName==0 it m
1f5b4 65 61 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 eans that we are
1f5b5 0a 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 . ** dealing wi
1f5b6 74 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 th a primary key
1f5b7 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 or UNIQUE const
1f5b8 72 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20 raint. We have
1f5b9 74 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 to invent our.
1f5ba 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a ** own name.. *
1f5bb 2f 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b /. if( pName ){
1f5bc 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c . zName = sql
1f5bd 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
1f5be 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 n(db, pName);.
1f5bf 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
1f5c0 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 =sqlite3ReadSche
1f5c1 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 ma(pParse) ) got
1f5c2 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e o exit_create_in
1f5c3 64 65 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 dex;. if( zNa
1f5c4 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 me==0 ) goto exi
1f5c5 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
1f5c6 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
1f5c7 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f K!=sqlite3CheckO
1f5c8 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 bjectName(pParse
1f5c9 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 , zName) ){.
1f5ca 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 goto exit_crea
1f5cb 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a te_index;. }.
1f5cc 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 if( !db->ini
1f5cd 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 t.busy ){.
1f5ce 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
1f5cf 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 qlite3ReadSchema
1f5d0 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 (pParse) ) goto
1f5d1 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
1f5d2 78 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c x;. if( sql
1f5d3 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 ite3FindTable(db
1f5d4 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29 , zName, 0)!=0 )
1f5d5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1f5d6 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1f5d7 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 , "there is alre
1f5d8 61 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 ady a table name
1f5d9 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 d %s", zName);.
1f5da 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 goto exit
1f5db 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
1f5dc 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1f5dd 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 if( sqlite3Find
1f5de 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c Index(db, zName,
1f5df 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 pDb->zName)!=0
1f5e0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 66 ){. if( !if
1f5e1 4e 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20 NotExist ){.
1f5e2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1f5e3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64 Msg(pParse, "ind
1f5e4 65 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78 ex %s already ex
1f5e5 69 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 ists", zName);.
1f5e6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 }. got
1f5e7 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e o exit_create_in
1f5e8 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c dex;. }. }el
1f5e9 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 se{. int n;.
1f5ea 20 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b Index *pLoop;
1f5eb 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 . for(pLoop=p
1f5ec 54 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31 Tab->pIndex, n=1
1f5ed 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 ; pLoop; pLoop=p
1f5ee 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b Loop->pNext, n++
1f5ef 29 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 ){}. zName =
1f5f0 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1f5f1 62 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f 69 b, "sqlite_autoi
1f5f2 6e 64 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 61 ndex_%s_%d", pTa
1f5f3 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 b->zName, n);.
1f5f4 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 if( zName==0 )
1f5f5 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 {. goto exi
1f5f6 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
1f5f7 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1f5f8 43 68 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 Check for author
1f5f9 69 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 ization to creat
1f5fa 65 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f e an index.. */
1f5fb 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f5fc 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
1f5fd 4f 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74 ON. {. const
1f5fe 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 char *zDb = pDb
1f5ff 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 ->zName;. if(
1f600 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
1f601 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 k(pParse, SQLITE
1f602 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f _INSERT, SCHEMA_
1f603 54 41 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a TABLE(iDb), 0, z
1f604 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 Db) ){. got
1f605 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e o exit_create_in
1f606 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 dex;. }. i
1f607 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 = SQLITE_CREATE
1f608 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 _INDEX;. if(
1f609 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 !OMIT_TEMPDB &&
1f60a 69 44 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c iDb==1 ) i = SQL
1f60b 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
1f60c 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 INDEX;. if( s
1f60d 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
1f60e 70 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 pParse, i, zName
1f60f 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a , pTab->zName, z
1f610 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 Db) ){. got
1f611 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e o exit_create_in
1f612 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 dex;. }. }.#
1f613 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 endif.. /* If p
1f614 4c 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e List==0, it mean
1f615 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 s this routine w
1f616 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b as called to mak
1f617 65 20 61 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a e a primary. **
1f618 20 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20 key out of the
1f619 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 last column adde
1f61a 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75 d to the table u
1f61b 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
1f61c 6e 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74 n.. ** So creat
1f61d 65 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f e a fake list to
1f61e 20 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a simulate this..
1f61f 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 */. if( pList
1f620 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 ==0 ){. nullI
1f621 64 2e 7a 20 3d 20 28 75 38 2a 29 70 54 61 62 2d d.z = (u8*)pTab-
1f622 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c >aCol[pTab->nCol
1f623 2d 31 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e -1].zName;. n
1f624 75 6c 6c 49 64 2e 6e 20 3d 20 73 74 72 6c 65 6e ullId.n = strlen
1f625 28 28 63 68 61 72 2a 29 6e 75 6c 6c 49 64 2e 7a ((char*)nullId.z
1f626 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 );. pList = s
1f627 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 qlite3ExprListAp
1f628 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 pend(pParse, 0,
1f629 30 2c 20 26 6e 75 6c 6c 49 64 29 3b 0a 20 20 20 0, &nullId);.
1f62a 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 if( pList==0 )
1f62b 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
1f62c 5f 69 6e 64 65 78 3b 0a 20 20 20 20 70 4c 69 73 _index;. pLis
1f62d 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 t->a[0].sortOrde
1f62e 72 20 3d 20 73 6f 72 74 4f 72 64 65 72 3b 0a 20 r = sortOrder;.
1f62f 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 }.. /* Figure
1f630 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 out how many byt
1f631 65 73 20 6f 66 20 73 70 61 63 65 20 61 72 65 20 es of space are
1f632 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 required to stor
1f633 65 20 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 2a e explicitly. *
1f634 2a 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 6c * specified coll
1f635 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e ation sequence n
1f636 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 ames.. */. for
1f637 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e (i=0; i<pList->n
1f638 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
1f639 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 Expr *pExpr;.
1f63a 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b CollSeq *pColl;
1f63b 0a 20 20 20 20 69 66 28 20 28 70 45 78 70 72 20 . if( (pExpr
1f63c 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 = pList->a[i].pE
1f63d 78 70 72 29 21 3d 30 20 26 26 20 28 70 43 6f 6c xpr)!=0 && (pCol
1f63e 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c l = pExpr->pColl
1f63f 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 )!=0 ){. nE
1f640 78 74 72 61 20 2b 3d 20 28 31 20 2b 20 73 74 72 xtra += (1 + str
1f641 6c 65 6e 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 len(pColl->zName
1f642 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ));. }. }..
1f643 20 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 /* . ** Alloca
1f644 74 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 te the index str
1f645 75 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 ucture. . */.
1f646 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a nName = strlen(z
1f647 4e 61 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 Name);. nCol =
1f648 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 pList->nExpr;.
1f649 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 pIndex = sqlite3
1f64a 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
1f64b 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49 . sizeof(I
1f64c 6e 64 65 78 29 20 2b 20 20 20 20 20 20 20 20 20 ndex) +
1f64d 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 /* Index st
1f64e 72 75 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 ructure */.
1f64f 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 sizeof(int)*nC
1f650 6f 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f ol + /
1f651 2a 20 49 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e * Index.aiColumn
1f652 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 */. size
1f653 6f 66 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29 of(int)*(nCol+1)
1f654 20 2b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 + /* Inde
1f655 78 2e 61 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a x.aiRowEst */.
1f656 20 20 20 20 20 20 73 69 7a 65 6f 66 28 63 68 61 sizeof(cha
1f657 72 20 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 r *)*nCol +
1f658 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f /* Index.azCo
1f659 6c 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 ll */.
1f65a 73 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20 sizeof(u8)*nCol
1f65b 2b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 + /*
1f65c 49 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 Index.aSortOrder
1f65d 20 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 */. nName
1f65e 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 + 1 +
1f65f 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e /* Index.
1f660 7a 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20 zName */.
1f661 20 20 20 20 6e 45 78 74 72 61 20 20 20 20 20 20 nExtra
1f662 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f663 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 /* Collation se
1f664 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a quence names */.
1f665 20 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
1f666 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f667 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 goto exit_cre
1f668 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 ate_index;. }.
1f669 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 pIndex->azColl
1f66a 3d 20 28 63 68 61 72 2a 2a 29 28 26 70 49 6e 64 = (char**)(&pInd
1f66b 65 78 5b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 ex[1]);. pIndex
1f66c 2d 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e ->aiColumn = (in
1f66d 74 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a t *)(&pIndex->az
1f66e 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 Coll[nCol]);. p
1f66f 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 Index->aiRowEst
1f670 3d 20 28 75 6e 73 69 67 6e 65 64 20 2a 29 28 26 = (unsigned *)(&
1f671 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e pIndex->aiColumn
1f672 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 [nCol]);. pInde
1f673 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 x->aSortOrder =
1f674 28 75 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e (u8 *)(&pIndex->
1f675 61 69 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d aiRowEst[nCol+1]
1f676 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 );. pIndex->zNa
1f677 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 me = (char *)(&p
1f678 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 Index->aSortOrde
1f679 72 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74 r[nCol]);. zExt
1f67a 72 61 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 ra = (char *)(&p
1f67b 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 Index->zName[nNa
1f67c 6d 65 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 me+1]);. memcpy
1f67d 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 (pIndex->zName,
1f67e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b zName, nName+1);
1f67f 0a 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c . pIndex->pTabl
1f680 65 20 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 e = pTab;. pInd
1f681 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c ex->nColumn = pL
1f682 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 ist->nExpr;. pI
1f683 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 ndex->onError =
1f684 6f 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65 onError;. pInde
1f685 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20 70 x->autoIndex = p
1f686 4e 61 6d 65 3d 3d 30 3b 0a 20 20 70 49 6e 64 65 Name==0;. pInde
1f687 78 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d x->pSchema = db-
1f688 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d >aDb[iDb].pSchem
1f689 61 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 a;.. /* Check t
1f68a 6f 20 73 65 65 20 69 66 20 77 65 20 73 68 6f 75 o see if we shou
1f68b 6c 64 20 68 6f 6e 6f 72 20 44 45 53 43 20 72 65 ld honor DESC re
1f68c 71 75 65 73 74 73 20 6f 6e 20 69 6e 64 65 78 20 quests on index
1f68d 63 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 columns. */. i
1f68e 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d f( pDb->pSchema-
1f68f 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 >file_format>=4
1f690 29 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 ){. sortOrder
1f691 4d 61 73 6b 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 Mask = -1; /*
1f692 48 6f 6e 6f 72 20 44 45 53 43 20 2a 2f 0a 20 20 Honor DESC */.
1f693 7d 65 6c 73 65 7b 0a 20 20 20 20 73 6f 72 74 4f }else{. sortO
1f694 72 64 65 72 4d 61 73 6b 20 3d 20 30 3b 20 20 20 rderMask = 0;
1f695 20 2f 2a 20 49 67 6e 6f 72 65 20 44 45 53 43 20 /* Ignore DESC
1f696 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 63 61 */. }.. /* Sca
1f697 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 n the names of t
1f698 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 he columns of th
1f699 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e e table to be in
1f69a 64 65 78 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c dexed and. ** l
1f69b 6f 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 oad the column i
1f69c 6e 64 69 63 65 73 20 69 6e 74 6f 20 74 68 65 20 ndices into the
1f69d 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2e Index structure.
1f69e 20 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f Report an erro
1f69f 72 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 63 6f r. ** if any co
1f6a0 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 66 6f 75 6e lumn is not foun
1f6a1 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d d.. */. for(i=
1f6a2 30 2c 20 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69 0, pListItem=pLi
1f6a3 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e st->a; i<pList->
1f6a4 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73 nExpr; i++, pLis
1f6a5 74 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f tItem++){. co
1f6a6 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61 nst char *zColNa
1f6a7 6d 65 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e me = pListItem->
1f6a8 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d zName;. Colum
1f6a9 6e 20 2a 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20 n *pTabCol;.
1f6aa 69 6e 74 20 72 65 71 75 65 73 74 65 64 53 6f 72 int requestedSor
1f6ab 74 4f 72 64 65 72 3b 0a 20 20 20 20 63 68 61 72 tOrder;. char
1f6ac 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 *zColl;
1f6ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
1f6ae 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
1f6af 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f name */.. fo
1f6b0 72 28 6a 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70 r(j=0, pTabCol=p
1f6b1 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 Tab->aCol; j<pTa
1f6b2 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 b->nCol; j++, pT
1f6b3 61 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 abCol++){.
1f6b4 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1f6b5 6d 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61 mp(zColName, pTa
1f6b6 62 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 bCol->zName)==0
1f6b7 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 ) break;. }.
1f6b8 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e if( j>=pTab->
1f6b9 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 nCol ){. sq
1f6ba 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1f6bb 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 arse, "table %s
1f6bc 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 has no column na
1f6bd 6d 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20 med %s",.
1f6be 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 pTab->zName, zC
1f6bf 6f 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 olName);. g
1f6c0 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
1f6c1 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 index;. }.
1f6c2 20 2f 2a 20 54 4f 44 4f 3a 20 20 41 64 64 20 61 /* TODO: Add a
1f6c3 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 test to make su
1f6c4 72 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 re that the same
1f6c5 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6e column is not n
1f6c6 61 6d 65 64 0a 20 20 20 20 2a 2a 20 6d 6f 72 65 amed. ** more
1f6c7 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 69 than once withi
1f6c8 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 n the same index
1f6c9 2e 20 20 4f 6e 6c 79 20 74 68 65 20 66 69 72 73 . Only the firs
1f6ca 74 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 20 t instance of.
1f6cb 20 20 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 ** the column
1f6cc 77 69 6c 6c 20 65 76 65 72 20 62 65 20 75 73 65 will ever be use
1f6cd 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a d by the optimiz
1f6ce 65 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 75 er. Note that u
1f6cf 73 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 sing the. **
1f6d0 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65 same column more
1f6d1 20 74 68 61 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f than once canno
1f6d2 74 20 62 65 20 61 6e 20 65 72 72 6f 72 20 62 65 t be an error be
1f6d3 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 cause that would
1f6d4 20 0a 20 20 20 20 2a 2a 20 62 72 65 61 6b 20 62 . ** break b
1f6d5 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
1f6d6 62 69 6c 69 74 79 20 2d 20 69 74 20 6e 65 65 64 bility - it need
1f6d7 73 20 74 6f 20 62 65 20 61 20 77 61 72 6e 69 6e s to be a warnin
1f6d8 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 g.. */. pI
1f6d9 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 ndex->aiColumn[i
1f6da 5d 20 3d 20 6a 3b 0a 20 20 20 20 69 66 28 20 70 ] = j;. if( p
1f6db 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20 ListItem->pExpr
1f6dc 26 26 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 && pListItem->pE
1f6dd 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20 20 xpr->pColl ){.
1f6de 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 assert( pLis
1f6df 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 tItem->pExpr->pC
1f6e0 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 7a 43 6f oll );. zCo
1f6e1 6c 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20 ll = zExtra;.
1f6e2 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1f6e3 6e 74 66 28 6e 45 78 74 72 61 2c 20 7a 45 78 74 ntf(nExtra, zExt
1f6e4 72 61 2c 20 22 25 73 22 2c 20 70 4c 69 73 74 49 ra, "%s", pListI
1f6e5 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c tem->pExpr->pCol
1f6e6 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 l->zName);.
1f6e7 20 7a 45 78 74 72 61 20 2b 3d 20 28 73 74 72 6c zExtra += (strl
1f6e8 65 6e 28 7a 43 6f 6c 6c 29 20 2b 20 31 29 3b 0a en(zColl) + 1);.
1f6e9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1f6ea 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 zColl = pTab->a
1f6eb 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 Col[j].zColl;.
1f6ec 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 if( !zColl )
1f6ed 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 {. zColl
1f6ee 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d = db->pDfltColl-
1f6ef 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a >zName;. }.
1f6f0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 }. if( !d
1f6f1 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 b->init.busy &&
1f6f2 21 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f !sqlite3LocateCo
1f6f3 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 llSeq(pParse, zC
1f6f4 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 oll, -1) ){.
1f6f5 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 goto exit_crea
1f6f6 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a te_index;. }.
1f6f7 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f pIndex->azCo
1f6f8 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 ll[i] = zColl;.
1f6f9 20 20 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 requestedSort
1f6fa 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 49 74 65 Order = pListIte
1f6fb 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 20 73 m->sortOrder & s
1f6fc 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a 20 20 ortOrderMask;.
1f6fd 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f pIndex->aSortO
1f6fe 72 64 65 72 5b 69 5d 20 3d 20 72 65 71 75 65 73 rder[i] = reques
1f6ff 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 tedSortOrder;.
1f700 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 }. sqlite3Defau
1f701 6c 74 52 6f 77 45 73 74 28 70 49 6e 64 65 78 29 ltRowEst(pIndex)
1f702 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70 ;.. if( pTab==p
1f703 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
1f704 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
1f705 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e routine has been
1f706 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 called to creat
1f707 65 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 e an automatic i
1f708 6e 64 65 78 20 61 73 20 61 0a 20 20 20 20 2a 2a ndex as a. **
1f709 20 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 49 result of a PRI
1f70a 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 MARY KEY or UNIQ
1f70b 55 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 63 UE clause on a c
1f70c 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e olumn definition
1f70d 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 50 52 , or. ** a PR
1f70e 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 IMARY KEY or UNI
1f70f 51 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c 6f QUE clause follo
1f710 77 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20 wing the column
1f711 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 20 definitions..
1f712 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a ** i.e. one of:
1f713 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 . **. ** C
1f714 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 20 REATE TABLE t(x
1f715 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b PRIMARY KEY, y);
1f716 0a 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 . ** CREATE T
1f717 41 42 4c 45 20 74 28 78 2c 20 79 2c 20 55 4e 49 ABLE t(x, y, UNI
1f718 51 55 45 28 78 2c 20 79 29 29 3b 0a 20 20 20 20 QUE(x, y));.
1f719 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68 65 72 **. ** Either
1f71a 20 77 61 79 2c 20 63 68 65 63 6b 20 74 6f 20 73 way, check to s
1f71b 65 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 ee if the table
1f71c 61 6c 72 65 61 64 79 20 68 61 73 20 73 75 63 68 already has such
1f71d 20 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a 20 20 an index. If.
1f71e 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 62 ** so, don't b
1f71f 6f 74 68 65 72 20 63 72 65 61 74 69 6e 67 20 74 other creating t
1f720 68 69 73 20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e his one. This on
1f721 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 0a 20 20 ly applies to.
1f722 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c ** automatical
1f723 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64 69 63 ly created indic
1f724 65 73 2e 20 55 73 65 72 73 20 63 61 6e 20 64 6f es. Users can do
1f725 20 61 73 20 74 68 65 79 20 77 69 73 68 20 77 69 as they wish wi
1f726 74 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 th. ** explic
1f727 69 74 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 it indices..
1f728 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 */. Index *pI
1f729 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78 dx;. for(pIdx
1f72a 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
1f72b 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
1f72c 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e pNext){. in
1f72d 74 20 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 t k;. asser
1f72e 74 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 t( pIdx->onError
1f72f 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20 !=OE_None );.
1f730 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d assert( pIdx-
1f731 3e 61 75 74 6f 49 6e 64 65 78 20 29 3b 0a 20 20 >autoIndex );.
1f732 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64 assert( pInd
1f733 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f ex->onError!=OE_
1f734 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20 20 20 20 69 None );.. i
1f735 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e f( pIdx->nColumn
1f736 21 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d !=pIndex->nColum
1f737 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 n ) continue;.
1f738 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 for(k=0; k<p
1f739 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b Idx->nColumn; k+
1f73a 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 +){. cons
1f73b 74 20 63 68 61 72 20 2a 7a 31 20 3d 20 70 49 64 t char *z1 = pId
1f73c 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 x->azColl[k];.
1f73d 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1f73e 20 2a 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61 *z2 = pIndex->a
1f73f 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 zColl[k];.
1f740 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f if( pIdx->aiCo
1f741 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d lumn[k]!=pIndex-
1f742 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62 >aiColumn[k] ) b
1f743 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 reak;. if
1f744 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 ( pIdx->aSortOrd
1f745 65 72 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 er[k]!=pIndex->a
1f746 53 6f 72 74 4f 72 64 65 72 5b 6b 5d 20 29 20 62 SortOrder[k] ) b
1f747 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 reak;. if
1f748 28 20 7a 31 21 3d 7a 32 20 26 26 20 73 71 6c 69 ( z1!=z2 && sqli
1f749 74 65 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a te3StrICmp(z1, z
1f74a 32 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 2) ) break;.
1f74b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d }. if( k=
1f74c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 =pIdx->nColumn )
1f74d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 {. if( pI
1f74e 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e dx->onError!=pIn
1f74f 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a dex->onError ){.
1f750 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 /* Thi
1f751 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65 s constraint cre
1f752 61 74 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e ates the same in
1f753 64 65 78 20 61 73 20 61 20 70 72 65 76 69 6f 75 dex as a previou
1f754 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 s. ** c
1f755 6f 6e 73 74 72 61 69 6e 74 20 73 70 65 63 69 66 onstraint specif
1f756 69 65 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e ied somewhere in
1f757 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c the CREATE TABL
1f758 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 E statement..
1f759 20 20 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65 ** Howeve
1f75a 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 r the ON CONFLIC
1f75b 54 20 63 6c 61 75 73 65 73 20 61 72 65 20 64 69 T clauses are di
1f75c 66 66 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68 fferent. If both
1f75d 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 20 this .
1f75e 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 ** constraint a
1f75f 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 nd the previous
1f760 65 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74 equivalent const
1f761 72 61 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69 raint have expli
1f762 63 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a cit. **
1f763 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 ON CONFLICT cla
1f764 75 73 65 73 20 74 68 69 73 20 69 73 20 61 6e 20 uses this is an
1f765 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 error. Otherwise
1f766 2c 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 , use the.
1f767 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c ** explicitl
1f768 79 20 73 70 65 63 69 66 69 65 64 20 62 65 68 61 y specified beha
1f769 76 69 6f 75 72 20 66 6f 72 20 74 68 65 20 69 6e viour for the in
1f76a 64 65 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a dex.. *
1f76b 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 /. if(
1f76c 21 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d !(pIdx->onError=
1f76d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70 =OE_Default || p
1f76e 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d Index->onError==
1f76f 4f 45 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20 OE_Default) ){.
1f770 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1f771 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1f772 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 e, .
1f773 20 20 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67 "conflicting
1f774 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 ON CONFLICT cla
1f775 75 73 65 73 20 73 70 65 63 69 66 69 65 64 22 2c uses specified",
1f776 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0);. }
1f777 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
1f778 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 Idx->onError==OE
1f779 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 _Default ){.
1f77a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e pIdx->on
1f77b 45 72 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e Error = pIndex->
1f77c 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 onError;.
1f77d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1f77e 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 goto exit
1f77f 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
1f780 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1f781 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 .. /* Link the
1f782 6e 65 77 20 49 6e 64 65 78 20 73 74 72 75 63 74 new Index struct
1f783 75 72 65 20 74 6f 20 69 74 73 20 74 61 62 6c 65 ure to its table
1f784 20 61 6e 64 20 74 6f 20 74 68 65 20 6f 74 68 65 and to the othe
1f785 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 r. ** in-memory
1f786 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 database struct
1f787 75 72 65 73 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 ures. . */. if
1f788 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 ( db->init.busy
1f789 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 3b ){. Index *p;
1f78a 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
1f78b 48 61 73 68 49 6e 73 65 72 74 28 26 70 49 6e 64 HashInsert(&pInd
1f78c 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 ex->pSchema->idx
1f78d 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20 20 20 Hash, .
1f78e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f78f 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73 pIndex->zName, s
1f790 74 72 6c 65 6e 28 70 49 6e 64 65 78 2d 3e 7a 4e trlen(pIndex->zN
1f791 61 6d 65 29 2b 31 2c 20 70 49 6e 64 65 78 29 3b ame)+1, pIndex);
1f792 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 . if( p ){.
1f793 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 assert( p==p
1f794 49 6e 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c Index ); /* Mal
1f795 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61 loc must have fa
1f796 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 64 62 iled */. db
1f797 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1f798 20 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 1;. goto e
1f799 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 xit_create_index
1f79a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e ;. }. db->
1f79b 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f flags |= SQLITE_
1f79c 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 InternChanges;.
1f79d 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21 if( pTblName!
1f79e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 =0 ){. pInd
1f79f 65 78 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 ex->tnum = db->i
1f7a0 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20 nit.newTnum;.
1f7a1 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
1f7a2 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 the db->init.bus
1f7a3 79 20 69 73 20 30 20 74 68 65 6e 20 63 72 65 61 y is 0 then crea
1f7a4 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 te the index on
1f7a5 64 69 73 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a disk. This. **
1f7a6 20 69 6e 76 6f 6c 76 65 73 20 77 72 69 74 69 6e involves writin
1f7a7 67 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f g the index into
1f7a8 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c the master tabl
1f7a9 65 20 61 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e e and filling in
1f7aa 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 the. ** index
1f7ab 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 with the current
1f7ac 20 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e table contents.
1f7ad 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 . **. ** The d
1f7ae 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 b->init.busy is
1f7af 30 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 0 when the user
1f7b0 66 69 72 73 74 20 65 6e 74 65 72 73 20 61 20 43 first enters a C
1f7b1 52 45 41 54 45 20 49 4e 44 45 58 20 0a 20 20 2a REATE INDEX . *
1f7b2 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e * command. db->
1f7b3 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 77 init.busy is 1 w
1f7b4 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69 hen a database i
1f7b5 73 20 6f 70 65 6e 65 64 20 61 6e 64 20 0a 20 20 s opened and .
1f7b6 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 ** CREATE INDEX
1f7b7 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72 statements are r
1f7b8 65 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d ead out of the m
1f7b9 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e aster table. In
1f7ba 0a 20 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72 . ** the latter
1f7bb 20 63 61 73 65 20 74 68 65 20 69 6e 64 65 78 20 case the index
1f7bc 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 6f already exists o
1f7bd 6e 20 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73 n disk, which is
1f7be 20 77 68 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e why. ** we don
1f7bf 27 74 20 77 61 6e 74 20 74 6f 20 72 65 63 72 65 't want to recre
1f7c0 61 74 65 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a ate it.. **. *
1f7c1 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 * If pTblName==0
1f7c2 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 it means this i
1f7c3 6e 64 65 78 20 69 73 20 67 65 6e 65 72 61 74 65 ndex is generate
1f7c4 64 20 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b d as a primary k
1f7c5 65 79 0a 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55 ey. ** or UNIQU
1f7c6 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 E constraint of
1f7c7 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 a CREATE TABLE s
1f7c8 74 61 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65 tatement. Since
1f7c9 20 74 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 the table. **
1f7ca 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72 has just been cr
1f7cb 65 61 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69 eated, it contai
1f7cc 6e 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74 ns no data and t
1f7cd 68 65 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c he index initial
1f7ce 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65 ization. ** ste
1f7cf 70 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 p can be skipped
1f7d0 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 .. */. else if
1f7d1 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d ( db->init.busy=
1f7d2 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a =0 ){. Vdbe *
1f7d3 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 v;. char *zSt
1f7d4 6d 74 3b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d mt;. int iMem
1f7d5 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 = ++pParse->nMe
1f7d6 6d 3b 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 m;.. v = sqli
1f7d7 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
1f7d8 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 e);. if( v==0
1f7d9 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 ) goto exit_cre
1f7da 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20 ate_index;...
1f7db 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 72 /* Create the r
1f7dc 6f 6f 74 70 61 67 65 20 66 6f 72 20 74 68 65 20 ootpage for the
1f7dd 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 index. */.
1f7de 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 sqlite3BeginWri
1f7df 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 teOperation(pPar
1f7e0 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 se, 1, iDb);.
1f7e1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f7e2 70 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 49 p2(v, OP_CreateI
1f7e3 6e 64 65 78 2c 20 69 44 62 2c 20 69 4d 65 6d 29 ndex, iDb, iMem)
1f7e4 3b 0a 0a 20 20 20 20 2f 2a 20 47 61 74 68 65 72 ;.. /* Gather
1f7e5 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 the complete te
1f7e6 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 xt of the CREATE
1f7e7 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 INDEX statement
1f7e8 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 into. ** the
1f7e9 20 7a 53 74 6d 74 20 76 61 72 69 61 62 6c 65 0a zStmt variable.
1f7ea 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
1f7eb 53 74 61 72 74 20 26 26 20 70 45 6e 64 20 29 7b Start && pEnd ){
1f7ec 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65 . /* A name
1f7ed 64 20 69 6e 64 65 78 20 77 69 74 68 20 61 6e 20 d index with an
1f7ee 65 78 70 6c 69 63 69 74 20 43 52 45 41 54 45 20 explicit CREATE
1f7ef 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 INDEX statement
1f7f0 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d */. zStmt =
1f7f1 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1f7f2 64 62 2c 20 22 43 52 45 41 54 45 25 73 20 49 4e db, "CREATE%s IN
1f7f3 44 45 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20 DEX %.*s",.
1f7f4 20 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e onError==OE_N
1f7f5 6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 one ? "" : " UNI
1f7f6 51 55 45 22 2c 0a 20 20 20 20 20 20 20 20 70 45 QUE",. pE
1f7f7 6e 64 2d 3e 7a 20 2d 20 70 4e 61 6d 65 2d 3e 7a nd->z - pName->z
1f7f8 20 2b 20 31 2c 0a 20 20 20 20 20 20 20 20 70 4e + 1,. pN
1f7f9 61 6d 65 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c ame->z);. }el
1f7fa 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 se{. /* An
1f7fb 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 automatic index
1f7fc 63 72 65 61 74 65 64 20 62 79 20 61 20 50 52 49 created by a PRI
1f7fd 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 MARY KEY or UNIQ
1f7fe 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f UE constraint */
1f7ff 0a 20 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74 20 . /* zStmt
1f800 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
1f801 28 22 22 29 3b 20 2a 2f 0a 20 20 20 20 20 20 7a (""); */. z
1f802 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Stmt = 0;. }.
1f803 0a 20 20 20 20 2f 2a 20 41 64 64 20 61 6e 20 65 . /* Add an e
1f804 6e 74 72 79 20 69 6e 20 73 71 6c 69 74 65 5f 6d ntry in sqlite_m
1f805 61 73 74 65 72 20 66 6f 72 20 74 68 69 73 20 69 aster for this i
1f806 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ndex. */.
1f807 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 sqlite3NestedPar
1f808 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 se(pParse, .
1f809 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f "INSERT INTO
1f80a 20 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27 69 %Q.%s VALUES('i
1f80b 6e 64 65 78 27 2c 25 51 2c 25 51 2c 23 25 64 2c ndex',%Q,%Q,#%d,
1f80c 25 51 29 3b 22 2c 0a 20 20 20 20 20 20 20 20 64 %Q);",. d
1f80d 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
1f80e 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 e, SCHEMA_TABLE(
1f80f 69 44 62 29 2c 0a 20 20 20 20 20 20 20 20 70 49 iDb),. pI
1f810 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 ndex->zName,.
1f811 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 pTab->zName
1f812 2c 0a 20 20 20 20 20 20 20 20 69 4d 65 6d 2c 0a ,. iMem,.
1f813 20 20 20 20 20 20 20 20 7a 53 74 6d 74 0a 20 20 zStmt.
1f814 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f815 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 DbFree(db, zStmt
1f816 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6c 6c 20 );.. /* Fill
1f817 74 68 65 20 69 6e 64 65 78 20 77 69 74 68 20 64 the index with d
1f818 61 74 61 20 61 6e 64 20 72 65 70 61 72 73 65 20 ata and reparse
1f819 74 68 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65 the schema. Code
1f81a 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20 20 an OP_Expire.
1f81b 20 20 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 ** to invalida
1f81c 74 65 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70 69 te all pre-compi
1f81d 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a led statements..
1f81e 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
1f81f 54 62 6c 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 TblName ){.
1f820 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e sqlite3RefillIn
1f821 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 dex(pParse, pInd
1f822 65 78 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 ex, iMem);.
1f823 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f sqlite3ChangeCo
1f824 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 okie(pParse, iDb
1f825 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f826 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1f827 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 _ParseSchema, iD
1f828 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 b, 0, 0,.
1f829 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 sqlite3MPrintf
1f82a 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 (db, "name='%q'"
1f82b 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 , pIndex->zName)
1f82c 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 , P4_DYNAMIC);.
1f82d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f82e 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 AddOp1(v, OP_Exp
1f82f 69 72 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 ire, 0);. }.
1f830 20 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64 }.. /* When ad
1f831 64 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f ding an index to
1f832 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64 the list of ind
1f833 69 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 ices for a table
1f834 2c 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 , make. ** sure
1f835 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62 all indices lab
1f836 65 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20 eled OE_Replace
1f837 63 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74 come after all t
1f838 68 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a hose labeled. *
1f839 2a 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68 * OE_Ignore. Th
1f83a 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 is is necessary
1f83b 66 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74 20 for the correct
1f83c 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 55 50 44 operation of UPD
1f83d 41 54 45 0a 20 20 2a 2a 20 61 6e 64 20 49 4e 53 ATE. ** and INS
1f83e 45 52 54 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ERT.. */. if(
1f83f 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c db->init.busy ||
1f840 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a pTblName==0 ){.
1f841 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 if( onError!
1f842 3d 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 =OE_Replace || p
1f843 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 Tab->pIndex==0.
1f844 20 20 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d || pTab-
1f845 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 >pIndex->onError
1f846 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20 ==OE_Replace){.
1f847 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 pIndex->pNe
1f848 78 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 xt = pTab->pInde
1f849 78 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 x;. pTab->p
1f84a 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a Index = pIndex;.
1f84b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1f84c 20 49 6e 64 65 78 20 2a 70 4f 74 68 65 72 20 3d Index *pOther =
1f84d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 pTab->pIndex;.
1f84e 20 20 20 20 20 77 68 69 6c 65 28 20 70 4f 74 68 while( pOth
1f84f 65 72 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f 74 er->pNext && pOt
1f850 68 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 her->pNext->onEr
1f851 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 ror!=OE_Replace
1f852 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 74 68 65 ){. pOthe
1f853 72 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 r = pOther->pNex
1f854 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
1f855 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d pIndex->pNext =
1f856 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a pOther->pNext;.
1f857 20 20 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e pOther->pN
1f858 65 78 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 ext = pIndex;.
1f859 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 20 3d }. pIndex =
1f85a 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 0;. }.. /* Cl
1f85b 65 61 6e 20 75 70 20 62 65 66 6f 72 65 20 65 78 ean up before ex
1f85c 69 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72 iting */.exit_cr
1f85d 65 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69 66 eate_index:. if
1f85e 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 ( pIndex ){.
1f85f 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 freeIndex(pIndex
1f860 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
1f861 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
1f862 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c b, pList);. sql
1f863 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 ite3SrcListDelet
1f864 65 28 64 62 2c 20 70 54 62 6c 4e 61 6d 65 29 3b e(db, pTblName);
1f865 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1f866 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 (db, zName);. r
1f867 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eturn;.}../*.**
1f868 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1f869 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 66 make sure the f
1f86a 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65 ile format numbe
1f86b 72 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69 r is at least mi
1f86c 6e 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20 nFormat..** The
1f86d 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77 generated code w
1f86e 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65 ill increase the
1f86f 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d file format num
1f870 62 65 72 20 69 66 20 6e 65 63 65 73 73 61 72 79 ber if necessary
1f871 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f872 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f873 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 MinimumFileForma
1f874 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c t(Parse *pParse,
1f875 20 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69 int iDb, int mi
1f876 6e 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65 nFormat){. Vdbe
1f877 20 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 *v;. v = sqlit
1f878 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1f879 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 );. if( v ){.
1f87a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 int r1 = sqlit
1f87b 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
1f87c 72 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 72 32 rse);. int r2
1f87d 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
1f87e 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
1f87f 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 73 71 int j1;. sq
1f880 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1f881 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 v, OP_ReadCookie
1f882 2c 20 69 44 62 2c 20 72 31 2c 20 31 29 3b 0a 20 , iDb, r1, 1);.
1f883 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 sqlite3VdbeUs
1f884 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b esBtree(v, iDb);
1f885 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1f886 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
1f887 65 67 65 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c eger, minFormat,
1f888 20 72 32 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 r2);. j1 = s
1f889 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1f88a 28 76 2c 20 4f 50 5f 47 65 2c 20 72 32 2c 20 30 (v, OP_Ge, r2, 0
1f88b 2c 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 , r1);. sqlit
1f88c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
1f88d 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 OP_SetCookie, iD
1f88e 62 2c 20 31 2c 20 72 32 29 3b 0a 20 20 20 20 73 b, 1, r2);. s
1f88f 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
1f890 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 re(v, j1);. s
1f891 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
1f892 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 pReg(pParse, r1)
1f893 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c ;. sqlite3Rel
1f894 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
1f895 73 65 2c 20 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a se, r2);. }.}..
1f896 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 /*.** Fill the I
1f897 6e 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 20 ndex.aiRowEst[]
1f898 61 72 72 61 79 20 77 69 74 68 20 64 65 66 61 75 array with defau
1f899 6c 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d lt information -
1f89a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
1f89b 74 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 to be used when
1f89c 77 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e 20 we have not run
1f89d 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d the ANALYZE comm
1f89e 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 6f 77 and..**.** aiRow
1f89f 45 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f 73 Est[0] is suppos
1f8a0 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 e to contain the
1f8a1 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 number of eleme
1f8a2 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 nts in the index
1f8a3 2e 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20 64 6f ..** Since we do
1f8a4 20 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65 73 73 not know, guess
1f8a5 20 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61 69 52 1 million. aiR
1f8a6 6f 77 45 73 74 5b 31 5d 20 69 73 20 61 6e 20 65 owEst[1] is an e
1f8a7 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 0a 2a stimate of the.*
1f8a8 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 * number of rows
1f8a9 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74 68 in the table th
1f8aa 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 at match any par
1f8ab 74 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 ticular value of
1f8ac 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 63 6f the.** first co
1f8ad 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 lumn of the inde
1f8ae 78 2e 20 20 61 69 52 6f 77 45 73 74 5b 32 5d 20 x. aiRowEst[2]
1f8af 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f is an estimate o
1f8b0 66 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 f the number.**
1f8b1 6f 66 20 72 6f 77 73 20 74 68 61 74 20 6d 61 74 of rows that mat
1f8b2 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 ch any particula
1f8b3 72 20 63 6f 6d 62 69 6e 69 61 74 69 6f 6e 20 6f r combiniation o
1f8b4 66 20 74 68 65 20 66 69 72 73 74 20 32 20 63 6f f the first 2 co
1f8b5 6c 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 20 lumns.** of the
1f8b6 69 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f 20 66 index. And so f
1f8b7 6f 72 74 68 2e 20 20 49 74 20 6d 75 73 74 20 61 orth. It must a
1f8b8 6c 77 61 79 73 20 62 65 20 74 68 65 20 63 61 73 lways be the cas
1f8b9 65 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 20 20 e that.*.**
1f8ba 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e aiRowEst[N
1f8bb 5d 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d 31 5d ]<=aiRowEst[N-1]
1f8bc 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 .** ai
1f8bd 52 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a RowEst[N]>=1.**.
1f8be 2a 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 74 68 ** Apart from th
1f8bf 61 74 2c 20 77 65 20 68 61 76 65 20 6c 69 74 74 at, we have litt
1f8c0 6c 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 73 69 le to go on besi
1f8c1 64 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 61 73 des intuition as
1f8c2 20 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 6f 77 to.** how aiRow
1f8c3 45 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 Est[] should be
1f8c4 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68 initialized. Th
1f8c5 65 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 72 61 e numbers genera
1f8c6 74 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 65 20 ted here.** are
1f8c7 62 61 73 65 64 20 6f 6e 20 74 79 70 69 63 61 6c based on typical
1f8c8 20 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 69 6e values found in
1f8c9 20 61 63 74 75 61 6c 20 69 6e 64 69 63 65 73 2e actual indices.
1f8ca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1f8cb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
1f8cc 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 efaultRowEst(Ind
1f8cd 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 75 6e 73 ex *pIdx){. uns
1f8ce 69 67 6e 65 64 20 2a 61 20 3d 20 70 49 64 78 2d igned *a = pIdx-
1f8cf 3e 61 69 52 6f 77 45 73 74 3b 0a 20 20 69 6e 74 >aiRowEst;. int
1f8d0 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 61 21 i;. assert( a!
1f8d1 3d 30 20 29 3b 0a 20 20 61 5b 30 5d 20 3d 20 31 =0 );. a[0] = 1
1f8d2 30 30 30 30 30 30 3b 0a 20 20 66 6f 72 28 69 3d 000000;. for(i=
1f8d3 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 pIdx->nColumn; i
1f8d4 3e 3d 35 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 61 >=5; i--){. a
1f8d5 5b 69 5d 20 3d 20 35 3b 0a 20 20 7d 0a 20 20 77 [i] = 5;. }. w
1f8d6 68 69 6c 65 28 20 69 3e 3d 31 20 29 7b 0a 20 20 hile( i>=1 ){.
1f8d7 20 20 61 5b 69 5d 20 3d 20 31 31 20 2d 20 69 3b a[i] = 11 - i;
1f8d8 0a 20 20 20 20 69 2d 2d 3b 0a 20 20 7d 0a 20 20 . i--;. }.
1f8d9 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f if( pIdx->onErro
1f8da 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 r!=OE_None ){.
1f8db 20 20 61 5b 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d a[pIdx->nColum
1f8dc 6e 5d 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f n] = 1;. }.}../
1f8dd 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1f8de 65 20 77 69 6c 6c 20 64 72 6f 70 20 61 6e 20 65 e will drop an e
1f8df 78 69 73 74 69 6e 67 20 6e 61 6d 65 64 20 69 6e xisting named in
1f8e0 64 65 78 2e 20 20 54 68 69 73 20 72 6f 75 74 69 dex. This routi
1f8e1 6e 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 73 ne.** implements
1f8e2 20 74 68 65 20 44 52 4f 50 20 49 4e 44 45 58 20 the DROP INDEX
1f8e3 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 statement..*/.SQ
1f8e4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1f8e5 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 d sqlite3DropInd
1f8e6 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ex(Parse *pParse
1f8e7 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 , SrcList *pName
1f8e8 2c 20 69 6e 74 20 69 66 45 78 69 73 74 73 29 7b , int ifExists){
1f8e9 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
1f8ea 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 ;. Vdbe *v;. s
1f8eb 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1f8ec 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 rse->db;. int i
1f8ed 44 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 Db;.. if( pPars
1f8ee 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d e->nErr || db->m
1f8ef 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f8f0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
1f8f1 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61 p_index;. }. a
1f8f2 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 ssert( pName->nS
1f8f3 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53 rc==1 );. if( S
1f8f4 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 QLITE_OK!=sqlite
1f8f5 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 3ReadSchema(pPar
1f8f6 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 se) ){. goto
1f8f7 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b exit_drop_index;
1f8f8 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 . }. pIndex =
1f8f9 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 sqlite3FindIndex
1f8fa 28 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d (db, pName->a[0]
1f8fb 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 .zName, pName->a
1f8fc 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a [0].zDatabase);.
1f8fd 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 if( pIndex==0
1f8fe 29 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78 ){. if( !ifEx
1f8ff 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71 ists ){. sq
1f900 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1f901 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 arse, "no such i
1f902 6e 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65 ndex: %S", pName
1f903 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 0);. }.
1f904 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 pParse->checkSch
1f905 65 6d 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 ema = 1;. got
1f906 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 o exit_drop_inde
1f907 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e x;. }. if( pIn
1f908 64 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 dex->autoIndex )
1f909 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
1f90a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 orMsg(pParse, "i
1f90b 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 ndex associated
1f90c 77 69 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20 with UNIQUE ".
1f90d 20 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20 "or PRIMARY
1f90e 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 KEY constraint c
1f90f 61 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 annot be dropped
1f910 22 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 ", 0);. goto
1f911 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b exit_drop_index;
1f912 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c . }. iDb = sql
1f913 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 ite3SchemaToInde
1f914 78 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 x(db, pIndex->pS
1f915 63 68 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20 chema);.#ifndef
1f916 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
1f917 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 ORIZATION. {.
1f918 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c int code = SQL
1f919 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a ITE_DROP_INDEX;.
1f91a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 Table *pTab
1f91b 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 = pIndex->pTable
1f91c 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 ;. const char
1f91d 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b *zDb = db->aDb[
1f91e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 iDb].zName;.
1f91f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 const char *zTab
1f920 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 = SCHEMA_TABLE(
1f921 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 iDb);. if( sq
1f922 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
1f923 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 Parse, SQLITE_DE
1f924 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a LETE, zTab, 0, z
1f925 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 Db) ){. got
1f926 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 o exit_drop_inde
1f927 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 x;. }. if(
1f928 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 !OMIT_TEMPDB &&
1f929 20 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51 iDb ) code = SQ
1f92a 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 LITE_DROP_TEMP_I
1f92b 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 NDEX;. if( sq
1f92c 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
1f92d 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e Parse, code, pIn
1f92e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 dex->zName, pTab
1f92f 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b ->zName, zDb) ){
1f930 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 . goto exit
1f931 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 _drop_index;.
1f932 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
1f933 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 /* Generate cod
1f934 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 e to remove the
1f935 69 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74 index and from t
1f936 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 he master table
1f937 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 */. v = sqlite3
1f938 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
1f939 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 . if( v ){.
1f93a 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 sqlite3BeginWrit
1f93b 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 eOperation(pPars
1f93c 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 e, 1, iDb);.
1f93d 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 sqlite3NestedPar
1f93e 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 se(pParse,.
1f93f 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 "DELETE FROM %
1f940 51 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d Q.%s WHERE name=
1f941 25 51 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e %Q",. db->
1f942 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 aDb[iDb].zName,
1f943 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 SCHEMA_TABLE(iDb
1f944 29 2c 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78 ),. pIndex
1f945 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 ->zName. );.
1f946 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
1f947 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c ndTable(db, "sql
1f948 69 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e ite_stat1", db->
1f949 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 aDb[iDb].zName)
1f94a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1f94b 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 NestedParse(pPar
1f94c 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c se,. "DEL
1f94d 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 ETE FROM %Q.sqli
1f94e 74 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 69 te_stat1 WHERE i
1f94f 64 78 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 dx=%Q",.
1f950 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 db->aDb[iDb].zNa
1f951 6d 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d me, pIndex->zNam
1f952 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d e. );. }
1f953 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e . sqlite3Chan
1f954 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c geCookie(pParse,
1f955 20 69 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72 iDb);. destr
1f956 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 oyRootPage(pPars
1f957 65 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c e, pIndex->tnum,
1f958 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 iDb);. sqlit
1f959 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
1f95a 4f 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44 OP_DropIndex, iD
1f95b 62 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d b, 0, 0, pIndex-
1f95c 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a >zName, 0);. }.
1f95d 0a 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 .exit_drop_index
1f95e 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 :. sqlite3SrcLi
1f95f 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 stDelete(db, pNa
1f960 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41 me);.}../*.** pA
1f961 72 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65 rray is a pointe
1f962 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 r to an array of
1f963 20 6f 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20 objects. Each
1f964 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a object in the.**
1f965 20 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72 array is szEntr
1f966 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e y bytes in size.
1f967 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 This routine a
1f968 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a llocates a new.*
1f969 2a 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65 20 * object on the
1f96a 65 6e 64 20 6f 66 20 74 68 65 20 61 72 72 61 79 end of the array
1f96b 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79 ..**.** *pnEntry
1f96c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
1f96d 66 20 65 6e 74 72 69 65 73 20 61 6c 72 65 61 64 f entries alread
1f96e 79 20 69 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c y in use. *pnAl
1f96f 6c 6f 63 20 69 73 0a 2a 2a 20 74 68 65 20 70 72 loc is.** the pr
1f970 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 eviously allocat
1f971 65 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 ed size of the a
1f972 72 72 61 79 2e 20 20 69 6e 69 74 53 69 7a 65 20 rray. initSize
1f973 69 73 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 is the.** sugges
1f974 74 65 64 20 69 6e 69 74 69 61 6c 20 61 72 72 61 ted initial arra
1f975 79 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f y size allocatio
1f976 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 n..**.** The ind
1f977 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e ex of the new en
1f978 74 72 79 20 69 73 20 72 65 74 75 72 6e 65 64 20 try is returned
1f979 69 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 in *pIdx..**.**
1f97a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 This routine ret
1f97b 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
1f97c 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f o the array of o
1f97d 62 6a 65 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a bjects. This.**
1f97e 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 73 61 might be the sa
1f97f 6d 65 20 61 73 20 74 68 65 20 70 41 72 72 61 79 me as the pArray
1f980 20 70 61 72 61 6d 65 74 65 72 20 6f 72 20 69 74 parameter or it
1f981 20 6d 69 67 68 74 20 62 65 20 61 20 64 69 66 66 might be a diff
1f982 65 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 erent.** pointer
1f983 20 69 66 20 74 68 65 20 61 72 72 61 79 20 77 61 if the array wa
1f984 73 20 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51 s resized..*/.SQ
1f985 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1f986 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 d *sqlite3ArrayA
1f987 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74 llocate(. sqlit
1f988 65 33 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 e3 *db, /*
1f989 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f Connection to no
1f98a 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 tify of malloc f
1f98b 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 ailures */. voi
1f98c 64 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f d *pArray, /
1f98d 2a 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 * Array of objec
1f98e 74 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65 ts. Might be re
1f98f 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 allocated */. i
1f990 6e 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 nt szEntry,
1f991 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 /* Size of each
1f992 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61 object in the a
1f993 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e rray */. int in
1f994 69 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53 itSize, /* S
1f995 75 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c uggested initial
1f996 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 allocation, in
1f997 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e elements */. in
1f998 74 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20 t *pnEntry,
1f999 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a /* Number of obj
1f99a 65 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 ects currently i
1f99b 6e 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a n use */. int *
1f99c 70 6e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20 pnAlloc, /*
1f99d 43 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 Current size of
1f99e 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 the allocation,
1f99f 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 in elements */.
1f9a0 20 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20 int *pIdx
1f9a1 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
1f9a2 69 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73 index of a new s
1f9a3 6c 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 lot here */.){.
1f9a4 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 char *z;. if(
1f9a5 2a 70 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41 *pnEntry >= *pnA
1f9a6 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64 lloc ){. void
1f9a7 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 *pNew;. int
1f9a8 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77 newSize;. new
1f9a9 53 69 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63 Size = (*pnAlloc
1f9aa 29 2a 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a )*2 + initSize;.
1f9ab 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
1f9ac 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 e3DbRealloc(db,
1f9ad 70 41 72 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a pArray, newSize*
1f9ae 73 7a 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 szEntry);. if
1f9af 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 ( pNew==0 ){.
1f9b0 20 20 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20 *pIdx = -1;.
1f9b1 20 20 20 20 20 72 65 74 75 72 6e 20 70 41 72 72 return pArr
1f9b2 61 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 ay;. }. *p
1f9b3 6e 41 6c 6c 6f 63 20 3d 20 6e 65 77 53 69 7a 65 nAlloc = newSize
1f9b4 3b 0a 20 20 20 20 70 41 72 72 61 79 20 3d 20 70 ;. pArray = p
1f9b5 4e 65 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 New;. }. z = (
1f9b6 63 68 61 72 2a 29 70 41 72 72 61 79 3b 0a 20 20 char*)pArray;.
1f9b7 6d 65 6d 73 65 74 28 26 7a 5b 2a 70 6e 45 6e 74 memset(&z[*pnEnt
1f9b8 72 79 20 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30 ry * szEntry], 0
1f9b9 2c 20 73 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70 , szEntry);. *p
1f9ba 49 64 78 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a Idx = *pnEntry;.
1f9bb 20 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 ++*pnEntry;.
1f9bc 72 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d return pArray;.}
1f9bd 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 ../*.** Append a
1f9be 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 new element to
1f9bf 74 68 65 20 67 69 76 65 6e 20 49 64 4c 69 73 74 the given IdList
1f9c0 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 . Create a new
1f9c1 49 64 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 IdList if.** nee
1f9c2 64 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 d be..**.** A ne
1f9c3 77 20 49 64 4c 69 73 74 20 69 73 20 72 65 74 75 w IdList is retu
1f9c4 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 rned, or NULL if
1f9c5 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e malloc() fails.
1f9c6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1f9c7 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 TE IdList *sqlit
1f9c8 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 e3IdListAppend(s
1f9c9 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 qlite3 *db, IdLi
1f9ca 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e st *pList, Token
1f9cb 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 *pToken){. int
1f9cc 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d i;. if( pList=
1f9cd 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 =0 ){. pList
1f9ce 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
1f9cf 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 cZero(db, sizeof
1f9d0 28 49 64 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 (IdList) );.
1f9d1 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 if( pList==0 ) r
1f9d2 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 eturn 0;. pLi
1f9d3 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a st->nAlloc = 0;.
1f9d4 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d }. pList->a =
1f9d5 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c sqlite3ArrayAll
1f9d6 6f 63 61 74 65 28 0a 20 20 20 20 20 20 64 62 2c ocate(. db,
1f9d7 0a 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c . pList->a,
1f9d8 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c . sizeof(pL
1f9d9 69 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 ist->a[0]),.
1f9da 20 20 35 2c 0a 20 20 20 20 20 20 26 70 4c 69 73 5,. &pLis
1f9db 74 2d 3e 6e 49 64 2c 0a 20 20 20 20 20 20 26 70 t->nId,. &p
1f9dc 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20 List->nAlloc,.
1f9dd 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 69 66 &i. );. if
1f9de 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c ( i<0 ){. sql
1f9df 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 ite3IdListDelete
1f9e0 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 (db, pList);.
1f9e1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
1f9e2 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 pList->a[i].zNa
1f9e3 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 me = sqlite3Name
1f9e4 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 FromToken(db, pT
1f9e5 6f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 oken);. return
1f9e6 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pList;.}../*.**
1f9e7 44 65 6c 65 74 65 20 61 6e 20 49 64 4c 69 73 74 Delete an IdList
1f9e8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f9e9 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f9ea 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c IdListDelete(sql
1f9eb 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 ite3 *db, IdList
1f9ec 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 *pList){. int
1f9ed 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d i;. if( pList==
1f9ee 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 0 ) return;. fo
1f9ef 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e r(i=0; i<pList->
1f9f0 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 nId; i++){. s
1f9f1 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f9f2 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 pList->a[i].zNa
1f9f3 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 me);. }. sqlit
1f9f4 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 e3DbFree(db, pLi
1f9f5 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 st->a);. sqlite
1f9f6 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 3DbFree(db, pLis
1f9f7 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 t);.}../*.** Ret
1f9f8 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e urn the index in
1f9f9 20 70 4c 69 73 74 20 6f 66 20 74 68 65 20 69 64 pList of the id
1f9fa 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 64 20 7a entifier named z
1f9fb 49 64 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a Id. Return -1.*
1f9fc 2a 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a * if not found..
1f9fd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f9fe 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c E int sqlite3IdL
1f9ff 69 73 74 49 6e 64 65 78 28 49 64 4c 69 73 74 20 istIndex(IdList
1fa00 2a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 *pList, const ch
1fa01 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e ar *zName){. in
1fa02 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 t i;. if( pList
1fa03 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b ==0 ) return -1;
1fa04 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c . for(i=0; i<pL
1fa05 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a ist->nId; i++){.
1fa06 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1fa07 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b trICmp(pList->a[
1fa08 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 i].zName, zName)
1fa09 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a ==0 ) return i;.
1fa0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b }. return -1;
1fa0b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 .}../*.** Append
1fa0c 20 61 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d a new table nam
1fa0d 65 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 53 e to the given S
1fa0e 72 63 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20 rcList. Create
1fa0f 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 69 66 a new SrcList if
1fa10 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 20 20 41 20 .** need be. A
1fa11 6e 65 77 20 65 6e 74 72 79 20 69 73 20 63 72 65 new entry is cre
1fa12 61 74 65 64 20 69 6e 20 74 68 65 20 53 72 63 4c ated in the SrcL
1fa13 69 73 74 20 65 76 65 6e 20 69 66 20 70 54 6f 6b ist even if pTok
1fa14 65 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a en is NULL..**.*
1fa15 2a 20 41 20 6e 65 77 20 53 72 63 4c 69 73 74 20 * A new SrcList
1fa16 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 is returned, or
1fa17 4e 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29 NULL if malloc()
1fa18 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 fails..**.** If
1fa19 20 70 44 61 74 61 62 61 73 65 20 69 73 20 6e 6f pDatabase is no
1fa1a 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 6e 73 t null, it means
1fa1b 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 that the table
1fa1c 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a has an optional.
1fa1d 2a 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 ** database name
1fa1e 20 70 72 65 66 69 78 2e 20 20 4c 69 6b 65 20 74 prefix. Like t
1fa1f 68 69 73 3a 20 20 22 64 61 74 61 62 61 73 65 2e his: "database.
1fa20 74 61 62 6c 65 22 2e 20 20 54 68 65 20 70 44 61 table". The pDa
1fa21 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69 6e 74 73 tabase.** points
1fa22 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6e 61 to the table na
1fa23 6d 65 20 61 6e 64 20 74 68 65 20 70 54 61 62 6c me and the pTabl
1fa24 65 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 e points to the
1fa25 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a database name..*
1fa26 2a 20 54 68 65 20 53 72 63 4c 69 73 74 2e 61 5b * The SrcList.a[
1fa27 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64 20 69 73 ].zName field is
1fa28 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 filled with the
1fa29 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 68 69 63 table name whic
1fa2a 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d 65 20 h might.** come
1fa2b 66 72 6f 6d 20 70 54 61 62 6c 65 20 28 69 66 20 from pTable (if
1fa2c 70 44 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c pDatabase is NUL
1fa2d 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44 61 74 61 L) or from pData
1fa2e 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72 63 4c 69 base. .** SrcLi
1fa2f 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 73 65 st.a[].zDatabase
1fa30 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 is filled with
1fa31 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
1fa32 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a e from pTable,.*
1fa33 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c 4c 20 69 * or with NULL i
1fa34 66 20 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73 f no database is
1fa35 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a specified..**.*
1fa36 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
1fa37 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b 65 20 74 , if call like t
1fa38 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 his:.**.**
1fa39 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 sqlite3SrcLis
1fa3a 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c 30 29 tAppend(D,A,B,0)
1fa3b 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42 20 69 ;.**.** Then B i
1fa3c 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 s a table name a
1fa3d 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nd the database
1fa3e 6e 61 6d 65 20 69 73 20 75 6e 73 70 65 63 69 66 name is unspecif
1fa3f 69 65 64 2e 20 20 49 66 20 63 61 6c 6c 65 64 0a ied. If called.
1fa40 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a ** like this:.**
1fa41 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 .** sqli
1fa42 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 te3SrcListAppend
1fa43 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a 2a 2a (D,A,B,C);.**.**
1fa44 20 54 68 65 6e 20 43 20 69 73 20 74 68 65 20 74 Then C is the t
1fa45 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 42 20 able name and B
1fa46 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 is the database
1fa47 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f name..*/.SQLITE_
1fa48 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 PRIVATE SrcList
1fa49 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 *sqlite3SrcListA
1fa4a 70 70 65 6e 64 28 0a 20 20 73 71 6c 69 74 65 33 ppend(. sqlite3
1fa4b 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 *db, /*
1fa4c 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f Connection to no
1fa4d 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 tify of malloc f
1fa4e 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 53 72 63 ailures */. Src
1fa4f 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 List *pList,
1fa50 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f 20 74 68 /* Append to th
1fa51 69 73 20 53 72 63 4c 69 73 74 2e 20 4e 55 4c 4c is SrcList. NULL
1fa52 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 20 53 creates a new S
1fa53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54 6f 6b 65 rcList */. Toke
1fa54 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 n *pTable,
1fa55 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61 70 70 65 /* Table to appe
1fa56 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 nd */. Token *p
1fa57 44 61 74 61 62 61 73 65 20 20 20 20 2f 2a 20 44 Database /* D
1fa58 61 74 61 62 61 73 65 20 6f 66 20 74 68 65 20 74 atabase of the t
1fa59 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 able */.){. str
1fa5a 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1fa5b 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 *pItem;. if( p
1fa5c 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 List==0 ){. p
1fa5d 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 List = sqlite3Db
1fa5e 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
1fa5f 69 7a 65 6f 66 28 53 72 63 4c 69 73 74 29 20 29 izeof(SrcList) )
1fa60 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d ;. if( pList=
1fa61 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
1fa62 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 pList->nAlloc
1fa63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 1;. }. if(
1fa64 70 4c 69 73 74 2d 3e 6e 53 72 63 3e 3d 70 4c 69 pList->nSrc>=pLi
1fa65 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 st->nAlloc ){.
1fa66 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b SrcList *pNew;
1fa67 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c . pList->nAll
1fa68 6f 63 20 2a 3d 20 32 3b 0a 20 20 20 20 70 4e 65 oc *= 2;. pNe
1fa69 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 w = sqlite3DbRea
1fa6a 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73 74 2c 0a lloc(db, pList,.
1fa6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
1fa6c 69 7a 65 6f 66 28 2a 70 4c 69 73 74 29 20 2b 20 izeof(*pList) +
1fa6d 28 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2d 31 (pList->nAlloc-1
1fa6e 29 2a 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e )*sizeof(pList->
1fa6f 61 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 a[0]) );. if(
1fa70 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 pNew==0 ){.
1fa71 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 sqlite3SrcList
1fa72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 Delete(db, pList
1fa73 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1fa74 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 69 0;. }. pLi
1fa75 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 st = pNew;. }.
1fa76 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d pItem = &pList-
1fa77 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63 5d 3b >a[pList->nSrc];
1fa78 0a 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c . memset(pItem,
1fa79 20 30 2c 20 73 69 7a 65 6f 66 28 70 4c 69 73 74 0, sizeof(pList
1fa7a 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 69 66 28 20 ->a[0]));. if(
1fa7b 70 44 61 74 61 62 61 73 65 20 26 26 20 70 44 61 pDatabase && pDa
1fa7c 74 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29 7b 0a tabase->z==0 ){.
1fa7d 20 20 20 20 70 44 61 74 61 62 61 73 65 20 3d 20 pDatabase =
1fa7e 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61 0;. }. if( pDa
1fa7f 74 61 62 61 73 65 20 26 26 20 70 54 61 62 6c 65 tabase && pTable
1fa80 20 29 7b 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70 ){. Token *p
1fa81 54 65 6d 70 20 3d 20 70 44 61 74 61 62 61 73 65 Temp = pDatabase
1fa82 3b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20 ;. pDatabase
1fa83 3d 20 70 54 61 62 6c 65 3b 0a 20 20 20 20 70 54 = pTable;. pT
1fa84 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0a 20 20 able = pTemp;.
1fa85 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 }. pItem->zName
1fa86 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
1fa87 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 omToken(db, pTab
1fa88 6c 65 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a 44 le);. pItem->zD
1fa89 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 atabase = sqlite
1fa8a 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
1fa8b 62 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20 b, pDatabase);.
1fa8c 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 pItem->iCursor
1fa8d 3d 20 2d 31 3b 0a 20 20 70 4c 69 73 74 2d 3e 6e = -1;. pList->n
1fa8e 53 72 63 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 Src++;. return
1fa8f 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pList;.}../*.**
1fa90 41 73 73 69 67 6e 20 56 64 62 65 43 75 72 73 6f Assign VdbeCurso
1fa91 72 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 73 20 r index numbers
1fa92 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e to all tables in
1fa93 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f 0a 53 51 a SrcList.*/.SQ
1fa94 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1fa95 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 d sqlite3SrcList
1fa96 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50 61 AssignCursors(Pa
1fa97 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 rse *pParse, Src
1fa98 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 List *pList){.
1fa99 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
1fa9a 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
1fa9b 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 70 4c tem;. assert(pL
1fa9c 69 73 74 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 ist || pParse->d
1fa9d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1fa9e 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 );. if( pList )
1fa9f 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 {. for(i=0, p
1faa0 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 Item=pList->a; i
1faa1 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b <pList->nSrc; i+
1faa2 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 +, pItem++){.
1faa3 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 if( pItem->iC
1faa4 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65 61 6b ursor>=0 ) break
1faa5 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 ;. pItem->i
1faa6 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d Cursor = pParse-
1faa7 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69 >nTab++;. i
1faa8 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 f( pItem->pSelec
1faa9 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c t ){. sql
1faaa 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 ite3SrcListAssig
1faab 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c nCursors(pParse,
1faac 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2d pItem->pSelect-
1faad 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a >pSrc);. }.
1faae 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1faaf 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 ** Delete an ent
1fab0 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e 63 6c ire SrcList incl
1fab1 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20 73 75 uding all its su
1fab2 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 bstructure..*/.S
1fab3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1fab4 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 id sqlite3SrcLis
1fab5 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 tDelete(sqlite3
1fab6 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c *db, SrcList *pL
1fab7 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ist){. int i;.
1fab8 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
1fab9 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 item *pItem;. i
1faba 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 f( pList==0 ) re
1fabb 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70 49 74 65 turn;. for(pIte
1fabc 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b m=pList->a, i=0;
1fabd 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 i<pList->nSrc;
1fabe 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
1fabf 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1fac0 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 (db, pItem->zDat
1fac1 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 abase);. sqli
1fac2 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 te3DbFree(db, pI
1fac3 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 tem->zName);.
1fac4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1fac5 62 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 b, pItem->zAlias
1fac6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
1fac7 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e Free(db, pItem->
1fac8 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 73 71 6c zIndex);. sql
1fac9 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 ite3DeleteTable(
1faca 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 pItem->pTab);.
1facb 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
1facc 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d elete(db, pItem-
1facd 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 >pSelect);. s
1face 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1facf 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 (db, pItem->pOn)
1fad0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c ;. sqlite3IdL
1fad1 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 49 istDelete(db, pI
1fad2 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 tem->pUsing);.
1fad3 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 }. sqlite3DbFre
1fad4 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a e(db, pList);.}.
1fad5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
1fad6 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ine is called by
1fad7 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 61 the parser to a
1fad8 64 64 20 61 20 6e 65 77 20 74 65 72 6d 20 74 6f dd a new term to
1fad9 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61 the.** end of a
1fada 20 67 72 6f 77 69 6e 67 20 46 52 4f 4d 20 63 6c growing FROM cl
1fadb 61 75 73 65 2e 20 20 54 68 65 20 22 70 22 20 70 ause. The "p" p
1fadc 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 arameter is the
1fadd 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 46 part of.** the F
1fade 52 4f 4d 20 63 6c 61 75 73 65 20 74 68 61 74 20 ROM clause that
1fadf 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
1fae0 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 20 22 constructed. "
1fae1 70 22 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66 p" is NULL.** if
1fae2 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 this is the fir
1fae3 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 46 st term of the F
1fae4 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 70 54 61 ROM clause. pTa
1fae5 62 6c 65 20 61 6e 64 20 70 44 61 74 61 62 61 73 ble and pDatabas
1fae6 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 6e 61 6d e.** are the nam
1fae7 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61 e of the table a
1fae8 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 nd database name
1fae9 64 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c d in the FROM cl
1faea 61 75 73 65 20 74 65 72 6d 2e 0a 2a 2a 20 70 44 ause term..** pD
1faeb 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 20 atabase is NULL
1faec 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 if the database
1faed 6e 61 6d 65 20 71 75 61 6c 69 66 69 65 72 20 69 name qualifier i
1faee 73 20 6d 69 73 73 69 6e 67 20 2d 20 74 68 65 0a s missing - the.
1faef 2a 2a 20 75 73 75 61 6c 20 63 61 73 65 2e 20 20 ** usual case.
1faf0 49 66 20 74 68 65 20 74 65 72 6d 20 68 61 73 20 If the term has
1faf1 61 20 61 6c 69 61 73 2c 20 74 68 65 6e 20 70 41 a alias, then pA
1faf2 6c 69 61 73 20 70 6f 69 6e 74 73 20 74 6f 20 74 lias points to t
1faf3 68 65 0a 2a 2a 20 61 6c 69 61 73 20 74 6f 6b 65 he.** alias toke
1faf4 6e 2e 20 20 49 66 20 74 68 65 20 74 65 72 6d 20 n. If the term
1faf5 69 73 20 61 20 73 75 62 71 75 65 72 79 2c 20 74 is a subquery, t
1faf6 68 65 6e 20 70 53 75 62 71 75 65 72 79 20 69 73 hen pSubquery is
1faf7 20 74 68 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73 the.** SELECT s
1faf8 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 74 68 tatement that th
1faf9 65 20 73 75 62 71 75 65 72 79 20 65 6e 63 6f 64 e subquery encod
1fafa 65 73 2e 20 20 54 68 65 20 70 54 61 62 6c 65 20 es. The pTable
1fafb 61 6e 64 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 and.** pDatabase
1fafc 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 parameters are
1fafd 4e 55 4c 4c 20 66 6f 72 20 73 75 62 71 75 65 72 NULL for subquer
1fafe 69 65 73 2e 20 20 54 68 65 20 70 4f 6e 20 61 6e ies. The pOn an
1faff 64 20 70 55 73 69 6e 67 0a 2a 2a 20 70 61 72 61 d pUsing.** para
1fb00 6d 65 74 65 72 73 20 61 72 65 20 74 68 65 20 63 meters are the c
1fb01 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 4f 4e ontent of the ON
1fb02 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 and USING claus
1fb03 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e es..**.** Return
1fb04 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 77 a new SrcList w
1fb05 68 69 63 68 20 65 6e 63 6f 64 65 73 20 69 73 20 hich encodes is
1fb06 74 68 65 20 46 52 4f 4d 20 77 69 74 68 20 74 68 the FROM with th
1fb07 65 20 6e 65 77 0a 2a 2a 20 74 65 72 6d 20 61 64 e new.** term ad
1fb08 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ded..*/.SQLITE_P
1fb09 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a RIVATE SrcList *
1fb0a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 sqlite3SrcListAp
1fb0b 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 0a 20 20 pendFromTerm(.
1fb0c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1fb0d 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
1fb0e 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
1fb0f 53 72 63 4c 69 73 74 20 2a 70 2c 20 20 20 20 20 SrcList *p,
1fb10 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c /* The l
1fb11 65 66 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 eft part of the
1fb12 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6c 72 65 FROM clause alre
1fb13 61 64 79 20 73 65 65 6e 20 2a 2f 0a 20 20 54 6f ady seen */. To
1fb14 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 ken *pTable,
1fb15 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1fb16 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 61 64 the table to ad
1fb17 64 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c d to the FROM cl
1fb18 61 75 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 ause */. Token
1fb19 2a 70 44 61 74 61 62 61 73 65 2c 20 20 20 20 20 *pDatabase,
1fb1a 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
1fb1b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
1fb1c 6e 69 6e 67 20 70 54 61 62 6c 65 20 2a 2f 0a 20 ning pTable */.
1fb1d 20 54 6f 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20 Token *pAlias,
1fb1e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1fb1f 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 right-hand side
1fb20 6f 66 20 74 68 65 20 41 53 20 73 75 62 65 78 70 of the AS subexp
1fb21 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c ression */. Sel
1fb22 65 63 74 20 2a 70 53 75 62 71 75 65 72 79 2c 20 ect *pSubquery,
1fb23 20 20 20 20 20 2f 2a 20 41 20 73 75 62 71 75 65 /* A subque
1fb24 72 79 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 ry used in place
1fb25 20 6f 66 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 of a table name
1fb26 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c */. Expr *pOn,
1fb27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1fb28 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f The ON clause o
1fb29 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64 f a join */. Id
1fb2a 4c 69 73 74 20 2a 70 55 73 69 6e 67 20 20 20 20 List *pUsing
1fb2b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55 53 49 /* The USI
1fb2c 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a NG clause of a j
1fb2d 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 oin */.){. stru
1fb2e 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1fb2f 2a 70 49 74 65 6d 3b 0a 20 20 73 71 6c 69 74 65 *pItem;. sqlite
1fb30 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1fb31 64 62 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 db;. p = sqlite
1fb32 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 3SrcListAppend(d
1fb33 62 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 b, p, pTable, pD
1fb34 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 atabase);. if(
1fb35 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 53 72 63 3d p==0 || p->nSrc=
1fb36 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1fb37 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
1fb38 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 pOn);. sqlite
1fb39 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 3IdListDelete(db
1fb3a 2c 20 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 73 , pUsing);. s
1fb3b 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
1fb3c 74 65 28 64 62 2c 20 70 53 75 62 71 75 65 72 79 te(db, pSubquery
1fb3d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 3b );. return p;
1fb3e 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26 . }. pItem = &
1fb3f 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b p->a[p->nSrc-1];
1fb40 0a 20 20 69 66 28 20 70 41 6c 69 61 73 20 26 26 . if( pAlias &&
1fb41 20 70 41 6c 69 61 73 2d 3e 6e 20 29 7b 0a 20 20 pAlias->n ){.
1fb42 20 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 pItem->zAlias
1fb43 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
1fb44 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 41 6c 69 61 mToken(db, pAlia
1fb45 73 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d s);. }. pItem-
1fb46 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 75 62 71 >pSelect = pSubq
1fb47 75 65 72 79 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 uery;. pItem->p
1fb48 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20 70 49 74 65 On = pOn;. pIte
1fb49 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 70 55 73 69 m->pUsing = pUsi
1fb4a 6e 67 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a ng;. return p;.
1fb4b 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 }../*.** Add an
1fb4c 49 4e 44 45 58 45 44 20 42 59 20 6f 72 20 4e 4f INDEXED BY or NO
1fb4d 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 T INDEXED clause
1fb4e 20 74 6f 20 74 68 65 20 6d 6f 73 74 20 72 65 63 to the most rec
1fb4f 65 6e 74 6c 79 20 61 64 64 65 64 20 0a 2a 2a 20 ently added .**
1fb50 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 73 element of the s
1fb51 6f 75 72 63 65 2d 6c 69 73 74 20 70 61 73 73 65 ource-list passe
1fb52 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
1fb53 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c argument..*/.SQL
1fb54 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1fb55 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 sqlite3SrcListI
1fb56 6e 64 65 78 65 64 42 79 28 50 61 72 73 65 20 2a ndexedBy(Parse *
1fb57 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 pParse, SrcList
1fb58 2a 70 2c 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 *p, Token *pInde
1fb59 78 65 64 42 79 29 7b 0a 20 20 69 66 28 20 70 49 xedBy){. if( pI
1fb5a 6e 64 65 78 65 64 42 79 20 26 26 20 70 20 26 26 ndexedBy && p &&
1fb5b 20 70 2d 3e 6e 53 72 63 3e 30 20 29 7b 0a 20 20 p->nSrc>0 ){.
1fb5c 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
1fb5d 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 _item *pItem = &
1fb5e 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b p->a[p->nSrc-1];
1fb5f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 . assert( pIt
1fb60 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d em->notIndexed==
1fb61 30 20 26 26 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 0 && pItem->zInd
1fb62 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 ex==0 );. if(
1fb63 20 70 49 6e 64 65 78 65 64 42 79 2d 3e 6e 3d 3d pIndexedBy->n==
1fb64 31 20 26 26 20 21 70 49 6e 64 65 78 65 64 42 79 1 && !pIndexedBy
1fb65 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ->z ){. /*
1fb66 41 20 22 4e 4f 54 20 49 4e 44 45 58 45 44 22 20 A "NOT INDEXED"
1fb67 63 6c 61 75 73 65 20 77 61 73 20 73 75 70 70 6c clause was suppl
1fb68 69 65 64 2e 20 53 65 65 20 70 61 72 73 65 2e 79 ied. See parse.y
1fb69 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 . ** const
1fb6a 72 75 63 74 20 22 69 6e 64 65 78 65 64 5f 6f 70 ruct "indexed_op
1fb6b 74 22 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 t" for details.
1fb6c 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e */. pItem->
1fb6d 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 31 3b 0a notIndexed = 1;.
1fb6e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1fb6f 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d pItem->zIndex =
1fb70 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
1fb71 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 Token(pParse->db
1fb72 2c 20 70 49 6e 64 65 78 65 64 42 79 29 3b 0a 20 , pIndexedBy);.
1fb73 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1fb74 2a 20 57 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 * When building
1fb75 75 70 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 up a FROM clause
1fb76 20 69 6e 20 74 68 65 20 70 61 72 73 65 72 2c 20 in the parser,
1fb77 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f the join operato
1fb78 72 0a 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 6c r.** is initiall
1fb79 79 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 y attached to th
1fb7a 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 2e 20 e left operand.
1fb7b 20 42 75 74 20 74 68 65 20 63 6f 64 65 20 67 65 But the code ge
1fb7c 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 78 70 65 63 nerator.** expec
1fb7d 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 ts the join oper
1fb7e 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e 20 74 68 ator to be on th
1fb7f 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 2e e right operand.
1fb80 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a This routine.*
1fb81 2a 20 53 68 69 66 74 73 20 61 6c 6c 20 6a 6f 69 * Shifts all joi
1fb82 6e 20 6f 70 65 72 61 74 6f 72 73 20 66 72 6f 6d n operators from
1fb83 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 20 66 left to right f
1fb84 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 46 52 4f or an entire FRO
1fb85 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a M.** clause..**.
1fb86 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 53 75 70 70 ** Example: Supp
1fb87 6f 73 65 20 74 68 65 20 6a 6f 69 6e 20 69 73 20 ose the join is
1fb88 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a like this:.**.**
1fb89 20 20 20 20 20 20 20 20 20 20 20 41 20 6e 61 74 A nat
1fb8a 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 20 ural cross join
1fb8b 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 65 72 B.**.** The oper
1fb8c 61 74 6f 72 20 69 73 20 22 6e 61 74 75 72 61 6c ator is "natural
1fb8d 20 63 72 6f 73 73 20 6a 6f 69 6e 22 2e 20 20 54 cross join". T
1fb8e 68 65 20 41 20 61 6e 64 20 42 20 6f 70 65 72 61 he A and B opera
1fb8f 6e 64 73 20 61 72 65 20 73 74 6f 72 65 64 0a 2a nds are stored.*
1fb90 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 * in p->a[0] and
1fb91 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 73 70 65 63 p->a[1], respec
1fb92 74 69 76 65 6c 79 2e 20 20 54 68 65 20 70 61 72 tively. The par
1fb93 73 65 72 20 69 6e 69 74 69 61 6c 6c 79 20 73 74 ser initially st
1fb94 6f 72 65 73 20 74 68 65 0a 2a 2a 20 6f 70 65 72 ores the.** oper
1fb95 61 74 6f 72 20 77 69 74 68 20 41 2e 20 20 54 68 ator with A. Th
1fb96 69 73 20 72 6f 75 74 69 6e 65 20 73 68 69 66 74 is routine shift
1fb97 73 20 74 68 61 74 20 6f 70 65 72 61 74 6f 72 20 s that operator
1fb98 6f 76 65 72 20 74 6f 20 42 2e 0a 2a 2f 0a 53 51 over to B..*/.SQ
1fb99 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1fb9a 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 d sqlite3SrcList
1fb9b 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 ShiftJoinType(Sr
1fb9c 63 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 66 28 cList *p){. if(
1fb9d 20 70 20 26 26 20 70 2d 3e 61 20 29 7b 0a 20 20 p && p->a ){.
1fb9e 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
1fb9f 28 69 3d 70 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e (i=p->nSrc-1; i>
1fba0 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 0; i--){. p
1fba1 2d 3e 61 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 ->a[i].jointype
1fba2 3d 20 70 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e = p->a[i-1].join
1fba3 74 79 70 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 type;. }.
1fba4 70 2d 3e 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 p->a[0].jointype
1fba5 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a = 0;. }.}../*.
1fba6 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 ** Begin a trans
1fba7 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 action.*/.SQLITE
1fba8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1fba9 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 lite3BeginTransa
1fbaa 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 ction(Parse *pPa
1fbab 72 73 65 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a rse, int type){.
1fbac 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
1fbad 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 Vdbe *v;. int
1fbae 69 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 i;.. if( pParse
1fbaf 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 ==0 || (db=pPars
1fbb0 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d e->db)==0 || db-
1fbb1 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 >aDb[0].pBt==0 )
1fbb2 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 return;. if( p
1fbb3 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 Parse->nErr || d
1fbb4 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1fbb5 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
1fbb6 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b sqlite3AuthCheck
1fbb7 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f (pParse, SQLITE_
1fbb8 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 42 45 TRANSACTION, "BE
1fbb9 47 49 4e 22 2c 20 30 2c 20 30 29 20 29 20 72 65 GIN", 0, 0) ) re
1fbba 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 6c turn;.. v = sql
1fbbb 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 ite3GetVdbe(pPar
1fbbc 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 29 20 se);. if( !v )
1fbbd 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 74 79 return;. if( ty
1fbbe 70 65 21 3d 54 4b 5f 44 45 46 45 52 52 45 44 20 pe!=TK_DEFERRED
1fbbf 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ){. for(i=0;
1fbc0 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
1fbc1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1fbc2 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 54 beAddOp2(v, OP_T
1fbc3 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 2c 20 28 ransaction, i, (
1fbc4 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c 55 53 49 type==TK_EXCLUSI
1fbc5 56 45 29 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 VE)+1);. sq
1fbc6 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 lite3VdbeUsesBtr
1fbc7 65 65 28 76 2c 20 69 29 3b 0a 20 20 20 20 7d 0a ee(v, i);. }.
1fbc8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
1fbc9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 eAddOp2(v, OP_Au
1fbca 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20 30 29 3b toCommit, 0, 0);
1fbcb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 .}../*.** Commit
1fbcc 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a a transaction.*
1fbcd 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1fbce 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d void sqlite3Com
1fbcf 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 mitTransaction(P
1fbd0 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 arse *pParse){.
1fbd1 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
1fbd2 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 Vdbe *v;.. if(
1fbd3 70 50 61 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 pParse==0 || (db
1fbd4 3d 70 50 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 =pParse->db)==0
1fbd5 7c 7c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 || db->aDb[0].pB
1fbd6 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 t==0 ) return;.
1fbd7 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 if( pParse->nEr
1fbd8 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 r || db->mallocF
1fbd9 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a ailed ) return;.
1fbda 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 if( sqlite3Aut
1fbdb 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 hCheck(pParse, S
1fbdc 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f QLITE_TRANSACTIO
1fbdd 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30 2c 20 N, "COMMIT", 0,
1fbde 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 0) ) return;..
1fbdf 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
1fbe0 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 be(pParse);. if
1fbe1 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( v ){. sqlit
1fbe2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1fbe3 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 OP_AutoCommit, 1
1fbe4 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a , 0);. }.}../*.
1fbe5 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20 74 72 ** Rollback a tr
1fbe6 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c ansaction.*/.SQL
1fbe7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1fbe8 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b sqlite3Rollback
1fbe9 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 Transaction(Pars
1fbea 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 e *pParse){. sq
1fbeb 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 lite3 *db;. Vdb
1fbec 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50 61 e *v;.. if( pPa
1fbed 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 rse==0 || (db=pP
1fbee 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 arse->db)==0 ||
1fbef 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d db->aDb[0].pBt==
1fbf0 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 0 ) return;. if
1fbf1 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c ( pParse->nErr |
1fbf2 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
1fbf3 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 ed ) return;. i
1fbf4 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 f( sqlite3AuthCh
1fbf5 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 eck(pParse, SQLI
1fbf6 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 TE_TRANSACTION,
1fbf7 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c 20 30 "ROLLBACK", 0, 0
1fbf8 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76 ) ) return;.. v
1fbf9 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
1fbfa 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 e(pParse);. if(
1fbfb 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 v ){. sqlite
1fbfc 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1fbfd 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c P_AutoCommit, 1,
1fbfe 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 1);. }.}../*.*
1fbff 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
1fc00 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 69 73 TEMP database is
1fc01 20 6f 70 65 6e 20 61 6e 64 20 61 76 61 69 6c 61 open and availa
1fc02 62 6c 65 20 66 6f 72 20 75 73 65 2e 20 20 52 65 ble for use. Re
1fc03 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 turn.** the numb
1fc04 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 4c er of errors. L
1fc05 65 61 76 65 20 61 6e 79 20 65 72 72 6f 72 20 6d eave any error m
1fc06 65 73 73 61 67 65 73 20 69 6e 20 74 68 65 20 70 essages in the p
1fc07 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 2e Parse structure.
1fc08 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1fc09 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 TE int sqlite3Op
1fc0a 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 50 enTempDatabase(P
1fc0b 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 arse *pParse){.
1fc0c 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1fc0d 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 Parse->db;. if(
1fc0e 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 3d db->aDb[1].pBt=
1fc0f 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e 65 =0 && !pParse->e
1fc10 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 69 6e xplain ){. in
1fc11 74 20 72 63 3b 0a 20 20 20 20 73 74 61 74 69 63 t rc;. static
1fc12 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 const int flags
1fc13 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 = . SQ
1fc14 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
1fc15 49 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 ITE |.
1fc16 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
1fc17 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 TE |. S
1fc18 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
1fc19 53 49 56 45 20 7c 0a 20 20 20 20 20 20 20 20 20 SIVE |.
1fc1a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
1fc1b 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 ETEONCLOSE |.
1fc1c 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
1fc1d 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a 20 20 20 EN_TEMP_DB;..
1fc1e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1fc1f 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c eeFactory(db, 0,
1fc20 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 0, SQLITE_DEFAU
1fc21 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 66 LT_CACHE_SIZE, f
1fc22 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 lags,.
1fc23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc24 20 20 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b &db->aDb[
1fc25 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 1].pBt);. if(
1fc26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1fc27 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1fc28 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1fc29 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 "unable to open
1fc2a 61 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 a temporary data
1fc2b 62 61 73 65 20 22 0a 20 20 20 20 20 20 20 20 22 base ". "
1fc2c 66 69 6c 65 20 66 6f 72 20 73 74 6f 72 69 6e 67 file for storing
1fc2d 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1fc2e 73 22 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 s");. pPars
1fc2f 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 e->rc = rc;.
1fc30 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
1fc31 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 64 }. assert( (d
1fc32 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 b->flags & SQLIT
1fc33 45 5f 49 6e 54 72 61 6e 73 29 3d 3d 30 20 7c 7c E_InTrans)==0 ||
1fc34 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
1fc35 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 );. assert( d
1fc36 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d b->aDb[1].pSchem
1fc37 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 a );. sqlite3
1fc38 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 PagerJournalMode
1fc39 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 (sqlite3BtreePag
1fc3a 65 72 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 er(db->aDb[1].pB
1fc3b 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 t),.
1fc3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc3d 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d db->dfltJournalM
1fc3e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ode);. }. retu
1fc3f 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 rn 0;.}../*.** G
1fc40 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 enerate VDBE cod
1fc41 65 20 74 68 61 74 20 77 69 6c 6c 20 76 65 72 69 e that will veri
1fc42 66 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f fy the schema co
1fc43 6f 6b 69 65 20 61 6e 64 20 73 74 61 72 74 0a 2a okie and start.*
1fc44 2a 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 * a read-transac
1fc45 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d tion for all nam
1fc46 65 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ed database file
1fc47 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 s..**.** It is i
1fc48 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 6c mportant that al
1fc49 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 l schema cookies
1fc4a 20 62 65 20 76 65 72 69 66 69 65 64 20 61 6e 64 be verified and
1fc4b 20 61 6c 6c 0a 2a 2a 20 72 65 61 64 20 74 72 61 all.** read tra
1fc4c 6e 73 61 63 74 69 6f 6e 73 20 62 65 20 73 74 61 nsactions be sta
1fc4d 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 74 rted before anyt
1fc4e 68 69 6e 67 20 65 6c 73 65 20 68 61 70 70 65 6e hing else happen
1fc4f 73 20 69 6e 0a 2a 2a 20 74 68 65 20 56 44 42 45 s in.** the VDBE
1fc50 20 70 72 6f 67 72 61 6d 2e 20 20 42 75 74 20 74 program. But t
1fc51 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 his routine can
1fc52 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 be called after
1fc53 6d 75 63 68 20 6f 74 68 65 72 0a 2a 2a 20 63 6f much other.** co
1fc54 64 65 20 68 61 73 20 62 65 65 6e 20 67 65 6e 65 de has been gene
1fc55 72 61 74 65 64 2e 20 20 53 6f 20 68 65 72 65 20 rated. So here
1fc56 69 73 20 77 68 61 74 20 77 65 20 64 6f 3a 0a 2a is what we do:.*
1fc57 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 *.** The first t
1fc58 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ime this routine
1fc59 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 63 is called, we c
1fc5a 6f 64 65 20 61 6e 20 4f 50 5f 47 6f 74 6f 20 74 ode an OP_Goto t
1fc5b 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70 hat.** will jump
1fc5c 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65 to a subroutine
1fc5d 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
1fc5e 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 65 he program. The
1fc5f 6e 20 77 65 0a 2a 2a 20 72 65 63 6f 72 64 20 65 n we.** record e
1fc60 76 65 72 79 20 64 61 74 61 62 61 73 65 20 74 68 very database th
1fc61 61 74 20 6e 65 65 64 73 20 69 74 73 20 73 63 68 at needs its sch
1fc62 65 6d 61 20 76 65 72 69 66 69 65 64 20 69 6e 20 ema verified in
1fc63 74 68 65 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 63 the.** pParse->c
1fc64 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65 6c 64 2e ookieMask field.
1fc65 20 20 4c 61 74 65 72 2c 20 61 66 74 65 72 20 61 Later, after a
1fc66 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61 ll other code ha
1fc67 73 20 62 65 65 6e 0a 2a 2a 20 67 65 6e 65 72 61 s been.** genera
1fc68 74 65 64 2c 20 74 68 65 20 73 75 62 72 6f 75 74 ted, the subrout
1fc69 69 6e 65 20 74 68 61 74 20 64 6f 65 73 20 74 68 ine that does th
1fc6a 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 e cookie verific
1fc6b 61 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 73 74 ations and.** st
1fc6c 61 72 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 arts the transac
1fc6d 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20 63 6f tions will be co
1fc6e 64 65 64 20 61 6e 64 20 74 68 65 20 4f 50 5f 47 ded and the OP_G
1fc6f 6f 74 6f 20 50 32 20 76 61 6c 75 65 0a 2a 2a 20 oto P2 value.**
1fc70 77 69 6c 6c 20 62 65 20 6d 61 64 65 20 74 6f 20 will be made to
1fc71 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73 75 point to that su
1fc72 62 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 67 broutine. The g
1fc73 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 eneration of the
1fc74 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 .** cookie verif
1fc75 69 63 61 74 69 6f 6e 20 73 75 62 72 6f 75 74 69 ication subrouti
1fc76 6e 65 20 63 6f 64 65 20 68 61 70 70 65 6e 73 20 ne code happens
1fc77 69 6e 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 in sqlite3Finish
1fc78 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20 Coding()..**.**
1fc79 49 66 20 69 44 62 3c 30 20 74 68 65 6e 20 63 6f If iDb<0 then co
1fc7a 64 65 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f de the OP_Goto o
1fc7b 6e 6c 79 20 2d 20 64 6f 6e 27 74 20 73 65 74 20 nly - don't set
1fc7c 66 6c 61 67 20 74 6f 20 76 65 72 69 66 79 20 74 flag to verify t
1fc7d 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20 he.** schema on
1fc7e 61 6e 79 20 64 61 74 61 62 61 73 65 73 2e 20 20 any databases.
1fc7f 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 This can be used
1fc80 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 to position the
1fc81 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c OP_Goto.** earl
1fc82 79 20 69 6e 20 74 68 65 20 63 6f 64 65 2c 20 62 y in the code, b
1fc83 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77 20 69 66 efore we know if
1fc84 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 74 61 any database ta
1fc85 62 6c 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65 bles will be use
1fc86 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1fc87 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1fc88 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 3CodeVerifySchem
1fc89 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c a(Parse *pParse,
1fc8a 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c int iDb){. sql
1fc8b 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 ite3 *db;. Vdbe
1fc8c 20 2a 76 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b *v;. int mask;
1fc8d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 .. v = sqlite3G
1fc8e 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
1fc8f 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 if( v==0 ) ret
1fc90 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e urn; /* This on
1fc91 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 ly happens if th
1fc92 65 72 65 20 77 61 73 20 61 20 70 72 69 6f 72 20 ere was a prior
1fc93 65 72 72 6f 72 20 2a 2f 0a 20 20 64 62 20 3d 20 error */. db =
1fc94 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 pParse->db;. if
1fc95 28 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 ( pParse->cookie
1fc96 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 70 Goto==0 ){. p
1fc97 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 Parse->cookieGot
1fc98 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 o = sqlite3VdbeA
1fc99 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f ddOp2(v, OP_Goto
1fc9a 2c 20 30 2c 20 30 29 2b 31 3b 0a 20 20 7d 0a 20 , 0, 0)+1;. }.
1fc9b 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 if( iDb>=0 ){.
1fc9c 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 assert( iDb<d
1fc9d 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 b->nDb );. as
1fc9e 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 sert( db->aDb[iD
1fc9f 62 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69 44 62 b].pBt!=0 || iDb
1fca0 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==1 );. asser
1fca1 74 28 20 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41 t( iDb<SQLITE_MA
1fca2 58 5f 41 54 54 41 43 48 45 44 2b 32 20 29 3b 0a X_ATTACHED+2 );.
1fca3 20 20 20 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 44 mask = 1<<iD
1fca4 62 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 72 b;. if( (pPar
1fca5 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 26 se->cookieMask &
1fca6 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 mask)==0 ){.
1fca7 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 pParse->cooki
1fca8 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 eMask |= mask;.
1fca9 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f pParse->coo
1fcaa 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 20 3d 20 kieValue[iDb] =
1fcab 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 db->aDb[iDb].pSc
1fcac 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f hema->schema_coo
1fcad 6b 69 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 kie;. if( !
1fcae 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 OMIT_TEMPDB && i
1fcaf 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 Db==1 ){.
1fcb0 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 sqlite3OpenTemp
1fcb1 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65 29 Database(pParse)
1fcb2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1fcb3 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}../*.** Gen
1fcb4 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 erate VDBE code
1fcb5 74 68 61 74 20 70 72 65 70 61 72 65 73 20 66 6f that prepares fo
1fcb6 72 20 64 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61 r doing an opera
1fcb7 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67 tion that.** mig
1fcb8 68 74 20 63 68 61 6e 67 65 20 74 68 65 20 64 61 ht change the da
1fcb9 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 tabase..**.** Th
1fcba 69 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 is routine start
1fcbb 73 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 s a new transact
1fcbc 69 6f 6e 20 69 66 20 77 65 20 61 72 65 20 6e 6f ion if we are no
1fcbd 74 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e t already within
1fcbe 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f .** a transactio
1fcbf 6e 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 6c n. If we are al
1fcc0 72 65 61 64 79 20 77 69 74 68 69 6e 20 61 20 74 ready within a t
1fcc1 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e ransaction, then
1fcc2 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a a checkpoint.**
1fcc3 20 69 73 20 73 65 74 20 69 66 20 74 68 65 20 73 is set if the s
1fcc4 65 74 53 74 61 74 65 6d 65 6e 74 20 70 61 72 61 etStatement para
1fcc5 6d 65 74 65 72 20 69 73 20 74 72 75 65 2e 20 20 meter is true.
1fcc6 41 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f A checkpoint sho
1fcc7 75 6c 64 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f uld.** be set fo
1fcc8 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 r operations tha
1fcc9 74 20 6d 69 67 68 74 20 66 61 69 6c 20 28 64 75 t might fail (du
1fcca 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e e to a constrain
1fccb 74 29 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 t) part of.** th
1fccc 65 20 77 61 79 20 74 68 72 6f 75 67 68 20 61 6e e way through an
1fccd 64 20 77 68 69 63 68 20 77 69 6c 6c 20 6e 65 65 d which will nee
1fcce 64 20 74 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77 d to undo some w
1fccf 72 69 74 65 73 20 77 69 74 68 6f 75 74 20 68 61 rites without ha
1fcd0 76 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 ving to.** rollb
1fcd1 61 63 6b 20 74 68 65 20 77 68 6f 6c 65 20 74 72 ack the whole tr
1fcd2 61 6e 73 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20 ansaction. For
1fcd3 6f 70 65 72 61 74 69 6f 6e 73 20 77 68 65 72 65 operations where
1fcd4 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 all constraints
1fcd5 0a 2a 2a 20 63 61 6e 20 62 65 20 63 68 65 63 6b .** can be check
1fcd6 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 ed before any ch
1fcd7 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 anges are made t
1fcd8 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 o the database,
1fcd9 69 74 20 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e it is never.** n
1fcda 65 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f ecessary to undo
1fcdb 20 61 20 77 72 69 74 65 20 61 6e 64 20 74 68 65 a write and the
1fcdc 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 checkpoint shou
1fcdd 6c 64 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a ld not be set..*
1fcde 2a 0a 2a 2a 20 4f 6e 6c 79 20 64 61 74 61 62 61 *.** Only databa
1fcdf 73 65 20 69 44 62 20 61 6e 64 20 74 68 65 20 74 se iDb and the t
1fce0 65 6d 70 20 64 61 74 61 62 61 73 65 20 61 72 65 emp database are
1fce1 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 62 made writable b
1fce2 79 20 74 68 69 73 20 63 61 6c 6c 2e 0a 2a 2a 20 y this call..**
1fce3 49 66 20 69 44 62 3d 3d 30 2c 20 74 68 65 6e 20 If iDb==0, then
1fce4 74 68 65 20 6d 61 69 6e 20 61 6e 64 20 74 65 6d the main and tem
1fce5 70 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 p databases are
1fce6 6d 61 64 65 20 77 72 69 74 61 62 6c 65 2e 20 20 made writable.
1fce7 20 49 66 0a 2a 2a 20 69 44 62 3d 3d 31 20 74 68 If.** iDb==1 th
1fce8 65 6e 20 6f 6e 6c 79 20 74 68 65 20 74 65 6d 70 en only the temp
1fce9 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 64 database is mad
1fcea 65 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66 20 e writable. If
1fceb 69 44 62 3e 31 20 74 68 65 6e 20 74 68 65 0a 2a iDb>1 then the.*
1fcec 2a 20 73 70 65 63 69 66 69 65 64 20 61 75 78 69 * specified auxi
1fced 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 61 liary database a
1fcee 6e 64 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 nd the temp data
1fcef 62 61 73 65 20 61 72 65 20 6d 61 64 65 20 77 72 base are made wr
1fcf0 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 itable..*/.SQLIT
1fcf1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1fcf2 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 qlite3BeginWrite
1fcf3 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 20 Operation(Parse
1fcf4 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 73 65 74 *pParse, int set
1fcf5 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69 Statement, int i
1fcf6 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d Db){. Vdbe *v =
1fcf7 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
1fcf8 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 pParse);. if( v
1fcf9 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1fcfa 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 sqlite3CodeVerif
1fcfb 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 ySchema(pParse,
1fcfc 69 44 62 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e iDb);. pParse->
1fcfd 77 72 69 74 65 4d 61 73 6b 20 7c 3d 20 31 3c 3c writeMask |= 1<<
1fcfe 69 44 62 3b 0a 20 20 69 66 28 20 73 65 74 53 74 iDb;. if( setSt
1fcff 61 74 65 6d 65 6e 74 20 26 26 20 70 50 61 72 73 atement && pPars
1fd00 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a e->nested==0 ){.
1fd01 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fd02 64 64 4f 70 31 28 76 2c 20 4f 50 5f 53 74 61 74 ddOp1(v, OP_Stat
1fd03 65 6d 65 6e 74 2c 20 69 44 62 29 3b 0a 20 20 7d ement, iDb);. }
1fd04 0a 20 20 69 66 28 20 28 4f 4d 49 54 5f 54 45 4d . if( (OMIT_TEM
1fd05 50 44 42 20 7c 7c 20 69 44 62 21 3d 31 29 20 26 PDB || iDb!=1) &
1fd06 26 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 & pParse->db->aD
1fd07 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20 b[1].pBt!=0 ){.
1fd08 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 sqlite3BeginW
1fd09 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 riteOperation(pP
1fd0a 61 72 73 65 2c 20 73 65 74 53 74 61 74 65 6d 65 arse, setStateme
1fd0b 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f nt, 1);. }.}../
1fd0c 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 *.** Check to se
1fd0d 65 20 69 66 20 70 49 6e 64 65 78 20 75 73 65 73 e if pIndex uses
1fd0e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 the collating s
1fd0f 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 equence pColl.
1fd10 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 Return.** true i
1fd11 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20 66 61 f it does and fa
1fd12 6c 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e lse if it does n
1fd13 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ot..*/.#ifndef S
1fd14 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 QLITE_OMIT_REIND
1fd15 45 58 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f EX.static int co
1fd16 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e llationMatch(con
1fd17 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 st char *zColl,
1fd18 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a Index *pIndex){.
1fd19 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
1fd1a 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d 3e 6e 43 =0; i<pIndex->nC
1fd1b 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 olumn; i++){.
1fd1c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d const char *z =
1fd1d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b pIndex->azColl[
1fd1e 69 5d 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 7a i];. if( z==z
1fd1f 43 6f 6c 6c 20 7c 7c 20 28 7a 20 26 26 20 7a 43 Coll || (z && zC
1fd20 6f 6c 6c 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 oll && 0==sqlite
1fd21 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c 3StrICmp(z, zCol
1fd22 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 l)) ){. ret
1fd23 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 1;. }. }
1fd24 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 . return 0;.}.#
1fd25 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 endif../*.** Rec
1fd26 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 ompute all indic
1fd27 65 73 20 6f 66 20 70 54 61 62 20 74 68 61 74 20 es of pTab that
1fd28 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e use the collatin
1fd29 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c g sequence pColl
1fd2a 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 ..** If pColl==0
1fd2b 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 20 then recompute
1fd2c 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 all indices of p
1fd2d 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 Tab..*/.#ifndef
1fd2e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e SQLITE_OMIT_REIN
1fd2f 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20 DEX.static void
1fd30 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50 61 72 reindexTable(Par
1fd31 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c se *pParse, Tabl
1fd32 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20 63 6f e *pTab, char co
1fd33 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49 nst *zColl){. I
1fd34 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 ndex *pIndex;
1fd35 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e /* An
1fd36 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 index associate
1fd37 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a d with pTab */..
1fd38 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54 61 for(pIndex=pTa
1fd39 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 b->pIndex; pInde
1fd3a 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65 78 x; pIndex=pIndex
1fd3b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
1fd3c 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f ( zColl==0 || co
1fd3d 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f llationMatch(zCo
1fd3e 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0a 20 ll, pIndex) ){.
1fd3f 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 int iDb = s
1fd40 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
1fd41 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 dex(pParse->db,
1fd42 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
1fd43 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 sqlite3Beg
1fd44 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e inWriteOperation
1fd45 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 (pParse, 0, iDb)
1fd46 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 ;. sqlite3R
1fd47 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 efillIndex(pPars
1fd48 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a e, pIndex, -1);.
1fd49 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 }. }.}.#end
1fd4a 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 if../*.** Recomp
1fd4b 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 ute all indices
1fd4c 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e of all tables in
1fd4d 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 77 all databases w
1fd4e 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69 here the.** indi
1fd4f 63 65 73 20 75 73 65 20 74 68 65 20 63 6f 6c 6c ces use the coll
1fd50 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70 ating sequence p
1fd51 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d Coll. If pColl=
1fd52 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 =0 then recomput
1fd53 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73 e.** all indices
1fd54 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a everywhere..*/.
1fd55 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1fd56 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 MIT_REINDEX.stat
1fd57 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78 44 ic void reindexD
1fd58 61 74 61 62 61 73 65 73 28 50 61 72 73 65 20 2a atabases(Parse *
1fd59 70 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f 6e pParse, char con
1fd5a 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62 st *zColl){. Db
1fd5b 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20 *pDb;
1fd5c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 /* A s
1fd5d 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 2a ingle database *
1fd5e 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 /. int iDb;
1fd5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd60 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
1fd61 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a index number */.
1fd62 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1fd63 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a pParse->db; /*
1fd64 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1fd65 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61 nnection */. Ha
1fd66 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20 shElem *k;
1fd67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 /* For
1fd68 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74 61 looping over ta
1fd69 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20 bles in pDb */.
1fd6a 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 Table *pTab;
1fd6b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fd6c 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 A table in the d
1fd6d 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20 66 6f atabase */.. fo
1fd6e 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d r(iDb=0, pDb=db-
1fd6f 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 >aDb; iDb<db->nD
1fd70 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29 b; iDb++, pDb++)
1fd71 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 44 {. assert( pD
1fd72 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 b!=0 );. for(
1fd73 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 k=sqliteHashFirs
1fd74 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d t(&pDb->pSchema-
1fd75 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b >tblHash); k; k
1fd76 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 =sqliteHashNext(
1fd77 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 k)){. pTab
1fd78 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 = (Table*)sqlite
1fd79 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 HashData(k);.
1fd7a 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28 reindexTable(
1fd7b 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 43 pParse, pTab, zC
1fd7c 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a oll);. }. }.
1fd7d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1fd7e 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f Generate code fo
1fd7f 72 20 74 68 65 20 52 45 49 4e 44 45 58 20 63 6f r the REINDEX co
1fd80 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 mmand..**.**
1fd81 20 20 20 20 52 45 49 4e 44 45 58 20 20 20 20 20 REINDEX
1fd82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd83 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 -- 1.**
1fd84 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3c REINDEX <
1fd85 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20 collation>
1fd86 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a -- 2.**
1fd87 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 REINDEX
1fd88 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 ?<database>.?<t
1fd89 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a ablename> -- 3.
1fd8a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 ** REINDE
1fd8b 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f X ?<database>.?
1fd8c 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20 <indexname> --
1fd8d 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63 4.**.** Form 1 c
1fd8e 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 auses all indice
1fd8f 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 s in all attache
1fd90 64 20 64 61 74 61 62 61 73 65 73 20 74 6f 20 62 d databases to b
1fd91 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f e rebuilt..** Fo
1fd92 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20 61 6c rm 2 rebuilds al
1fd93 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c l indices in all
1fd94 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20 databases that
1fd95 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a use the named.**
1fd96 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 collating funct
1fd97 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e ion. Forms 3 an
1fd98 64 20 34 20 72 65 62 75 69 6c 64 20 74 68 65 20 d 4 rebuild the
1fd99 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72 20 61 named index or a
1fd9a 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 61 73 ll.** indices as
1fd9b 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
1fd9c 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a e named table..*
1fd9d 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1fd9e 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 53 51 _OMIT_REINDEX.SQ
1fd9f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1fda0 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 d sqlite3Reindex
1fda1 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1fda2 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 Token *pName1, T
1fda3 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 oken *pName2){.
1fda4 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b CollSeq *pColl;
1fda5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fda6 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e Collating sequen
1fda7 63 65 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78 ce to be reindex
1fda8 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 ed, or NULL */.
1fda9 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 char *z;
1fdaa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fdab 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20 Name of a table
1fdac 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f or index */. co
1fdad 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 nst char *zDb;
1fdae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
1fdaf 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1fdb0 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 e */. Table *pT
1fdb1 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ab;
1fdb2 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e /* A table in
1fdb3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f the database */
1fdb4 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
1fdb5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1fdb6 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 * An index assoc
1fdb7 69 61 74 65 64 20 77 69 74 68 20 70 54 61 62 20 iated with pTab
1fdb8 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 */. int iDb;
1fdb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdba 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1fdbb 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f index number */
1fdbc 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1fdbd 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f pParse->db; /
1fdbe 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
1fdbf 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 onnection */. T
1fdc0 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20 oken *pObjName;
1fdc1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
1fdc2 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1fdc3 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 65 20 72 or index to be r
1fdc4 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f eindexed */.. /
1fdc5 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61 62 * Read the datab
1fdc6 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 ase schema. If a
1fdc7 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
1fdc8 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d leave an error m
1fdc9 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 essage. ** and
1fdca 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 code in pParse a
1fdcb 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 nd return NULL.
1fdcc 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f */. if( SQLITE_
1fdcd 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 OK!=sqlite3ReadS
1fdce 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b chema(pParse) ){
1fdcf 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1fdd0 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d .. if( pName1==
1fdd1 30 20 7c 7c 20 70 4e 61 6d 65 31 2d 3e 7a 3d 3d 0 || pName1->z==
1fdd2 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 0 ){. reindex
1fdd3 44 61 74 61 62 61 73 65 73 28 70 50 61 72 73 65 Databases(pParse
1fdd4 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e , 0);. return
1fdd5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e ;. }else if( pN
1fdd6 61 6d 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 ame2==0 || pName
1fdd7 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 63 2->z==0 ){. c
1fdd8 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 har *zColl;.
1fdd9 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e assert( pName1->
1fdda 7a 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d z );. zColl =
1fddb 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
1fddc 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 Token(pParse->db
1fddd 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 , pName1);. i
1fdde 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 f( !zColl ) retu
1fddf 72 6e 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 rn;. pColl =
1fde0 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 sqlite3FindCollS
1fde1 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 eq(db, ENC(db),
1fde2 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 29 3b 0a 20 zColl, -1, 0);.
1fde3 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a if( pColl ){.
1fde4 20 20 20 20 20 20 69 66 28 20 7a 43 6f 6c 6c 20 if( zColl
1fde5 29 7b 0a 20 20 20 20 20 20 20 20 72 65 69 6e 64 ){. reind
1fde6 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72 exDatabases(pPar
1fde7 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 se, zColl);.
1fde8 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1fde9 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 e(db, zColl);.
1fdea 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
1fdeb 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 rn;. }. sq
1fdec 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1fded 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 44 zColl);. }. iD
1fdee 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 b = sqlite3TwoPa
1fdef 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 rtName(pParse, p
1fdf0 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 Name1, pName2, &
1fdf1 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 pObjName);. if(
1fdf2 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b iDb<0 ) return;
1fdf3 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 . z = sqlite3Na
1fdf4 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 meFromToken(db,
1fdf5 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 pObjName);. if(
1fdf6 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a z==0 ) return;.
1fdf7 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b zDb = db->aDb[
1fdf8 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70 54 iDb].zName;. pT
1fdf9 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 ab = sqlite3Find
1fdfa 54 61 62 6c 65 28 64 62 2c 20 7a 2c 20 7a 44 62 Table(db, z, zDb
1fdfb 29 3b 0a 20 20 69 66 28 20 70 54 61 62 20 29 7b );. if( pTab ){
1fdfc 0a 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c . reindexTabl
1fdfd 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 e(pParse, pTab,
1fdfe 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 0);. sqlite3D
1fdff 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 bFree(db, z);.
1fe00 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
1fe01 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 pIndex = sqlite3
1fe02 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 2c FindIndex(db, z,
1fe03 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 zDb);. sqlite3
1fe04 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 DbFree(db, z);.
1fe05 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 if( pIndex ){.
1fe06 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 sqlite3BeginW
1fe07 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 riteOperation(pP
1fe08 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 arse, 0, iDb);.
1fe09 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c sqlite3Refill
1fe0a 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 Index(pParse, pI
1fe0b 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 72 ndex, -1);. r
1fe0c 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c eturn;. }. sql
1fe0d 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1fe0e 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 rse, "unable to
1fe0f 69 64 65 6e 74 69 66 79 20 74 68 65 20 6f 62 6a identify the obj
1fe10 65 63 74 20 74 6f 20 62 65 20 72 65 69 6e 64 65 ect to be reinde
1fe11 78 65 64 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a xed");.}.#endif.
1fe12 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
1fe13 64 79 6e 61 6d 69 63 6c 79 20 61 6c 6c 6f 63 61 dynamicly alloca
1fe14 74 65 64 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 ted KeyInfo stru
1fe15 63 74 75 72 65 20 74 68 61 74 20 63 61 6e 20 62 cture that can b
1fe16 65 20 75 73 65 64 0a 2a 2a 20 77 69 74 68 20 4f e used.** with O
1fe17 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 P_OpenRead or OP
1fe18 5f 4f 70 65 6e 57 72 69 74 65 20 74 6f 20 61 63 _OpenWrite to ac
1fe19 63 65 73 73 20 64 61 74 61 62 61 73 65 20 69 6e cess database in
1fe1a 64 65 78 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 dex pIdx..**.**
1fe1b 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 If successful, a
1fe1c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1fe1d 6e 65 77 20 73 74 72 75 63 74 75 72 65 20 69 73 new structure is
1fe1e 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 returned. In th
1fe1f 69 73 20 63 61 73 65 0a 2a 2a 20 74 68 65 20 63 is case.** the c
1fe20 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 aller is respons
1fe21 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 ible for calling
1fe22 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1fe23 62 2c 20 29 20 6f 6e 20 74 68 65 20 72 65 74 75 b, ) on the retu
1fe24 72 6e 65 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 rned .** pointer
1fe25 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
1fe26 63 75 72 73 20 28 6f 75 74 20 6f 66 20 6d 65 6d curs (out of mem
1fe27 6f 72 79 20 6f 72 20 6d 69 73 73 69 6e 67 20 63 ory or missing c
1fe28 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 73 65 71 ollation .** seq
1fe29 75 65 6e 63 65 29 2c 20 4e 55 4c 4c 20 69 73 20 uence), NULL is
1fe2a 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 returned and the
1fe2b 20 73 74 61 74 65 20 6f 66 20 70 50 61 72 73 65 state of pParse
1fe2c 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c updated to refl
1fe2d 65 63 74 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 ect.** the error
1fe2e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1fe2f 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c ATE KeyInfo *sql
1fe30 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f ite3IndexKeyinfo
1fe31 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1fe32 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 Index *pIdx){.
1fe33 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f int i;. int nCo
1fe34 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d l = pIdx->nColum
1fe35 6e 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20 n;. int nBytes
1fe36 3d 20 73 69 7a 65 6f 66 28 4b 65 79 49 6e 66 6f = sizeof(KeyInfo
1fe37 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a ) + (nCol-1)*siz
1fe38 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 eof(CollSeq*) +
1fe39 6e 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 nCol;. sqlite3
1fe3a 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1fe3b 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 ;. KeyInfo *pKe
1fe3c 79 20 3d 20 28 4b 65 79 49 6e 66 6f 20 2a 29 73 y = (KeyInfo *)s
1fe3d 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
1fe3e 72 6f 28 64 62 2c 20 6e 42 79 74 65 73 29 3b 0a ro(db, nBytes);.
1fe3f 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 . if( pKey ){.
1fe40 20 20 20 70 4b 65 79 2d 3e 64 62 20 3d 20 70 50 pKey->db = pP
1fe41 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 4b arse->db;. pK
1fe42 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d ey->aSortOrder =
1fe43 20 28 75 38 20 2a 29 26 28 70 4b 65 79 2d 3e 61 (u8 *)&(pKey->a
1fe44 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 20 Coll[nCol]);.
1fe45 20 61 73 73 65 72 74 28 20 26 70 4b 65 79 2d 3e assert( &pKey->
1fe46 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d aSortOrder[nCol]
1fe47 3d 3d 26 28 28 28 75 38 20 2a 29 70 4b 65 79 29 ==&(((u8 *)pKey)
1fe48 5b 6e 42 79 74 65 73 5d 29 20 29 3b 0a 20 20 20 [nBytes]) );.
1fe49 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c for(i=0; i<nCol
1fe4a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 ; i++){. ch
1fe4b 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 ar *zColl = pIdx
1fe4c 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 ->azColl[i];.
1fe4d 20 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c 6c assert( zColl
1fe4e 20 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e );. pKey->
1fe4f 61 43 6f 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69 74 aColl[i] = sqlit
1fe50 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 e3LocateCollSeq(
1fe51 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d pParse, zColl, -
1fe52 31 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 1);. pKey->
1fe53 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 aSortOrder[i] =
1fe54 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 pIdx->aSortOrder
1fe55 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 [i];. }. p
1fe56 4b 65 79 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 43 Key->nField = nC
1fe57 6f 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 ol;. }.. if( p
1fe58 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 Parse->nErr ){.
1fe59 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1fe5a 28 64 62 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 (db, pKey);.
1fe5b 70 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 pKey = 0;. }.
1fe5c 72 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a return pKey;.}..
1fe5d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1fe5e 45 6e 64 20 6f 66 20 62 75 69 6c 64 2e 63 20 2a End of build.c *
1fe5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fe61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1fe62 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1fe63 42 65 67 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62 Begin file callb
1fe64 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ack.c **********
1fe65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fe66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1fe67 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 /*.** 2005 May 2
1fe68 33 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 3 .**.** The aut
1fe69 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
1fe6a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1fe6b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1fe6c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1fe6d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1fe6e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1fe6f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1fe70 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
1fe71 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
1fe72 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
1fe73 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
1fe74 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
1fe75 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
1fe76 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
1fe77 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
1fe78 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
1fe79 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
1fe7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fe7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fe7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fe7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fe7e 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
1fe7f 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 e contains funct
1fe80 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 61 63 63 ions used to acc
1fe81 65 73 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ess the internal
1fe82 20 68 61 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20 hash tables.**
1fe83 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65 64 20 of user defined
1fe84 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f functions and co
1fe85 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
1fe86 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 61 s..**.** $Id: ca
1fe87 6c 6c 62 61 63 6b 2e 63 2c 76 20 31 2e 33 32 20 llback.c,v 1.32
1fe88 32 30 30 38 2f 31 30 2f 31 30 20 31 37 3a 34 31 2008/10/10 17:41
1fe89 3a 32 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :29 drh Exp $.*/
1fe8a 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 .../*.** Invoke
1fe8b 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e the 'collation n
1fe8c 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63 6b 20 eeded' callback
1fe8d 74 6f 20 72 65 71 75 65 73 74 20 61 20 63 6f 6c to request a col
1fe8e 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a lation sequence.
1fe8f 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ** in the databa
1fe90 73 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 se text encoding
1fe91 20 6f 66 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 of name zName,
1fe92 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a length nName..**
1fe93 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f If the collatio
1fe94 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2f 0a 73 74 n sequence.*/.st
1fe95 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c 43 6f atic void callCo
1fe96 6c 6c 4e 65 65 64 65 64 28 73 71 6c 69 74 65 33 llNeeded(sqlite3
1fe97 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
1fe98 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 *zName, int nNa
1fe99 6d 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 me){. assert( !
1fe9a 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 db->xCollNeeded
1fe9b 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 || !db->xCollNee
1fe9c 64 65 64 31 36 20 29 3b 0a 20 20 69 66 28 20 6e ded16 );. if( n
1fe9d 4e 61 6d 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d Name<0 ) nName =
1fe9e 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 sqlite3Strlen(d
1fe9f 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 b, zName);. if(
1fea0 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 db->xCollNeeded
1fea1 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 ){. char *zE
1fea2 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 xternal = sqlite
1fea3 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 3DbStrNDup(db, z
1fea4 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 Name, nName);.
1fea5 20 20 69 66 28 20 21 7a 45 78 74 65 72 6e 61 6c if( !zExternal
1fea6 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 64 ) return;. d
1fea7 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 28 64 b->xCollNeeded(d
1fea8 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 b->pCollNeededAr
1fea9 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e 43 28 g, db, (int)ENC(
1feaa 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b db), zExternal);
1feab 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1feac 65 65 28 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c ee(db, zExternal
1fead 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 );. }.#ifndef S
1feae 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
1feaf 0a 20 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c . if( db->xColl
1feb0 4e 65 65 64 65 64 31 36 20 29 7b 0a 20 20 20 20 Needed16 ){.
1feb1 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74 char const *zExt
1feb2 65 72 6e 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 ernal;. sqlit
1feb3 65 33 5f 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d e3_value *pTmp =
1feb4 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 sqlite3ValueNew
1feb5 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 (db);. sqlite
1feb6 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 54 6d 3ValueSetStr(pTm
1feb7 70 2c 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c p, nName, zName,
1feb8 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
1feb9 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
1feba 20 20 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 zExternal = sq
1febb 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 lite3ValueText(p
1febc 54 6d 70 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 Tmp, SQLITE_UTF1
1febd 36 4e 41 54 49 56 45 29 3b 0a 20 20 20 20 69 66 6NATIVE);. if
1febe 28 20 7a 45 78 74 65 72 6e 61 6c 20 29 7b 0a 20 ( zExternal ){.
1febf 20 20 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 db->xCollNe
1fec0 65 64 65 64 31 36 28 64 62 2d 3e 70 43 6f 6c 6c eded16(db->pColl
1fec1 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20 28 NeededArg, db, (
1fec2 69 6e 74 29 45 4e 43 28 64 62 29 2c 20 7a 45 78 int)ENC(db), zEx
1fec3 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 ternal);. }.
1fec4 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 sqlite3ValueF
1fec5 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 23 ree(pTmp);. }.#
1fec6 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 endif.}../*.** T
1fec7 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
1fec8 61 6c 6c 65 64 20 69 66 20 74 68 65 20 63 6f 6c alled if the col
1fec9 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 66 lation factory f
1feca 61 69 6c 73 20 74 6f 20 64 65 6c 69 76 65 72 20 ails to deliver
1fecb 61 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 a.** collation f
1fecc 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 62 unction in the b
1fecd 65 73 74 20 65 6e 63 6f 64 69 6e 67 20 62 75 74 est encoding but
1fece 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 6f 74 there may be ot
1fecf 68 65 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 her versions.**
1fed0 6f 66 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f of this collatio
1fed1 6e 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 n function (for
1fed2 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 other text encod
1fed3 69 6e 67 73 29 20 61 76 61 69 6c 61 62 6c 65 2e ings) available.
1fed4 20 55 73 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 Use one.** of t
1fed5 68 65 73 65 20 69 6e 73 74 65 61 64 20 69 66 20 hese instead if
1fed6 74 68 65 79 20 65 78 69 73 74 2e 20 41 76 6f 69 they exist. Avoi
1fed7 64 20 61 20 55 54 46 2d 38 20 3c 2d 3e 20 55 54 d a UTF-8 <-> UT
1fed8 46 2d 31 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20 F-16 conversion
1fed9 69 66 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 0a if.** possible..
1feda 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 79 */.static int sy
1fedb 6e 74 68 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 nthCollSeq(sqlit
1fedc 65 33 20 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 20 e3 *db, CollSeq
1fedd 2a 70 43 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53 *pColl){. CollS
1fede 65 71 20 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68 eq *pColl2;. ch
1fedf 61 72 20 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a ar *z = pColl->z
1fee0 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 Name;. int n =
1fee1 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 69 6e 74 strlen(z);. int
1fee2 20 69 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e i;. static con
1fee3 73 74 20 75 38 20 61 45 6e 63 5b 5d 20 3d 20 7b st u8 aEnc[] = {
1fee4 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c SQLITE_UTF16BE,
1fee5 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c SQLITE_UTF16LE,
1fee6 20 53 51 4c 49 54 45 5f 55 54 46 38 20 7d 3b 0a SQLITE_UTF8 };.
1fee7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 33 3b 20 for(i=0; i<3;
1fee8 69 2b 2b 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 32 i++){. pColl2
1fee9 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f = sqlite3FindCo
1feea 6c 6c 53 65 71 28 64 62 2c 20 61 45 6e 63 5b 69 llSeq(db, aEnc[i
1feeb 5d 2c 20 7a 2c 20 6e 2c 20 30 29 3b 0a 20 20 20 ], z, n, 0);.
1feec 20 69 66 28 20 70 43 6f 6c 6c 32 2d 3e 78 43 6d if( pColl2->xCm
1feed 70 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 p!=0 ){. me
1feee 6d 63 70 79 28 70 43 6f 6c 6c 2c 20 70 43 6f 6c mcpy(pColl, pCol
1feef 6c 32 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 l2, sizeof(CollS
1fef0 65 71 29 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c eq));. pCol
1fef1 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b 20 20 20 20 l->xDel = 0;
1fef2 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 /* Do not c
1fef3 6f 70 79 20 74 68 65 20 64 65 73 74 72 75 63 74 opy the destruct
1fef4 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 or */. retu
1fef5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1fef6 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1fef7 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d SQLITE_ERROR;.}
1fef8 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
1fef9 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 ction is respons
1fefa 69 62 6c 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e ible for invokin
1fefb 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 g the collation
1fefc 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b factory callback
1fefd 0a 2a 2a 20 6f 72 20 73 75 62 73 74 69 74 75 74 .** or substitut
1fefe 69 6e 67 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 ing a collation
1feff 73 65 71 75 65 6e 63 65 20 6f 66 20 61 20 64 69 sequence of a di
1ff00 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67 fferent encoding
1ff01 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 72 65 71 when the.** req
1ff02 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e uested collation
1ff03 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 sequence is not
1ff04 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 available in th
1ff05 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 e database nativ
1ff06 65 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a e.** encoding..*
1ff07 2a 20 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e * .** If it is n
1ff08 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 43 ot NULL, then pC
1ff09 6f 6c 6c 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 oll must point t
1ff0a 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e o the database n
1ff0b 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 0a ative encoding .
1ff0c 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 ** collation seq
1ff0d 75 65 6e 63 65 20 77 69 74 68 20 6e 61 6d 65 20 uence with name
1ff0e 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e zName, length nN
1ff0f 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ame..**.** The r
1ff10 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 65 eturn value is e
1ff11 69 74 68 65 72 20 74 68 65 20 63 6f 6c 6c 61 74 ither the collat
1ff12 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74 6f 20 ion sequence to
1ff13 62 65 20 75 73 65 64 20 69 6e 20 64 61 74 61 62 be used in datab
1ff14 61 73 65 0a 2a 2a 20 64 62 20 66 6f 72 20 63 6f ase.** db for co
1ff15 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6e 61 6d llation type nam
1ff16 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 e zName, length
1ff17 6e 4e 61 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20 nName, or NULL,
1ff18 69 66 20 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a if no collation.
1ff19 2a 2a 20 73 65 71 75 65 6e 63 65 20 63 61 6e 20 ** sequence can
1ff1a 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c be found..*/.SQL
1ff1b 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c ITE_PRIVATE Coll
1ff1c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 Seq *sqlite3GetC
1ff1d 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65 ollSeq(. sqlite
1ff1e 33 2a 20 64 62 2c 20 0a 20 20 43 6f 6c 6c 53 65 3* db, . CollSe
1ff1f 71 20 2a 70 43 6f 6c 6c 2c 20 0a 20 20 63 6f 6e q *pColl, . con
1ff20 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
1ff21 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 0a 29 7b 0a . int nName.){.
1ff22 20 20 43 6f 6c 6c 53 65 71 20 2a 70 3b 0a 0a 20 CollSeq *p;..
1ff23 20 70 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 69 66 p = pColl;. if
1ff24 28 20 21 70 20 29 7b 0a 20 20 20 20 70 20 3d 20 ( !p ){. p =
1ff25 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 sqlite3FindCollS
1ff26 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 eq(db, ENC(db),
1ff27 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 zName, nName, 0)
1ff28 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 7c ;. }. if( !p |
1ff29 7c 20 21 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 | !p->xCmp ){.
1ff2a 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f /* No collatio
1ff2b 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 n sequence of th
1ff2c 69 73 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 is type for this
1ff2d 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 67 encoding is reg
1ff2e 69 73 74 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20 istered.. **
1ff2f 43 61 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69 Call the collati
1ff30 6f 6e 20 66 61 63 74 6f 72 79 20 74 6f 20 73 65 on factory to se
1ff31 65 20 69 66 20 69 74 20 63 61 6e 20 73 75 70 70 e if it can supp
1ff32 6c 79 20 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a ly us with one..
1ff33 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 */. callC
1ff34 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2c 20 7a 4e ollNeeded(db, zN
1ff35 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 ame, nName);.
1ff36 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 p = sqlite3Find
1ff37 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 CollSeq(db, ENC(
1ff38 64 62 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d db), zName, nNam
1ff39 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 e, 0);. }. if(
1ff3a 20 70 20 26 26 20 21 70 2d 3e 78 43 6d 70 20 26 p && !p->xCmp &
1ff3b 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64 & synthCollSeq(d
1ff3c 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 70 20 3d b, p) ){. p =
1ff3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 0;. }. assert
1ff3e 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20 ( !p || p->xCmp
1ff3f 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d );. return p;.}
1ff40 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
1ff41 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f tine is called o
1ff42 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 n a collation se
1ff43 71 75 65 6e 63 65 20 62 65 66 6f 72 65 20 69 74 quence before it
1ff44 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 is used to.** c
1ff45 68 65 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 heck that it is
1ff46 64 65 66 69 6e 65 64 2e 20 41 6e 20 75 6e 64 65 defined. An unde
1ff47 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 fined collation
1ff48 73 65 71 75 65 6e 63 65 20 65 78 69 73 74 73 20 sequence exists
1ff49 77 68 65 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 when.** a databa
1ff4a 73 65 20 69 73 20 6c 6f 61 64 65 64 20 74 68 61 se is loaded tha
1ff4b 74 20 63 6f 6e 74 61 69 6e 73 20 72 65 66 65 72 t contains refer
1ff4c 65 6e 63 65 73 20 74 6f 20 63 6f 6c 6c 61 74 69 ences to collati
1ff4d 6f 6e 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 on sequences.**
1ff4e 74 68 61 74 20 68 61 76 65 20 6e 6f 74 20 62 65 that have not be
1ff4f 65 6e 20 64 65 66 69 6e 65 64 20 62 79 20 73 71 en defined by sq
1ff50 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
1ff51 6c 61 74 69 6f 6e 28 29 20 65 74 63 2e 0a 2a 2a lation() etc..**
1ff52 0a 2a 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c .** If required,
1ff53 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 this routine ca
1ff54 6c 6c 73 20 74 68 65 20 27 63 6f 6c 6c 61 74 69 lls the 'collati
1ff55 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c 6c 62 on needed' callb
1ff56 61 63 6b 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 ack to.** reques
1ff57 74 20 61 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f t a definition o
1ff58 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 f the collating
1ff59 73 65 71 75 65 6e 63 65 2e 20 49 66 20 74 68 69 sequence. If thi
1ff5a 73 20 64 6f 65 73 6e 27 74 20 77 6f 72 6b 2c 20 s doesn't work,
1ff5b 0a 2a 2a 20 61 6e 20 65 71 75 69 76 61 6c 65 6e .** an equivalen
1ff5c 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 t collating sequ
1ff5d 65 6e 63 65 20 74 68 61 74 20 75 73 65 73 20 61 ence that uses a
1ff5e 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 64 text encoding d
1ff5f 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d ifferent.** from
1ff60 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
1ff61 73 65 20 69 73 20 73 75 62 73 74 69 74 75 74 65 se is substitute
1ff62 64 2c 20 69 66 20 6f 6e 65 20 69 73 20 61 76 61 d, if one is ava
1ff63 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ilable..*/.SQLIT
1ff64 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1ff65 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 lite3CheckCollSe
1ff66 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c q(Parse *pParse,
1ff67 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 CollSeq *pColl)
1ff68 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b {. if( pColl ){
1ff69 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
1ff6a 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d 3e *zName = pColl->
1ff6b 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 6c 53 zName;. CollS
1ff6c 65 71 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 47 eq *p = sqlite3G
1ff6d 65 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 etCollSeq(pParse
1ff6e 2d 3e 64 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 ->db, pColl, zNa
1ff6f 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 me, -1);. if(
1ff70 20 21 70 20 29 7b 0a 20 20 20 20 20 20 69 66 28 !p ){. if(
1ff71 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 pParse->nErr==0
1ff72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1ff73 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1ff74 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c se, "no such col
1ff75 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a lation sequence:
1ff76 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 %s", zName);.
1ff77 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 72 }. pPar
1ff78 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 se->nErr++;.
1ff79 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1ff7a 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 ERROR;. }.
1ff7b 20 61 73 73 65 72 74 28 20 70 3d 3d 70 43 6f 6c assert( p==pCol
1ff7c 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 l );. }. retur
1ff7d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1ff7e 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 ../*.** Locate a
1ff7f 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 6e 74 nd return an ent
1ff80 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62 2e 61 ry from the db.a
1ff81 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74 61 62 CollSeq hash tab
1ff82 6c 65 2e 20 49 66 20 74 68 65 20 65 6e 74 72 79 le. If the entry
1ff83 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 .** specified by
1ff84 20 7a 4e 61 6d 65 20 61 6e 64 20 6e 4e 61 6d 65 zName and nName
1ff85 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e is not found an
1ff86 64 20 70 61 72 61 6d 65 74 65 72 20 27 63 72 65 d parameter 'cre
1ff87 61 74 65 27 20 69 73 0a 2a 2a 20 74 72 75 65 2c ate' is.** true,
1ff88 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e then create a n
1ff89 65 77 20 65 6e 74 72 79 2e 20 4f 74 68 65 72 77 ew entry. Otherw
1ff8a 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e ise return NULL.
1ff8b 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 6f 69 6e .**.** Each poin
1ff8c 74 65 72 20 73 74 6f 72 65 64 20 69 6e 20 74 68 ter stored in th
1ff8d 65 20 73 71 6c 69 74 65 33 2e 61 43 6f 6c 6c 53 e sqlite3.aCollS
1ff8e 65 71 20 68 61 73 68 20 74 61 62 6c 65 20 63 6f eq hash table co
1ff8f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a 20 61 72 72 ntains an.** arr
1ff90 61 79 20 6f 66 20 74 68 72 65 65 20 43 6f 6c 6c ay of three Coll
1ff91 53 65 71 20 73 74 72 75 63 74 75 72 65 73 2e 20 Seq structures.
1ff92 54 68 65 20 66 69 72 73 74 20 69 73 20 74 68 65 The first is the
1ff93 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
1ff94 6e 63 65 0a 2a 2a 20 70 72 65 66 66 65 72 72 65 nce.** prefferre
1ff95 64 20 66 6f 72 20 55 54 46 2d 38 2c 20 74 68 65 d for UTF-8, the
1ff96 20 73 65 63 6f 6e 64 20 55 54 46 2d 31 36 6c 65 second UTF-16le
1ff97 2c 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 , and the third
1ff98 55 54 46 2d 31 36 62 65 2e 0a 2a 2a 0a 2a 2a 20 UTF-16be..**.**
1ff99 53 74 6f 72 65 64 20 69 6d 6d 65 64 69 61 74 65 Stored immediate
1ff9a 6c 79 20 61 66 74 65 72 20 74 68 65 20 74 68 72 ly after the thr
1ff9b 65 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 ee collation seq
1ff9c 75 65 6e 63 65 73 20 69 73 20 61 20 63 6f 70 79 uences is a copy
1ff9d 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 of.** the colla
1ff9e 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 tion sequence na
1ff9f 6d 65 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f me. A pointer to
1ffa0 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 this string is
1ffa1 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 65 61 63 stored in.** eac
1ffa2 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 h collation sequ
1ffa3 65 6e 63 65 20 73 74 72 75 63 74 75 72 65 2e 0a ence structure..
1ffa4 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 */.static CollSe
1ffa5 71 20 2a 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e q *findCollSeqEn
1ffa6 74 72 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a try(. sqlite3 *
1ffa7 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 db,. const char
1ffa8 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e *zName,. int n
1ffa9 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65 61 Name,. int crea
1ffaa 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 te.){. CollSeq
1ffab 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 6e 4e *pColl;. if( nN
1ffac 61 6d 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20 ame<0 ) nName =
1ffad 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 sqlite3Strlen(db
1ffae 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c , zName);. pCol
1ffaf 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 l = sqlite3HashF
1ffb0 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 ind(&db->aCollSe
1ffb1 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 q, zName, nName)
1ffb2 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c ;.. if( 0==pCol
1ffb3 6c 20 26 26 20 63 72 65 61 74 65 20 29 7b 0a 20 l && create ){.
1ffb4 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 pColl = sqlit
1ffb5 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 e3DbMallocZero(d
1ffb6 62 2c 20 33 2a 73 69 7a 65 6f 66 28 2a 70 43 6f b, 3*sizeof(*pCo
1ffb7 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 20 ll) + nName + 1
1ffb8 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c );. if( pColl
1ffb9 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 ){. CollSe
1ffba 71 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20 q *pDel = 0;.
1ffbb 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d pColl[0].zNam
1ffbc 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c e = (char*)&pCol
1ffbd 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c l[3];. pCol
1ffbe 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 l[0].enc = SQLIT
1ffbf 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 43 E_UTF8;. pC
1ffc0 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28 oll[1].zName = (
1ffc1 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b char*)&pColl[3];
1ffc2 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e . pColl[1].
1ffc3 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 enc = SQLITE_UTF
1ffc4 31 36 4c 45 3b 0a 20 20 20 20 20 20 70 43 6f 6c 16LE;. pCol
1ffc5 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 l[2].zName = (ch
1ffc6 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 ar*)&pColl[3];.
1ffc7 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65 6e pColl[2].en
1ffc8 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 c = SQLITE_UTF16
1ffc9 42 45 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 BE;. memcpy
1ffca 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c (pColl[0].zName,
1ffcb 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a zName, nName);.
1ffcc 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a pColl[0].z
1ffcd 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b Name[nName] = 0;
1ffce 0a 20 20 20 20 20 20 70 44 65 6c 20 3d 20 73 71 . pDel = sq
1ffcf 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 lite3HashInsert(
1ffd0 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 70 &db->aCollSeq, p
1ffd1 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 6e Coll[0].zName, n
1ffd2 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a 20 Name, pColl);..
1ffd3 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c /* If a mal
1ffd4 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 63 loc() failure oc
1ffd5 63 75 72 65 64 20 69 6e 20 73 71 6c 69 74 65 33 cured in sqlite3
1ffd6 48 61 73 68 49 6e 73 65 72 74 28 29 2c 20 69 74 HashInsert(), it
1ffd7 20 77 69 6c 6c 20 0a 20 20 20 20 20 20 2a 2a 20 will . **
1ffd8 72 65 74 75 72 6e 20 74 68 65 20 70 43 6f 6c 6c return the pColl
1ffd9 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 64 pointer to be d
1ffda 65 6c 65 74 65 64 20 28 62 65 63 61 75 73 65 20 eleted (because
1ffdb 69 74 20 77 61 73 6e 27 74 20 61 64 64 65 64 0a it wasn't added.
1ffdc 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 ** to the
1ffdd 68 61 73 68 20 74 61 62 6c 65 29 2e 0a 20 20 20 hash table)..
1ffde 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 */. asse
1ffdf 72 74 28 20 70 44 65 6c 3d 3d 30 20 7c 7c 20 70 rt( pDel==0 || p
1ffe0 44 65 6c 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 Del==pColl );.
1ffe1 20 20 20 20 69 66 28 20 70 44 65 6c 21 3d 30 20 if( pDel!=0
1ffe2 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d ){. db->m
1ffe3 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1ffe4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1ffe5 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 6c 29 DbFree(db, pDel)
1ffe6 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 ;. pColl
1ffe7 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
1ffe8 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1ffe9 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pColl;.}../*.**
1ffea 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 Parameter zName
1ffeb 70 6f 69 6e 74 73 20 74 6f 20 61 20 55 54 46 2d points to a UTF-
1ffec 38 20 65 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 8 encoded string
1ffed 20 6e 4e 61 6d 65 20 62 79 74 65 73 20 6c 6f 6e nName bytes lon
1ffee 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 g..** Return the
1ffef 20 43 6f 6c 6c 53 65 71 2a 20 70 6f 69 6e 74 65 CollSeq* pointe
1fff0 72 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61 74 r for the collat
1fff1 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d ion sequence nam
1fff2 65 64 20 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72 20 ed zName.** for
1fff3 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 27 65 6e the encoding 'en
1fff4 63 27 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 c' from the data
1fff5 62 61 73 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a 2a base 'db'..**.**
1fff6 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 73 70 If the entry sp
1fff7 65 63 69 66 69 65 64 20 69 73 20 6e 6f 74 20 66 ecified is not f
1fff8 6f 75 6e 64 20 61 6e 64 20 27 63 72 65 61 74 65 ound and 'create
1fff9 27 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 ' is true, then
1fffa 63 72 65 61 74 65 20 61 0a 2a 2a 20 6e 65 77 20 create a.** new
1fffb 65 6e 74 72 79 2e 20 20 4f 74 68 65 72 77 69 73 entry. Otherwis
1fffc 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a e return NULL..*
1fffd 2a 0a 2a 2a 20 41 20 73 65 70 61 72 61 74 65 20 *.** A separate
1fffe 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 function sqlite3
1ffff 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 LocateCollSeq()
20000 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f is a wrapper aro
20001 75 6e 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 und.** this rout
20002 69 6e 65 2e 20 20 73 71 6c 69 74 65 33 4c 6f 63 ine. sqlite3Loc
20003 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 6e 76 ateCollSeq() inv
20004 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 okes the collati
20005 6f 6e 20 66 61 63 74 6f 72 79 0a 2a 2a 20 69 66 on factory.** if
20006 20 6e 65 63 65 73 73 61 72 79 20 61 6e 64 20 67 necessary and g
20007 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72 6f enerates an erro
20008 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68 65 r message if the
20009 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
2000a 6e 63 65 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 nce.** cannot be
2000b 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 found..*/.SQLIT
2000c 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 E_PRIVATE CollSe
2000d 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f q *sqlite3FindCo
2000e 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65 33 llSeq(. sqlite3
2000f 20 2a 64 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a *db,. u8 enc,.
20010 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
20011 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 ame,. int nName
20012 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a 29 ,. int create.)
20013 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f {. CollSeq *pCo
20014 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 ll;. if( zName
20015 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 66 ){. pColl = f
20016 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28 indCollSeqEntry(
20017 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 db, zName, nName
20018 2c 20 63 72 65 61 74 65 29 3b 0a 20 20 7d 65 6c , create);. }el
20019 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 se{. pColl =
2001a 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 db->pDfltColl;.
2001b 20 7d 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c }. assert( SQL
2001c 49 54 45 5f 55 54 46 38 3d 3d 31 20 26 26 20 53 ITE_UTF8==1 && S
2001d 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3d 3d 32 QLITE_UTF16LE==2
2001e 20 26 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36 && SQLITE_UTF16
2001f 42 45 3d 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 BE==3 );. asser
20020 74 28 20 65 6e 63 3e 3d 53 51 4c 49 54 45 5f 55 t( enc>=SQLITE_U
20021 54 46 38 20 26 26 20 65 6e 63 3c 3d 53 51 4c 49 TF8 && enc<=SQLI
20022 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 TE_UTF16BE );.
20023 69 66 28 20 70 43 6f 6c 6c 20 29 20 70 43 6f 6c if( pColl ) pCol
20024 6c 20 2b 3d 20 65 6e 63 2d 31 3b 0a 20 20 72 65 l += enc-1;. re
20025 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f turn pColl;.}../
20026 2a 20 44 75 72 69 6e 67 20 74 68 65 20 73 65 61 * During the sea
20027 72 63 68 20 66 6f 72 20 74 68 65 20 62 65 73 74 rch for the best
20028 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 function defini
20029 74 69 6f 6e 2c 20 74 68 69 73 20 70 72 6f 63 65 tion, this proce
2002a 64 75 72 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 dure.** is calle
2002b 64 20 74 6f 20 74 65 73 74 20 68 6f 77 20 77 65 d to test how we
2002c 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 ll the function
2002d 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
2002e 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 rst argument.**
2002f 6d 61 74 63 68 65 73 20 74 68 65 20 72 65 71 75 matches the requ
20030 65 73 74 20 66 6f 72 20 61 20 66 75 6e 63 74 69 est for a functi
20031 6f 6e 20 77 69 74 68 20 6e 41 72 67 20 61 72 67 on with nArg arg
20032 75 6d 65 6e 74 73 20 69 6e 20 61 20 73 79 73 74 uments in a syst
20033 65 6d 0a 2a 2a 20 74 68 61 74 20 75 73 65 73 20 em.** that uses
20034 65 6e 63 6f 64 69 6e 67 20 65 6e 63 2e 20 54 68 encoding enc. Th
20035 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
20036 20 69 6e 64 69 63 61 74 65 73 20 68 6f 77 20 77 indicates how w
20037 65 6c 6c 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 ell the.** reque
20038 73 74 20 69 73 20 6d 61 74 63 68 65 64 2e 20 41 st is matched. A
20039 20 68 69 67 68 65 72 20 76 61 6c 75 65 20 69 6e higher value in
2003a 64 69 63 61 74 65 73 20 61 20 62 65 74 74 65 72 dicates a better
2003b 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 match..**.** Th
2003c 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 e returned value
2003d 20 69 73 20 61 6c 77 61 79 73 20 62 65 74 77 65 is always betwe
2003e 65 6e 20 31 20 61 6e 64 20 36 2c 20 61 73 20 66 en 1 and 6, as f
2003f 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 31 3a ollows:.**.** 1:
20040 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75 A variable argu
20041 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 ments function t
20042 68 61 74 20 70 72 65 66 65 72 73 20 55 54 46 2d hat prefers UTF-
20043 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36 0a 8 when a UTF-16.
20044 2a 2a 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 69 ** encoding i
20045 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 s requested, or
20046 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 32 vice versa..** 2
20047 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 : A variable arg
20048 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 uments function
20049 74 68 61 74 20 75 73 65 73 20 55 54 46 2d 31 36 that uses UTF-16
2004a 42 45 20 77 68 65 6e 20 55 54 46 2d 31 36 4c 45 BE when UTF-16LE
2004b 20 69 73 0a 2a 2a 20 20 20 20 72 65 71 75 65 73 is.** reques
2004c 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 ted, or vice ver
2004d 73 61 2e 0a 2a 2a 20 33 3a 20 41 20 76 61 72 69 sa..** 3: A vari
2004e 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 able arguments f
2004f 75 6e 63 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 unction using th
20050 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63 6f e same text enco
20051 64 69 6e 67 2e 0a 2a 2a 20 34 3a 20 41 20 66 75 ding..** 4: A fu
20052 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 nction with the
20053 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 exact number of
20054 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65 73 arguments reques
20055 74 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20 70 ted that.** p
20056 72 65 66 65 72 73 20 55 54 46 2d 38 20 77 68 65 refers UTF-8 whe
20057 6e 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 n a UTF-16 encod
20058 69 6e 67 20 69 73 20 72 65 71 75 65 73 74 65 64 ing is requested
20059 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e , or vice versa.
2005a 0a 2a 2a 20 35 3a 20 41 20 66 75 6e 63 74 69 6f .** 5: A functio
2005b 6e 20 77 69 74 68 20 74 68 65 20 65 78 61 63 74 n with the exact
2005c 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d number of argum
2005d 65 6e 74 73 20 72 65 71 75 65 73 74 65 64 20 74 ents requested t
2005e 68 61 74 0a 2a 2a 20 20 20 20 70 72 65 66 65 72 hat.** prefer
2005f 73 20 55 54 46 2d 31 36 4c 45 20 77 68 65 6e 20 s UTF-16LE when
20060 55 54 46 2d 31 36 42 45 20 69 73 20 72 65 71 75 UTF-16BE is requ
20061 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 ested, or vice v
20062 65 72 73 61 2e 0a 2a 2a 20 36 3a 20 41 6e 20 65 ersa..** 6: An e
20063 78 61 63 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a xact match..**.*
20064 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61 74 /.static int mat
20065 63 68 51 75 61 6c 69 74 79 28 46 75 6e 63 44 65 chQuality(FuncDe
20066 66 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 f *p, int nArg,
20067 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 6d u8 enc){. int m
20068 61 74 63 68 20 3d 20 30 3b 0a 20 20 69 66 28 20 atch = 0;. if(
20069 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 70 p->nArg==-1 || p
2006a 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20 ->nArg==nArg ||
2006b 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 nArg==-1 ){.
2006c 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 69 match = 1;. i
2006d 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 f( p->nArg==nArg
2006e 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a || nArg==-1 ){.
2006f 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b match = 4;
20070 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 . }. if( e
20071 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20 nc==p->iPrefEnc
20072 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b ){. match +
20073 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 = 2;. }. e
20074 6c 73 65 20 69 66 28 20 28 65 6e 63 3d 3d 53 51 lse if( (enc==SQ
20075 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 26 26 20 LITE_UTF16LE &&
20076 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c p->iPrefEnc==SQL
20077 49 54 45 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a ITE_UTF16BE) ||.
20078 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 6e (en
20079 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 c==SQLITE_UTF16B
2007a 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 E && p->iPrefEnc
2007b 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 ==SQLITE_UTF16LE
2007c 29 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 ) ){. match
2007d 20 2b 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d += 1;. }. }
2007e 0a 20 20 72 65 74 75 72 6e 20 6d 61 74 63 68 3b . return match;
2007f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 .}../*.** Search
20080 20 61 20 46 75 6e 63 44 65 66 48 61 73 68 20 66 a FuncDefHash f
20081 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 or a function wi
20082 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d th the given nam
20083 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 e. Return.** a
20084 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d pointer to the m
20085 61 74 63 68 69 6e 67 20 46 75 6e 63 44 65 66 20 atching FuncDef
20086 69 66 20 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69 if found, or 0 i
20087 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 f there is no ma
20088 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46 tch..*/.static F
20089 75 6e 63 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e uncDef *function
2008a 53 65 61 72 63 68 28 0a 20 20 46 75 6e 63 44 65 Search(. FuncDe
2008b 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f fHash *pHash, /
2008c 2a 20 48 61 73 68 20 74 61 62 6c 65 20 74 6f 20 * Hash table to
2008d 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20 search */. int
2008e 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h,
2008f 20 2f 2a 20 48 61 73 68 20 6f 66 20 74 68 65 20 /* Hash of the
20090 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 name */. const
20091 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 2f char *zFunc, /
20092 2a 20 4e 61 6d 65 20 6f 66 20 66 75 6e 63 74 69 * Name of functi
20093 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e on */. int nFun
20094 63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 c /*
20095 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
20096 69 6e 20 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20 in zFunc */.){.
20097 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 66 FuncDef *p;. f
20098 6f 72 28 70 3d 70 48 61 73 68 2d 3e 61 5b 68 5d or(p=pHash->a[h]
20099 3b 20 70 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29 ; p; p=p->pHash)
2009a 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
2009b 33 53 74 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61 3StrNICmp(p->zNa
2009c 6d 65 2c 20 7a 46 75 6e 63 2c 20 6e 46 75 6e 63 me, zFunc, nFunc
2009d 29 3d 3d 30 20 26 26 20 70 2d 3e 7a 4e 61 6d 65 )==0 && p->zName
2009e 5b 6e 46 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20 [nFunc]==0 ){.
2009f 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 return p;.
200a0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
200a1 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 0;.}../*.** Ins
200a2 65 72 74 20 61 20 6e 65 77 20 46 75 6e 63 44 65 ert a new FuncDe
200a3 66 20 69 6e 74 6f 20 61 20 46 75 6e 63 44 65 66 f into a FuncDef
200a4 48 61 73 68 20 68 61 73 68 20 74 61 62 6c 65 2e Hash hash table.
200a5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
200a6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 TE void sqlite3F
200a7 75 6e 63 44 65 66 49 6e 73 65 72 74 28 0a 20 20 uncDefInsert(.
200a8 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 FuncDefHash *pHa
200a9 73 68 2c 20 20 2f 2a 20 54 68 65 20 68 61 73 68 sh, /* The hash
200aa 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 table into whic
200ab 68 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20 h to insert */.
200ac 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 20 FuncDef *pDef
200ad 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e /* The fun
200ae 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e ction definition
200af 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 29 7b to insert */.){
200b0 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 4f 74 68 . FuncDef *pOth
200b1 65 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 er;. int nName
200b2 3d 20 73 74 72 6c 65 6e 28 70 44 65 66 2d 3e 7a = strlen(pDef->z
200b3 4e 61 6d 65 29 3b 0a 20 20 75 38 20 63 31 20 3d Name);. u8 c1 =
200b4 20 28 75 38 29 70 44 65 66 2d 3e 7a 4e 61 6d 65 (u8)pDef->zName
200b5 5b 30 5d 3b 0a 20 20 69 6e 74 20 68 20 3d 20 28 [0];. int h = (
200b6 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
200b7 77 65 72 5b 63 31 5d 20 2b 20 6e 4e 61 6d 65 29 wer[c1] + nName)
200b8 20 25 20 41 72 72 61 79 53 69 7a 65 28 70 48 61 % ArraySize(pHa
200b9 73 68 2d 3e 61 29 3b 0a 20 20 70 4f 74 68 65 72 sh->a);. pOther
200ba 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 = functionSearc
200bb 68 28 70 48 61 73 68 2c 20 68 2c 20 70 44 65 66 h(pHash, h, pDef
200bc 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b ->zName, nName);
200bd 0a 20 20 69 66 28 20 70 4f 74 68 65 72 20 29 7b . if( pOther ){
200be 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78 74 . pDef->pNext
200bf 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 = pOther->pNext
200c0 3b 0a 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e ;. pOther->pN
200c1 65 78 74 20 3d 20 70 44 65 66 3b 0a 20 20 7d 65 ext = pDef;. }e
200c2 6c 73 65 7b 0a 20 20 20 20 70 44 65 66 2d 3e 70 lse{. pDef->p
200c3 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 44 Next = 0;. pD
200c4 65 66 2d 3e 70 48 61 73 68 20 3d 20 70 48 61 73 ef->pHash = pHas
200c5 68 2d 3e 61 5b 68 5d 3b 0a 20 20 20 20 70 48 61 h->a[h];. pHa
200c6 73 68 2d 3e 61 5b 68 5d 20 3d 20 70 44 65 66 3b sh->a[h] = pDef;
200c7 0a 20 20 7d 0a 7d 0a 20 20 0a 20 20 0a 0a 2f 2a . }.}. . ../*
200c8 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20 75 73 65 .** Locate a use
200c9 72 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e r function given
200ca 20 61 20 6e 61 6d 65 2c 20 61 20 6e 75 6d 62 65 a name, a numbe
200cb 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 r of arguments a
200cc 6e 64 20 61 20 66 6c 61 67 0a 2a 2a 20 69 6e 64 nd a flag.** ind
200cd 69 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 icating whether
200ce 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 the function pre
200cf 66 65 72 73 20 55 54 46 2d 31 36 20 6f 76 65 72 fers UTF-16 over
200d0 20 55 54 46 2d 38 2e 20 20 52 65 74 75 72 6e 20 UTF-8. Return
200d1 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 a.** pointer to
200d2 74 68 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 the FuncDef stru
200d3 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e cture that defin
200d4 65 73 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e es that function
200d5 2c 20 6f 72 20 72 65 74 75 72 6e 0a 2a 2a 20 4e , or return.** N
200d6 55 4c 4c 20 69 66 20 74 68 65 20 66 75 6e 63 74 ULL if the funct
200d7 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 ion does not exi
200d8 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 st..**.** If the
200d9 20 63 72 65 61 74 65 46 6c 61 67 20 61 72 67 75 createFlag argu
200da 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 ment is true, th
200db 65 6e 20 61 20 6e 65 77 20 28 62 6c 61 6e 6b 29 en a new (blank)
200dc 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 FuncDef.** stru
200dd 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 cture is created
200de 20 61 6e 64 20 6c 69 6b 65 64 20 69 6e 74 6f 20 and liked into
200df 74 68 65 20 22 64 62 22 20 73 74 72 75 63 74 75 the "db" structu
200e0 72 65 20 69 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61 re if a.** no ma
200e1 74 63 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 tching function
200e2 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74 previously exist
200e3 65 64 2e 20 20 57 68 65 6e 20 63 72 65 61 74 65 ed. When create
200e4 46 6c 61 67 20 69 73 20 74 72 75 65 0a 2a 2a 20 Flag is true.**
200e5 61 6e 64 20 74 68 65 20 6e 41 72 67 20 70 61 72 and the nArg par
200e6 61 6d 65 74 65 72 20 69 73 20 2d 31 2c 20 74 68 ameter is -1, th
200e7 65 6e 20 6f 6e 6c 79 20 61 20 66 75 6e 63 74 69 en only a functi
200e8 6f 6e 20 74 68 61 74 20 61 63 63 65 70 74 73 0a on that accepts.
200e9 2a 2a 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 ** any number of
200ea 20 61 72 67 75 6d 65 6e 74 73 20 77 69 6c 6c 20 arguments will
200eb 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a be returned..**.
200ec 2a 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 ** If createFlag
200ed 20 69 73 20 66 61 6c 73 65 20 61 6e 64 20 6e 41 is false and nA
200ee 72 67 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 74 rg is -1, then t
200ef 68 65 20 66 69 72 73 74 20 76 61 6c 69 64 0a 2a he first valid.*
200f0 2a 20 66 75 6e 63 74 69 6f 6e 20 66 6f 75 6e 64 * function found
200f1 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 41 is returned. A
200f2 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 76 61 6c function is val
200f3 69 64 20 69 66 20 65 69 74 68 65 72 20 78 46 75 id if either xFu
200f4 6e 63 0a 2a 2a 20 6f 72 20 78 53 74 65 70 20 69 nc.** or xStep i
200f5 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a s non-zero..**.*
200f6 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 * If createFlag
200f7 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 is false, then a
200f8 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 function with t
200f9 68 65 20 72 65 71 75 69 72 65 64 20 6e 61 6d 65 he required name
200fa 20 61 6e 64 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f and.** number o
200fb 66 20 61 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 f arguments may
200fc 62 65 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e be returned even
200fd 20 69 66 20 74 68 65 20 65 54 65 78 74 52 65 70 if the eTextRep
200fe 20 66 6c 61 67 20 64 6f 65 73 20 6e 6f 74 0a 2a flag does not.*
200ff 2a 20 6d 61 74 63 68 20 74 68 61 74 20 72 65 71 * match that req
20100 75 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 uested..*/.SQLIT
20101 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 E_PRIVATE FuncDe
20102 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75 f *sqlite3FindFu
20103 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 nction(. sqlite
20104 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 3 *db, /*
20105 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 An open database
20106 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
20107 20 2a 7a 4e 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 *zName, /* Name
20108 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
20109 2e 20 20 4e 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d . Not null-term
2010a 69 6e 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 inated */. int
2010b 6e 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f nName, /
2010c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 * Number of char
2010d 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 6e 61 acters in the na
2010e 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 me */. int nArg
2010f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 , /* Nu
20110 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
20111 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 61 6e 79 s. -1 means any
20112 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 number */. u8
20113 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 enc,
20114 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 78 /* Preferred tex
20115 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 t encoding */.
20116 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 20 20 int createFlag
20117 20 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 /* Create new
20118 20 65 6e 74 72 79 20 69 66 20 74 72 75 65 20 61 entry if true a
20119 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6f 74 68 65 nd does not othe
2011a 72 77 69 73 65 20 65 78 69 73 74 20 2a 2f 0a 29 rwise exist */.)
2011b 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 20 {. FuncDef *p;
2011c 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 /* Itera
2011d 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a tor variable */.
2011e 20 20 46 75 6e 63 44 65 66 20 2a 70 42 65 73 74 FuncDef *pBest
2011f 20 3d 20 30 3b 20 2f 2a 20 42 65 73 74 20 6d 61 = 0; /* Best ma
20120 74 63 68 20 66 6f 75 6e 64 20 73 6f 20 66 61 72 tch found so far
20121 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73 74 53 63 */. int bestSc
20122 6f 72 65 20 3d 20 30 3b 20 20 2f 2a 20 53 63 6f ore = 0; /* Sco
20123 72 65 20 6f 66 20 62 65 73 74 20 6d 61 74 63 68 re of best match
20124 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 */. int h;
20125 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
20126 68 20 76 61 6c 75 65 20 2a 2f 0a 0a 0a 20 20 61 h value */... a
20127 73 73 65 72 74 28 20 65 6e 63 3d 3d 53 51 4c 49 ssert( enc==SQLI
20128 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d TE_UTF8 || enc==
20129 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c SQLITE_UTF16LE |
2012a 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 | enc==SQLITE_UT
2012b 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20 6e F16BE );. if( n
2012c 41 72 67 3c 2d 31 20 29 20 6e 41 72 67 20 3d 20 Arg<-1 ) nArg =
2012d 2d 31 3b 0a 20 20 68 20 3d 20 28 73 71 6c 69 74 -1;. h = (sqlit
2012e 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 e3UpperToLower[(
2012f 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20 6e u8)zName[0]] + n
20130 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69 7a Name) % ArraySiz
20131 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 0a e(db->aFunc.a);.
20132 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 . /* First sear
20133 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68 20 61 ch for a match a
20134 6d 6f 6e 67 73 74 20 74 68 65 20 61 70 70 6c 69 mongst the appli
20135 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
20136 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 unctions.. */.
20137 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 p = functionSea
20138 72 63 68 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 rch(&db->aFunc,
20139 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 h, zName, nName)
2013a 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a ;. while( p ){.
2013b 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 int score =
2013c 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c 20 matchQuality(p,
2013d 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 nArg, enc);.
2013e 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53 63 if( score>bestSc
2013f 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70 42 65 ore ){. pBe
20140 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 62 65 st = p;. be
20141 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b stScore = score;
20142 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 . }. p = p
20143 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 ->pNext;. }..
20144 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 /* If no match i
20145 73 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 s found, search
20146 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e the built-in fun
20147 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a ctions.. **. *
20148 2a 20 45 78 63 65 70 74 2c 20 69 66 20 63 72 65 * Except, if cre
20149 61 74 65 46 6c 61 67 20 69 73 20 74 72 75 65 2c ateFlag is true,
2014a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
2014b 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 we are trying t
2014c 6f 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 o. ** install a
2014d 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20 new function.
2014e 57 68 61 74 65 76 65 72 20 46 75 6e 63 44 65 66 Whatever FuncDef
2014f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 structure is re
20150 74 75 72 6e 65 64 20 77 69 6c 6c 0a 20 20 2a 2a turned will. **
20151 20 68 61 76 65 20 66 69 65 6c 64 73 20 6f 76 65 have fields ove
20152 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 6e 65 rwritten with ne
20153 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 w information ap
20154 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 propriate for th
20155 65 0a 20 20 2a 2a 20 6e 65 77 20 66 75 6e 63 74 e. ** new funct
20156 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20 46 75 ion. But the Fu
20157 6e 63 44 65 66 73 20 66 6f 72 20 62 75 69 6c 74 ncDefs for built
20158 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 -in functions ar
20159 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20 2a e read-only.. *
2015a 2a 20 53 6f 20 77 65 20 6d 75 73 74 20 6e 6f 74 * So we must not
2015b 20 73 65 61 72 63 68 20 66 6f 72 20 62 75 69 6c search for buil
2015c 74 2d 69 6e 73 20 77 68 65 6e 20 63 72 65 61 74 t-ins when creat
2015d 69 6e 67 20 61 20 6e 65 77 20 66 75 6e 63 74 69 ing a new functi
2015e 6f 6e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 on.. */ . if(
2015f 21 63 72 65 61 74 65 46 6c 61 67 20 26 26 20 21 !createFlag && !
20160 70 42 65 73 74 20 29 7b 0a 20 20 20 20 46 75 6e pBest ){. Fun
20161 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20 cDefHash *pHash
20162 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 = &GLOBAL(FuncDe
20163 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c fHash, sqlite3Gl
20164 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a obalFunctions);.
20165 20 20 20 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e p = function
20166 53 65 61 72 63 68 28 70 48 61 73 68 2c 20 68 2c Search(pHash, h,
20167 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a zName, nName);.
20168 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a while( p ){.
20169 20 20 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20 int score
2016a 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70 = matchQuality(p
2016b 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 , nArg, enc);.
2016c 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e 62 65 if( score>be
2016d 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 stScore ){.
2016e 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20 20 pBest = p;.
2016f 20 20 20 20 20 20 62 65 73 74 53 63 6f 72 65 20 bestScore
20170 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 20 20 7d = score;. }
20171 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e . p = p->pN
20172 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ext;. }. }..
20173 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72 65 61 /* If the crea
20174 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72 teFlag parameter
20175 20 69 73 20 74 72 75 65 20 61 6e 64 20 74 68 65 is true and the
20176 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74 20 search did not
20177 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a 20 65 reveal an. ** e
20178 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20 74 xact match for t
20179 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65 72 20 he name, number
2017a 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 of arguments and
2017b 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 encoding, then
2017c 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77 20 65 add a. ** new e
2017d 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61 73 68 ntry to the hash
2017e 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74 75 72 table and retur
2017f 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 n it.. */. if(
20180 20 63 72 65 61 74 65 46 6c 61 67 20 26 26 20 28 createFlag && (
20181 62 65 73 74 53 63 6f 72 65 3c 36 20 7c 7c 20 70 bestScore<6 || p
20182 42 65 73 74 2d 3e 6e 41 72 67 21 3d 6e 41 72 67 Best->nArg!=nArg
20183 29 20 26 26 20 0a 20 20 20 20 20 20 28 70 42 65 ) && . (pBe
20184 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 st = sqlite3DbMa
20185 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
20186 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d eof(*pBest)+nNam
20187 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 e+1))!=0 ){.
20188 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20 28 pBest->zName = (
20189 63 68 61 72 20 2a 29 26 70 42 65 73 74 5b 31 5d char *)&pBest[1]
2018a 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 6e 41 72 ;. pBest->nAr
2018b 67 20 3d 20 6e 41 72 67 3b 0a 20 20 20 20 70 42 g = nArg;. pB
2018c 65 73 74 2d 3e 69 50 72 65 66 45 6e 63 20 3d 20 est->iPrefEnc =
2018d 65 6e 63 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 enc;. memcpy(
2018e 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e pBest->zName, zN
2018f 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 ame, nName);.
20190 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 5b 6e 4e pBest->zName[nN
20191 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 ame] = 0;. sq
20192 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 lite3FuncDefInse
20193 72 74 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 70 rt(&db->aFunc, p
20194 42 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 Best);. }.. if
20195 28 20 70 42 65 73 74 20 26 26 20 28 70 42 65 73 ( pBest && (pBes
20196 74 2d 3e 78 53 74 65 70 20 7c 7c 20 70 42 65 73 t->xStep || pBes
20197 74 2d 3e 78 46 75 6e 63 20 7c 7c 20 63 72 65 61 t->xFunc || crea
20198 74 65 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 72 teFlag) ){. r
20199 65 74 75 72 6e 20 70 42 65 73 74 3b 0a 20 20 7d eturn pBest;. }
2019a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
2019b 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 72 /*.** Free all r
2019c 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20 62 79 esources held by
2019d 20 74 68 65 20 73 63 68 65 6d 61 20 73 74 72 75 the schema stru
2019e 63 74 75 72 65 2e 20 54 68 65 20 76 6f 69 64 2a cture. The void*
2019f 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 argument points
201a0 0a 2a 2a 20 61 74 20 61 20 53 63 68 65 6d 61 20 .** at a Schema
201a1 73 74 72 75 63 74 2e 20 54 68 69 73 20 66 75 6e struct. This fun
201a2 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 63 ction does not c
201a3 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 all sqlite3DbFre
201a4 65 28 64 62 2c 20 29 20 6f 6e 20 74 68 65 20 0a e(db, ) on the .
201a5 2a 2a 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c ** pointer itsel
201a6 66 2c 20 69 74 20 6a 75 73 74 20 63 6c 65 61 6e f, it just clean
201a7 73 20 75 70 20 73 75 62 73 69 64 75 61 72 79 20 s up subsiduary
201a8 72 65 73 6f 75 72 63 65 73 20 28 69 2e 65 2e 20 resources (i.e.
201a9 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 the contents.**
201aa 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 of the schema ha
201ab 73 68 20 74 61 62 6c 65 73 29 2e 0a 2a 2a 0a 2a sh tables)..**.*
201ac 2a 20 54 68 65 20 53 63 68 65 6d 61 2e 63 61 63 * The Schema.cac
201ad 68 65 5f 73 69 7a 65 20 76 61 72 69 61 62 6c 65 he_size variable
201ae 20 69 73 20 6e 6f 74 20 63 6c 65 61 72 65 64 2e is not cleared.
201af 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
201b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
201b1 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a chemaFree(void *
201b2 70 29 7b 0a 20 20 48 61 73 68 20 74 65 6d 70 31 p){. Hash temp1
201b3 3b 0a 20 20 48 61 73 68 20 74 65 6d 70 32 3b 0a ;. Hash temp2;.
201b4 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 HashElem *pEle
201b5 6d 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 m;. Schema *pSc
201b6 68 65 6d 61 20 3d 20 28 53 63 68 65 6d 61 20 2a hema = (Schema *
201b7 29 70 3b 0a 0a 20 20 74 65 6d 70 31 20 3d 20 70 )p;.. temp1 = p
201b8 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b Schema->tblHash;
201b9 0a 20 20 74 65 6d 70 32 20 3d 20 70 53 63 68 65 . temp2 = pSche
201ba 6d 61 2d 3e 74 72 69 67 48 61 73 68 3b 0a 20 20 ma->trigHash;.
201bb 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 sqlite3HashInit(
201bc 26 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 &pSchema->trigHa
201bd 73 68 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 sh, 0);. sqlite
201be 33 48 61 73 68 43 6c 65 61 72 28 26 70 53 63 68 3HashClear(&pSch
201bf 65 6d 61 2d 3e 61 46 4b 65 79 29 3b 0a 20 20 73 ema->aFKey);. s
201c0 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 qlite3HashClear(
201c1 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 &pSchema->idxHas
201c2 68 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d h);. for(pElem=
201c3 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 sqliteHashFirst(
201c4 26 74 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 &temp2); pElem;
201c5 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 pElem=sqliteHash
201c6 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 Next(pElem)){.
201c7 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 sqlite3DeleteT
201c8 72 69 67 67 65 72 28 30 2c 20 28 54 72 69 67 67 rigger(0, (Trigg
201c9 65 72 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 er*)sqliteHashDa
201ca 74 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a ta(pElem));. }.
201cb 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 sqlite3HashCle
201cc 61 72 28 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 ar(&temp2);. sq
201cd 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 lite3HashInit(&p
201ce 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c Schema->tblHash,
201cf 20 30 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 0);. for(pElem
201d0 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 =sqliteHashFirst
201d1 28 26 74 65 6d 70 31 29 3b 20 70 45 6c 65 6d 3b (&temp1); pElem;
201d2 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 pElem=sqliteHas
201d3 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 hNext(pElem)){.
201d4 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d Table *pTab =
201d5 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 sqliteHashData(
201d6 70 45 6c 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 pElem);. sqli
201d7 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 te3DeleteTable(p
201d8 54 61 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Tab);. }. sqli
201d9 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 74 65 te3HashClear(&te
201da 6d 70 31 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d mp1);. pSchema-
201db 3e 70 53 65 71 54 61 62 20 3d 20 30 3b 0a 20 20 >pSeqTab = 0;.
201dc 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 pSchema->flags &
201dd 3d 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 = ~DB_SchemaLoad
201de 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e ed;.}../*.** Fin
201df 64 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 d and return the
201e0 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 schema associat
201e1 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65 2e ed with a BTree.
201e2 20 20 43 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 Create.** a ne
201e3 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61 w one if necessa
201e4 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ry..*/.SQLITE_PR
201e5 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 IVATE Schema *sq
201e6 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 73 lite3SchemaGet(s
201e7 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 72 65 qlite3 *db, Btre
201e8 65 20 2a 70 42 74 29 7b 0a 20 20 53 63 68 65 6d e *pBt){. Schem
201e9 61 20 2a 20 70 3b 0a 20 20 69 66 28 20 70 42 74 a * p;. if( pBt
201ea 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 53 63 68 ){. p = (Sch
201eb 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 42 74 72 ema *)sqlite3Btr
201ec 65 65 53 63 68 65 6d 61 28 70 42 74 2c 20 73 69 eeSchema(pBt, si
201ed 7a 65 6f 66 28 53 63 68 65 6d 61 29 2c 20 73 71 zeof(Schema), sq
201ee 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 29 lite3SchemaFree)
201ef 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
201f0 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c = (Schema *)sql
201f1 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 ite3MallocZero(s
201f2 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a izeof(Schema));.
201f3 20 20 7d 0a 20 20 69 66 28 20 21 70 20 29 7b 0a }. if( !p ){.
201f4 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
201f5 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 iled = 1;. }els
201f6 65 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c e if ( 0==p->fil
201f7 65 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 e_format ){.
201f8 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 sqlite3HashInit(
201f9 26 70 2d 3e 74 62 6c 48 61 73 68 2c 20 30 29 3b &p->tblHash, 0);
201fa 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 . sqlite3Hash
201fb 49 6e 69 74 28 26 70 2d 3e 69 64 78 48 61 73 68 Init(&p->idxHash
201fc 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 0);. sqlite
201fd 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 74 72 3HashInit(&p->tr
201fe 69 67 48 61 73 68 2c 20 30 29 3b 0a 20 20 20 20 igHash, 0);.
201ff 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 sqlite3HashInit(
20200 26 70 2d 3e 61 46 4b 65 79 2c 20 31 29 3b 0a 20 &p->aFKey, 1);.
20201 20 20 20 70 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 p->enc = SQLI
20202 54 45 5f 55 54 46 38 3b 0a 20 20 7d 0a 20 20 72 TE_UTF8;. }. r
20203 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a eturn p;.}../***
20204 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
20205 6f 66 20 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a of callback.c **
20206 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20207 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20208 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
20209 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
2020a 6e 20 66 69 6c 65 20 64 65 6c 65 74 65 2e 63 20 n file delete.c
2020b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2020c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2020d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
2020e 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
2020f 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
20210 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
20211 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
20212 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
20213 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
20214 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
20215 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
20216 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
20217 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
20218 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
20219 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
2021a 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
2021b 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
2021c 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
2021d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
2021e 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
2021f 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
20220 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
20221 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20223 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20225 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
20226 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 contains C code
20227 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 routines that ar
20228 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 e called by the
20229 70 61 72 73 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 parser.** in ord
2022a 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63 er to generate c
2022b 6f 64 65 20 66 6f 72 20 44 45 4c 45 54 45 20 46 ode for DELETE F
2022c 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a ROM statements..
2022d 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 65 6c 65 74 **.** $Id: delet
2022e 65 2e 63 2c 76 20 31 2e 31 38 36 20 32 30 30 38 e.c,v 1.186 2008
2022f 2f 31 30 2f 33 31 20 31 30 3a 35 33 3a 32 33 20 /10/31 10:53:23
20230 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
20231 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b $.*/../*.** Look
20232 20 75 70 20 65 76 65 72 79 20 74 61 62 6c 65 20 up every table
20233 74 68 61 74 20 69 73 20 6e 61 6d 65 64 20 69 6e that is named in
20234 20 70 53 72 63 2e 20 20 49 66 20 61 6e 79 20 74 pSrc. If any t
20235 61 62 6c 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e able is not foun
20236 64 2c 0a 2a 2a 20 61 64 64 20 61 6e 20 65 72 72 d,.** add an err
20237 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 or message to pP
20238 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e arse->zErrMsg an
20239 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 20 d return NULL.
2023a 49 66 20 61 6c 6c 20 74 61 62 6c 65 73 0a 2a 2a If all tables.**
2023b 20 61 72 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 are found, retu
2023c 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
2023d 74 68 65 20 6c 61 73 74 20 74 61 62 6c 65 2e 0a the last table..
2023e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
2023f 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 E Table *sqlite3
20240 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 SrcListLookup(Pa
20241 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 rse *pParse, Src
20242 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 73 List *pSrc){. s
20243 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
20244 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 em *pItem = pSrc
20245 2d 3e 61 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 ->a;. Table *pT
20246 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 ab;. assert( pI
20247 74 65 6d 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 tem && pSrc->nSr
20248 63 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62 20 3d c==1 );. pTab =
20249 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 sqlite3LocateTa
2024a 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 ble(pParse, 0, p
2024b 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 Item->zName, pIt
2024c 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a em->zDatabase);.
2024d 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 sqlite3DeleteT
2024e 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 54 61 62 able(pItem->pTab
2024f 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 );. pItem->pTab
20250 20 3d 20 70 54 61 62 3b 0a 20 20 69 66 28 20 70 = pTab;. if( p
20251 54 61 62 20 29 7b 0a 20 20 20 20 70 54 61 62 2d Tab ){. pTab-
20252 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 69 >nRef++;. }. i
20253 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 f( sqlite3Indexe
20254 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 dByLookup(pParse
20255 2c 20 70 49 74 65 6d 29 20 29 7b 0a 20 20 20 20 , pItem) ){.
20256 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 pTab = 0;. }.
20257 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a return pTab;.}..
20258 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d /*.** Check to m
20259 61 6b 65 20 73 75 72 65 20 74 68 65 20 67 69 76 ake sure the giv
2025a 65 6e 20 74 61 62 6c 65 20 69 73 20 77 72 69 74 en table is writ
2025b 61 62 6c 65 2e 20 20 49 66 20 69 74 20 69 73 20 able. If it is
2025c 6e 6f 74 0a 2a 2a 20 77 72 69 74 61 62 6c 65 2c not.** writable,
2025d 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 generate an err
2025e 6f 72 20 6d 65 73 73 61 67 65 20 61 6e 64 20 72 or message and r
2025f 65 74 75 72 6e 20 31 2e 20 20 49 66 20 69 74 20 eturn 1. If it
20260 69 73 0a 2a 2a 20 77 72 69 74 61 62 6c 65 20 72 is.** writable r
20261 65 74 75 72 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49 eturn 0;.*/.SQLI
20262 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
20263 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 qlite3IsReadOnly
20264 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
20265 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 Table *pTab, int
20266 20 76 69 65 77 4f 6b 29 7b 0a 20 20 69 66 28 20 viewOk){. if(
20267 28 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 ((pTab->tabFlags
20268 20 26 20 54 46 5f 52 65 61 64 6f 6e 6c 79 29 21 & TF_Readonly)!
20269 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 28 70 =0. && (p
2026a 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 Parse->db->flags
2026b 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 & SQLITE_WriteS
2026c 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 chema)==0.
2026d 20 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 && pParse->nes
2026e 74 65 64 3d 3d 30 29 20 0a 23 69 66 6e 64 65 66 ted==0) .#ifndef
2026f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
20270 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 TUALTABLE.
20271 7c 7c 20 28 70 54 61 62 2d 3e 70 4d 6f 64 20 26 || (pTab->pMod &
20272 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d & pTab->pMod->pM
20273 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d odule->xUpdate==
20274 30 29 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 0).#endif. ){.
20275 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
20276 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c sg(pParse, "tabl
20277 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 e %s may not be
20278 6d 6f 64 69 66 69 65 64 22 2c 20 70 54 61 62 2d modified", pTab-
20279 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 >zName);. ret
2027a 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 69 66 6e 64 urn 1;. }.#ifnd
2027b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
2027c 49 45 57 0a 20 20 69 66 28 20 21 76 69 65 77 4f IEW. if( !viewO
2027d 6b 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 k && pTab->pSele
2027e 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ct ){. sqlite
2027f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
20280 2c 22 63 61 6e 6e 6f 74 20 6d 6f 64 69 66 79 20 ,"cannot modify
20281 25 73 20 62 65 63 61 75 73 65 20 69 74 20 69 73 %s because it is
20282 20 61 20 76 69 65 77 22 2c 70 54 61 62 2d 3e 7a a view",pTab->z
20283 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Name);. retur
20284 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a n 1;. }.#endif.
20285 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
20286 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
20287 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 de that will ope
20288 6e 20 61 20 74 61 62 6c 65 20 66 6f 72 20 72 65 n a table for re
20289 61 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ading..*/.SQLITE
2028a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
2028b 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a lite3OpenTable(.
2028c 20 20 50 61 72 73 65 20 2a 70 2c 20 20 20 20 20 Parse *p,
2028d 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f /* Generate co
2028e 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 de into this VDB
2028f 45 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c E */. int iCur,
20290 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 /* The cu
20291 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 rsor number of t
20292 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e he table */. in
20293 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 2f 2a t iDb, /*
20294 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e The database in
20295 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 dex in sqlite3.a
20296 44 62 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 Db[] */. Table
20297 2a 70 54 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 *pTab, /* The
20298 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 table to be ope
20299 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 ned */. int opc
2029a 6f 64 65 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f ode /* OP_O
2029b 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 penRead or OP_Op
2029c 65 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 enWrite */.){.
2029d 56 64 62 65 20 2a 76 3b 0a 20 20 69 66 28 20 49 Vdbe *v;. if( I
2029e 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 sVirtual(pTab) )
2029f 20 72 65 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73 return;. v = s
202a0 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29 qlite3GetVdbe(p)
202a1 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f ;. assert( opco
202a2 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 de==OP_OpenWrite
202a3 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f || opcode==OP_O
202a4 70 65 6e 52 65 61 64 20 29 3b 0a 20 20 73 71 6c penRead );. sql
202a5 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c ite3TableLock(p,
202a6 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d iDb, pTab->tnum
202a7 2c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 , (opcode==OP_Op
202a8 65 6e 57 72 69 74 65 29 2c 20 70 54 61 62 2d 3e enWrite), pTab->
202a9 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 zName);. sqlite
202aa 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
202ab 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c P_SetNumColumns,
202ac 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0, pTab->nCol);
202ad 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
202ae 64 4f 70 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20 dOp3(v, opcode,
202af 69 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d iCur, pTab->tnum
202b0 2c 20 69 44 62 29 3b 0a 20 20 56 64 62 65 43 6f , iDb);. VdbeCo
202b1 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 mment((v, "%s",
202b2 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 7d pTab->zName));.}
202b3 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 ...#if !defined(
202b4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
202b5 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
202b6 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
202b7 52 29 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 R)./*.** Evaluat
202b8 65 20 61 20 76 69 65 77 20 61 6e 64 20 73 74 6f e a view and sto
202b9 72 65 20 69 74 73 20 72 65 73 75 6c 74 20 69 6e re its result in
202ba 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 an ephemeral ta
202bb 62 6c 65 2e 20 20 54 68 65 0a 2a 2a 20 70 57 68 ble. The.** pWh
202bc 65 72 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ere argument is
202bd 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 57 48 45 52 an optional WHER
202be 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65 E clause that re
202bf 73 74 72 69 63 74 73 20 74 68 65 0a 2a 2a 20 73 stricts the.** s
202c0 65 74 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 et of rows in th
202c1 65 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20 e view that are
202c2 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 to be added to t
202c3 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 he ephemeral tab
202c4 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
202c5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
202c6 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 e3MaterializeVie
202c7 77 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 w(. Parse *pPar
202c8 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 se, /* Par
202c9 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
202ca 20 20 54 61 62 6c 65 20 2a 70 56 69 65 77 2c 20 Table *pView,
202cb 20 20 20 20 20 20 20 2f 2a 20 56 69 65 77 20 64 /* View d
202cc 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 45 efinition */. E
202cd 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 xpr *pWhere,
202ce 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 /* Optional
202cf 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 WHERE clause to
202d0 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e be added */. in
202d1 74 20 69 43 75 72 20 20 20 20 20 20 20 20 20 20 t iCur
202d2 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d /* Cursor num
202d3 62 65 72 20 66 6f 72 20 65 70 68 65 6d 65 72 69 ber for ephemeri
202d4 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 al table */.){.
202d5 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 SelectDest dest
202d6 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 75 70 ;. Select *pDup
202d7 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
202d8 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 = pParse->db;..
202d9 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53 pDup = sqlite3S
202da 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 56 69 electDup(db, pVi
202db 65 77 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 ew->pSelect);.
202dc 69 66 28 20 70 57 68 65 72 65 20 29 7b 0a 20 20 if( pWhere ){.
202dd 20 20 53 72 63 4c 69 73 74 20 2a 70 46 72 6f 6d SrcList *pFrom
202de 3b 0a 20 20 20 20 54 6f 6b 65 6e 20 76 69 65 77 ;. Token view
202df 4e 61 6d 65 3b 0a 20 20 20 20 0a 20 20 20 20 70 Name;. . p
202e0 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 Where = sqlite3E
202e1 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 72 xprDup(db, pWher
202e2 65 29 3b 0a 20 20 20 20 76 69 65 77 4e 61 6d 65 e);. viewName
202e3 2e 7a 20 3d 20 28 75 38 2a 29 70 56 69 65 77 2d .z = (u8*)pView-
202e4 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 76 69 65 77 >zName;. view
202e5 4e 61 6d 65 2e 6e 20 3d 20 28 75 6e 73 69 67 6e Name.n = (unsign
202e6 65 64 20 69 6e 74 29 73 74 72 6c 65 6e 28 28 63 ed int)strlen((c
202e7 6f 6e 73 74 20 63 68 61 72 2a 29 76 69 65 77 4e onst char*)viewN
202e8 61 6d 65 2e 7a 29 3b 0a 20 20 20 20 70 46 72 6f ame.z);. pFro
202e9 6d 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 m = sqlite3SrcLi
202ea 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d stAppendFromTerm
202eb 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 (pParse, 0, 0, 0
202ec 2c 20 26 76 69 65 77 4e 61 6d 65 2c 20 70 44 75 , &viewName, pDu
202ed 70 2c 20 30 2c 30 29 3b 0a 20 20 20 20 70 44 75 p, 0,0);. pDu
202ee 70 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 p = sqlite3Selec
202ef 74 4e 65 77 28 70 50 61 72 73 65 2c 20 30 2c 20 tNew(pParse, 0,
202f0 70 46 72 6f 6d 2c 20 70 57 68 65 72 65 2c 20 30 pFrom, pWhere, 0
202f1 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 , 0, 0, 0, 0, 0)
202f2 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 ;. }. sqlite3S
202f3 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 electDestInit(&d
202f4 65 73 74 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 est, SRT_EphemTa
202f5 62 2c 20 69 43 75 72 29 3b 0a 20 20 73 71 6c 69 b, iCur);. sqli
202f6 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 te3Select(pParse
202f7 2c 20 70 44 75 70 2c 20 26 64 65 73 74 29 3b 0a , pDup, &dest);.
202f8 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
202f9 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b elete(db, pDup);
202fa 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 .}.#endif /* !de
202fb 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
202fc 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69 T_VIEW) && !defi
202fd 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
202fe 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 23 69 66 TRIGGER) */..#if
202ff 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
20300 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 ENABLE_UPDATE_DE
20301 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 LETE_LIMIT) && !
20302 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
20303 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a MIT_SUBQUERY)./*
20304 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 .** Generate an
20305 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 expression tree
20306 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
20307 20 57 48 45 52 45 2c 20 4f 52 44 45 52 20 42 59 WHERE, ORDER BY
20308 2c 0a 2a 2a 20 61 6e 64 20 4c 49 4d 49 54 2f 4f ,.** and LIMIT/O
20309 46 46 53 45 54 20 70 6f 72 74 69 6f 6e 20 6f 66 FFSET portion of
2030a 20 44 45 4c 45 54 45 20 61 6e 64 20 55 50 44 41 DELETE and UPDA
2030b 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a TE statements..*
2030c 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 *.** DELETE
2030d 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 FROM table_wxyz
2030e 57 48 45 52 45 20 61 3c 35 20 4f 52 44 45 52 20 WHERE a<5 ORDER
2030f 42 59 20 61 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a BY a LIMIT 1;.**
20310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20311 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f \___
20312 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f ________________
20313 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 _______/.**
20314 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20315 20 20 20 20 20 20 20 20 20 20 70 4c 69 6d 69 74 pLimit
20316 57 68 65 72 65 20 28 70 49 6e 43 6c 61 75 73 65 Where (pInClause
20317 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ).*/.SQLITE_PRIV
20318 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
20319 33 4c 69 6d 69 74 57 68 65 72 65 28 0a 20 20 50 3LimitWhere(. P
2031a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
2031b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
2031c 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 he parser contex
2031d 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a t */. SrcList *
2031e 70 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 pSrc,
2031f 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 /* the FROM
20320 63 6c 61 75 73 65 20 2d 2d 20 77 68 69 63 68 20 clause -- which
20321 74 61 62 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a tables to scan *
20322 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 /. Expr *pWhere
20323 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
20324 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c /* The WHERE cl
20325 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 ause. May be nu
20326 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 ll */. ExprList
20327 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 *pOrderBy,
20328 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 /* The ORDE
20329 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 R BY clause. Ma
2032a 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 y be null */. E
2032b 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 xpr *pLimit,
2032c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
2032d 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e he LIMIT clause.
2032e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f May be null */
2032f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 . Expr *pOffset
20330 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
20331 2f 2a 20 54 68 65 20 4f 46 46 53 45 54 20 63 6c /* The OFFSET cl
20332 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 ause. May be nu
20333 6c 6c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 ll */. char *zS
20334 74 6d 74 54 79 70 65 20 20 20 20 20 20 20 20 20 tmtType
20335 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 44 /* Either D
20336 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 2e ELETE or UPDATE.
20337 20 20 46 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 For error mess
20338 61 67 65 73 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78 ages. */.){. Ex
20339 70 72 20 2a 70 57 68 65 72 65 52 6f 77 69 64 20 pr *pWhereRowid
2033a 3d 20 4e 55 4c 4c 3b 20 20 20 20 2f 2a 20 57 48 = NULL; /* WH
2033b 45 52 45 20 72 6f 77 69 64 20 2e 2e 20 2a 2f 0a ERE rowid .. */.
2033c 20 20 45 78 70 72 20 2a 70 49 6e 43 6c 61 75 73 Expr *pInClaus
2033d 65 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f e = NULL; /
2033e 2a 20 57 48 45 52 45 20 72 6f 77 69 64 20 49 4e * WHERE rowid IN
2033f 20 28 20 73 65 6c 65 63 74 20 29 20 2a 2f 0a 20 ( select ) */.
20340 20 45 78 70 72 20 2a 70 53 65 6c 65 63 74 52 6f Expr *pSelectRo
20341 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a wid = NULL; /*
20342 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e SELECT rowid ..
20343 2e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 . */. ExprList
20344 2a 70 45 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20 *pEList = NULL;
20345 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f /* Expressio
20346 6e 20 6c 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67 n list contaning
20347 20 6f 6e 6c 79 20 70 53 65 6c 65 63 74 52 6f 77 only pSelectRow
20348 69 64 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 id */. SrcList
20349 2a 70 53 65 6c 65 63 74 53 72 63 20 3d 20 4e 55 *pSelectSrc = NU
2034a 4c 4c 3b 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 LL; /* SELECT r
2034b 6f 77 69 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20 owid FROM x ...
2034c 28 64 75 70 20 6f 66 20 70 53 72 63 29 20 2a 2f (dup of pSrc) */
2034d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
2034e 63 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 ct = NULL;
2034f 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45 /* Complete SELE
20350 43 54 20 74 72 65 65 20 2a 2f 0a 0a 20 20 2f 2a CT tree */.. /*
20351 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 Check that ther
20352 65 20 69 73 6e 27 74 20 61 6e 20 4f 52 44 45 52 e isn't an ORDER
20353 20 42 59 20 77 69 74 68 6f 75 74 20 61 20 4c 49 BY without a LI
20354 4d 49 54 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f MIT clause.. */
20355 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 . if( pOrderBy
20356 26 26 20 28 70 4c 69 6d 69 74 20 3d 3d 20 30 29 && (pLimit == 0)
20357 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) {. sqlite3
20358 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
20359 20 22 4f 52 44 45 52 20 42 59 20 77 69 74 68 6f "ORDER BY witho
2035a 75 74 20 4c 49 4d 49 54 20 6f 6e 20 25 73 22 2c ut LIMIT on %s",
2035b 20 7a 53 74 6d 74 54 79 70 65 29 3b 0a 20 20 20 zStmtType);.
2035c 20 70 50 61 72 73 65 2d 3e 70 61 72 73 65 45 72 pParse->parseEr
2035d 72 6f 72 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 ror = 1;. got
2035e 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c o limit_where_cl
2035f 65 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 eanup_2;. }..
20360 2f 2a 20 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20 /* We only need
20361 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 73 65 to generate a se
20362 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e 20 lect expression
20363 69 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 if there. ** is
20364 20 61 20 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20 a limit/offset
20365 74 65 72 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e term to enforce.
20366 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d . */. if( pLim
20367 69 74 20 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 it == 0 ) {.
20368 2f 2a 20 69 66 20 70 4c 69 6d 69 74 20 69 73 20 /* if pLimit is
20369 6e 75 6c 6c 2c 20 70 4f 66 66 73 65 74 20 77 69 null, pOffset wi
2036a 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 6e 75 6c ll always be nul
2036b 6c 20 61 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20 l as well. */.
2036c 20 20 61 73 73 65 72 74 28 20 70 4f 66 66 73 65 assert( pOffse
2036d 74 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 72 65 t == 0 );. re
2036e 74 75 72 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d turn pWhere;. }
2036f 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 .. /* Generate
20370 61 20 73 65 6c 65 63 74 20 65 78 70 72 65 73 73 a select express
20371 69 6f 6e 20 74 72 65 65 20 74 6f 20 65 6e 66 6f ion tree to enfo
20372 72 63 65 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66 rce the limit/of
20373 66 73 65 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20 fset . ** term
20374 66 6f 72 20 74 68 65 20 44 45 4c 45 54 45 20 6f for the DELETE o
20375 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 r UPDATE stateme
20376 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 nt. For example
20377 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54 45 20 :. ** DELETE
20378 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 FROM table_a WHE
20379 52 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 RE col1=1 ORDER
2037a 42 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 BY col2 LIMIT 1
2037b 4f 46 46 53 45 54 20 31 0a 20 20 2a 2a 20 62 65 OFFSET 1. ** be
2037c 63 6f 6d 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45 comes:. ** DE
2037d 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f LETE FROM table_
2037e 61 20 57 48 45 52 45 20 72 6f 77 69 64 20 49 4e a WHERE rowid IN
2037f 20 28 20 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c ( . ** SEL
20380 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 ECT rowid FROM t
20381 61 62 6c 65 5f 61 20 57 48 45 52 45 20 63 6f 6c able_a WHERE col
20382 31 3d 31 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 1=1 ORDER BY col
20383 32 20 4c 49 4d 49 54 20 31 20 4f 46 46 53 45 54 2 LIMIT 1 OFFSET
20384 20 31 0a 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a 1. ** );. *
20385 2f 0a 0a 20 20 70 53 65 6c 65 63 74 52 6f 77 69 /.. pSelectRowi
20386 64 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 d = sqlite3Expr(
20387 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52 pParse->db, TK_R
20388 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 OW, 0, 0, 0);.
20389 69 66 28 20 70 53 65 6c 65 63 74 52 6f 77 69 64 if( pSelectRowid
2038a 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d == 0 ) goto lim
2038b 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 it_where_cleanup
2038c 5f 32 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 _2;. pEList = s
2038d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 qlite3ExprListAp
2038e 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 pend(pParse, 0,
2038f 70 53 65 6c 65 63 74 52 6f 77 69 64 2c 20 30 29 pSelectRowid, 0)
20390 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 3d ;. if( pEList =
20391 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 = 0 ) goto limit
20392 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 _where_cleanup_2
20393 3b 0a 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61 74 ;.. /* duplicat
20394 65 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 e the FROM claus
20395 65 20 61 73 20 69 74 20 69 73 20 6e 65 65 64 65 e as it is neede
20396 64 20 62 79 20 62 6f 74 68 20 74 68 65 20 44 45 d by both the DE
20397 4c 45 54 45 2f 55 50 44 41 54 45 20 74 72 65 65 LETE/UPDATE tree
20398 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 . ** and the SE
20399 4c 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a 2f LECT subtree. */
2039a 0a 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d 20 . pSelectSrc =
2039b 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 sqlite3SrcListDu
2039c 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 p(pParse->db, pS
2039d 72 63 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 rc);. if( pSele
2039e 63 74 53 72 63 20 3d 3d 20 30 20 29 20 7b 0a 20 ctSrc == 0 ) {.
2039f 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 sqlite3ExprLi
203a0 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d stDelete(pParse-
203a1 3e 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 >db, pEList);.
203a2 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 goto limit_whe
203a3 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20 re_cleanup_2;.
203a4 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65 72 61 74 65 }.. /* generate
203a5 20 74 68 65 20 53 45 4c 45 43 54 20 65 78 70 72 the SELECT expr
203a6 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 2a 2f 0a ession tree. */.
203a7 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 pSelect = sqli
203a8 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 te3SelectNew(pPa
203a9 72 73 65 2c 70 45 4c 69 73 74 2c 70 53 65 6c 65 rse,pEList,pSele
203aa 63 74 53 72 63 2c 70 57 68 65 72 65 2c 30 2c 30 ctSrc,pWhere,0,0
203ab 2c 70 4f 72 64 65 72 42 79 2c 30 2c 70 4c 69 6d ,pOrderBy,0,pLim
203ac 69 74 2c 70 4f 66 66 73 65 74 29 3b 0a 20 20 69 it,pOffset);. i
203ad 66 28 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 20 f( pSelect == 0
203ae 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f ) return 0;.. /
203af 2a 20 6e 6f 77 20 67 65 6e 65 72 61 74 65 20 74 * now generate t
203b0 68 65 20 6e 65 77 20 57 48 45 52 45 20 72 6f 77 he new WHERE row
203b1 69 64 20 49 4e 20 63 6c 61 75 73 65 20 66 6f 72 id IN clause for
203b2 20 74 68 65 20 44 45 4c 45 54 45 2f 55 44 50 41 the DELETE/UDPA
203b3 54 45 20 2a 2f 0a 20 20 70 57 68 65 72 65 52 6f TE */. pWhereRo
203b4 77 69 64 20 3d 20 73 71 6c 69 74 65 33 45 78 70 wid = sqlite3Exp
203b5 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b r(pParse->db, TK
203b6 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a _ROW, 0, 0, 0);.
203b7 20 20 69 66 28 20 70 57 68 65 72 65 52 6f 77 69 if( pWhereRowi
203b8 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 d == 0 ) goto li
203b9 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 mit_where_cleanu
203ba 70 5f 31 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 p_1;. pInClause
203bb 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 = sqlite3PExpr(
203bc 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 pParse, TK_IN, p
203bd 57 68 65 72 65 52 6f 77 69 64 2c 20 30 2c 20 30 WhereRowid, 0, 0
203be 29 3b 0a 20 20 69 66 28 20 70 49 6e 43 6c 61 75 );. if( pInClau
203bf 73 65 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c se == 0 ) goto l
203c0 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e imit_where_clean
203c1 75 70 5f 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75 up_1;.. pInClau
203c2 73 65 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 se->pSelect = pS
203c3 65 6c 65 63 74 3b 0a 20 20 73 71 6c 69 74 65 33 elect;. sqlite3
203c4 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 ExprSetHeight(pP
203c5 61 72 73 65 2c 20 70 49 6e 43 6c 61 75 73 65 29 arse, pInClause)
203c6 3b 0a 20 20 72 65 74 75 72 6e 20 70 49 6e 43 6c ;. return pInCl
203c7 61 75 73 65 3b 0a 0a 20 20 2f 2a 20 73 6f 6d 65 ause;.. /* some
203c8 74 68 69 6e 67 20 77 65 6e 74 20 77 72 6f 6e 67 thing went wrong
203c9 2e 20 63 6c 65 61 6e 20 75 70 20 61 6e 79 74 68 . clean up anyth
203ca 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2e 20 2a ing allocated. *
203cb 2f 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c /.limit_where_cl
203cc 65 61 6e 75 70 5f 31 3a 0a 20 20 73 71 6c 69 74 eanup_1:. sqlit
203cd 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 e3SelectDelete(p
203ce 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c 65 Parse->db, pSele
203cf 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b ct);. return 0;
203d0 0a 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c ..limit_where_cl
203d1 65 61 6e 75 70 5f 32 3a 0a 20 20 73 71 6c 69 74 eanup_2:. sqlit
203d2 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 e3ExprDelete(pPa
203d3 72 73 65 2d 3e 64 62 2c 20 70 57 68 65 72 65 29 rse->db, pWhere)
203d4 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c ;. sqlite3ExprL
203d5 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 istDelete(pParse
203d6 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42 79 29 3b ->db, pOrderBy);
203d7 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 . sqlite3ExprDe
203d8 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c lete(pParse->db,
203d9 20 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69 pLimit);. sqli
203da 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 te3ExprDelete(pP
203db 61 72 73 65 2d 3e 64 62 2c 20 70 4f 66 66 73 65 arse->db, pOffse
203dc 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a t);. return 0;.
203dd 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 }.#endif /* defi
203de 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
203df 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f E_UPDATE_DELETE_
203e0 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e LIMIT) && !defin
203e1 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
203e2 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 2f 2a 0a UBQUERY) */../*.
203e3 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
203e4 20 66 6f 72 20 61 20 44 45 4c 45 54 45 20 46 52 for a DELETE FR
203e5 4f 4d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a OM statement..**
203e6 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 .** DELETE F
203e7 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 ROM table_wxyz W
203e8 48 45 52 45 20 61 3c 35 20 41 4e 44 20 62 20 4e HERE a<5 AND b N
203e9 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 OT NULL;.**
203ea 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f \___
203eb 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f _____/ \__
203ec 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a ______________/.
203ed 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
203ee 20 20 20 20 70 54 61 62 4c 69 73 74 20 20 20 20 pTabList
203ef 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 pWhere
203f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
203f1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
203f2 65 6c 65 74 65 46 72 6f 6d 28 0a 20 20 50 61 72 eleteFrom(. Par
203f3 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
203f4 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 /* The parse
203f5 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 r context */. S
203f6 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 rcList *pTabList
203f7 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 , /* The tab
203f8 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77 65 le from which we
203f9 20 73 68 6f 75 6c 64 20 64 65 6c 65 74 65 20 74 should delete t
203fa 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70 72 20 hings */. Expr
203fb 2a 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20 *pWhere
203fc 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 /* The WHERE c
203fd 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e lause. May be n
203fe 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 ull */.){. Vdbe
203ff 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 *v;
20400 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 /* The virtua
20401 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e l database engin
20402 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 e */. Table *pT
20403 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ab; /*
20404 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 The table from
20405 77 68 69 63 68 20 72 65 63 6f 72 64 73 20 77 69 which records wi
20406 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f ll be deleted */
20407 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
20408 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d Db; /* Nam
20409 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f e of database ho
2040a 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 lding pTab */.
2040b 69 6e 74 20 65 6e 64 2c 20 61 64 64 72 20 3d 20 int end, addr =
2040c 30 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 75 70 0; /* A coup
2040d 6c 65 20 61 64 64 72 65 73 73 65 73 20 6f 66 20 le addresses of
2040e 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a generated code *
2040f 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 /. int i;
20410 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
20411 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
20412 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 WhereInfo *pWInf
20413 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d o; /* Inform
20414 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
20415 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a WHERE clause */.
20416 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 Index *pIdx;
20417 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
20418 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 looping over ind
20419 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c ices of the tabl
2041a 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b e */. int iCur;
2041b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2041c 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d VDBE Cursor num
2041d 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a ber for pTab */.
2041e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
2041f 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e /* Main
20420 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 database struct
20421 75 72 65 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e ure */. AuthCon
20422 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20 20 text sContext;
20423 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e /* Authorization
20424 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e context */. in
20425 74 20 6f 6c 64 49 64 78 20 3d 20 2d 31 3b 20 20 t oldIdx = -1;
20426 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 /* Cursor f
20427 6f 72 20 74 68 65 20 4f 4c 44 20 74 61 62 6c 65 or the OLD table
20428 20 6f 66 20 41 46 54 45 52 20 74 72 69 67 67 65 of AFTER trigge
20429 72 73 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 rs */. NameCont
2042a 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 2f ext sNC; /
2042b 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 * Name context t
2042c 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 o resolve expres
2042d 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69 6e sions in */. in
2042e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 t iDb;
2042f 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
20430 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 number */. int
20431 20 6d 65 6d 43 6e 74 20 3d 20 2d 31 3b 20 20 20 memCnt = -1;
20432 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 /* Memory ce
20433 6c 6c 20 75 73 65 64 20 66 6f 72 20 63 68 61 6e ll used for chan
20434 67 65 20 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a 20 ge counting */.
20435 20 69 6e 74 20 72 63 61 75 74 68 3b 20 20 20 20 int rcauth;
20436 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
20437 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 75 74 returned by aut
20438 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 horization callb
20439 61 63 6b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 ack */..#ifndef
2043a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
2043b 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 GER. int isView
2043c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
2043d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74 /* True if at
2043e 74 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 6c 65 tempting to dele
2043f 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77 20 2a te from a view *
20440 2f 0a 20 20 69 6e 74 20 74 72 69 67 67 65 72 73 /. int triggers
20441 5f 65 78 69 73 74 20 3d 20 30 3b 20 20 20 20 20 _exist = 0;
20442 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 /* True if any
20443 74 72 69 67 67 65 72 73 20 65 78 69 73 74 20 2a triggers exist *
20444 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 /.#endif. int i
20445 42 65 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 BeginAfterTrigge
20446 72 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 r; /* Addre
20447 73 73 20 6f 66 20 61 66 74 65 72 20 74 72 69 67 ss of after trig
20448 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 ger program */.
20449 20 69 6e 74 20 69 45 6e 64 41 66 74 65 72 54 72 int iEndAfterTr
2044a 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a igger; /*
2044b 20 45 78 69 74 20 6f 66 20 61 66 74 65 72 20 74 Exit of after t
2044c 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a rigger program *
2044d 2f 0a 20 20 69 6e 74 20 69 42 65 67 69 6e 42 65 /. int iBeginBe
2044e 66 6f 72 65 54 72 69 67 67 65 72 3b 20 20 20 20 foreTrigger;
2044f 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 62 /* Address of b
20450 65 66 6f 72 65 20 74 72 69 67 67 65 72 20 70 72 efore trigger pr
20451 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 ogram */. int i
20452 45 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 EndBeforeTrigger
20453 3b 20 20 20 20 20 20 20 2f 2a 20 45 78 69 74 20 ; /* Exit
20454 6f 66 20 62 65 66 6f 72 65 20 74 72 69 67 67 65 of before trigge
20455 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75 r program */. u
20456 33 32 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 32 old_col_mask
20457 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d = 0; /* M
20458 61 73 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c ask of OLD.* col
20459 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a umns in use */..
2045a 20 20 73 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 sContext.pPars
2045b 65 20 3d 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 e = 0;. db = pP
2045c 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 arse->db;. if(
2045d 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 pParse->nErr ||
2045e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
2045f 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c ){. goto del
20460 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 ete_from_cleanup
20461 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
20462 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d pTabList->nSrc==
20463 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 1 );.. /* Locat
20464 65 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63 e the table whic
20465 68 20 77 65 20 77 61 6e 74 20 74 6f 20 64 65 6c h we want to del
20466 65 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c 65 ete. This table
20467 20 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 has to be. **
20468 70 75 74 20 69 6e 20 61 6e 20 53 72 63 4c 69 73 put in an SrcLis
20469 74 20 73 74 72 75 63 74 75 72 65 20 62 65 63 61 t structure beca
2046a 75 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 use some of the
2046b 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 0a 20 subroutines we.
2046c 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c ** will be call
2046d 69 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65 64 ing are designed
2046e 20 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d 75 to work with mu
2046f 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 6e ltiple tables an
20470 64 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e d expect. ** an
20471 20 53 72 63 4c 69 73 74 2a 20 70 61 72 61 6d 65 SrcList* parame
20472 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a ter instead of j
20473 75 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72 ust a Table* par
20474 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 ameter.. */. p
20475 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 Tab = sqlite3Src
20476 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 ListLookup(pPars
20477 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 e, pTabList);.
20478 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20 67 if( pTab==0 ) g
20479 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f oto delete_from_
2047a 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46 cleanup;.. /* F
2047b 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 igure out if we
2047c 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 have any trigger
2047d 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 s and if the tab
2047e 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65 le being. ** de
2047f 6c 65 74 65 64 20 66 72 6f 6d 20 69 73 20 61 20 leted from is a
20480 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 view. */.#ifnde
20481 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
20482 49 47 47 45 52 0a 20 20 74 72 69 67 67 65 72 73 IGGER. triggers
20483 5f 65 78 69 73 74 20 3d 20 73 71 6c 69 74 65 33 _exist = sqlite3
20484 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70 50 TriggersExist(pP
20485 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 44 arse, pTab, TK_D
20486 45 4c 45 54 45 2c 20 30 29 3b 0a 20 20 69 73 56 ELETE, 0);. isV
20487 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c iew = pTab->pSel
20488 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 ect!=0;.#else.#
20489 64 65 66 69 6e 65 20 74 72 69 67 67 65 72 73 5f define triggers_
2048a 65 78 69 73 74 20 30 0a 23 20 64 65 66 69 6e 65 exist 0.# define
2048b 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 isView 0.#endif
2048c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
2048d 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 MIT_VIEW.# undef
2048e 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65 isView.# define
2048f 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 isView 0.#endif
20490 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 .. if( sqlite3I
20491 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 sReadOnly(pParse
20492 2c 20 70 54 61 62 2c 20 74 72 69 67 67 65 72 73 , pTab, triggers
20493 5f 65 78 69 73 74 29 20 29 7b 0a 20 20 20 20 67 _exist) ){. g
20494 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f oto delete_from_
20495 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 cleanup;. }. i
20496 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
20497 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 maToIndex(db, pT
20498 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 ab->pSchema);.
20499 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e assert( iDb<db->
2049a 6e 44 62 20 29 3b 0a 20 20 7a 44 62 20 3d 20 64 nDb );. zDb = d
2049b 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
2049c 65 3b 0a 20 20 72 63 61 75 74 68 20 3d 20 73 71 e;. rcauth = sq
2049d 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
2049e 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 Parse, SQLITE_DE
2049f 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d LETE, pTab->zNam
204a0 65 2c 20 30 2c 20 7a 44 62 29 3b 0a 20 20 61 73 e, 0, zDb);. as
204a1 73 65 72 74 28 20 72 63 61 75 74 68 3d 3d 53 51 sert( rcauth==SQ
204a2 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 61 75 74 LITE_OK || rcaut
204a3 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 7c h==SQLITE_DENY |
204a4 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 | rcauth==SQLITE
204a5 5f 49 47 4e 4f 52 45 20 29 3b 0a 20 20 69 66 28 _IGNORE );. if(
204a6 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f rcauth==SQLITE_
204a7 44 45 4e 59 20 29 7b 0a 20 20 20 20 67 6f 74 6f DENY ){. goto
204a8 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 delete_from_cle
204a9 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 anup;. }. asse
204aa 72 74 28 21 69 73 56 69 65 77 20 7c 7c 20 74 72 rt(!isView || tr
204ab 69 67 67 65 72 73 5f 65 78 69 73 74 29 3b 0a 0a iggers_exist);..
204ac 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20 /* If pTab is
204ad 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d really a view, m
204ae 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20 ake sure it has
204af 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 been initialized
204b0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c .. */. if( sql
204b1 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d ite3ViewGetColum
204b2 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 nNames(pParse, p
204b3 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f Tab) ){. goto
204b4 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 delete_from_cle
204b5 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 anup;. }.. /*
204b6 41 6c 6c 6f 63 61 74 65 20 61 20 63 75 72 73 6f Allocate a curso
204b7 72 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 r used to store
204b8 74 68 65 20 6f 6c 64 2e 2a 20 64 61 74 61 20 66 the old.* data f
204b9 6f 72 20 61 20 74 72 69 67 67 65 72 2e 0a 20 20 or a trigger..
204ba 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 */. if( trigger
204bb 73 5f 65 78 69 73 74 20 29 7b 20 0a 20 20 20 20 s_exist ){ .
204bc 6f 6c 64 49 64 78 20 3d 20 70 50 61 72 73 65 2d oldIdx = pParse-
204bd 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 >nTab++;. }..
204be 2f 2a 20 41 73 73 69 67 6e 20 20 63 75 72 73 6f /* Assign curso
204bf 72 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 r number to the
204c0 74 61 62 6c 65 20 61 6e 64 20 61 6c 6c 20 69 74 table and all it
204c1 73 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a s indices.. */.
204c2 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 assert( pTabLi
204c3 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 st->nSrc==1 );.
204c4 20 69 43 75 72 20 3d 20 70 54 61 62 4c 69 73 74 iCur = pTabList
204c5 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d ->a[0].iCursor =
204c6 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b pParse->nTab++;
204c7 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 . for(pIdx=pTab
204c8 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 ->pIndex; pIdx;
204c9 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 pIdx=pIdx->pNext
204ca 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e ){. pParse->n
204cb 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a Tab++;. }.. /*
204cc 20 53 74 61 72 74 20 74 68 65 20 76 69 65 77 20 Start the view
204cd 63 6f 6e 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69 context. */. i
204ce 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 f( isView ){.
204cf 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 sqlite3AuthCont
204d0 65 78 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 extPush(pParse,
204d1 26 73 43 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d &sContext, pTab-
204d2 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 >zName);. }..
204d3 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 /* Begin generat
204d4 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 ing code.. */.
204d5 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 v = sqlite3GetV
204d6 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 dbe(pParse);. i
204d7 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 f( v==0 ){. g
204d8 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f oto delete_from_
204d9 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 cleanup;. }. i
204da 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 f( pParse->neste
204db 64 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 d==0 ) sqlite3Vd
204dc 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 beCountChanges(v
204dd 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 );. sqlite3Begi
204de 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 nWriteOperation(
204df 70 50 61 72 73 65 2c 20 74 72 69 67 67 65 72 73 pParse, triggers
204e0 5f 65 78 69 73 74 2c 20 69 44 62 29 3b 0a 0a 20 _exist, iDb);..
204e1 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 if( triggers_ex
204e2 69 73 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f ist ){. int o
204e3 72 63 6f 6e 66 20 3d 20 28 28 70 50 61 72 73 65 rconf = ((pParse
204e4 2d 3e 74 72 69 67 53 74 61 63 6b 29 3f 70 50 61 ->trigStack)?pPa
204e5 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e rse->trigStack->
204e6 6f 72 63 6f 6e 66 3a 4f 45 5f 44 65 66 61 75 6c orconf:OE_Defaul
204e7 74 29 3b 0a 20 20 20 20 69 6e 74 20 69 47 6f 74 t);. int iGot
204e8 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 o = sqlite3VdbeA
204e9 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f ddOp0(v, OP_Goto
204ea 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 );. addr = sq
204eb 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
204ec 65 6c 28 76 29 3b 0a 0a 20 20 20 20 69 42 65 67 el(v);.. iBeg
204ed 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 20 inBeforeTrigger
204ee 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 = sqlite3VdbeCur
204ef 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 rentAddr(v);.
204f0 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 43 6f (void)sqlite3Co
204f1 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 deRowTrigger(pPa
204f2 72 73 65 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 rse, TK_DELETE,
204f3 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 0, TRIGGER_BEFOR
204f4 45 2c 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20 E, pTab,.
204f5 20 2d 31 2c 20 6f 6c 64 49 64 78 2c 20 6f 72 63 -1, oldIdx, orc
204f6 6f 6e 66 2c 20 61 64 64 72 2c 20 26 6f 6c 64 5f onf, addr, &old_
204f7 63 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b 0a 20 20 col_mask, 0);.
204f8 20 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69 67 iEndBeforeTrig
204f9 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ger = sqlite3Vdb
204fa 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f eAddOp0(v, OP_Go
204fb 74 6f 29 3b 0a 0a 20 20 20 20 69 42 65 67 69 6e to);.. iBegin
204fc 41 66 74 65 72 54 72 69 67 67 65 72 20 3d 20 73 AfterTrigger = s
204fd 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
204fe 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 28 76 tAddr(v);. (v
204ff 6f 69 64 29 73 71 6c 69 74 65 33 43 6f 64 65 52 oid)sqlite3CodeR
20500 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 owTrigger(pParse
20501 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 , TK_DELETE, 0,
20502 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70 TRIGGER_AFTER, p
20503 54 61 62 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20 Tab, -1,.
20504 20 6f 6c 64 49 64 78 2c 20 6f 72 63 6f 6e 66 2c oldIdx, orconf,
20505 20 61 64 64 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f addr, &old_col_
20506 6d 61 73 6b 2c 20 30 29 3b 0a 20 20 20 20 69 45 mask, 0);. iE
20507 6e 64 41 66 74 65 72 54 72 69 67 67 65 72 20 3d ndAfterTrigger =
20508 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20509 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a p0(v, OP_Goto);.
2050a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
2050b 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 47 6f 74 JumpHere(v, iGot
2050c 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 o);. }.. /* If
2050d 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 we are trying t
2050e 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 61 20 o delete from a
2050f 76 69 65 77 2c 20 72 65 61 6c 69 7a 65 20 74 68 view, realize th
20510 61 74 20 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a at view into. *
20511 2a 20 61 20 65 70 68 65 6d 65 72 61 6c 20 74 61 * a ephemeral ta
20512 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 ble.. */.#if !d
20513 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
20514 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 IT_VIEW) && !def
20515 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
20516 5f 54 52 49 47 47 45 52 29 0a 20 20 69 66 28 20 _TRIGGER). if(
20517 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 isView ){. sq
20518 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 lite3Materialize
20519 56 69 65 77 28 70 50 61 72 73 65 2c 20 70 54 61 View(pParse, pTa
2051a 62 2c 20 70 57 68 65 72 65 2c 20 69 43 75 72 29 b, pWhere, iCur)
2051b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
2051c 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 /* Resolve the c
2051d 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 74 olumn names in t
2051e 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e he WHERE clause.
2051f 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 . */. memset(&
20520 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 sNC, 0, sizeof(s
20521 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 NC));. sNC.pPar
20522 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 se = pParse;. s
20523 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 NC.pSrcList = pT
20524 61 62 4c 69 73 74 3b 0a 20 20 69 66 28 20 73 71 abList;. if( sq
20525 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
20526 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 Names(&sNC, pWhe
20527 72 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 re) ){. goto
20528 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 delete_from_clea
20529 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 nup;. }.. /* I
2052a 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f nitialize the co
2052b 75 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d unter of the num
2052c 62 65 72 20 6f 66 20 72 6f 77 73 20 64 65 6c 65 ber of rows dele
2052d 74 65 64 2c 20 69 66 0a 20 20 2a 2a 20 77 65 20 ted, if. ** we
2052e 61 72 65 20 63 6f 75 6e 74 69 6e 67 20 72 6f 77 are counting row
2052f 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 s.. */. if( db
20530 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
20531 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 _CountRows ){.
20532 20 20 6d 65 6d 43 6e 74 20 3d 20 2b 2b 70 50 61 memCnt = ++pPa
20533 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 rse->nMem;. s
20534 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
20535 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
20536 30 2c 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 7d 0a 0, memCnt);. }.
20537 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
20538 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 OMIT_TRUNCATE_OP
20539 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 TIMIZATION. /*
2053a 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 41 20 Special case: A
2053b 44 45 4c 45 54 45 20 77 69 74 68 6f 75 74 20 61 DELETE without a
2053c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 64 65 WHERE clause de
2053d 6c 65 74 65 73 20 65 76 65 72 79 74 68 69 6e 67 letes everything
2053e 2e 0a 20 20 2a 2a 20 49 74 20 69 73 20 65 61 73 .. ** It is eas
2053f 69 65 72 20 6a 75 73 74 20 74 6f 20 65 72 61 73 ier just to eras
20540 65 20 74 68 65 20 77 68 6f 6c 65 20 74 61 62 6c e the whole tabl
20541 65 2e 20 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 e. Note, howeve
20542 72 2c 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 69 r, that. ** thi
20543 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 s means that the
20544 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e row change coun
20545 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 72 t will be incorr
20546 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ect.. */. if(
20547 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 4f rcauth==SQLITE_O
20548 4b 20 26 26 20 70 57 68 65 72 65 3d 3d 30 20 26 K && pWhere==0 &
20549 26 20 21 74 72 69 67 67 65 72 73 5f 65 78 69 73 & !triggers_exis
2054a 74 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 t && !IsVirtual(
2054b 70 54 61 62 29 20 29 7b 0a 20 20 20 20 61 73 73 pTab) ){. ass
2054c 65 72 74 28 20 21 69 73 56 69 65 77 20 29 3b 0a ert( !isView );.
2054d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2054e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6c 65 61 ddOp3(v, OP_Clea
2054f 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 r, pTab->tnum, i
20550 44 62 2c 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 20 Db, memCnt);.
20551 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 if( !pParse->ne
20552 73 74 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 sted ){. sq
20553 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
20554 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4(v, -1, pTab->z
20555 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 Name, P4_STATIC)
20556 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
20557 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 pIdx=pTab->pInde
20558 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 x; pIdx; pIdx=pI
20559 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 dx->pNext){.
2055a 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e assert( pIdx->
2055b 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 pSchema==pTab->p
2055c 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 Schema );.
2055d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
2055e 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 2(v, OP_Clear, p
2055f 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b Idx->tnum, iDb);
20560 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 . }. }else.#
20561 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
20562 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 OMIT_TRUNCATE_OP
20563 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 20 20 TIMIZATION */.
20564 2f 2a 20 54 68 65 20 75 73 75 61 6c 20 63 61 73 /* The usual cas
20565 65 3a 20 54 68 65 72 65 20 69 73 20 61 20 57 48 e: There is a WH
20566 45 52 45 20 63 6c 61 75 73 65 20 73 6f 20 77 65 ERE clause so we
20567 20 68 61 76 65 20 74 6f 20 73 63 61 6e 20 74 68 have to scan th
20568 72 6f 75 67 68 0a 20 20 2a 2a 20 74 68 65 20 74 rough. ** the t
20569 61 62 6c 65 20 61 6e 64 20 70 69 63 6b 20 77 68 able and pick wh
2056a 69 63 68 20 72 65 63 6f 72 64 73 20 74 6f 20 64 ich records to d
2056b 65 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20 20 7b 0a elete.. */. {.
2056c 20 20 20 20 69 6e 74 20 69 52 6f 77 69 64 20 3d int iRowid =
2056d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
2056e 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 /* Used for
2056f 73 74 6f 72 69 6e 67 20 72 6f 77 69 64 20 76 61 storing rowid va
20570 6c 75 65 73 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a lues. */.. /*
20571 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62 Begin the datab
20572 61 73 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f 0a ase scan. */.
20573 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c pWInfo = sql
20574 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 ite3WhereBegin(p
20575 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c Parse, pTabList,
20576 20 70 57 68 65 72 65 2c 20 30 2c 20 30 29 3b 0a pWhere, 0, 0);.
20577 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d if( pWInfo==
20578 30 20 29 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 0 ) goto delete_
20579 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20 from_cleanup;..
2057a 20 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74 /* Remember t
2057b 68 65 20 72 6f 77 69 64 20 6f 66 20 65 76 65 72 he rowid of ever
2057c 79 20 69 74 65 6d 20 74 6f 20 62 65 20 64 65 6c y item to be del
2057d 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 eted.. */.
2057e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2057f 70 32 28 76 2c 20 49 73 56 69 72 74 75 61 6c 28 p2(v, IsVirtual(
20580 70 54 61 62 29 20 3f 20 4f 50 5f 56 52 6f 77 69 pTab) ? OP_VRowi
20581 64 20 3a 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 d : OP_Rowid, iC
20582 75 72 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 20 ur, iRowid);.
20583 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20584 70 31 28 76 2c 20 4f 50 5f 46 69 66 6f 57 72 69 p1(v, OP_FifoWri
20585 74 65 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 20 te, iRowid);.
20586 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 if( db->flags &
20587 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 SQLITE_CountRow
20588 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 s ){. sqlit
20589 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
2058a 4f 50 5f 41 64 64 49 6d 6d 2c 20 6d 65 6d 43 6e OP_AddImm, memCn
2058b 74 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 t, 1);. }..
2058c 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74 /* End the dat
2058d 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e abase scan loop.
2058e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
2058f 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e te3WhereEnd(pWIn
20590 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 fo);.. /* Ope
20591 6e 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 n the pseudo-tab
20592 6c 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 le used to store
20593 20 4f 4c 44 20 69 66 20 74 68 65 72 65 20 61 72 OLD if there ar
20594 65 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20 e triggers..
20595 2a 2f 0a 20 20 20 20 69 66 28 20 74 72 69 67 67 */. if( trigg
20596 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 ers_exist ){.
20597 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20598 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e 75 dOp2(v, OP_SetNu
20599 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 54 61 mColumns, 0, pTa
2059a 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 b->nCol);.
2059b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
2059c 31 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 1(v, OP_OpenPseu
2059d 64 6f 2c 20 6f 6c 64 49 64 78 29 3b 0a 20 20 20 do, oldIdx);.
2059e 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 }.. /* Delet
2059f 65 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f e every item who
205a0 73 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74 se key was writt
205a1 65 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64 en to the list d
205a2 75 72 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a uring the. **
205a3 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20 database scan.
205a4 20 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65 We have to dele
205a5 74 65 20 69 74 65 6d 73 20 61 66 74 65 72 20 74 te items after t
205a6 68 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c he scan is compl
205a7 65 74 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75 ete. ** becau
205a8 73 65 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69 se deleting an i
205a9 74 65 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74 tem can change t
205aa 68 65 20 73 63 61 6e 20 6f 72 64 65 72 2e 0a 20 he scan order..
205ab 20 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20 */. end =
205ac 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
205ad 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 69 66 abel(v);.. if
205ae 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 ( !isView ){.
205af 20 20 20 2f 2a 20 4f 70 65 6e 20 63 75 72 73 6f /* Open curso
205b0 72 73 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 rs for the table
205b1 20 77 65 20 61 72 65 20 64 65 6c 65 74 69 6e 67 we are deleting
205b2 20 66 72 6f 6d 20 61 6e 64 20 0a 20 20 20 20 20 from and .
205b3 20 2a 2a 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 ** all its indi
205b4 63 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ces.. */.
205b5 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 sqlite3OpenT
205b6 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 ableAndIndices(p
205b7 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 Parse, pTab, iCu
205b8 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 r, OP_OpenWrite)
205b9 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
205ba 54 68 69 73 20 69 73 20 74 68 65 20 62 65 67 69 This is the begi
205bb 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 65 6c nning of the del
205bc 65 74 65 20 6c 6f 6f 70 2e 20 49 66 20 61 20 74 ete loop. If a t
205bd 72 69 67 67 65 72 20 65 6e 63 6f 75 6e 74 65 72 rigger encounter
205be 73 0a 20 20 20 20 2a 2a 20 61 6e 20 49 47 4e 4f s. ** an IGNO
205bf 52 45 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 69 RE constraint, i
205c0 74 20 6a 75 6d 70 73 20 62 61 63 6b 20 74 6f 20 t jumps back to
205c1 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 here.. */.
205c2 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 if( triggers_ex
205c3 69 73 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ist ){. sql
205c4 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
205c5 61 62 65 6c 28 76 2c 20 61 64 64 72 29 3b 0a 20 abel(v, addr);.
205c6 20 20 20 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 }. addr =
205c7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
205c8 32 28 76 2c 20 4f 50 5f 46 69 66 6f 52 65 61 64 2(v, OP_FifoRead
205c9 2c 20 69 52 6f 77 69 64 2c 20 65 6e 64 29 3b 0a , iRowid, end);.
205ca 0a 20 20 20 20 69 66 28 20 74 72 69 67 67 65 72 . if( trigger
205cb 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20 s_exist ){.
205cc 20 69 6e 74 20 69 44 61 74 61 20 3d 20 2b 2b 70 int iData = ++p
205cd 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 2f Parse->nMem; /
205ce 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f * For storing ro
205cf 77 20 64 61 74 61 20 6f 66 20 4f 4c 44 20 74 61 w data of OLD ta
205d0 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a ble */.. /*
205d1 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 If the record i
205d2 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 65 73 s no longer pres
205d3 65 6e 74 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ent in the table
205d4 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 0a 20 20 , jump to the.
205d5 20 20 20 20 2a 2a 20 6e 65 78 74 20 69 74 65 72 ** next iter
205d6 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f ation of the loo
205d7 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f p through the co
205d8 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 66 69 ntents of the fi
205d9 66 6f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 fo.. */.
205da 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
205db 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 dOp3(v, OP_NotEx
205dc 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 ists, iCur, addr
205dd 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 , iRowid);..
205de 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 /* Populate th
205df 65 20 4f 4c 44 2e 2a 20 70 73 65 75 64 6f 2d 74 e OLD.* pseudo-t
205e0 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 able */. if
205e1 28 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 29 ( old_col_mask )
205e2 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
205e3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
205e4 50 5f 52 6f 77 44 61 74 61 2c 20 69 43 75 72 2c P_RowData, iCur,
205e5 20 69 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d iData);. }
205e6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
205e7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
205e8 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 v, OP_Null, 0, i
205e9 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Data);. }.
205ea 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
205eb 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 AddOp3(v, OP_Ins
205ec 65 72 74 2c 20 6f 6c 64 49 64 78 2c 20 69 44 61 ert, oldIdx, iDa
205ed 74 61 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 ta, iRowid);..
205ee 20 20 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b /* Jump back
205ef 20 61 6e 64 20 72 75 6e 20 74 68 65 20 42 45 46 and run the BEF
205f0 4f 52 45 20 74 72 69 67 67 65 72 73 20 2a 2f 0a ORE triggers */.
205f1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
205f2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f eAddOp2(v, OP_Go
205f3 74 6f 2c 20 30 2c 20 69 42 65 67 69 6e 42 65 66 to, 0, iBeginBef
205f4 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20 20 oreTrigger);.
205f5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
205f6 6d 70 48 65 72 65 28 76 2c 20 69 45 6e 64 42 65 mpHere(v, iEndBe
205f7 66 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20 foreTrigger);.
205f8 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73 }.. if( !is
205f9 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a View ){. /*
205fa 20 44 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20 Delete the row
205fb 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
205fc 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
205fd 42 4c 45 0a 20 20 20 20 20 20 69 66 28 20 49 73 BLE. if( Is
205fe 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b Virtual(pTab) ){
205ff 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 . const c
20600 68 61 72 20 2a 70 56 74 61 62 20 3d 20 28 63 6f har *pVtab = (co
20601 6e 73 74 20 63 68 61 72 20 2a 29 70 54 61 62 2d nst char *)pTab-
20602 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 20 20 >pVtab;.
20603 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 sqlite3VtabMakeW
20604 72 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20 ritable(pParse,
20605 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 73 pTab);. s
20606 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
20607 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20 (v, OP_VUpdate,
20608 30 2c 20 31 2c 20 69 52 6f 77 69 64 2c 20 70 56 0, 1, iRowid, pV
20609 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 tab, P4_VTAB);.
2060a 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 }else.#endi
2060b 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 f. {.
2060c 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 sqlite3Generat
2060d 65 52 6f 77 44 65 6c 65 74 65 28 70 50 61 72 73 eRowDelete(pPars
2060e 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 69 e, pTab, iCur, i
2060f 52 6f 77 69 64 2c 20 70 50 61 72 73 65 2d 3e 6e Rowid, pParse->n
20610 65 73 74 65 64 3d 3d 30 29 3b 0a 20 20 20 20 20 ested==0);.
20611 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
20612 20 49 66 20 74 68 65 72 65 20 61 72 65 20 72 6f If there are ro
20613 77 20 74 72 69 67 67 65 72 73 2c 20 63 6c 6f 73 w triggers, clos
20614 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 74 68 e all cursors th
20615 65 6e 20 69 6e 76 6f 6b 65 0a 20 20 20 20 2a 2a en invoke. **
20616 20 74 68 65 20 41 46 54 45 52 20 74 72 69 67 67 the AFTER trigg
20617 65 72 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ers. */. i
20618 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 f( triggers_exis
20619 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4a 75 t ){. /* Ju
2061a 6d 70 20 62 61 63 6b 20 61 6e 64 20 72 75 6e 20 mp back and run
2061b 74 68 65 20 41 46 54 45 52 20 74 72 69 67 67 65 the AFTER trigge
2061c 72 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 rs */. sqli
2061d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
2061e 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65 OP_Goto, 0, iBe
2061f 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 72 29 ginAfterTrigger)
20620 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
20621 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 dbeJumpHere(v, i
20622 45 6e 64 41 66 74 65 72 54 72 69 67 67 65 72 29 EndAfterTrigger)
20623 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
20624 45 6e 64 20 6f 66 20 74 68 65 20 64 65 6c 65 74 End of the delet
20625 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 73 71 e loop */. sq
20626 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
20627 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 v, OP_Goto, 0, a
20628 64 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ddr);. sqlite
20629 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
2062a 6c 28 76 2c 20 65 6e 64 29 3b 0a 0a 20 20 20 20 l(v, end);..
2062b 2f 2a 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72 /* Close the cur
2062c 73 6f 72 73 20 61 66 74 65 72 20 74 68 65 20 6c sors after the l
2062d 6f 6f 70 20 69 66 20 74 68 65 72 65 20 61 72 65 oop if there are
2062e 20 6e 6f 20 72 6f 77 20 74 72 69 67 67 65 72 73 no row triggers
2062f 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56 */. if( !isV
20630 69 65 77 20 20 26 26 20 21 49 73 56 69 72 74 75 iew && !IsVirtu
20631 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 al(pTab) ){.
20632 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d for(i=1, pIdx=
20633 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 pTab->pIndex; pI
20634 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49 dx; i++, pIdx=pI
20635 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 dx->pNext){.
20636 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20637 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 ddOp2(v, OP_Clos
20638 65 2c 20 69 43 75 72 20 2b 20 69 2c 20 70 49 64 e, iCur + i, pId
20639 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 x->tnum);.
2063a 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
2063b 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
2063c 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 Close, iCur);.
2063d 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 }. }.. /*.
2063e 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
2063f 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 mber of rows tha
20640 74 20 77 65 72 65 20 64 65 6c 65 74 65 64 2e 20 t were deleted.
20641 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 If this routine
20642 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 is . ** generat
20643 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65 ing code because
20644 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 of a call to sq
20645 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
20646 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 (), do not. **
20647 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 invoke the callb
20648 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 ack function..
20649 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 */. if( db->fla
2064a 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e gs & SQLITE_Coun
2064b 74 52 6f 77 73 20 26 26 20 70 50 61 72 73 65 2d tRows && pParse-
2064c 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 21 70 >nested==0 && !p
2064d 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b Parse->trigStack
2064e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
2064f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
20650 52 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 43 6e ResultRow, memCn
20651 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 t, 1);. sqlit
20652 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 e3VdbeSetNumCols
20653 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 (v, 1);. sqli
20654 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d te3VdbeSetColNam
20655 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f e(v, 0, COLNAME_
20656 4e 41 4d 45 2c 20 22 72 6f 77 73 20 64 65 6c 65 NAME, "rows dele
20657 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 ted", SQLITE_STA
20658 54 49 43 29 3b 0a 20 20 7d 0a 0a 64 65 6c 65 74 TIC);. }..delet
20659 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a 0a e_from_cleanup:.
2065a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e sqlite3AuthCon
2065b 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78 textPop(&sContex
2065c 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 t);. sqlite3Src
2065d 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
2065e 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 TabList);. sqli
2065f 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
20660 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 , pWhere);. ret
20661 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 urn;.}../*.** Th
20662 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 is routine gener
20663 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 74 ates VDBE code t
20664 68 61 74 20 63 61 75 73 65 73 20 61 20 73 69 6e hat causes a sin
20665 67 6c 65 20 72 6f 77 20 6f 66 20 61 0a 2a 2a 20 gle row of a.**
20666 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 74 6f 20 single table to
20667 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a be deleted..**.*
20668 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73 74 20 * The VDBE must
20669 62 65 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c be in a particul
2066a 61 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 ar state when th
2066b 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
2066c 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61 lled..** These a
2066d 72 65 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 re the requireme
2066e 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 nts:.**.** 1.
2066f 20 41 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 A read/write cu
20670 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f rsor pointing to
20671 20 70 54 61 62 2c 20 74 68 65 20 74 61 62 6c 65 pTab, the table
20672 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
20673 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20 row.** to
20674 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75 73 74 be deleted, must
20675 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 63 75 be opened as cu
20676 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 62 61 73 rsor number "bas
20677 65 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 e"..**.** 2.
20678 52 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f Read/write curso
20679 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 rs for all indic
2067a 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20 es of pTab must
2067b 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20 be open as.**
2067c 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 cursor numbe
2067d 72 20 62 61 73 65 2b 69 20 66 6f 72 20 74 68 65 r base+i for the
2067e 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a i-th index..**.
2067f 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 72 65 63 ** 3. The rec
20680 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 ord number of th
20681 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 e row to be dele
20682 74 65 64 20 6d 75 73 74 20 62 65 20 73 74 6f 72 ted must be stor
20683 65 64 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 6d ed in.** m
20684 65 6d 6f 72 79 20 63 65 6c 6c 20 69 52 6f 77 69 emory cell iRowi
20685 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f d..**.** This ro
20686 75 74 69 6e 65 20 70 6f 70 73 20 74 68 65 20 74 utine pops the t
20687 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 op of the stack
20688 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 72 65 to remove the re
20689 63 6f 72 64 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 cord number.** a
2068a 6e 64 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 nd then generate
2068b 73 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 s code to remove
2068c 20 62 6f 74 68 20 74 68 65 20 74 61 62 6c 65 20 both the table
2068d 72 65 63 6f 72 64 20 61 6e 64 20 61 6c 6c 20 69 record and all i
2068e 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 69 65 73 20 ndex.** entries
2068f 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 that point to th
20690 61 74 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 at record..*/.SQ
20691 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20692 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 d sqlite3Generat
20693 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20 50 61 eRowDelete(. Pa
20694 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
20695 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20696 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ext */. Table *
20697 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 pTab, /* T
20698 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 able containing
20699 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 the row to be de
2069a 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 leted */. int i
2069b 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Cur, /*
2069c 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 Cursor number f
2069d 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a or the table */.
2069e 20 20 69 6e 74 20 69 52 6f 77 69 64 2c 20 20 20 int iRowid,
2069f 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 /* Memory c
206a0 65 6c 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ell that contain
206a1 73 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20 64 s the rowid to d
206a2 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 63 elete */. int c
206a3 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 2f 2a ount /*
206a4 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 Increment the r
206a5 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 ow change counte
206a6 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 r */.){. int ad
206a7 64 72 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a dr;. Vdbe *v;..
206a8 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 v = pParse->pV
206a9 64 62 65 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 dbe;. addr = sq
206aa 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
206ab 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c v, OP_NotExists,
206ac 20 69 43 75 72 2c 20 30 2c 20 69 52 6f 77 69 64 iCur, 0, iRowid
206ad 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 65 6e 65 );. sqlite3Gene
206ae 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 rateRowIndexDele
206af 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c te(pParse, pTab,
206b0 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 73 71 6c iCur, 0);. sql
206b1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
206b2 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75 , OP_Delete, iCu
206b3 72 2c 20 28 63 6f 75 6e 74 3f 4f 50 46 4c 41 47 r, (count?OPFLAG
206b4 5f 4e 43 48 41 4e 47 45 3a 30 29 29 3b 0a 20 20 _NCHANGE:0));.
206b5 69 66 28 20 63 6f 75 6e 74 20 29 7b 0a 20 20 20 if( count ){.
206b6 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
206b7 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 geP4(v, -1, pTab
206b8 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 ->zName, P4_STAT
206b9 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 IC);. }. sqlit
206ba 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
206bb 2c 20 61 64 64 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , addr);.}../*.*
206bc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 * This routine g
206bd 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f enerates VDBE co
206be 64 65 20 74 68 61 74 20 63 61 75 73 65 73 20 74 de that causes t
206bf 68 65 20 64 65 6c 65 74 69 6f 6e 20 6f 66 20 61 he deletion of a
206c0 6c 6c 0a 2a 2a 20 69 6e 64 65 78 20 65 6e 74 72 ll.** index entr
206c1 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ies associated w
206c2 69 74 68 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 ith a single row
206c3 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 of a single tab
206c4 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 le..**.** The VD
206c5 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61 20 BE must be in a
206c6 70 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 65 particular state
206c7 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
206c8 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a ne is called..**
206c9 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20 72 These are the r
206ca 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a equirements:.**.
206cb 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f ** 1. A read/
206cc 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 write cursor poi
206cd 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 nting to pTab, t
206ce 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e he table contain
206cf 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 ing the row.**
206d0 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74 to be delet
206d1 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e ed, must be open
206d2 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d ed as cursor num
206d3 62 65 72 20 22 69 43 75 72 22 2e 0a 2a 2a 0a 2a ber "iCur"..**.*
206d4 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69 * 2. Read/wri
206d5 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 te cursors for a
206d6 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 ll indices of pT
206d7 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 ab must be open
206d8 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73 as.** curs
206d9 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2b 69 or number iCur+i
206da 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e for the i-th in
206db 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 dex..**.** 3.
206dc 20 54 68 65 20 22 69 43 75 72 22 20 63 75 72 73 The "iCur" curs
206dd 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 or must be point
206de 69 6e 67 20 74 6f 20 74 68 65 20 72 6f 77 20 74 ing to the row t
206df 68 61 74 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 hat is to be.**
206e0 20 20 20 20 20 20 64 65 6c 65 74 65 64 2e 0a 2a deleted..*
206e1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
206e2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e void sqlite3Gen
206e3 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c erateRowIndexDel
206e4 65 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 ete(. Parse *pP
206e5 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 arse, /* Par
206e6 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 sing and code ge
206e7 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 nerating context
206e8 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 */. Table *pTa
206e9 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c b, /* Tabl
206ea 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
206eb 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 row to be delet
206ec 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 ed */. int iCur
206ed 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 , /* Cu
206ee 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 rsor number for
206ef 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 the table */. i
206f0 6e 74 20 2a 61 52 65 67 49 64 78 20 20 20 20 20 nt *aRegIdx
206f1 20 20 2f 2a 20 4f 6e 6c 79 20 64 65 6c 65 74 65 /* Only delete
206f2 20 69 66 20 61 52 65 67 49 64 78 21 3d 30 20 26 if aRegIdx!=0 &
206f3 26 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 2a & aRegIdx[i]>0 *
206f4 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 /.){. int i;.
206f5 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 Index *pIdx;. i
206f6 6e 74 20 72 31 3b 0a 0a 20 20 66 6f 72 28 69 3d nt r1;.. for(i=
206f7 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 1, pIdx=pTab->pI
206f8 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c ndex; pIdx; i++,
206f9 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 pIdx=pIdx->pNex
206fa 74 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67 t){. if( aReg
206fb 49 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64 Idx!=0 && aRegId
206fc 78 5b 69 2d 31 5d 3d 3d 30 20 29 20 63 6f 6e 74 x[i-1]==0 ) cont
206fd 69 6e 75 65 3b 0a 20 20 20 20 72 31 20 3d 20 73 inue;. r1 = s
206fe 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e qlite3GenerateIn
206ff 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70 dexKey(pParse, p
20700 49 64 78 2c 20 69 43 75 72 2c 20 30 2c 20 30 29 Idx, iCur, 0, 0)
20701 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
20702 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d 3e eAddOp3(pParse->
20703 70 56 64 62 65 2c 20 4f 50 5f 49 64 78 44 65 6c pVdbe, OP_IdxDel
20704 65 74 65 2c 20 69 43 75 72 2b 69 2c 20 72 31 2c ete, iCur+i, r1,
20705 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 pIdx->nColumn+1)
20706 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 ;. }.}../*.** G
20707 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 enerate code tha
20708 74 20 77 69 6c 6c 20 61 73 73 65 6d 62 6c 65 20 t will assemble
20709 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 61 6e 64 an index key and
2070a 20 70 75 74 20 69 74 20 69 6e 20 72 65 67 69 73 put it in regis
2070b 74 65 72 0a 2a 2a 20 72 65 67 4f 75 74 2e 20 20 ter.** regOut.
2070c 54 68 65 20 6b 65 79 20 77 69 74 68 20 62 65 20 The key with be
2070d 66 6f 72 20 69 6e 64 65 78 20 70 49 64 78 20 77 for index pIdx w
2070e 68 69 63 68 20 69 73 20 61 6e 20 69 6e 64 65 78 hich is an index
2070f 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a 20 69 43 75 on pTab..** iCu
20710 72 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f r is the index o
20711 66 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 20 f a cursor open
20712 6f 6e 20 74 68 65 20 70 54 61 62 20 74 61 62 6c on the pTab tabl
20713 65 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20 74 e and pointing t
20714 6f 0a 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 74 o.** the entry t
20715 68 61 74 20 6e 65 65 64 73 20 69 6e 64 65 78 69 hat needs indexi
20716 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ng..**.** Return
20717 20 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 a register numb
20718 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 er which is the
20719 66 69 72 73 74 20 69 6e 20 61 20 62 6c 6f 63 6b first in a block
2071a 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 of.** registers
2071b 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 that holds the
2071c 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 elements of the
2071d 69 6e 64 65 78 20 6b 65 79 2e 20 20 54 68 65 0a index key. The.
2071e 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20 72 65 67 69 ** block of regi
2071f 73 74 65 72 73 20 68 61 73 20 61 6c 72 65 61 64 sters has alread
20720 79 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 y been deallocat
20721 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 0a 2a ed by the time.*
20722 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * this routine r
20723 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 eturns..*/.SQLIT
20724 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20725 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 lite3GenerateInd
20726 65 78 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a exKey(. Parse *
20727 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 pParse, /* P
20728 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
20729 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c /. Index *pIdx,
2072a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e /* The in
2072b 64 65 78 20 66 6f 72 20 77 68 69 63 68 20 74 6f dex for which to
2072c 20 67 65 6e 65 72 61 74 65 20 61 20 6b 65 79 20 generate a key
2072d 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 */. int iCur,
2072e 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f /* Curso
2072f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 r number for the
20730 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 20 74 61 pIdx->pTable ta
20731 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 ble */. int reg
20732 4f 75 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 Out, /* W
20733 72 69 74 65 20 74 68 65 20 6e 65 77 20 69 6e 64 rite the new ind
20734 65 78 20 6b 65 79 20 74 6f 20 74 68 69 73 20 72 ex key to this r
20735 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 egister */. int
20736 20 64 6f 4d 61 6b 65 52 65 63 20 20 20 20 20 20 doMakeRec
20737 2f 2a 20 52 75 6e 20 74 68 65 20 4f 50 5f 4d 61 /* Run the OP_Ma
20738 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63 keRecord instruc
20739 74 69 6f 6e 20 69 66 20 74 72 75 65 20 2a 2f 0a tion if true */.
2073a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 ){. Vdbe *v = p
2073b 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 Parse->pVdbe;.
2073c 69 6e 74 20 6a 3b 0a 20 20 54 61 62 6c 65 20 2a int j;. Table *
2073d 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 pTab = pIdx->pTa
2073e 62 6c 65 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 ble;. int regBa
2073f 73 65 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a se;. int nCol;.
20740 0a 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e . nCol = pIdx->
20741 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 72 65 67 42 61 nColumn;. regBa
20742 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 se = sqlite3GetT
20743 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c empRange(pParse,
20744 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 73 71 6c 69 nCol+1);. sqli
20745 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
20746 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c OP_Rowid, iCur,
20747 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c 29 3b 0a regBase+nCol);.
20748 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f for(j=0; j<nCo
20749 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 l; j++){. int
2074a 20 69 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 idx = pIdx->aiC
2074b 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 69 66 olumn[j];. if
2074c 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b ( idx==pTab->iPK
2074d 65 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ey ){. sqli
2074e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
2074f 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 42 61 OP_SCopy, regBa
20750 73 65 2b 6e 43 6f 6c 2c 20 72 65 67 42 61 73 65 se+nCol, regBase
20751 2b 6a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a +j);. }else{.
20752 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
20753 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f eAddOp3(v, OP_Co
20754 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 64 78 2c lumn, iCur, idx,
20755 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 regBase+j);.
20756 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e sqlite3Column
20757 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c Default(v, pTab,
20758 20 69 64 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d idx);. }. }
20759 0a 20 20 69 66 28 20 64 6f 4d 61 6b 65 52 65 63 . if( doMakeRec
2075a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
2075b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
2075c 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 MakeRecord, regB
2075d 61 73 65 2c 20 6e 43 6f 6c 2b 31 2c 20 72 65 67 ase, nCol+1, reg
2075e 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Out);. sqlite
2075f 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 3IndexAffinitySt
20760 72 28 76 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 r(v, pIdx);.
20761 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
20762 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 AffinityChange(p
20763 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 Parse, regBase,
20764 6e 43 6f 6c 2b 31 29 3b 0a 20 20 7d 0a 20 20 73 nCol+1);. }. s
20765 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
20766 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 pRange(pParse, r
20767 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b egBase, nCol+1);
20768 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42 61 73 . return regBas
20769 65 3b 0a 7d 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 e;.}../* Make su
2076a 72 65 20 22 69 73 56 69 65 77 22 20 67 65 74 73 re "isView" gets
2076b 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 63 61 undefined in ca
2076c 73 65 20 74 68 69 73 20 66 69 6c 65 20 62 65 63 se this file bec
2076d 6f 6d 65 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20 omes part of.**
2076e 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e the amalgamation
2076f 20 2d 20 73 6f 20 74 68 61 74 20 73 75 62 73 65 - so that subse
20770 71 75 65 6e 74 20 66 69 6c 65 73 20 64 6f 20 6e quent files do n
20771 6f 74 20 73 65 65 20 69 73 56 69 65 77 20 61 73 ot see isView as
20772 20 61 0a 2a 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a a.** macro. */.
20773 23 75 6e 64 65 66 20 69 73 56 69 65 77 0a 0a 2f #undef isView../
20774 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
20775 6e 64 20 6f 66 20 64 65 6c 65 74 65 2e 63 20 2a nd of delete.c *
20776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20778 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
20779 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
2077a 65 67 69 6e 20 66 69 6c 65 20 66 75 6e 63 2e 63 egin file func.c
2077b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
2077c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2077d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
2077e 2a 0a 2a 2a 20 32 30 30 32 20 46 65 62 72 75 61 *.** 2002 Februa
2077f 72 79 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ry 23.**.** The
20780 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
20781 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
20782 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
20783 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
20784 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
20785 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
20786 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
20787 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
20788 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
20789 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
2078a 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
2078b 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
2078c 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
2078d 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
2078e 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
2078f 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
20790 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
20791 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20792 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20793 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20794 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20795 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
20796 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 e contains the C
20797 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
20798 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75 implement variou
20799 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f s SQL.** functio
2079a 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 0a ns of SQLite. .
2079b 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f **.** There is o
2079c 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 64 nly one exported
2079d 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20 symbol in this
2079e 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 74 file - the funct
2079f 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 52 65 67 ion.** sqliteReg
207a0 69 73 74 65 72 42 75 69 6c 64 69 6e 46 75 6e 63 isterBuildinFunc
207a1 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 tions() found at
207a2 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 the bottom of t
207a3 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 he file..** All
207a4 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 66 other code has f
207a5 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a ile scope..**.**
207a6 20 24 49 64 3a 20 66 75 6e 63 2e 63 2c 76 20 31 $Id: func.c,v 1
207a7 2e 32 30 34 20 32 30 30 38 2f 31 30 2f 32 38 20 .204 2008/10/28
207a8 31 37 3a 35 32 3a 33 39 20 64 61 6e 69 65 6c 6b 17:52:39 danielk
207a9 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 1977 Exp $.*/../
207aa 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
207ab 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 collating functi
207ac 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 on associated wi
207ad 74 68 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a th a function..*
207ae 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 /.static CollSeq
207af 20 2a 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 *sqlite3GetFunc
207b0 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 5f CollSeq(sqlite3_
207b1 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
207b2 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 ){. return cont
207b3 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f ext->pColl;.}../
207b4 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
207b5 69 6f 6e 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61 ion of the non-a
207b6 67 67 72 65 67 61 74 65 20 6d 69 6e 28 29 20 61 ggregate min() a
207b7 6e 64 20 6d 61 78 28 29 20 66 75 6e 63 74 69 6f nd max() functio
207b8 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ns.*/.static voi
207b9 64 20 6d 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20 d minmaxFunc(.
207ba 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
207bb 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
207bc 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f argc,. sqlite3_
207bd 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
207be 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d int i;. int m
207bf 61 73 6b 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72 ask; /* 0 for
207c0 20 6d 69 6e 28 29 20 6f 72 20 30 78 66 66 66 66 min() or 0xffff
207c1 66 66 66 66 20 66 6f 72 20 6d 61 78 28 29 20 2a ffff for max() *
207c2 2f 0a 20 20 69 6e 74 20 69 42 65 73 74 3b 0a 20 /. int iBest;.
207c3 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b CollSeq *pColl;
207c4 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 20 .. if( argc==0
207c5 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 61 73 6b ) return;. mask
207c6 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f = sqlite3_user_
207c7 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3d 3d 30 data(context)==0
207c8 20 3f 20 30 20 3a 20 2d 31 3b 0a 20 20 70 43 6f ? 0 : -1;. pCo
207c9 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 ll = sqlite3GetF
207ca 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 uncCollSeq(conte
207cb 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 xt);. assert( p
207cc 43 6f 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 Coll );. assert
207cd 28 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c 20 6d 61 ( mask==-1 || ma
207ce 73 6b 3d 3d 30 20 29 3b 0a 20 20 69 42 65 73 74 sk==0 );. iBest
207cf 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 = 0;. if( sqli
207d0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 te3_value_type(a
207d1 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f rgv[0])==SQLITE_
207d2 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 NULL ) return;.
207d3 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 for(i=1; i<argc
207d4 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
207d5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
207d6 70 65 28 61 72 67 76 5b 69 5d 29 3d 3d 53 51 4c pe(argv[i])==SQL
207d7 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 ITE_NULL ) retur
207d8 6e 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 n;. if( (sqli
207d9 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 te3MemCompare(ar
207da 67 76 5b 69 42 65 73 74 5d 2c 20 61 72 67 76 5b gv[iBest], argv[
207db 69 5d 2c 20 70 43 6f 6c 6c 29 5e 6d 61 73 6b 29 i], pColl)^mask)
207dc 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 42 65 >=0 ){. iBe
207dd 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 st = i;. }.
207de 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 }. sqlite3_resu
207df 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 lt_value(context
207e0 2c 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b 0a , argv[iBest]);.
207e1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
207e2 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20 the type of the
207e3 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 argument..*/.sta
207e4 74 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66 46 tic void typeofF
207e5 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
207e6 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
207e7 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
207e8 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
207e9 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 rgv.){. const c
207ea 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20 73 77 har *z = 0;. sw
207eb 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 itch( sqlite3_va
207ec 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d lue_type(argv[0]
207ed 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 ) ){. case SQ
207ee 4c 49 54 45 5f 4e 55 4c 4c 3a 20 20 20 20 7a 20 LITE_NULL: z
207ef 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65 = "null"; bre
207f0 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ak;. case SQL
207f1 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a 20 3d ITE_INTEGER: z =
207f2 20 22 69 6e 74 65 67 65 72 22 3b 20 62 72 65 61 "integer"; brea
207f3 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 k;. case SQLI
207f4 54 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 3d 20 TE_TEXT: z =
207f5 22 74 65 78 74 22 3b 20 20 20 20 62 72 65 61 6b "text"; break
207f6 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 ;. case SQLIT
207f7 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d 20 22 E_FLOAT: z = "
207f8 72 65 61 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b real"; break;
207f9 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
207fa 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 22 62 _BLOB: z = "b
207fb 6c 6f 62 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a lob"; break;.
207fc 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 }. sqlite3_re
207fd 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
207fe 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 t, z, -1, SQLITE
207ff 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f 2a _STATIC);.}.../*
20800 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
20801 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68 on of the length
20802 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 () function.*/.s
20803 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67 74 tatic void lengt
20804 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 hFunc(. sqlite3
20805 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
20806 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
20807 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
20808 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6c *argv.){. int l
20809 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 en;.. assert( a
2080a 72 67 63 3d 3d 31 20 29 3b 0a 20 20 73 77 69 74 rgc==1 );. swit
2080b 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ch( sqlite3_valu
2080c 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 e_type(argv[0])
2080d 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
2080e 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73 TE_BLOB:. cas
2080f 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 e SQLITE_INTEGER
20810 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 :. case SQLIT
20811 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 E_FLOAT: {.
20812 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
20813 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c int(context, sql
20814 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
20815 28 61 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 (argv[0]));.
20816 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
20817 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 case SQLITE_T
20818 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e EXT: {. con
20819 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
2081a 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 *z = sqlite3_va
2081b 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
2081c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d );. if( z==
2081d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 0 ) return;.
2081e 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 len = 0;.
2081f 20 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 while( *z ){.
20820 20 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 len++;.
20821 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 SQLITE_SKIP
20822 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 _UTF8(z);.
20823 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
20824 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 result_int(conte
20825 78 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20 xt, len);.
20826 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
20827 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
20828 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
20829 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a _null(context);.
2082a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2082b 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
2082c 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
2082d 66 20 74 68 65 20 61 62 73 28 29 20 66 75 6e 63 f the abs() func
2082e 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tion.*/.static v
2082f 6f 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c 69 oid absFunc(sqli
20830 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
20831 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 text, int argc,
20832 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
20833 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74 28 argv){. assert(
20834 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 73 77 argc==1 );. sw
20835 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 itch( sqlite3_va
20836 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d lue_type(argv[0]
20837 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 ) ){. case SQ
20838 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a LITE_INTEGER: {.
20839 20 20 20 20 20 20 69 36 34 20 69 56 61 6c 20 3d i64 iVal =
2083a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
2083b 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 nt64(argv[0]);.
2083c 20 20 20 20 20 69 66 28 20 69 56 61 6c 3c 30 20 if( iVal<0
2083d 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 ){. if( (
2083e 69 56 61 6c 3c 3c 31 29 3d 3d 30 20 29 7b 0a 20 iVal<<1)==0 ){.
2083f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
20840 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f _result_error(co
20841 6e 74 65 78 74 2c 20 22 69 6e 74 65 67 65 72 20 ntext, "integer
20842 6f 76 65 72 66 6c 6f 77 22 2c 20 2d 31 29 3b 0a overflow", -1);.
20843 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
20844 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
20845 20 20 20 20 69 56 61 6c 20 3d 20 2d 69 56 61 6c iVal = -iVal
20846 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 ;. } .
20847 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
20848 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 69 int64(context, i
20849 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 Val);. brea
2084a 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
2084b 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b e SQLITE_NULL: {
2084c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
2084d 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65 esult_null(conte
2084e 78 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b xt);. break
2084f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
20850 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75 ult: {. dou
20851 62 6c 65 20 72 56 61 6c 20 3d 20 73 71 6c 69 74 ble rVal = sqlit
20852 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 e3_value_double(
20853 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 argv[0]);.
20854 69 66 28 20 72 56 61 6c 3c 30 20 29 20 72 56 61 if( rVal<0 ) rVa
20855 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20 20 20 20 l = -rVal;.
20856 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
20857 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 double(context,
20858 72 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 rVal);. bre
20859 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a ak;. }. }.}.
2085a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
2085b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 62 ation of the sub
2085c 73 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a str() function..
2085d 2a 2a 0a 2a 2a 20 73 75 62 73 74 72 28 78 2c 70 **.** substr(x,p
2085e 31 2c 70 32 29 20 20 72 65 74 75 72 6e 73 20 70 1,p2) returns p
2085f 32 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 2 characters of
20860 78 5b 5d 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 x[] beginning wi
20861 74 68 20 70 31 2e 0a 2a 2a 20 70 31 20 69 73 20 th p1..** p1 is
20862 31 2d 69 6e 64 65 78 65 64 2e 20 20 53 6f 20 73 1-indexed. So s
20863 75 62 73 74 72 28 78 2c 31 2c 31 29 20 72 65 74 ubstr(x,1,1) ret
20864 75 72 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 urns the first c
20865 68 61 72 61 63 74 65 72 0a 2a 2a 20 6f 66 20 78 haracter.** of x
20866 2e 20 20 49 66 20 78 20 69 73 20 74 65 78 74 2c . If x is text,
20867 20 74 68 65 6e 20 77 65 20 61 63 74 75 61 6c 6c then we actuall
20868 79 20 63 6f 75 6e 74 20 55 54 46 2d 38 20 63 68 y count UTF-8 ch
20869 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 aracters..** If
2086a 78 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65 x is a blob, the
2086b 6e 20 77 65 20 63 6f 75 6e 74 20 62 79 74 65 73 n we count bytes
2086c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 31 20 69 73 ..**.** If p1 is
2086d 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 negative, then
2086e 77 65 20 62 65 67 69 6e 20 61 62 73 28 70 31 29 we begin abs(p1)
2086f 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 from the end of
20870 20 78 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 x[]..*/.static
20871 76 6f 69 64 20 73 75 62 73 74 72 46 75 6e 63 28 void substrFunc(
20872 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
20873 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
20874 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
20875 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
20876 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 ){. const unsig
20877 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 ned char *z;. c
20878 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
20879 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 6c 65 ar *z2;. int le
2087a 6e 3b 0a 20 20 69 6e 74 20 70 30 74 79 70 65 3b n;. int p0type;
2087b 0a 20 20 69 36 34 20 70 31 2c 20 70 32 3b 0a 0a . i64 p1, p2;..
2087c 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d assert( argc==
2087d 33 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a 3 || argc==2 );.
2087e 20 20 70 30 74 79 70 65 20 3d 20 73 71 6c 69 74 p0type = sqlit
2087f 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
20880 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70 30 gv[0]);. if( p0
20881 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f type==SQLITE_BLO
20882 42 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 B ){. len = s
20883 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
20884 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 es(argv[0]);.
20885 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c z = sqlite3_val
20886 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 ue_blob(argv[0])
20887 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 ;. if( z==0 )
20888 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73 73 return;. ass
20889 65 72 74 28 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 ert( len==sqlite
2088a 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
2088b 67 76 5b 30 5d 29 20 29 3b 0a 20 20 7d 65 6c 73 gv[0]) );. }els
2088c 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 e{. z = sqlit
2088d 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
2088e 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 gv[0]);. if(
2088f 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 z==0 ) return;.
20890 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 len = 0;.
20891 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c for(z2=z; *z2; l
20892 65 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c en++){. SQL
20893 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 ITE_SKIP_UTF8(z2
20894 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 );. }. }. p
20895 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 1 = sqlite3_valu
20896 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a e_int(argv[1]);.
20897 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b if( argc==3 ){
20898 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74 65 . p2 = sqlite
20899 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 3_value_int(argv
2089a 5b 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 [2]);. }else{.
2089b 20 20 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f p2 = sqlite3_
2089c 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
2089d 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d e(context)->aLim
2089e 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
2089f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 69 LENGTH];. }. i
208a0 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 70 f( p1<0 ){. p
208a1 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 69 66 1 += len;. if
208a2 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 ( p1<0 ){.
208a3 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 p2 += p1;.
208a4 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 p1 = 0;. }.
208a5 7d 65 6c 73 65 20 69 66 28 20 70 31 3e 30 20 29 }else if( p1>0 )
208a6 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20 20 7d 0a {. p1--;. }.
208a7 20 20 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e 20 if( p1+p2>len
208a8 29 7b 0a 20 20 20 20 70 32 20 3d 20 6c 65 6e 2d ){. p2 = len-
208a9 70 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 30 p1;. }. if( p0
208aa 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 42 4c 4f type!=SQLITE_BLO
208ab 42 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 B ){. while(
208ac 2a 7a 20 26 26 20 70 31 20 29 7b 0a 20 20 20 20 *z && p1 ){.
208ad 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 SQLITE_SKIP_UT
208ae 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 70 31 2d F8(z);. p1-
208af 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 -;. }. for
208b0 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26 20 70 32 (z2=z; *z2 && p2
208b1 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 20 20 20 53 ; p2--){. S
208b2 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 QLITE_SKIP_UTF8(
208b3 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 z2);. }. s
208b4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
208b5 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 xt(context, (cha
208b6 72 2a 29 7a 2c 20 7a 32 2d 7a 2c 20 53 51 4c 49 r*)z, z2-z, SQLI
208b7 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
208b8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 }else{. if(
208b9 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 p2<0 ) p2 = 0;.
208ba 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
208bb 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 t_blob(context,
208bc 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d 2c 20 70 (char*)&z[p1], p
208bd 32 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 2, SQLITE_TRANSI
208be 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ENT);. }.}../*.
208bf 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
208c0 6e 20 6f 66 20 74 68 65 20 72 6f 75 6e 64 28 29 n of the round()
208c1 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 function.*/.sta
208c2 74 69 63 20 76 6f 69 64 20 72 6f 75 6e 64 46 75 tic void roundFu
208c3 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nc(sqlite3_conte
208c4 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 xt *context, int
208c5 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 argc, sqlite3_v
208c6 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 alue **argv){.
208c7 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 75 int n = 0;. dou
208c8 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 7a 42 ble r;. char zB
208c9 75 66 5b 35 30 30 5d 3b 20 20 2f 2a 20 6c 61 72 uf[500]; /* lar
208ca 67 65 72 20 74 68 61 6e 20 74 68 65 20 25 66 20 ger than the %f
208cb 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
208cc 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 6f f the largest do
208cd 75 62 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 uble */. assert
208ce 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 ( argc==1 || arg
208cf 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72 c==2 );. if( ar
208d0 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66 28 gc==2 ){. if(
208d1 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 SQLITE_NULL==sq
208d2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
208d3 28 61 72 67 76 5b 31 5d 29 20 29 20 72 65 74 75 (argv[1]) ) retu
208d4 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 rn;. n = sqli
208d5 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 te3_value_int(ar
208d6 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 gv[1]);. if(
208d7 6e 3e 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a 20 n>30 ) n = 30;.
208d8 20 20 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d if( n<0 ) n =
208d9 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 0;. }. if( sq
208da 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
208db 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 (argv[0])==SQLIT
208dc 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b E_NULL ) return;
208dd 0a 20 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 . r = sqlite3_v
208de 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 alue_double(argv
208df 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f [0]);. sqlite3_
208e0 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
208e1 7a 42 75 66 29 2c 7a 42 75 66 2c 22 25 2e 2a 66 zBuf),zBuf,"%.*f
208e2 22 2c 6e 2c 72 29 3b 0a 20 20 73 71 6c 69 74 65 ",n,r);. sqlite
208e3 33 41 74 6f 46 28 7a 42 75 66 2c 20 26 72 29 3b 3AtoF(zBuf, &r);
208e4 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
208e5 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 t_double(context
208e6 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 , r);.}../*.** A
208e7 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 llocate nByte by
208e8 74 65 73 20 6f 66 20 73 70 61 63 65 20 75 73 69 tes of space usi
208e9 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ng sqlite3_mallo
208ea 63 28 29 2e 20 49 66 20 74 68 65 0a 2a 2a 20 61 c(). If the.** a
208eb 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
208ec 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 call sqlite3_re
208ed 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d sult_error_nomem
208ee 28 29 20 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20 () to notify.**
208ef 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e the database han
208f0 64 6c 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 dle that malloc(
208f1 29 20 68 61 73 20 66 61 69 6c 65 64 2e 0a 2a 2f ) has failed..*/
208f2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f .static void *co
208f3 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c 69 ntextMalloc(sqli
208f4 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
208f5 74 65 78 74 2c 20 69 36 34 20 6e 42 79 74 65 29 text, i64 nByte)
208f6 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 {. char *z;. i
208f7 66 28 20 6e 42 79 74 65 3e 73 71 6c 69 74 65 33 f( nByte>sqlite3
208f8 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
208f9 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 le(context)->aLi
208fa 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
208fb 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 _LENGTH] ){.
208fc 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
208fd 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 rror_toobig(cont
208fe 65 78 74 29 3b 0a 20 20 20 20 7a 20 3d 20 30 3b ext);. z = 0;
208ff 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 . }else{. z
20900 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
20901 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 nByte);. if(
20902 21 7a 20 26 26 20 6e 42 79 74 65 3e 30 20 29 7b !z && nByte>0 ){
20903 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
20904 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 esult_error_nome
20905 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 m(context);.
20906 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a }. }. return z
20907 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
20908 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
20909 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c 6f 77 upper() and low
2090a 65 72 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f er() SQL functio
2090b 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ns..*/.static vo
2090c 69 64 20 75 70 70 65 72 46 75 6e 63 28 73 71 6c id upperFunc(sql
2090d 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
2090e 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c ntext, int argc,
2090f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
20910 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a *argv){. char *
20911 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 z1;. const char
20912 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e *z2;. int i, n
20913 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 7c ;. if( argc<1 |
20914 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 | SQLITE_NULL==s
20915 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
20916 65 28 61 72 67 76 5b 30 5d 29 20 29 20 72 65 74 e(argv[0]) ) ret
20917 75 72 6e 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 urn;. z2 = (cha
20918 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r*)sqlite3_value
20919 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a _text(argv[0]);.
2091a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 n = sqlite3_va
2091b 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 lue_bytes(argv[0
2091c 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 ]);. /* Verify
2091d 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f that the call to
2091e 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20 6e _bytes() does n
2091f 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 ot invalidate th
20920 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74 65 e _text() pointe
20921 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a r */. assert( z
20922 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74 65 2==(char*)sqlite
20923 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
20924 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a v[0]) );. if( z
20925 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 2 ){. z1 = co
20926 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 ntextMalloc(cont
20927 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b 31 29 ext, ((i64)n)+1)
20928 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0a ;. if( z1 ){.
20929 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c memcpy(z1,
2092a 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 20 z2, n+1);.
2092b 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b for(i=0; z1[i];
2092c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a i++){. z
2092d 31 5b 69 5d 20 3d 20 74 6f 75 70 70 65 72 28 7a 1[i] = toupper(z
2092e 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 1[i]);. }.
2092f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
20930 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
20931 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 , z1, -1, sqlite
20932 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 3_free);. }.
20933 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
20934 20 6c 6f 77 65 72 46 75 6e 63 28 73 71 6c 69 74 lowerFunc(sqlit
20935 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
20936 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 ext, int argc, s
20937 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
20938 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a 31 rgv){. char *z1
20939 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
2093a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a z2;. int i, n;.
2093b 20 20 69 66 28 20 61 72 67 63 3c 31 20 7c 7c 20 if( argc<1 ||
2093c 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c SQLITE_NULL==sql
2093d 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
2093e 61 72 67 76 5b 30 5d 29 20 29 20 72 65 74 75 72 argv[0]) ) retur
2093f 6e 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a n;. z2 = (char*
20940 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
20941 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
20942 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 n = sqlite3_valu
20943 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 e_bytes(argv[0])
20944 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 ;. /* Verify th
20945 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f at the call to _
20946 62 79 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 bytes() does not
20947 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 invalidate the
20948 5f 74 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 _text() pointer
20949 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d */. assert( z2=
2094a 3d 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f =(char*)sqlite3_
2094b 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
2094c 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 0]) );. if( z2
2094d 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 ){. z1 = cont
2094e 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 extMalloc(contex
2094f 74 2c 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a t, ((i64)n)+1);.
20950 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20 if( z1 ){.
20951 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20 7a memcpy(z1, z
20952 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 20 20 66 2, n+1);. f
20953 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69 or(i=0; z1[i]; i
20954 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b ++){. z1[
20955 69 5d 20 3d 20 74 6f 6c 6f 77 65 72 28 7a 31 5b i] = tolower(z1[
20956 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 i]);. }.
20957 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
20958 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
20959 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f z1, -1, sqlite3_
2095a 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d free);. }. }
2095b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
2095c 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
2095d 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c 28 29 2c IFNULL(), NVL(),
2095e 20 61 6e 64 20 43 4f 41 4c 45 53 43 45 28 29 20 and COALESCE()
2095f 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a 2a 2a 20 functions. .**
20960 41 6c 6c 20 74 68 72 65 65 20 64 6f 20 74 68 65 All three do the
20961 20 73 61 6d 65 20 74 68 69 6e 67 2e 20 20 54 68 same thing. Th
20962 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 66 69 ey return the fi
20963 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 rst non-NULL.**
20964 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 argument..*/.sta
20965 74 69 63 20 76 6f 69 64 20 69 66 6e 75 6c 6c 46 tic void ifnullF
20966 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
20967 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
20968 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
20969 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
2096a 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a rgv.){. int i;.
2096b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72 67 for(i=0; i<arg
2096c 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 c; i++){. if(
2096d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 SQLITE_NULL!=sq
2096e 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
2096f 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a 20 20 20 (argv[i]) ){.
20970 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
20971 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c t_value(context,
20972 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 20 argv[i]);.
20973 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
20974 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 }.}../*.** Imple
20975 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e mentation of ran
20976 64 6f 6d 28 29 2e 20 20 52 65 74 75 72 6e 20 61 dom(). Return a
20977 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e random integer.
20978 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 .*/.static voi
20979 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0a 20 20 d randomFunc(.
2097a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
2097b 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
2097c 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f argc,. sqlite3_
2097d 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
2097e 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 sqlite_int64 r
2097f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 ;. sqlite3_rand
20980 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 29 omness(sizeof(r)
20981 2c 20 26 72 29 3b 0a 20 20 69 66 28 20 28 72 3c , &r);. if( (r<
20982 3c 31 29 3d 3d 30 20 29 20 72 20 3d 20 30 3b 20 <1)==0 ) r = 0;
20983 20 2f 2a 20 50 72 65 76 65 6e 74 20 30 78 38 30 /* Prevent 0x80
20984 30 30 2e 2e 2e 2e 20 61 73 20 74 68 65 20 72 65 00.... as the re
20985 73 75 6c 74 20 73 6f 20 74 68 61 74 20 77 65 20 sult so that we
20986 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 */.
20987 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20988 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20 61 62 can always do ab
20989 73 28 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c s() of the resul
2098a 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 t */. sqlite3_r
2098b 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 esult_int64(cont
2098c 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ext, r);.}../*.*
2098d 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
2098e 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 4e of randomblob(N
2098f 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e ). Return a ran
20990 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61 74 dom blob.** that
20991 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 is N bytes long
20992 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
20993 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20 73 randomBlob(. s
20994 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
20995 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
20996 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
20997 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
20998 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e int n;. unsign
20999 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20 61 73 ed char *p;. as
2099a 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b sert( argc==1 );
2099b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 . n = sqlite3_v
2099c 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d alue_int(argv[0]
2099d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29 7b 0a );. if( n<1 ){.
2099e 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 n = 1;. }.
2099f 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c p = contextMall
209a0 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b 0a oc(context, n);.
209a1 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 if( p ){. s
209a2 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
209a3 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c s(n, p);. sql
209a4 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
209a5 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a (context, (char*
209a6 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66 )p, n, sqlite3_f
209a7 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ree);. }.}../*.
209a8 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
209a9 6e 20 6f 66 20 74 68 65 20 6c 61 73 74 5f 69 6e n of the last_in
209aa 73 65 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c sert_rowid() SQL
209ab 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 function. The
209ac 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 return.** value
209ad 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 is the same as t
209ae 68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f he sqlite3_last_
209af 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 41 insert_rowid() A
209b0 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a PI function..*/.
209b1 73 74 61 74 69 63 20 76 6f 69 64 20 6c 61 73 74 static void last
209b2 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 0a 20 _insert_rowid(.
209b3 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
209b4 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e *context, . in
209b5 74 20 61 72 67 2c 20 0a 20 20 73 71 6c 69 74 65 t arg, . sqlite
209b6 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
209b7 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
209b8 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 = sqlite3_contex
209b9 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 t_db_handle(cont
209ba 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f ext);. sqlite3_
209bb 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e result_int64(con
209bc 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 6c 61 text, sqlite3_la
209bd 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 st_insert_rowid(
209be 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 db));.}../*.** I
209bf 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
209c0 20 74 68 65 20 63 68 61 6e 67 65 73 28 29 20 53 the changes() S
209c1 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 QL function. Th
209c2 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 e return value i
209c3 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73 s the.** same as
209c4 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61 the sqlite3_cha
209c5 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 nges() API funct
209c6 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
209c7 6f 69 64 20 63 68 61 6e 67 65 73 28 0a 20 20 73 oid changes(. s
209c8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
209c9 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
209ca 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 rg,. sqlite3_va
209cb 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
209cc 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
209cd 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
209ce 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
209cf 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 ;. sqlite3_resu
209d0 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 lt_int(context,
209d1 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 sqlite3_changes(
209d2 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 db));.}../*.** I
209d3 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
209d4 20 74 68 65 20 74 6f 74 61 6c 5f 63 68 61 6e 67 the total_chang
209d5 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f es() SQL functio
209d6 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 n. The return v
209d7 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 73 alue is.** the s
209d8 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74 ame as the sqlit
209d9 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
209da 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e () API function.
209db 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
209dc 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 0a 20 total_changes(.
209dd 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
209de 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
209df 20 61 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f arg,. sqlite3_
209e0 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
209e1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
209e2 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f sqlite3_context_
209e3 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 db_handle(contex
209e4 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 t);. sqlite3_re
209e5 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 sult_int(context
209e6 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f , sqlite3_total_
209e7 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a changes(db));.}.
209e8 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 ./*.** A structu
209e9 72 65 20 64 65 66 69 6e 69 6e 67 20 68 6f 77 20 re defining how
209ea 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 79 6c 65 to do GLOB-style
209eb 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f comparisons..*/
209ec 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 .struct compareI
209ed 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 74 63 68 nfo {. u8 match
209ee 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f All;. u8 matchO
209ef 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65 ne;. u8 matchSe
209f0 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 3b 0a t;. u8 noCase;.
209f1 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 4c 49 };../*.** For LI
209f2 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d 61 74 63 KE and GLOB matc
209f3 68 69 6e 67 20 6f 6e 20 45 42 43 44 49 43 20 6d hing on EBCDIC m
209f4 61 63 68 69 6e 65 73 2c 20 61 73 73 75 6d 65 20 achines, assume
209f5 74 68 61 74 20 65 76 65 72 79 0a 2a 2a 20 63 68 that every.** ch
209f6 61 72 61 63 74 65 72 20 69 73 20 65 78 61 63 74 aracter is exact
209f7 6c 79 20 6f 6e 65 20 62 79 74 65 20 69 6e 20 73 ly one byte in s
209f8 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 61 6c 6c 20 ize. Also, all
209f9 63 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 2a characters are.*
209fa 2a 20 61 62 6c 65 20 74 6f 20 70 61 72 74 69 63 * able to partic
209fb 69 70 61 74 65 20 69 6e 20 75 70 70 65 72 2d 63 ipate in upper-c
209fc 61 73 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63 61 73 ase-to-lower-cas
209fd 65 20 6d 61 70 70 69 6e 67 73 20 69 6e 20 45 42 e mappings in EB
209fe 43 44 49 43 0a 2a 2a 20 77 68 65 72 65 61 73 20 CDIC.** whereas
209ff 6f 6e 6c 79 20 63 68 61 72 61 63 74 65 72 73 20 only characters
20a00 6c 65 73 73 20 74 68 61 6e 20 30 78 38 30 20 64 less than 0x80 d
20a01 6f 20 69 6e 20 41 53 43 49 49 2e 0a 2a 2f 0a 23 o in ASCII..*/.#
20a02 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
20a03 45 5f 45 42 43 44 49 43 29 0a 23 20 64 65 66 69 E_EBCDIC).# defi
20a04 6e 65 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 ne sqlite3Utf8Re
20a05 61 64 28 41 2c 42 2c 43 29 20 20 28 2a 28 41 2b ad(A,B,C) (*(A+
20a06 2b 29 29 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f +)).# define Glo
20a07 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 41 29 gUpperToLower(A)
20a08 20 20 20 20 20 41 20 3d 20 73 71 6c 69 74 65 33 A = sqlite3
20a09 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 0a UpperToLower[A].
20a0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 47 #else.# define G
20a0b 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 logUpperToLower(
20a0c 41 29 20 20 20 20 20 69 66 28 20 41 3c 30 78 38 A) if( A<0x8
20a0d 30 20 29 7b 20 41 20 3d 20 73 71 6c 69 74 65 33 0 ){ A = sqlite3
20a0e 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 3b UpperToLower[A];
20a0f 20 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69 }.#endif..stati
20a10 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 c const struct c
20a11 6f 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f 62 49 ompareInfo globI
20a12 6e 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 3f 27 nfo = { '*', '?'
20a13 2c 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a 20 54 , '[', 0 };./* T
20a14 68 65 20 63 6f 72 72 65 63 74 20 53 51 4c 2d 39 he correct SQL-9
20a15 32 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 2 behavior is fo
20a16 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 r the LIKE opera
20a17 74 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a 2a 2a tor to ignore.**
20a18 20 63 61 73 65 2e 20 20 54 68 75 73 20 20 27 61 case. Thus 'a
20a19 27 20 4c 49 4b 45 20 27 41 27 20 77 6f 75 6c 64 ' LIKE 'A' would
20a1a 20 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73 74 61 be true. */.sta
20a1b 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 tic const struct
20a1c 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b compareInfo lik
20a1d 65 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 27 25 eInfoNorm = { '%
20a1e 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31 20 7d ', '_', 0, 1 }
20a1f 3b 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 ;./* If SQLITE_C
20a20 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 ASE_SENSITIVE_LI
20a21 4b 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 KE is defined, t
20a22 68 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 hen the LIKE ope
20a23 72 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61 73 65 rator.** is case
20a24 20 73 65 6e 73 69 74 69 76 65 20 63 61 75 73 69 sensitive causi
20a25 6e 67 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20 ng 'a' LIKE 'A'
20a26 74 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f 0a 73 to be false */.s
20a27 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 tatic const stru
20a28 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c ct compareInfo l
20a29 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 27 ikeInfoAlt = { '
20a2a 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 20 %', '_', 0, 0
20a2b 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 };../*.** Compar
20a2c 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69 e two UTF-8 stri
20a2d 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79 ngs for equality
20a2e 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 where the first
20a2f 20 73 74 72 69 6e 67 20 63 61 6e 0a 2a 2a 20 70 string can.** p
20a30 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 61 20 otentially be a
20a31 22 67 6c 6f 62 22 20 65 78 70 72 65 73 73 69 6f "glob" expressio
20a32 6e 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 n. Return true
20a33 28 31 29 20 69 66 20 74 68 65 79 0a 2a 2a 20 61 (1) if they.** a
20a34 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 re the same and
20a35 66 61 6c 73 65 20 28 30 29 20 69 66 20 74 68 65 false (0) if the
20a36 79 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e y are different.
20a37 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e 67 20 .**.** Globbing
20a38 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 rules:.**.**
20a39 20 20 27 2a 27 20 20 20 20 20 20 20 4d 61 74 63 '*' Matc
20a3a 68 65 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 hes any sequence
20a3b 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 of zero or more
20a3c 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a characters..**.
20a3d 2a 2a 20 20 20 20 20 20 27 3f 27 20 20 20 20 20 ** '?'
20a3e 20 20 4d 61 74 63 68 65 73 20 65 78 61 63 74 6c Matches exactl
20a3f 79 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 2e y one character.
20a40 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e 2e 5d .**.** [...]
20a41 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e Matches on
20a42 65 20 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d e character from
20a43 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 the enclosed li
20a44 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 st of.**
20a45 20 20 20 20 20 20 20 20 63 68 61 72 61 63 74 65 characte
20a46 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 5e rs..**.** [^
20a47 2e 2e 2e 5d 20 20 20 20 20 4d 61 74 63 68 65 73 ...] Matches
20a48 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 6e one character n
20a49 6f 74 20 69 6e 20 74 68 65 20 65 6e 63 6c 6f 73 ot in the enclos
20a4a 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 ed list..**.** W
20a4b 69 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e ith the [...] an
20a4c 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e d [^...] matchin
20a4d 67 2c 20 61 20 27 5d 27 20 63 68 61 72 61 63 74 g, a ']' charact
20a4e 65 72 20 63 61 6e 20 62 65 20 69 6e 63 6c 75 64 er can be includ
20a4f 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73 ed.** in the lis
20a50 74 20 62 79 20 6d 61 6b 69 6e 67 20 69 74 20 74 t by making it t
20a51 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 he first charact
20a52 65 72 20 61 66 74 65 72 20 27 5b 27 20 6f 72 20 er after '[' or
20a53 27 5e 27 2e 20 20 41 0a 2a 2a 20 72 61 6e 67 65 '^'. A.** range
20a54 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 63 of characters c
20a55 61 6e 20 62 65 20 73 70 65 63 69 66 69 65 64 20 an be specified
20a56 75 73 69 6e 67 20 27 2d 27 2e 20 20 45 78 61 6d using '-'. Exam
20a57 70 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d 22 20 ple:.** "[a-z]"
20a58 6d 61 74 63 68 65 73 20 61 6e 79 20 73 69 6e 67 matches any sing
20a59 6c 65 20 6c 6f 77 65 72 2d 63 61 73 65 20 6c 65 le lower-case le
20a5a 74 74 65 72 2e 20 20 54 6f 20 6d 61 74 63 68 20 tter. To match
20a5b 61 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a 20 69 a '-', make.** i
20a5c 74 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 t the last chara
20a5d 63 74 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 cter in the list
20a5e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
20a5f 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20 tine is usually
20a60 71 75 69 63 6b 2c 20 62 75 74 20 63 61 6e 20 62 quick, but can b
20a61 65 20 4e 2a 2a 32 20 69 6e 20 74 68 65 20 77 6f e N**2 in the wo
20a62 72 73 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 rst case..**.**
20a63 48 69 6e 74 73 3a 20 74 6f 20 6d 61 74 63 68 20 Hints: to match
20a64 27 2a 27 20 6f 72 20 27 3f 27 2c 20 70 75 74 20 '*' or '?', put
20a65 74 68 65 6d 20 69 6e 20 22 5b 5d 22 2e 20 20 4c them in "[]". L
20a66 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
20a67 20 20 20 20 20 20 20 20 61 62 63 5b 2a 5d 78 79 abc[*]xy
20a68 7a 20 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 z Matches
20a69 20 22 61 62 63 2a 78 79 7a 22 20 6f 6e 6c 79 0a "abc*xyz" only.
20a6a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
20a6b 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 0a 20 20 tternCompare(.
20a6c 63 6f 6e 73 74 20 75 38 20 2a 7a 50 61 74 74 65 const u8 *zPatte
20a6d 72 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 rn,
20a6e 20 2f 2a 20 54 68 65 20 67 6c 6f 62 20 70 61 74 /* The glob pat
20a6f 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 tern */. const
20a70 75 38 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 u8 *zString,
20a71 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
20a72 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70 e string to comp
20a73 61 72 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 are against the
20a74 67 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 glob */. const
20a75 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e struct compareIn
20a76 66 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e fo *pInfo, /* In
20a77 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
20a78 68 6f 77 20 74 6f 20 64 6f 20 74 68 65 20 63 6f how to do the co
20a79 6d 70 61 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 mpare */. const
20a7a 20 69 6e 74 20 65 73 63 20 20 20 20 20 20 20 20 int esc
20a7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
20a7c 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63 he escape charac
20a7d 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ter */.){. int
20a7e 63 2c 20 63 32 3b 0a 20 20 69 6e 74 20 69 6e 76 c, c2;. int inv
20a7f 65 72 74 3b 0a 20 20 69 6e 74 20 73 65 65 6e 3b ert;. int seen;
20a80 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 20 3d . u8 matchOne =
20a81 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65 pInfo->matchOne
20a82 3b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 20 ;. u8 matchAll
20a83 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c = pInfo->matchAl
20a84 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74 l;. u8 matchSet
20a85 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 53 = pInfo->matchS
20a86 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 20 et;. u8 noCase
20a87 3d 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b = pInfo->noCase;
20a88 20 0a 20 20 69 6e 74 20 70 72 65 76 45 73 63 61 . int prevEsca
20a89 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 pe = 0; /* T
20a8a 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76 69 rue if the previ
20a8b 6f 75 73 20 63 68 61 72 61 63 74 65 72 20 77 61 ous character wa
20a8c 73 20 27 65 73 63 61 70 65 27 20 2a 2f 0a 0a 20 s 'escape' */..
20a8d 20 77 68 69 6c 65 28 20 28 63 20 3d 20 73 71 6c while( (c = sql
20a8e 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 ite3Utf8Read(zPa
20a8f 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74 74 65 72 ttern,0,&zPatter
20a90 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 n))!=0 ){. if
20a91 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26 ( !prevEscape &&
20a92 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 29 7b 0a c==matchAll ){.
20a93 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d while( (c=
20a94 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
20a95 7a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74 zPattern,0,&zPat
20a96 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 68 41 tern)) == matchA
20a97 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ll.
20a98 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 68 4f || c == matchO
20a99 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ne ){. if
20a9a 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26 26 ( c==matchOne &&
20a9b 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
20a9c 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 (zString, 0, &zS
20a9d 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 20 tring)==0 ){.
20a9e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b return 0;
20a9f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
20aa0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d }. if( c==
20aa1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 0 ){. ret
20aa2 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c urn 1;. }el
20aa3 73 65 20 69 66 28 20 63 3d 3d 65 73 63 20 29 7b se if( c==esc ){
20aa4 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c . c = sql
20aa5 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 ite3Utf8Read(zPa
20aa6 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 ttern, 0, &zPatt
20aa7 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66 ern);. if
20aa8 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ( c==0 ){.
20aa9 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
20aaa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
20aab 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 lse if( c==match
20aac 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 Set ){. a
20aad 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b ssert( esc==0 );
20aae 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
20aaf 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 is GLOB, not LI
20ab0 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 KE */. as
20ab1 73 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c 30 sert( matchSet<0
20ab2 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 69 x80 ); /* '[' i
20ab3 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 s a single-byte
20ab4 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 character */.
20ab5 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 74 while( *zSt
20ab6 72 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e 43 ring && patternC
20ab7 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e ompare(&zPattern
20ab8 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e [-1],zString,pIn
20ab9 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 20 fo,esc)==0 ){.
20aba 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 SQLITE_S
20abb 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e 67 KIP_UTF8(zString
20abc 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
20abd 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 74 return *zSt
20abe 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 7d ring!=0;. }
20abf 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 . while( (c
20ac0 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 2 = sqlite3Utf8R
20ac1 65 61 64 28 7a 53 74 72 69 6e 67 2c 30 2c 26 7a ead(zString,0,&z
20ac2 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20 String))!=0 ){.
20ac3 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73 if( noCas
20ac4 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 47 e ){. G
20ac5 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 logUpperToLower(
20ac6 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 47 c2);. G
20ac7 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 logUpperToLower(
20ac8 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 c);. wh
20ac9 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20 ile( c2 != 0 &&
20aca 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 c2 != c ){.
20acb 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 c2 = sqli
20acc 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 te3Utf8Read(zStr
20acd 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 ing, 0, &zString
20ace 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 47 );. G
20acf 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 logUpperToLower(
20ad0 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d c2);. }
20ad1 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
20ad2 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 while(
20ad3 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 21 c2 != 0 && c2 !
20ad4 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 = c ){.
20ad5 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 c2 = sqlite3U
20ad6 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c tf8Read(zString,
20ad7 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 0, &zString);.
20ad8 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
20ad9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
20ada 20 63 32 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 c2==0 ) return
20adb 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 0;. if( p
20adc 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 50 atternCompare(zP
20add 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67 2c 70 attern,zString,p
20ade 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65 74 75 Info,esc) ) retu
20adf 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 1;. }.
20ae0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
20ae1 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65 }else if( !pre
20ae2 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61 vEscape && c==ma
20ae3 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20 tchOne ){.
20ae4 69 66 28 20 73 71 6c 69 74 65 33 55 74 66 38 52 if( sqlite3Utf8R
20ae5 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 ead(zString, 0,
20ae6 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a &zString)==0 ){.
20ae7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 return 0
20ae8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
20ae9 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 lse if( c==match
20aea 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 Set ){. int
20aeb 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 prior_c = 0;.
20aec 20 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d assert( esc=
20aed 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68 69 73 =0 ); /* This
20aee 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66 6f 72 only occurs for
20aef 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 GLOB, not LIKE
20af0 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20 3d 20 */. seen =
20af1 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72 74 20 0;. invert
20af2 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d 20 73 = 0;. c = s
20af3 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
20af4 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 String, 0, &zStr
20af5 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ing);. if(
20af6 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b c==0 ) return 0;
20af7 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 . c2 = sqli
20af8 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 te3Utf8Read(zPat
20af9 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 tern, 0, &zPatte
20afa 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 rn);. if( c
20afb 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20 2=='^' ){.
20afc 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20 invert = 1;.
20afd 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 c2 = sqlit
20afe 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 e3Utf8Read(zPatt
20aff 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72 ern, 0, &zPatter
20b00 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 n);. }.
20b01 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b if( c2==']' ){
20b02 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d . if( c==
20b03 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a ']' ) seen = 1;.
20b04 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c c2 = sql
20b05 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 ite3Utf8Read(zPa
20b06 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 ttern, 0, &zPatt
20b07 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ern);. }.
20b08 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 26 26 while( c2 &&
20b09 20 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20 20 20 c2!=']' ){.
20b0a 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d 27 20 if( c2=='-'
20b0b 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d && zPattern[0]!=
20b0c 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b ']' && zPattern[
20b0d 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72 5f 63 0]!=0 && prior_c
20b0e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 >0 ){.
20b0f 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c2 = sqlite3Utf8
20b10 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30 Read(zPattern, 0
20b11 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 , &zPattern);.
20b12 20 20 20 20 20 20 20 20 69 66 28 20 63 3e 3d 70 if( c>=p
20b13 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63 32 20 rior_c && c<=c2
20b14 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 ) seen = 1;.
20b15 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 prior_c =
20b16 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0;. }else
20b17 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
20b18 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20 c==c2 ){.
20b19 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a 20 seen = 1;.
20b1a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
20b1b 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 63 prior_c = c
20b1c 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 2;. }.
20b1d 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 c2 = sqlite
20b1e 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 3Utf8Read(zPatte
20b1f 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72 6e rn, 0, &zPattern
20b20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
20b21 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 73 if( c2==0 || (s
20b22 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d 30 een ^ invert)==0
20b23 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
20b24 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 0;. }.
20b25 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73 63 3d }else if( esc=
20b26 3d 63 20 26 26 20 21 70 72 65 76 45 73 63 61 70 =c && !prevEscap
20b27 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65 76 45 e ){. prevE
20b28 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20 20 7d scape = 1;. }
20b29 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32 20 3d else{. c2 =
20b2a 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
20b2b 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 (zString, 0, &zS
20b2c 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 tring);. if
20b2d 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 ( noCase ){.
20b2e 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c GlogUpperToL
20b2f 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20 20 ower(c);.
20b30 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 GlogUpperToLowe
20b31 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 r(c2);. }.
20b32 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 20 29 if( c!=c2 )
20b33 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
20b34 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
20b35 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 prevEscape = 0
20b36 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
20b37 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d 30 turn *zString==0
20b38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 ;.}../*.** Count
20b39 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 the number of t
20b3a 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c 49 imes that the LI
20b3b 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72 20 KE operator (or
20b3c 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a 2a GLOB which is.**
20b3d 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 69 6f just a variatio
20b3e 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73 20 n of LIKE) gets
20b3f 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73 called. This is
20b40 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
20b41 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 g.** only..*/.#i
20b42 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
20b43 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
20b44 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 sqlite3_like_cou
20b45 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
20b46 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
20b47 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b ation of the lik
20b48 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e e() SQL function
20b49 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e . This function
20b4a 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 implements.** t
20b4b 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 he build-in LIKE
20b4c 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 operator. The
20b4d 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
20b4e 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 o the function i
20b4f 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e s the.** pattern
20b50 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 and the second
20b51 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
20b52 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 string. So, the
20b53 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a SQL statements:
20b54 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c .**.** A L
20b55 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 IKE B.**.** is i
20b56 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 mplemented as li
20b57 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 54 ke(B,A)..**.** T
20b58 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74 69 6f his same functio
20b59 6e 20 28 77 69 74 68 20 61 20 64 69 66 66 65 72 n (with a differ
20b5a 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 ent compareInfo
20b5b 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d 70 75 structure) compu
20b5c 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42 20 tes.** the GLOB
20b5d 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 operator..*/.sta
20b5e 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 75 6e tic void likeFun
20b5f 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
20b60 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
20b61 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 int argc, . s
20b62 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
20b63 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 rgv.){. const u
20b64 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 nsigned char *zA
20b65 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 73 63 , *zB;. int esc
20b66 61 70 65 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 ape = 0;. sqlit
20b67 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 e3 *db = sqlite3
20b68 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
20b69 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 le(context);..
20b6a 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c zB = sqlite3_val
20b6b 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
20b6c 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 65 33 ;. zA = sqlite3
20b6d 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
20b6e 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 [1]);.. /* Limi
20b6f 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 t the length of
20b70 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 the LIKE or GLOB
20b71 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 6f 69 pattern to avoi
20b72 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 d problems. **
20b73 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 69 6f of deep recursio
20b74 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69 n and N*N behavi
20b75 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d or in patternCom
20b76 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 pare().. */. i
20b77 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 f( sqlite3_value
20b78 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20 _bytes(argv[0])
20b79 3e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c >. db->aL
20b7a 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
20b7b 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c T_LIKE_PATTERN_L
20b7c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 ENGTH] ){. sq
20b7d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
20b7e 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b or(context, "LIK
20b7f 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 E or GLOB patter
20b80 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 n too complex",
20b81 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b -1);. return;
20b82 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a . }. assert( z
20b83 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 B==sqlite3_value
20b84 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 _text(argv[0]) )
20b85 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 64 ; /* Encoding d
20b86 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 20 2a 2f id not change */
20b87 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 .. if( argc==3
20b88 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73 ){. /* The es
20b89 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20 73 cape character s
20b8a 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 tring must consi
20b8b 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 st of a single U
20b8c 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a TF-8 character..
20b8d 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 ** Otherwise
20b8e 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f , return an erro
20b8f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f r.. */. co
20b90 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
20b91 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69 74 65 r *zEsc = sqlite
20b92 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
20b93 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a v[2]);. if( z
20b94 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b Esc==0 ) return;
20b95 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
20b96 55 74 66 38 43 68 61 72 4c 65 6e 28 28 63 68 61 Utf8CharLen((cha
20b97 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d 31 20 r*)zEsc, -1)!=1
20b98 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
20b99 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f _result_error(co
20b9a 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 ntext, .
20b9b 20 20 22 45 53 43 41 50 45 20 65 78 70 72 65 73 "ESCAPE expres
20b9c 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 20 73 sion must be a s
20b9d 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 22 ingle character"
20b9e 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 , -1);. ret
20b9f 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 urn;. }. e
20ba0 73 63 61 70 65 20 3d 20 73 71 6c 69 74 65 33 55 scape = sqlite3U
20ba1 74 66 38 52 65 61 64 28 7a 45 73 63 2c 20 30 2c tf8Read(zEsc, 0,
20ba2 20 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20 20 69 &zEsc);. }. i
20ba3 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20 f( zA && zB ){.
20ba4 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 struct compar
20ba5 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 73 eInfo *pInfo = s
20ba6 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
20ba7 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66 64 65 (context);.#ifde
20ba8 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
20ba9 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 sqlite3_like_c
20baa 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 ount++;.#endif.
20bab 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f . sqlite3_
20bac 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 result_int(conte
20bad 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 xt, patternCompa
20bae 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e 66 6f re(zB, zA, pInfo
20baf 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20 7d 0a , escape));. }.
20bb0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
20bb1 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4e ntation of the N
20bb2 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e 63 74 ULLIF(x,y) funct
20bb3 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 ion. The result
20bb4 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a is the first.**
20bb5 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 68 65 argument if the
20bb6 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 64 arguments are d
20bb7 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65 20 72 ifferent. The r
20bb8 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 69 66 esult is NULL if
20bb9 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 the.** argument
20bba 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f 20 65 s are equal to e
20bbb 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 73 74 ach other..*/.st
20bbc 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c 69 66 atic void nullif
20bbd 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
20bbe 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
20bbf 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
20bc0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
20bc1 61 72 67 76 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 argv.){. CollSe
20bc2 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 q *pColl = sqlit
20bc3 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 e3GetFuncCollSeq
20bc4 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 (context);. if(
20bc5 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 sqlite3MemCompa
20bc6 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72 67 76 re(argv[0], argv
20bc7 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 20 29 [1], pColl)!=0 )
20bc8 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 {. sqlite3_re
20bc9 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 sult_value(conte
20bca 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 xt, argv[0]);.
20bcb 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 }.}../*.** Imple
20bcc 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
20bcd 20 56 45 52 53 49 4f 4e 28 2a 29 20 66 75 6e 63 VERSION(*) func
20bce 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c tion. The resul
20bcf 74 20 69 73 20 74 68 65 20 76 65 72 73 69 6f 6e t is the version
20bd0 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 69 74 .** of the SQLit
20bd1 65 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69 e library that i
20bd2 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 s running..*/.st
20bd3 61 74 69 63 20 76 6f 69 64 20 76 65 72 73 69 6f atic void versio
20bd4 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 nFunc(. sqlite3
20bd5 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
20bd6 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
20bd7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
20bd8 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 *argv.){. sqlit
20bd9 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 e3_result_text(c
20bda 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f ontext, sqlite3_
20bdb 76 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53 51 4c version, -1, SQL
20bdc 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a ITE_STATIC);.}..
20bdd 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63 6f 6e /* Array for con
20bde 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68 61 6c verting from hal
20bdf 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c 65 73 f-bytes (nybbles
20be0 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68 65 78 ) into ASCII hex
20be1 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f 0a 73 .** digits. */.s
20be2 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
20be3 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d 20 7b hexdigits[] = {
20be4 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27 32 27 . '0', '1', '2'
20be5 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35 27 2c , '3', '4', '5',
20be6 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27 38 27 '6', '7',. '8'
20be7 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42 27 2c , '9', 'A', 'B',
20be8 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c 20 'C', 'D', 'E',
20be9 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 'F' .};../*.** E
20bea 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 54 68 XPERIMENTAL - Th
20beb 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f 66 66 is is not an off
20bec 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 2e 20 icial function.
20bed 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d The interface m
20bee 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20 20 54 ay.** change. T
20bef 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
20bf0 20 64 69 73 61 70 70 65 61 72 2e 20 20 44 6f 20 disappear. Do
20bf1 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65 20 74 not write code t
20bf2 68 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f hat depends.** o
20bf3 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e n this function.
20bf4 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 .**.** Implement
20bf5 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51 55 4f ation of the QUO
20bf6 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 TE() function.
20bf7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 61 This function ta
20bf8 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 kes a single.**
20bf9 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 argument. If th
20bfa 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 75 e argument is nu
20bfb 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74 75 72 meric, the retur
20bfc 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73 n value is the s
20bfd 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20 61 72 ame as.** the ar
20bfe 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 gument. If the
20bff 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c argument is NULL
20c00 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c , the return val
20c01 75 65 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 ue is the string
20c02 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f 74 68 .** "NULL". Oth
20c03 65 72 77 69 73 65 2c 20 74 68 65 20 61 72 67 75 erwise, the argu
20c04 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73 65 64 ment is enclosed
20c05 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 in single quote
20c06 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67 6c 65 s with.** single
20c07 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73 2e 0a -quote escapes..
20c08 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71 */.static void q
20c09 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33 uoteFunc(sqlite3
20c0a 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
20c0b 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c t, int argc, sql
20c0c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
20c0d 76 29 7b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 v){. if( argc<1
20c0e 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 77 69 ) return;. swi
20c0f 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c tch( sqlite3_val
20c10 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
20c11 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
20c12 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 ITE_NULL: {.
20c13 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
20c14 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 _text(context, "
20c15 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54 45 NULL", 4, SQLITE
20c16 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 _STATIC);.
20c17 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
20c18 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 case SQLITE_INT
20c19 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 53 EGER:. case S
20c1a 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 QLITE_FLOAT: {.
20c1b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
20c1c 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 ult_value(contex
20c1d 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 t, argv[0]);.
20c1e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
20c1f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
20c20 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63 68 BLOB: {. ch
20c21 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20 ar *zText = 0;.
20c22 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 char const
20c23 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 *zBlob = sqlite3
20c24 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 _value_blob(argv
20c25 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 [0]);. int
20c26 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f nBlob = sqlite3_
20c27 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
20c28 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 [0]);. asse
20c29 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69 74 rt( zBlob==sqlit
20c2a 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 e3_value_blob(ar
20c2b 67 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f 20 gv[0]) ); /* No
20c2c 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 encoding change
20c2d 2a 2f 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d */. zText =
20c2e 20 28 63 68 61 72 20 2a 29 63 6f 6e 74 65 78 74 (char *)context
20c2f 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 Malloc(context,
20c30 28 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b 34 (2*(i64)nBlob)+4
20c31 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a 54 ); . if( zT
20c32 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ext ){. i
20c33 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f nt i;. fo
20c34 72 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b 20 r(i=0; i<nBlob;
20c35 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 i++){.
20c36 7a 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20 3d zText[(i*2)+2] =
20c37 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f hexdigits[(zBlo
20c38 62 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b 0a b[i]>>4)&0x0F];.
20c39 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b zText[
20c3a 28 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64 69 (i*2)+3] = hexdi
20c3b 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29 26 gits[(zBlob[i])&
20c3c 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0x0F];. }
20c3d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 . zText[(
20c3e 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 5c nBlob*2)+2] = '\
20c3f 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 '';. zTex
20c40 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 3d t[(nBlob*2)+3] =
20c41 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7a '\0';. z
20c42 54 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a 20 Text[0] = 'X';.
20c43 20 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d 20 zText[1]
20c44 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 = '\'';.
20c45 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
20c46 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54 65 ext(context, zTe
20c47 78 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 xt, -1, SQLITE_T
20c48 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 RANSIENT);.
20c49 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
20c4a 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a zText);. }.
20c4b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
20c4c 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 }. case SQLI
20c4d 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 TE_TEXT: {.
20c4e 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20 20 int i,j;.
20c4f 75 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e u64 n;. con
20c50 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
20c51 20 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 33 *zArg = sqlite3
20c52 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
20c53 5b 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61 72 [0]);. char
20c54 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 *z;.. if(
20c55 7a 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72 6e zArg==0 ) return
20c56 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c ;. for(i=0,
20c57 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 n=0; zArg[i]; i
20c58 2b 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 5d ++){ if( zArg[i]
20c59 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d 0a =='\'' ) n++; }.
20c5a 20 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 78 z = contex
20c5b 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c tMalloc(context,
20c5c 20 28 28 69 36 34 29 69 29 2b 28 28 69 36 34 29 ((i64)i)+((i64)
20c5d 6e 29 2b 33 29 3b 0a 20 20 20 20 20 20 69 66 28 n)+3);. if(
20c5e 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b z ){. z[
20c5f 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 0] = '\'';.
20c60 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31 3b for(i=0, j=1;
20c61 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a zArg[i]; i++){.
20c62 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d z[j++]
20c63 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20 = zArg[i];.
20c64 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b 69 if( zArg[i
20c65 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 ]=='\'' ){.
20c66 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 z[j++] =
20c67 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '\'';.
20c68 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
20c69 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 z[j++] = '\'
20c6a 27 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d 20 ';. z[j]
20c6b 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c = 0;. sql
20c6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
20c6d 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c 20 (context, z, j,
20c6e 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 sqlite3_free);.
20c6f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
20c70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 .}../*.** The he
20c71 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 x() function. I
20c72 6e 74 65 72 70 72 65 74 20 74 68 65 20 61 72 67 nterpret the arg
20c73 75 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62 2e ument as a blob.
20c74 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68 65 Return.** a he
20c75 78 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65 72 xadecimal render
20c76 69 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f 0a ing as text..*/.
20c77 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 78 46 static void hexF
20c78 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
20c79 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
20c7a 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
20c7b 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
20c7c 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 rgv.){. int i,
20c7d 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 n;. const unsig
20c7e 6e 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62 3b ned char *pBlob;
20c7f 0a 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20 2a . char *zHex, *
20c80 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 z;. assert( arg
20c81 63 3d 3d 31 20 29 3b 0a 20 20 70 42 6c 6f 62 20 c==1 );. pBlob
20c82 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
20c83 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 blob(argv[0]);.
20c84 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c n = sqlite3_val
20c85 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d ue_bytes(argv[0]
20c86 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 6c );. assert( pBl
20c87 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 ob==sqlite3_valu
20c88 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20 e_blob(argv[0])
20c89 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 ); /* No encodi
20c8a 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a ng change */. z
20c8b 20 3d 20 7a 48 65 78 20 3d 20 63 6f 6e 74 65 78 = zHex = contex
20c8c 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c tMalloc(context,
20c8d 20 28 28 69 36 34 29 6e 29 2a 32 20 2b 20 31 29 ((i64)n)*2 + 1)
20c8e 3b 0a 20 20 69 66 28 20 7a 48 65 78 20 29 7b 0a ;. if( zHex ){.
20c8f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
20c90 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b 29 7b ; i++, pBlob++){
20c91 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
20c92 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f 62 3b char c = *pBlob;
20c93 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 . *(z++) =
20c94 68 65 78 64 69 67 69 74 73 5b 28 63 3e 3e 34 29 hexdigits[(c>>4)
20c95 26 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a 28 7a &0xf];. *(z
20c96 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b ++) = hexdigits[
20c97 63 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a 20 20 c&0xf];. }.
20c98 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20 73 71 *z = 0;. sq
20c99 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
20c9a 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65 78 2c t(context, zHex,
20c9b 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f 66 72 n*2, sqlite3_fr
20c9c 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ee);. }.}../*.*
20c9d 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62 28 4e * The zeroblob(N
20c9e 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 ) function retur
20c9f 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 ns a zero-filled
20ca0 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20 4e 20 blob of size N
20ca1 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 bytes..*/.static
20ca2 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62 46 75 void zeroblobFu
20ca3 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
20ca4 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
20ca5 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
20ca6 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
20ca7 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b 0a 20 gv.){. i64 n;.
20ca8 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 assert( argc==1
20ca9 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 );. n = sqlite
20caa 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 3_value_int64(ar
20cab 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3e gv[0]);. if( n>
20cac 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 SQLITE_MAX_LENGT
20cad 48 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 H ){. sqlite3
20cae 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f _result_error_to
20caf 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 obig(context);.
20cb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
20cb1 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 te3_result_zerob
20cb2 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b lob(context, n);
20cb3 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
20cb4 65 20 72 65 70 6c 61 63 65 28 29 20 66 75 6e 63 e replace() func
20cb5 74 69 6f 6e 2e 20 20 54 68 72 65 65 20 61 72 67 tion. Three arg
20cb6 75 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c 20 73 uments are all s
20cb7 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20 trings: call.**
20cb8 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64 20 43 them A, B, and C
20cb9 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 . The result is
20cba 61 6c 73 6f 20 61 20 73 74 72 69 6e 67 20 77 68 also a string wh
20cbb 69 63 68 20 69 73 20 64 65 72 69 76 65 64 0a 2a ich is derived.*
20cbc 2a 20 66 72 6f 6d 20 41 20 62 79 20 72 65 70 6c * from A by repl
20cbd 61 63 69 6e 67 20 65 76 65 72 79 20 6f 63 63 75 acing every occu
20cbe 72 61 6e 63 65 20 6f 66 20 42 20 77 69 74 68 20 rance of B with
20cbf 43 2e 20 20 54 68 65 20 6d 61 74 63 68 0a 2a 2a C. The match.**
20cc0 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 2e 20 must be exact.
20cc1 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 Collating seque
20cc2 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 75 73 65 nces are not use
20cc3 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
20cc4 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28 0a 20 d replaceFunc(.
20cc5 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
20cc6 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
20cc7 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
20cc8 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
20cc9 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
20cca 64 20 63 68 61 72 20 2a 7a 53 74 72 3b 20 20 20 d char *zStr;
20ccb 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 /* The inpu
20ccc 74 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a 20 20 t string A */.
20ccd 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
20cce 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b 20 20 har *zPattern;
20ccf 20 20 2f 2a 20 54 68 65 20 70 61 74 74 65 72 6e /* The pattern
20cd0 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20 20 63 string B */. c
20cd1 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
20cd2 61 72 20 2a 7a 52 65 70 3b 20 20 20 20 20 20 20 ar *zRep;
20cd3 20 2f 2a 20 54 68 65 20 72 65 70 6c 61 63 65 6d /* The replacem
20cd4 65 6e 74 20 73 74 72 69 6e 67 20 43 20 2a 2f 0a ent string C */.
20cd5 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
20cd6 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 *zOut;
20cd7 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 /* The outpu
20cd8 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 72 3b t */. int nStr;
20cd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20cda 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74 72 20 /* Size of zStr
20cdb 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72 */. int nPatter
20cdc 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a n; /*
20cdd 20 53 69 7a 65 20 6f 66 20 7a 50 61 74 74 65 72 Size of zPatter
20cde 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 70 3b n */. int nRep;
20cdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20ce0 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65 70 20 /* Size of zRep
20ce1 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b 20 20 */. i64 nOut;
20ce2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20ce3 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 Maximum size of
20ce4 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6c zOut */. int l
20ce5 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 oopLimit;
20ce6 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72 /* Last zStr
20ce7 5b 5d 20 74 68 61 74 20 6d 69 67 68 74 20 6d 61 [] that might ma
20ce8 74 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a tch zPattern[] *
20ce9 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 /. int i, j;
20cea 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20ceb 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f Loop counters */
20cec 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 .. assert( argc
20ced 3d 3d 33 20 29 3b 0a 20 20 7a 53 74 72 20 3d 20 ==3 );. zStr =
20cee 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
20cef 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 xt(argv[0]);. i
20cf0 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 74 f( zStr==0 ) ret
20cf1 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 71 urn;. nStr = sq
20cf2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
20cf3 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 s(argv[0]);. as
20cf4 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69 sert( zStr==sqli
20cf5 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
20cf6 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e rgv[0]) ); /* N
20cf7 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 o encoding chang
20cf8 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e 20 e */. zPattern
20cf9 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
20cfa 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 text(argv[1]);.
20cfb 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d 30 if( zPattern==0
20cfc 20 7c 7c 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d || zPattern[0]=
20cfd 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e =0 ) return;. n
20cfe 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 Pattern = sqlite
20cff 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
20d00 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65 72 74 gv[1]);. assert
20d01 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71 6c 69 ( zPattern==sqli
20d02 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
20d03 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a 20 4e rgv[1]) ); /* N
20d04 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 o encoding chang
20d05 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20 73 71 e */. zRep = sq
20d06 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
20d07 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66 28 (argv[2]);. if(
20d08 20 7a 52 65 70 3d 3d 30 20 29 20 72 65 74 75 72 zRep==0 ) retur
20d09 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71 6c 69 n;. nRep = sqli
20d0a 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
20d0b 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73 73 65 argv[2]);. asse
20d0c 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69 74 65 rt( zRep==sqlite
20d0d 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
20d0e 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75 74 20 v[2]) );. nOut
20d0f 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20 61 73 = nStr + 1;. as
20d10 73 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c 49 54 sert( nOut<SQLIT
20d11 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a E_MAX_LENGTH );.
20d12 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78 74 zOut = context
20d13 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 Malloc(context,
20d14 28 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20 69 66 (i64)nOut);. if
20d15 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 ( zOut==0 ){.
20d16 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c return;. }. l
20d17 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74 72 20 oopLimit = nStr
20d18 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a 20 20 - nPattern; .
20d19 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f for(i=j=0; i<=lo
20d1a 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 opLimit; i++){.
20d1b 20 20 20 69 66 28 20 7a 53 74 72 5b 69 5d 21 3d if( zStr[i]!=
20d1c 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d zPattern[0] || m
20d1d 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d 2c 20 emcmp(&zStr[i],
20d1e 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61 74 74 65 zPattern, nPatte
20d1f 72 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 rn) ){. zOu
20d20 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d t[j++] = zStr[i]
20d21 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
20d22 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20 u8 *zOld;.
20d23 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d sqlite3 *db =
20d24 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
20d25 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 _db_handle(conte
20d26 78 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75 74 20 xt);. nOut
20d27 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74 74 65 += nRep - nPatte
20d28 72 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f rn;. if( nO
20d29 75 74 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 ut>=db->aLimit[S
20d2a 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
20d2b 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 TH] ){. s
20d2c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
20d2d 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 ror_toobig(conte
20d2e 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c xt);. sql
20d2f 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
20d30 4f 75 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 Out);. re
20d31 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 turn;. }.
20d32 20 20 20 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b zOld = zOut;
20d33 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 . zOut = sq
20d34 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f lite3_realloc(zO
20d35 75 74 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a ut, (int)nOut);.
20d36 20 20 20 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d if( zOut==
20d37 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 0 ){. sql
20d38 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
20d39 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 r_nomem(context)
20d3a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
20d3b 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 6c 64 3DbFree(db, zOld
20d3c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
20d3d 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 n;. }.
20d3e 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d memcpy(&zOut[j]
20d3f 2c 20 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20 , zRep, nRep);.
20d40 20 20 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a j += nRep;.
20d41 20 20 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74 i += nPatt
20d42 65 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d ern-1;. }. }
20d43 0a 20 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74 . assert( j+nSt
20d44 72 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 r-i+1==nOut );.
20d45 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d memcpy(&zOut[j]
20d46 2c 20 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72 , &zStr[i], nStr
20d47 2d 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 -i);. j += nStr
20d48 20 2d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 - i;. assert(
20d49 6a 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 j<=nOut );. zOu
20d4a 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 t[j] = 0;. sqli
20d4b 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
20d4c 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 context, (char*)
20d4d 7a 4f 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 zOut, j, sqlite3
20d4e 5f 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _free);.}../*.**
20d4f 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
20d50 6f 66 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c of the TRIM(), L
20d51 54 52 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49 TRIM(), and RTRI
20d52 4d 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a M() functions..*
20d53 2a 20 54 68 65 20 75 73 65 72 64 61 74 61 20 69 * The userdata i
20d54 73 20 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74 s 0x1 for left t
20d55 72 69 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67 rim, 0x2 for rig
20d56 68 74 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72 ht trim, 0x3 for
20d57 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 both..*/.static
20d58 20 76 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a void trimFunc(.
20d59 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
20d5a 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
20d5b 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
20d5c 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
20d5d 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e {. const unsign
20d5e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 ed char *zIn;
20d5f 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 /* Input s
20d60 74 72 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 tring */. const
20d61 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
20d62 7a 43 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20 zCharSet; /*
20d63 53 65 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 Set of character
20d64 73 20 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69 s to trim */. i
20d65 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 nt nIn;
20d66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d67 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
20d68 74 65 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a tes in input */.
20d69 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
20d6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d6b 20 20 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 /* 1: trimle
20d6c 66 74 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 ft 2: trimright
20d6d 20 20 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69 3: trim */. i
20d6e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
20d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d70 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
20d71 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
20d72 68 61 72 20 2a 61 4c 65 6e 3b 20 20 20 20 20 20 har *aLen;
20d73 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 /* Lengt
20d74 68 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63 h of each charac
20d75 74 65 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20 ter in zCharSet
20d76 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
20d77 61 72 20 2a 2a 61 7a 43 68 61 72 3b 20 20 20 20 ar **azChar;
20d78 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69 /* Indivi
20d79 64 75 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 dual characters
20d7a 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 in zCharSet */.
20d7b 20 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20 int nChar;
20d7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d7d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
20d7e 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 characters in zC
20d7f 68 61 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28 harSet */.. if(
20d80 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
20d81 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 ype(argv[0])==SQ
20d82 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 LITE_NULL ){.
20d83 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a return;. }. z
20d84 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c In = sqlite3_val
20d85 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
20d86 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 ;. if( zIn==0 )
20d87 20 72 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d return;. nIn =
20d88 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
20d89 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 ytes(argv[0]);.
20d8a 20 61 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71 assert( zIn==sq
20d8b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
20d8c 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 (argv[0]) );. i
20d8d 66 28 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 f( argc==1 ){.
20d8e 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
20d8f 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e nsigned char len
20d90 4f 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 One[] = { 1 };.
20d91 20 20 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e static unsign
20d92 65 64 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 ed char * const
20d93 61 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a azOne[] = { (u8*
20d94 29 22 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61 )" " };. nCha
20d95 72 20 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20 r = 1;. aLen
20d96 3d 20 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 = (u8*)lenOne;.
20d97 20 20 20 61 7a 43 68 61 72 20 3d 20 28 75 6e 73 azChar = (uns
20d98 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 29 61 7a igned char **)az
20d99 4f 6e 65 3b 0a 20 20 20 20 7a 43 68 61 72 53 65 One;. zCharSe
20d9a 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 t = 0;. }else i
20d9b 66 28 20 28 7a 43 68 61 72 53 65 74 20 3d 20 73 f( (zCharSet = s
20d9c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
20d9d 74 28 61 72 67 76 5b 31 5d 29 29 3d 3d 30 20 29 t(argv[1]))==0 )
20d9e 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
20d9f 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 }else{. const
20da0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
20da1 7a 3b 0a 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 z;. for(z=zCh
20da2 61 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 arSet, nChar=0;
20da3 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 *z; nChar++){.
20da4 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f SQLITE_SKIP_
20da5 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 UTF8(z);. }.
20da6 20 20 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 if( nChar>0 )
20da7 7b 0a 20 20 20 20 20 20 61 7a 43 68 61 72 20 3d {. azChar =
20da8 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 contextMalloc(c
20da9 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 43 ontext, ((i64)nC
20daa 68 61 72 29 2a 28 73 69 7a 65 6f 66 28 63 68 61 har)*(sizeof(cha
20dab 72 2a 29 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 r*)+1));. i
20dac 66 28 20 61 7a 43 68 61 72 3d 3d 30 20 29 7b 0a f( azChar==0 ){.
20dad 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
20dae 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4c }. aL
20daf 65 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 en = (unsigned c
20db0 68 61 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68 har*)&azChar[nCh
20db1 61 72 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 7a ar];. for(z
20db2 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 =zCharSet, nChar
20db3 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 =0; *z; nChar++)
20db4 7b 0a 20 20 20 20 20 20 20 20 61 7a 43 68 61 72 {. azChar
20db5 5b 6e 43 68 61 72 5d 20 3d 20 28 75 6e 73 69 67 [nChar] = (unsig
20db6 6e 65 64 20 63 68 61 72 20 2a 29 7a 3b 0a 20 20 ned char *)z;.
20db7 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 SQLITE_SKI
20db8 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 P_UTF8(z);.
20db9 20 20 20 61 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d aLen[nChar] =
20dba 20 7a 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 z - azChar[nCha
20dbb 72 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 r];. }.
20dbc 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 }. }. if( nCha
20dbd 72 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 r>0 ){. flags
20dbe 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f = SQLITE_PTR_TO
20dbf 5f 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 _INT(sqlite3_use
20dc0 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 r_data(context))
20dc1 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 ;. if( flags
20dc2 26 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 & 1 ){. whi
20dc3 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 le( nIn>0 ){.
20dc4 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 int len;.
20dc5 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
20dc6 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 <nChar; i++){.
20dc7 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c len = aL
20dc8 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 en[i];.
20dc9 20 69 66 28 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c if( memcmp(zIn,
20dca 20 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 azChar[i], len)
20dcb 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ==0 ) break;.
20dcc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
20dcd 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 f( i>=nChar ) br
20dce 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e eak;. zIn
20dcf 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 += len;.
20dd0 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 nIn -= len;.
20dd1 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
20dd2 66 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a f( flags & 2 ){.
20dd3 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e while( nIn
20dd4 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e >0 ){. in
20dd5 74 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 66 t len;. f
20dd6 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b or(i=0; i<nChar;
20dd7 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 i++){.
20dd8 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a len = aLen[i];.
20dd9 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65 if( le
20dda 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 n<=nIn && memcmp
20ddb 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61 (&zIn[nIn-len],a
20ddc 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30 zChar[i],len)==0
20ddd 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
20dde 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
20ddf 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b i>=nChar ) break
20de0 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d ;. nIn -=
20de1 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 len;. }.
20de2 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 68 61 }. if( zCha
20de3 72 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71 rSet ){. sq
20de4 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 68 61 lite3_free(azCha
20de5 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 r);. }. }.
20de6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
20de7 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 ext(context, (ch
20de8 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51 ar*)zIn, nIn, SQ
20de9 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
20dea 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 .}...#ifdef SQLI
20deb 54 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a TE_SOUNDEX./*.**
20dec 20 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 Compute the sou
20ded 6e 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 ndex encoding of
20dee 20 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 a word..*/.stat
20def 69 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 78 46 ic void soundexF
20df0 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
20df1 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
20df2 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
20df3 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
20df4 72 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 7a 52 rgv.){. char zR
20df5 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e 73 esult[8];. cons
20df6 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74 t u8 *zIn;. int
20df7 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 63 20 i, j;. static
20df8 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
20df9 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 7b 0a har iCode[] = {.
20dfa 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0,
20dfb 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
20dfc 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
20dfd 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 0,. 0, 0, 0,
20dfe 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
20dff 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
20e00 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 , 0, 0,. 0, 0
20e01 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
20e02 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
20e03 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 0, 0, 0, 0,.
20e04 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
20e05 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
20e06 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 0, 0, 0, 0, 0,.
20e07 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 0, 0, 1, 2,
20e08 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 3, 0, 1, 2, 0, 0
20e09 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c , 2, 2, 4, 5, 5,
20e0a 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 0,. 1, 2, 6,
20e0b 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 2, 3, 0, 1, 0,
20e0c 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2, 0, 2, 0, 0, 0
20e0d 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 , 0, 0,. 0, 0
20e0e 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c , 1, 2, 3, 0, 1,
20e0f 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 2, 0, 0, 2, 2,
20e10 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 4, 5, 5, 0,.
20e11 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 1, 2, 6, 2, 3, 0
20e12 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c , 1, 0, 2, 0, 2,
20e13 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 0, 0, 0, 0, 0,.
20e14 20 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20 61 };. assert( a
20e15 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e 20 rgc==1 );. zIn
20e16 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 5f 76 = (u8*)sqlite3_v
20e17 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
20e18 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 ]);. if( zIn==0
20e19 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 22 22 ) zIn = (u8*)""
20e1a 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 49 6e ;. for(i=0; zIn
20e1b 5b 69 5d 20 26 26 20 21 69 73 61 6c 70 68 61 28 [i] && !isalpha(
20e1c 7a 49 6e 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a zIn[i]); i++){}.
20e1d 20 20 69 66 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a if( zIn[i] ){.
20e1e 20 20 20 20 75 38 20 70 72 65 76 63 6f 64 65 20 u8 prevcode
20e1f 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 = iCode[zIn[i]&0
20e20 78 37 66 5d 3b 0a 20 20 20 20 7a 52 65 73 75 6c x7f];. zResul
20e21 74 5b 30 5d 20 3d 20 74 6f 75 70 70 65 72 28 7a t[0] = toupper(z
20e22 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28 In[i]);. for(
20e23 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b j=1; j<4 && zIn[
20e24 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 i]; i++){.
20e25 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65 int code = iCode
20e26 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 [zIn[i]&0x7f];.
20e27 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20 if( code>0
20e28 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 ){. if( c
20e29 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b ode!=prevcode ){
20e2a 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63 . prevc
20e2b 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20 ode = code;.
20e2c 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b zResult[j+
20e2d 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b +] = code + '0';
20e2e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
20e2f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
20e30 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20 prevcode = 0;.
20e31 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
20e32 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20 while( j<4 ){.
20e33 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d zResult[j++]
20e34 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20 = '0';. }.
20e35 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30 zResult[j] = 0
20e36 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
20e37 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
20e38 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53 t, zResult, 4, S
20e39 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
20e3a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
20e3b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
20e3c 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30 xt(context, "?00
20e3d 30 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 0", 4, SQLITE_ST
20e3e 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e ATIC);. }.}.#en
20e3f 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
20e40 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
20e41 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 TENSION./*.** A
20e42 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f function that lo
20e43 61 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69 62 ads a shared-lib
20e44 72 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20 74 rary extension t
20e45 68 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c hen returns NULL
20e46 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
20e47 20 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65 33 loadExt(sqlite3
20e48 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
20e49 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c t, int argc, sql
20e4a 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
20e4b 76 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 v){. const char
20e4c 20 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 *zFile = (const
20e4d 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f char *)sqlite3_
20e4e 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
20e4f 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 0]);. const cha
20e50 72 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69 r *zProc;. sqli
20e51 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 te3 *db = sqlite
20e52 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
20e53 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
20e54 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 char *zErrMsg =
20e55 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 0;.. if( argc==
20e56 32 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 2 ){. zProc =
20e57 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 (const char *)s
20e58 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
20e59 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65 t(argv[1]);. }e
20e5a 6c 73 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d lse{. zProc =
20e5b 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46 0;. }. if( zF
20e5c 69 6c 65 20 26 26 20 73 71 6c 69 74 65 33 5f 6c ile && sqlite3_l
20e5d 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62 oad_extension(db
20e5e 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 , zFile, zProc,
20e5f 26 7a 45 72 72 4d 73 67 29 20 29 7b 0a 20 20 20 &zErrMsg) ){.
20e60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
20e61 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a error(context, z
20e62 45 72 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20 ErrMsg, -1);.
20e63 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 sqlite3_free(zE
20e64 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65 rrMsg);. }.}.#e
20e65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 ndif.../*.** An
20e66 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
20e67 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
20e68 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63 6f ure holds the co
20e69 6e 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75 ntext of a.** su
20e6a 6d 28 29 20 6f 72 20 61 76 67 28 29 20 61 67 67 m() or avg() agg
20e6b 72 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74 69 regate computati
20e6c 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 on..*/.typedef s
20e6d 74 72 75 63 74 20 53 75 6d 43 74 78 20 53 75 6d truct SumCtx Sum
20e6e 43 74 78 3b 0a 73 74 72 75 63 74 20 53 75 6d 43 Ctx;.struct SumC
20e6f 74 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53 tx {. double rS
20e70 75 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61 um; /* Floa
20e71 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a ting point sum *
20e72 2f 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 20 20 /. i64 iSum;
20e73 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 /* Integer
20e74 20 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34 sum */ . i64
20e75 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f cnt; /
20e76 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d * Number of elem
20e77 65 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20 ents summed */.
20e78 20 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20 u8 overflow;
20e79 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e /* True if in
20e7a 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 73 teger overflow s
20e7b 65 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 70 72 een */. u8 appr
20e7c 6f 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ox; /* Tr
20e7d 75 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65 ue if non-intege
20e7e 72 20 76 61 6c 75 65 20 77 61 73 20 69 6e 70 75 r value was inpu
20e7f 74 20 74 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a t to the sum */.
20e80 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e };../*.** Routin
20e81 65 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 es used to compu
20e82 74 65 20 74 68 65 20 73 75 6d 2c 20 61 76 65 72 te the sum, aver
20e83 61 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a age, and total..
20e84 2a 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29 20 **.** The SUM()
20e85 66 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73 function follows
20e86 20 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 53 51 the (broken) SQ
20e87 4c 20 73 74 61 6e 64 61 72 64 20 77 68 69 63 68 L standard which
20e88 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 means.** that i
20e89 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 t returns NULL i
20e8a 66 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20 6e f it sums over n
20e8b 6f 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c o inputs. TOTAL
20e8c 20 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20 returns.** 0.0
20e8d 69 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20 49 in that case. I
20e8e 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41 n addition, TOTA
20e8f 4c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 L always returns
20e90 20 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a a float where.*
20e91 2a 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74 75 * SUM might retu
20e92 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66 rn an integer if
20e93 20 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e it never encoun
20e94 74 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 ters a floating
20e95 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 point.** value.
20e96 20 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 61 69 TOTAL never fai
20e97 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 67 68 ls, but SUM migh
20e98 74 20 74 68 72 6f 75 67 68 20 61 6e 20 65 78 63 t through an exc
20e99 65 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20 eption if.** it
20e9a 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74 overflows an int
20e9b 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eger..*/.static
20e9c 76 6f 69 64 20 73 75 6d 53 74 65 70 28 73 71 6c void sumStep(sql
20e9d 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
20e9e 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c ntext, int argc,
20e9f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
20ea0 2a 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 74 78 *argv){. SumCtx
20ea1 20 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b *p;. int type;
20ea2 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d . assert( argc=
20ea3 3d 31 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 =1 );. p = sqli
20ea4 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
20ea5 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 ntext(context, s
20ea6 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 74 79 izeof(*p));. ty
20ea7 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c pe = sqlite3_val
20ea8 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 ue_numeric_type(
20ea9 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 argv[0]);. if(
20eaa 70 20 26 26 20 74 79 70 65 21 3d 53 51 4c 49 54 p && type!=SQLIT
20eab 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 2d E_NULL ){. p-
20eac 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 >cnt++;. if(
20ead 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 type==SQLITE_INT
20eae 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 69 36 EGER ){. i6
20eaf 34 20 76 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 4 v = sqlite3_va
20eb0 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 lue_int64(argv[0
20eb1 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 ]);. p->rSu
20eb2 6d 20 2b 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 m += v;. if
20eb3 28 20 28 70 2d 3e 61 70 70 72 6f 78 7c 70 2d 3e ( (p->approx|p->
20eb4 6f 76 65 72 66 6c 6f 77 29 3d 3d 30 20 29 7b 0a overflow)==0 ){.
20eb5 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65 77 i64 iNew
20eb6 53 75 6d 20 3d 20 70 2d 3e 69 53 75 6d 20 2b 20 Sum = p->iSum +
20eb7 76 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 v;. int s
20eb8 31 20 3d 20 70 2d 3e 69 53 75 6d 20 3e 3e 20 28 1 = p->iSum >> (
20eb9 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 sizeof(i64)*8-1)
20eba 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 32 ;. int s2
20ebb 20 3d 20 76 20 20 20 20 20 20 20 3e 3e 20 28 73 = v >> (s
20ebc 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 3b izeof(i64)*8-1);
20ebd 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 33 20 . int s3
20ebe 3d 20 69 4e 65 77 53 75 6d 20 3e 3e 20 28 73 69 = iNewSum >> (si
20ebf 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 3b 0a zeof(i64)*8-1);.
20ec0 20 20 20 20 20 20 20 20 70 2d 3e 6f 76 65 72 66 p->overf
20ec1 6c 6f 77 20 3d 20 28 73 31 26 73 32 26 7e 73 33 low = (s1&s2&~s3
20ec2 29 20 7c 20 28 7e 73 31 26 7e 73 32 26 73 33 29 ) | (~s1&~s2&s3)
20ec3 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 53 75 ;. p->iSu
20ec4 6d 20 3d 20 69 4e 65 77 53 75 6d 3b 0a 20 20 20 m = iNewSum;.
20ec5 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
20ec6 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d p->rSum +=
20ec7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 sqlite3_value_d
20ec8 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a ouble(argv[0]);.
20ec9 20 20 20 20 20 20 70 2d 3e 61 70 70 72 6f 78 20 p->approx
20eca 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d = 1;. }. }.}
20ecb 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d .static void sum
20ecc 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 Finalize(sqlite3
20ecd 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
20ece 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b t){. SumCtx *p;
20ecf 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 . p = sqlite3_a
20ed0 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 ggregate_context
20ed1 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 (context, 0);.
20ed2 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e if( p && p->cnt>
20ed3 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 0 ){. if( p->
20ed4 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 overflow ){.
20ed5 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
20ed6 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 22 _error(context,"
20ed7 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 integer overflow
20ed8 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ",-1);. }else
20ed9 20 69 66 28 20 70 2d 3e 61 70 70 72 6f 78 20 29 if( p->approx )
20eda 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
20edb 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f result_double(co
20edc 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 29 3b ntext, p->rSum);
20edd 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
20ede 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
20edf 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 _int64(context,
20ee0 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 20 7d 0a p->iSum);. }.
20ee1 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 }.}.static voi
20ee2 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 28 73 71 d avgFinalize(sq
20ee3 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
20ee4 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 ontext){. SumCt
20ee5 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 x *p;. p = sqli
20ee6 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
20ee7 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 ntext(context, 0
20ee8 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d );. if( p && p-
20ee9 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 >cnt>0 ){. sq
20eea 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 lite3_result_dou
20eeb 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e ble(context, p->
20eec 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70 2d 3e rSum/(double)p->
20eed 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 cnt);. }.}.stat
20eee 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 46 69 6e ic void totalFin
20eef 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f alize(sqlite3_co
20ef0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b ntext *context){
20ef1 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 . SumCtx *p;.
20ef2 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 p = sqlite3_aggr
20ef3 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f egate_context(co
20ef4 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 71 6c ntext, 0);. sql
20ef5 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 ite3_result_doub
20ef6 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 le(context, p ?
20ef7 70 2d 3e 72 53 75 6d 20 3a 20 30 2e 30 29 3b 0a p->rSum : 0.0);.
20ef8 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
20ef9 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
20efa 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 keeps track of
20efb 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f state informatio
20efc 6e 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 75 n for the.** cou
20efd 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 20 66 nt() aggregate f
20efe 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 unction..*/.type
20eff 64 65 66 20 73 74 72 75 63 74 20 43 6f 75 6e 74 def struct Count
20f00 43 74 78 20 43 6f 75 6e 74 43 74 78 3b 0a 73 74 Ctx CountCtx;.st
20f01 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 7b 0a ruct CountCtx {.
20f02 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a i64 n;.};../*.
20f03 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 69 ** Routines to i
20f04 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 mplement the cou
20f05 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 20 66 nt() aggregate f
20f06 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 unction..*/.stat
20f07 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 53 74 65 ic void countSte
20f08 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 p(sqlite3_contex
20f09 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 t *context, int
20f0a 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 argc, sqlite3_va
20f0b 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 43 lue **argv){. C
20f0c 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70 20 ountCtx *p;. p
20f0d 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 = sqlite3_aggreg
20f0e 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 ate_context(cont
20f0f 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 ext, sizeof(*p))
20f10 3b 0a 20 20 69 66 28 20 28 61 72 67 63 3d 3d 30 ;. if( (argc==0
20f11 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 || SQLITE_NULL!
20f12 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 =sqlite3_value_t
20f13 79 70 65 28 61 72 67 76 5b 30 5d 29 29 20 26 26 ype(argv[0])) &&
20f14 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 2b 2b p ){. p->n++
20f15 3b 0a 20 20 7d 0a 7d 20 20 20 0a 73 74 61 74 69 ;. }.} .stati
20f16 63 20 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e 61 c void countFina
20f17 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e lize(sqlite3_con
20f18 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a text *context){.
20f19 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 CountCtx *p;.
20f1a 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 p = sqlite3_agg
20f1b 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 regate_context(c
20f1c 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 71 ontext, 0);. sq
20f1d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
20f1e 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 64(context, p ?
20f1f 70 2d 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a p->n : 0);.}../*
20f20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 .** Routines to
20f21 69 6d 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29 20 implement min()
20f22 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65 67 and max() aggreg
20f23 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a ate functions..*
20f24 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 /.static void mi
20f25 6e 6d 61 78 53 74 65 70 28 73 71 6c 69 74 65 33 nmaxStep(sqlite3
20f26 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
20f27 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c t, int argc, sql
20f28 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
20f29 76 29 7b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 20 v){. Mem *pArg
20f2a 20 3d 20 28 4d 65 6d 20 2a 29 61 72 67 76 5b 30 = (Mem *)argv[0
20f2b 5d 3b 0a 20 20 4d 65 6d 20 2a 70 42 65 73 74 3b ];. Mem *pBest;
20f2c 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f .. if( sqlite3_
20f2d 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
20f2e 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0])==SQLITE_NULL
20f2f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 42 65 ) return;. pBe
20f30 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71 6c 69 st = (Mem *)sqli
20f31 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
20f32 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 ntext(context, s
20f33 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 29 3b 0a izeof(*pBest));.
20f34 20 20 69 66 28 20 21 70 42 65 73 74 20 29 20 72 if( !pBest ) r
20f35 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 70 42 eturn;.. if( pB
20f36 65 73 74 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 est->flags ){.
20f37 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 20 20 69 int max;. i
20f38 6e 74 20 63 6d 70 3b 0a 20 20 20 20 43 6f 6c 6c nt cmp;. Coll
20f39 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c Seq *pColl = sql
20f3a 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 ite3GetFuncCollS
20f3b 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 eq(context);.
20f3c 20 2f 2a 20 54 68 69 73 20 73 74 65 70 20 66 75 /* This step fu
20f3d 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 nction is used f
20f3e 6f 72 20 62 6f 74 68 20 74 68 65 20 6d 69 6e 28 or both the min(
20f3f 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 ) and max() aggr
20f40 65 67 61 74 65 73 2c 0a 20 20 20 20 2a 2a 20 74 egates,. ** t
20f41 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e he only differen
20f42 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 ce between the t
20f43 77 6f 20 62 65 69 6e 67 20 74 68 61 74 20 74 68 wo being that th
20f44 65 20 73 65 6e 73 65 20 6f 66 20 74 68 65 0a 20 e sense of the.
20f45 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e ** comparison
20f46 20 69 73 20 69 6e 76 65 72 74 65 64 2e 20 46 6f is inverted. Fo
20f47 72 20 74 68 65 20 6d 61 78 28 29 20 61 67 67 72 r the max() aggr
20f48 65 67 61 74 65 2c 20 74 68 65 0a 20 20 20 20 2a egate, the. *
20f49 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 * sqlite3_user_d
20f4a 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 ata() function r
20f4b 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 2d eturns (void *)-
20f4c 31 2e 20 46 6f 72 20 6d 69 6e 28 29 20 69 74 0a 1. For min() it.
20f4d 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 28 ** returns (
20f4e 76 6f 69 64 20 2a 29 64 62 2c 20 77 68 65 72 65 void *)db, where
20f4f 20 64 62 20 69 73 20 74 68 65 20 73 71 6c 69 74 db is the sqlit
20f50 65 33 2a 20 64 61 74 61 62 61 73 65 20 70 6f 69 e3* database poi
20f51 6e 74 65 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65 nter.. ** The
20f52 72 65 66 6f 72 65 20 74 68 65 20 6e 65 78 74 20 refore the next
20f53 73 74 61 74 65 6d 65 6e 74 20 73 65 74 73 20 76 statement sets v
20f54 61 72 69 61 62 6c 65 20 27 6d 61 78 27 20 74 6f ariable 'max' to
20f55 20 31 20 66 6f 72 20 74 68 65 20 6d 61 78 28 29 1 for the max()
20f56 0a 20 20 20 20 2a 2a 20 61 67 67 72 65 67 61 74 . ** aggregat
20f57 65 2c 20 6f 72 20 30 20 66 6f 72 20 6d 69 6e 28 e, or 0 for min(
20f58 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 61 ).. */. ma
20f59 78 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 x = sqlite3_user
20f5a 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 21 3d _data(context)!=
20f5b 30 3b 0a 20 20 20 20 63 6d 70 20 3d 20 73 71 6c 0;. cmp = sql
20f5c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 ite3MemCompare(p
20f5d 42 65 73 74 2c 20 70 41 72 67 2c 20 70 43 6f 6c Best, pArg, pCol
20f5e 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 6d 61 78 l);. if( (max
20f5f 20 26 26 20 63 6d 70 3c 30 29 20 7c 7c 20 28 21 && cmp<0) || (!
20f60 6d 61 78 20 26 26 20 63 6d 70 3e 30 29 20 29 7b max && cmp>0) ){
20f61 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
20f62 62 65 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c beMemCopy(pBest,
20f63 20 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 20 20 pArg);. }.
20f64 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
20f65 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42 e3VdbeMemCopy(pB
20f66 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a est, pArg);. }.
20f67 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 }.static void mi
20f68 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73 71 6c nMaxFinalize(sql
20f69 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
20f6a 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c 69 74 65 ntext){. sqlite
20f6b 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b 0a 20 3_value *pRes;.
20f6c 20 70 52 65 73 20 3d 20 28 73 71 6c 69 74 65 33 pRes = (sqlite3
20f6d 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 74 65 33 _value *)sqlite3
20f6e 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
20f6f 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a xt(context, 0);.
20f70 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a 20 20 if( pRes ){.
20f71 20 20 69 66 28 20 70 52 65 73 2d 3e 66 6c 61 67 if( pRes->flag
20f72 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 s ){. sqlit
20f73 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
20f74 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 3b 0a context, pRes);.
20f75 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
20f76 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
20f77 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a pRes);. }.}../*
20f78 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 .** group_concat
20f79 28 45 58 50 52 2c 20 3f 53 45 50 41 52 41 54 4f (EXPR, ?SEPARATO
20f7a 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f R?).*/.static vo
20f7b 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 id groupConcatSt
20f7c 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f ep(. sqlite3_co
20f7d 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
20f7e 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
20f7f 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
20f80 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 gv.){. const ch
20f81 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 74 72 41 ar *zVal;. StrA
20f82 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 ccum *pAccum;.
20f83 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 const char *zSep
20f84 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53 ;. int nVal, nS
20f85 65 70 2c 20 69 3b 0a 20 20 69 66 28 20 61 72 67 ep, i;. if( arg
20f86 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f c==0 || sqlite3_
20f87 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
20f88 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0])==SQLITE_NULL
20f89 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 41 63 ) return;. pAc
20f8a 63 75 6d 20 3d 20 28 53 74 72 41 63 63 75 6d 2a cum = (StrAccum*
20f8b 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 )sqlite3_aggrega
20f8c 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 te_context(conte
20f8d 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 41 63 63 xt, sizeof(*pAcc
20f8e 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20 70 41 63 um));.. if( pAc
20f8f 63 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 cum ){. sqlit
20f90 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 e3 *db = sqlite3
20f91 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
20f92 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 le(context);.
20f93 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c pAccum->useMall
20f94 6f 63 20 3d 20 31 3b 0a 20 20 20 20 70 41 63 63 oc = 1;. pAcc
20f95 75 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 um->mxAlloc = db
20f96 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
20f97 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 LIMIT_LENGTH];.
20f98 20 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 6e if( pAccum->n
20f99 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 69 66 Char ){. if
20f9a 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 ( argc>1 ){.
20f9b 20 20 20 20 7a 53 65 70 20 3d 20 28 63 68 61 72 zSep = (char
20f9c 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *)sqlite3_value_
20f9d 74 65 78 74 28 61 72 67 76 5b 61 72 67 63 2d 31 text(argv[argc-1
20f9e 5d 29 3b 0a 20 20 20 20 20 20 20 20 6e 53 65 70 ]);. nSep
20f9f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
20fa0 5f 62 79 74 65 73 28 61 72 67 76 5b 61 72 67 63 _bytes(argv[argc
20fa1 2d 31 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 -1]);. }els
20fa2 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20 e{. zSep
20fa3 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 20 20 6e = ",";. n
20fa4 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d Sep = 1;. }
20fa5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
20fa6 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 rAccumAppend(pAc
20fa7 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70 29 cum, zSep, nSep)
20fa8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 ;. }. i =
20fa9 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 0;. do{.
20faa 20 7a 56 61 6c 20 3d 20 28 63 68 61 72 2a 29 73 zVal = (char*)s
20fab 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
20fac 74 28 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 t(argv[i]);.
20fad 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 nVal = sqlite3
20fae 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 _value_bytes(arg
20faf 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c v[i]);. sql
20fb0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
20fb1 6e 64 28 70 41 63 63 75 6d 2c 20 7a 56 61 6c 2c nd(pAccum, zVal,
20fb2 20 6e 56 61 6c 29 3b 0a 20 20 20 20 20 20 69 2b nVal);. i+
20fb3 2b 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 69 +;. }while( i
20fb4 3c 61 72 67 63 2d 31 20 29 3b 0a 20 20 7d 0a 7d <argc-1 );. }.}
20fb5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f .static void gro
20fb6 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 upConcatFinalize
20fb7 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
20fb8 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 74 *context){. St
20fb9 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a rAccum *pAccum;.
20fba 20 20 70 41 63 63 75 6d 20 3d 20 73 71 6c 69 74 pAccum = sqlit
20fbb 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e e3_aggregate_con
20fbc 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 text(context, 0)
20fbd 3b 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20 29 ;. if( pAccum )
20fbe 7b 0a 20 20 20 20 69 66 28 20 70 41 63 63 75 6d {. if( pAccum
20fbf 2d 3e 74 6f 6f 42 69 67 20 29 7b 0a 20 20 20 20 ->tooBig ){.
20fc0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
20fc1 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f _error_toobig(co
20fc2 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 ntext);. }els
20fc3 65 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 6d 61 e if( pAccum->ma
20fc4 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
20fc5 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
20fc6 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 lt_error_nomem(c
20fc7 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c ontext);. }el
20fc8 73 65 7b 20 20 20 20 0a 20 20 20 20 20 20 73 71 se{ . sq
20fc9 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
20fca 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 t(context, sqlit
20fcb 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 e3StrAccumFinish
20fcc 28 70 41 63 63 75 6d 29 2c 20 2d 31 2c 20 0a 20 (pAccum), -1, .
20fcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20fce 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
20fcf 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 _free);. }.
20fd0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
20fd1 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 function registe
20fd2 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 red all of the a
20fd3 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 bove C functions
20fd4 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 as SQL.** funct
20fd5 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f 75 ions. This shou
20fd6 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 ld be the only r
20fd7 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 outine in this f
20fd8 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 ile with.** exte
20fd9 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f rnal linkage..*/
20fda 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20fdb 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 void sqlite3Regi
20fdc 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 sterBuiltinFunct
20fdd 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 ions(sqlite3 *db
20fde 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ){.#ifndef SQLIT
20fdf 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c E_OMIT_ALTERTABL
20fe0 45 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 E. sqlite3Alter
20fe1 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 23 Functions(db);.#
20fe2 65 6e 64 69 66 0a 20 20 69 66 28 20 21 64 62 2d endif. if( !db-
20fe3 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
20fe4 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 . int rc = sq
20fe5 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 lite3_overload_f
20fe6 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 4d 41 54 unction(db, "MAT
20fe7 43 48 22 2c 20 32 29 3b 0a 20 20 20 20 61 73 73 CH", 2);. ass
20fe8 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
20fe9 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c NOMEM || rc==SQL
20fea 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 ITE_OK );. if
20feb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
20fec 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e EM ){. db->
20fed 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
20fee 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64 ;. }. }.#ifd
20fef 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 ef SQLITE_SSE.
20ff0 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 73 65 (void)sqlite3Sse
20ff1 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 23 Functions(db);.#
20ff2 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 endif.}../*.** S
20ff3 65 74 20 74 68 65 20 4c 49 4b 45 4f 50 54 20 66 et the LIKEOPT f
20ff4 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d 61 72 67 lag on the 2-arg
20ff5 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 20 77 ument function w
20ff6 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 ith the given na
20ff7 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f me..*/.static vo
20ff8 69 64 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 id setLikeOptFla
20ff9 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 g(sqlite3 *db, c
20ffa 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
20ffb 2c 20 69 6e 74 20 66 6c 61 67 56 61 6c 29 7b 0a , int flagVal){.
20ffc 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b FuncDef *pDef;
20ffd 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 . pDef = sqlite
20ffe 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 3FindFunction(db
20fff 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 , zName, strlen(
21000 7a 4e 61 6d 65 29 2c 20 32 2c 20 53 51 4c 49 54 zName), 2, SQLIT
21001 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 E_UTF8, 0);. if
21002 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 70 44 ( pDef ){. pD
21003 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 ef->flags = flag
21004 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a Val;. }.}../*.*
21005 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20 62 * Register the b
21006 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 61 6e 64 uilt-in LIKE and
21007 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e GLOB functions.
21008 20 20 54 68 65 20 63 61 73 65 53 65 6e 73 69 74 The caseSensit
21009 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 ive.** parameter
2100a 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 determines whet
2100b 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 4c her or not the L
2100c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 69 73 20 IKE operator is
2100d 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69 74 69 76 case.** sensitiv
2100e 65 2e 20 20 47 4c 4f 42 20 69 73 20 61 6c 77 61 e. GLOB is alwa
2100f 79 73 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 ys case sensitiv
21010 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
21011 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
21012 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 3RegisterLikeFun
21013 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a ctions(sqlite3 *
21014 64 62 2c 20 69 6e 74 20 63 61 73 65 53 65 6e 73 db, int caseSens
21015 69 74 69 76 65 29 7b 0a 20 20 73 74 72 75 63 74 itive){. struct
21016 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 compareInfo *pI
21017 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61 73 65 53 nfo;. if( caseS
21018 65 6e 73 69 74 69 76 65 20 29 7b 0a 20 20 20 20 ensitive ){.
21019 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20 pInfo = (struct
2101a 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 compareInfo*)&li
2101b 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c keInfoAlt;. }el
2101c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 se{. pInfo =
2101d 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 (struct compareI
2101e 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f nfo*)&likeInfoNo
2101f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 rm;. }. sqlite
21020 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 3CreateFunc(db,
21021 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c 49 54 "like", 2, SQLIT
21022 45 5f 55 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c E_UTF8, pInfo, l
21023 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a ikeFunc, 0, 0);.
21024 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 sqlite3CreateF
21025 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 unc(db, "like",
21026 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 3, SQLITE_UTF8,
21027 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c pInfo, likeFunc,
21028 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 0, 0);. sqlite
21029 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 3CreateFunc(db,
2102a 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54 "glob", 2, SQLIT
2102b 45 5f 55 54 46 38 2c 20 0a 20 20 20 20 20 20 28 E_UTF8, . (
2102c 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e struct compareIn
2102d 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c fo*)&globInfo, l
2102e 69 6b 65 46 75 6e 63 2c 20 30 2c 30 29 3b 0a 20 ikeFunc, 0,0);.
2102f 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 setLikeOptFlag(
21030 64 62 2c 20 22 67 6c 6f 62 22 2c 20 53 51 4c 49 db, "glob", SQLI
21031 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 TE_FUNC_LIKE | S
21032 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 QLITE_FUNC_CASE)
21033 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c ;. setLikeOptFl
21034 61 67 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 0a ag(db, "like", .
21035 20 20 20 20 20 20 63 61 73 65 53 65 6e 73 69 74 caseSensit
21036 69 76 65 20 3f 20 28 53 51 4c 49 54 45 5f 46 55 ive ? (SQLITE_FU
21037 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 NC_LIKE | SQLITE
21038 5f 46 55 4e 43 5f 43 41 53 45 29 20 3a 20 53 51 _FUNC_CASE) : SQ
21039 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b LITE_FUNC_LIKE);
2103a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 .}../*.** pExpr
2103b 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 78 70 points to an exp
2103c 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 6d ression which im
2103d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 plements a funct
2103e 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 ion. If.** it i
2103f 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 6f s appropriate to
21040 20 61 70 70 6c 79 20 74 68 65 20 4c 49 4b 45 20 apply the LIKE
21041 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 optimization to
21042 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a that function.**
21043 20 74 68 65 6e 20 73 65 74 20 61 57 63 5b 30 5d then set aWc[0]
21044 20 74 68 72 6f 75 67 68 20 61 57 63 5b 32 5d 20 through aWc[2]
21045 74 6f 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 to the wildcard
21046 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a characters and.*
21047 2a 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 * return TRUE.
21048 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 If the function
21049 69 73 20 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74 is not a LIKE-st
2104a 79 6c 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 65 yle function the
2104b 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 46 41 4c 53 n.** return FALS
2104c 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 E..*/.SQLITE_PRI
2104d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
2104e 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 IsLikeFunction(s
2104f 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 qlite3 *db, Expr
21050 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 49 *pExpr, int *pI
21051 73 4e 6f 63 61 73 65 2c 20 63 68 61 72 20 2a 61 sNocase, char *a
21052 57 63 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a Wc){. FuncDef *
21053 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45 78 70 pDef;. if( pExp
21054 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 r->op!=TK_FUNCTI
21055 4f 4e 20 7c 7c 20 21 70 45 78 70 72 2d 3e 70 4c ON || !pExpr->pL
21056 69 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ist ){. retur
21057 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 n 0;. }. if( p
21058 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 Expr->pList->nEx
21059 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 pr!=2 ){. ret
2105a 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 44 65 urn 0;. }. pDe
2105b 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 f = sqlite3FindF
2105c 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 63 68 61 unction(db, (cha
2105d 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e r*)pExpr->token.
2105e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e z, pExpr->token.
2105f 6e 2c 20 32 2c 0a 20 20 20 20 20 20 20 20 20 20 n, 2,.
21060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21061 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 SQLITE_UTF8,
21062 30 29 3b 0a 20 20 69 66 28 20 70 44 65 66 3d 3d 0);. if( pDef==
21063 30 20 7c 7c 20 28 70 44 65 66 2d 3e 66 6c 61 67 0 || (pDef->flag
21064 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f s & SQLITE_FUNC_
21065 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 LIKE)==0 ){.
21066 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
21067 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 28 29 /* The memcpy()
21068 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 75 6d statement assum
21069 65 73 20 74 68 61 74 20 74 68 65 20 77 69 6c 64 es that the wild
2106a 63 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20 card characters
2106b 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 are. ** the fir
2106c 73 74 20 74 68 72 65 65 20 73 74 61 74 65 6d 65 st three stateme
2106d 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 61 nts in the compa
2106e 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 reInfo structure
2106f 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 73 65 . The. ** asse
21070 72 74 73 28 29 20 74 68 61 74 20 66 6f 6c 6c 6f rts() that follo
21071 77 20 76 65 72 69 66 79 20 74 68 61 74 20 61 73 w verify that as
21072 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 sumption. */.
21073 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 44 65 66 memcpy(aWc, pDef
21074 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 33 29 3b ->pUserData, 3);
21075 0a 20 20 61 73 73 65 72 74 28 20 28 63 68 61 72 . assert( (char
21076 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d *)&likeInfoAlt =
21077 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e = (char*)&likeIn
21078 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20 29 foAlt.matchAll )
21079 3b 0a 20 20 61 73 73 65 72 74 28 20 26 28 28 63 ;. assert( &((c
2107a 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c har*)&likeInfoAl
2107b 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a 29 t)[1] == (char*)
2107c 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 &likeInfoAlt.mat
2107d 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 65 72 chOne );. asser
2107e 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b t( &((char*)&lik
2107f 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20 eInfoAlt)[2] ==
21080 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f (char*)&likeInfo
21081 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 3b 0a Alt.matchSet );.
21082 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d 20 28 *pIsNocase = (
21083 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 pDef->flags & SQ
21084 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3d LITE_FUNC_CASE)=
21085 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a =0;. return 1;.
21086 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c }../*.** All all
21087 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 20 of the FuncDef
21088 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 structures in th
21089 65 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d e aBuiltinFunc[]
2108a 20 61 72 72 61 79 20 61 62 6f 76 65 0a 2a 2a 20 array above.**
2108b 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 to the global fu
2108c 6e 63 74 69 6f 6e 20 68 61 73 68 20 74 61 62 6c nction hash tabl
2108d 65 2e 20 20 54 68 69 73 20 6f 63 63 75 72 73 20 e. This occurs
2108e 61 74 20 73 74 61 72 74 2d 74 69 6d 65 20 28 61 at start-time (a
2108f 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 75 65 6e s.** a consequen
21090 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67 20 73 71 ce of calling sq
21091 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
21092 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 ())..**.** After
21093 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 this routine ru
21094 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ns.*/.SQLITE_PRI
21095 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
21096 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 3RegisterGlobalF
21097 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a unctions(void){.
21098 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f /*. ** The fo
21099 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 68 6f llowing array ho
2109a 6c 64 73 20 46 75 6e 63 44 65 66 20 73 74 72 75 lds FuncDef stru
2109b 63 74 75 72 65 73 20 66 6f 72 20 61 6c 6c 20 6f ctures for all o
2109c 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a f the functions.
2109d 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 69 6e 20 ** defined in
2109e 74 68 69 73 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a this file.. **.
2109f 20 20 2a 2a 20 54 68 65 20 61 72 72 61 79 20 63 ** The array c
210a0 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 61 6e annot be constan
210a1 74 20 73 69 6e 63 65 20 63 68 61 6e 67 65 73 20 t since changes
210a2 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a are made to the.
210a3 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e 70 48 61 ** FuncDef.pHa
210a4 73 68 20 65 6c 65 6d 65 6e 74 73 20 61 74 20 73 sh elements at s
210a5 74 61 72 74 2d 74 69 6d 65 2e 20 20 54 68 65 20 tart-time. The
210a6 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 69 73 elements of this
210a7 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 72 65 20 array. ** are
210a8 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20 read-only after
210a9 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 initialization i
210aa 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 20 2a 2f s complete.. */
210ab 0a 20 20 73 74 61 74 69 63 20 53 51 4c 49 54 45 . static SQLITE
210ac 5f 57 53 44 20 46 75 6e 63 44 65 66 20 61 42 75 _WSD FuncDef aBu
210ad 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d 20 7b 0a iltinFunc[] = {.
210ae 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 FUNCTION(ltr
210af 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 im,
210b0 20 31 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 1, 1, 0, trimFu
210b1 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 nc ),.
210b2 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d FUNCTION(ltrim
210b3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 , 2
210b4 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 , 1, 0, trimFunc
210b5 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
210b6 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 FUNCTION(rtrim,
210b7 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
210b8 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 2, 0, trimFunc
210b9 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ),. FU
210ba 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20 NCTION(rtrim,
210bb 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 32 2c 2, 2,
210bc 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 0, trimFunc
210bd 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
210be 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 TION(trim,
210bf 20 20 20 20 20 20 20 20 20 31 2c 20 33 2c 20 30 1, 3, 0
210c0 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 , trimFunc
210c1 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
210c2 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20 20 ON(trim,
210c3 20 20 20 20 20 20 20 32 2c 20 33 2c 20 30 2c 20 2, 3, 0,
210c4 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 trimFunc
210c5 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
210c6 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 (min,
210c7 20 20 20 20 2d 31 2c 20 30 2c 20 31 2c 20 6d 69 -1, 0, 1, mi
210c8 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20 29 nmaxFunc )
210c9 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d ,. FUNCTION(m
210ca 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 in,
210cb 20 20 20 30 2c 20 30 2c 20 31 2c 20 30 20 20 20 0, 0, 1, 0
210cc 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a ),.
210cd 20 20 20 20 41 47 47 52 45 47 41 54 45 28 6d 69 AGGREGATE(mi
210ce 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
210cf 20 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 1, 0, 1, minmax
210d0 53 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 Step, minMa
210d1 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20 xFinalize ),.
210d2 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 FUNCTION(max,
210d3 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c -1,
210d4 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 1, 1, minmaxFun
210d5 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 c ),. F
210d6 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20 UNCTION(max,
210d7 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 31 0, 1
210d8 2c 20 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 , 1, 0
210d9 20 20 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 ),. AGG
210da 52 45 47 41 54 45 28 6d 61 78 2c 20 20 20 20 20 REGATE(max,
210db 20 20 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20 1, 1,
210dc 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 1, minmaxStep,
210dd 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 minMaxFinali
210de 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ze ),. FUNCTI
210df 4f 4e 28 74 79 70 65 6f 66 2c 20 20 20 20 20 20 ON(typeof,
210e0 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 1, 0, 0,
210e1 74 79 70 65 6f 66 46 75 6e 63 20 20 20 20 20 20 typeofFunc
210e2 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
210e3 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 20 20 (length,
210e4 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 65 1, 0, 0, le
210e5 6e 67 74 68 46 75 6e 63 20 20 20 20 20 20 20 29 ngthFunc )
210e6 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 ,. FUNCTION(s
210e7 75 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 ubstr,
210e8 20 20 20 32 2c 20 30 2c 20 30 2c 20 73 75 62 73 2, 0, 0, subs
210e9 74 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a trFunc ),.
210ea 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 FUNCTION(sub
210eb 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 str,
210ec 20 33 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 3, 0, 0, substr
210ed 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 Func ),.
210ee 20 20 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c 20 FUNCTION(abs,
210ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
210f0 2c 20 30 2c 20 30 2c 20 61 62 73 46 75 6e 63 20 , 0, 0, absFunc
210f1 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
210f2 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 FUNCTION(round,
210f3 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
210f4 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 0, 0, roundFunc
210f5 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ),. FU
210f6 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 NCTION(round,
210f7 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 2, 0,
210f8 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 0, roundFunc
210f9 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
210fa 54 49 4f 4e 28 75 70 70 65 72 2c 20 20 20 20 20 TION(upper,
210fb 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
210fc 2c 20 75 70 70 65 72 46 75 6e 63 20 20 20 20 20 , upperFunc
210fd 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
210fe 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20 20 20 20 ON(lower,
210ff 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 1, 0, 0,
21100 6c 6f 77 65 72 46 75 6e 63 20 20 20 20 20 20 20 lowerFunc
21101 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
21102 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 (coalesce,
21103 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 30 20 1, 0, 0, 0
21104 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 )
21105 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 ,. FUNCTION(c
21106 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 oalesce,
21107 20 20 2d 31 2c 20 30 2c 20 30 2c 20 69 66 6e 75 -1, 0, 0, ifnu
21108 6c 6c 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a llFunc ),.
21109 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 FUNCTION(coa
2110a 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 20 lesce,
2110b 20 30 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 20 0, 0, 0, 0
2110c 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 ),.
2110d 20 20 46 55 4e 43 54 49 4f 4e 28 68 65 78 2c 20 FUNCTION(hex,
2110e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
2110f 2c 20 30 2c 20 30 2c 20 68 65 78 46 75 6e 63 20 , 0, 0, hexFunc
21110 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
21111 46 55 4e 43 54 49 4f 4e 28 69 66 6e 75 6c 6c 2c FUNCTION(ifnull,
21112 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 2,
21113 30 2c 20 31 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 0, 1, ifnullFunc
21114 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ),. FU
21115 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c 20 20 NCTION(random,
21116 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c -1, 0,
21117 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 63 20 20 0, randomFunc
21118 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
21119 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f 62 2c TION(randomblob,
2111a 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
2111b 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 20 20 20 , randomBlob
2111c 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
2111d 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 20 20 20 ON(nullif,
2111e 20 20 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 20 2, 0, 1,
2111f 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 20 20 20 nullifFunc
21120 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
21121 28 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e 2c (sqlite_version,
21122 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 76 65 0, 0, 0, ve
21123 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 20 20 29 rsionFunc )
21124 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 71 ,. FUNCTION(q
21125 75 6f 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 uote,
21126 20 20 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74 1, 0, 0, quot
21127 65 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a eFunc ),.
21128 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73 FUNCTION(las
21129 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20 t_insert_rowid,
2112a 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69 0, 0, 0, last_i
2112b 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20 nsert_rowid),.
2112c 20 20 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67 FUNCTION(chang
2112d 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 es, 0
2112e 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 , 0, 0, changes
2112f 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
21130 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 FUNCTION(total_c
21131 68 61 6e 67 65 73 2c 20 20 20 20 20 20 30 2c 20 hanges, 0,
21132 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 0, 0, total_chan
21133 67 65 73 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ges ),. FU
21134 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 NCTION(replace,
21135 20 20 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c 3, 0,
21136 20 30 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20 0, replaceFunc
21137 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
21138 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 TION(zeroblob,
21139 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
2113a 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 , zeroblobFunc
2113b 20 20 20 29 2c 0a 20 20 23 69 66 64 65 66 20 53 ),. #ifdef S
2113c 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 QLITE_SOUNDEX.
2113d 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64 FUNCTION(sound
2113e 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 31 ex, 1
2113f 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46 , 0, 0, soundexF
21140 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 23 65 unc ),. #e
21141 6e 64 69 66 0a 20 20 23 69 66 6e 64 65 66 20 53 ndif. #ifndef S
21142 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
21143 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55 EXTENSION. FU
21144 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 NCTION(load_exte
21145 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c nsion, 1, 0,
21146 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 0, loadExt
21147 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
21148 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 TION(load_extens
21149 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30 ion, 2, 0, 0
2114a 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 , loadExt
2114b 20 20 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 ),. #endif.
2114c 20 20 20 41 47 47 52 45 47 41 54 45 28 73 75 6d AGGREGATE(sum
2114d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
2114e 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 1, 0, 0, sumStep
2114f 2c 20 20 20 20 20 20 20 20 20 73 75 6d 46 69 6e , sumFin
21150 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 alize ),.
21151 41 47 47 52 45 47 41 54 45 28 74 6f 74 61 6c 2c AGGREGATE(total,
21152 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
21153 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 0, 0, sumStep,
21154 20 20 20 20 20 20 20 74 6f 74 61 6c 46 69 6e 61 totalFina
21155 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 lize ),. A
21156 47 47 52 45 47 41 54 45 28 61 76 67 2c 20 20 20 GGREGATE(avg,
21157 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 1, 0
21158 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 , 0, sumStep,
21159 20 20 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a avgFinaliz
2115a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 e ),. AGGR
2115b 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 EGATE(count,
2115c 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 0, 0, 0
2115d 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 , countStep,
2115e 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 countFinalize
2115f 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 ),. AGGREGA
21160 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 TE(count,
21161 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 63 1, 0, 0, c
21162 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20 ountStep,
21163 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29 countFinalize )
21164 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 ,. AGGREGATE(
21165 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20 20 group_concat,
21166 20 20 2d 31 2c 20 30 2c 20 30 2c 20 67 72 6f 75 -1, 0, 0, grou
21167 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72 6f pConcatStep, gro
21168 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 upConcatFinalize
21169 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b 45 46 55 ),. . LIKEFU
2116a 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26 67 6c 6f NC(glob, 2, &glo
2116b 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45 5f 46 55 bInfo, SQLITE_FU
2116c 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 NC_LIKE|SQLITE_F
2116d 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 69 66 UNC_CASE),. #if
2116e 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f def SQLITE_CASE_
2116f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 0a 20 SENSITIVE_LIKE.
21170 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 LIKEFUNC(like
21171 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f 41 6c , 2, &likeInfoAl
21172 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c t, SQLITE_FUNC_L
21173 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f IKE|SQLITE_FUNC_
21174 43 41 53 45 29 2c 0a 20 20 20 20 4c 49 4b 45 46 CASE),. LIKEF
21175 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 UNC(like, 3, &li
21176 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49 54 keInfoAlt, SQLIT
21177 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 E_FUNC_LIKE|SQLI
21178 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 TE_FUNC_CASE),.
21179 20 23 65 6c 73 65 0a 20 20 20 20 4c 49 4b 45 46 #else. LIKEF
2117a 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 UNC(like, 2, &li
2117b 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 keInfoNorm, SQLI
2117c 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20 TE_FUNC_LIKE),.
2117d 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 LIKEFUNC(like
2117e 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f , 3, &likeInfoNo
2117f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f rm, SQLITE_FUNC_
21180 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64 69 66 0a LIKE),. #endif.
21181 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 3b 0a 20 };.. int i;.
21182 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 FuncDefHash *pH
21183 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 ash = &GLOBAL(Fu
21184 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74 ncDefHash, sqlit
21185 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e e3GlobalFunction
21186 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 s);. FuncDef *a
21187 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 2a Func = (FuncDef*
21188 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 )&GLOBAL(FuncDef
21189 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 29 3b , aBuiltinFunc);
2118a 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 .. for(i=0; i<A
2118b 72 72 61 79 53 69 7a 65 28 61 42 75 69 6c 74 69 rraySize(aBuilti
2118c 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b 0a 20 20 nFunc); i++){.
2118d 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 sqlite3FuncDef
2118e 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 61 Insert(pHash, &a
2118f 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 Func[i]);. }.
21190 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 sqlite3RegisterD
21191 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 ateTimeFunctions
21192 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ();.}../********
21193 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 75 ****** End of fu
21194 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nc.c ***********
21195 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21196 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21197 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
21198 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
21199 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a e insert.c *****
2119a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2119b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2119c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
2119d 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
2119e 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
2119f 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
211a0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
211a1 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
211a2 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
211a3 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
211a4 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
211a5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
211a6 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
211a7 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
211a8 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
211a9 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
211aa 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
211ab 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
211ac 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
211ad 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
211ae 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
211af 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
211b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
211b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
211b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
211b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
211b4 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
211b5 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 ins C code routi
211b6 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c nes that are cal
211b7 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 led by the parse
211b8 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 49 r.** to handle I
211b9 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73 NSERT statements
211ba 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a in SQLite..**.*
211bb 2a 20 24 49 64 3a 20 69 6e 73 65 72 74 2e 63 2c * $Id: insert.c,
211bc 76 20 31 2e 32 35 31 20 32 30 30 38 2f 31 31 2f v 1.251 2008/11/
211bd 30 33 20 32 30 3a 35 35 3a 30 37 20 64 72 68 20 03 20:55:07 drh
211be 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
211bf 53 65 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f Set P4 of the mo
211c0 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 st recently inse
211c1 72 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61 rted opcode to a
211c2 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 column affinity
211c3 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 69 .** string for i
211c4 6e 64 65 78 20 70 49 64 78 2e 20 41 20 63 6f 6c ndex pIdx. A col
211c5 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 umn affinity str
211c6 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72 ing has one char
211c7 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63 acter.** for eac
211c8 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 h column in the
211c9 74 61 62 6c 65 2c 20 61 63 63 6f 72 64 69 6e 67 table, according
211ca 20 74 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79 to the affinity
211cb 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 3a 0a of the column:.
211cc 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72 **.** Character
211cd 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 Column aff
211ce 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d inity.** ------
211cf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
211d0 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 --------.** 'a'
211d1 20 20 20 20 20 20 20 20 20 20 20 20 54 45 58 54 TEXT
211d2 0a 2a 2a 20 20 27 62 27 20 20 20 20 20 20 20 20 .** 'b'
211d3 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 NONE.** 'c'
211d4 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45 NUME
211d5 52 49 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 RIC.** 'd'
211d6 20 20 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a INTEGER.*
211d7 2a 20 20 27 65 27 20 20 20 20 20 20 20 20 20 20 * 'e'
211d8 20 20 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 REAL.**.** An
211d9 65 78 74 72 61 20 27 62 27 20 69 73 20 61 70 70 extra 'b' is app
211da 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 ended to the end
211db 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 74 of the string t
211dc 6f 20 63 6f 76 65 72 20 74 68 65 0a 2a 2a 20 72 o cover the.** r
211dd 6f 77 69 64 20 74 68 61 74 20 61 70 70 65 61 72 owid that appear
211de 73 20 61 73 20 74 68 65 20 6c 61 73 74 20 63 6f s as the last co
211df 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79 20 69 6e lumn in every in
211e0 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 dex..*/.SQLITE_P
211e1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
211e2 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 te3IndexAffinity
211e3 53 74 72 28 56 64 62 65 20 2a 76 2c 20 49 6e 64 Str(Vdbe *v, Ind
211e4 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 66 28 ex *pIdx){. if(
211e5 20 21 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 !pIdx->zColAff
211e6 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 69 ){. /* The fi
211e7 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c 75 6d rst time a colum
211e8 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e n affinity strin
211e9 67 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c g for a particul
211ea 61 72 20 69 6e 64 65 78 20 69 73 0a 20 20 20 20 ar index is.
211eb 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 69 74 20 ** required, it
211ec 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 is allocated and
211ed 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72 65 2e populated here.
211ee 20 49 74 20 69 73 20 74 68 65 6e 20 73 74 6f 72 It is then stor
211ef 65 64 20 61 73 0a 20 20 20 20 2a 2a 20 61 20 6d ed as. ** a m
211f0 65 6d 62 65 72 20 6f 66 20 74 68 65 20 49 6e 64 ember of the Ind
211f1 65 78 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 ex structure for
211f2 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e subsequent use.
211f3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
211f4 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 he column affini
211f5 74 79 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 ty string will e
211f6 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 64 65 6c ventually be del
211f7 65 74 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 73 eted by. ** s
211f8 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65 78 qliteDeleteIndex
211f9 28 29 20 77 68 65 6e 20 74 68 65 20 49 6e 64 65 () when the Inde
211fa 78 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 x structure itse
211fb 6c 66 20 69 73 20 63 6c 65 61 6e 65 64 0a 20 20 lf is cleaned.
211fc 20 20 2a 2a 20 75 70 2e 0a 20 20 20 20 2a 2f 0a ** up.. */.
211fd 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 54 int n;. T
211fe 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64 able *pTab = pId
211ff 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 73 x->pTable;. s
21200 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c qlite3 *db = sql
21201 69 74 65 33 56 64 62 65 44 62 28 76 29 3b 0a 20 ite3VdbeDb(v);.
21202 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 pIdx->zColAff
21203 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 = (char *)sqlit
21204 65 33 4d 61 6c 6c 6f 63 28 70 49 64 78 2d 3e 6e e3Malloc(pIdx->n
21205 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 69 Column+2);. i
21206 66 28 20 21 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 f( !pIdx->zColAf
21207 66 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d f ){. db->m
21208 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
21209 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
2120a 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 6e 3d 30 }. for(n=0
2120b 3b 20 6e 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d ; n<pIdx->nColum
2120c 6e 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 70 n; n++){. p
2120d 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 Idx->zColAff[n]
2120e 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 = pTab->aCol[pId
2120f 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e x->aiColumn[n]].
21210 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a affinity;. }.
21211 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 pIdx->zColAf
21212 66 5b 6e 2b 2b 5d 20 3d 20 53 51 4c 49 54 45 5f f[n++] = SQLITE_
21213 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 70 49 AFF_NONE;. pI
21214 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d dx->zColAff[n] =
21215 20 30 3b 0a 20 20 7d 0a 20 0a 20 20 73 71 6c 69 0;. }. . sqli
21216 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
21217 76 2c 20 2d 31 2c 20 70 49 64 78 2d 3e 7a 43 6f v, -1, pIdx->zCo
21218 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a lAff, 0);.}../*.
21219 2a 2a 20 53 65 74 20 50 34 20 6f 66 20 74 68 65 ** Set P4 of the
2121a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 most recently i
2121b 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 20 74 nserted opcode t
2121c 6f 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e o a column affin
2121d 69 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f ity.** string fo
2121e 72 20 74 61 62 6c 65 20 70 54 61 62 2e 20 41 20 r table pTab. A
2121f 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 column affinity
21220 73 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 string has one c
21221 68 61 72 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20 haracter.** for
21222 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 each column inde
21223 78 65 64 20 62 79 20 74 68 65 20 69 6e 64 65 78 xed by the index
21224 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 , according to t
21225 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 he affinity of t
21226 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a he.** column:.**
21227 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 .** Character
21228 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e Column affin
21229 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d ity.** --------
2122a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
2122b 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 ------.** 'a'
2122c 20 20 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a TEXT.*
2122d 2a 20 20 27 62 27 20 20 20 20 20 20 20 20 20 20 * 'b'
2122e 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 NONE.** 'c'
2122f 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 NUMERI
21230 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 C.** 'd'
21231 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 INTEGER.**
21232 20 27 65 27 20 20 20 20 20 20 20 20 20 20 20 20 'e'
21233 52 45 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 REAL.*/.SQLITE_P
21234 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21235 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 te3TableAffinity
21236 53 74 72 28 56 64 62 65 20 2a 76 2c 20 54 61 62 Str(Vdbe *v, Tab
21237 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 2f 2a 20 le *pTab){. /*
21238 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 The first time a
21239 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 column affinity
2123a 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 61 string for a pa
2123b 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 0a 20 rticular table.
2123c 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2c ** is required,
2123d 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 it is allocated
2123e 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 68 and populated h
2123f 65 72 65 2e 20 49 74 20 69 73 20 74 68 65 6e 20 ere. It is then
21240 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 61 73 20 . ** stored as
21241 61 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 a member of the
21242 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 Table structure
21243 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 75 for subsequent u
21244 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 se.. **. ** Th
21245 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 e column affinit
21246 79 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 y string will ev
21247 65 6e 74 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 entually be dele
21248 74 65 64 20 62 79 0a 20 20 2a 2a 20 73 71 6c 69 ted by. ** sqli
21249 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 29 te3DeleteTable()
2124a 20 77 68 65 6e 20 74 68 65 20 54 61 62 6c 65 20 when the Table
2124b 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 structure itself
2124c 20 69 73 20 63 6c 65 61 6e 65 64 20 75 70 2e 0a is cleaned up..
2124d 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 */. if( !pTab
2124e 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 ->zColAff ){.
2124f 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a char *zColAff;.
21250 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 int i;. s
21251 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c qlite3 *db = sql
21252 69 74 65 33 56 64 62 65 44 62 28 76 29 3b 0a 0a ite3VdbeDb(v);..
21253 20 20 20 20 7a 43 6f 6c 41 66 66 20 3d 20 28 63 zColAff = (c
21254 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c har *)sqlite3Mal
21255 6c 6f 63 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 loc(pTab->nCol+1
21256 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 6f 6c );. if( !zCol
21257 41 66 66 20 29 7b 0a 20 20 20 20 20 20 64 62 2d Aff ){. db-
21258 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
21259 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 1;. return;
2125a 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 . }.. for(
2125b 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f i=0; i<pTab->nCo
2125c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a l; i++){. z
2125d 43 6f 6c 41 66 66 5b 69 5d 20 3d 20 70 54 61 62 ColAff[i] = pTab
2125e 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 ->aCol[i].affini
2125f 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 ty;. }. zC
21260 6f 6c 41 66 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c olAff[pTab->nCol
21261 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20 70 ] = '\0';.. p
21262 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a Tab->zColAff = z
21263 43 6f 6c 41 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 ColAff;. }.. s
21264 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
21265 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e P4(v, -1, pTab->
21266 7a 43 6f 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a zColAff, 0);.}..
21267 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e /*.** Return non
21268 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 74 61 62 -zero if the tab
21269 6c 65 20 70 54 61 62 20 69 6e 20 64 61 74 61 62 le pTab in datab
2126a 61 73 65 20 69 44 62 20 6f 72 20 61 6e 79 20 6f ase iDb or any o
2126b 66 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a f its indices.**
2126c 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 have been opene
2126d 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 69 d at any point i
2126e 6e 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 n the VDBE progr
2126f 61 6d 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 am beginning at
21270 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 53 74 61 location.** iSta
21271 72 74 41 64 64 72 20 74 68 72 6f 75 67 68 74 20 rtAddr throught
21272 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 the end of the p
21273 72 6f 67 72 61 6d 2e 20 20 54 68 69 73 20 69 73 rogram. This is
21274 20 75 73 65 64 20 74 6f 20 73 65 65 20 69 66 20 used to see if
21275 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e 74 20 .** a statement
21276 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 22 49 4e of the form "IN
21277 53 45 52 54 20 49 4e 54 4f 20 3c 69 44 62 2c 20 SERT INTO <iDb,
21278 70 54 61 62 3e 20 53 45 4c 45 43 54 20 2e 2e 2e pTab> SELECT ...
21279 22 20 63 61 6e 20 0a 2a 2a 20 72 75 6e 20 77 69 " can .** run wi
2127a 74 68 6f 75 74 20 75 73 69 6e 67 20 74 65 6d 70 thout using temp
2127b 6f 72 61 72 79 20 74 61 62 6c 65 20 66 6f 72 20 orary table for
2127c 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 the results of t
2127d 68 65 20 53 45 4c 45 43 54 2e 20 0a 2a 2f 0a 73 he SELECT. .*/.s
2127e 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 73 54 tatic int readsT
2127f 61 62 6c 65 28 56 64 62 65 20 2a 76 2c 20 69 6e able(Vdbe *v, in
21280 74 20 69 53 74 61 72 74 41 64 64 72 2c 20 69 6e t iStartAddr, in
21281 74 20 69 44 62 2c 20 54 61 62 6c 65 20 2a 70 54 t iDb, Table *pT
21282 61 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 ab){. int i;.
21283 69 6e 74 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 int iEnd = sqlit
21284 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 e3VdbeCurrentAdd
21285 72 28 76 29 3b 0a 20 20 66 6f 72 28 69 3d 69 53 r(v);. for(i=iS
21286 74 61 72 74 41 64 64 72 3b 20 69 3c 69 45 6e 64 tartAddr; i<iEnd
21287 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 56 64 62 65 ; i++){. Vdbe
21288 4f 70 20 2a 70 4f 70 20 3d 20 73 71 6c 69 74 65 Op *pOp = sqlite
21289 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 69 29 3VdbeGetOp(v, i)
2128a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f ;. assert( pO
2128b 70 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 p!=0 );. if(
2128c 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
2128d 4f 70 65 6e 52 65 61 64 20 26 26 20 70 4f 70 2d OpenRead && pOp-
2128e 3e 70 33 3d 3d 69 44 62 20 29 7b 0a 20 20 20 20 >p3==iDb ){.
2128f 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b Index *pIndex;
21290 0a 20 20 20 20 20 20 69 6e 74 20 74 6e 75 6d 20 . int tnum
21291 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 20 20 20 = pOp->p2;.
21292 20 69 66 28 20 74 6e 75 6d 3d 3d 70 54 61 62 2d if( tnum==pTab-
21293 3e 74 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 >tnum ){.
21294 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
21295 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 70 49 6e }. for(pIn
21296 64 65 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 dex=pTab->pIndex
21297 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 ; pIndex; pIndex
21298 3d 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b =pIndex->pNext){
21299 0a 20 20 20 20 20 20 20 20 69 66 28 20 74 6e 75 . if( tnu
2129a 6d 3d 3d 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 m==pIndex->tnum
2129b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
2129c 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d urn 1;. }
2129d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
2129e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
2129f 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
212a0 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 if( pOp->opc
212a1 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65 6e 20 26 26 ode==OP_VOpen &&
212a2 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3d 3d pOp->p4.pVtab==
212a3 70 54 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 pTab->pVtab ){.
212a4 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
212a5 2d 3e 70 34 2e 70 56 74 61 62 21 3d 30 20 29 3b ->p4.pVtab!=0 );
212a6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
212a7 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 Op->p4type==P4_V
212a8 54 41 42 20 29 3b 0a 20 20 20 20 20 20 72 65 74 TAB );. ret
212a9 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e urn 1;. }.#en
212aa 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e dif. }. return
212ab 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 0;.}..#ifndef S
212ac 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
212ad 4e 43 52 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 57 NCREMENT./*.** W
212ae 72 69 74 65 20 6f 75 74 20 63 6f 64 65 20 74 6f rite out code to
212af 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 initialize the
212b0 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6c 6f autoincrement lo
212b1 67 69 63 2e 20 20 54 68 69 73 20 63 6f 64 65 0a gic. This code.
212b2 2a 2a 20 6c 6f 6f 6b 73 20 75 70 20 74 68 65 20 ** looks up the
212b3 63 75 72 72 65 6e 74 20 61 75 74 6f 69 6e 63 72 current autoincr
212b4 65 6d 65 6e 74 20 76 61 6c 75 65 20 69 6e 20 74 ement value in t
212b5 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e he sqlite_sequen
212b6 63 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 ce.** table and
212b7 73 74 6f 72 65 73 20 74 68 61 74 20 76 61 6c 75 stores that valu
212b8 65 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e e in a register.
212b9 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 Code generated
212ba 20 62 79 0a 2a 2a 20 61 75 74 6f 49 6e 63 53 74 by.** autoIncSt
212bb 65 70 28 29 20 77 69 6c 6c 20 6b 65 65 70 20 74 ep() will keep t
212bc 68 61 74 20 72 65 67 69 73 74 65 72 20 68 6f 6c hat register hol
212bd 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65 73 74 ding the largest
212be 0a 2a 2a 20 72 6f 77 69 64 20 76 61 6c 75 65 2e .** rowid value.
212bf 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 Code generated
212c0 20 62 79 20 61 75 74 6f 49 6e 63 45 6e 64 28 29 by autoIncEnd()
212c1 20 77 69 6c 6c 20 77 72 69 74 65 20 74 68 65 20 will write the
212c2 6e 65 77 0a 2a 2a 20 6c 61 72 67 65 73 74 20 76 new.** largest v
212c3 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 75 6e alue of the coun
212c4 74 65 72 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 ter back into th
212c5 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 e sqlite_sequenc
212c6 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 e table..**.** T
212c7 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
212c8 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 rns the index of
212c9 20 74 68 65 20 6d 65 6d 5b 5d 20 63 65 6c 6c 20 the mem[] cell
212ca 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a that contains.**
212cb 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 the maximum row
212cc 69 64 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a id counter..**.*
212cd 2a 20 54 68 72 65 65 20 63 6f 6e 73 65 63 75 74 * Three consecut
212ce 69 76 65 20 72 65 67 69 73 74 65 72 73 20 61 72 ive registers ar
212cf 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 e allocated by t
212d0 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 his routine. Th
212d1 65 0a 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 68 e.** first two h
212d2 6f 6c 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 old the name of
212d3 74 68 65 20 74 61 72 67 65 74 20 74 61 62 6c 65 the target table
212d4 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d and the maximum
212d5 20 72 6f 77 69 64 20 0a 2a 2a 20 69 6e 73 65 72 rowid .** inser
212d6 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 61 72 ted into the tar
212d7 67 65 74 20 74 61 62 6c 65 2c 20 72 65 73 70 65 get table, respe
212d8 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 54 68 65 20 ctively..** The
212d9 74 68 69 72 64 20 68 6f 6c 64 73 20 74 68 65 20 third holds the
212da 72 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f rowid in sqlite_
212db 73 65 71 75 65 6e 63 65 20 77 68 65 72 65 20 77 sequence where w
212dc 65 20 77 69 6c 6c 0a 2a 2a 20 77 72 69 74 65 20 e will.** write
212dd 62 61 63 6b 20 74 68 65 20 72 65 76 69 73 65 64 back the revised
212de 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 2e 20 maximum rowid.
212df 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
212e0 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64 turns the.** ind
212e1 65 78 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 ex of the second
212e2 20 6f 66 20 74 68 65 73 65 20 74 68 72 65 65 20 of these three
212e3 72 65 67 69 73 74 65 72 73 2e 0a 2a 2f 0a 73 74 registers..*/.st
212e4 61 74 69 63 20 69 6e 74 20 61 75 74 6f 49 6e 63 atic int autoInc
212e5 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a Begin(. Parse *
212e6 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 pParse, /*
212e7 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
212e8 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 */. int iDb,
212e9 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
212ea 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 x of the databas
212eb 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a e holding pTab *
212ec 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 /. Table *pTab
212ed 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
212ee 61 62 6c 65 20 77 65 20 61 72 65 20 77 72 69 74 able we are writ
212ef 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 69 ing to */.){. i
212f0 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b 20 20 20 nt memId = 0;
212f1 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
212f2 6f 6c 64 69 6e 67 20 6d 61 78 69 6d 75 6d 20 72 olding maximum r
212f3 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28 20 70 54 owid */. if( pT
212f4 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 ab->tabFlags & T
212f5 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 F_Autoincrement
212f6 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d ){. Vdbe *v =
212f7 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a pParse->pVdbe;.
212f8 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 70 Db *pDb = &p
212f9 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 Parse->db->aDb[i
212fa 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 69 43 75 Db];. int iCu
212fb 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 r = pParse->nTab
212fc 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20 ;. int addr;
212fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
212fe 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 Address of the
212ff 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 top of the loop
21300 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 76 */. assert( v
21301 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e );. pParse->
21302 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 nMem++;
21303 2f 2a 20 48 6f 6c 64 73 20 6e 61 6d 65 20 6f 66 /* Holds name of
21304 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 table */. me
21305 6d 49 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e mId = ++pParse->
21306 6e 4d 65 6d 3b 0a 20 20 20 20 70 50 61 72 73 65 nMem;. pParse
21307 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73 71 ->nMem++;. sq
21308 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 lite3OpenTable(p
21309 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 Parse, iCur, iDb
2130a 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e , pDb->pSchema->
2130b 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e pSeqTab, OP_Open
2130c 52 65 61 64 29 3b 0a 20 20 20 20 61 64 64 72 20 Read);. addr
2130d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 = sqlite3VdbeCur
2130e 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 rentAddr(v);.
2130f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21310 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 p4(v, OP_String8
21311 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c , 0, memId-1, 0,
21312 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 pTab->zName, 0)
21313 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21314 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 eAddOp2(v, OP_Re
21315 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64 72 wind, iCur, addr
21316 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 +9);. sqlite3
21317 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21318 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 30 _Column, iCur, 0
21319 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 , memId);. sq
2131a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
2131b 76 2c 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d v, OP_Ne, memId-
2131c 31 2c 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64 1, addr+7, memId
2131d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
2131e 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 beChangeP5(v, SQ
2131f 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 LITE_JUMPIFNULL)
21320 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21321 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f eAddOp2(v, OP_Ro
21322 77 69 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49 64 wid, iCur, memId
21323 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 +1);. sqlite3
21324 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21325 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 31 _Column, iCur, 1
21326 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 , memId);. sq
21327 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21328 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 v, OP_Goto, 0, a
21329 64 64 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 ddr+9);. sqli
2132a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
2132b 20 4f 50 5f 4e 65 78 74 2c 20 69 43 75 72 2c 20 OP_Next, iCur,
2132c 61 64 64 72 2b 32 29 3b 0a 20 20 20 20 73 71 6c addr+2);. sql
2132d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
2132e 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c , OP_Integer, 0,
2132f 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c memId);. sql
21330 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21331 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 , OP_Close, iCur
21332 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , 0);. }. retu
21333 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a rn memId;.}../*.
21334 2a 2a 20 55 70 64 61 74 65 20 74 68 65 20 6d 61 ** Update the ma
21335 78 69 6d 75 6d 20 72 6f 77 69 64 20 66 6f 72 20 ximum rowid for
21336 61 6e 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 an autoincrement
21337 20 63 61 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a calculation..**
21338 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
21339 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 should be calle
2133a 64 20 77 68 65 6e 20 74 68 65 20 74 6f 70 20 6f d when the top o
2133b 66 20 74 68 65 20 73 74 61 63 6b 20 68 6f 6c 64 f the stack hold
2133c 73 20 61 0a 2a 2a 20 6e 65 77 20 72 6f 77 69 64 s a.** new rowid
2133d 20 74 68 61 74 20 69 73 20 61 62 6f 75 74 20 74 that is about t
2133e 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 o be inserted.
2133f 49 66 20 74 68 61 74 20 6e 65 77 20 72 6f 77 69 If that new rowi
21340 64 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 d is.** larger t
21341 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 han the maximum
21342 72 6f 77 69 64 20 69 6e 20 74 68 65 20 6d 65 6d rowid in the mem
21343 49 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c 20 Id memory cell,
21344 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f then the.** memo
21345 72 79 20 63 65 6c 6c 20 69 73 20 75 70 64 61 74 ry cell is updat
21346 65 64 2e 20 20 54 68 65 20 73 74 61 63 6b 20 69 ed. The stack i
21347 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a s unchanged..*/.
21348 73 74 61 74 69 63 20 76 6f 69 64 20 61 75 74 6f static void auto
21349 49 6e 63 53 74 65 70 28 50 61 72 73 65 20 2a 70 IncStep(Parse *p
2134a 50 61 72 73 65 2c 20 69 6e 74 20 6d 65 6d 49 64 Parse, int memId
2134b 2c 20 69 6e 74 20 72 65 67 52 6f 77 69 64 29 7b , int regRowid){
2134c 0a 20 20 69 66 28 20 6d 65 6d 49 64 3e 30 20 29 . if( memId>0 )
2134d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
2134e 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e eAddOp2(pParse->
2134f 70 56 64 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78 pVdbe, OP_MemMax
21350 2c 20 6d 65 6d 49 64 2c 20 72 65 67 52 6f 77 69 , memId, regRowi
21351 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a d);. }.}../*.**
21352 20 41 66 74 65 72 20 64 6f 69 6e 67 20 6f 6e 65 After doing one
21353 20 6f 72 20 6d 6f 72 65 20 69 6e 73 65 72 74 73 or more inserts
21354 2c 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f , the maximum ro
21355 77 69 64 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a wid is stored.**
21356 20 69 6e 20 72 65 67 5b 6d 65 6d 49 64 5d 2e 20 in reg[memId].
21357 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
21358 6f 20 77 72 69 74 65 20 74 68 69 73 20 76 61 6c o write this val
21359 75 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 ue back into the
2135a 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 73 .** the sqlite_s
2135b 65 71 75 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a equence table..*
2135c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 75 /.static void au
2135d 74 6f 49 6e 63 45 6e 64 28 0a 20 20 50 61 72 73 toIncEnd(. Pars
2135e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f e *pParse, /
2135f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f * The parsing co
21360 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ntext */. int i
21361 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a Db, /*
21362 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 Index of the da
21363 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 tabase holding p
21364 54 61 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a Tab */. Table *
21365 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 pTab, /* T
21366 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 able we are inse
21367 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 rting into */.
21368 69 6e 74 20 6d 65 6d 49 64 20 20 20 20 20 20 20 int memId
21369 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c /* Memory cel
2136a 6c 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6d 61 l holding the ma
2136b 78 69 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0a 29 ximum rowid */.)
2136c 7b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 {. if( pTab->ta
2136d 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f bFlags & TF_Auto
2136e 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 increment ){.
2136f 20 69 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72 int iCur = pPar
21370 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 56 64 se->nTab;. Vd
21371 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e be *v = pParse->
21372 70 56 64 62 65 3b 0a 20 20 20 20 44 62 20 2a 70 pVdbe;. Db *p
21373 44 62 20 3d 20 26 70 50 61 72 73 65 2d 3e 64 62 Db = &pParse->db
21374 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 ->aDb[iDb];.
21375 69 6e 74 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20 int j1;. int
21376 69 52 65 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d iRec = ++pParse-
21377 3e 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 4d 65 6d >nMem; /* Mem
21378 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f ory cell used fo
21379 72 20 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 20 r record */..
2137a 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 assert( v );.
2137b 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 sqlite3OpenTab
2137c 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c le(pParse, iCur,
2137d 20 69 44 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 iDb, pDb->pSche
2137e 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f ma->pSeqTab, OP_
2137f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 OpenWrite);.
21380 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j1 = sqlite3Vdbe
21381 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 AddOp1(v, OP_Not
21382 4e 75 6c 6c 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a Null, memId+1);.
21383 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21384 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 ddOp2(v, OP_NewR
21385 6f 77 69 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49 owid, iCur, memI
21386 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 d+1);. sqlite
21387 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
21388 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 j1);. sqlite
21389 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
2138a 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 P_MakeRecord, me
2138b 6d 49 64 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b mId-1, 2, iRec);
2138c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
2138d 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 AddOp3(v, OP_Ins
2138e 65 72 74 2c 20 69 43 75 72 2c 20 69 52 65 63 2c ert, iCur, iRec,
2138f 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73 memId+1);. s
21390 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
21391 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 P5(v, OPFLAG_APP
21392 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 END);. sqlite
21393 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
21394 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a P_Close, iCur);.
21395 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a }.}.#else./*.*
21396 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 * If SQLITE_OMIT
21397 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 _AUTOINCREMENT i
21398 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 s defined, then
21399 74 68 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e the three routin
2139a 65 73 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 es.** above are
2139b 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 all no-ops.*/.#
2139c 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 define autoIncBe
2139d 67 69 6e 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 gin(A,B,C) (0).#
2139e 20 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 define autoIncS
2139f 74 65 70 28 41 2c 42 2c 43 29 0a 23 20 64 65 66 tep(A,B,C).# def
213a0 69 6e 65 20 61 75 74 6f 49 6e 63 45 6e 64 28 41 ine autoIncEnd(A
213a1 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 20 2f ,B,C,D).#endif /
213a2 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 * SQLITE_OMIT_AU
213a3 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a TOINCREMENT */..
213a4 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c ./* Forward decl
213a5 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 aration */.stati
213a6 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69 6d 69 c int xferOptimi
213a7 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 zation(. Parse
213a8 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
213a9 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 /* Parser contex
213aa 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44 t */. Table *pD
213ab 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 est, /*
213ac 54 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 The table we are
213ad 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 20 inserting into
213ae 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 */. Select *pSe
213af 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 lect, /* A
213b0 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
213b1 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64 to use as the d
213b2 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 ata source */.
213b3 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 int onError,
213b4 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 /* How to
213b5 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e handle constrain
213b6 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e t errors */. in
213b7 74 20 69 44 62 44 65 73 74 20 20 20 20 20 20 20 t iDbDest
213b8 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
213b9 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a ase of pDest */.
213ba 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 );../*.** This r
213bb 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 20 74 outine is call t
213bc 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20 o handle SQL of
213bd 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f the following fo
213be 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e rms:.**.** in
213bf 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 sert into TABLE
213c0 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65 73 28 (IDLIST) values(
213c1 45 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20 EXPRLIST).**
213c2 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c insert into TABL
213c3 45 20 28 49 44 4c 49 53 54 29 20 73 65 6c 65 63 E (IDLIST) selec
213c4 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49 t.**.** The IDLI
213c5 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ST following the
213c6 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 61 table name is a
213c7 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 lways optional.
213c8 20 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20 If omitted,.**
213c9 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 then a list of a
213ca 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 ll columns for t
213cb 68 65 20 74 61 62 6c 65 20 69 73 20 73 75 62 73 he table is subs
213cc 74 69 74 75 74 65 64 2e 20 20 54 68 65 20 49 44 tituted. The ID
213cd 4c 49 53 54 0a 2a 2a 20 61 70 70 65 61 72 73 20 LIST.** appears
213ce 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70 in the pColumn p
213cf 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75 arameter. pColu
213d0 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20 49 44 mn is NULL if ID
213d1 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65 64 2e LIST is omitted.
213d2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74 .**.** The pList
213d3 20 70 61 72 61 6d 65 74 65 72 20 68 6f 6c 64 73 parameter holds
213d4 20 45 58 50 52 4c 49 53 54 20 69 6e 20 74 68 65 EXPRLIST in the
213d5 20 66 69 72 73 74 20 66 6f 72 6d 20 6f 66 20 74 first form of t
213d6 68 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61 he INSERT.** sta
213d7 74 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e tement above, an
213d8 64 20 70 53 65 6c 65 63 74 20 69 73 20 4e 55 4c d pSelect is NUL
213d9 4c 2e 20 20 46 6f 72 20 74 68 65 20 73 65 63 6f L. For the seco
213da 6e 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69 nd form, pList i
213db 73 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53 s.** NULL and pS
213dc 65 6c 65 63 74 20 69 73 20 61 20 70 6f 69 6e 74 elect is a point
213dd 65 72 20 74 6f 20 74 68 65 20 73 65 6c 65 63 74 er to the select
213de 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20 statement used
213df 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64 to generate.** d
213e0 61 74 61 20 66 6f 72 20 74 68 65 20 69 6e 73 65 ata for the inse
213e1 72 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f rt..**.** The co
213e2 64 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f 6c de generated fol
213e3 6c 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72 lows one of four
213e4 20 74 65 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72 templates. For
213e5 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c a simple.** sel
213e6 65 63 74 20 77 69 74 68 20 64 61 74 61 20 63 6f ect with data co
213e7 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55 ming from a VALU
213e8 45 53 20 63 6c 61 75 73 65 2c 20 74 68 65 20 63 ES clause, the c
213e9 6f 64 65 20 65 78 65 63 75 74 65 73 0a 2a 2a 20 ode executes.**
213ea 6f 6e 63 65 20 73 74 72 61 69 67 68 74 20 64 6f once straight do
213eb 77 6e 20 74 68 72 6f 75 67 68 2e 20 20 50 73 65 wn through. Pse
213ec 75 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 udo-code follows
213ed 20 28 77 65 20 63 61 6c 6c 20 74 68 69 73 0a 2a (we call this.*
213ee 2a 20 74 68 65 20 22 31 73 74 20 74 65 6d 70 6c * the "1st templ
213ef 61 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ate"):.**.**
213f0 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74 65 20 open write
213f1 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 cursor to <table
213f2 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 > and its indice
213f3 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75 74 s.** put
213f4 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 s VALUES clause
213f5 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f expressions onto
213f6 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20 the stack.**
213f7 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65 20 write the
213f8 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72 64 resulting record
213f9 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a into <table>.**
213fa 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 cleanup
213fb 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 65 .**.** The three
213fc 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c remaining templ
213fd 61 74 65 73 20 61 73 73 75 6d 65 20 74 68 65 20 ates assume the
213fe 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 statement is of
213ff 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 the form.**.**
21400 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 INSERT INTO <ta
21401 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a ble> SELECT ....
21402 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45 4c **.** If the SEL
21403 45 43 54 20 63 6c 61 75 73 65 20 69 73 20 6f 66 ECT clause is of
21404 20 74 68 65 20 72 65 73 74 72 69 63 74 65 64 20 the restricted
21405 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a 20 46 form "SELECT * F
21406 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a ROM <table2>" -.
21407 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 ** in other word
21408 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 s if the SELECT
21409 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e pulls all column
2140a 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 s from a single
2140b 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 table.** and the
2140c 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45 20 6f re is no WHERE o
2140d 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55 50 r LIMIT or GROUP
2140e 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20 BY or ORDER BY
2140f 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20 clauses, and.**
21410 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20 if <table2> and
21411 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64 69 73 <table1> are dis
21412 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62 75 74 tinct tables but
21413 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c 0a have identical.
21414 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c ** schemas, incl
21415 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20 73 61 uding all the sa
21416 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68 65 6e me indices, then
21417 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d a special optim
21418 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e ization.** is in
21419 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70 69 65 voked that copie
2141a 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72 s raw records fr
2141b 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 om <table2> over
2141c 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a to <table1>..**
2141d 20 53 65 65 20 74 68 65 20 78 66 65 72 4f 70 74 See the xferOpt
2141e 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63 imization() func
2141f 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d 70 tion for the imp
21420 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
21421 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e his.** template.
21422 20 20 54 68 69 73 20 69 73 20 74 68 65 20 32 6e This is the 2n
21423 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a d template..**.*
21424 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 * open a
21425 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f write cursor to
21426 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 <table>.**
21427 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20 63 75 open read cu
21428 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65 32 3e rsor on <table2>
21429 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72 61 6e .** tran
2142a 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73 sfer all records
2142b 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 in <table2> ove
2142c 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 r to <table>.**
2142d 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75 close cu
2142e 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 rsors.**
2142f 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78 20 6f foreach index o
21430 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 n <table>.**
21431 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72 open a wr
21432 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 ite cursor on th
21433 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65 78 0a e <table> index.
21434 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70 65 ** ope
21435 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20 n a read cursor
21436 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e on the correspon
21437 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20 69 6e ding <table2> in
21438 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 dex.**
21439 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 transfer all re
2143a 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 72 cords from the r
2143b 65 61 64 20 74 6f 20 74 68 65 20 77 72 69 74 65 ead to the write
2143c 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 cursors.**
2143d 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72 73 close curs
2143e 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 ors.** e
2143f 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a 2a nd foreach.**.**
21440 20 54 68 65 20 33 72 64 20 74 65 6d 70 6c 61 74 The 3rd templat
21441 65 20 69 73 20 66 6f 72 20 77 68 65 6e 20 74 68 e is for when th
21442 65 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74 e second templat
21443 65 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 6c 79 e does not apply
21444 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45 .** and the SELE
21445 43 54 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e CT clause does n
21446 6f 74 20 72 65 61 64 20 66 72 6f 6d 20 3c 74 61 ot read from <ta
21447 62 6c 65 3e 20 61 74 20 61 6e 79 20 74 69 6d 65 ble> at any time
21448 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74 ..** The generat
21449 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 ed code follows
2144a 74 68 69 73 20 74 65 6d 70 6c 61 74 65 3a 0a 2a this template:.*
2144b 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 *.** EOF
2144c 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20 <- 0.**
2144d 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20 X <- A.**
2144e 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 goto B.**
2144f 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 A: setup for t
21450 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 he SELECT.**
21451 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 loop over t
21452 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20 53 he rows in the S
21453 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 ELECT.**
21454 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 73 20 69 load values i
21455 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20 52 2e nto registers R.
21456 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 .R+n.**
21457 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 yield X.**
21458 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a end loop.**
21459 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 cleanup
2145a 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43 after the SELEC
2145b 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 T.** EOF
2145c 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 <- 1.**
2145d 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 yield X.**
2145e 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20 20 20 goto A.**
2145f 20 20 20 42 3a 20 6f 70 65 6e 20 77 72 69 74 65 B: open write
21460 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c cursor to <tabl
21461 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 e> and its indic
21462 65 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 es.** C: yi
21463 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 eld X.**
21464 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a 2a if EOF goto D.*
21465 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 * insert
21466 20 74 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 the select resu
21467 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 lt into <table>
21468 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 from R..R+n.**
21469 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 2a 2a goto C.**
2146a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e 75 70 D: cleanup
2146b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68 20 74 .**.** The 4th t
2146c 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65 64 20 emplate is used
2146d 69 66 20 74 68 65 20 69 6e 73 65 72 74 20 73 74 if the insert st
2146e 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20 69 74 atement takes it
2146f 73 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72 6f 6d s.** values from
21470 20 61 20 53 45 4c 45 43 54 20 62 75 74 20 74 68 a SELECT but th
21471 65 20 64 61 74 61 20 69 73 20 62 65 69 6e 67 20 e data is being
21472 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 20 inserted into a
21473 74 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 table.** that is
21474 20 61 6c 73 6f 20 72 65 61 64 20 61 73 20 70 61 also read as pa
21475 72 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 rt of the SELECT
21476 2e 20 20 49 6e 20 74 68 65 20 74 68 69 72 64 20 . In the third
21477 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61 76 65 form,.** we have
21478 20 74 6f 20 75 73 65 20 61 20 69 6e 74 65 72 6d to use a interm
21479 65 64 69 61 74 65 20 74 61 62 6c 65 20 74 6f 20 ediate table to
2147a 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
2147b 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65 6c 65 s of.** the sele
2147c 63 74 2e 20 20 54 68 65 20 74 65 6d 70 6c 61 74 ct. The templat
2147d 65 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a e is like this:.
2147e 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f **.** EO
2147f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 F <- 0.**
21480 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 X <- A.**
21481 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 goto B.**
21482 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 A: setup for
21483 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 the SELECT.**
21484 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 loop over
21485 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 the tables in th
21486 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 e SELECT.**
21487 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 load value
21488 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52 into register R
21489 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ..R+n.**
2148a 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 yield X.**
2148b 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a end loop.*
2148c 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 * cleanu
2148d 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 p after the SELE
2148e 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f CT.** EO
2148f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 F <- 1.**
21490 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 yield X.**
21491 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a halt-error.
21492 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 ** B: open
21493 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 temp table.**
21494 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 2a 2a L: yield X.**
21495 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20 if EOF
21496 67 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20 20 20 goto M.**
21497 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f insert row fro
21498 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 m R..R+n into te
21499 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 mp table.**
2149a 20 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20 20 goto L.**
2149b 20 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69 74 65 M: open write
2149c 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c cursor to <tabl
2149d 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 e> and its indic
2149e 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 72 65 es.** re
2149f 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c 65 0a wind temp table.
214a0 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20 ** C: loop
214a1 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e 74 over rows of int
214a2 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 0a ermediate table.
214a3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 61 ** tra
214a4 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f 72 nsfer values for
214a5 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 m intermediate t
214a6 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 able into <table
214a7 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 >.** end
214a8 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20 44 3a loop.** D:
214a9 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c 49 cleanup.*/.SQLI
214aa 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
214ab 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 0a 20 sqlite3Insert(.
214ac 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
214ad 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 /* Parser
214ae 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 context */. Sr
214af 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c cList *pTabList,
214b0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
214b1 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 able into which
214b2 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 we are inserting
214b3 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
214b4 70 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 4c pList, /* L
214b5 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f ist of values to
214b6 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a be inserted */.
214b7 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 Select *pSelec
214b8 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c t, /* A SEL
214b9 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f ECT statement to
214ba 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61 use as the data
214bb 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49 64 4c source */. IdL
214bc 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 ist *pColumn,
214bd 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d /* Column nam
214be 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 es corresponding
214bf 20 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f 0a 20 to IDLIST. */.
214c0 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 int onError
214c1 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f /* How to
214c2 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 handle constrai
214c3 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a nt errors */.){.
214c4 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
214c5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d /* The m
214c6 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 ain database str
214c7 75 63 74 75 72 65 20 2a 2f 0a 20 20 54 61 62 6c ucture */. Tabl
214c8 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 e *pTab;
214c9 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 /* The table t
214ca 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 20 20 o insert into.
214cb 61 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 aka TABLE */. c
214cc 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 har *zTab;
214cd 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
214ce 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 the table into w
214cf 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 hich we are inse
214d0 72 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 rting */. const
214d1 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 char *zDb;
214d2 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
214d3 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 database holding
214d4 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 this table */.
214d5 20 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20 int i, j, idx;
214d6 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
214d7 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56 64 62 ounters */. Vdb
214d8 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 e *v;
214d9 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 /* Generate c
214da 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69 ode into this vi
214db 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f rtual machine */
214dc 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 . Index *pIdx;
214dd 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
214de 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 looping over ind
214df 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c ices of the tabl
214e0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 e */. int nColu
214e1 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 mn; /*
214e2 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e Number of column
214e3 73 20 69 6e 20 74 68 65 20 64 61 74 61 20 2a 2f s in the data */
214e4 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d . int nHidden =
214e5 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 0; /* Numb
214e6 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c er of hidden col
214e7 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20 69 73 umns if TABLE is
214e8 20 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20 69 6e virtual */. in
214e9 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b 20 20 t baseCur = 0;
214ea 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 /* VDBE Curs
214eb 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 or number for pT
214ec 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 43 ab */. int keyC
214ed 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f 2a olumn = -1; /*
214ee 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 Column that is
214ef 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d the INTEGER PRIM
214f0 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69 6e 74 ARY KEY */. int
214f1 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 endOfLoop;
214f2 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 /* Label for
214f3 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 the end of the i
214f4 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f nsertion loop */
214f5 0a 20 20 69 6e 74 20 75 73 65 54 65 6d 70 54 61 . int useTempTa
214f6 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74 6f 72 ble = 0; /* Stor
214f7 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73 e SELECT results
214f8 20 69 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65 in intermediate
214f9 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
214fa 73 72 63 54 61 62 20 3d 20 30 3b 20 20 20 20 20 srcTab = 0;
214fb 20 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65 73 20 /* Data comes
214fc 66 72 6f 6d 20 74 68 69 73 20 74 65 6d 70 6f 72 from this tempor
214fd 61 72 79 20 63 75 72 73 6f 72 20 69 66 20 3e 3d ary cursor if >=
214fe 30 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 49 0 */. int addrI
214ff 6e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f 2a 20 nsTop = 0; /*
21500 4a 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20 22 44 Jump to label "D
21501 22 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 " */. int addrC
21502 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 ont = 0; /*
21503 54 6f 70 20 6f 66 20 69 6e 73 65 72 74 20 6c 6f Top of insert lo
21504 6f 70 2e 20 4c 61 62 65 6c 20 22 43 22 20 69 6e op. Label "C" in
21505 20 74 65 6d 70 6c 61 74 65 73 20 33 20 61 6e 64 templates 3 and
21506 20 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4 */. int addr
21507 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 2f 2a Select = 0; /*
21508 20 41 64 64 72 65 73 73 20 6f 66 20 63 6f 72 6f Address of coro
21509 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 utine that imple
2150a 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43 54 ments the SELECT
2150b 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 */. SelectDest
2150c 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a 20 44 dest; /* D
2150d 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20 53 estination for S
2150e 45 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f 66 20 ELECT on rhs of
2150f 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 INSERT */. int
21510 6e 65 77 49 64 78 20 3d 20 2d 31 3b 20 20 20 20 newIdx = -1;
21511 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 6f 72 20 /* Cursor for
21512 74 68 65 20 4e 45 57 20 70 73 65 75 64 6f 2d 74 the NEW pseudo-t
21513 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 able */. int iD
21514 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
21515 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 /* Index of data
21516 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 54 41 42 base holding TAB
21517 4c 45 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b LE */. Db *pDb;
21518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21519 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
2151a 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 62 ntaining table b
2151b 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e eing inserted in
2151c 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 to */. int appe
2151d 6e 64 46 6c 61 67 20 3d 20 30 3b 20 20 20 2f 2a ndFlag = 0; /*
2151e 20 54 72 75 65 20 69 66 20 74 68 65 20 69 6e 73 True if the ins
2151f 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f ert is likely to
21520 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f be an append */
21521 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 .. /* Register
21522 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 allocations */.
21523 20 69 6e 74 20 72 65 67 46 72 6f 6d 53 65 6c 65 int regFromSele
21524 63 74 3b 20 20 20 20 2f 2a 20 42 61 73 65 20 72 ct; /* Base r
21525 65 67 69 73 74 65 72 20 66 6f 72 20 64 61 74 61 egister for data
21526 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 53 45 4c coming from SEL
21527 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 ECT */. int reg
21528 41 75 74 6f 69 6e 63 20 3d 20 30 3b 20 20 20 2f Autoinc = 0; /
21529 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 * Register holdi
2152a 6e 67 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45 ng the AUTOINCRE
2152b 4d 45 4e 54 20 63 6f 75 6e 74 65 72 20 2a 2f 0a MENT counter */.
2152c 20 20 69 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e int regRowCoun
2152d 74 20 3d 20 30 3b 20 20 2f 2a 20 4d 65 6d 6f 72 t = 0; /* Memor
2152e 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 y cell used for
2152f 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65 72 20 the row counter
21530 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 6e 73 3b */. int regIns;
21531 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c /* Bl
21532 6f 63 6b 20 6f 66 20 72 65 67 73 20 68 6f 6c 64 ock of regs hold
21533 69 6e 67 20 72 6f 77 69 64 2b 64 61 74 61 20 62 ing rowid+data b
21534 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 2a 2f eing inserted */
21535 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b . int regRowid;
21536 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67 69 /* regi
21537 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 69 6e sters holding in
21538 73 65 72 74 20 72 6f 77 69 64 20 2a 2f 0a 20 20 sert rowid */.
21539 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20 20 int regData;
2153a 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65 /* registe
2153b 72 20 68 6f 6c 64 69 6e 67 20 66 69 72 73 74 20 r holding first
2153c 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e 73 65 72 74 column to insert
2153d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63 */. int regRec
2153e 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 48 ord; /* H
2153f 6f 6c 64 73 20 74 68 65 20 61 73 73 65 6d 62 6c olds the assembl
21540 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64 20 2a ied row record *
21541 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 3b 20 /. int regEof;
21542 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
21543 69 73 74 65 72 20 72 65 63 6f 72 64 69 6e 67 20 ister recording
21544 65 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 64 61 end of SELECT da
21545 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 ta */. int *aRe
21546 67 49 64 78 20 3d 20 30 3b 20 20 20 20 20 2f 2a gIdx = 0; /*
21547 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61 6c One register al
21548 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 20 located to each
21549 69 6e 64 65 78 20 2a 2f 0a 0a 0a 23 69 66 6e 64 index */...#ifnd
2154a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
2154b 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56 RIGGER. int isV
2154c 69 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 iew;
2154d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
2154e 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 69 6e attempting to in
2154f 73 65 72 74 20 69 6e 74 6f 20 61 20 76 69 65 77 sert into a view
21550 20 2a 2f 0a 20 20 69 6e 74 20 74 72 69 67 67 65 */. int trigge
21551 72 73 5f 65 78 69 73 74 20 3d 20 30 3b 20 20 20 rs_exist = 0;
21552 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
21553 72 65 20 61 72 65 20 46 4f 52 20 45 41 43 48 20 re are FOR EACH
21554 52 4f 57 20 74 72 69 67 67 65 72 73 20 2a 2f 0a ROW triggers */.
21555 23 65 6e 64 69 66 0a 0a 20 20 64 62 20 3d 20 70 #endif.. db = p
21556 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 Parse->db;. if(
21557 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c pParse->nErr ||
21558 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
21559 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e d ){. goto in
2155a 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 sert_cleanup;.
2155b 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 }.. /* Locate t
2155c 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 he table into wh
2155d 69 63 68 20 77 65 20 77 69 6c 6c 20 62 65 20 69 ich we will be i
2155e 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 69 6e 66 nserting new inf
2155f 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 ormation.. */.
21560 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 assert( pTabLis
21561 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 t->nSrc==1 );.
21562 7a 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d zTab = pTabList-
21563 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 >a[0].zName;. i
21564 66 28 20 7a 54 61 62 3d 3d 30 20 29 20 67 6f 74 f( zTab==0 ) got
21565 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 o insert_cleanup
21566 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 ;. pTab = sqlit
21567 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 e3SrcListLookup(
21568 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 pParse, pTabList
21569 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 );. if( pTab==0
2156a 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 ){. goto ins
2156b 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d ert_cleanup;. }
2156c 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 . iDb = sqlite3
2156d 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
2156e 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 , pTab->pSchema)
2156f 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3c ;. assert( iDb<
21570 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 44 62 db->nDb );. pDb
21571 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d = &db->aDb[iDb]
21572 3b 0a 20 20 7a 44 62 20 3d 20 70 44 62 2d 3e 7a ;. zDb = pDb->z
21573 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c 69 Name;. if( sqli
21574 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
21575 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 rse, SQLITE_INSE
21576 52 54 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c RT, pTab->zName,
21577 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 0, zDb) ){.
21578 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 goto insert_clea
21579 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 nup;. }.. /* F
2157a 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 igure out if we
2157b 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 have any trigger
2157c 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 s and if the tab
2157d 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e le being. ** in
2157e 73 65 72 74 65 64 20 69 6e 74 6f 20 69 73 20 61 serted into is a
2157f 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 view. */.#ifnd
21580 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
21581 52 49 47 47 45 52 0a 20 20 74 72 69 67 67 65 72 RIGGER. trigger
21582 73 5f 65 78 69 73 74 20 3d 20 73 71 6c 69 74 65 s_exist = sqlite
21583 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70 3TriggersExist(p
21584 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f Parse, pTab, TK_
21585 49 4e 53 45 52 54 2c 20 30 29 3b 0a 20 20 69 73 INSERT, 0);. is
21586 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 View = pTab->pSe
21587 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 lect!=0;.#else.#
21588 20 64 65 66 69 6e 65 20 74 72 69 67 67 65 72 73 define triggers
21589 5f 65 78 69 73 74 20 30 0a 23 20 64 65 66 69 6e _exist 0.# defin
2158a 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 e isView 0.#endi
2158b 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
2158c 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 OMIT_VIEW.# unde
2158d 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e f isView.# defin
2158e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 e isView 0.#endi
2158f 66 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 f.. /* Ensure t
21590 68 61 74 3a 0a 20 20 2a 20 20 28 61 29 20 74 68 hat:. * (a) th
21591 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 72 e table is not r
21592 65 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20 20 ead-only, . *
21593 28 62 29 20 74 68 61 74 20 69 66 20 69 74 20 69 (b) that if it i
21594 73 20 61 20 76 69 65 77 20 74 68 65 6e 20 4f 4e s a view then ON
21595 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 73 INSERT triggers
21596 20 65 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69 66 exist. */. if
21597 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f ( sqlite3IsReadO
21598 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 nly(pParse, pTab
21599 2c 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 , triggers_exist
2159a 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e ) ){. goto in
2159b 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 sert_cleanup;.
2159c 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 }. assert( pTab
2159d 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 !=0 );.. /* If
2159e 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20 61 pTab is really a
2159f 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72 65 view, make sure
215a0 20 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e 69 it has been ini
215a1 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a 20 56 tialized.. ** V
215a2 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 iewGetColumnName
215a3 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 s() is a no-op i
215a4 66 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20 f pTab is not a
215a5 76 69 65 77 20 28 6f 72 20 76 69 72 74 75 61 6c view (or virtual
215a6 20 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 74 61 . ** module ta
215a7 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ble).. */. if(
215a8 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 sqlite3ViewGetC
215a9 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 olumnNames(pPars
215aa 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 e, pTab) ){.
215ab 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 goto insert_clea
215ac 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 nup;. }.. /* A
215ad 6c 6c 6f 63 61 74 65 20 61 20 56 44 42 45 0a 20 llocate a VDBE.
215ae 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */. v = sqlite
215af 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
215b0 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 ;. if( v==0 ) g
215b1 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e oto insert_clean
215b2 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 up;. if( pParse
215b3 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 ->nested==0 ) sq
215b4 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 lite3VdbeCountCh
215b5 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 anges(v);. sqli
215b6 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 te3BeginWriteOpe
215b7 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 ration(pParse, p
215b8 53 65 6c 65 63 74 20 7c 7c 20 74 72 69 67 67 65 Select || trigge
215b9 72 73 5f 65 78 69 73 74 2c 20 69 44 62 29 3b 0a rs_exist, iDb);.
215ba 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 61 . /* if there a
215bb 72 65 20 72 6f 77 20 74 72 69 67 67 65 72 73 2c re row triggers,
215bc 20 61 6c 6c 6f 63 61 74 65 20 61 20 74 65 6d 70 allocate a temp
215bd 20 74 61 62 6c 65 20 66 6f 72 20 6e 65 77 2e 2a table for new.*
215be 20 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a references. */.
215bf 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 if( triggers_e
215c0 78 69 73 74 20 29 7b 0a 20 20 20 20 6e 65 77 49 xist ){. newI
215c1 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 dx = pParse->nTa
215c2 62 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 b++;. }..#ifnde
215c3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 f SQLITE_OMIT_XF
215c4 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66 20 74 ER_OPT. /* If t
215c5 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 he statement is
215c6 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20 2a 2a of the form. **
215c7 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 53 45 . ** INSE
215c8 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 31 3e RT INTO <table1>
215c9 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c SELECT * FROM <
215ca 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a 20 20 table2>;. **.
215cb 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 61 6c 20 ** Then special
215cc 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 63 61 optimizations ca
215cd 6e 20 62 65 20 61 70 70 6c 69 65 64 20 74 68 61 n be applied tha
215ce 74 20 6d 61 6b 65 20 74 68 65 20 74 72 61 6e 73 t make the trans
215cf 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 20 66 61 fer. ** very fa
215d0 73 74 20 61 6e 64 20 77 68 69 63 68 20 72 65 64 st and which red
215d1 75 63 65 20 66 72 61 67 6d 65 6e 74 61 74 69 6f uce fragmentatio
215d2 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a 20 20 n of indices..
215d3 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 **. ** This is
215d4 74 68 65 20 32 6e 64 20 74 65 6d 70 6c 61 74 65 the 2nd template
215d5 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f .. */. if( pCo
215d6 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65 72 4f lumn==0 && xferO
215d7 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50 61 72 ptimization(pPar
215d8 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c 65 63 se, pTab, pSelec
215d9 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44 62 29 t, onError, iDb)
215da 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
215db 21 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 !triggers_exist
215dc 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
215dd 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 67 List==0 );. g
215de 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e oto insert_clean
215df 75 70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f up;. }.#endif /
215e0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 * SQLITE_OMIT_XF
215e1 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20 2f 2a 20 ER_OPT */.. /*
215e2 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 41 55 If this is an AU
215e3 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c TOINCREMENT tabl
215e4 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 e, look up the s
215e5 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 equence number i
215e6 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 n the. ** sqlit
215e7 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 e_sequence table
215e8 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e and store it in
215e9 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 72 65 67 memory cell reg
215ea 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f 0a 20 20 Autoinc.. */.
215eb 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75 74 regAutoinc = aut
215ec 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65 oIncBegin(pParse
215ed 2c 20 69 44 62 2c 20 70 54 61 62 29 3b 0a 0a 20 , iDb, pTab);..
215ee 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 /* Figure out h
215ef 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 ow many columns
215f0 6f 66 20 64 61 74 61 20 61 72 65 20 73 75 70 70 of data are supp
215f1 6c 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61 lied. If the da
215f2 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f 6d 69 6e ta. ** is comin
215f3 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 g from a SELECT
215f4 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 statement, then
215f5 67 65 6e 65 72 61 74 65 20 61 20 63 6f 2d 72 6f generate a co-ro
215f6 75 74 69 6e 65 20 74 68 61 74 0a 20 20 2a 2a 20 utine that. **
215f7 70 72 6f 64 75 63 65 73 20 61 20 73 69 6e 67 6c produces a singl
215f8 65 20 72 6f 77 20 6f 66 20 74 68 65 20 53 45 4c e row of the SEL
215f9 45 43 54 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f ECT on each invo
215fa 63 61 74 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a cation. The. *
215fb 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20 * co-routine is
215fc 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 the common heade
215fd 72 20 74 6f 20 74 68 65 20 33 72 64 20 61 6e 64 r to the 3rd and
215fe 20 34 74 68 20 74 65 6d 70 6c 61 74 65 73 2e 0a 4th templates..
215ff 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 */. if( pSele
21600 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20 44 61 74 ct ){. /* Dat
21601 61 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d a is coming from
21602 20 61 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 a SELECT. Gene
21603 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 rate code to imp
21604 6c 65 6d 65 6e 74 20 74 68 61 74 20 53 45 4c 45 lement that SELE
21605 43 54 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 63 CT. ** as a c
21606 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 o-routine. The
21607 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 code is common t
21608 6f 20 62 6f 74 68 20 74 68 65 20 33 72 64 20 61 o both the 3rd a
21609 6e 64 20 34 74 68 0a 20 20 20 20 2a 2a 20 74 65 nd 4th. ** te
2160a 6d 70 6c 61 74 65 73 3a 0a 20 20 20 20 2a 2a 0a mplates:. **.
2160b 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 45 ** E
2160c 4f 46 20 3c 2d 20 30 0a 20 20 20 20 2a 2a 20 20 OF <- 0. **
2160d 20 20 20 20 20 20 20 58 20 3c 2d 20 41 0a 20 20 X <- A.
2160e 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74 ** got
2160f 6f 20 42 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 o B. **
21610 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 A: setup for the
21611 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20 SELECT. **
21612 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 loop over
21613 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 the tables in t
21614 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a he SELECT. **
21615 20 20 20 20 20 20 20 20 20 20 20 6c 6f 61 64 20 load
21616 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 value into regis
21617 74 65 72 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a ter R..R+n. *
21618 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65 6c * yiel
21619 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 d X. **
2161a 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 20 20 20 end loop.
2161b 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e ** clean
2161c 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c up after the SEL
2161d 45 43 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 ECT. **
2161e 20 20 20 45 4f 46 20 3c 2d 20 31 0a 20 20 20 20 EOF <- 1.
2161f 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 ** yield
21620 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 X. **
21621 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a 20 20 20 halt-error.
21622 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 20 65 61 **. ** On ea
21623 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 ch invocation of
21624 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 2c the co-routine,
21625 20 69 74 20 70 75 74 73 20 61 20 73 69 6e 67 6c it puts a singl
21626 65 20 72 6f 77 20 6f 66 20 74 68 65 0a 20 20 20 e row of the.
21627 20 2a 2a 20 53 45 4c 45 43 54 20 72 65 73 75 6c ** SELECT resul
21628 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 t into registers
21629 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e 64 65 73 dest.iMem...des
2162a 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e 4d 65 6d t.iMem+dest.nMem
2162b 2d 31 2e 0a 20 20 20 20 2a 2a 20 28 54 68 65 73 -1.. ** (Thes
2162c 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 e output registe
2162d 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 rs are allocated
2162e 20 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 by sqlite3Selec
2162f 74 28 29 2e 29 20 20 57 68 65 6e 0a 20 20 20 20 t().) When.
21630 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 63 6f ** the SELECT co
21631 6d 70 6c 65 74 65 73 2c 20 69 74 20 73 65 74 73 mpletes, it sets
21632 20 74 68 65 20 45 4f 46 20 66 6c 61 67 20 73 74 the EOF flag st
21633 6f 72 65 64 20 69 6e 20 72 65 67 45 6f 66 2e 0a ored in regEof..
21634 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 */. int r
21635 63 2c 20 6a 31 3b 0a 0a 20 20 20 20 72 65 67 45 c, j1;.. regE
21636 6f 66 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e of = ++pParse->n
21637 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Mem;. sqlite3
21638 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21639 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 _Integer, 0, reg
2163a 45 6f 66 29 3b 20 20 20 20 20 20 2f 2a 20 45 4f Eof); /* EO
2163b 46 20 3c 2d 20 30 20 2a 2f 0a 20 20 20 20 56 64 F <- 0 */. Vd
2163c 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53 beComment((v, "S
2163d 45 4c 45 43 54 20 65 6f 66 20 66 6c 61 67 22 29 ELECT eof flag")
2163e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 );. sqlite3Se
2163f 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 lectDestInit(&de
21640 73 74 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e st, SRT_Coroutin
21641 65 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 e, ++pParse->nMe
21642 6d 29 3b 0a 20 20 20 20 61 64 64 72 53 65 6c 65 m);. addrSele
21643 63 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 ct = sqlite3Vdbe
21644 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32 CurrentAddr(v)+2
21645 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21646 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
21647 74 65 67 65 72 2c 20 61 64 64 72 53 65 6c 65 63 teger, addrSelec
21648 74 2d 31 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 t-1, dest.iParm)
21649 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 ;. j1 = sqlit
2164a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
2164b 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a OP_Goto, 0, 0);.
2164c 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 VdbeComment(
2164d 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65 72 20 53 (v, "Jump over S
2164e 45 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65 22 ELECT coroutine"
2164f 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f ));.. /* Reso
21650 6c 76 65 20 74 68 65 20 65 78 70 72 65 73 73 69 lve the expressi
21651 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 ons in the SELEC
21652 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 T statement and
21653 65 78 65 63 75 74 65 20 69 74 2e 20 2a 2f 0a 20 execute it. */.
21654 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 rc = sqlite3S
21655 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 elect(pParse, pS
21656 65 6c 65 63 74 2c 20 26 64 65 73 74 29 3b 0a 20 elect, &dest);.
21657 20 20 20 69 66 28 20 72 63 20 7c 7c 20 70 50 61 if( rc || pPa
21658 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d rse->nErr || db-
21659 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
2165a 0a 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 . goto inse
2165b 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 rt_cleanup;.
2165c 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
2165d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
2165e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 teger, 1, regEof
2165f 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 4f ); /* EO
21660 46 20 3c 2d 20 31 20 2a 2f 0a 20 20 20 20 73 71 F <- 1 */. sq
21661 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
21662 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 v, OP_Yield, des
21663 74 2e 69 50 61 72 6d 29 3b 20 20 20 2f 2a 20 79 t.iParm); /* y
21664 69 65 6c 64 20 58 20 2a 2f 0a 20 20 20 20 73 71 ield X */. sq
21665 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21666 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 v, OP_Halt, SQLI
21667 54 45 5f 49 4e 54 45 52 4e 41 4c 2c 20 4f 45 5f TE_INTERNAL, OE_
21668 41 62 6f 72 74 29 3b 0a 20 20 20 20 56 64 62 65 Abort);. Vdbe
21669 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64 Comment((v, "End
2166a 20 6f 66 20 53 45 4c 45 43 54 20 63 6f 72 6f 75 of SELECT corou
2166b 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 73 71 6c tine"));. sql
2166c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
2166d 28 76 2c 20 6a 31 29 3b 20 20 20 20 20 20 20 20 (v, j1);
2166e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2166f 20 20 2f 2a 20 6c 61 62 65 6c 20 42 3a 20 2a 2f /* label B: */
21670 0a 0a 20 20 20 20 72 65 67 46 72 6f 6d 53 65 6c .. regFromSel
21671 65 63 74 20 3d 20 64 65 73 74 2e 69 4d 65 6d 3b ect = dest.iMem;
21672 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 . assert( pSe
21673 6c 65 63 74 2d 3e 70 45 4c 69 73 74 20 29 3b 0a lect->pEList );.
21674 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53 nColumn = pS
21675 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e elect->pEList->n
21676 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 Expr;. assert
21677 28 20 64 65 73 74 2e 6e 4d 65 6d 3d 3d 6e 43 6f ( dest.nMem==nCo
21678 6c 75 6d 6e 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 lumn );.. /*
21679 53 65 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65 Set useTempTable
2167a 20 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 to TRUE if the
2167b 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 result of the SE
2167c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 LECT statement.
2167d 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 ** should be
2167e 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 74 written into a t
2167f 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28 emporary table (
21680 74 65 6d 70 6c 61 74 65 20 34 29 2e 20 20 53 65 template 4). Se
21681 74 20 74 6f 0a 20 20 20 20 2a 2a 20 46 41 4c 53 t to. ** FALS
21682 45 20 69 66 20 65 61 63 68 2a 20 72 6f 77 20 6f E if each* row o
21683 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6e f the SELECT can
21684 20 62 65 20 77 72 69 74 74 65 6e 20 64 69 72 65 be written dire
21685 63 74 6c 79 20 69 6e 74 6f 0a 20 20 20 20 2a 2a ctly into. **
21686 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
21687 20 74 61 62 6c 65 20 28 74 65 6d 70 6c 61 74 65 table (template
21688 20 33 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 3).. **.
21689 2a 2a 20 41 20 74 65 6d 70 20 74 61 62 6c 65 20 ** A temp table
2168a 6d 75 73 74 20 62 65 20 75 73 65 64 20 69 66 20 must be used if
2168b 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
2168c 75 70 64 61 74 65 64 20 69 73 20 61 6c 73 6f 20 updated is also
2168d 6f 6e 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 one. ** of th
2168e 65 20 74 61 62 6c 65 73 20 62 65 69 6e 67 20 72 e tables being r
2168f 65 61 64 20 62 79 20 74 68 65 20 53 45 4c 45 43 ead by the SELEC
21690 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c T statement. Al
21691 73 6f 20 75 73 65 20 61 20 0a 20 20 20 20 2a 2a so use a . **
21692 20 74 65 6d 70 20 74 61 62 6c 65 20 69 6e 20 74 temp table in t
21693 68 65 20 63 61 73 65 20 6f 66 20 72 6f 77 20 74 he case of row t
21694 72 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f 0a riggers.. */.
21695 20 20 20 20 69 66 28 20 74 72 69 67 67 65 72 73 if( triggers
21696 5f 65 78 69 73 74 20 7c 7c 20 72 65 61 64 73 54 _exist || readsT
21697 61 62 6c 65 28 76 2c 20 61 64 64 72 53 65 6c 65 able(v, addrSele
21698 63 74 2c 20 69 44 62 2c 20 70 54 61 62 29 20 29 ct, iDb, pTab) )
21699 7b 0a 20 20 20 20 20 20 75 73 65 54 65 6d 70 54 {. useTempT
2169a 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a able = 1;. }.
2169b 0a 20 20 20 20 69 66 28 20 75 73 65 54 65 6d 70 . if( useTemp
2169c 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 2f Table ){. /
2169d 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 * Invoke the cor
2169e 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 63 outine to extrac
2169f 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 t information fr
216a0 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 om the SELECT.
216a1 20 20 20 20 2a 2a 20 61 6e 64 20 61 64 64 20 69 ** and add i
216a2 74 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 t to a transient
216a3 20 74 61 62 6c 65 20 73 72 63 54 61 62 2e 20 20 table srcTab.
216a4 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 The code generat
216a5 65 64 0a 20 20 20 20 20 20 2a 2a 20 68 65 72 65 ed. ** here
216a6 20 69 73 20 66 72 6f 6d 20 74 68 65 20 34 74 68 is from the 4th
216a7 20 74 65 6d 70 6c 61 74 65 3a 0a 20 20 20 20 20 template:.
216a8 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 **. **
216a9 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20 74 B: open temp t
216aa 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 able. **
216ab 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 20 20 L: yield X.
216ac 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 ** i
216ad 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0a 20 20 20 f EOF goto M.
216ae 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e ** in
216af 73 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52 2e sert row from R.
216b0 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20 74 .R+n into temp t
216b1 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 able. **
216b2 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a 20 20 20 goto L.
216b3 20 20 20 2a 2a 20 20 20 20 20 20 4d 3a 20 2e 2e ** M: ..
216b4 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
216b5 20 69 6e 74 20 72 65 67 52 65 63 3b 20 20 20 20 int regRec;
216b6 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f /* Register to
216b7 20 68 6f 6c 64 20 70 61 63 6b 65 64 20 72 65 63 hold packed rec
216b8 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ord */. int
216b9 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 2f 2a regRowid; /*
216ba 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c Register to hol
216bb 64 20 74 65 6d 70 20 74 61 62 6c 65 20 52 4f 57 d temp table ROW
216bc 49 44 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ID */. int
216bd 61 64 64 72 54 6f 70 3b 20 20 20 20 20 2f 2a 20 addrTop; /*
216be 4c 61 62 65 6c 20 22 4c 22 20 2a 2f 0a 20 20 20 Label "L" */.
216bf 20 20 20 69 6e 74 20 61 64 64 72 49 66 3b 20 20 int addrIf;
216c0 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f /* Address o
216c1 66 20 6a 75 6d 70 20 74 6f 20 4d 20 2a 2f 0a 0a f jump to M */..
216c2 20 20 20 20 20 20 73 72 63 54 61 62 20 3d 20 70 srcTab = p
216c3 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 Parse->nTab++;.
216c4 20 20 20 20 20 72 65 67 52 65 63 20 3d 20 73 71 regRec = sq
216c5 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
216c6 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72 pParse);. r
216c7 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 egRowid = sqlite
216c8 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 3GetTempReg(pPar
216c9 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 se);. sqlit
216ca 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
216cb 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
216cc 2c 20 73 72 63 54 61 62 2c 20 6e 43 6f 6c 75 6d , srcTab, nColum
216cd 6e 29 3b 0a 20 20 20 20 20 20 61 64 64 72 54 6f n);. addrTo
216ce 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 p = sqlite3VdbeA
216cf 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c ddOp1(v, OP_Yiel
216d0 64 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a d, dest.iParm);.
216d1 20 20 20 20 20 20 61 64 64 72 49 66 20 3d 20 73 addrIf = s
216d2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
216d3 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f (v, OP_If, regEo
216d4 66 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 f);. sqlite
216d5 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
216d6 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 P_MakeRecord, re
216d7 67 46 72 6f 6d 53 65 6c 65 63 74 2c 20 6e 43 6f gFromSelect, nCo
216d8 6c 75 6d 6e 2c 20 72 65 67 52 65 63 29 3b 0a 20 lumn, regRec);.
216d9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
216da 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 AddOp2(v, OP_New
216db 52 6f 77 69 64 2c 20 73 72 63 54 61 62 2c 20 72 Rowid, srcTab, r
216dc 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 egRowid);.
216dd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
216de 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 3(v, OP_Insert,
216df 73 72 63 54 61 62 2c 20 72 65 67 52 65 63 2c 20 srcTab, regRec,
216e0 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 regRowid);.
216e1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
216e2 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 p2(v, OP_Goto, 0
216e3 2c 20 61 64 64 72 54 6f 70 29 3b 0a 20 20 20 20 , addrTop);.
216e4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
216e5 70 48 65 72 65 28 76 2c 20 61 64 64 72 49 66 29 pHere(v, addrIf)
216e6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 ;. sqlite3R
216e7 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
216e8 61 72 73 65 2c 20 72 65 67 52 65 63 29 3b 0a 20 arse, regRec);.
216e9 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
216ea 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
216eb 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 e, regRowid);.
216ec 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
216ed 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 /* This is the
216ee 63 61 73 65 20 69 66 20 74 68 65 20 64 61 74 61 case if the data
216ef 20 66 6f 72 20 74 68 65 20 49 4e 53 45 52 54 20 for the INSERT
216f0 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 is coming from a
216f1 20 56 41 4c 55 45 53 0a 20 20 20 20 2a 2a 20 63 VALUES. ** c
216f2 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a 20 20 20 lause. */.
216f3 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 NameContext sNC
216f4 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e ;. memset(&sN
216f5 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 C, 0, sizeof(sNC
216f6 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 ));. sNC.pPar
216f7 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 se = pParse;.
216f8 20 73 72 63 54 61 62 20 3d 20 2d 31 3b 0a 20 20 srcTab = -1;.
216f9 20 20 61 73 73 65 72 74 28 20 75 73 65 54 65 6d assert( useTem
216fa 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 pTable==0 );.
216fb 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74 nColumn = pList
216fc 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 ? pList->nExpr
216fd 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 : 0;. for(i=0
216fe 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b ; i<nColumn; i++
216ff 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
21700 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
21701 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c 69 73 74 ames(&sNC, pList
21702 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b ->a[i].pExpr) ){
21703 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e . goto in
21704 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 sert_cleanup;.
21705 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
21706 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 . /* Make sure
21707 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f the number of co
21708 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 75 lumns in the sou
21709 72 63 65 20 64 61 74 61 20 6d 61 74 63 68 65 73 rce data matches
2170a 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a the number. **
2170b 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 of columns to b
2170c 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 e inserted into
2170d 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a the table.. */.
2170e 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 if( IsVirtual(
2170f 70 54 61 62 29 20 29 7b 0a 20 20 20 20 66 6f 72 pTab) ){. for
21710 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 (i=0; i<pTab->nC
21711 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ol; i++){.
21712 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49 73 48 69 nHidden += (IsHi
21713 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 ddenColumn(&pTab
21714 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20 31 20 3a ->aCol[i]) ? 1 :
21715 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 0);. }. }.
21716 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 if( pColumn==0
21717 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26 20 6e 43 && nColumn && nC
21718 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d 3e 6e 43 olumn!=(pTab->nC
21719 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29 7b 0a 20 ol-nHidden) ){.
2171a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
2171b 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 sg(pParse, .
2171c 20 20 20 22 74 61 62 6c 65 20 25 53 20 68 61 73 "table %S has
2171d 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62 75 74 20 %d columns but
2171e 25 64 20 76 61 6c 75 65 73 20 77 65 72 65 20 73 %d values were s
2171f 75 70 70 6c 69 65 64 22 2c 0a 20 20 20 20 20 20 upplied",.
21720 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 54 pTabList, 0, pT
21721 61 62 2d 3e 6e 43 6f 6c 2c 20 6e 43 6f 6c 75 6d ab->nCol, nColum
21722 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 n);. goto ins
21723 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d ert_cleanup;. }
21724 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 21 3d . if( pColumn!=
21725 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21 3d 70 43 0 && nColumn!=pC
21726 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 olumn->nId ){.
21727 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
21728 67 28 70 50 61 72 73 65 2c 20 22 25 64 20 76 61 g(pParse, "%d va
21729 6c 75 65 73 20 66 6f 72 20 25 64 20 63 6f 6c 75 lues for %d colu
2172a 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 70 mns", nColumn, p
2172b 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b 0a 20 20 Column->nId);.
2172c 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c goto insert_cl
2172d 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a eanup;. }.. /*
2172e 20 49 66 20 74 68 65 20 49 4e 53 45 52 54 20 73 If the INSERT s
2172f 74 61 74 65 6d 65 6e 74 20 69 6e 63 6c 75 64 65 tatement include
21730 64 20 61 6e 20 49 44 4c 49 53 54 20 74 65 72 6d d an IDLIST term
21731 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 , then make sure
21732 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c 65 6d 65 6e . ** all elemen
21733 74 73 20 6f 66 20 74 68 65 20 49 44 4c 49 53 54 ts of the IDLIST
21734 20 72 65 61 6c 6c 79 20 61 72 65 20 63 6f 6c 75 really are colu
21735 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 mns of the table
21736 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 65 6d and . ** remem
21737 62 65 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 ber the column i
21738 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a ndices.. **. *
21739 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 68 * If the table h
2173a 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 as an INTEGER PR
2173b 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e IMARY KEY column
2173c 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e and that column
2173d 0a 20 20 2a 2a 20 69 73 20 6e 61 6d 65 64 20 69 . ** is named i
2173e 6e 20 74 68 65 20 49 44 4c 49 53 54 2c 20 74 68 n the IDLIST, th
2173f 65 6e 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 en record in the
21740 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61 72 69 61 keyColumn varia
21741 62 6c 65 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 ble. ** the ind
21742 65 78 20 69 6e 74 6f 20 49 44 4c 49 53 54 20 6f ex into IDLIST o
21743 66 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 f the primary ke
21744 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b 65 79 43 6f y column. keyCo
21745 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a 20 74 68 65 lumn is. ** the
21746 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 72 index of the pr
21747 69 6d 61 72 79 20 6b 65 79 20 61 73 20 69 74 20 imary key as it
21748 61 70 70 65 61 72 73 20 69 6e 20 49 44 4c 49 53 appears in IDLIS
21749 54 2c 20 6e 6f 74 20 61 73 0a 20 20 2a 2a 20 69 T, not as. ** i
2174a 73 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65 s appears in the
2174b 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 2e original table.
2174c 20 20 28 54 68 65 20 69 6e 64 65 78 20 6f 66 20 (The index of
2174d 74 68 65 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a the primary. **
2174e 20 6b 65 79 20 69 6e 20 74 68 65 20 6f 72 69 67 key in the orig
2174f 69 6e 61 6c 20 74 61 62 6c 65 20 69 73 20 70 54 inal table is pT
21750 61 62 2d 3e 69 50 4b 65 79 2e 29 0a 20 20 2a 2f ab->iPKey.). */
21751 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 20 29 . if( pColumn )
21752 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 {. for(i=0; i
21753 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69 <pColumn->nId; i
21754 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 75 ++){. pColu
21755 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d 20 2d mn->a[i].idx = -
21756 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 1;. }. for
21757 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d (i=0; i<pColumn-
21758 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nId; i++){.
21759 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 for(j=0; j<pTa
2175a 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 b->nCol; j++){.
2175b 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
2175c 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 75 6d e3StrICmp(pColum
2175d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70 n->a[i].zName, p
2175e 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 Tab->aCol[j].zNa
2175f 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 me)==0 ){.
21760 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 pColumn->a[i
21761 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20 20 20 20 ].idx = j;.
21762 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61 62 if( j==pTab
21763 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 ->iPKey ){.
21764 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e keyColumn
21765 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 = i;.
21766 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 }. brea
21767 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
21768 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a }. if( j
21769 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a >=pTab->nCol ){.
2176a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
2176b 74 65 33 49 73 52 6f 77 69 64 28 70 43 6f 6c 75 te3IsRowid(pColu
2176c 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 mn->a[i].zName)
2176d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79 ){. key
2176e 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20 Column = i;.
2176f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21770 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
21771 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 rMsg(pParse, "ta
21772 62 6c 65 20 25 53 20 68 61 73 20 6e 6f 20 63 6f ble %S has no co
21773 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c 0a lumn named %s",.
21774 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 pT
21775 61 62 4c 69 73 74 2c 20 30 2c 20 70 43 6f 6c 75 abList, 0, pColu
21776 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b mn->a[i].zName);
21777 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 . pPars
21778 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 e->nErr++;.
21779 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 goto insert
2177a 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 _cleanup;.
2177b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
2177c 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
2177d 68 65 72 65 20 69 73 20 6e 6f 20 49 44 4c 49 53 here is no IDLIS
2177e 54 20 74 65 72 6d 20 62 75 74 20 74 68 65 20 74 T term but the t
2177f 61 62 6c 65 20 68 61 73 20 61 6e 20 69 6e 74 65 able has an inte
21780 67 65 72 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a ger primary. **
21781 20 6b 65 79 2c 20 74 68 65 20 73 65 74 20 74 68 key, the set th
21782 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61 72 69 e keyColumn vari
21783 61 62 6c 65 20 74 6f 20 74 68 65 20 70 72 69 6d able to the prim
21784 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 69 ary key column i
21785 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 74 68 65 ndex. ** in the
21786 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20 original table
21787 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20 2a 2f definition.. */
21788 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d . if( pColumn==
21789 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 0 && nColumn>0 )
2178a 7b 0a 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 {. keyColumn
2178b 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 = pTab->iPKey;.
2178c 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 }.. /* Open th
2178d 65 20 74 65 6d 70 20 74 61 62 6c 65 20 66 6f 72 e temp table for
2178e 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74 72 FOR EACH ROW tr
2178f 69 67 67 65 72 73 0a 20 20 2a 2f 0a 20 20 69 66 iggers. */. if
21790 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 ( triggers_exist
21791 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
21792 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21793 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 SetNumColumns, 0
21794 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 , pTab->nCol);.
21795 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21796 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 dOp2(v, OP_OpenP
21797 73 65 75 64 6f 2c 20 6e 65 77 49 64 78 2c 20 30 seudo, newIdx, 0
21798 29 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a );. }. . /*
21799 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
2179a 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 6f count of rows to
2179b 20 62 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a be inserted. *
2179c 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 /. if( db->flag
2179d 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 s & SQLITE_Count
2179e 52 6f 77 73 20 29 7b 0a 20 20 20 20 72 65 67 52 Rows ){. regR
2179f 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 owCount = ++pPar
217a0 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 se->nMem;. sq
217a1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
217a2 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
217a3 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a , regRowCount);.
217a4 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 }.. /* If thi
217a5 73 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 2c s is not a view,
217a6 20 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 20 open the table
217a7 61 6e 64 20 61 6e 64 20 61 6c 6c 20 69 6e 64 69 and and all indi
217a8 63 65 73 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 ces */. if( !is
217a9 56 69 65 77 20 29 7b 0a 20 20 20 20 69 6e 74 20 View ){. int
217aa 6e 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 69 3b nIdx;. int i;
217ab 0a 0a 20 20 20 20 62 61 73 65 43 75 72 20 3d 20 .. baseCur =
217ac 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 pParse->nTab;.
217ad 20 20 6e 49 64 78 20 3d 20 73 71 6c 69 74 65 33 nIdx = sqlite3
217ae 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 OpenTableAndIndi
217af 63 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 ces(pParse, pTab
217b0 2c 20 62 61 73 65 43 75 72 2c 20 4f 50 5f 4f 70 , baseCur, OP_Op
217b1 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 61 52 enWrite);. aR
217b2 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33 44 egIdx = sqlite3D
217b3 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 bMallocRaw(db, s
217b4 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 49 64 78 izeof(int)*(nIdx
217b5 2b 31 29 29 3b 0a 20 20 20 20 69 66 28 20 61 52 +1));. if( aR
217b6 65 67 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 egIdx==0 ){.
217b7 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c goto insert_cl
217b8 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 eanup;. }.
217b9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 64 78 for(i=0; i<nIdx
217ba 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 52 ; i++){. aR
217bb 65 67 49 64 78 5b 69 5d 20 3d 20 2b 2b 70 50 61 egIdx[i] = ++pPa
217bc 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d rse->nMem;. }
217bd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 . }.. /* This
217be 69 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 is the top of th
217bf 65 20 6d 61 69 6e 20 69 6e 73 65 72 74 69 6f 6e e main insertion
217c0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20 75 loop */. if( u
217c1 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 seTempTable ){.
217c2 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b /* This block
217c3 20 63 6f 64 65 73 20 74 68 65 20 74 6f 70 20 6f codes the top o
217c4 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 f loop only. Th
217c5 65 20 63 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 e complete loop
217c6 69 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f is the. ** fo
217c7 6c 6c 6f 77 69 6e 67 20 70 73 65 75 64 6f 63 6f llowing pseudoco
217c8 64 65 20 28 74 65 6d 70 6c 61 74 65 20 34 29 3a de (template 4):
217c9 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
217ca 20 20 20 20 20 20 20 72 65 77 69 6e 64 20 74 65 rewind te
217cb 6d 70 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 mp table. **
217cc 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 C: loop ove
217cd 72 20 72 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d r rows of interm
217ce 65 64 69 61 74 65 20 74 61 62 6c 65 0a 20 20 20 ediate table.
217cf 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 ** tr
217d0 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f ansfer values fo
217d1 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 rm intermediate
217d2 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c table into <tabl
217d3 65 3e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 e>. **
217d4 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 20 20 20 2a end loop. *
217d5 2a 20 20 20 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 * D: ....
217d6 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 49 6e 73 */. addrIns
217d7 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 Top = sqlite3Vdb
217d8 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 eAddOp1(v, OP_Re
217d9 77 69 6e 64 2c 20 73 72 63 54 61 62 29 3b 0a 20 wind, srcTab);.
217da 20 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 addrCont = sq
217db 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 lite3VdbeCurrent
217dc 41 64 64 72 28 76 29 3b 0a 20 20 7d 65 6c 73 65 Addr(v);. }else
217dd 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a if( pSelect ){.
217de 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 /* This bloc
217df 6b 20 63 6f 64 65 73 20 74 68 65 20 74 6f 70 20 k codes the top
217e0 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 of loop only. T
217e1 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 he complete loop
217e2 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 is the. ** f
217e3 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75 64 6f 63 ollowing pseudoc
217e4 6f 64 65 20 28 74 65 6d 70 6c 61 74 65 20 33 29 ode (template 3)
217e5 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 :. **. **
217e6 20 20 20 20 20 43 3a 20 79 69 65 6c 64 20 58 0a C: yield X.
217e7 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 ** i
217e8 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a 20 20 20 f EOF goto D.
217e9 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65 ** inse
217ea 72 74 20 74 68 65 20 73 65 6c 65 63 74 20 72 65 rt the select re
217eb 73 75 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 sult into <table
217ec 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 20 20 > from R..R+n.
217ed 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74 ** got
217ee 6f 20 43 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 o C. **
217ef 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20 D: .... */.
217f0 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c addrCont = sql
217f1 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
217f2 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 , OP_Yield, dest
217f3 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 61 64 64 .iParm);. add
217f4 72 49 6e 73 54 6f 70 20 3d 20 73 71 6c 69 74 65 rInsTop = sqlite
217f5 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
217f6 50 5f 49 66 2c 20 72 65 67 45 6f 66 29 3b 0a 20 P_If, regEof);.
217f7 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 }.. /* Allocat
217f8 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 e registers for
217f9 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 6f 77 69 holding the rowi
217fa 64 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77 d of the new row
217fb 2c 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65 ,. ** the conte
217fc 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f nt of the new ro
217fd 77 2c 20 61 6e 64 20 74 68 65 20 61 73 73 65 6d w, and the assem
217fe 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64 blied row record
217ff 2e 0a 20 20 2a 2f 0a 20 20 72 65 67 52 65 63 6f .. */. regReco
21800 72 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e rd = ++pParse->n
21801 4d 65 6d 3b 0a 20 20 72 65 67 52 6f 77 69 64 20 Mem;. regRowid
21802 3d 20 72 65 67 49 6e 73 20 3d 20 70 50 61 72 73 = regIns = pPars
21803 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 70 50 61 e->nMem+1;. pPa
21804 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 rse->nMem += pTa
21805 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20 69 b->nCol + 1;. i
21806 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 f( IsVirtual(pTa
21807 62 29 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 77 b) ){. regRow
21808 69 64 2b 2b 3b 0a 20 20 20 20 70 50 61 72 73 65 id++;. pParse
21809 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 ->nMem++;. }.
2180a 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f 77 regData = regRow
2180b 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 id+1;.. /* Run
2180c 74 68 65 20 42 45 46 4f 52 45 20 61 6e 64 20 49 the BEFORE and I
2180d 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 NSTEAD OF trigge
2180e 72 73 2c 20 69 66 20 74 68 65 72 65 20 61 72 65 rs, if there are
2180f 20 61 6e 79 0a 20 20 2a 2f 0a 20 20 65 6e 64 4f any. */. endO
21810 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 fLoop = sqlite3V
21811 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b dbeMakeLabel(v);
21812 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f . if( triggers_
21813 65 78 69 73 74 20 26 20 54 52 49 47 47 45 52 5f exist & TRIGGER_
21814 42 45 46 4f 52 45 20 29 7b 0a 20 20 20 20 69 6e BEFORE ){. in
21815 74 20 72 65 67 52 6f 77 69 64 3b 0a 20 20 20 20 t regRowid;.
21816 69 6e 74 20 72 65 67 43 6f 6c 73 3b 0a 20 20 20 int regCols;.
21817 20 69 6e 74 20 72 65 67 52 65 63 3b 0a 0a 20 20 int regRec;..
21818 20 20 2f 2a 20 62 75 69 6c 64 20 74 68 65 20 4e /* build the N
21819 45 57 2e 2a 20 72 65 66 65 72 65 6e 63 65 20 72 EW.* reference r
2181a 6f 77 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69 ow. Note that i
2181b 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 49 4e f there is an IN
2181c 54 45 47 45 52 0a 20 20 20 20 2a 2a 20 50 52 49 TEGER. ** PRI
2181d 4d 41 52 59 20 4b 45 59 20 69 6e 74 6f 20 77 68 MARY KEY into wh
2181e 69 63 68 20 61 20 4e 55 4c 4c 20 69 73 20 62 65 ich a NULL is be
2181f 69 6e 67 20 69 6e 73 65 72 74 65 64 2c 20 74 68 ing inserted, th
21820 61 74 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 0a at NULL will be.
21821 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 65 ** translate
21822 64 20 69 6e 74 6f 20 61 20 75 6e 69 71 75 65 20 d into a unique
21823 49 44 20 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 ID for the row.
21824 20 42 75 74 20 6f 6e 20 61 20 42 45 46 4f 52 45 But on a BEFORE
21825 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a trigger,. **
21826 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 we do not know
21827 77 68 61 74 20 74 68 65 20 75 6e 69 71 75 65 20 what the unique
21828 49 44 20 77 69 6c 6c 20 62 65 20 28 62 65 63 61 ID will be (beca
21829 75 73 65 20 74 68 65 20 69 6e 73 65 72 74 20 68 use the insert h
2182a 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 as. ** not ha
2182b 70 70 65 6e 65 64 20 79 65 74 29 20 73 6f 20 77 ppened yet) so w
2182c 65 20 73 75 62 73 74 69 74 75 74 65 20 61 20 72 e substitute a r
2182d 6f 77 69 64 20 6f 66 20 2d 31 0a 20 20 20 20 2a owid of -1. *
2182e 2f 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d /. regRowid =
2182f 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
21830 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 eg(pParse);.
21831 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3c 30 20 if( keyColumn<0
21832 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
21833 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21834 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65 _Integer, -1, re
21835 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c gRowid);. }el
21836 73 65 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 se if( useTempTa
21837 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ble ){. sql
21838 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
21839 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 , OP_Column, src
2183a 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20 Tab, keyColumn,
2183b 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d regRowid);. }
2183c 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 else{. int
2183d 6a 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 j1;. assert
2183e 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 ( pSelect==0 );
2183f 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 75 73 /* Otherwise us
21840 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20 74 72 eTempTable is tr
21841 75 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 ue */. sqli
21842 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 te3ExprCode(pPar
21843 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 se, pList->a[key
21844 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 Column].pExpr, r
21845 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 egRowid);.
21846 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j1 = sqlite3Vdbe
21847 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 AddOp1(v, OP_Not
21848 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 29 3b Null, regRowid);
21849 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
2184a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
2184b 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65 67 52 nteger, -1, regR
2184c 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c owid);. sql
2184d 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
2184e 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 73 (v, j1);. s
2184f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
21850 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 (v, OP_MustBeInt
21851 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 , regRowid);.
21852 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 61 6e 6e 6f }.. /* Canno
21853 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73 20 t have triggers
21854 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 on a virtual tab
21855 6c 65 2e 20 49 66 20 69 74 20 77 65 72 65 20 70 le. If it were p
21856 6f 73 73 69 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 ossible,. **
21857 74 68 69 73 20 62 6c 6f 63 6b 20 77 6f 75 6c 64 this block would
21858 20 68 61 76 65 20 74 6f 20 61 63 63 6f 75 6e 74 have to account
21859 20 66 6f 72 20 68 69 64 64 65 6e 20 63 6f 6c 75 for hidden colu
2185a 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 mn.. */. a
2185b 73 73 65 72 74 28 21 49 73 56 69 72 74 75 61 6c ssert(!IsVirtual
2185c 28 70 54 61 62 29 29 3b 0a 0a 20 20 20 20 2f 2a (pTab));.. /*
2185d 20 43 72 65 61 74 65 20 74 68 65 20 6e 65 77 20 Create the new
2185e 63 6f 6c 75 6d 6e 20 64 61 74 61 0a 20 20 20 20 column data.
2185f 2a 2f 0a 20 20 20 20 72 65 67 43 6f 6c 73 20 3d */. regCols =
21860 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
21861 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 54 61 ange(pParse, pTa
21862 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 66 6f b->nCol);. fo
21863 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e r(i=0; i<pTab->n
21864 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 Col; i++){.
21865 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 if( pColumn==0
21866 29 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 69 ){. j = i
21867 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
21868 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 for(j=0;
21869 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 j<pColumn->nId;
2186a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 j++){.
2186b 69 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a if( pColumn->a[j
2186c 5d 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b ].idx==i ) break
2186d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
2186e 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 }. if( pC
2186f 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c olumn && j>=pCol
21870 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 umn->nId ){.
21871 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
21872 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 ode(pParse, pTab
21873 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c ->aCol[i].pDflt,
21874 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 regCols+i);.
21875 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65 }else if( use
21876 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 TempTable ){.
21877 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21878 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c AddOp3(v, OP_Col
21879 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20 umn, srcTab, j,
2187a 72 65 67 43 6f 6c 73 2b 69 29 3b 20 0a 20 20 20 regCols+i); .
2187b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2187c 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 assert( pSelec
2187d 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74 68 65 72 t==0 ); /* Other
2187e 77 69 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c wise useTempTabl
2187f 65 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 e is true */.
21880 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21881 43 6f 64 65 41 6e 64 43 61 63 68 65 28 70 50 61 CodeAndCache(pPa
21882 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d rse, pList->a[j]
21883 2e 70 45 78 70 72 2c 20 72 65 67 43 6f 6c 73 2b .pExpr, regCols+
21884 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 i);. }.
21885 7d 0a 20 20 20 20 72 65 67 52 65 63 20 3d 20 73 }. regRec = s
21886 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
21887 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71 (pParse);. sq
21888 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21889 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 v, OP_MakeRecord
2188a 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d , regCols, pTab-
2188b 3e 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a >nCol, regRec);.
2188c 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
2188d 69 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 6e 20 is an INSERT on
2188e 61 20 76 69 65 77 20 77 69 74 68 20 61 6e 20 49 a view with an I
2188f 4e 53 54 45 41 44 20 4f 46 20 49 4e 53 45 52 54 NSTEAD OF INSERT
21890 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a trigger,. **
21891 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 do not attempt
21892 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 any conversions
21893 62 65 66 6f 72 65 20 61 73 73 65 6d 62 6c 69 6e before assemblin
21894 67 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 g the record..
21895 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 ** If this is
21896 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 61 74 a real table, at
21897 74 65 6d 70 74 20 63 6f 6e 76 65 72 73 69 6f 6e tempt conversion
21898 73 20 61 73 20 72 65 71 75 69 72 65 64 20 62 79 s as required by
21899 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c the. ** tabl
2189a 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 e column affinit
2189b 69 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ies.. */.
2189c 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 if( !isView ){.
2189d 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c sqlite3Tabl
2189e 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 eAffinityStr(v,
2189f 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 pTab);. }.
218a0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
218a1 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c p3(v, OP_Insert,
218a2 20 6e 65 77 49 64 78 2c 20 72 65 67 52 65 63 2c newIdx, regRec,
218a3 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 regRowid);.
218a4 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
218a5 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 mpReg(pParse, re
218a6 67 52 65 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 gRec);. sqlit
218a7 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
218a8 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69 (pParse, regRowi
218a9 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 d);. sqlite3R
218aa 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 eleaseTempRange(
218ab 70 50 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c pParse, regCols,
218ac 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a 20 pTab->nCol);..
218ad 20 20 20 2f 2a 20 46 69 72 65 20 42 45 46 4f 52 /* Fire BEFOR
218ae 45 20 6f 72 20 49 4e 53 54 45 41 44 20 4f 46 20 E or INSTEAD OF
218af 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 triggers */.
218b0 69 66 28 20 73 71 6c 69 74 65 33 43 6f 64 65 52 if( sqlite3CodeR
218b1 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 owTrigger(pParse
218b2 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 , TK_INSERT, 0,
218b3 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 TRIGGER_BEFORE,
218b4 70 54 61 62 2c 20 0a 20 20 20 20 20 20 20 20 6e pTab, . n
218b5 65 77 49 64 78 2c 20 2d 31 2c 20 6f 6e 45 72 72 ewIdx, -1, onErr
218b6 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 30 or, endOfLoop, 0
218b7 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 67 6f , 0) ){. go
218b8 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 to insert_cleanu
218b9 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 p;. }. }..
218ba 2f 2a 20 50 75 73 68 20 74 68 65 20 72 65 63 6f /* Push the reco
218bb 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 rd number for th
218bc 65 20 6e 65 77 20 65 6e 74 72 79 20 6f 6e 74 6f e new entry onto
218bd 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 65 the stack. The
218be 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d . ** record num
218bf 62 65 72 20 69 73 20 61 20 72 61 6e 64 6f 6d 6c ber is a randoml
218c0 79 20 67 65 6e 65 72 61 74 65 20 69 6e 74 65 67 y generate integ
218c1 65 72 20 63 72 65 61 74 65 64 20 62 79 20 4e 65 er created by Ne
218c2 77 52 6f 77 69 64 0a 20 20 2a 2a 20 65 78 63 65 wRowid. ** exce
218c3 70 74 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c pt when the tabl
218c4 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 e has an INTEGER
218c5 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c PRIMARY KEY col
218c6 75 6d 6e 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 umn, in which.
218c7 2a 2a 20 63 61 73 65 20 74 68 65 20 72 65 63 6f ** case the reco
218c8 72 64 20 6e 75 6d 62 65 72 20 69 73 20 74 68 65 rd number is the
218c9 20 73 61 6d 65 20 61 73 20 74 68 61 74 20 63 6f same as that co
218ca 6c 75 6d 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 lumn. . */. if
218cb 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 ( !isView ){.
218cc 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 if( IsVirtual(p
218cd 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a Tab) ){. /*
218ce 20 54 68 65 20 72 6f 77 20 74 68 61 74 20 74 68 The row that th
218cf 65 20 56 55 70 64 61 74 65 20 6f 70 63 6f 64 65 e VUpdate opcode
218d0 20 77 69 6c 6c 20 64 65 6c 65 74 65 3a 20 6e 6f will delete: no
218d1 6e 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 ne */. sqli
218d2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
218d3 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 OP_Null, 0, reg
218d4 49 6e 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Ins);. }.
218d5 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 if( keyColumn>=0
218d6 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75 73 ){. if( us
218d7 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 eTempTable ){.
218d8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
218d9 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f eAddOp3(v, OP_Co
218da 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 lumn, srcTab, ke
218db 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69 yColumn, regRowi
218dc 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 d);. }else
218dd 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 if( pSelect ){.
218de 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
218df 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 beAddOp2(v, OP_S
218e0 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c Copy, regFromSel
218e1 65 63 74 2b 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 ect+keyColumn, r
218e2 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 egRowid);.
218e3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 56 }else{. V
218e4 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 dbeOp *pOp;.
218e5 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
218e6 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 ode(pParse, pLis
218e7 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e t->a[keyColumn].
218e8 70 45 78 70 72 2c 20 72 65 67 52 6f 77 69 64 29 pExpr, regRowid)
218e9 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 20 3d 20 ;. pOp =
218ea 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 sqlite3VdbeGetOp
218eb 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 (v, sqlite3VdbeC
218ec 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2d 20 urrentAddr(v) -
218ed 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1);. if(
218ee 70 4f 70 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f pOp && pOp->opco
218ef 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20 26 26 20 21 de==OP_Null && !
218f0 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 IsVirtual(pTab)
218f1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 70 ){. app
218f2 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 endFlag = 1;.
218f3 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f pOp->opco
218f4 64 65 20 3d 20 4f 50 5f 4e 65 77 52 6f 77 69 64 de = OP_NewRowid
218f5 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d ;. pOp-
218f6 3e 70 31 20 3d 20 62 61 73 65 43 75 72 3b 0a 20 >p1 = baseCur;.
218f7 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 pOp->p2
218f8 20 3d 20 72 65 67 52 6f 77 69 64 3b 0a 20 20 20 = regRowid;.
218f9 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d pOp->p3 =
218fa 20 72 65 67 41 75 74 6f 69 6e 63 3b 0a 20 20 20 regAutoinc;.
218fb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
218fc 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 /* If the P
218fd 52 49 4d 41 52 59 20 4b 45 59 20 65 78 70 72 65 RIMARY KEY expre
218fe 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 ssion is NULL, t
218ff 68 65 6e 20 75 73 65 20 4f 50 5f 4e 65 77 52 6f hen use OP_NewRo
21900 77 69 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 wid. ** to
21901 67 65 6e 65 72 61 74 65 20 61 20 75 6e 69 71 75 generate a uniqu
21902 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 76 61 e primary key va
21903 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 lue.. */.
21904 20 20 20 20 69 66 28 20 21 61 70 70 65 6e 64 46 if( !appendF
21905 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 69 lag ){. i
21906 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20 20 20 69 nt j1;. i
21907 66 28 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 f( !IsVirtual(pT
21908 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ab) ){.
21909 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j1 = sqlite3Vdb
2190a 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f eAddOp1(v, OP_No
2190b 74 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 29 tNull, regRowid)
2190c 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
2190d 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
2190e 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62 61 OP_NewRowid, ba
2190f 73 65 43 75 72 2c 20 72 65 67 52 6f 77 69 64 2c seCur, regRowid,
21910 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20 regAutoinc);.
21911 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21912 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
21913 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 1);. }els
21914 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6a 31 20 e{. j1
21915 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 = sqlite3VdbeCur
21916 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 rentAddr(v);.
21917 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21918 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
21919 73 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 2c sNull, regRowid,
2191a 20 6a 31 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 j1+2);.
2191b 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
2191c 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
2191d 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67 P_MustBeInt, reg
2191e 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a Rowid);. }.
2191f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73 }else if( Is
21920 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b Virtual(pTab) ){
21921 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
21922 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
21923 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 ull, 0, regRowid
21924 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
21925 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21926 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 77 52 ddOp3(v, OP_NewR
21927 6f 77 69 64 2c 20 62 61 73 65 43 75 72 2c 20 72 owid, baseCur, r
21928 65 67 52 6f 77 69 64 2c 20 72 65 67 41 75 74 6f egRowid, regAuto
21929 69 6e 63 29 3b 0a 20 20 20 20 20 20 61 70 70 65 inc);. appe
2192a 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 ndFlag = 1;.
2192b 7d 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65 }. autoIncSte
2192c 70 28 70 50 61 72 73 65 2c 20 72 65 67 41 75 74 p(pParse, regAut
2192d 6f 69 6e 63 2c 20 72 65 67 52 6f 77 69 64 29 3b oinc, regRowid);
2192e 0a 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 6f 6e .. /* Push on
2192f 74 6f 20 74 68 65 20 73 74 61 63 6b 2c 20 64 61 to the stack, da
21930 74 61 20 66 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d ta for all colum
21931 6e 73 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e ns of the new en
21932 74 72 79 2c 20 62 65 67 69 6e 6e 69 6e 67 0a 20 try, beginning.
21933 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 66 ** with the f
21934 69 72 73 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 irst column..
21935 20 2a 2f 0a 20 20 20 20 6e 48 69 64 64 65 6e 20 */. nHidden
21936 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 = 0;. for(i=0
21937 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 ; i<pTab->nCol;
21938 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 i++){. int
21939 69 52 65 67 53 74 6f 72 65 20 3d 20 72 65 67 52 iRegStore = regR
2193a 6f 77 69 64 2b 31 2b 69 3b 0a 20 20 20 20 20 20 owid+1+i;.
2193b 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b if( i==pTab->iPK
2193c 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ey ){. /*
2193d 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 The value of th
2193e 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 e INTEGER PRIMAR
2193f 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 69 73 20 Y KEY column is
21940 61 6c 77 61 79 73 20 61 20 4e 55 4c 4c 2e 0a 20 always a NULL..
21941 20 20 20 20 20 20 20 2a 2a 20 57 68 65 6e 65 76 ** Whenev
21942 65 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 er this column i
21943 73 20 72 65 61 64 2c 20 74 68 65 20 72 65 63 6f s read, the reco
21944 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20 62 rd number will b
21945 65 20 73 75 62 73 74 69 74 75 74 65 64 0a 20 20 e substituted.
21946 20 20 20 20 20 20 2a 2a 20 69 6e 20 69 74 73 20 ** in its
21947 70 6c 61 63 65 2e 20 20 53 6f 20 77 69 6c 6c 20 place. So will
21948 66 69 6c 6c 20 74 68 69 73 20 63 6f 6c 75 6d 6e fill this column
21949 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 74 6f 20 with a NULL to
2194a 61 76 6f 69 64 0a 20 20 20 20 20 20 20 20 2a 2a avoid. **
2194b 20 74 61 6b 69 6e 67 20 75 70 20 64 61 74 61 20 taking up data
2194c 73 70 61 63 65 20 77 69 74 68 20 69 6e 66 6f 72 space with infor
2194d 6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c mation that will
2194e 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 never be used.
2194f 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
21950 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21951 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 65 67 OP_Null, 0, iReg
21952 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 20 20 Store);.
21953 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
21954 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c }. if( pCol
21955 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 umn==0 ){.
21956 20 20 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f if( IsHiddenCo
21957 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c lumn(&pTab->aCol
21958 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 [i]) ){.
21959 20 20 61 73 73 65 72 74 28 20 49 73 56 69 72 74 assert( IsVirt
2195a 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 20 20 20 ual(pTab) );.
2195b 20 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 j = -1;.
2195c 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e nHidden
2195d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ++;. }els
2195e 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 3d e{. j =
2195f 20 69 20 2d 20 6e 48 69 64 64 65 6e 3b 0a 20 20 i - nHidden;.
21960 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
21961 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 lse{. for
21962 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d (j=0; j<pColumn-
21963 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 >nId; j++){.
21964 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d if( pColum
21965 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29 n->a[j].idx==i )
21966 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
21967 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
21968 69 66 28 20 6a 3c 30 20 7c 7c 20 6e 43 6f 6c 75 if( j<0 || nColu
21969 6d 6e 3d 3d 30 20 7c 7c 20 28 70 43 6f 6c 75 6d mn==0 || (pColum
2196a 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d n && j>=pColumn-
2196b 3e 6e 49 64 29 20 29 7b 0a 20 20 20 20 20 20 20 >nId) ){.
2196c 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
2196d 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 (pParse, pTab->a
2196e 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 69 52 Col[i].pDflt, iR
2196f 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 egStore);.
21970 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d }else if( useTem
21971 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 pTable ){.
21972 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21973 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e Op3(v, OP_Column
21974 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20 69 52 65 , srcTab, j, iRe
21975 67 53 74 6f 72 65 29 3b 20 0a 20 20 20 20 20 20 gStore); .
21976 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 }else if( pSelec
21977 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c t ){. sql
21978 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21979 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 46 , OP_SCopy, regF
2197a 72 6f 6d 53 65 6c 65 63 74 2b 6a 2c 20 69 52 65 romSelect+j, iRe
2197b 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d gStore);. }
2197c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
2197d 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 lite3ExprCode(pP
2197e 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a arse, pList->a[j
2197f 5d 2e 70 45 78 70 72 2c 20 69 52 65 67 53 74 6f ].pExpr, iRegSto
21980 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 re);. }.
21981 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 }.. /* Gener
21982 61 74 65 20 63 6f 64 65 20 74 6f 20 63 68 65 63 ate code to chec
21983 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 6e k constraints an
21984 64 20 67 65 6e 65 72 61 74 65 20 69 6e 64 65 78 d generate index
21985 20 6b 65 79 73 20 61 6e 64 0a 20 20 20 20 2a 2a keys and. **
21986 20 64 6f 20 74 68 65 20 69 6e 73 65 72 74 69 6f do the insertio
21987 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 n.. */.#ifnde
21988 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
21989 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 RTUALTABLE. i
2198a 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 f( IsVirtual(pTa
2198b 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 b) ){. sqli
2198c 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 te3VtabMakeWrita
2198d 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 ble(pParse, pTab
2198e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
2198f 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
21990 5f 56 55 70 64 61 74 65 2c 20 31 2c 20 70 54 61 _VUpdate, 1, pTa
21991 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 72 65 67 49 6e b->nCol+2, regIn
21992 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 s,.
21993 20 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63 (const c
21994 68 61 72 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 har*)pTab->pVtab
21995 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 , P4_VTAB);.
21996 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 }else.#endif.
21997 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
21998 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 GenerateConstrai
21999 6e 74 43 68 65 63 6b 73 28 0a 20 20 20 20 20 20 ntChecks(.
2199a 20 20 20 20 70 50 61 72 73 65 2c 0a 20 20 20 20 pParse,.
2199b 20 20 20 20 20 20 70 54 61 62 2c 0a 20 20 20 20 pTab,.
2199c 20 20 20 20 20 20 62 61 73 65 43 75 72 2c 0a 20 baseCur,.
2199d 20 20 20 20 20 20 20 20 20 72 65 67 49 6e 73 2c regIns,
2199e 0a 20 20 20 20 20 20 20 20 20 20 61 52 65 67 49 . aRegI
2199f 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 6b 65 dx,. ke
219a0 79 43 6f 6c 75 6d 6e 3e 3d 30 2c 0a 20 20 20 20 yColumn>=0,.
219a1 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 0,.
219a2 20 20 20 6f 6e 45 72 72 6f 72 2c 0a 20 20 20 20 onError,.
219a3 20 20 20 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 0a endOfLoop.
219a4 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 73 );. s
219a5 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e qlite3CompleteIn
219a6 73 65 72 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 sertion(.
219a7 20 20 20 70 50 61 72 73 65 2c 0a 20 20 20 20 20 pParse,.
219a8 20 20 20 20 20 70 54 61 62 2c 0a 20 20 20 20 20 pTab,.
219a9 20 20 20 20 20 62 61 73 65 43 75 72 2c 0a 20 20 baseCur,.
219aa 20 20 20 20 20 20 20 20 72 65 67 49 6e 73 2c 0a regIns,.
219ab 20 20 20 20 20 20 20 20 20 20 61 52 65 67 49 64 aRegId
219ac 78 2c 0a 20 20 20 20 20 20 20 20 20 20 30 2c 0a x,. 0,.
219ad 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 0,.
219ae 20 20 20 20 20 20 20 28 74 72 69 67 67 65 72 73 (triggers
219af 5f 65 78 69 73 74 20 26 20 54 52 49 47 47 45 52 _exist & TRIGGER
219b0 5f 41 46 54 45 52 29 21 3d 30 20 3f 20 6e 65 77 _AFTER)!=0 ? new
219b1 49 64 78 20 3a 20 2d 31 2c 0a 20 20 20 20 20 20 Idx : -1,.
219b2 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67 0a 20 appendFlag.
219b3 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 );. }.
219b4 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 }.. /* Update
219b5 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 the count of row
219b6 73 20 74 68 61 74 20 61 72 65 20 69 6e 73 65 72 s that are inser
219b7 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 ted. */. if( (
219b8 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
219b9 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30 TE_CountRows)!=0
219ba 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
219bb 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
219bc 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f AddImm, regRowCo
219bd 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 unt, 1);. }..
219be 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 if( triggers_exi
219bf 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 64 st ){. /* Cod
219c0 65 20 41 46 54 45 52 20 74 72 69 67 67 65 72 73 e AFTER triggers
219c1 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 */. if( sqli
219c2 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 te3CodeRowTrigge
219c3 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 53 r(pParse, TK_INS
219c4 45 52 54 2c 20 30 2c 20 54 52 49 47 47 45 52 5f ERT, 0, TRIGGER_
219c5 41 46 54 45 52 2c 20 70 54 61 62 2c 0a 20 20 20 AFTER, pTab,.
219c6 20 20 20 20 20 20 20 6e 65 77 49 64 78 2c 20 2d newIdx, -
219c7 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 1, onError, endO
219c8 66 4c 6f 6f 70 2c 20 30 2c 20 30 29 20 29 7b 0a fLoop, 0, 0) ){.
219c9 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 goto inser
219ca 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d t_cleanup;. }
219cb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 62 . }.. /* The b
219cc 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6d 61 69 ottom of the mai
219cd 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 n insertion loop
219ce 2c 20 69 66 20 74 68 65 20 64 61 74 61 20 73 6f , if the data so
219cf 75 72 63 65 0a 20 20 2a 2a 20 69 73 20 61 20 53 urce. ** is a S
219d0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
219d1 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 . */. sqlite3V
219d2 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
219d3 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 v, endOfLoop);.
219d4 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c if( useTempTabl
219d5 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
219d6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
219d7 5f 4e 65 78 74 2c 20 73 72 63 54 61 62 2c 20 61 _Next, srcTab, a
219d8 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 73 71 ddrCont);. sq
219d9 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
219da 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f 70 29 e(v, addrInsTop)
219db 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
219dc 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c eAddOp1(v, OP_Cl
219dd 6f 73 65 2c 20 73 72 63 54 61 62 29 3b 0a 20 20 ose, srcTab);.
219de 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 }else if( pSelec
219df 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
219e0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
219e1 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 43 6f _Goto, 0, addrCo
219e2 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 nt);. sqlite3
219e3 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
219e4 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20 7d addrInsTop);. }
219e5 0a 0a 20 20 69 66 28 20 21 49 73 56 69 72 74 75 .. if( !IsVirtu
219e6 61 6c 28 70 54 61 62 29 20 26 26 20 21 69 73 56 al(pTab) && !isV
219e7 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6c iew ){. /* Cl
219e8 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20 6f ose all tables o
219e9 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c pened */. sql
219ea 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
219eb 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 62 61 73 65 , OP_Close, base
219ec 43 75 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 64 Cur);. for(id
219ed 78 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e x=1, pIdx=pTab->
219ee 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 pIndex; pIdx; pI
219ef 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 dx=pIdx->pNext,
219f0 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 idx++){. sq
219f1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
219f2 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 64 78 v, OP_Close, idx
219f3 2b 62 61 73 65 43 75 72 29 3b 0a 20 20 20 20 7d +baseCur);. }
219f4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 . }.. /* Updat
219f5 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 e the sqlite_seq
219f6 75 65 6e 63 65 20 74 61 62 6c 65 20 62 79 20 73 uence table by s
219f7 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 toring the conte
219f8 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 nt of the. ** c
219f9 6f 75 6e 74 65 72 20 76 61 6c 75 65 20 69 6e 20 ounter value in
219fa 6d 65 6d 6f 72 79 20 72 65 67 41 75 74 6f 69 6e memory regAutoin
219fb 63 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 c back into the
219fc 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a sqlite_sequence.
219fd 20 20 2a 2a 20 74 61 62 6c 65 2e 0a 20 20 2a 2f ** table.. */
219fe 0a 20 20 61 75 74 6f 49 6e 63 45 6e 64 28 70 50 . autoIncEnd(pP
219ff 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2c arse, iDb, pTab,
21a00 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 0a 20 regAutoinc);..
21a01 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 /*. ** Return
21a02 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f the number of ro
21a03 77 73 20 69 6e 73 65 72 74 65 64 2e 20 49 66 20 ws inserted. If
21a04 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
21a05 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 . ** generating
21a06 20 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 code because of
21a07 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
21a08 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c e3NestedParse(),
21a09 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 do not. ** inv
21a0a 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b oke the callback
21a0b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a function.. */.
21a0c 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 if( db->flags
21a0d 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f & SQLITE_CountRo
21a0e 77 73 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 ws && pParse->ne
21a0f 73 74 65 64 3d 3d 30 20 26 26 20 21 70 50 61 72 sted==0 && !pPar
21a10 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b se->trigStack ){
21a11 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
21a12 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 AddOp2(v, OP_Res
21a13 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f ultRow, regRowCo
21a14 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c unt, 1);. sql
21a15 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f ite3VdbeSetNumCo
21a16 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 ls(v, 1);. sq
21a17 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e lite3VdbeSetColN
21a18 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d ame(v, 0, COLNAM
21a19 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 69 6e E_NAME, "rows in
21a1a 73 65 72 74 65 64 22 2c 20 53 51 4c 49 54 45 5f serted", SQLITE_
21a1b 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 69 6e STATIC);. }..in
21a1c 73 65 72 74 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 sert_cleanup:.
21a1d 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 sqlite3SrcListDe
21a1e 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73 lete(db, pTabLis
21a1f 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 t);. sqlite3Exp
21a20 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 rListDelete(db,
21a21 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 pList);. sqlite
21a22 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 3SelectDelete(db
21a23 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71 , pSelect);. sq
21a24 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 lite3IdListDelet
21a25 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a e(db, pColumn);.
21a26 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
21a27 64 62 2c 20 61 52 65 67 49 64 78 29 3b 0a 7d 0a db, aRegIdx);.}.
21a28 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
21a29 63 6f 64 65 20 74 6f 20 64 6f 20 63 6f 6e 73 74 code to do const
21a2a 72 61 69 6e 74 20 63 68 65 63 6b 73 20 70 72 69 raint checks pri
21a2b 6f 72 20 74 6f 20 61 6e 20 49 4e 53 45 52 54 20 or to an INSERT
21a2c 6f 72 20 61 6e 20 55 50 44 41 54 45 2e 0a 2a 2a or an UPDATE..**
21a2d 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73 .** The input is
21a2e 20 61 20 72 61 6e 67 65 20 6f 66 20 63 6f 6e 73 a range of cons
21a2f 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65 72 ecutive register
21a30 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
21a31 0a 2a 2a 20 20 20 20 31 2e 20 20 54 68 65 20 72 .** 1. The r
21a32 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20 owid of the row
21a33 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 62 65 to be updated be
21a34 66 6f 72 65 20 74 68 65 20 75 70 64 61 74 65 2e fore the update.
21a35 20 20 54 68 69 73 0a 2a 2a 20 20 20 20 20 20 20 This.**
21a36 20 76 61 6c 75 65 20 69 73 20 6f 6d 69 74 74 65 value is omitte
21a37 64 20 75 6e 6c 65 73 73 20 77 65 20 61 72 65 20 d unless we are
21a38 64 6f 69 6e 67 20 61 6e 20 55 50 44 41 54 45 20 doing an UPDATE
21a39 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 61 0a that involves a.
21a3a 2a 2a 20 20 20 20 20 20 20 20 63 68 61 6e 67 65 ** change
21a3b 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 6e to the record n
21a3c 75 6d 62 65 72 20 6f 72 20 77 72 69 74 69 6e 67 umber or writing
21a3d 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 to a virtual ta
21a3e 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 32 2e ble..**.** 2.
21a3f 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74 The rowid of t
21a40 68 65 20 72 6f 77 20 61 66 74 65 72 20 74 68 65 he row after the
21a41 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 update..**.**
21a42 20 20 33 2e 20 20 54 68 65 20 64 61 74 61 20 69 3. The data i
21a43 6e 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 n the first colu
21a44 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 mn of the entry
21a45 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65 after the update
21a46 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20 44 ..**.** i. D
21a47 61 74 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65 20 ata from middle
21a48 63 6f 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a columns....**.**
21a49 20 20 20 20 4e 2e 20 20 54 68 65 20 64 61 74 61 N. The data
21a4a 20 69 6e 20 74 68 65 20 6c 61 73 74 20 63 6f 6c in the last col
21a4b 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 umn of the entry
21a4c 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 after the updat
21a4d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 e..**.** The reg
21a4e 52 6f 77 69 64 20 70 61 72 61 6d 65 74 65 72 20 Rowid parameter
21a4f 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 is the index of
21a50 74 68 65 20 72 65 67 69 73 74 65 72 20 63 6f 6e the register con
21a51 74 61 69 6e 69 6e 67 20 28 32 29 2e 0a 2a 2a 0a taining (2)..**.
21a52 2a 2a 20 54 68 65 20 6f 6c 64 20 72 6f 77 69 64 ** The old rowid
21a53 20 73 68 6f 77 6e 20 61 73 20 65 6e 74 72 79 20 shown as entry
21a54 28 31 29 20 61 62 6f 76 65 20 69 73 20 6f 6d 69 (1) above is omi
21a55 74 74 65 64 20 75 6e 6c 65 73 73 20 62 6f 74 68 tted unless both
21a56 20 69 73 55 70 64 61 74 65 0a 2a 2a 20 61 6e 64 isUpdate.** and
21a57 20 72 6f 77 69 64 43 68 6e 67 20 61 72 65 20 31 rowidChng are 1
21a58 2e 20 20 69 73 55 70 64 61 74 65 20 69 73 20 74 . isUpdate is t
21a59 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 73 20 rue for UPDATEs
21a5a 61 6e 64 20 66 61 6c 73 65 20 66 6f 72 0a 2a 2a and false for.**
21a5b 20 49 4e 53 45 52 54 73 2e 20 20 52 6f 77 69 64 INSERTs. Rowid
21a5c 43 68 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 20 Chng means that
21a5d 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20 69 73 the new rowid is
21a5e 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63 explicitly spec
21a5f 69 66 69 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 ified by.** the
21a60 75 70 64 61 74 65 20 6f 72 20 69 6e 73 65 72 74 update or insert
21a61 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 statement. If
21a62 72 6f 77 69 64 43 68 6e 67 20 69 73 20 66 61 6c rowidChng is fal
21a63 73 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 se, it means tha
21a64 74 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 t.** the rowid i
21a65 73 20 63 6f 6d 70 75 74 65 64 20 61 75 74 6f 6d s computed autom
21a66 61 74 69 63 61 6c 6c 79 20 69 6e 20 61 6e 20 69 atically in an i
21a67 6e 73 65 72 74 20 6f 72 20 74 68 61 74 20 74 68 nsert or that th
21a68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 0a 2a 2a e rowid value.**
21a69 20 69 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 is not modified
21a6a 20 62 79 20 74 68 65 20 75 70 64 61 74 65 2e 0a by the update..
21a6b 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 **.** The code g
21a6c 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 69 73 enerated by this
21a6d 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 20 6e routine store n
21a6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 ew index entries
21a6f 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 into.** registe
21a70 72 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 rs identified by
21a71 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 4e 6f 20 aRegIdx[]. No
21a72 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 63 index entry is c
21a73 72 65 61 74 65 64 20 66 6f 72 0a 2a 2a 20 69 6e reated for.** in
21a74 64 69 63 65 73 20 77 68 65 72 65 20 61 52 65 67 dices where aReg
21a75 49 64 78 5b 69 5d 3d 3d 30 2e 20 20 54 68 65 20 Idx[i]==0. The
21a76 6f 72 64 65 72 20 6f 66 20 69 6e 64 69 63 65 73 order of indices
21a77 20 69 6e 20 61 52 65 67 49 64 78 5b 5d 20 69 73 in aRegIdx[] is
21a78 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 .** the same as
21a79 74 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64 the order of ind
21a7a 69 63 65 73 20 6f 6e 20 74 68 65 20 6c 69 6e 6b ices on the link
21a7b 65 64 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 63 ed list of indic
21a7c 65 73 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 74 es.** attached t
21a7d 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a o the table..**.
21a7e 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
21a7f 61 6c 73 6f 20 67 65 6e 65 72 61 74 65 73 20 63 also generates c
21a80 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e ode to check con
21a81 73 74 72 61 69 6e 74 73 2e 20 20 4e 4f 54 20 4e straints. NOT N
21a82 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43 4b 2c 20 61 ULL,.** CHECK, a
21a83 6e 64 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 nd UNIQUE constr
21a84 61 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 63 68 aints are all ch
21a85 65 63 6b 65 64 2e 20 20 49 66 20 61 20 63 6f 6e ecked. If a con
21a86 73 74 72 61 69 6e 74 20 66 61 69 6c 73 2c 0a 2a straint fails,.*
21a87 2a 20 74 68 65 6e 20 74 68 65 20 61 70 70 72 6f * then the appro
21a88 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 69 73 priate action is
21a89 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 54 68 65 performed. The
21a8a 72 65 20 61 72 65 20 66 69 76 65 20 70 6f 73 73 re are five poss
21a8b 69 62 6c 65 0a 2a 2a 20 61 63 74 69 6f 6e 73 3a ible.** actions:
21a8c 20 52 4f 4c 4c 42 41 43 4b 2c 20 41 42 4f 52 54 ROLLBACK, ABORT
21a8d 2c 20 46 41 49 4c 2c 20 52 45 50 4c 41 43 45 2c , FAIL, REPLACE,
21a8e 20 61 6e 64 20 49 47 4e 4f 52 45 2e 0a 2a 2a 0a and IGNORE..**.
21a8f 2a 2a 20 20 43 6f 6e 73 74 72 61 69 6e 74 20 74 ** Constraint t
21a90 79 70 65 20 20 41 63 74 69 6f 6e 20 20 20 20 20 ype Action
21a91 20 20 57 68 61 74 20 48 61 70 70 65 6e 73 0a 2a What Happens.*
21a92 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * -------------
21a93 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 -- ----------
21a94 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
21a95 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
21a96 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 61 6e ---------.** an
21a97 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 52 y R
21a98 4f 4c 4c 42 41 43 4b 20 20 20 20 20 54 68 65 20 OLLBACK The
21a99 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
21a9a 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 ion is rolled ba
21a9b 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 ck and.**
21a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21a9d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
21a9e 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e 73 20 _exec() returns
21a9f 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68 immediately with
21aa0 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 a.**
21aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21aa2 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65 return code
21aa3 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 of SQLITE_CONST
21aa4 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e RAINT..**.** an
21aa5 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41 y A
21aa6 42 4f 52 54 20 20 20 20 20 20 20 20 42 61 63 6b BORT Back
21aa7 20 6f 75 74 20 63 68 61 6e 67 65 73 20 66 72 6f out changes fro
21aa8 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f m the current co
21aa9 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 mmand.**
21aaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21aab 20 20 20 20 20 20 20 20 6f 6e 6c 79 20 28 64 6f only (do
21aac 20 6e 6f 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65 not do a comple
21aad 74 65 20 72 6f 6c 6c 62 61 63 6b 29 20 74 68 65 te rollback) the
21aae 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 n.**
21aaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ab0 20 20 20 20 63 61 75 73 65 20 73 71 6c 69 74 65 cause sqlite
21ab1 33 5f 65 78 65 63 28 29 20 74 6f 20 72 65 74 75 3_exec() to retu
21ab2 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a rn immediately.*
21ab3 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
21ab4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ab5 20 77 69 74 68 20 53 51 4c 49 54 45 5f 43 4f 4e with SQLITE_CON
21ab6 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 STRAINT..**.**
21ab7 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 any
21ab8 20 46 41 49 4c 20 20 20 20 20 20 20 20 20 53 71 FAIL Sq
21ab9 6c 69 74 65 5f 65 78 65 63 28 29 20 72 65 74 75 lite_exec() retu
21aba 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 rns immediately
21abb 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20 20 with a.**
21abc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21abd 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
21abe 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 code of SQLITE_C
21abf 4f 4e 53 54 52 41 49 4e 54 2e 20 20 54 68 65 0a ONSTRAINT. The.
21ac0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
21ac1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ac2 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 transaction is
21ac3 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b not rolled back
21ac4 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 20 20 20 20 and any.**
21ac5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ac6 20 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 prior
21ac7 20 63 68 61 6e 67 65 73 20 61 72 65 20 72 65 74 changes are ret
21ac8 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e ained..**.** an
21ac9 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49 y I
21aca 47 4e 4f 52 45 20 20 20 20 20 20 20 54 68 65 20 GNORE The
21acb 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61 6e record number an
21acc 64 20 64 61 74 61 20 69 73 20 70 6f 70 70 65 64 d data is popped
21acd 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 from.**
21ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21acf 20 20 20 20 20 20 20 20 74 68 65 20 73 74 61 63 the stac
21ad0 6b 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 61 k and there is a
21ad1 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70 n immediate jump
21ad2 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
21ad3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ad4 20 20 20 74 6f 20 6c 61 62 65 6c 20 69 67 6e 6f to label igno
21ad5 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 4e reDest..**.** N
21ad6 4f 54 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 OT NULL
21ad7 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68 65 REPLACE The
21ad8 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 72 NULL value is r
21ad9 65 70 6c 61 63 65 20 62 79 20 74 68 65 20 64 65 eplace by the de
21ada 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 fault.**
21adb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21adc 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 66 6f value fo
21add 72 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20 20 r that column.
21ade 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 If the default v
21adf 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 alue.**
21ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ae1 20 20 20 20 20 20 20 69 73 20 4e 55 4c 4c 2c 20 is NULL,
21ae2 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 74 68 the action is th
21ae3 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52 54 2e e same as ABORT.
21ae4 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51 55 45 20 20 .**.** UNIQUE
21ae5 20 20 20 20 20 20 20 20 20 52 45 50 4c 41 43 45 REPLACE
21ae6 20 20 20 20 20 20 54 68 65 20 6f 74 68 65 72 20 The other
21ae7 72 6f 77 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 row that conflic
21ae8 74 73 20 77 69 74 68 20 74 68 65 20 72 6f 77 0a ts with the row.
21ae9 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
21aea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21aeb 20 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 being inserted
21aec 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a is removed..**.
21aed 2a 2a 20 20 43 48 45 43 4b 20 20 20 20 20 20 20 ** CHECK
21aee 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20 REPLACE
21aef 20 20 49 6c 6c 65 67 61 6c 2e 20 20 54 68 65 20 Illegal. The
21af0 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 65 78 results in an ex
21af1 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 ception..**.** W
21af2 68 69 63 68 20 61 63 74 69 6f 6e 20 74 6f 20 74 hich action to t
21af3 61 6b 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 ake is determine
21af4 64 20 62 79 20 74 68 65 20 6f 76 65 72 72 69 64 d by the overrid
21af5 65 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 eError parameter
21af6 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f 76 65 72 72 ..** Or if overr
21af7 69 64 65 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 ideError==OE_Def
21af8 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65 20 70 ault, then the p
21af9 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 20 70 Parse->onError p
21afa 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 75 arameter.** is u
21afb 73 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 sed. Or if pPar
21afc 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f se->onError==OE_
21afd 44 65 66 61 75 6c 74 20 74 68 65 6e 20 74 68 65 Default then the
21afe 20 6f 6e 45 72 72 6f 72 20 76 61 6c 75 65 0a 2a onError value.*
21aff 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74 72 * for the constr
21b00 61 69 6e 74 20 69 73 20 75 73 65 64 2e 0a 2a 2a aint is used..**
21b01 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 .** The calling
21b02 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6f 70 65 routine must ope
21b03 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 n a read/write c
21b04 75 72 73 6f 72 20 66 6f 72 20 70 54 61 62 20 77 ursor for pTab w
21b05 69 74 68 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 ith.** cursor nu
21b06 6d 62 65 72 20 22 62 61 73 65 43 75 72 22 2e 20 mber "baseCur".
21b07 20 41 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 All indices of
21b08 70 54 61 62 20 6d 75 73 74 20 61 6c 73 6f 20 68 pTab must also h
21b09 61 76 65 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 ave open.** read
21b0a 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 77 /write cursors w
21b0b 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 ith cursor numbe
21b0c 72 20 62 61 73 65 43 75 72 2b 69 20 66 6f 72 20 r baseCur+i for
21b0d 74 68 65 20 69 2d 74 68 20 63 75 72 73 6f 72 2e the i-th cursor.
21b0e 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 74 .** Except, if t
21b0f 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 73 73 69 here is no possi
21b10 62 69 6c 69 74 79 20 6f 66 20 61 20 52 45 50 4c bility of a REPL
21b11 41 43 45 20 61 63 74 69 6f 6e 20 74 68 65 6e 0a ACE action then.
21b12 2a 2a 20 63 75 72 73 6f 72 73 20 64 6f 20 6e 6f ** cursors do no
21b13 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 t need to be ope
21b14 6e 20 66 6f 72 20 69 6e 64 69 63 65 73 20 77 68 n for indices wh
21b15 65 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d ere aRegIdx[i]==
21b16 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 0..*/.SQLITE_PRI
21b17 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
21b18 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 3GenerateConstra
21b19 69 6e 74 43 68 65 63 6b 73 28 0a 20 20 50 61 72 intChecks(. Par
21b1a 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
21b1b 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 /* The parser c
21b1c 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c ontext */. Tabl
21b1d 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 e *pTab,
21b1e 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 /* the table int
21b1f 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69 o which we are i
21b20 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e nserting */. in
21b21 74 20 62 61 73 65 43 75 72 2c 20 20 20 20 20 20 t baseCur,
21b22 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 /* Index of a
21b23 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f read/write curso
21b24 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70 54 r pointing at pT
21b25 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 ab */. int regR
21b26 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20 49 owid, /* I
21b27 6e 64 65 78 20 6f 66 20 74 68 65 20 72 61 6e 67 ndex of the rang
21b28 65 20 6f 66 20 69 6e 70 75 74 20 72 65 67 69 73 e of input regis
21b29 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 ters */. int *a
21b2a 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a RegIdx, /*
21b2b 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20 62 Register used b
21b2c 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30 y each index. 0
21b2d 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 for unused indi
21b2e 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 6f 77 ces */. int row
21b2f 69 64 43 68 6e 67 2c 20 20 20 20 20 20 2f 2a 20 idChng, /*
21b30 54 72 75 65 20 69 66 20 74 68 65 20 72 6f 77 69 True if the rowi
21b31 64 20 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65 20 d might collide
21b32 77 69 74 68 20 65 78 69 73 74 69 6e 67 20 65 6e with existing en
21b33 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 try */. int isU
21b34 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 pdate, /*
21b35 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c True for UPDATE,
21b36 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 False for INSER
21b37 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 76 65 72 72 T */. int overr
21b38 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a 20 4f 76 ideError, /* Ov
21b39 65 72 72 69 64 65 20 6f 6e 45 72 72 6f 72 20 74 erride onError t
21b3a 6f 20 74 68 69 73 20 69 66 20 6e 6f 74 20 4f 45 o this if not OE
21b3b 5f 44 65 66 61 75 6c 74 20 2a 2f 0a 20 20 69 6e _Default */. in
21b3c 74 20 69 67 6e 6f 72 65 44 65 73 74 20 20 20 20 t ignoreDest
21b3d 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 69 /* Jump to thi
21b3e 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e 20 4f 45 s label on an OE
21b3f 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c 75 74 69 _Ignore resoluti
21b40 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 on */.){. int i
21b41 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 ;. Vdbe *v;. i
21b42 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6f nt nCol;. int o
21b43 6e 45 72 72 6f 72 3b 0a 20 20 69 6e 74 20 6a 31 nError;. int j1
21b44 2c 20 6a 32 2c 20 6a 33 3b 20 20 20 20 20 2f 2a , j2, j3; /*
21b45 20 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a 75 Addresses of ju
21b46 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 mp instructions
21b47 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 */. int regData
21b48 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 ; /* Regi
21b49 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ster containing
21b4a 66 69 72 73 74 20 64 61 74 61 20 63 6f 6c 75 6d first data colum
21b4b 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b n */. int iCur;
21b4c 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a . Index *pIdx;.
21b4d 20 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63 int seenReplac
21b4e 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 e = 0;. int has
21b4f 54 77 6f 52 6f 77 69 64 73 20 3d 20 28 69 73 55 TwoRowids = (isU
21b50 70 64 61 74 65 20 26 26 20 72 6f 77 69 64 43 68 pdate && rowidCh
21b51 6e 67 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 ng);.. v = sqli
21b52 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
21b53 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 e);. assert( v!
21b54 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
21b55 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 pTab->pSelect==0
21b56 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62 ); /* This tab
21b57 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57 le is not a VIEW
21b58 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61 */. nCol = pTa
21b59 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61 b->nCol;. regDa
21b5a 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 ta = regRowid +
21b5b 31 3b 0a 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 1;... /* Test a
21b5c 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 ll NOT NULL cons
21b5d 74 72 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 traints.. */.
21b5e 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b for(i=0; i<nCol;
21b5f 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 i++){. if( i
21b60 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b ==pTab->iPKey ){
21b61 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b . continue;
21b62 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72 72 . }. onErr
21b63 6f 72 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b or = pTab->aCol[
21b64 69 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20 i].notNull;.
21b65 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f if( onError==OE_
21b66 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b None ) continue;
21b67 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 . if( overrid
21b68 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 eError!=OE_Defau
21b69 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 lt ){. onEr
21b6a 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 ror = overrideEr
21b6b 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 ror;. }else i
21b6c 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 f( onError==OE_D
21b6d 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 efault ){.
21b6e 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f onError = OE_Abo
21b6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 rt;. }. if
21b70 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 ( onError==OE_Re
21b71 70 6c 61 63 65 20 26 26 20 70 54 61 62 2d 3e 61 place && pTab->a
21b72 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20 Col[i].pDflt==0
21b73 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 ){. onError
21b74 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 = OE_Abort;.
21b75 20 7d 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 }. j1 = sqli
21b76 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
21b77 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 OP_NotNull, reg
21b78 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 61 73 73 Data+i);. ass
21b79 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 ert( onError==OE
21b7a 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 _Rollback || onE
21b7b 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c rror==OE_Abort |
21b7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 | onError==OE_Fa
21b7d 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e il. || on
21b7e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 Error==OE_Ignore
21b7f 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f || onError==OE_
21b80 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73 Replace );. s
21b81 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 witch( onError )
21b82 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f {. case OE_
21b83 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 Rollback:.
21b84 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 case OE_Abort:.
21b85 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 case OE_Fai
21b86 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 68 61 l: {. cha
21b87 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20 20 r *zMsg;.
21b88 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21b89 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 p2(v, OP_Halt, S
21b8a 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
21b8b 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20 , onError);.
21b8c 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 zMsg = sqlit
21b8d 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 e3MPrintf(pParse
21b8e 2d 3e 64 62 2c 20 22 25 73 2e 25 73 20 6d 61 79 ->db, "%s.%s may
21b8f 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a 20 not be NULL",.
21b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b91 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 pTa
21b92 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e b->zName, pTab->
21b93 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a aCol[i].zName);.
21b94 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21b95 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d dbeChangeP4(v, -
21b96 31 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 1, zMsg, P4_DYNA
21b97 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72 MIC);. br
21b98 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
21b99 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 case OE_Ignor
21b9a 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c e: {. sql
21b9b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21b9c 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 , OP_Goto, 0, ig
21b9d 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 noreDest);.
21b9e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
21b9f 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f }. case OE_
21ba0 52 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 Replace: {.
21ba1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
21ba2 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d de(pParse, pTab-
21ba3 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 >aCol[i].pDflt,
21ba4 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 regData+i);.
21ba5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
21ba6 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c }. }. sql
21ba7 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
21ba8 28 76 2c 20 6a 31 29 3b 0a 20 20 7d 0a 0a 20 20 (v, j1);. }..
21ba9 2f 2a 20 54 65 73 74 20 61 6c 6c 20 43 48 45 43 /* Test all CHEC
21baa 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 K constraints.
21bab 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
21bac 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 69 E_OMIT_CHECK. i
21bad 66 28 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 f( pTab->pCheck
21bae 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e && (pParse->db->
21baf 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49 flags & SQLITE_I
21bb0 67 6e 6f 72 65 43 68 65 63 6b 73 29 3d 3d 30 20 gnoreChecks)==0
21bb1 29 7b 0a 20 20 20 20 69 6e 74 20 61 6c 6c 4f 6b ){. int allOk
21bb2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
21bb3 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 keLabel(v);.
21bb4 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 20 3d pParse->ckBase =
21bb5 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20 73 71 regData;. sq
21bb6 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
21bb7 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 70 43 pParse, pTab->pC
21bb8 68 65 63 6b 2c 20 61 6c 6c 4f 6b 2c 20 53 51 4c heck, allOk, SQL
21bb9 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b ITE_JUMPIFNULL);
21bba 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f . onError = o
21bbb 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 verrideError!=OE
21bbc 5f 44 65 66 61 75 6c 74 20 3f 20 6f 76 65 72 72 _Default ? overr
21bbd 69 64 65 45 72 72 6f 72 20 3a 20 4f 45 5f 41 62 ideError : OE_Ab
21bbe 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 ort;. if( onE
21bbf 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 rror==OE_Ignore
21bc0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
21bc1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21bc2 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 _Goto, 0, ignore
21bc3 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Dest);. }else
21bc4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
21bc5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21bc6 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e Halt, SQLITE_CON
21bc7 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 STRAINT, onError
21bc8 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
21bc9 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
21bca 61 62 65 6c 28 76 2c 20 61 6c 6c 4f 6b 29 3b 0a abel(v, allOk);.
21bcb 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 }.#endif /* !d
21bcc 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
21bcd 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 IT_CHECK) */..
21bce 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61 6e /* If we have an
21bcf 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
21bd0 20 4b 45 59 2c 20 6d 61 6b 65 20 73 75 72 65 20 KEY, make sure
21bd1 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 0a the primary key.
21bd2 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 ** of the new
21bd3 72 65 63 6f 72 64 20 64 6f 65 73 20 6e 6f 74 20 record does not
21bd4 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74 previously exist
21bd5 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 . Except, if th
21bd6 69 73 0a 20 20 2a 2a 20 69 73 20 61 6e 20 55 50 is. ** is an UP
21bd7 44 41 54 45 20 61 6e 64 20 74 68 65 20 70 72 69 DATE and the pri
21bd8 6d 61 72 79 20 6b 65 79 20 69 73 20 6e 6f 74 20 mary key is not
21bd9 63 68 61 6e 67 69 6e 67 2c 20 74 68 61 74 20 69 changing, that i
21bda 73 20 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 s OK.. */. if(
21bdb 20 72 6f 77 69 64 43 68 6e 67 20 29 7b 0a 20 20 rowidChng ){.
21bdc 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61 62 onError = pTab
21bdd 2d 3e 6b 65 79 43 6f 6e 66 3b 0a 20 20 20 20 69 ->keyConf;. i
21bde 66 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 f( overrideError
21bdf 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a !=OE_Default ){.
21be0 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 onError =
21be1 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 overrideError;.
21be2 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 }else if( onE
21be3 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 rror==OE_Default
21be4 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f ){. onErro
21be5 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 r = OE_Abort;.
21be6 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 69 66 28 }. . if(
21be7 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 onError!=OE_Rep
21be8 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49 lace || pTab->pI
21be9 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 69 66 ndex ){. if
21bea 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20 ( isUpdate ){.
21beb 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 j2 = sqlit
21bec 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
21bed 4f 50 5f 45 71 2c 20 72 65 67 52 6f 77 69 64 2c OP_Eq, regRowid,
21bee 20 30 2c 20 72 65 67 52 6f 77 69 64 2d 31 29 3b 0, regRowid-1);
21bef 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a . }. j
21bf0 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 3 = sqlite3VdbeA
21bf1 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 ddOp3(v, OP_NotE
21bf2 78 69 73 74 73 2c 20 62 61 73 65 43 75 72 2c 20 xists, baseCur,
21bf3 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 0, regRowid);.
21bf4 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72 switch( onEr
21bf5 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 64 ror ){. d
21bf6 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
21bf7 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 onError = OE
21bf8 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 _Abort;.
21bf9 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 /* Fall thru i
21bfa 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 nto the next cas
21bfb 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 e */. }.
21bfc 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 case OE_R
21bfd 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 20 ollback:.
21bfe 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a case OE_Abort:.
21bff 20 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f case OE_
21c00 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 Fail: {.
21c01 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21c02 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 Op4(v, OP_Halt,
21c03 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
21c04 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 0a 20 T, onError, 0,.
21c05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c06 20 20 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 "PRIMA
21c07 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75 RY KEY must be u
21c08 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 nique", P4_STATI
21c09 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 C);. br
21c0a 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
21c0b 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 case OE_R
21c0c 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 20 eplace: {.
21c0d 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 sqlite3Gener
21c0e 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 ateRowIndexDelet
21c0f 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 e(pParse, pTab,
21c10 62 61 73 65 43 75 72 2c 20 30 29 3b 0a 20 20 20 baseCur, 0);.
21c11 20 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61 seenRepla
21c12 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ce = 1;.
21c13 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
21c14 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 }. case
21c15 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 OE_Ignore: {.
21c16 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 assert( s
21c17 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b eenReplace==0 );
21c18 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
21c19 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21c1a 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f OP_Goto, 0, igno
21c1b 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 reDest);.
21c1c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
21c1d 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
21c1e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
21c1f 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 pHere(v, j3);.
21c20 20 20 20 20 69 66 28 20 69 73 55 70 64 61 74 65 if( isUpdate
21c21 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
21c22 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
21c23 76 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20 7d 0a v, j2);. }.
21c24 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
21c25 54 65 73 74 20 61 6c 6c 20 55 4e 49 51 55 45 20 Test all UNIQUE
21c26 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 79 20 63 constraints by c
21c27 72 65 61 74 69 6e 67 20 65 6e 74 72 69 65 73 20 reating entries
21c28 66 6f 72 20 65 61 63 68 20 55 4e 49 51 55 45 0a for each UNIQUE.
21c29 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d ** index and m
21c2a 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 aking sure that
21c2b 64 75 70 6c 69 63 61 74 65 20 65 6e 74 72 69 65 duplicate entrie
21c2c 73 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 s do not already
21c2d 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41 64 64 exist.. ** Add
21c2e 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 73 the new records
21c2f 20 74 6f 20 74 68 65 20 69 6e 64 69 63 65 73 20 to the indices
21c30 61 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20 as we go.. */.
21c31 20 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64 for(iCur=0, pId
21c32 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 x=pTab->pIndex;
21c33 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d pIdx; pIdx=pIdx-
21c34 3e 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b >pNext, iCur++){
21c35 0a 20 20 20 20 69 6e 74 20 72 65 67 49 64 78 3b . int regIdx;
21c36 0a 20 20 20 20 69 6e 74 20 72 65 67 52 3b 0a 0a . int regR;..
21c37 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b if( aRegIdx[
21c38 69 43 75 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 iCur]==0 ) conti
21c39 6e 75 65 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e nue; /* Skip un
21c3a 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a used indices */.
21c3b 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 . /* Create a
21c3c 20 6b 65 79 20 66 6f 72 20 61 63 63 65 73 73 69 key for accessi
21c3d 6e 67 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 ng the index ent
21c3e 72 79 20 2a 2f 0a 20 20 20 20 72 65 67 49 64 78 ry */. regIdx
21c3f 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
21c40 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 pRange(pParse, p
21c41 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b Idx->nColumn+1);
21c42 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
21c43 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 pIdx->nColumn; i
21c44 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 ++){. int i
21c45 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c dx = pIdx->aiCol
21c46 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 umn[i];. if
21c47 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b ( idx==pTab->iPK
21c48 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ey ){. sq
21c49 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21c4a 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 v, OP_SCopy, reg
21c4b 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29 Rowid, regIdx+i)
21c4c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
21c4d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21c4e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 beAddOp2(v, OP_S
21c4f 43 6f 70 79 2c 20 72 65 67 44 61 74 61 2b 69 64 Copy, regData+id
21c50 78 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 x, regIdx+i);.
21c51 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
21c52 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21c53 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 2(v, OP_SCopy, r
21c54 65 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b egRowid, regIdx+
21c55 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 i);. sqlite3V
21c56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
21c57 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 49 MakeRecord, regI
21c58 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d dx, pIdx->nColum
21c59 6e 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 43 75 n+1, aRegIdx[iCu
21c5a 72 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 r]);. sqlite3
21c5b 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 IndexAffinityStr
21c5c 28 76 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 73 (v, pIdx);. s
21c5d 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 qlite3ExprCacheA
21c5e 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 ffinityChange(pP
21c5f 61 72 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 arse, regIdx, pI
21c60 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a dx->nColumn+1);.
21c61 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 sqlite3Relea
21c62 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 seTempRange(pPar
21c63 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 se, regIdx, pIdx
21c64 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 ->nColumn+1);..
21c65 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 /* Find out w
21c66 68 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 hat action to ta
21c67 6b 65 20 69 6e 20 63 61 73 65 20 74 68 65 72 65 ke in case there
21c68 20 69 73 20 61 6e 20 69 6e 64 65 78 69 6e 67 20 is an indexing
21c69 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 20 20 conflict */.
21c6a 6f 6e 45 72 72 6f 72 20 3d 20 70 49 64 78 2d 3e onError = pIdx->
21c6b 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 onError;. if(
21c6c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e onError==OE_Non
21c6d 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f e ) continue; /
21c6e 2a 20 70 49 64 78 20 69 73 20 6e 6f 74 20 61 20 * pIdx is not a
21c6f 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a UNIQUE index */.
21c70 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 if( override
21c71 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c Error!=OE_Defaul
21c72 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 t ){. onErr
21c73 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 or = overrideErr
21c74 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 or;. }else if
21c75 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 ( onError==OE_De
21c76 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f fault ){. o
21c77 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 nError = OE_Abor
21c78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
21c79 20 73 65 65 6e 52 65 70 6c 61 63 65 20 29 7b 0a seenReplace ){.
21c7a 20 20 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f if( onErro
21c7b 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 20 6f r==OE_Ignore ) o
21c7c 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c nError = OE_Repl
21c7d 61 63 65 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 ace;. else
21c7e 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f if( onError==OE_
21c7f 46 61 69 6c 20 29 20 6f 6e 45 72 72 6f 72 20 3d Fail ) onError =
21c80 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d OE_Abort;. }
21c81 0a 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 . .. /* Ch
21c82 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 eck to see if th
21c83 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 e new index entr
21c84 79 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 y will be unique
21c85 20 2a 2f 0a 20 20 20 20 6a 32 20 3d 20 73 71 6c */. j2 = sql
21c86 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
21c87 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 , OP_IsNull, reg
21c88 49 64 78 2c 20 30 2c 20 70 49 64 78 2d 3e 6e 43 Idx, 0, pIdx->nC
21c89 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 72 65 67 52 olumn);. regR
21c8a 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
21c8b 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
21c8c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21c8d 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c Op2(v, OP_SCopy,
21c8e 20 72 65 67 52 6f 77 69 64 2d 68 61 73 54 77 6f regRowid-hasTwo
21c8f 52 6f 77 69 64 73 2c 20 72 65 67 52 29 3b 0a 20 Rowids, regR);.
21c90 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 j3 = sqlite3V
21c91 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
21c92 49 73 55 6e 69 71 75 65 2c 20 62 61 73 65 43 75 IsUnique, baseCu
21c93 72 2b 69 43 75 72 2b 31 2c 20 30 2c 0a 20 20 20 r+iCur+1, 0,.
21c94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c95 20 20 20 20 20 20 20 20 72 65 67 52 2c 20 53 51 regR, SQ
21c96 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 LITE_INT_TO_PTR(
21c97 61 52 65 67 49 64 78 5b 69 43 75 72 5d 29 2c 0a aRegIdx[iCur]),.
21c98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c99 20 20 20 20 20 20 20 20 20 20 20 50 34 5f 49 4e P4_IN
21c9a 54 33 32 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 T32);.. /* Ge
21c9b 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 nerate code that
21c9c 20 65 78 65 63 75 74 65 73 20 69 66 20 74 68 65 executes if the
21c9d 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79 new index entry
21c9e 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 20 2a is not unique *
21c9f 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e /. assert( on
21ca0 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 Error==OE_Rollba
21ca1 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f ck || onError==O
21ca2 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 E_Abort || onErr
21ca3 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 or==OE_Fail.
21ca4 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d || onError==
21ca5 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 OE_Ignore || onE
21ca6 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 rror==OE_Replace
21ca7 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 );. switch(
21ca8 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 onError ){.
21ca9 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 case OE_Rollbac
21caa 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 k:. case OE
21cab 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20 63 61 _Abort:. ca
21cac 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 se OE_Fail: {.
21cad 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 31 2c int j, n1,
21cae 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 63 68 61 n2;. cha
21caf 72 20 7a 45 72 72 4d 73 67 5b 32 30 30 5d 3b 0a r zErrMsg[200];.
21cb0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
21cb1 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
21cb2 7a 45 72 72 4d 73 67 29 2c 20 7a 45 72 72 4d 73 zErrMsg), zErrMs
21cb3 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 g,.
21cb4 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78 pIdx
21cb5 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 63 ->nColumn>1 ? "c
21cb6 6f 6c 75 6d 6e 73 20 22 20 3a 20 22 63 6f 6c 75 olumns " : "colu
21cb7 6d 6e 20 22 29 3b 0a 20 20 20 20 20 20 20 20 6e mn ");. n
21cb8 31 20 3d 20 73 74 72 6c 65 6e 28 7a 45 72 72 4d 1 = strlen(zErrM
21cb9 73 67 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 sg);. for
21cba 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 (j=0; j<pIdx->nC
21cbb 6f 6c 75 6d 6e 20 26 26 20 6e 31 3c 73 69 7a 65 olumn && n1<size
21cbc 6f 66 28 7a 45 72 72 4d 73 67 29 2d 33 30 3b 20 of(zErrMsg)-30;
21cbd 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 j++){.
21cbe 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 61 char *zCol = pTa
21cbf 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 b->aCol[pIdx->ai
21cc0 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 Column[j]].zName
21cc1 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 32 20 3d ;. n2 =
21cc2 20 73 74 72 6c 65 6e 28 7a 43 6f 6c 29 3b 0a 20 strlen(zCol);.
21cc3 20 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30 if( j>0
21cc4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
21cc5 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
21cc6 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29 (sizeof(zErrMsg)
21cc7 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e 31 -n1, &zErrMsg[n1
21cc8 5d 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 20 20 ], ", ");.
21cc9 20 20 20 20 20 20 6e 31 20 2b 3d 20 32 3b 0a 20 n1 += 2;.
21cca 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
21ccb 20 20 20 20 20 69 66 28 20 6e 31 2b 6e 32 3e 73 if( n1+n2>s
21ccc 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29 2d 33 izeof(zErrMsg)-3
21ccd 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
21cce 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
21ccf 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 f(sizeof(zErrMsg
21cd0 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e )-n1, &zErrMsg[n
21cd1 31 5d 2c 20 22 2e 2e 2e 22 29 3b 0a 20 20 20 20 1], "...");.
21cd2 20 20 20 20 20 20 20 20 6e 31 20 2b 3d 20 33 3b n1 += 3;
21cd3 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 . bre
21cd4 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 ak;. }e
21cd5 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
21cd6 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
21cd7 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 f(sizeof(zErrMsg
21cd8 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e )-n1, &zErrMsg[n
21cd9 31 5d 2c 20 22 25 73 22 2c 20 7a 43 6f 6c 29 3b 1], "%s", zCol);
21cda 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 31 20 . n1
21cdb 2b 3d 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 20 += n2;.
21cdc 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
21cdd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
21cde 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 rintf(sizeof(zEr
21cdf 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d rMsg)-n1, &zErrM
21ce0 73 67 5b 6e 31 5d 2c 20 0a 20 20 20 20 20 20 20 sg[n1], .
21ce1 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 pIdx->nColu
21ce2 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20 6e 6f 74 mn>1 ? " are not
21ce3 20 75 6e 69 71 75 65 22 20 3a 20 22 20 69 73 20 unique" : " is
21ce4 6e 6f 74 20 75 6e 69 71 75 65 22 29 3b 0a 20 20 not unique");.
21ce5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21ce6 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 eAddOp4(v, OP_Ha
21ce7 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 lt, SQLITE_CONST
21ce8 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 RAINT, onError,
21ce9 30 2c 20 7a 45 72 72 4d 73 67 2c 30 29 3b 0a 20 0, zErrMsg,0);.
21cea 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
21ceb 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 }. case
21cec 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 OE_Ignore: {.
21ced 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65 assert( se
21cee 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a enReplace==0 );.
21cef 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21cf0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21cf1 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 Goto, 0, ignoreD
21cf2 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 est);. br
21cf3 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
21cf4 20 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c 61 case OE_Repla
21cf5 63 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 ce: {. sq
21cf6 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 lite3GenerateRow
21cf7 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70 Delete(pParse, p
21cf8 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65 Tab, baseCur, re
21cf9 67 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 gR, 0);.
21cfa 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31 3b seenReplace = 1;
21cfb 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
21cfc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
21cfd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
21cfe 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 pHere(v, j2);.
21cff 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
21d00 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 pHere(v, j3);.
21d01 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
21d02 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
21d03 72 65 67 52 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a regR);. }.}../*
21d04 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
21d05 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 generates code
21d06 74 6f 20 66 69 6e 69 73 68 20 74 68 65 20 49 4e to finish the IN
21d07 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20 6f SERT or UPDATE o
21d08 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 peration.** that
21d09 20 77 61 73 20 73 74 61 72 74 65 64 20 62 79 20 was started by
21d0a 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 a prior call to
21d0b 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 sqlite3GenerateC
21d0c 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 2e onstraintChecks.
21d0d 0a 2a 2a 20 41 20 63 6f 6e 73 65 63 75 74 69 76 .** A consecutiv
21d0e 65 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69 73 e range of regis
21d0f 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 ters starting at
21d10 20 72 65 67 52 6f 77 69 64 20 63 6f 6e 74 61 69 regRowid contai
21d11 6e 73 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20 ns the.** rowid
21d12 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 and the content
21d13 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a to be inserted..
21d14 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 **.** The argume
21d15 6e 74 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 nts to this rout
21d16 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20 74 68 ine should be th
21d17 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 66 69 e same as the fi
21d18 72 73 74 20 73 69 78 0a 2a 2a 20 61 72 67 75 6d rst six.** argum
21d19 65 6e 74 73 20 74 6f 20 73 71 6c 69 74 65 33 47 ents to sqlite3G
21d1a 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e enerateConstrain
21d1b 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 tChecks..*/.SQLI
21d1c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
21d1d 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 sqlite3CompleteI
21d1e 6e 73 65 72 74 69 6f 6e 28 0a 20 20 50 61 72 73 nsertion(. Pars
21d1f 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
21d20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f /* The parser co
21d21 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 ntext */. Table
21d22 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 2f *pTab, /
21d23 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f * the table into
21d24 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69 6e which we are in
21d25 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 serting */. int
21d26 20 62 61 73 65 43 75 72 2c 20 20 20 20 20 20 20 baseCur,
21d27 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 72 /* Index of a r
21d28 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 ead/write cursor
21d29 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70 54 61 pointing at pTa
21d2a 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f b */. int regRo
21d2b 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20 52 61 wid, /* Ra
21d2c 6e 67 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 2a nge of content *
21d2d 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 /. int *aRegIdx
21d2e 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 , /* Regis
21d2f 74 65 72 20 75 73 65 64 20 62 79 20 65 61 63 68 ter used by each
21d30 20 69 6e 64 65 78 2e 20 20 30 20 66 6f 72 20 75 index. 0 for u
21d31 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f nused indices */
21d32 0a 20 20 69 6e 74 20 72 6f 77 69 64 43 68 6e 67 . int rowidChng
21d33 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 , /* True i
21d34 66 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d f the record num
21d35 62 65 72 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 ber will change
21d36 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74 */. int isUpdat
21d37 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 e, /* True
21d38 20 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c for UPDATE, Fal
21d39 73 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f se for INSERT */
21d3a 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 2c 20 20 . int newIdx,
21d3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
21d3c 6f 66 20 4e 45 57 20 74 61 62 6c 65 20 66 6f 72 of NEW table for
21d3d 20 74 72 69 67 67 65 72 73 2e 20 20 2d 31 20 69 triggers. -1 i
21d3e 66 20 6e 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 f none */. int
21d3f 61 70 70 65 6e 64 42 69 61 73 20 20 20 20 20 20 appendBias
21d40 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
21d41 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 is likely to be
21d42 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 29 7b 0a an append */.){.
21d43 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 int i;. Vdbe
21d44 2a 76 3b 0a 20 20 69 6e 74 20 6e 49 64 78 3b 0a *v;. int nIdx;.
21d45 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 Index *pIdx;.
21d46 20 69 6e 74 20 70 69 6b 5f 66 6c 61 67 73 3b 0a int pik_flags;.
21d47 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b 0a 20 int regData;.
21d48 20 69 6e 74 20 72 65 67 52 65 63 3b 0a 0a 20 20 int regRec;..
21d49 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
21d4a 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 be(pParse);. as
21d4b 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 sert( v!=0 );.
21d4c 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 70 53 assert( pTab->pS
21d4d 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 elect==0 ); /*
21d4e 54 68 69 73 20 74 61 62 6c 65 20 69 73 20 6e 6f This table is no
21d4f 74 20 61 20 56 49 45 57 20 2a 2f 0a 20 20 66 6f t a VIEW */. fo
21d50 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 r(nIdx=0, pIdx=p
21d51 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 Tab->pIndex; pId
21d52 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e x; pIdx=pIdx->pN
21d53 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20 ext, nIdx++){}.
21d54 20 66 6f 72 28 69 3d 6e 49 64 78 2d 31 3b 20 69 for(i=nIdx-1; i
21d55 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 >=0; i--){. i
21d56 66 28 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30 f( aRegIdx[i]==0
21d57 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
21d58 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21d59 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 p2(v, OP_IdxInse
21d5a 72 74 2c 20 62 61 73 65 43 75 72 2b 69 2b 31 2c rt, baseCur+i+1,
21d5b 20 61 52 65 67 49 64 78 5b 69 5d 29 3b 0a 20 20 aRegIdx[i]);.
21d5c 7d 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65 }. regData = re
21d5d 67 52 6f 77 69 64 20 2b 20 31 3b 0a 20 20 72 65 gRowid + 1;. re
21d5e 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65 gRec = sqlite3Ge
21d5f 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 tTempReg(pParse)
21d60 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
21d61 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 ddOp3(v, OP_Make
21d62 52 65 63 6f 72 64 2c 20 72 65 67 44 61 74 61 2c Record, regData,
21d63 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 pTab->nCol, reg
21d64 52 65 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 Rec);. sqlite3T
21d65 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 ableAffinityStr(
21d66 76 2c 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 v, pTab);. sqli
21d67 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 te3ExprCacheAffi
21d68 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 nityChange(pPars
21d69 65 2c 20 72 65 67 44 61 74 61 2c 20 70 54 61 62 e, regData, pTab
21d6a 2d 3e 6e 43 6f 6c 29 3b 0a 23 69 66 6e 64 65 66 ->nCol);.#ifndef
21d6b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 SQLITE_OMIT_TRI
21d6c 47 47 45 52 0a 20 20 69 66 28 20 6e 65 77 49 64 GGER. if( newId
21d6d 78 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 x>=0 ){. sqli
21d6e 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
21d6f 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77 49 OP_Insert, newI
21d70 64 78 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52 dx, regRec, regR
21d71 6f 77 69 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 owid);. }.#endi
21d72 66 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e f. if( pParse->
21d73 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 70 69 nested ){. pi
21d74 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 7d k_flags = 0;. }
21d75 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b 5f 66 6c else{. pik_fl
21d76 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f 4e 43 48 ags = OPFLAG_NCH
21d77 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b 5f 66 6c ANGE;. pik_fl
21d78 61 67 73 20 7c 3d 20 28 69 73 55 70 64 61 74 65 ags |= (isUpdate
21d79 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 ?OPFLAG_ISUPDATE
21d7a 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 :OPFLAG_LASTROWI
21d7b 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 70 D);. }. if( ap
21d7c 70 65 6e 64 42 69 61 73 20 29 7b 0a 20 20 20 20 pendBias ){.
21d7d 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46 pik_flags |= OPF
21d7e 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20 20 7d 0a LAG_APPEND;. }.
21d7f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21d80 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 Op3(v, OP_Insert
21d81 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 65 , baseCur, regRe
21d82 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 c, regRowid);.
21d83 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 if( !pParse->nes
21d84 74 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ted ){. sqlit
21d85 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 e3VdbeChangeP4(v
21d86 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d , -1, pTab->zNam
21d87 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 e, P4_STATIC);.
21d88 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 }. sqlite3Vdbe
21d89 43 68 61 6e 67 65 50 35 28 76 2c 20 70 69 6b 5f ChangeP5(v, pik_
21d8a 66 6c 61 67 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a flags);.}../*.**
21d8b 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
21d8c 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20 63 75 hat will open cu
21d8d 72 73 6f 72 73 20 66 6f 72 20 61 20 74 61 62 6c rsors for a tabl
21d8e 65 20 61 6e 64 20 66 6f 72 20 61 6c 6c 0a 2a 2a e and for all.**
21d8f 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 61 74 indices of that
21d90 20 74 61 62 6c 65 2e 20 20 54 68 65 20 22 62 61 table. The "ba
21d91 73 65 43 75 72 22 20 70 61 72 61 6d 65 74 65 72 seCur" parameter
21d92 20 69 73 20 74 68 65 20 63 75 72 73 6f 72 20 6e is the cursor n
21d93 75 6d 62 65 72 20 75 73 65 64 0a 2a 2a 20 66 6f umber used.** fo
21d94 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e r the table. In
21d95 64 69 63 65 73 20 61 72 65 20 6f 70 65 6e 65 64 dices are opened
21d96 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 on subsequent c
21d97 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 ursors..**.** Re
21d98 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
21d99 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 of indices on th
21d9a 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 e table..*/.SQLI
21d9b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
21d9c 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 qlite3OpenTableA
21d9d 6e 64 49 6e 64 69 63 65 73 28 0a 20 20 50 61 72 ndIndices(. Par
21d9e 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a se *pParse, /*
21d9f 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
21da0 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 */. Table *pTa
21da1 62 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 b, /* Table
21da2 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a to be opened */.
21da3 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 int baseCur,
21da4 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d /* Cursor num
21da5 62 65 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 ber assigned to
21da6 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 the table */. i
21da7 6e 74 20 6f 70 20 20 20 20 20 20 20 20 20 20 20 nt op
21da8 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f /* OP_OpenRead o
21da9 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a r OP_OpenWrite *
21daa 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 /.){. int i;.
21dab 69 6e 74 20 69 44 62 3b 0a 20 20 49 6e 64 65 78 int iDb;. Index
21dac 20 2a 70 49 64 78 3b 0a 20 20 56 64 62 65 20 2a *pIdx;. Vdbe *
21dad 76 3b 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74 v;.. if( IsVirt
21dae 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 ual(pTab) ) retu
21daf 72 6e 20 30 3b 0a 20 20 69 44 62 20 3d 20 73 71 rn 0;. iDb = sq
21db0 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
21db1 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 ex(pParse->db, p
21db2 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Tab->pSchema);.
21db3 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 v = sqlite3GetV
21db4 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 dbe(pParse);. a
21db5 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 ssert( v!=0 );.
21db6 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c sqlite3OpenTabl
21db7 65 28 70 50 61 72 73 65 2c 20 62 61 73 65 43 75 e(pParse, baseCu
21db8 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 r, iDb, pTab, op
21db9 29 3b 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 );. for(i=1, pI
21dba 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b dx=pTab->pIndex;
21dbb 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 pIdx; pIdx=pIdx
21dbc 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 ->pNext, i++){.
21dbd 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 KeyInfo *pKey
21dbe 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b = sqlite3IndexK
21dbf 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 eyinfo(pParse, p
21dc0 49 64 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Idx);. assert
21dc1 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d ( pIdx->pSchema=
21dc2 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 =pTab->pSchema )
21dc3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21dc4 65 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 eAddOp4(v, op, i
21dc5 2b 62 61 73 65 43 75 72 2c 20 70 49 64 78 2d 3e +baseCur, pIdx->
21dc6 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 tnum, iDb,.
21dc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21dc8 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 (char*)pKey, P4
21dc9 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
21dca 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 );. VdbeComme
21dcb 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 nt((v, "%s", pId
21dcc 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a x->zName));. }.
21dcd 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 if( pParse->nT
21dce 61 62 3c 3d 62 61 73 65 43 75 72 2b 69 20 29 7b ab<=baseCur+i ){
21dcf 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 . pParse->nTa
21dd0 62 20 3d 20 62 61 73 65 43 75 72 2b 69 3b 0a 20 b = baseCur+i;.
21dd1 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 2d 31 3b }. return i-1;
21dd2 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 .}...#ifdef SQLI
21dd3 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 TE_TEST./*.** Th
21dd4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 e following glob
21dd5 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 al variable is i
21dd6 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 ncremented whene
21dd7 76 65 72 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 ver the.** trans
21dd8 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e fer optimization
21dd9 20 69 73 20 75 73 65 64 2e 20 20 54 68 69 73 20 is used. This
21dda 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 is used for test
21ddb 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 ing.** purposes
21ddc 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20 73 only - to make s
21ddd 75 72 65 20 74 68 65 20 74 72 61 6e 73 66 65 72 ure the transfer
21dde 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 72 65 optimization re
21ddf 61 6c 6c 79 0a 2a 2a 20 69 73 20 68 61 70 70 65 ally.** is happe
21de0 6e 69 6e 67 20 77 68 65 6e 20 69 74 20 69 73 20 ning when it is
21de1 73 75 70 70 6f 73 65 20 74 6f 2e 0a 2a 2f 0a 53 suppose to..*/.S
21de2 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
21de3 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f lite3_xferopt_co
21de4 75 6e 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 unt;.#endif /* S
21de5 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a QLITE_TEST */...
21de6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
21de7 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a 0a MIT_XFER_OPT./*.
21de8 2a 2a 20 43 68 65 63 6b 20 74 6f 20 63 6f 6c 6c ** Check to coll
21de9 61 74 69 6f 6e 20 6e 61 6d 65 73 20 74 6f 20 73 ation names to s
21dea 65 65 20 69 66 20 74 68 65 79 20 61 72 65 20 63 ee if they are c
21deb 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73 74 ompatible..*/.st
21dec 61 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f 6d atic int xferCom
21ded 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e patibleCollation
21dee 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31 2c (const char *z1,
21def 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 29 const char *z2)
21df0 7b 0a 20 20 69 66 28 20 7a 31 3d 3d 30 20 29 7b {. if( z1==0 ){
21df1 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 32 3d 3d . return z2==
21df2 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 32 3d 0;. }. if( z2=
21df3 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
21df4 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
21df5 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
21df6 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a 0a z1, z2)==0;.}...
21df7 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 /*.** Check to s
21df8 65 65 20 69 66 20 69 6e 64 65 78 20 70 53 72 63 ee if index pSrc
21df9 20 69 73 20 63 6f 6d 70 61 74 69 62 6c 65 20 61 is compatible a
21dfa 73 20 61 20 73 6f 75 72 63 65 20 6f 66 20 64 61 s a source of da
21dfb 74 61 0a 2a 2a 20 66 6f 72 20 69 6e 64 65 78 20 ta.** for index
21dfc 70 44 65 73 74 20 69 6e 20 61 6e 20 69 6e 73 65 pDest in an inse
21dfd 72 74 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 rt transfer opti
21dfe 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 72 mization. The r
21dff 75 6c 65 73 0a 2a 2a 20 66 6f 72 20 61 20 63 6f ules.** for a co
21e00 6d 70 61 74 69 62 6c 65 20 69 6e 64 65 78 3a 0a mpatible index:.
21e01 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68 65 **.** * The
21e02 20 69 6e 64 65 78 20 69 73 20 6f 76 65 72 20 74 index is over t
21e03 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20 63 he same set of c
21e04 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 olumns.** *
21e05 20 54 68 65 20 73 61 6d 65 20 44 45 53 43 20 61 The same DESC a
21e06 6e 64 20 41 53 43 20 6d 61 72 6b 69 6e 67 73 20 nd ASC markings
21e07 6f 63 63 75 72 73 20 6f 6e 20 61 6c 6c 20 63 6f occurs on all co
21e08 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 lumns.** *
21e09 54 68 65 20 73 61 6d 65 20 6f 6e 45 72 72 6f 72 The same onError
21e0a 20 70 72 6f 63 65 73 73 69 6e 67 20 28 4f 45 5f processing (OE_
21e0b 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 Abort, OE_Ignore
21e0c 2c 20 65 74 63 29 0a 2a 2a 20 20 20 20 2a 20 20 , etc).** *
21e0d 20 54 68 65 20 73 61 6d 65 20 63 6f 6c 6c 61 74 The same collat
21e0e 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 6e 20 ing sequence on
21e0f 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 each column.*/.s
21e10 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f tatic int xferCo
21e11 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 49 6e mpatibleIndex(In
21e12 64 65 78 20 2a 70 44 65 73 74 2c 20 49 6e 64 65 dex *pDest, Inde
21e13 78 20 2a 70 53 72 63 29 7b 0a 20 20 69 6e 74 20 x *pSrc){. int
21e14 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 65 i;. assert( pDe
21e15 73 74 20 26 26 20 70 53 72 63 20 29 3b 0a 20 20 st && pSrc );.
21e16 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e 70 assert( pDest->p
21e17 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 70 54 61 Table!=pSrc->pTa
21e18 62 6c 65 20 29 3b 0a 20 20 69 66 28 20 70 44 65 ble );. if( pDe
21e19 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53 72 st->nColumn!=pSr
21e1a 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 c->nColumn ){.
21e1b 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a return 0; /*
21e1c 20 44 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 Different numbe
21e1d 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a r of columns */.
21e1e 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d }. if( pDest-
21e1f 3e 6f 6e 45 72 72 6f 72 21 3d 70 53 72 63 2d 3e >onError!=pSrc->
21e20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 72 onError ){. r
21e21 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 eturn 0; /* Di
21e22 66 66 65 72 65 6e 74 20 63 6f 6e 66 6c 69 63 74 fferent conflict
21e23 20 72 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61 resolution stra
21e24 74 65 67 69 65 73 20 2a 2f 0a 20 20 7d 0a 20 20 tegies */. }.
21e25 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d for(i=0; i<pSrc-
21e26 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a >nColumn; i++){.
21e27 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61 69 if( pSrc->ai
21e28 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70 44 65 73 74 Column[i]!=pDest
21e29 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29 7b ->aiColumn[i] ){
21e2a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
21e2b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 /* Different
21e2c 63 6f 6c 75 6d 6e 73 20 69 6e 64 65 78 65 64 20 columns indexed
21e2d 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 */. }. if(
21e2e 20 70 53 72 63 2d 3e 61 53 6f 72 74 4f 72 64 65 pSrc->aSortOrde
21e2f 72 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61 53 6f r[i]!=pDest->aSo
21e30 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 rtOrder[i] ){.
21e31 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 return 0;
21e32 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 73 6f 72 /* Different sor
21e33 74 20 6f 72 64 65 72 73 20 2a 2f 0a 20 20 20 20 t orders */.
21e34 7d 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e }. if( pSrc->
21e35 61 7a 43 6f 6c 6c 5b 69 5d 21 3d 70 44 65 73 74 azColl[i]!=pDest
21e36 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20 29 7b 0a 20 ->azColl[i] ){.
21e37 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 return 0;
21e38 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f /* Different co
21e39 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
21e3a 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a s */. }. }..
21e3b 20 20 2f 2a 20 49 66 20 6e 6f 20 74 65 73 74 20 /* If no test
21e3c 61 62 6f 76 65 20 66 61 69 6c 73 20 74 68 65 6e above fails then
21e3d 20 74 68 65 20 69 6e 64 69 63 65 73 20 6d 75 73 the indices mus
21e3e 74 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 t be compatible
21e3f 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d */. return 1;.}
21e40 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 ../*.** Attempt
21e41 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74 the transfer opt
21e42 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e 53 imization on INS
21e43 45 52 54 73 20 6f 66 20 74 68 65 20 66 6f 72 6d ERTs of the form
21e44 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 .**.** INSER
21e45 54 20 49 4e 54 4f 20 74 61 62 31 20 53 45 4c 45 T INTO tab1 SELE
21e46 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 32 3b 0a CT * FROM tab2;.
21e47 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6d **.** This optim
21e48 69 7a 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 ization is only
21e49 61 74 74 65 6d 70 74 65 64 20 69 66 0a 2a 2a 0a attempted if.**.
21e4a 2a 2a 20 20 20 20 28 31 29 20 20 74 61 62 31 20 ** (1) tab1
21e4b 61 6e 64 20 74 61 62 32 20 68 61 76 65 20 69 64 and tab2 have id
21e4c 65 6e 74 69 63 61 6c 20 73 63 68 65 6d 61 73 20 entical schemas
21e4d 69 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 including all th
21e4e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 61 6d e.** sam
21e4f 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20 63 6f e indices and co
21e50 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 0a 2a 2a 20 nstraints.**.**
21e51 20 20 20 28 32 29 20 20 74 61 62 31 20 61 6e 64 (2) tab1 and
21e52 20 74 61 62 32 20 61 72 65 20 64 69 66 66 65 72 tab2 are differ
21e53 65 6e 74 20 74 61 62 6c 65 73 0a 2a 2a 0a 2a 2a ent tables.**.**
21e54 20 20 20 20 28 33 29 20 20 54 68 65 72 65 20 6d (3) There m
21e55 75 73 74 20 62 65 20 6e 6f 20 74 72 69 67 67 65 ust be no trigge
21e56 72 73 20 6f 6e 20 74 61 62 31 0a 2a 2a 0a 2a 2a rs on tab1.**.**
21e57 20 20 20 20 28 34 29 20 20 54 68 65 20 72 65 73 (4) The res
21e58 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 ult set of the S
21e59 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
21e5a 69 73 20 22 2a 22 0a 2a 2a 0a 2a 2a 20 20 20 20 is "*".**.**
21e5b 28 35 29 20 20 54 68 65 20 53 45 4c 45 43 54 20 (5) The SELECT
21e5c 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 6f statement has no
21e5d 20 57 48 45 52 45 2c 20 48 41 56 49 4e 47 2c 20 WHERE, HAVING,
21e5e 4f 52 44 45 52 20 42 59 2c 20 47 52 4f 55 50 20 ORDER BY, GROUP
21e5f 42 59 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f BY,.** o
21e60 72 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a r LIMIT clause..
21e61 2a 2a 0a 2a 2a 20 20 20 20 28 36 29 20 20 54 68 **.** (6) Th
21e62 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
21e63 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 65 20 28 nt is a simple (
21e64 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 29 20 not a compound)
21e65 73 65 6c 65 63 74 20 74 68 61 74 0a 2a 2a 20 20 select that.**
21e66 20 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 contains
21e67 6f 6e 6c 79 20 74 61 62 32 20 69 6e 20 69 74 73 only tab2 in its
21e68 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 0a FROM clause.**.
21e69 2a 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20 66 ** This method f
21e6a 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 or implementing
21e6b 74 68 65 20 49 4e 53 45 52 54 20 74 72 61 6e 73 the INSERT trans
21e6c 66 65 72 73 20 72 61 77 20 72 65 63 6f 72 64 73 fers raw records
21e6d 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 32 20 6f 76 from.** tab2 ov
21e6e 65 72 20 74 6f 20 74 61 62 31 2e 20 20 54 68 65 er to tab1. The
21e6f 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 columns are not
21e70 20 64 65 63 6f 64 65 64 2e 20 20 52 61 77 20 72 decoded. Raw r
21e71 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74 ecords from.** t
21e72 68 65 20 69 6e 64 69 63 65 73 20 6f 66 20 74 61 he indices of ta
21e73 62 32 20 61 72 65 20 74 72 61 6e 73 66 65 72 65 b2 are transfere
21e74 64 20 74 6f 20 74 61 62 31 20 61 73 20 77 65 6c d to tab1 as wel
21e75 6c 2e 20 20 49 6e 20 73 6f 20 64 6f 69 6e 67 2c l. In so doing,
21e76 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e .** the resultin
21e77 67 20 74 61 62 31 20 68 61 73 20 6d 75 63 68 20 g tab1 has much
21e78 6c 65 73 73 20 66 72 61 67 6d 65 6e 74 61 74 69 less fragmentati
21e79 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 on..**.** This r
21e7a 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 54 outine returns T
21e7b 52 55 45 20 69 66 20 74 68 65 20 6f 70 74 69 6d RUE if the optim
21e7c 69 7a 61 74 69 6f 6e 20 69 73 20 61 74 74 65 6d ization is attem
21e7d 70 74 65 64 2e 20 20 49 66 20 61 6e 79 0a 2a 2a pted. If any.**
21e7e 20 6f 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f of the conditio
21e7f 6e 73 20 61 62 6f 76 65 20 66 61 69 6c 20 73 6f ns above fail so
21e80 20 74 68 61 74 20 74 68 65 20 6f 70 74 69 6d 69 that the optimi
21e81 7a 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f zation should no
21e82 74 0a 2a 2a 20 62 65 20 61 74 74 65 6d 70 74 65 t.** be attempte
21e83 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 d, then this rou
21e84 74 69 6e 65 20 72 65 74 75 72 6